Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@jimp/plugin-print/test/print.test.js
1126 views
1
/* eslint key-spacing: ["error", { "align": "value" }] */
2
3
import { Jimp, getTestDir, hasOwnProp } from '@jimp/test-utils';
4
import configure from '@jimp/custom';
5
import blit from '@jimp/plugin-blit';
6
7
import print from '../src';
8
9
const jimp = configure({ plugins: [print, blit] }, Jimp);
10
11
async function createTextImage(
12
width,
13
height,
14
font,
15
{ x = 0, y = 0, text, maxWidth, maxHeight }
16
) {
17
const loadedFont = await jimp.loadFont(font);
18
const image = await Jimp.create(width, height, 0xffffffff);
19
20
return image.print(loadedFont, x, y, text, maxWidth, maxHeight);
21
}
22
23
describe('Write text over image', function() {
24
this.timeout(30000);
25
26
const fontDefs = {
27
SANS_8_BLACK: { w: 28, h: 28, bg: 0xffffffff },
28
29
SANS_16_BLACK: { w: 54, h: 54, bg: 0xffffffff },
30
SANS_32_BLACK: { w: 114, h: 114, bg: 0xffffffff },
31
SANS_64_BLACK: { w: 220, h: 220, bg: 0xffffffff },
32
33
SANS_8_WHITE: { w: 28, h: 28, bg: 0x000000ff },
34
35
SANS_16_WHITE: { w: 54, h: 54, bg: 0x000000ff },
36
SANS_32_WHITE: { w: 114, h: 114, bg: 0x000000ff },
37
SANS_64_WHITE: { w: 220, h: 220, bg: 0x000000ff }
38
};
39
40
for (const fontName in fontDefs)
41
if (hasOwnProp(fontDefs, fontName))
42
((fontName, conf) => {
43
it('Jimp preset ' + fontName + ' bitmap font', async () => {
44
const font = await jimp.loadFont(Jimp['FONT_' + fontName]);
45
const expected =
46
getTestDir(__dirname) + '/images/' + fontName + '.png';
47
48
const expectedImg = await Jimp.read(expected);
49
const image = await Jimp.create(conf.w, conf.h, conf.bg);
50
51
image
52
.print(font, 0, 0, 'This is only a test.', image.bitmap.width)
53
.bitmap.data.should.be.deepEqual(expectedImg.bitmap.data);
54
});
55
})(fontName, fontDefs[fontName]);
56
57
it('Jimp preset SANS_16_BLACK bitmap font positioned', async () => {
58
const font = await jimp.loadFont(Jimp.FONT_SANS_16_BLACK);
59
const expected =
60
getTestDir(__dirname) + '/images/SANS_16_BLACK-positioned.png';
61
const expectedImg = await Jimp.read(expected);
62
const image = await Jimp.create('300', '100', 0xff8800ff);
63
64
image
65
.print(font, 150, 50, 'This is only a test.', 100)
66
.bitmap.data.should.be.deepEqual(expectedImg.bitmap.data);
67
});
68
69
it('Jimp loads font from URL', async () => {
70
const font = await Jimp.loadFont(
71
'https://raw.githubusercontent.com/oliver-moran/jimp/master/packages/plugin-print/fonts/open-sans/open-sans-16-black/open-sans-16-black.fnt'
72
);
73
const expected =
74
getTestDir(__dirname) + '/images/SANS_16_BLACK-positioned.png';
75
const expectedImg = await Jimp.read(expected);
76
const image = await Jimp.create('300', '100', 0xff8800ff);
77
78
image
79
.print(font, 150, 50, 'This is only a test.', 100)
80
.bitmap.data.should.be.deepEqual(expectedImg.bitmap.data);
81
});
82
83
it('Jimp renders ? for unknown characters', async () => {
84
const font = await jimp.loadFont(Jimp.FONT_SANS_16_BLACK);
85
86
const expected = getTestDir(__dirname) + '/images/unknown-char-test.png';
87
const expectedImg = await Jimp.read(expected);
88
const image = await Jimp.read('300', '100', 0xff8800ff);
89
90
image
91
.print(font, 0, 0, 'ツ ツ ツ', 100)
92
.bitmap.data.should.be.deepEqual(expectedImg.bitmap.data);
93
});
94
95
it('Jimp can print numbers too', async () => {
96
const font = await Jimp.loadFont(Jimp.FONT_SANS_16_BLACK);
97
98
const expected = getTestDir(__dirname) + '/images/print-number.png';
99
const expectedImg = await Jimp.read(expected);
100
const image = await Jimp.read('300', '100', 0xff8800ff);
101
102
image
103
.print(font, 0, 0, 12345678, 100)
104
.bitmap.data.should.be.deepEqual(expectedImg.bitmap.data);
105
});
106
107
it('left-align text by default', async () => {
108
const expectedImage = await Jimp.read(
109
getTestDir(__dirname) + '/images/left-aligned.png'
110
);
111
const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {
112
text: 'This is only a test.',
113
114
maxWidth: 100
115
});
116
117
expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);
118
});
119
120
it('left-align text by default when passing object', async () => {
121
const expectedImage = await Jimp.read(
122
getTestDir(__dirname) + '/images/left-aligned.png'
123
);
124
const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {
125
text: { text: 'This is only a test.' },
126
127
maxWidth: 100
128
});
129
130
expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);
131
});
132
133
it('left-align text when passing object with alignmentX', async () => {
134
const expectedImage = await Jimp.read(
135
getTestDir(__dirname) + '/images/left-aligned.png'
136
);
137
const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {
138
text: {
139
text: 'This is only a test.',
140
141
alignmentX: Jimp.HORIZONTAL_ALIGN_LEFT
142
},
143
maxWidth: 100
144
});
145
146
expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);
147
});
148
149
it('center-align text when passing object with alignmentX', async () => {
150
const expectedImage = await Jimp.read(
151
getTestDir(__dirname) + '/images/center-aligned.png'
152
);
153
const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {
154
text: {
155
text: 'This is only a test.',
156
157
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER
158
},
159
maxWidth: 100
160
});
161
162
expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);
163
});
164
165
it('right-align text when passing object with alignmentX', async () => {
166
const expectedImage = await Jimp.read(
167
getTestDir(__dirname) + '/images/right-aligned.png'
168
);
169
const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {
170
text: {
171
text: 'This is only a test.',
172
173
alignmentX: Jimp.HORIZONTAL_ALIGN_RIGHT
174
},
175
maxWidth: 100
176
});
177
178
expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);
179
});
180
181
it('middle-align text when passing object with alignmentY', async () => {
182
const expectedImage = await Jimp.read(
183
getTestDir(__dirname) + '/images/middle-aligned.png'
184
);
185
const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {
186
text: {
187
text: 'This is only a test.',
188
189
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
190
},
191
maxWidth: 100,
192
193
maxHeight: 240
194
});
195
196
expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);
197
});
198
199
it('middle-align text when passing object with alignmentY can offset y', async () => {
200
const expectedImage = await Jimp.read(
201
getTestDir(__dirname) + '/images/middle-aligned-y.png'
202
);
203
const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {
204
y: 50,
205
206
text: {
207
text: 'This is only a test.',
208
209
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
210
},
211
maxWidth: 100,
212
213
maxHeight: 240
214
});
215
216
expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);
217
});
218
219
it('bottom-align text when passing object with alignmentY', async () => {
220
const expectedImage = await Jimp.read(
221
getTestDir(__dirname) + '/images/bottom-aligned.png'
222
);
223
const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {
224
text: {
225
text: 'This is only a test.',
226
227
alignmentY: Jimp.VERTICAL_ALIGN_BOTTOM
228
},
229
maxWidth: 100,
230
231
maxHeight: 240
232
});
233
234
expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);
235
});
236
237
it('bottom-align text when passing object with alignmentY offset y', async () => {
238
const expectedImage = await Jimp.read(
239
getTestDir(__dirname) + '/images/bottom-aligned-y.png'
240
);
241
const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {
242
y: 100,
243
244
text: {
245
text: 'This is only a test.',
246
247
alignmentY: Jimp.VERTICAL_ALIGN_BOTTOM
248
},
249
maxWidth: 100,
250
251
maxHeight: 100
252
});
253
254
expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);
255
});
256
257
it('exposes print y position in cb', async () => {
258
const expectedImage = await Jimp.read(
259
getTestDir(__dirname) + '/images/spacing.png'
260
);
261
262
const loadedFont = await jimp.loadFont(Jimp.FONT_SANS_16_BLACK);
263
const image = await Jimp.create(500, 500, 0xffffffff);
264
265
image.print(
266
loadedFont,
267
0,
268
0,
269
'One two three four fix six seven eight nine ten eleven twelve',
270
250,
271
(err, image, { x, y }) => {
272
image.print(
273
loadedFont,
274
x,
275
y + 50,
276
'thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty',
277
250
278
);
279
}
280
);
281
282
expectedImage.bitmap.data.should.be.deepEqual(image.bitmap.data);
283
});
284
});
285
286