canvas.js 63 KB

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