canvas.js 62 KB

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