svg.js 44 KB

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