pdfmake 0.3.2 → 0.3.4

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 (94) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/LICENSE +21 -21
  3. package/README.md +75 -78
  4. package/build/fonts/Roboto/Roboto-Italic.ttf +0 -0
  5. package/build/fonts/Roboto/Roboto-Medium.ttf +0 -0
  6. package/build/fonts/Roboto/Roboto-MediumItalic.ttf +0 -0
  7. package/build/fonts/Roboto/Roboto-Regular.ttf +0 -0
  8. package/build/fonts/Roboto.js +27 -0
  9. package/build/pdfmake.js +64813 -64584
  10. package/build/pdfmake.js.map +1 -1
  11. package/build/pdfmake.min.js +2 -2
  12. package/build/pdfmake.min.js.map +1 -1
  13. package/build/standard-fonts/Courier.js +27 -27
  14. package/build/standard-fonts/Helvetica.js +27 -27
  15. package/build/standard-fonts/Symbol.js +21 -21
  16. package/build/standard-fonts/Times.js +27 -27
  17. package/build/standard-fonts/ZapfDingbats.js +21 -21
  18. package/build/vfs_fonts.js +5 -5
  19. package/build-vfs.js +44 -44
  20. package/fonts/Roboto.js +8 -8
  21. package/js/3rd-party/svg-to-pdfkit/source.js +1 -1
  22. package/js/DocMeasure.js +11 -6
  23. package/js/DocumentContext.js +8 -3
  24. package/js/ElementWriter.js +42 -16
  25. package/js/LayoutBuilder.js +144 -78
  26. package/js/Line.js +16 -16
  27. package/js/OutputDocument.js +10 -10
  28. package/js/OutputDocumentServer.js +3 -3
  29. package/js/PDFDocument.js +3 -3
  30. package/js/PageElementWriter.js +15 -9
  31. package/js/Printer.js +28 -28
  32. package/js/Renderer.js +40 -8
  33. package/js/SVGMeasure.js +10 -10
  34. package/js/StyleContextStack.js +74 -51
  35. package/js/TableProcessor.js +14 -0
  36. package/js/TextBreaker.js +17 -17
  37. package/js/TextDecorator.js +12 -3
  38. package/js/TextInlines.js +34 -33
  39. package/js/base.js +4 -4
  40. package/js/browser-extensions/OutputDocumentBrowser.js +24 -24
  41. package/js/columnCalculator.js +2 -2
  42. package/js/helpers/node.js +47 -23
  43. package/js/helpers/variableType.js +18 -18
  44. package/js/qrEnc.js +38 -38
  45. package/js/virtual-fs.js +11 -11
  46. package/package.json +12 -12
  47. package/src/3rd-party/svg-to-pdfkit/LICENSE +9 -9
  48. package/src/3rd-party/svg-to-pdfkit/source.js +2745 -2745
  49. package/src/3rd-party/svg-to-pdfkit.js +3 -3
  50. package/src/DocMeasure.js +745 -738
  51. package/src/DocPreprocessor.js +283 -283
  52. package/src/DocumentContext.js +345 -338
  53. package/src/ElementWriter.js +441 -417
  54. package/src/LayoutBuilder.js +1336 -1258
  55. package/src/Line.js +114 -114
  56. package/src/OutputDocument.js +64 -64
  57. package/src/OutputDocumentServer.js +32 -32
  58. package/src/PDFDocument.js +174 -174
  59. package/src/PageElementWriter.js +187 -179
  60. package/src/PageSize.js +53 -53
  61. package/src/Printer.js +306 -306
  62. package/src/Renderer.js +445 -409
  63. package/src/SVGMeasure.js +109 -109
  64. package/src/StyleContextStack.js +208 -179
  65. package/src/TableProcessor.js +620 -602
  66. package/src/TextBreaker.js +168 -168
  67. package/src/TextDecorator.js +175 -161
  68. package/src/TextInlines.js +224 -223
  69. package/src/URLResolver.js +43 -43
  70. package/src/base.js +70 -70
  71. package/src/browser-extensions/OutputDocumentBrowser.js +80 -80
  72. package/src/browser-extensions/fonts/Roboto.js +27 -27
  73. package/src/browser-extensions/index.js +55 -55
  74. package/src/browser-extensions/pdfMake.js +1 -1
  75. package/src/browser-extensions/standard-fonts/Courier.js +27 -27
  76. package/src/browser-extensions/standard-fonts/Helvetica.js +27 -27
  77. package/src/browser-extensions/standard-fonts/Symbol.js +21 -21
  78. package/src/browser-extensions/standard-fonts/Times.js +27 -27
  79. package/src/browser-extensions/standard-fonts/ZapfDingbats.js +21 -21
  80. package/src/browser-extensions/virtual-fs-cjs.js +1 -1
  81. package/src/columnCalculator.js +154 -154
  82. package/src/helpers/node.js +134 -110
  83. package/src/helpers/tools.js +44 -44
  84. package/src/helpers/variableType.js +50 -50
  85. package/src/index.js +16 -16
  86. package/src/qrEnc.js +796 -796
  87. package/src/standardPageSizes.js +52 -52
  88. package/src/tableLayouts.js +100 -100
  89. package/src/virtual-fs.js +66 -66
  90. package/standard-fonts/Courier.js +8 -8
  91. package/standard-fonts/Helvetica.js +8 -8
  92. package/standard-fonts/Symbol.js +5 -5
  93. package/standard-fonts/Times.js +8 -8
  94. package/standard-fonts/ZapfDingbats.js +5 -5
