canvas.js 73 KB

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