svg.js 38 KB

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