@digicole/pdfmake-rtl 1.2.0 → 2.1.1

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.
Files changed (100) hide show
  1. package/.vscode/tasks.json +17 -0
  2. package/CHANGELOG.md +83 -128
  3. package/LICENSE +22 -22
  4. package/README.md +188 -681
  5. package/build/fonts/Cairo/Cairo-Black.ttf +0 -0
  6. package/build/fonts/Cairo/Cairo-Bold.ttf +0 -0
  7. package/build/fonts/Cairo/Cairo-ExtraLight.ttf +0 -0
  8. package/build/fonts/Cairo/Cairo-Light.ttf +0 -0
  9. package/build/fonts/Cairo/Cairo-Regular.ttf +0 -0
  10. package/build/fonts/Cairo/Cairo-SemiBold.ttf +0 -0
  11. package/build/fonts/Cairo.js +27 -0
  12. package/build/fonts/Roboto/Roboto-Italic.ttf +0 -0
  13. package/build/fonts/Roboto/Roboto-Medium.ttf +0 -0
  14. package/build/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
  15. package/build/fonts/Roboto/Roboto-Regular.ttf +0 -0
  16. package/build/fonts/Roboto.js +27 -0
  17. package/build/pdfmake.js +63736 -71285
  18. package/build/pdfmake.js.map +1 -1
  19. package/build/pdfmake.min.js +2 -2
  20. package/build/pdfmake.min.js.map +1 -1
  21. package/build/standard-fonts/Courier.js +27 -0
  22. package/build/standard-fonts/Helvetica.js +27 -0
  23. package/build/standard-fonts/Symbol.js +21 -0
  24. package/build/standard-fonts/Times.js +27 -0
  25. package/build/standard-fonts/ZapfDingbats.js +21 -0
  26. package/build/vfs_fonts.js +11 -7
  27. package/build-vfs.js +44 -44
  28. package/fonts/Cairo/Cairo-Black.ttf +0 -0
  29. package/fonts/Cairo/Cairo-Bold.ttf +0 -0
  30. package/fonts/Cairo/Cairo-ExtraLight.ttf +0 -0
  31. package/fonts/Cairo/Cairo-Light.ttf +0 -0
  32. package/fonts/Cairo/Cairo-Regular.ttf +0 -0
  33. package/fonts/Cairo/Cairo-SemiBold.ttf +0 -0
  34. package/fonts/Cairo.js +8 -0
  35. package/fonts/Roboto/Roboto-Italic.ttf +0 -0
  36. package/fonts/Roboto/Roboto-Medium.ttf +0 -0
  37. package/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
  38. package/fonts/Roboto/Roboto-Regular.ttf +0 -0
  39. package/fonts/Roboto.js +8 -0
  40. package/index.js +26 -26
  41. package/package.json +42 -39
  42. package/src/3rd-party/svg-to-pdfkit/LICENSE +9 -9
  43. package/src/3rd-party/svg-to-pdfkit/source.js +229 -36
  44. package/src/3rd-party/svg-to-pdfkit.js +3 -3
  45. package/src/OutputDocument.js +64 -0
  46. package/src/OutputDocumentServer.js +32 -0
  47. package/src/PDFDocument.js +174 -0
  48. package/src/PageSize.js +53 -0
  49. package/src/Renderer.js +445 -0
  50. package/src/TextBreaker.js +168 -0
  51. package/src/TextInlines.js +263 -0
  52. package/src/URLResolver.js +43 -0
  53. package/src/base.js +70 -0
  54. package/src/browser-extensions/OutputDocumentBrowser.js +80 -0
  55. package/src/browser-extensions/fonts/Cairo.js +27 -0
  56. package/src/browser-extensions/fonts/Roboto.js +27 -0
  57. package/src/browser-extensions/index.js +61 -0
  58. package/src/browser-extensions/pdfMake.js +1 -355
  59. package/src/browser-extensions/standard-fonts/Courier.js +27 -0
  60. package/src/browser-extensions/standard-fonts/Helvetica.js +27 -0
  61. package/src/browser-extensions/standard-fonts/Symbol.js +21 -0
  62. package/src/browser-extensions/standard-fonts/Times.js +27 -0
  63. package/src/browser-extensions/standard-fonts/ZapfDingbats.js +21 -0
  64. package/src/browser-extensions/virtual-fs-cjs.js +1 -0
  65. package/src/columnCalculator.js +154 -157
  66. package/src/docMeasure.js +802 -810
  67. package/src/docPreprocessor.js +306 -273
  68. package/src/documentContext.js +345 -340
  69. package/src/elementWriter.js +736 -411
  70. package/src/helpers/node.js +136 -0
  71. package/src/helpers/tools.js +44 -0
  72. package/src/helpers/variableType.js +50 -0
  73. package/src/index.js +16 -0
  74. package/src/layoutBuilder.js +1393 -1197
  75. package/src/line.js +122 -104
  76. package/src/pageElementWriter.js +187 -174
  77. package/src/printer.js +370 -727
  78. package/src/qrEnc.js +796 -791
  79. package/src/rtlUtils.js +500 -485
  80. package/src/standardPageSizes.js +52 -54
  81. package/src/styleContextStack.js +208 -138
  82. package/src/svgMeasure.js +109 -70
  83. package/src/tableLayouts.js +100 -0
  84. package/src/tableProcessor.js +620 -606
  85. package/src/textDecorator.js +175 -157
  86. package/src/virtual-fs.js +66 -0
  87. package/standard-fonts/Courier.js +8 -0
  88. package/standard-fonts/Helvetica.js +8 -0
  89. package/standard-fonts/Symbol.js +5 -0
  90. package/standard-fonts/Times.js +8 -0
  91. package/standard-fonts/ZapfDingbats.js +5 -0
  92. package/index.html +0 -396
  93. package/src/browser-extensions/URLBrowserResolver.js +0 -96
  94. package/src/browser-extensions/virtual-fs.js +0 -55
  95. package/src/fontProvider.js +0 -68
  96. package/src/helpers.js +0 -138
  97. package/src/imageMeasure.js +0 -62
  98. package/src/pdfKitEngine.js +0 -21
  99. package/src/textTools.js +0 -391
  100. package/src/traversalTracker.js +0 -47
