123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385 |
- /**
- * @licstart The following is the entire license notice for the
- * Javascript code in this page
- *
- * Copyright 2021 Mozilla Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * @licend The above is the entire license notice for the
- * Javascript code in this page
- */
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.CanvasGraphics = void 0;
- var _util = require("../shared/util.js");
- var _pattern_helper = require("./pattern_helper.js");
- const MIN_FONT_SIZE = 16;
- const MAX_FONT_SIZE = 100;
- const MAX_GROUP_SIZE = 4096;
- const COMPILE_TYPE3_GLYPHS = true;
- const MAX_SIZE_TO_COMPILE = 1000;
- const FULL_CHUNK_HEIGHT = 16;
- const LINEWIDTH_SCALE_FACTOR = 1.000001;
- function addContextCurrentTransform(ctx) {
- if (ctx.mozCurrentTransform) {
- return;
- }
- ctx._originalSave = ctx.save;
- ctx._originalRestore = ctx.restore;
- ctx._originalRotate = ctx.rotate;
- ctx._originalScale = ctx.scale;
- ctx._originalTranslate = ctx.translate;
- ctx._originalTransform = ctx.transform;
- ctx._originalSetTransform = ctx.setTransform;
- ctx._originalResetTransform = ctx.resetTransform;
- ctx._transformMatrix = ctx._transformMatrix || [1, 0, 0, 1, 0, 0];
- ctx._transformStack = [];
- try {
- const desc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(ctx), "lineWidth");
- ctx._setLineWidth = desc.set;
- ctx._getLineWidth = desc.get;
- Object.defineProperty(ctx, "lineWidth", {
- set: function setLineWidth(width) {
- this._setLineWidth(width * LINEWIDTH_SCALE_FACTOR);
- },
- get: function getLineWidth() {
- return this._getLineWidth();
- }
- });
- } catch (_) {}
- Object.defineProperty(ctx, "mozCurrentTransform", {
- get: function getCurrentTransform() {
- return this._transformMatrix;
- }
- });
- Object.defineProperty(ctx, "mozCurrentTransformInverse", {
- get: function getCurrentTransformInverse() {
- const [a, b, c, d, e, f] = this._transformMatrix;
- const ad_bc = a * d - b * c;
- const bc_ad = b * c - a * d;
- 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];
- }
- });
- ctx.save = function ctxSave() {
- const old = this._transformMatrix;
- this._transformStack.push(old);
- this._transformMatrix = old.slice(0, 6);
- this._originalSave();
- };
- ctx.restore = function ctxRestore() {
- const prev = this._transformStack.pop();
- if (prev) {
- this._transformMatrix = prev;
- this._originalRestore();
- }
- };
- ctx.translate = function ctxTranslate(x, y) {
- const m = this._transformMatrix;
- m[4] = m[0] * x + m[2] * y + m[4];
- m[5] = m[1] * x + m[3] * y + m[5];
- this._originalTranslate(x, y);
- };
- ctx.scale = function ctxScale(x, y) {
- const m = this._transformMatrix;
- m[0] *= x;
- m[1] *= x;
- m[2] *= y;
- m[3] *= y;
- this._originalScale(x, y);
- };
- ctx.transform = function ctxTransform(a, b, c, d, e, f) {
- const m = this._transformMatrix;
- 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]];
- ctx._originalTransform(a, b, c, d, e, f);
- };
- ctx.setTransform = function ctxSetTransform(a, b, c, d, e, f) {
- this._transformMatrix = [a, b, c, d, e, f];
- ctx._originalSetTransform(a, b, c, d, e, f);
- };
- ctx.resetTransform = function ctxResetTransform() {
- this._transformMatrix = [1, 0, 0, 1, 0, 0];
- ctx._originalResetTransform();
- };
- ctx.rotate = function ctxRotate(angle) {
- const cosValue = Math.cos(angle);
- const sinValue = Math.sin(angle);
- const m = this._transformMatrix;
- 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]];
- this._originalRotate(angle);
- };
- }
- class CachedCanvases {
- constructor(canvasFactory) {
- this.canvasFactory = canvasFactory;
- this.cache = Object.create(null);
- }
- getCanvas(id, width, height, trackTransform) {
- let canvasEntry;
- if (this.cache[id] !== undefined) {
- canvasEntry = this.cache[id];
- this.canvasFactory.reset(canvasEntry, width, height);
- canvasEntry.context.setTransform(1, 0, 0, 1, 0, 0);
- } else {
- canvasEntry = this.canvasFactory.create(width, height);
- this.cache[id] = canvasEntry;
- }
- if (trackTransform) {
- addContextCurrentTransform(canvasEntry.context);
- }
- return canvasEntry;
- }
- clear() {
- for (const id in this.cache) {
- const canvasEntry = this.cache[id];
- this.canvasFactory.destroy(canvasEntry);
- delete this.cache[id];
- }
- }
- }
- function compileType3Glyph(imgData) {
- const POINT_TO_PROCESS_LIMIT = 1000;
- const POINT_TYPES = new Uint8Array([0, 2, 4, 0, 1, 0, 5, 4, 8, 10, 0, 8, 0, 2, 1, 0]);
- const width = imgData.width,
- height = imgData.height,
- width1 = width + 1;
- let i, ii, j, j0;
- const points = new Uint8Array(width1 * (height + 1));
- const lineSize = width + 7 & ~7,
- data0 = imgData.data;
- const data = new Uint8Array(lineSize * height);
- let pos = 0;
- for (i = 0, ii = data0.length; i < ii; i++) {
- const elem = data0[i];
- let mask = 128;
- while (mask > 0) {
- data[pos++] = elem & mask ? 0 : 255;
- mask >>= 1;
- }
- }
- let count = 0;
- pos = 0;
- if (data[pos] !== 0) {
- points[0] = 1;
- ++count;
- }
- for (j = 1; j < width; j++) {
- if (data[pos] !== data[pos + 1]) {
- points[j] = data[pos] ? 2 : 1;
- ++count;
- }
- pos++;
- }
- if (data[pos] !== 0) {
- points[j] = 2;
- ++count;
- }
- for (i = 1; i < height; i++) {
- pos = i * lineSize;
- j0 = i * width1;
- if (data[pos - lineSize] !== data[pos]) {
- points[j0] = data[pos] ? 1 : 8;
- ++count;
- }
- let sum = (data[pos] ? 4 : 0) + (data[pos - lineSize] ? 8 : 0);
- for (j = 1; j < width; j++) {
- sum = (sum >> 2) + (data[pos + 1] ? 4 : 0) + (data[pos - lineSize + 1] ? 8 : 0);
- if (POINT_TYPES[sum]) {
- points[j0 + j] = POINT_TYPES[sum];
- ++count;
- }
- pos++;
- }
- if (data[pos - lineSize] !== data[pos]) {
- points[j0 + j] = data[pos] ? 2 : 4;
- ++count;
- }
- if (count > POINT_TO_PROCESS_LIMIT) {
- return null;
- }
- }
- pos = lineSize * (height - 1);
- j0 = i * width1;
- if (data[pos] !== 0) {
- points[j0] = 8;
- ++count;
- }
- for (j = 1; j < width; j++) {
- if (data[pos] !== data[pos + 1]) {
- points[j0 + j] = data[pos] ? 4 : 8;
- ++count;
- }
- pos++;
- }
- if (data[pos] !== 0) {
- points[j0 + j] = 4;
- ++count;
- }
- if (count > POINT_TO_PROCESS_LIMIT) {
- return null;
- }
- const steps = new Int32Array([0, width1, -1, 0, -width1, 0, 0, 0, 1]);
- const outlines = [];
- for (i = 0; count && i <= height; i++) {
- let p = i * width1;
- const end = p + width;
- while (p < end && !points[p]) {
- p++;
- }
- if (p === end) {
- continue;
- }
- const coords = [p % width1, i];
- const p0 = p;
- let type = points[p];
- do {
- const step = steps[type];
- do {
- p += step;
- } while (!points[p]);
- const pp = points[p];
- if (pp !== 5 && pp !== 10) {
- type = pp;
- points[p] = 0;
- } else {
- type = pp & 0x33 * type >> 4;
- points[p] &= type >> 2 | type << 2;
- }
- coords.push(p % width1, p / width1 | 0);
- if (!points[p]) {
- --count;
- }
- } while (p0 !== p);
- outlines.push(coords);
- --i;
- }
- const drawOutline = function (c) {
- c.save();
- c.scale(1 / width, -1 / height);
- c.translate(0, -height);
- c.beginPath();
- for (let k = 0, kk = outlines.length; k < kk; k++) {
- const o = outlines[k];
- c.moveTo(o[0], o[1]);
- for (let l = 2, ll = o.length; l < ll; l += 2) {
- c.lineTo(o[l], o[l + 1]);
- }
- }
- c.fill();
- c.beginPath();
- c.restore();
- };
- return drawOutline;
- }
- class CanvasExtraState {
- constructor() {
- this.alphaIsShape = false;
- this.fontSize = 0;
- this.fontSizeScale = 1;
- this.textMatrix = _util.IDENTITY_MATRIX;
- this.textMatrixScale = 1;
- this.fontMatrix = _util.FONT_IDENTITY_MATRIX;
- this.leading = 0;
- this.x = 0;
- this.y = 0;
- this.lineX = 0;
- this.lineY = 0;
- this.charSpacing = 0;
- this.wordSpacing = 0;
- this.textHScale = 1;
- this.textRenderingMode = _util.TextRenderingMode.FILL;
- this.textRise = 0;
- this.fillColor = "#000000";
- this.strokeColor = "#000000";
- this.patternFill = false;
- this.fillAlpha = 1;
- this.strokeAlpha = 1;
- this.lineWidth = 1;
- this.activeSMask = null;
- this.resumeSMaskCtx = null;
- this.transferMaps = null;
- }
- clone() {
- return Object.create(this);
- }
- setCurrentPoint(x, y) {
- this.x = x;
- this.y = y;
- }
- }
- const CanvasGraphics = function CanvasGraphicsClosure() {
- const EXECUTION_TIME = 15;
- const EXECUTION_STEPS = 10;
- function putBinaryImageData(ctx, imgData, transferMaps = null) {
- if (typeof ImageData !== "undefined" && imgData instanceof ImageData) {
- ctx.putImageData(imgData, 0, 0);
- return;
- }
- const height = imgData.height,
- width = imgData.width;
- const partialChunkHeight = height % FULL_CHUNK_HEIGHT;
- const fullChunks = (height - partialChunkHeight) / FULL_CHUNK_HEIGHT;
- const totalChunks = partialChunkHeight === 0 ? fullChunks : fullChunks + 1;
- const chunkImgData = ctx.createImageData(width, FULL_CHUNK_HEIGHT);
- let srcPos = 0,
- destPos;
- const src = imgData.data;
- const dest = chunkImgData.data;
- let i, j, thisChunkHeight, elemsInThisChunk;
- let transferMapRed, transferMapGreen, transferMapBlue, transferMapGray;
- if (transferMaps) {
- switch (transferMaps.length) {
- case 1:
- transferMapRed = transferMaps[0];
- transferMapGreen = transferMaps[0];
- transferMapBlue = transferMaps[0];
- transferMapGray = transferMaps[0];
- break;
- case 4:
- transferMapRed = transferMaps[0];
- transferMapGreen = transferMaps[1];
- transferMapBlue = transferMaps[2];
- transferMapGray = transferMaps[3];
- break;
- }
- }
- if (imgData.kind === _util.ImageKind.GRAYSCALE_1BPP) {
- const srcLength = src.byteLength;
- const dest32 = new Uint32Array(dest.buffer, 0, dest.byteLength >> 2);
- const dest32DataLength = dest32.length;
- const fullSrcDiff = width + 7 >> 3;
- let white = 0xffffffff;
- let black = _util.IsLittleEndianCached.value ? 0xff000000 : 0x000000ff;
- if (transferMapGray) {
- if (transferMapGray[0] === 0xff && transferMapGray[0xff] === 0) {
- [white, black] = [black, white];
- }
- }
- for (i = 0; i < totalChunks; i++) {
- thisChunkHeight = i < fullChunks ? FULL_CHUNK_HEIGHT : partialChunkHeight;
- destPos = 0;
- for (j = 0; j < thisChunkHeight; j++) {
- const srcDiff = srcLength - srcPos;
- let k = 0;
- const kEnd = srcDiff > fullSrcDiff ? width : srcDiff * 8 - 7;
- const kEndUnrolled = kEnd & ~7;
- let mask = 0;
- let srcByte = 0;
- for (; k < kEndUnrolled; k += 8) {
- srcByte = src[srcPos++];
- dest32[destPos++] = srcByte & 128 ? white : black;
- dest32[destPos++] = srcByte & 64 ? white : black;
- dest32[destPos++] = srcByte & 32 ? white : black;
- dest32[destPos++] = srcByte & 16 ? white : black;
- dest32[destPos++] = srcByte & 8 ? white : black;
- dest32[destPos++] = srcByte & 4 ? white : black;
- dest32[destPos++] = srcByte & 2 ? white : black;
- dest32[destPos++] = srcByte & 1 ? white : black;
- }
- for (; k < kEnd; k++) {
- if (mask === 0) {
- srcByte = src[srcPos++];
- mask = 128;
- }
- dest32[destPos++] = srcByte & mask ? white : black;
- mask >>= 1;
- }
- }
- while (destPos < dest32DataLength) {
- dest32[destPos++] = 0;
- }
- ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
- }
- } else if (imgData.kind === _util.ImageKind.RGBA_32BPP) {
- const hasTransferMaps = !!(transferMapRed || transferMapGreen || transferMapBlue);
- j = 0;
- elemsInThisChunk = width * FULL_CHUNK_HEIGHT * 4;
- for (i = 0; i < fullChunks; i++) {
- dest.set(src.subarray(srcPos, srcPos + elemsInThisChunk));
- srcPos += elemsInThisChunk;
- if (hasTransferMaps) {
- for (let k = 0; k < elemsInThisChunk; k += 4) {
- if (transferMapRed) {
- dest[k + 0] = transferMapRed[dest[k + 0]];
- }
- if (transferMapGreen) {
- dest[k + 1] = transferMapGreen[dest[k + 1]];
- }
- if (transferMapBlue) {
- dest[k + 2] = transferMapBlue[dest[k + 2]];
- }
- }
- }
- ctx.putImageData(chunkImgData, 0, j);
- j += FULL_CHUNK_HEIGHT;
- }
- if (i < totalChunks) {
- elemsInThisChunk = width * partialChunkHeight * 4;
- dest.set(src.subarray(srcPos, srcPos + elemsInThisChunk));
- if (hasTransferMaps) {
- for (let k = 0; k < elemsInThisChunk; k += 4) {
- if (transferMapRed) {
- dest[k + 0] = transferMapRed[dest[k + 0]];
- }
- if (transferMapGreen) {
- dest[k + 1] = transferMapGreen[dest[k + 1]];
- }
- if (transferMapBlue) {
- dest[k + 2] = transferMapBlue[dest[k + 2]];
- }
- }
- }
- ctx.putImageData(chunkImgData, 0, j);
- }
- } else if (imgData.kind === _util.ImageKind.RGB_24BPP) {
- const hasTransferMaps = !!(transferMapRed || transferMapGreen || transferMapBlue);
- thisChunkHeight = FULL_CHUNK_HEIGHT;
- elemsInThisChunk = width * thisChunkHeight;
- for (i = 0; i < totalChunks; i++) {
- if (i >= fullChunks) {
- thisChunkHeight = partialChunkHeight;
- elemsInThisChunk = width * thisChunkHeight;
- }
- destPos = 0;
- for (j = elemsInThisChunk; j--;) {
- dest[destPos++] = src[srcPos++];
- dest[destPos++] = src[srcPos++];
- dest[destPos++] = src[srcPos++];
- dest[destPos++] = 255;
- }
- if (hasTransferMaps) {
- for (let k = 0; k < destPos; k += 4) {
- if (transferMapRed) {
- dest[k + 0] = transferMapRed[dest[k + 0]];
- }
- if (transferMapGreen) {
- dest[k + 1] = transferMapGreen[dest[k + 1]];
- }
- if (transferMapBlue) {
- dest[k + 2] = transferMapBlue[dest[k + 2]];
- }
- }
- }
- ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
- }
- } else {
- throw new Error(`bad image kind: ${imgData.kind}`);
- }
- }
- function putBinaryImageMask(ctx, imgData) {
- const height = imgData.height,
- width = imgData.width;
- const partialChunkHeight = height % FULL_CHUNK_HEIGHT;
- const fullChunks = (height - partialChunkHeight) / FULL_CHUNK_HEIGHT;
- const totalChunks = partialChunkHeight === 0 ? fullChunks : fullChunks + 1;
- const chunkImgData = ctx.createImageData(width, FULL_CHUNK_HEIGHT);
- let srcPos = 0;
- const src = imgData.data;
- const dest = chunkImgData.data;
- for (let i = 0; i < totalChunks; i++) {
- const thisChunkHeight = i < fullChunks ? FULL_CHUNK_HEIGHT : partialChunkHeight;
- let destPos = 3;
- for (let j = 0; j < thisChunkHeight; j++) {
- let elem,
- mask = 0;
- for (let k = 0; k < width; k++) {
- if (!mask) {
- elem = src[srcPos++];
- mask = 128;
- }
- dest[destPos] = elem & mask ? 0 : 255;
- destPos += 4;
- mask >>= 1;
- }
- }
- ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
- }
- }
- function copyCtxState(sourceCtx, destCtx) {
- const properties = ["strokeStyle", "fillStyle", "fillRule", "globalAlpha", "lineWidth", "lineCap", "lineJoin", "miterLimit", "globalCompositeOperation", "font"];
- for (let i = 0, ii = properties.length; i < ii; i++) {
- const property = properties[i];
- if (sourceCtx[property] !== undefined) {
- destCtx[property] = sourceCtx[property];
- }
- }
- if (sourceCtx.setLineDash !== undefined) {
- destCtx.setLineDash(sourceCtx.getLineDash());
- destCtx.lineDashOffset = sourceCtx.lineDashOffset;
- }
- }
- function resetCtxToDefault(ctx) {
- ctx.strokeStyle = "#000000";
- ctx.fillStyle = "#000000";
- ctx.fillRule = "nonzero";
- ctx.globalAlpha = 1;
- ctx.lineWidth = 1;
- ctx.lineCap = "butt";
- ctx.lineJoin = "miter";
- ctx.miterLimit = 10;
- ctx.globalCompositeOperation = "source-over";
- ctx.font = "10px sans-serif";
- if (ctx.setLineDash !== undefined) {
- ctx.setLineDash([]);
- ctx.lineDashOffset = 0;
- }
- }
- function composeSMaskBackdrop(bytes, r0, g0, b0) {
- const length = bytes.length;
- for (let i = 3; i < length; i += 4) {
- const alpha = bytes[i];
- if (alpha === 0) {
- bytes[i - 3] = r0;
- bytes[i - 2] = g0;
- bytes[i - 1] = b0;
- } else if (alpha < 255) {
- const alpha_ = 255 - alpha;
- bytes[i - 3] = bytes[i - 3] * alpha + r0 * alpha_ >> 8;
- bytes[i - 2] = bytes[i - 2] * alpha + g0 * alpha_ >> 8;
- bytes[i - 1] = bytes[i - 1] * alpha + b0 * alpha_ >> 8;
- }
- }
- }
- function composeSMaskAlpha(maskData, layerData, transferMap) {
- const length = maskData.length;
- const scale = 1 / 255;
- for (let i = 3; i < length; i += 4) {
- const alpha = transferMap ? transferMap[maskData[i]] : maskData[i];
- layerData[i] = layerData[i] * alpha * scale | 0;
- }
- }
- function composeSMaskLuminosity(maskData, layerData, transferMap) {
- const length = maskData.length;
- for (let i = 3; i < length; i += 4) {
- const y = maskData[i - 3] * 77 + maskData[i - 2] * 152 + maskData[i - 1] * 28;
- layerData[i] = transferMap ? layerData[i] * transferMap[y >> 8] >> 8 : layerData[i] * y >> 16;
- }
- }
- function genericComposeSMask(maskCtx, layerCtx, width, height, subtype, backdrop, transferMap) {
- const hasBackdrop = !!backdrop;
- const r0 = hasBackdrop ? backdrop[0] : 0;
- const g0 = hasBackdrop ? backdrop[1] : 0;
- const b0 = hasBackdrop ? backdrop[2] : 0;
- let composeFn;
- if (subtype === "Luminosity") {
- composeFn = composeSMaskLuminosity;
- } else {
- composeFn = composeSMaskAlpha;
- }
- const PIXELS_TO_PROCESS = 1048576;
- const chunkSize = Math.min(height, Math.ceil(PIXELS_TO_PROCESS / width));
- for (let row = 0; row < height; row += chunkSize) {
- const chunkHeight = Math.min(chunkSize, height - row);
- const maskData = maskCtx.getImageData(0, row, width, chunkHeight);
- const layerData = layerCtx.getImageData(0, row, width, chunkHeight);
- if (hasBackdrop) {
- composeSMaskBackdrop(maskData.data, r0, g0, b0);
- }
- composeFn(maskData.data, layerData.data, transferMap);
- maskCtx.putImageData(layerData, 0, row);
- }
- }
- function composeSMask(ctx, smask, layerCtx) {
- const mask = smask.canvas;
- const maskCtx = smask.context;
- ctx.setTransform(smask.scaleX, 0, 0, smask.scaleY, smask.offsetX, smask.offsetY);
- genericComposeSMask(maskCtx, layerCtx, mask.width, mask.height, smask.subtype, smask.backdrop, smask.transferMap);
- ctx.drawImage(mask, 0, 0);
- }
- const LINE_CAP_STYLES = ["butt", "round", "square"];
- const LINE_JOIN_STYLES = ["miter", "round", "bevel"];
- const NORMAL_CLIP = {};
- const EO_CLIP = {};
- class CanvasGraphics {
- constructor(canvasCtx, commonObjs, objs, canvasFactory, imageLayer, optionalContentConfig) {
- this.ctx = canvasCtx;
- this.current = new CanvasExtraState();
- this.stateStack = [];
- this.pendingClip = null;
- this.pendingEOFill = false;
- this.res = null;
- this.xobjs = null;
- this.commonObjs = commonObjs;
- this.objs = objs;
- this.canvasFactory = canvasFactory;
- this.imageLayer = imageLayer;
- this.groupStack = [];
- this.processingType3 = null;
- this.baseTransform = null;
- this.baseTransformStack = [];
- this.groupLevel = 0;
- this.smaskStack = [];
- this.smaskCounter = 0;
- this.tempSMask = null;
- this.contentVisible = true;
- this.markedContentStack = [];
- this.optionalContentConfig = optionalContentConfig;
- this.cachedCanvases = new CachedCanvases(this.canvasFactory);
- this.cachedPatterns = new Map();
- if (canvasCtx) {
- addContextCurrentTransform(canvasCtx);
- }
- this._cachedGetSinglePixelWidth = null;
- }
- beginDrawing({
- transform,
- viewport,
- transparency = false,
- background = null
- }) {
- const width = this.ctx.canvas.width;
- const height = this.ctx.canvas.height;
- this.ctx.save();
- this.ctx.fillStyle = background || "rgb(255, 255, 255)";
- this.ctx.fillRect(0, 0, width, height);
- this.ctx.restore();
- if (transparency) {
- const transparentCanvas = this.cachedCanvases.getCanvas("transparent", width, height, true);
- this.compositeCtx = this.ctx;
- this.transparentCanvas = transparentCanvas.canvas;
- this.ctx = transparentCanvas.context;
- this.ctx.save();
- this.ctx.transform.apply(this.ctx, this.compositeCtx.mozCurrentTransform);
- }
- this.ctx.save();
- resetCtxToDefault(this.ctx);
- if (transform) {
- this.ctx.transform.apply(this.ctx, transform);
- }
- this.ctx.transform.apply(this.ctx, viewport.transform);
- this.baseTransform = this.ctx.mozCurrentTransform.slice();
- this._combinedScaleFactor = Math.hypot(this.baseTransform[0], this.baseTransform[2]);
- if (this.imageLayer) {
- this.imageLayer.beginLayout();
- }
- }
- executeOperatorList(operatorList, executionStartIdx, continueCallback, stepper) {
- const argsArray = operatorList.argsArray;
- const fnArray = operatorList.fnArray;
- let i = executionStartIdx || 0;
- const argsArrayLen = argsArray.length;
- if (argsArrayLen === i) {
- return i;
- }
- const chunkOperations = argsArrayLen - i > EXECUTION_STEPS && typeof continueCallback === "function";
- const endTime = chunkOperations ? Date.now() + EXECUTION_TIME : 0;
- let steps = 0;
- const commonObjs = this.commonObjs;
- const objs = this.objs;
- let fnId;
- while (true) {
- if (stepper !== undefined && i === stepper.nextBreakPoint) {
- stepper.breakIt(i, continueCallback);
- return i;
- }
- fnId = fnArray[i];
- if (fnId !== _util.OPS.dependency) {
- this[fnId].apply(this, argsArray[i]);
- } else {
- for (const depObjId of argsArray[i]) {
- const objsPool = depObjId.startsWith("g_") ? commonObjs : objs;
- if (!objsPool.has(depObjId)) {
- objsPool.get(depObjId, continueCallback);
- return i;
- }
- }
- }
- i++;
- if (i === argsArrayLen) {
- return i;
- }
- if (chunkOperations && ++steps > EXECUTION_STEPS) {
- if (Date.now() > endTime) {
- continueCallback();
- return i;
- }
- steps = 0;
- }
- }
- }
- endDrawing() {
- while (this.stateStack.length || this.current.activeSMask !== null) {
- this.restore();
- }
- this.ctx.restore();
- if (this.transparentCanvas) {
- this.ctx = this.compositeCtx;
- this.ctx.save();
- this.ctx.setTransform(1, 0, 0, 1, 0, 0);
- this.ctx.drawImage(this.transparentCanvas, 0, 0);
- this.ctx.restore();
- this.transparentCanvas = null;
- }
- this.cachedCanvases.clear();
- this.cachedPatterns.clear();
- if (this.imageLayer) {
- this.imageLayer.endLayout();
- }
- }
- _scaleImage(img, inverseTransform) {
- const width = img.width;
- const height = img.height;
- let widthScale = Math.max(Math.hypot(inverseTransform[0], inverseTransform[1]), 1);
- let heightScale = Math.max(Math.hypot(inverseTransform[2], inverseTransform[3]), 1);
- let paintWidth = width,
- paintHeight = height;
- let tmpCanvasId = "prescale1";
- let tmpCanvas, tmpCtx;
- while (widthScale > 2 && paintWidth > 1 || heightScale > 2 && paintHeight > 1) {
- let newWidth = paintWidth,
- newHeight = paintHeight;
- if (widthScale > 2 && paintWidth > 1) {
- newWidth = Math.ceil(paintWidth / 2);
- widthScale /= paintWidth / newWidth;
- }
- if (heightScale > 2 && paintHeight > 1) {
- newHeight = Math.ceil(paintHeight / 2);
- heightScale /= paintHeight / newHeight;
- }
- tmpCanvas = this.cachedCanvases.getCanvas(tmpCanvasId, newWidth, newHeight);
- tmpCtx = tmpCanvas.context;
- tmpCtx.clearRect(0, 0, newWidth, newHeight);
- tmpCtx.drawImage(img, 0, 0, paintWidth, paintHeight, 0, 0, newWidth, newHeight);
- img = tmpCanvas.canvas;
- paintWidth = newWidth;
- paintHeight = newHeight;
- tmpCanvasId = tmpCanvasId === "prescale1" ? "prescale2" : "prescale1";
- }
- return {
- img,
- paintWidth,
- paintHeight
- };
- }
- _createMaskCanvas(img) {
- const ctx = this.ctx;
- const width = img.width,
- height = img.height;
- const fillColor = this.current.fillColor;
- const isPatternFill = this.current.patternFill;
- const maskCanvas = this.cachedCanvases.getCanvas("maskCanvas", width, height);
- const maskCtx = maskCanvas.context;
- putBinaryImageMask(maskCtx, img);
- const objToCanvas = ctx.mozCurrentTransform;
- let maskToCanvas = _util.Util.transform(objToCanvas, [1 / width, 0, 0, -1 / height, 0, 0]);
- maskToCanvas = _util.Util.transform(maskToCanvas, [1, 0, 0, 1, 0, -height]);
- const cord1 = _util.Util.applyTransform([0, 0], maskToCanvas);
- const cord2 = _util.Util.applyTransform([width, height], maskToCanvas);
- const rect = _util.Util.normalizeRect([cord1[0], cord1[1], cord2[0], cord2[1]]);
- const drawnWidth = Math.ceil(rect[2] - rect[0]);
- const drawnHeight = Math.ceil(rect[3] - rect[1]);
- const fillCanvas = this.cachedCanvases.getCanvas("fillCanvas", drawnWidth, drawnHeight, true);
- const fillCtx = fillCanvas.context;
- const offsetX = Math.min(cord1[0], cord2[0]);
- const offsetY = Math.min(cord1[1], cord2[1]);
- fillCtx.translate(-offsetX, -offsetY);
- fillCtx.transform.apply(fillCtx, maskToCanvas);
- const scaled = this._scaleImage(maskCanvas.canvas, fillCtx.mozCurrentTransformInverse);
- fillCtx.drawImage(scaled.img, 0, 0, scaled.img.width, scaled.img.height, 0, 0, width, height);
- fillCtx.globalCompositeOperation = "source-in";
- const inverse = _util.Util.transform(fillCtx.mozCurrentTransformInverse, [1, 0, 0, 1, -offsetX, -offsetY]);
- fillCtx.fillStyle = isPatternFill ? fillColor.getPattern(ctx, this, inverse, false) : fillColor;
- fillCtx.fillRect(0, 0, width, height);
- return {
- canvas: fillCanvas.canvas,
- offsetX: Math.round(offsetX),
- offsetY: Math.round(offsetY)
- };
- }
- setLineWidth(width) {
- this.current.lineWidth = width;
- this.ctx.lineWidth = width;
- }
- setLineCap(style) {
- this.ctx.lineCap = LINE_CAP_STYLES[style];
- }
- setLineJoin(style) {
- this.ctx.lineJoin = LINE_JOIN_STYLES[style];
- }
- setMiterLimit(limit) {
- this.ctx.miterLimit = limit;
- }
- setDash(dashArray, dashPhase) {
- const ctx = this.ctx;
- if (ctx.setLineDash !== undefined) {
- ctx.setLineDash(dashArray);
- ctx.lineDashOffset = dashPhase;
- }
- }
- setRenderingIntent(intent) {}
- setFlatness(flatness) {}
- setGState(states) {
- for (let i = 0, ii = states.length; i < ii; i++) {
- const state = states[i];
- const key = state[0];
- const value = state[1];
- switch (key) {
- case "LW":
- this.setLineWidth(value);
- break;
- case "LC":
- this.setLineCap(value);
- break;
- case "LJ":
- this.setLineJoin(value);
- break;
- case "ML":
- this.setMiterLimit(value);
- break;
- case "D":
- this.setDash(value[0], value[1]);
- break;
- case "RI":
- this.setRenderingIntent(value);
- break;
- case "FL":
- this.setFlatness(value);
- break;
- case "Font":
- this.setFont(value[0], value[1]);
- break;
- case "CA":
- this.current.strokeAlpha = state[1];
- break;
- case "ca":
- this.current.fillAlpha = state[1];
- this.ctx.globalAlpha = state[1];
- break;
- case "BM":
- this.ctx.globalCompositeOperation = value;
- break;
- case "SMask":
- if (this.current.activeSMask) {
- if (this.stateStack.length > 0 && this.stateStack[this.stateStack.length - 1].activeSMask === this.current.activeSMask) {
- this.suspendSMaskGroup();
- } else {
- this.endSMaskGroup();
- }
- }
- this.current.activeSMask = value ? this.tempSMask : null;
- if (this.current.activeSMask) {
- this.beginSMaskGroup();
- }
- this.tempSMask = null;
- break;
- case "TR":
- this.current.transferMaps = value;
- }
- }
- }
- beginSMaskGroup() {
- const activeSMask = this.current.activeSMask;
- const drawnWidth = activeSMask.canvas.width;
- const drawnHeight = activeSMask.canvas.height;
- const cacheId = "smaskGroupAt" + this.groupLevel;
- const scratchCanvas = this.cachedCanvases.getCanvas(cacheId, drawnWidth, drawnHeight, true);
- const currentCtx = this.ctx;
- const currentTransform = currentCtx.mozCurrentTransform;
- this.ctx.save();
- const groupCtx = scratchCanvas.context;
- groupCtx.scale(1 / activeSMask.scaleX, 1 / activeSMask.scaleY);
- groupCtx.translate(-activeSMask.offsetX, -activeSMask.offsetY);
- groupCtx.transform.apply(groupCtx, currentTransform);
- activeSMask.startTransformInverse = groupCtx.mozCurrentTransformInverse;
- copyCtxState(currentCtx, groupCtx);
- this.ctx = groupCtx;
- this.setGState([["BM", "source-over"], ["ca", 1], ["CA", 1]]);
- this.groupStack.push(currentCtx);
- this.groupLevel++;
- }
- suspendSMaskGroup() {
- const groupCtx = this.ctx;
- this.groupLevel--;
- this.ctx = this.groupStack.pop();
- composeSMask(this.ctx, this.current.activeSMask, groupCtx);
- this.ctx.restore();
- this.ctx.save();
- copyCtxState(groupCtx, this.ctx);
- this.current.resumeSMaskCtx = groupCtx;
- const deltaTransform = _util.Util.transform(this.current.activeSMask.startTransformInverse, groupCtx.mozCurrentTransform);
- this.ctx.transform.apply(this.ctx, deltaTransform);
- groupCtx.save();
- groupCtx.setTransform(1, 0, 0, 1, 0, 0);
- groupCtx.clearRect(0, 0, groupCtx.canvas.width, groupCtx.canvas.height);
- groupCtx.restore();
- }
- resumeSMaskGroup() {
- const groupCtx = this.current.resumeSMaskCtx;
- const currentCtx = this.ctx;
- this.ctx = groupCtx;
- this.groupStack.push(currentCtx);
- this.groupLevel++;
- }
- endSMaskGroup() {
- const groupCtx = this.ctx;
- this.groupLevel--;
- this.ctx = this.groupStack.pop();
- composeSMask(this.ctx, this.current.activeSMask, groupCtx);
- this.ctx.restore();
- copyCtxState(groupCtx, this.ctx);
- const deltaTransform = _util.Util.transform(this.current.activeSMask.startTransformInverse, groupCtx.mozCurrentTransform);
- this.ctx.transform.apply(this.ctx, deltaTransform);
- }
- save() {
- this.ctx.save();
- const old = this.current;
- this.stateStack.push(old);
- this.current = old.clone();
- this.current.resumeSMaskCtx = null;
- }
- restore() {
- if (this.current.resumeSMaskCtx) {
- this.resumeSMaskGroup();
- }
- if (this.current.activeSMask !== null && (this.stateStack.length === 0 || this.stateStack[this.stateStack.length - 1].activeSMask !== this.current.activeSMask)) {
- this.endSMaskGroup();
- }
- if (this.stateStack.length !== 0) {
- this.current = this.stateStack.pop();
- this.ctx.restore();
- this.pendingClip = null;
- this._cachedGetSinglePixelWidth = null;
- } else {
- this.current.activeSMask = null;
- }
- }
- transform(a, b, c, d, e, f) {
- this.ctx.transform(a, b, c, d, e, f);
- this._cachedGetSinglePixelWidth = null;
- }
- constructPath(ops, args) {
- const ctx = this.ctx;
- const current = this.current;
- let x = current.x,
- y = current.y;
- for (let i = 0, j = 0, ii = ops.length; i < ii; i++) {
- switch (ops[i] | 0) {
- case _util.OPS.rectangle:
- x = args[j++];
- y = args[j++];
- const width = args[j++];
- const height = args[j++];
- const xw = x + width;
- const yh = y + height;
- ctx.moveTo(x, y);
- if (width === 0 || height === 0) {
- ctx.lineTo(xw, yh);
- } else {
- ctx.lineTo(xw, y);
- ctx.lineTo(xw, yh);
- ctx.lineTo(x, yh);
- }
- ctx.closePath();
- break;
- case _util.OPS.moveTo:
- x = args[j++];
- y = args[j++];
- ctx.moveTo(x, y);
- break;
- case _util.OPS.lineTo:
- x = args[j++];
- y = args[j++];
- ctx.lineTo(x, y);
- break;
- case _util.OPS.curveTo:
- x = args[j + 4];
- y = args[j + 5];
- ctx.bezierCurveTo(args[j], args[j + 1], args[j + 2], args[j + 3], x, y);
- j += 6;
- break;
- case _util.OPS.curveTo2:
- ctx.bezierCurveTo(x, y, args[j], args[j + 1], args[j + 2], args[j + 3]);
- x = args[j + 2];
- y = args[j + 3];
- j += 4;
- break;
- case _util.OPS.curveTo3:
- x = args[j + 2];
- y = args[j + 3];
- ctx.bezierCurveTo(args[j], args[j + 1], x, y, x, y);
- j += 4;
- break;
- case _util.OPS.closePath:
- ctx.closePath();
- break;
- }
- }
- current.setCurrentPoint(x, y);
- }
- closePath() {
- this.ctx.closePath();
- }
- stroke(consumePath) {
- consumePath = typeof consumePath !== "undefined" ? consumePath : true;
- const ctx = this.ctx;
- const strokeColor = this.current.strokeColor;
- ctx.globalAlpha = this.current.strokeAlpha;
- if (this.contentVisible) {
- if (typeof strokeColor === "object" && strokeColor?.getPattern) {
- const lineWidth = this.getSinglePixelWidth();
- ctx.save();
- ctx.strokeStyle = strokeColor.getPattern(ctx, this, ctx.mozCurrentTransformInverse);
- ctx.lineWidth = Math.max(lineWidth, this.current.lineWidth);
- ctx.stroke();
- ctx.restore();
- } else {
- const lineWidth = this.getSinglePixelWidth();
- if (lineWidth < 0 && -lineWidth >= this.current.lineWidth) {
- ctx.save();
- ctx.resetTransform();
- ctx.lineWidth = Math.round(this._combinedScaleFactor);
- ctx.stroke();
- ctx.restore();
- } else {
- ctx.lineWidth = Math.max(lineWidth, this.current.lineWidth);
- ctx.stroke();
- }
- }
- }
- if (consumePath) {
- this.consumePath();
- }
- ctx.globalAlpha = this.current.fillAlpha;
- }
- closeStroke() {
- this.closePath();
- this.stroke();
- }
- fill(consumePath) {
- consumePath = typeof consumePath !== "undefined" ? consumePath : true;
- const ctx = this.ctx;
- const fillColor = this.current.fillColor;
- const isPatternFill = this.current.patternFill;
- let needRestore = false;
- if (isPatternFill) {
- ctx.save();
- ctx.fillStyle = fillColor.getPattern(ctx, this, ctx.mozCurrentTransformInverse);
- needRestore = true;
- }
- if (this.contentVisible) {
- if (this.pendingEOFill) {
- ctx.fill("evenodd");
- this.pendingEOFill = false;
- } else {
- ctx.fill();
- }
- }
- if (needRestore) {
- ctx.restore();
- }
- if (consumePath) {
- this.consumePath();
- }
- }
- eoFill() {
- this.pendingEOFill = true;
- this.fill();
- }
- fillStroke() {
- this.fill(false);
- this.stroke(false);
- this.consumePath();
- }
- eoFillStroke() {
- this.pendingEOFill = true;
- this.fillStroke();
- }
- closeFillStroke() {
- this.closePath();
- this.fillStroke();
- }
- closeEOFillStroke() {
- this.pendingEOFill = true;
- this.closePath();
- this.fillStroke();
- }
- endPath() {
- this.consumePath();
- }
- clip() {
- this.pendingClip = NORMAL_CLIP;
- }
- eoClip() {
- this.pendingClip = EO_CLIP;
- }
- beginText() {
- this.current.textMatrix = _util.IDENTITY_MATRIX;
- this.current.textMatrixScale = 1;
- this.current.x = this.current.lineX = 0;
- this.current.y = this.current.lineY = 0;
- }
- endText() {
- const paths = this.pendingTextPaths;
- const ctx = this.ctx;
- if (paths === undefined) {
- ctx.beginPath();
- return;
- }
- ctx.save();
- ctx.beginPath();
- for (let i = 0; i < paths.length; i++) {
- const path = paths[i];
- ctx.setTransform.apply(ctx, path.transform);
- ctx.translate(path.x, path.y);
- path.addToPath(ctx, path.fontSize);
- }
- ctx.restore();
- ctx.clip();
- ctx.beginPath();
- delete this.pendingTextPaths;
- }
- setCharSpacing(spacing) {
- this.current.charSpacing = spacing;
- }
- setWordSpacing(spacing) {
- this.current.wordSpacing = spacing;
- }
- setHScale(scale) {
- this.current.textHScale = scale / 100;
- }
- setLeading(leading) {
- this.current.leading = -leading;
- }
- setFont(fontRefName, size) {
- const fontObj = this.commonObjs.get(fontRefName);
- const current = this.current;
- if (!fontObj) {
- throw new Error(`Can't find font for ${fontRefName}`);
- }
- current.fontMatrix = fontObj.fontMatrix || _util.FONT_IDENTITY_MATRIX;
- if (current.fontMatrix[0] === 0 || current.fontMatrix[3] === 0) {
- (0, _util.warn)("Invalid font matrix for font " + fontRefName);
- }
- if (size < 0) {
- size = -size;
- current.fontDirection = -1;
- } else {
- current.fontDirection = 1;
- }
- this.current.font = fontObj;
- this.current.fontSize = size;
- if (fontObj.isType3Font) {
- return;
- }
- const name = fontObj.loadedName || "sans-serif";
- let bold = "normal";
- if (fontObj.black) {
- bold = "900";
- } else if (fontObj.bold) {
- bold = "bold";
- }
- const italic = fontObj.italic ? "italic" : "normal";
- const typeface = `"${name}", ${fontObj.fallbackName}`;
- let browserFontSize = size;
- if (size < MIN_FONT_SIZE) {
- browserFontSize = MIN_FONT_SIZE;
- } else if (size > MAX_FONT_SIZE) {
- browserFontSize = MAX_FONT_SIZE;
- }
- this.current.fontSizeScale = size / browserFontSize;
- this.ctx.font = `${italic} ${bold} ${browserFontSize}px ${typeface}`;
- }
- setTextRenderingMode(mode) {
- this.current.textRenderingMode = mode;
- }
- setTextRise(rise) {
- this.current.textRise = rise;
- }
- moveText(x, y) {
- this.current.x = this.current.lineX += x;
- this.current.y = this.current.lineY += y;
- }
- setLeadingMoveText(x, y) {
- this.setLeading(-y);
- this.moveText(x, y);
- }
- setTextMatrix(a, b, c, d, e, f) {
- this.current.textMatrix = [a, b, c, d, e, f];
- this.current.textMatrixScale = Math.hypot(a, b);
- this.current.x = this.current.lineX = 0;
- this.current.y = this.current.lineY = 0;
- }
- nextLine() {
- this.moveText(0, this.current.leading);
- }
- paintChar(character, x, y, patternTransform, resetLineWidthToOne) {
- const ctx = this.ctx;
- const current = this.current;
- const font = current.font;
- const textRenderingMode = current.textRenderingMode;
- const fontSize = current.fontSize / current.fontSizeScale;
- const fillStrokeMode = textRenderingMode & _util.TextRenderingMode.FILL_STROKE_MASK;
- const isAddToPathSet = !!(textRenderingMode & _util.TextRenderingMode.ADD_TO_PATH_FLAG);
- const patternFill = current.patternFill && !font.missingFile;
- let addToPath;
- if (font.disableFontFace || isAddToPathSet || patternFill) {
- addToPath = font.getPathGenerator(this.commonObjs, character);
- }
- if (font.disableFontFace || patternFill) {
- ctx.save();
- ctx.translate(x, y);
- ctx.beginPath();
- addToPath(ctx, fontSize);
- if (patternTransform) {
- ctx.setTransform.apply(ctx, patternTransform);
- }
- if (fillStrokeMode === _util.TextRenderingMode.FILL || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
- ctx.fill();
- }
- if (fillStrokeMode === _util.TextRenderingMode.STROKE || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
- if (resetLineWidthToOne) {
- ctx.resetTransform();
- ctx.lineWidth = Math.round(this._combinedScaleFactor);
- }
- ctx.stroke();
- }
- ctx.restore();
- } else {
- if (fillStrokeMode === _util.TextRenderingMode.FILL || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
- ctx.fillText(character, x, y);
- }
- if (fillStrokeMode === _util.TextRenderingMode.STROKE || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
- if (resetLineWidthToOne) {
- ctx.save();
- ctx.moveTo(x, y);
- ctx.resetTransform();
- ctx.lineWidth = Math.round(this._combinedScaleFactor);
- ctx.strokeText(character, 0, 0);
- ctx.restore();
- } else {
- ctx.strokeText(character, x, y);
- }
- }
- }
- if (isAddToPathSet) {
- const paths = this.pendingTextPaths || (this.pendingTextPaths = []);
- paths.push({
- transform: ctx.mozCurrentTransform,
- x,
- y,
- fontSize,
- addToPath
- });
- }
- }
- get isFontSubpixelAAEnabled() {
- const {
- context: ctx
- } = this.cachedCanvases.getCanvas("isFontSubpixelAAEnabled", 10, 10);
- ctx.scale(1.5, 1);
- ctx.fillText("I", 0, 10);
- const data = ctx.getImageData(0, 0, 10, 10).data;
- let enabled = false;
- for (let i = 3; i < data.length; i += 4) {
- if (data[i] > 0 && data[i] < 255) {
- enabled = true;
- break;
- }
- }
- return (0, _util.shadow)(this, "isFontSubpixelAAEnabled", enabled);
- }
- showText(glyphs) {
- const current = this.current;
- const font = current.font;
- if (font.isType3Font) {
- return this.showType3Text(glyphs);
- }
- const fontSize = current.fontSize;
- if (fontSize === 0) {
- return undefined;
- }
- const ctx = this.ctx;
- const fontSizeScale = current.fontSizeScale;
- const charSpacing = current.charSpacing;
- const wordSpacing = current.wordSpacing;
- const fontDirection = current.fontDirection;
- const textHScale = current.textHScale * fontDirection;
- const glyphsLength = glyphs.length;
- const vertical = font.vertical;
- const spacingDir = vertical ? 1 : -1;
- const defaultVMetrics = font.defaultVMetrics;
- const widthAdvanceScale = fontSize * current.fontMatrix[0];
- const simpleFillText = current.textRenderingMode === _util.TextRenderingMode.FILL && !font.disableFontFace && !current.patternFill;
- ctx.save();
- let patternTransform;
- if (current.patternFill) {
- ctx.save();
- const pattern = current.fillColor.getPattern(ctx, this, ctx.mozCurrentTransformInverse);
- patternTransform = ctx.mozCurrentTransform;
- ctx.restore();
- ctx.fillStyle = pattern;
- }
- ctx.transform.apply(ctx, current.textMatrix);
- ctx.translate(current.x, current.y + current.textRise);
- if (fontDirection > 0) {
- ctx.scale(textHScale, -1);
- } else {
- ctx.scale(textHScale, 1);
- }
- let lineWidth = current.lineWidth;
- let resetLineWidthToOne = false;
- const scale = current.textMatrixScale;
- if (scale === 0 || lineWidth === 0) {
- const fillStrokeMode = current.textRenderingMode & _util.TextRenderingMode.FILL_STROKE_MASK;
- if (fillStrokeMode === _util.TextRenderingMode.STROKE || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
- this._cachedGetSinglePixelWidth = null;
- lineWidth = this.getSinglePixelWidth();
- resetLineWidthToOne = lineWidth < 0;
- }
- } else {
- lineWidth /= scale;
- }
- if (fontSizeScale !== 1.0) {
- ctx.scale(fontSizeScale, fontSizeScale);
- lineWidth /= fontSizeScale;
- }
- ctx.lineWidth = lineWidth;
- let x = 0,
- i;
- for (i = 0; i < glyphsLength; ++i) {
- const glyph = glyphs[i];
- if ((0, _util.isNum)(glyph)) {
- x += spacingDir * glyph * fontSize / 1000;
- continue;
- }
- let restoreNeeded = false;
- const spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
- const character = glyph.fontChar;
- const accent = glyph.accent;
- let scaledX, scaledY;
- let width = glyph.width;
- if (vertical) {
- const vmetric = glyph.vmetric || defaultVMetrics;
- const vx = -(glyph.vmetric ? vmetric[1] : width * 0.5) * widthAdvanceScale;
- const vy = vmetric[2] * widthAdvanceScale;
- width = vmetric ? -vmetric[0] : width;
- scaledX = vx / fontSizeScale;
- scaledY = (x + vy) / fontSizeScale;
- } else {
- scaledX = x / fontSizeScale;
- scaledY = 0;
- }
- if (font.remeasure && width > 0) {
- const measuredWidth = ctx.measureText(character).width * 1000 / fontSize * fontSizeScale;
- if (width < measuredWidth && this.isFontSubpixelAAEnabled) {
- const characterScaleX = width / measuredWidth;
- restoreNeeded = true;
- ctx.save();
- ctx.scale(characterScaleX, 1);
- scaledX /= characterScaleX;
- } else if (width !== measuredWidth) {
- scaledX += (width - measuredWidth) / 2000 * fontSize / fontSizeScale;
- }
- }
- if (this.contentVisible && (glyph.isInFont || font.missingFile)) {
- if (simpleFillText && !accent) {
- ctx.fillText(character, scaledX, scaledY);
- } else {
- this.paintChar(character, scaledX, scaledY, patternTransform, resetLineWidthToOne);
- if (accent) {
- const scaledAccentX = scaledX + fontSize * accent.offset.x / fontSizeScale;
- const scaledAccentY = scaledY - fontSize * accent.offset.y / fontSizeScale;
- this.paintChar(accent.fontChar, scaledAccentX, scaledAccentY, patternTransform, resetLineWidthToOne);
- }
- }
- }
- let charWidth;
- if (vertical) {
- charWidth = width * widthAdvanceScale - spacing * fontDirection;
- } else {
- charWidth = width * widthAdvanceScale + spacing * fontDirection;
- }
- x += charWidth;
- if (restoreNeeded) {
- ctx.restore();
- }
- }
- if (vertical) {
- current.y -= x;
- } else {
- current.x += x * textHScale;
- }
- ctx.restore();
- return undefined;
- }
- showType3Text(glyphs) {
- const ctx = this.ctx;
- const current = this.current;
- const font = current.font;
- const fontSize = current.fontSize;
- const fontDirection = current.fontDirection;
- const spacingDir = font.vertical ? 1 : -1;
- const charSpacing = current.charSpacing;
- const wordSpacing = current.wordSpacing;
- const textHScale = current.textHScale * fontDirection;
- const fontMatrix = current.fontMatrix || _util.FONT_IDENTITY_MATRIX;
- const glyphsLength = glyphs.length;
- const isTextInvisible = current.textRenderingMode === _util.TextRenderingMode.INVISIBLE;
- let i, glyph, width, spacingLength;
- if (isTextInvisible || fontSize === 0) {
- return;
- }
- this._cachedGetSinglePixelWidth = null;
- ctx.save();
- ctx.transform.apply(ctx, current.textMatrix);
- ctx.translate(current.x, current.y);
- ctx.scale(textHScale, fontDirection);
- for (i = 0; i < glyphsLength; ++i) {
- glyph = glyphs[i];
- if ((0, _util.isNum)(glyph)) {
- spacingLength = spacingDir * glyph * fontSize / 1000;
- this.ctx.translate(spacingLength, 0);
- current.x += spacingLength * textHScale;
- continue;
- }
- const spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
- const operatorList = font.charProcOperatorList[glyph.operatorListId];
- if (!operatorList) {
- (0, _util.warn)(`Type3 character "${glyph.operatorListId}" is not available.`);
- continue;
- }
- if (this.contentVisible) {
- this.processingType3 = glyph;
- this.save();
- ctx.scale(fontSize, fontSize);
- ctx.transform.apply(ctx, fontMatrix);
- this.executeOperatorList(operatorList);
- this.restore();
- }
- const transformed = _util.Util.applyTransform([glyph.width, 0], fontMatrix);
- width = transformed[0] * fontSize + spacing;
- ctx.translate(width, 0);
- current.x += width * textHScale;
- }
- ctx.restore();
- this.processingType3 = null;
- }
- setCharWidth(xWidth, yWidth) {}
- setCharWidthAndBounds(xWidth, yWidth, llx, lly, urx, ury) {
- this.ctx.rect(llx, lly, urx - llx, ury - lly);
- this.clip();
- this.endPath();
- }
- getColorN_Pattern(IR) {
- let pattern;
- if (IR[0] === "TilingPattern") {
- const color = IR[1];
- const baseTransform = this.baseTransform || this.ctx.mozCurrentTransform.slice();
- const canvasGraphicsFactory = {
- createCanvasGraphics: ctx => {
- return new CanvasGraphics(ctx, this.commonObjs, this.objs, this.canvasFactory);
- }
- };
- pattern = new _pattern_helper.TilingPattern(IR, color, this.ctx, canvasGraphicsFactory, baseTransform);
- } else {
- pattern = this._getPattern(IR[1]);
- }
- return pattern;
- }
- setStrokeColorN() {
- this.current.strokeColor = this.getColorN_Pattern(arguments);
- }
- setFillColorN() {
- this.current.fillColor = this.getColorN_Pattern(arguments);
- this.current.patternFill = true;
- }
- setStrokeRGBColor(r, g, b) {
- const color = _util.Util.makeHexColor(r, g, b);
- this.ctx.strokeStyle = color;
- this.current.strokeColor = color;
- }
- setFillRGBColor(r, g, b) {
- const color = _util.Util.makeHexColor(r, g, b);
- this.ctx.fillStyle = color;
- this.current.fillColor = color;
- this.current.patternFill = false;
- }
- _getPattern(objId) {
- if (this.cachedPatterns.has(objId)) {
- return this.cachedPatterns.get(objId);
- }
- const pattern = (0, _pattern_helper.getShadingPattern)(this.objs.get(objId));
- this.cachedPatterns.set(objId, pattern);
- return pattern;
- }
- shadingFill(objId) {
- if (!this.contentVisible) {
- return;
- }
- const ctx = this.ctx;
- this.save();
- const pattern = this._getPattern(objId);
- ctx.fillStyle = pattern.getPattern(ctx, this, ctx.mozCurrentTransformInverse, true);
- const inv = ctx.mozCurrentTransformInverse;
- if (inv) {
- const canvas = ctx.canvas;
- const width = canvas.width;
- const height = canvas.height;
- const bl = _util.Util.applyTransform([0, 0], inv);
- const br = _util.Util.applyTransform([0, height], inv);
- const ul = _util.Util.applyTransform([width, 0], inv);
- const ur = _util.Util.applyTransform([width, height], inv);
- const x0 = Math.min(bl[0], br[0], ul[0], ur[0]);
- const y0 = Math.min(bl[1], br[1], ul[1], ur[1]);
- const x1 = Math.max(bl[0], br[0], ul[0], ur[0]);
- const y1 = Math.max(bl[1], br[1], ul[1], ur[1]);
- this.ctx.fillRect(x0, y0, x1 - x0, y1 - y0);
- } else {
- this.ctx.fillRect(-1e10, -1e10, 2e10, 2e10);
- }
- this.restore();
- }
- beginInlineImage() {
- (0, _util.unreachable)("Should not call beginInlineImage");
- }
- beginImageData() {
- (0, _util.unreachable)("Should not call beginImageData");
- }
- paintFormXObjectBegin(matrix, bbox) {
- if (!this.contentVisible) {
- return;
- }
- this.save();
- this.baseTransformStack.push(this.baseTransform);
- if (Array.isArray(matrix) && matrix.length === 6) {
- this.transform.apply(this, matrix);
- }
- this.baseTransform = this.ctx.mozCurrentTransform;
- if (bbox) {
- const width = bbox[2] - bbox[0];
- const height = bbox[3] - bbox[1];
- this.ctx.rect(bbox[0], bbox[1], width, height);
- this.clip();
- this.endPath();
- }
- }
- paintFormXObjectEnd() {
- if (!this.contentVisible) {
- return;
- }
- this.restore();
- this.baseTransform = this.baseTransformStack.pop();
- }
- beginGroup(group) {
- if (!this.contentVisible) {
- return;
- }
- this.save();
- const currentCtx = this.ctx;
- if (!group.isolated) {
- (0, _util.info)("TODO: Support non-isolated groups.");
- }
- if (group.knockout) {
- (0, _util.warn)("Knockout groups not supported.");
- }
- const currentTransform = currentCtx.mozCurrentTransform;
- if (group.matrix) {
- currentCtx.transform.apply(currentCtx, group.matrix);
- }
- if (!group.bbox) {
- throw new Error("Bounding box is required.");
- }
- let bounds = _util.Util.getAxialAlignedBoundingBox(group.bbox, currentCtx.mozCurrentTransform);
- const canvasBounds = [0, 0, currentCtx.canvas.width, currentCtx.canvas.height];
- bounds = _util.Util.intersect(bounds, canvasBounds) || [0, 0, 0, 0];
- const offsetX = Math.floor(bounds[0]);
- const offsetY = Math.floor(bounds[1]);
- let drawnWidth = Math.max(Math.ceil(bounds[2]) - offsetX, 1);
- let drawnHeight = Math.max(Math.ceil(bounds[3]) - offsetY, 1);
- let scaleX = 1,
- scaleY = 1;
- if (drawnWidth > MAX_GROUP_SIZE) {
- scaleX = drawnWidth / MAX_GROUP_SIZE;
- drawnWidth = MAX_GROUP_SIZE;
- }
- if (drawnHeight > MAX_GROUP_SIZE) {
- scaleY = drawnHeight / MAX_GROUP_SIZE;
- drawnHeight = MAX_GROUP_SIZE;
- }
- let cacheId = "groupAt" + this.groupLevel;
- if (group.smask) {
- cacheId += "_smask_" + this.smaskCounter++ % 2;
- }
- const scratchCanvas = this.cachedCanvases.getCanvas(cacheId, drawnWidth, drawnHeight, true);
- const groupCtx = scratchCanvas.context;
- groupCtx.scale(1 / scaleX, 1 / scaleY);
- groupCtx.translate(-offsetX, -offsetY);
- groupCtx.transform.apply(groupCtx, currentTransform);
- if (group.smask) {
- this.smaskStack.push({
- canvas: scratchCanvas.canvas,
- context: groupCtx,
- offsetX,
- offsetY,
- scaleX,
- scaleY,
- subtype: group.smask.subtype,
- backdrop: group.smask.backdrop,
- transferMap: group.smask.transferMap || null,
- startTransformInverse: null
- });
- } else {
- currentCtx.setTransform(1, 0, 0, 1, 0, 0);
- currentCtx.translate(offsetX, offsetY);
- currentCtx.scale(scaleX, scaleY);
- }
- copyCtxState(currentCtx, groupCtx);
- this.ctx = groupCtx;
- this.setGState([["BM", "source-over"], ["ca", 1], ["CA", 1]]);
- this.groupStack.push(currentCtx);
- this.groupLevel++;
- this.current.activeSMask = null;
- }
- endGroup(group) {
- if (!this.contentVisible) {
- return;
- }
- this.groupLevel--;
- const groupCtx = this.ctx;
- this.ctx = this.groupStack.pop();
- if (this.ctx.imageSmoothingEnabled !== undefined) {
- this.ctx.imageSmoothingEnabled = false;
- } else {
- this.ctx.mozImageSmoothingEnabled = false;
- }
- if (group.smask) {
- this.tempSMask = this.smaskStack.pop();
- } else {
- this.ctx.drawImage(groupCtx.canvas, 0, 0);
- }
- this.restore();
- }
- beginAnnotations() {
- this.save();
- if (this.baseTransform) {
- this.ctx.setTransform.apply(this.ctx, this.baseTransform);
- }
- }
- endAnnotations() {
- this.restore();
- }
- beginAnnotation(id, rect, transform, matrix) {
- this.save();
- resetCtxToDefault(this.ctx);
- this.current = new CanvasExtraState();
- if (Array.isArray(rect) && rect.length === 4) {
- const width = rect[2] - rect[0];
- const height = rect[3] - rect[1];
- this.ctx.rect(rect[0], rect[1], width, height);
- this.clip();
- this.endPath();
- }
- this.transform.apply(this, transform);
- this.transform.apply(this, matrix);
- }
- endAnnotation() {
- this.restore();
- }
- paintImageMaskXObject(img) {
- if (!this.contentVisible) {
- return;
- }
- const ctx = this.ctx;
- const width = img.width,
- height = img.height;
- const glyph = this.processingType3;
- if (COMPILE_TYPE3_GLYPHS && glyph && glyph.compiled === undefined) {
- if (width <= MAX_SIZE_TO_COMPILE && height <= MAX_SIZE_TO_COMPILE) {
- glyph.compiled = compileType3Glyph({
- data: img.data,
- width,
- height
- });
- } else {
- glyph.compiled = null;
- }
- }
- if (glyph?.compiled) {
- glyph.compiled(ctx);
- return;
- }
- const mask = this._createMaskCanvas(img);
- const maskCanvas = mask.canvas;
- ctx.save();
- ctx.setTransform(1, 0, 0, 1, 0, 0);
- ctx.drawImage(maskCanvas, mask.offsetX, mask.offsetY);
- ctx.restore();
- }
- paintImageMaskXObjectRepeat(imgData, scaleX, skewX = 0, skewY = 0, scaleY, positions) {
- if (!this.contentVisible) {
- return;
- }
- const ctx = this.ctx;
- ctx.save();
- const currentTransform = ctx.mozCurrentTransform;
- ctx.transform(scaleX, skewX, skewY, scaleY, 0, 0);
- const mask = this._createMaskCanvas(imgData);
- ctx.setTransform(1, 0, 0, 1, 0, 0);
- for (let i = 0, ii = positions.length; i < ii; i += 2) {
- const trans = _util.Util.transform(currentTransform, [scaleX, skewX, skewY, scaleY, positions[i], positions[i + 1]]);
- const [x, y] = _util.Util.applyTransform([0, 0], trans);
- ctx.drawImage(mask.canvas, x, y);
- }
- ctx.restore();
- }
- paintImageMaskXObjectGroup(images) {
- if (!this.contentVisible) {
- return;
- }
- const ctx = this.ctx;
- const fillColor = this.current.fillColor;
- const isPatternFill = this.current.patternFill;
- for (let i = 0, ii = images.length; i < ii; i++) {
- const image = images[i];
- const width = image.width,
- height = image.height;
- const maskCanvas = this.cachedCanvases.getCanvas("maskCanvas", width, height);
- const maskCtx = maskCanvas.context;
- maskCtx.save();
- putBinaryImageMask(maskCtx, image);
- maskCtx.globalCompositeOperation = "source-in";
- maskCtx.fillStyle = isPatternFill ? fillColor.getPattern(maskCtx, this, ctx.mozCurrentTransformInverse, false) : fillColor;
- maskCtx.fillRect(0, 0, width, height);
- maskCtx.restore();
- ctx.save();
- ctx.transform.apply(ctx, image.transform);
- ctx.scale(1, -1);
- ctx.drawImage(maskCanvas.canvas, 0, 0, width, height, 0, -1, 1, 1);
- ctx.restore();
- }
- }
- paintImageXObject(objId) {
- if (!this.contentVisible) {
- return;
- }
- const imgData = objId.startsWith("g_") ? this.commonObjs.get(objId) : this.objs.get(objId);
- if (!imgData) {
- (0, _util.warn)("Dependent image isn't ready yet");
- return;
- }
- this.paintInlineImageXObject(imgData);
- }
- paintImageXObjectRepeat(objId, scaleX, scaleY, positions) {
- if (!this.contentVisible) {
- return;
- }
- const imgData = objId.startsWith("g_") ? this.commonObjs.get(objId) : this.objs.get(objId);
- if (!imgData) {
- (0, _util.warn)("Dependent image isn't ready yet");
- return;
- }
- const width = imgData.width;
- const height = imgData.height;
- const map = [];
- for (let i = 0, ii = positions.length; i < ii; i += 2) {
- map.push({
- transform: [scaleX, 0, 0, scaleY, positions[i], positions[i + 1]],
- x: 0,
- y: 0,
- w: width,
- h: height
- });
- }
- this.paintInlineImageXObjectGroup(imgData, map);
- }
- paintInlineImageXObject(imgData) {
- if (!this.contentVisible) {
- return;
- }
- const width = imgData.width;
- const height = imgData.height;
- const ctx = this.ctx;
- this.save();
- ctx.scale(1 / width, -1 / height);
- let imgToPaint;
- if (typeof HTMLElement === "function" && imgData instanceof HTMLElement || !imgData.data) {
- imgToPaint = imgData;
- } else {
- const tmpCanvas = this.cachedCanvases.getCanvas("inlineImage", width, height);
- const tmpCtx = tmpCanvas.context;
- putBinaryImageData(tmpCtx, imgData, this.current.transferMaps);
- imgToPaint = tmpCanvas.canvas;
- }
- const scaled = this._scaleImage(imgToPaint, ctx.mozCurrentTransformInverse);
- ctx.drawImage(scaled.img, 0, 0, scaled.paintWidth, scaled.paintHeight, 0, -height, width, height);
- if (this.imageLayer) {
- const position = this.getCanvasPosition(0, -height);
- this.imageLayer.appendImage({
- imgData,
- left: position[0],
- top: position[1],
- width: width / ctx.mozCurrentTransformInverse[0],
- height: height / ctx.mozCurrentTransformInverse[3]
- });
- }
- this.restore();
- }
- paintInlineImageXObjectGroup(imgData, map) {
- if (!this.contentVisible) {
- return;
- }
- const ctx = this.ctx;
- const w = imgData.width;
- const h = imgData.height;
- const tmpCanvas = this.cachedCanvases.getCanvas("inlineImage", w, h);
- const tmpCtx = tmpCanvas.context;
- putBinaryImageData(tmpCtx, imgData, this.current.transferMaps);
- for (let i = 0, ii = map.length; i < ii; i++) {
- const entry = map[i];
- ctx.save();
- ctx.transform.apply(ctx, entry.transform);
- ctx.scale(1, -1);
- ctx.drawImage(tmpCanvas.canvas, entry.x, entry.y, entry.w, entry.h, 0, -1, 1, 1);
- if (this.imageLayer) {
- const position = this.getCanvasPosition(entry.x, entry.y);
- this.imageLayer.appendImage({
- imgData,
- left: position[0],
- top: position[1],
- width: w,
- height: h
- });
- }
- ctx.restore();
- }
- }
- paintSolidColorImageMask() {
- if (!this.contentVisible) {
- return;
- }
- this.ctx.fillRect(0, 0, 1, 1);
- }
- markPoint(tag) {}
- markPointProps(tag, properties) {}
- beginMarkedContent(tag) {
- this.markedContentStack.push({
- visible: true
- });
- }
- beginMarkedContentProps(tag, properties) {
- if (tag === "OC") {
- this.markedContentStack.push({
- visible: this.optionalContentConfig.isVisible(properties)
- });
- } else {
- this.markedContentStack.push({
- visible: true
- });
- }
- this.contentVisible = this.isContentVisible();
- }
- endMarkedContent() {
- this.markedContentStack.pop();
- this.contentVisible = this.isContentVisible();
- }
- beginCompat() {}
- endCompat() {}
- consumePath() {
- const ctx = this.ctx;
- if (this.pendingClip) {
- if (this.pendingClip === EO_CLIP) {
- ctx.clip("evenodd");
- } else {
- ctx.clip();
- }
- this.pendingClip = null;
- }
- ctx.beginPath();
- }
- getSinglePixelWidth() {
- if (this._cachedGetSinglePixelWidth === null) {
- const m = this.ctx.mozCurrentTransform;
- const absDet = Math.abs(m[0] * m[3] - m[2] * m[1]);
- const sqNorm1 = m[0] ** 2 + m[2] ** 2;
- const sqNorm2 = m[1] ** 2 + m[3] ** 2;
- const pixelHeight = Math.sqrt(Math.max(sqNorm1, sqNorm2)) / absDet;
- if (sqNorm1 !== sqNorm2 && this._combinedScaleFactor * pixelHeight > 1) {
- this._cachedGetSinglePixelWidth = -(this._combinedScaleFactor * pixelHeight);
- } else if (absDet > Number.EPSILON) {
- this._cachedGetSinglePixelWidth = pixelHeight;
- } else {
- this._cachedGetSinglePixelWidth = 1;
- }
- }
- return this._cachedGetSinglePixelWidth;
- }
- getCanvasPosition(x, y) {
- const transform = this.ctx.mozCurrentTransform;
- return [transform[0] * x + transform[2] * y + transform[4], transform[1] * x + transform[3] * y + transform[5]];
- }
- isContentVisible() {
- for (let i = this.markedContentStack.length - 1; i >= 0; i--) {
- if (!this.markedContentStack[i].visible) {
- return false;
- }
- }
- return true;
- }
- }
- for (const op in _util.OPS) {
- CanvasGraphics.prototype[_util.OPS[op]] = CanvasGraphics.prototype[op];
- }
- return CanvasGraphics;
- }();
- exports.CanvasGraphics = CanvasGraphics;
|