2
0

svg.js 37 KB

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