pdfmake 0.1.66 → 0.1.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -103,7 +103,9 @@ StyleContextStack.prototype.autopush = function (item) {
103
103
  'characterSpacing',
104
104
  'noWrap',
105
105
  'markerColor',
106
- 'leadingIndent'
106
+ 'leadingIndent',
107
+ 'sup',
108
+ 'sub'
107
109
  //'tableCellPadding'
108
110
  // 'cellBorder',
109
111
  // 'headerCellBorder',
package/src/svgMeasure.js CHANGED
@@ -1,106 +1,70 @@
1
1
  'use strict';
2
2
 
3
- function SVGMeasure() {
4
- }
3
+ var xmldoc = require('xmldoc');
5
4
 
6
- SVGMeasure.prototype.getSVGNode = function (svgString) {
7
- // remove newlines
8
- svgString = svgString.replace(/\r?\n|\r/g, "");
9
-
10
- var svgNodeMatches = svgString.match(/<svg(.*?)>/);
5
+ /** Strip unit postfix, parse number, but return undefined instead of NaN for bad input */
6
+ function stripUnits(textVal) {
7
+ var n = parseFloat(textVal);
8
+ if (typeof n !== 'number' || isNaN(n)) {
9
+ return undefined;
10
+ }
11
+ return n;
12
+ }
11
13
 
12
- if (svgNodeMatches) {
13
- // extract svg node <svg ... >
14
- return svgNodeMatches[0];
14
+ /** Make sure it's valid XML and the root tage is <svg/>, returns xmldoc DOM */
15
+ function parseSVG(svgString) {
16
+ var doc;
17
+
18
+ try {
19
+ doc = new xmldoc.XmlDocument(svgString);
20
+ } catch (err) {
21
+ throw new Error('SVGMeasure: ' + err);
15
22
  }
16
23
 
17
- return "";
18
- };
24
+ if (doc.name !== "svg") {
25
+ throw new Error('SVGMeasure: expected <svg> document');
26
+ }
19
27
 
20
- SVGMeasure.prototype.getHeightAndWidth = function (svgString) {
21
- var svgNode = this.getSVGNode(svgString);
28
+ return doc;
29
+ }
22
30
 
23
- var widthMatches = svgNode.match(/width="([0-9]+(\.[0-9]+)?)(em|ex|px|in|cm|mm|pt|pc|%)?"/);
24
- var heightMatches = svgNode.match(/height="([0-9]+(\.[0-9]+)?)(em|ex|px|in|cm|mm|pt|pc|%)?"/);
31
+ function SVGMeasure() {
32
+ }
25
33
 
26
- if (widthMatches || heightMatches) {
27
- return {
28
- width: widthMatches ? +widthMatches[1] : undefined,
29
- height: heightMatches ? +heightMatches[1] : undefined
30
- };
31
- }
32
- };
34
+ SVGMeasure.prototype.measureSVG = function (svgString) {
33
35
 
34
- SVGMeasure.prototype.getViewboxHeightAndWidth = function (svgString) {
35
- var svgNode = this.getSVGNode(svgString);
36
+ var doc = parseSVG(svgString);
36
37
 
37
- var viewboxMatches = svgNode.match(/viewBox="([+-]?(\d*\.)?\d+(,|\s+|,\s+)[+-]?(\d*\.)?\d+(,|\s+|,\s+)[+-]?(\d*\.)?\d+(,|\s+|,\s+)[+-]?(\d*\.)?\d+)"/);
38
- if (viewboxMatches) {
39
- var viewboxStr = viewboxMatches[1];
40
- var allVieboxEntries = viewboxStr.split(" ");
38
+ var docWidth = stripUnits(doc.attr.width);
39
+ var docHeight = stripUnits(doc.attr.height);
41
40
 
42
- var viewboxEntries = []; // weeding out empty strings
43
- for (var i = 0; i < allVieboxEntries.length; i++) {
44
- if (allVieboxEntries[i]) {
45
- viewboxEntries.push(allVieboxEntries[i]);
46
- }
41
+ if ((docWidth == undefined || docHeight == undefined) && typeof doc.attr.viewBox == 'string') {
42
+ var viewBoxParts = doc.attr.viewBox.split(/[,\s]+/);
43
+ if (viewBoxParts.length !== 4) {
44
+ throw new Error("Unexpected svg viewbox format, should have 4 entries but found: '" + doc.attr.viewBox + "'");
47
45
  }
48
-
49
- if (viewboxEntries.length === 4) {
50
- return { width: +viewboxEntries[2], height: +viewboxEntries[3] };
46
+ if (docWidth == undefined) {
47
+ docWidth = stripUnits(viewBoxParts[2]);
48
+ }
49
+ if (docHeight == undefined) {
50
+ docHeight = stripUnits(viewBoxParts[3]);
51
51
  }
52
-
53
- throw new Error("Unexpected svg viewbox format, should have 4 entries but found: '" + viewboxStr + "'");
54
52
  }
55
- };
56
-
57
- SVGMeasure.prototype.measureSVG = function (svgString) {
58
-
59
- var heightAndWidth = this.getHeightAndWidth(svgString);
60
- var viewboxHeightAndWidth = this.getViewboxHeightAndWidth(svgString);
61
53
 
62
- return heightAndWidth || viewboxHeightAndWidth || {};
54
+ return {
55
+ width: docWidth,
56
+ height: docHeight
57
+ };
63
58
  };
64
59
 
65
60
  SVGMeasure.prototype.writeDimensions = function (svgString, dimensions) {
66
61
 
67
- var svgNode = this.getSVGNode(svgString);
68
-
69
- if (svgNode) {
70
-
71
- var nodeDimensions = this.getHeightAndWidth(svgString);
72
-
73
- if (dimensions.width) {
74
-
75
- var newWidth = 'width="' + dimensions.width + '"';
62
+ var doc = parseSVG(svgString);
76
63
 
77
- if (nodeDimensions && nodeDimensions.width) {
78
- // replace existing width
79
- svgNode = svgNode.replace(/width="[0-9]+(\.[0-9]+)?(em|ex|px|in|cm|mm|pt|pc|%)?"/, newWidth);
80
- } else {
81
- // insert new width
82
- svgNode = svgNode.replace(">", " " + newWidth + ">");
83
- }
84
- }
85
-
86
- if (dimensions.height) {
87
-
88
- var newHeight = 'height="' + dimensions.height + '"';
89
-
90
- if (nodeDimensions && nodeDimensions.height) {
91
- // replace existing height
92
- svgNode = svgNode.replace(/height="[0-9]+(\.[0-9]+)?(em|ex|px|in|cm|mm|pt|pc|%)?"/, newHeight);
93
- } else {
94
- // insert new height
95
- svgNode = svgNode.replace(">", " " + newHeight + ">");
96
- }
97
- }
98
-
99
- // insert updated svg node
100
- return svgString.replace(/<svg(.*?)>/, svgNode);
101
- }
64
+ doc.attr.width = "" + dimensions.width;
65
+ doc.attr.height = "" + dimensions.height;
102
66
 
103
- return svgString;
67
+ return doc.toString();
104
68
  };
105
69
 
106
70
  module.exports = SVGMeasure;
package/src/textTools.js CHANGED
@@ -315,6 +315,13 @@ function measure(fontProvider, textArray, styleContextStack) {
315
315
  var preserveLeadingSpaces = getStyleProperty(item, styleContextStack, 'preserveLeadingSpaces', false);
316
316
  var preserveTrailingSpaces = getStyleProperty(item, styleContextStack, 'preserveTrailingSpaces', false);
317
317
  var opacity = getStyleProperty(item, styleContextStack, 'opacity', 1);
318
+ var sup = getStyleProperty(item, styleContextStack, 'sup', false);
319
+ var sub = getStyleProperty(item, styleContextStack, 'sub', false);
320
+
321
+ if ((sup || sub) && item.fontSize === undefined) {
322
+ // font size reduction taken from here: https://en.wikipedia.org/wiki/Subscript_and_superscript#Desktop_publishing
323
+ fontSize *= 0.58
324
+ }
318
325
 
319
326
  var font = fontProvider.provideFont(fontName, bold, italics);
320
327
 
@@ -352,6 +359,8 @@ function measure(fontProvider, textArray, styleContextStack) {
352
359
  item.linkToDestination = linkToDestination;
353
360
  item.noWrap = noWrap;
354
361
  item.opacity = opacity;
362
+ item.sup = sup;
363
+ item.sub = sub;
355
364
  });
356
365
 
357
366
  return normalized;
package/webpack.config.js CHANGED
@@ -1,5 +1,5 @@
1
1
  var path = require('path');
2
- var UglifyJsPlugin = require('uglifyjs-webpack-plugin');
2
+ var TerserPlugin = require('terser-webpack-plugin');
3
3
  var StringReplacePlugin = require("string-replace-webpack-plugin");
4
4
  var webpack = require('webpack');
5
5
  var pkg = require('./package.json');
@@ -42,7 +42,8 @@ module.exports = {
42
42
  return "var fs = require('fs');";
43
43
  }
44
44
  }
45
- ]})
45
+ ]
46
+ })
46
47
  },
