imagescript 4.0.0-rc.1 → 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/LICENSE +16 -12
- package/README.md +2 -2
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/dist/animation-encoder.d.ts +1 -0
- package/dist/animation-encoder.js +27 -11
- package/dist/animation.d.ts +22 -22
- package/dist/animation.js +50 -27
- package/dist/codecs/av1/decode.js +1 -1
- package/dist/codecs/av1/encode.js +1 -1
- package/dist/codecs/jxl/frame-header.js +6 -4
- package/dist/codecs/jxl/headers.js +1 -1
- package/dist/codecs/jxl/patches-enc.js +1 -1
- package/dist/codecs/quantize.js +23 -9
- package/dist/codecs/zlib.js +1 -1
- package/dist/colors.js +10 -0
- package/dist/frame.d.ts +5 -2
- package/dist/frame.js +4 -7
- package/dist/gainmap.d.ts +6 -3
- package/dist/gainmap.js +23 -17
- package/dist/image.d.ts +109 -72
- package/dist/image.js +120 -100
- package/dist/index.d.ts +6 -1
- package/dist/index.js +1 -0
- package/dist/metadata.d.ts +6 -5
- 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 +102 -35
- package/dist/render/text.js +17 -17
- package/dist/text-layout.d.ts +21 -11
- package/dist/text-layout.js +18 -32
- package/dist/util/limits.d.ts +35 -0
- package/dist/util/limits.js +1 -0
- package/dist/util/optimize.js +3 -1
- package/dist/util/options.js +42 -2
- package/package.json +15 -10
- package/LICENSE.AGPL-3.0 +0 -619
- package/LICENSE.MIT +0 -19
- package/dist/version.js +0 -1
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
|
@@ -21,7 +21,7 @@ import { fontFace, layOutText, parseFontFamilies, parseVariationSettings, pathLe
|
|
|
21
21
|
import { rasterizeGlyph } from "./text.js";
|
|
22
22
|
import { renderPaintGlyph } from "./colr.js";
|
|
23
23
|
import { decodeSync as decodePNG } from "../codecs/png.js";
|
|
24
|
-
import { MAX_PIXELS, checkPixels } from "../util/limits.js";
|
|
24
|
+
import { MAX_PIXELS, LimitError, checkPixels } from "../util/limits.js";
|
|
25
25
|
const UNSUPPORTED = new Set(['filter', 'pattern', 'marker', 'foreignObject', 'script', 'style', 'title', 'desc', 'metadata',
|
|
26
26
|
'defs', 'clipPath', 'mask', 'linearGradient', 'radialGradient', 'stop', 'symbol', 'font', 'font-face', 'animate', 'animateTransform', 'animateMotion', 'set']);
|
|
27
27
|
const INHERITED = new Set(['fill', 'fill-opacity', 'fill-rule', 'stroke', 'stroke-width', 'stroke-opacity', 'stroke-linecap', 'stroke-linejoin',
|
|
@@ -53,7 +53,7 @@ const DEFAULTS = {
|
|
|
53
53
|
};
|
|
54
54
|
const SAMPLES = 16;
|
|
55
55
|
const OFFSETS = Float64Array.from({ length: SAMPLES }, (_, s) => (s + 0.5) / SAMPLES);
|
|
56
|
-
export function render(source, mode, size, images = new Map(), fonts = [], maxPixels = MAX_PIXELS) {
|
|
56
|
+
export function render(source, mode, size, images = new Map(), fonts = [], maxPixels = MAX_PIXELS, maxLayerPixels = 4 * maxPixels) {
|
|
57
57
|
if (typeof source !== 'string')
|
|
58
58
|
source = decodeUTF8(source);
|
|
59
59
|
const root = parseXML(source).children.find(node => node.name === 'svg' && node.svg);
|
|
@@ -83,10 +83,18 @@ export function render(source, mode, size, images = new Map(), fonts = [], maxPi
|
|
|
83
83
|
if (!Number.isSafeInteger(outWidth) || !Number.isSafeInteger(outHeight))
|
|
84
84
|
throw new RangeError(`SVG output dimensions are too large: ${outWidth}x${outHeight}`);
|
|
85
85
|
checkPixels(outWidth, outHeight, maxPixels);
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
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
|
+
}
|
|
89
96
|
}
|
|
97
|
+
let layerBudget = null;
|
|
90
98
|
function documentSize(root) {
|
|
91
99
|
const viewBox = parseViewBox(root.attrs.viewBox);
|
|
92
100
|
const lengthOf = (value, fallback) => {
|
|
@@ -145,17 +153,20 @@ function isRaster(data) {
|
|
|
145
153
|
}
|
|
146
154
|
function nestedSvg(href) {
|
|
147
155
|
const uri = dataURI(href);
|
|
148
|
-
|
|
149
|
-
|
|
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`) };
|
|
150
161
|
try {
|
|
151
162
|
const root = parseXML(svgText(uri.data)).children.find(node => node.name === 'svg' && node.svg);
|
|
152
163
|
if (!root)
|
|
153
|
-
return
|
|
164
|
+
return { error: new Error('SVG: an embedded SVG image has no <svg> element') };
|
|
154
165
|
const { width, height } = documentSize(root);
|
|
155
166
|
return width > 0 && height > 0 ? { width, height, document: new Document(root) } : null;
|
|
156
167
|
}
|
|
157
|
-
catch {
|
|
158
|
-
return
|
|
168
|
+
catch (cause) {
|
|
169
|
+
return { error: new Error('SVG: an embedded SVG image can\'t be read', { cause }) };
|
|
159
170
|
}
|
|
160
171
|
}
|
|
161
172
|
export function imageReferences(source) {
|
|
@@ -545,10 +556,12 @@ function validProperty(name, value) {
|
|
|
545
556
|
}
|
|
546
557
|
}
|
|
547
558
|
const MAX_INSTANCES = 100000;
|
|
559
|
+
const MAX_OCTAVES = 24;
|
|
560
|
+
const MAX_NESTED = 2 ** 26, MIN_NESTED = 2 ** 16;
|
|
548
561
|
const MAX_DASHES = 1000000;
|
|
549
562
|
const MAX_TILE = 4096 * 1024;
|
|
550
563
|
class Document {
|
|
551
|
-
budget = { instances: 0 };
|
|
564
|
+
budget = { instances: 0, nested: 0, nesting: 0 };
|
|
552
565
|
ids;
|
|
553
566
|
active;
|
|
554
567
|
root;
|
|
@@ -781,6 +794,7 @@ class Document {
|
|
|
781
794
|
alpha = values;
|
|
782
795
|
}
|
|
783
796
|
context.layer.composite(layer, opacity, alpha, blend);
|
|
797
|
+
layer.release();
|
|
784
798
|
}
|
|
785
799
|
clipped(ctx, x, y, width, height, draw) {
|
|
786
800
|
const layer = new Layer(ctx.layer.width, ctx.layer.height);
|
|
@@ -793,6 +807,7 @@ class Document {
|
|
|
793
807
|
for (let column = 0; column < coverage.w; column++)
|
|
794
808
|
alpha[(coverage.y0 + row) * w + coverage.x0 + column] = coverage.data[row * coverage.w + column];
|
|
795
809
|
ctx.layer.composite(layer, 1, alpha);
|
|
810
|
+
layer.release();
|
|
796
811
|
}
|
|
797
812
|
renderNestedSvg(node, ctx) {
|
|
798
813
|
const { viewport } = ctx;
|
|
@@ -851,6 +866,7 @@ class Document {
|
|
|
851
866
|
const layer = new Layer(ctx.layer.width, ctx.layer.height);
|
|
852
867
|
draw(layer);
|
|
853
868
|
ctx.layer.composite(layer, opacity, null);
|
|
869
|
+
layer.release();
|
|
854
870
|
return;
|
|
855
871
|
}
|
|
856
872
|
const computed = target.computed, computedParent = target.computedParent;
|
|
@@ -1250,8 +1266,10 @@ class Document {
|
|
|
1250
1266
|
}
|
|
1251
1267
|
renderImage(node, ctx) {
|
|
1252
1268
|
const style = ctx.style;
|
|
1269
|
+
if (style.visibility !== 'visible')
|
|
1270
|
+
return;
|
|
1253
1271
|
const image = this.imageOf(node, ctx.viewport);
|
|
1254
|
-
if (!image
|
|
1272
|
+
if (!image)
|
|
1255
1273
|
return;
|
|
1256
1274
|
const { raster, nested, intrinsic, x, y, width, height } = image;
|
|
1257
1275
|
const aspect = node.attrs.preserveAspectRatio || 'xMidYMid meet';
|
|
@@ -1272,17 +1290,24 @@ class Document {
|
|
|
1272
1290
|
else
|
|
1273
1291
|
draw(ctx.layer);
|
|
1274
1292
|
}
|
|
1275
|
-
imageOf(node, viewport) {
|
|
1293
|
+
imageOf(node, viewport, draws = true) {
|
|
1276
1294
|
const href = this.href(node)?.trim();
|
|
1277
1295
|
if (!href)
|
|
1278
1296
|
return null;
|
|
1279
|
-
const
|
|
1280
|
-
let
|
|
1281
|
-
if (!
|
|
1297
|
+
const decoded = this.images.get(href);
|
|
1298
|
+
let embedded = null;
|
|
1299
|
+
if (!decoded) {
|
|
1282
1300
|
if (!this.nested.has(href))
|
|
1283
1301
|
this.nested.set(href, nestedSvg(href));
|
|
1284
|
-
|
|
1302
|
+
embedded = this.nested.get(href);
|
|
1303
|
+
}
|
|
1304
|
+
const broken = decoded && 'error' in decoded ? decoded : embedded && 'error' in embedded ? embedded : null;
|
|
1305
|
+
if (broken) {
|
|
1306
|
+
if (draws)
|
|
1307
|
+
throw broken.error;
|
|
1308
|
+
return null;
|
|
1285
1309
|
}
|
|
1310
|
+
const raster = decoded, nested = embedded;
|
|
1286
1311
|
if (!raster && !nested)
|
|
1287
1312
|
return null;
|
|
1288
1313
|
const intrinsic = raster || nested;
|
|
@@ -1412,10 +1437,12 @@ class Document {
|
|
|
1412
1437
|
matrix = multiply(matrix, [sx * units, 0, 0, sy * units, -refX * sx * units, -refY * sy * units]);
|
|
1413
1438
|
const inner = { ...ctx, matrix, style: markerStyle, depth: ctx.depth + 1, viewport: viewBox ? { width: viewBox[2], height: viewBox[3] } : ctx.viewport, contextElement };
|
|
1414
1439
|
this.active.add(marker);
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
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
|
+
});
|
|
1419
1446
|
this.active.delete(marker);
|
|
1420
1447
|
};
|
|
1421
1448
|
if (k === 0) {
|
|
@@ -1653,9 +1680,10 @@ class Document {
|
|
|
1653
1680
|
contentMatrix = multiply(contentMatrix, [box.width, 0, 0, box.height, 0, 0]);
|
|
1654
1681
|
const tile = new Layer(width, height);
|
|
1655
1682
|
this.active.add(node);
|
|
1656
|
-
this.renderChildren(content, { ...ctx, matrix: contentMatrix, style: this.styleOf(content), layer: tile, depth: ctx.depth + 1,
|
|
1657
|
-
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 }));
|
|
1658
1685
|
this.active.delete(node);
|
|
1686
|
+
tile.release();
|
|
1659
1687
|
if (tile.x1 <= tile.x0)
|
|
1660
1688
|
return null;
|
|
1661
1689
|
const translated = !rotated && patternMatrix[0] > 0 && patternMatrix[3] > 0;
|
|
@@ -1696,20 +1724,22 @@ class Document {
|
|
|
1696
1724
|
const layer = new Layer(lw, lh);
|
|
1697
1725
|
draw(layer, shifted);
|
|
1698
1726
|
const pixmap = layer.toPixmap();
|
|
1727
|
+
layer.release();
|
|
1699
1728
|
for (const filter of filters)
|
|
1700
1729
|
applyFilter(filter, shifted, pixmap);
|
|
1701
1730
|
const result = Layer.fromPixmap(pixmap);
|
|
1702
1731
|
if (result.x1 <= result.x0)
|
|
1703
|
-
return;
|
|
1732
|
+
return result.release();
|
|
1704
1733
|
if (rest[0] === 1 && rest[1] === 0 && rest[2] === 0 && rest[3] === 1) {
|
|
1705
1734
|
target.blit(result, lx, ly);
|
|
1706
|
-
return;
|
|
1735
|
+
return result.release();
|
|
1707
1736
|
}
|
|
1708
1737
|
const place = multiply(rest, [1, 0, 0, 1, lx, ly]);
|
|
1709
1738
|
const inverse = invert(place);
|
|
1710
1739
|
const coverage = inverse && rasterize([transformPoints([0, 0, lw, 0, lw, lh, 0, lh], place)], false, target.width, target.height);
|
|
1711
1740
|
if (coverage)
|
|
1712
1741
|
target.fill(coverage, sampler(result.data, lw, lh, inverse, false, 1, 1));
|
|
1742
|
+
result.release();
|
|
1713
1743
|
}
|
|
1714
1744
|
filtersOf(node, style, viewport) {
|
|
1715
1745
|
const value = (style.filter || '').trim();
|
|
@@ -1913,7 +1943,7 @@ class Document {
|
|
|
1913
1943
|
let [fx, fy] = list && list.length === 2 ? list : list && list.length === 1 ? [list[0], list[0]] : [0, 0];
|
|
1914
1944
|
if (!(fx >= 0 && fy >= 0))
|
|
1915
1945
|
fx = fy = 0;
|
|
1916
|
-
const octaves = Math.max(0, number('numOctaves', 1));
|
|
1946
|
+
const octaves = Math.min(MAX_OCTAVES, Math.max(0, number('numOctaves', 1)));
|
|
1917
1947
|
return { type: 'turbulence', fx, fy, octaves: Math.round(octaves), seed: Math.trunc(number('seed', 0)), stitch: node.attrs.stitchTiles?.trim() === 'stitch',
|
|
1918
1948
|
fractal: node.attrs.type?.trim() === 'fractalNoise' };
|
|
1919
1949
|
}
|
|
@@ -1975,12 +2005,16 @@ class Document {
|
|
|
1975
2005
|
this.active.add(node);
|
|
1976
2006
|
const parentStyle = (target || node).parent && (target || node).parent.name !== '#root' ? this.styleOf((target || node).parent) : null;
|
|
1977
2007
|
const ctx = { matrix, style: parentStyle, viewport, layer, depth: 16 };
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
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
|
+
});
|
|
1982
2014
|
this.active.delete(node);
|
|
1983
|
-
|
|
2015
|
+
const pixmap = layer.toPixmap();
|
|
2016
|
+
layer.release();
|
|
2017
|
+
return pixmap;
|
|
1984
2018
|
}
|
|
1985
2019
|
};
|
|
1986
2020
|
}
|
|
@@ -2089,7 +2123,22 @@ class Document {
|
|
|
2089
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 };
|
|
2090
2124
|
return { rect, primitives: [{ rect, linear: false, result: 'result', kind }] };
|
|
2091
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
|
+
}
|
|
2092
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) {
|
|
2093
2142
|
const { width, height } = context.layer;
|
|
2094
2143
|
const alpha = new Float32Array(width * height);
|
|
2095
2144
|
const style = this.styleOf(clip);
|
|
@@ -2164,7 +2213,7 @@ class Document {
|
|
|
2164
2213
|
groupBoundingBox(node, viewport, matrix = [1, 0, 0, 1, 0, 0]) {
|
|
2165
2214
|
let box = null;
|
|
2166
2215
|
const visit = (n, matrix) => {
|
|
2167
|
-
const image = n.name === 'image' ? this.imageOf(n, viewport) : null;
|
|
2216
|
+
const image = n.name === 'image' ? this.imageOf(n, viewport, false) : null;
|
|
2168
2217
|
const path = image ? [['M', image.x, image.y], ['L', image.x + image.width, image.y], ['L', image.x + image.width, image.y + image.height],
|
|
2169
2218
|
['L', image.x, image.y + image.height], ['Z']] : n.name === 'text' ? this.textBox(n, viewport) : shapePath(n, viewport, 16);
|
|
2170
2219
|
if (path) {
|
|
@@ -2181,6 +2230,9 @@ class Document {
|
|
|
2181
2230
|
return box;
|
|
2182
2231
|
}
|
|
2183
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) {
|
|
2184
2236
|
const { width, height } = context.layer;
|
|
2185
2237
|
const values = new Float32Array(width * height);
|
|
2186
2238
|
const path = shapePath(node, context.viewport, 16);
|
|
@@ -2222,6 +2274,7 @@ class Document {
|
|
|
2222
2274
|
const regionCoverage = rasterize([transformPoints([rx, ry, rx + rw, ry, rx + rw, ry + rh, rx, ry + rh], matrix)], false, width, height);
|
|
2223
2275
|
if (!regionCoverage) {
|
|
2224
2276
|
this.active.delete(mask);
|
|
2277
|
+
layer.release();
|
|
2225
2278
|
return values;
|
|
2226
2279
|
}
|
|
2227
2280
|
const { x0, y0, w, h, data } = regionCoverage;
|
|
@@ -2231,6 +2284,7 @@ class Document {
|
|
|
2231
2284
|
const i = (y0 + y) * width + x0 + x;
|
|
2232
2285
|
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]);
|
|
2233
2286
|
}
|
|
2287
|
+
layer.release();
|
|
2234
2288
|
const nested = this.reference(maskStyle.mask);
|
|
2235
2289
|
if (nested && nested.name === 'mask' && !this.active.has(nested)) {
|
|
2236
2290
|
const outer = this.maskValues(nested, node, matrix, context);
|
|
@@ -2536,11 +2590,24 @@ class Layer {
|
|
|
2536
2590
|
y0 = Infinity;
|
|
2537
2591
|
x1 = -Infinity;
|
|
2538
2592
|
y1 = -Infinity;
|
|
2593
|
+
budgeted = 0;
|
|
2539
2594
|
constructor(width, height) {
|
|
2540
2595
|
this.width = width;
|
|
2541
2596
|
this.height = height;
|
|
2597
|
+
const budget = layerBudget;
|
|
2598
|
+
if (budget) {
|
|
2599
|
+
if (!(width * height <= budget.left))
|
|
2600
|
+
throw new LimitError(`SVG too complex: its layers (for opacity, masks, filters, ...) exceed ${budget.limit} pixels at once (the render options' maxLayerPixels)`);
|
|
2601
|
+
budget.left -= width * height;
|
|
2602
|
+
this.budgeted = width * height;
|
|
2603
|
+
}
|
|
2542
2604
|
this.data = new Float32Array(width * height * 4);
|
|
2543
2605
|
}
|
|
2606
|
+
release() {
|
|
2607
|
+
if (layerBudget)
|
|
2608
|
+
layerBudget.left += this.budgeted;
|
|
2609
|
+
this.budgeted = 0;
|
|
2610
|
+
}
|
|
2544
2611
|
extend(x0, y0, x1, y1) {
|
|
2545
2612
|
this.x0 = Math.min(this.x0, x0);
|
|
2546
2613
|
this.y0 = Math.min(this.y0, y0);
|
|
@@ -2736,7 +2803,7 @@ function rasterize(polygons, evenOdd, width, height) {
|
|
|
2736
2803
|
const steps = scratch.f32(1, w + 2);
|
|
2737
2804
|
const weight = 1 / SAMPLES;
|
|
2738
2805
|
const rowStarts = scratch.i32(1, h + 1).fill(0, 0, h + 1), order = scratch.i32(2, numEdges);
|
|
2739
|
-
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);
|
|
2740
2807
|
for (let e = 0; e < numEdges; e++)
|
|
2741
2808
|
if (startRow(e) < h)
|
|
2742
2809
|
rowStarts[startRow(e) + 1]++;
|
|
@@ -2800,7 +2867,7 @@ function rasterize(polygons, evenOdd, width, height) {
|
|
|
2800
2867
|
while (s1 >= 0 && !(py + OFFSETS[s1] < bottom))
|
|
2801
2868
|
s1--;
|
|
2802
2869
|
firsts[k] = s0;
|
|
2803
|
-
lasts[k] = s1;
|
|
2870
|
+
lasts[k] = Math.max(-1, s1);
|
|
2804
2871
|
for (let s = s0; s <= s1; s++)
|
|
2805
2872
|
counts[s]++;
|
|
2806
2873
|
if (s1 >= s0)
|
|
@@ -3347,7 +3414,7 @@ function strokePolygons(subpaths, halfWidth, cap, join, miterLimit, tolerance) {
|
|
|
3347
3414
|
}
|
|
3348
3415
|
function parseTransform(value) {
|
|
3349
3416
|
let matrix = [1, 0, 0, 1, 0, 0];
|
|
3350
|
-
const functions = /(matrix|translate|scale|rotate|skewX|skewY)\s*\(([^)]*)\)/g;
|
|
3417
|
+
const functions = /(matrix|translate|scale|rotate|skewX|skewY)\s*\(([^()]*)\)/g;
|
|
3351
3418
|
for (const [, name, args] of value.matchAll(functions)) {
|
|
3352
3419
|
const a = parseNumberList(args);
|
|
3353
3420
|
let m = [1, 0, 0, 1, 0, 0];
|
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,42 +105,41 @@ 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);
|
|
124
122
|
const ascent = Math.ceil(primary.font.ascender * primary.scale);
|
|
125
123
|
const lineHeight = Math.max(1, Math.ceil((primary.font.ascender - primary.font.descender + primary.font.lineGap) * primary.scale));
|
|
126
|
-
const paragraphs =
|
|
124
|
+
const paragraphs = text.split(/\r\n|[\r\n\u0085\u2028\u2029]/);
|
|
127
125
|
const lines = [];
|
|
128
126
|
for (const paragraph of paragraphs)
|
|
129
127
|
lines.push(...layOutParagraph(analyze(paragraph, fonts.length, font), layout, font, size, features));
|
|
130
128
|
let visible = lines;
|
|
131
|
-
if (
|
|
132
|
-
const count = Math.max(1, Math.floor(layout.
|
|
129
|
+
if (layout.height !== undefined) {
|
|
130
|
+
const count = Math.max(1, Math.floor(layout.height / lineHeight));
|
|
133
131
|
if (count < lines.length) {
|
|
134
132
|
const start = layout.verticalAlign === 'bottom' ? lines.length - count
|
|
135
133
|
: layout.verticalAlign === 'middle' ? lines.length - count >> 1 : 0;
|
|
136
134
|
visible = lines.slice(start, start + count);
|
|
137
135
|
}
|
|
138
136
|
}
|
|
139
|
-
const width = Math.max(1, Math.ceil(Math.max(0, ...visible.map(line => line.extent))));
|
|
140
|
-
const height = visible.length * lineHeight;
|
|
141
|
-
|
|
142
|
-
|
|
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;
|
|
141
|
+
if (!((width + 2 * pad) * (height + 2 * pad) <= maxPixels))
|
|
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)`);
|
|
143
143
|
const coverage = new Uint8Array(width * height);
|
|
144
144
|
let color = null;
|
|
145
145
|
for (const [n, line] of visible.entries()) {
|
|
@@ -147,7 +147,7 @@ async function layOut(fonts, size, text, layout, hinting, foreground, features,
|
|
|
147
147
|
: layout.horizontalAlign === 'center' ? (width - line.extent) / 2 : 0;
|
|
148
148
|
if (primary.hinter)
|
|
149
149
|
offset = Math.round(offset);
|
|
150
|
-
const baseline = ascent + n * lineHeight;
|
|
150
|
+
const baseline = top + ascent + n * lineHeight;
|
|
151
151
|
for (const { x, dx, dy, glyph: g } of line.glyphs) {
|
|
152
152
|
if (!g.ink)
|
|
153
153
|
continue;
|
|
@@ -308,11 +308,11 @@ function layOutParagraph(p, layout, font, size, features) {
|
|
|
308
308
|
const end = i + 1;
|
|
309
309
|
if (!starts[end])
|
|
310
310
|
continue;
|
|
311
|
-
if (p.breaks[end] === 2 && end < n
|
|
311
|
+
if (p.breaks[end] === 2 && end < n) {
|
|
312
312
|
fit = end;
|
|
313
313
|
break;
|
|
314
314
|
}
|
|
315
|
-
const fits = used - trailing <= layout.
|
|
315
|
+
const fits = used - trailing <= (layout.width ?? Infinity);
|
|
316
316
|
if (!fits)
|
|
317
317
|
break;
|
|
318
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,35 +1,21 @@
|
|
|
1
|
+
import { oneOf } from "./util/options.js";
|
|
1
2
|
const HORIZONTAL = ['left', 'center', 'right'];
|
|
2
3
|
const VERTICAL = ['top', 'middle', 'bottom'];
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
throw new RangeError('Invalid wrapStyle');
|
|
21
|
-
if (horizontalAlign !== undefined) {
|
|
22
|
-
if (!HORIZONTAL.includes(horizontalAlign))
|
|
23
|
-
throw new TypeError(`horizontalAlign must be 'left', 'center' or 'right', not '${horizontalAlign}'`);
|
|
24
|
-
this.horizontalAlign = horizontalAlign;
|
|
25
|
-
}
|
|
26
|
-
if (verticalAlign !== undefined) {
|
|
27
|
-
if (!VERTICAL.includes(verticalAlign))
|
|
28
|
-
throw new TypeError(`verticalAlign must be 'top', 'middle' or 'bottom', not '${verticalAlign}'`);
|
|
29
|
-
this.verticalAlign = verticalAlign;
|
|
30
|
-
}
|
|
31
|
-
this.wrapHardBreaks = typeof wrapHardBreaks === 'undefined' ? true : wrapHardBreaks;
|
|
32
|
-
if (typeof this.wrapHardBreaks !== 'boolean')
|
|
33
|
-
throw new TypeError('Invalid wrapHardBreaks');
|
|
34
|
-
}
|
|
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
|
+
};
|
|
35
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/optimize.js
CHANGED
|
@@ -62,8 +62,10 @@ export class Optimizer {
|
|
|
62
62
|
const { replace, even, alpha, palette, tolerance } = this.options;
|
|
63
63
|
let changed = bounds(state, { x: 0, y: 0, width, height });
|
|
64
64
|
if (!changed) {
|
|
65
|
-
if (this.last)
|
|
65
|
+
if (this.last && this.last.duration + duration <= (this.options.maxDuration ?? Infinity))
|
|
66
66
|
this.last.duration += duration;
|
|
67
|
+
else if (this.last)
|
|
68
|
+
this.output(empty(duration));
|
|
67
69
|
else {
|
|
68
70
|
this.output(empty(duration));
|
|
69
71
|
this.opaque = false;
|