imagescript 4.0.0-rc.2 → 4.0.0-rc.4
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 +10 -1
- package/dist/animation-encoder.d.ts +14 -6
- package/dist/animation-encoder.js +24 -9
- package/dist/animation.d.ts +68 -28
- package/dist/animation.js +82 -27
- package/dist/codecs/gif.js +16 -5
- package/dist/codecs/jpeg.js +2 -0
- package/dist/codecs/jxl/frame-header.js +6 -4
- package/dist/codecs/jxl/frame.js +4 -1
- package/dist/codecs/png.js +46 -20
- package/dist/codecs/quantize.js +25 -13
- package/dist/codecs/tiff.js +7 -0
- package/dist/codecs/webp.js +19 -18
- package/dist/colors.js +25 -17
- package/dist/frame.d.ts +18 -5
- package/dist/gainmap.d.ts +43 -14
- package/dist/gainmap.js +25 -16
- package/dist/image.d.ts +430 -173
- package/dist/image.js +109 -101
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/metadata.d.ts +11 -5
- package/dist/metadata.js +1 -1
- package/dist/package.json +1 -1
- package/dist/render/svg.js +172 -64
- package/dist/render/text.js +10 -8
- package/dist/text-layout.d.ts +28 -12
- package/dist/text-layout.js +17 -30
- package/dist/util/limits.d.ts +40 -0
- package/dist/util/limits.js +5 -0
- package/dist/util/options.js +44 -3
- package/dist/util/rotate.js +43 -0
- package/package.json +1 -2
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, LimitError
|
|
24
|
+
import { MAX_PIXELS, LimitError } 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',
|
|
@@ -82,9 +82,11 @@ export function render(source, mode, size, images = new Map(), fonts = [], maxPi
|
|
|
82
82
|
outHeight = Math.max(1, outHeight);
|
|
83
83
|
if (!Number.isSafeInteger(outWidth) || !Number.isSafeInteger(outHeight))
|
|
84
84
|
throw new RangeError(`SVG output dimensions are too large: ${outWidth}x${outHeight}`);
|
|
85
|
-
|
|
86
|
-
|
|
85
|
+
if (!(outWidth * outHeight <= maxPixels))
|
|
86
|
+
throw new LimitError(`SVG too large to render: ${outWidth}x${outHeight} pixels exceed the limit of ${maxPixels} (the render options' maxPixels)`);
|
|
87
|
+
const previous = layerBudget, previousCrossings = crossingsLeft;
|
|
87
88
|
layerBudget = { left: maxLayerPixels, limit: maxLayerPixels };
|
|
89
|
+
crossingsLeft = MAX_CROSSINGS;
|
|
88
90
|
try {
|
|
89
91
|
const canvas = new Layer(outWidth, outHeight);
|
|
90
92
|
doc.renderRoot([outWidth / width, 0, 0, outHeight / height, 0, 0], canvas, 0);
|
|
@@ -92,6 +94,7 @@ export function render(source, mode, size, images = new Map(), fonts = [], maxPi
|
|
|
92
94
|
}
|
|
93
95
|
finally {
|
|
94
96
|
layerBudget = previous;
|
|
97
|
+
crossingsLeft = previousCrossings;
|
|
95
98
|
}
|
|
96
99
|
}
|
|
97
100
|
let layerBudget = null;
|
|
@@ -556,10 +559,25 @@ function validProperty(name, value) {
|
|
|
556
559
|
}
|
|
557
560
|
}
|
|
558
561
|
const MAX_INSTANCES = 100000;
|
|
562
|
+
const MAX_OCTAVES = 24;
|
|
563
|
+
const DASH_BATCH = 20000;
|
|
564
|
+
function take(iterator, n) {
|
|
565
|
+
const items = [];
|
|
566
|
+
while (items.length < n) {
|
|
567
|
+
const next = iterator.next();
|
|
568
|
+
if (next.done)
|
|
569
|
+
break;
|
|
570
|
+
items.push(next.value);
|
|
571
|
+
}
|
|
572
|
+
return items;
|
|
573
|
+
}
|
|
574
|
+
const MAX_NESTED = 2 ** 26, MIN_NESTED = 2 ** 16;
|
|
559
575
|
const MAX_DASHES = 1000000;
|
|
576
|
+
const MAX_CROSSINGS = 2 ** 27;
|
|
577
|
+
let crossingsLeft = Infinity;
|
|
560
578
|
const MAX_TILE = 4096 * 1024;
|
|
561
579
|
class Document {
|
|
562
|
-
budget = { instances: 0 };
|
|
580
|
+
budget = { instances: 0, nested: 0, nesting: 0 };
|
|
563
581
|
ids;
|
|
564
582
|
active;
|
|
565
583
|
root;
|
|
@@ -1378,16 +1396,51 @@ class Document {
|
|
|
1378
1396
|
const strokeWidth = parseLength(style['stroke-width'], ctx.viewport, 16, 'diagonal');
|
|
1379
1397
|
if (!stroke || !(strokeWidth > 0))
|
|
1380
1398
|
return;
|
|
1381
|
-
|
|
1399
|
+
const lines = subpaths;
|
|
1382
1400
|
const fontSize = parseNumber(style['font-size'], 16);
|
|
1383
1401
|
const dashes = parseDashArray(style['stroke-dasharray'], ctx.viewport, fontSize);
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1402
|
+
const miterLimit = Math.max(1, parseNumber(style['stroke-miterlimit'], 4));
|
|
1403
|
+
if (dashes) {
|
|
1404
|
+
const reach = strokeWidth / 2 * Math.max(miterLimit, Math.SQRT2) * Math.hypot(matrix[0], matrix[1], matrix[2], matrix[3]) + 1;
|
|
1405
|
+
const visible = (points) => {
|
|
1406
|
+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
1407
|
+
for (let i = 0; i < points.length; i += 2) {
|
|
1408
|
+
const x = matrix[0] * points[i] + matrix[2] * points[i + 1] + matrix[4], y = matrix[1] * points[i] + matrix[3] * points[i + 1] + matrix[5];
|
|
1409
|
+
x0 = Math.min(x0, x);
|
|
1410
|
+
x1 = Math.max(x1, x);
|
|
1411
|
+
y0 = Math.min(y0, y);
|
|
1412
|
+
y1 = Math.max(y1, y);
|
|
1413
|
+
}
|
|
1414
|
+
return !(x1 + reach <= 0 || x0 - reach >= layer.width || y1 + reach <= 0 || y0 - reach >= layer.height);
|
|
1415
|
+
};
|
|
1416
|
+
const generated = dash(subpaths, dashes, parseLength(style['stroke-dashoffset'], ctx.viewport, fontSize, 'diagonal'), visible);
|
|
1417
|
+
let total = null;
|
|
1418
|
+
for (let batch = take(generated, DASH_BATCH); batch.length; batch = take(generated, DASH_BATCH)) {
|
|
1419
|
+
const coverage = this.strokeCoverage(batch, strokeWidth, miterLimit, tolerance, ctx);
|
|
1420
|
+
if (!coverage)
|
|
1421
|
+
continue;
|
|
1422
|
+
if (batch.length < DASH_BATCH && !total) {
|
|
1423
|
+
total = coverage;
|
|
1424
|
+
break;
|
|
1425
|
+
}
|
|
1426
|
+
total ??= { x0: 0, y0: 0, w: layer.width, h: layer.height, data: new Float32Array(layer.width * layer.height) };
|
|
1427
|
+
for (let y = 0; y < coverage.h; y++)
|
|
1428
|
+
for (let x = 0, o = y * coverage.w, t = (coverage.y0 + y) * total.w + coverage.x0; x < coverage.w; x++)
|
|
1429
|
+
total.data[t + x] = Math.min(1, total.data[t + x] + coverage.data[o + x]);
|
|
1430
|
+
}
|
|
1431
|
+
if (total)
|
|
1432
|
+
layer.fill(crisp(total, style), stroke);
|
|
1433
|
+
return;
|
|
1434
|
+
}
|
|
1435
|
+
const coverage = this.strokeCoverage(lines, strokeWidth, miterLimit, tolerance, ctx);
|
|
1388
1436
|
if (coverage)
|
|
1389
1437
|
layer.fill(crisp(coverage, style), stroke);
|
|
1390
1438
|
}
|
|
1439
|
+
strokeCoverage(lines, strokeWidth, miterLimit, tolerance, ctx) {
|
|
1440
|
+
const style = ctx.style;
|
|
1441
|
+
const polygons = strokePolygons(lines, strokeWidth / 2, style['stroke-linecap'], style['stroke-linejoin'], miterLimit, tolerance).map(points => transformPoints(points, ctx.matrix));
|
|
1442
|
+
return rasterize(polygons, false, ctx.layer.width, ctx.layer.height);
|
|
1443
|
+
}
|
|
1391
1444
|
renderMarkers(path, bbox, node, ctx) {
|
|
1392
1445
|
const style = ctx.style;
|
|
1393
1446
|
for (let p = node.parent; p; p = p.parent)
|
|
@@ -1435,10 +1488,12 @@ class Document {
|
|
|
1435
1488
|
matrix = multiply(matrix, [sx * units, 0, 0, sy * units, -refX * sx * units, -refY * sy * units]);
|
|
1436
1489
|
const inner = { ...ctx, matrix, style: markerStyle, depth: ctx.depth + 1, viewport: viewBox ? { width: viewBox[2], height: viewBox[3] } : ctx.viewport, contextElement };
|
|
1437
1490
|
this.active.add(marker);
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1491
|
+
this.indirect(0, () => {
|
|
1492
|
+
if (!clip)
|
|
1493
|
+
this.renderChildren(marker, inner);
|
|
1494
|
+
else
|
|
1495
|
+
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 }));
|
|
1496
|
+
});
|
|
1442
1497
|
this.active.delete(marker);
|
|
1443
1498
|
};
|
|
1444
1499
|
if (k === 0) {
|
|
@@ -1676,8 +1731,8 @@ class Document {
|
|
|
1676
1731
|
contentMatrix = multiply(contentMatrix, [box.width, 0, 0, box.height, 0, 0]);
|
|
1677
1732
|
const tile = new Layer(width, height);
|
|
1678
1733
|
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 });
|
|
1734
|
+
this.indirect(width * height, () => this.renderChildren(content, { ...ctx, matrix: contentMatrix, style: this.styleOf(content), layer: tile, depth: ctx.depth + 1,
|
|
1735
|
+
viewport: viewBox ? { width: viewBox[2], height: viewBox[3] } : ctx.viewport }));
|
|
1681
1736
|
this.active.delete(node);
|
|
1682
1737
|
tile.release();
|
|
1683
1738
|
if (tile.x1 <= tile.x0)
|
|
@@ -1939,7 +1994,7 @@ class Document {
|
|
|
1939
1994
|
let [fx, fy] = list && list.length === 2 ? list : list && list.length === 1 ? [list[0], list[0]] : [0, 0];
|
|
1940
1995
|
if (!(fx >= 0 && fy >= 0))
|
|
1941
1996
|
fx = fy = 0;
|
|
1942
|
-
const octaves = Math.max(0, number('numOctaves', 1));
|
|
1997
|
+
const octaves = Math.min(MAX_OCTAVES, Math.max(0, number('numOctaves', 1)));
|
|
1943
1998
|
return { type: 'turbulence', fx, fy, octaves: Math.round(octaves), seed: Math.trunc(number('seed', 0)), stitch: node.attrs.stitchTiles?.trim() === 'stitch',
|
|
1944
1999
|
fractal: node.attrs.type?.trim() === 'fractalNoise' };
|
|
1945
2000
|
}
|
|
@@ -2001,10 +2056,12 @@ class Document {
|
|
|
2001
2056
|
this.active.add(node);
|
|
2002
2057
|
const parentStyle = (target || node).parent && (target || node).parent.name !== '#root' ? this.styleOf((target || node).parent) : null;
|
|
2003
2058
|
const ctx = { matrix, style: parentStyle, viewport, layer, depth: 16 };
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2059
|
+
this.indirect(width * height, () => {
|
|
2060
|
+
if (target)
|
|
2061
|
+
this.renderNode(target, ctx);
|
|
2062
|
+
else
|
|
2063
|
+
this.renderImage(image, { ...ctx, style: this.computeStyle(image, this.styleOf(node)) });
|
|
2064
|
+
});
|
|
2008
2065
|
this.active.delete(node);
|
|
2009
2066
|
const pixmap = layer.toPixmap();
|
|
2010
2067
|
layer.release();
|
|
@@ -2117,7 +2174,22 @@ class Document {
|
|
|
2117
2174
|
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
2175
|
return { rect, primitives: [{ rect, linear: false, result: 'result', kind }] };
|
|
2119
2176
|
}
|
|
2177
|
+
indirect(pixels, render) {
|
|
2178
|
+
const budget = this.budget;
|
|
2179
|
+
if (budget.nesting > 0 && (budget.nested += Math.max(pixels, MIN_NESTED)) > MAX_NESTED)
|
|
2180
|
+
throw new Error('SVG: too many masks, clip paths, patterns, markers or filter images inside each other');
|
|
2181
|
+
budget.nesting++;
|
|
2182
|
+
try {
|
|
2183
|
+
return render();
|
|
2184
|
+
}
|
|
2185
|
+
finally {
|
|
2186
|
+
budget.nesting--;
|
|
2187
|
+
}
|
|
2188
|
+
}
|
|
2120
2189
|
clipMask(clip, node, matrix, context) {
|
|
2190
|
+
return this.indirect(context.layer.width * context.layer.height, () => this.clipMaskOf(clip, node, matrix, context));
|
|
2191
|
+
}
|
|
2192
|
+
clipMaskOf(clip, node, matrix, context) {
|
|
2121
2193
|
const { width, height } = context.layer;
|
|
2122
2194
|
const alpha = new Float32Array(width * height);
|
|
2123
2195
|
const style = this.styleOf(clip);
|
|
@@ -2209,6 +2281,9 @@ class Document {
|
|
|
2209
2281
|
return box;
|
|
2210
2282
|
}
|
|
2211
2283
|
maskValues(mask, node, matrix, context) {
|
|
2284
|
+
return this.indirect(context.layer.width * context.layer.height, () => this.maskValuesOf(mask, node, matrix, context));
|
|
2285
|
+
}
|
|
2286
|
+
maskValuesOf(mask, node, matrix, context) {
|
|
2212
2287
|
const { width, height } = context.layer;
|
|
2213
2288
|
const values = new Float32Array(width * height);
|
|
2214
2289
|
const path = shapePath(node, context.viewport, 16);
|
|
@@ -2748,6 +2823,20 @@ function rasterize(polygons, evenOdd, width, height) {
|
|
|
2748
2823
|
const n = points.length >> 1;
|
|
2749
2824
|
if (n < 2)
|
|
2750
2825
|
continue;
|
|
2826
|
+
let px0 = Infinity, py0 = Infinity, px1 = -Infinity, py1 = -Infinity;
|
|
2827
|
+
for (let i = 0; i < n; i++) {
|
|
2828
|
+
const x = points[i * 2], y = points[i * 2 + 1];
|
|
2829
|
+
if (x < px0)
|
|
2830
|
+
px0 = x;
|
|
2831
|
+
if (x > px1)
|
|
2832
|
+
px1 = x;
|
|
2833
|
+
if (y < py0)
|
|
2834
|
+
py0 = y;
|
|
2835
|
+
if (y > py1)
|
|
2836
|
+
py1 = y;
|
|
2837
|
+
}
|
|
2838
|
+
if (px1 <= 0 || px0 >= width || py1 <= 0 || py0 >= height)
|
|
2839
|
+
continue;
|
|
2751
2840
|
for (let i = 0; i < n; i++) {
|
|
2752
2841
|
const ax = points[i * 2], ay = points[i * 2 + 1];
|
|
2753
2842
|
const j = (i + 1) % n;
|
|
@@ -2779,7 +2868,7 @@ function rasterize(polygons, evenOdd, width, height) {
|
|
|
2779
2868
|
const steps = scratch.f32(1, w + 2);
|
|
2780
2869
|
const weight = 1 / SAMPLES;
|
|
2781
2870
|
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);
|
|
2871
|
+
const startRow = (e) => bottoms[e] <= y0 ? h : Math.max(0, Math.floor(tops[e]) - y0);
|
|
2783
2872
|
for (let e = 0; e < numEdges; e++)
|
|
2784
2873
|
if (startRow(e) < h)
|
|
2785
2874
|
rowStarts[startRow(e) + 1]++;
|
|
@@ -2792,7 +2881,6 @@ function rasterize(polygons, evenOdd, width, height) {
|
|
|
2792
2881
|
order[placed[startRow(e)]++] = e;
|
|
2793
2882
|
let numActive = 0;
|
|
2794
2883
|
const active = scratch.i32(4, numEdges), keys = scratch.f64(4, numEdges), firsts = scratch.i32(5, numEdges), lasts = scratch.i32(6, numEdges);
|
|
2795
|
-
const counts = new Int32Array(SAMPLES), starts = new Int32Array(SAMPLES), ends = new Int32Array(SAMPLES);
|
|
2796
2884
|
const span = (a, b) => {
|
|
2797
2885
|
a = Math.max(a, x0) - x0;
|
|
2798
2886
|
b = Math.min(b, x1) - x0;
|
|
@@ -2816,11 +2904,10 @@ function rasterize(polygons, evenOdd, width, height) {
|
|
|
2816
2904
|
if (bottoms[active[k]] > py)
|
|
2817
2905
|
active[kept++] = active[k];
|
|
2818
2906
|
numActive = kept;
|
|
2819
|
-
for (let k = rowStarts[py - y0]; k < rowStarts[py - y0 + 1]; k++)
|
|
2820
|
-
active[numActive++] = order[k];
|
|
2821
2907
|
const mid = py + 0.5;
|
|
2822
|
-
|
|
2823
|
-
|
|
2908
|
+
const keyOf = (e) => xs[e] + (Math.min(Math.max(mid, tops[e]), bottoms[e]) - tops[e]) * slopes[e];
|
|
2909
|
+
for (let k = 0; k < kept; k++) {
|
|
2910
|
+
const e = active[k], key = keyOf(e);
|
|
2824
2911
|
let j = k - 1;
|
|
2825
2912
|
for (; j >= 0 && keys[j] > key; j--) {
|
|
2826
2913
|
keys[j + 1] = keys[j];
|
|
@@ -2829,8 +2916,28 @@ function rasterize(polygons, evenOdd, width, height) {
|
|
|
2829
2916
|
keys[j + 1] = key;
|
|
2830
2917
|
active[j + 1] = e;
|
|
2831
2918
|
}
|
|
2832
|
-
|
|
2833
|
-
|
|
2919
|
+
const added = rowStarts[py - y0 + 1] - rowStarts[py - y0];
|
|
2920
|
+
if (added) {
|
|
2921
|
+
const first = rowStarts[py - y0];
|
|
2922
|
+
const newKeys = scratch.f64(6, added), ranks = scratch.i32(7, added).subarray(0, added);
|
|
2923
|
+
for (let n = 0; n < added; n++) {
|
|
2924
|
+
newKeys[n] = keyOf(order[first + n]);
|
|
2925
|
+
ranks[n] = n;
|
|
2926
|
+
}
|
|
2927
|
+
ranks.sort((a, b) => newKeys[a] - newKeys[b]);
|
|
2928
|
+
let i = kept - 1, n = added - 1, out = kept + added - 1;
|
|
2929
|
+
while (n >= 0) {
|
|
2930
|
+
if (i >= 0 && keys[i] > newKeys[ranks[n]]) {
|
|
2931
|
+
keys[out] = keys[i];
|
|
2932
|
+
active[out--] = active[i--];
|
|
2933
|
+
}
|
|
2934
|
+
else {
|
|
2935
|
+
keys[out] = newKeys[ranks[n]];
|
|
2936
|
+
active[out--] = order[first + ranks[n--]];
|
|
2937
|
+
}
|
|
2938
|
+
}
|
|
2939
|
+
numActive += added;
|
|
2940
|
+
}
|
|
2834
2941
|
for (let k = 0; k < numActive; k++) {
|
|
2835
2942
|
const e = active[k], top = tops[e], bottom = bottoms[e];
|
|
2836
2943
|
let s0 = Math.max(0, Math.ceil((top - py) * SAMPLES - 0.5)), s1 = Math.min(SAMPLES - 1, Math.ceil((bottom - py) * SAMPLES - 0.5) - 1);
|
|
@@ -2843,35 +2950,27 @@ function rasterize(polygons, evenOdd, width, height) {
|
|
|
2843
2950
|
while (s1 >= 0 && !(py + OFFSETS[s1] < bottom))
|
|
2844
2951
|
s1--;
|
|
2845
2952
|
firsts[k] = s0;
|
|
2846
|
-
lasts[k] = s1;
|
|
2847
|
-
for (let s = s0; s <= s1; s++)
|
|
2848
|
-
counts[s]++;
|
|
2849
|
-
if (s1 >= s0)
|
|
2850
|
-
total += s1 - s0 + 1;
|
|
2851
|
-
}
|
|
2852
|
-
const crossings = scratch.f64(5, 2 * total);
|
|
2853
|
-
for (let s = 0, at = 0; s < SAMPLES; s++) {
|
|
2854
|
-
starts[s] = at;
|
|
2855
|
-
at += 2 * counts[s];
|
|
2856
|
-
}
|
|
2857
|
-
for (let s = 0; s < SAMPLES; s++)
|
|
2858
|
-
ends[s] = starts[s];
|
|
2859
|
-
for (let k = 0; k < numActive; k++) {
|
|
2860
|
-
const e = active[k];
|
|
2861
|
-
for (let s = firsts[k]; s <= lasts[k]; s++) {
|
|
2862
|
-
const sy = py + OFFSETS[s];
|
|
2863
|
-
crossings[ends[s]++] = xs[e] + (sy - tops[e]) * slopes[e];
|
|
2864
|
-
crossings[ends[s]++] = dirs[e];
|
|
2865
|
-
}
|
|
2953
|
+
lasts[k] = Math.max(-1, s1);
|
|
2866
2954
|
}
|
|
2955
|
+
const crossings = scratch.f64(5, 2 * numActive);
|
|
2867
2956
|
for (let s = 0; s < SAMPLES; s++) {
|
|
2868
|
-
const
|
|
2869
|
-
|
|
2957
|
+
const sy = py + OFFSETS[s];
|
|
2958
|
+
let end = 0;
|
|
2959
|
+
for (let k = 0; k < numActive; k++) {
|
|
2960
|
+
if (s < firsts[k] || s > lasts[k])
|
|
2961
|
+
continue;
|
|
2962
|
+
const e = active[k];
|
|
2963
|
+
crossings[end++] = xs[e] + (sy - tops[e]) * slopes[e];
|
|
2964
|
+
crossings[end++] = dirs[e];
|
|
2965
|
+
}
|
|
2966
|
+
if (end < 4)
|
|
2870
2967
|
continue;
|
|
2871
|
-
|
|
2968
|
+
if ((crossingsLeft -= end >> 1) < 0)
|
|
2969
|
+
throw new LimitError(`SVG too complex: its shapes' edges cross the rows of the image more than ${MAX_CROSSINGS} times (many large overlapping strokes or shapes)`);
|
|
2970
|
+
for (let i = 2; i < end; i += 2) {
|
|
2872
2971
|
const x = crossings[i], dir = crossings[i + 1];
|
|
2873
2972
|
let j = i - 2;
|
|
2874
|
-
for (; j >=
|
|
2973
|
+
for (; j >= 0 && crossings[j] > x; j -= 2) {
|
|
2875
2974
|
crossings[j + 2] = crossings[j];
|
|
2876
2975
|
crossings[j + 3] = crossings[j + 1];
|
|
2877
2976
|
}
|
|
@@ -2879,7 +2978,7 @@ function rasterize(polygons, evenOdd, width, height) {
|
|
|
2879
2978
|
crossings[j + 3] = dir;
|
|
2880
2979
|
}
|
|
2881
2980
|
let winding = 0;
|
|
2882
|
-
for (let i =
|
|
2981
|
+
for (let i = 0; i < end - 2; i += 2) {
|
|
2883
2982
|
winding += crossings[i + 1];
|
|
2884
2983
|
if (evenOdd ? winding & 1 : winding !== 0)
|
|
2885
2984
|
span(crossings[i], crossings[i + 2]);
|
|
@@ -3237,17 +3336,18 @@ function transformPoints(points, m) {
|
|
|
3237
3336
|
}
|
|
3238
3337
|
return out;
|
|
3239
3338
|
}
|
|
3240
|
-
function dash(subpaths, pattern, offset) {
|
|
3339
|
+
function* dash(subpaths, pattern, offset, visible = () => true) {
|
|
3241
3340
|
const total = pattern.reduce((a, b) => a + b, 0);
|
|
3242
|
-
const result = [];
|
|
3243
3341
|
let length = 0;
|
|
3244
3342
|
for (const { points, closed } of subpaths) {
|
|
3245
3343
|
const pts = closed ? [...points, points[0], points[1]] : points;
|
|
3246
3344
|
for (let i = 0; i + 3 < pts.length; i += 2)
|
|
3247
3345
|
length += Math.hypot(pts[i + 2] - pts[i], pts[i + 3] - pts[i + 1]);
|
|
3248
3346
|
}
|
|
3249
|
-
if (!(length / total * pattern.length <= MAX_DASHES))
|
|
3250
|
-
|
|
3347
|
+
if (!(length / total * pattern.length <= MAX_DASHES)) {
|
|
3348
|
+
yield* subpaths;
|
|
3349
|
+
return;
|
|
3350
|
+
}
|
|
3251
3351
|
for (const { points, closed } of subpaths) {
|
|
3252
3352
|
const pts = closed ? [...points, points[0], points[1]] : points;
|
|
3253
3353
|
let index = 0;
|
|
@@ -3276,7 +3376,8 @@ function dash(subpaths, pattern, offset) {
|
|
|
3276
3376
|
if (on) {
|
|
3277
3377
|
if (current) {
|
|
3278
3378
|
current.push(mx, my);
|
|
3279
|
-
|
|
3379
|
+
if (visible(current))
|
|
3380
|
+
yield { points: current, closed: false, direction: [(bx - ax) / length, (by - ay) / length] };
|
|
3280
3381
|
}
|
|
3281
3382
|
current = null;
|
|
3282
3383
|
}
|
|
@@ -3291,10 +3392,9 @@ function dash(subpaths, pattern, offset) {
|
|
|
3291
3392
|
}
|
|
3292
3393
|
}
|
|
3293
3394
|
}
|
|
3294
|
-
if (on && current && current.length >= 4)
|
|
3295
|
-
|
|
3395
|
+
if (on && current && current.length >= 4 && visible(current))
|
|
3396
|
+
yield { points: current, closed: false };
|
|
3296
3397
|
}
|
|
3297
|
-
return result;
|
|
3298
3398
|
}
|
|
3299
3399
|
function strokePolygons(subpaths, halfWidth, cap, join, miterLimit, tolerance) {
|
|
3300
3400
|
const polygons = [];
|
|
@@ -3357,11 +3457,19 @@ function strokePolygons(subpaths, halfWidth, cap, join, miterLimit, tolerance) {
|
|
|
3357
3457
|
const dot = prev[0] * next[0] + prev[1] * next[1];
|
|
3358
3458
|
if (Math.abs(cross) < 1e-9 && dot > 0)
|
|
3359
3459
|
continue;
|
|
3460
|
+
const side = cross > 0 ? -1 : 1;
|
|
3360
3461
|
if (join === 'round') {
|
|
3361
|
-
|
|
3462
|
+
const turn = Math.acos(Math.max(-1, Math.min(1, dot)));
|
|
3463
|
+
const steps = Math.max(1, Math.ceil(turn / (2 * Math.PI / circleSegments)));
|
|
3464
|
+
const start = Math.atan2(side * prev[0], side * -prev[1]), sweep = side === -1 ? turn : -turn;
|
|
3465
|
+
const wedge = [vx, vy];
|
|
3466
|
+
for (let k = 0; k <= steps; k++) {
|
|
3467
|
+
const a = start + sweep * k / steps;
|
|
3468
|
+
wedge.push(vx + Math.cos(a) * halfWidth, vy + Math.sin(a) * halfWidth);
|
|
3469
|
+
}
|
|
3470
|
+
add(wedge);
|
|
3362
3471
|
continue;
|
|
3363
3472
|
}
|
|
3364
|
-
const side = cross > 0 ? -1 : 1;
|
|
3365
3473
|
const ax = vx + side * -prev[1] * halfWidth, ay = vy + side * prev[0] * halfWidth;
|
|
3366
3474
|
const bx = vx + side * -next[1] * halfWidth, by = vy + side * next[0] * halfWidth;
|
|
3367
3475
|
const theta = Math.acos(Math.max(-1, Math.min(1, dot)));
|
|
@@ -3390,7 +3498,7 @@ function strokePolygons(subpaths, halfWidth, cap, join, miterLimit, tolerance) {
|
|
|
3390
3498
|
}
|
|
3391
3499
|
function parseTransform(value) {
|
|
3392
3500
|
let matrix = [1, 0, 0, 1, 0, 0];
|
|
3393
|
-
const functions = /(matrix|translate|scale|rotate|skewX|skewY)\s*\(([^)]*)\)/g;
|
|
3501
|
+
const functions = /(matrix|translate|scale|rotate|skewX|skewY)\s*\(([^()]*)\)/g;
|
|
3394
3502
|
for (const [, name, args] of value.matchAll(functions)) {
|
|
3395
3503
|
const a = parseNumberList(args);
|
|
3396
3504
|
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,37 @@
|
|
|
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
|
-
|
|
13
|
-
|
|
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`
|
|
12
|
+
* @defaultValue The widest line's width
|
|
13
|
+
*/
|
|
14
|
+
width?: number;
|
|
15
|
+
/**
|
|
16
|
+
* The image's height in pixels: lines that don't fit are left out, and the text is placed in it by `verticalAlign`.
|
|
17
|
+
* @defaultValue The height of the lines
|
|
18
|
+
*/
|
|
19
|
+
height?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Whether to wrap between words or at any character
|
|
22
|
+
* @defaultValue `'word'`
|
|
23
|
+
*/
|
|
14
24
|
wrapStyle?: WrapStyle;
|
|
15
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Where the lines are placed horizontally: `'left'`, `'center'` or `'right'` in the `width`, or (without one)
|
|
27
|
+
* relative to the widest line
|
|
28
|
+
* @defaultValue `'left'`
|
|
29
|
+
*/
|
|
16
30
|
horizontalAlign?: HorizontalAlign;
|
|
17
|
-
/**
|
|
31
|
+
/**
|
|
32
|
+
* Where the text is placed vertically in the `height`: `'top'`, `'middle'` or `'bottom'`. When not all lines fit,
|
|
33
|
+
* these keep the first, the middle or the last lines. Without a `height`, it has no effect
|
|
34
|
+
* @defaultValue `'top'`
|
|
35
|
+
*/
|
|
18
36
|
verticalAlign?: VerticalAlign;
|
|
19
|
-
/** Whether to break lines at newline characters. Default: `true` */
|
|
20
|
-
wrapHardBreaks?: boolean;
|
|
21
37
|
}
|
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,40 @@
|
|
|
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
|
+
/**
|
|
17
|
+
* Throws a {@link LimitError} naming the operation if an image it makes would have more pixels than an image can
|
|
18
|
+
* (the default `maxPixels` of the decoders): a shadow or outline of a huge size or offset would only fail allocating
|
|
19
|
+
*/
|
|
20
|
+
export declare function checkGrowth(width: number, height: number, what: string): void;
|
|
21
|
+
/** The most bytes of raw samples per pixel of the limit (4 samples of 16 bits) */
|
|
22
|
+
export declare const MAX_BYTES_PER_PIXEL = 8;
|
|
23
|
+
/**
|
|
24
|
+
* Throws if an image's raw samples (bytes) would exceed what the pixel limit allows, for formats with any number of
|
|
25
|
+
* samples per pixel
|
|
26
|
+
*/
|
|
27
|
+
export declare function checkBytes(bytes: number, limit: number): void;
|
|
28
|
+
/** Counts the pixels of decoded frames against a limit */
|
|
29
|
+
export declare class PixelBudget {
|
|
30
|
+
private left;
|
|
31
|
+
private limit;
|
|
32
|
+
constructor(limit: number);
|
|
33
|
+
/** Throws if another frame of the given size would exceed the limit */
|
|
34
|
+
take(width: number, height: number): void;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Runs a decoder: reading past the end of damaged or truncated data throws a RangeError, which becomes an Error saying
|
|
38
|
+
* so (with the RangeError as its cause); exceeded limits stay RangeErrors
|
|
39
|
+
*/
|
|
40
|
+
export declare function decoding<T>(decode: () => T | Promise<T>): Promise<T>;
|
package/dist/util/limits.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
export class LimitError extends RangeError {
|
|
2
|
+
name = 'LimitError';
|
|
2
3
|
}
|
|
3
4
|
export const MAX_PIXELS = 0x3fff * 0x3fff;
|
|
4
5
|
export function checkPixels(width, height, limit, count = 1) {
|
|
5
6
|
if (!(width * height * count <= limit))
|
|
6
7
|
throw new LimitError(`Image too large: ${count > 1 ? `${count} frames of ` : ''}${width}x${height} pixels exceed the limit of ${limit} (the decode options' maxPixels)`);
|
|
7
8
|
}
|
|
9
|
+
export function checkGrowth(width, height, what) {
|
|
10
|
+
if (!(width * height <= MAX_PIXELS))
|
|
11
|
+
throw new LimitError(`${what} would make the image ${width}x${height} pixels, more than the limit of ${MAX_PIXELS}`);
|
|
12
|
+
}
|
|
8
13
|
export const MAX_BYTES_PER_PIXEL = 8;
|
|
9
14
|
export function checkBytes(bytes, limit) {
|
|
10
15
|
if (!(bytes <= limit * MAX_BYTES_PER_PIXEL))
|