imagescript 4.0.0-rc.4 → 4.0.0-rc.6
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 +9 -5
- package/dist/animation-encoder.d.ts +2 -1
- package/dist/animation-encoder.js +23 -19
- package/dist/animation.d.ts +26 -20
- package/dist/animation.js +58 -55
- package/dist/codecs/avif-enc.js +3 -3
- package/dist/codecs/avif.js +7 -2
- package/dist/codecs/bmp.js +1 -1
- package/dist/codecs/gif.js +2 -2
- package/dist/codecs/jpeg.js +55 -14
- package/dist/codecs/jxl/vardct-enc.js +2 -1
- package/dist/codecs/png.js +7 -3
- package/dist/codecs/tga.js +4 -3
- package/dist/codecs/tiff.js +1 -1
- package/dist/codecs/vp8.js +1 -1
- package/dist/codecs/vp8l.js +4 -3
- package/dist/codecs/webp.js +11 -9
- package/dist/colors.d.ts +10 -8
- package/dist/colors.js +28 -31
- package/dist/frame.d.ts +4 -3
- package/dist/frame.js +6 -8
- package/dist/gainmap.d.ts +6 -6
- package/dist/gainmap.js +32 -24
- package/dist/image.d.ts +75 -37
- package/dist/image.js +189 -163
- package/dist/metadata.d.ts +12 -1
- package/dist/metadata.js +30 -5
- package/dist/render/color.js +16 -3
- package/dist/render/colr.js +3 -3
- package/dist/render/filter.js +27 -3
- package/dist/render/font.js +54 -18
- package/dist/render/layout.js +2 -2
- package/dist/render/linebreak.js +9 -12
- package/dist/render/svg-text.js +112 -28
- package/dist/render/svg.js +183 -34
- package/dist/render/text.js +93 -23
- package/dist/text-layout.d.ts +2 -1
- package/dist/text-layout.js +3 -8
- package/dist/util/alpha.js +1 -1
- package/dist/util/inspect.js +16 -0
- package/dist/util/limits.d.ts +16 -3
- package/dist/util/limits.js +20 -5
- package/dist/util/mem.d.ts +2 -0
- package/dist/util/mem.js +24 -4
- package/dist/util/optimize.js +4 -3
- package/dist/util/options.js +104 -7
- package/package.json +1 -1
package/dist/render/svg.js
CHANGED
|
@@ -17,10 +17,11 @@ import { gunzip } from "../codecs/zlib.js";
|
|
|
17
17
|
import { applyFilter } from "./filter.js";
|
|
18
18
|
import { multiply, invert } from "../util/matrix.js";
|
|
19
19
|
import { BLEND_MODES, blendPixel } from "./blend.js";
|
|
20
|
-
import { fontFace, layOutText, parseFontFamilies, parseVariationSettings, pathLength, placeOutline, colorTablesOf } from "./svg-text.js";
|
|
20
|
+
import { fontFace, givenFontFace, damaged, layOutText, parseFontFamilies, parseVariationSettings, pathLength, placeOutline, colorTablesOf } from "./svg-text.js";
|
|
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 { bitmapPixels, MAX_BITMAP_GLYPH_PIXELS } from "./color.js";
|
|
24
25
|
import { MAX_PIXELS, LimitError } from "../util/limits.js";
|
|
25
26
|
const UNSUPPORTED = new Set(['filter', 'pattern', 'marker', 'foreignObject', 'script', 'style', 'title', 'desc', 'metadata',
|
|
26
27
|
'defs', 'clipPath', 'mask', 'linearGradient', 'radialGradient', 'stop', 'symbol', 'font', 'font-face', 'animate', 'animateTransform', 'animateMotion', 'set']);
|
|
@@ -61,7 +62,7 @@ export function render(source, mode, size, images = new Map(), fonts = [], maxPi
|
|
|
61
62
|
throw new Error('SVG: no <svg> element');
|
|
62
63
|
const doc = new Document(root);
|
|
63
64
|
doc.images = images;
|
|
64
|
-
doc.given = fonts.map(
|
|
65
|
+
doc.given = fonts.map(givenFontFace);
|
|
65
66
|
const { width, height } = documentSize(root);
|
|
66
67
|
if (!(width > 0) || !(height > 0))
|
|
67
68
|
throw new Error('SVG: invalid dimensions');
|
|
@@ -81,12 +82,25 @@ export function render(source, mode, size, images = new Map(), fonts = [], maxPi
|
|
|
81
82
|
outWidth = Math.max(1, outWidth);
|
|
82
83
|
outHeight = Math.max(1, outHeight);
|
|
83
84
|
if (!Number.isSafeInteger(outWidth) || !Number.isSafeInteger(outHeight))
|
|
84
|
-
throw new
|
|
85
|
+
throw new LimitError(`SVG too large to render: ${outWidth}x${outHeight} pixels`);
|
|
85
86
|
if (!(outWidth * outHeight <= maxPixels))
|
|
86
87
|
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;
|
|
88
|
+
const previous = layerBudget, previousCrossings = crossingsLeft, previousFilterWork = filterWorkLeft, previousPaint = paintLeft, previousPoints = pointsLeft;
|
|
88
89
|
layerBudget = { left: maxLayerPixels, limit: maxLayerPixels };
|
|
89
90
|
crossingsLeft = MAX_CROSSINGS;
|
|
91
|
+
filterWorkLeft = MAX_FILTER_WORK + FILTER_WORK_PER_PIXEL * outWidth * outHeight;
|
|
92
|
+
paintLeft = MAX_PAINT + PAINT_PER_PIXEL * outWidth * outHeight;
|
|
93
|
+
const previousPaths = parsedPaths, previousCached = cachedCommands;
|
|
94
|
+
if (!previousPaths)
|
|
95
|
+
pointsLeft = MAX_POINTS + POINTS_PER_PIXEL * outWidth * outHeight;
|
|
96
|
+
parsedPaths = new Map();
|
|
97
|
+
const previousBitmaps = decodedBitmaps, previousBitmapPixels = decodedBitmapPixels;
|
|
98
|
+
if (!decodedBitmaps) {
|
|
99
|
+
decodedBitmaps = new WeakMap();
|
|
100
|
+
decodedBitmapPixels = 0;
|
|
101
|
+
}
|
|
102
|
+
if (!previousPaths)
|
|
103
|
+
cachedCommands = 0;
|
|
90
104
|
try {
|
|
91
105
|
const canvas = new Layer(outWidth, outHeight);
|
|
92
106
|
doc.renderRoot([outWidth / width, 0, 0, outHeight / height, 0, 0], canvas, 0);
|
|
@@ -95,6 +109,15 @@ export function render(source, mode, size, images = new Map(), fonts = [], maxPi
|
|
|
95
109
|
finally {
|
|
96
110
|
layerBudget = previous;
|
|
97
111
|
crossingsLeft = previousCrossings;
|
|
112
|
+
filterWorkLeft = previousFilterWork;
|
|
113
|
+
paintLeft = previousPaint;
|
|
114
|
+
if (!previousPaths)
|
|
115
|
+
pointsLeft = previousPoints;
|
|
116
|
+
parsedPaths = previousPaths;
|
|
117
|
+
decodedBitmaps = previousBitmaps;
|
|
118
|
+
if (!previousBitmaps)
|
|
119
|
+
decodedBitmapPixels = previousBitmapPixels;
|
|
120
|
+
cachedCommands = previousCached;
|
|
98
121
|
}
|
|
99
122
|
}
|
|
100
123
|
let layerBudget = null;
|
|
@@ -107,7 +130,11 @@ function documentSize(root) {
|
|
|
107
130
|
return fallback * parseFloat(value) / 100;
|
|
108
131
|
return parseLength(value, null, 16);
|
|
109
132
|
};
|
|
110
|
-
|
|
133
|
+
const width = lengthOf(root.attrs.width, viewBox ? viewBox[2] : 100), height = lengthOf(root.attrs.height, viewBox ? viewBox[3] : 100);
|
|
134
|
+
const given = (value) => value !== undefined && value !== 'auto';
|
|
135
|
+
if (viewBox && viewBox[2] > 0 && viewBox[3] > 0 && given(root.attrs.width) !== given(root.attrs.height))
|
|
136
|
+
return given(root.attrs.width) ? { width, height: width * viewBox[3] / viewBox[2] } : { width: height * viewBox[2] / viewBox[3], height };
|
|
137
|
+
return { width, height };
|
|
111
138
|
}
|
|
112
139
|
const glyphDocuments = new WeakMap();
|
|
113
140
|
function drawRaster(layer, image, matrix, filter) {
|
|
@@ -185,7 +212,8 @@ export function imageReferences(source) {
|
|
|
185
212
|
const stack = [root];
|
|
186
213
|
while (stack.length) {
|
|
187
214
|
const node = stack.pop();
|
|
188
|
-
|
|
215
|
+
for (const child of node.children)
|
|
216
|
+
stack.push(child);
|
|
189
217
|
if ((node.name !== 'image' && node.name !== 'feImage') || !node.svg)
|
|
190
218
|
continue;
|
|
191
219
|
const href = (node.attrs.href || node.attrs['xlink:href'] || '').trim();
|
|
@@ -230,7 +258,7 @@ function parseXML(text) {
|
|
|
230
258
|
return match;
|
|
231
259
|
size += custom[name].length - match.length;
|
|
232
260
|
if (size > limit)
|
|
233
|
-
throw new
|
|
261
|
+
throw new LimitError('SVG too complex: entity expansion exceeds the size limit');
|
|
234
262
|
return custom[name];
|
|
235
263
|
});
|
|
236
264
|
}
|
|
@@ -559,6 +587,7 @@ function validProperty(name, value) {
|
|
|
559
587
|
}
|
|
560
588
|
}
|
|
561
589
|
const MAX_INSTANCES = 100000;
|
|
590
|
+
const MAX_MARKERS = 10000;
|
|
562
591
|
const MAX_OCTAVES = 24;
|
|
563
592
|
const DASH_BATCH = 20000;
|
|
564
593
|
function take(iterator, n) {
|
|
@@ -575,9 +604,35 @@ const MAX_NESTED = 2 ** 26, MIN_NESTED = 2 ** 16;
|
|
|
575
604
|
const MAX_DASHES = 1000000;
|
|
576
605
|
const MAX_CROSSINGS = 2 ** 27;
|
|
577
606
|
let crossingsLeft = Infinity;
|
|
607
|
+
const MAX_FILTER_WORK = 2 ** 25, FILTER_WORK_PER_PIXEL = 16;
|
|
608
|
+
let filterWorkLeft = Infinity;
|
|
609
|
+
const MAX_PAINT = 2 ** 28, PAINT_PER_PIXEL = 64;
|
|
610
|
+
let paintLeft = Infinity;
|
|
611
|
+
function charge(pixels) {
|
|
612
|
+
if ((paintLeft -= pixels) < 0)
|
|
613
|
+
throw new LimitError(`SVG too complex: its shapes and layers paint more pixels than a render of this size allows (${MAX_PAINT} and ${PAINT_PER_PIXEL} per output pixel)`);
|
|
614
|
+
}
|
|
615
|
+
const filterBudget = {
|
|
616
|
+
work(pixels) {
|
|
617
|
+
if ((filterWorkLeft -= pixels) < 0)
|
|
618
|
+
throw new LimitError(`SVG too complex: its filters' primitives work on more pixels than a render of this size allows (${MAX_FILTER_WORK} and ${FILTER_WORK_PER_PIXEL} per output pixel)`);
|
|
619
|
+
},
|
|
620
|
+
keep(pixels) {
|
|
621
|
+
const budget = layerBudget;
|
|
622
|
+
if (!budget)
|
|
623
|
+
return;
|
|
624
|
+
if (!(pixels <= budget.left))
|
|
625
|
+
throw new LimitError(`SVG too complex: its layers (for opacity, masks, filters, ...) exceed ${budget.limit} pixels at once (the render options' maxLayerPixels)`);
|
|
626
|
+
budget.left -= pixels;
|
|
627
|
+
},
|
|
628
|
+
free(pixels) {
|
|
629
|
+
if (layerBudget)
|
|
630
|
+
layerBudget.left += pixels;
|
|
631
|
+
}
|
|
632
|
+
};
|
|
578
633
|
const MAX_TILE = 4096 * 1024;
|
|
579
634
|
class Document {
|
|
580
|
-
budget = { instances: 0, nested: 0, nesting: 0 };
|
|
635
|
+
budget = { instances: 0, markers: 0, nested: 0, nesting: 0 };
|
|
581
636
|
ids;
|
|
582
637
|
active;
|
|
583
638
|
root;
|
|
@@ -615,7 +670,8 @@ class Document {
|
|
|
615
670
|
const stack = [this.root];
|
|
616
671
|
while (stack.length) {
|
|
617
672
|
const node = stack.pop();
|
|
618
|
-
|
|
673
|
+
for (const child of node.children)
|
|
674
|
+
stack.push(child);
|
|
619
675
|
if (node.name !== 'feImage' || !node.parent || !node.parent.attrs.id)
|
|
620
676
|
continue;
|
|
621
677
|
const href = this.href(node)?.trim(), target = href?.startsWith('#') ? this.reference(href) : null;
|
|
@@ -633,11 +689,12 @@ class Document {
|
|
|
633
689
|
return inline?.important[name] ?? inline?.normal[name] ?? node.attrs[name];
|
|
634
690
|
};
|
|
635
691
|
const descendants = (node) => {
|
|
636
|
-
const list = [], stack = [...node.children];
|
|
692
|
+
const list = [], stack = [...node.children].reverse();
|
|
637
693
|
while (stack.length) {
|
|
638
|
-
const n = stack.
|
|
694
|
+
const n = stack.pop();
|
|
639
695
|
list.push(n);
|
|
640
|
-
|
|
696
|
+
for (let i = n.children.length - 1; i >= 0; i--)
|
|
697
|
+
stack.push(n.children[i]);
|
|
641
698
|
}
|
|
642
699
|
return list;
|
|
643
700
|
};
|
|
@@ -750,7 +807,7 @@ class Document {
|
|
|
750
807
|
if (!node.svg || UNSUPPORTED.has(node.name) || context.depth > 32 || !this.passesConditions(node))
|
|
751
808
|
return;
|
|
752
809
|
if (context.instanced && ++this.budget.instances > MAX_INSTANCES)
|
|
753
|
-
throw new
|
|
810
|
+
throw new LimitError(`SVG too complex: more than ${MAX_INSTANCES} elements rendered through <use> references, markers or embedded SVGs`);
|
|
754
811
|
const style = this.computeStyle(node, context.style);
|
|
755
812
|
if (style.display === 'none')
|
|
756
813
|
return;
|
|
@@ -902,16 +959,32 @@ class Document {
|
|
|
902
959
|
const fonts = this.fonts;
|
|
903
960
|
if (fonts.length) {
|
|
904
961
|
try {
|
|
905
|
-
layout = layOutText(this.textInput(node, style, viewport, scale), fonts);
|
|
962
|
+
layout = layOutText(this.textInput(node, style, viewport, scale), fonts, this.given[0] ?? fonts[0]);
|
|
906
963
|
}
|
|
907
964
|
catch (err) {
|
|
908
|
-
if (!(err instanceof RangeError))
|
|
965
|
+
if (!(err instanceof RangeError) || err instanceof LimitError)
|
|
909
966
|
throw err;
|
|
967
|
+
if (this.given.length && this.givenFontsFail(node, style, viewport, scale))
|
|
968
|
+
throw damaged(err);
|
|
910
969
|
}
|
|
911
970
|
}
|
|
971
|
+
if (layout)
|
|
972
|
+
for (const span of layout.spans)
|
|
973
|
+
glyphPaths.add(span.glyphs);
|
|
912
974
|
this.texts.set(node, { style, width: viewport.width, height: viewport.height, scale, layout });
|
|
913
975
|
return layout;
|
|
914
976
|
}
|
|
977
|
+
givenFontsFail(node, style, viewport, scale) {
|
|
978
|
+
if (!this.embedded.length)
|
|
979
|
+
return true;
|
|
980
|
+
try {
|
|
981
|
+
layOutText(this.textInput(node, style, viewport, scale), this.given);
|
|
982
|
+
return false;
|
|
983
|
+
}
|
|
984
|
+
catch (err) {
|
|
985
|
+
return err instanceof RangeError && !(err instanceof LimitError);
|
|
986
|
+
}
|
|
987
|
+
}
|
|
915
988
|
textInput(text, textStyle, viewport, scale) {
|
|
916
989
|
const leaves = [];
|
|
917
990
|
const preserveOf = (node, inherited) => node.attrs['xml:space'] === undefined ? inherited : node.attrs['xml:space'] === 'preserve';
|
|
@@ -1138,13 +1211,14 @@ class Document {
|
|
|
1138
1211
|
};
|
|
1139
1212
|
collect(root);
|
|
1140
1213
|
const mode = textStyle['writing-mode'];
|
|
1141
|
-
return { chunks, dx, dy, rotate: rotations, vertical: mode === 'tb' || mode === 'tb-rl' || mode === 'vertical-rl' || mode === 'vertical-lr', scale };
|
|
1214
|
+
return { chunks, dx, dy, rotate: rotations, vertical: mode === 'tb' || mode === 'tb-rl' || mode === 'vertical-rl' || mode === 'vertical-lr', scale, charge: commands => chargePoints(GLYPH_COMMAND_POINTS * commands) };
|
|
1142
1215
|
}
|
|
1143
1216
|
textPathOf(node, viewport, fontSize) {
|
|
1144
1217
|
const target = this.reference(this.href(node));
|
|
1145
1218
|
const raw = target ? shapePath(target, viewport, fontSize) : null;
|
|
1146
1219
|
if (!target || !raw || !raw.length)
|
|
1147
1220
|
return null;
|
|
1221
|
+
chargePoints(16 * raw.length);
|
|
1148
1222
|
const m = target.attrs.transform ? originTransform(target.attrs.transform, target.attrs['transform-origin'], viewport) : null;
|
|
1149
1223
|
const path = m ? raw.map(c => [c[0], ...transformPoints(c.slice(1), m)]) : raw;
|
|
1150
1224
|
const offset = (node.attrs.startOffset ?? '').trim();
|
|
@@ -1167,8 +1241,17 @@ class Document {
|
|
|
1167
1241
|
paint(d.path, d.paint, 'normal');
|
|
1168
1242
|
if (span.glyphs.length)
|
|
1169
1243
|
paint(span.glyphs, span.paint, span.paint['paint-order']);
|
|
1170
|
-
for (const glyph of span.colors)
|
|
1171
|
-
|
|
1244
|
+
for (const glyph of span.colors) {
|
|
1245
|
+
try {
|
|
1246
|
+
this.renderColorGlyph(glyph, span.paint, node, ctx, paint);
|
|
1247
|
+
}
|
|
1248
|
+
catch (err) {
|
|
1249
|
+
if (!(err instanceof RangeError) || err instanceof LimitError)
|
|
1250
|
+
throw err;
|
|
1251
|
+
if (this.given.includes(glyph.face))
|
|
1252
|
+
throw damaged(err);
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1172
1255
|
for (const d of span.after)
|
|
1173
1256
|
paint(d.path, d.paint, 'normal');
|
|
1174
1257
|
}
|
|
@@ -1204,13 +1287,9 @@ class Document {
|
|
|
1204
1287
|
const bitmap = tables.bitmaps(Math.round(s * font.unitsPerEm))?.(g.glyph) ?? tables.bitmaps(g.fontSize)?.(g.glyph);
|
|
1205
1288
|
if (!bitmap)
|
|
1206
1289
|
return;
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
decoded = decodePNG(bitmap.png);
|
|
1210
|
-
}
|
|
1211
|
-
catch {
|
|
1290
|
+
const decoded = decodedBitmap(bitmap.png);
|
|
1291
|
+
if (!decoded)
|
|
1212
1292
|
return;
|
|
1213
|
-
}
|
|
1214
1293
|
const k = font.unitsPerEm / bitmap.ppem;
|
|
1215
1294
|
let left = bitmap.left * k, top = bitmap.top * k;
|
|
1216
1295
|
if (font.tables.sbix && !font.tables.CBDT) {
|
|
@@ -1277,7 +1356,9 @@ class Document {
|
|
|
1277
1356
|
for (const span of layout.spans)
|
|
1278
1357
|
if (span.visible)
|
|
1279
1358
|
for (const part of [...span.before, { path: span.glyphs }, ...span.after])
|
|
1280
|
-
|
|
1359
|
+
for (const command of part.path)
|
|
1360
|
+
path.push(command);
|
|
1361
|
+
glyphPaths.add(path);
|
|
1281
1362
|
return path.length ? path : null;
|
|
1282
1363
|
}
|
|
1283
1364
|
renderImage(node, ctx) {
|
|
@@ -1486,7 +1567,9 @@ class Document {
|
|
|
1486
1567
|
matrix = multiply(matrix, [cos, sin, -sin, cos, 0, 0]);
|
|
1487
1568
|
}
|
|
1488
1569
|
matrix = multiply(matrix, [sx * units, 0, 0, sy * units, -refX * sx * units, -refY * sy * units]);
|
|
1489
|
-
|
|
1570
|
+
if (++this.budget.markers > MAX_MARKERS)
|
|
1571
|
+
throw new LimitError(`SVG too complex: more than ${MAX_MARKERS} markers`);
|
|
1572
|
+
const inner = { ...ctx, matrix, style: markerStyle, depth: ctx.depth + 1, viewport: viewBox ? { width: viewBox[2], height: viewBox[3] } : ctx.viewport, contextElement, instanced: true };
|
|
1490
1573
|
this.active.add(marker);
|
|
1491
1574
|
this.indirect(0, () => {
|
|
1492
1575
|
if (!clip)
|
|
@@ -1769,7 +1852,7 @@ class Document {
|
|
|
1769
1852
|
lh = Math.min(ly + lh, Math.ceil(cy1)) - top;
|
|
1770
1853
|
lx = left;
|
|
1771
1854
|
ly = top;
|
|
1772
|
-
if (lw <= 0 || lh <= 0
|
|
1855
|
+
if (lw <= 0 || lh <= 0)
|
|
1773
1856
|
return;
|
|
1774
1857
|
const shifted = multiply([1, 0, 0, 1, -lx, -ly], scaled);
|
|
1775
1858
|
const layer = new Layer(lw, lh);
|
|
@@ -1777,7 +1860,7 @@ class Document {
|
|
|
1777
1860
|
const pixmap = layer.toPixmap();
|
|
1778
1861
|
layer.release();
|
|
1779
1862
|
for (const filter of filters)
|
|
1780
|
-
applyFilter(filter, shifted, pixmap);
|
|
1863
|
+
applyFilter(filter, shifted, pixmap, filterBudget);
|
|
1781
1864
|
const result = Layer.fromPixmap(pixmap);
|
|
1782
1865
|
if (result.x1 <= result.x0)
|
|
1783
1866
|
return result.release();
|
|
@@ -2177,7 +2260,7 @@ class Document {
|
|
|
2177
2260
|
indirect(pixels, render) {
|
|
2178
2261
|
const budget = this.budget;
|
|
2179
2262
|
if (budget.nesting > 0 && (budget.nested += Math.max(pixels, MIN_NESTED)) > MAX_NESTED)
|
|
2180
|
-
throw new
|
|
2263
|
+
throw new LimitError('SVG too complex: too many masks, clip paths, patterns, markers or filter images inside each other');
|
|
2181
2264
|
budget.nesting++;
|
|
2182
2265
|
try {
|
|
2183
2266
|
return render();
|
|
@@ -2667,6 +2750,7 @@ class Layer {
|
|
|
2667
2750
|
}
|
|
2668
2751
|
fill({ x0, y0, w, h, data }, paint) {
|
|
2669
2752
|
const d = this.data;
|
|
2753
|
+
let painted = 0;
|
|
2670
2754
|
const solid = paint.solid;
|
|
2671
2755
|
this.extend(x0, y0, x0 + w, y0 + h);
|
|
2672
2756
|
for (let y = 0; y < h; y++) {
|
|
@@ -2675,6 +2759,7 @@ class Layer {
|
|
|
2675
2759
|
const c = data[o];
|
|
2676
2760
|
if (c <= 0)
|
|
2677
2761
|
continue;
|
|
2762
|
+
painted++;
|
|
2678
2763
|
const color = solid || paint(x0 + x + 0.5, y0 + y + 0.5);
|
|
2679
2764
|
const a = color[3] * c;
|
|
2680
2765
|
if (a <= 0)
|
|
@@ -2689,6 +2774,7 @@ class Layer {
|
|
|
2689
2774
|
while (x + run < w && data[o + run] >= 1)
|
|
2690
2775
|
run++;
|
|
2691
2776
|
if (run > 1) {
|
|
2777
|
+
painted += run - 1;
|
|
2692
2778
|
for (let filled = 1; filled < run; filled *= 2)
|
|
2693
2779
|
d.copyWithin(i + filled * 4, i, i + Math.min(filled, run - filled) * 4);
|
|
2694
2780
|
x += run - 1;
|
|
@@ -2705,6 +2791,7 @@ class Layer {
|
|
|
2705
2791
|
d[i + 3] = a + d[i + 3] * inv;
|
|
2706
2792
|
}
|
|
2707
2793
|
}
|
|
2794
|
+
charge(painted + (w * h >> 4));
|
|
2708
2795
|
}
|
|
2709
2796
|
toPixmap() {
|
|
2710
2797
|
const d = this.data, out = new Uint8Array(d.length);
|
|
@@ -2746,6 +2833,7 @@ class Layer {
|
|
|
2746
2833
|
composite(layer, opacity, mask, blend = 'normal') {
|
|
2747
2834
|
if (layer.x1 <= layer.x0)
|
|
2748
2835
|
return;
|
|
2836
|
+
charge((layer.x1 - layer.x0) * (layer.y1 - layer.y0));
|
|
2749
2837
|
this.extend(layer.x0, layer.y0, layer.x1, layer.y1);
|
|
2750
2838
|
const s = layer.data, d = this.data;
|
|
2751
2839
|
if (blend !== 'normal') {
|
|
@@ -2997,8 +3085,16 @@ function rasterize(polygons, evenOdd, width, height) {
|
|
|
2997
3085
|
function shapePath(node, viewport, fontSize) {
|
|
2998
3086
|
const length = (name, axis, fallback = 0) => node.attrs[name] === undefined ? fallback : parseLength(node.attrs[name], viewport, fontSize, axis);
|
|
2999
3087
|
switch (node.name) {
|
|
3000
|
-
case 'path':
|
|
3001
|
-
|
|
3088
|
+
case 'path': {
|
|
3089
|
+
const d = node.attrs.d || '';
|
|
3090
|
+
const cached = parsedPaths?.get(d);
|
|
3091
|
+
if (cached)
|
|
3092
|
+
return cached;
|
|
3093
|
+
const path = parsePath(d);
|
|
3094
|
+
if (parsedPaths && (cachedCommands += path.length) <= MAX_CACHED_COMMANDS)
|
|
3095
|
+
parsedPaths.set(d, path);
|
|
3096
|
+
return path;
|
|
3097
|
+
}
|
|
3002
3098
|
case 'rect': {
|
|
3003
3099
|
const x = length('x', 'x'), y = length('y', 'y');
|
|
3004
3100
|
const w = length('width', 'x'), h = length('height', 'y');
|
|
@@ -3070,8 +3166,55 @@ function shapePath(node, viewport, fontSize) {
|
|
|
3070
3166
|
}
|
|
3071
3167
|
return null;
|
|
3072
3168
|
}
|
|
3169
|
+
const MAX_POINTS = 2 ** 25, POINTS_PER_PIXEL = 4, MAX_PATH_POINTS = 2 ** 21;
|
|
3170
|
+
let pointsLeft = Infinity;
|
|
3171
|
+
let parsedPaths = null, cachedCommands = 0;
|
|
3172
|
+
const glyphPaths = new WeakSet();
|
|
3173
|
+
let decodedBitmaps = null, decodedBitmapPixels = 0;
|
|
3174
|
+
const MAX_KEPT_BITMAP_PIXELS = 2 ** 22;
|
|
3175
|
+
function decodedBitmap(png) {
|
|
3176
|
+
let kept = decodedBitmaps?.get(png.buffer);
|
|
3177
|
+
const key = png.byteOffset * 2 ** 21 + Math.min(png.length, 2 ** 21 - 1);
|
|
3178
|
+
const cached = kept?.get(key);
|
|
3179
|
+
if (cached !== undefined)
|
|
3180
|
+
return cached;
|
|
3181
|
+
let decoded = null;
|
|
3182
|
+
const declared = bitmapPixels(png);
|
|
3183
|
+
if (declared <= MAX_BITMAP_GLYPH_PIXELS) {
|
|
3184
|
+
charge(4 * Math.max(declared, 1));
|
|
3185
|
+
try {
|
|
3186
|
+
decoded = decodePNG(png, MAX_BITMAP_GLYPH_PIXELS);
|
|
3187
|
+
}
|
|
3188
|
+
catch {
|
|
3189
|
+
decoded = null;
|
|
3190
|
+
}
|
|
3191
|
+
}
|
|
3192
|
+
const pixels = decoded ? decoded.width * decoded.height : 0;
|
|
3193
|
+
if (decodedBitmaps && decodedBitmapPixels + pixels <= MAX_KEPT_BITMAP_PIXELS) {
|
|
3194
|
+
decodedBitmapPixels += pixels;
|
|
3195
|
+
if (!kept)
|
|
3196
|
+
decodedBitmaps.set(png.buffer, kept = new Map());
|
|
3197
|
+
kept.set(key, decoded);
|
|
3198
|
+
}
|
|
3199
|
+
return decoded;
|
|
3200
|
+
}
|
|
3201
|
+
const MAX_CACHED_COMMANDS = 2 ** 20;
|
|
3202
|
+
function pointCounter() {
|
|
3203
|
+
let pathPoints = 0;
|
|
3204
|
+
return points => {
|
|
3205
|
+
pathPoints += points;
|
|
3206
|
+
if ((pointsLeft -= points) < 0 || pathPoints > MAX_PATH_POINTS)
|
|
3207
|
+
throw new LimitError(`SVG too complex: its paths are made of more points than a render of this size allows (${MAX_PATH_POINTS} in a path, ${MAX_POINTS} and ${POINTS_PER_PIXEL} per output pixel in all)`);
|
|
3208
|
+
};
|
|
3209
|
+
}
|
|
3210
|
+
const GLYPH_COMMAND_POINTS = 16;
|
|
3211
|
+
function chargePoints(points) {
|
|
3212
|
+
if ((pointsLeft -= points) < 0)
|
|
3213
|
+
pointCounter()(0);
|
|
3214
|
+
}
|
|
3073
3215
|
function parsePath(d) {
|
|
3074
3216
|
const path = [];
|
|
3217
|
+
const count = pointCounter();
|
|
3075
3218
|
const items = [];
|
|
3076
3219
|
let end = 0;
|
|
3077
3220
|
for (const m of d.matchAll(PATH_TOKENS)) {
|
|
@@ -3080,6 +3223,7 @@ function parsePath(d) {
|
|
|
3080
3223
|
end = m.index + m[0].length;
|
|
3081
3224
|
items.push(m[1] ? { command: m[1] } : m[2]);
|
|
3082
3225
|
}
|
|
3226
|
+
chargePoints(2 * items.length);
|
|
3083
3227
|
let i = 0;
|
|
3084
3228
|
let command = null;
|
|
3085
3229
|
let x = 0, y = 0, sx = 0, sy = 0;
|
|
@@ -3195,7 +3339,7 @@ function parsePath(d) {
|
|
|
3195
3339
|
const nx = number() + ox, ny = number() + oy;
|
|
3196
3340
|
if ([rx, ry, rotation, large, sweep, nx, ny].some(isNaN))
|
|
3197
3341
|
return path;
|
|
3198
|
-
arcToCubics(path, x, y, rx, ry, rotation, large, sweep, nx, ny);
|
|
3342
|
+
arcToCubics(path, x, y, rx, ry, rotation, large, sweep, nx, ny, count);
|
|
3199
3343
|
x = nx;
|
|
3200
3344
|
y = ny;
|
|
3201
3345
|
}
|
|
@@ -3205,7 +3349,7 @@ function parsePath(d) {
|
|
|
3205
3349
|
}
|
|
3206
3350
|
return path;
|
|
3207
3351
|
}
|
|
3208
|
-
function arcToCubics(path, x1, y1, rx, ry, rotation, large, sweep, x2, y2) {
|
|
3352
|
+
function arcToCubics(path, x1, y1, rx, ry, rotation, large, sweep, x2, y2, count = () => { }) {
|
|
3209
3353
|
if (x1 === x2 && y1 === y2)
|
|
3210
3354
|
return;
|
|
3211
3355
|
rx = Math.abs(rx);
|
|
@@ -3241,8 +3385,9 @@ function arcToCubics(path, x1, y1, rx, ry, rotation, large, sweep, x2, y2) {
|
|
|
3241
3385
|
delta -= 2 * Math.PI;
|
|
3242
3386
|
else if (sweep && delta < 0)
|
|
3243
3387
|
delta += 2 * Math.PI;
|
|
3244
|
-
const perTurn = Math.max(Math.pow(1.1163 * Math.max(rx, ry) / 0.1, 1 / 6), 3.999999);
|
|
3388
|
+
const perTurn = Math.min(Math.max(Math.pow(1.1163 * Math.max(rx, ry) / 0.1, 1 / 6), 3.999999), 64);
|
|
3245
3389
|
const segments = Math.max(1, Math.ceil(perTurn * Math.abs(delta) / (2 * Math.PI)));
|
|
3390
|
+
count(4 * segments);
|
|
3246
3391
|
const step = delta / segments;
|
|
3247
3392
|
const k = 4 / 3 * Math.tan(step / 4);
|
|
3248
3393
|
const point = (t) => [cx + rx * Math.cos(t) * cos - ry * Math.sin(t) * sin, cy + rx * Math.cos(t) * sin + ry * Math.sin(t) * cos];
|
|
@@ -3257,6 +3402,7 @@ function arcToCubics(path, x1, y1, rx, ry, rotation, large, sweep, x2, y2) {
|
|
|
3257
3402
|
}
|
|
3258
3403
|
}
|
|
3259
3404
|
function flatten(path, tolerance) {
|
|
3405
|
+
const count = glyphPaths.has(path) ? chargePoints : pointCounter();
|
|
3260
3406
|
const subpaths = [];
|
|
3261
3407
|
let current = null;
|
|
3262
3408
|
let x = 0, y = 0, sx = 0, sy = 0;
|
|
@@ -3270,9 +3416,11 @@ function flatten(path, tolerance) {
|
|
|
3270
3416
|
case 'M':
|
|
3271
3417
|
x = sx = command[1];
|
|
3272
3418
|
y = sy = command[2];
|
|
3419
|
+
count(1);
|
|
3273
3420
|
current = start(x, y);
|
|
3274
3421
|
break;
|
|
3275
3422
|
case 'L':
|
|
3423
|
+
count(1);
|
|
3276
3424
|
if (!current)
|
|
3277
3425
|
current = start(x, y);
|
|
3278
3426
|
x = command[1];
|
|
@@ -3286,6 +3434,7 @@ function flatten(path, tolerance) {
|
|
|
3286
3434
|
const ddx = Math.max(Math.abs(x - 2 * c1x + c2x), Math.abs(c1x - 2 * c2x + ex));
|
|
3287
3435
|
const ddy = Math.max(Math.abs(y - 2 * c1y + c2y), Math.abs(c1y - 2 * c2y + ey));
|
|
3288
3436
|
const n = Math.min(512, Math.max(1, Math.ceil(Math.sqrt(0.75 * Math.hypot(ddx, ddy) / tolerance))));
|
|
3437
|
+
count(n);
|
|
3289
3438
|
for (let i = 1; i <= n; i++) {
|
|
3290
3439
|
const t = i / n, mt = 1 - t;
|
|
3291
3440
|
current.points.push(mt * mt * mt * x + 3 * mt * mt * t * c1x + 3 * mt * t * t * c2x + t * t * t * ex, mt * mt * mt * y + 3 * mt * mt * t * c1y + 3 * mt * t * t * c2y + t * t * t * ey);
|