svg.js 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 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 = undefined;
  27. var _util = require('../shared/util');
  28. var _dom_utils = require('./dom_utils');
  29. var _is_node = require('../shared/is_node');
  30. var _is_node2 = _interopRequireDefault(_is_node);
  31. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  32. var SVGGraphics = function SVGGraphics() {
  33. throw new Error('Not implemented: SVGGraphics');
  34. };
  35. {
  36. var SVG_DEFAULTS = {
  37. fontStyle: 'normal',
  38. fontWeight: 'normal',
  39. fillColor: '#000000'
  40. };
  41. var convertImgDataToPng = function convertImgDataToPngClosure() {
  42. var PNG_HEADER = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
  43. var CHUNK_WRAPPER_SIZE = 12;
  44. var crcTable = new Int32Array(256);
  45. for (var i = 0; i < 256; i++) {
  46. var c = i;
  47. for (var h = 0; h < 8; h++) {
  48. if (c & 1) {
  49. c = 0xedB88320 ^ c >> 1 & 0x7fffffff;
  50. } else {
  51. c = c >> 1 & 0x7fffffff;
  52. }
  53. }
  54. crcTable[i] = c;
  55. }
  56. function crc32(data, start, end) {
  57. var crc = -1;
  58. for (var i = start; i < end; i++) {
  59. var a = (crc ^ data[i]) & 0xff;
  60. var b = crcTable[a];
  61. crc = crc >>> 8 ^ b;
  62. }
  63. return crc ^ -1;
  64. }
  65. function writePngChunk(type, body, data, offset) {
  66. var p = offset;
  67. var len = body.length;
  68. data[p] = len >> 24 & 0xff;
  69. data[p + 1] = len >> 16 & 0xff;
  70. data[p + 2] = len >> 8 & 0xff;
  71. data[p + 3] = len & 0xff;
  72. p += 4;
  73. data[p] = type.charCodeAt(0) & 0xff;
  74. data[p + 1] = type.charCodeAt(1) & 0xff;
  75. data[p + 2] = type.charCodeAt(2) & 0xff;
  76. data[p + 3] = type.charCodeAt(3) & 0xff;
  77. p += 4;
  78. data.set(body, p);
  79. p += body.length;
  80. var crc = crc32(data, offset + 4, p);
  81. data[p] = crc >> 24 & 0xff;
  82. data[p + 1] = crc >> 16 & 0xff;
  83. data[p + 2] = crc >> 8 & 0xff;
  84. data[p + 3] = crc & 0xff;
  85. }
  86. function adler32(data, start, end) {
  87. var a = 1;
  88. var b = 0;
  89. for (var i = start; i < end; ++i) {
  90. a = (a + (data[i] & 0xff)) % 65521;
  91. b = (b + a) % 65521;
  92. }
  93. return b << 16 | a;
  94. }
  95. function deflateSync(literals) {
  96. if (!(0, _is_node2.default)()) {
  97. return deflateSyncUncompressed(literals);
  98. }
  99. try {
  100. var input;
  101. if (parseInt(process.versions.node) >= 8) {
  102. input = literals;
  103. } else {
  104. input = new Buffer(literals);
  105. }
  106. var output = require('zlib').deflateSync(input, { level: 9 });
  107. return output instanceof Uint8Array ? output : new Uint8Array(output);
  108. } catch (e) {
  109. (0, _util.warn)('Not compressing PNG because zlib.deflateSync is unavailable: ' + e);
  110. }
  111. return deflateSyncUncompressed(literals);
  112. }
  113. function deflateSyncUncompressed(literals) {
  114. var len = literals.length;
  115. var maxBlockLength = 0xFFFF;
  116. var deflateBlocks = Math.ceil(len / maxBlockLength);
  117. var idat = new Uint8Array(2 + len + deflateBlocks * 5 + 4);
  118. var pi = 0;
  119. idat[pi++] = 0x78;
  120. idat[pi++] = 0x9c;
  121. var pos = 0;
  122. while (len > maxBlockLength) {
  123. idat[pi++] = 0x00;
  124. idat[pi++] = 0xff;
  125. idat[pi++] = 0xff;
  126. idat[pi++] = 0x00;
  127. idat[pi++] = 0x00;
  128. idat.set(literals.subarray(pos, pos + maxBlockLength), pi);
  129. pi += maxBlockLength;
  130. pos += maxBlockLength;
  131. len -= maxBlockLength;
  132. }
  133. idat[pi++] = 0x01;
  134. idat[pi++] = len & 0xff;
  135. idat[pi++] = len >> 8 & 0xff;
  136. idat[pi++] = ~len & 0xffff & 0xff;
  137. idat[pi++] = (~len & 0xffff) >> 8 & 0xff;
  138. idat.set(literals.subarray(pos), pi);
  139. pi += literals.length - pos;
  140. var adler = adler32(literals, 0, literals.length);
  141. idat[pi++] = adler >> 24 & 0xff;
  142. idat[pi++] = adler >> 16 & 0xff;
  143. idat[pi++] = adler >> 8 & 0xff;
  144. idat[pi++] = adler & 0xff;
  145. return idat;
  146. }
  147. function encode(imgData, kind, forceDataSchema, isMask) {
  148. var width = imgData.width;
  149. var height = imgData.height;
  150. var bitDepth, colorType, lineSize;
  151. var bytes = imgData.data;
  152. switch (kind) {
  153. case _util.ImageKind.GRAYSCALE_1BPP:
  154. colorType = 0;
  155. bitDepth = 1;
  156. lineSize = width + 7 >> 3;
  157. break;
  158. case _util.ImageKind.RGB_24BPP:
  159. colorType = 2;
  160. bitDepth = 8;
  161. lineSize = width * 3;
  162. break;
  163. case _util.ImageKind.RGBA_32BPP:
  164. colorType = 6;
  165. bitDepth = 8;
  166. lineSize = width * 4;
  167. break;
  168. default:
  169. throw new Error('invalid format');
  170. }
  171. var literals = new Uint8Array((1 + lineSize) * height);
  172. var offsetLiterals = 0,
  173. offsetBytes = 0;
  174. var y, i;
  175. for (y = 0; y < height; ++y) {
  176. literals[offsetLiterals++] = 0;
  177. literals.set(bytes.subarray(offsetBytes, offsetBytes + lineSize), offsetLiterals);
  178. offsetBytes += lineSize;
  179. offsetLiterals += lineSize;
  180. }
  181. if (kind === _util.ImageKind.GRAYSCALE_1BPP && isMask) {
  182. offsetLiterals = 0;
  183. for (y = 0; y < height; y++) {
  184. offsetLiterals++;
  185. for (i = 0; i < lineSize; i++) {
  186. literals[offsetLiterals++] ^= 0xFF;
  187. }
  188. }
  189. }
  190. 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]);
  191. var idat = deflateSync(literals);
  192. var pngLength = PNG_HEADER.length + CHUNK_WRAPPER_SIZE * 3 + ihdr.length + idat.length;
  193. var data = new Uint8Array(pngLength);
  194. var offset = 0;
  195. data.set(PNG_HEADER, offset);
  196. offset += PNG_HEADER.length;
  197. writePngChunk('IHDR', ihdr, data, offset);
  198. offset += CHUNK_WRAPPER_SIZE + ihdr.length;
  199. writePngChunk('IDATA', idat, data, offset);
  200. offset += CHUNK_WRAPPER_SIZE + idat.length;
  201. writePngChunk('IEND', new Uint8Array(0), data, offset);
  202. return (0, _util.createObjectURL)(data, 'image/png', forceDataSchema);
  203. }
  204. return function convertImgDataToPng(imgData, forceDataSchema, isMask) {
  205. var kind = imgData.kind === undefined ? _util.ImageKind.GRAYSCALE_1BPP : imgData.kind;
  206. return encode(imgData, kind, forceDataSchema, isMask);
  207. };
  208. }();
  209. var SVGExtraState = function SVGExtraStateClosure() {
  210. function SVGExtraState() {
  211. this.fontSizeScale = 1;
  212. this.fontWeight = SVG_DEFAULTS.fontWeight;
  213. this.fontSize = 0;
  214. this.textMatrix = _util.IDENTITY_MATRIX;
  215. this.fontMatrix = _util.FONT_IDENTITY_MATRIX;
  216. this.leading = 0;
  217. this.x = 0;
  218. this.y = 0;
  219. this.lineX = 0;
  220. this.lineY = 0;
  221. this.charSpacing = 0;
  222. this.wordSpacing = 0;
  223. this.textHScale = 1;
  224. this.textRise = 0;
  225. this.fillColor = SVG_DEFAULTS.fillColor;
  226. this.strokeColor = '#000000';
  227. this.fillAlpha = 1;
  228. this.strokeAlpha = 1;
  229. this.lineWidth = 1;
  230. this.lineJoin = '';
  231. this.lineCap = '';
  232. this.miterLimit = 0;
  233. this.dashArray = [];
  234. this.dashPhase = 0;
  235. this.dependencies = [];
  236. this.activeClipUrl = null;
  237. this.clipGroup = null;
  238. this.maskId = '';
  239. }
  240. SVGExtraState.prototype = {
  241. clone: function SVGExtraState_clone() {
  242. return Object.create(this);
  243. },
  244. setCurrentPoint: function SVGExtraState_setCurrentPoint(x, y) {
  245. this.x = x;
  246. this.y = y;
  247. }
  248. };
  249. return SVGExtraState;
  250. }();
  251. exports.SVGGraphics = SVGGraphics = function SVGGraphicsClosure() {
  252. function opListToTree(opList) {
  253. var opTree = [];
  254. var tmp = [];
  255. var opListLen = opList.length;
  256. for (var x = 0; x < opListLen; x++) {
  257. if (opList[x].fn === 'save') {
  258. opTree.push({
  259. 'fnId': 92,
  260. 'fn': 'group',
  261. 'items': []
  262. });
  263. tmp.push(opTree);
  264. opTree = opTree[opTree.length - 1].items;
  265. continue;
  266. }
  267. if (opList[x].fn === 'restore') {
  268. opTree = tmp.pop();
  269. } else {
  270. opTree.push(opList[x]);
  271. }
  272. }
  273. return opTree;
  274. }
  275. function pf(value) {
  276. if (Number.isInteger(value)) {
  277. return value.toString();
  278. }
  279. var s = value.toFixed(10);
  280. var i = s.length - 1;
  281. if (s[i] !== '0') {
  282. return s;
  283. }
  284. do {
  285. i--;
  286. } while (s[i] === '0');
  287. return s.substr(0, s[i] === '.' ? i : i + 1);
  288. }
  289. function pm(m) {
  290. if (m[4] === 0 && m[5] === 0) {
  291. if (m[1] === 0 && m[2] === 0) {
  292. if (m[0] === 1 && m[3] === 1) {
  293. return '';
  294. }
  295. return 'scale(' + pf(m[0]) + ' ' + pf(m[3]) + ')';
  296. }
  297. if (m[0] === m[3] && m[1] === -m[2]) {
  298. var a = Math.acos(m[0]) * 180 / Math.PI;
  299. return 'rotate(' + pf(a) + ')';
  300. }
  301. } else {
  302. if (m[0] === 1 && m[1] === 0 && m[2] === 0 && m[3] === 1) {
  303. return 'translate(' + pf(m[4]) + ' ' + pf(m[5]) + ')';
  304. }
  305. }
  306. return 'matrix(' + pf(m[0]) + ' ' + pf(m[1]) + ' ' + pf(m[2]) + ' ' + pf(m[3]) + ' ' + pf(m[4]) + ' ' + pf(m[5]) + ')';
  307. }
  308. function SVGGraphics(commonObjs, objs, forceDataSchema) {
  309. this.svgFactory = new _dom_utils.DOMSVGFactory();
  310. this.current = new SVGExtraState();
  311. this.transformMatrix = _util.IDENTITY_MATRIX;
  312. this.transformStack = [];
  313. this.extraStack = [];
  314. this.commonObjs = commonObjs;
  315. this.objs = objs;
  316. this.pendingClip = null;
  317. this.pendingEOFill = false;
  318. this.embedFonts = false;
  319. this.embeddedFonts = Object.create(null);
  320. this.cssStyle = null;
  321. this.forceDataSchema = !!forceDataSchema;
  322. }
  323. var XML_NS = 'http://www.w3.org/XML/1998/namespace';
  324. var XLINK_NS = 'http://www.w3.org/1999/xlink';
  325. var LINE_CAP_STYLES = ['butt', 'round', 'square'];
  326. var LINE_JOIN_STYLES = ['miter', 'round', 'bevel'];
  327. var clipCount = 0;
  328. var maskCount = 0;
  329. SVGGraphics.prototype = {
  330. save: function SVGGraphics_save() {
  331. this.transformStack.push(this.transformMatrix);
  332. var old = this.current;
  333. this.extraStack.push(old);
  334. this.current = old.clone();
  335. },
  336. restore: function SVGGraphics_restore() {
  337. this.transformMatrix = this.transformStack.pop();
  338. this.current = this.extraStack.pop();
  339. this.pendingClip = null;
  340. this.tgrp = null;
  341. },
  342. group: function SVGGraphics_group(items) {
  343. this.save();
  344. this.executeOpTree(items);
  345. this.restore();
  346. },
  347. loadDependencies: function SVGGraphics_loadDependencies(operatorList) {
  348. var _this = this;
  349. var fnArray = operatorList.fnArray;
  350. var fnArrayLen = fnArray.length;
  351. var argsArray = operatorList.argsArray;
  352. for (var i = 0; i < fnArrayLen; i++) {
  353. if (_util.OPS.dependency === fnArray[i]) {
  354. var deps = argsArray[i];
  355. for (var n = 0, nn = deps.length; n < nn; n++) {
  356. var obj = deps[n];
  357. var common = obj.substring(0, 2) === 'g_';
  358. var promise;
  359. if (common) {
  360. promise = new Promise(function (resolve) {
  361. _this.commonObjs.get(obj, resolve);
  362. });
  363. } else {
  364. promise = new Promise(function (resolve) {
  365. _this.objs.get(obj, resolve);
  366. });
  367. }
  368. this.current.dependencies.push(promise);
  369. }
  370. }
  371. }
  372. return Promise.all(this.current.dependencies);
  373. },
  374. transform: function SVGGraphics_transform(a, b, c, d, e, f) {
  375. var transformMatrix = [a, b, c, d, e, f];
  376. this.transformMatrix = _util.Util.transform(this.transformMatrix, transformMatrix);
  377. this.tgrp = null;
  378. },
  379. getSVG: function SVGGraphics_getSVG(operatorList, viewport) {
  380. var _this2 = this;
  381. this.viewport = viewport;
  382. var svgElement = this._initialize(viewport);
  383. return this.loadDependencies(operatorList).then(function () {
  384. _this2.transformMatrix = _util.IDENTITY_MATRIX;
  385. var opTree = _this2.convertOpList(operatorList);
  386. _this2.executeOpTree(opTree);
  387. return svgElement;
  388. });
  389. },
  390. convertOpList: function SVGGraphics_convertOpList(operatorList) {
  391. var argsArray = operatorList.argsArray;
  392. var fnArray = operatorList.fnArray;
  393. var fnArrayLen = fnArray.length;
  394. var REVOPS = [];
  395. var opList = [];
  396. for (var op in _util.OPS) {
  397. REVOPS[_util.OPS[op]] = op;
  398. }
  399. for (var x = 0; x < fnArrayLen; x++) {
  400. var fnId = fnArray[x];
  401. opList.push({
  402. 'fnId': fnId,
  403. 'fn': REVOPS[fnId],
  404. 'args': argsArray[x]
  405. });
  406. }
  407. return opListToTree(opList);
  408. },
  409. executeOpTree: function SVGGraphics_executeOpTree(opTree) {
  410. var opTreeLen = opTree.length;
  411. for (var x = 0; x < opTreeLen; x++) {
  412. var fn = opTree[x].fn;
  413. var fnId = opTree[x].fnId;
  414. var args = opTree[x].args;
  415. switch (fnId | 0) {
  416. case _util.OPS.beginText:
  417. this.beginText();
  418. break;
  419. case _util.OPS.dependency:
  420. break;
  421. case _util.OPS.setLeading:
  422. this.setLeading(args);
  423. break;
  424. case _util.OPS.setLeadingMoveText:
  425. this.setLeadingMoveText(args[0], args[1]);
  426. break;
  427. case _util.OPS.setFont:
  428. this.setFont(args);
  429. break;
  430. case _util.OPS.showText:
  431. this.showText(args[0]);
  432. break;
  433. case _util.OPS.showSpacedText:
  434. this.showText(args[0]);
  435. break;
  436. case _util.OPS.endText:
  437. this.endText();
  438. break;
  439. case _util.OPS.moveText:
  440. this.moveText(args[0], args[1]);
  441. break;
  442. case _util.OPS.setCharSpacing:
  443. this.setCharSpacing(args[0]);
  444. break;
  445. case _util.OPS.setWordSpacing:
  446. this.setWordSpacing(args[0]);
  447. break;
  448. case _util.OPS.setHScale:
  449. this.setHScale(args[0]);
  450. break;
  451. case _util.OPS.setTextMatrix:
  452. this.setTextMatrix(args[0], args[1], args[2], args[3], args[4], args[5]);
  453. break;
  454. case _util.OPS.setTextRise:
  455. this.setTextRise(args[0]);
  456. break;
  457. case _util.OPS.setLineWidth:
  458. this.setLineWidth(args[0]);
  459. break;
  460. case _util.OPS.setLineJoin:
  461. this.setLineJoin(args[0]);
  462. break;
  463. case _util.OPS.setLineCap:
  464. this.setLineCap(args[0]);
  465. break;
  466. case _util.OPS.setMiterLimit:
  467. this.setMiterLimit(args[0]);
  468. break;
  469. case _util.OPS.setFillRGBColor:
  470. this.setFillRGBColor(args[0], args[1], args[2]);
  471. break;
  472. case _util.OPS.setStrokeRGBColor:
  473. this.setStrokeRGBColor(args[0], args[1], args[2]);
  474. break;
  475. case _util.OPS.setDash:
  476. this.setDash(args[0], args[1]);
  477. break;
  478. case _util.OPS.setGState:
  479. this.setGState(args[0]);
  480. break;
  481. case _util.OPS.fill:
  482. this.fill();
  483. break;
  484. case _util.OPS.eoFill:
  485. this.eoFill();
  486. break;
  487. case _util.OPS.stroke:
  488. this.stroke();
  489. break;
  490. case _util.OPS.fillStroke:
  491. this.fillStroke();
  492. break;
  493. case _util.OPS.eoFillStroke:
  494. this.eoFillStroke();
  495. break;
  496. case _util.OPS.clip:
  497. this.clip('nonzero');
  498. break;
  499. case _util.OPS.eoClip:
  500. this.clip('evenodd');
  501. break;
  502. case _util.OPS.paintSolidColorImageMask:
  503. this.paintSolidColorImageMask();
  504. break;
  505. case _util.OPS.paintJpegXObject:
  506. this.paintJpegXObject(args[0], args[1], args[2]);
  507. break;
  508. case _util.OPS.paintImageXObject:
  509. this.paintImageXObject(args[0]);
  510. break;
  511. case _util.OPS.paintInlineImageXObject:
  512. this.paintInlineImageXObject(args[0]);
  513. break;
  514. case _util.OPS.paintImageMaskXObject:
  515. this.paintImageMaskXObject(args[0]);
  516. break;
  517. case _util.OPS.paintFormXObjectBegin:
  518. this.paintFormXObjectBegin(args[0], args[1]);
  519. break;
  520. case _util.OPS.paintFormXObjectEnd:
  521. this.paintFormXObjectEnd();
  522. break;
  523. case _util.OPS.closePath:
  524. this.closePath();
  525. break;
  526. case _util.OPS.closeStroke:
  527. this.closeStroke();
  528. break;
  529. case _util.OPS.closeFillStroke:
  530. this.closeFillStroke();
  531. break;
  532. case _util.OPS.closeEOFillStroke:
  533. this.closeEOFillStroke();
  534. break;
  535. case _util.OPS.nextLine:
  536. this.nextLine();
  537. break;
  538. case _util.OPS.transform:
  539. this.transform(args[0], args[1], args[2], args[3], args[4], args[5]);
  540. break;
  541. case _util.OPS.constructPath:
  542. this.constructPath(args[0], args[1]);
  543. break;
  544. case _util.OPS.endPath:
  545. this.endPath();
  546. break;
  547. case 92:
  548. this.group(opTree[x].items);
  549. break;
  550. default:
  551. (0, _util.warn)('Unimplemented operator ' + fn);
  552. break;
  553. }
  554. }
  555. },
  556. setWordSpacing: function SVGGraphics_setWordSpacing(wordSpacing) {
  557. this.current.wordSpacing = wordSpacing;
  558. },
  559. setCharSpacing: function SVGGraphics_setCharSpacing(charSpacing) {
  560. this.current.charSpacing = charSpacing;
  561. },
  562. nextLine: function SVGGraphics_nextLine() {
  563. this.moveText(0, this.current.leading);
  564. },
  565. setTextMatrix: function SVGGraphics_setTextMatrix(a, b, c, d, e, f) {
  566. var current = this.current;
  567. this.current.textMatrix = this.current.lineMatrix = [a, b, c, d, e, f];
  568. this.current.x = this.current.lineX = 0;
  569. this.current.y = this.current.lineY = 0;
  570. current.xcoords = [];
  571. current.tspan = this.svgFactory.createElement('svg:tspan');
  572. current.tspan.setAttributeNS(null, 'font-family', current.fontFamily);
  573. current.tspan.setAttributeNS(null, 'font-size', pf(current.fontSize) + 'px');
  574. current.tspan.setAttributeNS(null, 'y', pf(-current.y));
  575. current.txtElement = this.svgFactory.createElement('svg:text');
  576. current.txtElement.appendChild(current.tspan);
  577. },
  578. beginText: function SVGGraphics_beginText() {
  579. this.current.x = this.current.lineX = 0;
  580. this.current.y = this.current.lineY = 0;
  581. this.current.textMatrix = _util.IDENTITY_MATRIX;
  582. this.current.lineMatrix = _util.IDENTITY_MATRIX;
  583. this.current.tspan = this.svgFactory.createElement('svg:tspan');
  584. this.current.txtElement = this.svgFactory.createElement('svg:text');
  585. this.current.txtgrp = this.svgFactory.createElement('svg:g');
  586. this.current.xcoords = [];
  587. },
  588. moveText: function SVGGraphics_moveText(x, y) {
  589. var current = this.current;
  590. this.current.x = this.current.lineX += x;
  591. this.current.y = this.current.lineY += y;
  592. current.xcoords = [];
  593. current.tspan = this.svgFactory.createElement('svg:tspan');
  594. current.tspan.setAttributeNS(null, 'font-family', current.fontFamily);
  595. current.tspan.setAttributeNS(null, 'font-size', pf(current.fontSize) + 'px');
  596. current.tspan.setAttributeNS(null, 'y', pf(-current.y));
  597. },
  598. showText: function SVGGraphics_showText(glyphs) {
  599. var current = this.current;
  600. var font = current.font;
  601. var fontSize = current.fontSize;
  602. if (fontSize === 0) {
  603. return;
  604. }
  605. var charSpacing = current.charSpacing;
  606. var wordSpacing = current.wordSpacing;
  607. var fontDirection = current.fontDirection;
  608. var textHScale = current.textHScale * fontDirection;
  609. var glyphsLength = glyphs.length;
  610. var vertical = font.vertical;
  611. var widthAdvanceScale = fontSize * current.fontMatrix[0];
  612. var x = 0,
  613. i;
  614. for (i = 0; i < glyphsLength; ++i) {
  615. var glyph = glyphs[i];
  616. if (glyph === null) {
  617. x += fontDirection * wordSpacing;
  618. continue;
  619. } else if ((0, _util.isNum)(glyph)) {
  620. x += -glyph * fontSize * 0.001;
  621. continue;
  622. }
  623. var width = glyph.width;
  624. var character = glyph.fontChar;
  625. var spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
  626. var charWidth = width * widthAdvanceScale + spacing * fontDirection;
  627. if (!glyph.isInFont && !font.missingFile) {
  628. x += charWidth;
  629. continue;
  630. }
  631. current.xcoords.push(current.x + x * textHScale);
  632. current.tspan.textContent += character;
  633. x += charWidth;
  634. }
  635. if (vertical) {
  636. current.y -= x * textHScale;
  637. } else {
  638. current.x += x * textHScale;
  639. }
  640. current.tspan.setAttributeNS(null, 'x', current.xcoords.map(pf).join(' '));
  641. current.tspan.setAttributeNS(null, 'y', pf(-current.y));
  642. current.tspan.setAttributeNS(null, 'font-family', current.fontFamily);
  643. current.tspan.setAttributeNS(null, 'font-size', pf(current.fontSize) + 'px');
  644. if (current.fontStyle !== SVG_DEFAULTS.fontStyle) {
  645. current.tspan.setAttributeNS(null, 'font-style', current.fontStyle);
  646. }
  647. if (current.fontWeight !== SVG_DEFAULTS.fontWeight) {
  648. current.tspan.setAttributeNS(null, 'font-weight', current.fontWeight);
  649. }
  650. if (current.fillColor !== SVG_DEFAULTS.fillColor) {
  651. current.tspan.setAttributeNS(null, 'fill', current.fillColor);
  652. }
  653. var textMatrix = current.textMatrix;
  654. if (current.textRise !== 0) {
  655. textMatrix = textMatrix.slice();
  656. textMatrix[5] += current.textRise;
  657. }
  658. current.txtElement.setAttributeNS(null, 'transform', pm(textMatrix) + ' scale(1, -1)');
  659. current.txtElement.setAttributeNS(XML_NS, 'xml:space', 'preserve');
  660. current.txtElement.appendChild(current.tspan);
  661. current.txtgrp.appendChild(current.txtElement);
  662. this._ensureTransformGroup().appendChild(current.txtElement);
  663. },
  664. setLeadingMoveText: function SVGGraphics_setLeadingMoveText(x, y) {
  665. this.setLeading(-y);
  666. this.moveText(x, y);
  667. },
  668. addFontStyle: function SVGGraphics_addFontStyle(fontObj) {
  669. if (!this.cssStyle) {
  670. this.cssStyle = this.svgFactory.createElement('svg:style');
  671. this.cssStyle.setAttributeNS(null, 'type', 'text/css');
  672. this.defs.appendChild(this.cssStyle);
  673. }
  674. var url = (0, _util.createObjectURL)(fontObj.data, fontObj.mimetype, this.forceDataSchema);
  675. this.cssStyle.textContent += '@font-face { font-family: "' + fontObj.loadedName + '";' + ' src: url(' + url + '); }\n';
  676. },
  677. setFont: function SVGGraphics_setFont(details) {
  678. var current = this.current;
  679. var fontObj = this.commonObjs.get(details[0]);
  680. var size = details[1];
  681. this.current.font = fontObj;
  682. if (this.embedFonts && fontObj.data && !this.embeddedFonts[fontObj.loadedName]) {
  683. this.addFontStyle(fontObj);
  684. this.embeddedFonts[fontObj.loadedName] = fontObj;
  685. }
  686. current.fontMatrix = fontObj.fontMatrix ? fontObj.fontMatrix : _util.FONT_IDENTITY_MATRIX;
  687. var bold = fontObj.black ? fontObj.bold ? 'bolder' : 'bold' : fontObj.bold ? 'bold' : 'normal';
  688. var italic = fontObj.italic ? 'italic' : 'normal';
  689. if (size < 0) {
  690. size = -size;
  691. current.fontDirection = -1;
  692. } else {
  693. current.fontDirection = 1;
  694. }
  695. current.fontSize = size;
  696. current.fontFamily = fontObj.loadedName;
  697. current.fontWeight = bold;
  698. current.fontStyle = italic;
  699. current.tspan = this.svgFactory.createElement('svg:tspan');
  700. current.tspan.setAttributeNS(null, 'y', pf(-current.y));
  701. current.xcoords = [];
  702. },
  703. endText: function SVGGraphics_endText() {},
  704. setLineWidth: function SVGGraphics_setLineWidth(width) {
  705. this.current.lineWidth = width;
  706. },
  707. setLineCap: function SVGGraphics_setLineCap(style) {
  708. this.current.lineCap = LINE_CAP_STYLES[style];
  709. },
  710. setLineJoin: function SVGGraphics_setLineJoin(style) {
  711. this.current.lineJoin = LINE_JOIN_STYLES[style];
  712. },
  713. setMiterLimit: function SVGGraphics_setMiterLimit(limit) {
  714. this.current.miterLimit = limit;
  715. },
  716. setStrokeAlpha: function SVGGraphics_setStrokeAlpha(strokeAlpha) {
  717. this.current.strokeAlpha = strokeAlpha;
  718. },
  719. setStrokeRGBColor: function SVGGraphics_setStrokeRGBColor(r, g, b) {
  720. var color = _util.Util.makeCssRgb(r, g, b);
  721. this.current.strokeColor = color;
  722. },
  723. setFillAlpha: function SVGGraphics_setFillAlpha(fillAlpha) {
  724. this.current.fillAlpha = fillAlpha;
  725. },
  726. setFillRGBColor: function SVGGraphics_setFillRGBColor(r, g, b) {
  727. var color = _util.Util.makeCssRgb(r, g, b);
  728. this.current.fillColor = color;
  729. this.current.tspan = this.svgFactory.createElement('svg:tspan');
  730. this.current.xcoords = [];
  731. },
  732. setDash: function SVGGraphics_setDash(dashArray, dashPhase) {
  733. this.current.dashArray = dashArray;
  734. this.current.dashPhase = dashPhase;
  735. },
  736. constructPath: function SVGGraphics_constructPath(ops, args) {
  737. var current = this.current;
  738. var x = current.x,
  739. y = current.y;
  740. current.path = this.svgFactory.createElement('svg:path');
  741. var d = [];
  742. var opLength = ops.length;
  743. for (var i = 0, j = 0; i < opLength; i++) {
  744. switch (ops[i] | 0) {
  745. case _util.OPS.rectangle:
  746. x = args[j++];
  747. y = args[j++];
  748. var width = args[j++];
  749. var height = args[j++];
  750. var xw = x + width;
  751. var yh = y + height;
  752. d.push('M', pf(x), pf(y), 'L', pf(xw), pf(y), 'L', pf(xw), pf(yh), 'L', pf(x), pf(yh), 'Z');
  753. break;
  754. case _util.OPS.moveTo:
  755. x = args[j++];
  756. y = args[j++];
  757. d.push('M', pf(x), pf(y));
  758. break;
  759. case _util.OPS.lineTo:
  760. x = args[j++];
  761. y = args[j++];
  762. d.push('L', pf(x), pf(y));
  763. break;
  764. case _util.OPS.curveTo:
  765. x = args[j + 4];
  766. y = args[j + 5];
  767. d.push('C', pf(args[j]), pf(args[j + 1]), pf(args[j + 2]), pf(args[j + 3]), pf(x), pf(y));
  768. j += 6;
  769. break;
  770. case _util.OPS.curveTo2:
  771. x = args[j + 2];
  772. y = args[j + 3];
  773. d.push('C', pf(x), pf(y), pf(args[j]), pf(args[j + 1]), pf(args[j + 2]), pf(args[j + 3]));
  774. j += 4;
  775. break;
  776. case _util.OPS.curveTo3:
  777. x = args[j + 2];
  778. y = args[j + 3];
  779. d.push('C', pf(args[j]), pf(args[j + 1]), pf(x), pf(y), pf(x), pf(y));
  780. j += 4;
  781. break;
  782. case _util.OPS.closePath:
  783. d.push('Z');
  784. break;
  785. }
  786. }
  787. current.path.setAttributeNS(null, 'd', d.join(' '));
  788. current.path.setAttributeNS(null, 'fill', 'none');
  789. this._ensureTransformGroup().appendChild(current.path);
  790. current.element = current.path;
  791. current.setCurrentPoint(x, y);
  792. },
  793. endPath: function SVGGraphics_endPath() {
  794. if (!this.pendingClip) {
  795. return;
  796. }
  797. var current = this.current;
  798. var clipId = 'clippath' + clipCount;
  799. clipCount++;
  800. var clipPath = this.svgFactory.createElement('svg:clipPath');
  801. clipPath.setAttributeNS(null, 'id', clipId);
  802. clipPath.setAttributeNS(null, 'transform', pm(this.transformMatrix));
  803. var clipElement = current.element.cloneNode();
  804. if (this.pendingClip === 'evenodd') {
  805. clipElement.setAttributeNS(null, 'clip-rule', 'evenodd');
  806. } else {
  807. clipElement.setAttributeNS(null, 'clip-rule', 'nonzero');
  808. }
  809. this.pendingClip = null;
  810. clipPath.appendChild(clipElement);
  811. this.defs.appendChild(clipPath);
  812. if (current.activeClipUrl) {
  813. current.clipGroup = null;
  814. this.extraStack.forEach(function (prev) {
  815. prev.clipGroup = null;
  816. });
  817. }
  818. current.activeClipUrl = 'url(#' + clipId + ')';
  819. this.tgrp = null;
  820. },
  821. clip: function SVGGraphics_clip(type) {
  822. this.pendingClip = type;
  823. },
  824. closePath: function SVGGraphics_closePath() {
  825. var current = this.current;
  826. if (current.path) {
  827. var d = current.path.getAttributeNS(null, 'd');
  828. d += 'Z';
  829. current.path.setAttributeNS(null, 'd', d);
  830. }
  831. },
  832. setLeading: function SVGGraphics_setLeading(leading) {
  833. this.current.leading = -leading;
  834. },
  835. setTextRise: function SVGGraphics_setTextRise(textRise) {
  836. this.current.textRise = textRise;
  837. },
  838. setHScale: function SVGGraphics_setHScale(scale) {
  839. this.current.textHScale = scale / 100;
  840. },
  841. setGState: function SVGGraphics_setGState(states) {
  842. for (var i = 0, ii = states.length; i < ii; i++) {
  843. var state = states[i];
  844. var key = state[0];
  845. var value = state[1];
  846. switch (key) {
  847. case 'LW':
  848. this.setLineWidth(value);
  849. break;
  850. case 'LC':
  851. this.setLineCap(value);
  852. break;
  853. case 'LJ':
  854. this.setLineJoin(value);
  855. break;
  856. case 'ML':
  857. this.setMiterLimit(value);
  858. break;
  859. case 'D':
  860. this.setDash(value[0], value[1]);
  861. break;
  862. case 'Font':
  863. this.setFont(value);
  864. break;
  865. case 'CA':
  866. this.setStrokeAlpha(value);
  867. break;
  868. case 'ca':
  869. this.setFillAlpha(value);
  870. break;
  871. default:
  872. (0, _util.warn)('Unimplemented graphic state ' + key);
  873. break;
  874. }
  875. }
  876. },
  877. fill: function SVGGraphics_fill() {
  878. var current = this.current;
  879. if (current.element) {
  880. current.element.setAttributeNS(null, 'fill', current.fillColor);
  881. current.element.setAttributeNS(null, 'fill-opacity', current.fillAlpha);
  882. }
  883. },
  884. stroke: function SVGGraphics_stroke() {
  885. var current = this.current;
  886. if (current.element) {
  887. current.element.setAttributeNS(null, 'stroke', current.strokeColor);
  888. current.element.setAttributeNS(null, 'stroke-opacity', current.strokeAlpha);
  889. current.element.setAttributeNS(null, 'stroke-miterlimit', pf(current.miterLimit));
  890. current.element.setAttributeNS(null, 'stroke-linecap', current.lineCap);
  891. current.element.setAttributeNS(null, 'stroke-linejoin', current.lineJoin);
  892. current.element.setAttributeNS(null, 'stroke-width', pf(current.lineWidth) + 'px');
  893. current.element.setAttributeNS(null, 'stroke-dasharray', current.dashArray.map(pf).join(' '));
  894. current.element.setAttributeNS(null, 'stroke-dashoffset', pf(current.dashPhase) + 'px');
  895. current.element.setAttributeNS(null, 'fill', 'none');
  896. }
  897. },
  898. eoFill: function SVGGraphics_eoFill() {
  899. if (this.current.element) {
  900. this.current.element.setAttributeNS(null, 'fill-rule', 'evenodd');
  901. }
  902. this.fill();
  903. },
  904. fillStroke: function SVGGraphics_fillStroke() {
  905. this.stroke();
  906. this.fill();
  907. },
  908. eoFillStroke: function SVGGraphics_eoFillStroke() {
  909. if (this.current.element) {
  910. this.current.element.setAttributeNS(null, 'fill-rule', 'evenodd');
  911. }
  912. this.fillStroke();
  913. },
  914. closeStroke: function SVGGraphics_closeStroke() {
  915. this.closePath();
  916. this.stroke();
  917. },
  918. closeFillStroke: function SVGGraphics_closeFillStroke() {
  919. this.closePath();
  920. this.fillStroke();
  921. },
  922. closeEOFillStroke: function closeEOFillStroke() {
  923. this.closePath();
  924. this.eoFillStroke();
  925. },
  926. paintSolidColorImageMask: function SVGGraphics_paintSolidColorImageMask() {
  927. var current = this.current;
  928. var rect = this.svgFactory.createElement('svg:rect');
  929. rect.setAttributeNS(null, 'x', '0');
  930. rect.setAttributeNS(null, 'y', '0');
  931. rect.setAttributeNS(null, 'width', '1px');
  932. rect.setAttributeNS(null, 'height', '1px');
  933. rect.setAttributeNS(null, 'fill', current.fillColor);
  934. this._ensureTransformGroup().appendChild(rect);
  935. },
  936. paintJpegXObject: function SVGGraphics_paintJpegXObject(objId, w, h) {
  937. var imgObj = this.objs.get(objId);
  938. var imgEl = this.svgFactory.createElement('svg:image');
  939. imgEl.setAttributeNS(XLINK_NS, 'xlink:href', imgObj.src);
  940. imgEl.setAttributeNS(null, 'width', pf(w));
  941. imgEl.setAttributeNS(null, 'height', pf(h));
  942. imgEl.setAttributeNS(null, 'x', '0');
  943. imgEl.setAttributeNS(null, 'y', pf(-h));
  944. imgEl.setAttributeNS(null, 'transform', 'scale(' + pf(1 / w) + ' ' + pf(-1 / h) + ')');
  945. this._ensureTransformGroup().appendChild(imgEl);
  946. },
  947. paintImageXObject: function SVGGraphics_paintImageXObject(objId) {
  948. var imgData = this.objs.get(objId);
  949. if (!imgData) {
  950. (0, _util.warn)('Dependent image isn\'t ready yet');
  951. return;
  952. }
  953. this.paintInlineImageXObject(imgData);
  954. },
  955. paintInlineImageXObject: function SVGGraphics_paintInlineImageXObject(imgData, mask) {
  956. var width = imgData.width;
  957. var height = imgData.height;
  958. var imgSrc = convertImgDataToPng(imgData, this.forceDataSchema, !!mask);
  959. var cliprect = this.svgFactory.createElement('svg:rect');
  960. cliprect.setAttributeNS(null, 'x', '0');
  961. cliprect.setAttributeNS(null, 'y', '0');
  962. cliprect.setAttributeNS(null, 'width', pf(width));
  963. cliprect.setAttributeNS(null, 'height', pf(height));
  964. this.current.element = cliprect;
  965. this.clip('nonzero');
  966. var imgEl = this.svgFactory.createElement('svg:image');
  967. imgEl.setAttributeNS(XLINK_NS, 'xlink:href', imgSrc);
  968. imgEl.setAttributeNS(null, 'x', '0');
  969. imgEl.setAttributeNS(null, 'y', pf(-height));
  970. imgEl.setAttributeNS(null, 'width', pf(width) + 'px');
  971. imgEl.setAttributeNS(null, 'height', pf(height) + 'px');
  972. imgEl.setAttributeNS(null, 'transform', 'scale(' + pf(1 / width) + ' ' + pf(-1 / height) + ')');
  973. if (mask) {
  974. mask.appendChild(imgEl);
  975. } else {
  976. this._ensureTransformGroup().appendChild(imgEl);
  977. }
  978. },
  979. paintImageMaskXObject: function SVGGraphics_paintImageMaskXObject(imgData) {
  980. var current = this.current;
  981. var width = imgData.width;
  982. var height = imgData.height;
  983. var fillColor = current.fillColor;
  984. current.maskId = 'mask' + maskCount++;
  985. var mask = this.svgFactory.createElement('svg:mask');
  986. mask.setAttributeNS(null, 'id', current.maskId);
  987. var rect = this.svgFactory.createElement('svg:rect');
  988. rect.setAttributeNS(null, 'x', '0');
  989. rect.setAttributeNS(null, 'y', '0');
  990. rect.setAttributeNS(null, 'width', pf(width));
  991. rect.setAttributeNS(null, 'height', pf(height));
  992. rect.setAttributeNS(null, 'fill', fillColor);
  993. rect.setAttributeNS(null, 'mask', 'url(#' + current.maskId + ')');
  994. this.defs.appendChild(mask);
  995. this._ensureTransformGroup().appendChild(rect);
  996. this.paintInlineImageXObject(imgData, mask);
  997. },
  998. paintFormXObjectBegin: function SVGGraphics_paintFormXObjectBegin(matrix, bbox) {
  999. if (Array.isArray(matrix) && matrix.length === 6) {
  1000. this.transform(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
  1001. }
  1002. if (Array.isArray(bbox) && bbox.length === 4) {
  1003. var width = bbox[2] - bbox[0];
  1004. var height = bbox[3] - bbox[1];
  1005. var cliprect = this.svgFactory.createElement('svg:rect');
  1006. cliprect.setAttributeNS(null, 'x', bbox[0]);
  1007. cliprect.setAttributeNS(null, 'y', bbox[1]);
  1008. cliprect.setAttributeNS(null, 'width', pf(width));
  1009. cliprect.setAttributeNS(null, 'height', pf(height));
  1010. this.current.element = cliprect;
  1011. this.clip('nonzero');
  1012. this.endPath();
  1013. }
  1014. },
  1015. paintFormXObjectEnd: function SVGGraphics_paintFormXObjectEnd() {},
  1016. _initialize: function _initialize(viewport) {
  1017. var svg = this.svgFactory.create(viewport.width, viewport.height);
  1018. var definitions = this.svgFactory.createElement('svg:defs');
  1019. svg.appendChild(definitions);
  1020. this.defs = definitions;
  1021. var rootGroup = this.svgFactory.createElement('svg:g');
  1022. rootGroup.setAttributeNS(null, 'transform', pm(viewport.transform));
  1023. svg.appendChild(rootGroup);
  1024. this.svg = rootGroup;
  1025. return svg;
  1026. },
  1027. _ensureClipGroup: function SVGGraphics_ensureClipGroup() {
  1028. if (!this.current.clipGroup) {
  1029. var clipGroup = this.svgFactory.createElement('svg:g');
  1030. clipGroup.setAttributeNS(null, 'clip-path', this.current.activeClipUrl);
  1031. this.svg.appendChild(clipGroup);
  1032. this.current.clipGroup = clipGroup;
  1033. }
  1034. return this.current.clipGroup;
  1035. },
  1036. _ensureTransformGroup: function SVGGraphics_ensureTransformGroup() {
  1037. if (!this.tgrp) {
  1038. this.tgrp = this.svgFactory.createElement('svg:g');
  1039. this.tgrp.setAttributeNS(null, 'transform', pm(this.transformMatrix));
  1040. if (this.current.activeClipUrl) {
  1041. this._ensureClipGroup().appendChild(this.tgrp);
  1042. } else {
  1043. this.svg.appendChild(this.tgrp);
  1044. }
  1045. }
  1046. return this.tgrp;
  1047. }
  1048. };
  1049. return SVGGraphics;
  1050. }();
  1051. }
  1052. exports.SVGGraphics = SVGGraphics;