svg.js 39 KB

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