svg.js 44 KB

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