svg.js 58 KB

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