Skip to content

Commit a43e20f

Browse files
committed
name Colors.gradient with its class and constructors with new, and document the path budget
1 parent 78de882 commit a43e20f

7 files changed

Lines changed: 16 additions & 11 deletions

File tree

‎CHANGELOG.md‎

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

137137
### Fixes
138138

139-
- `renderSVG()` bounds the curves an arc between far-apart ends is drawn with and the points paths flatten to: a 139-byte path (`M 1e25 100 A 40 30 20 1 1 160 100`, stroked) ran the process out of memory. Too many points throw a `LimitError`.
139+
- `renderSVG()` bounds the points paths are made of: an arc between far-apart ends is drawn with at most 64 curves a turn (a 139-byte path, `M 1e25 100 A 40 30 20 1 1 160 100`, stroked, ran the process out of memory), and the curves of arcs and the points of all paths, also of each copy drawn through `<use>`, count against 2^25 points plus 4 per pixel of the output in one render and 2^21 in one path. More throw a `LimitError`. A path drawn through `<use>` is read once a render instead of once a copy.
140140
- `encodeJPEG()` and `encodeWebP()` check the image's size before going over its pixels; `metadata.creationTime` of another type than a `Date`, a number or a string throws a `TypeError` (`true` was 1970).
141141
- `Colors.gradient()` has the later stop's color at every hard edge (two stops at one position), like CSS, also inside the gradient and with 2 stops.
142142
- `encodeJPEG()` of an image with transparency composites it onto the background row by row instead of copying the image first: the peak memory of a 6000x6000 image is 378 MB instead of 485 MB, the same as without transparency, and the file is the same (images with a gain map still take the copy).

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
**ImageScript** decodes, edits and encodes images in pure TypeScript: no WebAssembly, no native binaries, and no
1717
runtime dependencies. It runs in Node.js 22.13+, Deno, Bun, browsers and edge runtimes (Cloudflare Workers, Vercel's
1818
Edge Runtime, Deno Deploy), works with `import` and, on supported Node.js versions, `require()`, and ships with type
19-
definitions (TypeScript 5.7 or newer; CommonJS projects with `module: nodenext` need 5.8 or newer to `import` it. With older versions and `skipLibCheck`, the encoders' return types become `any`). Large AVIF and JPEG XL operations, and lossless WebP encoding, can use multiple CPU cores when the runtime
19+
definitions (TypeScript 5.7 or newer; CommonJS projects need `module: nodenext` and 5.8 or newer to `import` it (`module: node16` can't). With older versions and `skipLibCheck`, the encoders' return types become `any`). Large AVIF and JPEG XL operations, and lossless WebP encoding, can use multiple CPU cores when the runtime
2020
supports it.
2121

2222
```sh

‎src/animation.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class Animation {
120120
*/
121121
constructor(frames: Frame[], options?: AnimationCanvasOptions);
122122
constructor(frames: Frame[], options: AnimationCanvasOptions = {}) {
123-
checkOptions(options, 'Animation(frames, { loopCount: 2 })');
123+
checkOptions(options, 'new Animation(frames, { loopCount: 2 })');
124124
const { loopCount: count = Infinity, width, height } = options;
125125
const loops = loopCount(count);
126126
if ((width === undefined) !== (height === undefined)) throw new TypeError('Animation() takes both a canvas width and height, or neither');

‎src/colors.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,10 @@ export class Colors {
200200
* @return The gradient generator. The function argument is the position in the gradient (0..1).
201201
*/
202202
static gradient(stops: Record<number, Color> | Color[]): (position: number) => Color {
203-
if (typeof stops !== 'object' || stops === null) throw new TypeError(`gradient() takes an array of colors or an object of positions and colors, not ${shown(stops)}`);
203+
if (typeof stops !== 'object' || stops === null) throw new TypeError(`Colors.gradient() takes an array of colors or an object of positions and colors, not ${shown(stops)}`);
204204
let colors: Record<number, Color>;
205205
if (Array.isArray(stops)) {
206-
if (!stops.length) throw new RangeError('gradient() takes at least one color, not 0');
206+
if (!stops.length) throw new RangeError('Colors.gradient() takes at least one color, not 0');
207207
// (one color is a gradient of two equal ones)
208208
const list = stops.length === 1 ? [stops[0], stops[0]] : stops;
209209
colors = Object.fromEntries(list.map((color, i) => [i / (list.length - 1), color]));
@@ -212,7 +212,7 @@ export class Colors {
212212
const positions = entries.map(e => finite(parseFloat(e[0]), 'gradient position'));
213213
const values = entries.map(e => checkColor(e[1]));
214214

215-
if (positions.length === 0) throw new RangeError('gradient() takes at least one color, not 0');
215+
if (positions.length === 0) throw new RangeError('Colors.gradient() takes at least one color, not 0');
216216
else if (positions.length === 1) {
217217
return position => {
218218
checkPosition(position);

‎src/frame.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class Frame {
8080
constructor(image: Image, options?: FrameOptions);
8181
constructor(image: Image, options: FrameOptions = {}) {
8282
if (!(image instanceof Image)) throw new TypeError('A frame takes an Image, like new Frame(image, { duration: 100 })');
83-
checkOptions(options, 'Frame(image, { duration: 100 })');
83+
checkOptions(options, 'new Frame(image, { duration: 100 })');
8484
const { duration = 100, xOffset: x = 0, yOffset: y = 0, disposalMode: disposal = 'keep' } = options;
8585
this.__image__ = image;
8686
this.duration = duration;

‎src/image.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,11 @@ export interface SVGResourceOptions {
496496
* about 90 bytes per pixel of `maxPixels` (64 for the layers, up to 20 for the images, 4 for the output): lower
497497
* `maxPixels` to bound it for untrusted SVGs.
498498
*
499-
* Two limits on the work of a render don't depend on these options, and grow with the size of the output (so
499+
* Three limits on the work of a render don't depend on these options, and grow with the size of the output (so
500500
* lowering `maxPixels` is what bounds them too). Each throws a {@link LimitError} when exceeded:
501501
* - Painting: shapes and layers may paint 2²⁸ pixels plus 64 per pixel of the output
502+
* - Paths: all paths together may be made of 2²⁵ points plus 4 per pixel of the output (curves flattened, each
503+
* copy drawn through `<use>` counted), one path of 2²¹
502504
* - Filters: all the filters' primitives together may work on 2²⁵ pixels plus 16 per pixel of the output (a
503505
* primitive counts its region's pixels once per pass: an `feConvolveMatrix` per 9 taps of its kernel, an
504506
* `feTurbulence` per octave, an `feMerge` per node)

‎tests/error-messages.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const panic = message => {
99
// A value outside of what an argument takes throws a RangeError that says which argument, what it takes, and which
1010
// value it got: `<name> must be <what it takes>, not <value>` (or `<name> must have <count>, not <count>`, or `<name>
1111
// takes <what>, not <value>`). Only that shape is checked here, the wording of each message is free
12-
const shape = /^[\w() ]+ (must be|must have|takes) .+, not .+$/;
12+
const shape = /^[\w(). ]+ (must be|must have|takes) .+, not .+$/;
1313

1414
(async () => {
1515
const font = fs.readFileSync('./tests/fonts/carbon phyber.ttf');
@@ -105,7 +105,7 @@ const shape = /^[\w() ]+ (must be|must have|takes) .+, not .+$/;
105105
for (const [name, fn, start] of [
106106
['image.scale(2 ** 31)', () => image().scale(2 ** 31), 'scale factor'],
107107
['animation.scale(2 ** 31)', () => new Animation([new Frame(image())]).scale(2 ** 31), 'scale factor'],
108-
['radialGradient without stops', () => Image.radialGradient(1e6, 1e6, {stops: []}), 'gradient()']
108+
['radialGradient without stops', () => Image.radialGradient(1e6, 1e6, {stops: []}), 'Colors.gradient()']
109109
]) {
110110
try {
111111
fn();
@@ -137,7 +137,10 @@ const shape = /^[\w() ]+ (must be|must have|takes) .+, not .+$/;
137137
['creationTime object', () => image().encodePNG({metadata: {creationTime: {}}}), /, not an object$/],
138138
['creationTime array', () => image().encodePNG({metadata: {creationTime: []}}), /, not an array$/],
139139
// a cut string keeps whole characters (no lone half of a surrogate pair)
140-
// statics are named with their class, like Image.decode()
140+
// statics are named with their class, like Image.decode(), and constructors' examples use new
141+
['Colors.gradient', () => Colors.gradient(5), /^Colors\.gradient\(\) takes/],
142+
['Frame options', () => new Frame(image(), 5), /^Frame\(\) takes an options object, like new Frame\(/],
143+
['Animation options', () => new Animation([], 5), /^Animation\(\) takes an options object, like new Animation\(/],
141144
['renderSVG options', () => Image.renderSVG('<svg/>', 5), /^Image\.renderSVG\(\) takes an options object.*, not 5$/],
142145
['renderText options', () => Image.renderText('A', 5), /^Image\.renderText\(\) takes an options object.*, not 5$/],
143146
['renderText text', () => Image.renderText(5, {}), /^Image\.renderText\(\) takes the text as a string.*, not 5$/],

0 commit comments

Comments
 (0)