2
0

svg.js 39 KB

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