Skip to content

Commit 97475e9

Browse files
committed
check renderText's stroke color without a stroke and keep an encoder usable after an empty finish()
1 parent d7aad92 commit 97475e9

4 files changed

Lines changed: 41 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ Upgrading from 3.x? Upgrade to the latest 3.x release first, fix the deprecation
136136

137137
### Fixes
138138

139+
- `Image.renderText()` checks `strokeColor` without a `strokeWidth` too (a wrong color passed until a stroke was set), and an animation encoder whose `finish()` found no frame can still be given frames.
139140
- A `creationTime` with a year outside 1000-9999 throws a `RangeError` instead of being written wrapped (TGA) or without 4 digits (Exif).
140141
- An SVG with an element of more than about 100,000 children renders (or throws a `LimitError`) instead of a bare `RangeError` from the call stack.
141142
- A damaged CFF font (a real number in a DICT without its end, which ran the process out of memory within seconds, through `renderText()` and an SVG's `@font-face` alike) throws an `Error`; a CID-keyed one whose FDSelect names a missing private dict throws an `Error` instead of an internal `TypeError`.

‎src/animation-encoder.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ export class AnimationEncoder {
114114
if (this.finished) throw new Error('The animation encoder is finished');
115115
this.finished = true;
116116
await this.queue;
117-
if (!this.count) throw new RangeError('An animation needs at least one frame, not 0');
117+
if (!this.count) {
118+
// (nothing was encoded: the encoder isn't used up, frames can still be added)
119+
this.finished = false;
120+
throw new RangeError('An animation needs at least one frame, not 0');
121+
}
118122
if (this.optimizer) for (const f of this.optimizer.end()) await this.format.frame(f, true);
119123
const optimizer = this.optimizer;
120124
this.optimizer = null;

‎src/image.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,8 +1072,8 @@ export class Image {
10721072

10731073
/**
10741074
* Gets the pixel color at the specified position (0-based). {@link Colors.toRGBA} splits it into its channels
1075-
* @param x The x coordinate
1076-
* @param y The y coordinate
1075+
* @param x The x coordinate (a fraction is dropped: 1.9 is the pixel 1)
1076+
* @param y The y coordinate (likewise)
10771077
* @returns The color value (`0xRRGGBBAA`)
10781078
* @throws `RangeError` if the position isn't inside the image
10791079
* @see {@link Image.pixels} to go over every pixel
@@ -1118,7 +1118,8 @@ export class Image {
11181118
}
11191119

11201120
/**
1121-
* Fills the image data with the supplied color
1121+
* Fills the image data with the supplied color. The image's gain map, if it has one, is made neutral (no boost
1122+
* anywhere): the new pixels have no HDR light beyond their own
11221123
* @param color The color, or a function returning the color of each pixel (checked like any color: a result that
11231124
* isn't a number throws a TypeError, a number that isn't a color a RangeError)
11241125
*/
@@ -1168,7 +1169,8 @@ export class Image {
11681169
declare __moved__?: (scaleX: number, scaleY: number, x: number, y: number) => void;
11691170

11701171
/**
1171-
* Resizes the image by the given factor
1172+
* Resizes the image by the given factor: a side of 7 pixels scaled by 0.5 is 3 (fractions are dropped, a side
1173+
* doesn't go below 1 pixel)
11721174
* @param factor The factor to resize the image with
11731175
* @param options See {@link ResizeOptions}
11741176
*/
@@ -3119,6 +3121,8 @@ export class Image {
31193121
if (!fonts.length) throw new TypeError('font must be a font or an array of one or more fonts, not an empty array');
31203122

31213123
const [r, g, b, a] = Colors.toRGBA(color);
3124+
// (checked with the stroke width at 0 too: a wrong color doesn't pass until a stroke is set)
3125+
const [sr, sg, sb, sa] = Colors.toRGBA(strokeColor);
31223126
// the outline needs room around the glyphs (checked against maxPixels with them): a given size is the image's,
31233127
// the text goes inside the outline
31243128
const pad = Math.ceil(strokeWidth);
@@ -3159,7 +3163,6 @@ export class Image {
31593163
for (let i = 0; i < shape.length; i++) shape[i] = Math.max(shape[i], colored[i * 4 + 3]);
31603164
}
31613165
const stroke = strokeWidth ? outline(shape, width, height, strokeWidth) : null;
3162-
const [sr, sg, sb, sa] = Colors.toRGBA(strokeColor);
31633166

31643167
// color glyphs over the text (premultiplied, with the text's alpha), both within their outline
31653168
for (let i = 0; i < coverage.length; i++) {

‎tests/review9-api.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const fs = require('fs');
2+
const {Image, Animation, Frame} = require('..');
3+
4+
const panic = message => {
5+
console.error(message);
6+
process.exit(1);
7+
};
8+
9+
(async () => {
10+
// renderText checks strokeColor like color, with or without a stroke: a wrong one doesn't pass until the width is set
11+
const font = fs.readFileSync('./tests/fonts/carbon phyber.ttf');
12+
for (const strokeWidth of [0, 2]) {
13+
const error = await Image.renderText('Hi', {font, size: 32, strokeWidth, strokeColor: 'bad'}).then(() => null, err => err);
14+
if (!(error instanceof TypeError) || !/not 'bad'$/.test(error.message)) panic(`strokeColor 'bad' with strokeWidth ${strokeWidth}: ${error?.constructor?.name}: ${error?.message}`);
15+
}
16+
await Image.renderText('Hi', {font, size: 32, strokeColor: 0xff0000ff});
17+
18+
// an encoder that has no frame at finish() isn't used up by the error: frames can be added and it finishes
19+
const encoder = Animation.gifEncoder(4, 4);
20+
const error = await encoder.finish().then(() => null, err => err);
21+
if (!(error instanceof RangeError)) panic(`finish() without frames: ${error?.constructor?.name}`);
22+
await encoder.add(new Frame(new Image(4, 4).fill(0xff0000ff)));
23+
const file = await encoder.finish();
24+
if (file.length < 20) panic('the encoder should finish after the frame was added');
25+
const again = await encoder.finish().then(() => null, err => err);
26+
if (!(again instanceof Error) || /frame/.test(again.message)) panic(`finish() twice: ${again?.message}`);
27+
})();

0 commit comments

Comments
 (0)