@digicole/pdfmake-rtl 1.0.0

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.
package/src/line.js ADDED
@@ -0,0 +1,104 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Creates an instance of Line
5
+ *
6
+ * @constructor
7
+ * @this {Line}
8
+ * @param {Number} Maximum width this line can have
9
+ */
10
+ function Line(maxWidth) {
11
+ this.maxWidth = maxWidth;
12
+ this.leadingCut = 0;
13
+ this.trailingCut = 0;
14
+ this.inlineWidths = 0;
15
+ this.inlines = [];
16
+ }
17
+
18
+ Line.prototype.getAscenderHeight = function () {
19
+ var y = 0;
20
+
21
+ this.inlines.forEach(function (inline) {
22
+ y = Math.max(y, inline.font.ascender / 1000 * inline.fontSize);
23
+ });
24
+ return y;
25
+ };
26
+
27
+ Line.prototype.hasEnoughSpaceForInline = function (inline, nextInlines) {
28
+ nextInlines = nextInlines || [];
29
+
30
+ if (this.inlines.length === 0) {
31
+ return true;
32
+ }
33
+ if (this.newLineForced) {
34
+ return false;
35
+ }
36
+
37
+ var inlineWidth = inline.width;
38
+ var inlineTrailingCut = inline.trailingCut || 0;
39
+ if (inline.noNewLine) {
40
+ for (var i = 0, l = nextInlines.length; i < l; i++) {
41
+ var nextInline = nextInlines[i];
42
+ inlineWidth += nextInline.width;
43
+ inlineTrailingCut += nextInline.trailingCut || 0;
44
+ if (!nextInline.noNewLine) {
45
+ break;
46
+ }
47
+ }
48
+ }
49
+
50
+ return (this.inlineWidths + inlineWidth - this.leadingCut - inlineTrailingCut) <= this.maxWidth;
51
+ };
52
+
53
+ Line.prototype.addInline = function (inline) {
54
+ if (this.inlines.length === 0) {
55
+ this.leadingCut = inline.leadingCut || 0;
56
+ }
57
+ this.trailingCut = inline.trailingCut || 0;
58
+
59
+ // For RTL text, we'll position inlines from right to left
60
+ // The actual RTL positioning will be handled in the alignment phase
61
+ inline.x = this.inlineWidths - this.leadingCut;
62
+
63
+ this.inlines.push(inline);
64
+ this.inlineWidths += inline.width;
65
+
66
+ if (inline.lineEnd) {
67
+ this.newLineForced = true;
68
+ }
69
+ };
70
+
71
+ Line.prototype.getWidth = function () {
72
+ return this.inlineWidths - this.leadingCut - this.trailingCut;
73
+ };
74
+
75
+ Line.prototype.getAvailableWidth = function () {
76
+ return this.maxWidth - this.getWidth();
77
+ };
78
+
79
+ /**
80
+ * Returns line height
81
+ * @return {Number}
82
+ */
83
+ Line.prototype.getHeight = function () {
84
+ var max = 0;
85
+
86
+ this.inlines.forEach(function (item) {
87
+ max = Math.max(max, item.height || 0);
88
+ });
89
+
90
+ return max;
91
+ };
92
+
93
+ /**
94
+ * Check if this line should be rendered RTL
95
+ * @return {Boolean}
96
+ */
97
+ Line.prototype.isRTL = function () {
98
+ // Check if any inline in this line is marked as RTL
99
+ return this.inlines.some(function (inline) {
100
+ return inline.isRTL || inline.direction === 'rtl';
101
+ });
102
+ };
103
+
104
+ module.exports = Line;
@@ -0,0 +1,174 @@
1
+ 'use strict';
2
+
3
+ var isUndefined = require('./helpers').isUndefined;
4
+ var ElementWriter = require('./elementWriter');
5
+
6
+ /**
7
+ * Creates an instance of PageElementWriter - an extended ElementWriter
8
+ * which can handle:
9
+ * - page-breaks (it adds new pages when there's not enough space left),
10
+ * - repeatable fragments (like table-headers, which are repeated everytime
11
+ * a page-break occurs)
12
+ * - transactions (used for unbreakable-blocks when we want to make sure
13
+ * whole block will be rendered on the same page)
14
+ */
15
+ function PageElementWriter(context, tracker) {
16
+ this.transactionLevel = 0;
17
+ this.repeatables = [];
18
+ this.tracker = tracker;
19
+ this.writer = new ElementWriter(context, tracker);
20
+ }
21
+
22
+ function fitOnPage(self, addFct) {
23
+ var position = addFct(self);
24
+ if (!position) {
25
+ self.moveToNextPage();
26
+ position = addFct(self);
27
+ }
28
+ return position;
29
+ }
30
+
31
+ PageElementWriter.prototype.addLine = function (line, dontUpdateContextPosition, index) {
32
+ return fitOnPage(this, function (self) {
33
+ return self.writer.addLine(line, dontUpdateContextPosition, index);
34
+ });
35
+ };
36
+
37
+ PageElementWriter.prototype.addImage = function (image, index) {
38
+ return fitOnPage(this, function (self) {
39
+ return self.writer.addImage(image, index);
40
+ });
41
+ };
42
+
43
+ PageElementWriter.prototype.addSVG = function (image, index) {
44
+ return fitOnPage(this, function (self) {
45
+ return self.writer.addSVG(image, index);
46
+ });
47
+ };
48
+
49
+ PageElementWriter.prototype.addQr = function (qr, index) {
50
+ return fitOnPage(this, function (self) {
51
+ return self.writer.addQr(qr, index);
52
+ });
53
+ };
54
+
55
+ PageElementWriter.prototype.addVector = function (vector, ignoreContextX, ignoreContextY, index, forcePage) {
56
+ return this.writer.addVector(vector, ignoreContextX, ignoreContextY, index, forcePage);
57
+ };
58
+
59
+ PageElementWriter.prototype.beginClip = function (width, height) {
60
+ return this.writer.beginClip(width, height);
61
+ };
62
+
63
+ PageElementWriter.prototype.endClip = function () {
64
+ return this.writer.endClip();
65
+ };
66
+
67
+ PageElementWriter.prototype.alignCanvas = function (node) {
68
+ this.writer.alignCanvas(node);
69
+ };
70
+
71
+ PageElementWriter.prototype.addFragment = function (fragment, useBlockXOffset, useBlockYOffset, dontUpdateContextPosition) {
72
+ if (!this.writer.addFragment(fragment, useBlockXOffset, useBlockYOffset, dontUpdateContextPosition)) {
73
+ this.moveToNextPage();
74
+ this.writer.addFragment(fragment, useBlockXOffset, useBlockYOffset, dontUpdateContextPosition);
75
+ }
76
+ };
77
+
78
+ PageElementWriter.prototype.moveToNextPage = function (pageOrientation) {
79
+
80
+ var nextPage = this.writer.context.moveToNextPage(pageOrientation);
81
+
82
+ // moveToNextPage is called multiple times for table, because is called for each column
83
+ // and repeatables are inserted only in the first time. If columns are used, is needed
84
+ // call for table in first column and then for table in the second column (is other repeatables).
85
+ this.repeatables.forEach(function (rep) {
86
+ if (isUndefined(rep.insertedOnPages[this.writer.context.page])) {
87
+ rep.insertedOnPages[this.writer.context.page] = true;
88
+ this.writer.addFragment(rep, true);
89
+ } else {
90
+ this.writer.context.moveDown(rep.height);
91
+ }
92
+ }, this);
93
+
94
+ this.writer.tracker.emit('pageChanged', {
95
+ prevPage: nextPage.prevPage,
96
+ prevY: nextPage.prevY,
97
+ y: this.writer.context.y
98
+ });
99
+ };
100
+
101
+ PageElementWriter.prototype.beginUnbreakableBlock = function (width, height) {
102
+ if (this.transactionLevel++ === 0) {
103
+ this.originalX = this.writer.context.x;
104
+ this.writer.pushContext(width, height);
105
+ }
106
+ };
107
+
108
+ PageElementWriter.prototype.commitUnbreakableBlock = function (forcedX, forcedY) {
109
+ if (--this.transactionLevel === 0) {
110
+ var unbreakableContext = this.writer.context;
111
+ this.writer.popContext();
112
+
113
+ var nbPages = unbreakableContext.pages.length;
114
+ if (nbPages > 0) {
115
+ // no support for multi-page unbreakableBlocks
116
+ var fragment = unbreakableContext.pages[0];
117
+ fragment.xOffset = forcedX;
118
+ fragment.yOffset = forcedY;
119
+
120
+ //TODO: vectors can influence height in some situations
121
+ if (nbPages > 1) {
122
+ // on out-of-context blocs (headers, footers, background) height should be the whole DocumentContext height
123
+ if (forcedX !== undefined || forcedY !== undefined) {
124
+ fragment.height = unbreakableContext.getCurrentPage().pageSize.height - unbreakableContext.pageMargins.top - unbreakableContext.pageMargins.bottom;
125
+ } else {
126
+ fragment.height = this.writer.context.getCurrentPage().pageSize.height - this.writer.context.pageMargins.top - this.writer.context.pageMargins.bottom;
127
+ for (var i = 0, l = this.repeatables.length; i < l; i++) {
128
+ fragment.height -= this.repeatables[i].height;
129
+ }
130
+ }
131
+ } else {
132
+ fragment.height = unbreakableContext.y;
133
+ }
134
+
135
+ if (forcedX !== undefined || forcedY !== undefined) {
136
+ this.writer.addFragment(fragment, true, true, true);
137
+ } else {
138
+ this.addFragment(fragment);
139
+ }
140
+ }
141
+ }
142
+ };
143
+
144
+ PageElementWriter.prototype.currentBlockToRepeatable = function () {
145
+ var unbreakableContext = this.writer.context;
146
+ var rep = { items: [] };
147
+
148
+ unbreakableContext.pages[0].items.forEach(function (item) {
149
+ rep.items.push(item);
150
+ });
151
+
152
+ rep.xOffset = this.originalX;
153
+
154
+ //TODO: vectors can influence height in some situations
155
+ rep.height = unbreakableContext.y;
156
+
157
+ rep.insertedOnPages = [];
158
+
159
+ return rep;
160
+ };
161
+
162
+ PageElementWriter.prototype.pushToRepeatables = function (rep) {
163
+ this.repeatables.push(rep);
164
+ };
165
+
166
+ PageElementWriter.prototype.popFromRepeatables = function () {
167
+ this.repeatables.pop();
168
+ };
169
+
170
+ PageElementWriter.prototype.context = function () {
171
+ return this.writer.context;
172
+ };
173
+
174
+ module.exports = PageElementWriter;
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ function _interopDefault(ex) {
4
+ return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex;
5
+ }
6
+
7
+ var PdfKit = _interopDefault(require('@foliojs-fork/pdfkit'));
8
+
9
+ function getEngineInstance() {
10
+ return PdfKit;
11
+ }
12
+
13
+ function createPdfDocument(options) {
14
+ options = options || {};
15
+ return new PdfKit(options);
16
+ }
17
+
18
+ module.exports = {
19
+ getEngineInstance: getEngineInstance,
20
+ createPdfDocument: createPdfDocument
21
+ };