47
48
  {
48
49
  test: /\.js$/,
@@ -80,7 +81,8 @@ module.exports = {
80
81
 
81
82
  /* temporary bugfix for FileSaver: added hack for mobile device support, see https://github.com/bpampuch/pdfmake/issues/1664 */
82
83
  /* waiting to merge and release PR https://github.com/eligrey/FileSaver.js/pull/533 */
83
- {test: /FileSaver.min.js$/, loader: StringReplacePlugin.replace({
84
+ {
85
+ test: /FileSaver.min.js$/, loader: StringReplacePlugin.replace({
84
86
  replacements: [
85
87
  {
86
88
  pattern: '"download"in HTMLAnchorElement.prototype',
@@ -88,26 +90,35 @@ module.exports = {
88
90
  return '(typeof HTMLAnchorElement !== "undefined" && "download" in HTMLAnchorElement.prototype)';
89
91
  }
90
92
  }
91
- ]})
93
+ ]
94
+ })
92
95
  },
93
96
 
94
- {enforce: 'post', test: /fontkit[/\\]index.js$/, loader: "transform-loader?brfs"},
95
- {enforce: 'post', test: /unicode-properties[/\\]index.js$/, loader: "transform-loader?brfs"},
96
- {enforce: 'post', test: /linebreak[/\\]src[/\\]linebreaker.js/, loader: "transform-loader?brfs"}
97
+ { enforce: 'post', test: /fontkit[/\\]index.js$/, loader: "transform-loader?brfs" },
98
+ { enforce: 'post', test: /unicode-properties[/\\]index.js$/, loader: "transform-loader?brfs" },
99
+ { enforce: 'post', test: /linebreak[/\\]src[/\\]linebreaker.js/, loader: "transform-loader?brfs" }
97
100
  ]
98
101
  },
99
102
  optimization: {
100
103
  minimizer: [
101
- new UglifyJsPlugin({
104
+ new TerserPlugin({
102
105
  include: /\.min\.js$/,
103
106
  sourceMap: true,
104
- uglifyOptions: {
107
+ extractComments: false,
108
+ terserOptions: {
109
+ format: {
110
+ preamble: banner,
111
+ comments: false,
112
+ },
113
+ output: {
114
+ preamble: banner,
115
+ comments: false,
116
+ },
105
117
  compress: {
106
118
  drop_console: true
107
119
  },
108
- mangle: {
109
- reserved: ['HeadTable', 'NameTable', 'CmapTable', 'HheaTable', 'MaxpTable', 'HmtxTable', 'PostTable', 'OS2Table', 'LocaTable', 'GlyfTable']
110
- }
120
+ keep_classnames: true,
121
+ keep_fnames: true
111
122
  }
112
123
  })
113
124
  ]