Skip to content

Commit cc3daa3

Browse files
committed
bound the memory of dashed strokes
1 parent ba74b9f commit cc3daa3

3 files changed

Lines changed: 120 additions & 39 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ Upgrading from 3.x? Upgrade to the latest 3.x release first, fix the deprecation
145145
- TIFF files that ask for far more memory than they are are refused before anything is allocated: YCbCr subsampling factors other than 1, 2 and 4 (65534x65535 took 4 GB and 5 s for a 32 KB file) and more than 32 samples per pixel (1 GB for 33 KB), and the memory of the YCbCr units counts against `maxPixels`.
146146
- `Animation.decode()` of an APNG or WebP composites each frame as it is decoded, so its peak memory is 1.3 times the result instead of 2.2 (all decoded frames were held next to the composited ones); GIFs let go of each frame's color indices once it is composited (1.45 times, now 1.2).
147147
- `renderSVG()` rasterizes shapes with many edges in near linear time: the edges that start in a row were inserted one by one into the sorted active ones, so text of 20000 glyphs took 15 s (now 0.4 s) and 8000 random curves 18 s (now 2.3 s).
148+
- `renderSVG()` keeps the memory of dashed strokes bounded: dashes beside the image are left out and the others are stroked a batch at a time, so a line of a million dashes (157 bytes of SVG) no longer takes 650 MB. The rasterizer also keeps one sub-scanline's crossings at a time instead of 16.
148149
- `renderSVG()` parses `transform` lists in linear time: a long list of unclosed functions took 4.8 s at 280 KB.
149150

150151
## 3.20.1

‎src/render/svg.ts‎

Lines changed: 95 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,19 @@ function validProperty(name: string, value: string): boolean {
702702
// the most elements to render through <use> references (like librsvg's limit), and dashes of a path
703703
const MAX_INSTANCES = 100000;
704704
const MAX_OCTAVES = 24;
705+
// the dashes stroked and rasterized at once
706+
const DASH_BATCH = 20000;
707+
708+
// the next n items of an iterator (fewer at its end)
709+
function take<T>(iterator: Iterator<T>, n: number): T[] {
710+
const items: T[] = [];
711+
while (items.length < n) {
712+
const next = iterator.next();
713+
if (next.done) break;
714+
items.push(next.value);
715+
}
716+
return items;
717+
}
705718
// the most pixels (at least MIN_NESTED each) that masks, clip paths, pattern tiles, markers and filter images may take
706719
// when rendered inside each other, which can multiply: three of them per level take minutes at ten levels
707720
// (32 nested ones the size of a 1080p image, or 1024 small ones)
@@ -1526,17 +1539,58 @@ class Document {
15261539
const strokeWidth = parseLength(style['stroke-width'], ctx.viewport, 16, 'diagonal');
15271540
if (!stroke || !(strokeWidth > 0)) return;
15281541

1529-
let lines = subpaths;
1542+
const lines = subpaths;
15301543
const fontSize = parseNumber(style['font-size'], 16);
15311544
const dashes = parseDashArray(style['stroke-dasharray'], ctx.viewport, fontSize);
1532-
if (dashes) lines = dash(subpaths, dashes, parseLength(style['stroke-dashoffset'], ctx.viewport, fontSize, 'diagonal'));
1545+
const miterLimit = Math.max(1, parseNumber(style['stroke-miterlimit'], 4));
1546+
if (dashes) {
1547+
// dashes whose stroke can't reach the layer are left out (their polygons would be anyway, see rasterize):
1548+
// the stroke reaches past a dash's points by half its width, at most the miter limit times that at a
1549+
// join, scaled by the matrix (its Frobenius norm is at least how much it stretches)
1550+
const reach = strokeWidth / 2 * Math.max(miterLimit, Math.SQRT2) * Math.hypot(matrix[0], matrix[1], matrix[2], matrix[3]) + 1;
1551+
const visible = (points: number[]): boolean => {
1552+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
1553+
for (let i = 0; i < points.length; i += 2) {
1554+
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];
1555+
x0 = Math.min(x0, x);
1556+
x1 = Math.max(x1, x);
1557+
y0 = Math.min(y0, y);
1558+
y1 = Math.max(y1, y);
1559+
}
1560+
return !(x1 + reach <= 0 || x0 - reach >= layer.width || y1 + reach <= 0 || y0 - reach >= layer.height);
1561+
};
1562+
// stroked and rasterized a batch of dashes at a time, their coverage added up: dashes don't overlap (but at
1563+
// sharp turns of wide strokes), so it's the same, and a million dashes don't take a gigabyte at once
1564+
const generated = dash(subpaths, dashes, parseLength(style['stroke-dashoffset'], ctx.viewport, fontSize, 'diagonal'), visible);
1565+
let total: Coverage | null = null;
1566+
for (let batch = take(generated, DASH_BATCH); batch.length; batch = take(generated, DASH_BATCH)) {
1567+
const coverage = this.strokeCoverage(batch, strokeWidth, miterLimit, tolerance, ctx);
1568+
if (!coverage) continue;
1569+
if (batch.length < DASH_BATCH && !total) {
1570+
total = coverage;
1571+
break;
1572+
}
1573+
total ??= { x0: 0, y0: 0, w: layer.width, h: layer.height, data: new Float32Array(layer.width * layer.height) };
1574+
for (let y = 0; y < coverage.h; y++)
1575+
for (let x = 0, o = y * coverage.w, t = (coverage.y0 + y) * total.w + coverage.x0; x < coverage.w; x++)
1576+
total.data[t + x] = Math.min(1, total.data[t + x] + coverage.data[o + x]);
1577+
}
1578+
if (total) layer.fill(crisp(total, style), stroke);
1579+
return;
1580+
}
15331581

1534-
const polygons = strokePolygons(lines, strokeWidth / 2, style['stroke-linecap'], style['stroke-linejoin'],
1535-
Math.max(1, parseNumber(style['stroke-miterlimit'], 4)), tolerance).map(points => transformPoints(points, matrix));
1536-
const coverage = rasterize(polygons, false, layer.width, layer.height);
1582+
const coverage = this.strokeCoverage(lines, strokeWidth, miterLimit, tolerance, ctx);
15371583
if (coverage) layer.fill(crisp(coverage, style), stroke);
15381584
}
15391585

