@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.
@@ -0,0 +1,411 @@
1
+ 'use strict';
2
+
3
+ var Line = require('./line');
4
+ var isNumber = require('./helpers').isNumber;
5
+ var pack = require('./helpers').pack;
6
+ var offsetVector = require('./helpers').offsetVector;
7
+ var DocumentContext = require('./documentContext');
8
+
9
+ /**
10
+ * Creates an instance of ElementWriter - a line/vector writer, which adds
11
+ * elements to current page and sets their positions based on the context
12
+ */
13
+ function ElementWriter(context, tracker) {
14
+ this.context = context;
15
+ this.contextStack = [];
16
+ this.tracker = tracker;
17
+ }
18
+
19
+ function addPageItem(page, item, index) {
20
+ if (index === null || index === undefined || index < 0 || index > page.items.length) {
21
+ page.items.push(item);
22
+ } else {
23
+ page.items.splice(index, 0, item);
24
+ }
25
+ }
26
+
27
+ ElementWriter.prototype.addLine = function (line, dontUpdateContextPosition, index) {
28
+ var height = line.getHeight();
29
+ var context = this.context;
30
+ var page = context.getCurrentPage(),
31
+ position = this.getCurrentPositionOnPage();
32
+
33
+ if (context.availableHeight < height || !page) {
34
+ return false;
35
+ }
36
+
37
+ line.x = context.x + (line.x || 0);
38
+ line.y = context.y + (line.y || 0);
39
+
40
+ this.alignLine(line);
41
+
42
+ addPageItem(page, {
43
+ type: 'line',
44
+ item: line
45
+ }, index);
46
+ this.tracker.emit('lineAdded', line);
47
+
48
+ if (!dontUpdateContextPosition) {
49
+ context.moveDown(height);
50
+ }
51
+
52
+ return position;
53
+ };
54
+
55
+ ElementWriter.prototype.alignLine = function (line) {
56
+ var width = this.context.availableWidth;
57
+ var lineWidth = line.getWidth();
58
+
59
+ var alignment = line.inlines && line.inlines.length > 0 && line.inlines[0].alignment;
60
+ var isRTL = line.isRTL && line.isRTL();
61
+
62
+ var offset = 0;
63
+
64
+ // For RTL lines, we need special handling
65
+ if (isRTL) {
66
+ // If it's RTL and no explicit alignment, default to right
67
+ if (!alignment || alignment === 'left') {
68
+ alignment = 'right';
69
+ }
70
+
71
+ // For RTL, we need to reverse the order of inlines and adjust their positions
72
+ this.adjustRTLInlines(line, width);
73
+ }
74
+
75
+ switch (alignment) {
76
+ case 'right':
77
+ offset = width - lineWidth;
78
+ break;
79
+ case 'center':
80
+ offset = (width - lineWidth) / 2;
81
+ break;
82
+ }
83
+
84
+ if (offset) {
85
+ line.x = (line.x || 0) + offset;
86
+ }
87
+
88
+ if (alignment === 'justify' &&
89
+ !line.newLineForced &&
90
+ !line.lastLineInParagraph &&
91
+ line.inlines.length > 1) {
92
+ var additionalSpacing = (width - lineWidth) / (line.inlines.length - 1);
93
+
94
+ for (var i = 1, l = line.inlines.length; i < l; i++) {
95
+ offset = i * additionalSpacing;
96
+
97
+ line.inlines[i].x += offset;
98
+ line.inlines[i].justifyShift = additionalSpacing;
99
+ }
100
+ }
101
+ };
102
+
103
+ ElementWriter.prototype.addImage = function (image, index, type) {
104
+ var context = this.context;
105
+ var page = context.getCurrentPage(),
106
+ position = this.getCurrentPositionOnPage();
107
+
108
+ if (!page || (image.absolutePosition === undefined && context.availableHeight < image._height && page.items.length > 0)) {
109
+ return false;
110
+ }
111
+
112
+ if (image._x === undefined) {
113
+ image._x = image.x || 0;
114
+ }
115
+
116
+ image.x = context.x + image._x;
117
+ image.y = context.y;
118
+
119
+ this.alignImage(image);
120
+
121
+ addPageItem(page, {
122
+ type: type || 'image',
123
+ item: image
124
+ }, index);
125
+
126
+ context.moveDown(image._height);
127
+
128
+ return position;
129
+ };
130
+
131
+ ElementWriter.prototype.addSVG = function (image, index) {
132
+ return this.addImage(image, index, 'svg');
133
+ };
134
+
135
+ ElementWriter.prototype.addQr = function (qr, index) {
136
+ var context = this.context;
137
+ var page = context.getCurrentPage(),
138
+ position = this.getCurrentPositionOnPage();
139
+
140
+ if (!page || (qr.absolutePosition === undefined && context.availableHeight < qr._height)) {
141
+ return false;
142
+ }
143
+
144
+ if (qr._x === undefined) {
145
+ qr._x = qr.x || 0;
146
+ }
147
+
148
+ qr.x = context.x + qr._x;
149
+ qr.y = context.y;
150
+
151
+ this.alignImage(qr);
152
+
153
+ for (var i = 0, l = qr._canvas.length; i < l; i++) {
154
+ var vector = qr._canvas[i];
155
+ vector.x += qr.x;
156
+ vector.y += qr.y;
157
+ this.addVector(vector, true, true, index);
158
+ }
159
+
160
+ context.moveDown(qr._height);
161
+
162
+ return position;
163
+ };
164
+
165
+ ElementWriter.prototype.alignImage = function (image) {
166
+ var width = this.context.availableWidth;
167
+ var imageWidth = image._minWidth;
168
+ var offset = 0;
169
+ switch (image._alignment) {
170
+ case 'right':
171
+ offset = width - imageWidth;
172
+ break;
173
+ case 'center':
174
+ offset = (width - imageWidth) / 2;
175
+ break;
176
+ }
177
+
178
+ if (offset) {
179
+ image.x = (image.x || 0) + offset;
180
+ }
181
+ };
182
+
183
+ ElementWriter.prototype.alignCanvas = function (node) {
184
+ var width = this.context.availableWidth;
185
+ var canvasWidth = node._minWidth;
186
+ var offset = 0;
187
+ switch (node._alignment) {
188
+ case 'right':
189
+ offset = width - canvasWidth;
190
+ break;
191
+ case 'center':
192
+ offset = (width - canvasWidth) / 2;
193
+ break;
194
+ }
195
+ if (offset) {
196
+ node.canvas.forEach(function (vector) {
197
+ offsetVector(vector, offset, 0);
198
+ });
199
+ }
200
+ };
201
+
202
+ ElementWriter.prototype.addVector = function (vector, ignoreContextX, ignoreContextY, index, forcePage) {
203
+ var context = this.context;
204
+ var page = context.getCurrentPage();
205
+ if (isNumber(forcePage)) {
206
+ page = context.pages[forcePage];
207
+ }
208
+ var position = this.getCurrentPositionOnPage();
209
+
210
+ if (page) {
211
+ offsetVector(vector, ignoreContextX ? 0 : context.x, ignoreContextY ? 0 : context.y);
212
+ addPageItem(page, {
213
+ type: 'vector',
214
+ item: vector
215
+ }, index);
216
+ return position;
217
+ }
218
+ };
219
+
220
+ ElementWriter.prototype.beginClip = function (width, height) {
221
+ var ctx = this.context;
222
+ var page = ctx.getCurrentPage();
223
+ page.items.push({
224
+ type: 'beginClip',
225
+ item: { x: ctx.x, y: ctx.y, width: width, height: height }
226
+ });
227
+ return true;
228
+ };
229
+
230
+ ElementWriter.prototype.endClip = function () {
231
+ var ctx = this.context;
232
+ var page = ctx.getCurrentPage();
233
+ page.items.push({
234
+ type: 'endClip'
235
+ });
236
+ return true;
237
+ };
238
+
239
+ /**
240
+ * Adjust RTL inline positioning
241
+ * @param {Line} line - Line containing RTL text
242
+ * @param {number} availableWidth - Available width for the line
243
+ */
244
+ ElementWriter.prototype.adjustRTLInlines = function (line, availableWidth) {
245
+ if (!line.inlines || line.inlines.length === 0) {
246
+ return;
247
+ }
248
+
249
+ // For RTL text, we need to reverse the visual order of inlines
250
+ // and recalculate their positions from right to left
251
+ var rtlInlines = [];
252
+ var ltrInlines = [];
253
+ var neutralInlines = [];
254
+
255
+ // Separate RTL, LTR, and neutral inlines
256
+ line.inlines.forEach(function(inline) {
257
+ if (inline.isRTL || inline.direction === 'rtl') {
258
+ rtlInlines.push(inline);
259
+ } else if (inline.direction === 'ltr') {
260
+ ltrInlines.push(inline);
261
+ } else {
262
+ neutralInlines.push(inline);
263
+ }
264
+ });
265
+
266
+ // If we have RTL inlines, reverse their order and recalculate positions
267
+ if (rtlInlines.length > 0) {
268
+ // Reverse the order of RTL inlines for proper display
269
+ rtlInlines.reverse();
270
+
271
+ // Recalculate x positions from right to left
272
+ var currentX = 0;
273
+ var reorderedInlines = [];
274
+
275
+ // Add LTR inlines first (if any)
276
+ ltrInlines.forEach(function(inline) {
277
+ inline.x = currentX;
278
+ currentX += inline.width;
279
+ reorderedInlines.push(inline);
280
+ });
281
+
282
+ // Add neutral inlines
283
+ neutralInlines.forEach(function(inline) {
284
+ inline.x = currentX;
285
+ currentX += inline.width;
286
+ reorderedInlines.push(inline);
287
+ });
288
+
289
+ // Add RTL inlines (already reversed)
290
+ rtlInlines.forEach(function(inline) {
291
+ inline.x = currentX;
292
+ currentX += inline.width;
293
+ reorderedInlines.push(inline);
294
+ });
295
+
296
+ // Replace the line's inlines with the reordered ones
297
+ line.inlines = reorderedInlines;
298
+ }
299
+ };
300
+
301
+ function cloneLine(line) {
302
+ var result = new Line(line.maxWidth);
303
+
304
+ for (var key in line) {
305
+ if (line.hasOwnProperty(key)) {
306
+ result[key] = line[key];
307
+ }
308
+ }
309
+
310
+ return result;
311
+ }
312
+
313
+ ElementWriter.prototype.addFragment = function (block, useBlockXOffset, useBlockYOffset, dontUpdateContextPosition) {
314
+ var ctx = this.context;
315
+ var page = ctx.getCurrentPage();
316
+
317
+ if (!useBlockXOffset && block.height > ctx.availableHeight) {
318
+ return false;
319
+ }
320
+
321
+ block.items.forEach(function (item) {
322
+ switch (item.type) {
323
+ case 'line':
324
+ var l = cloneLine(item.item);
325
+
326
+ if (l._node) {
327
+ l._node.positions[0].pageNumber = ctx.page + 1;
328
+ }
329
+ l.x = (l.x || 0) + (useBlockXOffset ? (block.xOffset || 0) : ctx.x);
330
+ l.y = (l.y || 0) + (useBlockYOffset ? (block.yOffset || 0) : ctx.y);
331
+
332
+ page.items.push({
333
+ type: 'line',
334
+ item: l
335
+ });
336
+ break;
337
+
338
+ case 'vector':
339
+ var v = pack(item.item);
340
+
341
+ offsetVector(v, useBlockXOffset ? (block.xOffset || 0) : ctx.x, useBlockYOffset ? (block.yOffset || 0) : ctx.y);
342
+ if (v._isFillColorFromUnbreakable) {
343
+ // If the item is a fillColor from an unbreakable block
344
+ // We have to add it at the beginning of the items body array of the page
345
+ delete v._isFillColorFromUnbreakable;
346
+ const endOfBackgroundItemsIndex = ctx.backgroundLength[ctx.page];
347
+ page.items.splice(endOfBackgroundItemsIndex, 0, {
348
+ type: 'vector',
349
+ item: v
350
+ });
351
+ } else {
352
+ page.items.push({
353
+ type: 'vector',
354
+ item: v
355
+ });
356
+ }
357
+ break;
358
+
359
+ case 'image':
360
+ case 'svg':
361
+ var img = pack(item.item);
362
+
363
+ img.x = (img.x || 0) + (useBlockXOffset ? (block.xOffset || 0) : ctx.x);
364
+ img.y = (img.y || 0) + (useBlockYOffset ? (block.yOffset || 0) : ctx.y);
365
+
366
+ page.items.push({
367
+ type: item.type,
368
+ item: img
369
+ });
370
+ break;
371
+ }
372
+ });
373
+
374
+ if (!dontUpdateContextPosition) {
375
+ ctx.moveDown(block.height);
376
+ }
377
+
378
+ return true;
379
+ };
380
+
381
+ /**
382
+ * Pushes the provided context onto the stack or creates a new one
383
+ *
384
+ * pushContext(context) - pushes the provided context and makes it current
385
+ * pushContext(width, height) - creates and pushes a new context with the specified width and height
386
+ * pushContext() - creates a new context for unbreakable blocks (with current availableWidth and full-page-height)
387
+ */
388
+ ElementWriter.prototype.pushContext = function (contextOrWidth, height) {
389
+ if (contextOrWidth === undefined) {
390
+ height = this.context.getCurrentPage().height - this.context.pageMargins.top - this.context.pageMargins.bottom;
391
+ contextOrWidth = this.context.availableWidth;
392
+ }
393
+
394
+ if (isNumber(contextOrWidth)) {
395
+ contextOrWidth = new DocumentContext({ width: contextOrWidth, height: height }, { left: 0, right: 0, top: 0, bottom: 0 });
396
+ }
397
+
398
+ this.contextStack.push(this.context);
399
+ this.context = contextOrWidth;
400
+ };
401
+
402
+ ElementWriter.prototype.popContext = function () {
403
+ this.context = this.contextStack.pop();
404
+ };
405
+
406
+ ElementWriter.prototype.getCurrentPositionOnPage = function () {
407
+ return (this.contextStack[0] || this.context).getCurrentPosition();
408
+ };
409
+
410
+
411
+ module.exports = ElementWriter;
@@ -0,0 +1,68 @@
1
+ 'use strict';
2
+
3
+ var isArray = require('./helpers').isArray;
4
+
5
+ function typeName(bold, italics) {
6
+ var type = 'normal';
7
+ if (bold && italics) {
8
+ type = 'bolditalics';
9
+ } else if (bold) {
10
+ type = 'bold';
11
+ } else if (italics) {
12
+ type = 'italics';
13
+ }
14
+ return type;
15
+ }
16
+
17
+ function FontProvider(fontDescriptors, pdfKitDoc) {
18
+ this.fonts = {};
19
+ this.pdfKitDoc = pdfKitDoc;
20
+ this.fontCache = {};
21
+
22
+ for (var font in fontDescriptors) {
23
+ if (fontDescriptors.hasOwnProperty(font)) {
24
+ var fontDef = fontDescriptors[font];
25
+
26
+ this.fonts[font] = {
27
+ normal: fontDef.normal,
28
+ bold: fontDef.bold,
29
+ italics: fontDef.italics,
30
+ bolditalics: fontDef.bolditalics
31
+ };
32
+ }
33
+ }
34
+ }
35
+
36
+ FontProvider.prototype.getFontType = function (bold, italics) {
37
+ return typeName(bold, italics);
38
+ };
39
+
40
+ FontProvider.prototype.getFontFile = function (familyName, bold, italics) {
41
+ var type = this.getFontType(bold, italics);
42
+ if (!this.fonts[familyName] || !this.fonts[familyName][type]) {
43
+ return null;
44
+ }
45
+
46
+ return this.fonts[familyName][type];
47
+ };
48
+
49
+ FontProvider.prototype.provideFont = function (familyName, bold, italics) {
50
+ var type = this.getFontType(bold, italics);
51
+ if (this.getFontFile(familyName, bold, italics) === null) {
52
+ throw new Error('Font \'' + familyName + '\' in style \'' + type + '\' is not defined in the font section of the document definition.');
53
+ }
54
+
55
+ this.fontCache[familyName] = this.fontCache[familyName] || {};
56
+
57
+ if (!this.fontCache[familyName][type]) {
58
+ var def = this.fonts[familyName][type];
59
+ if (!isArray(def)) {
60
+ def = [def];
61
+ }
62
+ this.fontCache[familyName][type] = this.pdfKitDoc.font.apply(this.pdfKitDoc, def)._font;
63
+ }
64
+
65
+ return this.fontCache[familyName][type];
66
+ };
67
+
68
+ module.exports = FontProvider;
package/src/helpers.js ADDED
@@ -0,0 +1,138 @@
1
+ 'use strict';
2
+
3
+ function isString(variable) {
4
+ return typeof variable === 'string' || variable instanceof String;
5
+ }
6
+
7
+ function isNumber(variable) {
8
+ return typeof variable === 'number' || variable instanceof Number;
9
+ }
10
+
11
+ function isBoolean(variable) {
12
+ return typeof variable === 'boolean';
13
+ }
14
+
15
+ function isArray(variable) {
16
+ return Array.isArray(variable);
17
+ }
18
+
19
+ function isFunction(variable) {
20
+ return typeof variable === 'function';
21
+ }
22
+
23
+ function isObject(variable) {
24
+ return variable !== null && typeof variable === 'object';
25
+ }
26
+
27
+ function isNull(variable) {
28
+ return variable === null;
29
+ }
30
+
31
+ function isUndefined(variable) {
32
+ return variable === undefined;
33
+ }
34
+
35
+ /**
36
+ * @param {any} variable
37
+ * @returns {boolean}
38
+ */
39
+ function isPositiveInteger(variable) {
40
+ if (!isNumber(variable) || !Number.isInteger(variable) || variable <= 0) {
41
+ return false;
42
+ }
43
+ return true;
44
+ }
45
+
46
+ function pack() {
47
+ var result = {};
48
+
49
+ for (var i = 0, l = arguments.length; i < l; i++) {
50
+ var obj = arguments[i];
51
+
52
+ if (obj) {
53
+ for (var key in obj) {
54
+ if (obj.hasOwnProperty(key)) {
55
+ result[key] = obj[key];
56
+ }
57
+ }
58
+ }
59
+ }
60
+
61
+ return result;
62
+ }
63
+
64
+ function offsetVector(vector, x, y) {
65
+ switch (vector.type) {
66
+ case 'ellipse':
67
+ case 'rect':
68
+ vector.x += x;
69
+ vector.y += y;
70
+ break;
71
+ case 'line':
72
+ vector.x1 += x;
73
+ vector.x2 += x;
74
+ vector.y1 += y;
75
+ vector.y2 += y;
76
+ break;
77
+ case 'polyline':
78
+ for (var i = 0, l = vector.points.length; i < l; i++) {
79
+ vector.points[i].x += x;
80
+ vector.points[i].y += y;
81
+ }
82
+ break;
83
+ }
84
+ }
85
+
86
+ function fontStringify(key, val) {
87
+ if (key === 'font') {
88
+ return 'font';
89
+ }
90
+ return val;
91
+ }
92
+
93
+ function getNodeId(node) {
94
+ if (node.id) {
95
+ return node.id;
96
+ }
97
+
98
+ if (isArray(node.text)) {
99
+ for (var i = 0, l = node.text.length; i < l; i++) {
100
+ var n = node.text[i];
101
+ var nodeId = getNodeId(n);
102
+ if (nodeId) {
103
+ return nodeId;
104
+ }
105
+ }
106
+ }
107
+
108
+ return null;
109
+ }
110
+
111
+ function isPattern(color) {
112
+ return isArray(color) && color.length === 2;
113
+ }
114
+
115
+ // converts from a [<pattern name>, <color>] as used by pdfmake
116
+ // into [<pattern object>, <color>] as used by pdfkit
117
+ // (the pattern has to be registered in the doc definition of course)
118
+ function getPattern(color, patterns) {
119
+ return [patterns[color[0]], color[1]];
120
+ }
121
+
122
+ module.exports = {
123
+ isString: isString,
124
+ isNumber: isNumber,
125
+ isBoolean: isBoolean,
126
+ isArray: isArray,
127
+ isFunction: isFunction,
128
+ isObject: isObject,
129
+ isNull: isNull,
130
+ isUndefined: isUndefined,
131
+ isPositiveInteger: isPositiveInteger,
132
+ pack: pack,
133
+ fontStringify: fontStringify,
134
+ offsetVector: offsetVector,
135
+ getNodeId: getNodeId,
136
+ isPattern: isPattern,
137
+ getPattern: getPattern
138
+ };
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+
3
+ var fs = require('fs');
4
+
5
+ function ImageMeasure(pdfKitDoc, imageDictionary) {
6
+ this.pdfKitDoc = pdfKitDoc;
7
+ this.imageDictionary = imageDictionary || {};
8
+ }
9
+
10
+ ImageMeasure.prototype.measureImage = function (src) {
11
+ var image;
12
+ var that = this;
13
+
14
+ if (!this.pdfKitDoc._imageRegistry[src]) {
15
+ try {
16
+ image = this.pdfKitDoc.openImage(realImageSrc(src));
17
+ if (!image) {
18
+ throw 'No image';
19
+ }
20
+ } catch (error) {
21
+ throw 'Invalid image: ' + error.toString() + '\nImages dictionary should contain dataURL entries (or local file paths in node.js)';
22
+ }
23
+ image.embed(this.pdfKitDoc);
24
+ this.pdfKitDoc._imageRegistry[src] = image;
25
+ } else {
26
+ image = this.pdfKitDoc._imageRegistry[src];
27
+ }
28
+
29
+ var imageSize = { width: image.width, height: image.height };
30
+
31
+ // If EXIF orientation calls for it, swap width and height
32
+ if (image.orientation > 4) {
33
+ imageSize = { width: image.height, height: image.width };
34
+ }
35
+
36
+ return imageSize;
37
+
38
+ function realImageSrc(src) {
39
+ var img = that.imageDictionary[src];
40
+
41
+ if (!img) {
42
+ return src;
43
+ }
44
+
45
+ if (typeof img === 'object') {
46
+ throw 'Not supported image definition: ' + JSON.stringify(img);
47
+ }
48
+
49
+ if (fs.existsSync(img)) {
50
+ return fs.readFileSync(img);
51
+ }
52
+
53
+ var index = img.indexOf('base64,');
54
+ if (index < 0) {
55
+ return that.imageDictionary[src];
56
+ }
57
+
58
+ return Buffer.from(img.substring(index + 7), 'base64');
59
+ }
60
+ };
61
+
62
+ module.exports = ImageMeasure;