2
0

canvas.js 64 KB

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