svg.js 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  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) {
  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.paintJpegXObject:
  509. this.paintJpegXObject(args[0], args[1], args[2]);
  510. break;
  511. case _util.OPS.paintImageXObject:
  512. this.paintImageXObject(args[0]);
  513. break;
  514. case _util.OPS.paintInlineImageXObject:
  515. this.paintInlineImageXObject(args[0]);
  516. break;
  517. case _util.OPS.paintImageMaskXObject:
  518. this.paintImageMaskXObject(args[0]);
  519. break;
  520. case _util.OPS.paintFormXObjectBegin:
  521. this.paintFormXObjectBegin(args[0], args[1]);
  522. break;
  523. case _util.OPS.paintFormXObjectEnd:
  524. this.paintFormXObjectEnd();
  525. break;
  526. case _util.OPS.closePath:
  527. this.closePath();
  528. break;
  529. case _util.OPS.closeStroke:
  530. this.closeStroke();
  531. break;
  532. case _util.OPS.closeFillStroke:
  533. this.closeFillStroke();
  534. break;
  535. case _util.OPS.closeEOFillStroke:
  536. this.closeEOFillStroke();
  537. break;
  538. case _util.OPS.nextLine:
  539. this.nextLine();
  540. break;
  541. case _util.OPS.transform:
  542. this.transform(args[0], args[1], args[2], args[3], args[4], args[5]);
  543. break;
  544. case _util.OPS.constructPath:
  545. this.constructPath(args[0], args[1]);
  546. break;
  547. case _util.OPS.endPath:
  548. this.endPath();
  549. break;
  550. case 92:
  551. this.group(opTreeElement.items);
  552. break;
  553. default:
  554. (0, _util.warn)(`Unimplemented operator ${fn}`);
  555. break;
  556. }
  557. }
  558. }
  559. setWordSpacing(wordSpacing) {
  560. this.current.wordSpacing = wordSpacing;
  561. }
  562. setCharSpacing(charSpacing) {
  563. this.current.charSpacing = charSpacing;
  564. }
  565. nextLine() {
  566. this.moveText(0, this.current.leading);
  567. }
  568. setTextMatrix(a, b, c, d, e, f) {
  569. const current = this.current;
  570. current.textMatrix = current.lineMatrix = [a, b, c, d, e, f];
  571. current.textMatrixScale = Math.sqrt(a * a + b * b);
  572. current.x = current.lineX = 0;
  573. current.y = current.lineY = 0;
  574. current.xcoords = [];
  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. }
  594. moveText(x, y) {
  595. const current = this.current;
  596. current.x = current.lineX += x;
  597. current.y = current.lineY += y;
  598. current.xcoords = [];
  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 charSpacing = current.charSpacing;
  612. const wordSpacing = current.wordSpacing;
  613. const fontDirection = current.fontDirection;
  614. const textHScale = current.textHScale * fontDirection;
  615. const vertical = font.vertical;
  616. const widthAdvanceScale = fontSize * current.fontMatrix[0];
  617. let x = 0;
  618. for (const glyph of glyphs) {
  619. if (glyph === null) {
  620. x += fontDirection * wordSpacing;
  621. continue;
  622. } else if ((0, _util.isNum)(glyph)) {
  623. x += -glyph * fontSize * 0.001;
  624. continue;
  625. }
  626. const width = glyph.width;
  627. const character = glyph.fontChar;
  628. const spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
  629. const charWidth = width * widthAdvanceScale + spacing * fontDirection;
  630. if (!glyph.isInFont && !font.missingFile) {
  631. x += charWidth;
  632. continue;
  633. }
  634. current.xcoords.push(current.x + x);
  635. current.tspan.textContent += character;
  636. x += charWidth;
  637. }
  638. if (vertical) {
  639. current.y -= x * textHScale;
  640. } else {
  641. current.x += x * textHScale;
  642. }
  643. current.tspan.setAttributeNS(null, "x", current.xcoords.map(pf).join(" "));
  644. current.tspan.setAttributeNS(null, "y", pf(-current.y));
  645. current.tspan.setAttributeNS(null, "font-family", current.fontFamily);
  646. current.tspan.setAttributeNS(null, "font-size", `${pf(current.fontSize)}px`);
  647. if (current.fontStyle !== SVG_DEFAULTS.fontStyle) {
  648. current.tspan.setAttributeNS(null, "font-style", current.fontStyle);
  649. }
  650. if (current.fontWeight !== SVG_DEFAULTS.fontWeight) {
  651. current.tspan.setAttributeNS(null, "font-weight", current.fontWeight);
  652. }
  653. const fillStrokeMode = current.textRenderingMode & _util.TextRenderingMode.FILL_STROKE_MASK;
  654. if (fillStrokeMode === _util.TextRenderingMode.FILL || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
  655. if (current.fillColor !== SVG_DEFAULTS.fillColor) {
  656. current.tspan.setAttributeNS(null, "fill", current.fillColor);
  657. }
  658. if (current.fillAlpha < 1) {
  659. current.tspan.setAttributeNS(null, "fill-opacity", current.fillAlpha);
  660. }
  661. } else if (current.textRenderingMode === _util.TextRenderingMode.ADD_TO_PATH) {
  662. current.tspan.setAttributeNS(null, "fill", "transparent");
  663. } else {
  664. current.tspan.setAttributeNS(null, "fill", "none");
  665. }
  666. if (fillStrokeMode === _util.TextRenderingMode.STROKE || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
  667. const lineWidthScale = 1 / (current.textMatrixScale || 1);
  668. this._setStrokeAttributes(current.tspan, lineWidthScale);
  669. }
  670. let textMatrix = current.textMatrix;
  671. if (current.textRise !== 0) {
  672. textMatrix = textMatrix.slice();
  673. textMatrix[5] += current.textRise;
  674. }
  675. current.txtElement.setAttributeNS(null, "transform", `${pm(textMatrix)} scale(${pf(textHScale)}, -1)`);
  676. current.txtElement.setAttributeNS(XML_NS, "xml:space", "preserve");
  677. current.txtElement.appendChild(current.tspan);
  678. current.txtgrp.appendChild(current.txtElement);
  679. this._ensureTransformGroup().appendChild(current.txtElement);
  680. }
  681. setLeadingMoveText(x, y) {
  682. this.setLeading(-y);
  683. this.moveText(x, y);
  684. }
  685. addFontStyle(fontObj) {
  686. if (!this.cssStyle) {
  687. this.cssStyle = this.svgFactory.createElement("svg:style");
  688. this.cssStyle.setAttributeNS(null, "type", "text/css");
  689. this.defs.appendChild(this.cssStyle);
  690. }
  691. const url = (0, _util.createObjectURL)(fontObj.data, fontObj.mimetype, this.forceDataSchema);
  692. this.cssStyle.textContent += `@font-face { font-family: "${fontObj.loadedName}";` + ` src: url(${url}); }\n`;
  693. }
  694. setFont(details) {
  695. const current = this.current;
  696. const fontObj = this.commonObjs.get(details[0]);
  697. let size = details[1];
  698. current.font = fontObj;
  699. if (this.embedFonts && fontObj.data && !this.embeddedFonts[fontObj.loadedName]) {
  700. this.addFontStyle(fontObj);
  701. this.embeddedFonts[fontObj.loadedName] = fontObj;
  702. }
  703. current.fontMatrix = fontObj.fontMatrix ? fontObj.fontMatrix : _util.FONT_IDENTITY_MATRIX;
  704. let bold = "normal";
  705. if (fontObj.black) {
  706. bold = "900";
  707. } else if (fontObj.bold) {
  708. bold = "bold";
  709. }
  710. const italic = fontObj.italic ? "italic" : "normal";
  711. if (size < 0) {
  712. size = -size;
  713. current.fontDirection = -1;
  714. } else {
  715. current.fontDirection = 1;
  716. }
  717. current.fontSize = size;
  718. current.fontFamily = fontObj.loadedName;
  719. current.fontWeight = bold;
  720. current.fontStyle = italic;
  721. current.tspan = this.svgFactory.createElement("svg:tspan");
  722. current.tspan.setAttributeNS(null, "y", pf(-current.y));
  723. current.xcoords = [];
  724. }
  725. endText() {
  726. const current = this.current;
  727. if (current.textRenderingMode & _util.TextRenderingMode.ADD_TO_PATH_FLAG && current.txtElement && current.txtElement.hasChildNodes()) {
  728. current.element = current.txtElement;
  729. this.clip("nonzero");
  730. this.endPath();
  731. }
  732. }
  733. setLineWidth(width) {
  734. if (width > 0) {
  735. this.current.lineWidth = width;
  736. }
  737. }
  738. setLineCap(style) {
  739. this.current.lineCap = LINE_CAP_STYLES[style];
  740. }
  741. setLineJoin(style) {
  742. this.current.lineJoin = LINE_JOIN_STYLES[style];
  743. }
  744. setMiterLimit(limit) {
  745. this.current.miterLimit = limit;
  746. }
  747. setStrokeAlpha(strokeAlpha) {
  748. this.current.strokeAlpha = strokeAlpha;
  749. }
  750. setStrokeRGBColor(r, g, b) {
  751. this.current.strokeColor = _util.Util.makeCssRgb(r, g, b);
  752. }
  753. setFillAlpha(fillAlpha) {
  754. this.current.fillAlpha = fillAlpha;
  755. }
  756. setFillRGBColor(r, g, b) {
  757. this.current.fillColor = _util.Util.makeCssRgb(r, g, b);
  758. this.current.tspan = this.svgFactory.createElement("svg:tspan");
  759. this.current.xcoords = [];
  760. }
  761. setStrokeColorN(args) {
  762. this.current.strokeColor = this._makeColorN_Pattern(args);
  763. }
  764. setFillColorN(args) {
  765. this.current.fillColor = this._makeColorN_Pattern(args);
  766. }
  767. shadingFill(args) {
  768. const width = this.viewport.width;
  769. const height = this.viewport.height;
  770. const inv = _util.Util.inverseTransform(this.transformMatrix);
  771. const bl = _util.Util.applyTransform([0, 0], inv);
  772. const br = _util.Util.applyTransform([0, height], inv);
  773. const ul = _util.Util.applyTransform([width, 0], inv);
  774. const ur = _util.Util.applyTransform([width, height], inv);
  775. const x0 = Math.min(bl[0], br[0], ul[0], ur[0]);
  776. const y0 = Math.min(bl[1], br[1], ul[1], ur[1]);
  777. const x1 = Math.max(bl[0], br[0], ul[0], ur[0]);
  778. const y1 = Math.max(bl[1], br[1], ul[1], ur[1]);
  779. const rect = this.svgFactory.createElement("svg:rect");
  780. rect.setAttributeNS(null, "x", x0);
  781. rect.setAttributeNS(null, "y", y0);
  782. rect.setAttributeNS(null, "width", x1 - x0);
  783. rect.setAttributeNS(null, "height", y1 - y0);
  784. rect.setAttributeNS(null, "fill", this._makeShadingPattern(args));
  785. this._ensureTransformGroup().appendChild(rect);
  786. }
  787. _makeColorN_Pattern(args) {
  788. if (args[0] === "TilingPattern") {
  789. return this._makeTilingPattern(args);
  790. }
  791. return this._makeShadingPattern(args);
  792. }
  793. _makeTilingPattern(args) {
  794. const color = args[1];
  795. const operatorList = args[2];
  796. const matrix = args[3] || _util.IDENTITY_MATRIX;
  797. const [x0, y0, x1, y1] = args[4];
  798. const xstep = args[5];
  799. const ystep = args[6];
  800. const paintType = args[7];
  801. const tilingId = `shading${shadingCount++}`;
  802. const [tx0, ty0] = _util.Util.applyTransform([x0, y0], matrix);
  803. const [tx1, ty1] = _util.Util.applyTransform([x1, y1], matrix);
  804. const [xscale, yscale] = _util.Util.singularValueDecompose2dScale(matrix);
  805. const txstep = xstep * xscale;
  806. const tystep = ystep * yscale;
  807. const tiling = this.svgFactory.createElement("svg:pattern");
  808. tiling.setAttributeNS(null, "id", tilingId);
  809. tiling.setAttributeNS(null, "patternUnits", "userSpaceOnUse");
  810. tiling.setAttributeNS(null, "width", txstep);
  811. tiling.setAttributeNS(null, "height", tystep);
  812. tiling.setAttributeNS(null, "x", `${tx0}`);
  813. tiling.setAttributeNS(null, "y", `${ty0}`);
  814. const svg = this.svg;
  815. const transformMatrix = this.transformMatrix;
  816. const fillColor = this.current.fillColor;
  817. const strokeColor = this.current.strokeColor;
  818. const bbox = this.svgFactory.create(tx1 - tx0, ty1 - ty0);
  819. this.svg = bbox;
  820. this.transformMatrix = matrix;
  821. if (paintType === 2) {
  822. const cssColor = _util.Util.makeCssRgb(...color);
  823. this.current.fillColor = cssColor;
  824. this.current.strokeColor = cssColor;
  825. }
  826. this.executeOpTree(this.convertOpList(operatorList));
  827. this.svg = svg;
  828. this.transformMatrix = transformMatrix;
  829. this.current.fillColor = fillColor;
  830. this.current.strokeColor = strokeColor;
  831. tiling.appendChild(bbox.childNodes[0]);
  832. this.defs.appendChild(tiling);
  833. return `url(#${tilingId})`;
  834. }
  835. _makeShadingPattern(args) {
  836. switch (args[0]) {
  837. case "RadialAxial":
  838. const shadingId = `shading${shadingCount++}`;
  839. const colorStops = args[3];
  840. let gradient;
  841. switch (args[1]) {
  842. case "axial":
  843. const point0 = args[4];
  844. const point1 = args[5];
  845. gradient = this.svgFactory.createElement("svg:linearGradient");
  846. gradient.setAttributeNS(null, "id", shadingId);
  847. gradient.setAttributeNS(null, "gradientUnits", "userSpaceOnUse");
  848. gradient.setAttributeNS(null, "x1", point0[0]);
  849. gradient.setAttributeNS(null, "y1", point0[1]);
  850. gradient.setAttributeNS(null, "x2", point1[0]);
  851. gradient.setAttributeNS(null, "y2", point1[1]);
  852. break;
  853. case "radial":
  854. const focalPoint = args[4];
  855. const circlePoint = args[5];
  856. const focalRadius = args[6];
  857. const circleRadius = args[7];
  858. gradient = this.svgFactory.createElement("svg:radialGradient");
  859. gradient.setAttributeNS(null, "id", shadingId);
  860. gradient.setAttributeNS(null, "gradientUnits", "userSpaceOnUse");
  861. gradient.setAttributeNS(null, "cx", circlePoint[0]);
  862. gradient.setAttributeNS(null, "cy", circlePoint[1]);
  863. gradient.setAttributeNS(null, "r", circleRadius);
  864. gradient.setAttributeNS(null, "fx", focalPoint[0]);
  865. gradient.setAttributeNS(null, "fy", focalPoint[1]);
  866. gradient.setAttributeNS(null, "fr", focalRadius);
  867. break;
  868. default:
  869. throw new Error(`Unknown RadialAxial type: ${args[1]}`);
  870. }
  871. for (const colorStop of colorStops) {
  872. const stop = this.svgFactory.createElement("svg:stop");
  873. stop.setAttributeNS(null, "offset", colorStop[0]);
  874. stop.setAttributeNS(null, "stop-color", colorStop[1]);
  875. gradient.appendChild(stop);
  876. }
  877. this.defs.appendChild(gradient);
  878. return `url(#${shadingId})`;
  879. case "Mesh":
  880. (0, _util.warn)("Unimplemented pattern Mesh");
  881. return null;
  882. case "Dummy":
  883. return "hotpink";
  884. default:
  885. throw new Error(`Unknown IR type: ${args[0]}`);
  886. }
  887. }
  888. setDash(dashArray, dashPhase) {
  889. this.current.dashArray = dashArray;
  890. this.current.dashPhase = dashPhase;
  891. }
  892. constructPath(ops, args) {
  893. const current = this.current;
  894. let x = current.x,
  895. y = current.y;
  896. let d = [];
  897. let j = 0;
  898. for (const op of ops) {
  899. switch (op | 0) {
  900. case _util.OPS.rectangle:
  901. x = args[j++];
  902. y = args[j++];
  903. const width = args[j++];
  904. const height = args[j++];
  905. const xw = x + width;
  906. const yh = y + height;
  907. d.push("M", pf(x), pf(y), "L", pf(xw), pf(y), "L", pf(xw), pf(yh), "L", pf(x), pf(yh), "Z");
  908. break;
  909. case _util.OPS.moveTo:
  910. x = args[j++];
  911. y = args[j++];
  912. d.push("M", pf(x), pf(y));
  913. break;
  914. case _util.OPS.lineTo:
  915. x = args[j++];
  916. y = args[j++];
  917. d.push("L", pf(x), pf(y));
  918. break;
  919. case _util.OPS.curveTo:
  920. x = args[j + 4];
  921. y = args[j + 5];
  922. d.push("C", pf(args[j]), pf(args[j + 1]), pf(args[j + 2]), pf(args[j + 3]), pf(x), pf(y));
  923. j += 6;
  924. break;
  925. case _util.OPS.curveTo2:
  926. d.push("C", pf(x), pf(y), pf(args[j]), pf(args[j + 1]), pf(args[j + 2]), pf(args[j + 3]));
  927. x = args[j + 2];
  928. y = args[j + 3];
  929. j += 4;
  930. break;
  931. case _util.OPS.curveTo3:
  932. x = args[j + 2];
  933. y = args[j + 3];
  934. d.push("C", pf(args[j]), pf(args[j + 1]), pf(x), pf(y), pf(x), pf(y));
  935. j += 4;
  936. break;
  937. case _util.OPS.closePath:
  938. d.push("Z");
  939. break;
  940. }
  941. }
  942. d = d.join(" ");
  943. if (current.path && ops.length > 0 && ops[0] !== _util.OPS.rectangle && ops[0] !== _util.OPS.moveTo) {
  944. d = current.path.getAttributeNS(null, "d") + d;
  945. } else {
  946. current.path = this.svgFactory.createElement("svg:path");
  947. this._ensureTransformGroup().appendChild(current.path);
  948. }
  949. current.path.setAttributeNS(null, "d", d);
  950. current.path.setAttributeNS(null, "fill", "none");
  951. current.element = current.path;
  952. current.setCurrentPoint(x, y);
  953. }
  954. endPath() {
  955. const current = this.current;
  956. current.path = null;
  957. if (!this.pendingClip) {
  958. return;
  959. }
  960. if (!current.element) {
  961. this.pendingClip = null;
  962. return;
  963. }
  964. const clipId = `clippath${clipCount++}`;
  965. const clipPath = this.svgFactory.createElement("svg:clipPath");
  966. clipPath.setAttributeNS(null, "id", clipId);
  967. clipPath.setAttributeNS(null, "transform", pm(this.transformMatrix));
  968. const clipElement = current.element.cloneNode(true);
  969. if (this.pendingClip === "evenodd") {
  970. clipElement.setAttributeNS(null, "clip-rule", "evenodd");
  971. } else {
  972. clipElement.setAttributeNS(null, "clip-rule", "nonzero");
  973. }
  974. this.pendingClip = null;
  975. clipPath.appendChild(clipElement);
  976. this.defs.appendChild(clipPath);
  977. if (current.activeClipUrl) {
  978. current.clipGroup = null;
  979. this.extraStack.forEach(function (prev) {
  980. prev.clipGroup = null;
  981. });
  982. clipPath.setAttributeNS(null, "clip-path", current.activeClipUrl);
  983. }
  984. current.activeClipUrl = `url(#${clipId})`;
  985. this.tgrp = null;
  986. }
  987. clip(type) {
  988. this.pendingClip = type;
  989. }
  990. closePath() {
  991. const current = this.current;
  992. if (current.path) {
  993. const d = `${current.path.getAttributeNS(null, "d")}Z`;
  994. current.path.setAttributeNS(null, "d", d);
  995. }
  996. }
  997. setLeading(leading) {
  998. this.current.leading = -leading;
  999. }
  1000. setTextRise(textRise) {
  1001. this.current.textRise = textRise;
  1002. }
  1003. setTextRenderingMode(textRenderingMode) {
  1004. this.current.textRenderingMode = textRenderingMode;
  1005. }
  1006. setHScale(scale) {
  1007. this.current.textHScale = scale / 100;
  1008. }
  1009. setRenderingIntent(intent) {}
  1010. setFlatness(flatness) {}
  1011. setGState(states) {
  1012. for (const [key, value] of states) {
  1013. switch (key) {
  1014. case "LW":
  1015. this.setLineWidth(value);
  1016. break;
  1017. case "LC":
  1018. this.setLineCap(value);
  1019. break;
  1020. case "LJ":
  1021. this.setLineJoin(value);
  1022. break;
  1023. case "ML":
  1024. this.setMiterLimit(value);
  1025. break;
  1026. case "D":
  1027. this.setDash(value[0], value[1]);
  1028. break;
  1029. case "RI":
  1030. this.setRenderingIntent(value);
  1031. break;
  1032. case "FL":
  1033. this.setFlatness(value);
  1034. break;
  1035. case "Font":
  1036. this.setFont(value);
  1037. break;
  1038. case "CA":
  1039. this.setStrokeAlpha(value);
  1040. break;
  1041. case "ca":
  1042. this.setFillAlpha(value);
  1043. break;
  1044. default:
  1045. (0, _util.warn)(`Unimplemented graphic state operator ${key}`);
  1046. break;
  1047. }
  1048. }
  1049. }
  1050. fill() {
  1051. const current = this.current;
  1052. if (current.element) {
  1053. current.element.setAttributeNS(null, "fill", current.fillColor);
  1054. current.element.setAttributeNS(null, "fill-opacity", current.fillAlpha);
  1055. this.endPath();
  1056. }
  1057. }
  1058. stroke() {
  1059. const current = this.current;
  1060. if (current.element) {
  1061. this._setStrokeAttributes(current.element);
  1062. current.element.setAttributeNS(null, "fill", "none");
  1063. this.endPath();
  1064. }
  1065. }
  1066. _setStrokeAttributes(element, lineWidthScale = 1) {
  1067. const current = this.current;
  1068. let dashArray = current.dashArray;
  1069. if (lineWidthScale !== 1 && dashArray.length > 0) {
  1070. dashArray = dashArray.map(function (value) {
  1071. return lineWidthScale * value;
  1072. });
  1073. }
  1074. element.setAttributeNS(null, "stroke", current.strokeColor);
  1075. element.setAttributeNS(null, "stroke-opacity", current.strokeAlpha);
  1076. element.setAttributeNS(null, "stroke-miterlimit", pf(current.miterLimit));
  1077. element.setAttributeNS(null, "stroke-linecap", current.lineCap);
  1078. element.setAttributeNS(null, "stroke-linejoin", current.lineJoin);
  1079. element.setAttributeNS(null, "stroke-width", pf(lineWidthScale * current.lineWidth) + "px");
  1080. element.setAttributeNS(null, "stroke-dasharray", dashArray.map(pf).join(" "));
  1081. element.setAttributeNS(null, "stroke-dashoffset", pf(lineWidthScale * current.dashPhase) + "px");
  1082. }
  1083. eoFill() {
  1084. if (this.current.element) {
  1085. this.current.element.setAttributeNS(null, "fill-rule", "evenodd");
  1086. }
  1087. this.fill();
  1088. }
  1089. fillStroke() {
  1090. this.stroke();
  1091. this.fill();
  1092. }
  1093. eoFillStroke() {
  1094. if (this.current.element) {
  1095. this.current.element.setAttributeNS(null, "fill-rule", "evenodd");
  1096. }
  1097. this.fillStroke();
  1098. }
  1099. closeStroke() {
  1100. this.closePath();
  1101. this.stroke();
  1102. }
  1103. closeFillStroke() {
  1104. this.closePath();
  1105. this.fillStroke();
  1106. }
  1107. closeEOFillStroke() {
  1108. this.closePath();
  1109. this.eoFillStroke();
  1110. }
  1111. paintSolidColorImageMask() {
  1112. const rect = this.svgFactory.createElement("svg:rect");
  1113. rect.setAttributeNS(null, "x", "0");
  1114. rect.setAttributeNS(null, "y", "0");
  1115. rect.setAttributeNS(null, "width", "1px");
  1116. rect.setAttributeNS(null, "height", "1px");
  1117. rect.setAttributeNS(null, "fill", this.current.fillColor);
  1118. this._ensureTransformGroup().appendChild(rect);
  1119. }
  1120. paintJpegXObject(objId, w, h) {
  1121. const imgObj = this.objs.get(objId);
  1122. const imgEl = this.svgFactory.createElement("svg:image");
  1123. imgEl.setAttributeNS(XLINK_NS, "xlink:href", imgObj.src);
  1124. imgEl.setAttributeNS(null, "width", pf(w));
  1125. imgEl.setAttributeNS(null, "height", pf(h));
  1126. imgEl.setAttributeNS(null, "x", "0");
  1127. imgEl.setAttributeNS(null, "y", pf(-h));
  1128. imgEl.setAttributeNS(null, "transform", `scale(${pf(1 / w)} ${pf(-1 / h)})`);
  1129. this._ensureTransformGroup().appendChild(imgEl);
  1130. }
  1131. paintImageXObject(objId) {
  1132. const imgData = this.objs.get(objId);
  1133. if (!imgData) {
  1134. (0, _util.warn)(`Dependent image with object ID ${objId} is not ready yet`);
  1135. return;
  1136. }
  1137. this.paintInlineImageXObject(imgData);
  1138. }
  1139. paintInlineImageXObject(imgData, mask) {
  1140. const width = imgData.width;
  1141. const height = imgData.height;
  1142. const imgSrc = convertImgDataToPng(imgData, this.forceDataSchema, !!mask);
  1143. const cliprect = this.svgFactory.createElement("svg:rect");
  1144. cliprect.setAttributeNS(null, "x", "0");
  1145. cliprect.setAttributeNS(null, "y", "0");
  1146. cliprect.setAttributeNS(null, "width", pf(width));
  1147. cliprect.setAttributeNS(null, "height", pf(height));
  1148. this.current.element = cliprect;
  1149. this.clip("nonzero");
  1150. const imgEl = this.svgFactory.createElement("svg:image");
  1151. imgEl.setAttributeNS(XLINK_NS, "xlink:href", imgSrc);
  1152. imgEl.setAttributeNS(null, "x", "0");
  1153. imgEl.setAttributeNS(null, "y", pf(-height));
  1154. imgEl.setAttributeNS(null, "width", pf(width) + "px");
  1155. imgEl.setAttributeNS(null, "height", pf(height) + "px");
  1156. imgEl.setAttributeNS(null, "transform", `scale(${pf(1 / width)} ${pf(-1 / height)})`);
  1157. if (mask) {
  1158. mask.appendChild(imgEl);
  1159. } else {
  1160. this._ensureTransformGroup().appendChild(imgEl);
  1161. }
  1162. }
  1163. paintImageMaskXObject(imgData) {
  1164. const current = this.current;
  1165. const width = imgData.width;
  1166. const height = imgData.height;
  1167. const fillColor = current.fillColor;
  1168. current.maskId = `mask${maskCount++}`;
  1169. const mask = this.svgFactory.createElement("svg:mask");
  1170. mask.setAttributeNS(null, "id", current.maskId);
  1171. const rect = this.svgFactory.createElement("svg:rect");
  1172. rect.setAttributeNS(null, "x", "0");
  1173. rect.setAttributeNS(null, "y", "0");
  1174. rect.setAttributeNS(null, "width", pf(width));
  1175. rect.setAttributeNS(null, "height", pf(height));
  1176. rect.setAttributeNS(null, "fill", fillColor);
  1177. rect.setAttributeNS(null, "mask", `url(#${current.maskId})`);
  1178. this.defs.appendChild(mask);
  1179. this._ensureTransformGroup().appendChild(rect);
  1180. this.paintInlineImageXObject(imgData, mask);
  1181. }
  1182. paintFormXObjectBegin(matrix, bbox) {
  1183. if (Array.isArray(matrix) && matrix.length === 6) {
  1184. this.transform(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
  1185. }
  1186. if (bbox) {
  1187. const width = bbox[2] - bbox[0];
  1188. const height = bbox[3] - bbox[1];
  1189. const cliprect = this.svgFactory.createElement("svg:rect");
  1190. cliprect.setAttributeNS(null, "x", bbox[0]);
  1191. cliprect.setAttributeNS(null, "y", bbox[1]);
  1192. cliprect.setAttributeNS(null, "width", pf(width));
  1193. cliprect.setAttributeNS(null, "height", pf(height));
  1194. this.current.element = cliprect;
  1195. this.clip("nonzero");
  1196. this.endPath();
  1197. }
  1198. }
  1199. paintFormXObjectEnd() {}
  1200. _initialize(viewport) {
  1201. const svg = this.svgFactory.create(viewport.width, viewport.height);
  1202. const definitions = this.svgFactory.createElement("svg:defs");
  1203. svg.appendChild(definitions);
  1204. this.defs = definitions;
  1205. const rootGroup = this.svgFactory.createElement("svg:g");
  1206. rootGroup.setAttributeNS(null, "transform", pm(viewport.transform));
  1207. svg.appendChild(rootGroup);
  1208. this.svg = rootGroup;
  1209. return svg;
  1210. }
  1211. _ensureClipGroup() {
  1212. if (!this.current.clipGroup) {
  1213. const clipGroup = this.svgFactory.createElement("svg:g");
  1214. clipGroup.setAttributeNS(null, "clip-path", this.current.activeClipUrl);
  1215. this.svg.appendChild(clipGroup);
  1216. this.current.clipGroup = clipGroup;
  1217. }
  1218. return this.current.clipGroup;
  1219. }
  1220. _ensureTransformGroup() {
  1221. if (!this.tgrp) {
  1222. this.tgrp = this.svgFactory.createElement("svg:g");
  1223. this.tgrp.setAttributeNS(null, "transform", pm(this.transformMatrix));
  1224. if (this.current.activeClipUrl) {
  1225. this._ensureClipGroup().appendChild(this.tgrp);
  1226. } else {
  1227. this.svg.appendChild(this.tgrp);
  1228. }
  1229. }
  1230. return this.tgrp;
  1231. }
  1232. };
  1233. }