svg.js 37 KB

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