Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@jimp/plugin-print/src/measure-text.js
1126 views
1
export function measureText(font, text) {
2
let x = 0;
3
4
for (let i = 0; i < text.length; i++) {
5
if (font.chars[text[i]]) {
6
const kerning =
7
font.kernings[text[i]] && font.kernings[text[i]][text[i + 1]]
8
? font.kernings[text[i]][text[i + 1]]
9
: 0;
10
11
x += (font.chars[text[i]].xadvance || 0) + kerning;
12
}
13
}
14
15
return x;
16
}
17
18
export function measureTextHeight(font, text, maxWidth) {
19
const words = text.split(' ');
20
let line = '';
21
let textTotalHeight = font.common.lineHeight;
22
23
for (let n = 0; n < words.length; n++) {
24
const testLine = line + words[n] + ' ';
25
const testWidth = measureText(font, testLine);
26
27
if (testWidth > maxWidth && n > 0) {
28
textTotalHeight += font.common.lineHeight;
29
line = words[n] + ' ';
30
} else {
31
line = testLine;
32
}
33
}
34
35
return textTotalHeight;
36
}
37
38