2
0

canvas.js 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.CanvasGraphics = undefined;
  20. var _util = require('../shared/util');
  21. var _pattern_helper = require('./pattern_helper');
  22. var _webgl = require('./webgl');
  23. var MIN_FONT_SIZE = 16;
  24. var MAX_FONT_SIZE = 100;
  25. var MAX_GROUP_SIZE = 4096;
  26. var MIN_WIDTH_FACTOR = 0.65;
  27. var COMPILE_TYPE3_GLYPHS = true;
  28. var MAX_SIZE_TO_COMPILE = 1000;
  29. var FULL_CHUNK_HEIGHT = 16;
  30. var IsLittleEndianCached = {
  31. get value() {
  32. return (0, _util.shadow)(IsLittleEndianCached, 'value', (0, _util.isLittleEndian)());
  33. }
  34. };
  35. function addContextCurrentTransform(ctx) {
  36. if (!ctx.mozCurrentTransform) {
  37. ctx._originalSave = ctx.save;
  38. ctx._originalRestore = ctx.restore;
  39. ctx._originalRotate = ctx.rotate;
  40. ctx._originalScale = ctx.scale;
  41. ctx._originalTranslate = ctx.translate;
  42. ctx._originalTransform = ctx.transform;
  43. ctx._originalSetTransform = ctx.setTransform;
  44. ctx._transformMatrix = ctx._transformMatrix || [1, 0, 0, 1, 0, 0];
  45. ctx._transformStack = [];
  46. Object.defineProperty(ctx, 'mozCurrentTransform', {
  47. get: function getCurrentTransform() {
  48. return this._transformMatrix;
  49. }
  50. });
  51. Object.defineProperty(ctx, 'mozCurrentTransformInverse', {
  52. get: function getCurrentTransformInverse() {
  53. var m = this._transformMatrix;
  54. var a = m[0],
  55. b = m[1],
  56. c = m[2],
  57. d = m[3],
  58. e = m[4],
  59. f = m[5];
  60. var ad_bc = a * d - b * c;
  61. var bc_ad = b * c - a * d;
  62. return [d / ad_bc, b / bc_ad, c / bc_ad, a / ad_bc, (d * e - c * f) / bc_ad, (b * e - a * f) / ad_bc];
  63. }
  64. });
  65. ctx.save = function ctxSave() {
  66. var old = this._transformMatrix;
  67. this._transformStack.push(old);
  68. this._transformMatrix = old.slice(0, 6);
  69. this._originalSave();
  70. };
  71. ctx.restore = function ctxRestore() {
  72. var prev = this._transformStack.pop();
  73. if (prev) {
  74. this._transformMatrix = prev;
  75. this._originalRestore();
  76. }
  77. };
  78. ctx.translate = function ctxTranslate(x, y) {
  79. var m = this._transformMatrix;
  80. m[4] = m[0] * x + m[2] * y + m[4];
  81. m[5] = m[1] * x + m[3] * y + m[5];
  82. this._originalTranslate(x, y);
  83. };
  84. ctx.scale = function ctxScale(x, y) {
  85. var m = this._transformMatrix;
  86. m[0] = m[0] * x;
  87. m[1] = m[1] * x;
  88. m[2] = m[2] * y;
  89. m[3] = m[3] * y;
  90. this._originalScale(x, y);
  91. };
  92. ctx.transform = function ctxTransform(a, b, c, d, e, f) {
  93. var m = this._transformMatrix;
  94. this._transformMatrix = [m[0] * a + m[2] * b, m[1] * a + m[3] * b, m[0] * c + m[2] * d, m[1] * c + m[3] * d, m[0] * e + m[2] * f + m[4], m[1] * e + m[3] * f + m[5]];
  95. ctx._originalTransform(a, b, c, d, e, f);
  96. };
  97. ctx.setTransform = function ctxSetTransform(a, b, c, d, e, f) {
  98. this._transformMatrix = [a, b, c, d, e, f];
  99. ctx._originalSetTransform(a, b, c, d, e, f);
  100. };
  101. ctx.rotate = function ctxRotate(angle) {
  102. var cosValue = Math.cos(angle);
  103. var sinValue = Math.sin(angle);
  104. var m = this._transformMatrix;
  105. this._transformMatrix = [m[0] * cosValue + m[2] * sinValue, m[1] * cosValue + m[3] * sinValue, m[0] * -sinValue + m[2] * cosValue, m[1] * -sinValue + m[3] * cosValue, m[4], m[5]];
  106. this._originalRotate(angle);
  107. };
  108. }
  109. }
  110. var CachedCanvases = function CachedCanvasesClosure() {
  111. function CachedCanvases(canvasFactory) {
  112. this.canvasFactory = canvasFactory;
  113. this.cache = Object.create(null);
  114. }
  115. CachedCanvases.prototype = {
  116. getCanvas: function CachedCanvases_getCanvas(id, width, height, trackTransform) {
  117. var canvasEntry;
  118. if (this.cache[id] !== undefined) {
  119. canvasEntry = this.cache[id];
  120. this.canvasFactory.reset(canvasEntry, width, height);
  121. canvasEntry.context.setTransform(1, 0, 0, 1, 0, 0);
  122. } else {
  123. canvasEntry = this.canvasFactory.create(width, height);
  124. this.cache[id] = canvasEntry;
  125. }
  126. if (trackTransform) {
  127. addContextCurrentTransform(canvasEntry.context);
  128. }
  129. return canvasEntry;
  130. },
  131. clear: function clear() {
  132. for (var id in this.cache) {
  133. var canvasEntry = this.cache[id];
  134. this.canvasFactory.destroy(canvasEntry);
  135. delete this.cache[id];
  136. }
  137. }
  138. };
  139. return CachedCanvases;
  140. }();
  141. function compileType3Glyph(imgData) {
  142. var POINT_TO_PROCESS_LIMIT = 1000;
  143. var width = imgData.width,
  144. height = imgData.height;
  145. var i,
  146. j,
  147. j0,
  148. width1 = width + 1;
  149. var points = new Uint8Array(width1 * (height + 1));
  150. var POINT_TYPES = new Uint8Array([0, 2, 4, 0, 1, 0, 5, 4, 8, 10, 0, 8, 0, 2, 1, 0]);
  151. var lineSize = width + 7 & ~7,
  152. data0 = imgData.data;
  153. var data = new Uint8Array(lineSize * height),
  154. pos = 0,
  155. ii;
  156. for (i = 0, ii = data0.length; i < ii; i++) {
  157. var mask = 128,
  158. elem = data0[i];
  159. while (mask > 0) {
  160. data[pos++] = elem & mask ? 0 : 255;
  161. mask >>= 1;
  162. }
  163. }
  164. var count = 0;
  165. pos = 0;
  166. if (data[pos] !== 0) {
  167. points[0] = 1;
  168. ++count;
  169. }
  170. for (j = 1; j < width; j++) {
  171. if (data[pos] !== data[pos + 1]) {
  172. points[j] = data[pos] ? 2 : 1;
  173. ++count;
  174. }
  175. pos++;
  176. }
  177. if (data[pos] !== 0) {
  178. points[j] = 2;
  179. ++count;
  180. }
  181. for (i = 1; i < height; i++) {
  182. pos = i * lineSize;
  183. j0 = i * width1;
  184. if (data[pos - lineSize] !== data[pos]) {
  185. points[j0] = data[pos] ? 1 : 8;
  186. ++count;
  187. }
  188. var sum = (data[pos] ? 4 : 0) + (data[pos - lineSize] ? 8 : 0);
  189. for (j = 1; j < width; j++) {
  190. sum = (sum >> 2) + (data[pos + 1] ? 4 : 0) + (data[pos - lineSize + 1] ? 8 : 0);
  191. if (POINT_TYPES[sum]) {
  192. points[j0 + j] = POINT_TYPES[sum];
  193. ++count;
  194. }
  195. pos++;
  196. }
  197. if (data[pos - lineSize] !== data[pos]) {
  198. points[j0 + j] = data[pos] ? 2 : 4;
  199. ++count;
  200. }
  201. if (count > POINT_TO_PROCESS_LIMIT) {
  202. return null;
  203. }
  204. }
  205. pos = lineSize * (height - 1);
  206. j0 = i * width1;
  207. if (data[pos] !== 0) {
  208. points[j0] = 8;
  209. ++count;
  210. }
  211. for (j = 1; j < width; j++) {
  212. if (data[pos] !== data[pos + 1]) {
  213. points[j0 + j] = data[pos] ? 4 : 8;
  214. ++count;
  215. }
  216. pos++;
  217. }
  218. if (data[pos] !== 0) {
  219. points[j0 + j] = 4;
  220. ++count;
  221. }
  222. if (count > POINT_TO_PROCESS_LIMIT) {
  223. return null;
  224. }
  225. var steps = new Int32Array([0, width1, -1, 0, -width1, 0, 0, 0, 1]);
  226. var outlines = [];
  227. for (i = 0; count && i <= height; i++) {
  228. var p = i * width1;
  229. var end = p + width;
  230. while (p < end && !points[p]) {
  231. p++;
  232. }
  233. if (p === end) {
  234. continue;
  235. }
  236. var coords = [p % width1, i];
  237. var type = points[p],
  238. p0 = p,
  239. pp;
  240. do {
  241. var step = steps[type];
  242. do {
  243. p += step;
  244. } while (!points[p]);
  245. pp = points[p];
  246. if (pp !== 5 && pp !== 10) {
  247. type = pp;
  248. points[p] = 0;
  249. } else {
  250. type = pp & 0x33 * type >> 4;
  251. points[p] &= type >> 2 | type << 2;
  252. }
  253. coords.push(p % width1);
  254. coords.push(p / width1 | 0);
  255. --count;
  256. } while (p0 !== p);
  257. outlines.push(coords);
  258. --i;
  259. }
  260. var drawOutline = function drawOutline(c) {
  261. c.save();
  262. c.scale(1 / width, -1 / height);
  263. c.translate(0, -height);
  264. c.beginPath();
  265. for (var i = 0, ii = outlines.length; i < ii; i++) {
  266. var o = outlines[i];
  267. c.moveTo(o[0], o[1]);
  268. for (var j = 2, jj = o.length; j < jj; j += 2) {
  269. c.lineTo(o[j], o[j + 1]);
  270. }
  271. }
  272. c.fill();
  273. c.beginPath();
  274. c.restore();
  275. };
  276. return drawOutline;
  277. }
  278. var CanvasExtraState = function CanvasExtraStateClosure() {
  279. function CanvasExtraState(old) {
  280. this.alphaIsShape = false;
  281. this.fontSize = 0;
  282. this.fontSizeScale = 1;
  283. this.textMatrix = _util.IDENTITY_MATRIX;
  284. this.textMatrixScale = 1;
  285. this.fontMatrix = _util.FONT_IDENTITY_MATRIX;
  286. this.leading = 0;
  287. this.x = 0;
  288. this.y = 0;
  289. this.lineX = 0;
  290. this.lineY = 0;
  291. this.charSpacing = 0;
  292. this.wordSpacing = 0;
  293. this.textHScale = 1;
  294. this.textRenderingMode = _util.TextRenderingMode.FILL;
  295. this.textRise = 0;
  296. this.fillColor = '#000000';
  297. this.strokeColor = '#000000';
  298. this.patternFill = false;
  299. this.fillAlpha = 1;
  300. this.strokeAlpha = 1;
  301. this.lineWidth = 1;
  302. this.activeSMask = null;
  303. this.resumeSMaskCtx = null;
  304. this.old = old;
  305. }
  306. CanvasExtraState.prototype = {
  307. clone: function CanvasExtraState_clone() {
  308. return Object.create(this);
  309. },
  310. setCurrentPoint: function CanvasExtraState_setCurrentPoint(x, y) {
  311. this.x = x;
  312. this.y = y;
  313. }
  314. };
  315. return CanvasExtraState;
  316. }();
  317. var CanvasGraphics = function CanvasGraphicsClosure() {
  318. var EXECUTION_TIME = 15;
  319. var EXECUTION_STEPS = 10;
  320. function CanvasGraphics(canvasCtx, commonObjs, objs, canvasFactory, imageLayer) {
  321. this.ctx = canvasCtx;
  322. this.current = new CanvasExtraState();
  323. this.stateStack = [];
  324. this.pendingClip = null;
  325. this.pendingEOFill = false;
  326. this.res = null;
  327. this.xobjs = null;
  328. this.commonObjs = commonObjs;
  329. this.objs = objs;
  330. this.canvasFactory = canvasFactory;
  331. this.imageLayer = imageLayer;
  332. this.groupStack = [];
  333. this.processingType3 = null;
  334. this.baseTransform = null;
  335. this.baseTransformStack = [];
  336. this.groupLevel = 0;
  337. this.smaskStack = [];
  338. this.smaskCounter = 0;
  339. this.tempSMask = null;
  340. this.cachedCanvases = new CachedCanvases(this.canvasFactory);
  341. if (canvasCtx) {
  342. addContextCurrentTransform(canvasCtx);
  343. }
  344. this.cachedGetSinglePixelWidth = null;
  345. }
  346. function putBinaryImageData(ctx, imgData) {
  347. if (typeof ImageData !== 'undefined' && imgData instanceof ImageData) {
  348. ctx.putImageData(imgData, 0, 0);
  349. return;
  350. }
  351. var height = imgData.height,
  352. width = imgData.width;
  353. var partialChunkHeight = height % FULL_CHUNK_HEIGHT;
  354. var fullChunks = (height - partialChunkHeight) / FULL_CHUNK_HEIGHT;
  355. var totalChunks = partialChunkHeight === 0 ? fullChunks : fullChunks + 1;
  356. var chunkImgData = ctx.createImageData(width, FULL_CHUNK_HEIGHT);
  357. var srcPos = 0,
  358. destPos;
  359. var src = imgData.data;
  360. var dest = chunkImgData.data;
  361. var i, j, thisChunkHeight, elemsInThisChunk;
  362. if (imgData.kind === _util.ImageKind.GRAYSCALE_1BPP) {
  363. var srcLength = src.byteLength;
  364. var dest32 = new Uint32Array(dest.buffer, 0, dest.byteLength >> 2);
  365. var dest32DataLength = dest32.length;
  366. var fullSrcDiff = width + 7 >> 3;
  367. var white = 0xFFFFFFFF;
  368. var black = IsLittleEndianCached.value ? 0xFF000000 : 0x000000FF;
  369. for (i = 0; i < totalChunks; i++) {
  370. thisChunkHeight = i < fullChunks ? FULL_CHUNK_HEIGHT : partialChunkHeight;
  371. destPos = 0;
  372. for (j = 0; j < thisChunkHeight; j++) {
  373. var srcDiff = srcLength - srcPos;
  374. var k = 0;
  375. var kEnd = srcDiff > fullSrcDiff ? width : srcDiff * 8 - 7;
  376. var kEndUnrolled = kEnd & ~7;
  377. var mask = 0;
  378. var srcByte = 0;
  379. for (; k < kEndUnrolled; k += 8) {
  380. srcByte = src[srcPos++];
  381. dest32[destPos++] = srcByte & 128 ? white : black;
  382. dest32[destPos++] = srcByte & 64 ? white : black;
  383. dest32[destPos++] = srcByte & 32 ? white : black;
  384. dest32[destPos++] = srcByte & 16 ? white : black;
  385. dest32[destPos++] = srcByte & 8 ? white : black;
  386. dest32[destPos++] = srcByte & 4 ? white : black;
  387. dest32[destPos++] = srcByte & 2 ? white : black;
  388. dest32[destPos++] = srcByte & 1 ? white : black;
  389. }
  390. for (; k < kEnd; k++) {
  391. if (mask === 0) {
  392. srcByte = src[srcPos++];
  393. mask = 128;
  394. }
  395. dest32[destPos++] = srcByte & mask ? white : black;
  396. mask >>= 1;
  397. }
  398. }
  399. while (destPos < dest32DataLength) {
  400. dest32[destPos++] = 0;
  401. }
  402. ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
  403. }
  404. } else if (imgData.kind === _util.ImageKind.RGBA_32BPP) {
  405. j = 0;
  406. elemsInThisChunk = width * FULL_CHUNK_HEIGHT * 4;
  407. for (i = 0; i < fullChunks; i++) {
  408. dest.set(src.subarray(srcPos, srcPos + elemsInThisChunk));
  409. srcPos += elemsInThisChunk;
  410. ctx.putImageData(chunkImgData, 0, j);
  411. j += FULL_CHUNK_HEIGHT;
  412. }
  413. if (i < totalChunks) {
  414. elemsInThisChunk = width * partialChunkHeight * 4;
  415. dest.set(src.subarray(srcPos, srcPos + elemsInThisChunk));
  416. ctx.putImageData(chunkImgData, 0, j);
  417. }
  418. } else if (imgData.kind === _util.ImageKind.RGB_24BPP) {
  419. thisChunkHeight = FULL_CHUNK_HEIGHT;
  420. elemsInThisChunk = width * thisChunkHeight;
  421. for (i = 0; i < totalChunks; i++) {
  422. if (i >= fullChunks) {
  423. thisChunkHeight = partialChunkHeight;
  424. elemsInThisChunk = width * thisChunkHeight;
  425. }
  426. destPos = 0;
  427. for (j = elemsInThisChunk; j--;) {
  428. dest[destPos++] = src[srcPos++];
  429. dest[destPos++] = src[srcPos++];
  430. dest[destPos++] = src[srcPos++];
  431. dest[destPos++] = 255;
  432. }
  433. ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
  434. }
  435. } else {
  436. (0, _util.error)('bad image kind: ' + imgData.kind);
  437. }
  438. }
  439. function putBinaryImageMask(ctx, imgData) {
  440. var height = imgData.height,
  441. width = imgData.width;
  442. var partialChunkHeight = height % FULL_CHUNK_HEIGHT;
  443. var fullChunks = (height - partialChunkHeight) / FULL_CHUNK_HEIGHT;
  444. var totalChunks = partialChunkHeight === 0 ? fullChunks : fullChunks + 1;
  445. var chunkImgData = ctx.createImageData(width, FULL_CHUNK_HEIGHT);
  446. var srcPos = 0;
  447. var src = imgData.data;
  448. var dest = chunkImgData.data;
  449. for (var i = 0; i < totalChunks; i++) {
  450. var thisChunkHeight = i < fullChunks ? FULL_CHUNK_HEIGHT : partialChunkHeight;
  451. var destPos = 3;
  452. for (var j = 0; j < thisChunkHeight; j++) {
  453. var mask = 0;
  454. for (var k = 0; k < width; k++) {
  455. if (!mask) {
  456. var elem = src[srcPos++];
  457. mask = 128;
  458. }
  459. dest[destPos] = elem & mask ? 0 : 255;
  460. destPos += 4;
  461. mask >>= 1;
  462. }
  463. }
  464. ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
  465. }
  466. }
  467. function copyCtxState(sourceCtx, destCtx) {
  468. var properties = ['strokeStyle', 'fillStyle', 'fillRule', 'globalAlpha', 'lineWidth', 'lineCap', 'lineJoin', 'miterLimit', 'globalCompositeOperation', 'font'];
  469. for (var i = 0, ii = properties.length; i < ii; i++) {
  470. var property = properties[i];
  471. if (sourceCtx[property] !== undefined) {
  472. destCtx[property] = sourceCtx[property];
  473. }
  474. }
  475. if (sourceCtx.setLineDash !== undefined) {
  476. destCtx.setLineDash(sourceCtx.getLineDash());
  477. destCtx.lineDashOffset = sourceCtx.lineDashOffset;
  478. }
  479. }
  480. function composeSMaskBackdrop(bytes, r0, g0, b0) {
  481. var length = bytes.length;
  482. for (var i = 3; i < length; i += 4) {
  483. var alpha = bytes[i];
  484. if (alpha === 0) {
  485. bytes[i - 3] = r0;
  486. bytes[i - 2] = g0;
  487. bytes[i - 1] = b0;
  488. } else if (alpha < 255) {
  489. var alpha_ = 255 - alpha;
  490. bytes[i - 3] = bytes[i - 3] * alpha + r0 * alpha_ >> 8;
  491. bytes[i - 2] = bytes[i - 2] * alpha + g0 * alpha_ >> 8;
  492. bytes[i - 1] = bytes[i - 1] * alpha + b0 * alpha_ >> 8;
  493. }
  494. }
  495. }
  496. function composeSMaskAlpha(maskData, layerData, transferMap) {
  497. var length = maskData.length;
  498. var scale = 1 / 255;
  499. for (var i = 3; i < length; i += 4) {
  500. var alpha = transferMap ? transferMap[maskData[i]] : maskData[i];
  501. layerData[i] = layerData[i] * alpha * scale | 0;
  502. }
  503. }
  504. function composeSMaskLuminosity(maskData, layerData, transferMap) {
  505. var length = maskData.length;
  506. for (var i = 3; i < length; i += 4) {
  507. var y = maskData[i - 3] * 77 + maskData[i - 2] * 152 + maskData[i - 1] * 28;
  508. layerData[i] = transferMap ? layerData[i] * transferMap[y >> 8] >> 8 : layerData[i] * y >> 16;
  509. }
  510. }
  511. function genericComposeSMask(maskCtx, layerCtx, width, height, subtype, backdrop, transferMap) {
  512. var hasBackdrop = !!backdrop;
  513. var r0 = hasBackdrop ? backdrop[0] : 0;
  514. var g0 = hasBackdrop ? backdrop[1] : 0;
  515. var b0 = hasBackdrop ? backdrop[2] : 0;
  516. var composeFn;
  517. if (subtype === 'Luminosity') {
  518. composeFn = composeSMaskLuminosity;
  519. } else {
  520. composeFn = composeSMaskAlpha;
  521. }
  522. var PIXELS_TO_PROCESS = 1048576;
  523. var chunkSize = Math.min(height, Math.ceil(PIXELS_TO_PROCESS / width));
  524. for (var row = 0; row < height; row += chunkSize) {
  525. var chunkHeight = Math.min(chunkSize, height - row);
  526. var maskData = maskCtx.getImageData(0, row, width, chunkHeight);
  527. var layerData = layerCtx.getImageData(0, row, width, chunkHeight);
  528. if (hasBackdrop) {
  529. composeSMaskBackdrop(maskData.data, r0, g0, b0);
  530. }
  531. composeFn(maskData.data, layerData.data, transferMap);
  532. maskCtx.putImageData(layerData, 0, row);
  533. }
  534. }
  535. function composeSMask(ctx, smask, layerCtx) {
  536. var mask = smask.canvas;
  537. var maskCtx = smask.context;
  538. ctx.setTransform(smask.scaleX, 0, 0, smask.scaleY, smask.offsetX, smask.offsetY);
  539. var backdrop = smask.backdrop || null;
  540. if (!smask.transferMap && _webgl.WebGLUtils.isEnabled) {
  541. var composed = _webgl.WebGLUtils.composeSMask(layerCtx.canvas, mask, {
  542. subtype: smask.subtype,
  543. backdrop: backdrop
  544. });
  545. ctx.setTransform(1, 0, 0, 1, 0, 0);
  546. ctx.drawImage(composed, smask.offsetX, smask.offsetY);
  547. return;
  548. }
  549. genericComposeSMask(maskCtx, layerCtx, mask.width, mask.height, smask.subtype, backdrop, smask.transferMap);
  550. ctx.drawImage(mask, 0, 0);
  551. }
  552. var LINE_CAP_STYLES = ['butt', 'round', 'square'];
  553. var LINE_JOIN_STYLES = ['miter', 'round', 'bevel'];
  554. var NORMAL_CLIP = {};
  555. var EO_CLIP = {};
  556. CanvasGraphics.prototype = {
  557. beginDrawing: function beginDrawing(_ref) {
  558. var transform = _ref.transform,
  559. viewport = _ref.viewport,
  560. transparency = _ref.transparency,
  561. _ref$background = _ref.background,
  562. background = _ref$background === undefined ? null : _ref$background;
  563. var width = this.ctx.canvas.width;
  564. var height = this.ctx.canvas.height;
  565. this.ctx.save();
  566. this.ctx.fillStyle = background || 'rgb(255, 255, 255)';
  567. this.ctx.fillRect(0, 0, width, height);
  568. this.ctx.restore();
  569. if (transparency) {
  570. var transparentCanvas = this.cachedCanvases.getCanvas('transparent', width, height, true);
  571. this.compositeCtx = this.ctx;
  572. this.transparentCanvas = transparentCanvas.canvas;
  573. this.ctx = transparentCanvas.context;
  574. this.ctx.save();
  575. this.ctx.transform.apply(this.ctx, this.compositeCtx.mozCurrentTransform);
  576. }
  577. this.ctx.save();
  578. if (transform) {
  579. this.ctx.transform.apply(this.ctx, transform);
  580. }
  581. this.ctx.transform.apply(this.ctx, viewport.transform);
  582. this.baseTransform = this.ctx.mozCurrentTransform.slice();
  583. if (this.imageLayer) {
  584. this.imageLayer.beginLayout();
  585. }
  586. },
  587. executeOperatorList: function CanvasGraphics_executeOperatorList(operatorList, executionStartIdx, continueCallback, stepper) {
  588. var argsArray = operatorList.argsArray;
  589. var fnArray = operatorList.fnArray;
  590. var i = executionStartIdx || 0;
  591. var argsArrayLen = argsArray.length;
  592. if (argsArrayLen === i) {
  593. return i;
  594. }
  595. var chunkOperations = argsArrayLen - i > EXECUTION_STEPS && typeof continueCallback === 'function';
  596. var endTime = chunkOperations ? Date.now() + EXECUTION_TIME : 0;
  597. var steps = 0;
  598. var commonObjs = this.commonObjs;
  599. var objs = this.objs;
  600. var fnId;
  601. while (true) {
  602. if (stepper !== undefined && i === stepper.nextBreakPoint) {
  603. stepper.breakIt(i, continueCallback);
  604. return i;
  605. }
  606. fnId = fnArray[i];
  607. if (fnId !== _util.OPS.dependency) {
  608. this[fnId].apply(this, argsArray[i]);
  609. } else {
  610. var deps = argsArray[i];
  611. for (var n = 0, nn = deps.length; n < nn; n++) {
  612. var depObjId = deps[n];
  613. var common = depObjId[0] === 'g' && depObjId[1] === '_';
  614. var objsPool = common ? commonObjs : objs;
  615. if (!objsPool.isResolved(depObjId)) {
  616. objsPool.get(depObjId, continueCallback);
  617. return i;
  618. }
  619. }
  620. }
  621. i++;
  622. if (i === argsArrayLen) {
  623. return i;
  624. }
  625. if (chunkOperations && ++steps > EXECUTION_STEPS) {
  626. if (Date.now() > endTime) {
  627. continueCallback();
  628. return i;
  629. }
  630. steps = 0;
  631. }
  632. }
  633. },
  634. endDrawing: function CanvasGraphics_endDrawing() {
  635. if (this.current.activeSMask !== null) {
  636. this.endSMaskGroup();
  637. }
  638. this.ctx.restore();
  639. if (this.transparentCanvas) {
  640. this.ctx = this.compositeCtx;
  641. this.ctx.save();
  642. this.ctx.setTransform(1, 0, 0, 1, 0, 0);
  643. this.ctx.drawImage(this.transparentCanvas, 0, 0);
  644. this.ctx.restore();
  645. this.transparentCanvas = null;
  646. }
  647. this.cachedCanvases.clear();
  648. _webgl.WebGLUtils.clear();
  649. if (this.imageLayer) {
  650. this.imageLayer.endLayout();
  651. }
  652. },
  653. setLineWidth: function CanvasGraphics_setLineWidth(width) {
  654. this.current.lineWidth = width;
  655. this.ctx.lineWidth = width;
  656. },
  657. setLineCap: function CanvasGraphics_setLineCap(style) {
  658. this.ctx.lineCap = LINE_CAP_STYLES[style];
  659. },
  660. setLineJoin: function CanvasGraphics_setLineJoin(style) {
  661. this.ctx.lineJoin = LINE_JOIN_STYLES[style];
  662. },
  663. setMiterLimit: function CanvasGraphics_setMiterLimit(limit) {
  664. this.ctx.miterLimit = limit;
  665. },
  666. setDash: function CanvasGraphics_setDash(dashArray, dashPhase) {
  667. var ctx = this.ctx;
  668. if (ctx.setLineDash !== undefined) {
  669. ctx.setLineDash(dashArray);
  670. ctx.lineDashOffset = dashPhase;
  671. }
  672. },
  673. setRenderingIntent: function CanvasGraphics_setRenderingIntent(intent) {},
  674. setFlatness: function CanvasGraphics_setFlatness(flatness) {},
  675. setGState: function CanvasGraphics_setGState(states) {
  676. for (var i = 0, ii = states.length; i < ii; i++) {
  677. var state = states[i];
  678. var key = state[0];
  679. var value = state[1];
  680. switch (key) {
  681. case 'LW':
  682. this.setLineWidth(value);
  683. break;
  684. case 'LC':
  685. this.setLineCap(value);
  686. break;
  687. case 'LJ':
  688. this.setLineJoin(value);
  689. break;
  690. case 'ML':
  691. this.setMiterLimit(value);
  692. break;
  693. case 'D':
  694. this.setDash(value[0], value[1]);
  695. break;
  696. case 'RI':
  697. this.setRenderingIntent(value);
  698. break;
  699. case 'FL':
  700. this.setFlatness(value);
  701. break;
  702. case 'Font':
  703. this.setFont(value[0], value[1]);
  704. break;
  705. case 'CA':
  706. this.current.strokeAlpha = state[1];
  707. break;
  708. case 'ca':
  709. this.current.fillAlpha = state[1];
  710. this.ctx.globalAlpha = state[1];
  711. break;
  712. case 'BM':
  713. this.ctx.globalCompositeOperation = value;
  714. break;
  715. case 'SMask':
  716. if (this.current.activeSMask) {
  717. if (this.stateStack.length > 0 && this.stateStack[this.stateStack.length - 1].activeSMask === this.current.activeSMask) {
  718. this.suspendSMaskGroup();
  719. } else {
  720. this.endSMaskGroup();
  721. }
  722. }
  723. this.current.activeSMask = value ? this.tempSMask : null;
  724. if (this.current.activeSMask) {
  725. this.beginSMaskGroup();
  726. }
  727. this.tempSMask = null;
  728. break;
  729. }
  730. }
  731. },
  732. beginSMaskGroup: function CanvasGraphics_beginSMaskGroup() {
  733. var activeSMask = this.current.activeSMask;
  734. var drawnWidth = activeSMask.canvas.width;
  735. var drawnHeight = activeSMask.canvas.height;
  736. var cacheId = 'smaskGroupAt' + this.groupLevel;
  737. var scratchCanvas = this.cachedCanvases.getCanvas(cacheId, drawnWidth, drawnHeight, true);
  738. var currentCtx = this.ctx;
  739. var currentTransform = currentCtx.mozCurrentTransform;
  740. this.ctx.save();
  741. var groupCtx = scratchCanvas.context;
  742. groupCtx.scale(1 / activeSMask.scaleX, 1 / activeSMask.scaleY);
  743. groupCtx.translate(-activeSMask.offsetX, -activeSMask.offsetY);
  744. groupCtx.transform.apply(groupCtx, currentTransform);
  745. activeSMask.startTransformInverse = groupCtx.mozCurrentTransformInverse;
  746. copyCtxState(currentCtx, groupCtx);
  747. this.ctx = groupCtx;
  748. this.setGState([['BM', 'source-over'], ['ca', 1], ['CA', 1]]);
  749. this.groupStack.push(currentCtx);
  750. this.groupLevel++;
  751. },
  752. suspendSMaskGroup: function CanvasGraphics_endSMaskGroup() {
  753. var groupCtx = this.ctx;
  754. this.groupLevel--;
  755. this.ctx = this.groupStack.pop();
  756. composeSMask(this.ctx, this.current.activeSMask, groupCtx);
  757. this.ctx.restore();
  758. this.ctx.save();
  759. copyCtxState(groupCtx, this.ctx);
  760. this.current.resumeSMaskCtx = groupCtx;
  761. var deltaTransform = _util.Util.transform(this.current.activeSMask.startTransformInverse, groupCtx.mozCurrentTransform);
  762. this.ctx.transform.apply(this.ctx, deltaTransform);
  763. groupCtx.save();
  764. groupCtx.setTransform(1, 0, 0, 1, 0, 0);
  765. groupCtx.clearRect(0, 0, groupCtx.canvas.width, groupCtx.canvas.height);
  766. groupCtx.restore();
  767. },
  768. resumeSMaskGroup: function CanvasGraphics_endSMaskGroup() {
  769. var groupCtx = this.current.resumeSMaskCtx;
  770. var currentCtx = this.ctx;
  771. this.ctx = groupCtx;
  772. this.groupStack.push(currentCtx);
  773. this.groupLevel++;
  774. },
  775. endSMaskGroup: function CanvasGraphics_endSMaskGroup() {
  776. var groupCtx = this.ctx;
  777. this.groupLevel--;
  778. this.ctx = this.groupStack.pop();
  779. composeSMask(this.ctx, this.current.activeSMask, groupCtx);
  780. this.ctx.restore();
  781. copyCtxState(groupCtx, this.ctx);
  782. var deltaTransform = _util.Util.transform(this.current.activeSMask.startTransformInverse, groupCtx.mozCurrentTransform);
  783. this.ctx.transform.apply(this.ctx, deltaTransform);
  784. },
  785. save: function CanvasGraphics_save() {
  786. this.ctx.save();
  787. var old = this.current;
  788. this.stateStack.push(old);
  789. this.current = old.clone();
  790. this.current.resumeSMaskCtx = null;
  791. },
  792. restore: function CanvasGraphics_restore() {
  793. if (this.current.resumeSMaskCtx) {
  794. this.resumeSMaskGroup();
  795. }
  796. if (this.current.activeSMask !== null && (this.stateStack.length === 0 || this.stateStack[this.stateStack.length - 1].activeSMask !== this.current.activeSMask)) {
  797. this.endSMaskGroup();
  798. }
  799. if (this.stateStack.length !== 0) {
  800. this.current = this.stateStack.pop();
  801. this.ctx.restore();
  802. this.pendingClip = null;
  803. this.cachedGetSinglePixelWidth = null;
  804. }
  805. },
  806. transform: function CanvasGraphics_transform(a, b, c, d, e, f) {
  807. this.ctx.transform(a, b, c, d, e, f);
  808. this.cachedGetSinglePixelWidth = null;
  809. },
  810. constructPath: function CanvasGraphics_constructPath(ops, args) {
  811. var ctx = this.ctx;
  812. var current = this.current;
  813. var x = current.x,
  814. y = current.y;
  815. for (var i = 0, j = 0, ii = ops.length; i < ii; i++) {
  816. switch (ops[i] | 0) {
  817. case _util.OPS.rectangle:
  818. x = args[j++];
  819. y = args[j++];
  820. var width = args[j++];
  821. var height = args[j++];
  822. if (width === 0) {
  823. width = this.getSinglePixelWidth();
  824. }
  825. if (height === 0) {
  826. height = this.getSinglePixelWidth();
  827. }
  828. var xw = x + width;
  829. var yh = y + height;
  830. this.ctx.moveTo(x, y);
  831. this.ctx.lineTo(xw, y);
  832. this.ctx.lineTo(xw, yh);
  833. this.ctx.lineTo(x, yh);
  834. this.ctx.lineTo(x, y);
  835. this.ctx.closePath();
  836. break;
  837. case _util.OPS.moveTo:
  838. x = args[j++];
  839. y = args[j++];
  840. ctx.moveTo(x, y);
  841. break;
  842. case _util.OPS.lineTo:
  843. x = args[j++];
  844. y = args[j++];
  845. ctx.lineTo(x, y);
  846. break;
  847. case _util.OPS.curveTo:
  848. x = args[j + 4];
  849. y = args[j + 5];
  850. ctx.bezierCurveTo(args[j], args[j + 1], args[j + 2], args[j + 3], x, y);
  851. j += 6;
  852. break;
  853. case _util.OPS.curveTo2:
  854. ctx.bezierCurveTo(x, y, args[j], args[j + 1], args[j + 2], args[j + 3]);
  855. x = args[j + 2];
  856. y = args[j + 3];
  857. j += 4;
  858. break;
  859. case _util.OPS.curveTo3:
  860. x = args[j + 2];
  861. y = args[j + 3];
  862. ctx.bezierCurveTo(args[j], args[j + 1], x, y, x, y);
  863. j += 4;
  864. break;
  865. case _util.OPS.closePath:
  866. ctx.closePath();
  867. break;
  868. }
  869. }
  870. current.setCurrentPoint(x, y);
  871. },
  872. closePath: function CanvasGraphics_closePath() {
  873. this.ctx.closePath();
  874. },
  875. stroke: function CanvasGraphics_stroke(consumePath) {
  876. consumePath = typeof consumePath !== 'undefined' ? consumePath : true;
  877. var ctx = this.ctx;
  878. var strokeColor = this.current.strokeColor;
  879. ctx.lineWidth = Math.max(this.getSinglePixelWidth() * MIN_WIDTH_FACTOR, this.current.lineWidth);
  880. ctx.globalAlpha = this.current.strokeAlpha;
  881. if (strokeColor && strokeColor.hasOwnProperty('type') && strokeColor.type === 'Pattern') {
  882. ctx.save();
  883. ctx.strokeStyle = strokeColor.getPattern(ctx, this);
  884. ctx.stroke();
  885. ctx.restore();
  886. } else {
  887. ctx.stroke();
  888. }
  889. if (consumePath) {
  890. this.consumePath();
  891. }
  892. ctx.globalAlpha = this.current.fillAlpha;
  893. },
  894. closeStroke: function CanvasGraphics_closeStroke() {
  895. this.closePath();
  896. this.stroke();
  897. },
  898. fill: function CanvasGraphics_fill(consumePath) {
  899. consumePath = typeof consumePath !== 'undefined' ? consumePath : true;
  900. var ctx = this.ctx;
  901. var fillColor = this.current.fillColor;
  902. var isPatternFill = this.current.patternFill;
  903. var needRestore = false;
  904. if (isPatternFill) {
  905. ctx.save();
  906. if (this.baseTransform) {
  907. ctx.setTransform.apply(ctx, this.baseTransform);
  908. }
  909. ctx.fillStyle = fillColor.getPattern(ctx, this);
  910. needRestore = true;
  911. }
  912. if (this.pendingEOFill) {
  913. ctx.fill('evenodd');
  914. this.pendingEOFill = false;
  915. } else {
  916. ctx.fill();
  917. }
  918. if (needRestore) {
  919. ctx.restore();
  920. }
  921. if (consumePath) {
  922. this.consumePath();
  923. }
  924. },
  925. eoFill: function CanvasGraphics_eoFill() {
  926. this.pendingEOFill = true;
  927. this.fill();
  928. },
  929. fillStroke: function CanvasGraphics_fillStroke() {
  930. this.fill(false);
  931. this.stroke(false);
  932. this.consumePath();
  933. },
  934. eoFillStroke: function CanvasGraphics_eoFillStroke() {
  935. this.pendingEOFill = true;
  936. this.fillStroke();
  937. },
  938. closeFillStroke: function CanvasGraphics_closeFillStroke() {
  939. this.closePath();
  940. this.fillStroke();
  941. },
  942. closeEOFillStroke: function CanvasGraphics_closeEOFillStroke() {
  943. this.pendingEOFill = true;
  944. this.closePath();
  945. this.fillStroke();
  946. },
  947. endPath: function CanvasGraphics_endPath() {
  948. this.consumePath();
  949. },
  950. clip: function CanvasGraphics_clip() {
  951. this.pendingClip = NORMAL_CLIP;
  952. },
  953. eoClip: function CanvasGraphics_eoClip() {
  954. this.pendingClip = EO_CLIP;
  955. },
  956. beginText: function CanvasGraphics_beginText() {
  957. this.current.textMatrix = _util.IDENTITY_MATRIX;
  958. this.current.textMatrixScale = 1;
  959. this.current.x = this.current.lineX = 0;
  960. this.current.y = this.current.lineY = 0;
  961. },
  962. endText: function CanvasGraphics_endText() {
  963. var paths = this.pendingTextPaths;
  964. var ctx = this.ctx;
  965. if (paths === undefined) {
  966. ctx.beginPath();
  967. return;
  968. }
  969. ctx.save();
  970. ctx.beginPath();
  971. for (var i = 0; i < paths.length; i++) {
  972. var path = paths[i];
  973. ctx.setTransform.apply(ctx, path.transform);
  974. ctx.translate(path.x, path.y);
  975. path.addToPath(ctx, path.fontSize);
  976. }
  977. ctx.restore();
  978. ctx.clip();
  979. ctx.beginPath();
  980. delete this.pendingTextPaths;
  981. },
  982. setCharSpacing: function CanvasGraphics_setCharSpacing(spacing) {
  983. this.current.charSpacing = spacing;
  984. },
  985. setWordSpacing: function CanvasGraphics_setWordSpacing(spacing) {
  986. this.current.wordSpacing = spacing;
  987. },
  988. setHScale: function CanvasGraphics_setHScale(scale) {
  989. this.current.textHScale = scale / 100;
  990. },
  991. setLeading: function CanvasGraphics_setLeading(leading) {
  992. this.current.leading = -leading;
  993. },
  994. setFont: function CanvasGraphics_setFont(fontRefName, size) {
  995. var fontObj = this.commonObjs.get(fontRefName);
  996. var current = this.current;
  997. if (!fontObj) {
  998. (0, _util.error)('Can\'t find font for ' + fontRefName);
  999. }
  1000. current.fontMatrix = fontObj.fontMatrix ? fontObj.fontMatrix : _util.FONT_IDENTITY_MATRIX;
  1001. if (current.fontMatrix[0] === 0 || current.fontMatrix[3] === 0) {
  1002. (0, _util.warn)('Invalid font matrix for font ' + fontRefName);
  1003. }
  1004. if (size < 0) {
  1005. size = -size;
  1006. current.fontDirection = -1;
  1007. } else {
  1008. current.fontDirection = 1;
  1009. }
  1010. this.current.font = fontObj;
  1011. this.current.fontSize = size;
  1012. if (fontObj.isType3Font) {
  1013. return;
  1014. }
  1015. var name = fontObj.loadedName || 'sans-serif';
  1016. var bold = fontObj.black ? '900' : fontObj.bold ? 'bold' : 'normal';
  1017. var italic = fontObj.italic ? 'italic' : 'normal';
  1018. var typeface = '"' + name + '", ' + fontObj.fallbackName;
  1019. var browserFontSize = size < MIN_FONT_SIZE ? MIN_FONT_SIZE : size > MAX_FONT_SIZE ? MAX_FONT_SIZE : size;
  1020. this.current.fontSizeScale = size / browserFontSize;
  1021. var rule = italic + ' ' + bold + ' ' + browserFontSize + 'px ' + typeface;
  1022. this.ctx.font = rule;
  1023. },
  1024. setTextRenderingMode: function CanvasGraphics_setTextRenderingMode(mode) {
  1025. this.current.textRenderingMode = mode;
  1026. },
  1027. setTextRise: function CanvasGraphics_setTextRise(rise) {
  1028. this.current.textRise = rise;
  1029. },
  1030. moveText: function CanvasGraphics_moveText(x, y) {
  1031. this.current.x = this.current.lineX += x;
  1032. this.current.y = this.current.lineY += y;
  1033. },
  1034. setLeadingMoveText: function CanvasGraphics_setLeadingMoveText(x, y) {
  1035. this.setLeading(-y);
  1036. this.moveText(x, y);
  1037. },
  1038. setTextMatrix: function CanvasGraphics_setTextMatrix(a, b, c, d, e, f) {
  1039. this.current.textMatrix = [a, b, c, d, e, f];
  1040. this.current.textMatrixScale = Math.sqrt(a * a + b * b);
  1041. this.current.x = this.current.lineX = 0;
  1042. this.current.y = this.current.lineY = 0;
  1043. },
  1044. nextLine: function CanvasGraphics_nextLine() {
  1045. this.moveText(0, this.current.leading);
  1046. },
  1047. paintChar: function CanvasGraphics_paintChar(character, x, y) {
  1048. var ctx = this.ctx;
  1049. var current = this.current;
  1050. var font = current.font;
  1051. var textRenderingMode = current.textRenderingMode;
  1052. var fontSize = current.fontSize / current.fontSizeScale;
  1053. var fillStrokeMode = textRenderingMode & _util.TextRenderingMode.FILL_STROKE_MASK;
  1054. var isAddToPathSet = !!(textRenderingMode & _util.TextRenderingMode.ADD_TO_PATH_FLAG);
  1055. var addToPath;
  1056. if (font.disableFontFace || isAddToPathSet) {
  1057. addToPath = font.getPathGenerator(this.commonObjs, character);
  1058. }
  1059. if (font.disableFontFace) {
  1060. ctx.save();
  1061. ctx.translate(x, y);
  1062. ctx.beginPath();
  1063. addToPath(ctx, fontSize);
  1064. if (fillStrokeMode === _util.TextRenderingMode.FILL || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
  1065. ctx.fill();
  1066. }
  1067. if (fillStrokeMode === _util.TextRenderingMode.STROKE || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
  1068. ctx.stroke();
  1069. }
  1070. ctx.restore();
  1071. } else {
  1072. if (fillStrokeMode === _util.TextRenderingMode.FILL || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
  1073. ctx.fillText(character, x, y);
  1074. }
  1075. if (fillStrokeMode === _util.TextRenderingMode.STROKE || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
  1076. ctx.strokeText(character, x, y);
  1077. }
  1078. }
  1079. if (isAddToPathSet) {
  1080. var paths = this.pendingTextPaths || (this.pendingTextPaths = []);
  1081. paths.push({
  1082. transform: ctx.mozCurrentTransform,
  1083. x: x,
  1084. y: y,
  1085. fontSize: fontSize,
  1086. addToPath: addToPath
  1087. });
  1088. }
  1089. },
  1090. get isFontSubpixelAAEnabled() {
  1091. var ctx = this.canvasFactory.create(10, 10).context;
  1092. ctx.scale(1.5, 1);
  1093. ctx.fillText('I', 0, 10);
  1094. var data = ctx.getImageData(0, 0, 10, 10).data;
  1095. var enabled = false;
  1096. for (var i = 3; i < data.length; i += 4) {
  1097. if (data[i] > 0 && data[i] < 255) {
  1098. enabled = true;
  1099. break;
  1100. }
  1101. }
  1102. return (0, _util.shadow)(this, 'isFontSubpixelAAEnabled', enabled);
  1103. },
  1104. showText: function CanvasGraphics_showText(glyphs) {
  1105. var current = this.current;
  1106. var font = current.font;
  1107. if (font.isType3Font) {
  1108. return this.showType3Text(glyphs);
  1109. }
  1110. var fontSize = current.fontSize;
  1111. if (fontSize === 0) {
  1112. return;
  1113. }
  1114. var ctx = this.ctx;
  1115. var fontSizeScale = current.fontSizeScale;
  1116. var charSpacing = current.charSpacing;
  1117. var wordSpacing = current.wordSpacing;
  1118. var fontDirection = current.fontDirection;
  1119. var textHScale = current.textHScale * fontDirection;
  1120. var glyphsLength = glyphs.length;
  1121. var vertical = font.vertical;
  1122. var spacingDir = vertical ? 1 : -1;
  1123. var defaultVMetrics = font.defaultVMetrics;
  1124. var widthAdvanceScale = fontSize * current.fontMatrix[0];
  1125. var simpleFillText = current.textRenderingMode === _util.TextRenderingMode.FILL && !font.disableFontFace;
  1126. ctx.save();
  1127. ctx.transform.apply(ctx, current.textMatrix);
  1128. ctx.translate(current.x, current.y + current.textRise);
  1129. if (current.patternFill) {
  1130. ctx.fillStyle = current.fillColor.getPattern(ctx, this);
  1131. }
  1132. if (fontDirection > 0) {
  1133. ctx.scale(textHScale, -1);
  1134. } else {
  1135. ctx.scale(textHScale, 1);
  1136. }
  1137. var lineWidth = current.lineWidth;
  1138. var scale = current.textMatrixScale;
  1139. if (scale === 0 || lineWidth === 0) {
  1140. var fillStrokeMode = current.textRenderingMode & _util.TextRenderingMode.FILL_STROKE_MASK;
  1141. if (fillStrokeMode === _util.TextRenderingMode.STROKE || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
  1142. this.cachedGetSinglePixelWidth = null;
  1143. lineWidth = this.getSinglePixelWidth() * MIN_WIDTH_FACTOR;
  1144. }
  1145. } else {
  1146. lineWidth /= scale;
  1147. }
  1148. if (fontSizeScale !== 1.0) {
  1149. ctx.scale(fontSizeScale, fontSizeScale);
  1150. lineWidth /= fontSizeScale;
  1151. }
  1152. ctx.lineWidth = lineWidth;
  1153. var x = 0,
  1154. i;
  1155. for (i = 0; i < glyphsLength; ++i) {
  1156. var glyph = glyphs[i];
  1157. if ((0, _util.isNum)(glyph)) {
  1158. x += spacingDir * glyph * fontSize / 1000;
  1159. continue;
  1160. }
  1161. var restoreNeeded = false;
  1162. var spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
  1163. var character = glyph.fontChar;
  1164. var accent = glyph.accent;
  1165. var scaledX, scaledY, scaledAccentX, scaledAccentY;
  1166. var width = glyph.width;
  1167. if (vertical) {
  1168. var vmetric, vx, vy;
  1169. vmetric = glyph.vmetric || defaultVMetrics;
  1170. vx = glyph.vmetric ? vmetric[1] : width * 0.5;
  1171. vx = -vx * widthAdvanceScale;
  1172. vy = vmetric[2] * widthAdvanceScale;
  1173. width = vmetric ? -vmetric[0] : width;
  1174. scaledX = vx / fontSizeScale;
  1175. scaledY = (x + vy) / fontSizeScale;
  1176. } else {
  1177. scaledX = x / fontSizeScale;
  1178. scaledY = 0;
  1179. }
  1180. if (font.remeasure && width > 0) {
  1181. var measuredWidth = ctx.measureText(character).width * 1000 / fontSize * fontSizeScale;
  1182. if (width < measuredWidth && this.isFontSubpixelAAEnabled) {
  1183. var characterScaleX = width / measuredWidth;
  1184. restoreNeeded = true;
  1185. ctx.save();
  1186. ctx.scale(characterScaleX, 1);
  1187. scaledX /= characterScaleX;
  1188. } else if (width !== measuredWidth) {
  1189. scaledX += (width - measuredWidth) / 2000 * fontSize / fontSizeScale;
  1190. }
  1191. }
  1192. if (glyph.isInFont || font.missingFile) {
  1193. if (simpleFillText && !accent) {
  1194. ctx.fillText(character, scaledX, scaledY);
  1195. } else {
  1196. this.paintChar(character, scaledX, scaledY);
  1197. if (accent) {
  1198. scaledAccentX = scaledX + accent.offset.x / fontSizeScale;
  1199. scaledAccentY = scaledY - accent.offset.y / fontSizeScale;
  1200. this.paintChar(accent.fontChar, scaledAccentX, scaledAccentY);
  1201. }
  1202. }
  1203. }
  1204. var charWidth = width * widthAdvanceScale + spacing * fontDirection;
  1205. x += charWidth;
  1206. if (restoreNeeded) {
  1207. ctx.restore();
  1208. }
  1209. }
  1210. if (vertical) {
  1211. current.y -= x * textHScale;
  1212. } else {
  1213. current.x += x * textHScale;
  1214. }
  1215. ctx.restore();
  1216. },
  1217. showType3Text: function CanvasGraphics_showType3Text(glyphs) {
  1218. var ctx = this.ctx;
  1219. var current = this.current;
  1220. var font = current.font;
  1221. var fontSize = current.fontSize;
  1222. var fontDirection = current.fontDirection;
  1223. var spacingDir = font.vertical ? 1 : -1;
  1224. var charSpacing = current.charSpacing;
  1225. var wordSpacing = current.wordSpacing;
  1226. var textHScale = current.textHScale * fontDirection;
  1227. var fontMatrix = current.fontMatrix || _util.FONT_IDENTITY_MATRIX;
  1228. var glyphsLength = glyphs.length;
  1229. var isTextInvisible = current.textRenderingMode === _util.TextRenderingMode.INVISIBLE;
  1230. var i, glyph, width, spacingLength;
  1231. if (isTextInvisible || fontSize === 0) {
  1232. return;
  1233. }
  1234. this.cachedGetSinglePixelWidth = null;
  1235. ctx.save();
  1236. ctx.transform.apply(ctx, current.textMatrix);
  1237. ctx.translate(current.x, current.y);
  1238. ctx.scale(textHScale, fontDirection);
  1239. for (i = 0; i < glyphsLength; ++i) {
  1240. glyph = glyphs[i];
  1241. if ((0, _util.isNum)(glyph)) {
  1242. spacingLength = spacingDir * glyph * fontSize / 1000;
  1243. this.ctx.translate(spacingLength, 0);
  1244. current.x += spacingLength * textHScale;
  1245. continue;
  1246. }
  1247. var spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
  1248. var operatorList = font.charProcOperatorList[glyph.operatorListId];
  1249. if (!operatorList) {
  1250. (0, _util.warn)('Type3 character "' + glyph.operatorListId + '" is not available.');
  1251. continue;
  1252. }
  1253. this.processingType3 = glyph;
  1254. this.save();
  1255. ctx.scale(fontSize, fontSize);
  1256. ctx.transform.apply(ctx, fontMatrix);
  1257. this.executeOperatorList(operatorList);
  1258. this.restore();
  1259. var transformed = _util.Util.applyTransform([glyph.width, 0], fontMatrix);
  1260. width = transformed[0] * fontSize + spacing;
  1261. ctx.translate(width, 0);
  1262. current.x += width * textHScale;
  1263. }
  1264. ctx.restore();
  1265. this.processingType3 = null;
  1266. },
  1267. setCharWidth: function CanvasGraphics_setCharWidth(xWidth, yWidth) {},
  1268. setCharWidthAndBounds: function CanvasGraphics_setCharWidthAndBounds(xWidth, yWidth, llx, lly, urx, ury) {
  1269. this.ctx.rect(llx, lly, urx - llx, ury - lly);
  1270. this.clip();
  1271. this.endPath();
  1272. },
  1273. getColorN_Pattern: function CanvasGraphics_getColorN_Pattern(IR) {
  1274. var _this = this;
  1275. var pattern;
  1276. if (IR[0] === 'TilingPattern') {
  1277. var color = IR[1];
  1278. var baseTransform = this.baseTransform || this.ctx.mozCurrentTransform.slice();
  1279. var canvasGraphicsFactory = {
  1280. createCanvasGraphics: function createCanvasGraphics(ctx) {
  1281. return new CanvasGraphics(ctx, _this.commonObjs, _this.objs, _this.canvasFactory);
  1282. }
  1283. };
  1284. pattern = new _pattern_helper.TilingPattern(IR, color, this.ctx, canvasGraphicsFactory, baseTransform);
  1285. } else {
  1286. pattern = (0, _pattern_helper.getShadingPatternFromIR)(IR);
  1287. }
  1288. return pattern;
  1289. },
  1290. setStrokeColorN: function CanvasGraphics_setStrokeColorN() {
  1291. this.current.strokeColor = this.getColorN_Pattern(arguments);
  1292. },
  1293. setFillColorN: function CanvasGraphics_setFillColorN() {
  1294. this.current.fillColor = this.getColorN_Pattern(arguments);
  1295. this.current.patternFill = true;
  1296. },
  1297. setStrokeRGBColor: function CanvasGraphics_setStrokeRGBColor(r, g, b) {
  1298. var color = _util.Util.makeCssRgb(r, g, b);
  1299. this.ctx.strokeStyle = color;
  1300. this.current.strokeColor = color;
  1301. },
  1302. setFillRGBColor: function CanvasGraphics_setFillRGBColor(r, g, b) {
  1303. var color = _util.Util.makeCssRgb(r, g, b);
  1304. this.ctx.fillStyle = color;
  1305. this.current.fillColor = color;
  1306. this.current.patternFill = false;
  1307. },
  1308. shadingFill: function CanvasGraphics_shadingFill(patternIR) {
  1309. var ctx = this.ctx;
  1310. this.save();
  1311. var pattern = (0, _pattern_helper.getShadingPatternFromIR)(patternIR);
  1312. ctx.fillStyle = pattern.getPattern(ctx, this, true);
  1313. var inv = ctx.mozCurrentTransformInverse;
  1314. if (inv) {
  1315. var canvas = ctx.canvas;
  1316. var width = canvas.width;
  1317. var height = canvas.height;
  1318. var bl = _util.Util.applyTransform([0, 0], inv);
  1319. var br = _util.Util.applyTransform([0, height], inv);
  1320. var ul = _util.Util.applyTransform([width, 0], inv);
  1321. var ur = _util.Util.applyTransform([width, height], inv);
  1322. var x0 = Math.min(bl[0], br[0], ul[0], ur[0]);
  1323. var y0 = Math.min(bl[1], br[1], ul[1], ur[1]);
  1324. var x1 = Math.max(bl[0], br[0], ul[0], ur[0]);
  1325. var y1 = Math.max(bl[1], br[1], ul[1], ur[1]);
  1326. this.ctx.fillRect(x0, y0, x1 - x0, y1 - y0);
  1327. } else {
  1328. this.ctx.fillRect(-1e10, -1e10, 2e10, 2e10);
  1329. }
  1330. this.restore();
  1331. },
  1332. beginInlineImage: function CanvasGraphics_beginInlineImage() {
  1333. (0, _util.error)('Should not call beginInlineImage');
  1334. },
  1335. beginImageData: function CanvasGraphics_beginImageData() {
  1336. (0, _util.error)('Should not call beginImageData');
  1337. },
  1338. paintFormXObjectBegin: function CanvasGraphics_paintFormXObjectBegin(matrix, bbox) {
  1339. this.save();
  1340. this.baseTransformStack.push(this.baseTransform);
  1341. if ((0, _util.isArray)(matrix) && matrix.length === 6) {
  1342. this.transform.apply(this, matrix);
  1343. }
  1344. this.baseTransform = this.ctx.mozCurrentTransform;
  1345. if ((0, _util.isArray)(bbox) && bbox.length === 4) {
  1346. var width = bbox[2] - bbox[0];
  1347. var height = bbox[3] - bbox[1];
  1348. this.ctx.rect(bbox[0], bbox[1], width, height);
  1349. this.clip();
  1350. this.endPath();
  1351. }
  1352. },
  1353. paintFormXObjectEnd: function CanvasGraphics_paintFormXObjectEnd() {
  1354. this.restore();
  1355. this.baseTransform = this.baseTransformStack.pop();
  1356. },
  1357. beginGroup: function CanvasGraphics_beginGroup(group) {
  1358. this.save();
  1359. var currentCtx = this.ctx;
  1360. if (!group.isolated) {
  1361. (0, _util.info)('TODO: Support non-isolated groups.');
  1362. }
  1363. if (group.knockout) {
  1364. (0, _util.warn)('Knockout groups not supported.');
  1365. }
  1366. var currentTransform = currentCtx.mozCurrentTransform;
  1367. if (group.matrix) {
  1368. currentCtx.transform.apply(currentCtx, group.matrix);
  1369. }
  1370. (0, _util.assert)(group.bbox, 'Bounding box is required.');
  1371. var bounds = _util.Util.getAxialAlignedBoundingBox(group.bbox, currentCtx.mozCurrentTransform);
  1372. var canvasBounds = [0, 0, currentCtx.canvas.width, currentCtx.canvas.height];
  1373. bounds = _util.Util.intersect(bounds, canvasBounds) || [0, 0, 0, 0];
  1374. var offsetX = Math.floor(bounds[0]);
  1375. var offsetY = Math.floor(bounds[1]);
  1376. var drawnWidth = Math.max(Math.ceil(bounds[2]) - offsetX, 1);
  1377. var drawnHeight = Math.max(Math.ceil(bounds[3]) - offsetY, 1);
  1378. var scaleX = 1,
  1379. scaleY = 1;
  1380. if (drawnWidth > MAX_GROUP_SIZE) {
  1381. scaleX = drawnWidth / MAX_GROUP_SIZE;
  1382. drawnWidth = MAX_GROUP_SIZE;
  1383. }
  1384. if (drawnHeight > MAX_GROUP_SIZE) {
  1385. scaleY = drawnHeight / MAX_GROUP_SIZE;
  1386. drawnHeight = MAX_GROUP_SIZE;
  1387. }
  1388. var cacheId = 'groupAt' + this.groupLevel;
  1389. if (group.smask) {
  1390. cacheId += '_smask_' + this.smaskCounter++ % 2;
  1391. }
  1392. var scratchCanvas = this.cachedCanvases.getCanvas(cacheId, drawnWidth, drawnHeight, true);
  1393. var groupCtx = scratchCanvas.context;
  1394. groupCtx.scale(1 / scaleX, 1 / scaleY);
  1395. groupCtx.translate(-offsetX, -offsetY);
  1396. groupCtx.transform.apply(groupCtx, currentTransform);
  1397. if (group.smask) {
  1398. this.smaskStack.push({
  1399. canvas: scratchCanvas.canvas,
  1400. context: groupCtx,
  1401. offsetX: offsetX,
  1402. offsetY: offsetY,
  1403. scaleX: scaleX,
  1404. scaleY: scaleY,
  1405. subtype: group.smask.subtype,
  1406. backdrop: group.smask.backdrop,
  1407. transferMap: group.smask.transferMap || null,
  1408. startTransformInverse: null
  1409. });
  1410. } else {
  1411. currentCtx.setTransform(1, 0, 0, 1, 0, 0);
  1412. currentCtx.translate(offsetX, offsetY);
  1413. currentCtx.scale(scaleX, scaleY);
  1414. }
  1415. copyCtxState(currentCtx, groupCtx);
  1416. this.ctx = groupCtx;
  1417. this.setGState([['BM', 'source-over'], ['ca', 1], ['CA', 1]]);
  1418. this.groupStack.push(currentCtx);
  1419. this.groupLevel++;
  1420. this.current.activeSMask = null;
  1421. },
  1422. endGroup: function CanvasGraphics_endGroup(group) {
  1423. this.groupLevel--;
  1424. var groupCtx = this.ctx;
  1425. this.ctx = this.groupStack.pop();
  1426. if (this.ctx.imageSmoothingEnabled !== undefined) {
  1427. this.ctx.imageSmoothingEnabled = false;
  1428. } else {
  1429. this.ctx.mozImageSmoothingEnabled = false;
  1430. }
  1431. if (group.smask) {
  1432. this.tempSMask = this.smaskStack.pop();
  1433. } else {
  1434. this.ctx.drawImage(groupCtx.canvas, 0, 0);
  1435. }
  1436. this.restore();
  1437. },
  1438. beginAnnotations: function CanvasGraphics_beginAnnotations() {
  1439. this.save();
  1440. this.current = new CanvasExtraState();
  1441. if (this.baseTransform) {
  1442. this.ctx.setTransform.apply(this.ctx, this.baseTransform);
  1443. }
  1444. },
  1445. endAnnotations: function CanvasGraphics_endAnnotations() {
  1446. this.restore();
  1447. },
  1448. beginAnnotation: function CanvasGraphics_beginAnnotation(rect, transform, matrix) {
  1449. this.save();
  1450. if ((0, _util.isArray)(rect) && rect.length === 4) {
  1451. var width = rect[2] - rect[0];
  1452. var height = rect[3] - rect[1];
  1453. this.ctx.rect(rect[0], rect[1], width, height);
  1454. this.clip();
  1455. this.endPath();
  1456. }
  1457. this.transform.apply(this, transform);
  1458. this.transform.apply(this, matrix);
  1459. },
  1460. endAnnotation: function CanvasGraphics_endAnnotation() {
  1461. this.restore();
  1462. },
  1463. paintJpegXObject: function CanvasGraphics_paintJpegXObject(objId, w, h) {
  1464. var domImage = this.objs.get(objId);
  1465. if (!domImage) {
  1466. (0, _util.warn)('Dependent image isn\'t ready yet');
  1467. return;
  1468. }
  1469. this.save();
  1470. var ctx = this.ctx;
  1471. ctx.scale(1 / w, -1 / h);
  1472. ctx.drawImage(domImage, 0, 0, domImage.width, domImage.height, 0, -h, w, h);
  1473. if (this.imageLayer) {
  1474. var currentTransform = ctx.mozCurrentTransformInverse;
  1475. var position = this.getCanvasPosition(0, 0);
  1476. this.imageLayer.appendImage({
  1477. objId: objId,
  1478. left: position[0],
  1479. top: position[1],
  1480. width: w / currentTransform[0],
  1481. height: h / currentTransform[3]
  1482. });
  1483. }
  1484. this.restore();
  1485. },
  1486. paintImageMaskXObject: function CanvasGraphics_paintImageMaskXObject(img) {
  1487. var ctx = this.ctx;
  1488. var width = img.width,
  1489. height = img.height;
  1490. var fillColor = this.current.fillColor;
  1491. var isPatternFill = this.current.patternFill;
  1492. var glyph = this.processingType3;
  1493. if (COMPILE_TYPE3_GLYPHS && glyph && glyph.compiled === undefined) {
  1494. if (width <= MAX_SIZE_TO_COMPILE && height <= MAX_SIZE_TO_COMPILE) {
  1495. glyph.compiled = compileType3Glyph({
  1496. data: img.data,
  1497. width: width,
  1498. height: height
  1499. });
  1500. } else {
  1501. glyph.compiled = null;
  1502. }
  1503. }
  1504. if (glyph && glyph.compiled) {
  1505. glyph.compiled(ctx);
  1506. return;
  1507. }
  1508. var maskCanvas = this.cachedCanvases.getCanvas('maskCanvas', width, height);
  1509. var maskCtx = maskCanvas.context;
  1510. maskCtx.save();
  1511. putBinaryImageMask(maskCtx, img);
  1512. maskCtx.globalCompositeOperation = 'source-in';
  1513. maskCtx.fillStyle = isPatternFill ? fillColor.getPattern(maskCtx, this) : fillColor;
  1514. maskCtx.fillRect(0, 0, width, height);
  1515. maskCtx.restore();
  1516. this.paintInlineImageXObject(maskCanvas.canvas);
  1517. },
  1518. paintImageMaskXObjectRepeat: function CanvasGraphics_paintImageMaskXObjectRepeat(imgData, scaleX, scaleY, positions) {
  1519. var width = imgData.width;
  1520. var height = imgData.height;
  1521. var fillColor = this.current.fillColor;
  1522. var isPatternFill = this.current.patternFill;
  1523. var maskCanvas = this.cachedCanvases.getCanvas('maskCanvas', width, height);
  1524. var maskCtx = maskCanvas.context;
  1525. maskCtx.save();
  1526. putBinaryImageMask(maskCtx, imgData);
  1527. maskCtx.globalCompositeOperation = 'source-in';
  1528. maskCtx.fillStyle = isPatternFill ? fillColor.getPattern(maskCtx, this) : fillColor;
  1529. maskCtx.fillRect(0, 0, width, height);
  1530. maskCtx.restore();
  1531. var ctx = this.ctx;
  1532. for (var i = 0, ii = positions.length; i < ii; i += 2) {
  1533. ctx.save();
  1534. ctx.transform(scaleX, 0, 0, scaleY, positions[i], positions[i + 1]);
  1535. ctx.scale(1, -1);
  1536. ctx.drawImage(maskCanvas.canvas, 0, 0, width, height, 0, -1, 1, 1);
  1537. ctx.restore();
  1538. }
  1539. },
  1540. paintImageMaskXObjectGroup: function CanvasGraphics_paintImageMaskXObjectGroup(images) {
  1541. var ctx = this.ctx;
  1542. var fillColor = this.current.fillColor;
  1543. var isPatternFill = this.current.patternFill;
  1544. for (var i = 0, ii = images.length; i < ii; i++) {
  1545. var image = images[i];
  1546. var width = image.width,
  1547. height = image.height;
  1548. var maskCanvas = this.cachedCanvases.getCanvas('maskCanvas', width, height);
  1549. var maskCtx = maskCanvas.context;
  1550. maskCtx.save();
  1551. putBinaryImageMask(maskCtx, image);
  1552. maskCtx.globalCompositeOperation = 'source-in';
  1553. maskCtx.fillStyle = isPatternFill ? fillColor.getPattern(maskCtx, this) : fillColor;
  1554. maskCtx.fillRect(0, 0, width, height);
  1555. maskCtx.restore();
  1556. ctx.save();
  1557. ctx.transform.apply(ctx, image.transform);
  1558. ctx.scale(1, -1);
  1559. ctx.drawImage(maskCanvas.canvas, 0, 0, width, height, 0, -1, 1, 1);
  1560. ctx.restore();
  1561. }
  1562. },
  1563. paintImageXObject: function CanvasGraphics_paintImageXObject(objId) {
  1564. var imgData = this.objs.get(objId);
  1565. if (!imgData) {
  1566. (0, _util.warn)('Dependent image isn\'t ready yet');
  1567. return;
  1568. }
  1569. this.paintInlineImageXObject(imgData);
  1570. },
  1571. paintImageXObjectRepeat: function CanvasGraphics_paintImageXObjectRepeat(objId, scaleX, scaleY, positions) {
  1572. var imgData = this.objs.get(objId);
  1573. if (!imgData) {
  1574. (0, _util.warn)('Dependent image isn\'t ready yet');
  1575. return;
  1576. }
  1577. var width = imgData.width;
  1578. var height = imgData.height;
  1579. var map = [];
  1580. for (var i = 0, ii = positions.length; i < ii; i += 2) {
  1581. map.push({
  1582. transform: [scaleX, 0, 0, scaleY, positions[i], positions[i + 1]],
  1583. x: 0,
  1584. y: 0,
  1585. w: width,
  1586. h: height
  1587. });
  1588. }
  1589. this.paintInlineImageXObjectGroup(imgData, map);
  1590. },
  1591. paintInlineImageXObject: function CanvasGraphics_paintInlineImageXObject(imgData) {
  1592. var width = imgData.width;
  1593. var height = imgData.height;
  1594. var ctx = this.ctx;
  1595. this.save();
  1596. ctx.scale(1 / width, -1 / height);
  1597. var currentTransform = ctx.mozCurrentTransformInverse;
  1598. var a = currentTransform[0],
  1599. b = currentTransform[1];
  1600. var widthScale = Math.max(Math.sqrt(a * a + b * b), 1);
  1601. var c = currentTransform[2],
  1602. d = currentTransform[3];
  1603. var heightScale = Math.max(Math.sqrt(c * c + d * d), 1);
  1604. var imgToPaint, tmpCanvas;
  1605. if (imgData instanceof HTMLElement || !imgData.data) {
  1606. imgToPaint = imgData;
  1607. } else {
  1608. tmpCanvas = this.cachedCanvases.getCanvas('inlineImage', width, height);
  1609. var tmpCtx = tmpCanvas.context;
  1610. putBinaryImageData(tmpCtx, imgData);
  1611. imgToPaint = tmpCanvas.canvas;
  1612. }
  1613. var paintWidth = width,
  1614. paintHeight = height;
  1615. var tmpCanvasId = 'prescale1';
  1616. while (widthScale > 2 && paintWidth > 1 || heightScale > 2 && paintHeight > 1) {
  1617. var newWidth = paintWidth,
  1618. newHeight = paintHeight;
  1619. if (widthScale > 2 && paintWidth > 1) {
  1620. newWidth = Math.ceil(paintWidth / 2);
  1621. widthScale /= paintWidth / newWidth;
  1622. }
  1623. if (heightScale > 2 && paintHeight > 1) {
  1624. newHeight = Math.ceil(paintHeight / 2);
  1625. heightScale /= paintHeight / newHeight;
  1626. }
  1627. tmpCanvas = this.cachedCanvases.getCanvas(tmpCanvasId, newWidth, newHeight);
  1628. tmpCtx = tmpCanvas.context;
  1629. tmpCtx.clearRect(0, 0, newWidth, newHeight);
  1630. tmpCtx.drawImage(imgToPaint, 0, 0, paintWidth, paintHeight, 0, 0, newWidth, newHeight);
  1631. imgToPaint = tmpCanvas.canvas;
  1632. paintWidth = newWidth;
  1633. paintHeight = newHeight;
  1634. tmpCanvasId = tmpCanvasId === 'prescale1' ? 'prescale2' : 'prescale1';
  1635. }
  1636. ctx.drawImage(imgToPaint, 0, 0, paintWidth, paintHeight, 0, -height, width, height);
  1637. if (this.imageLayer) {
  1638. var position = this.getCanvasPosition(0, -height);
  1639. this.imageLayer.appendImage({
  1640. imgData: imgData,
  1641. left: position[0],
  1642. top: position[1],
  1643. width: width / currentTransform[0],
  1644. height: height / currentTransform[3]
  1645. });
  1646. }
  1647. this.restore();
  1648. },
  1649. paintInlineImageXObjectGroup: function CanvasGraphics_paintInlineImageXObjectGroup(imgData, map) {
  1650. var ctx = this.ctx;
  1651. var w = imgData.width;
  1652. var h = imgData.height;
  1653. var tmpCanvas = this.cachedCanvases.getCanvas('inlineImage', w, h);
  1654. var tmpCtx = tmpCanvas.context;
  1655. putBinaryImageData(tmpCtx, imgData);
  1656. for (var i = 0, ii = map.length; i < ii; i++) {
  1657. var entry = map[i];
  1658. ctx.save();
  1659. ctx.transform.apply(ctx, entry.transform);
  1660. ctx.scale(1, -1);
  1661. ctx.drawImage(tmpCanvas.canvas, entry.x, entry.y, entry.w, entry.h, 0, -1, 1, 1);
  1662. if (this.imageLayer) {
  1663. var position = this.getCanvasPosition(entry.x, entry.y);
  1664. this.imageLayer.appendImage({
  1665. imgData: imgData,
  1666. left: position[0],
  1667. top: position[1],
  1668. width: w,
  1669. height: h
  1670. });
  1671. }
  1672. ctx.restore();
  1673. }
  1674. },
  1675. paintSolidColorImageMask: function CanvasGraphics_paintSolidColorImageMask() {
  1676. this.ctx.fillRect(0, 0, 1, 1);
  1677. },
  1678. paintXObject: function CanvasGraphics_paintXObject() {
  1679. (0, _util.warn)('Unsupported \'paintXObject\' command.');
  1680. },
  1681. markPoint: function CanvasGraphics_markPoint(tag) {},
  1682. markPointProps: function CanvasGraphics_markPointProps(tag, properties) {},
  1683. beginMarkedContent: function CanvasGraphics_beginMarkedContent(tag) {},
  1684. beginMarkedContentProps: function CanvasGraphics_beginMarkedContentProps(tag, properties) {},
  1685. endMarkedContent: function CanvasGraphics_endMarkedContent() {},
  1686. beginCompat: function CanvasGraphics_beginCompat() {},
  1687. endCompat: function CanvasGraphics_endCompat() {},
  1688. consumePath: function CanvasGraphics_consumePath() {
  1689. var ctx = this.ctx;
  1690. if (this.pendingClip) {
  1691. if (this.pendingClip === EO_CLIP) {
  1692. ctx.clip('evenodd');
  1693. } else {
  1694. ctx.clip();
  1695. }
  1696. this.pendingClip = null;
  1697. }
  1698. ctx.beginPath();
  1699. },
  1700. getSinglePixelWidth: function CanvasGraphics_getSinglePixelWidth(scale) {
  1701. if (this.cachedGetSinglePixelWidth === null) {
  1702. this.ctx.save();
  1703. var inverse = this.ctx.mozCurrentTransformInverse;
  1704. this.ctx.restore();
  1705. this.cachedGetSinglePixelWidth = Math.sqrt(Math.max(inverse[0] * inverse[0] + inverse[1] * inverse[1], inverse[2] * inverse[2] + inverse[3] * inverse[3]));
  1706. }
  1707. return this.cachedGetSinglePixelWidth;
  1708. },
  1709. getCanvasPosition: function CanvasGraphics_getCanvasPosition(x, y) {
  1710. var transform = this.ctx.mozCurrentTransform;
  1711. return [transform[0] * x + transform[2] * y + transform[4], transform[1] * x + transform[3] * y + transform[5]];
  1712. }
  1713. };
  1714. for (var op in _util.OPS) {
  1715. CanvasGraphics.prototype[_util.OPS[op]] = CanvasGraphics.prototype[op];
  1716. }
  1717. return CanvasGraphics;
  1718. }();
  1719. exports.CanvasGraphics = CanvasGraphics;