2
0

svg.js 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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.closeEOFillStroke:
  528. this.closeEOFillStroke();
  529. break;
  530. case _util.OPS.nextLine:
  531. this.nextLine();
  532. break;
  533. case _util.OPS.transform:
  534. this.transform(args[0], args[1], args[2], args[3], args[4], args[5]);
  535. break;
  536. case _util.OPS.constructPath:
  537. this.constructPath(args[0], args[1]);
  538. break;
  539. case _util.OPS.endPath:
  540. this.endPath();
  541. break;
  542. case 92:
  543. this.group(opTree[x].items);
  544. break;
  545. default:
  546. (0, _util.warn)('Unimplemented operator ' + fn);
  547. break;
  548. }
  549. }
  550. },
  551. setWordSpacing: function SVGGraphics_setWordSpacing(wordSpacing) {
  552. this.current.wordSpacing = wordSpacing;
  553. },
  554. setCharSpacing: function SVGGraphics_setCharSpacing(charSpacing) {
  555. this.current.charSpacing = charSpacing;
  556. },
  557. nextLine: function SVGGraphics_nextLine() {
  558. this.moveText(0, this.current.leading);
  559. },
  560. setTextMatrix: function SVGGraphics_setTextMatrix(a, b, c, d, e, f) {
  561. var current = this.current;
  562. this.current.textMatrix = this.current.lineMatrix = [a, b, c, d, e, f];
  563. this.current.x = this.current.lineX = 0;
  564. this.current.y = this.current.lineY = 0;
  565. current.xcoords = [];
  566. current.tspan = this.svgFactory.createElement('svg:tspan');
  567. current.tspan.setAttributeNS(null, 'font-family', current.fontFamily);
  568. current.tspan.setAttributeNS(null, 'font-size', pf(current.fontSize) + 'px');
  569. current.tspan.setAttributeNS(null, 'y', pf(-current.y));
  570. current.txtElement = this.svgFactory.createElement('svg:text');
  571. current.txtElement.appendChild(current.tspan);
  572. },
  573. beginText: function SVGGraphics_beginText() {
  574. this.current.x = this.current.lineX = 0;
  575. this.current.y = this.current.lineY = 0;
  576. this.current.textMatrix = _util.IDENTITY_MATRIX;
  577. this.current.lineMatrix = _util.IDENTITY_MATRIX;
  578. this.current.tspan = this.svgFactory.createElement('svg:tspan');
  579. this.current.txtElement = this.svgFactory.createElement('svg:text');
  580. this.current.txtgrp = this.svgFactory.createElement('svg:g');
  581. this.current.xcoords = [];
  582. },
  583. moveText: function SVGGraphics_moveText(x, y) {
  584. var current = this.current;
  585. this.current.x = this.current.lineX += x;
  586. this.current.y = this.current.lineY += y;
  587. current.xcoords = [];
  588. current.tspan = this.svgFactory.createElement('svg:tspan');
  589. current.tspan.setAttributeNS(null, 'font-family', current.fontFamily);
  590. current.tspan.setAttributeNS(null, 'font-size', pf(current.fontSize) + 'px');
  591. current.tspan.setAttributeNS(null, 'y', pf(-current.y));
  592. },
  593. showText: function SVGGraphics_showText(glyphs) {
  594. var current = this.current;
  595. var font = current.font;
  596. var fontSize = current.fontSize;
  597. if (fontSize === 0) {
  598. return;
  599. }
  600. var charSpacing = current.charSpacing;
  601. var wordSpacing = current.wordSpacing;
  602. var fontDirection = current.fontDirection;
  603. var textHScale = current.textHScale * fontDirection;
  604. var glyphsLength = glyphs.length;
  605. var vertical = font.vertical;
  606. var widthAdvanceScale = fontSize * current.fontMatrix[0];
  607. var x = 0,
  608. i;
  609. for (i = 0; i < glyphsLength; ++i) {
  610. var glyph = glyphs[i];
  611. if (glyph === null) {
  612. x += fontDirection * wordSpacing;
  613. continue;
  614. } else if ((0, _util.isNum)(glyph)) {
  615. x += -glyph * fontSize * 0.001;
  616. continue;
  617. }
  618. var width = glyph.width;
  619. var character = glyph.fontChar;
  620. var spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
  621. var charWidth = width * widthAdvanceScale + spacing * fontDirection;
  622. if (!glyph.isInFont && !font.missingFile) {
  623. x += charWidth;
  624. continue;
  625. }
  626. current.xcoords.push(current.x + x * textHScale);
  627. current.tspan.textContent += character;
  628. x += charWidth;
  629. }
  630. if (vertical) {
  631. current.y -= x * textHScale;
  632. } else {
  633. current.x += x * textHScale;
  634. }
  635. current.tspan.setAttributeNS(null, 'x', current.xcoords.map(pf).join(' '));
  636. current.tspan.setAttributeNS(null, 'y', pf(-current.y));
  637. current.tspan.setAttributeNS(null, 'font-family', current.fontFamily);
  638. current.tspan.setAttributeNS(null, 'font-size', pf(current.fontSize) + 'px');
  639. if (current.fontStyle !== SVG_DEFAULTS.fontStyle) {
  640. current.tspan.setAttributeNS(null, 'font-style', current.fontStyle);
  641. }
  642. if (current.fontWeight !== SVG_DEFAULTS.fontWeight) {
  643. current.tspan.setAttributeNS(null, 'font-weight', current.fontWeight);
  644. }
  645. if (current.fillColor !== SVG_DEFAULTS.fillColor) {
  646. current.tspan.setAttributeNS(null, 'fill', current.fillColor);
  647. }
  648. var textMatrix = current.textMatrix;
  649. if (current.textRise !== 0) {
  650. textMatrix = textMatrix.slice();
  651. textMatrix[5] += current.textRise;
  652. }
  653. current.txtElement.setAttributeNS(null, 'transform', pm(textMatrix) + ' scale(1, -1)');
  654. current.txtElement.setAttributeNS(XML_NS, 'xml:space', 'preserve');
  655. current.txtElement.appendChild(current.tspan);
  656. current.txtgrp.appendChild(current.txtElement);
  657. this._ensureTransformGroup().appendChild(current.txtElement);
  658. },
  659. setLeadingMoveText: function SVGGraphics_setLeadingMoveText(x, y) {
  660. this.setLeading(-y);
  661. this.moveText(x, y);
  662. },
  663. addFontStyle: function SVGGraphics_addFontStyle(fontObj) {
  664. if (!this.cssStyle) {
  665. this.cssStyle = this.svgFactory.createElement('svg:style');
  666. this.cssStyle.setAttributeNS(null, 'type', 'text/css');
  667. this.defs.appendChild(this.cssStyle);
  668. }
  669. var url = (0, _util.createObjectURL)(fontObj.data, fontObj.mimetype, this.forceDataSchema);
  670. this.cssStyle.textContent += '@font-face { font-family: "' + fontObj.loadedName + '";' + ' src: url(' + url + '); }\n';
  671. },
  672. setFont: function SVGGraphics_setFont(details) {
  673. var current = this.current;
  674. var fontObj = this.commonObjs.get(details[0]);
  675. var size = details[1];
  676. this.current.font = fontObj;
  677. if (this.embedFonts && fontObj.data && !this.embeddedFonts[fontObj.loadedName]) {
  678. this.addFontStyle(fontObj);
  679. this.embeddedFonts[fontObj.loadedName] = fontObj;
  680. }
  681. current.fontMatrix = fontObj.fontMatrix ? fontObj.fontMatrix : _util.FONT_IDENTITY_MATRIX;
  682. var bold = fontObj.black ? fontObj.bold ? 'bolder' : 'bold' : fontObj.bold ? 'bold' : 'normal';
  683. var italic = fontObj.italic ? 'italic' : 'normal';
  684. if (size < 0) {
  685. size = -size;
  686. current.fontDirection = -1;
  687. } else {
  688. current.fontDirection = 1;
  689. }
  690. current.fontSize = size;
  691. current.fontFamily = fontObj.loadedName;
  692. current.fontWeight = bold;
  693. current.fontStyle = italic;
  694. current.tspan = this.svgFactory.createElement('svg:tspan');
  695. current.tspan.setAttributeNS(null, 'y', pf(-current.y));
  696. current.xcoords = [];
  697. },
  698. endText: function SVGGraphics_endText() {},
  699. setLineWidth: function SVGGraphics_setLineWidth(width) {
  700. this.current.lineWidth = width;
  701. },
  702. setLineCap: function SVGGraphics_setLineCap(style) {
  703. this.current.lineCap = LINE_CAP_STYLES[style];
  704. },
  705. setLineJoin: function SVGGraphics_setLineJoin(style) {
  706. this.current.lineJoin = LINE_JOIN_STYLES[style];
  707. },
  708. setMiterLimit: function SVGGraphics_setMiterLimit(limit) {
  709. this.current.miterLimit = limit;
  710. },
  711. setStrokeAlpha: function SVGGraphics_setStrokeAlpha(strokeAlpha) {
  712. this.current.strokeAlpha = strokeAlpha;
  713. },
  714. setStrokeRGBColor: function SVGGraphics_setStrokeRGBColor(r, g, b) {
  715. var color = _util.Util.makeCssRgb(r, g, b);
  716. this.current.strokeColor = color;
  717. },
  718. setFillAlpha: function SVGGraphics_setFillAlpha(fillAlpha) {
  719. this.current.fillAlpha = fillAlpha;
  720. },
  721. setFillRGBColor: function SVGGraphics_setFillRGBColor(r, g, b) {
  722. var color = _util.Util.makeCssRgb(r, g, b);
  723. this.current.fillColor = color;
  724. this.current.tspan = this.svgFactory.createElement('svg:tspan');
  725. this.current.xcoords = [];
  726. },
  727. setDash: function SVGGraphics_setDash(dashArray, dashPhase) {
  728. this.current.dashArray = dashArray;
  729. this.current.dashPhase = dashPhase;
  730. },
  731. constructPath: function SVGGraphics_constructPath(ops, args) {
  732. var current = this.current;
  733. var x = current.x,
  734. y = current.y;
  735. current.path = this.svgFactory.createElement('svg:path');
  736. var d = [];
  737. var opLength = ops.length;
  738. for (var i = 0, j = 0; i < opLength; i++) {
  739. switch (ops[i] | 0) {
  740. case _util.OPS.rectangle:
  741. x = args[j++];
  742. y = args[j++];
  743. var width = args[j++];
  744. var height = args[j++];
  745. var xw = x + width;
  746. var yh = y + height;
  747. d.push('M', pf(x), pf(y), 'L', pf(xw), pf(y), 'L', pf(xw), pf(yh), 'L', pf(x), pf(yh), 'Z');
  748. break;
  749. case _util.OPS.moveTo:
  750. x = args[j++];
  751. y = args[j++];
  752. d.push('M', pf(x), pf(y));
  753. break;
  754. case _util.OPS.lineTo:
  755. x = args[j++];
  756. y = args[j++];
  757. d.push('L', pf(x), pf(y));
  758. break;
  759. case _util.OPS.curveTo:
  760. x = args[j + 4];
  761. y = args[j + 5];
  762. d.push('C', pf(args[j]), pf(args[j + 1]), pf(args[j + 2]), pf(args[j + 3]), pf(x), pf(y));
  763. j += 6;
  764. break;
  765. case _util.OPS.curveTo2:
  766. x = args[j + 2];
  767. y = args[j + 3];
  768. d.push('C', pf(x), pf(y), pf(args[j]), pf(args[j + 1]), pf(args[j + 2]), pf(args[j + 3]));
  769. j += 4;
  770. break;
  771. case _util.OPS.curveTo3:
  772. x = args[j + 2];
  773. y = args[j + 3];
  774. d.push('C', pf(args[j]), pf(args[j + 1]), pf(x), pf(y), pf(x), pf(y));
  775. j += 4;
  776. break;
  777. case _util.OPS.closePath:
  778. d.push('Z');
  779. break;
  780. }
  781. }
  782. current.path.setAttributeNS(null, 'd', d.join(' '));
  783. current.path.setAttributeNS(null, 'fill', 'none');
  784. this._ensureTransformGroup().appendChild(current.path);
  785. current.element = current.path;
  786. current.setCurrentPoint(x, y);
  787. },
  788. endPath: function SVGGraphics_endPath() {
  789. if (!this.pendingClip) {
  790. return;
  791. }
  792. var current = this.current;
  793. var clipId = 'clippath' + clipCount;
  794. clipCount++;
  795. var clipPath = this.svgFactory.createElement('svg:clipPath');
  796. clipPath.setAttributeNS(null, 'id', clipId);
  797. clipPath.setAttributeNS(null, 'transform', pm(this.transformMatrix));
  798. var clipElement = current.element.cloneNode();
  799. if (this.pendingClip === 'evenodd') {
  800. clipElement.setAttributeNS(null, 'clip-rule', 'evenodd');
  801. } else {
  802. clipElement.setAttributeNS(null, 'clip-rule', 'nonzero');
  803. }
  804. this.pendingClip = null;
  805. clipPath.appendChild(clipElement);
  806. this.defs.appendChild(clipPath);
  807. if (current.activeClipUrl) {
  808. current.clipGroup = null;
  809. this.extraStack.forEach(function (prev) {
  810. prev.clipGroup = null;
  811. });
  812. }
  813. current.activeClipUrl = 'url(#' + clipId + ')';
  814. this.tgrp = null;
  815. },
  816. clip: function SVGGraphics_clip(type) {
  817. this.pendingClip = type;
  818. },
  819. closePath: function SVGGraphics_closePath() {
  820. var current = this.current;
  821. if (current.path) {
  822. var d = current.path.getAttributeNS(null, 'd');
  823. d += 'Z';
  824. current.path.setAttributeNS(null, 'd', d);
  825. }
  826. },
  827. setLeading: function SVGGraphics_setLeading(leading) {
  828. this.current.leading = -leading;
  829. },
  830. setTextRise: function SVGGraphics_setTextRise(textRise) {
  831. this.current.textRise = textRise;
  832. },
  833. setHScale: function SVGGraphics_setHScale(scale) {
  834. this.current.textHScale = scale / 100;
  835. },
  836. setGState: function SVGGraphics_setGState(states) {
  837. for (var i = 0, ii = states.length; i < ii; i++) {
  838. var state = states[i];
  839. var key = state[0];
  840. var value = state[1];
  841. switch (key) {
  842. case 'LW':
  843. this.setLineWidth(value);
  844. break;
  845. case 'LC':
  846. this.setLineCap(value);
  847. break;
  848. case 'LJ':
  849. this.setLineJoin(value);
  850. break;
  851. case 'ML':
  852. this.setMiterLimit(value);
  853. break;
  854. case 'D':
  855. this.setDash(value[0], value[1]);
  856. break;
  857. case 'Font':
  858. this.setFont(value);
  859. break;
  860. case 'CA':
  861. this.setStrokeAlpha(value);
  862. break;
  863. case 'ca':
  864. this.setFillAlpha(value);
  865. break;
  866. default:
  867. (0, _util.warn)('Unimplemented graphic state ' + key);
  868. break;
  869. }
  870. }
  871. },
  872. fill: function SVGGraphics_fill() {
  873. var current = this.current;
  874. if (current.element) {
  875. current.element.setAttributeNS(null, 'fill', current.fillColor);
  876. current.element.setAttributeNS(null, 'fill-opacity', current.fillAlpha);
  877. }
  878. },
  879. stroke: function SVGGraphics_stroke() {
  880. var current = this.current;
  881. if (current.element) {
  882. current.element.setAttributeNS(null, 'stroke', current.strokeColor);
  883. current.element.setAttributeNS(null, 'stroke-opacity', current.strokeAlpha);
  884. current.element.setAttributeNS(null, 'stroke-miterlimit', pf(current.miterLimit));
  885. current.element.setAttributeNS(null, 'stroke-linecap', current.lineCap);
  886. current.element.setAttributeNS(null, 'stroke-linejoin', current.lineJoin);
  887. current.element.setAttributeNS(null, 'stroke-width', pf(current.lineWidth) + 'px');
  888. current.element.setAttributeNS(null, 'stroke-dasharray', current.dashArray.map(pf).join(' '));
  889. current.element.setAttributeNS(null, 'stroke-dashoffset', pf(current.dashPhase) + 'px');
  890. current.element.setAttributeNS(null, 'fill', 'none');
  891. }
  892. },
  893. eoFill: function SVGGraphics_eoFill() {
  894. if (this.current.element) {
  895. this.current.element.setAttributeNS(null, 'fill-rule', 'evenodd');
  896. }
  897. this.fill();
  898. },
  899. fillStroke: function SVGGraphics_fillStroke() {
  900. this.stroke();
  901. this.fill();
  902. },
  903. eoFillStroke: function SVGGraphics_eoFillStroke() {
  904. if (this.current.element) {
  905. this.current.element.setAttributeNS(null, 'fill-rule', 'evenodd');
  906. }
  907. this.fillStroke();
  908. },
  909. closeStroke: function SVGGraphics_closeStroke() {
  910. this.closePath();
  911. this.stroke();
  912. },
  913. closeFillStroke: function SVGGraphics_closeFillStroke() {
  914. this.closePath();
  915. this.fillStroke();
  916. },
  917. closeEOFillStroke: function closeEOFillStroke() {
  918. this.closePath();
  919. this.eoFillStroke();
  920. },
  921. paintSolidColorImageMask: function SVGGraphics_paintSolidColorImageMask() {
  922. var current = this.current;
  923. var rect = this.svgFactory.createElement('svg:rect');
  924. rect.setAttributeNS(null, 'x', '0');
  925. rect.setAttributeNS(null, 'y', '0');
  926. rect.setAttributeNS(null, 'width', '1px');
  927. rect.setAttributeNS(null, 'height', '1px');
  928. rect.setAttributeNS(null, 'fill', current.fillColor);
  929. this._ensureTransformGroup().appendChild(rect);
  930. },
  931. paintJpegXObject: function SVGGraphics_paintJpegXObject(objId, w, h) {
  932. var imgObj = this.objs.get(objId);
  933. var imgEl = this.svgFactory.createElement('svg:image');
  934. imgEl.setAttributeNS(XLINK_NS, 'xlink:href', imgObj.src);
  935. imgEl.setAttributeNS(null, 'width', pf(w));
  936. imgEl.setAttributeNS(null, 'height', pf(h));
  937. imgEl.setAttributeNS(null, 'x', '0');
  938. imgEl.setAttributeNS(null, 'y', pf(-h));
  939. imgEl.setAttributeNS(null, 'transform', 'scale(' + pf(1 / w) + ' ' + pf(-1 / h) + ')');
  940. this._ensureTransformGroup().appendChild(imgEl);
  941. },
  942. paintImageXObject: function SVGGraphics_paintImageXObject(objId) {
  943. var imgData = this.objs.get(objId);
  944. if (!imgData) {
  945. (0, _util.warn)('Dependent image isn\'t ready yet');
  946. return;
  947. }
  948. this.paintInlineImageXObject(imgData);
  949. },
  950. paintInlineImageXObject: function SVGGraphics_paintInlineImageXObject(imgData, mask) {
  951. var width = imgData.width;
  952. var height = imgData.height;
  953. var imgSrc = convertImgDataToPng(imgData, this.forceDataSchema, !!mask);
  954. var cliprect = this.svgFactory.createElement('svg:rect');
  955. cliprect.setAttributeNS(null, 'x', '0');
  956. cliprect.setAttributeNS(null, 'y', '0');
  957. cliprect.setAttributeNS(null, 'width', pf(width));
  958. cliprect.setAttributeNS(null, 'height', pf(height));
  959. this.current.element = cliprect;
  960. this.clip('nonzero');
  961. var imgEl = this.svgFactory.createElement('svg:image');
  962. imgEl.setAttributeNS(XLINK_NS, 'xlink:href', imgSrc);
  963. imgEl.setAttributeNS(null, 'x', '0');
  964. imgEl.setAttributeNS(null, 'y', pf(-height));
  965. imgEl.setAttributeNS(null, 'width', pf(width) + 'px');
  966. imgEl.setAttributeNS(null, 'height', pf(height) + 'px');
  967. imgEl.setAttributeNS(null, 'transform', 'scale(' + pf(1 / width) + ' ' + pf(-1 / height) + ')');
  968. if (mask) {
  969. mask.appendChild(imgEl);
  970. } else {
  971. this._ensureTransformGroup().appendChild(imgEl);
  972. }
  973. },
  974. paintImageMaskXObject: function SVGGraphics_paintImageMaskXObject(imgData) {
  975. var current = this.current;
  976. var width = imgData.width;
  977. var height = imgData.height;
  978. var fillColor = current.fillColor;
  979. current.maskId = 'mask' + maskCount++;
  980. var mask = this.svgFactory.createElement('svg:mask');
  981. mask.setAttributeNS(null, 'id', current.maskId);
  982. var rect = this.svgFactory.createElement('svg:rect');
  983. rect.setAttributeNS(null, 'x', '0');
  984. rect.setAttributeNS(null, 'y', '0');
  985. rect.setAttributeNS(null, 'width', pf(width));
  986. rect.setAttributeNS(null, 'height', pf(height));
  987. rect.setAttributeNS(null, 'fill', fillColor);
  988. rect.setAttributeNS(null, 'mask', 'url(#' + current.maskId + ')');
  989. this.defs.appendChild(mask);
  990. this._ensureTransformGroup().appendChild(rect);
  991. this.paintInlineImageXObject(imgData, mask);
  992. },
  993. paintFormXObjectBegin: function SVGGraphics_paintFormXObjectBegin(matrix, bbox) {
  994. if (Array.isArray(matrix) && matrix.length === 6) {
  995. this.transform(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
  996. }
  997. if (Array.isArray(bbox) && bbox.length === 4) {
  998. var width = bbox[2] - bbox[0];
  999. var height = bbox[3] - bbox[1];
  1000. var cliprect = this.svgFactory.createElement('svg:rect');
  1001. cliprect.setAttributeNS(null, 'x', bbox[0]);
  1002. cliprect.setAttributeNS(null, 'y', bbox[1]);
  1003. cliprect.setAttributeNS(null, 'width', pf(width));
  1004. cliprect.setAttributeNS(null, 'height', pf(height));
  1005. this.current.element = cliprect;
  1006. this.clip('nonzero');
  1007. this.endPath();
  1008. }
  1009. },
  1010. paintFormXObjectEnd: function SVGGraphics_paintFormXObjectEnd() {},
  1011. _initialize: function _initialize(viewport) {
  1012. var svg = this.svgFactory.create(viewport.width, viewport.height);
  1013. var definitions = this.svgFactory.createElement('svg:defs');
  1014. svg.appendChild(definitions);
  1015. this.defs = definitions;
  1016. var rootGroup = this.svgFactory.createElement('svg:g');
  1017. rootGroup.setAttributeNS(null, 'transform', pm(viewport.transform));
  1018. svg.appendChild(rootGroup);
  1019. this.svg = rootGroup;
  1020. return svg;
  1021. },
  1022. _ensureClipGroup: function SVGGraphics_ensureClipGroup() {
  1023. if (!this.current.clipGroup) {
  1024. var clipGroup = this.svgFactory.createElement('svg:g');
  1025. clipGroup.setAttributeNS(null, 'clip-path', this.current.activeClipUrl);
  1026. this.svg.appendChild(clipGroup);
  1027. this.current.clipGroup = clipGroup;
  1028. }
  1029. return this.current.clipGroup;
  1030. },
  1031. _ensureTransformGroup: function SVGGraphics_ensureTransformGroup() {
  1032. if (!this.tgrp) {
  1033. this.tgrp = this.svgFactory.createElement('svg:g');
  1034. this.tgrp.setAttributeNS(null, 'transform', pm(this.transformMatrix));
  1035. if (this.current.activeClipUrl) {
  1036. this._ensureClipGroup().appendChild(this.tgrp);
  1037. } else {
  1038. this.svg.appendChild(this.tgrp);
  1039. }
  1040. }
  1041. return this.tgrp;
  1042. }
  1043. };
  1044. return SVGGraphics;
  1045. }();
  1046. }
  1047. exports.SVGGraphics = SVGGraphics;