ntk 8.26.2 → 8.28.0
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/lib/widgets/svgview.js +367 -27
- package/package.json +1 -1
package/lib/widgets/svgview.js
CHANGED
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
import { parseDocument } from 'htmlparser2';
|
|
25
25
|
|
|
26
26
|
import { cssColorStraight } from '../color.js';
|
|
27
|
-
import { MAT_IDENTITY, Path2D, flattenPath, matIsIdentity, matMultiply } from '../path.js';
|
|
27
|
+
import { MAT_IDENTITY, Path2D, flattenPath, matInvert, matIsIdentity, matMultiply } from '../path.js';
|
|
28
28
|
|
|
29
29
|
const INHERITED = {
|
|
30
30
|
fill: '#000',
|
|
@@ -642,17 +642,26 @@ function resolveStyle(node, parent) {
|
|
|
642
642
|
}
|
|
643
643
|
|
|
644
644
|
/**
|
|
645
|
-
* A fill or a stroke as `_paint` and the paint scan read it: `currentColor
|
|
646
|
-
* and `
|
|
647
|
-
* and anything else is as written.
|
|
648
|
-
* and as Apple's pages do, reached
|
|
649
|
-
* painted nothing
|
|
645
|
+
* A fill or a stroke as `_paint` and the paint scan read it: `currentColor`,
|
|
646
|
+
* `none`, `context-fill` and `context-stroke` are keywords, which CSS reads
|
|
647
|
+
* in any case (CSS Values 4, 2.5), and anything else is as written.
|
|
648
|
+
* `currentcolor`, as CSS Color 4 writes it and as Apple's pages do, reached
|
|
649
|
+
* the context as a colour nobody knows and painted nothing; `context-fill`
|
|
650
|
+
* reached it as one too, which the context threw on, and a host that drew
|
|
651
|
+
* the document gave the whole of it up.
|
|
650
652
|
*/
|
|
651
653
|
function paintValue(value) {
|
|
652
654
|
const k = value.toLowerCase();
|
|
653
|
-
return k === 'currentcolor' ? 'currentColor' : k === 'none' ?
|
|
655
|
+
return k === 'currentcolor' ? 'currentColor' : k === 'none' || CONTEXT_PAINTS.has(k) ? k : value;
|
|
654
656
|
}
|
|
655
657
|
|
|
658
|
+
/** The paints that are another element's (SVG 2, 13.3): its context
|
|
659
|
+
* element's fill and stroke. */
|
|
660
|
+
const CONTEXT_PAINTS = new Set(['context-fill', 'context-stroke']);
|
|
661
|
+
|
|
662
|
+
/** Which of a context's paints a `context-fill` or a `context-stroke` is. */
|
|
663
|
+
const contextSide = (value) => (value === 'context-fill' ? 'fill' : 'stroke');
|
|
664
|
+
|
|
656
665
|
/**
|
|
657
666
|
* A length in a dash pattern or its offset, in user units: a number, or one
|
|
658
667
|
* in `px` (SVG 2, 13.5.6). A percentage of the viewport's diagonal, or a
|
|
@@ -722,12 +731,31 @@ function dashArrayValue(value) {
|
|
|
722
731
|
function scanPaints(root, ids, languages) {
|
|
723
732
|
const paints = new Set();
|
|
724
733
|
let multi = false;
|
|
734
|
+
// the fill and the stroke of what a marker or a use draws for, as
|
|
735
|
+
// `_contextOf` has them: what a `context-fill` in it paints
|
|
736
|
+
let context = null;
|
|
725
737
|
|
|
726
738
|
const note = (paint) => {
|
|
739
|
+
if (CONTEXT_PAINTS.has(paint)) paint = context?.[contextSide(paint)] ?? 'none';
|
|
727
740
|
if (paint === 'none') return;
|
|
728
741
|
if (/^url\(/i.test(paint)) multi = true;
|
|
729
742
|
else paints.add(paint);
|
|
730
743
|
};
|
|
744
|
+
/** What an element is as a context: its paints, each one that is a
|
|
745
|
+
* context's already the one that context has. */
|
|
746
|
+
const contextOf = (style) => {
|
|
747
|
+
const own = (paint) => (CONTEXT_PAINTS.has(paint) ? (context?.[contextSide(paint)] ?? 'none') : paint);
|
|
748
|
+
return { fill: own(style.fill), stroke: own(style.stroke) };
|
|
749
|
+
};
|
|
750
|
+
const within = (style, walk) => {
|
|
751
|
+
const outer = context;
|
|
752
|
+
context = contextOf(style);
|
|
753
|
+
try {
|
|
754
|
+
walk();
|
|
755
|
+
} finally {
|
|
756
|
+
context = outer;
|
|
757
|
+
}
|
|
758
|
+
};
|
|
731
759
|
|
|
732
760
|
const kids = (node, style, depth) => {
|
|
733
761
|
for (const child of node.children || []) {
|
|
@@ -760,8 +788,10 @@ function scanPaints(root, ids, languages) {
|
|
|
760
788
|
const target = href.startsWith('#') ? ids.get(href.slice(1)) : null;
|
|
761
789
|
if (!target) return;
|
|
762
790
|
// <symbol> is non-rendered on its own but renders through <use>
|
|
763
|
-
|
|
764
|
-
|
|
791
|
+
within(style, () => {
|
|
792
|
+
if (tag(target) === 'symbol') kids(target, style, depth);
|
|
793
|
+
else visit(target, style, depth + 1);
|
|
794
|
+
});
|
|
765
795
|
return;
|
|
766
796
|
}
|
|
767
797
|
case 'text': {
|
|
@@ -790,7 +820,7 @@ function scanPaints(root, ids, languages) {
|
|
|
790
820
|
const marker = ids.get(id);
|
|
791
821
|
if (marker && tag(marker) === 'marker' && !inMarker.has(marker) && !hiddenAbove(marker, root)) {
|
|
792
822
|
inMarker.add(marker);
|
|
793
|
-
kids(marker, styleWhereItStands(marker, root), depth);
|
|
823
|
+
within(style, () => kids(marker, styleWhereItStands(marker, root), depth));
|
|
794
824
|
inMarker.delete(marker);
|
|
795
825
|
}
|
|
796
826
|
}
|
|
@@ -837,6 +867,13 @@ export default class SvgView {
|
|
|
837
867
|
* and whether there is one, which `#Extensibility` holds where */
|
|
838
868
|
this._foreignObject = null;
|
|
839
869
|
this._extensible = false;
|
|
870
|
+
/** What a marker or a use is drawn for, in this draw (`_contextOf`):
|
|
871
|
+
* the paints a `context-fill` and a `context-stroke` in it are, or
|
|
872
|
+
* null outside either. */
|
|
873
|
+
this._context = null;
|
|
874
|
+
/** Where the surface drawn on is in the device the draw started on: a
|
|
875
|
+
* mask's or a clip's surface is the part of it its box covers. */
|
|
876
|
+
this._shift = [0, 0];
|
|
840
877
|
this.naturalWidth = 0;
|
|
841
878
|
this.naturalHeight = 0;
|
|
842
879
|
this.viewBox = null;
|
|
@@ -973,6 +1010,8 @@ export default class SvgView {
|
|
|
973
1010
|
this._surface = opts.surface ?? surfaceMaker(ctx) ?? surfaceMaker(this._ctx);
|
|
974
1011
|
this._bounds = deviceBox(ctx, x, y, w, h);
|
|
975
1012
|
this._masking = new Set();
|
|
1013
|
+
this._context = null;
|
|
1014
|
+
this._shift = [0, 0];
|
|
976
1015
|
ctx.save();
|
|
977
1016
|
ctx.translate(x, y);
|
|
978
1017
|
// the user-space size of the viewport, what a nested <svg>'s
|
|
@@ -1010,14 +1049,85 @@ export default class SvgView {
|
|
|
1010
1049
|
* What a fill or a stroke paints with: a colour, `currentColor`'s, or a
|
|
1011
1050
|
* gradient for `path`. `tint` maps each colour it is made of — a luminance
|
|
1012
1051
|
* mask's content is drawn as the coverage it makes (`_ink`).
|
|
1052
|
+
*
|
|
1053
|
+
* A `context-fill` or a `context-stroke` is the paint of the element a
|
|
1054
|
+
* marker or a use is drawn for (`_contextOf`), and nothing outside one:
|
|
1055
|
+
* a gradient as it is on that element, laid against its bounding box and
|
|
1056
|
+
* in its user space, whatever turns and scales the marker. A
|
|
1057
|
+
* `currentColor` it hands over is this element's colour, which is what
|
|
1058
|
+
* Chrome and Safari paint (paint-context-003), and not that element's.
|
|
1013
1059
|
*/
|
|
1014
1060
|
_paint(ctx, value, style, path, tint = null) {
|
|
1061
|
+
let box = () => pathBBox(path);
|
|
1062
|
+
let space = null;
|
|
1063
|
+
if (CONTEXT_PAINTS.has(value)) {
|
|
1064
|
+
const from = this._context?.[contextSide(value)];
|
|
1065
|
+
if (!from) return null;
|
|
1066
|
+
value = from.value;
|
|
1067
|
+
box = from.box;
|
|
1068
|
+
space = this._spaceOf(ctx, from.matrix);
|
|
1069
|
+
}
|
|
1015
1070
|
if (value === 'currentColor') value = style.color;
|
|
1016
1071
|
const url = /^url\(['"]?#([^'")]+)['"]?\)/.exec(value);
|
|
1017
1072
|
if (!url) return tint ? tint(value) : value;
|
|
1018
1073
|
const node = this._ids.get(url[1]);
|
|
1019
1074
|
if (!node || (tag(node) !== 'lineargradient' && tag(node) !== 'radialgradient')) return null;
|
|
1020
|
-
return this._gradient(ctx, node,
|
|
1075
|
+
return this._gradient(ctx, node, box, tint, space);
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
/**
|
|
1079
|
+
* What an element is to a marker it draws or the use it is (SVG 2,
|
|
1080
|
+
* 13.3): its fill and its stroke as they paint, each with what a
|
|
1081
|
+
* gradient in it is laid against — its bounding box, which `box`
|
|
1082
|
+
* measures where a gradient asks, and its user space, as the device
|
|
1083
|
+
* matrix in force at it. A paint of its own that is a context's is that
|
|
1084
|
+
* context's, so a marker in a use's copy paints what the use does.
|
|
1085
|
+
*/
|
|
1086
|
+
_contextOf(ctx, style, box) {
|
|
1087
|
+
// style's fill and stroke, as computed: a `currentColor` in one stays
|
|
1088
|
+
// the keyword
|
|
1089
|
+
let measured;
|
|
1090
|
+
const once = () => (measured === undefined ? (measured = box() ?? null) : measured);
|
|
1091
|
+
let matrix = null;
|
|
1092
|
+
const paint = (value) => {
|
|
1093
|
+
if (value === 'none') return null;
|
|
1094
|
+
if (CONTEXT_PAINTS.has(value)) return this._context?.[contextSide(value)] ?? null;
|
|
1095
|
+
// the space is a gradient's alone, and asked of the context only for
|
|
1096
|
+
// one: an icon sprite's uses are drawn by the hundred
|
|
1097
|
+
const gradient = /^url\(/i.test(value);
|
|
1098
|
+
if (gradient) matrix ??= this._deviceMatrix(ctx);
|
|
1099
|
+
return { value, box: once, matrix: gradient ? matrix : null };
|
|
1100
|
+
};
|
|
1101
|
+
return { fill: paint(style.fill), stroke: paint(style.stroke) };
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
/** Draw what `walk` draws with `context` as its context element's. */
|
|
1105
|
+
_withContext(context, walk) {
|
|
1106
|
+
const outer = this._context;
|
|
1107
|
+
this._context = context;
|
|
1108
|
+
try {
|
|
1109
|
+
walk();
|
|
1110
|
+
} finally {
|
|
1111
|
+
this._context = outer;
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
/** The matrix that takes the user space in force on `ctx` to the device
|
|
1116
|
+
* the draw started on, or null where the context does not say. */
|
|
1117
|
+
_deviceMatrix(ctx) {
|
|
1118
|
+
const m = ctx.getTransform?.();
|
|
1119
|
+
if (!m) return null;
|
|
1120
|
+
return [m.a, m.b, m.c, m.d, m.e + this._shift[0], m.f + this._shift[1]];
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
/** The matrix that takes the user space `matrix` is of to the one in
|
|
1124
|
+
* force on `ctx`, or null for the same space or one no context says. */
|
|
1125
|
+
_spaceOf(ctx, matrix) {
|
|
1126
|
+
const now = matrix && this._deviceMatrix(ctx);
|
|
1127
|
+
const back = now && matInvert(now);
|
|
1128
|
+
if (!back) return null;
|
|
1129
|
+
const space = matMultiply(back, matrix);
|
|
1130
|
+
return matIsIdentity(space) ? null : space;
|
|
1021
1131
|
}
|
|
1022
1132
|
|
|
1023
1133
|
/**
|
|
@@ -1073,20 +1183,26 @@ export default class SvgView {
|
|
|
1073
1183
|
* every gradient that uses them as one line with an `xlink:href` to it:
|
|
1074
1184
|
* read without it, each had no stops, and what it filled was not drawn.
|
|
1075
1185
|
*/
|
|
1076
|
-
_gradientChain(node) {
|
|
1186
|
+
_gradientChain(node, kind = null) {
|
|
1077
1187
|
const chain = [node];
|
|
1078
1188
|
for (let at = node; chain.length < 32;) {
|
|
1079
1189
|
const href = attr(at, 'href') || attr(at, 'xlink:href') || '';
|
|
1080
1190
|
const next = href.startsWith('#') ? this._ids.get(href.slice(1)) : null;
|
|
1081
1191
|
if (!next || chain.includes(next)) break;
|
|
1082
|
-
if (tag(next) !== 'lineargradient' && tag(next) !== 'radialgradient') break;
|
|
1192
|
+
if (kind ? tag(next) !== kind : tag(next) !== 'lineargradient' && tag(next) !== 'radialgradient') break;
|
|
1083
1193
|
chain.push(next);
|
|
1084
1194
|
at = next;
|
|
1085
1195
|
}
|
|
1086
1196
|
return chain;
|
|
1087
1197
|
}
|
|
1088
1198
|
|
|
1089
|
-
|
|
1199
|
+
/**
|
|
1200
|
+
* A gradient, laid against the bounding box `box` measures where its
|
|
1201
|
+
* units are the box's. `space` takes the user space it is laid out in to
|
|
1202
|
+
* the one it paints in, where they differ: a marker's, for a context's
|
|
1203
|
+
* gradient (`_paint`).
|
|
1204
|
+
*/
|
|
1205
|
+
_gradient(ctx, node, box, tint = null, space = null) {
|
|
1090
1206
|
const chain = this._gradientChain(node);
|
|
1091
1207
|
const own = (name) => {
|
|
1092
1208
|
for (const from of chain) {
|
|
@@ -1096,7 +1212,8 @@ export default class SvgView {
|
|
|
1096
1212
|
return undefined;
|
|
1097
1213
|
};
|
|
1098
1214
|
const units = own('gradientUnits') || 'objectBoundingBox';
|
|
1099
|
-
const bbox = units === 'objectBoundingBox' ?
|
|
1215
|
+
const bbox = units === 'objectBoundingBox' ? box() : null;
|
|
1216
|
+
if (units === 'objectBoundingBox' && !bbox) return null;
|
|
1100
1217
|
const coord = (raw, fallback, axis) => {
|
|
1101
1218
|
let v = raw === undefined ? fallback : parseFloat(raw);
|
|
1102
1219
|
if (String(raw ?? '').endsWith('%')) v /= 100;
|
|
@@ -1106,8 +1223,9 @@ export default class SvgView {
|
|
|
1106
1223
|
};
|
|
1107
1224
|
// `gradientTransform` is set in the gradient's own space, the bounding
|
|
1108
1225
|
// box's where that is its units: here, as the matrix it comes to in
|
|
1109
|
-
// the path's user space
|
|
1110
|
-
const
|
|
1226
|
+
// the path's user space, and from there in the one it paints in
|
|
1227
|
+
const local = gradientMatrix(own('gradientTransform'), bbox);
|
|
1228
|
+
const matrix = space ? (local ? matMultiply(space, local) : space) : local;
|
|
1111
1229
|
// gradient coordinates are user space, like the path's own — the
|
|
1112
1230
|
// context resolves them against the transform in force when it paints,
|
|
1113
1231
|
// which is this one (issue #271)
|
|
@@ -1162,6 +1280,193 @@ export default class SvgView {
|
|
|
1162
1280
|
return gradient;
|
|
1163
1281
|
}
|
|
1164
1282
|
|
|
1283
|
+
/** The `<pattern>` a paint names by its `url()`, or null where it names
|
|
1284
|
+
* none. */
|
|
1285
|
+
_patternOf(value) {
|
|
1286
|
+
const url = /^url\(['"]?#([^'")]+)['"]?\)/.exec(value || '');
|
|
1287
|
+
const node = url ? this._ids.get(url[1]) : null;
|
|
1288
|
+
return node && tag(node) === 'pattern' ? node : null;
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
/**
|
|
1292
|
+
* Fill a path with a pattern (SVG 2, 14.3): a tile `width` by `height` at
|
|
1293
|
+
* `x`,`y` of its `patternUnits`, repeated across the plane and moved by
|
|
1294
|
+
* its `patternTransform`, each a copy of what the pattern holds — set
|
|
1295
|
+
* from the tile's corner, in `patternContentUnits`, or fitted in by its
|
|
1296
|
+
* `viewBox` — cut to the tile. What a pattern does not set itself it
|
|
1297
|
+
* takes from the patterns its `href` names, its content from the first
|
|
1298
|
+
* of them that has some, as a gradient does (`_gradientChain`).
|
|
1299
|
+
*
|
|
1300
|
+
* The tile is drawn once on a surface, at about the size the context
|
|
1301
|
+
* draws it, then a row of tiles from it and a block of rows, and the
|
|
1302
|
+
* block is drawn across the path: a grid of dots a window across is a
|
|
1303
|
+
* few dozen draws, where a tile at a time is thousands. With no surface
|
|
1304
|
+
* to draw on, or inside a luminance mask, each tile is drawn where it
|
|
1305
|
+
* goes. A path that would take more than `MAX_PATTERN_TILES` of either
|
|
1306
|
+
* is tiled as far as the box the drawing is drawn into, and not at all
|
|
1307
|
+
* where that is too many as well.
|
|
1308
|
+
*/
|
|
1309
|
+
_fillPattern(ctx, node, path, rule, alpha, depth) {
|
|
1310
|
+
if ((this._patterning ??= new Set()).has(node)) return;
|
|
1311
|
+
const chain = this._gradientChain(node, 'pattern');
|
|
1312
|
+
const own = (name) => {
|
|
1313
|
+
for (const from of chain) {
|
|
1314
|
+
const value = attr(from, name);
|
|
1315
|
+
if (value !== undefined) return value;
|
|
1316
|
+
}
|
|
1317
|
+
return undefined;
|
|
1318
|
+
};
|
|
1319
|
+
const content = chain.find((from) => (from.children || []).some((child) => child.type === 'tag'));
|
|
1320
|
+
if (!content) return;
|
|
1321
|
+
const byBox = (own('patternUnits') || '').trim() !== 'userSpaceOnUse';
|
|
1322
|
+
const contentByBox = (own('patternContentUnits') || '').trim() === 'objectBoundingBox';
|
|
1323
|
+
const bbox = byBox || contentByBox ? pathBBox(path) : null;
|
|
1324
|
+
if (bbox && !(bbox.w > 0 && bbox.h > 0)) return;
|
|
1325
|
+
// in the user space, a percentage of the viewport; by the bounding
|
|
1326
|
+
// box, a fraction of it from its corner
|
|
1327
|
+
const [pw, ph] = this._viewport;
|
|
1328
|
+
const length = (name, size, origin) => {
|
|
1329
|
+
const raw = String(own(name) ?? '').trim();
|
|
1330
|
+
const n = parseFloat(raw);
|
|
1331
|
+
if (!Number.isFinite(n)) return origin;
|
|
1332
|
+
const part = raw.endsWith('%') ? n / 100 : n;
|
|
1333
|
+
return byBox ? origin + part * size : raw.endsWith('%') ? part * size : n;
|
|
1334
|
+
};
|
|
1335
|
+
const x = length('x', byBox ? bbox.w : pw, byBox ? bbox.x : 0);
|
|
1336
|
+
const y = length('y', byBox ? bbox.h : ph, byBox ? bbox.y : 0);
|
|
1337
|
+
const w = length('width', byBox ? bbox.w : pw, 0);
|
|
1338
|
+
const h = length('height', byBox ? bbox.h : ph, 0);
|
|
1339
|
+
if (!(w > 0 && h > 0)) return;
|
|
1340
|
+
// pattern space to the path's user space, and what the tile's content
|
|
1341
|
+
// is set in, from the tile's corner
|
|
1342
|
+
const placed = matrixOf(parseSvgTransform(own('patternTransform')));
|
|
1343
|
+
const vb = chain.map(parseViewBox).find(Boolean);
|
|
1344
|
+
let inner = MAT_IDENTITY;
|
|
1345
|
+
if (vb && vb[2] > 0 && vb[3] > 0) {
|
|
1346
|
+
const [sx, sy, tx, ty] = viewBoxFit(vb, 0, 0, w, h, own('preserveAspectRatio'));
|
|
1347
|
+
inner = [sx, 0, 0, sy, tx, ty];
|
|
1348
|
+
} else if (contentByBox) {
|
|
1349
|
+
inner = [bbox.w, 0, 0, bbox.h, 0, 0];
|
|
1350
|
+
}
|
|
1351
|
+
const style = this._inheritedStyle(content);
|
|
1352
|
+
const drawTile = (c) => {
|
|
1353
|
+
this._patterning.add(node);
|
|
1354
|
+
const viewport = this._viewport;
|
|
1355
|
+
try {
|
|
1356
|
+
if (vb) this._viewport = [vb[2], vb[3]];
|
|
1357
|
+
c.transform(...inner);
|
|
1358
|
+
this._renderChildren(content, c, style, 1, depth + 1);
|
|
1359
|
+
} finally {
|
|
1360
|
+
this._viewport = viewport;
|
|
1361
|
+
this._patterning.delete(node);
|
|
1362
|
+
}
|
|
1363
|
+
};
|
|
1364
|
+
|
|
1365
|
+
// the tiles that cover the path, in pattern space
|
|
1366
|
+
const m = ctx.getTransform?.();
|
|
1367
|
+
const device = m ? [m.a, m.b, m.c, m.d, m.e, m.f] : null;
|
|
1368
|
+
const along = device ? matMultiply(device, placed) : placed;
|
|
1369
|
+
const toPattern = matInvert(along);
|
|
1370
|
+
if (!toPattern) return;
|
|
1371
|
+
const span = (reach) => {
|
|
1372
|
+
const i0 = Math.floor((reach.x - x) / w);
|
|
1373
|
+
const j0 = Math.floor((reach.y - y) / h);
|
|
1374
|
+
const i1 = Math.ceil((reach.x + reach.w - x) / w);
|
|
1375
|
+
const j1 = Math.ceil((reach.y + reach.h - y) / h);
|
|
1376
|
+
return reach.w > 0 && reach.h > 0 && i1 > i0 && j1 > j0 ? { i0, j0, i1, j1 } : null;
|
|
1377
|
+
};
|
|
1378
|
+
const reach = matrixBox(asMatrix(matInvert(placed) ?? MAT_IDENTITY), pathBBox(path));
|
|
1379
|
+
let tiles = span(reach);
|
|
1380
|
+
if (!tiles) return;
|
|
1381
|
+
// the tile at about the size it is drawn at: a whole number of pixels a
|
|
1382
|
+
// side, so that tiles side by side on a surface meet exactly
|
|
1383
|
+
const tw = Math.max(1, Math.min(MAX_TILE, Math.round(w * Math.hypot(along[0], along[1]))));
|
|
1384
|
+
const th = Math.max(1, Math.min(MAX_TILE, Math.round(h * Math.hypot(along[2], along[3]))));
|
|
1385
|
+
const make = !this._luminance && !this._silhouetting && ctx.drawImage ? this._surface : null;
|
|
1386
|
+
const nx = Math.max(1, Math.floor(BLOCK / tw));
|
|
1387
|
+
const ny = Math.max(1, Math.floor(BLOCK / th));
|
|
1388
|
+
const count = ({ i0, j0, i1, j1 }) =>
|
|
1389
|
+
make ? Math.ceil((i1 - i0) / nx) * Math.ceil((j1 - j0) / ny) : (i1 - i0) * (j1 - j0);
|
|
1390
|
+
if (count(tiles) > MAX_PATTERN_TILES && device && this._bounds) {
|
|
1391
|
+
// a shape too large to tile all of: as far as the box the drawing is
|
|
1392
|
+
// drawn into, which is what of it a caller mostly shows
|
|
1393
|
+
const seen = matrixBox(asMatrix(toPattern), this._bounds);
|
|
1394
|
+
const x0 = Math.max(reach.x, seen.x);
|
|
1395
|
+
const y0 = Math.max(reach.y, seen.y);
|
|
1396
|
+
tiles = span({
|
|
1397
|
+
x: x0,
|
|
1398
|
+
y: y0,
|
|
1399
|
+
w: Math.min(reach.x + reach.w, seen.x + seen.w) - x0,
|
|
1400
|
+
h: Math.min(reach.y + reach.h, seen.y + seen.h) - y0
|
|
1401
|
+
});
|
|
1402
|
+
}
|
|
1403
|
+
if (!tiles || count(tiles) > MAX_PATTERN_TILES) return;
|
|
1404
|
+
const { i0, j0, i1, j1 } = tiles;
|
|
1405
|
+
|
|
1406
|
+
ctx.save();
|
|
1407
|
+
try {
|
|
1408
|
+
ctx.clip(path, rule);
|
|
1409
|
+
ctx.transform(...placed);
|
|
1410
|
+
ctx.globalAlpha = alpha;
|
|
1411
|
+
const block = { x, y, w, h, tw, th, nx: Math.min(i1 - i0, nx), ny: Math.min(j1 - j0, ny), i0, j0, i1, j1 };
|
|
1412
|
+
if (make && this._fillBlocks(ctx, make, drawTile, block)) return;
|
|
1413
|
+
if ((i1 - i0) * (j1 - j0) > MAX_PATTERN_TILES) return;
|
|
1414
|
+
for (let j = j0; j < j1; j++) {
|
|
1415
|
+
for (let i = i0; i < i1; i++) {
|
|
1416
|
+
ctx.save();
|
|
1417
|
+
ctx.translate(x + i * w, y + j * h);
|
|
1418
|
+
const cut = new Path2D();
|
|
1419
|
+
cut.rect(0, 0, w, h);
|
|
1420
|
+
ctx.clip(cut);
|
|
1421
|
+
drawTile(ctx);
|
|
1422
|
+
ctx.restore();
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
} finally {
|
|
1426
|
+
ctx.restore();
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1430
|
+
/** The tiles of a pattern drawn from a surface (`_fillPattern`): one
|
|
1431
|
+
* tile, a row of `nx` of them, a block of `ny` rows, and the block
|
|
1432
|
+
* across the tiles `i0`…`i1` by `j0`…`j1`, in pattern space. False where
|
|
1433
|
+
* a surface could not be made, and nothing is drawn. */
|
|
1434
|
+
_fillBlocks(ctx, make, drawTile, { x, y, w, h, tw, th, nx, ny, i0, j0, i1, j1 }) {
|
|
1435
|
+
let tile = null;
|
|
1436
|
+
let row = null;
|
|
1437
|
+
let block = null;
|
|
1438
|
+
const contexts = [];
|
|
1439
|
+
const context = (surface) => {
|
|
1440
|
+
const c = surface.getContext('2d');
|
|
1441
|
+
contexts.push(c);
|
|
1442
|
+
return c;
|
|
1443
|
+
};
|
|
1444
|
+
try {
|
|
1445
|
+
tile = make(tw, th);
|
|
1446
|
+
row = tile && make(tw * nx, th);
|
|
1447
|
+
block = row && make(tw * nx, th * ny);
|
|
1448
|
+
if (!block) return false;
|
|
1449
|
+
const tctx = context(tile);
|
|
1450
|
+
tctx.scale(tw / w, th / h);
|
|
1451
|
+
drawTile(tctx);
|
|
1452
|
+
const rctx = context(row);
|
|
1453
|
+
for (let i = 0; i < nx; i++) rctx.drawImage(tile, i * tw, 0);
|
|
1454
|
+
const bctx = context(block);
|
|
1455
|
+
for (let j = 0; j < ny; j++) bctx.drawImage(row, 0, j * th);
|
|
1456
|
+
for (let j = j0; j < j1; j += ny) {
|
|
1457
|
+
for (let i = i0; i < i1; i += nx) {
|
|
1458
|
+
ctx.drawImage(block, x + i * w, y + j * h, nx * w, ny * h);
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1461
|
+
return true;
|
|
1462
|
+
} finally {
|
|
1463
|
+
for (const c of contexts) c?.destroy?.();
|
|
1464
|
+
block?.destroy?.();
|
|
1465
|
+
row?.destroy?.();
|
|
1466
|
+
tile?.destroy?.();
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1165
1470
|
_renderChildren(node, ctx, style, alpha, depth) {
|
|
1166
1471
|
for (const child of node.children || []) {
|
|
1167
1472
|
if (child.type !== 'tag') continue;
|
|
@@ -1267,11 +1572,17 @@ export default class SvgView {
|
|
|
1267
1572
|
break;
|
|
1268
1573
|
}
|
|
1269
1574
|
case 'use': {
|
|
1270
|
-
// its `x` and `y` are in its transforms (`ownTransforms`)
|
|
1575
|
+
// its `x` and `y` are in its transforms (`ownTransforms`), and it is
|
|
1576
|
+
// what what it draws is drawn for (`_contextOf`)
|
|
1271
1577
|
const target = this._useTarget(node);
|
|
1272
1578
|
if (target) {
|
|
1273
|
-
|
|
1274
|
-
|
|
1579
|
+
this._withContext(
|
|
1580
|
+
this._contextOf(ctx, style, () => this._bbox(node, ctx, style, depth)),
|
|
1581
|
+
() => {
|
|
1582
|
+
if (tag(target) === 'symbol') this._renderChildren(target, ctx, style, nodeAlpha, depth + 1);
|
|
1583
|
+
else this._renderNode(target, ctx, style, nodeAlpha, depth + 1);
|
|
1584
|
+
}
|
|
1585
|
+
);
|
|
1275
1586
|
}
|
|
1276
1587
|
break;
|
|
1277
1588
|
}
|
|
@@ -1292,7 +1603,9 @@ export default class SvgView {
|
|
|
1292
1603
|
if (!path) break;
|
|
1293
1604
|
if (name !== 'line' && style.fill !== 'none') {
|
|
1294
1605
|
const rule = style.fillRule === 'evenodd' ? 'evenodd' : 'nonzero';
|
|
1295
|
-
|
|
1606
|
+
const pattern = this._patternOf(style.fill);
|
|
1607
|
+
if (pattern) this._fillPattern(ctx, pattern, path, rule, nodeAlpha * style.fillOpacity, depth);
|
|
1608
|
+
else this._ink(ctx, 'fill', style.fill, style, path, nodeAlpha * style.fillOpacity, () => ctx.fill(path, rule));
|
|
1296
1609
|
}
|
|
1297
1610
|
if (style.stroke !== 'none' && style.strokeWidth > 0) {
|
|
1298
1611
|
ctx.lineWidth = style.strokeWidth;
|
|
@@ -1358,10 +1671,14 @@ export default class SvgView {
|
|
|
1358
1671
|
const vertices = pathVertices(path);
|
|
1359
1672
|
if (!vertices.length) return;
|
|
1360
1673
|
const last = vertices.length - 1;
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1674
|
+
// the shape is what each of them is drawn for (`_contextOf`)
|
|
1675
|
+
this._withContext(
|
|
1676
|
+
this._contextOf(ctx, style, () => pathBBox(path)),
|
|
1677
|
+
() => vertices.forEach((v, i) => {
|
|
1678
|
+
const id = i === 0 ? style.markerStart : i === last ? style.markerEnd : style.markerMid;
|
|
1679
|
+
if (id !== 'none') this._renderMarker(id, v, i === 0, ctx, style, alpha, depth);
|
|
1680
|
+
})
|
|
1681
|
+
);
|
|
1365
1682
|
}
|
|
1366
1683
|
|
|
1367
1684
|
_renderMarker(id, vertex, first, ctx, style, alpha, depth) {
|
|
@@ -1461,6 +1778,7 @@ export default class SvgView {
|
|
|
1461
1778
|
let done = false;
|
|
1462
1779
|
const bounds = this._bounds;
|
|
1463
1780
|
const luminance = this._luminance;
|
|
1781
|
+
const shift = this._shift;
|
|
1464
1782
|
try {
|
|
1465
1783
|
if (box) {
|
|
1466
1784
|
content = make(box.w, box.h);
|
|
@@ -1472,8 +1790,10 @@ export default class SvgView {
|
|
|
1472
1790
|
if (cctx.globalCompositeOperation === 'destination-in') {
|
|
1473
1791
|
cctx.globalCompositeOperation = 'source-over';
|
|
1474
1792
|
const place = [m.a, m.b, m.c, m.d, m.e - box.x, m.f - box.y];
|
|
1475
|
-
// what the surfaces show is all of them
|
|
1793
|
+
// what the surfaces show is all of them, and they are the part of
|
|
1794
|
+
// the device the box covers
|
|
1476
1795
|
this._bounds = { x: 0, y: 0, w: box.w, h: box.h };
|
|
1796
|
+
this._shift = [shift[0] + box.x, shift[1] + box.y];
|
|
1477
1797
|
this._luminance = false;
|
|
1478
1798
|
cctx.transform(...place);
|
|
1479
1799
|
this._renderElement(node, cctx, style, 1, depth);
|
|
@@ -1507,6 +1827,7 @@ export default class SvgView {
|
|
|
1507
1827
|
} finally {
|
|
1508
1828
|
this._bounds = bounds;
|
|
1509
1829
|
this._luminance = luminance;
|
|
1830
|
+
this._shift = shift;
|
|
1510
1831
|
mctx?.destroy?.();
|
|
1511
1832
|
cctx?.destroy?.();
|
|
1512
1833
|
cover?.destroy?.();
|
|
@@ -1725,6 +2046,7 @@ export default class SvgView {
|
|
|
1725
2046
|
let done = false;
|
|
1726
2047
|
const bounds = this._bounds;
|
|
1727
2048
|
const luminance = this._luminance;
|
|
2049
|
+
const shift = this._shift;
|
|
1728
2050
|
try {
|
|
1729
2051
|
if (box) {
|
|
1730
2052
|
content = make(box.w, box.h);
|
|
@@ -1736,8 +2058,10 @@ export default class SvgView {
|
|
|
1736
2058
|
if (cctx.globalCompositeOperation === 'destination-in') {
|
|
1737
2059
|
cctx.globalCompositeOperation = 'source-over';
|
|
1738
2060
|
const place = [m.a, m.b, m.c, m.d, m.e - box.x, m.f - box.y];
|
|
1739
|
-
// what the surfaces show is all of them
|
|
2061
|
+
// what the surfaces show is all of them, and they are the part of
|
|
2062
|
+
// the device the box covers
|
|
1740
2063
|
this._bounds = { x: 0, y: 0, w: box.w, h: box.h };
|
|
2064
|
+
this._shift = [shift[0] + box.x, shift[1] + box.y];
|
|
1741
2065
|
cctx.transform(...place);
|
|
1742
2066
|
paint(cctx, 1);
|
|
1743
2067
|
|
|
@@ -1761,6 +2085,7 @@ export default class SvgView {
|
|
|
1761
2085
|
} finally {
|
|
1762
2086
|
this._bounds = bounds;
|
|
1763
2087
|
this._luminance = luminance;
|
|
2088
|
+
this._shift = shift;
|
|
1764
2089
|
mctx?.destroy?.();
|
|
1765
2090
|
cctx?.destroy?.();
|
|
1766
2091
|
cover?.destroy?.();
|
|
@@ -2442,6 +2767,21 @@ const EMPTY = Symbol('empty');
|
|
|
2442
2767
|
/** The largest surface a mask is drawn on: 4096 by 4096. */
|
|
2443
2768
|
const MAX_SURFACE = 4096 * 4096;
|
|
2444
2769
|
|
|
2770
|
+
/** The most tiles of a pattern drawn one at a time, or blocks of them
|
|
2771
|
+
* (`_fillPattern`): past it, the pattern is not drawn. */
|
|
2772
|
+
const MAX_PATTERN_TILES = 4096;
|
|
2773
|
+
|
|
2774
|
+
/** About how many pixels a side a block of a pattern's tiles is. */
|
|
2775
|
+
const BLOCK = 256;
|
|
2776
|
+
|
|
2777
|
+
/** The most pixels a side a pattern's tile is drawn at. */
|
|
2778
|
+
const MAX_TILE = 2048;
|
|
2779
|
+
|
|
2780
|
+
/** A matrix as an array, as the `{ a, b, … }` `matrixBox` takes. */
|
|
2781
|
+
function asMatrix([a, b, c, d, e, f]) {
|
|
2782
|
+
return { a, b, c, d, e, f };
|
|
2783
|
+
}
|
|
2784
|
+
|
|
2445
2785
|
/**
|
|
2446
2786
|
* The device-pixel rectangle a mask's region lands in, under `m` and
|
|
2447
2787
|
* inside what the drawing can show, whole pixels out: `EMPTY` where none of
|