/
text.js
355 lines (331 loc) · 9.92 KB
/
text.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2013 Lovell Fuller and others.
// SPDX-License-Identifier: Apache-2.0
'use strict';
const assert = require('assert');
const sharp = require('../../');
const fixtures = require('../fixtures');
const { inRange } = require('../../lib/is');
describe('Text to image', function () {
this.retries(3);
it('text with default values', async function () {
const output = fixtures.path('output.text-default.png');
const text = sharp({
text: {
text: 'Hello, world !'
}
});
if (!sharp.versions.pango) {
return this.skip();
}
const info = await text.png().toFile(output);
assert.strictEqual('png', info.format);
assert.strictEqual(3, info.channels);
assert.strictEqual(false, info.premultiplied);
assert.ok(info.width > 10);
assert.ok(info.height > 8);
const metadata = await sharp(output).metadata();
assert.strictEqual('uchar', metadata.depth);
assert.strictEqual('srgb', metadata.space);
assert.strictEqual(72, metadata.density);
const stats = await sharp(output).stats();
assert.strictEqual(0, stats.channels[0].min);
assert.strictEqual(255, stats.channels[0].max);
assert.strictEqual(0, stats.channels[1].min);
assert.strictEqual(255, stats.channels[1].max);
assert.strictEqual(0, stats.channels[2].min);
assert.strictEqual(255, stats.channels[2].max);
assert.ok(info.textAutofitDpi > 0);
});
it('text with width and height', function (done) {
const output = fixtures.path('output.text-width-height.png');
const text = sharp({
text: {
text: 'Hello, world!',
width: 500,
height: 400
}
});
if (!sharp.versions.pango) {
return this.skip();
}
text.toFile(output, function (err, info) {
if (err) throw err;
assert.strictEqual('png', info.format);
assert.strictEqual(3, info.channels);
assert.ok(inRange(info.width, 400, 600), `Actual width ${info.width}`);
assert.ok(inRange(info.height, 300, 500), `Actual height ${info.height}`);
assert.ok(inRange(info.textAutofitDpi, 900, 1300), `Actual textAutofitDpi ${info.textAutofitDpi}`);
done();
});
});
it('text with dpi', function (done) {
const output = fixtures.path('output.text-dpi.png');
const dpi = 300;
const text = sharp({
text: {
text: 'Hello, world!',
dpi
}
});
if (!sharp.versions.pango) {
return this.skip();
}
text.toFile(output, function (err, info) {
if (err) throw err;
assert.strictEqual('png', info.format);
sharp(output).metadata(function (err, metadata) {
if (err) throw err;
assert.strictEqual(dpi, metadata.density);
done();
});
});
});
it('text with color and pango markup', function (done) {
const output = fixtures.path('output.text-color-pango.png');
const dpi = 300;
const text = sharp({
text: {
text: '<span foreground="red" font="100">red</span><span font="50" background="cyan">blue</span>',
rgba: true,
dpi
}
});
if (!sharp.versions.pango) {
return this.skip();
}
text.toFile(output, function (err, info) {
if (err) throw err;
assert.strictEqual('png', info.format);
assert.strictEqual(4, info.channels);
sharp(output).metadata(function (err, metadata) {
if (err) throw err;
assert.strictEqual(dpi, metadata.density);
assert.strictEqual('uchar', metadata.depth);
assert.strictEqual(true, metadata.hasAlpha);
done();
});
});
});
it('text with font', function (done) {
const output = fixtures.path('output.text-with-font.png');
const text = sharp({
text: {
text: 'Hello, world!',
font: 'sans 100'
}
});
if (!sharp.versions.pango) {
return this.skip();
}
text.toFile(output, function (err, info) {
if (err) throw err;
assert.strictEqual('png', info.format);
assert.strictEqual(3, info.channels);
assert.ok(info.width > 30);
assert.ok(info.height > 10);
done();
});
});
it('text with justify and composite', function (done) {
const output = fixtures.path('output.text-composite.png');
const width = 500;
const dpi = 300;
const text = sharp(fixtures.inputJpg)
.resize(width)
.composite([{
input: {
text: {
text: '<span foreground="#ffff00">Watermark</span> <span foreground="white"><i>is cool</i></span>',
width: 300,
height: 300,
justify: true,
align: 'right',
spacing: 50,
rgba: true
}
},
gravity: 'northeast'
}, {
input: {
text: {
text: '<span background="cyan">cool</span>',
font: 'sans 30',
dpi,
rgba: true
}
},
left: 30,
top: 250
}]);
if (!sharp.versions.pango) {
return this.skip();
}
text.toFile(output, function (err, info) {
if (err) throw err;
assert.strictEqual('png', info.format);
assert.strictEqual(4, info.channels);
assert.strictEqual(width, info.width);
assert.strictEqual(true, info.premultiplied);
sharp(output).metadata(function (err, metadata) {
if (err) throw err;
assert.strictEqual('srgb', metadata.space);
assert.strictEqual('uchar', metadata.depth);
assert.strictEqual(true, metadata.hasAlpha);
done();
});
});
});
it('bad text input', function () {
assert.throws(function () {
sharp({
text: {
}
});
});
});
it('fontfile input', function () {
assert.doesNotThrow(function () {
sharp({
text: {
text: 'text',
fontfile: 'UnknownFont.ttf'
}
});
});
});
it('bad font input', function () {
assert.throws(function () {
sharp({
text: {
text: 'text',
font: 12
}
});
});
});
it('bad fontfile input', function () {
assert.throws(function () {
sharp({
text: {
text: 'text',
fontfile: true
}
});
});
});
it('invalid width', () => {
assert.throws(
() => sharp({ text: { text: 'text', width: 'bad' } }),
/Expected positive integer for text\.width but received bad of type string/
);
assert.throws(
() => sharp({ text: { text: 'text', width: 0.1 } }),
/Expected positive integer for text\.width but received 0.1 of type number/
);
assert.throws(
() => sharp({ text: { text: 'text', width: -1 } }),
/Expected positive integer for text\.width but received -1 of type number/
);
});
it('invalid height', () => {
assert.throws(
() => sharp({ text: { text: 'text', height: 'bad' } }),
/Expected positive integer for text\.height but received bad of type string/
);
assert.throws(
() => sharp({ text: { text: 'text', height: 0.1 } }),
/Expected positive integer for text\.height but received 0.1 of type number/
);
assert.throws(
() => sharp({ text: { text: 'text', height: -1 } }),
/Expected positive integer for text\.height but received -1 of type number/
);
});
it('bad align input', function () {
assert.throws(function () {
sharp({
text: {
text: 'text',
align: 'unknown'
}
});
});
});
it('bad justify input', function () {
assert.throws(function () {
sharp({
text: {
text: 'text',
justify: 'unknown'
}
});
});
});
it('invalid dpi', () => {
assert.throws(
() => sharp({ text: { text: 'text', dpi: 'bad' } }),
/Expected integer between 1 and 1000000 for text\.dpi but received bad of type string/
);
assert.throws(
() => sharp({ text: { text: 'text', dpi: 0.1 } }),
/Expected integer between 1 and 1000000 for text\.dpi but received 0.1 of type number/
);
assert.throws(
() => sharp({ text: { text: 'text', dpi: -1 } }),
/Expected integer between 1 and 1000000 for text\.dpi but received -1 of type number/
);
});
it('bad rgba input', function () {
assert.throws(function () {
sharp({
text: {
text: 'text',
rgba: -10
}
});
});
});
it('invalid spacing', () => {
assert.throws(
() => sharp({ text: { text: 'text', spacing: 'bad' } }),
/Expected integer between -1000000 and 1000000 for text\.spacing but received bad of type string/
);
assert.throws(
() => sharp({ text: { text: 'text', spacing: 0.1 } }),
/Expected integer between -1000000 and 1000000 for text\.spacing but received 0.1 of type number/
);
assert.throws(
() => sharp({ text: { text: 'text', spacing: -1000001 } }),
/Expected integer between -1000000 and 1000000 for text\.spacing but received -1000001 of type number/
);
});
it('only height or dpi not both', function () {
assert.throws(function () {
sharp({
text: {
text: 'text',
height: 400,
dpi: 100
}
});
});
});
it('valid wrap throws', () => {
assert.doesNotThrow(() => sharp({ text: { text: 'text', wrap: 'none' } }));
assert.doesNotThrow(() => sharp({ text: { text: 'text', wrap: 'word-char' } }));
});
it('invalid wrap throws', () => {
assert.throws(
() => sharp({ text: { text: 'text', wrap: 1 } }),
/Expected one of: word, char, word-char, none for text\.wrap but received 1 of type number/
);
assert.throws(
() => sharp({ text: { text: 'text', wrap: false } }),
/Expected one of: word, char, word-char, none for text\.wrap but received false of type boolean/
);
assert.throws(
() => sharp({ text: { text: 'text', wrap: 'invalid' } }),
/Expected one of: word, char, word-char, none for text\.wrap but received invalid of type string/
);
});
});