svg.js 38 KB

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