svg.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  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] = _util.Util.applyTransform([x0, y0], matrix);
  840. const [tx1, ty1] = _util.Util.applyTransform([x1, y1], matrix);
  841. const [xscale, yscale] = _util.Util.singularValueDecompose2dScale(matrix);
  842. const txstep = xstep * xscale;
  843. const tystep = ystep * yscale;
  844. const tiling = this.svgFactory.createElement("svg:pattern");
  845. tiling.setAttributeNS(null, "id", tilingId);
  846. tiling.setAttributeNS(null, "patternUnits", "userSpaceOnUse");
  847. tiling.setAttributeNS(null, "width", txstep);
  848. tiling.setAttributeNS(null, "height", tystep);
  849. tiling.setAttributeNS(null, "x", `${tx0}`);
  850. tiling.setAttributeNS(null, "y", `${ty0}`);
  851. const svg = this.svg;
  852. const transformMatrix = this.transformMatrix;
  853. const fillColor = this.current.fillColor;
  854. const strokeColor = this.current.strokeColor;
  855. const bbox = this.svgFactory.create(tx1 - tx0, ty1 - ty0);
  856. this.svg = bbox;
  857. this.transformMatrix = matrix;
  858. if (paintType === 2) {
  859. const cssColor = _util.Util.makeHexColor(...color);
  860. this.current.fillColor = cssColor;
  861. this.current.strokeColor = cssColor;
  862. }
  863. this.executeOpTree(this.convertOpList(operatorList));
  864. this.svg = svg;
  865. this.transformMatrix = transformMatrix;
  866. this.current.fillColor = fillColor;
  867. this.current.strokeColor = strokeColor;
  868. tiling.appendChild(bbox.childNodes[0]);
  869. this.defs.appendChild(tiling);
  870. return `url(#${tilingId})`;
  871. }
  872. _makeShadingPattern(args) {
  873. switch (args[0]) {
  874. case "RadialAxial":
  875. const shadingId = `shading${shadingCount++}`;
  876. const colorStops = args[3];
  877. let gradient;
  878. switch (args[1]) {
  879. case "axial":
  880. const point0 = args[4];
  881. const point1 = args[5];
  882. gradient = this.svgFactory.createElement("svg:linearGradient");
  883. gradient.setAttributeNS(null, "id", shadingId);
  884. gradient.setAttributeNS(null, "gradientUnits", "userSpaceOnUse");
  885. gradient.setAttributeNS(null, "x1", point0[0]);
  886. gradient.setAttributeNS(null, "y1", point0[1]);
  887. gradient.setAttributeNS(null, "x2", point1[0]);
  888. gradient.setAttributeNS(null, "y2", point1[1]);
  889. break;
  890. case "radial":
  891. const focalPoint = args[4];
  892. const circlePoint = args[5];
  893. const focalRadius = args[6];
  894. const circleRadius = args[7];
  895. gradient = this.svgFactory.createElement("svg:radialGradient");
  896. gradient.setAttributeNS(null, "id", shadingId);
  897. gradient.setAttributeNS(null, "gradientUnits", "userSpaceOnUse");
  898. gradient.setAttributeNS(null, "cx", circlePoint[0]);
  899. gradient.setAttributeNS(null, "cy", circlePoint[1]);
  900. gradient.setAttributeNS(null, "r", circleRadius);
  901. gradient.setAttributeNS(null, "fx", focalPoint[0]);
  902. gradient.setAttributeNS(null, "fy", focalPoint[1]);
  903. gradient.setAttributeNS(null, "fr", focalRadius);
  904. break;
  905. default:
  906. throw new Error(`Unknown RadialAxial type: ${args[1]}`);
  907. }
  908. for (const colorStop of colorStops) {
  909. const stop = this.svgFactory.createElement("svg:stop");
  910. stop.setAttributeNS(null, "offset", colorStop[0]);
  911. stop.setAttributeNS(null, "stop-color", colorStop[1]);
  912. gradient.appendChild(stop);
  913. }
  914. this.defs.appendChild(gradient);
  915. return `url(#${shadingId})`;
  916. case "Mesh":
  917. (0, _util.warn)("Unimplemented pattern Mesh");
  918. return null;
  919. case "Dummy":
  920. return "hotpink";
  921. default:
  922. throw new Error(`Unknown IR type: ${args[0]}`);
  923. }
  924. }
  925. setDash(dashArray, dashPhase) {
  926. this.current.dashArray = dashArray;
  927. this.current.dashPhase = dashPhase;
  928. }
  929. constructPath(ops, args) {
  930. const current = this.current;
  931. let x = current.x,
  932. y = current.y;
  933. let d = [];
  934. let j = 0;
  935. for (const op of ops) {
  936. switch (op | 0) {
  937. case _util.OPS.rectangle:
  938. x = args[j++];
  939. y = args[j++];
  940. const width = args[j++];
  941. const height = args[j++];
  942. const xw = x + width;
  943. const yh = y + height;
  944. d.push("M", pf(x), pf(y), "L", pf(xw), pf(y), "L", pf(xw), pf(yh), "L", pf(x), pf(yh), "Z");
  945. break;
  946. case _util.OPS.moveTo:
  947. x = args[j++];
  948. y = args[j++];
  949. d.push("M", pf(x), pf(y));
  950. break;
  951. case _util.OPS.lineTo:
  952. x = args[j++];
  953. y = args[j++];
  954. d.push("L", pf(x), pf(y));
  955. break;
  956. case _util.OPS.curveTo:
  957. x = args[j + 4];
  958. y = args[j + 5];
  959. d.push("C", pf(args[j]), pf(args[j + 1]), pf(args[j + 2]), pf(args[j + 3]), pf(x), pf(y));
  960. j += 6;
  961. break;
  962. case _util.OPS.curveTo2:
  963. d.push("C", pf(x), pf(y), pf(args[j]), pf(args[j + 1]), pf(args[j + 2]), pf(args[j + 3]));
  964. x = args[j + 2];
  965. y = args[j + 3];
  966. j += 4;
  967. break;
  968. case _util.OPS.curveTo3:
  969. x = args[j + 2];
  970. y = args[j + 3];
  971. d.push("C", pf(args[j]), pf(args[j + 1]), pf(x), pf(y), pf(x), pf(y));
  972. j += 4;
  973. break;
  974. case _util.OPS.closePath:
  975. d.push("Z");
  976. break;
  977. }
  978. }
  979. d = d.join(" ");
  980. if (current.path && ops.length > 0 && ops[0] !== _util.OPS.rectangle && ops[0] !== _util.OPS.moveTo) {
  981. d = current.path.getAttributeNS(null, "d") + d;
  982. } else {
  983. current.path = this.svgFactory.createElement("svg:path");
  984. this._ensureTransformGroup().appendChild(current.path);
  985. }
  986. current.path.setAttributeNS(null, "d", d);
  987. current.path.setAttributeNS(null, "fill", "none");
  988. current.element = current.path;
  989. current.setCurrentPoint(x, y);
  990. }
  991. endPath() {
  992. const current = this.current;
  993. current.path = null;
  994. if (!this.pendingClip) {
  995. return;
  996. }
  997. if (!current.element) {
  998. this.pendingClip = null;
  999. return;
  1000. }
  1001. const clipId = `clippath${clipCount++}`;
  1002. const clipPath = this.svgFactory.createElement("svg:clipPath");
  1003. clipPath.setAttributeNS(null, "id", clipId);
  1004. clipPath.setAttributeNS(null, "transform", pm(this.transformMatrix));
  1005. const clipElement = current.element.cloneNode(true);
  1006. if (this.pendingClip === "evenodd") {
  1007. clipElement.setAttributeNS(null, "clip-rule", "evenodd");
  1008. } else {
  1009. clipElement.setAttributeNS(null, "clip-rule", "nonzero");
  1010. }
  1011. this.pendingClip = null;
  1012. clipPath.appendChild(clipElement);
  1013. this.defs.appendChild(clipPath);
  1014. if (current.activeClipUrl) {
  1015. current.clipGroup = null;
  1016. for (const prev of this.extraStack) {
  1017. prev.clipGroup = null;
  1018. }
  1019. clipPath.setAttributeNS(null, "clip-path", current.activeClipUrl);
  1020. }
  1021. current.activeClipUrl = `url(#${clipId})`;
  1022. this.tgrp = null;
  1023. }
  1024. clip(type) {
  1025. this.pendingClip = type;
  1026. }
  1027. closePath() {
  1028. const current = this.current;
  1029. if (current.path) {
  1030. const d = `${current.path.getAttributeNS(null, "d")}Z`;
  1031. current.path.setAttributeNS(null, "d", d);
  1032. }
  1033. }
  1034. setLeading(leading) {
  1035. this.current.leading = -leading;
  1036. }
  1037. setTextRise(textRise) {
  1038. this.current.textRise = textRise;
  1039. }
  1040. setTextRenderingMode(textRenderingMode) {
  1041. this.current.textRenderingMode = textRenderingMode;
  1042. }
  1043. setHScale(scale) {
  1044. this.current.textHScale = scale / 100;
  1045. }
  1046. setRenderingIntent(intent) {}
  1047. setFlatness(flatness) {}
  1048. setGState(states) {
  1049. for (const [key, value] of states) {
  1050. switch (key) {
  1051. case "LW":
  1052. this.setLineWidth(value);
  1053. break;
  1054. case "LC":
  1055. this.setLineCap(value);
  1056. break;
  1057. case "LJ":
  1058. this.setLineJoin(value);
  1059. break;
  1060. case "ML":
  1061. this.setMiterLimit(value);
  1062. break;
  1063. case "D":
  1064. this.setDash(value[0], value[1]);
  1065. break;
  1066. case "RI":
  1067. this.setRenderingIntent(value);
  1068. break;
  1069. case "FL":
  1070. this.setFlatness(value);
  1071. break;
  1072. case "Font":
  1073. this.setFont(value);
  1074. break;
  1075. case "CA":
  1076. this.setStrokeAlpha(value);
  1077. break;
  1078. case "ca":
  1079. this.setFillAlpha(value);
  1080. break;
  1081. default:
  1082. (0, _util.warn)(`Unimplemented graphic state operator ${key}`);
  1083. break;
  1084. }
  1085. }
  1086. }
  1087. fill() {
  1088. const current = this.current;
  1089. if (current.element) {
  1090. current.element.setAttributeNS(null, "fill", current.fillColor);
  1091. current.element.setAttributeNS(null, "fill-opacity", current.fillAlpha);
  1092. this.endPath();
  1093. }
  1094. }
  1095. stroke() {
  1096. const current = this.current;
  1097. if (current.element) {
  1098. this._setStrokeAttributes(current.element);
  1099. current.element.setAttributeNS(null, "fill", "none");
  1100. this.endPath();
  1101. }
  1102. }
  1103. _setStrokeAttributes(element, lineWidthScale = 1) {
  1104. const current = this.current;
  1105. let dashArray = current.dashArray;
  1106. if (lineWidthScale !== 1 && dashArray.length > 0) {
  1107. dashArray = dashArray.map(function (value) {
  1108. return lineWidthScale * value;
  1109. });
  1110. }
  1111. element.setAttributeNS(null, "stroke", current.strokeColor);
  1112. element.setAttributeNS(null, "stroke-opacity", current.strokeAlpha);
  1113. element.setAttributeNS(null, "stroke-miterlimit", pf(current.miterLimit));
  1114. element.setAttributeNS(null, "stroke-linecap", current.lineCap);
  1115. element.setAttributeNS(null, "stroke-linejoin", current.lineJoin);
  1116. element.setAttributeNS(null, "stroke-width", pf(lineWidthScale * current.lineWidth) + "px");
  1117. element.setAttributeNS(null, "stroke-dasharray", dashArray.map(pf).join(" "));
  1118. element.setAttributeNS(null, "stroke-dashoffset", pf(lineWidthScale * current.dashPhase) + "px");
  1119. }
  1120. eoFill() {
  1121. if (this.current.element) {
  1122. this.current.element.setAttributeNS(null, "fill-rule", "evenodd");
  1123. }
  1124. this.fill();
  1125. }
  1126. fillStroke() {
  1127. this.stroke();
  1128. this.fill();
  1129. }
  1130. eoFillStroke() {
  1131. if (this.current.element) {
  1132. this.current.element.setAttributeNS(null, "fill-rule", "evenodd");
  1133. }
  1134. this.fillStroke();
  1135. }
  1136. closeStroke() {
  1137. this.closePath();
  1138. this.stroke();
  1139. }
  1140. closeFillStroke() {
  1141. this.closePath();
  1142. this.fillStroke();
  1143. }
  1144. closeEOFillStroke() {
  1145. this.closePath();
  1146. this.eoFillStroke();
  1147. }
  1148. paintSolidColorImageMask() {
  1149. const rect = this.svgFactory.createElement("svg:rect");
  1150. rect.setAttributeNS(null, "x", "0");
  1151. rect.setAttributeNS(null, "y", "0");
  1152. rect.setAttributeNS(null, "width", "1px");
  1153. rect.setAttributeNS(null, "height", "1px");
  1154. rect.setAttributeNS(null, "fill", this.current.fillColor);
  1155. this._ensureTransformGroup().appendChild(rect);
  1156. }
  1157. paintImageXObject(objId) {
  1158. const imgData = objId.startsWith("g_") ? this.commonObjs.get(objId) : this.objs.get(objId);
  1159. if (!imgData) {
  1160. (0, _util.warn)(`Dependent image with object ID ${objId} is not ready yet`);
  1161. return;
  1162. }
  1163. this.paintInlineImageXObject(imgData);
  1164. }
  1165. paintInlineImageXObject(imgData, mask) {
  1166. const width = imgData.width;
  1167. const height = imgData.height;
  1168. const imgSrc = convertImgDataToPng(imgData, this.forceDataSchema, !!mask);
  1169. const cliprect = this.svgFactory.createElement("svg:rect");
  1170. cliprect.setAttributeNS(null, "x", "0");
  1171. cliprect.setAttributeNS(null, "y", "0");
  1172. cliprect.setAttributeNS(null, "width", pf(width));
  1173. cliprect.setAttributeNS(null, "height", pf(height));
  1174. this.current.element = cliprect;
  1175. this.clip("nonzero");
  1176. const imgEl = this.svgFactory.createElement("svg:image");
  1177. imgEl.setAttributeNS(XLINK_NS, "xlink:href", imgSrc);
  1178. imgEl.setAttributeNS(null, "x", "0");
  1179. imgEl.setAttributeNS(null, "y", pf(-height));
  1180. imgEl.setAttributeNS(null, "width", pf(width) + "px");
  1181. imgEl.setAttributeNS(null, "height", pf(height) + "px");
  1182. imgEl.setAttributeNS(null, "transform", `scale(${pf(1 / width)} ${pf(-1 / height)})`);
  1183. if (mask) {
  1184. mask.appendChild(imgEl);
  1185. } else {
  1186. this._ensureTransformGroup().appendChild(imgEl);
  1187. }
  1188. }
  1189. paintImageMaskXObject(imgData) {
  1190. const current = this.current;
  1191. const width = imgData.width;
  1192. const height = imgData.height;
  1193. const fillColor = current.fillColor;
  1194. current.maskId = `mask${maskCount++}`;
  1195. const mask = this.svgFactory.createElement("svg:mask");
  1196. mask.setAttributeNS(null, "id", current.maskId);
  1197. const rect = this.svgFactory.createElement("svg:rect");
  1198. rect.setAttributeNS(null, "x", "0");
  1199. rect.setAttributeNS(null, "y", "0");
  1200. rect.setAttributeNS(null, "width", pf(width));
  1201. rect.setAttributeNS(null, "height", pf(height));
  1202. rect.setAttributeNS(null, "fill", fillColor);
  1203. rect.setAttributeNS(null, "mask", `url(#${current.maskId})`);
  1204. this.defs.appendChild(mask);
  1205. this._ensureTransformGroup().appendChild(rect);
  1206. this.paintInlineImageXObject(imgData, mask);
  1207. }
  1208. paintFormXObjectBegin(matrix, bbox) {
  1209. if (Array.isArray(matrix) && matrix.length === 6) {
  1210. this.transform(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
  1211. }
  1212. if (bbox) {
  1213. const width = bbox[2] - bbox[0];
  1214. const height = bbox[3] - bbox[1];
  1215. const cliprect = this.svgFactory.createElement("svg:rect");
  1216. cliprect.setAttributeNS(null, "x", bbox[0]);
  1217. cliprect.setAttributeNS(null, "y", bbox[1]);
  1218. cliprect.setAttributeNS(null, "width", pf(width));
  1219. cliprect.setAttributeNS(null, "height", pf(height));
  1220. this.current.element = cliprect;
  1221. this.clip("nonzero");
  1222. this.endPath();
  1223. }
  1224. }
  1225. paintFormXObjectEnd() {}
  1226. _initialize(viewport) {
  1227. const svg = this.svgFactory.create(viewport.width, viewport.height);
  1228. const definitions = this.svgFactory.createElement("svg:defs");
  1229. svg.appendChild(definitions);
  1230. this.defs = definitions;
  1231. const rootGroup = this.svgFactory.createElement("svg:g");
  1232. rootGroup.setAttributeNS(null, "transform", pm(viewport.transform));
  1233. svg.appendChild(rootGroup);
  1234. this.svg = rootGroup;
  1235. return svg;
  1236. }
  1237. _ensureClipGroup() {
  1238. if (!this.current.clipGroup) {
  1239. const clipGroup = this.svgFactory.createElement("svg:g");
  1240. clipGroup.setAttributeNS(null, "clip-path", this.current.activeClipUrl);
  1241. this.svg.appendChild(clipGroup);
  1242. this.current.clipGroup = clipGroup;
  1243. }
  1244. return this.current.clipGroup;
  1245. }
  1246. _ensureTransformGroup() {
  1247. if (!this.tgrp) {
  1248. this.tgrp = this.svgFactory.createElement("svg:g");
  1249. this.tgrp.setAttributeNS(null, "transform", pm(this.transformMatrix));
  1250. if (this.current.activeClipUrl) {
  1251. this._ensureClipGroup().appendChild(this.tgrp);
  1252. } else {
  1253. this.svg.appendChild(this.tgrp);
  1254. }
  1255. }
  1256. return this.tgrp;
  1257. }
  1258. };
  1259. }