2
0

svg.js 44 KB

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