svg.js 45 KB

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