svg.js 31 KB

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