canvas.js 62 KB

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