@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,157 @@
1
+ 'use strict';
2
+
3
+ var isArray = require('./helpers').isArray;
4
+ var isPattern = require('./helpers').isPattern;
5
+ var getPattern = require('./helpers').getPattern;
6
+
7
+ function groupDecorations(line) {
8
+ var groups = [], currentGroup = null;
9
+ for (var i = 0, l = line.inlines.length; i < l; i++) {
10
+ var inline = line.inlines[i];
11
+ var decoration = inline.decoration;
12
+ if (!decoration) {
13
+ currentGroup = null;
14
+ continue;
15
+ }
16
+ if (!isArray(decoration)) {
17
+ decoration = [decoration];
18
+ }
19
+ var color = inline.decorationColor || inline.color || 'black';
20
+ var style = inline.decorationStyle || 'solid';
21
+ for (var ii = 0, ll = decoration.length; ii < ll; ii++) {
22
+ var decorationItem = decoration[ii];
23
+ if (!currentGroup || decorationItem !== currentGroup.decoration ||
24
+ style !== currentGroup.decorationStyle || color !== currentGroup.decorationColor) {
25
+
26
+ currentGroup = {
27
+ line: line,
28
+ decoration: decorationItem,
29
+ decorationColor: color,
30
+ decorationStyle: style,
31
+ inlines: [inline]
32
+ };
33
+ groups.push(currentGroup);
34
+ } else {
35
+ currentGroup.inlines.push(inline);
36
+ }
37
+ }
38
+ }
39
+
40
+ return groups;
41
+ }
42
+
43
+ function drawDecoration(group, x, y, pdfKitDoc) {
44
+ function maxInline() {
45
+ var max = 0;
46
+ for (var i = 0, l = group.inlines.length; i < l; i++) {
47
+ var inline = group.inlines[i];
48
+ max = inline.fontSize > max ? i : max;
49
+ }
50
+ return group.inlines[max];
51
+ }
52
+ function width() {
53
+ var sum = 0;
54
+ for (var i = 0, l = group.inlines.length; i < l; i++) {
55
+ var justifyShift = (group.inlines[i].justifyShift || 0);
56
+ sum += group.inlines[i].width + justifyShift;
57
+ }
58
+ return sum;
59
+ }
60
+ var firstInline = group.inlines[0],
61
+ biggerInline = maxInline(),
62
+ totalWidth = width(),
63
+ lineAscent = group.line.getAscenderHeight(),
64
+ ascent = biggerInline.font.ascender / 1000 * biggerInline.fontSize,
65
+ height = biggerInline.height,
66
+ descent = height - ascent;
67
+
68
+ var lw = 0.5 + Math.floor(Math.max(biggerInline.fontSize - 8, 0) / 2) * 0.12;
69
+
70
+ switch (group.decoration) {
71
+ case 'underline':
72
+ y += lineAscent + descent * 0.45;
73
+ break;
74
+ case 'overline':
75
+ y += lineAscent - (ascent * 0.85);
76
+ break;
77
+ case 'lineThrough':
78
+ y += lineAscent - (ascent * 0.25);
79
+ break;
80
+ default:
81
+ throw 'Unknown decoration : ' + group.decoration;
82
+ }
83
+ pdfKitDoc.save();
84
+
85
+ if (group.decorationStyle === 'double') {
86
+ var gap = Math.max(0.5, lw * 2);
87
+ pdfKitDoc.fillColor(group.decorationColor)
88
+ .rect(x + firstInline.x, y - lw / 2, totalWidth, lw / 2).fill()
89
+ .rect(x + firstInline.x, y + gap - lw / 2, totalWidth, lw / 2).fill();
90
+ } else if (group.decorationStyle === 'dashed') {
91
+ var nbDashes = Math.ceil(totalWidth / (3.96 + 2.84));
92
+ var rdx = x + firstInline.x;
93
+ pdfKitDoc.rect(rdx, y, totalWidth, lw).clip();
94
+ pdfKitDoc.fillColor(group.decorationColor);
95
+ for (var i = 0; i < nbDashes; i++) {
96
+ pdfKitDoc.rect(rdx, y - lw / 2, 3.96, lw).fill();
97
+ rdx += 3.96 + 2.84;
98
+ }
99
+ } else if (group.decorationStyle === 'dotted') {
100
+ var nbDots = Math.ceil(totalWidth / (lw * 3));
101
+ var rx = x + firstInline.x;
102
+ pdfKitDoc.rect(rx, y, totalWidth, lw).clip();
103
+ pdfKitDoc.fillColor(group.decorationColor);
104
+ for (var ii = 0; ii < nbDots; ii++) {
105
+ pdfKitDoc.rect(rx, y - lw / 2, lw, lw).fill();
106
+ rx += (lw * 3);
107
+ }
108
+ } else if (group.decorationStyle === 'wavy') {
109
+ var sh = 0.7, sv = 1;
110
+ var nbWaves = Math.ceil(totalWidth / (sh * 2)) + 1;
111
+ var rwx = x + firstInline.x - 1;
112
+ pdfKitDoc.rect(x + firstInline.x, y - sv, totalWidth, y + sv).clip();
113
+ pdfKitDoc.lineWidth(0.24);
114
+ pdfKitDoc.moveTo(rwx, y);
115
+ for (var iii = 0; iii < nbWaves; iii++) {
116
+ pdfKitDoc.bezierCurveTo(rwx + sh, y - sv, rwx + sh * 2, y - sv, rwx + sh * 3, y)
117
+ .bezierCurveTo(rwx + sh * 4, y + sv, rwx + sh * 5, y + sv, rwx + sh * 6, y);
118
+ rwx += sh * 6;
119
+ }
120
+ pdfKitDoc.stroke(group.decorationColor);
121
+ } else {
122
+ pdfKitDoc.fillColor(group.decorationColor)
123
+ .rect(x + firstInline.x, y - lw / 2, totalWidth, lw)
124
+ .fill();
125
+ }
126
+ pdfKitDoc.restore();
127
+ }
128
+
129
+ function drawDecorations(line, x, y, pdfKitDoc) {
130
+ var groups = groupDecorations(line);
131
+ for (var i = 0, l = groups.length; i < l; i++) {
132
+ drawDecoration(groups[i], x, y, pdfKitDoc);
133
+ }
134
+ }
135
+
136
+ function drawBackground(line, x, y, patterns, pdfKitDoc) {
137
+ var height = line.getHeight();
138
+ for (var i = 0, l = line.inlines.length; i < l; i++) {
139
+ var inline = line.inlines[i];
140
+ if (!inline.background) {
141
+ continue;
142
+ }
143
+ var color = inline.background;
144
+ if (isPattern(inline.background)) {
145
+ color = getPattern(inline.background, patterns);
146
+ }
147
+ var justifyShift = (inline.justifyShift || 0);
148
+ pdfKitDoc.fillColor(color)
149
+ .rect(x + inline.x - justifyShift, y, inline.width + justifyShift, height)
150
+ .fill();
151
+ }
152
+ }
153
+
154
+ module.exports = {
155
+ drawBackground: drawBackground,
156
+ drawDecorations: drawDecorations
157
+ };
@@ -0,0 +1,391 @@
1
+ 'use strict';
2
+
3
+ var isString = require('./helpers').isString;
4
+ var isNumber = require('./helpers').isNumber;
5
+ var isObject = require('./helpers').isObject;
6
+ var isArray = require('./helpers').isArray;
7
+ var isUndefined = require('./helpers').isUndefined;
8
+ var LineBreaker = require('@foliojs-fork/linebreak');
9
+ var rtlUtils = require('./rtlUtils');
10
+
11
+ var LEADING = /^(\s)+/g;
12
+ var TRAILING = /(\s)+$/g;
13
+
14
+ /**
15
+ * Creates an instance of TextTools - text measurement utility
16
+ *
17
+ * @constructor
18
+ * @param {FontProvider} fontProvider
19
+ */
20
+ function TextTools(fontProvider) {
21
+ this.fontProvider = fontProvider;
22
+ }
23
+
24
+ /**
25
+ * Converts an array of strings (or inline-definition-objects) into a collection
26
+ * of inlines and calculated minWidth/maxWidth.
27
+ * and their min/max widths
28
+ * @param {Object} textArray - an array of inline-definition-objects (or strings)
29
+ * @param {Object} styleContextStack current style stack
30
+ * @return {Object} collection of inlines, minWidth, maxWidth
31
+ */
32
+ TextTools.prototype.buildInlines = function (textArray, styleContextStack) {
33
+ var measured = measure(this.fontProvider, textArray, styleContextStack);
34
+
35
+ var minWidth = 0,
36
+ maxWidth = 0,
37
+ currentLineWidth;
38
+
39
+ measured.forEach(function (inline) {
40
+ minWidth = Math.max(minWidth, inline.width - inline.leadingCut - inline.trailingCut);
41
+
42
+ if (!currentLineWidth) {
43
+ currentLineWidth = { width: 0, leadingCut: inline.leadingCut, trailingCut: 0 };
44
+ }
45
+
46
+ currentLineWidth.width += inline.width;
47
+ currentLineWidth.trailingCut = inline.trailingCut;
48
+
49
+ maxWidth = Math.max(maxWidth, getTrimmedWidth(currentLineWidth));
50
+
51
+ if (inline.lineEnd) {
52
+ currentLineWidth = null;
53
+ }
54
+ });
55
+
56
+ if (getStyleProperty({}, styleContextStack, 'noWrap', false)) {
57
+ minWidth = maxWidth;
58
+ }
59
+
60
+ return {
61
+ items: measured,
62
+ minWidth: minWidth,
63
+ maxWidth: maxWidth
64
+ };
65
+
66
+ function getTrimmedWidth(item) {
67
+ return Math.max(0, item.width - item.leadingCut - item.trailingCut);
68
+ }
69
+ };
70
+
71
+ /**
72
+ * Returns size of the specified string (without breaking it) using the current style
73
+ * @param {String} text text to be measured
74
+ * @param {Object} styleContextStack current style stack
75
+ * @return {Object} size of the specified string
76
+ */
77
+ TextTools.prototype.sizeOfString = function (text, styleContextStack) {
78
+ text = text ? text.toString().replace(/\t/g, ' ') : '';
79
+
80
+ //TODO: refactor - extract from measure
81
+ var fontName = getStyleProperty({}, styleContextStack, 'font', 'Roboto');
82
+ var fontSize = getStyleProperty({}, styleContextStack, 'fontSize', 12);
83
+ var fontFeatures = getStyleProperty({}, styleContextStack, 'fontFeatures', null);
84
+ var bold = getStyleProperty({}, styleContextStack, 'bold', false);
85
+ var italics = getStyleProperty({}, styleContextStack, 'italics', false);
86
+ var lineHeight = getStyleProperty({}, styleContextStack, 'lineHeight', 1);
87
+ var characterSpacing = getStyleProperty({}, styleContextStack, 'characterSpacing', 0);
88
+
89
+ var font = this.fontProvider.provideFont(fontName, bold, italics);
90
+
91
+ return {
92
+ width: widthOfString(text, font, fontSize, characterSpacing, fontFeatures),
93
+ height: font.lineHeight(fontSize) * lineHeight,
94
+ fontSize: fontSize,
95
+ lineHeight: lineHeight,
96
+ ascender: font.ascender / 1000 * fontSize,
97
+ descender: font.descender / 1000 * fontSize
98
+ };
99
+ };
100
+
101
+ /**
102
+ * Returns size of the specified rotated string (without breaking it) using the current style
103
+ *
104
+ * @param {string} text text to be measured
105
+ * @param {number} angle
106
+ * @param {object} styleContextStack current style stack
107
+ * @returns {object} size of the specified string
108
+ */
109
+ TextTools.prototype.sizeOfRotatedText = function (text, angle, styleContextStack) {
110
+ var angleRad = angle * Math.PI / -180;
111
+ var size = this.sizeOfString(text, styleContextStack);
112
+ return {
113
+ width: Math.abs(size.height * Math.sin(angleRad)) + Math.abs(size.width * Math.cos(angleRad)),
114
+ height: Math.abs(size.width * Math.sin(angleRad)) + Math.abs(size.height * Math.cos(angleRad))
115
+ };
116
+ };
117
+
118
+ TextTools.prototype.widthOfString = function (text, font, fontSize, characterSpacing, fontFeatures) {
119
+ return widthOfString(text, font, fontSize, characterSpacing, fontFeatures);
120
+ };
121
+
122
+ function splitWords(text, noWrap) {
123
+ var results = [];
124
+ text = text.replace(/\t/g, ' ');
125
+
126
+ if (noWrap) {
127
+ results.push({ text: text });
128
+ return results;
129
+ }
130
+
131
+ var breaker = new LineBreaker(text);
132
+ var last = 0;
133
+ var bk;
134
+
135
+ while (bk = breaker.nextBreak()) {
136
+ var word = text.slice(last, bk.position);
137
+
138
+ if (bk.required || word.match(/\r?\n$|\r$/)) { // new line
139
+ word = word.replace(/\r?\n$|\r$/, '');
140
+ results.push({ text: word, lineEnd: true });
141
+ } else {
142
+ results.push({ text: word });
143
+ }
144
+
145
+ last = bk.position;
146
+ }
147
+
148
+ return results;
149
+ }
150
+
151
+ function copyStyle(source, destination) {
152
+ destination = destination || {};
153
+ source = source || {}; //TODO: default style
154
+
155
+ for (var key in source) {
156
+ if (key != 'text' && source.hasOwnProperty(key)) {
157
+ destination[key] = source[key];
158
+ }
159
+ }
160
+
161
+ return destination;
162
+ }
163
+
164
+ function normalizeTextArray(array, styleContextStack) {
165
+ function flatten(array) {
166
+ return array.reduce(function (prev, cur) {
167
+ var current = isArray(cur.text) ? flatten(cur.text) : cur;
168
+ var more = [].concat(current).some(Array.isArray);
169
+ return prev.concat(more ? flatten(current) : current);
170
+ }, []);
171
+ }
172
+
173
+ function getOneWord(index, words, noWrap) {
174
+ if (isUndefined(words[index])) {
175
+ return null;
176
+ }
177
+
178
+ if (words[index].lineEnd) {
179
+ return null;
180
+ }
181
+
182
+ var word = words[index].text;
183
+
184
+ if (noWrap) {
185
+ var tmpWords = splitWords(normalizeString(word), false);
186
+ if (isUndefined(tmpWords[tmpWords.length - 1])) {
187
+ return null;
188
+ }
189
+ word = tmpWords[tmpWords.length - 1].text;
190
+ }
191
+
192
+ return word;
193
+ }
194
+
195
+ var results = [];
196
+
197
+ if (!isArray(array)) {
198
+ array = [array];
199
+ }
200
+
201
+ array = flatten(array);
202
+
203
+ var lastWord = null;
204
+ for (var i = 0, l = array.length; i < l; i++) {
205
+ var item = array[i];
206
+ var style = null;
207
+ var words;
208
+
209
+ var noWrap = getStyleProperty(item || {}, styleContextStack, 'noWrap', false);
210
+ if (isObject(item)) {
211
+ if (item._textRef && item._textRef._textNodeRef.text) {
212
+ item.text = item._textRef._textNodeRef.text;
213
+ }
214
+ words = splitWords(normalizeString(item.text), noWrap);
215
+ style = copyStyle(item);
216
+ } else {
217
+ words = splitWords(normalizeString(item), noWrap);
218
+ }
219
+
220
+ if (lastWord && words.length) {
221
+ var firstWord = getOneWord(0, words, noWrap);
222
+
223
+ var wrapWords = splitWords(normalizeString(lastWord + firstWord), false);
224
+ if (wrapWords.length === 1) {
225
+ results[results.length - 1].noNewLine = true;
226
+ }
227
+ }
228
+
229
+ for (var i2 = 0, l2 = words.length; i2 < l2; i2++) {
230
+ var result = {
231
+ text: words[i2].text
232
+ };
233
+
234
+ if (words[i2].lineEnd) {
235
+ result.lineEnd = true;
236
+ }
237
+
238
+ copyStyle(style, result);
239
+
240
+ results.push(result);
241
+ }
242
+
243
+ lastWord = null;
244
+ if (i + 1 < l) {
245
+ lastWord = getOneWord(words.length - 1, words, noWrap);
246
+ }
247
+ }
248
+
249
+ return results;
250
+ }
251
+
252
+ function normalizeString(value) {
253
+ if (value === undefined || value === null) {
254
+ return '';
255
+ } else if (isNumber(value)) {
256
+ return value.toString();
257
+ } else if (isString(value)) {
258
+ return value;
259
+ } else {
260
+ return value.toString();
261
+ }
262
+ }
263
+
264
+ function getStyleProperty(item, styleContextStack, property, defaultValue) {
265
+ var value;
266
+
267
+ if (item[property] !== undefined && item[property] !== null) {
268
+ // item defines this property
269
+ return item[property];
270
+ }
271
+
272
+ if (!styleContextStack) {
273
+ return defaultValue;
274
+ }
275
+
276
+ styleContextStack.auto(item, function () {
277
+ value = styleContextStack.getProperty(property);
278
+ });
279
+
280
+ if (value !== null && value !== undefined) {
281
+ return value;
282
+ } else {
283
+ return defaultValue;
284
+ }
285
+ }
286
+
287
+ function measure(fontProvider, textArray, styleContextStack) {
288
+ var normalized = normalizeTextArray(textArray, styleContextStack);
289
+
290
+ if (normalized.length) {
291
+ var leadingIndent = getStyleProperty(normalized[0], styleContextStack, 'leadingIndent', 0);
292
+
293
+ if (leadingIndent) {
294
+ normalized[0].leadingCut = -leadingIndent;
295
+ normalized[0].leadingIndent = leadingIndent;
296
+ }
297
+ }
298
+
299
+ normalized.forEach(function (item) {
300
+ var fontName = getStyleProperty(item, styleContextStack, 'font', 'Roboto');
301
+ var fontSize = getStyleProperty(item, styleContextStack, 'fontSize', 12);
302
+ var fontFeatures = getStyleProperty(item, styleContextStack, 'fontFeatures', null);
303
+ var bold = getStyleProperty(item, styleContextStack, 'bold', false);
304
+ var italics = getStyleProperty(item, styleContextStack, 'italics', false);
305
+ var color = getStyleProperty(item, styleContextStack, 'color', 'black');
306
+ var decoration = getStyleProperty(item, styleContextStack, 'decoration', null);
307
+ var decorationColor = getStyleProperty(item, styleContextStack, 'decorationColor', null);
308
+ var decorationStyle = getStyleProperty(item, styleContextStack, 'decorationStyle', null);
309
+ var background = getStyleProperty(item, styleContextStack, 'background', null);
310
+ var lineHeight = getStyleProperty(item, styleContextStack, 'lineHeight', 1);
311
+ var characterSpacing = getStyleProperty(item, styleContextStack, 'characterSpacing', 0);
312
+ var link = getStyleProperty(item, styleContextStack, 'link', null);
313
+ var linkToPage = getStyleProperty(item, styleContextStack, 'linkToPage', null);
314
+ var linkToDestination = getStyleProperty(item, styleContextStack, 'linkToDestination', null);
315
+ var noWrap = getStyleProperty(item, styleContextStack, 'noWrap', null);
316
+ var preserveLeadingSpaces = getStyleProperty(item, styleContextStack, 'preserveLeadingSpaces', false);
317
+ var preserveTrailingSpaces = getStyleProperty(item, styleContextStack, 'preserveTrailingSpaces', false);
318
+ var opacity = getStyleProperty(item, styleContextStack, 'opacity', 1);
319
+ var sup = getStyleProperty(item, styleContextStack, 'sup', false);
320
+ var sub = getStyleProperty(item, styleContextStack, 'sub', false);
321
+
322
+ // RTL Support
323
+ var direction = getStyleProperty(item, styleContextStack, 'direction', null);
324
+
325
+ if ((sup || sub) && item.fontSize === undefined) {
326
+ // font size reduction taken from here: https://en.wikipedia.org/wiki/Subscript_and_superscript#Desktop_publishing
327
+ fontSize *= 0.58;
328
+ }
329
+
330
+ var font = fontProvider.provideFont(fontName, bold, italics);
331
+
332
+ // Process RTL text if needed
333
+ var originalText = item.text;
334
+ var rtlResult = rtlUtils.processRTLText(originalText, direction);
335
+ item.text = rtlResult.text;
336
+ item.isRTL = rtlResult.isRTL;
337
+ item.direction = rtlResult.isRTL ? 'rtl' : 'ltr';
338
+
339
+
340
+ item.width = widthOfString(item.text, font, fontSize, characterSpacing, fontFeatures);
341
+ item.height = font.lineHeight(fontSize) * lineHeight;
342
+
343
+ if (!item.leadingCut) {
344
+ item.leadingCut = 0;
345
+ }
346
+
347
+ var leadingSpaces;
348
+ if (!preserveLeadingSpaces && (leadingSpaces = item.text.match(LEADING))) {
349
+ item.leadingCut += widthOfString(leadingSpaces[0], font, fontSize, characterSpacing, fontFeatures);
350
+ }
351
+
352
+ var trailingSpaces;
353
+ if (!preserveTrailingSpaces && (trailingSpaces = item.text.match(TRAILING))) {
354
+ item.trailingCut = widthOfString(trailingSpaces[0], font, fontSize, characterSpacing, fontFeatures);
355
+ } else {
356
+ item.trailingCut = 0;
357
+ }
358
+
359
+ item.alignment = getStyleProperty(item, styleContextStack, 'alignment', 'left');
360
+
361
+ // For RTL text, if no explicit alignment is set, default to 'right'
362
+ if (item.isRTL && item.alignment === 'left' && !getStyleProperty(item, styleContextStack, 'alignment', null)) {
363
+ item.alignment = 'right';
364
+ }
365
+
366
+ item.font = font;
367
+ item.fontSize = fontSize;
368
+ item.fontFeatures = fontFeatures;
369
+ item.characterSpacing = characterSpacing;
370
+ item.color = color;
371
+ item.decoration = decoration;
372
+ item.decorationColor = decorationColor;
373
+ item.decorationStyle = decorationStyle;
374
+ item.background = background;
375
+ item.link = link;
376
+ item.linkToPage = linkToPage;
377
+ item.linkToDestination = linkToDestination;
378
+ item.noWrap = noWrap;
379
+ item.opacity = opacity;
380
+ item.sup = sup;
381
+ item.sub = sub;
382
+ });
383
+
384
+ return normalized;
385
+ }
386
+
387
+ function widthOfString(text, font, fontSize, characterSpacing, fontFeatures) {
388
+ return font.widthOfString(text, fontSize, fontFeatures) + ((characterSpacing || 0) * (text.length - 1));
389
+ }
390
+
391
+ module.exports = TextTools;
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+
3
+ function TraversalTracker() {
4
+ this.events = {};
5
+ }
6
+
7
+ TraversalTracker.prototype.startTracking = function (event, callback) {
8
+ var callbacks = this.events[event] || (this.events[event] = []);
9
+
10
+ if (callbacks.indexOf(callback) < 0) {
11
+ callbacks.push(callback);
12
+ }
13
+ };
14
+
15
+ TraversalTracker.prototype.stopTracking = function (event, callback) {
16
+ var callbacks = this.events[event];
17
+
18
+ if (!callbacks) {
19
+ return;
20
+ }
21
+
22
+ var index = callbacks.indexOf(callback);
23
+ if (index >= 0) {
24
+ callbacks.splice(index, 1);
25
+ }
26
+ };
27
+
28
+ TraversalTracker.prototype.emit = function (event) {
29
+ var args = Array.prototype.slice.call(arguments, 1);
30
+ var callbacks = this.events[event];
31
+
32
+ if (!callbacks) {
33
+ return;
34
+ }
35
+
36
+ callbacks.forEach(function (callback) {
37
+ callback.apply(this, args);
38
+ });
39
+ };
40
+
41
+ TraversalTracker.prototype.auto = function (event, callback, innerFunction) {
42
+ this.startTracking(event, callback);
43
+ innerFunction();
44
+ this.stopTracking(event, callback);
45
+ };
46
+
47
+ module.exports = TraversalTracker;