@@ -1,27 +1,27 @@
1
- var fs = require('fs');
2
-
3
- var fontContainer = {
4
- vfs: {
5
- 'data/Times-Roman.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Times-Roman.afm', 'utf8'), encoding: 'utf8' },
6
- 'data/Times-Bold.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Times-Bold.afm', 'utf8'), encoding: 'utf8' },
7
- 'data/Times-Italic.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Times-Italic.afm', 'utf8'), encoding: 'utf8' },
8
- 'data/Times-BoldItalic.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Times-BoldItalic.afm', 'utf8'), encoding: 'utf8' }
9
- },
10
- fonts: {
11
- Times: {
12
- normal: 'Times-Roman',
13
- bold: 'Times-Bold',
14
- italics: 'Times-Italic',
15
- bolditalics: 'Times-BoldItalic'
16
- }
17
- }
18
- };
19
-
20
- var _global = typeof window === 'object' ? window : typeof global === 'object' ? global : typeof self === 'object' ? self : this;
21
- if (typeof _global.pdfMake !== 'undefined' && typeof _global.pdfMake.addFontContainer !== 'undefined') {
22
- _global.pdfMake.addFontContainer(fontContainer);
23
- }
24
-
25
- if (typeof module !== 'undefined') {
26
- module.exports = fontContainer;
27
- }
1
+ var fs = require('fs');
2
+
3
+ var fontContainer = {
4
+ vfs: {
5
+ 'data/Times-Roman.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Times-Roman.afm', 'utf8'), encoding: 'utf8' },
6
+ 'data/Times-Bold.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Times-Bold.afm', 'utf8'), encoding: 'utf8' },
7
+ 'data/Times-Italic.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Times-Italic.afm', 'utf8'), encoding: 'utf8' },
8
+ 'data/Times-BoldItalic.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/Times-BoldItalic.afm', 'utf8'), encoding: 'utf8' }
9
+ },
10
+ fonts: {
11
+ Times: {
12
+ normal: 'Times-Roman',
13
+ bold: 'Times-Bold',
14
+ italics: 'Times-Italic',
15
+ bolditalics: 'Times-BoldItalic'
16
+ }
17
+ }
18
+ };
19
+
20
+ var _global = typeof window === 'object' ? window : typeof global === 'object' ? global : typeof self === 'object' ? self : this;
21
+ if (typeof _global.pdfMake !== 'undefined' && typeof _global.pdfMake.addFontContainer !== 'undefined') {
22
+ _global.pdfMake.addFontContainer(fontContainer);
23
+ }
24
+
25
+ if (typeof module !== 'undefined') {
26
+ module.exports = fontContainer;
27
+ }
@@ -1,21 +1,21 @@
1
- var fs = require('fs');
2
-
3
- var fontContainer = {
4
- vfs: {
5
- 'data/ZapfDingbats.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/ZapfDingbats.afm', 'utf8'), encoding: 'utf8' }
6
- },
7
- fonts: {
8
- ZapfDingbats: {
9
- normal: 'ZapfDingbats'
10
- }
11
- }
12
- };
13
-
14
- var _global = typeof window === 'object' ? window : typeof global === 'object' ? global : typeof self === 'object' ? self : this;
15
- if (typeof _global.pdfMake !== 'undefined' && typeof _global.pdfMake.addFontContainer !== 'undefined') {
16
- _global.pdfMake.addFontContainer(fontContainer);
17
- }
18
-
19
- if (typeof module !== 'undefined') {
20
- module.exports = fontContainer;
21
- }
1
+ var fs = require('fs');
2
+
3
+ var fontContainer = {
4
+ vfs: {
5
+ 'data/ZapfDingbats.afm': { data: fs.readFileSync(__dirname + '/../../../node_modules/pdfkit/js/data/ZapfDingbats.afm', 'utf8'), encoding: 'utf8' }
6
+ },
7
+ fonts: {
8
+ ZapfDingbats: {
9
+ normal: 'ZapfDingbats'
10
+ }
11
+ }
12
+ };
13
+
14
+ var _global = typeof window === 'object' ? window : typeof global === 'object' ? global : typeof self === 'object' ? self : this;
15
+ if (typeof _global.pdfMake !== 'undefined' && typeof _global.pdfMake.addFontContainer !== 'undefined') {
16
+ _global.pdfMake.addFontContainer(fontContainer);
17
+ }
18
+
19
+ if (typeof module !== 'undefined') {
20
+ module.exports = fontContainer;
21
+ }
@@ -1 +1 @@
1
- module.exports = require('../virtual-fs').default;
1
+ module.exports = require('../virtual-fs').default;
@@ -1,154 +1,154 @@
1
- import { isString } from './helpers/variableType';
2
-
3
- function buildColumnWidths(columns, availableWidth, offsetTotal = 0, tableNode) {
4
- let autoColumns = [];
5
- let autoMin = 0;
6
- let autoMax = 0;
7
- let starColumns = [];
8
- let starMaxMin = 0;
9
- let starMaxMax = 0;
10
- let fixedColumns = [];
11
- let initial_availableWidth = availableWidth;
12
-
13
- columns.forEach(column => {
14
- if (isAutoColumn(column)) {
15
- autoColumns.push(column);
16
- autoMin += column._minWidth;
17
- autoMax += column._maxWidth;
18
- } else if (isStarColumn(column)) {
19
- starColumns.push(column);
20
- starMaxMin = Math.max(starMaxMin, column._minWidth);
21
- starMaxMax = Math.max(starMaxMax, column._maxWidth);
22
- } else {
23
- fixedColumns.push(column);
24
- }
25
- });
26
-
27
- fixedColumns.forEach((col, colIndex) => {
28
- // width specified as %
29
- if (isString(col.width) && /\d+%/.test(col.width)) {
30
- // In tables we have to take into consideration the reserved width for paddings and borders
31
- let reservedWidth = 0;
32
- if (tableNode) {
33
- const paddingLeft = tableNode._layout.paddingLeft(colIndex, tableNode);
34
- const paddingRight = tableNode._layout.paddingRight(colIndex, tableNode);
35
- const borderLeft = tableNode._layout.vLineWidth(colIndex, tableNode);
36
- const borderRight = tableNode._layout.vLineWidth(colIndex + 1, tableNode);
37
- if (colIndex === 0) {
38
- // first column assumes whole borderLeft and half of border right
39
- reservedWidth = paddingLeft + paddingRight + borderLeft + (borderRight / 2);
40
-
41
- } else if (colIndex === fixedColumns.length - 1) {
42
- // last column assumes whole borderRight and half of border left
43
- reservedWidth = paddingLeft + paddingRight + (borderLeft / 2) + borderRight;
44
-
45
- } else {
46
- // Columns in the middle assume half of each border
47
- reservedWidth = paddingLeft + paddingRight + (borderLeft / 2) + (borderRight / 2);
48
- }
49
- }
50
- const totalAvailableWidth = initial_availableWidth + offsetTotal;
51
- col.width = (parseFloat(col.width) * totalAvailableWidth / 100) - reservedWidth;
52
- }
53
- if (col.width < (col._minWidth) && col.elasticWidth) {
54
- col._calcWidth = col._minWidth;
55
- } else {
56
- col._calcWidth = col.width;
57
- }
58
-
59
- availableWidth -= col._calcWidth;
60
- });
61
-
62
- // http://www.freesoft.org/CIE/RFC/1942/18.htm
63
- // http://www.w3.org/TR/CSS2/tables.html#width-layout
64
- // http://dev.w3.org/csswg/css3-tables-algorithms/Overview.src.htm
65
- let minW = autoMin + starMaxMin * starColumns.length;
66
- let maxW = autoMax + starMaxMax * starColumns.length;
67
- if (minW >= availableWidth) {
68
- // case 1 - there's no way to fit all columns within available width
69
- // that's actually pretty bad situation with PDF as we have no horizontal scroll
70
- // no easy workaround (unless we decide, in the future, to split single words)
71
- // currently we simply use minWidths for all columns
72
- autoColumns.forEach(col => {
73
- col._calcWidth = col._minWidth;
74
- });
75
-
76
- starColumns.forEach(col => {
77
- col._calcWidth = starMaxMin; // starMaxMin already contains padding
78
- });
79
- } else {
80
- if (maxW < availableWidth) {
81
- // case 2 - we can fit rest of the table within available space
82
- autoColumns.forEach(col => {
83
- col._calcWidth = col._maxWidth;
84
- availableWidth -= col._calcWidth;
85
- });
86
- } else {
87
- // maxW is too large, but minW fits within available width
88
- let W = availableWidth - minW;
89
- let D = maxW - minW;
90
-
91
- autoColumns.forEach(col => {
92
- let d = col._maxWidth - col._minWidth;
93
- col._calcWidth = col._minWidth + d * W / D;
94
- availableWidth -= col._calcWidth;
95
- });
96
- }
97
-
98
- if (starColumns.length > 0) {
99
- let starSize = availableWidth / starColumns.length;
100
-
101
- starColumns.forEach(col => {
102
- col._calcWidth = starSize;
103
- });
104
- }
105
- }
106
- }
107
-
108
- function isAutoColumn(column) {
109
- return column.width === 'auto';
110
- }
111
-
112
- function isStarColumn(column) {
113
- return column.width === null || column.width === undefined || column.width === '*' || column.width === 'star';
114
- }
115
-
116
- //TODO: refactor and reuse in measureTable
117
- function measureMinMax(columns) {
118
- let result = { min: 0, max: 0 };
119
- let maxStar = { min: 0, max: 0 };
120
- let starCount = 0;
121
-
122
- for (let i = 0, l = columns.length; i < l; i++) {
123
- let c = columns[i];
124
-
125
- if (isStarColumn(c)) {
126
- maxStar.min = Math.max(maxStar.min, c._minWidth);
127
- maxStar.max = Math.max(maxStar.max, c._maxWidth);
128
- starCount++;
129
- } else if (isAutoColumn(c)) {
130
- result.min += c._minWidth;
131
- result.max += c._maxWidth;
132
- } else {
133
- result.min += ((c.width !== undefined && c.width) || c._minWidth);
134
- result.max += ((c.width !== undefined && c.width) || c._maxWidth);
135
- }
136
- }
137
-
138
- if (starCount) {
139
- result.min += starCount * maxStar.min;
140
- result.max += starCount * maxStar.max;
141
- }
142
-
143
- return result;
144
- }
145
-
146
- /**
147
- * Calculates column widths
148
- */
149
- export default {
150
- buildColumnWidths: buildColumnWidths,
151
- measureMinMax: measureMinMax,
152
- isAutoColumn: isAutoColumn,
153
- isStarColumn: isStarColumn
154
- };
1
+ import { isString } from './helpers/variableType';
2
+
3
+ function buildColumnWidths(columns, availableWidth, offsetTotal = 0, tableNode) {
4
+ let autoColumns = [];
5
+ let autoMin = 0;
6
+ let autoMax = 0;
7
+ let starColumns = [];
8
+ let starMaxMin = 0;
9
+ let starMaxMax = 0;
10
+ let fixedColumns = [];
11
+ let initial_availableWidth = availableWidth;
12
+
13
+ columns.forEach(column => {
14
+ if (isAutoColumn(column)) {
15
+ autoColumns.push(column);
16
+ autoMin += column._minWidth;
17
+ autoMax += column._maxWidth;
18
+ } else if (isStarColumn(column)) {
19
+ starColumns.push(column);
20
+ starMaxMin = Math.max(starMaxMin, column._minWidth);
21
+ starMaxMax = Math.max(starMaxMax, column._maxWidth);
22
+ } else {
23
+ fixedColumns.push(column);
24
+ }
25
+ });
26
+
27
+ fixedColumns.forEach((col, colIndex) => {
28
+ // width specified as %
29
+ if (isString(col.width) && /\d+%/.test(col.width)) {
30
+ // In tables we have to take into consideration the reserved width for paddings and borders
31
+ let reservedWidth = 0;
32
+ if (tableNode) {
33
+ const paddingLeft = tableNode._layout.paddingLeft(colIndex, tableNode);
34
+ const paddingRight = tableNode._layout.paddingRight(colIndex, tableNode);
35
+ const borderLeft = tableNode._layout.vLineWidth(colIndex, tableNode);
36
+ const borderRight = tableNode._layout.vLineWidth(colIndex + 1, tableNode);
37
+ if (colIndex === 0) {
38
+ // first column assumes whole borderLeft and half of border right
39
+ reservedWidth = paddingLeft + paddingRight + borderLeft + (borderRight / 2);
40
+
41
+ } else if (colIndex === fixedColumns.length - 1) {
42
+ // last column assumes whole borderRight and half of border left
43
+ reservedWidth = paddingLeft + paddingRight + (borderLeft / 2) + borderRight;
44
+
45
+ } else {
46
+ // Columns in the middle assume half of each border
47
+ reservedWidth = paddingLeft + paddingRight + (borderLeft / 2) + (borderRight / 2);
48
+ }
49
+ }
50
+ const totalAvailableWidth = initial_availableWidth + offsetTotal;
51
+ col.width = (parseFloat(col.width) * totalAvailableWidth / 100) - reservedWidth;
52
+ }
53
+ if (col.width < (col._minWidth) && col.elasticWidth) {
54
+ col._calcWidth = col._minWidth;
55
+ } else {
56
+ col._calcWidth = col.width;
57
+ }
58
+
59
+ availableWidth -= col._calcWidth;
60
+ });
61
+
62
+ // http://www.freesoft.org/CIE/RFC/1942/18.htm
63
+ // http://www.w3.org/TR/CSS2/tables.html#width-layout
64
+ // http://dev.w3.org/csswg/css3-tables-algorithms/Overview.src.htm
65
+ let minW = autoMin + starMaxMin * starColumns.length;
66
+ let maxW = autoMax + starMaxMax * starColumns.length;
67
+ if (minW >= availableWidth) {
68
+ // case 1 - there's no way to fit all columns within available width
69
+ // that's actually pretty bad situation with PDF as we have no horizontal scroll
70
+ // no easy workaround (unless we decide, in the future, to split single words)
71
+ // currently we simply use minWidths for all columns
72
+ autoColumns.forEach(col => {
73
+ col._calcWidth = col._minWidth;
74
+ });
75
+
76
+ starColumns.forEach(col => {
77
+ col._calcWidth = starMaxMin; // starMaxMin already contains padding
78
+ });
79
+ } else {
80
+ if (maxW < availableWidth) {
81
+ // case 2 - we can fit rest of the table within available space
82
+ autoColumns.forEach(col => {
83
+ col._calcWidth = col._maxWidth;
84
+ availableWidth -= col._calcWidth;
85
+ });
86
+ } else {
87
+ // maxW is too large, but minW fits within available width
88
+ let W = availableWidth - minW;
89
+ let D = maxW - minW;
90
+
91
+ autoColumns.forEach(col => {
92
+ let d = col._maxWidth - col._minWidth;
93
+ col._calcWidth = col._minWidth + d * W / D;
94
+ availableWidth -= col._calcWidth;
95
+ });
96
+ }
97
+
98
+ if (starColumns.length > 0) {
99
+ let starSize = availableWidth / starColumns.length;
100
+
101
+ starColumns.forEach(col => {
102
+ col._calcWidth = starSize;
103
+ });
104
+ }
105
+ }
106
+ }
107
+
108
+ function isAutoColumn(column) {
109
+ return column.width === 'auto';
110
+ }
111
+
112
+ function isStarColumn(column) {
113
+ return column.width === null || column.width === undefined || column.width === '*' || column.width === 'star';
114
+ }
115
+
116
+ //TODO: refactor and reuse in measureTable
117
+ function measureMinMax(columns) {
118
+ let result = { min: 0, max: 0 };
119
+ let maxStar = { min: 0, max: 0 };
120
+ let starCount = 0;
121
+
122
+ for (let i = 0, l = columns.length; i < l; i++) {
123
+ let c = columns[i];
124
+
125
+ if (isStarColumn(c)) {
126
+ maxStar.min = Math.max(maxStar.min, c._minWidth);
127
+ maxStar.max = Math.max(maxStar.max, c._maxWidth);
128
+ starCount++;
129
+ } else if (isAutoColumn(c)) {
130
+ result.min += c._minWidth;
131
+ result.max += c._maxWidth;
132
+ } else {
133
+ result.min += ((c.width !== undefined && c.width) || c._minWidth);
134
+ result.max += ((c.width !== undefined && c.width) || c._maxWidth);
135
+ }
136
+ }
137
+
138
+ if (starCount) {
139
+ result.min += starCount * maxStar.min;
140
+ result.max += starCount * maxStar.max;
141
+ }
142
+
143
+ return result;
144
+ }
145
+
146
+ /**
147
+ * Calculates column widths
148
+ */
149
+ export default {
150
+ buildColumnWidths: buildColumnWidths,
151
+ measureMinMax: measureMinMax,
152
+ isAutoColumn: isAutoColumn,
153
+ isStarColumn: isStarColumn
154
+ };
@@ -1,110 +1,134 @@
1
- import { isNumber } from './variableType';
2
-
3
- function fontStringify(key, val) {
4
- if (key === 'font') {
5
- return 'font';
6
- }
7
- return val;
8
- }
9
-
10
- /**
11
- * Convert node to readable string
12
- *
13
- * @param {object} node
14
- * @returns {string}
15
- */
16
- export function stringifyNode(node) {
17
- return JSON.stringify(node, fontStringify);
18
- }
19
-
20
- /**
21
- * @param {object} node
22
- * @returns {?string}
23
- */
24
- export function getNodeId(node) {
25
- if (node.id) {
26
- return node.id;
27
- }
28
-
29
- if (Array.isArray(node.text)) {
30
- for (let n of node.text) {
31
- let nodeId = getNodeId(n);
32
- if (nodeId) {
33
- return nodeId;
34
- }
35
- }
36
- }
37
-
38
- return null;
39
- }
40
-
41
- /**
42
- * @param {object} node
43
- * @param {object} styleStack object is instance of PDFDocument
44
- * @returns {?Array}
45
- */
46
- export function getNodeMargin(node, styleStack) {
47
- function processSingleMargins(node, currentMargin) {
48
- if (node.marginLeft || node.marginTop || node.marginRight || node.marginBottom) {
49
- return [
50
- node.marginLeft || currentMargin[0] || 0,
51
- node.marginTop || currentMargin[1] || 0,
52
- node.marginRight || currentMargin[2] || 0,
53
- node.marginBottom || currentMargin[3] || 0
54
- ];
55
- }
56
- return currentMargin;
57
- }
58
-
59
- function flattenStyleArray(styleArray, styleStack) {
60
- let flattenedStyles = {};
61
- for (let i = styleArray.length - 1; i >= 0; i--) {
62
- let styleName = styleArray[i];
63
- let style = styleStack.styleDictionary[styleName];
64
- for (let key in style) {
65
- if (style.hasOwnProperty(key)) {
66
- flattenedStyles[key] = style[key];
67
- }
68
- }
69
- }
70
- return flattenedStyles;
71
- }
72
-
73
- function convertMargin(margin) {
74
- if (isNumber(margin)) {
75
- margin = [margin, margin, margin, margin];
76
- } else if (Array.isArray(margin)) {
77
- if (margin.length === 2) {
78
- margin = [margin[0], margin[1], margin[0], margin[1]];
79
- }
80
- }
81
- return margin;
82
- }
83
-
84
- let margin = [undefined, undefined, undefined, undefined];
85
-
86
- if (node.style) {
87
- let styleArray = Array.isArray(node.style) ? node.style : [node.style];
88
- let flattenedStyleArray = flattenStyleArray(styleArray, styleStack);
89
-
90
- if (flattenedStyleArray) {
91
- margin = processSingleMargins(flattenedStyleArray, margin);
92
- }
93
-
94
- if (flattenedStyleArray.margin) {
95
- margin = convertMargin(flattenedStyleArray.margin);
96
- }
97
- }
98
-
99
- margin = processSingleMargins(node, margin);
100
-
101
- if (node.margin) {
102
- margin = convertMargin(node.margin);
103
- }
104
-
105
- if (margin[0] === undefined && margin[1] === undefined && margin[2] === undefined && margin[3] === undefined) {
106
- return null;
107
- }
108
-
109
- return margin;
110
- }
1
+ import { isNumber, isString } from './variableType';
2
+
3
+ function fontStringify(key, val) {
4
+ if (key === 'font') {
5
+ return 'font';
6
+ }
7
+ return val;
8
+ }
9
+
10
+ /**
11
+ * Convert node to readable string
12
+ *
13
+ * @param {object} node
14
+ * @returns {string}
15
+ */
16
+ export function stringifyNode(node) {
17
+ return JSON.stringify(node, fontStringify);
18
+ }
19
+
20
+ /**
21
+ * @param {object} node
22
+ * @returns {?string}
23
+ */
24
+ export function getNodeId(node) {
25
+ if (node.id) {
26
+ return node.id;
27
+ }
28
+
29
+ if (Array.isArray(node.text)) {
30
+ for (let n of node.text) {
31
+ let nodeId = getNodeId(n);
32
+ if (nodeId) {
33
+ return nodeId;
34
+ }
35
+ }
36
+ }
37
+
38
+ return null;
39
+ }
40
+
41
+ /**
42
+ * @param {object} node
43
+ * @param {object} styleStack object is instance of PDFDocument
44
+ * @returns {?Array}
45
+ */
46
+ export function getNodeMargin(node, styleStack) {
47
+
48
+ function processSingleMargins(node, currentMargin, defaultMargin = 0) {
49
+ if (node.marginLeft !== undefined || node.marginTop !== undefined || node.marginRight !== undefined || node.marginBottom !== undefined) {
50
+ return [
51
+ node.marginLeft ?? currentMargin[0] ?? defaultMargin,
52
+ node.marginTop ?? currentMargin[1] ?? defaultMargin,
53
+ node.marginRight ?? currentMargin[2] ?? defaultMargin,
54
+ node.marginBottom ?? currentMargin[3] ?? defaultMargin
55
+ ];
56
+ }
57
+ return currentMargin;
58
+ }
59
+
60
+ function flattenStyleArray(styleArray, styleStack, visited = new Set()) {
61
+ styleArray = Array.isArray(styleArray) ? styleArray : [styleArray];
62
+
63
+ // style is not valid array of strings
64
+ if (!styleArray.every(item => isString(item))) {
65
+ return {};
66
+ }
67
+
68
+ let flattenedStyles = {};
69
+ for (let index = 0; index < styleArray.length; index++) {
70
+ let styleName = styleArray[index];
71
+ let style = styleStack.styleDictionary[styleName];
72
+
73
+ // style not found
74
+ if (style === undefined) {
75
+ continue;
76
+ }
77
+
78
+ if (visited.has(styleName)) {
79
+ continue;
80
+ }
81
+
82
+ if (style.extends !== undefined) {
83
+ flattenedStyles = { ...flattenedStyles, ...flattenStyleArray(style.extends, styleStack, new Set([...visited, styleName])) };
84
+ }
85
+
86
+ if (style.margin !== undefined) {
87
+ flattenedStyles = { margin: convertMargin(style.margin) };
88
+ continue;
89
+ }
90
+
91
+ flattenedStyles = { margin: processSingleMargins(style, flattenedStyles.margin ?? {}, undefined) };
92
+ }
93
+
94
+ return flattenedStyles;
95
+ }
96
+
97
+ function convertMargin(margin) {
98
+ if (isNumber(margin)) {
99
+ margin = [margin, margin, margin, margin];
100
+ } else if (Array.isArray(margin)) {
101
+ if (margin.length === 2) {
102
+ margin = [margin[0], margin[1], margin[0], margin[1]];
103
+ }
104
+ }
105
+ return margin;
106
+ }
107
+
108
+ let margin = [undefined, undefined, undefined, undefined];
109
+
110
+ if (node.style) {
111
+ let styleArray = Array.isArray(node.style) ? node.style : [node.style];
112
+ let flattenedStyleArray = flattenStyleArray(styleArray, styleStack);
113
+
114
+ if (flattenedStyleArray) {
115
+ margin = processSingleMargins(flattenedStyleArray, margin);
116
+ }
117
+
118
+ if (flattenedStyleArray.margin) {
119
+ margin = convertMargin(flattenedStyleArray.margin);
120
+ }
121
+ }
122
+
123
+ margin = processSingleMargins(node, margin);
124
+
125
+ if (node.margin !== undefined) {
126
+ margin = convertMargin(node.margin);
127
+ }
128
+
129
+ if (margin[0] === undefined && margin[1] === undefined && margin[2] === undefined && margin[3] === undefined) {
130
+ return null;
131
+ }
132
+
133
+ return margin;
134
+ }