imagescript 3.20.3 → 4.0.0-rc.2
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/LICENSE +16 -12
- package/README.md +21 -12
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/dist/animation-encoder.d.ts +1 -34
- package/dist/animation-encoder.js +35 -34
- package/dist/animation.d.ts +108 -122
- package/dist/animation.js +285 -121
- package/dist/codecs/av1/decode.js +1 -1
- package/dist/codecs/av1/encode.js +1 -1
- package/dist/codecs/gif.js +11 -9
- package/dist/codecs/hdr.d.ts +1 -1
- package/dist/codecs/jpeg.js +13 -13
- package/dist/codecs/jxl/headers.js +3 -3
- package/dist/codecs/jxl/patches-enc.js +1 -1
- package/dist/codecs/jxl.js +5 -5
- package/dist/codecs/netpbm.js +7 -7
- package/dist/codecs/png.js +15 -15
- package/dist/codecs/quantize.js +34 -8
- package/dist/codecs/tiff.js +1 -1
- package/dist/codecs/webp.js +3 -3
- package/dist/codecs/zlib.js +1 -1
- package/dist/colors.d.ts +8 -7
- package/dist/colors.js +23 -3
- package/dist/frame.d.ts +94 -100
- package/dist/frame.js +89 -65
- package/dist/gainmap.d.ts +88 -46
- package/dist/gainmap.js +75 -51
- package/dist/image-type.d.ts +6 -1
- package/dist/image-type.js +4 -1
- package/dist/image.d.ts +317 -738
- package/dist/image.js +308 -438
- package/dist/index.d.ts +19 -25
- package/dist/index.js +15 -14
- package/dist/metadata.d.ts +4 -3
- package/dist/metadata.js +9 -7
- package/dist/render/colr.js +1 -1
- package/dist/render/font.js +1 -1
- package/dist/render/layout.js +1 -1
- package/dist/render/segment.js +1 -1
- package/dist/render/shapers/hangul.js +1 -1
- package/dist/render/svg.js +68 -20
- package/dist/render/text.js +7 -9
- package/dist/text-layout.d.ts +5 -35
- package/dist/text-layout.js +11 -12
- package/dist/util/gain.js +12 -12
- package/dist/util/mem.d.ts +1 -1
- package/dist/util/optimize.js +3 -1
- package/dist/util/options.js +46 -7
- package/package.json +15 -10
- package/LICENSE.AGPL-3.0 +0 -619
- package/LICENSE.MIT +0 -19
- package/dist/gif.d.ts +0 -36
- package/dist/gif.js +0 -22
- package/dist/util/rotate.js +0 -43
- package/dist/version.js +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* @document ../guides/Migrating to 4.0.md
|
|
4
|
+
*/
|
|
1
5
|
import { BinaryData } from './util/mem.js';
|
|
2
6
|
import { Image, DecodeOptions } from './image.js';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
export { Image, ResizeModes, SVGScaleModes } from './image.js';
|
|
7
|
+
import { Animation } from './animation.js';
|
|
8
|
+
export { Image } from './image.js';
|
|
6
9
|
export { Colors } from './colors.js';
|
|
7
|
-
export type { Color, ColorFunction, ResizeMode,
|
|
8
|
-
export { Frame
|
|
9
|
-
export type {
|
|
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';
|
|
11
|
+
export { Frame } from './frame.js';
|
|
12
|
+
export type { FrameDisposalMode, FrameOptions } from './frame.js';
|
|
10
13
|
export { GainMap } from './gainmap.js';
|
|
11
|
-
export type { GainMapOptions, GainMapEncodeOptions } from './gainmap.js';
|
|
12
|
-
export { GIF } from './gif.js';
|
|
14
|
+
export type { GainMapOptions, GainMapEncodeOptions, GainMapChannelValues } from './gainmap.js';
|
|
13
15
|
export { Animation } from './animation.js';
|
|
14
|
-
export type { AnimationOptions,
|
|
16
|
+
export type { AnimationOptions, AnimationCanvasOptions, AnimationEncodeOptions, WebPAnimationEncodeOptions, GIFAnimationEncodeOptions, APNGEncodeOptions } from './animation.js';
|
|
15
17
|
export { AnimationEncoder } from './animation-encoder.js';
|
|
16
|
-
export type { AnimationEncoderOptions, WebPAnimationEncoderOptions, GIFAnimationEncoderOptions, APNGAnimationEncoderOptions } from './animation-encoder.js';
|
|
17
|
-
export { TextLayout } from './text-layout.js';
|
|
18
18
|
export type { TextLayoutOptions, WrapStyle, HorizontalAlign, VerticalAlign } from './text-layout.js';
|
|
19
19
|
export { ImageType } from './image-type.js';
|
|
20
20
|
export type { ImageTypeName } from './image-type.js';
|
|
@@ -22,18 +22,12 @@ export type { ToneMapping } from './codecs/hdr.js';
|
|
|
22
22
|
export type { BinaryData } from './util/mem.js';
|
|
23
23
|
export type { Metadata } from './metadata.js';
|
|
24
24
|
/**
|
|
25
|
-
* Decodes any supported image
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* @
|
|
29
|
-
*
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Decodes any supported image: GIFs are returned as {@link GIF} (an {@link Animation}), animated WebPs as
|
|
34
|
-
* {@link Animation}, everything else (including APNGs, as their default image) as {@link Image}.
|
|
35
|
-
* {@link Animation.decode} decodes the frames of GIFs, APNGs and WebPs
|
|
36
|
-
* @param data The image data
|
|
37
|
-
* @param onlyExtractFirstFrame Whether to end animation decoding after the first frame
|
|
25
|
+
* Decodes any supported image. A file with more than one frame or an explicit animation marker (including a one-frame
|
|
26
|
+
* APNG or animated WebP) is returned as an {@link Animation}; otherwise it is an {@link Image}. A one-frame animation
|
|
27
|
+
* keeps its timing, offsets and loop metadata. Static formats (PNG, JPEG, AVIF, ...) are Images, and AVIF image
|
|
28
|
+
* sequences decode to their first frame, as {@link Image.decode} does. The options are passed on to
|
|
29
|
+
* {@link Animation.decode} or {@link Image.decode}.
|
|
30
|
+
* @param input The image data
|
|
31
|
+
* @param options See {@link DecodeOptions}
|
|
38
32
|
*/
|
|
39
|
-
export declare function decode(
|
|
33
|
+
export declare function decode(input: BinaryData, options?: DecodeOptions): Promise<Image | Animation>;
|
package/dist/index.js
CHANGED
|
@@ -1,25 +1,26 @@
|
|
|
1
1
|
import { Image } from "./image.js";
|
|
2
|
-
import { GIF } from "./gif.js";
|
|
3
2
|
import { Animation } from "./animation.js";
|
|
4
|
-
import {
|
|
3
|
+
import { checkOptions } from "./util/options.js";
|
|
4
|
+
import * as png from "./codecs/png.js";
|
|
5
5
|
import * as webp from "./codecs/webp.js";
|
|
6
6
|
import { view } from "./util/mem.js";
|
|
7
7
|
import { ImageType } from "./image-type.js";
|
|
8
|
-
export { Image
|
|
8
|
+
export { Image } from "./image.js";
|
|
9
9
|
export { Colors } from "./colors.js";
|
|
10
|
-
export { Frame
|
|
10
|
+
export { Frame } from "./frame.js";
|
|
11
11
|
export { GainMap } from "./gainmap.js";
|
|
12
|
-
export { GIF } from "./gif.js";
|
|
13
12
|
export { Animation } from "./animation.js";
|
|
14
13
|
export { AnimationEncoder } from "./animation-encoder.js";
|
|
15
|
-
export { TextLayout } from "./text-layout.js";
|
|
16
14
|
export { ImageType } from "./image-type.js";
|
|
17
|
-
export function decode(
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (
|
|
23
|
-
return
|
|
24
|
-
|
|
15
|
+
export async function decode(input, options = {}) {
|
|
16
|
+
checkOptions(options, 'decode(data, { maxPixels: 1e8 })');
|
|
17
|
+
const bytes = view(input);
|
|
18
|
+
const type = ImageType.getType(bytes);
|
|
19
|
+
const animated = type === 'gif' || type === 'jxl' || (type === 'webp' && webp.isAnimated(bytes)) || (type === 'png' && png.isAnimated(bytes));
|
|
20
|
+
if (!animated)
|
|
21
|
+
return Image.decode(input, options);
|
|
22
|
+
const animation = await Animation.decode(input, options);
|
|
23
|
+
if (animation.length !== 1 || Animation.__is_animated__(animation))
|
|
24
|
+
return animation;
|
|
25
|
+
return animation.frames[0].image;
|
|
25
26
|
}
|
package/dist/metadata.d.ts
CHANGED
|
@@ -9,7 +9,8 @@
|
|
|
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
|
|
12
|
+
* Empty fields are left out. Without metadata, the files only say which software made them (`software`), so the same
|
|
13
|
+
* image always encodes to the same bytes
|
|
13
14
|
*/
|
|
14
15
|
export interface Metadata {
|
|
15
16
|
/** The image's title */
|
|
@@ -20,9 +21,9 @@ export interface Metadata {
|
|
|
20
21
|
description?: string;
|
|
21
22
|
/** The image's copyright info */
|
|
22
23
|
copyright?: string;
|
|
23
|
-
/** The image's creation time. Default:
|
|
24
|
+
/** The image's creation time. Default: none (left out) */
|
|
24
25
|
creationTime?: string | number | Date;
|
|
25
|
-
/** The software used to create the image. Default: `'ImageScript
|
|
26
|
+
/** The software used to create the image. Default: `'ImageScript'` (without a version), `''` leaves it out */
|
|
26
27
|
software?: string;
|
|
27
28
|
/** A disclaimer for the image */
|
|
28
29
|
disclaimer?: string;
|
package/dist/metadata.js
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
import { VERSION } from "./version.js";
|
|
2
1
|
import { encode as utf8 } from "./util/utf8.js";
|
|
3
2
|
import * as exif from "./codecs/exif.js";
|
|
4
3
|
const TEXT_FIELDS = ['title', 'author', 'description', 'copyright', 'software', 'disclaimer', 'warning', 'source', 'comment'];
|
|
5
4
|
export function resolveMetadata(metadata = {}) {
|
|
6
5
|
const { creationTime, software } = metadata || {};
|
|
7
|
-
const
|
|
8
|
-
if (
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
const resolved = {};
|
|
7
|
+
if (creationTime !== undefined) {
|
|
8
|
+
const time = new Date(creationTime);
|
|
9
|
+
if (isNaN(time.getTime()))
|
|
10
|
+
throw new RangeError('Invalid creation time');
|
|
11
|
+
resolved.creationTime = time;
|
|
12
|
+
}
|
|
11
13
|
for (const key of TEXT_FIELDS) {
|
|
12
|
-
const value = key === 'software' && software === undefined ?
|
|
14
|
+
const value = key === 'software' && software === undefined ? 'ImageScript' : metadata?.[key];
|
|
13
15
|
if (value)
|
|
14
16
|
resolved[key] = String(value);
|
|
15
17
|
}
|
|
@@ -21,7 +23,7 @@ export function pngText(m) {
|
|
|
21
23
|
Author: m.author,
|
|
22
24
|
Description: m.description,
|
|
23
25
|
Copyright: m.copyright,
|
|
24
|
-
'Creation Time': m.creationTime
|
|
26
|
+
'Creation Time': m.creationTime?.toUTCString(),
|
|
25
27
|
Software: m.software,
|
|
26
28
|
Disclaimer: m.disclaimer,
|
|
27
29
|
Warning: m.warning,
|
package/dist/render/colr.js
CHANGED
package/dist/render/font.js
CHANGED
|
@@ -281,7 +281,7 @@ export class Font {
|
|
|
281
281
|
components(offset) {
|
|
282
282
|
const components = [];
|
|
283
283
|
const f2dot14 = (o) => this.i16(o) / 16384;
|
|
284
|
-
let p = offset + 10, flags
|
|
284
|
+
let p = offset + 10, flags;
|
|
285
285
|
do {
|
|
286
286
|
flags = this.u16(p);
|
|
287
287
|
const glyph = this.u16(p + 2);
|
package/dist/render/layout.js
CHANGED
|
@@ -1096,7 +1096,7 @@ export class LayoutTable {
|
|
|
1096
1096
|
return false;
|
|
1097
1097
|
const j = it.idx;
|
|
1098
1098
|
const id1 = ligId(buffer.cur()), id2 = ligId(info[j]), comp1 = ligComp(buffer.cur()), comp2 = ligComp(info[j]);
|
|
1099
|
-
let good
|
|
1099
|
+
let good;
|
|
1100
1100
|
if (id1 === id2)
|
|
1101
1101
|
good = id1 === 0 || comp1 === comp2;
|
|
1102
1102
|
else
|
package/dist/render/segment.js
CHANGED
|
@@ -11,7 +11,7 @@ export function graphemeBoundaries(codepoints) {
|
|
|
11
11
|
incb[i] = value(conjunctBreak, codepoints[i]);
|
|
12
12
|
pictographic[i] = valueIndex(extendedPictographic, codepoints[i]) ? 1 : 0;
|
|
13
13
|
}
|
|
14
|
-
let emoji = false, emojiZWJ
|
|
14
|
+
let emoji = false, emojiZWJ, ri = 0;
|
|
15
15
|
let linker = false;
|
|
16
16
|
for (let i = 1; i < n; i++) {
|
|
17
17
|
const a = gcb[i - 1], b = gcb[i];
|
package/dist/render/svg.js
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* <style> sheets (type, class and id selectors), solid colors, linear and radial gradients, patterns, strokes
|
|
5
5
|
* (joins, caps, dashes), markers, opacity, clip paths, luminance masks, images in data URIs (rasters decoded by
|
|
6
6
|
* the caller, SVGs and SVGZs) and filters (all primitives, see filter.ts).
|
|
7
|
-
* Not supported:
|
|
7
|
+
* Text is supported with supplied or embedded fonts. Not supported: animation, scripting, foreignObject or automatic
|
|
8
|
+
* loading of external files and URLs; images can be embedded as data URIs or supplied by the caller.
|
|
8
9
|
* How patterns are rendered (a tile in device resolution, its filtering) and their reference cycles broken, and how
|
|
9
10
|
* images are fitted and sampled follows resvg/usvg, the markers (their placement and orientation,
|
|
10
11
|
* crates/usvg/src/parser/marker.rs) are ported from usvg (https://github.com/linebender/resvg; Copyright 2017 the
|
|
@@ -20,6 +21,7 @@ import { fontFace, layOutText, parseFontFamilies, parseVariationSettings, pathLe
|
|
|
20
21
|
import { rasterizeGlyph } from "./text.js";
|
|
21
22
|
import { renderPaintGlyph } from "./colr.js";
|
|
22
23
|
import { decodeSync as decodePNG } from "../codecs/png.js";
|
|
24
|
+
import { MAX_PIXELS, LimitError, checkPixels } from "../util/limits.js";
|
|
23
25
|
const UNSUPPORTED = new Set(['filter', 'pattern', 'marker', 'foreignObject', 'script', 'style', 'title', 'desc', 'metadata',
|
|
24
26
|
'defs', 'clipPath', 'mask', 'linearGradient', 'radialGradient', 'stop', 'symbol', 'font', 'font-face', 'animate', 'animateTransform', 'animateMotion', 'set']);
|
|
25
27
|
const INHERITED = new Set(['fill', 'fill-opacity', 'fill-rule', 'stroke', 'stroke-width', 'stroke-opacity', 'stroke-linecap', 'stroke-linejoin',
|
|
@@ -51,7 +53,7 @@ const DEFAULTS = {
|
|
|
51
53
|
};
|
|
52
54
|
const SAMPLES = 16;
|
|
53
55
|
const OFFSETS = Float64Array.from({ length: SAMPLES }, (_, s) => (s + 0.5) / SAMPLES);
|
|
54
|
-
export function render(source, mode, size, images = new Map(), fonts = []) {
|
|
56
|
+
export function render(source, mode, size, images = new Map(), fonts = [], maxPixels = MAX_PIXELS, maxLayerPixels = 4 * maxPixels) {
|
|
55
57
|
if (typeof source !== 'string')
|
|
56
58
|
source = decodeUTF8(source);
|
|
57
59
|
const root = parseXML(source).children.find(node => node.name === 'svg' && node.svg);
|
|
@@ -78,10 +80,21 @@ export function render(source, mode, size, images = new Map(), fonts = []) {
|
|
|
78
80
|
}
|
|
79
81
|
outWidth = Math.max(1, outWidth);
|
|
80
82
|
outHeight = Math.max(1, outHeight);
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
if (!Number.isSafeInteger(outWidth) || !Number.isSafeInteger(outHeight))
|
|
84
|
+
throw new RangeError(`SVG output dimensions are too large: ${outWidth}x${outHeight}`);
|
|
85
|
+
checkPixels(outWidth, outHeight, maxPixels);
|
|
86
|
+
const previous = layerBudget;
|
|
87
|
+
layerBudget = { left: maxLayerPixels, limit: maxLayerPixels };
|
|
88
|
+
try {
|
|
89
|
+
const canvas = new Layer(outWidth, outHeight);
|
|
90
|
+
doc.renderRoot([outWidth / width, 0, 0, outHeight / height, 0, 0], canvas, 0);
|
|
91
|
+
return { width: outWidth, height: outHeight, pixels: canvas.toRGBA() };
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
layerBudget = previous;
|
|
95
|
+
}
|
|
84
96
|
}
|
|
97
|
+
let layerBudget = null;
|
|
85
98
|
function documentSize(root) {
|
|
86
99
|
const viewBox = parseViewBox(root.attrs.viewBox);
|
|
87
100
|
const lengthOf = (value, fallback) => {
|
|
@@ -140,17 +153,20 @@ function isRaster(data) {
|
|
|
140
153
|
}
|
|
141
154
|
function nestedSvg(href) {
|
|
142
155
|
const uri = dataURI(href);
|
|
143
|
-
|
|
144
|
-
|
|
156
|
+
const shown = href.length > 100 ? `${href.slice(0, 100)}...` : href;
|
|
157
|
+
if (!uri)
|
|
158
|
+
return { error: new Error(/^\s*data:/i.test(href) ? 'SVG: an image\'s data URI is invalid' : `SVG: the image ${JSON.stringify(shown)} isn't a data URI or one of the images given to render it`) };
|
|
159
|
+
if (uri.type !== 'image/svg+xml' && (uri.type !== 'text/plain' || isRaster(uri.data)))
|
|
160
|
+
return { error: new Error(`SVG: images of type ${uri.type} aren't supported`) };
|
|
145
161
|
try {
|
|
146
162
|
const root = parseXML(svgText(uri.data)).children.find(node => node.name === 'svg' && node.svg);
|
|
147
163
|
if (!root)
|
|
148
|
-
return
|
|
164
|
+
return { error: new Error('SVG: an embedded SVG image has no <svg> element') };
|
|
149
165
|
const { width, height } = documentSize(root);
|
|
150
166
|
return width > 0 && height > 0 ? { width, height, document: new Document(root) } : null;
|
|
151
167
|
}
|
|
152
|
-
catch {
|
|
153
|
-
return
|
|
168
|
+
catch (cause) {
|
|
169
|
+
return { error: new Error('SVG: an embedded SVG image can\'t be read', { cause }) };
|
|
154
170
|
}
|
|
155
171
|
}
|
|
156
172
|
export function imageReferences(source) {
|
|
@@ -776,6 +792,7 @@ class Document {
|
|
|
776
792
|
alpha = values;
|
|
777
793
|
}
|
|
778
794
|
context.layer.composite(layer, opacity, alpha, blend);
|
|
795
|
+
layer.release();
|
|
779
796
|
}
|
|
780
797
|
clipped(ctx, x, y, width, height, draw) {
|
|
781
798
|
const layer = new Layer(ctx.layer.width, ctx.layer.height);
|
|
@@ -788,6 +805,7 @@ class Document {
|
|
|
788
805
|
for (let column = 0; column < coverage.w; column++)
|
|
789
806
|
alpha[(coverage.y0 + row) * w + coverage.x0 + column] = coverage.data[row * coverage.w + column];
|
|
790
807
|
ctx.layer.composite(layer, 1, alpha);
|
|
808
|
+
layer.release();
|
|
791
809
|
}
|
|
792
810
|
renderNestedSvg(node, ctx) {
|
|
793
811
|
const { viewport } = ctx;
|
|
@@ -846,6 +864,7 @@ class Document {
|
|
|
846
864
|
const layer = new Layer(ctx.layer.width, ctx.layer.height);
|
|
847
865
|
draw(layer);
|
|
848
866
|
ctx.layer.composite(layer, opacity, null);
|
|
867
|
+
layer.release();
|
|
849
868
|
return;
|
|
850
869
|
}
|
|
851
870
|
const computed = target.computed, computedParent = target.computedParent;
|
|
@@ -1245,8 +1264,10 @@ class Document {
|
|
|
1245
1264
|
}
|
|
1246
1265
|
renderImage(node, ctx) {
|
|
1247
1266
|
const style = ctx.style;
|
|
1267
|
+
if (style.visibility !== 'visible')
|
|
1268
|
+
return;
|
|
1248
1269
|
const image = this.imageOf(node, ctx.viewport);
|
|
1249
|
-
if (!image
|
|
1270
|
+
if (!image)
|
|
1250
1271
|
return;
|
|
1251
1272
|
const { raster, nested, intrinsic, x, y, width, height } = image;
|
|
1252
1273
|
const aspect = node.attrs.preserveAspectRatio || 'xMidYMid meet';
|
|
@@ -1267,17 +1288,24 @@ class Document {
|
|
|
1267
1288
|
else
|
|
1268
1289
|
draw(ctx.layer);
|
|
1269
1290
|
}
|
|
1270
|
-
imageOf(node, viewport) {
|
|
1291
|
+
imageOf(node, viewport, draws = true) {
|
|
1271
1292
|
const href = this.href(node)?.trim();
|
|
1272
1293
|
if (!href)
|
|
1273
1294
|
return null;
|
|
1274
|
-
const
|
|
1275
|
-
let
|
|
1276
|
-
if (!
|
|
1295
|
+
const decoded = this.images.get(href);
|
|
1296
|
+
let embedded = null;
|
|
1297
|
+
if (!decoded) {
|
|
1277
1298
|
if (!this.nested.has(href))
|
|
1278
1299
|
this.nested.set(href, nestedSvg(href));
|
|
1279
|
-
|
|
1300
|
+
embedded = this.nested.get(href);
|
|
1301
|
+
}
|
|
1302
|
+
const broken = decoded && 'error' in decoded ? decoded : embedded && 'error' in embedded ? embedded : null;
|
|
1303
|
+
if (broken) {
|
|
1304
|
+
if (draws)
|
|
1305
|
+
throw broken.error;
|
|
1306
|
+
return null;
|
|
1280
1307
|
}
|
|
1308
|
+
const raster = decoded, nested = embedded;
|
|
1281
1309
|
if (!raster && !nested)
|
|
1282
1310
|
return null;
|
|
1283
1311
|
const intrinsic = raster || nested;
|
|
@@ -1651,6 +1679,7 @@ class Document {
|
|
|
1651
1679
|
this.renderChildren(content, { ...ctx, matrix: contentMatrix, style: this.styleOf(content), layer: tile, depth: ctx.depth + 1,
|
|
1652
1680
|
viewport: viewBox ? { width: viewBox[2], height: viewBox[3] } : ctx.viewport });
|
|
1653
1681
|
this.active.delete(node);
|
|
1682
|
+
tile.release();
|
|
1654
1683
|
if (tile.x1 <= tile.x0)
|
|
1655
1684
|
return null;
|
|
1656
1685
|
const translated = !rotated && patternMatrix[0] > 0 && patternMatrix[3] > 0;
|
|
@@ -1691,20 +1720,22 @@ class Document {
|
|
|
1691
1720
|
const layer = new Layer(lw, lh);
|
|
1692
1721
|
draw(layer, shifted);
|
|
1693
1722
|
const pixmap = layer.toPixmap();
|
|
1723
|
+
layer.release();
|
|
1694
1724
|
for (const filter of filters)
|
|
1695
1725
|
applyFilter(filter, shifted, pixmap);
|
|
1696
1726
|
const result = Layer.fromPixmap(pixmap);
|
|
1697
1727
|
if (result.x1 <= result.x0)
|
|
1698
|
-
return;
|
|
1728
|
+
return result.release();
|
|
1699
1729
|
if (rest[0] === 1 && rest[1] === 0 && rest[2] === 0 && rest[3] === 1) {
|
|
1700
1730
|
target.blit(result, lx, ly);
|
|
1701
|
-
return;
|
|
1731
|
+
return result.release();
|
|
1702
1732
|
}
|
|
1703
1733
|
const place = multiply(rest, [1, 0, 0, 1, lx, ly]);
|
|
1704
1734
|
const inverse = invert(place);
|
|
1705
1735
|
const coverage = inverse && rasterize([transformPoints([0, 0, lw, 0, lw, lh, 0, lh], place)], false, target.width, target.height);
|
|
1706
1736
|
if (coverage)
|
|
1707
1737
|
target.fill(coverage, sampler(result.data, lw, lh, inverse, false, 1, 1));
|
|
1738
|
+
result.release();
|
|
1708
1739
|
}
|
|
1709
1740
|
filtersOf(node, style, viewport) {
|
|
1710
1741
|
const value = (style.filter || '').trim();
|
|
@@ -1975,7 +2006,9 @@ class Document {
|
|
|
1975
2006
|
else
|
|
1976
2007
|
this.renderImage(image, { ...ctx, style: this.computeStyle(image, this.styleOf(node)) });
|
|
1977
2008
|
this.active.delete(node);
|
|
1978
|
-
|
|
2009
|
+
const pixmap = layer.toPixmap();
|
|
2010
|
+
layer.release();
|
|
2011
|
+
return pixmap;
|
|
1979
2012
|
}
|
|
1980
2013
|
};
|
|
1981
2014
|
}
|
|
@@ -2159,7 +2192,7 @@ class Document {
|
|
|
2159
2192
|
groupBoundingBox(node, viewport, matrix = [1, 0, 0, 1, 0, 0]) {
|
|
2160
2193
|
let box = null;
|
|
2161
2194
|
const visit = (n, matrix) => {
|
|
2162
|
-
const image = n.name === 'image' ? this.imageOf(n, viewport) : null;
|
|
2195
|
+
const image = n.name === 'image' ? this.imageOf(n, viewport, false) : null;
|
|
2163
2196
|
const path = image ? [['M', image.x, image.y], ['L', image.x + image.width, image.y], ['L', image.x + image.width, image.y + image.height],
|
|
2164
2197
|
['L', image.x, image.y + image.height], ['Z']] : n.name === 'text' ? this.textBox(n, viewport) : shapePath(n, viewport, 16);
|
|
2165
2198
|
if (path) {
|
|
@@ -2217,6 +2250,7 @@ class Document {
|
|
|
2217
2250
|
const regionCoverage = rasterize([transformPoints([rx, ry, rx + rw, ry, rx + rw, ry + rh, rx, ry + rh], matrix)], false, width, height);
|
|
2218
2251
|
if (!regionCoverage) {
|
|
2219
2252
|
this.active.delete(mask);
|
|
2253
|
+
layer.release();
|
|
2220
2254
|
return values;
|
|
2221
2255
|
}
|
|
2222
2256
|
const { x0, y0, w, h, data } = regionCoverage;
|
|
@@ -2226,6 +2260,7 @@ class Document {
|
|
|
2226
2260
|
const i = (y0 + y) * width + x0 + x;
|
|
2227
2261
|
values[i] = data[y * w + x] * (alphaMask ? d[i * 4 + 3] : 0.2125 * d[i * 4] + 0.7154 * d[i * 4 + 1] + 0.0721 * d[i * 4 + 2]);
|
|
2228
2262
|
}
|
|
2263
|
+
layer.release();
|
|
2229
2264
|
const nested = this.reference(maskStyle.mask);
|
|
2230
2265
|
if (nested && nested.name === 'mask' && !this.active.has(nested)) {
|
|
2231
2266
|
const outer = this.maskValues(nested, node, matrix, context);
|
|
@@ -2531,11 +2566,24 @@ class Layer {
|
|
|
2531
2566
|
y0 = Infinity;
|
|
2532
2567
|
x1 = -Infinity;
|
|
2533
2568
|
y1 = -Infinity;
|
|
2569
|
+
budgeted = 0;
|
|
2534
2570
|
constructor(width, height) {
|
|
2535
2571
|
this.width = width;
|
|
2536
2572
|
this.height = height;
|
|
2573
|
+
const budget = layerBudget;
|
|
2574
|
+
if (budget) {
|
|
2575
|
+
if (!(width * height <= budget.left))
|
|
2576
|
+
throw new LimitError(`SVG too complex: its layers (for opacity, masks, filters, ...) exceed ${budget.limit} pixels at once (the render options' maxLayerPixels)`);
|
|
2577
|
+
budget.left -= width * height;
|
|
2578
|
+
this.budgeted = width * height;
|
|
2579
|
+
}
|
|
2537
2580
|
this.data = new Float32Array(width * height * 4);
|
|
2538
2581
|
}
|
|
2582
|
+
release() {
|
|
2583
|
+
if (layerBudget)
|
|
2584
|
+
layerBudget.left += this.budgeted;
|
|
2585
|
+
this.budgeted = 0;
|
|
2586
|
+
}
|
|
2539
2587
|
extend(x0, y0, x1, y1) {
|
|
2540
2588
|
this.x0 = Math.min(this.x0, x0);
|
|
2541
2589
|
this.y0 = Math.min(this.y0, y0);
|
package/dist/render/text.js
CHANGED
|
@@ -13,6 +13,7 @@ import { graphemeBoundaries, scripts } from "./segment.js";
|
|
|
13
13
|
import { parsePaintGlyphs, paintExtent, renderPaintGlyph } from "./colr.js";
|
|
14
14
|
import { decode as decodePNG } from "../codecs/png.js";
|
|
15
15
|
import { resample, TRIANGLE } from "../util/resample.js";
|
|
16
|
+
import { MAX_PIXELS, LimitError } from "../util/limits.js";
|
|
16
17
|
const SUBPIXELS = 4;
|
|
17
18
|
const MAX_GLYPH_PIXELS = 1 << 26;
|
|
18
19
|
const fonts = new WeakMap();
|
|
@@ -104,20 +105,17 @@ function glyphOf(f, index, size) {
|
|
|
104
105
|
f.glyphs.set(index, g);
|
|
105
106
|
return g;
|
|
106
107
|
}
|
|
107
|
-
export async function render(fonts, size, text, layout, hinting = false, foreground = 0xffffffff, features = [], variations = {}) {
|
|
108
|
+
export async function render(fonts, size, text, layout, hinting = false, foreground = 0xffffffff, features = [], variations = {}, maxPixels = MAX_PIXELS, pad = 0) {
|
|
108
109
|
try {
|
|
109
|
-
return await layOut(fonts, size, text, layout, hinting, foreground, features, variations);
|
|
110
|
+
return await layOut(fonts, size, text, layout, hinting, foreground, features, variations, maxPixels, pad);
|
|
110
111
|
}
|
|
111
112
|
catch (err) {
|
|
112
|
-
if (err instanceof RangeError && !(err instanceof
|
|
113
|
+
if (err instanceof RangeError && !(err instanceof LimitError))
|
|
113
114
|
throw new Error('Font: damaged or truncated font data', { cause: err });
|
|
114
115
|
throw err;
|
|
115
116
|
}
|
|
116
117
|
}
|
|
117
|
-
|
|
118
|
-
class TooLarge extends RangeError {
|
|
119
|
-
}
|
|
120
|
-
async function layOut(fonts, size, text, layout, hinting, foreground, features, variations) {
|
|
118
|
+
async function layOut(fonts, size, text, layout, hinting, foreground, features, variations, maxPixels, pad) {
|
|
121
119
|
const sized = [];
|
|
122
120
|
const font = (i) => sized[i] ??= sizedFont(fonts[i], size, hinting, variations);
|
|
123
121
|
const primary = font(0);
|
|
@@ -138,8 +136,8 @@ async function layOut(fonts, size, text, layout, hinting, foreground, features,
|
|
|
138
136
|
}
|
|
139
137
|
const width = Math.max(1, Math.ceil(Math.max(0, ...visible.map(line => line.extent))));
|
|
140
138
|
const height = visible.length * lineHeight;
|
|
141
|
-
if (!(width * height <=
|
|
142
|
-
throw new
|
|
139
|
+
if (!((width + 2 * pad) * (height + 2 * pad) <= maxPixels))
|
|
140
|
+
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)`);
|
|
143
141
|
const coverage = new Uint8Array(width * height);
|
|
144
142
|
let color = null;
|
|
145
143
|
for (const [n, line] of visible.entries()) {
|
package/dist/text-layout.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export type WrapStyle = 'word' | 'char';
|
|
|
4
4
|
export type HorizontalAlign = 'left' | 'center' | 'right';
|
|
5
5
|
/** Which lines to keep when the text is cut off at `maxHeight` */
|
|
6
6
|
export type VerticalAlign = 'top' | 'middle' | 'bottom';
|
|
7
|
-
/**
|
|
7
|
+
/** The layout options of {@link Image.renderText} (part of {@link RenderTextOptions}): wrapping, cutting off and alignment */
|
|
8
8
|
export interface TextLayoutOptions {
|
|
9
9
|
/** Longer lines are wrapped. Default: `Infinity` */
|
|
10
10
|
maxWidth?: number;
|
|
@@ -12,40 +12,10 @@ export interface TextLayoutOptions {
|
|
|
12
12
|
maxHeight?: number;
|
|
13
13
|
/** Whether to wrap between words or at any character. Default: `'word'` */
|
|
14
14
|
wrapStyle?: WrapStyle;
|
|
15
|
-
/**
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
*/
|
|
20
|
-
horizontalAlign?: HorizontalAlign | VerticalAlign;
|
|
21
|
-
/**
|
|
22
|
-
* Which lines to keep when cutting off at `maxHeight`. Default: `'top'`
|
|
23
|
-
* (before 3.0 this was called horizontalAlign, the values of the other axis (`'left'`, `'center'`, `'right'`) are
|
|
24
|
-
* accepted for compatibility, which will be removed in 4.0)
|
|
25
|
-
*/
|
|
26
|
-
verticalAlign?: VerticalAlign | HorizontalAlign;
|
|
15
|
+
/** Alignment of the lines relative to the widest line: `'left'`, `'center'` or `'right'`. Default: `'left'` */
|
|
16
|
+
horizontalAlign?: HorizontalAlign;
|
|
17
|
+
/** Which lines to keep when cutting off at `maxHeight`: `'top'`, `'middle'` or `'bottom'`. Default: `'top'` */
|
|
18
|
+
verticalAlign?: VerticalAlign;
|
|
27
19
|
/** Whether to break lines at newline characters. Default: `true` */
|
|
28
20
|
wrapHardBreaks?: boolean;
|
|
29
21
|
}
|
|
30
|
-
/**
|
|
31
|
-
* Layout options for {@link Image.renderText}
|
|
32
|
-
*/
|
|
33
|
-
export declare class TextLayout {
|
|
34
|
-
/** Longer lines are wrapped */
|
|
35
|
-
maxWidth: number;
|
|
36
|
-
/** Lines that don't fit are cut off */
|
|
37
|
-
maxHeight: number;
|
|
38
|
-
/** Whether to wrap between words or at any character */
|
|
39
|
-
wrapStyle: WrapStyle;
|
|
40
|
-
/** Alignment of the lines relative to the widest line */
|
|
41
|
-
horizontalAlign: HorizontalAlign;
|
|
42
|
-
/** Which lines to keep when cutting off at `maxHeight` */
|
|
43
|
-
verticalAlign: VerticalAlign;
|
|
44
|
-
/** Whether to break lines at newline characters */
|
|
45
|
-
wrapHardBreaks: boolean;
|
|
46
|
-
/**
|
|
47
|
-
* Creates a layout, missing options take their defaults
|
|
48
|
-
* @param options See {@link TextLayoutOptions}
|
|
49
|
-
*/
|
|
50
|
-
constructor(options?: TextLayoutOptions);
|
|
51
|
-
}
|
package/dist/text-layout.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { oneOf } from "./util/options.js";
|
|
1
2
|
const HORIZONTAL = ['left', 'center', 'right'];
|
|
2
3
|
const VERTICAL = ['top', 'middle', 'bottom'];
|
|
3
4
|
export class TextLayout {
|
|
@@ -15,18 +16,16 @@ export class TextLayout {
|
|
|
15
16
|
this.maxHeight = maxHeight === undefined || maxHeight === 0 ? Infinity : Number(maxHeight);
|
|
16
17
|
if (isNaN(this.maxHeight) || this.maxHeight < 1)
|
|
17
18
|
throw new RangeError('Invalid maxHeight');
|
|
18
|
-
this.wrapStyle = wrapStyle
|
|
19
|
-
if (
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
else
|
|
29
|
-
throw new RangeError(`Invalid alignment: ${value}`);
|
|
19
|
+
this.wrapStyle = wrapStyle === undefined ? 'word' : oneOf(wrapStyle, ['word', 'char'], 'wrapStyle');
|
|
20
|
+
if (horizontalAlign !== undefined) {
|
|
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;
|
|
30
29
|
}
|
|
31
30
|
this.wrapHardBreaks = typeof wrapHardBreaks === 'undefined' ? true : wrapHardBreaks;
|
|
32
31
|
if (typeof this.wrapHardBreaks !== 'boolean')
|