@@ -1,54 +1,52 @@
1
- 'use strict';
2
-
3
- module.exports = {
4
- '4A0': [4767.87, 6740.79],
5
- '2A0': [3370.39, 4767.87],
6
- A0: [2383.94, 3370.39],
7
- A1: [1683.78, 2383.94],
8
- A2: [1190.55, 1683.78],
9
- A3: [841.89, 1190.55],
10
- A4: [595.28, 841.89],
11
- A5: [419.53, 595.28],
12
- A6: [297.64, 419.53],
13
- A7: [209.76, 297.64],
14
- A8: [147.40, 209.76],
15
- A9: [104.88, 147.40],
16
- A10: [73.70, 104.88],
17
- B0: [2834.65, 4008.19],
18
- B1: [2004.09, 2834.65],
19
- B2: [1417.32, 2004.09],
20
- B3: [1000.63, 1417.32],
21
- B4: [708.66, 1000.63],
22
- B5: [498.90, 708.66],
23
- B6: [354.33, 498.90],
24
- B7: [249.45, 354.33],
25
- B8: [175.75, 249.45],
26
- B9: [124.72, 175.75],
27
- B10: [87.87, 124.72],
28
- C0: [2599.37, 3676.54],
29
- C1: [1836.85, 2599.37],
30
- C2: [1298.27, 1836.85],
31
- C3: [918.43, 1298.27],
32
- C4: [649.13, 918.43],
33
- C5: [459.21, 649.13],
34
- C6: [323.15, 459.21],
35
- C7: [229.61, 323.15],
36
- C8: [161.57, 229.61],
37
- C9: [113.39, 161.57],
38
- C10: [79.37, 113.39],
39
- RA0: [2437.80, 3458.27],
40
- RA1: [1729.13, 2437.80],
41
- RA2: [1218.90, 1729.13],
42
- RA3: [864.57, 1218.90],
43
- RA4: [609.45, 864.57],
44
- SRA0: [2551.18, 3628.35],
45
- SRA1: [1814.17, 2551.18],
46
- SRA2: [1275.59, 1814.17],
47
- SRA3: [907.09, 1275.59],
48
- SRA4: [637.80, 907.09],
49
- EXECUTIVE: [521.86, 756.00],
50
- FOLIO: [612.00, 936.00],
51
- LEGAL: [612.00, 1008.00],
52
- LETTER: [612.00, 792.00],
53
- TABLOID: [792.00, 1224.00]
54
- };
1
+ export default {
2
+ '4A0': [4767.87, 6740.79],
3
+ '2A0': [3370.39, 4767.87],
4
+ A0: [2383.94, 3370.39],
5
+ A1: [1683.78, 2383.94],
6
+ A2: [1190.55, 1683.78],
7
+ A3: [841.89, 1190.55],
8
+ A4: [595.28, 841.89],
9
+ A5: [419.53, 595.28],
10
+ A6: [297.64, 419.53],
11
+ A7: [209.76, 297.64],
12
+ A8: [147.40, 209.76],
13
+ A9: [104.88, 147.40],
14
+ A10: [73.70, 104.88],
15
+ B0: [2834.65, 4008.19],
16
+ B1: [2004.09, 2834.65],
17
+ B2: [1417.32, 2004.09],
18
+ B3: [1000.63, 1417.32],
19
+ B4: [708.66, 1000.63],
20
+ B5: [498.90, 708.66],
21
+ B6: [354.33, 498.90],
22
+ B7: [249.45, 354.33],
23
+ B8: [175.75, 249.45],
24
+ B9: [124.72, 175.75],
25
+ B10: [87.87, 124.72],
26
+ C0: [2599.37, 3676.54],
27
+ C1: [1836.85, 2599.37],
28
+ C2: [1298.27, 1836.85],
29
+ C3: [918.43, 1298.27],
30
+ C4: [649.13, 918.43],
31
+ C5: [459.21, 649.13],
32
+ C6: [323.15, 459.21],
33
+ C7: [229.61, 323.15],
34
+ C8: [161.57, 229.61],
35
+ C9: [113.39, 161.57],
36
+ C10: [79.37, 113.39],
37
+ RA0: [2437.80, 3458.27],
38
+ RA1: [1729.13, 2437.80],
39
+ RA2: [1218.90, 1729.13],
40
+ RA3: [864.57, 1218.90],
41
+ RA4: [609.45, 864.57],
42
+ SRA0: [2551.18, 3628.35],
43
+ SRA1: [1814.17, 2551.18],
44
+ SRA2: [1275.59, 1814.17],
45
+ SRA3: [907.09, 1275.59],
46
+ SRA4: [637.80, 907.09],
47
+ EXECUTIVE: [521.86, 756.00],
48
+ FOLIO: [612.00, 936.00],
49
+ LEGAL: [612.00, 1008.00],
50
+ LETTER: [612.00, 792.00],
51
+ TABLOID: [792.00, 1224.00]
52
+ };
@@ -1,138 +1,208 @@
1
- 'use strict';
2
-
3
- var isString = require('./helpers').isString;
4
- var isArray = require('./helpers').isArray;
5
- var isUndefined = require('./helpers').isUndefined;
6
- var isNull = require('./helpers').isNull;
7
-
8
- /**
9
- * Creates an instance of StyleContextStack used for style inheritance and style overrides
10
- *
11
- * @constructor
12
- * @this {StyleContextStack}
13
- * @param {Object} named styles dictionary
14
- * @param {Object} optional default style definition
15
- */
16
- function StyleContextStack(styleDictionary, defaultStyle) {
17
- this.defaultStyle = defaultStyle || {};
18
- this.styleDictionary = styleDictionary;
19
- this.styleOverrides = [];
20
- }
21
-
22
- /**
23
- * Creates cloned version of current stack
24
- * @return {StyleContextStack} current stack snapshot
25
- */
26
- StyleContextStack.prototype.clone = function () {
27
- var stack = new StyleContextStack(this.styleDictionary, this.defaultStyle);
28
-
29
- this.styleOverrides.forEach(function (item) {
30
- stack.styleOverrides.push(item);
31
- });
32
-
33
- return stack;
34
- };
35
-
36
- /**
37
- * Pushes style-name or style-overrides-object onto the stack for future evaluation
38
- *
39
- * @param {String|Object} styleNameOrOverride style-name (referring to styleDictionary) or
40
- * a new dictionary defining overriding properties
41
- */
42
- StyleContextStack.prototype.push = function (styleNameOrOverride) {
43
- this.styleOverrides.push(styleNameOrOverride);
44
- };
45
-
46
- /**
47
- * Removes last style-name or style-overrides-object from the stack
48
- *
49
- * @param {Number} howMany - optional number of elements to be popped (if not specified,
50
- * one element will be removed from the stack)
51
- */
52
- StyleContextStack.prototype.pop = function (howMany) {
53
- howMany = howMany || 1;
54
-
55
- while (howMany-- > 0) {
56
- this.styleOverrides.pop();
57
- }
58
- };
59
-
60
- /**
61
- * Creates a set of named styles or/and a style-overrides-object based on the item,
62
- * pushes those elements onto the stack for future evaluation and returns the number
63
- * of elements pushed, so they can be easily poped then.
64
- *
65
- * @param {Object} item - an object with optional style property and/or style overrides
66
- * @return the number of items pushed onto the stack
67
- */
68
- StyleContextStack.prototype.autopush = function (item) {
69
- if (isString(item)) {
70
- return 0;
71
- }
72
-
73
- var styleNames = [];
74
-
75
- if (item.style) {
76
- if (isArray(item.style)) {
77
- styleNames = item.style;
78
- } else {
79
- styleNames = [item.style];
80
- }
81
- }
82
-
83
- for (var i = 0, l = styleNames.length; i < l; i++) {
84
- this.push(styleNames[i]);
85
- }
86
-
87
- // rather than spend significant time making a styleOverrideObject, just add item
88
- this.push(item);
89
- return styleNames.length + 1;
90
- };
91
-
92
- /**
93
- * Automatically pushes elements onto the stack, using autopush based on item,
94
- * executes callback and then pops elements back. Returns value returned by callback
95
- *
96
- * @param {Object} item - an object with optional style property and/or style overrides
97
- * @param {Function} function to be called between autopush and pop
98
- * @return {Object} value returned by callback
99
- */
100
- StyleContextStack.prototype.auto = function (item, callback) {
101
- var pushedItems = this.autopush(item);
102
- var result = callback();
103
-
104
- if (pushedItems > 0) {
105
- this.pop(pushedItems);
106
- }
107
-
108
- return result;
109
- };
110
-
111
- /**
112
- * Evaluates stack and returns value of a named property
113
- *
114
- * @param {String} property - property name
115
- * @return property value or null if not found
116
- */
117
- StyleContextStack.prototype.getProperty = function (property) {
118
- if (this.styleOverrides) {
119
- for (var i = this.styleOverrides.length - 1; i >= 0; i--) {
120
- var item = this.styleOverrides[i];
121
-
122
- if (isString(item)) {
123
- // named-style-override
124
- var style = this.styleDictionary[item];
125
- if (style && !isUndefined(style[property]) && !isNull(style[property])) {
126
- return style[property];
127
- }
128
- } else if (!isUndefined(item[property]) && !isNull(item[property])) {
129
- // style-overrides-object
130
- return item[property];
131
- }
132
- }
133
- }
134
-
135
- return this.defaultStyle && this.defaultStyle[property];
136
- };
137
-
138
- module.exports = StyleContextStack;
1
+ import { isString, isValue } from './helpers/variableType';
2
+
3
+ /**
4
+ * Used for style inheritance and style overrides
5
+ */
6
+ class StyleContextStack {
7
+
8
+ /**
9
+ * @param {object} styleDictionary named styles dictionary
10
+ * @param {object} defaultStyle optional default style definition
11
+ */
12
+ constructor(styleDictionary, defaultStyle = {}) {
13
+ this.styleDictionary = styleDictionary;
14
+ this.defaultStyle = defaultStyle;
15
+ this.styleOverrides = [];
16
+ }
17
+
18
+ /**
19
+ * Creates cloned version of current stack
20
+ *
21
+ * @returns {StyleContextStack} current stack snapshot
22
+ */
23
+ clone() {
24
+ let stack = new StyleContextStack(this.styleDictionary, this.defaultStyle);
25
+
26
+ this.styleOverrides.forEach(item => {
27
+ stack.styleOverrides.push(item);
28
+ });
29
+
30
+ return stack;
31
+ }
32
+
33
+ /**
34
+ * Pushes style-name or style-overrides-object onto the stack for future evaluation
35
+ *
36
+ * @param {string|object} styleNameOrOverride style-name (referring to styleDictionary) or
37
+ * a new dictionary defining overriding properties
38
+ */
39
+ push(styleNameOrOverride) {
40
+ this.styleOverrides.push(styleNameOrOverride);
41
+ }
42
+
43
+ /**
44
+ * Removes last style-name or style-overrides-object from the stack
45
+ *
46
+ * @param {number} howMany optional number of elements to be popped (if not specified,
47
+ * one element will be removed from the stack)
48
+ */
49
+ pop(howMany = 1) {
50
+ while (howMany-- > 0) {
51
+ this.styleOverrides.pop();
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Creates a set of named styles or/and a style-overrides-object based on the item,
57
+ * pushes those elements onto the stack for future evaluation and returns the number
58
+ * of elements pushed, so they can be easily popped then.
59
+ *
60
+ * @param {object} item - an object with optional style property and/or style overrides
61
+ * @returns {number} the number of items pushed onto the stack
62
+ */
63
+ autopush(item) {
64
+ if (isString(item)) {
65
+ return 0;
66
+ }
67
+
68
+ if (typeof item.section !== 'undefined') { // section node not support style overrides
69
+ return 0;
70
+ }
71
+
72
+ let styleNames = [];
73
+
74
+ if (item.style) {
75
+ if (Array.isArray(item.style)) {
76
+ styleNames = item.style;
77
+ } else {
78
+ styleNames = [item.style];
79
+ }
80
+ }
81
+
82
+ for (let i = 0, l = styleNames.length; i < l; i++) {
83
+ this.push(styleNames[i]);
84
+ }
85
+
86
+ // rather than spend significant time making a styleOverrideObject, just add item
87
+ this.push(item);
88
+ return styleNames.length + 1;
89
+ }
90
+
91
+ /**
92
+ * Automatically pushes elements onto the stack, using autopush based on item,
93
+ * executes callback and then pops elements back. Returns value returned by callback
94
+ *
95
+ * @param {object} item - an object with optional style property and/or style overrides
96
+ * @param {Function} callback to be called between autopush and pop
97
+ * @returns {object} value returned by callback
98
+ */
99
+ auto(item, callback) {
100
+ let pushedItems = this.autopush(item);
101
+ let result = callback();
102
+
103
+ if (pushedItems > 0) {
104
+ this.pop(pushedItems);
105
+ }
106
+
107
+ return result;
108
+ }
109
+
110
+ /**
111
+ * Evaluates stack and returns value of a named property
112
+ *
113
+ * @param {string} property - property name
114
+ * @returns {?any} property value or null if not found
115
+ */
116
+ getProperty(property) {
117
+
118
+ const getStylePropertyFromStyle = (styleName, property, visited = new Set()) => {
119
+ if (visited.has(styleName)) {
120
+ return undefined;
121
+ }
122
+ visited.add(styleName);
123
+
124
+ const style = this.styleDictionary[styleName];
125
+ if (!style) {
126
+ return undefined;
127
+ }
128
+
129
+ if (isValue(style[property])) {
130
+ return style[property];
131
+ }
132
+
133
+ if (style.extends) {
134
+ let parents = Array.isArray(style.extends) ? style.extends : [style.extends];
135
+ for (let i = parents.length - 1; i >= 0; i--) {
136
+ let value = getStylePropertyFromStyle(parents[i], property, visited);
137
+ if (isValue(value)) {
138
+ return value;
139
+ }
140
+ }
141
+ }
142
+
143
+ return undefined;
144
+ };
145
+
146
+ if (this.styleOverrides) {
147
+ for (let i = this.styleOverrides.length - 1; i >= 0; i--) {
148
+ let item = this.styleOverrides[i];
149
+
150
+ if (isString(item)) { // named-style-override
151
+ let value = getStylePropertyFromStyle(item, property);
152
+ if (isValue(value)) {
153
+ return value;
154
+ }
155
+ } else if (isValue(item[property])) { // style-overrides-object
156
+ return item[property];
157
+ }
158
+ }
159
+ }
160
+
161
+ return this.defaultStyle && this.defaultStyle[property];
162
+ }
163
+
164
+ /**
165
+ * @param {object} item
166
+ * @param {StyleContextStack} styleContextStack
167
+ * @param {string} property
168
+ * @param {any} defaultValue
169
+ * @returns {any}
170
+ */
171
+ static getStyleProperty(item, styleContextStack, property, defaultValue) {
172
+ let value;
173
+
174
+ if (isValue(item[property])) { // item defines this property
175
+ return item[property];
176
+ }
177
+
178
+ if (!styleContextStack) {
179
+ return defaultValue;
180
+ }
181
+
182
+ styleContextStack.auto(item, () => {
183
+ value = styleContextStack.getProperty(property);
184
+ });
185
+
186
+ return isValue(value) ? value : defaultValue;
187
+ }
188
+
189
+ /**
190
+ * @param {object} source
191
+ * @param {object} destination
192
+ * @returns {object}
193
+ */
194
+ static copyStyle(source = {}, destination = {}) {
195
+ // TODO: default style to source
196
+
197
+ for (let key in source) {
198
+ if (key != 'text' && source.hasOwnProperty(key)) {
199
+ destination[key] = source[key];
200
+ }
201
+ }
202
+
203
+ return destination;
204
+ }
205
+
206
+ }
207
+
208
+ export default StyleContextStack;
package/src/svgMeasure.js CHANGED
@@ -1,70 +1,109 @@
1
- 'use strict';
2
-
3
- var xmldoc = require('xmldoc');
4
-
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
- }
13
-
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);
22
- }
23
-
24
- if (doc.name !== "svg") {
25
- throw new Error('SVGMeasure: expected <svg> document');
26
- }
27
-
28
- return doc;
29
- }
30
-
31
- function SVGMeasure() {
32
- }
33
-
34
- SVGMeasure.prototype.measureSVG = function (svgString) {
35
-
36
- var doc = parseSVG(svgString);
37
-
38
- var docWidth = stripUnits(doc.attr.width);
39
- var docHeight = stripUnits(doc.attr.height);
40
-
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 + "'");
45
- }
46
- if (docWidth == undefined) {
47
- docWidth = stripUnits(viewBoxParts[2]);
48
- }
49
- if (docHeight == undefined) {
50
- docHeight = stripUnits(viewBoxParts[3]);
51
- }
52
- }
53
-
54
- return {
55
- width: docWidth,
56
- height: docHeight
57
- };
58
- };
59
-
60
- SVGMeasure.prototype.writeDimensions = function (svgString, dimensions) {
61
-
62
- var doc = parseSVG(svgString);
63
-
64
- doc.attr.width = "" + dimensions.width;
65
- doc.attr.height = "" + dimensions.height;
66
-
67
- return doc.toString();
68
- };
69
-
70
- module.exports = SVGMeasure;
1
+ import { XmlDocument } from "xmldoc";
2
+ import { isString } from './helpers/variableType';
3
+
4
+ /**
5
+ * Strip unit postfix, parse number, but return undefined instead of NaN for bad input
6
+ *
7
+ * @param {string} textVal
8
+ * @returns {?number}
9
+ */
10
+ const stripUnits = textVal => {
11
+ let n = parseFloat(textVal);
12
+ if (typeof n !== 'number' || isNaN(n)) {
13
+ return undefined;
14
+ }
15
+ return n;
16
+ };
17
+
18
+ /**
19
+ * Make sure it's valid XML and the root tag is <svg/>, returns xmldoc DOM
20
+ *
21
+ * @param {string} svgString
22
+ * @returns {object}
23
+ */
24
+ const parseSVG = (svgString) => {
25
+ let doc;
26
+
27
+ try {
28
+ doc = new XmlDocument(svgString);
29
+ } catch (err) {
30
+ throw new Error('Invalid svg document (' + err + ')');
31
+ }
32
+
33
+ if (doc.name !== "svg") {
34
+ throw new Error('Invalid svg document (expected <svg>)');
35
+ }
36
+
37
+ return doc;
38
+ };
39
+
40
+ class SVGMeasure {
41
+ constructor() {
42
+
43
+ }
44
+
45
+ measureSVG(svg) {
46
+ let width, height, viewBox;
47
+
48
+ if (isString(svg)) {
49
+ let doc = parseSVG(svg);
50
+
51
+ width = doc.attr.width;
52
+ height = doc.attr.height;
53
+ viewBox = doc.attr.viewBox;
54
+ } else if (typeof SVGElement !== 'undefined' && svg instanceof SVGElement && typeof getComputedStyle === 'function') {
55
+ width = svg.getAttribute("width");
56
+ height = svg.getAttribute("height");
57
+ viewBox = svg.getAttribute("viewBox");
58
+ } else {
59
+ throw new Error('Invalid SVG document');
60
+ }
61
+
62
+ let docWidth = stripUnits(width);
63
+ let docHeight = stripUnits(height);
64
+
65
+ if ((docWidth === undefined || docHeight === undefined) && typeof viewBox === 'string') {
66
+ let viewBoxParts = viewBox.split(/[,\s]+/);
67
+ if (viewBoxParts.length !== 4) {
68
+ throw new Error("Unexpected svg viewBox format, should have 4 entries but found: '" + viewBox + "'");
69
+ }
70
+ if (docWidth === undefined) {
71
+ docWidth = stripUnits(viewBoxParts[2]);
72
+ }
73
+ if (docHeight === undefined) {
74
+ docHeight = stripUnits(viewBoxParts[3]);
75
+ }
76
+ }
77
+
78
+ return {
79
+ width: docWidth,
80
+ height: docHeight
81
+ };
82
+ }
83
+
84
+ writeDimensions(svg, dimensions) {
85
+ if (isString(svg)) {
86
+ let doc = parseSVG(svg);
87
+
88
+ if (typeof doc.attr.viewBox !== 'string') {
89
+ doc.attr.viewBox = `0 0 ${stripUnits(doc.attr.width)} ${stripUnits(doc.attr.height)}`;
90
+ }
91
+
92
+ doc.attr.width = "" + dimensions.width;
93
+ doc.attr.height = "" + dimensions.height;
94
+
95
+ return doc.toString();
96
+ }
97
+
98
+ if (!svg.hasAttribute('viewBox')) {
99
+ svg.setAttribute('viewBox', `0 0 ${stripUnits(svg.getAttribute('width'))} ${stripUnits(svg.getAttribute('height'))}`);
100
+ }
101
+
102
+ svg.setAttribute('width', "" + dimensions.width);
103
+ svg.setAttribute('height', "" + dimensions.height);
104
+
105
+ return svg;
106
+ }
107
+ }
108
+
109
+ export default SVGMeasure;