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/index.js
1126 views
1
import Path from 'path';
2
import bMFont from 'load-bmfont';
3
import { isNodePattern, throwError } from '@jimp/utils';
4
import { measureText, measureTextHeight } from './measure-text';
5
6
function xOffsetBasedOnAlignment(constants, font, line, maxWidth, alignment) {
7
if (alignment === constants.HORIZONTAL_ALIGN_LEFT) {
8
return 0;
9
}
10
11
if (alignment === constants.HORIZONTAL_ALIGN_CENTER) {
12
return (maxWidth - measureText(font, line)) / 2;
13
}
14
15
return maxWidth - measureText(font, line);
16
}
17
18
function drawCharacter(image, font, x, y, char) {
19
if (char.width > 0 && char.height > 0) {
20
const characterPage = font.pages[char.page];
21
22
image.blit(
23
characterPage,
24
x + char.xoffset,
25
y + char.yoffset,
26
char.x,
27
char.y,
28
char.width,
29
char.height
30
);
31
}
32
33
return image;
34
}
35
36
function printText(font, x, y, text, defaultCharWidth) {
37
for (let i = 0; i < text.length; i++) {
38
let char;
39
40
if (font.chars[text[i]]) {
41
char = text[i];
42
} else if (/\s/.test(text[i])) {
43
char = '';
44
} else {
45
char = '?';
46
}
47
48
const fontChar = font.chars[char] || {};
49
const fontKerning = font.kernings[char];
50
51
drawCharacter(this, font, x, y, fontChar || {});
52
53
const kerning =
54
fontKerning && fontKerning[text[i + 1]] ? fontKerning[text[i + 1]] : 0;
55
56
x += kerning + (fontChar.xadvance || defaultCharWidth);
57
}
58
}
59
60
function splitLines(font, text, maxWidth) {
61
const words = text.split(' ');
62
const lines = [];
63
let currentLine = [];
64
let longestLine = 0;
65
66
words.forEach(word => {
67
const line = [...currentLine, word].join(' ');
68
const length = measureText(font, line);
69
70
if (length <= maxWidth) {
71
if (length > longestLine) {
72
longestLine = length;
73
}
74
75
currentLine.push(word);
76
} else {
77
lines.push(currentLine);
78
currentLine = [word];
79
}
80
});
81
82
lines.push(currentLine);
83
84
return {
85
lines,
86
longestLine
87
};
88
}
89
90
function loadPages(Jimp, dir, pages) {
91
const newPages = pages.map(page => {
92
return Jimp.read(dir + '/' + page);
93
});
94
95
return Promise.all(newPages);
96
}
97
98
const dir = process.env.DIRNAME || `${__dirname}/../`;
99
100
export default () => ({
101
constants: {
102
measureText,
103
measureTextHeight,
104
FONT_SANS_8_BLACK: Path.join(
105
dir,
106
'fonts/open-sans/open-sans-8-black/open-sans-8-black.fnt'
107
),
108
FONT_SANS_10_BLACK: Path.join(
109
dir,
110
'fonts/open-sans/open-sans-10-black/open-sans-10-black.fnt'
111
),
112
FONT_SANS_12_BLACK: Path.join(
113
dir,
114
'fonts/open-sans/open-sans-12-black/open-sans-12-black.fnt'
115
),
116
FONT_SANS_14_BLACK: Path.join(
117
dir,
118
'fonts/open-sans/open-sans-14-black/open-sans-14-black.fnt'
119
),
120
FONT_SANS_16_BLACK: Path.join(
121
dir,
122
'fonts/open-sans/open-sans-16-black/open-sans-16-black.fnt'
123
),
124
FONT_SANS_32_BLACK: Path.join(
125
dir,
126
'fonts/open-sans/open-sans-32-black/open-sans-32-black.fnt'
127
),
128
FONT_SANS_64_BLACK: Path.join(
129
dir,
130
'fonts/open-sans/open-sans-64-black/open-sans-64-black.fnt'
131
),
132
FONT_SANS_128_BLACK: Path.join(
133
dir,
134
'fonts/open-sans/open-sans-128-black/open-sans-128-black.fnt'
135
),
136
137
FONT_SANS_8_WHITE: Path.join(
138
dir,
139
'fonts/open-sans/open-sans-8-white/open-sans-8-white.fnt'
140
),
141
FONT_SANS_16_WHITE: Path.join(
142
dir,
143
'fonts/open-sans/open-sans-16-white/open-sans-16-white.fnt'
144
),
145
FONT_SANS_32_WHITE: Path.join(
146
dir,
147
'fonts/open-sans/open-sans-32-white/open-sans-32-white.fnt'
148
),
149
FONT_SANS_64_WHITE: Path.join(
150
dir,
151
'fonts/open-sans/open-sans-64-white/open-sans-64-white.fnt'
152
),
153
FONT_SANS_128_WHITE: Path.join(
154
dir,
155
'fonts/open-sans/open-sans-128-white/open-sans-128-white.fnt'
156
),
157
158
/**
159
* Loads a bitmap font from a file
160
* @param {string} file the file path of a .fnt file
161
* @param {function(Error, Jimp)} cb (optional) a function to call when the font is loaded
162
* @returns {Promise} a promise
163
*/
164
loadFont(file, cb) {
165
if (typeof file !== 'string')
166
return throwError.call(this, 'file must be a string', cb);
167
168
return new Promise((resolve, reject) => {
169
cb =
170
cb ||
171
function(err, font) {
172
if (err) reject(err);
173
else resolve(font);
174
};
175
176
bMFont(file, (err, font) => {
177
const chars = {};
178
const kernings = {};
179
180
if (err) {
181
return throwError.call(this, err, cb);
182
}
183
184
for (let i = 0; i < font.chars.length; i++) {
185
chars[String.fromCharCode(font.chars[i].id)] = font.chars[i];
186
}
187
188
for (let i = 0; i < font.kernings.length; i++) {
189
const firstString = String.fromCharCode(font.kernings[i].first);
190
kernings[firstString] = kernings[firstString] || {};
191
kernings[firstString][
192
String.fromCharCode(font.kernings[i].second)
193
] = font.kernings[i].amount;
194
}
195
196
loadPages(this, Path.dirname(file), font.pages).then(pages => {
197
cb(null, {
198
chars,
199
kernings,
200
pages,
201
common: font.common,
202
info: font.info
203
});
204
});
205
});
206
});
207
}
208
},
209
210
class: {
211
/**
212
* Draws a text on a image on a given boundary
213
* @param {Jimp} font a bitmap font loaded from `Jimp.loadFont` command
214
* @param {number} x the x position to start drawing the text
215
* @param {number} y the y position to start drawing the text
216
* @param {any} text the text to draw (string or object with `text`, `alignmentX`, and/or `alignmentY`)
217
* @param {number} maxWidth (optional) the boundary width to draw in
218
* @param {number} maxHeight (optional) the boundary height to draw in
219
* @param {function(Error, Jimp)} cb (optional) a function to call when the text is written
220
* @returns {Jimp} this for chaining of methods
221
*/
222
print(font, x, y, text, maxWidth, maxHeight, cb) {
223
if (typeof maxWidth === 'function' && typeof cb === 'undefined') {
224
cb = maxWidth;
225
maxWidth = Infinity;
226
}
227
228
if (typeof maxWidth === 'undefined') {
229
maxWidth = Infinity;
230
}
231
232
if (typeof maxHeight === 'function' && typeof cb === 'undefined') {
233
cb = maxHeight;
234
maxHeight = Infinity;
235
}
236
237
if (typeof maxHeight === 'undefined') {
238
maxHeight = Infinity;
239
}
240
241
if (typeof font !== 'object') {
242
return throwError.call(this, 'font must be a Jimp loadFont', cb);
243
}
244
245
if (
246
typeof x !== 'number' ||
247
typeof y !== 'number' ||
248
typeof maxWidth !== 'number'
249
) {
250
return throwError.call(this, 'x, y and maxWidth must be numbers', cb);
251
}
252
253
if (typeof maxWidth !== 'number') {
254
return throwError.call(this, 'maxWidth must be a number', cb);
255
}
256
257
if (typeof maxHeight !== 'number') {
258
return throwError.call(this, 'maxHeight must be a number', cb);
259
}
260
261
let alignmentX;
262
let alignmentY;
263
264
if (
265
typeof text === 'object' &&
266
text.text !== null &&
267
text.text !== undefined
268
) {
269
alignmentX = text.alignmentX || this.constructor.HORIZONTAL_ALIGN_LEFT;
270
alignmentY = text.alignmentY || this.constructor.VERTICAL_ALIGN_TOP;
271
({ text } = text);
272
} else {
273
alignmentX = this.constructor.HORIZONTAL_ALIGN_LEFT;
274
alignmentY = this.constructor.VERTICAL_ALIGN_TOP;
275
text = text.toString();
276
}
277
278
if (
279
maxHeight !== Infinity &&
280
alignmentY === this.constructor.VERTICAL_ALIGN_BOTTOM
281
) {
282
y += maxHeight - measureTextHeight(font, text, maxWidth);
283
} else if (
284
maxHeight !== Infinity &&
285
alignmentY === this.constructor.VERTICAL_ALIGN_MIDDLE
286
) {
287
y += maxHeight / 2 - measureTextHeight(font, text, maxWidth) / 2;
288
}
289
290
const defaultCharWidth = Object.entries(font.chars)[0][1].xadvance;
291
const { lines, longestLine } = splitLines(font, text, maxWidth);
292
293
lines.forEach(line => {
294
const lineString = line.join(' ');
295
const alignmentWidth = xOffsetBasedOnAlignment(
296
this.constructor,
297
font,
298
lineString,
299
maxWidth,
300
alignmentX
301
);
302
303
printText.call(
304
this,
305
font,
306
x + alignmentWidth,
307
y,
308
lineString,
309
defaultCharWidth
310
);
311
312
y += font.common.lineHeight;
313
});
314
315
if (isNodePattern(cb)) {
316
cb.call(this, null, this, { x: x + longestLine, y });
317
}
318
319
return this;
320
}
321
}
322
});
323
324