2
0

canvas.js 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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 _display_utils = require("./display_utils.js");
  30. const MIN_FONT_SIZE = 16;
  31. const MAX_FONT_SIZE = 100;
  32. const MAX_GROUP_SIZE = 4096;
  33. const EXECUTION_TIME = 15;
  34. const EXECUTION_STEPS = 10;
  35. const COMPILE_TYPE3_GLYPHS = true;
  36. const MAX_SIZE_TO_COMPILE = 1000;
  37. const FULL_CHUNK_HEIGHT = 16;
  38. const LINEWIDTH_SCALE_FACTOR = 1.000001;
  39. function mirrorContextOperations(ctx, destCtx) {
  40. if (ctx._removeMirroring) {
  41. throw new Error("Context is already forwarding operations.");
  42. }
  43. ctx.__originalSave = ctx.save;
  44. ctx.__originalRestore = ctx.restore;
  45. ctx.__originalRotate = ctx.rotate;
  46. ctx.__originalScale = ctx.scale;
  47. ctx.__originalTranslate = ctx.translate;
  48. ctx.__originalTransform = ctx.transform;
  49. ctx.__originalSetTransform = ctx.setTransform;
  50. ctx.__originalResetTransform = ctx.resetTransform;
  51. ctx.__originalClip = ctx.clip;
  52. ctx.__originalMoveTo = ctx.moveTo;
  53. ctx.__originalLineTo = ctx.lineTo;
  54. ctx.__originalBezierCurveTo = ctx.bezierCurveTo;
  55. ctx.__originalRect = ctx.rect;
  56. ctx.__originalClosePath = ctx.closePath;
  57. ctx.__originalBeginPath = ctx.beginPath;
  58. ctx._removeMirroring = () => {
  59. ctx.save = ctx.__originalSave;
  60. ctx.restore = ctx.__originalRestore;
  61. ctx.rotate = ctx.__originalRotate;
  62. ctx.scale = ctx.__originalScale;
  63. ctx.translate = ctx.__originalTranslate;
  64. ctx.transform = ctx.__originalTransform;
  65. ctx.setTransform = ctx.__originalSetTransform;
  66. ctx.resetTransform = ctx.__originalResetTransform;
  67. ctx.clip = ctx.__originalClip;
  68. ctx.moveTo = ctx.__originalMoveTo;
  69. ctx.lineTo = ctx.__originalLineTo;
  70. ctx.bezierCurveTo = ctx.__originalBezierCurveTo;
  71. ctx.rect = ctx.__originalRect;
  72. ctx.closePath = ctx.__originalClosePath;
  73. ctx.beginPath = ctx.__originalBeginPath;
  74. delete ctx._removeMirroring;
  75. };
  76. ctx.save = function ctxSave() {
  77. destCtx.save();
  78. this.__originalSave();
  79. };
  80. ctx.restore = function ctxRestore() {
  81. destCtx.restore();
  82. this.__originalRestore();
  83. };
  84. ctx.translate = function ctxTranslate(x, y) {
  85. destCtx.translate(x, y);
  86. this.__originalTranslate(x, y);
  87. };
  88. ctx.scale = function ctxScale(x, y) {
  89. destCtx.scale(x, y);
  90. this.__originalScale(x, y);
  91. };
  92. ctx.transform = function ctxTransform(a, b, c, d, e, f) {
  93. destCtx.transform(a, b, c, d, e, f);
  94. this.__originalTransform(a, b, c, d, e, f);
  95. };
  96. ctx.setTransform = function ctxSetTransform(a, b, c, d, e, f) {
  97. destCtx.setTransform(a, b, c, d, e, f);
  98. this.__originalSetTransform(a, b, c, d, e, f);
  99. };
  100. ctx.resetTransform = function ctxResetTransform() {
  101. destCtx.resetTransform();
  102. this.__originalResetTransform();
  103. };
  104. ctx.rotate = function ctxRotate(angle) {
  105. destCtx.rotate(angle);
  106. this.__originalRotate(angle);
  107. };
  108. ctx.clip = function ctxRotate(rule) {
  109. destCtx.clip(rule);
  110. this.__originalClip(rule);
  111. };
  112. ctx.moveTo = function (x, y) {
  113. destCtx.moveTo(x, y);
  114. this.__originalMoveTo(x, y);
  115. };
  116. ctx.lineTo = function (x, y) {
  117. destCtx.lineTo(x, y);
  118. this.__originalLineTo(x, y);
  119. };
  120. ctx.bezierCurveTo = function (cp1x, cp1y, cp2x, cp2y, x, y) {
  121. destCtx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
  122. this.__originalBezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
  123. };
  124. ctx.rect = function (x, y, width, height) {
  125. destCtx.rect(x, y, width, height);
  126. this.__originalRect(x, y, width, height);
  127. };
  128. ctx.closePath = function () {
  129. destCtx.closePath();
  130. this.__originalClosePath();
  131. };
  132. ctx.beginPath = function () {
  133. destCtx.beginPath();
  134. this.__originalBeginPath();
  135. };
  136. }
  137. function addContextCurrentTransform(ctx) {
  138. if (ctx.mozCurrentTransform) {
  139. return;
  140. }
  141. ctx._originalSave = ctx.save;
  142. ctx._originalRestore = ctx.restore;
  143. ctx._originalRotate = ctx.rotate;
  144. ctx._originalScale = ctx.scale;
  145. ctx._originalTranslate = ctx.translate;
  146. ctx._originalTransform = ctx.transform;
  147. ctx._originalSetTransform = ctx.setTransform;
  148. ctx._originalResetTransform = ctx.resetTransform;
  149. ctx._transformMatrix = ctx._transformMatrix || [1, 0, 0, 1, 0, 0];
  150. ctx._transformStack = [];
  151. try {
  152. const desc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(ctx), "lineWidth");
  153. ctx._setLineWidth = desc.set;
  154. ctx._getLineWidth = desc.get;
  155. Object.defineProperty(ctx, "lineWidth", {
  156. set: function setLineWidth(width) {
  157. this._setLineWidth(width * LINEWIDTH_SCALE_FACTOR);
  158. },
  159. get: function getLineWidth() {
  160. return this._getLineWidth();
  161. }
  162. });
  163. } catch (_) {}
  164. Object.defineProperty(ctx, "mozCurrentTransform", {
  165. get: function getCurrentTransform() {
  166. return this._transformMatrix;
  167. }
  168. });
  169. Object.defineProperty(ctx, "mozCurrentTransformInverse", {
  170. get: function getCurrentTransformInverse() {
  171. const [a, b, c, d, e, f] = this._transformMatrix;
  172. const ad_bc = a * d - b * c;
  173. const bc_ad = b * c - a * d;
  174. 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];
  175. }
  176. });
  177. ctx.save = function ctxSave() {
  178. const old = this._transformMatrix;
  179. this._transformStack.push(old);
  180. this._transformMatrix = old.slice(0, 6);
  181. this._originalSave();
  182. };
  183. ctx.restore = function ctxRestore() {
  184. const prev = this._transformStack.pop();
  185. if (prev) {
  186. this._transformMatrix = prev;
  187. this._originalRestore();
  188. }
  189. };
  190. ctx.translate = function ctxTranslate(x, y) {
  191. const m = this._transformMatrix;
  192. m[4] = m[0] * x + m[2] * y + m[4];
  193. m[5] = m[1] * x + m[3] * y + m[5];
  194. this._originalTranslate(x, y);
  195. };
  196. ctx.scale = function ctxScale(x, y) {
  197. const m = this._transformMatrix;
  198. m[0] *= x;
  199. m[1] *= x;
  200. m[2] *= y;
  201. m[3] *= y;
  202. this._originalScale(x, y);
  203. };
  204. ctx.transform = function ctxTransform(a, b, c, d, e, f) {
  205. const m = this._transformMatrix;
  206. 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]];
  207. ctx._originalTransform(a, b, c, d, e, f);
  208. };
  209. ctx.setTransform = function ctxSetTransform(a, b, c, d, e, f) {
  210. this._transformMatrix = [a, b, c, d, e, f];
  211. ctx._originalSetTransform(a, b, c, d, e, f);
  212. };
  213. ctx.resetTransform = function ctxResetTransform() {
  214. this._transformMatrix = [1, 0, 0, 1, 0, 0];
  215. ctx._originalResetTransform();
  216. };
  217. ctx.rotate = function ctxRotate(angle) {
  218. const cosValue = Math.cos(angle);
  219. const sinValue = Math.sin(angle);
  220. const m = this._transformMatrix;
  221. 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]];
  222. this._originalRotate(angle);
  223. };
  224. }
  225. class CachedCanvases {
  226. constructor(canvasFactory) {
  227. this.canvasFactory = canvasFactory;
  228. this.cache = Object.create(null);
  229. }
  230. getCanvas(id, width, height, trackTransform) {
  231. let canvasEntry;
  232. if (this.cache[id] !== undefined) {
  233. canvasEntry = this.cache[id];
  234. this.canvasFactory.reset(canvasEntry, width, height);
  235. canvasEntry.context.setTransform(1, 0, 0, 1, 0, 0);
  236. } else {
  237. canvasEntry = this.canvasFactory.create(width, height);
  238. this.cache[id] = canvasEntry;
  239. }
  240. if (trackTransform) {
  241. addContextCurrentTransform(canvasEntry.context);
  242. }
  243. return canvasEntry;
  244. }
  245. clear() {
  246. for (const id in this.cache) {
  247. const canvasEntry = this.cache[id];
  248. this.canvasFactory.destroy(canvasEntry);
  249. delete this.cache[id];
  250. }
  251. }
  252. }
  253. function compileType3Glyph(imgData) {
  254. const POINT_TO_PROCESS_LIMIT = 1000;
  255. const POINT_TYPES = new Uint8Array([0, 2, 4, 0, 1, 0, 5, 4, 8, 10, 0, 8, 0, 2, 1, 0]);
  256. const width = imgData.width,
  257. height = imgData.height,
  258. width1 = width + 1;
  259. let i, ii, j, j0;
  260. const points = new Uint8Array(width1 * (height + 1));
  261. const lineSize = width + 7 & ~7,
  262. data0 = imgData.data;
  263. const data = new Uint8Array(lineSize * height);
  264. let pos = 0;
  265. for (i = 0, ii = data0.length; i < ii; i++) {
  266. const elem = data0[i];
  267. let mask = 128;
  268. while (mask > 0) {
  269. data[pos++] = elem & mask ? 0 : 255;
  270. mask >>= 1;
  271. }
  272. }
  273. let count = 0;
  274. pos = 0;
  275. if (data[pos] !== 0) {
  276. points[0] = 1;
  277. ++count;
  278. }
  279. for (j = 1; j < width; j++) {
  280. if (data[pos] !== data[pos + 1]) {
  281. points[j] = data[pos] ? 2 : 1;
  282. ++count;
  283. }
  284. pos++;
  285. }
  286. if (data[pos] !== 0) {
  287. points[j] = 2;
  288. ++count;
  289. }
  290. for (i = 1; i < height; i++) {
  291. pos = i * lineSize;
  292. j0 = i * width1;
  293. if (data[pos - lineSize] !== data[pos]) {
  294. points[j0] = data[pos] ? 1 : 8;
  295. ++count;
  296. }
  297. let sum = (data[pos] ? 4 : 0) + (data[pos - lineSize] ? 8 : 0);
  298. for (j = 1; j < width; j++) {
  299. sum = (sum >> 2) + (data[pos + 1] ? 4 : 0) + (data[pos - lineSize + 1] ? 8 : 0);
  300. if (POINT_TYPES[sum]) {
  301. points[j0 + j] = POINT_TYPES[sum];
  302. ++count;
  303. }
  304. pos++;
  305. }
  306. if (data[pos - lineSize] !== data[pos]) {
  307. points[j0 + j] = data[pos] ? 2 : 4;
  308. ++count;
  309. }
  310. if (count > POINT_TO_PROCESS_LIMIT) {
  311. return null;
  312. }
  313. }
  314. pos = lineSize * (height - 1);
  315. j0 = i * width1;
  316. if (data[pos] !== 0) {
  317. points[j0] = 8;
  318. ++count;
  319. }
  320. for (j = 1; j < width; j++) {
  321. if (data[pos] !== data[pos + 1]) {
  322. points[j0 + j] = data[pos] ? 4 : 8;
  323. ++count;
  324. }
  325. pos++;
  326. }
  327. if (data[pos] !== 0) {
  328. points[j0 + j] = 4;
  329. ++count;
  330. }
  331. if (count > POINT_TO_PROCESS_LIMIT) {
  332. return null;
  333. }
  334. const steps = new Int32Array([0, width1, -1, 0, -width1, 0, 0, 0, 1]);
  335. const outlines = [];
  336. for (i = 0; count && i <= height; i++) {
  337. let p = i * width1;
  338. const end = p + width;
  339. while (p < end && !points[p]) {
  340. p++;
  341. }
  342. if (p === end) {
  343. continue;
  344. }
  345. const coords = [p % width1, i];
  346. const p0 = p;
  347. let type = points[p];
  348. do {
  349. const step = steps[type];
  350. do {
  351. p += step;
  352. } while (!points[p]);
  353. const pp = points[p];
  354. if (pp !== 5 && pp !== 10) {
  355. type = pp;
  356. points[p] = 0;
  357. } else {
  358. type = pp & 0x33 * type >> 4;
  359. points[p] &= type >> 2 | type << 2;
  360. }
  361. coords.push(p % width1, p / width1 | 0);
  362. if (!points[p]) {
  363. --count;
  364. }
  365. } while (p0 !== p);
  366. outlines.push(coords);
  367. --i;
  368. }
  369. const drawOutline = function (c) {
  370. c.save();
  371. c.scale(1 / width, -1 / height);
  372. c.translate(0, -height);
  373. c.beginPath();
  374. for (let k = 0, kk = outlines.length; k < kk; k++) {
  375. const o = outlines[k];
  376. c.moveTo(o[0], o[1]);
  377. for (let l = 2, ll = o.length; l < ll; l += 2) {
  378. c.lineTo(o[l], o[l + 1]);
  379. }
  380. }
  381. c.fill();
  382. c.beginPath();
  383. c.restore();
  384. };
  385. return drawOutline;
  386. }
  387. class CanvasExtraState {
  388. constructor(width, height) {
  389. this.alphaIsShape = false;
  390. this.fontSize = 0;
  391. this.fontSizeScale = 1;
  392. this.textMatrix = _util.IDENTITY_MATRIX;
  393. this.textMatrixScale = 1;
  394. this.fontMatrix = _util.FONT_IDENTITY_MATRIX;
  395. this.leading = 0;
  396. this.x = 0;
  397. this.y = 0;
  398. this.lineX = 0;
  399. this.lineY = 0;
  400. this.charSpacing = 0;
  401. this.wordSpacing = 0;
  402. this.textHScale = 1;
  403. this.textRenderingMode = _util.TextRenderingMode.FILL;
  404. this.textRise = 0;
  405. this.fillColor = "#000000";
  406. this.strokeColor = "#000000";
  407. this.patternFill = false;
  408. this.fillAlpha = 1;
  409. this.strokeAlpha = 1;
  410. this.lineWidth = 1;
  411. this.activeSMask = null;
  412. this.transferMaps = null;
  413. this.startNewPathAndClipBox([0, 0, width, height]);
  414. }
  415. clone() {
  416. const clone = Object.create(this);
  417. clone.clipBox = this.clipBox.slice();
  418. return clone;
  419. }
  420. setCurrentPoint(x, y) {
  421. this.x = x;
  422. this.y = y;
  423. }
  424. updatePathMinMax(transform, x, y) {
  425. [x, y] = _util.Util.applyTransform([x, y], transform);
  426. this.minX = Math.min(this.minX, x);
  427. this.minY = Math.min(this.minY, y);
  428. this.maxX = Math.max(this.maxX, x);
  429. this.maxY = Math.max(this.maxY, y);
  430. }
  431. updateCurvePathMinMax(transform, x0, y0, x1, y1, x2, y2, x3, y3) {
  432. const box = _util.Util.bezierBoundingBox(x0, y0, x1, y1, x2, y2, x3, y3);
  433. this.updatePathMinMax(transform, box[0], box[1]);
  434. this.updatePathMinMax(transform, box[2], box[3]);
  435. }
  436. getPathBoundingBox(pathType = _pattern_helper.PathType.FILL, transform = null) {
  437. const box = [this.minX, this.minY, this.maxX, this.maxY];
  438. if (pathType === _pattern_helper.PathType.STROKE) {
  439. if (!transform) {
  440. (0, _util.unreachable)("Stroke bounding box must include transform.");
  441. }
  442. const scale = _util.Util.singularValueDecompose2dScale(transform);
  443. const xStrokePad = scale[0] * this.lineWidth / 2;
  444. const yStrokePad = scale[1] * this.lineWidth / 2;
  445. box[0] -= xStrokePad;
  446. box[1] -= yStrokePad;
  447. box[2] += xStrokePad;
  448. box[3] += yStrokePad;
  449. }
  450. return box;
  451. }
  452. updateClipFromPath() {
  453. const intersect = _util.Util.intersect(this.clipBox, this.getPathBoundingBox());
  454. this.startNewPathAndClipBox(intersect || [0, 0, 0, 0]);
  455. }
  456. startNewPathAndClipBox(box) {
  457. this.clipBox = box;
  458. this.minX = Infinity;
  459. this.minY = Infinity;
  460. this.maxX = 0;
  461. this.maxY = 0;
  462. }
  463. getClippedPathBoundingBox(pathType = _pattern_helper.PathType.FILL, transform = null) {
  464. return _util.Util.intersect(this.clipBox, this.getPathBoundingBox(pathType, transform));
  465. }
  466. }
  467. function putBinaryImageData(ctx, imgData, transferMaps = null) {
  468. if (typeof ImageData !== "undefined" && imgData instanceof ImageData) {
  469. ctx.putImageData(imgData, 0, 0);
  470. return;
  471. }
  472. const height = imgData.height,
  473. width = imgData.width;
  474. const partialChunkHeight = height % FULL_CHUNK_HEIGHT;
  475. const fullChunks = (height - partialChunkHeight) / FULL_CHUNK_HEIGHT;
  476. const totalChunks = partialChunkHeight === 0 ? fullChunks : fullChunks + 1;
  477. const chunkImgData = ctx.createImageData(width, FULL_CHUNK_HEIGHT);
  478. let srcPos = 0,
  479. destPos;
  480. const src = imgData.data;
  481. const dest = chunkImgData.data;
  482. let i, j, thisChunkHeight, elemsInThisChunk;
  483. let transferMapRed, transferMapGreen, transferMapBlue, transferMapGray;
  484. if (transferMaps) {
  485. switch (transferMaps.length) {
  486. case 1:
  487. transferMapRed = transferMaps[0];
  488. transferMapGreen = transferMaps[0];
  489. transferMapBlue = transferMaps[0];
  490. transferMapGray = transferMaps[0];
  491. break;
  492. case 4:
  493. transferMapRed = transferMaps[0];
  494. transferMapGreen = transferMaps[1];
  495. transferMapBlue = transferMaps[2];
  496. transferMapGray = transferMaps[3];
  497. break;
  498. }
  499. }
  500. if (imgData.kind === _util.ImageKind.GRAYSCALE_1BPP) {
  501. const srcLength = src.byteLength;
  502. const dest32 = new Uint32Array(dest.buffer, 0, dest.byteLength >> 2);
  503. const dest32DataLength = dest32.length;
  504. const fullSrcDiff = width + 7 >> 3;
  505. let white = 0xffffffff;
  506. let black = _util.IsLittleEndianCached.value ? 0xff000000 : 0x000000ff;
  507. if (transferMapGray) {
  508. if (transferMapGray[0] === 0xff && transferMapGray[0xff] === 0) {
  509. [white, black] = [black, white];
  510. }
  511. }
  512. for (i = 0; i < totalChunks; i++) {
  513. thisChunkHeight = i < fullChunks ? FULL_CHUNK_HEIGHT : partialChunkHeight;
  514. destPos = 0;
  515. for (j = 0; j < thisChunkHeight; j++) {
  516. const srcDiff = srcLength - srcPos;
  517. let k = 0;
  518. const kEnd = srcDiff > fullSrcDiff ? width : srcDiff * 8 - 7;
  519. const kEndUnrolled = kEnd & ~7;
  520. let mask = 0;
  521. let srcByte = 0;
  522. for (; k < kEndUnrolled; k += 8) {
  523. srcByte = src[srcPos++];
  524. dest32[destPos++] = srcByte & 128 ? white : black;
  525. dest32[destPos++] = srcByte & 64 ? white : black;
  526. dest32[destPos++] = srcByte & 32 ? white : black;
  527. dest32[destPos++] = srcByte & 16 ? white : black;
  528. dest32[destPos++] = srcByte & 8 ? white : black;
  529. dest32[destPos++] = srcByte & 4 ? white : black;
  530. dest32[destPos++] = srcByte & 2 ? white : black;
  531. dest32[destPos++] = srcByte & 1 ? white : black;
  532. }
  533. for (; k < kEnd; k++) {
  534. if (mask === 0) {
  535. srcByte = src[srcPos++];
  536. mask = 128;
  537. }
  538. dest32[destPos++] = srcByte & mask ? white : black;
  539. mask >>= 1;
  540. }
  541. }
  542. while (destPos < dest32DataLength) {
  543. dest32[destPos++] = 0;
  544. }
  545. ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
  546. }
  547. } else if (imgData.kind === _util.ImageKind.RGBA_32BPP) {
  548. const hasTransferMaps = !!(transferMapRed || transferMapGreen || transferMapBlue);
  549. j = 0;
  550. elemsInThisChunk = width * FULL_CHUNK_HEIGHT * 4;
  551. for (i = 0; i < fullChunks; i++) {
  552. dest.set(src.subarray(srcPos, srcPos + elemsInThisChunk));
  553. srcPos += elemsInThisChunk;
  554. if (hasTransferMaps) {
  555. for (let k = 0; k < elemsInThisChunk; k += 4) {
  556. if (transferMapRed) {
  557. dest[k + 0] = transferMapRed[dest[k + 0]];
  558. }
  559. if (transferMapGreen) {
  560. dest[k + 1] = transferMapGreen[dest[k + 1]];
  561. }
  562. if (transferMapBlue) {
  563. dest[k + 2] = transferMapBlue[dest[k + 2]];
  564. }
  565. }
  566. }
  567. ctx.putImageData(chunkImgData, 0, j);
  568. j += FULL_CHUNK_HEIGHT;
  569. }
  570. if (i < totalChunks) {
  571. elemsInThisChunk = width * partialChunkHeight * 4;
  572. dest.set(src.subarray(srcPos, srcPos + elemsInThisChunk));
  573. if (hasTransferMaps) {
  574. for (let k = 0; k < elemsInThisChunk; k += 4) {
  575. if (transferMapRed) {
  576. dest[k + 0] = transferMapRed[dest[k + 0]];
  577. }
  578. if (transferMapGreen) {
  579. dest[k + 1] = transferMapGreen[dest[k + 1]];
  580. }
  581. if (transferMapBlue) {
  582. dest[k + 2] = transferMapBlue[dest[k + 2]];
  583. }
  584. }
  585. }
  586. ctx.putImageData(chunkImgData, 0, j);
  587. }
  588. } else if (imgData.kind === _util.ImageKind.RGB_24BPP) {
  589. const hasTransferMaps = !!(transferMapRed || transferMapGreen || transferMapBlue);
  590. thisChunkHeight = FULL_CHUNK_HEIGHT;
  591. elemsInThisChunk = width * thisChunkHeight;
  592. for (i = 0; i < totalChunks; i++) {
  593. if (i >= fullChunks) {
  594. thisChunkHeight = partialChunkHeight;
  595. elemsInThisChunk = width * thisChunkHeight;
  596. }
  597. destPos = 0;
  598. for (j = elemsInThisChunk; j--;) {
  599. dest[destPos++] = src[srcPos++];
  600. dest[destPos++] = src[srcPos++];
  601. dest[destPos++] = src[srcPos++];
  602. dest[destPos++] = 255;
  603. }
  604. if (hasTransferMaps) {
  605. for (let k = 0; k < destPos; k += 4) {
  606. if (transferMapRed) {
  607. dest[k + 0] = transferMapRed[dest[k + 0]];
  608. }
  609. if (transferMapGreen) {
  610. dest[k + 1] = transferMapGreen[dest[k + 1]];
  611. }
  612. if (transferMapBlue) {
  613. dest[k + 2] = transferMapBlue[dest[k + 2]];
  614. }
  615. }
  616. }
  617. ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
  618. }
  619. } else {
  620. throw new Error(`bad image kind: ${imgData.kind}`);
  621. }
  622. }
  623. function putBinaryImageMask(ctx, imgData) {
  624. const height = imgData.height,
  625. width = imgData.width;
  626. const partialChunkHeight = height % FULL_CHUNK_HEIGHT;
  627. const fullChunks = (height - partialChunkHeight) / FULL_CHUNK_HEIGHT;
  628. const totalChunks = partialChunkHeight === 0 ? fullChunks : fullChunks + 1;
  629. const chunkImgData = ctx.createImageData(width, FULL_CHUNK_HEIGHT);
  630. let srcPos = 0;
  631. const src = imgData.data;
  632. const dest = chunkImgData.data;
  633. for (let i = 0; i < totalChunks; i++) {
  634. const thisChunkHeight = i < fullChunks ? FULL_CHUNK_HEIGHT : partialChunkHeight;
  635. let destPos = 3;
  636. for (let j = 0; j < thisChunkHeight; j++) {
  637. let elem,
  638. mask = 0;
  639. for (let k = 0; k < width; k++) {
  640. if (!mask) {
  641. elem = src[srcPos++];
  642. mask = 128;
  643. }
  644. dest[destPos] = elem & mask ? 0 : 255;
  645. destPos += 4;
  646. mask >>= 1;
  647. }
  648. }
  649. ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
  650. }
  651. }
  652. function copyCtxState(sourceCtx, destCtx) {
  653. const properties = ["strokeStyle", "fillStyle", "fillRule", "globalAlpha", "lineWidth", "lineCap", "lineJoin", "miterLimit", "globalCompositeOperation", "font"];
  654. for (let i = 0, ii = properties.length; i < ii; i++) {
  655. const property = properties[i];
  656. if (sourceCtx[property] !== undefined) {
  657. destCtx[property] = sourceCtx[property];
  658. }
  659. }
  660. if (sourceCtx.setLineDash !== undefined) {
  661. destCtx.setLineDash(sourceCtx.getLineDash());
  662. destCtx.lineDashOffset = sourceCtx.lineDashOffset;
  663. }
  664. }
  665. function resetCtxToDefault(ctx) {
  666. ctx.strokeStyle = "#000000";
  667. ctx.fillStyle = "#000000";
  668. ctx.fillRule = "nonzero";
  669. ctx.globalAlpha = 1;
  670. ctx.lineWidth = 1;
  671. ctx.lineCap = "butt";
  672. ctx.lineJoin = "miter";
  673. ctx.miterLimit = 10;
  674. ctx.globalCompositeOperation = "source-over";
  675. ctx.font = "10px sans-serif";
  676. if (ctx.setLineDash !== undefined) {
  677. ctx.setLineDash([]);
  678. ctx.lineDashOffset = 0;
  679. }
  680. }
  681. function composeSMaskBackdrop(bytes, r0, g0, b0) {
  682. const length = bytes.length;
  683. for (let i = 3; i < length; i += 4) {
  684. const alpha = bytes[i];
  685. if (alpha === 0) {
  686. bytes[i - 3] = r0;
  687. bytes[i - 2] = g0;
  688. bytes[i - 1] = b0;
  689. } else if (alpha < 255) {
  690. const alpha_ = 255 - alpha;
  691. bytes[i - 3] = bytes[i - 3] * alpha + r0 * alpha_ >> 8;
  692. bytes[i - 2] = bytes[i - 2] * alpha + g0 * alpha_ >> 8;
  693. bytes[i - 1] = bytes[i - 1] * alpha + b0 * alpha_ >> 8;
  694. }
  695. }
  696. }
  697. function composeSMaskAlpha(maskData, layerData, transferMap) {
  698. const length = maskData.length;
  699. const scale = 1 / 255;
  700. for (let i = 3; i < length; i += 4) {
  701. const alpha = transferMap ? transferMap[maskData[i]] : maskData[i];
  702. layerData[i] = layerData[i] * alpha * scale | 0;
  703. }
  704. }
  705. function composeSMaskLuminosity(maskData, layerData, transferMap) {
  706. const length = maskData.length;
  707. for (let i = 3; i < length; i += 4) {
  708. const y = maskData[i - 3] * 77 + maskData[i - 2] * 152 + maskData[i - 1] * 28;
  709. layerData[i] = transferMap ? layerData[i] * transferMap[y >> 8] >> 8 : layerData[i] * y >> 16;
  710. }
  711. }
  712. function genericComposeSMask(maskCtx, layerCtx, width, height, subtype, backdrop, transferMap, layerOffsetX, layerOffsetY, maskOffsetX, maskOffsetY) {
  713. const hasBackdrop = !!backdrop;
  714. const r0 = hasBackdrop ? backdrop[0] : 0;
  715. const g0 = hasBackdrop ? backdrop[1] : 0;
  716. const b0 = hasBackdrop ? backdrop[2] : 0;
  717. let composeFn;
  718. if (subtype === "Luminosity") {
  719. composeFn = composeSMaskLuminosity;
  720. } else {
  721. composeFn = composeSMaskAlpha;
  722. }
  723. const PIXELS_TO_PROCESS = 1048576;
  724. const chunkSize = Math.min(height, Math.ceil(PIXELS_TO_PROCESS / width));
  725. for (let row = 0; row < height; row += chunkSize) {
  726. const chunkHeight = Math.min(chunkSize, height - row);
  727. const maskData = maskCtx.getImageData(layerOffsetX - maskOffsetX, row + (layerOffsetY - maskOffsetY), width, chunkHeight);
  728. const layerData = layerCtx.getImageData(layerOffsetX, row + layerOffsetY, width, chunkHeight);
  729. if (hasBackdrop) {
  730. composeSMaskBackdrop(maskData.data, r0, g0, b0);
  731. }
  732. composeFn(maskData.data, layerData.data, transferMap);
  733. layerCtx.putImageData(layerData, layerOffsetX, row + layerOffsetY);
  734. }
  735. }
  736. function composeSMask(ctx, smask, layerCtx, layerBox) {
  737. const layerOffsetX = layerBox[0];
  738. const layerOffsetY = layerBox[1];
  739. const layerWidth = layerBox[2] - layerOffsetX;
  740. const layerHeight = layerBox[3] - layerOffsetY;
  741. if (layerWidth === 0 || layerHeight === 0) {
  742. return;
  743. }
  744. genericComposeSMask(smask.context, layerCtx, layerWidth, layerHeight, smask.subtype, smask.backdrop, smask.transferMap, layerOffsetX, layerOffsetY, smask.offsetX, smask.offsetY);
  745. ctx.save();
  746. ctx.globalAlpha = 1;
  747. ctx.globalCompositeOperation = "source-over";
  748. ctx.setTransform(1, 0, 0, 1, 0, 0);
  749. ctx.drawImage(layerCtx.canvas, 0, 0);
  750. ctx.restore();
  751. }
  752. function getImageSmoothingEnabled(transform, interpolate) {
  753. const scale = _util.Util.singularValueDecompose2dScale(transform);
  754. scale[0] = Math.fround(scale[0]);
  755. scale[1] = Math.fround(scale[1]);
  756. const actualScale = Math.fround((globalThis.devicePixelRatio || 1) * _display_utils.PixelsPerInch.PDF_TO_CSS_UNITS);
  757. if (interpolate !== undefined) {
  758. return interpolate;
  759. } else if (scale[0] <= actualScale || scale[1] <= actualScale) {
  760. return true;
  761. }
  762. return false;
  763. }
  764. const LINE_CAP_STYLES = ["butt", "round", "square"];
  765. const LINE_JOIN_STYLES = ["miter", "round", "bevel"];
  766. const NORMAL_CLIP = {};
  767. const EO_CLIP = {};
  768. class CanvasGraphics {
  769. constructor(canvasCtx, commonObjs, objs, canvasFactory, imageLayer, optionalContentConfig, annotationCanvasMap) {
  770. this.ctx = canvasCtx;
  771. this.current = new CanvasExtraState(this.ctx.canvas.width, this.ctx.canvas.height);
  772. this.stateStack = [];
  773. this.pendingClip = null;
  774. this.pendingEOFill = false;
  775. this.res = null;
  776. this.xobjs = null;
  777. this.commonObjs = commonObjs;
  778. this.objs = objs;
  779. this.canvasFactory = canvasFactory;
  780. this.imageLayer = imageLayer;
  781. this.groupStack = [];
  782. this.processingType3 = null;
  783. this.baseTransform = null;
  784. this.baseTransformStack = [];
  785. this.groupLevel = 0;
  786. this.smaskStack = [];
  787. this.smaskCounter = 0;
  788. this.tempSMask = null;
  789. this.suspendedCtx = null;
  790. this.contentVisible = true;
  791. this.markedContentStack = [];
  792. this.optionalContentConfig = optionalContentConfig;
  793. this.cachedCanvases = new CachedCanvases(this.canvasFactory);
  794. this.cachedPatterns = new Map();
  795. this.annotationCanvasMap = annotationCanvasMap;
  796. this.viewportScale = 1;
  797. this.outputScaleX = 1;
  798. this.outputScaleY = 1;
  799. if (canvasCtx) {
  800. addContextCurrentTransform(canvasCtx);
  801. }
  802. this._cachedGetSinglePixelWidth = null;
  803. }
  804. beginDrawing({
  805. transform,
  806. viewport,
  807. transparency = false,
  808. background = null
  809. }) {
  810. const width = this.ctx.canvas.width;
  811. const height = this.ctx.canvas.height;
  812. this.ctx.save();
  813. this.ctx.fillStyle = background || "rgb(255, 255, 255)";
  814. this.ctx.fillRect(0, 0, width, height);
  815. this.ctx.restore();
  816. if (transparency) {
  817. const transparentCanvas = this.cachedCanvases.getCanvas("transparent", width, height, true);
  818. this.compositeCtx = this.ctx;
  819. this.transparentCanvas = transparentCanvas.canvas;
  820. this.ctx = transparentCanvas.context;
  821. this.ctx.save();
  822. this.ctx.transform.apply(this.ctx, this.compositeCtx.mozCurrentTransform);
  823. }
  824. this.ctx.save();
  825. resetCtxToDefault(this.ctx);
  826. if (transform) {
  827. this.ctx.transform.apply(this.ctx, transform);
  828. this.outputScaleX = transform[0];
  829. this.outputScaleY = transform[0];
  830. }
  831. this.ctx.transform.apply(this.ctx, viewport.transform);
  832. this.viewportScale = viewport.scale;
  833. this.baseTransform = this.ctx.mozCurrentTransform.slice();
  834. this._combinedScaleFactor = Math.hypot(this.baseTransform[0], this.baseTransform[2]);
  835. if (this.imageLayer) {
  836. this.imageLayer.beginLayout();
  837. }
  838. }
  839. executeOperatorList(operatorList, executionStartIdx, continueCallback, stepper) {
  840. const argsArray = operatorList.argsArray;
  841. const fnArray = operatorList.fnArray;
  842. let i = executionStartIdx || 0;
  843. const argsArrayLen = argsArray.length;
  844. if (argsArrayLen === i) {
  845. return i;
  846. }
  847. const chunkOperations = argsArrayLen - i > EXECUTION_STEPS && typeof continueCallback === "function";
  848. const endTime = chunkOperations ? Date.now() + EXECUTION_TIME : 0;
  849. let steps = 0;
  850. const commonObjs = this.commonObjs;
  851. const objs = this.objs;
  852. let fnId;
  853. while (true) {
  854. if (stepper !== undefined && i === stepper.nextBreakPoint) {
  855. stepper.breakIt(i, continueCallback);
  856. return i;
  857. }
  858. fnId = fnArray[i];
  859. if (fnId !== _util.OPS.dependency) {
  860. this[fnId].apply(this, argsArray[i]);
  861. } else {
  862. for (const depObjId of argsArray[i]) {
  863. const objsPool = depObjId.startsWith("g_") ? commonObjs : objs;
  864. if (!objsPool.has(depObjId)) {
  865. objsPool.get(depObjId, continueCallback);
  866. return i;
  867. }
  868. }
  869. }
  870. i++;
  871. if (i === argsArrayLen) {
  872. return i;
  873. }
  874. if (chunkOperations && ++steps > EXECUTION_STEPS) {
  875. if (Date.now() > endTime) {
  876. continueCallback();
  877. return i;
  878. }
  879. steps = 0;
  880. }
  881. }
  882. }
  883. endDrawing() {
  884. while (this.stateStack.length || this.current.activeSMask !== null) {
  885. this.restore();
  886. }
  887. this.ctx.restore();
  888. if (this.transparentCanvas) {
  889. this.ctx = this.compositeCtx;
  890. this.ctx.save();
  891. this.ctx.setTransform(1, 0, 0, 1, 0, 0);
  892. this.ctx.drawImage(this.transparentCanvas, 0, 0);
  893. this.ctx.restore();
  894. this.transparentCanvas = null;
  895. }
  896. this.cachedCanvases.clear();
  897. this.cachedPatterns.clear();
  898. if (this.imageLayer) {
  899. this.imageLayer.endLayout();
  900. }
  901. }
  902. _scaleImage(img, inverseTransform) {
  903. const width = img.width;
  904. const height = img.height;
  905. let widthScale = Math.max(Math.hypot(inverseTransform[0], inverseTransform[1]), 1);
  906. let heightScale = Math.max(Math.hypot(inverseTransform[2], inverseTransform[3]), 1);
  907. let paintWidth = width,
  908. paintHeight = height;
  909. let tmpCanvasId = "prescale1";
  910. let tmpCanvas, tmpCtx;
  911. while (widthScale > 2 && paintWidth > 1 || heightScale > 2 && paintHeight > 1) {
  912. let newWidth = paintWidth,
  913. newHeight = paintHeight;
  914. if (widthScale > 2 && paintWidth > 1) {
  915. newWidth = Math.ceil(paintWidth / 2);
  916. widthScale /= paintWidth / newWidth;
  917. }
  918. if (heightScale > 2 && paintHeight > 1) {
  919. newHeight = Math.ceil(paintHeight / 2);
  920. heightScale /= paintHeight / newHeight;
  921. }
  922. tmpCanvas = this.cachedCanvases.getCanvas(tmpCanvasId, newWidth, newHeight);
  923. tmpCtx = tmpCanvas.context;
  924. tmpCtx.clearRect(0, 0, newWidth, newHeight);
  925. tmpCtx.drawImage(img, 0, 0, paintWidth, paintHeight, 0, 0, newWidth, newHeight);
  926. img = tmpCanvas.canvas;
  927. paintWidth = newWidth;
  928. paintHeight = newHeight;
  929. tmpCanvasId = tmpCanvasId === "prescale1" ? "prescale2" : "prescale1";
  930. }
  931. return {
  932. img,
  933. paintWidth,
  934. paintHeight
  935. };
  936. }
  937. _createMaskCanvas(img) {
  938. const ctx = this.ctx;
  939. const width = img.width,
  940. height = img.height;
  941. const fillColor = this.current.fillColor;
  942. const isPatternFill = this.current.patternFill;
  943. const maskCanvas = this.cachedCanvases.getCanvas("maskCanvas", width, height);
  944. const maskCtx = maskCanvas.context;
  945. putBinaryImageMask(maskCtx, img);
  946. const objToCanvas = ctx.mozCurrentTransform;
  947. let maskToCanvas = _util.Util.transform(objToCanvas, [1 / width, 0, 0, -1 / height, 0, 0]);
  948. maskToCanvas = _util.Util.transform(maskToCanvas, [1, 0, 0, 1, 0, -height]);
  949. const cord1 = _util.Util.applyTransform([0, 0], maskToCanvas);
  950. const cord2 = _util.Util.applyTransform([width, height], maskToCanvas);
  951. const rect = _util.Util.normalizeRect([cord1[0], cord1[1], cord2[0], cord2[1]]);
  952. const drawnWidth = Math.ceil(rect[2] - rect[0]);
  953. const drawnHeight = Math.ceil(rect[3] - rect[1]);
  954. const fillCanvas = this.cachedCanvases.getCanvas("fillCanvas", drawnWidth, drawnHeight, true);
  955. const fillCtx = fillCanvas.context;
  956. const offsetX = Math.min(cord1[0], cord2[0]);
  957. const offsetY = Math.min(cord1[1], cord2[1]);
  958. fillCtx.translate(-offsetX, -offsetY);
  959. fillCtx.transform.apply(fillCtx, maskToCanvas);
  960. const scaled = this._scaleImage(maskCanvas.canvas, fillCtx.mozCurrentTransformInverse);
  961. fillCtx.imageSmoothingEnabled = getImageSmoothingEnabled(fillCtx.mozCurrentTransform, img.interpolate);
  962. fillCtx.drawImage(scaled.img, 0, 0, scaled.img.width, scaled.img.height, 0, 0, width, height);
  963. fillCtx.globalCompositeOperation = "source-in";
  964. const inverse = _util.Util.transform(fillCtx.mozCurrentTransformInverse, [1, 0, 0, 1, -offsetX, -offsetY]);
  965. fillCtx.fillStyle = isPatternFill ? fillColor.getPattern(ctx, this, inverse, _pattern_helper.PathType.FILL) : fillColor;
  966. fillCtx.fillRect(0, 0, width, height);
  967. return {
  968. canvas: fillCanvas.canvas,
  969. offsetX: Math.round(offsetX),
  970. offsetY: Math.round(offsetY)
  971. };
  972. }
  973. setLineWidth(width) {
  974. this.current.lineWidth = width;
  975. this.ctx.lineWidth = width;
  976. }
  977. setLineCap(style) {
  978. this.ctx.lineCap = LINE_CAP_STYLES[style];
  979. }
  980. setLineJoin(style) {
  981. this.ctx.lineJoin = LINE_JOIN_STYLES[style];
  982. }
  983. setMiterLimit(limit) {
  984. this.ctx.miterLimit = limit;
  985. }
  986. setDash(dashArray, dashPhase) {
  987. const ctx = this.ctx;
  988. if (ctx.setLineDash !== undefined) {
  989. ctx.setLineDash(dashArray);
  990. ctx.lineDashOffset = dashPhase;
  991. }
  992. }
  993. setRenderingIntent(intent) {}
  994. setFlatness(flatness) {}
  995. setGState(states) {
  996. for (let i = 0, ii = states.length; i < ii; i++) {
  997. const state = states[i];
  998. const key = state[0];
  999. const value = state[1];
  1000. switch (key) {
  1001. case "LW":
  1002. this.setLineWidth(value);
  1003. break;
  1004. case "LC":
  1005. this.setLineCap(value);
  1006. break;
  1007. case "LJ":
  1008. this.setLineJoin(value);
  1009. break;
  1010. case "ML":
  1011. this.setMiterLimit(value);
  1012. break;
  1013. case "D":
  1014. this.setDash(value[0], value[1]);
  1015. break;
  1016. case "RI":
  1017. this.setRenderingIntent(value);
  1018. break;
  1019. case "FL":
  1020. this.setFlatness(value);
  1021. break;
  1022. case "Font":
  1023. this.setFont(value[0], value[1]);
  1024. break;
  1025. case "CA":
  1026. this.current.strokeAlpha = state[1];
  1027. break;
  1028. case "ca":
  1029. this.current.fillAlpha = state[1];
  1030. this.ctx.globalAlpha = state[1];
  1031. break;
  1032. case "BM":
  1033. this.ctx.globalCompositeOperation = value;
  1034. break;
  1035. case "SMask":
  1036. this.current.activeSMask = value ? this.tempSMask : null;
  1037. this.tempSMask = null;
  1038. this.checkSMaskState();
  1039. break;
  1040. case "TR":
  1041. this.current.transferMaps = value;
  1042. }
  1043. }
  1044. }
  1045. checkSMaskState() {
  1046. const inSMaskMode = !!this.suspendedCtx;
  1047. if (this.current.activeSMask && !inSMaskMode) {
  1048. this.beginSMaskMode();
  1049. } else if (!this.current.activeSMask && inSMaskMode) {
  1050. this.endSMaskMode();
  1051. }
  1052. }
  1053. beginSMaskMode() {
  1054. if (this.suspendedCtx) {
  1055. throw new Error("beginSMaskMode called while already in smask mode");
  1056. }
  1057. const drawnWidth = this.ctx.canvas.width;
  1058. const drawnHeight = this.ctx.canvas.height;
  1059. const cacheId = "smaskGroupAt" + this.groupLevel;
  1060. const scratchCanvas = this.cachedCanvases.getCanvas(cacheId, drawnWidth, drawnHeight, true);
  1061. this.suspendedCtx = this.ctx;
  1062. this.ctx = scratchCanvas.context;
  1063. const ctx = this.ctx;
  1064. ctx.setTransform.apply(ctx, this.suspendedCtx.mozCurrentTransform);
  1065. copyCtxState(this.suspendedCtx, ctx);
  1066. mirrorContextOperations(ctx, this.suspendedCtx);
  1067. this.setGState([["BM", "source-over"], ["ca", 1], ["CA", 1]]);
  1068. }
  1069. endSMaskMode() {
  1070. if (!this.suspendedCtx) {
  1071. throw new Error("endSMaskMode called while not in smask mode");
  1072. }
  1073. this.ctx._removeMirroring();
  1074. copyCtxState(this.ctx, this.suspendedCtx);
  1075. this.ctx = this.suspendedCtx;
  1076. this.current.activeSMask = null;
  1077. this.suspendedCtx = null;
  1078. }
  1079. compose(dirtyBox) {
  1080. if (!this.current.activeSMask) {
  1081. return;
  1082. }
  1083. if (!dirtyBox) {
  1084. dirtyBox = [0, 0, this.ctx.canvas.width, this.ctx.canvas.height];
  1085. } else {
  1086. dirtyBox[0] = Math.floor(dirtyBox[0]);
  1087. dirtyBox[1] = Math.floor(dirtyBox[1]);
  1088. dirtyBox[2] = Math.ceil(dirtyBox[2]);
  1089. dirtyBox[3] = Math.ceil(dirtyBox[3]);
  1090. }
  1091. const smask = this.current.activeSMask;
  1092. const suspendedCtx = this.suspendedCtx;
  1093. composeSMask(suspendedCtx, smask, this.ctx, dirtyBox);
  1094. this.ctx.save();
  1095. this.ctx.setTransform(1, 0, 0, 1, 0, 0);
  1096. this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
  1097. this.ctx.restore();
  1098. }
  1099. save() {
  1100. this.ctx.save();
  1101. const old = this.current;
  1102. this.stateStack.push(old);
  1103. this.current = old.clone();
  1104. }
  1105. restore() {
  1106. if (this.stateStack.length === 0 && this.current.activeSMask) {
  1107. this.endSMaskMode();
  1108. }
  1109. if (this.stateStack.length !== 0) {
  1110. this.current = this.stateStack.pop();
  1111. this.ctx.restore();
  1112. this.checkSMaskState();
  1113. this.pendingClip = null;
  1114. this._cachedGetSinglePixelWidth = null;
  1115. }
  1116. }
  1117. transform(a, b, c, d, e, f) {
  1118. this.ctx.transform(a, b, c, d, e, f);
  1119. this._cachedGetSinglePixelWidth = null;
  1120. }
  1121. constructPath(ops, args) {
  1122. const ctx = this.ctx;
  1123. const current = this.current;
  1124. let x = current.x,
  1125. y = current.y;
  1126. let startX, startY;
  1127. for (let i = 0, j = 0, ii = ops.length; i < ii; i++) {
  1128. switch (ops[i] | 0) {
  1129. case _util.OPS.rectangle:
  1130. x = args[j++];
  1131. y = args[j++];
  1132. const width = args[j++];
  1133. const height = args[j++];
  1134. const xw = x + width;
  1135. const yh = y + height;
  1136. ctx.moveTo(x, y);
  1137. if (width === 0 || height === 0) {
  1138. ctx.lineTo(xw, yh);
  1139. } else {
  1140. ctx.lineTo(xw, y);
  1141. ctx.lineTo(xw, yh);
  1142. ctx.lineTo(x, yh);
  1143. }
  1144. current.updatePathMinMax(ctx.mozCurrentTransform, x, y);
  1145. current.updatePathMinMax(ctx.mozCurrentTransform, xw, yh);
  1146. ctx.closePath();
  1147. break;
  1148. case _util.OPS.moveTo:
  1149. x = args[j++];
  1150. y = args[j++];
  1151. ctx.moveTo(x, y);
  1152. current.updatePathMinMax(ctx.mozCurrentTransform, x, y);
  1153. break;
  1154. case _util.OPS.lineTo:
  1155. x = args[j++];
  1156. y = args[j++];
  1157. ctx.lineTo(x, y);
  1158. current.updatePathMinMax(ctx.mozCurrentTransform, x, y);
  1159. break;
  1160. case _util.OPS.curveTo:
  1161. startX = x;
  1162. startY = y;
  1163. x = args[j + 4];
  1164. y = args[j + 5];
  1165. ctx.bezierCurveTo(args[j], args[j + 1], args[j + 2], args[j + 3], x, y);
  1166. current.updateCurvePathMinMax(ctx.mozCurrentTransform, startX, startY, args[j], args[j + 1], args[j + 2], args[j + 3], x, y);
  1167. j += 6;
  1168. break;
  1169. case _util.OPS.curveTo2:
  1170. startX = x;
  1171. startY = y;
  1172. ctx.bezierCurveTo(x, y, args[j], args[j + 1], args[j + 2], args[j + 3]);
  1173. current.updateCurvePathMinMax(ctx.mozCurrentTransform, startX, startY, x, y, args[j], args[j + 1], args[j + 2], args[j + 3]);
  1174. x = args[j + 2];
  1175. y = args[j + 3];
  1176. j += 4;
  1177. break;
  1178. case _util.OPS.curveTo3:
  1179. startX = x;
  1180. startY = y;
  1181. x = args[j + 2];
  1182. y = args[j + 3];
  1183. ctx.bezierCurveTo(args[j], args[j + 1], x, y, x, y);
  1184. current.updateCurvePathMinMax(ctx.mozCurrentTransform, startX, startY, args[j], args[j + 1], x, y, x, y);
  1185. j += 4;
  1186. break;
  1187. case _util.OPS.closePath:
  1188. ctx.closePath();
  1189. break;
  1190. }
  1191. }
  1192. current.setCurrentPoint(x, y);
  1193. }
  1194. closePath() {
  1195. this.ctx.closePath();
  1196. }
  1197. stroke(consumePath) {
  1198. consumePath = typeof consumePath !== "undefined" ? consumePath : true;
  1199. const ctx = this.ctx;
  1200. const strokeColor = this.current.strokeColor;
  1201. ctx.globalAlpha = this.current.strokeAlpha;
  1202. if (this.contentVisible) {
  1203. if (typeof strokeColor === "object" && strokeColor?.getPattern) {
  1204. const lineWidth = this.getSinglePixelWidth();
  1205. ctx.save();
  1206. ctx.strokeStyle = strokeColor.getPattern(ctx, this, ctx.mozCurrentTransformInverse, _pattern_helper.PathType.STROKE);
  1207. ctx.lineWidth = Math.max(lineWidth, this.current.lineWidth);
  1208. ctx.stroke();
  1209. ctx.restore();
  1210. } else {
  1211. const lineWidth = this.getSinglePixelWidth();
  1212. if (lineWidth < 0 && -lineWidth >= this.current.lineWidth) {
  1213. ctx.save();
  1214. ctx.resetTransform();
  1215. ctx.lineWidth = Math.round(this._combinedScaleFactor);
  1216. ctx.stroke();
  1217. ctx.restore();
  1218. } else {
  1219. ctx.lineWidth = Math.max(lineWidth, this.current.lineWidth);
  1220. ctx.stroke();
  1221. }
  1222. }
  1223. }
  1224. if (consumePath) {
  1225. this.consumePath(this.current.getClippedPathBoundingBox());
  1226. }
  1227. ctx.globalAlpha = this.current.fillAlpha;
  1228. }
  1229. closeStroke() {
  1230. this.closePath();
  1231. this.stroke();
  1232. }
  1233. fill(consumePath) {
  1234. consumePath = typeof consumePath !== "undefined" ? consumePath : true;
  1235. const ctx = this.ctx;
  1236. const fillColor = this.current.fillColor;
  1237. const isPatternFill = this.current.patternFill;
  1238. let needRestore = false;
  1239. if (isPatternFill) {
  1240. ctx.save();
  1241. ctx.fillStyle = fillColor.getPattern(ctx, this, ctx.mozCurrentTransformInverse, _pattern_helper.PathType.FILL);
  1242. needRestore = true;
  1243. }
  1244. const intersect = this.current.getClippedPathBoundingBox();
  1245. if (this.contentVisible && intersect !== null) {
  1246. if (this.pendingEOFill) {
  1247. ctx.fill("evenodd");
  1248. this.pendingEOFill = false;
  1249. } else {
  1250. ctx.fill();
  1251. }
  1252. }
  1253. if (needRestore) {
  1254. ctx.restore();
  1255. }
  1256. if (consumePath) {
  1257. this.consumePath(intersect);
  1258. }
  1259. }
  1260. eoFill() {
  1261. this.pendingEOFill = true;
  1262. this.fill();
  1263. }
  1264. fillStroke() {
  1265. this.fill(false);
  1266. this.stroke(false);
  1267. this.consumePath();
  1268. }
  1269. eoFillStroke() {
  1270. this.pendingEOFill = true;
  1271. this.fillStroke();
  1272. }
  1273. closeFillStroke() {
  1274. this.closePath();
  1275. this.fillStroke();
  1276. }
  1277. closeEOFillStroke() {
  1278. this.pendingEOFill = true;
  1279. this.closePath();
  1280. this.fillStroke();
  1281. }
  1282. endPath() {
  1283. this.consumePath();
  1284. }
  1285. clip() {
  1286. this.pendingClip = NORMAL_CLIP;
  1287. }
  1288. eoClip() {
  1289. this.pendingClip = EO_CLIP;
  1290. }
  1291. beginText() {
  1292. this.current.textMatrix = _util.IDENTITY_MATRIX;
  1293. this.current.textMatrixScale = 1;
  1294. this.current.x = this.current.lineX = 0;
  1295. this.current.y = this.current.lineY = 0;
  1296. }
  1297. endText() {
  1298. const paths = this.pendingTextPaths;
  1299. const ctx = this.ctx;
  1300. if (paths === undefined) {
  1301. ctx.beginPath();
  1302. return;
  1303. }
  1304. ctx.save();
  1305. ctx.beginPath();
  1306. for (let i = 0; i < paths.length; i++) {
  1307. const path = paths[i];
  1308. ctx.setTransform.apply(ctx, path.transform);
  1309. ctx.translate(path.x, path.y);
  1310. path.addToPath(ctx, path.fontSize);
  1311. }
  1312. ctx.restore();
  1313. ctx.clip();
  1314. ctx.beginPath();
  1315. delete this.pendingTextPaths;
  1316. }
  1317. setCharSpacing(spacing) {
  1318. this.current.charSpacing = spacing;
  1319. }
  1320. setWordSpacing(spacing) {
  1321. this.current.wordSpacing = spacing;
  1322. }
  1323. setHScale(scale) {
  1324. this.current.textHScale = scale / 100;
  1325. }
  1326. setLeading(leading) {
  1327. this.current.leading = -leading;
  1328. }
  1329. setFont(fontRefName, size) {
  1330. const fontObj = this.commonObjs.get(fontRefName);
  1331. const current = this.current;
  1332. if (!fontObj) {
  1333. throw new Error(`Can't find font for ${fontRefName}`);
  1334. }
  1335. current.fontMatrix = fontObj.fontMatrix || _util.FONT_IDENTITY_MATRIX;
  1336. if (current.fontMatrix[0] === 0 || current.fontMatrix[3] === 0) {
  1337. (0, _util.warn)("Invalid font matrix for font " + fontRefName);
  1338. }
  1339. if (size < 0) {
  1340. size = -size;
  1341. current.fontDirection = -1;
  1342. } else {
  1343. current.fontDirection = 1;
  1344. }
  1345. this.current.font = fontObj;
  1346. this.current.fontSize = size;
  1347. if (fontObj.isType3Font) {
  1348. return;
  1349. }
  1350. const name = fontObj.loadedName || "sans-serif";
  1351. let bold = "normal";
  1352. if (fontObj.black) {
  1353. bold = "900";
  1354. } else if (fontObj.bold) {
  1355. bold = "bold";
  1356. }
  1357. const italic = fontObj.italic ? "italic" : "normal";
  1358. const typeface = `"${name}", ${fontObj.fallbackName}`;
  1359. let browserFontSize = size;
  1360. if (size < MIN_FONT_SIZE) {
  1361. browserFontSize = MIN_FONT_SIZE;
  1362. } else if (size > MAX_FONT_SIZE) {
  1363. browserFontSize = MAX_FONT_SIZE;
  1364. }
  1365. this.current.fontSizeScale = size / browserFontSize;
  1366. this.ctx.font = `${italic} ${bold} ${browserFontSize}px ${typeface}`;
  1367. }
  1368. setTextRenderingMode(mode) {
  1369. this.current.textRenderingMode = mode;
  1370. }
  1371. setTextRise(rise) {
  1372. this.current.textRise = rise;
  1373. }
  1374. moveText(x, y) {
  1375. this.current.x = this.current.lineX += x;
  1376. this.current.y = this.current.lineY += y;
  1377. }
  1378. setLeadingMoveText(x, y) {
  1379. this.setLeading(-y);
  1380. this.moveText(x, y);
  1381. }
  1382. setTextMatrix(a, b, c, d, e, f) {
  1383. this.current.textMatrix = [a, b, c, d, e, f];
  1384. this.current.textMatrixScale = Math.hypot(a, b);
  1385. this.current.x = this.current.lineX = 0;
  1386. this.current.y = this.current.lineY = 0;
  1387. }
  1388. nextLine() {
  1389. this.moveText(0, this.current.leading);
  1390. }
  1391. paintChar(character, x, y, patternTransform, resetLineWidthToOne) {
  1392. const ctx = this.ctx;
  1393. const current = this.current;
  1394. const font = current.font;
  1395. const textRenderingMode = current.textRenderingMode;
  1396. const fontSize = current.fontSize / current.fontSizeScale;
  1397. const fillStrokeMode = textRenderingMode & _util.TextRenderingMode.FILL_STROKE_MASK;
  1398. const isAddToPathSet = !!(textRenderingMode & _util.TextRenderingMode.ADD_TO_PATH_FLAG);
  1399. const patternFill = current.patternFill && !font.missingFile;
  1400. let addToPath;
  1401. if (font.disableFontFace || isAddToPathSet || patternFill) {
  1402. addToPath = font.getPathGenerator(this.commonObjs, character);
  1403. }
  1404. if (font.disableFontFace || patternFill) {
  1405. ctx.save();
  1406. ctx.translate(x, y);
  1407. ctx.beginPath();
  1408. addToPath(ctx, fontSize);
  1409. if (patternTransform) {
  1410. ctx.setTransform.apply(ctx, patternTransform);
  1411. }
  1412. if (fillStrokeMode === _util.TextRenderingMode.FILL || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
  1413. ctx.fill();
  1414. }
  1415. if (fillStrokeMode === _util.TextRenderingMode.STROKE || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
  1416. if (resetLineWidthToOne) {
  1417. ctx.resetTransform();
  1418. ctx.lineWidth = Math.round(this._combinedScaleFactor);
  1419. }
  1420. ctx.stroke();
  1421. }
  1422. ctx.restore();
  1423. } else {
  1424. if (fillStrokeMode === _util.TextRenderingMode.FILL || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
  1425. ctx.fillText(character, x, y);
  1426. }
  1427. if (fillStrokeMode === _util.TextRenderingMode.STROKE || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
  1428. if (resetLineWidthToOne) {
  1429. ctx.save();
  1430. ctx.moveTo(x, y);
  1431. ctx.resetTransform();
  1432. ctx.lineWidth = Math.round(this._combinedScaleFactor);
  1433. ctx.strokeText(character, 0, 0);
  1434. ctx.restore();
  1435. } else {
  1436. ctx.strokeText(character, x, y);
  1437. }
  1438. }
  1439. }
  1440. if (isAddToPathSet) {
  1441. const paths = this.pendingTextPaths || (this.pendingTextPaths = []);
  1442. paths.push({
  1443. transform: ctx.mozCurrentTransform,
  1444. x,
  1445. y,
  1446. fontSize,
  1447. addToPath
  1448. });
  1449. }
  1450. }
  1451. get isFontSubpixelAAEnabled() {
  1452. const {
  1453. context: ctx
  1454. } = this.cachedCanvases.getCanvas("isFontSubpixelAAEnabled", 10, 10);
  1455. ctx.scale(1.5, 1);
  1456. ctx.fillText("I", 0, 10);
  1457. const data = ctx.getImageData(0, 0, 10, 10).data;
  1458. let enabled = false;
  1459. for (let i = 3; i < data.length; i += 4) {
  1460. if (data[i] > 0 && data[i] < 255) {
  1461. enabled = true;
  1462. break;
  1463. }
  1464. }
  1465. return (0, _util.shadow)(this, "isFontSubpixelAAEnabled", enabled);
  1466. }
  1467. showText(glyphs) {
  1468. const current = this.current;
  1469. const font = current.font;
  1470. if (font.isType3Font) {
  1471. return this.showType3Text(glyphs);
  1472. }
  1473. const fontSize = current.fontSize;
  1474. if (fontSize === 0) {
  1475. return undefined;
  1476. }
  1477. const ctx = this.ctx;
  1478. const fontSizeScale = current.fontSizeScale;
  1479. const charSpacing = current.charSpacing;
  1480. const wordSpacing = current.wordSpacing;
  1481. const fontDirection = current.fontDirection;
  1482. const textHScale = current.textHScale * fontDirection;
  1483. const glyphsLength = glyphs.length;
  1484. const vertical = font.vertical;
  1485. const spacingDir = vertical ? 1 : -1;
  1486. const defaultVMetrics = font.defaultVMetrics;
  1487. const widthAdvanceScale = fontSize * current.fontMatrix[0];
  1488. const simpleFillText = current.textRenderingMode === _util.TextRenderingMode.FILL && !font.disableFontFace && !current.patternFill;
  1489. ctx.save();
  1490. ctx.transform.apply(ctx, current.textMatrix);
  1491. ctx.translate(current.x, current.y + current.textRise);
  1492. if (fontDirection > 0) {
  1493. ctx.scale(textHScale, -1);
  1494. } else {
  1495. ctx.scale(textHScale, 1);
  1496. }
  1497. let patternTransform;
  1498. if (current.patternFill) {
  1499. ctx.save();
  1500. const pattern = current.fillColor.getPattern(ctx, this, ctx.mozCurrentTransformInverse, _pattern_helper.PathType.FILL);
  1501. patternTransform = ctx.mozCurrentTransform;
  1502. ctx.restore();
  1503. ctx.fillStyle = pattern;
  1504. }
  1505. let lineWidth = current.lineWidth;
  1506. let resetLineWidthToOne = false;
  1507. const scale = current.textMatrixScale;
  1508. if (scale === 0 || lineWidth === 0) {
  1509. const fillStrokeMode = current.textRenderingMode & _util.TextRenderingMode.FILL_STROKE_MASK;
  1510. if (fillStrokeMode === _util.TextRenderingMode.STROKE || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
  1511. this._cachedGetSinglePixelWidth = null;
  1512. lineWidth = this.getSinglePixelWidth();
  1513. resetLineWidthToOne = lineWidth < 0;
  1514. }
  1515. } else {
  1516. lineWidth /= scale;
  1517. }
  1518. if (fontSizeScale !== 1.0) {
  1519. ctx.scale(fontSizeScale, fontSizeScale);
  1520. lineWidth /= fontSizeScale;
  1521. }
  1522. ctx.lineWidth = lineWidth;
  1523. let x = 0,
  1524. i;
  1525. for (i = 0; i < glyphsLength; ++i) {
  1526. const glyph = glyphs[i];
  1527. if ((0, _util.isNum)(glyph)) {
  1528. x += spacingDir * glyph * fontSize / 1000;
  1529. continue;
  1530. }
  1531. let restoreNeeded = false;
  1532. const spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
  1533. const character = glyph.fontChar;
  1534. const accent = glyph.accent;
  1535. let scaledX, scaledY;
  1536. let width = glyph.width;
  1537. if (vertical) {
  1538. const vmetric = glyph.vmetric || defaultVMetrics;
  1539. const vx = -(glyph.vmetric ? vmetric[1] : width * 0.5) * widthAdvanceScale;
  1540. const vy = vmetric[2] * widthAdvanceScale;
  1541. width = vmetric ? -vmetric[0] : width;
  1542. scaledX = vx / fontSizeScale;
  1543. scaledY = (x + vy) / fontSizeScale;
  1544. } else {
  1545. scaledX = x / fontSizeScale;
  1546. scaledY = 0;
  1547. }
  1548. if (font.remeasure && width > 0) {
  1549. const measuredWidth = ctx.measureText(character).width * 1000 / fontSize * fontSizeScale;
  1550. if (width < measuredWidth && this.isFontSubpixelAAEnabled) {
  1551. const characterScaleX = width / measuredWidth;
  1552. restoreNeeded = true;
  1553. ctx.save();
  1554. ctx.scale(characterScaleX, 1);
  1555. scaledX /= characterScaleX;
  1556. } else if (width !== measuredWidth) {
  1557. scaledX += (width - measuredWidth) / 2000 * fontSize / fontSizeScale;
  1558. }
  1559. }
  1560. if (this.contentVisible && (glyph.isInFont || font.missingFile)) {
  1561. if (simpleFillText && !accent) {
  1562. ctx.fillText(character, scaledX, scaledY);
  1563. } else {
  1564. this.paintChar(character, scaledX, scaledY, patternTransform, resetLineWidthToOne);
  1565. if (accent) {
  1566. const scaledAccentX = scaledX + fontSize * accent.offset.x / fontSizeScale;
  1567. const scaledAccentY = scaledY - fontSize * accent.offset.y / fontSizeScale;
  1568. this.paintChar(accent.fontChar, scaledAccentX, scaledAccentY, patternTransform, resetLineWidthToOne);
  1569. }
  1570. }
  1571. }
  1572. let charWidth;
  1573. if (vertical) {
  1574. charWidth = width * widthAdvanceScale - spacing * fontDirection;
  1575. } else {
  1576. charWidth = width * widthAdvanceScale + spacing * fontDirection;
  1577. }
  1578. x += charWidth;
  1579. if (restoreNeeded) {
  1580. ctx.restore();
  1581. }
  1582. }
  1583. if (vertical) {
  1584. current.y -= x;
  1585. } else {
  1586. current.x += x * textHScale;
  1587. }
  1588. ctx.restore();
  1589. this.compose();
  1590. return undefined;
  1591. }
  1592. showType3Text(glyphs) {
  1593. const ctx = this.ctx;
  1594. const current = this.current;
  1595. const font = current.font;
  1596. const fontSize = current.fontSize;
  1597. const fontDirection = current.fontDirection;
  1598. const spacingDir = font.vertical ? 1 : -1;
  1599. const charSpacing = current.charSpacing;
  1600. const wordSpacing = current.wordSpacing;
  1601. const textHScale = current.textHScale * fontDirection;
  1602. const fontMatrix = current.fontMatrix || _util.FONT_IDENTITY_MATRIX;
  1603. const glyphsLength = glyphs.length;
  1604. const isTextInvisible = current.textRenderingMode === _util.TextRenderingMode.INVISIBLE;
  1605. let i, glyph, width, spacingLength;
  1606. if (isTextInvisible || fontSize === 0) {
  1607. return;
  1608. }
  1609. this._cachedGetSinglePixelWidth = null;
  1610. ctx.save();
  1611. ctx.transform.apply(ctx, current.textMatrix);
  1612. ctx.translate(current.x, current.y);
  1613. ctx.scale(textHScale, fontDirection);
  1614. for (i = 0; i < glyphsLength; ++i) {
  1615. glyph = glyphs[i];
  1616. if ((0, _util.isNum)(glyph)) {
  1617. spacingLength = spacingDir * glyph * fontSize / 1000;
  1618. this.ctx.translate(spacingLength, 0);
  1619. current.x += spacingLength * textHScale;
  1620. continue;
  1621. }
  1622. const spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
  1623. const operatorList = font.charProcOperatorList[glyph.operatorListId];
  1624. if (!operatorList) {
  1625. (0, _util.warn)(`Type3 character "${glyph.operatorListId}" is not available.`);
  1626. continue;
  1627. }
  1628. if (this.contentVisible) {
  1629. this.processingType3 = glyph;
  1630. this.save();
  1631. ctx.scale(fontSize, fontSize);
  1632. ctx.transform.apply(ctx, fontMatrix);
  1633. this.executeOperatorList(operatorList);
  1634. this.restore();
  1635. }
  1636. const transformed = _util.Util.applyTransform([glyph.width, 0], fontMatrix);
  1637. width = transformed[0] * fontSize + spacing;
  1638. ctx.translate(width, 0);
  1639. current.x += width * textHScale;
  1640. }
  1641. ctx.restore();
  1642. this.processingType3 = null;
  1643. }
  1644. setCharWidth(xWidth, yWidth) {}
  1645. setCharWidthAndBounds(xWidth, yWidth, llx, lly, urx, ury) {
  1646. this.ctx.rect(llx, lly, urx - llx, ury - lly);
  1647. this.clip();
  1648. this.endPath();
  1649. }
  1650. getColorN_Pattern(IR) {
  1651. let pattern;
  1652. if (IR[0] === "TilingPattern") {
  1653. const color = IR[1];
  1654. const baseTransform = this.baseTransform || this.ctx.mozCurrentTransform.slice();
  1655. const canvasGraphicsFactory = {
  1656. createCanvasGraphics: ctx => {
  1657. return new CanvasGraphics(ctx, this.commonObjs, this.objs, this.canvasFactory);
  1658. }
  1659. };
  1660. pattern = new _pattern_helper.TilingPattern(IR, color, this.ctx, canvasGraphicsFactory, baseTransform);
  1661. } else {
  1662. pattern = this._getPattern(IR[1], IR[2]);
  1663. }
  1664. return pattern;
  1665. }
  1666. setStrokeColorN() {
  1667. this.current.strokeColor = this.getColorN_Pattern(arguments);
  1668. }
  1669. setFillColorN() {
  1670. this.current.fillColor = this.getColorN_Pattern(arguments);
  1671. this.current.patternFill = true;
  1672. }
  1673. setStrokeRGBColor(r, g, b) {
  1674. const color = _util.Util.makeHexColor(r, g, b);
  1675. this.ctx.strokeStyle = color;
  1676. this.current.strokeColor = color;
  1677. }
  1678. setFillRGBColor(r, g, b) {
  1679. const color = _util.Util.makeHexColor(r, g, b);
  1680. this.ctx.fillStyle = color;
  1681. this.current.fillColor = color;
  1682. this.current.patternFill = false;
  1683. }
  1684. _getPattern(objId, matrix = null) {
  1685. let pattern;
  1686. if (this.cachedPatterns.has(objId)) {
  1687. pattern = this.cachedPatterns.get(objId);
  1688. } else {
  1689. pattern = (0, _pattern_helper.getShadingPattern)(this.objs.get(objId));
  1690. this.cachedPatterns.set(objId, pattern);
  1691. }
  1692. if (matrix) {
  1693. pattern.matrix = matrix;
  1694. }
  1695. return pattern;
  1696. }
  1697. shadingFill(objId) {
  1698. if (!this.contentVisible) {
  1699. return;
  1700. }
  1701. const ctx = this.ctx;
  1702. this.save();
  1703. const pattern = this._getPattern(objId);
  1704. ctx.fillStyle = pattern.getPattern(ctx, this, ctx.mozCurrentTransformInverse, _pattern_helper.PathType.SHADING);
  1705. const inv = ctx.mozCurrentTransformInverse;
  1706. if (inv) {
  1707. const canvas = ctx.canvas;
  1708. const width = canvas.width;
  1709. const height = canvas.height;
  1710. const bl = _util.Util.applyTransform([0, 0], inv);
  1711. const br = _util.Util.applyTransform([0, height], inv);
  1712. const ul = _util.Util.applyTransform([width, 0], inv);
  1713. const ur = _util.Util.applyTransform([width, height], inv);
  1714. const x0 = Math.min(bl[0], br[0], ul[0], ur[0]);
  1715. const y0 = Math.min(bl[1], br[1], ul[1], ur[1]);
  1716. const x1 = Math.max(bl[0], br[0], ul[0], ur[0]);
  1717. const y1 = Math.max(bl[1], br[1], ul[1], ur[1]);
  1718. this.ctx.fillRect(x0, y0, x1 - x0, y1 - y0);
  1719. } else {
  1720. this.ctx.fillRect(-1e10, -1e10, 2e10, 2e10);
  1721. }
  1722. this.compose(this.current.getClippedPathBoundingBox());
  1723. this.restore();
  1724. }
  1725. beginInlineImage() {
  1726. (0, _util.unreachable)("Should not call beginInlineImage");
  1727. }
  1728. beginImageData() {
  1729. (0, _util.unreachable)("Should not call beginImageData");
  1730. }
  1731. paintFormXObjectBegin(matrix, bbox) {
  1732. if (!this.contentVisible) {
  1733. return;
  1734. }
  1735. this.save();
  1736. this.baseTransformStack.push(this.baseTransform);
  1737. if (Array.isArray(matrix) && matrix.length === 6) {
  1738. this.transform.apply(this, matrix);
  1739. }
  1740. this.baseTransform = this.ctx.mozCurrentTransform;
  1741. if (bbox) {
  1742. const width = bbox[2] - bbox[0];
  1743. const height = bbox[3] - bbox[1];
  1744. this.ctx.rect(bbox[0], bbox[1], width, height);
  1745. this.current.updatePathMinMax(this.ctx.mozCurrentTransform, bbox[0], bbox[1]);
  1746. this.current.updatePathMinMax(this.ctx.mozCurrentTransform, bbox[2], bbox[3]);
  1747. this.clip();
  1748. this.endPath();
  1749. }
  1750. }
  1751. paintFormXObjectEnd() {
  1752. if (!this.contentVisible) {
  1753. return;
  1754. }
  1755. this.restore();
  1756. this.baseTransform = this.baseTransformStack.pop();
  1757. }
  1758. beginGroup(group) {
  1759. if (!this.contentVisible) {
  1760. return;
  1761. }
  1762. this.save();
  1763. const suspendedCtx = this.suspendedCtx;
  1764. if (this.current.activeSMask) {
  1765. this.suspendedCtx = null;
  1766. this.current.activeSMask = null;
  1767. }
  1768. const currentCtx = this.ctx;
  1769. if (!group.isolated) {
  1770. (0, _util.info)("TODO: Support non-isolated groups.");
  1771. }
  1772. if (group.knockout) {
  1773. (0, _util.warn)("Knockout groups not supported.");
  1774. }
  1775. const currentTransform = currentCtx.mozCurrentTransform;
  1776. if (group.matrix) {
  1777. currentCtx.transform.apply(currentCtx, group.matrix);
  1778. }
  1779. if (!group.bbox) {
  1780. throw new Error("Bounding box is required.");
  1781. }
  1782. let bounds = _util.Util.getAxialAlignedBoundingBox(group.bbox, currentCtx.mozCurrentTransform);
  1783. const canvasBounds = [0, 0, currentCtx.canvas.width, currentCtx.canvas.height];
  1784. bounds = _util.Util.intersect(bounds, canvasBounds) || [0, 0, 0, 0];
  1785. const offsetX = Math.floor(bounds[0]);
  1786. const offsetY = Math.floor(bounds[1]);
  1787. let drawnWidth = Math.max(Math.ceil(bounds[2]) - offsetX, 1);
  1788. let drawnHeight = Math.max(Math.ceil(bounds[3]) - offsetY, 1);
  1789. let scaleX = 1,
  1790. scaleY = 1;
  1791. if (drawnWidth > MAX_GROUP_SIZE) {
  1792. scaleX = drawnWidth / MAX_GROUP_SIZE;
  1793. drawnWidth = MAX_GROUP_SIZE;
  1794. }
  1795. if (drawnHeight > MAX_GROUP_SIZE) {
  1796. scaleY = drawnHeight / MAX_GROUP_SIZE;
  1797. drawnHeight = MAX_GROUP_SIZE;
  1798. }
  1799. this.current.startNewPathAndClipBox([0, 0, drawnWidth, drawnHeight]);
  1800. let cacheId = "groupAt" + this.groupLevel;
  1801. if (group.smask) {
  1802. cacheId += "_smask_" + this.smaskCounter++ % 2;
  1803. }
  1804. const scratchCanvas = this.cachedCanvases.getCanvas(cacheId, drawnWidth, drawnHeight, true);
  1805. const groupCtx = scratchCanvas.context;
  1806. groupCtx.scale(1 / scaleX, 1 / scaleY);
  1807. groupCtx.translate(-offsetX, -offsetY);
  1808. groupCtx.transform.apply(groupCtx, currentTransform);
  1809. if (group.smask) {
  1810. this.smaskStack.push({
  1811. canvas: scratchCanvas.canvas,
  1812. context: groupCtx,
  1813. offsetX,
  1814. offsetY,
  1815. scaleX,
  1816. scaleY,
  1817. subtype: group.smask.subtype,
  1818. backdrop: group.smask.backdrop,
  1819. transferMap: group.smask.transferMap || null,
  1820. startTransformInverse: null
  1821. });
  1822. } else {
  1823. currentCtx.setTransform(1, 0, 0, 1, 0, 0);
  1824. currentCtx.translate(offsetX, offsetY);
  1825. currentCtx.scale(scaleX, scaleY);
  1826. currentCtx.save();
  1827. }
  1828. copyCtxState(currentCtx, groupCtx);
  1829. this.ctx = groupCtx;
  1830. this.setGState([["BM", "source-over"], ["ca", 1], ["CA", 1]]);
  1831. this.groupStack.push({
  1832. ctx: currentCtx,
  1833. suspendedCtx
  1834. });
  1835. this.groupLevel++;
  1836. }
  1837. endGroup(group) {
  1838. if (!this.contentVisible) {
  1839. return;
  1840. }
  1841. this.groupLevel--;
  1842. const groupCtx = this.ctx;
  1843. const {
  1844. ctx,
  1845. suspendedCtx
  1846. } = this.groupStack.pop();
  1847. this.ctx = ctx;
  1848. this.ctx.imageSmoothingEnabled = false;
  1849. if (suspendedCtx) {
  1850. this.suspendedCtx = suspendedCtx;
  1851. }
  1852. if (group.smask) {
  1853. this.tempSMask = this.smaskStack.pop();
  1854. this.restore();
  1855. } else {
  1856. this.ctx.restore();
  1857. const currentMtx = this.ctx.mozCurrentTransform;
  1858. this.restore();
  1859. this.ctx.save();
  1860. this.ctx.setTransform.apply(this.ctx, currentMtx);
  1861. const dirtyBox = _util.Util.getAxialAlignedBoundingBox([0, 0, groupCtx.canvas.width, groupCtx.canvas.height], currentMtx);
  1862. this.ctx.drawImage(groupCtx.canvas, 0, 0);
  1863. this.ctx.restore();
  1864. this.compose(dirtyBox);
  1865. }
  1866. }
  1867. beginAnnotations() {
  1868. this.save();
  1869. if (this.baseTransform) {
  1870. this.ctx.setTransform.apply(this.ctx, this.baseTransform);
  1871. }
  1872. }
  1873. endAnnotations() {
  1874. this.restore();
  1875. }
  1876. beginAnnotation(id, rect, transform, matrix, hasOwnCanvas) {
  1877. this.save();
  1878. if (Array.isArray(rect) && rect.length === 4) {
  1879. const width = rect[2] - rect[0];
  1880. const height = rect[3] - rect[1];
  1881. if (hasOwnCanvas && this.annotationCanvasMap) {
  1882. transform = transform.slice();
  1883. transform[4] -= rect[0];
  1884. transform[5] -= rect[1];
  1885. rect = rect.slice();
  1886. rect[0] = rect[1] = 0;
  1887. rect[2] = width;
  1888. rect[3] = height;
  1889. const [scaleX, scaleY] = _util.Util.singularValueDecompose2dScale(this.ctx.mozCurrentTransform);
  1890. const {
  1891. viewportScale
  1892. } = this;
  1893. const canvasWidth = Math.ceil(width * this.outputScaleX * viewportScale);
  1894. const canvasHeight = Math.ceil(height * this.outputScaleY * viewportScale);
  1895. this.annotationCanvas = this.canvasFactory.create(canvasWidth, canvasHeight);
  1896. const {
  1897. canvas,
  1898. context
  1899. } = this.annotationCanvas;
  1900. canvas.style.width = `calc(${width}px * var(--viewport-scale-factor))`;
  1901. canvas.style.height = `calc(${height}px * var(--viewport-scale-factor))`;
  1902. this.annotationCanvasMap.set(id, canvas);
  1903. this.annotationCanvas.savedCtx = this.ctx;
  1904. this.ctx = context;
  1905. this.ctx.setTransform(scaleX, 0, 0, -scaleY, 0, height * scaleY);
  1906. addContextCurrentTransform(this.ctx);
  1907. resetCtxToDefault(this.ctx);
  1908. } else {
  1909. resetCtxToDefault(this.ctx);
  1910. this.ctx.rect(rect[0], rect[1], width, height);
  1911. this.clip();
  1912. this.endPath();
  1913. }
  1914. }
  1915. this.current = new CanvasExtraState(this.ctx.canvas.width, this.ctx.canvas.height);
  1916. this.transform.apply(this, transform);
  1917. this.transform.apply(this, matrix);
  1918. }
  1919. endAnnotation() {
  1920. if (this.annotationCanvas) {
  1921. this.ctx = this.annotationCanvas.savedCtx;
  1922. delete this.annotationCanvas.savedCtx;
  1923. delete this.annotationCanvas;
  1924. }
  1925. this.restore();
  1926. }
  1927. paintImageMaskXObject(img) {
  1928. if (!this.contentVisible) {
  1929. return;
  1930. }
  1931. const ctx = this.ctx;
  1932. const width = img.width,
  1933. height = img.height;
  1934. const glyph = this.processingType3;
  1935. if (COMPILE_TYPE3_GLYPHS && glyph && glyph.compiled === undefined) {
  1936. if (width <= MAX_SIZE_TO_COMPILE && height <= MAX_SIZE_TO_COMPILE) {
  1937. glyph.compiled = compileType3Glyph({
  1938. data: img.data,
  1939. width,
  1940. height
  1941. });
  1942. } else {
  1943. glyph.compiled = null;
  1944. }
  1945. }
  1946. if (glyph?.compiled) {
  1947. glyph.compiled(ctx);
  1948. return;
  1949. }
  1950. const mask = this._createMaskCanvas(img);
  1951. const maskCanvas = mask.canvas;
  1952. ctx.save();
  1953. ctx.setTransform(1, 0, 0, 1, 0, 0);
  1954. ctx.drawImage(maskCanvas, mask.offsetX, mask.offsetY);
  1955. ctx.restore();
  1956. this.compose();
  1957. }
  1958. paintImageMaskXObjectRepeat(imgData, scaleX, skewX = 0, skewY = 0, scaleY, positions) {
  1959. if (!this.contentVisible) {
  1960. return;
  1961. }
  1962. const ctx = this.ctx;
  1963. ctx.save();
  1964. const currentTransform = ctx.mozCurrentTransform;
  1965. ctx.transform(scaleX, skewX, skewY, scaleY, 0, 0);
  1966. const mask = this._createMaskCanvas(imgData);
  1967. ctx.setTransform(1, 0, 0, 1, 0, 0);
  1968. for (let i = 0, ii = positions.length; i < ii; i += 2) {
  1969. const trans = _util.Util.transform(currentTransform, [scaleX, skewX, skewY, scaleY, positions[i], positions[i + 1]]);
  1970. const [x, y] = _util.Util.applyTransform([0, 0], trans);
  1971. ctx.drawImage(mask.canvas, x, y);
  1972. }
  1973. ctx.restore();
  1974. this.compose();
  1975. }
  1976. paintImageMaskXObjectGroup(images) {
  1977. if (!this.contentVisible) {
  1978. return;
  1979. }
  1980. const ctx = this.ctx;
  1981. const fillColor = this.current.fillColor;
  1982. const isPatternFill = this.current.patternFill;
  1983. for (let i = 0, ii = images.length; i < ii; i++) {
  1984. const image = images[i];
  1985. const width = image.width,
  1986. height = image.height;
  1987. const maskCanvas = this.cachedCanvases.getCanvas("maskCanvas", width, height);
  1988. const maskCtx = maskCanvas.context;
  1989. maskCtx.save();
  1990. putBinaryImageMask(maskCtx, image);
  1991. maskCtx.globalCompositeOperation = "source-in";
  1992. maskCtx.fillStyle = isPatternFill ? fillColor.getPattern(maskCtx, this, ctx.mozCurrentTransformInverse, _pattern_helper.PathType.FILL) : fillColor;
  1993. maskCtx.fillRect(0, 0, width, height);
  1994. maskCtx.restore();
  1995. ctx.save();
  1996. ctx.transform.apply(ctx, image.transform);
  1997. ctx.scale(1, -1);
  1998. ctx.drawImage(maskCanvas.canvas, 0, 0, width, height, 0, -1, 1, 1);
  1999. ctx.restore();
  2000. }
  2001. this.compose();
  2002. }
  2003. paintImageXObject(objId) {
  2004. if (!this.contentVisible) {
  2005. return;
  2006. }
  2007. const imgData = objId.startsWith("g_") ? this.commonObjs.get(objId) : this.objs.get(objId);
  2008. if (!imgData) {
  2009. (0, _util.warn)("Dependent image isn't ready yet");
  2010. return;
  2011. }
  2012. this.paintInlineImageXObject(imgData);
  2013. }
  2014. paintImageXObjectRepeat(objId, scaleX, scaleY, positions) {
  2015. if (!this.contentVisible) {
  2016. return;
  2017. }
  2018. const imgData = objId.startsWith("g_") ? this.commonObjs.get(objId) : this.objs.get(objId);
  2019. if (!imgData) {
  2020. (0, _util.warn)("Dependent image isn't ready yet");
  2021. return;
  2022. }
  2023. const width = imgData.width;
  2024. const height = imgData.height;
  2025. const map = [];
  2026. for (let i = 0, ii = positions.length; i < ii; i += 2) {
  2027. map.push({
  2028. transform: [scaleX, 0, 0, scaleY, positions[i], positions[i + 1]],
  2029. x: 0,
  2030. y: 0,
  2031. w: width,
  2032. h: height
  2033. });
  2034. }
  2035. this.paintInlineImageXObjectGroup(imgData, map);
  2036. }
  2037. paintInlineImageXObject(imgData) {
  2038. if (!this.contentVisible) {
  2039. return;
  2040. }
  2041. const width = imgData.width;
  2042. const height = imgData.height;
  2043. const ctx = this.ctx;
  2044. this.save();
  2045. ctx.scale(1 / width, -1 / height);
  2046. let imgToPaint;
  2047. if (typeof HTMLElement === "function" && imgData instanceof HTMLElement || !imgData.data) {
  2048. imgToPaint = imgData;
  2049. } else {
  2050. const tmpCanvas = this.cachedCanvases.getCanvas("inlineImage", width, height);
  2051. const tmpCtx = tmpCanvas.context;
  2052. putBinaryImageData(tmpCtx, imgData, this.current.transferMaps);
  2053. imgToPaint = tmpCanvas.canvas;
  2054. }
  2055. const scaled = this._scaleImage(imgToPaint, ctx.mozCurrentTransformInverse);
  2056. ctx.imageSmoothingEnabled = getImageSmoothingEnabled(ctx.mozCurrentTransform, imgData.interpolate);
  2057. ctx.drawImage(scaled.img, 0, 0, scaled.paintWidth, scaled.paintHeight, 0, -height, width, height);
  2058. if (this.imageLayer) {
  2059. const position = this.getCanvasPosition(0, -height);
  2060. this.imageLayer.appendImage({
  2061. imgData,
  2062. left: position[0],
  2063. top: position[1],
  2064. width: width / ctx.mozCurrentTransformInverse[0],
  2065. height: height / ctx.mozCurrentTransformInverse[3]
  2066. });
  2067. }
  2068. this.compose();
  2069. this.restore();
  2070. }
  2071. paintInlineImageXObjectGroup(imgData, map) {
  2072. if (!this.contentVisible) {
  2073. return;
  2074. }
  2075. const ctx = this.ctx;
  2076. const w = imgData.width;
  2077. const h = imgData.height;
  2078. const tmpCanvas = this.cachedCanvases.getCanvas("inlineImage", w, h);
  2079. const tmpCtx = tmpCanvas.context;
  2080. putBinaryImageData(tmpCtx, imgData, this.current.transferMaps);
  2081. for (let i = 0, ii = map.length; i < ii; i++) {
  2082. const entry = map[i];
  2083. ctx.save();
  2084. ctx.transform.apply(ctx, entry.transform);
  2085. ctx.scale(1, -1);
  2086. ctx.drawImage(tmpCanvas.canvas, entry.x, entry.y, entry.w, entry.h, 0, -1, 1, 1);
  2087. if (this.imageLayer) {
  2088. const position = this.getCanvasPosition(entry.x, entry.y);
  2089. this.imageLayer.appendImage({
  2090. imgData,
  2091. left: position[0],
  2092. top: position[1],
  2093. width: w,
  2094. height: h
  2095. });
  2096. }
  2097. ctx.restore();
  2098. }
  2099. this.compose();
  2100. }
  2101. paintSolidColorImageMask() {
  2102. if (!this.contentVisible) {
  2103. return;
  2104. }
  2105. this.ctx.fillRect(0, 0, 1, 1);
  2106. this.compose();
  2107. }
  2108. markPoint(tag) {}
  2109. markPointProps(tag, properties) {}
  2110. beginMarkedContent(tag) {
  2111. this.markedContentStack.push({
  2112. visible: true
  2113. });
  2114. }
  2115. beginMarkedContentProps(tag, properties) {
  2116. if (tag === "OC") {
  2117. this.markedContentStack.push({
  2118. visible: this.optionalContentConfig.isVisible(properties)
  2119. });
  2120. } else {
  2121. this.markedContentStack.push({
  2122. visible: true
  2123. });
  2124. }
  2125. this.contentVisible = this.isContentVisible();
  2126. }
  2127. endMarkedContent() {
  2128. this.markedContentStack.pop();
  2129. this.contentVisible = this.isContentVisible();
  2130. }
  2131. beginCompat() {}
  2132. endCompat() {}
  2133. consumePath(clipBox) {
  2134. if (this.pendingClip) {
  2135. this.current.updateClipFromPath();
  2136. }
  2137. if (!this.pendingClip) {
  2138. this.compose(clipBox);
  2139. }
  2140. const ctx = this.ctx;
  2141. if (this.pendingClip) {
  2142. if (this.pendingClip === EO_CLIP) {
  2143. ctx.clip("evenodd");
  2144. } else {
  2145. ctx.clip();
  2146. }
  2147. this.pendingClip = null;
  2148. }
  2149. this.current.startNewPathAndClipBox(this.current.clipBox);
  2150. ctx.beginPath();
  2151. }
  2152. getSinglePixelWidth() {
  2153. if (this._cachedGetSinglePixelWidth === null) {
  2154. const m = this.ctx.mozCurrentTransform;
  2155. const absDet = Math.abs(m[0] * m[3] - m[2] * m[1]);
  2156. const sqNorm1 = m[0] ** 2 + m[2] ** 2;
  2157. const sqNorm2 = m[1] ** 2 + m[3] ** 2;
  2158. const pixelHeight = Math.sqrt(Math.max(sqNorm1, sqNorm2)) / absDet;
  2159. if (sqNorm1 !== sqNorm2 && this._combinedScaleFactor * pixelHeight > 1) {
  2160. this._cachedGetSinglePixelWidth = -(this._combinedScaleFactor * pixelHeight);
  2161. } else if (absDet > Number.EPSILON) {
  2162. this._cachedGetSinglePixelWidth = pixelHeight;
  2163. } else {
  2164. this._cachedGetSinglePixelWidth = 1;
  2165. }
  2166. }
  2167. return this._cachedGetSinglePixelWidth;
  2168. }
  2169. getCanvasPosition(x, y) {
  2170. const transform = this.ctx.mozCurrentTransform;
  2171. return [transform[0] * x + transform[2] * y + transform[4], transform[1] * x + transform[3] * y + transform[5]];
  2172. }
  2173. isContentVisible() {
  2174. for (let i = this.markedContentStack.length - 1; i >= 0; i--) {
  2175. if (!this.markedContentStack[i].visible) {
  2176. return false;
  2177. }
  2178. }
  2179. return true;
  2180. }
  2181. }
  2182. exports.CanvasGraphics = CanvasGraphics;
  2183. for (const op in _util.OPS) {
  2184. if (CanvasGraphics.prototype[op] !== undefined) {
  2185. CanvasGraphics.prototype[_util.OPS[op]] = CanvasGraphics.prototype[op];
  2186. }
  2187. }