@johnmorrisdotca/kyogijo-stadiums 0.0.0-stage → 0.4.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/CHANGELOG.md +129 -0
- package/LICENSE +21 -0
- package/NOTICE.md +293 -0
- package/README.md +600 -2
- package/dist/annotate.d.ts +47 -0
- package/dist/annotate.js +349 -0
- package/dist/atlas-entry.d.ts +4 -0
- package/dist/atlas-entry.js +3 -0
- package/dist/atlas.d.ts +131 -0
- package/dist/atlas.js +149 -0
- package/dist/background.d.ts +56 -0
- package/dist/background.js +127 -0
- package/dist/ballpark.d.ts +48 -0
- package/dist/ballpark.js +129 -0
- package/dist/canvas-entry.d.ts +2 -0
- package/dist/canvas-entry.js +2 -0
- package/dist/canvas.d.ts +85 -0
- package/dist/canvas.js +279 -0
- package/dist/charts-entry.d.ts +10 -0
- package/dist/charts-entry.js +8 -0
- package/dist/charts.baseball.d.ts +30 -0
- package/dist/charts.baseball.js +152 -0
- package/dist/charts.curling.d.ts +52 -0
- package/dist/charts.curling.js +196 -0
- package/dist/charts.d.ts +172 -0
- package/dist/charts.js +508 -0
- package/dist/charts.network.d.ts +66 -0
- package/dist/charts.network.js +64 -0
- package/dist/charts.soccer.d.ts +43 -0
- package/dist/charts.soccer.js +191 -0
- package/dist/charts.types.d.ts +396 -0
- package/dist/charts.types.js +1 -0
- package/dist/colour.d.ts +29 -0
- package/dist/colour.js +97 -0
- package/dist/common.d.ts +14 -0
- package/dist/common.js +32 -0
- package/dist/court.d.ts +89 -0
- package/dist/court.js +248 -0
- package/dist/courts.style.d.ts +20 -0
- package/dist/courts.style.js +54 -0
- package/dist/cricket.board.d.ts +18 -0
- package/dist/cricket.board.js +67 -0
- package/dist/curling.board.d.ts +24 -0
- package/dist/curling.board.js +147 -0
- package/dist/d3-entry.d.ts +3 -0
- package/dist/d3-entry.js +2 -0
- package/dist/d3.d.ts +45 -0
- package/dist/d3.js +56 -0
- package/dist/draw.d.ts +52 -0
- package/dist/draw.js +705 -0
- package/dist/element-define.d.ts +2 -0
- package/dist/element-define.js +4 -0
- package/dist/element-entry.d.ts +1 -0
- package/dist/element-entry.js +2 -0
- package/dist/element.d.ts +39 -0
- package/dist/element.js +321 -0
- package/dist/eras-entry.d.ts +2 -0
- package/dist/eras-entry.js +2 -0
- package/dist/eras.d.ts +204 -0
- package/dist/eras.js +396 -0
- package/dist/events-entry.d.ts +3 -0
- package/dist/events-entry.js +3 -0
- package/dist/events.d.ts +54 -0
- package/dist/events.data.d.ts +59 -0
- package/dist/events.data.js +297 -0
- package/dist/events.js +127 -0
- package/dist/features.d.ts +31 -0
- package/dist/features.js +284 -0
- package/dist/field.score.d.ts +5 -0
- package/dist/field.score.js +33 -0
- package/dist/fields/afl.d.ts +11 -0
- package/dist/fields/afl.js +103 -0
- package/dist/fields/cricket.d.ts +9 -0
- package/dist/fields/cricket.js +81 -0
- package/dist/fields/fieldhockey.d.ts +6 -0
- package/dist/fields/fieldhockey.js +81 -0
- package/dist/fields/gaelic.d.ts +10 -0
- package/dist/fields/gaelic.js +86 -0
- package/dist/fields/handball.d.ts +6 -0
- package/dist/fields/handball.js +85 -0
- package/dist/fields/lacrosse.d.ts +17 -0
- package/dist/fields/lacrosse.js +169 -0
- package/dist/fields/rugby.d.ts +10 -0
- package/dist/fields/rugby.js +130 -0
- package/dist/fields.d.ts +12 -0
- package/dist/fields.js +44 -0
- package/dist/fields.kit.d.ts +63 -0
- package/dist/fields.kit.js +108 -0
- package/dist/fields.style.d.ts +4 -0
- package/dist/fields.style.js +19 -0
- package/dist/fields.types.d.ts +155 -0
- package/dist/fields.types.js +1 -0
- package/dist/format.d.ts +10 -0
- package/dist/format.js +28 -0
- package/dist/formations.d.ts +18 -0
- package/dist/formations.js +211 -0
- package/dist/frames-entry.d.ts +8 -0
- package/dist/frames-entry.js +7 -0
- package/dist/frames.d.ts +199 -0
- package/dist/frames.js +271 -0
- package/dist/frames.providers.d.ts +16 -0
- package/dist/frames.providers.js +302 -0
- package/dist/geojson-entry.d.ts +2 -0
- package/dist/geojson-entry.js +2 -0
- package/dist/geojson.d.ts +79 -0
- package/dist/geojson.js +147 -0
- package/dist/geometry-entry.d.ts +7 -0
- package/dist/geometry-entry.js +5 -0
- package/dist/geometry.d.ts +295 -0
- package/dist/geometry.js +361 -0
- package/dist/grounds-entry.d.ts +4 -0
- package/dist/grounds-entry.js +3 -0
- package/dist/grounds.d.ts +134 -0
- package/dist/grounds.data.d.ts +2 -0
- package/dist/grounds.data.js +1849 -0
- package/dist/grounds.js +74 -0
- package/dist/heat.colour.d.ts +37 -0
- package/dist/heat.colour.js +54 -0
- package/dist/heat.d.ts +43 -0
- package/dist/heat.js +614 -0
- package/dist/heat.stats.d.ts +123 -0
- package/dist/heat.stats.js +413 -0
- package/dist/heat.types.d.ts +145 -0
- package/dist/heat.types.js +1 -0
- package/dist/heat.zones.d.ts +54 -0
- package/dist/heat.zones.js +424 -0
- package/dist/homeside.d.ts +16 -0
- package/dist/homeside.js +43 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +29 -0
- package/dist/layers-entry.d.ts +19 -0
- package/dist/layers-entry.js +11 -0
- package/dist/layers.d.ts +41 -0
- package/dist/layers.env.d.ts +72 -0
- package/dist/layers.env.js +79 -0
- package/dist/layers.js +533 -0
- package/dist/layers.marks.d.ts +45 -0
- package/dist/layers.marks.js +168 -0
- package/dist/layers.types.d.ts +226 -0
- package/dist/layers.types.js +1 -0
- package/dist/layout.d.ts +97 -0
- package/dist/layout.js +381 -0
- package/dist/layout.style.d.ts +6 -0
- package/dist/layout.style.js +17 -0
- package/dist/layouts-entry.d.ts +5 -0
- package/dist/layouts-entry.js +4 -0
- package/dist/leagues/aussie.d.ts +57 -0
- package/dist/leagues/aussie.js +36 -0
- package/dist/leagues/baseball.d.ts +58 -0
- package/dist/leagues/baseball.js +171 -0
- package/dist/leagues/basketball.d.ts +67 -0
- package/dist/leagues/basketball.js +128 -0
- package/dist/leagues/courts.d.ts +212 -0
- package/dist/leagues/courts.js +114 -0
- package/dist/leagues/cricket.d.ts +45 -0
- package/dist/leagues/cricket.js +34 -0
- package/dist/leagues/curling.d.ts +64 -0
- package/dist/leagues/curling.js +53 -0
- package/dist/leagues/fieldhockey.d.ts +48 -0
- package/dist/leagues/fieldhockey.js +29 -0
- package/dist/leagues/fields.d.ts +10 -0
- package/dist/leagues/fields.js +6 -0
- package/dist/leagues/football.d.ts +8 -0
- package/dist/leagues/football.js +82 -0
- package/dist/leagues/gaelic.d.ts +50 -0
- package/dist/leagues/gaelic.js +29 -0
- package/dist/leagues/handball.d.ts +45 -0
- package/dist/leagues/handball.js +32 -0
- package/dist/leagues/hockey.d.ts +63 -0
- package/dist/leagues/hockey.js +166 -0
- package/dist/leagues/lacrosse.d.ts +103 -0
- package/dist/leagues/lacrosse.js +175 -0
- package/dist/leagues/rugby.d.ts +59 -0
- package/dist/leagues/rugby.js +72 -0
- package/dist/leagues/sources.d.ts +18 -0
- package/dist/leagues/sources.js +95 -0
- package/dist/leagues/types.d.ts +43 -0
- package/dist/leagues/types.js +5 -0
- package/dist/leagues-entry.d.ts +23 -0
- package/dist/leagues-entry.js +15 -0
- package/dist/leagues.d.ts +172 -0
- package/dist/leagues.js +462 -0
- package/dist/look.d.ts +50 -0
- package/dist/look.js +186 -0
- package/dist/mark.d.ts +35 -0
- package/dist/mark.js +59 -0
- package/dist/mount-entry.d.ts +2 -0
- package/dist/mount-entry.js +2 -0
- package/dist/mount.d.ts +63 -0
- package/dist/mount.js +167 -0
- package/dist/mow.d.ts +36 -0
- package/dist/mow.js +111 -0
- package/dist/names.d.ts +77 -0
- package/dist/names.js +97 -0
- package/dist/outline.d.ts +25 -0
- package/dist/outline.js +112 -0
- package/dist/parks-entry.d.ts +4 -0
- package/dist/parks-entry.js +3 -0
- package/dist/parks.d.ts +200 -0
- package/dist/parks.data.d.ts +2 -0
- package/dist/parks.data.js +4489 -0
- package/dist/parks.js +92 -0
- package/dist/plot-entry.d.ts +2 -0
- package/dist/plot-entry.js +2 -0
- package/dist/plot.d.ts +74 -0
- package/dist/plot.js +68 -0
- package/dist/png-entry.d.ts +2 -0
- package/dist/png-entry.js +2 -0
- package/dist/png.d.ts +26 -0
- package/dist/png.js +46 -0
- package/dist/ranges.d.ts +33 -0
- package/dist/ranges.js +178 -0
- package/dist/ranges.names.d.ts +31 -0
- package/dist/ranges.names.js +65 -0
- package/dist/react-entry.d.ts +2 -0
- package/dist/react-entry.js +2 -0
- package/dist/react.d.ts +53 -0
- package/dist/react.js +58 -0
- package/dist/replay-entry.d.ts +5 -0
- package/dist/replay-entry.js +4 -0
- package/dist/replay.control.d.ts +47 -0
- package/dist/replay.control.js +184 -0
- package/dist/replay.d.ts +170 -0
- package/dist/replay.dom.d.ts +21 -0
- package/dist/replay.dom.js +388 -0
- package/dist/replay.js +579 -0
- package/dist/replay.types.d.ts +152 -0
- package/dist/replay.types.js +1 -0
- package/dist/restyle.d.ts +21 -0
- package/dist/restyle.js +70 -0
- package/dist/road.d.ts +26 -0
- package/dist/road.draw.d.ts +6 -0
- package/dist/road.draw.js +269 -0
- package/dist/road.js +59 -0
- package/dist/road.model.d.ts +63 -0
- package/dist/road.model.js +215 -0
- package/dist/road.style.d.ts +7 -0
- package/dist/road.style.js +26 -0
- package/dist/road.types.d.ts +122 -0
- package/dist/road.types.js +1 -0
- package/dist/scene-entry.d.ts +7 -0
- package/dist/scene-entry.js +5 -0
- package/dist/scene.css.d.ts +44 -0
- package/dist/scene.css.js +296 -0
- package/dist/scene.d.ts +27 -0
- package/dist/scene.fit.d.ts +12 -0
- package/dist/scene.fit.js +18 -0
- package/dist/scene.flat.d.ts +38 -0
- package/dist/scene.flat.js +167 -0
- package/dist/scene.js +420 -0
- package/dist/scene.path.d.ts +40 -0
- package/dist/scene.path.js +357 -0
- package/dist/scene.types.d.ts +172 -0
- package/dist/scene.types.js +1 -0
- package/dist/scene.xml.d.ts +31 -0
- package/dist/scene.xml.js +81 -0
- package/dist/score.d.ts +70 -0
- package/dist/score.js +212 -0
- package/dist/sets.board.d.ts +22 -0
- package/dist/sets.board.js +89 -0
- package/dist/sheet.d.ts +18 -0
- package/dist/sheet.js +38 -0
- package/dist/special.d.ts +44 -0
- package/dist/special.js +204 -0
- package/dist/special.style.d.ts +8 -0
- package/dist/special.style.js +36 -0
- package/dist/strings.d.ts +581 -0
- package/dist/strings.js +411 -0
- package/dist/style.d.ts +35 -0
- package/dist/style.js +177 -0
- package/dist/surfaces/baseball.d.ts +33 -0
- package/dist/surfaces/baseball.js +208 -0
- package/dist/surfaces/baseball.walls.d.ts +16 -0
- package/dist/surfaces/baseball.walls.js +150 -0
- package/dist/surfaces/basketball.d.ts +8 -0
- package/dist/surfaces/basketball.js +199 -0
- package/dist/surfaces/curling.d.ts +23 -0
- package/dist/surfaces/curling.js +128 -0
- package/dist/surfaces/field.d.ts +12 -0
- package/dist/surfaces/field.js +163 -0
- package/dist/surfaces/football.d.ts +12 -0
- package/dist/surfaces/football.js +208 -0
- package/dist/surfaces/hockey.d.ts +10 -0
- package/dist/surfaces/hockey.js +166 -0
- package/dist/surfaces/net.d.ts +7 -0
- package/dist/surfaces/net.js +151 -0
- package/dist/surfaces/soccer-providers.d.ts +124 -0
- package/dist/surfaces/soccer-providers.js +334 -0
- package/dist/surfaces/soccer-spec.d.ts +138 -0
- package/dist/surfaces/soccer-spec.js +175 -0
- package/dist/surfaces/soccer.d.ts +25 -0
- package/dist/surfaces/soccer.js +270 -0
- package/dist/surfaces/surface.d.ts +180 -0
- package/dist/surfaces/surface.js +49 -0
- package/dist/team.common.d.ts +75 -0
- package/dist/team.common.js +97 -0
- package/dist/team.formation.d.ts +24 -0
- package/dist/team.formation.js +121 -0
- package/dist/team.geometry.d.ts +66 -0
- package/dist/team.geometry.js +304 -0
- package/dist/team.layers.d.ts +16 -0
- package/dist/team.layers.js +32 -0
- package/dist/team.motion.d.ts +9 -0
- package/dist/team.motion.js +325 -0
- package/dist/team.network.d.ts +43 -0
- package/dist/team.network.js +215 -0
- package/dist/team.shape.d.ts +12 -0
- package/dist/team.shape.js +217 -0
- package/dist/team.types.d.ts +189 -0
- package/dist/team.types.js +1 -0
- package/dist/text.d.ts +31 -0
- package/dist/text.js +88 -0
- package/dist/themes.d.ts +14 -0
- package/dist/themes.js +24 -0
- package/dist/types.d.ts +565 -0
- package/dist/types.js +1 -0
- package/dist/units.d.ts +45 -0
- package/dist/units.js +90 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/package.json +234 -4
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
export const IDENTITY = [1, 0, 0, 1, 0, 0];
|
|
2
|
+
/** The matrix that does `b` first and then `a`'s frame: the transform of a child inside a parent. */
|
|
3
|
+
export const multiply = (a, b) => [
|
|
4
|
+
a[0] * b[0] + a[2] * b[1],
|
|
5
|
+
a[1] * b[0] + a[3] * b[1],
|
|
6
|
+
a[0] * b[2] + a[2] * b[3],
|
|
7
|
+
a[1] * b[2] + a[3] * b[3],
|
|
8
|
+
a[0] * b[4] + a[2] * b[5] + a[4],
|
|
9
|
+
a[1] * b[4] + a[3] * b[5] + a[5],
|
|
10
|
+
];
|
|
11
|
+
/** The matrix that undoes `m`, or the identity when `m` flattens the plane. */
|
|
12
|
+
export function invert(m) {
|
|
13
|
+
const det = m[0] * m[3] - m[1] * m[2];
|
|
14
|
+
if (det === 0)
|
|
15
|
+
return IDENTITY;
|
|
16
|
+
return [m[3] / det, -m[1] / det, -m[2] / det, m[0] / det, (m[2] * m[5] - m[3] * m[4]) / det, (m[1] * m[4] - m[0] * m[5]) / det];
|
|
17
|
+
}
|
|
18
|
+
export const isIdentity = (m) => m[0] === 1 && m[1] === 0 && m[2] === 0 && m[3] === 1 && m[4] === 0 && m[5] === 0;
|
|
19
|
+
/** A point moved by a matrix. */
|
|
20
|
+
export const apply = (m, x, y) => [m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5]];
|
|
21
|
+
/** A `transform` attribute (`matrix`, `translate`, `scale`, `rotate`, in any run) as one matrix. */
|
|
22
|
+
export function parseTransform(text) {
|
|
23
|
+
if (text === undefined || text.trim() === "")
|
|
24
|
+
return IDENTITY;
|
|
25
|
+
let m = IDENTITY;
|
|
26
|
+
for (const part of text.matchAll(/(matrix|translate|scale|rotate|skewX|skewY)\s*\(([^)]*)\)/g)) {
|
|
27
|
+
const n = part[2].split(/[\s,]+/).filter((v) => v !== "").map(Number);
|
|
28
|
+
let t;
|
|
29
|
+
switch (part[1]) {
|
|
30
|
+
case "matrix":
|
|
31
|
+
t = [n[0], n[1], n[2], n[3], n[4], n[5]];
|
|
32
|
+
break;
|
|
33
|
+
case "translate":
|
|
34
|
+
t = [1, 0, 0, 1, n[0], n[1] ?? 0];
|
|
35
|
+
break;
|
|
36
|
+
case "scale":
|
|
37
|
+
t = [n[0], 0, 0, n[1] ?? n[0], 0, 0];
|
|
38
|
+
break;
|
|
39
|
+
case "rotate": {
|
|
40
|
+
const a = (n[0] * Math.PI) / 180;
|
|
41
|
+
const c = Math.cos(a);
|
|
42
|
+
const s = Math.sin(a);
|
|
43
|
+
const r = [c, s, -s, c, 0, 0];
|
|
44
|
+
t = n.length >= 3 ? multiply(multiply([1, 0, 0, 1, n[1], n[2]], r), [1, 0, 0, 1, -n[1], -n[2]]) : r;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
case "skewX":
|
|
48
|
+
t = [1, 0, Math.tan((n[0] * Math.PI) / 180), 1, 0, 0];
|
|
49
|
+
break;
|
|
50
|
+
default:
|
|
51
|
+
t = [1, Math.tan((n[0] * Math.PI) / 180), 0, 1, 0, 0];
|
|
52
|
+
}
|
|
53
|
+
if (t.some((v) => !Number.isFinite(v)))
|
|
54
|
+
throw new Error(`Cannot read the transform "${text}"`);
|
|
55
|
+
m = multiply(m, t);
|
|
56
|
+
}
|
|
57
|
+
return m;
|
|
58
|
+
}
|
|
59
|
+
/** An SVG elliptical arc as cubic curves (the endpoint parametrisation of the SVG specification, appendix F.6). */
|
|
60
|
+
function arcToCurves(x0, y0, rx, ry, rotation, large, sweep, x, y) {
|
|
61
|
+
if ((x0 === x && y0 === y) || rx === 0 || ry === 0)
|
|
62
|
+
return [["L", x, y]];
|
|
63
|
+
rx = Math.abs(rx);
|
|
64
|
+
ry = Math.abs(ry);
|
|
65
|
+
const phi = (rotation * Math.PI) / 180;
|
|
66
|
+
const cos = Math.cos(phi);
|
|
67
|
+
const sin = Math.sin(phi);
|
|
68
|
+
const dx = (x0 - x) / 2;
|
|
69
|
+
const dy = (y0 - y) / 2;
|
|
70
|
+
const x1 = cos * dx + sin * dy;
|
|
71
|
+
const y1 = -sin * dx + cos * dy;
|
|
72
|
+
const lambda = (x1 * x1) / (rx * rx) + (y1 * y1) / (ry * ry);
|
|
73
|
+
if (lambda > 1) {
|
|
74
|
+
const s = Math.sqrt(lambda);
|
|
75
|
+
rx *= s;
|
|
76
|
+
ry *= s;
|
|
77
|
+
}
|
|
78
|
+
const num = rx * rx * ry * ry - rx * rx * y1 * y1 - ry * ry * x1 * x1;
|
|
79
|
+
const den = rx * rx * y1 * y1 + ry * ry * x1 * x1;
|
|
80
|
+
const k = (large === sweep ? -1 : 1) * Math.sqrt(Math.max(0, num / den));
|
|
81
|
+
const cx1 = (k * rx * y1) / ry;
|
|
82
|
+
const cy1 = (-k * ry * x1) / rx;
|
|
83
|
+
const cx = cos * cx1 - sin * cy1 + (x0 + x) / 2;
|
|
84
|
+
const cy = sin * cx1 + cos * cy1 + (y0 + y) / 2;
|
|
85
|
+
const angle = (ux, uy, vx, vy) => {
|
|
86
|
+
const a = Math.atan2(ux * vy - uy * vx, ux * vx + uy * vy);
|
|
87
|
+
return a;
|
|
88
|
+
};
|
|
89
|
+
const start = angle(1, 0, (x1 - cx1) / rx, (y1 - cy1) / ry);
|
|
90
|
+
let sweepBy = angle((x1 - cx1) / rx, (y1 - cy1) / ry, (-x1 - cx1) / rx, (-y1 - cy1) / ry);
|
|
91
|
+
if (!sweep && sweepBy > 0)
|
|
92
|
+
sweepBy -= 2 * Math.PI;
|
|
93
|
+
else if (sweep && sweepBy < 0)
|
|
94
|
+
sweepBy += 2 * Math.PI;
|
|
95
|
+
const pieces = Math.max(1, Math.ceil(Math.abs(sweepBy) / (Math.PI / 2) - 1e-9));
|
|
96
|
+
const step = sweepBy / pieces;
|
|
97
|
+
const t = (4 / 3) * Math.tan(step / 4);
|
|
98
|
+
const out = [];
|
|
99
|
+
const at = (a) => {
|
|
100
|
+
const ca = Math.cos(a);
|
|
101
|
+
const sa = Math.sin(a);
|
|
102
|
+
return [ca, sa, -sa, ca];
|
|
103
|
+
};
|
|
104
|
+
for (let i = 0; i < pieces; i += 1) {
|
|
105
|
+
const a1 = start + i * step;
|
|
106
|
+
const a2 = a1 + step;
|
|
107
|
+
const [c1, s1] = at(a1);
|
|
108
|
+
const [c2, s2] = at(a2);
|
|
109
|
+
const p = (ux, uy) => [cos * rx * ux - sin * ry * uy + cx, sin * rx * ux + cos * ry * uy + cy];
|
|
110
|
+
const [q1x, q1y] = p(c1 - t * s1, s1 + t * c1);
|
|
111
|
+
const [q2x, q2y] = p(c2 + t * s2, s2 - t * c2);
|
|
112
|
+
const [ex, ey] = i === pieces - 1 ? [x, y] : p(c2, s2);
|
|
113
|
+
out.push(["C", q1x, q1y, q2x, q2y, ex, ey]);
|
|
114
|
+
}
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
const ARGS = { M: 2, L: 2, H: 1, V: 1, C: 6, S: 4, Q: 4, T: 2, A: 7, Z: 0 };
|
|
118
|
+
/**
|
|
119
|
+
* Path data as absolute segments: `M`, `L`, `C`, `Q` and `Z`. Relative commands, `H`, `V`, `S` and `T` are worked out, and an arc is cubic curves.
|
|
120
|
+
* @example
|
|
121
|
+
* ```ts
|
|
122
|
+
* import { pathCommands } from "@johnmorrisdotca/kyogijo-stadiums/scene";
|
|
123
|
+
* console.log(pathCommands("M0 0h10v5H0Z").map((c) => c.join(" ")));
|
|
124
|
+
* // [ 'M 0 0', 'L 10 0', 'L 10 5', 'L 0 5', 'Z' ]
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
export function pathCommands(d) {
|
|
128
|
+
const out = [];
|
|
129
|
+
const tokens = d.match(/[MmLlHhVvCcSsQqTtAaZz]|[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?|[01](?=[01]\b)/g) ?? [];
|
|
130
|
+
let i = 0;
|
|
131
|
+
let x = 0;
|
|
132
|
+
let y = 0;
|
|
133
|
+
let sx = 0;
|
|
134
|
+
let sy = 0;
|
|
135
|
+
let cmd = "";
|
|
136
|
+
let lastCubic;
|
|
137
|
+
let lastQuad;
|
|
138
|
+
// The two flags of an arc may be written without a space (`a1 1 0 011 1`): flags are read one digit at a time.
|
|
139
|
+
const flag = () => {
|
|
140
|
+
const t = tokens[i];
|
|
141
|
+
if (t === "0" || t === "1") {
|
|
142
|
+
i += 1;
|
|
143
|
+
return Number(t);
|
|
144
|
+
}
|
|
145
|
+
const first = t[0];
|
|
146
|
+
tokens[i] = t.slice(1);
|
|
147
|
+
return Number(first);
|
|
148
|
+
};
|
|
149
|
+
const next = () => {
|
|
150
|
+
const v = Number(tokens[i]);
|
|
151
|
+
if (!Number.isFinite(v))
|
|
152
|
+
throw new Error(`Cannot read the path "${d.slice(0, 60)}"`);
|
|
153
|
+
i += 1;
|
|
154
|
+
return v;
|
|
155
|
+
};
|
|
156
|
+
while (i < tokens.length) {
|
|
157
|
+
if (/^[A-Za-z]$/.test(tokens[i])) {
|
|
158
|
+
cmd = tokens[i];
|
|
159
|
+
i += 1;
|
|
160
|
+
if (cmd === "Z" || cmd === "z") {
|
|
161
|
+
out.push(["Z"]);
|
|
162
|
+
x = sx;
|
|
163
|
+
y = sy;
|
|
164
|
+
lastCubic = lastQuad = undefined;
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
else if (cmd === "" || cmd === "Z" || cmd === "z")
|
|
169
|
+
throw new Error(`Cannot read the path "${d.slice(0, 60)}"`);
|
|
170
|
+
const up = cmd.toUpperCase();
|
|
171
|
+
const rel = cmd !== up;
|
|
172
|
+
if (ARGS[up] === undefined)
|
|
173
|
+
throw new Error(`Unknown path command "${cmd}"`);
|
|
174
|
+
const ox = rel ? x : 0;
|
|
175
|
+
const oy = rel ? y : 0;
|
|
176
|
+
let nextCubic;
|
|
177
|
+
let nextQuad;
|
|
178
|
+
switch (up) {
|
|
179
|
+
case "M": {
|
|
180
|
+
x = ox + next();
|
|
181
|
+
y = oy + next();
|
|
182
|
+
sx = x;
|
|
183
|
+
sy = y;
|
|
184
|
+
out.push(["M", x, y]);
|
|
185
|
+
// More pairs after a move are lines.
|
|
186
|
+
cmd = rel ? "l" : "L";
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
case "L":
|
|
190
|
+
x = ox + next();
|
|
191
|
+
y = oy + next();
|
|
192
|
+
out.push(["L", x, y]);
|
|
193
|
+
break;
|
|
194
|
+
case "H":
|
|
195
|
+
x = (rel ? x : 0) + next();
|
|
196
|
+
out.push(["L", x, y]);
|
|
197
|
+
break;
|
|
198
|
+
case "V":
|
|
199
|
+
y = (rel ? y : 0) + next();
|
|
200
|
+
out.push(["L", x, y]);
|
|
201
|
+
break;
|
|
202
|
+
case "C": {
|
|
203
|
+
const c = ["C", ox + next(), oy + next(), ox + next(), oy + next(), ox + next(), oy + next()];
|
|
204
|
+
out.push(c);
|
|
205
|
+
nextCubic = [c[3], c[4]];
|
|
206
|
+
x = c[5];
|
|
207
|
+
y = c[6];
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
case "S": {
|
|
211
|
+
const [c2x, c2y, ex, ey] = [ox + next(), oy + next(), ox + next(), oy + next()];
|
|
212
|
+
const c1 = lastCubic === undefined ? [x, y] : [2 * x - lastCubic[0], 2 * y - lastCubic[1]];
|
|
213
|
+
out.push(["C", c1[0], c1[1], c2x, c2y, ex, ey]);
|
|
214
|
+
nextCubic = [c2x, c2y];
|
|
215
|
+
x = ex;
|
|
216
|
+
y = ey;
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
case "Q": {
|
|
220
|
+
const c = ["Q", ox + next(), oy + next(), ox + next(), oy + next()];
|
|
221
|
+
out.push(c);
|
|
222
|
+
nextQuad = [c[1], c[2]];
|
|
223
|
+
x = c[3];
|
|
224
|
+
y = c[4];
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
case "T": {
|
|
228
|
+
const [ex, ey] = [ox + next(), oy + next()];
|
|
229
|
+
const c = lastQuad === undefined ? [x, y] : [2 * x - lastQuad[0], 2 * y - lastQuad[1]];
|
|
230
|
+
out.push(["Q", c[0], c[1], ex, ey]);
|
|
231
|
+
nextQuad = c;
|
|
232
|
+
x = ex;
|
|
233
|
+
y = ey;
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
default: {
|
|
237
|
+
const rx = next();
|
|
238
|
+
const ry = next();
|
|
239
|
+
const rotation = next();
|
|
240
|
+
const large = flag() === 1;
|
|
241
|
+
const sweep = flag() === 1;
|
|
242
|
+
const ex = ox + next();
|
|
243
|
+
const ey = oy + next();
|
|
244
|
+
out.push(...arcToCurves(x, y, rx, ry, rotation, large, sweep, ex, ey));
|
|
245
|
+
x = ex;
|
|
246
|
+
y = ey;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
lastCubic = nextCubic;
|
|
250
|
+
lastQuad = nextQuad;
|
|
251
|
+
}
|
|
252
|
+
return out;
|
|
253
|
+
}
|
|
254
|
+
/** The segments of a shape in its own frame: a rectangle, circle, ellipse, line, polygon, polyline or path as path commands. */
|
|
255
|
+
export function shapeCommands(shape) {
|
|
256
|
+
switch (shape.kind) {
|
|
257
|
+
case "path":
|
|
258
|
+
return [...shape.commands];
|
|
259
|
+
case "rect": {
|
|
260
|
+
const { x, y, width: w, height: h } = shape;
|
|
261
|
+
const r = Math.min(shape.rx, w / 2, h / 2);
|
|
262
|
+
if (r <= 0)
|
|
263
|
+
return [["M", x, y], ["L", x + w, y], ["L", x + w, y + h], ["L", x, y + h], ["Z"]];
|
|
264
|
+
return [["M", x + r, y], ["L", x + w - r, y], ...arcToCurves(x + w - r, y, r, r, 0, false, true, x + w, y + r), ["L", x + w, y + h - r], ...arcToCurves(x + w, y + h - r, r, r, 0, false, true, x + w - r, y + h), ["L", x + r, y + h], ...arcToCurves(x + r, y + h, r, r, 0, false, true, x, y + h - r), ["L", x, y + r], ...arcToCurves(x, y + r, r, r, 0, false, true, x + r, y), ["Z"]];
|
|
265
|
+
}
|
|
266
|
+
case "circle":
|
|
267
|
+
return ellipseCommands(shape.cx, shape.cy, shape.r, shape.r);
|
|
268
|
+
case "ellipse":
|
|
269
|
+
return ellipseCommands(shape.cx, shape.cy, shape.rx, shape.ry);
|
|
270
|
+
case "line":
|
|
271
|
+
return [["M", shape.x1, shape.y1], ["L", shape.x2, shape.y2]];
|
|
272
|
+
default: {
|
|
273
|
+
const [first, ...rest] = shape.points;
|
|
274
|
+
if (first === undefined)
|
|
275
|
+
return [];
|
|
276
|
+
return [["M", first[0], first[1]], ...rest.map((p) => ["L", p[0], p[1]]), ...(shape.kind === "polygon" ? [["Z"]] : [])];
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
function ellipseCommands(cx, cy, rx, ry) {
|
|
281
|
+
return [["M", cx + rx, cy], ...arcToCurves(cx + rx, cy, rx, ry, 0, false, true, cx, cy + ry), ...arcToCurves(cx, cy + ry, rx, ry, 0, false, true, cx - rx, cy), ...arcToCurves(cx - rx, cy, rx, ry, 0, false, true, cx, cy - ry), ...arcToCurves(cx, cy - ry, rx, ry, 0, false, true, cx + rx, cy), ["Z"]];
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Segments as runs of points, curves cut into straight pieces no further than `tolerance` (in the same units) from the curve. `matrix` moves every
|
|
285
|
+
* point first.
|
|
286
|
+
*/
|
|
287
|
+
export function flatten(commands, matrix = IDENTITY, tolerance = 0.05) {
|
|
288
|
+
const rings = [];
|
|
289
|
+
let ring;
|
|
290
|
+
let x = 0;
|
|
291
|
+
let y = 0;
|
|
292
|
+
let startX = 0;
|
|
293
|
+
let startY = 0;
|
|
294
|
+
const add = (px, py) => {
|
|
295
|
+
ring.points.push(apply(matrix, px, py));
|
|
296
|
+
x = px;
|
|
297
|
+
y = py;
|
|
298
|
+
};
|
|
299
|
+
const begin = (px, py) => {
|
|
300
|
+
ring = { points: [], closed: false };
|
|
301
|
+
rings.push(ring);
|
|
302
|
+
startX = px;
|
|
303
|
+
startY = py;
|
|
304
|
+
add(px, py);
|
|
305
|
+
};
|
|
306
|
+
const steps = (length) => Math.max(2, Math.min(64, Math.ceil(Math.sqrt(length / tolerance) / 2) * 2));
|
|
307
|
+
for (const c of commands) {
|
|
308
|
+
if (c[0] === "M") {
|
|
309
|
+
begin(c[1], c[2]);
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
if (c[0] === "Z") {
|
|
313
|
+
if (ring !== undefined)
|
|
314
|
+
ring.closed = true;
|
|
315
|
+
x = startX;
|
|
316
|
+
y = startY;
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
// A segment after a close starts a new run at the point the close came back to.
|
|
320
|
+
if (ring === undefined || ring.closed)
|
|
321
|
+
begin(x, y);
|
|
322
|
+
if (c[0] === "L")
|
|
323
|
+
add(c[1], c[2]);
|
|
324
|
+
else if (c[0] === "C") {
|
|
325
|
+
const [, x1, y1, x2, y2, ex, ey] = c;
|
|
326
|
+
const n = steps(Math.hypot(x1 - x, y1 - y) + Math.hypot(x2 - x1, y2 - y1) + Math.hypot(ex - x2, ey - y2));
|
|
327
|
+
const [x0, y0] = [x, y];
|
|
328
|
+
for (let k = 1; k <= n; k += 1) {
|
|
329
|
+
const t = k / n;
|
|
330
|
+
const u = 1 - t;
|
|
331
|
+
add(u * u * u * x0 + 3 * u * u * t * x1 + 3 * u * t * t * x2 + t * t * t * ex, u * u * u * y0 + 3 * u * u * t * y1 + 3 * u * t * t * y2 + t * t * t * ey);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
const [, x1, y1, ex, ey] = c;
|
|
336
|
+
const n = steps(Math.hypot(x1 - x, y1 - y) + Math.hypot(ex - x1, ey - y1));
|
|
337
|
+
const [x0, y0] = [x, y];
|
|
338
|
+
for (let k = 1; k <= n; k += 1) {
|
|
339
|
+
const t = k / n;
|
|
340
|
+
const u = 1 - t;
|
|
341
|
+
add(u * u * x0 + 2 * u * t * x1 + t * t * ex, u * u * y0 + 2 * u * t * y1 + t * t * ey);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
return rings;
|
|
346
|
+
}
|
|
347
|
+
/** The box round a shape's outline (not its stroke) after `matrix`, or undefined for a shape with no points. */
|
|
348
|
+
export function boundsOf(shape, matrix = IDENTITY) {
|
|
349
|
+
const points = flatten(shapeCommands(shape), matrix, 0.5).flatMap((ring) => ring.points);
|
|
350
|
+
if (points.length === 0)
|
|
351
|
+
return undefined;
|
|
352
|
+
const xs = points.map((p) => p[0]);
|
|
353
|
+
const ys = points.map((p) => p[1]);
|
|
354
|
+
const x = Math.min(...xs);
|
|
355
|
+
const y = Math.min(...ys);
|
|
356
|
+
return { x, y, w: Math.max(...xs) - x, h: Math.max(...ys) - y };
|
|
357
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import type { ScoreOptions, SurfaceOptions } from "./types.ts";
|
|
2
|
+
/** An affine transform as the six numbers of an SVG `matrix(a b c d e f)`: x' = a x + c y + e, y' = b x + d y + f. */
|
|
3
|
+
export type Matrix = readonly [number, number, number, number, number, number];
|
|
4
|
+
/** A path segment in absolute coordinates. An arc has been turned into cubic curves, so a renderer needs only these five. */
|
|
5
|
+
export type PathCommand = readonly ["M", number, number] | readonly ["L", number, number] | readonly ["C", number, number, number, number, number, number] | readonly ["Q", number, number, number, number] | readonly ["Z"];
|
|
6
|
+
/** A colour in CSS (`#3f8f55`, `rgba(255,255,255,.92)`, a name), `none`, or a reference to a gradient or a pattern of the scene. */
|
|
7
|
+
export type Paint = string | {
|
|
8
|
+
readonly gradient: string;
|
|
9
|
+
} | {
|
|
10
|
+
readonly pattern: string;
|
|
11
|
+
};
|
|
12
|
+
/** What a shape is painted with. Every field is a number, a colour or a keyword: nothing is left to a stylesheet. */
|
|
13
|
+
export interface SceneStyle {
|
|
14
|
+
fill: Paint;
|
|
15
|
+
stroke: Paint;
|
|
16
|
+
strokeWidth: number;
|
|
17
|
+
/** The shape's own opacity multiplied by those of the groups it is in (so overlapping parts of one group are not blended as a group would be). */
|
|
18
|
+
opacity: number;
|
|
19
|
+
fillOpacity: number;
|
|
20
|
+
strokeOpacity: number;
|
|
21
|
+
lineJoin: "miter" | "round" | "bevel";
|
|
22
|
+
lineCap: "butt" | "round" | "square";
|
|
23
|
+
/** The dash lengths, or none for a solid line. */
|
|
24
|
+
dash?: readonly number[];
|
|
25
|
+
fillRule: "nonzero" | "evenodd";
|
|
26
|
+
/** `stroke` when the outline is painted under the fill (a halo round a label). */
|
|
27
|
+
paintOrder?: "stroke";
|
|
28
|
+
/** A CSS filter such as `grayscale(.35) brightness(.92)`, when the stylesheet gives one. */
|
|
29
|
+
filter?: string;
|
|
30
|
+
}
|
|
31
|
+
/** What every shape has. */
|
|
32
|
+
export interface SceneBase {
|
|
33
|
+
/** The element's class, as written. */
|
|
34
|
+
class: string;
|
|
35
|
+
/** The element's id, when it has one. */
|
|
36
|
+
id?: string;
|
|
37
|
+
/** The `data-*` attributes of the element and the groups it is in, without the `data-` (the nearest wins). */
|
|
38
|
+
data?: Readonly<Record<string, string>>;
|
|
39
|
+
/** The element's `<title>`, a tooltip. */
|
|
40
|
+
label?: string;
|
|
41
|
+
/** The drawing's frame to the shape's: everything above it, and its own `transform`, multiplied. */
|
|
42
|
+
matrix: Matrix;
|
|
43
|
+
/** The clips the shape is cut by, outermost first: indexes into `Scene.clips`. */
|
|
44
|
+
clips: readonly number[];
|
|
45
|
+
/** True for what is drawn on the surface itself (its markings, the teams' paint, the data), false for what lies beside it (the compact strip). Only such a shape is in the surface's frame, once `Scene.turn` is undone. */
|
|
46
|
+
surface?: true;
|
|
47
|
+
style: SceneStyle;
|
|
48
|
+
}
|
|
49
|
+
/** A rectangle, with rounded corners when `rx` is more than 0. */
|
|
50
|
+
export interface SceneRect extends SceneBase {
|
|
51
|
+
kind: "rect";
|
|
52
|
+
x: number;
|
|
53
|
+
y: number;
|
|
54
|
+
width: number;
|
|
55
|
+
height: number;
|
|
56
|
+
rx: number;
|
|
57
|
+
}
|
|
58
|
+
export interface SceneCircle extends SceneBase {
|
|
59
|
+
kind: "circle";
|
|
60
|
+
cx: number;
|
|
61
|
+
cy: number;
|
|
62
|
+
r: number;
|
|
63
|
+
}
|
|
64
|
+
export interface SceneEllipse extends SceneBase {
|
|
65
|
+
kind: "ellipse";
|
|
66
|
+
cx: number;
|
|
67
|
+
cy: number;
|
|
68
|
+
rx: number;
|
|
69
|
+
ry: number;
|
|
70
|
+
}
|
|
71
|
+
export interface SceneLine extends SceneBase {
|
|
72
|
+
kind: "line";
|
|
73
|
+
x1: number;
|
|
74
|
+
y1: number;
|
|
75
|
+
x2: number;
|
|
76
|
+
y2: number;
|
|
77
|
+
}
|
|
78
|
+
/** A closed (`polygon`) or open (`polyline`) run of points. */
|
|
79
|
+
export interface ScenePoly extends SceneBase {
|
|
80
|
+
kind: "polygon" | "polyline";
|
|
81
|
+
points: readonly (readonly [number, number])[];
|
|
82
|
+
}
|
|
83
|
+
/** A path: `d` as written, and the same as absolute segments (lines, curves, arcs as curves). */
|
|
84
|
+
export interface ScenePath extends SceneBase {
|
|
85
|
+
kind: "path";
|
|
86
|
+
d: string;
|
|
87
|
+
commands: readonly PathCommand[];
|
|
88
|
+
}
|
|
89
|
+
export interface SceneText extends SceneBase {
|
|
90
|
+
kind: "text";
|
|
91
|
+
x: number;
|
|
92
|
+
y: number;
|
|
93
|
+
text: string;
|
|
94
|
+
fontSize: number;
|
|
95
|
+
fontWeight: number;
|
|
96
|
+
fontFamily: string;
|
|
97
|
+
anchor: "start" | "middle" | "end";
|
|
98
|
+
letterSpacing: number;
|
|
99
|
+
}
|
|
100
|
+
export interface SceneImage extends SceneBase {
|
|
101
|
+
kind: "image";
|
|
102
|
+
href: string;
|
|
103
|
+
x: number;
|
|
104
|
+
y: number;
|
|
105
|
+
width: number;
|
|
106
|
+
height: number;
|
|
107
|
+
preserveAspectRatio?: string;
|
|
108
|
+
}
|
|
109
|
+
/** A shape that is outlined and filled: everything but text and images. */
|
|
110
|
+
export type SceneShape = SceneRect | SceneCircle | SceneEllipse | SceneLine | ScenePoly | ScenePath;
|
|
111
|
+
export type SceneItem = SceneShape | SceneText | SceneImage;
|
|
112
|
+
/** A region shapes are cut to: the shapes of a `<clipPath>`, in the frame of the group that was clipped. */
|
|
113
|
+
export interface SceneClip {
|
|
114
|
+
id: string;
|
|
115
|
+
/** The frame of the group the clip was set on, to the drawing's. */
|
|
116
|
+
matrix: Matrix;
|
|
117
|
+
shapes: readonly SceneShape[];
|
|
118
|
+
}
|
|
119
|
+
export interface SceneGradient {
|
|
120
|
+
id: string;
|
|
121
|
+
/** In the frame of the shape painted with it. */
|
|
122
|
+
x1: number;
|
|
123
|
+
y1: number;
|
|
124
|
+
x2: number;
|
|
125
|
+
y2: number;
|
|
126
|
+
stops: readonly {
|
|
127
|
+
offset: number;
|
|
128
|
+
color: string;
|
|
129
|
+
opacity: number;
|
|
130
|
+
}[];
|
|
131
|
+
}
|
|
132
|
+
/** A tile of shapes, repeated over `width` by `height` in the frame of the shape painted with it. */
|
|
133
|
+
export interface ScenePattern {
|
|
134
|
+
id: string;
|
|
135
|
+
width: number;
|
|
136
|
+
height: number;
|
|
137
|
+
shapes: readonly SceneShape[];
|
|
138
|
+
}
|
|
139
|
+
/** A drawing as data. */
|
|
140
|
+
export interface Scene {
|
|
141
|
+
/** The theme the colours were worked out for. */
|
|
142
|
+
theme: "light" | "dark";
|
|
143
|
+
/** The sport the drawing is of, when it says. */
|
|
144
|
+
sport?: string;
|
|
145
|
+
/** The drawing's id: the prefix of the ids a renderer writes, so two scenes on one page do not share one. */
|
|
146
|
+
id: string;
|
|
147
|
+
title: string;
|
|
148
|
+
description: string;
|
|
149
|
+
/** `portrait` when the surface lies tall: `turn` is then the quarter turn that laid it down, in the frame of `viewBox`. */
|
|
150
|
+
orientation: "landscape" | "portrait";
|
|
151
|
+
turn?: Matrix;
|
|
152
|
+
/** The frame of the drawing (feet; x along the surface from its left end, y across it from the top; a portrait drawing is already turned). */
|
|
153
|
+
viewBox: {
|
|
154
|
+
x: number;
|
|
155
|
+
y: number;
|
|
156
|
+
w: number;
|
|
157
|
+
h: number;
|
|
158
|
+
};
|
|
159
|
+
items: readonly SceneItem[];
|
|
160
|
+
clips: readonly SceneClip[];
|
|
161
|
+
gradients: readonly SceneGradient[];
|
|
162
|
+
patterns: readonly ScenePattern[];
|
|
163
|
+
}
|
|
164
|
+
/** A scene's source: the options of `drawKyogijo`, an SVG the package wrote, or a scene. */
|
|
165
|
+
export type SceneSource = SurfaceOptions | ScoreOptions | string | Scene;
|
|
166
|
+
/** What a scene is made with. */
|
|
167
|
+
export interface SceneOptions {
|
|
168
|
+
/** The theme to work the colours out for. Without one, the drawing's own `data-theme`, else light. */
|
|
169
|
+
theme?: "light" | "dark";
|
|
170
|
+
/** More rules, read after the drawing's own: a page's stylesheet for the classes you add. */
|
|
171
|
+
css?: string;
|
|
172
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** An element, or a run of text. Attribute values and text are as written (escaped); `attr` and `textOf` read them. */
|
|
2
|
+
export interface SvgNode {
|
|
3
|
+
name: string;
|
|
4
|
+
attrs: Record<string, string>;
|
|
5
|
+
children: (SvgNode | string)[];
|
|
6
|
+
/** Written as `<a/>` rather than `<a></a>`. */
|
|
7
|
+
empty: boolean;
|
|
8
|
+
}
|
|
9
|
+
/** Text with its entities read: the five of XML and numeric ones. */
|
|
10
|
+
export declare function decode(text: string): string;
|
|
11
|
+
/** Text made safe for an attribute or a run of text, the way `esc` in format.ts does it. */
|
|
12
|
+
export declare const encode: (text: string) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Read an SVG string into a tree. Anything that is not the markup this package writes (an unclosed tag, an unquoted attribute) is an error that says where.
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { parseSvg } from "@johnmorrisdotca/kyogijo-stadiums/scene";
|
|
18
|
+
* const tree = parseSvg('<svg viewBox="0 0 10 5"><rect x="1" y="1" width="3" height="2"/></svg>');
|
|
19
|
+
* console.log(tree.name, tree.attrs.viewBox, tree.children.length);
|
|
20
|
+
* // svg 0 0 10 5 1
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function parseSvg(text: string): SvgNode;
|
|
24
|
+
/** An SVG tree as text: the string `parseSvg` was given, exactly. */
|
|
25
|
+
export declare function writeSvg(node: SvgNode): string;
|
|
26
|
+
/** The value of an attribute with its entities read. */
|
|
27
|
+
export declare const attr: (node: SvgNode, name: string) => string | undefined;
|
|
28
|
+
/** The text inside an element and its elements, with its entities read. */
|
|
29
|
+
export declare const textOf: (node: SvgNode) => string;
|
|
30
|
+
/** The elements inside an element, in order. */
|
|
31
|
+
export declare const elements: (node: SvgNode) => SvgNode[];
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// A small, exact reader and writer of the SVG this package writes: every tag closed, every attribute double-quoted. The tree keeps the text of every attribute
|
|
2
|
+
// and every run of text as it was written, so that writing it again gives back the same string, byte for byte.
|
|
3
|
+
const TOKEN = /<!--[\s\S]*?-->|<(\/?)([A-Za-z][\w:-]*)((?:\s+[\w:-]+="[^"<]*")*)\s*(\/?)>|([^<]+)/y;
|
|
4
|
+
const ATTRIBUTE = /([\w:-]+)="([^"]*)"/g;
|
|
5
|
+
/** Text with its entities read: the five of XML and numeric ones. */
|
|
6
|
+
export function decode(text) {
|
|
7
|
+
if (!text.includes("&"))
|
|
8
|
+
return text;
|
|
9
|
+
return text.replace(/&(?:#(\d+)|#x([0-9a-f]+)|(amp|lt|gt|quot|apos));/gi, (_, dec, hex, name) => {
|
|
10
|
+
if (dec !== undefined)
|
|
11
|
+
return String.fromCodePoint(Number(dec));
|
|
12
|
+
if (hex !== undefined)
|
|
13
|
+
return String.fromCodePoint(parseInt(hex, 16));
|
|
14
|
+
return { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" }[name.toLowerCase()];
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
/** Text made safe for an attribute or a run of text, the way `esc` in format.ts does it. */
|
|
18
|
+
export const encode = (text) => text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
19
|
+
/**
|
|
20
|
+
* Read an SVG string into a tree. Anything that is not the markup this package writes (an unclosed tag, an unquoted attribute) is an error that says where.
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { parseSvg } from "@johnmorrisdotca/kyogijo-stadiums/scene";
|
|
24
|
+
* const tree = parseSvg('<svg viewBox="0 0 10 5"><rect x="1" y="1" width="3" height="2"/></svg>');
|
|
25
|
+
* console.log(tree.name, tree.attrs.viewBox, tree.children.length);
|
|
26
|
+
* // svg 0 0 10 5 1
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function parseSvg(text) {
|
|
30
|
+
const root = { name: "#root", attrs: {}, children: [], empty: false };
|
|
31
|
+
const stack = [root];
|
|
32
|
+
let at = 0;
|
|
33
|
+
while (at < text.length) {
|
|
34
|
+
TOKEN.lastIndex = at;
|
|
35
|
+
const match = TOKEN.exec(text);
|
|
36
|
+
if (match === null)
|
|
37
|
+
throw new Error(`The SVG is not well-formed near: ${text.slice(at, at + 60)}`);
|
|
38
|
+
at = TOKEN.lastIndex;
|
|
39
|
+
if (match[0].startsWith("<!--"))
|
|
40
|
+
continue;
|
|
41
|
+
const top = stack[stack.length - 1];
|
|
42
|
+
if (match[5] !== undefined) {
|
|
43
|
+
top.children.push(match[5]);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (match[1] === "/") {
|
|
47
|
+
if (top.name !== match[2])
|
|
48
|
+
throw new Error(`</${match[2]}> closes <${top.name}>`);
|
|
49
|
+
stack.pop();
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const attrs = {};
|
|
53
|
+
for (const a of match[3].matchAll(ATTRIBUTE))
|
|
54
|
+
attrs[a[1]] = a[2];
|
|
55
|
+
const node = { name: match[2], attrs, children: [], empty: match[4] === "/" };
|
|
56
|
+
top.children.push(node);
|
|
57
|
+
if (!node.empty)
|
|
58
|
+
stack.push(node);
|
|
59
|
+
}
|
|
60
|
+
if (stack.length !== 1)
|
|
61
|
+
throw new Error(`<${stack[stack.length - 1].name}> is not closed`);
|
|
62
|
+
const elements = root.children.filter((child) => typeof child !== "string");
|
|
63
|
+
if (elements.length !== 1)
|
|
64
|
+
throw new Error("One root element is expected");
|
|
65
|
+
return elements[0];
|
|
66
|
+
}
|
|
67
|
+
/** An SVG tree as text: the string `parseSvg` was given, exactly. */
|
|
68
|
+
export function writeSvg(node) {
|
|
69
|
+
let out = `<${node.name}`;
|
|
70
|
+
for (const [key, value] of Object.entries(node.attrs))
|
|
71
|
+
out += ` ${key}="${value}"`;
|
|
72
|
+
if (node.empty)
|
|
73
|
+
return `${out}/>`;
|
|
74
|
+
return `${out}>${node.children.map((child) => (typeof child === "string" ? child : writeSvg(child))).join("")}</${node.name}>`;
|
|
75
|
+
}
|
|
76
|
+
/** The value of an attribute with its entities read. */
|
|
77
|
+
export const attr = (node, name) => (node.attrs[name] === undefined ? undefined : decode(node.attrs[name]));
|
|
78
|
+
/** The text inside an element and its elements, with its entities read. */
|
|
79
|
+
export const textOf = (node) => node.children.map((child) => (typeof child === "string" ? decode(child) : textOf(child))).join("");
|
|
80
|
+
/** The elements inside an element, in order. */
|
|
81
|
+
export const elements = (node) => node.children.filter((child) => typeof child !== "string");
|