2
0

svg.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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 _this = this;
  314. var fnArray = operatorList.fnArray;
  315. var fnArrayLen = fnArray.length;
  316. var argsArray = operatorList.argsArray;
  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. _this.commonObjs.get(obj, resolve);
  327. });
  328. } else {
  329. promise = new Promise(function (resolve) {
  330. _this.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. var _this2 = this;
  346. this.viewport = viewport;
  347. var svgElement = this._initialize(viewport);
  348. return this.loadDependencies(operatorList).then(function () {
  349. _this2.transformMatrix = _util.IDENTITY_MATRIX;
  350. var opTree = _this2.convertOpList(operatorList);
  351. _this2.executeOpTree(opTree);
  352. return svgElement;
  353. });
  354. },
  355. convertOpList: function SVGGraphics_convertOpList(operatorList) {
  356. var argsArray = operatorList.argsArray;
  357. var fnArray = operatorList.fnArray;
  358. var fnArrayLen = fnArray.length;
  359. var REVOPS = [];
  360. var opList = [];
  361. for (var op in _util.OPS) {
  362. REVOPS[_util.OPS[op]] = op;
  363. }
  364. for (var x = 0; x < fnArrayLen; x++) {
  365. var fnId = fnArray[x];
  366. opList.push({
  367. 'fnId': fnId,
  368. 'fn': REVOPS[fnId],
  369. 'args': argsArray[x]
  370. });
  371. }
  372. return opListToTree(opList);
  373. },
  374. executeOpTree: function SVGGraphics_executeOpTree(opTree) {
  375. var opTreeLen = opTree.length;
  376. for (var x = 0; x < opTreeLen; x++) {
  377. var fn = opTree[x].fn;
  378. var fnId = opTree[x].fnId;
  379. var args = opTree[x].args;
  380. switch (fnId | 0) {
  381. case _util.OPS.beginText:
  382. this.beginText();
  383. break;
  384. case _util.OPS.setLeading:
  385. this.setLeading(args);
  386. break;
  387. case _util.OPS.setLeadingMoveText:
  388. this.setLeadingMoveText(args[0], args[1]);
  389. break;
  390. case _util.OPS.setFont:
  391. this.setFont(args);
  392. break;
  393. case _util.OPS.showText:
  394. this.showText(args[0]);
  395. break;
  396. case _util.OPS.showSpacedText:
  397. this.showText(args[0]);
  398. break;
  399. case _util.OPS.endText:
  400. this.endText();
  401. break;
  402. case _util.OPS.moveText:
  403. this.moveText(args[0], args[1]);
  404. break;
  405. case _util.OPS.setCharSpacing:
  406. this.setCharSpacing(args[0]);
  407. break;
  408. case _util.OPS.setWordSpacing:
  409. this.setWordSpacing(args[0]);
  410. break;
  411. case _util.OPS.setHScale:
  412. this.setHScale(args[0]);
  413. break;
  414. case _util.OPS.setTextMatrix:
  415. this.setTextMatrix(args[0], args[1], args[2], args[3], args[4], args[5]);
  416. break;
  417. case _util.OPS.setLineWidth:
  418. this.setLineWidth(args[0]);
  419. break;
  420. case _util.OPS.setLineJoin:
  421. this.setLineJoin(args[0]);
  422. break;
  423. case _util.OPS.setLineCap:
  424. this.setLineCap(args[0]);
  425. break;
  426. case _util.OPS.setMiterLimit:
  427. this.setMiterLimit(args[0]);
  428. break;
  429. case _util.OPS.setFillRGBColor:
  430. this.setFillRGBColor(args[0], args[1], args[2]);
  431. break;
  432. case _util.OPS.setStrokeRGBColor:
  433. this.setStrokeRGBColor(args[0], args[1], args[2]);
  434. break;
  435. case _util.OPS.setDash:
  436. this.setDash(args[0], args[1]);
  437. break;
  438. case _util.OPS.setGState:
  439. this.setGState(args[0]);
  440. break;
  441. case _util.OPS.fill:
  442. this.fill();
  443. break;
  444. case _util.OPS.eoFill:
  445. this.eoFill();
  446. break;
  447. case _util.OPS.stroke:
  448. this.stroke();
  449. break;
  450. case _util.OPS.fillStroke:
  451. this.fillStroke();
  452. break;
  453. case _util.OPS.eoFillStroke:
  454. this.eoFillStroke();
  455. break;
  456. case _util.OPS.clip:
  457. this.clip('nonzero');
  458. break;
  459. case _util.OPS.eoClip:
  460. this.clip('evenodd');
  461. break;
  462. case _util.OPS.paintSolidColorImageMask:
  463. this.paintSolidColorImageMask();
  464. break;
  465. case _util.OPS.paintJpegXObject:
  466. this.paintJpegXObject(args[0], args[1], args[2]);
  467. break;
  468. case _util.OPS.paintImageXObject:
  469. this.paintImageXObject(args[0]);
  470. break;
  471. case _util.OPS.paintInlineImageXObject:
  472. this.paintInlineImageXObject(args[0]);
  473. break;
  474. case _util.OPS.paintImageMaskXObject:
  475. this.paintImageMaskXObject(args[0]);
  476. break;
  477. case _util.OPS.paintFormXObjectBegin:
  478. this.paintFormXObjectBegin(args[0], args[1]);
  479. break;
  480. case _util.OPS.paintFormXObjectEnd:
  481. this.paintFormXObjectEnd();
  482. break;
  483. case _util.OPS.closePath:
  484. this.closePath();
  485. break;
  486. case _util.OPS.closeStroke:
  487. this.closeStroke();
  488. break;
  489. case _util.OPS.closeFillStroke:
  490. this.closeFillStroke();
  491. break;
  492. case _util.OPS.nextLine:
  493. this.nextLine();
  494. break;
  495. case _util.OPS.transform:
  496. this.transform(args[0], args[1], args[2], args[3], args[4], args[5]);
  497. break;
  498. case _util.OPS.constructPath:
  499. this.constructPath(args[0], args[1]);
  500. break;
  501. case _util.OPS.endPath:
  502. this.endPath();
  503. break;
  504. case 92:
  505. this.group(opTree[x].items);
  506. break;
  507. default:
  508. (0, _util.warn)('Unimplemented operator ' + fn);
  509. break;
  510. }
  511. }
  512. },
  513. setWordSpacing: function SVGGraphics_setWordSpacing(wordSpacing) {
  514. this.current.wordSpacing = wordSpacing;
  515. },
  516. setCharSpacing: function SVGGraphics_setCharSpacing(charSpacing) {
  517. this.current.charSpacing = charSpacing;
  518. },
  519. nextLine: function SVGGraphics_nextLine() {
  520. this.moveText(0, this.current.leading);
  521. },
  522. setTextMatrix: function SVGGraphics_setTextMatrix(a, b, c, d, e, f) {
  523. var current = this.current;
  524. this.current.textMatrix = this.current.lineMatrix = [a, b, c, d, e, f];
  525. this.current.x = this.current.lineX = 0;
  526. this.current.y = this.current.lineY = 0;
  527. current.xcoords = [];
  528. current.tspan = document.createElementNS(NS, 'svg:tspan');
  529. current.tspan.setAttributeNS(null, 'font-family', current.fontFamily);
  530. current.tspan.setAttributeNS(null, 'font-size', pf(current.fontSize) + 'px');
  531. current.tspan.setAttributeNS(null, 'y', pf(-current.y));
  532. current.txtElement = document.createElementNS(NS, 'svg:text');
  533. current.txtElement.appendChild(current.tspan);
  534. },
  535. beginText: function SVGGraphics_beginText() {
  536. this.current.x = this.current.lineX = 0;
  537. this.current.y = this.current.lineY = 0;
  538. this.current.textMatrix = _util.IDENTITY_MATRIX;
  539. this.current.lineMatrix = _util.IDENTITY_MATRIX;
  540. this.current.tspan = document.createElementNS(NS, 'svg:tspan');
  541. this.current.txtElement = document.createElementNS(NS, 'svg:text');
  542. this.current.txtgrp = document.createElementNS(NS, 'svg:g');
  543. this.current.xcoords = [];
  544. },
  545. moveText: function SVGGraphics_moveText(x, y) {
  546. var current = this.current;
  547. this.current.x = this.current.lineX += x;
  548. this.current.y = this.current.lineY += y;
  549. current.xcoords = [];
  550. current.tspan = document.createElementNS(NS, 'svg:tspan');
  551. current.tspan.setAttributeNS(null, 'font-family', current.fontFamily);
  552. current.tspan.setAttributeNS(null, 'font-size', pf(current.fontSize) + 'px');
  553. current.tspan.setAttributeNS(null, 'y', pf(-current.y));
  554. },
  555. showText: function SVGGraphics_showText(glyphs) {
  556. var current = this.current;
  557. var font = current.font;
  558. var fontSize = current.fontSize;
  559. if (fontSize === 0) {
  560. return;
  561. }
  562. var charSpacing = current.charSpacing;
  563. var wordSpacing = current.wordSpacing;
  564. var fontDirection = current.fontDirection;
  565. var textHScale = current.textHScale * fontDirection;
  566. var glyphsLength = glyphs.length;
  567. var vertical = font.vertical;
  568. var widthAdvanceScale = fontSize * current.fontMatrix[0];
  569. var x = 0,
  570. i;
  571. for (i = 0; i < glyphsLength; ++i) {
  572. var glyph = glyphs[i];
  573. if (glyph === null) {
  574. x += fontDirection * wordSpacing;
  575. continue;
  576. } else if ((0, _util.isNum)(glyph)) {
  577. x += -glyph * fontSize * 0.001;
  578. continue;
  579. }
  580. current.xcoords.push(current.x + x * textHScale);
  581. var width = glyph.width;
  582. var character = glyph.fontChar;
  583. var spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
  584. var charWidth = width * widthAdvanceScale + spacing * fontDirection;
  585. x += charWidth;
  586. current.tspan.textContent += character;
  587. }
  588. if (vertical) {
  589. current.y -= x * textHScale;
  590. } else {
  591. current.x += x * textHScale;
  592. }
  593. current.tspan.setAttributeNS(null, 'x', current.xcoords.map(pf).join(' '));
  594. current.tspan.setAttributeNS(null, 'y', pf(-current.y));
  595. current.tspan.setAttributeNS(null, 'font-family', current.fontFamily);
  596. current.tspan.setAttributeNS(null, 'font-size', pf(current.fontSize) + 'px');
  597. if (current.fontStyle !== SVG_DEFAULTS.fontStyle) {
  598. current.tspan.setAttributeNS(null, 'font-style', current.fontStyle);
  599. }
  600. if (current.fontWeight !== SVG_DEFAULTS.fontWeight) {
  601. current.tspan.setAttributeNS(null, 'font-weight', current.fontWeight);
  602. }
  603. if (current.fillColor !== SVG_DEFAULTS.fillColor) {
  604. current.tspan.setAttributeNS(null, 'fill', current.fillColor);
  605. }
  606. current.txtElement.setAttributeNS(null, 'transform', pm(current.textMatrix) + ' scale(1, -1)');
  607. current.txtElement.setAttributeNS(XML_NS, 'xml:space', 'preserve');
  608. current.txtElement.appendChild(current.tspan);
  609. current.txtgrp.appendChild(current.txtElement);
  610. this._ensureTransformGroup().appendChild(current.txtElement);
  611. },
  612. setLeadingMoveText: function SVGGraphics_setLeadingMoveText(x, y) {
  613. this.setLeading(-y);
  614. this.moveText(x, y);
  615. },
  616. addFontStyle: function SVGGraphics_addFontStyle(fontObj) {
  617. if (!this.cssStyle) {
  618. this.cssStyle = document.createElementNS(NS, 'svg:style');
  619. this.cssStyle.setAttributeNS(null, 'type', 'text/css');
  620. this.defs.appendChild(this.cssStyle);
  621. }
  622. var url = (0, _util.createObjectURL)(fontObj.data, fontObj.mimetype, this.forceDataSchema);
  623. this.cssStyle.textContent += '@font-face { font-family: "' + fontObj.loadedName + '";' + ' src: url(' + url + '); }\n';
  624. },
  625. setFont: function SVGGraphics_setFont(details) {
  626. var current = this.current;
  627. var fontObj = this.commonObjs.get(details[0]);
  628. var size = details[1];
  629. this.current.font = fontObj;
  630. if (this.embedFonts && fontObj.data && !this.embeddedFonts[fontObj.loadedName]) {
  631. this.addFontStyle(fontObj);
  632. this.embeddedFonts[fontObj.loadedName] = fontObj;
  633. }
  634. current.fontMatrix = fontObj.fontMatrix ? fontObj.fontMatrix : _util.FONT_IDENTITY_MATRIX;
  635. var bold = fontObj.black ? fontObj.bold ? 'bolder' : 'bold' : fontObj.bold ? 'bold' : 'normal';
  636. var italic = fontObj.italic ? 'italic' : 'normal';
  637. if (size < 0) {
  638. size = -size;
  639. current.fontDirection = -1;
  640. } else {
  641. current.fontDirection = 1;
  642. }
  643. current.fontSize = size;
  644. current.fontFamily = fontObj.loadedName;
  645. current.fontWeight = bold;
  646. current.fontStyle = italic;
  647. current.tspan = document.createElementNS(NS, 'svg:tspan');
  648. current.tspan.setAttributeNS(null, 'y', pf(-current.y));
  649. current.xcoords = [];
  650. },
  651. endText: function SVGGraphics_endText() {},
  652. setLineWidth: function SVGGraphics_setLineWidth(width) {
  653. this.current.lineWidth = width;
  654. },
  655. setLineCap: function SVGGraphics_setLineCap(style) {
  656. this.current.lineCap = LINE_CAP_STYLES[style];
  657. },
  658. setLineJoin: function SVGGraphics_setLineJoin(style) {
  659. this.current.lineJoin = LINE_JOIN_STYLES[style];
  660. },
  661. setMiterLimit: function SVGGraphics_setMiterLimit(limit) {
  662. this.current.miterLimit = limit;
  663. },
  664. setStrokeRGBColor: function SVGGraphics_setStrokeRGBColor(r, g, b) {
  665. var color = _util.Util.makeCssRgb(r, g, b);
  666. this.current.strokeColor = color;
  667. },
  668. setFillRGBColor: function SVGGraphics_setFillRGBColor(r, g, b) {
  669. var color = _util.Util.makeCssRgb(r, g, b);
  670. this.current.fillColor = color;
  671. this.current.tspan = document.createElementNS(NS, 'svg:tspan');
  672. this.current.xcoords = [];
  673. },
  674. setDash: function SVGGraphics_setDash(dashArray, dashPhase) {
  675. this.current.dashArray = dashArray;
  676. this.current.dashPhase = dashPhase;
  677. },
  678. constructPath: function SVGGraphics_constructPath(ops, args) {
  679. var current = this.current;
  680. var x = current.x,
  681. y = current.y;
  682. current.path = document.createElementNS(NS, 'svg:path');
  683. var d = [];
  684. var opLength = ops.length;
  685. for (var i = 0, j = 0; i < opLength; i++) {
  686. switch (ops[i] | 0) {
  687. case _util.OPS.rectangle:
  688. x = args[j++];
  689. y = args[j++];
  690. var width = args[j++];
  691. var height = args[j++];
  692. var xw = x + width;
  693. var yh = y + height;
  694. d.push('M', pf(x), pf(y), 'L', pf(xw), pf(y), 'L', pf(xw), pf(yh), 'L', pf(x), pf(yh), 'Z');
  695. break;
  696. case _util.OPS.moveTo:
  697. x = args[j++];
  698. y = args[j++];
  699. d.push('M', pf(x), pf(y));
  700. break;
  701. case _util.OPS.lineTo:
  702. x = args[j++];
  703. y = args[j++];
  704. d.push('L', pf(x), pf(y));
  705. break;
  706. case _util.OPS.curveTo:
  707. x = args[j + 4];
  708. y = args[j + 5];
  709. d.push('C', pf(args[j]), pf(args[j + 1]), pf(args[j + 2]), pf(args[j + 3]), pf(x), pf(y));
  710. j += 6;
  711. break;
  712. case _util.OPS.curveTo2:
  713. x = args[j + 2];
  714. y = args[j + 3];
  715. d.push('C', pf(x), pf(y), pf(args[j]), pf(args[j + 1]), pf(args[j + 2]), pf(args[j + 3]));
  716. j += 4;
  717. break;
  718. case _util.OPS.curveTo3:
  719. x = args[j + 2];
  720. y = args[j + 3];
  721. d.push('C', pf(args[j]), pf(args[j + 1]), pf(x), pf(y), pf(x), pf(y));
  722. j += 4;
  723. break;
  724. case _util.OPS.closePath:
  725. d.push('Z');
  726. break;
  727. }
  728. }
  729. current.path.setAttributeNS(null, 'd', d.join(' '));
  730. current.path.setAttributeNS(null, 'stroke-miterlimit', pf(current.miterLimit));
  731. current.path.setAttributeNS(null, 'stroke-linecap', current.lineCap);
  732. current.path.setAttributeNS(null, 'stroke-linejoin', current.lineJoin);
  733. current.path.setAttributeNS(null, 'stroke-width', pf(current.lineWidth) + 'px');
  734. current.path.setAttributeNS(null, 'stroke-dasharray', current.dashArray.map(pf).join(' '));
  735. current.path.setAttributeNS(null, 'stroke-dashoffset', pf(current.dashPhase) + 'px');
  736. current.path.setAttributeNS(null, 'fill', 'none');
  737. this._ensureTransformGroup().appendChild(current.path);
  738. current.element = current.path;
  739. current.setCurrentPoint(x, y);
  740. },
  741. endPath: function SVGGraphics_endPath() {},
  742. clip: function SVGGraphics_clip(type) {
  743. var current = this.current;
  744. var clipId = 'clippath' + clipCount;
  745. clipCount++;
  746. var clipPath = document.createElementNS(NS, 'svg:clipPath');
  747. clipPath.setAttributeNS(null, 'id', clipId);
  748. clipPath.setAttributeNS(null, 'transform', pm(this.transformMatrix));
  749. var clipElement = current.element.cloneNode();
  750. if (type === 'evenodd') {
  751. clipElement.setAttributeNS(null, 'clip-rule', 'evenodd');
  752. } else {
  753. clipElement.setAttributeNS(null, 'clip-rule', 'nonzero');
  754. }
  755. clipPath.appendChild(clipElement);
  756. this.defs.appendChild(clipPath);
  757. if (current.activeClipUrl) {
  758. current.clipGroup = null;
  759. this.extraStack.forEach(function (prev) {
  760. prev.clipGroup = null;
  761. });
  762. }
  763. current.activeClipUrl = 'url(#' + clipId + ')';
  764. this.tgrp = null;
  765. },
  766. closePath: function SVGGraphics_closePath() {
  767. var current = this.current;
  768. var d = current.path.getAttributeNS(null, 'd');
  769. d += 'Z';
  770. current.path.setAttributeNS(null, 'd', d);
  771. },
  772. setLeading: function SVGGraphics_setLeading(leading) {
  773. this.current.leading = -leading;
  774. },
  775. setTextRise: function SVGGraphics_setTextRise(textRise) {
  776. this.current.textRise = textRise;
  777. },
  778. setHScale: function SVGGraphics_setHScale(scale) {
  779. this.current.textHScale = scale / 100;
  780. },
  781. setGState: function SVGGraphics_setGState(states) {
  782. for (var i = 0, ii = states.length; i < ii; i++) {
  783. var state = states[i];
  784. var key = state[0];
  785. var value = state[1];
  786. switch (key) {
  787. case 'LW':
  788. this.setLineWidth(value);
  789. break;
  790. case 'LC':
  791. this.setLineCap(value);
  792. break;
  793. case 'LJ':
  794. this.setLineJoin(value);
  795. break;
  796. case 'ML':
  797. this.setMiterLimit(value);
  798. break;
  799. case 'D':
  800. this.setDash(value[0], value[1]);
  801. break;
  802. case 'Font':
  803. this.setFont(value);
  804. break;
  805. default:
  806. (0, _util.warn)('Unimplemented graphic state ' + key);
  807. break;
  808. }
  809. }
  810. },
  811. fill: function SVGGraphics_fill() {
  812. var current = this.current;
  813. current.element.setAttributeNS(null, 'fill', current.fillColor);
  814. },
  815. stroke: function SVGGraphics_stroke() {
  816. var current = this.current;
  817. current.element.setAttributeNS(null, 'stroke', current.strokeColor);
  818. current.element.setAttributeNS(null, 'fill', 'none');
  819. },
  820. eoFill: function SVGGraphics_eoFill() {
  821. var current = this.current;
  822. current.element.setAttributeNS(null, 'fill', current.fillColor);
  823. current.element.setAttributeNS(null, 'fill-rule', 'evenodd');
  824. },
  825. fillStroke: function SVGGraphics_fillStroke() {
  826. this.stroke();
  827. this.fill();
  828. },
  829. eoFillStroke: function SVGGraphics_eoFillStroke() {
  830. this.current.element.setAttributeNS(null, 'fill-rule', 'evenodd');
  831. this.fillStroke();
  832. },
  833. closeStroke: function SVGGraphics_closeStroke() {
  834. this.closePath();
  835. this.stroke();
  836. },
  837. closeFillStroke: function SVGGraphics_closeFillStroke() {
  838. this.closePath();
  839. this.fillStroke();
  840. },
  841. paintSolidColorImageMask: function SVGGraphics_paintSolidColorImageMask() {
  842. var current = this.current;
  843. var rect = document.createElementNS(NS, 'svg:rect');
  844. rect.setAttributeNS(null, 'x', '0');
  845. rect.setAttributeNS(null, 'y', '0');
  846. rect.setAttributeNS(null, 'width', '1px');
  847. rect.setAttributeNS(null, 'height', '1px');
  848. rect.setAttributeNS(null, 'fill', current.fillColor);
  849. this._ensureTransformGroup().appendChild(rect);
  850. },
  851. paintJpegXObject: function SVGGraphics_paintJpegXObject(objId, w, h) {
  852. var imgObj = this.objs.get(objId);
  853. var imgEl = document.createElementNS(NS, 'svg:image');
  854. imgEl.setAttributeNS(XLINK_NS, 'xlink:href', imgObj.src);
  855. imgEl.setAttributeNS(null, 'width', pf(w));
  856. imgEl.setAttributeNS(null, 'height', pf(h));
  857. imgEl.setAttributeNS(null, 'x', '0');
  858. imgEl.setAttributeNS(null, 'y', pf(-h));
  859. imgEl.setAttributeNS(null, 'transform', 'scale(' + pf(1 / w) + ' ' + pf(-1 / h) + ')');
  860. this._ensureTransformGroup().appendChild(imgEl);
  861. },
  862. paintImageXObject: function SVGGraphics_paintImageXObject(objId) {
  863. var imgData = this.objs.get(objId);
  864. if (!imgData) {
  865. (0, _util.warn)('Dependent image isn\'t ready yet');
  866. return;
  867. }
  868. this.paintInlineImageXObject(imgData);
  869. },
  870. paintInlineImageXObject: function SVGGraphics_paintInlineImageXObject(imgData, mask) {
  871. var width = imgData.width;
  872. var height = imgData.height;
  873. var imgSrc = convertImgDataToPng(imgData, this.forceDataSchema);
  874. var cliprect = document.createElementNS(NS, 'svg:rect');
  875. cliprect.setAttributeNS(null, 'x', '0');
  876. cliprect.setAttributeNS(null, 'y', '0');
  877. cliprect.setAttributeNS(null, 'width', pf(width));
  878. cliprect.setAttributeNS(null, 'height', pf(height));
  879. this.current.element = cliprect;
  880. this.clip('nonzero');
  881. var imgEl = document.createElementNS(NS, 'svg:image');
  882. imgEl.setAttributeNS(XLINK_NS, 'xlink:href', imgSrc);
  883. imgEl.setAttributeNS(null, 'x', '0');
  884. imgEl.setAttributeNS(null, 'y', pf(-height));
  885. imgEl.setAttributeNS(null, 'width', pf(width) + 'px');
  886. imgEl.setAttributeNS(null, 'height', pf(height) + 'px');
  887. imgEl.setAttributeNS(null, 'transform', 'scale(' + pf(1 / width) + ' ' + pf(-1 / height) + ')');
  888. if (mask) {
  889. mask.appendChild(imgEl);
  890. } else {
  891. this._ensureTransformGroup().appendChild(imgEl);
  892. }
  893. },
  894. paintImageMaskXObject: function SVGGraphics_paintImageMaskXObject(imgData) {
  895. var current = this.current;
  896. var width = imgData.width;
  897. var height = imgData.height;
  898. var fillColor = current.fillColor;
  899. current.maskId = 'mask' + maskCount++;
  900. var mask = document.createElementNS(NS, 'svg:mask');
  901. mask.setAttributeNS(null, 'id', current.maskId);
  902. var rect = document.createElementNS(NS, 'svg:rect');
  903. rect.setAttributeNS(null, 'x', '0');
  904. rect.setAttributeNS(null, 'y', '0');
  905. rect.setAttributeNS(null, 'width', pf(width));
  906. rect.setAttributeNS(null, 'height', pf(height));
  907. rect.setAttributeNS(null, 'fill', fillColor);
  908. rect.setAttributeNS(null, 'mask', 'url(#' + current.maskId + ')');
  909. this.defs.appendChild(mask);
  910. this._ensureTransformGroup().appendChild(rect);
  911. this.paintInlineImageXObject(imgData, mask);
  912. },
  913. paintFormXObjectBegin: function SVGGraphics_paintFormXObjectBegin(matrix, bbox) {
  914. if ((0, _util.isArray)(matrix) && matrix.length === 6) {
  915. this.transform(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
  916. }
  917. if ((0, _util.isArray)(bbox) && bbox.length === 4) {
  918. var width = bbox[2] - bbox[0];
  919. var height = bbox[3] - bbox[1];
  920. var cliprect = document.createElementNS(NS, 'svg:rect');
  921. cliprect.setAttributeNS(null, 'x', bbox[0]);
  922. cliprect.setAttributeNS(null, 'y', bbox[1]);
  923. cliprect.setAttributeNS(null, 'width', pf(width));
  924. cliprect.setAttributeNS(null, 'height', pf(height));
  925. this.current.element = cliprect;
  926. this.clip('nonzero');
  927. this.endPath();
  928. }
  929. },
  930. paintFormXObjectEnd: function SVGGraphics_paintFormXObjectEnd() {},
  931. _initialize: function SVGGraphics_initialize(viewport) {
  932. var svg = document.createElementNS(NS, 'svg:svg');
  933. svg.setAttributeNS(null, 'version', '1.1');
  934. svg.setAttributeNS(null, 'width', viewport.width + 'px');
  935. svg.setAttributeNS(null, 'height', viewport.height + 'px');
  936. svg.setAttributeNS(null, 'preserveAspectRatio', 'none');
  937. svg.setAttributeNS(null, 'viewBox', '0 0 ' + viewport.width + ' ' + viewport.height);
  938. var definitions = document.createElementNS(NS, 'svg:defs');
  939. svg.appendChild(definitions);
  940. this.defs = definitions;
  941. var rootGroup = document.createElementNS(NS, 'svg:g');
  942. rootGroup.setAttributeNS(null, 'transform', pm(viewport.transform));
  943. svg.appendChild(rootGroup);
  944. this.svg = rootGroup;
  945. return svg;
  946. },
  947. _ensureClipGroup: function SVGGraphics_ensureClipGroup() {
  948. if (!this.current.clipGroup) {
  949. var clipGroup = document.createElementNS(NS, 'svg:g');
  950. clipGroup.setAttributeNS(null, 'clip-path', this.current.activeClipUrl);
  951. this.svg.appendChild(clipGroup);
  952. this.current.clipGroup = clipGroup;
  953. }
  954. return this.current.clipGroup;
  955. },
  956. _ensureTransformGroup: function SVGGraphics_ensureTransformGroup() {
  957. if (!this.tgrp) {
  958. this.tgrp = document.createElementNS(NS, 'svg:g');
  959. this.tgrp.setAttributeNS(null, 'transform', pm(this.transformMatrix));
  960. if (this.current.activeClipUrl) {
  961. this._ensureClipGroup().appendChild(this.tgrp);
  962. } else {
  963. this.svg.appendChild(this.tgrp);
  964. }
  965. }
  966. return this.tgrp;
  967. }
  968. };
  969. return SVGGraphics;
  970. }();
  971. }
  972. exports.SVGGraphics = SVGGraphics;