canvas.js 66 KB

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