forked from bpampuch/pdfmake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline.js
73 lines (59 loc) · 1.42 KB
/
line.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* jslint node: true */
'use strict';
/**
* Creates an instance of Line
*
* @constructor
* @this {Line}
* @param {Number} Maximum width this line can have
*/
function Line(maxWidth) {
this.maxWidth = maxWidth;
this.leadingCut = 0;
this.trailingCut = 0;
this.inlineWidths = 0;
this.inlines = [];
}
Line.prototype.getAscenderHeight = function () {
var y = 0;
this.inlines.forEach(function (inline) {
y = Math.max(y, inline.font.ascender / 1000 * inline.fontSize);
});
return y;
};
Line.prototype.hasEnoughSpaceForInline = function (inline) {
if (this.inlines.length === 0) {
return true;
}
if (this.newLineForced) {
return false;
}
return this.inlineWidths + inline.width - this.leadingCut - (inline.trailingCut || 0) <= this.maxWidth;
};
Line.prototype.addInline = function (inline) {
if (this.inlines.length === 0) {
this.leadingCut = inline.leadingCut || 0;
}
this.trailingCut = inline.trailingCut || 0;
inline.x = this.inlineWidths - this.leadingCut;
this.inlines.push(inline);
this.inlineWidths += inline.width;
if (inline.lineEnd) {
this.newLineForced = true;
}
};
Line.prototype.getWidth = function () {
return this.inlineWidths - this.leadingCut - this.trailingCut;
};
/**
* Returns line height
* @return {Number}
*/
Line.prototype.getHeight = function () {
var max = 0;
this.inlines.forEach(function (item) {
max = Math.max(max, item.height || 0);
});
return max;
};
module.exports = Line;