svg.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. current.xcoords.push(current.x + x * textHScale);
  605. var width = glyph.width;
  606. var character = glyph.fontChar;
  607. var spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
  608. var charWidth = width * widthAdvanceScale + spacing * fontDirection;
  609. x += charWidth;
  610. current.tspan.textContent += character;
  611. }
  612. if (vertical) {
  613. current.y -= x * textHScale;
  614. } else {
  615. current.x += x * textHScale;
  616. }
  617. current.tspan.setAttributeNS(null, 'x', current.xcoords.map(pf).join(' '));
  618. current.tspan.setAttributeNS(null, 'y', pf(-current.y));
  619. current.tspan.setAttributeNS(null, 'font-family', current.fontFamily);
  620. current.tspan.setAttributeNS(null, 'font-size', pf(current.fontSize) + 'px');
  621. if (current.fontStyle !== SVG_DEFAULTS.fontStyle) {
  622. current.tspan.setAttributeNS(null, 'font-style', current.fontStyle);
  623. }
  624. if (current.fontWeight !== SVG_DEFAULTS.fontWeight) {
  625. current.tspan.setAttributeNS(null, 'font-weight', current.fontWeight);
  626. }
  627. if (current.fillColor !== SVG_DEFAULTS.fillColor) {
  628. current.tspan.setAttributeNS(null, 'fill', current.fillColor);
  629. }
  630. current.txtElement.setAttributeNS(null, 'transform', pm(current.textMatrix) + ' scale(1, -1)');
  631. current.txtElement.setAttributeNS(XML_NS, 'xml:space', 'preserve');
  632. current.txtElement.appendChild(current.tspan);
  633. current.txtgrp.appendChild(current.txtElement);
  634. this._ensureTransformGroup().appendChild(current.txtElement);
  635. },
  636. setLeadingMoveText: function SVGGraphics_setLeadingMoveText(x, y) {
  637. this.setLeading(-y);
  638. this.moveText(x, y);
  639. },
  640. addFontStyle: function SVGGraphics_addFontStyle(fontObj) {
  641. if (!this.cssStyle) {
  642. this.cssStyle = document.createElementNS(NS, 'svg:style');
  643. this.cssStyle.setAttributeNS(null, 'type', 'text/css');
  644. this.defs.appendChild(this.cssStyle);
  645. }
  646. var url = (0, _util.createObjectURL)(fontObj.data, fontObj.mimetype, this.forceDataSchema);
  647. this.cssStyle.textContent += '@font-face { font-family: "' + fontObj.loadedName + '";' + ' src: url(' + url + '); }\n';
  648. },
  649. setFont: function SVGGraphics_setFont(details) {
  650. var current = this.current;
  651. var fontObj = this.commonObjs.get(details[0]);
  652. var size = details[1];
  653. this.current.font = fontObj;
  654. if (this.embedFonts && fontObj.data && !this.embeddedFonts[fontObj.loadedName]) {
  655. this.addFontStyle(fontObj);
  656. this.embeddedFonts[fontObj.loadedName] = fontObj;
  657. }
  658. current.fontMatrix = fontObj.fontMatrix ? fontObj.fontMatrix : _util.FONT_IDENTITY_MATRIX;
  659. var bold = fontObj.black ? fontObj.bold ? 'bolder' : 'bold' : fontObj.bold ? 'bold' : 'normal';
  660. var italic = fontObj.italic ? 'italic' : 'normal';
  661. if (size < 0) {
  662. size = -size;
  663. current.fontDirection = -1;
  664. } else {
  665. current.fontDirection = 1;
  666. }
  667. current.fontSize = size;
  668. current.fontFamily = fontObj.loadedName;
  669. current.fontWeight = bold;
  670. current.fontStyle = italic;
  671. current.tspan = document.createElementNS(NS, 'svg:tspan');
  672. current.tspan.setAttributeNS(null, 'y', pf(-current.y));
  673. current.xcoords = [];
  674. },
  675. endText: function SVGGraphics_endText() {},
  676. setLineWidth: function SVGGraphics_setLineWidth(width) {
  677. this.current.lineWidth = width;
  678. },
  679. setLineCap: function SVGGraphics_setLineCap(style) {
  680. this.current.lineCap = LINE_CAP_STYLES[style];
  681. },
  682. setLineJoin: function SVGGraphics_setLineJoin(style) {
  683. this.current.lineJoin = LINE_JOIN_STYLES[style];
  684. },
  685. setMiterLimit: function SVGGraphics_setMiterLimit(limit) {
  686. this.current.miterLimit = limit;
  687. },
  688. setStrokeAlpha: function SVGGraphics_setStrokeAlpha(strokeAlpha) {
  689. this.current.strokeAlpha = strokeAlpha;
  690. },
  691. setStrokeRGBColor: function SVGGraphics_setStrokeRGBColor(r, g, b) {
  692. var color = _util.Util.makeCssRgb(r, g, b);
  693. this.current.strokeColor = color;
  694. },
  695. setFillAlpha: function SVGGraphics_setFillAlpha(fillAlpha) {
  696. this.current.fillAlpha = fillAlpha;
  697. },
  698. setFillRGBColor: function SVGGraphics_setFillRGBColor(r, g, b) {
  699. var color = _util.Util.makeCssRgb(r, g, b);
  700. this.current.fillColor = color;
  701. this.current.tspan = document.createElementNS(NS, 'svg:tspan');
  702. this.current.xcoords = [];
  703. },
  704. setDash: function SVGGraphics_setDash(dashArray, dashPhase) {
  705. this.current.dashArray = dashArray;
  706. this.current.dashPhase = dashPhase;
  707. },
  708. constructPath: function SVGGraphics_constructPath(ops, args) {
  709. var current = this.current;
  710. var x = current.x,
  711. y = current.y;
  712. current.path = document.createElementNS(NS, 'svg:path');
  713. var d = [];
  714. var opLength = ops.length;
  715. for (var i = 0, j = 0; i < opLength; i++) {
  716. switch (ops[i] | 0) {
  717. case _util.OPS.rectangle:
  718. x = args[j++];
  719. y = args[j++];
  720. var width = args[j++];
  721. var height = args[j++];
  722. var xw = x + width;
  723. var yh = y + height;
  724. d.push('M', pf(x), pf(y), 'L', pf(xw), pf(y), 'L', pf(xw), pf(yh), 'L', pf(x), pf(yh), 'Z');
  725. break;
  726. case _util.OPS.moveTo:
  727. x = args[j++];
  728. y = args[j++];
  729. d.push('M', pf(x), pf(y));
  730. break;
  731. case _util.OPS.lineTo:
  732. x = args[j++];
  733. y = args[j++];
  734. d.push('L', pf(x), pf(y));
  735. break;
  736. case _util.OPS.curveTo:
  737. x = args[j + 4];
  738. y = args[j + 5];
  739. d.push('C', pf(args[j]), pf(args[j + 1]), pf(args[j + 2]), pf(args[j + 3]), pf(x), pf(y));
  740. j += 6;
  741. break;
  742. case _util.OPS.curveTo2:
  743. x = args[j + 2];
  744. y = args[j + 3];
  745. d.push('C', pf(x), pf(y), pf(args[j]), pf(args[j + 1]), pf(args[j + 2]), pf(args[j + 3]));
  746. j += 4;
  747. break;
  748. case _util.OPS.curveTo3:
  749. x = args[j + 2];
  750. y = args[j + 3];
  751. d.push('C', pf(args[j]), pf(args[j + 1]), pf(x), pf(y), pf(x), pf(y));
  752. j += 4;
  753. break;
  754. case _util.OPS.closePath:
  755. d.push('Z');
  756. break;
  757. }
  758. }
  759. current.path.setAttributeNS(null, 'd', d.join(' '));
  760. current.path.setAttributeNS(null, 'fill', 'none');
  761. this._ensureTransformGroup().appendChild(current.path);
  762. current.element = current.path;
  763. current.setCurrentPoint(x, y);
  764. },
  765. endPath: function SVGGraphics_endPath() {
  766. if (!this.pendingClip) {
  767. return;
  768. }
  769. var current = this.current;
  770. var clipId = 'clippath' + clipCount;
  771. clipCount++;
  772. var clipPath = document.createElementNS(NS, 'svg:clipPath');
  773. clipPath.setAttributeNS(null, 'id', clipId);
  774. clipPath.setAttributeNS(null, 'transform', pm(this.transformMatrix));
  775. var clipElement = current.element.cloneNode();
  776. if (this.pendingClip === 'evenodd') {
  777. clipElement.setAttributeNS(null, 'clip-rule', 'evenodd');
  778. } else {
  779. clipElement.setAttributeNS(null, 'clip-rule', 'nonzero');
  780. }
  781. this.pendingClip = null;
  782. clipPath.appendChild(clipElement);
  783. this.defs.appendChild(clipPath);
  784. if (current.activeClipUrl) {
  785. current.clipGroup = null;
  786. this.extraStack.forEach(function (prev) {
  787. prev.clipGroup = null;
  788. });
  789. }
  790. current.activeClipUrl = 'url(#' + clipId + ')';
  791. this.tgrp = null;
  792. },
  793. clip: function SVGGraphics_clip(type) {
  794. this.pendingClip = type;
  795. },
  796. closePath: function SVGGraphics_closePath() {
  797. var current = this.current;
  798. var d = current.path.getAttributeNS(null, 'd');
  799. d += 'Z';
  800. current.path.setAttributeNS(null, 'd', d);
  801. },
  802. setLeading: function SVGGraphics_setLeading(leading) {
  803. this.current.leading = -leading;
  804. },
  805. setTextRise: function SVGGraphics_setTextRise(textRise) {
  806. this.current.textRise = textRise;
  807. },
  808. setHScale: function SVGGraphics_setHScale(scale) {
  809. this.current.textHScale = scale / 100;
  810. },
  811. setGState: function SVGGraphics_setGState(states) {
  812. for (var i = 0, ii = states.length; i < ii; i++) {
  813. var state = states[i];
  814. var key = state[0];
  815. var value = state[1];
  816. switch (key) {
  817. case 'LW':
  818. this.setLineWidth(value);
  819. break;
  820. case 'LC':
  821. this.setLineCap(value);
  822. break;
  823. case 'LJ':
  824. this.setLineJoin(value);
  825. break;
  826. case 'ML':
  827. this.setMiterLimit(value);
  828. break;
  829. case 'D':
  830. this.setDash(value[0], value[1]);
  831. break;
  832. case 'Font':
  833. this.setFont(value);
  834. break;
  835. case 'CA':
  836. this.setStrokeAlpha(value);
  837. break;
  838. case 'ca':
  839. this.setFillAlpha(value);
  840. break;
  841. default:
  842. (0, _util.warn)('Unimplemented graphic state ' + key);
  843. break;
  844. }
  845. }
  846. },
  847. fill: function SVGGraphics_fill() {
  848. var current = this.current;
  849. current.element.setAttributeNS(null, 'fill', current.fillColor);
  850. current.element.setAttributeNS(null, 'fill-opacity', current.fillAlpha);
  851. },
  852. stroke: function SVGGraphics_stroke() {
  853. var current = this.current;
  854. current.element.setAttributeNS(null, 'stroke', current.strokeColor);
  855. current.element.setAttributeNS(null, 'stroke-opacity', current.strokeAlpha);
  856. current.element.setAttributeNS(null, 'stroke-miterlimit', pf(current.miterLimit));
  857. current.element.setAttributeNS(null, 'stroke-linecap', current.lineCap);
  858. current.element.setAttributeNS(null, 'stroke-linejoin', current.lineJoin);
  859. current.element.setAttributeNS(null, 'stroke-width', pf(current.lineWidth) + 'px');
  860. current.element.setAttributeNS(null, 'stroke-dasharray', current.dashArray.map(pf).join(' '));
  861. current.element.setAttributeNS(null, 'stroke-dashoffset', pf(current.dashPhase) + 'px');
  862. current.element.setAttributeNS(null, 'fill', 'none');
  863. },
  864. eoFill: function SVGGraphics_eoFill() {
  865. this.current.element.setAttributeNS(null, 'fill-rule', 'evenodd');
  866. this.fill();
  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', pf(w));
  899. imgEl.setAttributeNS(null, 'height', pf(h));
  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. (0, _util.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 ((0, _util.isArray)(matrix) && matrix.length === 6) {
  958. this.transform(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
  959. }
  960. if ((0, _util.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. _initialize: function SVGGraphics_initialize(viewport) {
  975. var svg = document.createElementNS(NS, 'svg:svg');
  976. svg.setAttributeNS(null, 'version', '1.1');
  977. svg.setAttributeNS(null, 'width', viewport.width + 'px');
  978. svg.setAttributeNS(null, 'height', viewport.height + 'px');
  979. svg.setAttributeNS(null, 'preserveAspectRatio', 'none');
  980. svg.setAttributeNS(null, 'viewBox', '0 0 ' + viewport.width + ' ' + viewport.height);
  981. var definitions = document.createElementNS(NS, 'svg:defs');
  982. svg.appendChild(definitions);
  983. this.defs = definitions;
  984. var rootGroup = document.createElementNS(NS, 'svg:g');
  985. rootGroup.setAttributeNS(null, 'transform', pm(viewport.transform));
  986. svg.appendChild(rootGroup);
  987. this.svg = rootGroup;
  988. return svg;
  989. },
  990. _ensureClipGroup: function SVGGraphics_ensureClipGroup() {
  991. if (!this.current.clipGroup) {
  992. var clipGroup = document.createElementNS(NS, 'svg:g');
  993. clipGroup.setAttributeNS(null, 'clip-path', this.current.activeClipUrl);
  994. this.svg.appendChild(clipGroup);
  995. this.current.clipGroup = clipGroup;
  996. }
  997. return this.current.clipGroup;
  998. },
  999. _ensureTransformGroup: function SVGGraphics_ensureTransformGroup() {
  1000. if (!this.tgrp) {
  1001. this.tgrp = document.createElementNS(NS, 'svg:g');
  1002. this.tgrp.setAttributeNS(null, 'transform', pm(this.transformMatrix));
  1003. if (this.current.activeClipUrl) {
  1004. this._ensureClipGroup().appendChild(this.tgrp);
  1005. } else {
  1006. this.svg.appendChild(this.tgrp);
  1007. }
  1008. }
  1009. return this.tgrp;
  1010. }
  1011. };
  1012. return SVGGraphics;
  1013. }();
  1014. }
  1015. exports.SVGGraphics = SVGGraphics;