imagescript 4.0.0-rc.2 → 4.0.0-rc.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/animation-encoder.d.ts +1 -0
- package/dist/animation-encoder.js +15 -2
- package/dist/animation.d.ts +22 -22
- package/dist/animation.js +35 -12
- package/dist/codecs/jxl/frame-header.js +6 -4
- package/dist/codecs/quantize.js +23 -9
- package/dist/gainmap.d.ts +6 -3
- package/dist/gainmap.js +21 -15
- package/dist/image.d.ts +78 -67
- package/dist/image.js +69 -68
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/metadata.d.ts +5 -5
- package/dist/metadata.js +1 -1
- package/dist/render/svg.js +39 -15
- package/dist/render/text.js +10 -8
- package/dist/text-layout.d.ts +21 -11
- package/dist/text-layout.js +17 -30
- package/dist/util/limits.d.ts +35 -0
- package/dist/util/limits.js +1 -0
- package/dist/util/options.js +15 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { Image, DecodeOptions } from './image.js';
|
|
|
7
7
|
import { Animation } from './animation.js';
|
|
8
8
|
export { Image } from './image.js';
|
|
9
9
|
export { Colors } from './colors.js';
|
|
10
|
-
export type { Color, ColorFunction, ResizeMode, ResizeFit, TextStyleOptions, DecodeOptions, CompositeOptions, BlendMode, CropCircleOptions, DominantColorOptions, RotateOptions, ColorToAlphaOptions, ResizeOptions, ResizeToOptions, SharpenOptions, PNGEncodeOptions, JPEGEncodeOptions, WebPEncodeOptions, AVIFEncodeOptions, JXLEncodeOptions, EncodeOptions, TGAEncodeOptions, GIFEncodeOptions, DitherMethod, GainMapEmbedOptions, RenderSVGOptions, RenderTextOptions, ShadowOptions, OutlineOptions, QuantizeOptions, TransformOptions, TransformMode, AffineTransform, GradientOptions, LinearGradientOptions, RadialGradientOptions, RoundCornersOptions, FisheyeOptions, TrimOptions } from './image.js';
|
|
10
|
+
export type { Color, ColorFunction, ResizeMode, ResizeFit, TextStyleOptions, DecodeOptions, CompositeOptions, BlendMode, CropCircleOptions, DominantColorOptions, RotateOptions, ColorToAlphaOptions, ResizeOptions, ResizeToOptions, SharpenOptions, PNGEncodeOptions, JPEGEncodeOptions, WebPEncodeOptions, AVIFEncodeOptions, JXLEncodeOptions, EncodeOptions, TGAEncodeOptions, GIFEncodeOptions, DitherMethod, DitherOptions, GainMapEmbedOptions, RenderSVGOptions, SVGResourceOptions, RenderTextOptions, ShadowOptions, OutlineOptions, QuantizeOptions, TransformOptions, TransformMode, FlipDirection, AffineTransform, GradientOptions, LinearGradientOptions, RadialGradientOptions, RoundCornersOptions, FisheyeOptions, TrimOptions } from './image.js';
|
|
11
11
|
export { Frame } from './frame.js';
|
|
12
12
|
export type { FrameDisposalMode, FrameOptions } from './frame.js';
|
|
13
13
|
export { GainMap } from './gainmap.js';
|
|
@@ -17,6 +17,7 @@ export type { AnimationOptions, AnimationCanvasOptions, AnimationEncodeOptions,
|
|
|
17
17
|
export { AnimationEncoder } from './animation-encoder.js';
|
|
18
18
|
export type { TextLayoutOptions, WrapStyle, HorizontalAlign, VerticalAlign } from './text-layout.js';
|
|
19
19
|
export { ImageType } from './image-type.js';
|
|
20
|
+
export { LimitError } from './util/limits.js';
|
|
20
21
|
export type { ImageTypeName } from './image-type.js';
|
|
21
22
|
export type { ToneMapping } from './codecs/hdr.js';
|
|
22
23
|
export type { BinaryData } from './util/mem.js';
|
package/dist/index.js
CHANGED
|
@@ -12,6 +12,7 @@ export { GainMap } from "./gainmap.js";
|
|
|
12
12
|
export { Animation } from "./animation.js";
|
|
13
13
|
export { AnimationEncoder } from "./animation-encoder.js";
|
|
14
14
|
export { ImageType } from "./image-type.js";
|
|
15
|
+
export { LimitError } from "./util/limits.js";
|
|
15
16
|
export async function decode(input, options = {}) {
|
|
16
17
|
checkOptions(options, 'decode(data, { maxPixels: 1e8 })');
|
|
17
18
|
const bytes = view(input);
|
package/dist/metadata.d.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* | GIF | software, comment (comment extensions) |
|
|
10
10
|
* | TGA | author, comment, creation time, software (extension area) |
|
|
11
11
|
*
|
|
12
|
-
* Empty fields are left out. Without metadata, the files only say which software made them (`software`), so the same
|
|
12
|
+
* Empty fields (and `null` ones) are left out. Without metadata, the files only say which software made them (`software`), so the same
|
|
13
13
|
* image always encodes to the same bytes
|
|
14
14
|
*/
|
|
15
15
|
export interface Metadata {
|
|
@@ -21,10 +21,10 @@ export interface Metadata {
|
|
|
21
21
|
description?: string;
|
|
22
22
|
/** The image's copyright info */
|
|
23
23
|
copyright?: string;
|
|
24
|
-
/** The image's creation time. Default: none (
|
|
25
|
-
creationTime?: string | number | Date;
|
|
26
|
-
/** The software used to create the image. Default: `'ImageScript'` (without a version), `
|
|
27
|
-
software?: string;
|
|
24
|
+
/** The image's creation time. Default: none (`null` leaves it out, too) */
|
|
25
|
+
creationTime?: string | number | Date | null;
|
|
26
|
+
/** The software used to create the image. Default: `'ImageScript'` (without a version), `null` leaves it out */
|
|
27
|
+
software?: string | null;
|
|
28
28
|
/** A disclaimer for the image */
|
|
29
29
|
disclaimer?: string;
|
|
30
30
|
/** A warning for the image */
|
package/dist/metadata.js
CHANGED
|
@@ -4,7 +4,7 @@ const TEXT_FIELDS = ['title', 'author', 'description', 'copyright', 'software',
|
|
|
4
4
|
export function resolveMetadata(metadata = {}) {
|
|
5
5
|
const { creationTime, software } = metadata || {};
|
|
6
6
|
const resolved = {};
|
|
7
|
-
if (creationTime !== undefined) {
|
|
7
|
+
if (creationTime !== undefined && creationTime !== null) {
|
|
8
8
|
const time = new Date(creationTime);
|
|
9
9
|
if (isNaN(time.getTime()))
|
|
10
10
|
throw new RangeError('Invalid creation time');
|
package/dist/render/svg.js
CHANGED
|
@@ -556,10 +556,12 @@ function validProperty(name, value) {
|
|
|
556
556
|
}
|
|
557
557
|
}
|
|
558
558
|
const MAX_INSTANCES = 100000;
|
|
559
|
+
const MAX_OCTAVES = 24;
|
|
560
|
+
const MAX_NESTED = 2 ** 26, MIN_NESTED = 2 ** 16;
|
|
559
561
|
const MAX_DASHES = 1000000;
|
|
560
562
|
const MAX_TILE = 4096 * 1024;
|
|
561
563
|
class Document {
|
|
562
|
-
budget = { instances: 0 };
|
|
564
|
+
budget = { instances: 0, nested: 0, nesting: 0 };
|
|
563
565
|
ids;
|
|
564
566
|
active;
|
|
565
567
|
root;
|
|
@@ -1435,10 +1437,12 @@ class Document {
|
|
|
1435
1437
|
matrix = multiply(matrix, [sx * units, 0, 0, sy * units, -refX * sx * units, -refY * sy * units]);
|
|
1436
1438
|
const inner = { ...ctx, matrix, style: markerStyle, depth: ctx.depth + 1, viewport: viewBox ? { width: viewBox[2], height: viewBox[3] } : ctx.viewport, contextElement };
|
|
1437
1439
|
this.active.add(marker);
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1440
|
+
this.indirect(0, () => {
|
|
1441
|
+
if (!clip)
|
|
1442
|
+
this.renderChildren(marker, inner);
|
|
1443
|
+
else
|
|
1444
|
+
this.clipped(inner, Math.min(clip[0], clip[2]), Math.min(clip[1], clip[3]), Math.abs(clip[2] - clip[0]), Math.abs(clip[3] - clip[1]), layer => this.renderChildren(marker, { ...inner, layer }));
|
|
1445
|
+
});
|
|
1442
1446
|
this.active.delete(marker);
|
|
1443
1447
|
};
|
|
1444
1448
|
if (k === 0) {
|
|
@@ -1676,8 +1680,8 @@ class Document {
|
|
|
1676
1680
|
contentMatrix = multiply(contentMatrix, [box.width, 0, 0, box.height, 0, 0]);
|
|
1677
1681
|
const tile = new Layer(width, height);
|
|
1678
1682
|
this.active.add(node);
|
|
1679
|
-
this.renderChildren(content, { ...ctx, matrix: contentMatrix, style: this.styleOf(content), layer: tile, depth: ctx.depth + 1,
|
|
1680
|
-
viewport: viewBox ? { width: viewBox[2], height: viewBox[3] } : ctx.viewport });
|
|
1683
|
+
this.indirect(width * height, () => this.renderChildren(content, { ...ctx, matrix: contentMatrix, style: this.styleOf(content), layer: tile, depth: ctx.depth + 1,
|
|
1684
|
+
viewport: viewBox ? { width: viewBox[2], height: viewBox[3] } : ctx.viewport }));
|
|
1681
1685
|
this.active.delete(node);
|
|
1682
1686
|
tile.release();
|
|
1683
1687
|
if (tile.x1 <= tile.x0)
|
|
@@ -1939,7 +1943,7 @@ class Document {
|
|
|
1939
1943
|
let [fx, fy] = list && list.length === 2 ? list : list && list.length === 1 ? [list[0], list[0]] : [0, 0];
|
|
1940
1944
|
if (!(fx >= 0 && fy >= 0))
|
|
1941
1945
|
fx = fy = 0;
|
|
1942
|
-
const octaves = Math.max(0, number('numOctaves', 1));
|
|
1946
|
+
const octaves = Math.min(MAX_OCTAVES, Math.max(0, number('numOctaves', 1)));
|
|
1943
1947
|
return { type: 'turbulence', fx, fy, octaves: Math.round(octaves), seed: Math.trunc(number('seed', 0)), stitch: node.attrs.stitchTiles?.trim() === 'stitch',
|
|
1944
1948
|
fractal: node.attrs.type?.trim() === 'fractalNoise' };
|
|
1945
1949
|
}
|
|
@@ -2001,10 +2005,12 @@ class Document {
|
|
|
2001
2005
|
this.active.add(node);
|
|
2002
2006
|
const parentStyle = (target || node).parent && (target || node).parent.name !== '#root' ? this.styleOf((target || node).parent) : null;
|
|
2003
2007
|
const ctx = { matrix, style: parentStyle, viewport, layer, depth: 16 };
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
+
this.indirect(width * height, () => {
|
|
2009
|
+
if (target)
|
|
2010
|
+
this.renderNode(target, ctx);
|
|
2011
|
+
else
|
|
2012
|
+
this.renderImage(image, { ...ctx, style: this.computeStyle(image, this.styleOf(node)) });
|
|
2013
|
+
});
|
|
2008
2014
|
this.active.delete(node);
|
|
2009
2015
|
const pixmap = layer.toPixmap();
|
|
2010
2016
|
layer.release();
|
|
@@ -2117,7 +2123,22 @@ class Document {
|
|
|
2117
2123
|
const rect = { x: bbox.x + f[0] * bbox.width, y: bbox.y + f[0] * bbox.height, width: f[1] * bbox.width, height: f[1] * bbox.height };
|
|
2118
2124
|
return { rect, primitives: [{ rect, linear: false, result: 'result', kind }] };
|
|
2119
2125
|
}
|
|
2126
|
+
indirect(pixels, render) {
|
|
2127
|
+
const budget = this.budget;
|
|
2128
|
+
if (budget.nesting > 0 && (budget.nested += Math.max(pixels, MIN_NESTED)) > MAX_NESTED)
|
|
2129
|
+
throw new Error('SVG: too many masks, clip paths, patterns, markers or filter images inside each other');
|
|
2130
|
+
budget.nesting++;
|
|
2131
|
+
try {
|
|
2132
|
+
return render();
|
|
2133
|
+
}
|
|
2134
|
+
finally {
|
|
2135
|
+
budget.nesting--;
|
|
2136
|
+
}
|
|
2137
|
+
}
|
|
2120
2138
|
clipMask(clip, node, matrix, context) {
|
|
2139
|
+
return this.indirect(context.layer.width * context.layer.height, () => this.clipMaskOf(clip, node, matrix, context));
|
|
2140
|
+
}
|
|
2141
|
+
clipMaskOf(clip, node, matrix, context) {
|
|
2121
2142
|
const { width, height } = context.layer;
|
|
2122
2143
|
const alpha = new Float32Array(width * height);
|
|
2123
2144
|
const style = this.styleOf(clip);
|
|
@@ -2209,6 +2230,9 @@ class Document {
|
|
|
2209
2230
|
return box;
|
|
2210
2231
|
}
|
|
2211
2232
|
maskValues(mask, node, matrix, context) {
|
|
2233
|
+
return this.indirect(context.layer.width * context.layer.height, () => this.maskValuesOf(mask, node, matrix, context));
|
|
2234
|
+
}
|
|
2235
|
+
maskValuesOf(mask, node, matrix, context) {
|
|
2212
2236
|
const { width, height } = context.layer;
|
|
2213
2237
|
const values = new Float32Array(width * height);
|
|
2214
2238
|
const path = shapePath(node, context.viewport, 16);
|
|
@@ -2779,7 +2803,7 @@ function rasterize(polygons, evenOdd, width, height) {
|
|
|
2779
2803
|
const steps = scratch.f32(1, w + 2);
|
|
2780
2804
|
const weight = 1 / SAMPLES;
|
|
2781
2805
|
const rowStarts = scratch.i32(1, h + 1).fill(0, 0, h + 1), order = scratch.i32(2, numEdges);
|
|
2782
|
-
const startRow = (e) => Math.max(0, Math.floor(tops[e]) - y0);
|
|
2806
|
+
const startRow = (e) => bottoms[e] <= y0 ? h : Math.max(0, Math.floor(tops[e]) - y0);
|
|
2783
2807
|
for (let e = 0; e < numEdges; e++)
|
|
2784
2808
|
if (startRow(e) < h)
|
|
2785
2809
|
rowStarts[startRow(e) + 1]++;
|
|
@@ -2843,7 +2867,7 @@ function rasterize(polygons, evenOdd, width, height) {
|
|
|
2843
2867
|
while (s1 >= 0 && !(py + OFFSETS[s1] < bottom))
|
|
2844
2868
|
s1--;
|
|
2845
2869
|
firsts[k] = s0;
|
|
2846
|
-
lasts[k] = s1;
|
|
2870
|
+
lasts[k] = Math.max(-1, s1);
|
|
2847
2871
|
for (let s = s0; s <= s1; s++)
|
|
2848
2872
|
counts[s]++;
|
|
2849
2873
|
if (s1 >= s0)
|
|
@@ -3390,7 +3414,7 @@ function strokePolygons(subpaths, halfWidth, cap, join, miterLimit, tolerance) {
|
|
|
3390
3414
|
}
|
|
3391
3415
|
function parseTransform(value) {
|
|
3392
3416
|
let matrix = [1, 0, 0, 1, 0, 0];
|
|
3393
|
-
const functions = /(matrix|translate|scale|rotate|skewX|skewY)\s*\(([^)]*)\)/g;
|
|
3417
|
+
const functions = /(matrix|translate|scale|rotate|skewX|skewY)\s*\(([^()]*)\)/g;
|
|
3394
3418
|
for (const [, name, args] of value.matchAll(functions)) {
|
|
3395
3419
|
const a = parseNumberList(args);
|
|
3396
3420
|
let m = [1, 0, 0, 1, 0, 0];
|
package/dist/render/text.js
CHANGED
|
@@ -121,21 +121,23 @@ async function layOut(fonts, size, text, layout, hinting, foreground, features,
|
|
|
121
121
|
const primary = font(0);
|
|
122
122
|
const ascent = Math.ceil(primary.font.ascender * primary.scale);
|
|
123
123
|
const lineHeight = Math.max(1, Math.ceil((primary.font.ascender - primary.font.descender + primary.font.lineGap) * primary.scale));
|
|
124
|
-
const paragraphs =
|
|
124
|
+
const paragraphs = text.split(/\r\n|[\r\n\u0085\u2028\u2029]/);
|
|
125
125
|
const lines = [];
|
|
126
126
|
for (const paragraph of paragraphs)
|
|
127
127
|
lines.push(...layOutParagraph(analyze(paragraph, fonts.length, font), layout, font, size, features));
|
|
128
128
|
let visible = lines;
|
|
129
|
-
if (
|
|
130
|
-
const count = Math.max(1, Math.floor(layout.
|
|
129
|
+
if (layout.height !== undefined) {
|
|
130
|
+
const count = Math.max(1, Math.floor(layout.height / lineHeight));
|
|
131
131
|
if (count < lines.length) {
|
|
132
132
|
const start = layout.verticalAlign === 'bottom' ? lines.length - count
|
|
133
133
|
: layout.verticalAlign === 'middle' ? lines.length - count >> 1 : 0;
|
|
134
134
|
visible = lines.slice(start, start + count);
|
|
135
135
|
}
|
|
136
136
|
}
|
|
137
|
-
const width = Math.max(1, Math.ceil(Math.max(0, ...visible.map(line => line.extent))));
|
|
138
|
-
const height = visible.length * lineHeight;
|
|
137
|
+
const width = layout.width ?? Math.max(1, Math.ceil(Math.max(0, ...visible.map(line => line.extent))));
|
|
138
|
+
const height = layout.height ?? visible.length * lineHeight;
|
|
139
|
+
const free = height - visible.length * lineHeight;
|
|
140
|
+
const top = layout.verticalAlign === 'bottom' ? free : layout.verticalAlign === 'middle' ? Math.floor(free / 2) : 0;
|
|
139
141
|
if (!((width + 2 * pad) * (height + 2 * pad) <= maxPixels))
|
|
140
142
|
throw new LimitError(`Text too large to render: ${width + 2 * pad}x${height + 2 * pad} pixels exceed the limit of ${maxPixels} (the render options' maxPixels)`);
|
|
141
143
|
const coverage = new Uint8Array(width * height);
|
|
@@ -145,7 +147,7 @@ async function layOut(fonts, size, text, layout, hinting, foreground, features,
|
|
|
145
147
|
: layout.horizontalAlign === 'center' ? (width - line.extent) / 2 : 0;
|
|
146
148
|
if (primary.hinter)
|
|
147
149
|
offset = Math.round(offset);
|
|
148
|
-
const baseline = ascent + n * lineHeight;
|
|
150
|
+
const baseline = top + ascent + n * lineHeight;
|
|
149
151
|
for (const { x, dx, dy, glyph: g } of line.glyphs) {
|
|
150
152
|
if (!g.ink)
|
|
151
153
|
continue;
|
|
@@ -306,11 +308,11 @@ function layOutParagraph(p, layout, font, size, features) {
|
|
|
306
308
|
const end = i + 1;
|
|
307
309
|
if (!starts[end])
|
|
308
310
|
continue;
|
|
309
|
-
if (p.breaks[end] === 2 && end < n
|
|
311
|
+
if (p.breaks[end] === 2 && end < n) {
|
|
310
312
|
fit = end;
|
|
311
313
|
break;
|
|
312
314
|
}
|
|
313
|
-
const fits = used - trailing <= layout.
|
|
315
|
+
const fits = used - trailing <= (layout.width ?? Infinity);
|
|
314
316
|
if (!fits)
|
|
315
317
|
break;
|
|
316
318
|
if (canBreak(end))
|
package/dist/text-layout.d.ts
CHANGED
|
@@ -1,21 +1,31 @@
|
|
|
1
1
|
/** Where lines are wrapped: between words or at any character */
|
|
2
2
|
export type WrapStyle = 'word' | 'char';
|
|
3
|
-
/**
|
|
3
|
+
/** Where the lines are placed horizontally: in the `width`, or (without one) relative to the widest line */
|
|
4
4
|
export type HorizontalAlign = 'left' | 'center' | 'right';
|
|
5
|
-
/**
|
|
5
|
+
/** Where the text is placed vertically in the `height` (and which lines are kept when not all of them fit) */
|
|
6
6
|
export type VerticalAlign = 'top' | 'middle' | 'bottom';
|
|
7
|
-
/** The layout options of {@link Image.renderText} (part of {@link RenderTextOptions}):
|
|
7
|
+
/** The layout options of {@link Image.renderText} (part of {@link RenderTextOptions}): the size, wrapping and alignment */
|
|
8
8
|
export interface TextLayoutOptions {
|
|
9
|
-
/**
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
/**
|
|
10
|
+
* The image's width in pixels: lines are wrapped to fit into it (inside the outline, see
|
|
11
|
+
* {@link TextStyleOptions.strokeWidth}) and placed in it by `horizontalAlign`. Default: the widest line's width
|
|
12
|
+
*/
|
|
13
|
+
width?: number;
|
|
14
|
+
/**
|
|
15
|
+
* The image's height in pixels: lines that don't fit are left out, and the text is placed in it by `verticalAlign`.
|
|
16
|
+
* Default: the height of the lines
|
|
17
|
+
*/
|
|
18
|
+
height?: number;
|
|
13
19
|
/** Whether to wrap between words or at any character. Default: `'word'` */
|
|
14
20
|
wrapStyle?: WrapStyle;
|
|
15
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* Where the lines are placed horizontally: `'left'`, `'center'` or `'right'` in the `width`, or (without one)
|
|
23
|
+
* relative to the widest line. Default: `'left'`
|
|
24
|
+
*/
|
|
16
25
|
horizontalAlign?: HorizontalAlign;
|
|
17
|
-
/**
|
|
26
|
+
/**
|
|
27
|
+
* Where the text is placed vertically in the `height`: `'top'`, `'middle'` or `'bottom'`. When not all lines fit,
|
|
28
|
+
* these keep the first, the middle or the last lines. Without a `height`, it has no effect. Default: `'top'`
|
|
29
|
+
*/
|
|
18
30
|
verticalAlign?: VerticalAlign;
|
|
19
|
-
/** Whether to break lines at newline characters. Default: `true` */
|
|
20
|
-
wrapHardBreaks?: boolean;
|
|
21
31
|
}
|
package/dist/text-layout.js
CHANGED
|
@@ -1,34 +1,21 @@
|
|
|
1
1
|
import { oneOf } from "./util/options.js";
|
|
2
2
|
const HORIZONTAL = ['left', 'center', 'right'];
|
|
3
3
|
const VERTICAL = ['top', 'middle', 'bottom'];
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (!HORIZONTAL.includes(horizontalAlign))
|
|
22
|
-
throw new TypeError(`horizontalAlign must be 'left', 'center' or 'right', not '${horizontalAlign}'`);
|
|
23
|
-
this.horizontalAlign = horizontalAlign;
|
|
24
|
-
}
|
|
25
|
-
if (verticalAlign !== undefined) {
|
|
26
|
-
if (!VERTICAL.includes(verticalAlign))
|
|
27
|
-
throw new TypeError(`verticalAlign must be 'top', 'middle' or 'bottom', not '${verticalAlign}'`);
|
|
28
|
-
this.verticalAlign = verticalAlign;
|
|
29
|
-
}
|
|
30
|
-
this.wrapHardBreaks = typeof wrapHardBreaks === 'undefined' ? true : wrapHardBreaks;
|
|
31
|
-
if (typeof this.wrapHardBreaks !== 'boolean')
|
|
32
|
-
throw new TypeError('Invalid wrapHardBreaks');
|
|
33
|
-
}
|
|
4
|
+
function boxSide(value, name) {
|
|
5
|
+
if (value === undefined)
|
|
6
|
+
return undefined;
|
|
7
|
+
const side = Math.floor(Number(value));
|
|
8
|
+
if (typeof value !== 'number' || !(side >= 1) || !Number.isFinite(side))
|
|
9
|
+
throw new RangeError(`${name} must be a finite number from 1, not ${String(value)}`);
|
|
10
|
+
return side;
|
|
11
|
+
}
|
|
12
|
+
export function textLayout(options) {
|
|
13
|
+
const { width, height, wrapStyle, verticalAlign, horizontalAlign } = options;
|
|
14
|
+
return {
|
|
15
|
+
width: boxSide(width, 'width'),
|
|
16
|
+
height: boxSide(height, 'height'),
|
|
17
|
+
wrapStyle: wrapStyle === undefined ? 'word' : oneOf(wrapStyle, ['word', 'char'], 'wrapStyle'),
|
|
18
|
+
horizontalAlign: horizontalAlign === undefined ? 'left' : oneOf(horizontalAlign, HORIZONTAL, 'horizontalAlign'),
|
|
19
|
+
verticalAlign: verticalAlign === undefined ? 'top' : oneOf(verticalAlign, VERTICAL, 'verticalAlign')
|
|
20
|
+
};
|
|
34
21
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when an image exceeds a limit of the decode options (`maxPixels`) or the render options of SVGs
|
|
3
|
+
* @hideconstructor
|
|
4
|
+
*/
|
|
5
|
+
export declare class LimitError extends RangeError {
|
|
6
|
+
/** `'LimitError'`: tells a limit apart from other `RangeError`s */
|
|
7
|
+
name: string;
|
|
8
|
+
}
|
|
9
|
+
/** The default of the decode options' maxPixels: 16383 squared (like sharp's limitInputPixels) */
|
|
10
|
+
export declare const MAX_PIXELS: number;
|
|
11
|
+
/**
|
|
12
|
+
* Throws if decoding an image of the given size would exceed the limit, before anything that size is allocated
|
|
13
|
+
* @param count the number of such images (frames)
|
|
14
|
+
*/
|
|
15
|
+
export declare function checkPixels(width: number, height: number, limit: number, count?: number): void;
|
|
16
|
+
/** The most bytes of raw samples per pixel of the limit (4 samples of 16 bits) */
|
|
17
|
+
export declare const MAX_BYTES_PER_PIXEL = 8;
|
|
18
|
+
/**
|
|
19
|
+
* Throws if an image's raw samples (bytes) would exceed what the pixel limit allows, for formats with any number of
|
|
20
|
+
* samples per pixel
|
|
21
|
+
*/
|
|
22
|
+
export declare function checkBytes(bytes: number, limit: number): void;
|
|
23
|
+
/** Counts the pixels of decoded frames against a limit */
|
|
24
|
+
export declare class PixelBudget {
|
|
25
|
+
private left;
|
|
26
|
+
private limit;
|
|
27
|
+
constructor(limit: number);
|
|
28
|
+
/** Throws if another frame of the given size would exceed the limit */
|
|
29
|
+
take(width: number, height: number): void;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Runs a decoder: reading past the end of damaged or truncated data throws a RangeError, which becomes an Error saying
|
|
33
|
+
* so (with the RangeError as its cause); exceeded limits stay RangeErrors
|
|
34
|
+
*/
|
|
35
|
+
export declare function decoding<T>(decode: () => T | Promise<T>): Promise<T>;
|
package/dist/util/limits.js
CHANGED
package/dist/util/options.js
CHANGED
|
@@ -49,6 +49,21 @@ export function quality(value, name = 'quality') {
|
|
|
49
49
|
throw new RangeError(`${name} must be 0-100, not ${q}`);
|
|
50
50
|
return q;
|
|
51
51
|
}
|
|
52
|
+
export function pixelLimit(value, name = 'maxPixels') {
|
|
53
|
+
if (!(Number(value) > 0))
|
|
54
|
+
throw new RangeError(`${name} must be a positive number, not ${value}`);
|
|
55
|
+
return Number(value);
|
|
56
|
+
}
|
|
57
|
+
export function gainMapScale(value) {
|
|
58
|
+
const scale = finite(value, 'gain map scale');
|
|
59
|
+
if (!(scale > 0 && scale <= 1))
|
|
60
|
+
throw new RangeError(`The gain map scale must be above 0 and at most 1, not ${scale}`);
|
|
61
|
+
return scale;
|
|
62
|
+
}
|
|
63
|
+
export function checkThreads(threads) {
|
|
64
|
+
if (!Number.isInteger(threads) || threads < 1)
|
|
65
|
+
throw new RangeError(`threads must be a positive integer, not ${threads}`);
|
|
66
|
+
}
|
|
52
67
|
export function effort(value, max) {
|
|
53
68
|
if (!Number.isInteger(value) || value < 0 || value > max)
|
|
54
69
|
throw new RangeError(`effort must be an integer 0-${max}, not ${value}`);
|