1586+
// the coverage of the stroke of lines
1587+
strokeCoverage(lines: Subpath[], strokeWidth: number, miterLimit: number, tolerance: number, ctx: Context): Coverage | null {
1588+
const style = ctx.style as Style;
1589+
const polygons = strokePolygons(lines, strokeWidth / 2, style['stroke-linecap'], style['stroke-linejoin'],
1590+
miterLimit, tolerance).map(points => transformPoints(points, ctx.matrix));
1591+
return rasterize(polygons, false, ctx.layer.width, ctx.layer.height);
1592+
}
1593+
15401594
// draws the markers of a shape (like usvg's markers, crates/usvg/src/parser/marker.rs)
15411595
renderMarkers(path: PathCommand[], bbox: BBox | null, node: XNode, ctx: Context): void {
15421596
const style = ctx.style as Style;
@@ -2972,6 +3026,17 @@ function rasterize(polygons: number[][], evenOdd: boolean, width: number, height
29723026
for (const points of polygons) {
29733027
const n = points.length >> 1;
29743028
if (n < 2) continue;
3029+
// a polygon entirely beside the image has a winding number of 0 everywhere on it: it's left out (a dashed line
3030+
// reaching far past the image has a million dashes there)
3031+
let px0 = Infinity, py0 = Infinity, px1 = -Infinity, py1 = -Infinity;
3032+
for (let i = 0; i < n; i++) {
3033+
const x = points[i * 2], y = points[i * 2 + 1];
3034+
if (x < px0) px0 = x;
3035+
if (x > px1) px1 = x;
3036+
if (y < py0) py0 = y;
3037+
if (y > py1) py1 = y;
3038+
}
3039+
if (px1 <= 0 || px0 >= width || py1 <= 0 || py0 >= height) continue;
29753040

29763041
for (let i = 0; i < n; i++) {
29773042
const ax = points[i * 2], ay = points[i * 2 + 1];
@@ -3020,7 +3085,6 @@ function rasterize(polygons: number[][], evenOdd: boolean, width: number, height
30203085
for (let e = 0; e < numEdges; e++) if (startRow(e) < h) order[placed[startRow(e)]++] = e;
30213086
let numActive = 0;
30223087
const active = scratch.i32(4, numEdges), keys = scratch.f64(4, numEdges), firsts = scratch.i32(5, numEdges), lasts = scratch.i32(6, numEdges);
3023-
const counts = new Int32Array(SAMPLES), starts = new Int32Array(SAMPLES), ends = new Int32Array(SAMPLES);
30243088

30253089
const span = (a: number, b: number): void => {
30263090
a = Math.max(a, x0) - x0;
@@ -3081,10 +3145,8 @@ function rasterize(polygons: number[][], evenOdd: boolean, width: number, height
30813145
numActive += added;
30823146
}
30833147

3084-
// each active edge's crossings go to the sub-scanlines it spans (the bounds by the test of each sub-scanline,
3085-
// sy >= top and sy < bottom), each sub-scanline's in the edges' order
3086-
counts.fill(0);
3087-
let total = 0;
3148+
// the sub-scanlines each active edge spans (the bounds by the test of each sub-scanline, sy >= top and
3149+
// sy < bottom)
30883150
for (let k = 0; k < numActive; k++) {
30893151
const e = active[k], top = tops[e], bottom = bottoms[e];
30903152
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);
@@ -3095,33 +3157,27 @@ function rasterize(polygons: number[][], evenOdd: boolean, width: number, height
30953157
// (an edge ending far above the row has s1 below the Int32Array's range: it would wrap to a huge count)
30963158
firsts[k] = s0;
30973159
lasts[k] = Math.max(-1, s1);
3098-
for (let s = s0; s <= s1; s++) counts[s]++;
3099-
if (s1 >= s0) total += s1 - s0 + 1;
3100-
}
3101-
const crossings = scratch.f64(5, 2 * total);
3102-
for (let s = 0, at = 0; s < SAMPLES; s++) {
3103-
starts[s] = at;
3104-
at += 2 * counts[s];
3105-
}
3106-
for (let s = 0; s < SAMPLES; s++) ends[s] = starts[s];
3107-
for (let k = 0; k < numActive; k++) {
3108-
const e = active[k];
3109-
for (let s = firsts[k]; s <= lasts[k]; s++) {
3110-
const sy = py + OFFSETS[s];
3111-
crossings[ends[s]++] = xs[e] + (sy - tops[e]) * slopes[e];
3112-
crossings[ends[s]++] = dirs[e];
3113-
}
31143160
}
31153161

3162+
// each sub-scanline's crossings, in the edges' order (nearly sorted by x), one sub-scanline at a time: all of
3163+
// them at once took 16 times the memory of the active edges
3164+
const crossings = scratch.f64(5, 2 * numActive);
31163165
for (let s = 0; s < SAMPLES; s++) {
3117-
const base = starts[s], end = ends[s];
3118-
if (end - base < 4) continue;
3166+
const sy = py + OFFSETS[s];
3167+
let end = 0;
3168+
for (let k = 0; k < numActive; k++) {
3169+
if (s < firsts[k] || s > lasts[k]) continue;
3170+
const e = active[k];
3171+
crossings[end++] = xs[e] + (sy - tops[e]) * slopes[e];
3172+
crossings[end++] = dirs[e];
3173+
}
3174+
if (end < 4) continue;
31193175

31203176
// insertion sort pairs by x
3121-
for (let i = base + 2; i < end; i += 2) {
3177+
for (let i = 2; i < end; i += 2) {
31223178
const x = crossings[i], dir = crossings[i + 1];
31233179
let j = i - 2;
3124-
for (; j >= base && crossings[j] > x; j -= 2) {
3180+
for (; j >= 0 && crossings[j] > x; j -= 2) {
31253181
crossings[j + 2] = crossings[j];
31263182
crossings[j + 3] = crossings[j + 1];
31273183
}
@@ -3130,7 +3186,7 @@ function rasterize(polygons: number[][], evenOdd: boolean, width: number, height
31303186
}
31313187

31323188
let winding = 0;
3133-
for (let i = base; i < end - 2; i += 2) {
3189+
for (let i = 0; i < end - 2; i += 2) {
31343190
winding += crossings[i + 1];
31353191
if (evenOdd ? winding & 1 : winding !== 0) span(crossings[i], crossings[i + 2]);
31363192
}
@@ -3495,18 +3551,20 @@ function transformPoints(points: number[], m: Matrix): number[] {
34953551
return out;
34963552
}
34973553

3498-
// splits polylines into dashes
3499-
function dash(subpaths: Subpath[], pattern: number[], offset: number): Subpath[] {
3554+
// splits polylines into dashes, the ones visible says can be seen, as they are made (a path can have a million)
3555+
function* dash(subpaths: Subpath[], pattern: number[], offset: number, visible: (points: number[]) => boolean = () => true): Generator<Subpath> {
35003556
const total = pattern.reduce((a, b) => a + b, 0);
3501-
const result: Subpath[] = [];
35023557

35033558
// too many dashes stroke the path solid, as Skia does (they'd take any amount of memory)
35043559
let length = 0;
35053560
for (const { points, closed } of subpaths) {
35063561
const pts = closed ? [...points, points[0], points[1]] : points;
35073562
for (let i = 0; i + 3 < pts.length; i += 2) length += Math.hypot(pts[i + 2] - pts[i], pts[i + 3] - pts[i + 1]);
35083563
}
3509-
if (!(length / total * pattern.length <= MAX_DASHES)) return subpaths;
3564+
if (!(length / total * pattern.length <= MAX_DASHES)) {
3565+
yield* subpaths;
3566+
return;
3567+
}
35103568

35113569
for (const { points, closed } of subpaths) {
35123570
const pts = closed ? [...points, points[0], points[1]] : points;
@@ -3539,7 +3597,7 @@ function dash(subpaths: Subpath[], pattern: number[], offset: number): Subpath[]
35393597
if (on) {
35403598
if (current) {
35413599
current.push(mx, my);
3542-
result.push({ points: current, closed: false, direction: [(bx - ax) / length, (by - ay) / length] });
3600+
if (visible(current)) yield { points: current, closed: false, direction: [(bx - ax) / length, (by - ay) / length] };
35433601
}
35443602
current = null;
35453603
} else current = [mx, my];
@@ -3553,10 +3611,8 @@ function dash(subpaths: Subpath[], pattern: number[], offset: number): Subpath[]
35533611
}
35543612
}
35553613

3556-
if (on && current && current.length >= 4) result.push({ points: current, closed: false });
3614+
if (on && current && current.length >= 4 && visible(current)) yield { points: current, closed: false };
35573615
}
3558-
3559-
return result;
35603616
}
35613617

35623618
// builds fill polygons (counter-clockwise, to be filled nonzero) for stroked polylines

‎tests/svg-dos.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,28 @@ const timed = async (name, svg, limit = 5000) => {
112112
const moved = await Image.renderSVG(`<svg ${NS} width="20" height="20"><rect width="10" height="10" transform="rotate(rotate(translate(10 0)"/></svg>`);
113113
if (Colors.toRGBA(moved.getPixelAt(15, 5))[3] !== 255 || Colors.toRGBA(moved.getPixelAt(5, 5))[3] !== 0) panic('transform: the closed function should apply');
114114
}
115+
116+
// half a million dashes or more render in a 64 MB heap: the ones beside the image are left out, the others are
117+
// stroked a batch at a time (a 157 byte SVG took 650 MB, and failed in a 256 MB heap)
118+
{
119+
let zigzag = 'M0 0';
120+
for (let i = 1; i <= 200; i++) zigzag += ` L${i % 2 ? 500 : 0} ${i * 2.5}`;
121+
const script = `
122+
const {Image} = require(${JSON.stringify(require.resolve('..'))});
123+
const NS = 'xmlns="http://www.w3.org/2000/svg"';
124+
(async () => {
125+
const beside = await Image.renderSVG(\`<svg \${NS} width="100" height="10"><path d="M0 5 L100000 5" stroke="black" stroke-dasharray="0.1"/></svg>\`);
126+
const across = await Image.renderSVG(\`<svg \${NS} width="500" height="500"><path d="${zigzag}" fill="none" stroke="black" stroke-dasharray="0.2"/></svg>\`);
127+
let a = 0, b = 0;
128+
for (let i = 3; i < beside.bitmap.length; i += 4) a += beside.bitmap[i];
129+
for (let i = 3; i < across.bitmap.length; i += 4) b += across.bitmap[i];
130+
console.log(a, b);
131+
})();`;
132+
const child = require('child_process').spawnSync(process.execPath, ['--max-old-space-size=64', '-e', script], {encoding: 'utf8'});
133+
if (child.status !== 0) panic(`dashes in a 64 MB heap: ${child.stderr.split('\n').find(line => /heap|Error/.test(line)) ?? child.status}`);
134+
const [beside, across] = child.stdout.trim().split(' ').map(Number);
135+
// (half of the line shows: dashes of 0.1 with gaps as long; the zigzag about half of its length too)
136+
if (Math.abs(beside / (100 * 255) - 0.5) > 0.05) panic(`dashes beside the image: ${beside / 255} pixels covered of the line's 100, expected about half`);
137+
if (!(across > 0)) panic('the dashed zigzag shows nothing');
138+
}
115139
})();

0 commit comments

Comments
 (0)