2
0

svg.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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.SVGGraphics = void 0;
  27. var _display_utils = require("./display_utils.js");
  28. var _util = require("../shared/util.js");
  29. var _is_node = require("../shared/is_node.js");
  30. let SVGGraphics = class {
  31. constructor() {
  32. (0, _util.unreachable)("Not implemented: SVGGraphics");
  33. }
  34. };
  35. exports.SVGGraphics = SVGGraphics;
  36. {
  37. const SVG_DEFAULTS = {
  38. fontStyle: "normal",
  39. fontWeight: "normal",
  40. fillColor: "#000000"
  41. };
  42. const XML_NS = "http://www.w3.org/XML/1998/namespace";
  43. const XLINK_NS = "http://www.w3.org/1999/xlink";
  44. const LINE_CAP_STYLES = ["butt", "round", "square"];
  45. const LINE_JOIN_STYLES = ["miter", "round", "bevel"];
  46. const createObjectURL = function (data, contentType = "", forceDataSchema = false) {
  47. if (URL.createObjectURL && typeof Blob !== "undefined" && !forceDataSchema) {
  48. return URL.createObjectURL(new Blob([data], {
  49. type: contentType
  50. }));
  51. }
  52. const digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  53. let buffer = `data:${contentType};base64,`;
  54. for (let i = 0, ii = data.length; i < ii; i += 3) {
  55. const b1 = data[i] & 0xff;
  56. const b2 = data[i + 1] & 0xff;
  57. const b3 = data[i + 2] & 0xff;
  58. const d1 = b1 >> 2,
  59. d2 = (b1 & 3) << 4 | b2 >> 4;
  60. const d3 = i + 1 < ii ? (b2 & 0xf) << 2 | b3 >> 6 : 64;
  61. const d4 = i + 2 < ii ? b3 & 0x3f : 64;
  62. buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4];
  63. }
  64. return buffer;
  65. };
  66. const convertImgDataToPng = function () {
  67. const PNG_HEADER = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
  68. const CHUNK_WRAPPER_SIZE = 12;
  69. const crcTable = new Int32Array(256);
  70. for (let i = 0; i < 256; i++) {
  71. let c = i;
  72. for (let h = 0; h < 8; h++) {
  73. if (c & 1) {
  74. c = 0xedb88320 ^ c >> 1 & 0x7fffffff;
  75. } else {
  76. c = c >> 1 & 0x7fffffff;
  77. }
  78. }
  79. crcTable[i] = c;
  80. }
  81. function crc32(data, start, end) {
  82. let crc = -1;
  83. for (let i = start; i < end; i++) {
  84. const a = (crc ^ data[i]) & 0xff;
  85. const b = crcTable[a];
  86. crc = crc >>> 8 ^ b;
  87. }
  88. return crc ^ -1;
  89. }
  90. function writePngChunk(type, body, data, offset) {
  91. let p = offset;
  92. const len = body.length;
  93. data[p] = len >> 24 & 0xff;
  94. data[p + 1] = len >> 16 & 0xff;
  95. data[p + 2] = len >> 8 & 0xff;
  96. data[p + 3] = len & 0xff;
  97. p += 4;
  98. data[p] = type.charCodeAt(0) & 0xff;
  99. data[p + 1] = type.charCodeAt(1) & 0xff;
  100. data[p + 2] = type.charCodeAt(2) & 0xff;
  101. data[p + 3] = type.charCodeAt(3) & 0xff;
  102. p += 4;
  103. data.set(body, p);
  104. p += body.length;
  105. const crc = crc32(data, offset + 4, p);
  106. data[p] = crc >> 24 & 0xff;
  107. data[p + 1] = crc >> 16 & 0xff;
  108. data[p + 2] = crc >> 8 & 0xff;
  109. data[p + 3] = crc & 0xff;
  110. }
  111. function adler32(data, start, end) {
  112. let a = 1;
  113. let b = 0;
  114. for (let i = start; i < end; ++i) {
  115. a = (a + (data[i] & 0xff)) % 65521;
  116. b = (b + a) % 65521;
  117. }
  118. return b << 16 | a;
  119. }
  120. function deflateSync(literals) {
  121. if (!_is_node.isNodeJS) {
  122. return deflateSyncUncompressed(literals);
  123. }
  124. try {
  125. let input;
  126. if (parseInt(process.versions.node) >= 8) {
  127. input = literals;
  128. } else {
  129. input = Buffer.from(literals);
  130. }
  131. const output = require("zlib").deflateSync(input, {
  132. level: 9
  133. });
  134. return output instanceof Uint8Array ? output : new Uint8Array(output);
  135. } catch (e) {
  136. (0, _util.warn)("Not compressing PNG because zlib.deflateSync is unavailable: " + e);
  137. }
  138. return deflateSyncUncompressed(literals);
  139. }
  140. function deflateSyncUncompressed(literals) {
  141. let len = literals.length;
  142. const maxBlockLength = 0xffff;
  143. const deflateBlocks = Math.ceil(len / maxBlockLength);
  144. const idat = new Uint8Array(2 + len + deflateBlocks * 5 + 4);
  145. let pi = 0;
  146. idat[pi++] = 0x78;
  147. idat[pi++] = 0x9c;
  148. let pos = 0;
  149. while (len > maxBlockLength) {
  150. idat[pi++] = 0x00;
  151. idat[pi++] = 0xff;
  152. idat[pi++] = 0xff;
  153. idat[pi++] = 0x00;
  154. idat[pi++] = 0x00;
  155. idat.set(literals.subarray(pos, pos + maxBlockLength), pi);
  156. pi += maxBlockLength;
  157. pos += maxBlockLength;
  158. len -= maxBlockLength;
  159. }
  160. idat[pi++] = 0x01;
  161. idat[pi++] = len & 0xff;
  162. idat[pi++] = len >> 8 & 0xff;
  163. idat[pi++] = ~len & 0xffff & 0xff;
  164. idat[pi++] = (~len & 0xffff) >> 8 & 0xff;
  165. idat.set(literals.subarray(pos), pi);
  166. pi += literals.length - pos;
  167. const adler = adler32(literals, 0, literals.length);
  168. idat[pi++] = adler >> 24 & 0xff;
  169. idat[pi++] = adler >> 16 & 0xff;
  170. idat[pi++] = adler >> 8 & 0xff;
  171. idat[pi++] = adler & 0xff;
  172. return idat;
  173. }
  174. function encode(imgData, kind, forceDataSchema, isMask) {
  175. const width = imgData.width;
  176. const height = imgData.height;
  177. let bitDepth, colorType, lineSize;
  178. const bytes = imgData.data;
  179. switch (kind) {
  180. case _util.ImageKind.GRAYSCALE_1BPP:
  181. colorType = 0;
  182. bitDepth = 1;
  183. lineSize = width + 7 >> 3;
  184. break;
  185. case _util.ImageKind.RGB_24BPP:
  186. colorType = 2;
  187. bitDepth = 8;
  188. lineSize = width * 3;
  189. break;
  190. case _util.ImageKind.RGBA_32BPP:
  191. colorType = 6;
  192. bitDepth = 8;
  193. lineSize = width * 4;
  194. break;
  195. default:
  196. throw new Error("invalid format");
  197. }
  198. const literals = new Uint8Array((1 + lineSize) * height);
  199. let offsetLiterals = 0,
  200. offsetBytes = 0;
  201. for (let y = 0; y < height; ++y) {
  202. literals[offsetLiterals++] = 0;
  203. literals.set(bytes.subarray(offsetBytes, offsetBytes + lineSize), offsetLiterals);
  204. offsetBytes += lineSize;
  205. offsetLiterals += lineSize;
  206. }
  207. if (kind === _util.ImageKind.GRAYSCALE_1BPP && isMask) {
  208. offsetLiterals = 0;
  209. for (let y = 0; y < height; y++) {
  210. offsetLiterals++;
  211. for (let i = 0; i < lineSize; i++) {
  212. literals[offsetLiterals++] ^= 0xff;
  213. }
  214. }
  215. }
  216. const ihdr = new Uint8Array([width >> 24 & 0xff, width >> 16 & 0xff, width >> 8 & 0xff, width & 0xff, height >> 24 & 0xff, height >> 16 & 0xff, height >> 8 & 0xff, height & 0xff, bitDepth, colorType, 0x00, 0x00, 0x00]);
  217. const idat = deflateSync(literals);
  218. const pngLength = PNG_HEADER.length + CHUNK_WRAPPER_SIZE * 3 + ihdr.length + idat.length;
  219. const data = new Uint8Array(pngLength);
  220. let offset = 0;
  221. data.set(PNG_HEADER, offset);
  222. offset += PNG_HEADER.length;
  223. writePngChunk("IHDR", ihdr, data, offset);
  224. offset += CHUNK_WRAPPER_SIZE + ihdr.length;
  225. writePngChunk("IDATA", idat, data, offset);
  226. offset += CHUNK_WRAPPER_SIZE + idat.length;
  227. writePngChunk("IEND", new Uint8Array(0), data, offset);
  228. return createObjectURL(data, "image/png", forceDataSchema);
  229. }
  230. return function convertImgDataToPng(imgData, forceDataSchema, isMask) {
  231. const kind = imgData.kind === undefined ? _util.ImageKind.GRAYSCALE_1BPP : imgData.kind;
  232. return encode(imgData, kind, forceDataSchema, isMask);
  233. };
  234. }();
  235. class SVGExtraState {
  236. constructor() {
  237. this.fontSizeScale = 1;
  238. this.fontWeight = SVG_DEFAULTS.fontWeight;
  239. this.fontSize = 0;
  240. this.textMatrix = _util.IDENTITY_MATRIX;
  241. this.fontMatrix = _util.FONT_IDENTITY_MATRIX;
  242. this.leading = 0;
  243. this.textRenderingMode = _util.TextRenderingMode.FILL;
  244. this.textMatrixScale = 1;
  245. this.x = 0;
  246. this.y = 0;
  247. this.lineX = 0;
  248. this.lineY = 0;
  249. this.charSpacing = 0;
  250. this.wordSpacing = 0;
  251. this.textHScale = 1;
  252. this.textRise = 0;
  253. this.fillColor = SVG_DEFAULTS.fillColor;
  254. this.strokeColor = "#000000";
  255. this.fillAlpha = 1;
  256. this.strokeAlpha = 1;
  257. this.lineWidth = 1;
  258. this.lineJoin = "";
  259. this.lineCap = "";
  260. this.miterLimit = 0;
  261. this.dashArray = [];
  262. this.dashPhase = 0;
  263. this.dependencies = [];
  264. this.activeClipUrl = null;
  265. this.clipGroup = null;
  266. this.maskId = "";
  267. }
  268. clone() {
  269. return Object.create(this);
  270. }
  271. setCurrentPoint(x, y) {
  272. this.x = x;
  273. this.y = y;
  274. }
  275. }
  276. function opListToTree(opList) {
  277. let opTree = [];
  278. const tmp = [];
  279. for (const opListElement of opList) {
  280. if (opListElement.fn === "save") {
  281. opTree.push({
  282. fnId: 92,
  283. fn: "group",
  284. items: []
  285. });
  286. tmp.push(opTree);
  287. opTree = opTree.at(-1).items;
  288. continue;
  289. }
  290. if (opListElement.fn === "restore") {
  291. opTree = tmp.pop();
  292. } else {
  293. opTree.push(opListElement);
  294. }
  295. }
  296. return opTree;
  297. }
  298. function pf(value) {
  299. if (Number.isInteger(value)) {
  300. return value.toString();
  301. }
  302. const s = value.toFixed(10);
  303. let i = s.length - 1;
  304. if (s[i] !== "0") {
  305. return s;
  306. }
  307. do {
  308. i--;
  309. } while (s[i] === "0");
  310. return s.substring(0, s[i] === "." ? i : i + 1);
  311. }
  312. function pm(m) {
  313. if (m[4] === 0 && m[5] === 0) {
  314. if (m[1] === 0 && m[2] === 0) {
  315. if (m[0] === 1 && m[3] === 1) {
  316. return "";
  317. }
  318. return `scale(${pf(m[0])} ${pf(m[3])})`;
  319. }
  320. if (m[0] === m[3] && m[1] === -m[2]) {
  321. const a = Math.acos(m[0]) * 180 / Math.PI;
  322. return `rotate(${pf(a)})`;
  323. }
  324. } else {
  325. if (m[0] === 1 && m[1] === 0 && m[2] === 0 && m[3] === 1) {
  326. return `translate(${pf(m[4])} ${pf(m[5])})`;
  327. }
  328. }
  329. return `matrix(${pf(m[0])} ${pf(m[1])} ${pf(m[2])} ${pf(m[3])} ${pf(m[4])} ` + `${pf(m[5])})`;
  330. }
  331. let clipCount = 0;
  332. let maskCount = 0;
  333. let shadingCount = 0;
  334. exports.SVGGraphics = SVGGraphics = class {
  335. constructor(commonObjs, objs, forceDataSchema = false) {
  336. (0, _display_utils.deprecated)("The SVG back-end is no longer maintained and *may* be removed in the future.");
  337. this.svgFactory = new _display_utils.DOMSVGFactory();
  338. this.current = new SVGExtraState();
  339. this.transformMatrix = _util.IDENTITY_MATRIX;
  340. this.transformStack = [];
  341. this.extraStack = [];
  342. this.commonObjs = commonObjs;
  343. this.objs = objs;
  344. this.pendingClip = null;
  345. this.pendingEOFill = false;
  346. this.embedFonts = false;
  347. this.embeddedFonts = Object.create(null);
  348. this.cssStyle = null;
  349. this.forceDataSchema = !!forceDataSchema;
  350. this._operatorIdMapping = [];
  351. for (const op in _util.OPS) {
  352. this._operatorIdMapping[_util.OPS[op]] = op;
  353. }
  354. }
  355. getObject(data, fallback = null) {
  356. if (typeof data === "string") {
  357. return data.startsWith("g_") ? this.commonObjs.get(data) : this.objs.get(data);
  358. }
  359. return fallback;
  360. }
  361. save() {
  362. this.transformStack.push(this.transformMatrix);
  363. const old = this.current;
  364. this.extraStack.push(old);
  365. this.current = old.clone();
  366. }
  367. restore() {
  368. this.transformMatrix = this.transformStack.pop();
  369. this.current = this.extraStack.pop();
  370. this.pendingClip = null;
  371. this.tgrp = null;
  372. }
  373. group(items) {
  374. this.save();
  375. this.executeOpTree(items);
  376. this.restore();
  377. }
  378. loadDependencies(operatorList) {
  379. const fnArray = operatorList.fnArray;
  380. const argsArray = operatorList.argsArray;
  381. for (let i = 0, ii = fnArray.length; i < ii; i++) {
  382. if (fnArray[i] !== _util.OPS.dependency) {
  383. continue;
  384. }
  385. for (const obj of argsArray[i]) {
  386. const objsPool = obj.startsWith("g_") ? this.commonObjs : this.objs;
  387. const promise = new Promise(resolve => {
  388. objsPool.get(obj, resolve);
  389. });
  390. this.current.dependencies.push(promise);
  391. }
  392. }
  393. return Promise.all(this.current.dependencies);
  394. }
  395. transform(a, b, c, d, e, f) {
  396. const transformMatrix = [a, b, c, d, e, f];
  397. this.transformMatrix = _util.Util.transform(this.transformMatrix, transformMatrix);
  398. this.tgrp = null;
  399. }
  400. getSVG(operatorList, viewport) {
  401. this.viewport = viewport;
  402. const svgElement = this._initialize(viewport);
  403. return this.loadDependencies(operatorList).then(() => {
  404. this.transformMatrix = _util.IDENTITY_MATRIX;
  405. this.executeOpTree(this.convertOpList(operatorList));
  406. return svgElement;
  407. });
  408. }
  409. convertOpList(operatorList) {
  410. const operatorIdMapping = this._operatorIdMapping;
  411. const argsArray = operatorList.argsArray;
  412. const fnArray = operatorList.fnArray;
  413. const opList = [];
  414. for (let i = 0, ii = fnArray.length; i < ii; i++) {
  415. const fnId = fnArray[i];
  416. opList.push({
  417. fnId,
  418. fn: operatorIdMapping[fnId],
  419. args: argsArray[i]
  420. });
  421. }
  422. return opListToTree(opList);
  423. }
  424. executeOpTree(opTree) {
  425. for (const opTreeElement of opTree) {
  426. const fn = opTreeElement.fn;
  427. const fnId = opTreeElement.fnId;
  428. const args = opTreeElement.args;
  429. switch (fnId | 0) {
  430. case _util.OPS.beginText:
  431. this.beginText();
  432. break;
  433. case _util.OPS.dependency:
  434. break;
  435. case _util.OPS.setLeading:
  436. this.setLeading(args);
  437. break;
  438. case _util.OPS.setLeadingMoveText:
  439. this.setLeadingMoveText(args[0], args[1]);
  440. break;
  441. case _util.OPS.setFont:
  442. this.setFont(args);
  443. break;
  444. case _util.OPS.showText:
  445. this.showText(args[0]);
  446. break;
  447. case _util.OPS.showSpacedText:
  448. this.showText(args[0]);
  449. break;
  450. case _util.OPS.endText:
  451. this.endText();
  452. break;
  453. case _util.OPS.moveText:
  454. this.moveText(args[0], args[1]);
  455. break;
  456. case _util.OPS.setCharSpacing:
  457. this.setCharSpacing(args[0]);
  458. break;
  459. case _util.OPS.setWordSpacing:
  460. this.setWordSpacing(args[0]);
  461. break;
  462. case _util.OPS.setHScale:
  463. this.setHScale(args[0]);
  464. break;
  465. case _util.OPS.setTextMatrix:
  466. this.setTextMatrix(args[0], args[1], args[2], args[3], args[4], args[5]);
  467. break;
  468. case _util.OPS.setTextRise:
  469. this.setTextRise(args[0]);
  470. break;
  471. case _util.OPS.setTextRenderingMode:
  472. this.setTextRenderingMode(args[0]);
  473. break;
  474. case _util.OPS.setLineWidth:
  475. this.setLineWidth(args[0]);
  476. break;
  477. case _util.OPS.setLineJoin:
  478. this.setLineJoin(args[0]);
  479. break;
  480. case _util.OPS.setLineCap:
  481. this.setLineCap(args[0]);
  482. break;
  483. case _util.OPS.setMiterLimit:
  484. this.setMiterLimit(args[0]);
  485. break;
  486. case _util.OPS.setFillRGBColor:
  487. this.setFillRGBColor(args[0], args[1], args[2]);
  488. break;
  489. case _util.OPS.setStrokeRGBColor:
  490. this.setStrokeRGBColor(args[0], args[1], args[2]);
  491. break;
  492. case _util.OPS.setStrokeColorN:
  493. this.setStrokeColorN(args);
  494. break;
  495. case _util.OPS.setFillColorN:
  496. this.setFillColorN(args);
  497. break;
  498. case _util.OPS.shadingFill:
  499. this.shadingFill(args[0]);
  500. break;
  501. case _util.OPS.setDash:
  502. this.setDash(args[0], args[1]);
  503. break;
  504. case _util.OPS.setRenderingIntent:
  505. this.setRenderingIntent(args[0]);
  506. break;
  507. case _util.OPS.setFlatness:
  508. this.setFlatness(args[0]);
  509. break;
  510. case _util.OPS.setGState:
  511. this.setGState(args[0]);
  512. break;
  513. case _util.OPS.fill:
  514. this.fill();
  515. break;
  516. case _util.OPS.eoFill:
  517. this.eoFill();
  518. break;
  519. case _util.OPS.stroke:
  520. this.stroke();
  521. break;
  522. case _util.OPS.fillStroke:
  523. this.fillStroke();
  524. break;
  525. case _util.OPS.eoFillStroke:
  526. this.eoFillStroke();
  527. break;
  528. case _util.OPS.clip:
  529. this.clip("nonzero");
  530. break;
  531. case _util.OPS.eoClip:
  532. this.clip("evenodd");
  533. break;
  534. case _util.OPS.paintSolidColorImageMask:
  535. this.paintSolidColorImageMask();
  536. break;
  537. case _util.OPS.paintImageXObject:
  538. this.paintImageXObject(args[0]);
  539. break;
  540. case _util.OPS.paintInlineImageXObject:
  541. this.paintInlineImageXObject(args[0]);
  542. break;
  543. case _util.OPS.paintImageMaskXObject:
  544. this.paintImageMaskXObject(args[0]);
  545. break;
  546. case _util.OPS.paintFormXObjectBegin:
  547. this.paintFormXObjectBegin(args[0], args[1]);
  548. break;
  549. case _util.OPS.paintFormXObjectEnd:
  550. this.paintFormXObjectEnd();
  551. break;
  552. case _util.OPS.closePath:
  553. this.closePath();
  554. break;
  555. case _util.OPS.closeStroke:
  556. this.closeStroke();
  557. break;
  558. case _util.OPS.closeFillStroke:
  559. this.closeFillStroke();
  560. break;
  561. case _util.OPS.closeEOFillStroke:
  562. this.closeEOFillStroke();
  563. break;
  564. case _util.OPS.nextLine:
  565. this.nextLine();
  566. break;
  567. case _util.OPS.transform:
  568. this.transform(args[0], args[1], args[2], args[3], args[4], args[5]);
  569. break;
  570. case _util.OPS.constructPath:
  571. this.constructPath(args[0], args[1]);
  572. break;
  573. case _util.OPS.endPath:
  574. this.endPath();
  575. break;
  576. case 92:
  577. this.group(opTreeElement.items);
  578. break;
  579. default:
  580. (0, _util.warn)(`Unimplemented operator ${fn}`);
  581. break;
  582. }
  583. }
  584. }
  585. setWordSpacing(wordSpacing) {
  586. this.current.wordSpacing = wordSpacing;
  587. }
  588. setCharSpacing(charSpacing) {
  589. this.current.charSpacing = charSpacing;
  590. }
  591. nextLine() {
  592. this.moveText(0, this.current.leading);
  593. }
  594. setTextMatrix(a, b, c, d, e, f) {
  595. const current = this.current;
  596. current.textMatrix = current.lineMatrix = [a, b, c, d, e, f];
  597. current.textMatrixScale = Math.hypot(a, b);
  598. current.x = current.lineX = 0;
  599. current.y = current.lineY = 0;
  600. current.xcoords = [];
  601. current.ycoords = [];
  602. current.tspan = this.svgFactory.createElement("svg:tspan");
  603. current.tspan.setAttributeNS(null, "font-family", current.fontFamily);
  604. current.tspan.setAttributeNS(null, "font-size", `${pf(current.fontSize)}px`);
  605. current.tspan.setAttributeNS(null, "y", pf(-current.y));
  606. current.txtElement = this.svgFactory.createElement("svg:text");
  607. current.txtElement.append(current.tspan);
  608. }
  609. beginText() {
  610. const current = this.current;
  611. current.x = current.lineX = 0;
  612. current.y = current.lineY = 0;
  613. current.textMatrix = _util.IDENTITY_MATRIX;
  614. current.lineMatrix = _util.IDENTITY_MATRIX;
  615. current.textMatrixScale = 1;
  616. current.tspan = this.svgFactory.createElement("svg:tspan");
  617. current.txtElement = this.svgFactory.createElement("svg:text");
  618. current.txtgrp = this.svgFactory.createElement("svg:g");
  619. current.xcoords = [];
  620. current.ycoords = [];
  621. }
  622. moveText(x, y) {
  623. const current = this.current;
  624. current.x = current.lineX += x;
  625. current.y = current.lineY += y;
  626. current.xcoords = [];
  627. current.ycoords = [];
  628. current.tspan = this.svgFactory.createElement("svg:tspan");
  629. current.tspan.setAttributeNS(null, "font-family", current.fontFamily);
  630. current.tspan.setAttributeNS(null, "font-size", `${pf(current.fontSize)}px`);
  631. current.tspan.setAttributeNS(null, "y", pf(-current.y));
  632. }
  633. showText(glyphs) {
  634. const current = this.current;
  635. const font = current.font;
  636. const fontSize = current.fontSize;
  637. if (fontSize === 0) {
  638. return;
  639. }
  640. const fontSizeScale = current.fontSizeScale;
  641. const charSpacing = current.charSpacing;
  642. const wordSpacing = current.wordSpacing;
  643. const fontDirection = current.fontDirection;
  644. const textHScale = current.textHScale * fontDirection;
  645. const vertical = font.vertical;
  646. const spacingDir = vertical ? 1 : -1;
  647. const defaultVMetrics = font.defaultVMetrics;
  648. const widthAdvanceScale = fontSize * current.fontMatrix[0];
  649. let x = 0;
  650. for (const glyph of glyphs) {
  651. if (glyph === null) {
  652. x += fontDirection * wordSpacing;
  653. continue;
  654. } else if (typeof glyph === "number") {
  655. x += spacingDir * glyph * fontSize / 1000;
  656. continue;
  657. }
  658. const spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
  659. const character = glyph.fontChar;
  660. let scaledX, scaledY;
  661. let width = glyph.width;
  662. if (vertical) {
  663. let vx;
  664. const vmetric = glyph.vmetric || defaultVMetrics;
  665. vx = glyph.vmetric ? vmetric[1] : width * 0.5;
  666. vx = -vx * widthAdvanceScale;
  667. const vy = vmetric[2] * widthAdvanceScale;
  668. width = vmetric ? -vmetric[0] : width;
  669. scaledX = vx / fontSizeScale;
  670. scaledY = (x + vy) / fontSizeScale;
  671. } else {
  672. scaledX = x / fontSizeScale;
  673. scaledY = 0;
  674. }
  675. if (glyph.isInFont || font.missingFile) {
  676. current.xcoords.push(current.x + scaledX);
  677. if (vertical) {
  678. current.ycoords.push(-current.y + scaledY);
  679. }
  680. current.tspan.textContent += character;
  681. } else {}
  682. let charWidth;
  683. if (vertical) {
  684. charWidth = width * widthAdvanceScale - spacing * fontDirection;
  685. } else {
  686. charWidth = width * widthAdvanceScale + spacing * fontDirection;
  687. }
  688. x += charWidth;
  689. }
  690. current.tspan.setAttributeNS(null, "x", current.xcoords.map(pf).join(" "));
  691. if (vertical) {
  692. current.tspan.setAttributeNS(null, "y", current.ycoords.map(pf).join(" "));
  693. } else {
  694. current.tspan.setAttributeNS(null, "y", pf(-current.y));
  695. }
  696. if (vertical) {
  697. current.y -= x;
  698. } else {
  699. current.x += x * textHScale;
  700. }
  701. current.tspan.setAttributeNS(null, "font-family", current.fontFamily);
  702. current.tspan.setAttributeNS(null, "font-size", `${pf(current.fontSize)}px`);
  703. if (current.fontStyle !== SVG_DEFAULTS.fontStyle) {
  704. current.tspan.setAttributeNS(null, "font-style", current.fontStyle);
  705. }
  706. if (current.fontWeight !== SVG_DEFAULTS.fontWeight) {
  707. current.tspan.setAttributeNS(null, "font-weight", current.fontWeight);
  708. }
  709. const fillStrokeMode = current.textRenderingMode & _util.TextRenderingMode.FILL_STROKE_MASK;
  710. if (fillStrokeMode === _util.TextRenderingMode.FILL || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
  711. if (current.fillColor !== SVG_DEFAULTS.fillColor) {
  712. current.tspan.setAttributeNS(null, "fill", current.fillColor);
  713. }
  714. if (current.fillAlpha < 1) {
  715. current.tspan.setAttributeNS(null, "fill-opacity", current.fillAlpha);
  716. }
  717. } else if (current.textRenderingMode === _util.TextRenderingMode.ADD_TO_PATH) {
  718. current.tspan.setAttributeNS(null, "fill", "transparent");
  719. } else {
  720. current.tspan.setAttributeNS(null, "fill", "none");
  721. }
  722. if (fillStrokeMode === _util.TextRenderingMode.STROKE || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
  723. const lineWidthScale = 1 / (current.textMatrixScale || 1);
  724. this._setStrokeAttributes(current.tspan, lineWidthScale);
  725. }
  726. let textMatrix = current.textMatrix;
  727. if (current.textRise !== 0) {
  728. textMatrix = textMatrix.slice();
  729. textMatrix[5] += current.textRise;
  730. }
  731. current.txtElement.setAttributeNS(null, "transform", `${pm(textMatrix)} scale(${pf(textHScale)}, -1)`);
  732. current.txtElement.setAttributeNS(XML_NS, "xml:space", "preserve");
  733. current.txtElement.append(current.tspan);
  734. current.txtgrp.append(current.txtElement);
  735. this._ensureTransformGroup().append(current.txtElement);
  736. }
  737. setLeadingMoveText(x, y) {
  738. this.setLeading(-y);
  739. this.moveText(x, y);
  740. }
  741. addFontStyle(fontObj) {
  742. if (!fontObj.data) {
  743. throw new Error("addFontStyle: No font data available, " + 'ensure that the "fontExtraProperties" API parameter is set.');
  744. }
  745. if (!this.cssStyle) {
  746. this.cssStyle = this.svgFactory.createElement("svg:style");
  747. this.cssStyle.setAttributeNS(null, "type", "text/css");
  748. this.defs.append(this.cssStyle);
  749. }
  750. const url = createObjectURL(fontObj.data, fontObj.mimetype, this.forceDataSchema);
  751. this.cssStyle.textContent += `@font-face { font-family: "${fontObj.loadedName}";` + ` src: url(${url}); }\n`;
  752. }
  753. setFont(details) {
  754. const current = this.current;
  755. const fontObj = this.commonObjs.get(details[0]);
  756. let size = details[1];
  757. current.font = fontObj;
  758. if (this.embedFonts && !fontObj.missingFile && !this.embeddedFonts[fontObj.loadedName]) {
  759. this.addFontStyle(fontObj);
  760. this.embeddedFonts[fontObj.loadedName] = fontObj;
  761. }
  762. current.fontMatrix = fontObj.fontMatrix || _util.FONT_IDENTITY_MATRIX;
  763. let bold = "normal";
  764. if (fontObj.black) {
  765. bold = "900";
  766. } else if (fontObj.bold) {
  767. bold = "bold";
  768. }
  769. const italic = fontObj.italic ? "italic" : "normal";
  770. if (size < 0) {
  771. size = -size;
  772. current.fontDirection = -1;
  773. } else {
  774. current.fontDirection = 1;
  775. }
  776. current.fontSize = size;
  777. current.fontFamily = fontObj.loadedName;
  778. current.fontWeight = bold;
  779. current.fontStyle = italic;
  780. current.tspan = this.svgFactory.createElement("svg:tspan");
  781. current.tspan.setAttributeNS(null, "y", pf(-current.y));
  782. current.xcoords = [];
  783. current.ycoords = [];
  784. }
  785. endText() {
  786. const current = this.current;
  787. if (current.textRenderingMode & _util.TextRenderingMode.ADD_TO_PATH_FLAG && current.txtElement?.hasChildNodes()) {
  788. current.element = current.txtElement;
  789. this.clip("nonzero");
  790. this.endPath();
  791. }
  792. }
  793. setLineWidth(width) {
  794. if (width > 0) {
  795. this.current.lineWidth = width;
  796. }
  797. }
  798. setLineCap(style) {
  799. this.current.lineCap = LINE_CAP_STYLES[style];
  800. }
  801. setLineJoin(style) {
  802. this.current.lineJoin = LINE_JOIN_STYLES[style];
  803. }
  804. setMiterLimit(limit) {
  805. this.current.miterLimit = limit;
  806. }
  807. setStrokeAlpha(strokeAlpha) {
  808. this.current.strokeAlpha = strokeAlpha;
  809. }
  810. setStrokeRGBColor(r, g, b) {
  811. this.current.strokeColor = _util.Util.makeHexColor(r, g, b);
  812. }
  813. setFillAlpha(fillAlpha) {
  814. this.current.fillAlpha = fillAlpha;
  815. }
  816. setFillRGBColor(r, g, b) {
  817. this.current.fillColor = _util.Util.makeHexColor(r, g, b);
  818. this.current.tspan = this.svgFactory.createElement("svg:tspan");
  819. this.current.xcoords = [];
  820. this.current.ycoords = [];
  821. }
  822. setStrokeColorN(args) {
  823. this.current.strokeColor = this._makeColorN_Pattern(args);
  824. }
  825. setFillColorN(args) {
  826. this.current.fillColor = this._makeColorN_Pattern(args);
  827. }
  828. shadingFill(args) {
  829. const width = this.viewport.width;
  830. const height = this.viewport.height;
  831. const inv = _util.Util.inverseTransform(this.transformMatrix);
  832. const bl = _util.Util.applyTransform([0, 0], inv);
  833. const br = _util.Util.applyTransform([0, height], inv);
  834. const ul = _util.Util.applyTransform([width, 0], inv);
  835. const ur = _util.Util.applyTransform([width, height], inv);
  836. const x0 = Math.min(bl[0], br[0], ul[0], ur[0]);
  837. const y0 = Math.min(bl[1], br[1], ul[1], ur[1]);
  838. const x1 = Math.max(bl[0], br[0], ul[0], ur[0]);
  839. const y1 = Math.max(bl[1], br[1], ul[1], ur[1]);
  840. const rect = this.svgFactory.createElement("svg:rect");
  841. rect.setAttributeNS(null, "x", x0);
  842. rect.setAttributeNS(null, "y", y0);
  843. rect.setAttributeNS(null, "width", x1 - x0);
  844. rect.setAttributeNS(null, "height", y1 - y0);
  845. rect.setAttributeNS(null, "fill", this._makeShadingPattern(args));
  846. if (this.current.fillAlpha < 1) {
  847. rect.setAttributeNS(null, "fill-opacity", this.current.fillAlpha);
  848. }
  849. this._ensureTransformGroup().append(rect);
  850. }
  851. _makeColorN_Pattern(args) {
  852. if (args[0] === "TilingPattern") {
  853. return this._makeTilingPattern(args);
  854. }
  855. return this._makeShadingPattern(args);
  856. }
  857. _makeTilingPattern(args) {
  858. const color = args[1];
  859. const operatorList = args[2];
  860. const matrix = args[3] || _util.IDENTITY_MATRIX;
  861. const [x0, y0, x1, y1] = args[4];
  862. const xstep = args[5];
  863. const ystep = args[6];
  864. const paintType = args[7];
  865. const tilingId = `shading${shadingCount++}`;
  866. const [tx0, ty0, tx1, ty1] = _util.Util.normalizeRect([..._util.Util.applyTransform([x0, y0], matrix), ..._util.Util.applyTransform([x1, y1], matrix)]);
  867. const [xscale, yscale] = _util.Util.singularValueDecompose2dScale(matrix);
  868. const txstep = xstep * xscale;
  869. const tystep = ystep * yscale;
  870. const tiling = this.svgFactory.createElement("svg:pattern");
  871. tiling.setAttributeNS(null, "id", tilingId);
  872. tiling.setAttributeNS(null, "patternUnits", "userSpaceOnUse");
  873. tiling.setAttributeNS(null, "width", txstep);
  874. tiling.setAttributeNS(null, "height", tystep);
  875. tiling.setAttributeNS(null, "x", `${tx0}`);
  876. tiling.setAttributeNS(null, "y", `${ty0}`);
  877. const svg = this.svg;
  878. const transformMatrix = this.transformMatrix;
  879. const fillColor = this.current.fillColor;
  880. const strokeColor = this.current.strokeColor;
  881. const bbox = this.svgFactory.create(tx1 - tx0, ty1 - ty0);
  882. this.svg = bbox;
  883. this.transformMatrix = matrix;
  884. if (paintType === 2) {
  885. const cssColor = _util.Util.makeHexColor(...color);
  886. this.current.fillColor = cssColor;
  887. this.current.strokeColor = cssColor;
  888. }
  889. this.executeOpTree(this.convertOpList(operatorList));
  890. this.svg = svg;
  891. this.transformMatrix = transformMatrix;
  892. this.current.fillColor = fillColor;
  893. this.current.strokeColor = strokeColor;
  894. tiling.append(bbox.childNodes[0]);
  895. this.defs.append(tiling);
  896. return `url(#${tilingId})`;
  897. }
  898. _makeShadingPattern(args) {
  899. if (typeof args === "string") {
  900. args = this.objs.get(args);
  901. }
  902. switch (args[0]) {
  903. case "RadialAxial":
  904. const shadingId = `shading${shadingCount++}`;
  905. const colorStops = args[3];
  906. let gradient;
  907. switch (args[1]) {
  908. case "axial":
  909. const point0 = args[4];
  910. const point1 = args[5];
  911. gradient = this.svgFactory.createElement("svg:linearGradient");
  912. gradient.setAttributeNS(null, "id", shadingId);
  913. gradient.setAttributeNS(null, "gradientUnits", "userSpaceOnUse");
  914. gradient.setAttributeNS(null, "x1", point0[0]);
  915. gradient.setAttributeNS(null, "y1", point0[1]);
  916. gradient.setAttributeNS(null, "x2", point1[0]);
  917. gradient.setAttributeNS(null, "y2", point1[1]);
  918. break;
  919. case "radial":
  920. const focalPoint = args[4];
  921. const circlePoint = args[5];
  922. const focalRadius = args[6];
  923. const circleRadius = args[7];
  924. gradient = this.svgFactory.createElement("svg:radialGradient");
  925. gradient.setAttributeNS(null, "id", shadingId);
  926. gradient.setAttributeNS(null, "gradientUnits", "userSpaceOnUse");
  927. gradient.setAttributeNS(null, "cx", circlePoint[0]);
  928. gradient.setAttributeNS(null, "cy", circlePoint[1]);
  929. gradient.setAttributeNS(null, "r", circleRadius);
  930. gradient.setAttributeNS(null, "fx", focalPoint[0]);
  931. gradient.setAttributeNS(null, "fy", focalPoint[1]);
  932. gradient.setAttributeNS(null, "fr", focalRadius);
  933. break;
  934. default:
  935. throw new Error(`Unknown RadialAxial type: ${args[1]}`);
  936. }
  937. for (const colorStop of colorStops) {
  938. const stop = this.svgFactory.createElement("svg:stop");
  939. stop.setAttributeNS(null, "offset", colorStop[0]);
  940. stop.setAttributeNS(null, "stop-color", colorStop[1]);
  941. gradient.append(stop);
  942. }
  943. this.defs.append(gradient);
  944. return `url(#${shadingId})`;
  945. case "Mesh":
  946. (0, _util.warn)("Unimplemented pattern Mesh");
  947. return null;
  948. case "Dummy":
  949. return "hotpink";
  950. default:
  951. throw new Error(`Unknown IR type: ${args[0]}`);
  952. }
  953. }
  954. setDash(dashArray, dashPhase) {
  955. this.current.dashArray = dashArray;
  956. this.current.dashPhase = dashPhase;
  957. }
  958. constructPath(ops, args) {
  959. const current = this.current;
  960. let x = current.x,
  961. y = current.y;
  962. let d = [];
  963. let j = 0;
  964. for (const op of ops) {
  965. switch (op | 0) {
  966. case _util.OPS.rectangle:
  967. x = args[j++];
  968. y = args[j++];
  969. const width = args[j++];
  970. const height = args[j++];
  971. const xw = x + width;
  972. const yh = y + height;
  973. d.push("M", pf(x), pf(y), "L", pf(xw), pf(y), "L", pf(xw), pf(yh), "L", pf(x), pf(yh), "Z");
  974. break;
  975. case _util.OPS.moveTo:
  976. x = args[j++];
  977. y = args[j++];
  978. d.push("M", pf(x), pf(y));
  979. break;
  980. case _util.OPS.lineTo:
  981. x = args[j++];
  982. y = args[j++];
  983. d.push("L", pf(x), pf(y));
  984. break;
  985. case _util.OPS.curveTo:
  986. x = args[j + 4];
  987. y = args[j + 5];
  988. d.push("C", pf(args[j]), pf(args[j + 1]), pf(args[j + 2]), pf(args[j + 3]), pf(x), pf(y));
  989. j += 6;
  990. break;
  991. case _util.OPS.curveTo2:
  992. d.push("C", pf(x), pf(y), pf(args[j]), pf(args[j + 1]), pf(args[j + 2]), pf(args[j + 3]));
  993. x = args[j + 2];
  994. y = args[j + 3];
  995. j += 4;
  996. break;
  997. case _util.OPS.curveTo3:
  998. x = args[j + 2];
  999. y = args[j + 3];
  1000. d.push("C", pf(args[j]), pf(args[j + 1]), pf(x), pf(y), pf(x), pf(y));
  1001. j += 4;
  1002. break;
  1003. case _util.OPS.closePath:
  1004. d.push("Z");
  1005. break;
  1006. }
  1007. }
  1008. d = d.join(" ");
  1009. if (current.path && ops.length > 0 && ops[0] !== _util.OPS.rectangle && ops[0] !== _util.OPS.moveTo) {
  1010. d = current.path.getAttributeNS(null, "d") + d;
  1011. } else {
  1012. current.path = this.svgFactory.createElement("svg:path");
  1013. this._ensureTransformGroup().append(current.path);
  1014. }
  1015. current.path.setAttributeNS(null, "d", d);
  1016. current.path.setAttributeNS(null, "fill", "none");
  1017. current.element = current.path;
  1018. current.setCurrentPoint(x, y);
  1019. }
  1020. endPath() {
  1021. const current = this.current;
  1022. current.path = null;
  1023. if (!this.pendingClip) {
  1024. return;
  1025. }
  1026. if (!current.element) {
  1027. this.pendingClip = null;
  1028. return;
  1029. }
  1030. const clipId = `clippath${clipCount++}`;
  1031. const clipPath = this.svgFactory.createElement("svg:clipPath");
  1032. clipPath.setAttributeNS(null, "id", clipId);
  1033. clipPath.setAttributeNS(null, "transform", pm(this.transformMatrix));
  1034. const clipElement = current.element.cloneNode(true);
  1035. if (this.pendingClip === "evenodd") {
  1036. clipElement.setAttributeNS(null, "clip-rule", "evenodd");
  1037. } else {
  1038. clipElement.setAttributeNS(null, "clip-rule", "nonzero");
  1039. }
  1040. this.pendingClip = null;
  1041. clipPath.append(clipElement);
  1042. this.defs.append(clipPath);
  1043. if (current.activeClipUrl) {
  1044. current.clipGroup = null;
  1045. for (const prev of this.extraStack) {
  1046. prev.clipGroup = null;
  1047. }
  1048. clipPath.setAttributeNS(null, "clip-path", current.activeClipUrl);
  1049. }
  1050. current.activeClipUrl = `url(#${clipId})`;
  1051. this.tgrp = null;
  1052. }
  1053. clip(type) {
  1054. this.pendingClip = type;
  1055. }
  1056. closePath() {
  1057. const current = this.current;
  1058. if (current.path) {
  1059. const d = `${current.path.getAttributeNS(null, "d")}Z`;
  1060. current.path.setAttributeNS(null, "d", d);
  1061. }
  1062. }
  1063. setLeading(leading) {
  1064. this.current.leading = -leading;
  1065. }
  1066. setTextRise(textRise) {
  1067. this.current.textRise = textRise;
  1068. }
  1069. setTextRenderingMode(textRenderingMode) {
  1070. this.current.textRenderingMode = textRenderingMode;
  1071. }
  1072. setHScale(scale) {
  1073. this.current.textHScale = scale / 100;
  1074. }
  1075. setRenderingIntent(intent) {}
  1076. setFlatness(flatness) {}
  1077. setGState(states) {
  1078. for (const [key, value] of states) {
  1079. switch (key) {
  1080. case "LW":
  1081. this.setLineWidth(value);
  1082. break;
  1083. case "LC":
  1084. this.setLineCap(value);
  1085. break;
  1086. case "LJ":
  1087. this.setLineJoin(value);
  1088. break;
  1089. case "ML":
  1090. this.setMiterLimit(value);
  1091. break;
  1092. case "D":
  1093. this.setDash(value[0], value[1]);
  1094. break;
  1095. case "RI":
  1096. this.setRenderingIntent(value);
  1097. break;
  1098. case "FL":
  1099. this.setFlatness(value);
  1100. break;
  1101. case "Font":
  1102. this.setFont(value);
  1103. break;
  1104. case "CA":
  1105. this.setStrokeAlpha(value);
  1106. break;
  1107. case "ca":
  1108. this.setFillAlpha(value);
  1109. break;
  1110. default:
  1111. (0, _util.warn)(`Unimplemented graphic state operator ${key}`);
  1112. break;
  1113. }
  1114. }
  1115. }
  1116. fill() {
  1117. const current = this.current;
  1118. if (current.element) {
  1119. current.element.setAttributeNS(null, "fill", current.fillColor);
  1120. current.element.setAttributeNS(null, "fill-opacity", current.fillAlpha);
  1121. this.endPath();
  1122. }
  1123. }
  1124. stroke() {
  1125. const current = this.current;
  1126. if (current.element) {
  1127. this._setStrokeAttributes(current.element);
  1128. current.element.setAttributeNS(null, "fill", "none");
  1129. this.endPath();
  1130. }
  1131. }
  1132. _setStrokeAttributes(element, lineWidthScale = 1) {
  1133. const current = this.current;
  1134. let dashArray = current.dashArray;
  1135. if (lineWidthScale !== 1 && dashArray.length > 0) {
  1136. dashArray = dashArray.map(function (value) {
  1137. return lineWidthScale * value;
  1138. });
  1139. }
  1140. element.setAttributeNS(null, "stroke", current.strokeColor);
  1141. element.setAttributeNS(null, "stroke-opacity", current.strokeAlpha);
  1142. element.setAttributeNS(null, "stroke-miterlimit", pf(current.miterLimit));
  1143. element.setAttributeNS(null, "stroke-linecap", current.lineCap);
  1144. element.setAttributeNS(null, "stroke-linejoin", current.lineJoin);
  1145. element.setAttributeNS(null, "stroke-width", pf(lineWidthScale * current.lineWidth) + "px");
  1146. element.setAttributeNS(null, "stroke-dasharray", dashArray.map(pf).join(" "));
  1147. element.setAttributeNS(null, "stroke-dashoffset", pf(lineWidthScale * current.dashPhase) + "px");
  1148. }
  1149. eoFill() {
  1150. this.current.element?.setAttributeNS(null, "fill-rule", "evenodd");
  1151. this.fill();
  1152. }
  1153. fillStroke() {
  1154. this.stroke();
  1155. this.fill();
  1156. }
  1157. eoFillStroke() {
  1158. this.current.element?.setAttributeNS(null, "fill-rule", "evenodd");
  1159. this.fillStroke();
  1160. }
  1161. closeStroke() {
  1162. this.closePath();
  1163. this.stroke();
  1164. }
  1165. closeFillStroke() {
  1166. this.closePath();
  1167. this.fillStroke();
  1168. }
  1169. closeEOFillStroke() {
  1170. this.closePath();
  1171. this.eoFillStroke();
  1172. }
  1173. paintSolidColorImageMask() {
  1174. const rect = this.svgFactory.createElement("svg:rect");
  1175. rect.setAttributeNS(null, "x", "0");
  1176. rect.setAttributeNS(null, "y", "0");
  1177. rect.setAttributeNS(null, "width", "1px");
  1178. rect.setAttributeNS(null, "height", "1px");
  1179. rect.setAttributeNS(null, "fill", this.current.fillColor);
  1180. this._ensureTransformGroup().append(rect);
  1181. }
  1182. paintImageXObject(objId) {
  1183. const imgData = this.getObject(objId);
  1184. if (!imgData) {
  1185. (0, _util.warn)(`Dependent image with object ID ${objId} is not ready yet`);
  1186. return;
  1187. }
  1188. this.paintInlineImageXObject(imgData);
  1189. }
  1190. paintInlineImageXObject(imgData, mask) {
  1191. const width = imgData.width;
  1192. const height = imgData.height;
  1193. const imgSrc = convertImgDataToPng(imgData, this.forceDataSchema, !!mask);
  1194. const cliprect = this.svgFactory.createElement("svg:rect");
  1195. cliprect.setAttributeNS(null, "x", "0");
  1196. cliprect.setAttributeNS(null, "y", "0");
  1197. cliprect.setAttributeNS(null, "width", pf(width));
  1198. cliprect.setAttributeNS(null, "height", pf(height));
  1199. this.current.element = cliprect;
  1200. this.clip("nonzero");
  1201. const imgEl = this.svgFactory.createElement("svg:image");
  1202. imgEl.setAttributeNS(XLINK_NS, "xlink:href", imgSrc);
  1203. imgEl.setAttributeNS(null, "x", "0");
  1204. imgEl.setAttributeNS(null, "y", pf(-height));
  1205. imgEl.setAttributeNS(null, "width", pf(width) + "px");
  1206. imgEl.setAttributeNS(null, "height", pf(height) + "px");
  1207. imgEl.setAttributeNS(null, "transform", `scale(${pf(1 / width)} ${pf(-1 / height)})`);
  1208. if (mask) {
  1209. mask.append(imgEl);
  1210. } else {
  1211. this._ensureTransformGroup().append(imgEl);
  1212. }
  1213. }
  1214. paintImageMaskXObject(img) {
  1215. const imgData = this.getObject(img.data, img);
  1216. if (imgData.bitmap) {
  1217. (0, _util.warn)("paintImageMaskXObject: ImageBitmap support is not implemented, " + "ensure that the `isOffscreenCanvasSupported` API parameter is disabled.");
  1218. return;
  1219. }
  1220. const current = this.current;
  1221. const width = imgData.width;
  1222. const height = imgData.height;
  1223. const fillColor = current.fillColor;
  1224. current.maskId = `mask${maskCount++}`;
  1225. const mask = this.svgFactory.createElement("svg:mask");
  1226. mask.setAttributeNS(null, "id", current.maskId);
  1227. const rect = this.svgFactory.createElement("svg:rect");
  1228. rect.setAttributeNS(null, "x", "0");
  1229. rect.setAttributeNS(null, "y", "0");
  1230. rect.setAttributeNS(null, "width", pf(width));
  1231. rect.setAttributeNS(null, "height", pf(height));
  1232. rect.setAttributeNS(null, "fill", fillColor);
  1233. rect.setAttributeNS(null, "mask", `url(#${current.maskId})`);
  1234. this.defs.append(mask);
  1235. this._ensureTransformGroup().append(rect);
  1236. this.paintInlineImageXObject(imgData, mask);
  1237. }
  1238. paintFormXObjectBegin(matrix, bbox) {
  1239. if (Array.isArray(matrix) && matrix.length === 6) {
  1240. this.transform(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
  1241. }
  1242. if (bbox) {
  1243. const width = bbox[2] - bbox[0];
  1244. const height = bbox[3] - bbox[1];
  1245. const cliprect = this.svgFactory.createElement("svg:rect");
  1246. cliprect.setAttributeNS(null, "x", bbox[0]);
  1247. cliprect.setAttributeNS(null, "y", bbox[1]);
  1248. cliprect.setAttributeNS(null, "width", pf(width));
  1249. cliprect.setAttributeNS(null, "height", pf(height));
  1250. this.current.element = cliprect;
  1251. this.clip("nonzero");
  1252. this.endPath();
  1253. }
  1254. }
  1255. paintFormXObjectEnd() {}
  1256. _initialize(viewport) {
  1257. const svg = this.svgFactory.create(viewport.width, viewport.height);
  1258. const definitions = this.svgFactory.createElement("svg:defs");
  1259. svg.append(definitions);
  1260. this.defs = definitions;
  1261. const rootGroup = this.svgFactory.createElement("svg:g");
  1262. rootGroup.setAttributeNS(null, "transform", pm(viewport.transform));
  1263. svg.append(rootGroup);
  1264. this.svg = rootGroup;
  1265. return svg;
  1266. }
  1267. _ensureClipGroup() {
  1268. if (!this.current.clipGroup) {
  1269. const clipGroup = this.svgFactory.createElement("svg:g");
  1270. clipGroup.setAttributeNS(null, "clip-path", this.current.activeClipUrl);
  1271. this.svg.append(clipGroup);
  1272. this.current.clipGroup = clipGroup;
  1273. }
  1274. return this.current.clipGroup;
  1275. }
  1276. _ensureTransformGroup() {
  1277. if (!this.tgrp) {
  1278. this.tgrp = this.svgFactory.createElement("svg:g");
  1279. this.tgrp.setAttributeNS(null, "transform", pm(this.transformMatrix));
  1280. if (this.current.activeClipUrl) {
  1281. this._ensureClipGroup().append(this.tgrp);
  1282. } else {
  1283. this.svg.append(this.tgrp);
  1284. }
  1285. }
  1286. return this.tgrp;
  1287. }
  1288. };
  1289. }