ntk 8.27.0 → 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 +159 -23
- package/package.json +1 -1
package/lib/widgets/svgview.js
CHANGED
|
@@ -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
|
/**
|
|
@@ -1086,7 +1196,13 @@ export default class SvgView {
|
|
|
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)
|
|
@@ -1454,11 +1572,17 @@ export default class SvgView {
|
|
|
1454
1572
|
break;
|
|
1455
1573
|
}
|
|
1456
1574
|
case 'use': {
|
|
1457
|
-
// 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`)
|
|
1458
1577
|
const target = this._useTarget(node);
|
|
1459
1578
|
if (target) {
|
|
1460
|
-
|
|
1461
|
-
|
|
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
|
+
);
|
|
1462
1586
|
}
|
|
1463
1587
|
break;
|
|
1464
1588
|
}
|
|
@@ -1547,10 +1671,14 @@ export default class SvgView {
|
|
|
1547
1671
|
const vertices = pathVertices(path);
|
|
1548
1672
|
if (!vertices.length) return;
|
|
1549
1673
|
const last = vertices.length - 1;
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
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
|
+
);
|
|
1554
1682
|
}
|
|
1555
1683
|
|
|
1556
1684
|
_renderMarker(id, vertex, first, ctx, style, alpha, depth) {
|
|
@@ -1650,6 +1778,7 @@ export default class SvgView {
|
|
|
1650
1778
|
let done = false;
|
|
1651
1779
|
const bounds = this._bounds;
|
|
1652
1780
|
const luminance = this._luminance;
|
|
1781
|
+
const shift = this._shift;
|
|
1653
1782
|
try {
|
|
1654
1783
|
if (box) {
|
|
1655
1784
|
content = make(box.w, box.h);
|
|
@@ -1661,8 +1790,10 @@ export default class SvgView {
|
|
|
1661
1790
|
if (cctx.globalCompositeOperation === 'destination-in') {
|
|
1662
1791
|
cctx.globalCompositeOperation = 'source-over';
|
|
1663
1792
|
const place = [m.a, m.b, m.c, m.d, m.e - box.x, m.f - box.y];
|
|
1664
|
-
// 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
|
|
1665
1795
|
this._bounds = { x: 0, y: 0, w: box.w, h: box.h };
|
|
1796
|
+
this._shift = [shift[0] + box.x, shift[1] + box.y];
|
|
1666
1797
|
this._luminance = false;
|
|
1667
1798
|
cctx.transform(...place);
|
|
1668
1799
|
this._renderElement(node, cctx, style, 1, depth);
|
|
@@ -1696,6 +1827,7 @@ export default class SvgView {
|
|
|
1696
1827
|
} finally {
|
|
1697
1828
|
this._bounds = bounds;
|
|
1698
1829
|
this._luminance = luminance;
|
|
1830
|
+
this._shift = shift;
|
|
1699
1831
|
mctx?.destroy?.();
|
|
1700
1832
|
cctx?.destroy?.();
|
|
1701
1833
|
cover?.destroy?.();
|
|
@@ -1914,6 +2046,7 @@ export default class SvgView {
|
|
|
1914
2046
|
let done = false;
|
|
1915
2047
|
const bounds = this._bounds;
|
|
1916
2048
|
const luminance = this._luminance;
|
|
2049
|
+
const shift = this._shift;
|
|
1917
2050
|
try {
|
|
1918
2051
|
if (box) {
|
|
1919
2052
|
content = make(box.w, box.h);
|
|
@@ -1925,8 +2058,10 @@ export default class SvgView {
|
|
|
1925
2058
|
if (cctx.globalCompositeOperation === 'destination-in') {
|
|
1926
2059
|
cctx.globalCompositeOperation = 'source-over';
|
|
1927
2060
|
const place = [m.a, m.b, m.c, m.d, m.e - box.x, m.f - box.y];
|
|
1928
|
-
// 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
|
|
1929
2063
|
this._bounds = { x: 0, y: 0, w: box.w, h: box.h };
|
|
2064
|
+
this._shift = [shift[0] + box.x, shift[1] + box.y];
|
|
1930
2065
|
cctx.transform(...place);
|
|
1931
2066
|
paint(cctx, 1);
|
|
1932
2067
|
|
|
@@ -1950,6 +2085,7 @@ export default class SvgView {
|
|
|
1950
2085
|
} finally {
|
|
1951
2086
|
this._bounds = bounds;
|
|
1952
2087
|
this._luminance = luminance;
|
|
2088
|
+
this._shift = shift;
|
|
1953
2089
|
mctx?.destroy?.();
|
|
1954
2090
|
cctx?.destroy?.();
|
|
1955
2091
|
cover?.destroy?.();
|