@blackbelt-technology/pi-dashboard-deck3d 0.9.0 → 0.11.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.
Files changed (105) hide show
  1. package/.pi/skills/deck3d/SKILL.agent.md +78 -0
  2. package/.pi/skills/deck3d/SKILL.md +68 -0
  3. package/.pi/skills/deck3d/reference/effects.md +185 -2
  4. package/.pi/skills/deck3d/reference/ir-fields.md +26 -3
  5. package/.pi/skills/deck3d-from-pptx/SKILL.md +134 -0
  6. package/.pi/skills/deck3d-from-pptx/reference/steps.md +72 -0
  7. package/README.md +30 -2
  8. package/assets/preview/stub-chrome.png +0 -0
  9. package/assets/preview/stub-icon.json +59 -0
  10. package/assets/preview/stub-layout.json +195 -0
  11. package/assets/preview/stub-outline.json +48 -0
  12. package/dist/cli.js +11780 -5934
  13. package/dist/runtime.js +5916 -4007
  14. package/fixtures/business-2031/assets/stub-chrome.png +0 -0
  15. package/fixtures/business-2031/assets/stub-icon.json +59 -0
  16. package/fixtures/business-2031/assets/stub-layout.json +195 -0
  17. package/fixtures/business-2031/assets/stub-outline.json +48 -0
  18. package/fixtures/business-2031/build-overrides.mjs +58 -13
  19. package/fixtures/business-2031/deck.agent.md +60 -0
  20. package/fixtures/business-2031/deck.json +133 -8
  21. package/fixtures/business-2031/overrides.json +133 -8
  22. package/package.json +8 -4
  23. package/scripts/gen-stub-assets.ts +31 -0
  24. package/src/art/cutout.ts +97 -0
  25. package/src/art/decode.ts +169 -0
  26. package/src/art/icon.ts +380 -0
  27. package/src/art/png.ts +232 -0
  28. package/src/art/trace.ts +288 -0
  29. package/src/check/rules.ts +13 -8
  30. package/src/cli.ts +313 -51
  31. package/src/compare/index.ts +131 -0
  32. package/src/compare/ssim.ts +91 -0
  33. package/src/fx/art-geometry.ts +137 -0
  34. package/src/fx/art3d.meta.json +30 -0
  35. package/src/fx/art3d.ts +131 -0
  36. package/src/fx/bank.meta.json +26 -0
  37. package/src/fx/bank.ts +8 -0
  38. package/src/fx/compose.ts +5 -4
  39. package/src/fx/crane.meta.json +26 -0
  40. package/src/fx/crane.ts +8 -0
  41. package/src/fx/dolly.ts +5 -1
  42. package/src/fx/fade.ts +5 -1
  43. package/src/fx/flythrough.ts +5 -1
  44. package/src/fx/frame-chrome.meta.json +17 -0
  45. package/src/fx/frame-chrome.ts +134 -0
  46. package/src/fx/haze.meta.json +19 -0
  47. package/src/fx/haze.ts +91 -0
  48. package/src/fx/icon3d.meta.json +21 -0
  49. package/src/fx/icon3d.ts +167 -0
  50. package/src/fx/index.ts +96 -66
  51. package/src/fx/iris.ts +5 -1
  52. package/src/fx/meta.schema.json +10 -2
  53. package/src/fx/path.meta.json +25 -0
  54. package/src/fx/path.ts +8 -0
  55. package/src/fx/persistent.ts +11 -0
  56. package/src/fx/revolve.meta.json +26 -0
  57. package/src/fx/revolve.ts +8 -0
  58. package/src/fx/slide-layer.meta.json +26 -0
  59. package/src/fx/slide-layer.ts +557 -0
  60. package/src/fx/slide-space.ts +142 -0
  61. package/src/fx/stub-assets.ts +117 -0
  62. package/src/fx/swing.meta.json +26 -0
  63. package/src/fx/swing.ts +8 -0
  64. package/src/fx/types.ts +63 -2
  65. package/src/ir/merge.ts +5 -2
  66. package/src/ir/schema.json +140 -3
  67. package/src/ir/types.ts +54 -0
  68. package/src/ir/validate.ts +91 -3
  69. package/src/parse/harvest/index.ts +4 -1
  70. package/src/parse/markdown.ts +38 -7
  71. package/src/pptx/baseline.ts +183 -0
  72. package/src/pptx/extract.ts +161 -0
  73. package/src/pptx/fallback.ts +70 -0
  74. package/src/pptx/import.ts +256 -0
  75. package/src/pptx/master.ts +135 -0
  76. package/src/pptx/media.ts +81 -0
  77. package/src/pptx/package.ts +109 -0
  78. package/src/pptx/ref.ts +100 -0
  79. package/src/pptx/shapes.ts +276 -0
  80. package/src/pptx/stage.ts +82 -0
  81. package/src/pptx/text.ts +202 -0
  82. package/src/pptx/theme.ts +194 -0
  83. package/src/pptx/types.ts +159 -0
  84. package/src/pptx/xfrm.ts +93 -0
  85. package/src/pptx/xml.ts +106 -0
  86. package/src/pptx/zip.ts +239 -0
  87. package/src/render/assets.ts +57 -0
  88. package/src/render/build.ts +75 -0
  89. package/src/render/index.ts +7 -2
  90. package/src/render/template.html +1 -1
  91. package/src/runtime/assets.ts +75 -0
  92. package/src/runtime/fade.ts +110 -0
  93. package/src/runtime/hud.ts +37 -9
  94. package/src/runtime/index.ts +431 -67
  95. package/src/runtime/post.ts +11 -5
  96. package/src/runtime/scene.ts +25 -2
  97. package/src/runtime/transition.ts +222 -0
  98. package/src/runtime/types.ts +13 -2
  99. package/src/serve/index.ts +199 -16
  100. package/src/util/cleanup.ts +13 -0
  101. package/src/util/confine.ts +15 -0
  102. package/src/variants/pick.ts +282 -0
  103. package/src/variants/render.ts +179 -0
  104. package/src/variants/schema.json +48 -0
  105. package/src/variants/style.ts +318 -0
@@ -0,0 +1,194 @@
1
+ /**
2
+ * Theme colour scheme, fonts, and DrawingML colour resolution
3
+ * (`srgbClr | schemeClr | sysClr | prstClr` + `lumMod/lumOff/tint/shade/satMod/alpha`).
4
+ */
5
+ import type { ThemeInfo } from "./types.js";
6
+ import { child, num, path, type XNode } from "./xml.js";
7
+
8
+ export interface ResolvedColor {
9
+ hex: string;
10
+ alpha: number;
11
+ }
12
+
13
+ /** Master `p:clrMap`: logical slot (`bg1`, `tx1`, …) → scheme slot (`lt1`, `dk1`, …). */
14
+ export type ClrMap = Record<string, string>;
15
+
16
+ export const DEFAULT_CLR_MAP: ClrMap = {
17
+ bg1: "lt1",
18
+ tx1: "dk1",
19
+ bg2: "lt2",
20
+ tx2: "dk2",
21
+ accent1: "accent1",
22
+ accent2: "accent2",
23
+ accent3: "accent3",
24
+ accent4: "accent4",
25
+ accent5: "accent5",
26
+ accent6: "accent6",
27
+ hlink: "hlink",
28
+ folHlink: "folHlink",
29
+ };
30
+
31
+ const SCHEME_SLOTS = ["dk1", "lt1", "dk2", "lt2", "accent1", "accent2", "accent3", "accent4", "accent5", "accent6", "hlink", "folHlink"];
32
+
33
+ const PRESET: Record<string, string> = {
34
+ black: "#000000",
35
+ white: "#ffffff",
36
+ red: "#ff0000",
37
+ green: "#008000",
38
+ blue: "#0000ff",
39
+ yellow: "#ffff00",
40
+ gray: "#808080",
41
+ grey: "#808080",
42
+ orange: "#ffa500",
43
+ };
44
+
45
+ function normHex(v: string | undefined): string | undefined {
46
+ if (!v || !/^[0-9a-fA-F]{6}$/.test(v)) return undefined;
47
+ return `#${v.toLowerCase()}`;
48
+ }
49
+
50
+ export function parseTheme(theme: XNode | undefined): ThemeInfo {
51
+ const colors: Record<string, string> = {};
52
+ const scheme = path(theme, "a:themeElements", "a:clrScheme");
53
+ for (const slot of SCHEME_SLOTS) {
54
+ const node = child(scheme, `a:${slot}`);
55
+ const srgb = normHex(child(node, "a:srgbClr")?.attrs.val);
56
+ const sys = normHex(child(node, "a:sysClr")?.attrs.lastClr);
57
+ colors[slot] = srgb ?? sys ?? (slot.startsWith("dk") ? "#000000" : "#ffffff");
58
+ }
59
+ const fonts = path(theme, "a:themeElements", "a:fontScheme");
60
+ return {
61
+ colors,
62
+ fonts: {
63
+ major: path(fonts, "a:majorFont", "a:latin")?.attrs.typeface ?? "Calibri Light",
64
+ minor: path(fonts, "a:minorFont", "a:latin")?.attrs.typeface ?? "Calibri",
65
+ },
66
+ };
67
+ }
68
+
69
+ export function parseClrMap(node: XNode | undefined): ClrMap {
70
+ return node ? { ...DEFAULT_CLR_MAP, ...node.attrs } : { ...DEFAULT_CLR_MAP };
71
+ }
72
+
73
+ function hexToRgb(hex: string): [number, number, number] {
74
+ const n = Number.parseInt(hex.slice(1), 16);
75
+ return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
76
+ }
77
+
78
+ function rgbToHex(r: number, g: number, b: number): string {
79
+ const c = (v: number) =>
80
+ Math.round(Math.min(255, Math.max(0, v)))
81
+ .toString(16)
82
+ .padStart(2, "0");
83
+ return `#${c(r)}${c(g)}${c(b)}`;
84
+ }
85
+
86
+ export function rgbToHsl(r: number, g: number, b: number): [number, number, number] {
87
+ const rn = r / 255;
88
+ const gn = g / 255;
89
+ const bn = b / 255;
90
+ const max = Math.max(rn, gn, bn);
91
+ const min = Math.min(rn, gn, bn);
92
+ const l = (max + min) / 2;
93
+ if (max === min) return [0, 0, l];
94
+ const d = max - min;
95
+ const s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
96
+ let h: number;
97
+ if (max === rn) h = (gn - bn) / d + (gn < bn ? 6 : 0);
98
+ else if (max === gn) h = (bn - rn) / d + 2;
99
+ else h = (rn - gn) / d + 4;
100
+ return [h / 6, s, l];
101
+ }
102
+
103
+ export function hslToRgb(h: number, s: number, l: number): [number, number, number] {
104
+ if (s === 0) return [l * 255, l * 255, l * 255];
105
+ const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
106
+ const p = 2 * l - q;
107
+ const hue = (t: number): number => {
108
+ let x = t;
109
+ if (x < 0) x += 1;
110
+ if (x > 1) x -= 1;
111
+ if (x < 1 / 6) return p + (q - p) * 6 * x;
112
+ if (x < 1 / 2) return q;
113
+ if (x < 2 / 3) return p + (q - p) * (2 / 3 - x) * 6;
114
+ return p;
115
+ };
116
+ return [hue(h + 1 / 3) * 255, hue(h) * 255, hue(h - 1 / 3) * 255];
117
+ }
118
+
119
+ const clamp01 = (v: number) => Math.min(1, Math.max(0, v));
120
+
121
+ /** Apply DrawingML colour transforms in document order. */
122
+ function applyMods(hex: string, mods: XNode[]): ResolvedColor {
123
+ let [r, g, b] = hexToRgb(hex);
124
+ let alpha = 1;
125
+ for (const m of mods) {
126
+ const v = num(m.attrs.val) / 100000;
127
+ switch (m.name) {
128
+ case "a:lumMod":
129
+ case "a:lumOff": {
130
+ const [h, s, l] = rgbToHsl(r, g, b);
131
+ const nl = m.name === "a:lumMod" ? l * v : l + v;
132
+ [r, g, b] = hslToRgb(h, s, clamp01(nl));
133
+ break;
134
+ }
135
+ case "a:satMod": {
136
+ const [h, s, l] = rgbToHsl(r, g, b);
137
+ [r, g, b] = hslToRgb(h, clamp01(s * v), l);
138
+ break;
139
+ }
140
+ case "a:tint":
141
+ r += (255 - r) * (1 - v);
142
+ g += (255 - g) * (1 - v);
143
+ b += (255 - b) * (1 - v);
144
+ break;
145
+ case "a:shade":
146
+ r *= v;
147
+ g *= v;
148
+ b *= v;
149
+ break;
150
+ case "a:alpha":
151
+ alpha = clamp01(v);
152
+ break;
153
+ default:
154
+ break;
155
+ }
156
+ }
157
+ return { hex: rgbToHex(r, g, b), alpha };
158
+ }
159
+
160
+ export interface ColorEnv {
161
+ theme: ThemeInfo;
162
+ clrMap: ClrMap;
163
+ /** Placeholder colour (`phClr`) supplied by a style reference. */
164
+ phClr?: string;
165
+ }
166
+
167
+ /** Resolve the colour child of a fill-like node (`a:solidFill`, `a:fontRef`, …). */
168
+ export function resolveColor(holder: XNode | undefined, env: ColorEnv): ResolvedColor | undefined {
169
+ if (!holder) return undefined;
170
+ for (const c of holder.children) {
171
+ let base: string | undefined;
172
+ if (c.name === "a:srgbClr") base = normHex(c.attrs.val);
173
+ else if (c.name === "a:sysClr") base = normHex(c.attrs.lastClr) ?? (c.attrs.val === "window" ? "#ffffff" : "#000000");
174
+ else if (c.name === "a:prstClr") base = PRESET[c.attrs.val ?? ""] ?? "#000000";
175
+ else if (c.name === "a:schemeClr") {
176
+ const val = c.attrs.val ?? "tx1";
177
+ if (val === "phClr") base = env.phClr ?? env.theme.colors.dk1;
178
+ else base = env.theme.colors[env.clrMap[val] ?? val] ?? env.theme.colors.dk1;
179
+ } else continue;
180
+ if (!base) continue;
181
+ return applyMods(base, c.children);
182
+ }
183
+ return undefined;
184
+ }
185
+
186
+ /** Relative luminance (0–1) of a `#rrggbb` colour, sRGB → linear. */
187
+ export function luminance(hex: string): number {
188
+ const lin = (v: number) => {
189
+ const c = v / 255;
190
+ return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
191
+ };
192
+ const [r, g, b] = hexToRgb(hex);
193
+ return 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b);
194
+ }
@@ -0,0 +1,159 @@
1
+ /**
2
+ * `layout.json` — the deterministic slide-space description `pptx import`
3
+ * writes and `slide-layer` draws (spec deck3d-pptx-import "Slide space and
4
+ * layout description"). Slide space is 1920 units wide and keeps the deck's
5
+ * aspect; every rect is in those units, origin top-left.
6
+ */
7
+
8
+ export interface Rect {
9
+ x: number;
10
+ y: number;
11
+ w: number;
12
+ h: number;
13
+ }
14
+
15
+ export interface Insets {
16
+ l: number;
17
+ t: number;
18
+ r: number;
19
+ b: number;
20
+ }
21
+
22
+ export interface LayoutRun {
23
+ text: string;
24
+ /** Font size in slide-space units, after the stored autofit font scale. */
25
+ size: number;
26
+ bold: boolean;
27
+ italic: boolean;
28
+ underline: boolean;
29
+ /** Resolved `#rrggbb`. */
30
+ color: string;
31
+ font: string;
32
+ }
33
+
34
+ export interface LayoutParagraph {
35
+ align: "l" | "ctr" | "r" | "just";
36
+ /** Line spacing as a multiple of single spacing, after `lnSpcReduction`. */
37
+ lineSpacing: number;
38
+ /** Space before/after in slide-space units. */
39
+ spaceBefore: number;
40
+ spaceAfter: number;
41
+ level: number;
42
+ /** Bullet character when the paragraph is bulleted. */
43
+ bullet?: string;
44
+ runs: LayoutRun[];
45
+ }
46
+
47
+ interface ItemBase {
48
+ /** Stable `sNN-iMMM`. */
49
+ id: string;
50
+ rect: Rect;
51
+ /** Degrees, clockwise. */
52
+ rotation: number;
53
+ flipH: boolean;
54
+ flipV: boolean;
55
+ /** Inherited from the slide layout or master. */
56
+ origin?: "master";
57
+ }
58
+
59
+ export interface TextItem extends ItemBase {
60
+ kind: "text";
61
+ /** Placeholder type (`title`, `ctrTitle`, `body`, `subTitle`, …) when a placeholder. */
62
+ placeholder?: string;
63
+ anchor: "t" | "ctr" | "b";
64
+ insets: Insets;
65
+ vertical: boolean;
66
+ /** Direction vertical text reads in: 90 = top to bottom (`vert`), 270 = bottom to top (`vert270`). */
67
+ textRotation?: 90 | 270;
68
+ /**
69
+ * Id of the `fallback` item this text lives inside (a shape with an unsupported
70
+ * fill or preset that also carries text). When that fallback's raster crop is
71
+ * drawn it already contains the text, so `slide-layer` does not paint it again.
72
+ */
73
+ coveredBy?: string;
74
+ autofit: "none" | "norm" | "shape";
75
+ paragraphs: LayoutParagraph[];
76
+ }
77
+
78
+ export interface RectItem extends ItemBase {
79
+ kind: "rect";
80
+ preset: "rect" | "line" | "ellipse" | "roundRect";
81
+ fill?: string;
82
+ alpha: number;
83
+ line?: { color: string; width: number };
84
+ }
85
+
86
+ export interface ImgItem extends ItemBase {
87
+ kind: "img";
88
+ /** Media file name under `pptx/media/`. */
89
+ media: string;
90
+ /** Deck asset id of `media`. */
91
+ asset: string;
92
+ /** Crop fractions of the source image (0–1) per side. */
93
+ crop: Insets;
94
+ svg?: string;
95
+ svgAsset?: string;
96
+ }
97
+
98
+ export type FallbackConstruct =
99
+ | "chart"
100
+ | "smartart"
101
+ | "table"
102
+ | "ole"
103
+ | "emf"
104
+ | "wmf"
105
+ | "wordart"
106
+ | "shape"
107
+ | "fill"
108
+ | "picture-effect"
109
+ | "media";
110
+
111
+ export interface FallbackItem extends ItemBase {
112
+ kind: "fallback";
113
+ construct: FallbackConstruct;
114
+ /** Crop of the reference render; absent ⇒ outlined placeholder. */
115
+ media?: string;
116
+ asset?: string;
117
+ }
118
+
119
+ export type LayoutItem = TextItem | RectItem | ImgItem | FallbackItem;
120
+
121
+ export interface LayoutSlide {
122
+ id: string;
123
+ index: number;
124
+ title: string;
125
+ /** sha256 of the normalised title + the ordered text of the other (non-master) items. */
126
+ fingerprint: string;
127
+ background?: { color?: string; media?: string; asset?: string };
128
+ items: LayoutItem[];
129
+ }
130
+
131
+ export interface ThemeInfo {
132
+ colors: Record<string, string>;
133
+ fonts: { major: string; minor: string };
134
+ }
135
+
136
+ export interface Layout {
137
+ version: 1;
138
+ size: { w: number; h: number };
139
+ theme: ThemeInfo;
140
+ slides: LayoutSlide[];
141
+ }
142
+
143
+ /** One media file the import writes, with every package part that resolved to it. */
144
+ export interface MediaFile {
145
+ name: string;
146
+ /** Stable deck asset id (`pptx-<first part slug>`). */
147
+ asset: string;
148
+ mime: string;
149
+ bytes: Uint8Array;
150
+ sha256: string;
151
+ }
152
+
153
+ /** Thrown for every import failure; the message is the one-line CLI reason. */
154
+ export class PptxImportError extends Error {
155
+ constructor(message: string) {
156
+ super(message);
157
+ this.name = "PptxImportError";
158
+ }
159
+ }
@@ -0,0 +1,93 @@
1
+ /**
2
+ * EMU → slide space, and group transform composition (design D1 `xfrm.ts`).
3
+ *
4
+ * A child of `p:grpSp` lives in the group's child space (`chOff`/`chExt`).
5
+ * Mapping it into the parent: scale by `ext/chExt` about `chOff`→`off`, then
6
+ * mirror its centre about the group centre for each group flip, then rotate
7
+ * its centre about the group centre by the group rotation. The child's own
8
+ * rotation is negated by an odd number of flips, then adds the group's; its
9
+ * flips toggle with the group's. Frames nest, so groups of groups compose.
10
+ */
11
+ import type { Rect } from "./types.js";
12
+ import { child, num, type XNode } from "./xml.js";
13
+
14
+ export interface Placed {
15
+ /** EMU in the parent space. */
16
+ x: number;
17
+ y: number;
18
+ w: number;
19
+ h: number;
20
+ /** Degrees clockwise. */
21
+ rot: number;
22
+ flipH: boolean;
23
+ flipV: boolean;
24
+ }
25
+
26
+ export type Frame = (p: Placed) => Placed;
27
+
28
+ export const IDENTITY: Frame = (p) => p;
29
+
30
+ /** Read `a:xfrm` (or `p:xfrm`) of a shape. Undefined when it carries no `a:off`. */
31
+ export function readXfrm(xfrm: XNode | undefined): Placed | undefined {
32
+ const off = child(xfrm, "a:off");
33
+ const ext = child(xfrm, "a:ext");
34
+ if (!xfrm || !off || !ext) return undefined;
35
+ return {
36
+ x: num(off.attrs.x),
37
+ y: num(off.attrs.y),
38
+ w: num(ext.attrs.cx),
39
+ h: num(ext.attrs.cy),
40
+ rot: num(xfrm.attrs.rot) / 60000,
41
+ flipH: xfrm.attrs.flipH === "1" || xfrm.attrs.flipH === "true",
42
+ flipV: xfrm.attrs.flipV === "1" || xfrm.attrs.flipV === "true",
43
+ };
44
+ }
45
+
46
+ function rotateAbout(px: number, py: number, cx: number, cy: number, deg: number): [number, number] {
47
+ const r = (deg * Math.PI) / 180;
48
+ const dx = px - cx;
49
+ const dy = py - cy;
50
+ // Clockwise in a y-down space.
51
+ return [cx + dx * Math.cos(r) - dy * Math.sin(r), cy + dx * Math.sin(r) + dy * Math.cos(r)];
52
+ }
53
+
54
+ /** Frame for the children of a group whose `a:xfrm` is `xfrm`, nested in `parent`. */
55
+ export function groupFrame(xfrm: XNode | undefined, parent: Frame): Frame {
56
+ const g = readXfrm(xfrm);
57
+ if (!g) return parent;
58
+ const chOff = child(xfrm, "a:chOff");
59
+ const chExt = child(xfrm, "a:chExt");
60
+ const cox = num(chOff?.attrs.x, g.x);
61
+ const coy = num(chOff?.attrs.y, g.y);
62
+ const cw = num(chExt?.attrs.cx, g.w) || g.w || 1;
63
+ const ch = num(chExt?.attrs.cy, g.h) || g.h || 1;
64
+ const sx = g.w / cw;
65
+ const sy = g.h / ch;
66
+ const gcx = g.x + g.w / 2;
67
+ const gcy = g.y + g.h / 2;
68
+ return (p) => {
69
+ const w = p.w * sx;
70
+ const h = p.h * sy;
71
+ let cx = g.x + (p.x + p.w / 2 - cox) * sx;
72
+ let cy = g.y + (p.y + p.h / 2 - coy) * sy;
73
+ if (g.flipH) cx = 2 * gcx - cx;
74
+ if (g.flipV) cy = 2 * gcy - cy;
75
+ [cx, cy] = rotateAbout(cx, cy, gcx, gcy, g.rot);
76
+ const odd = g.flipH !== g.flipV;
77
+ const rot = (odd ? -p.rot : p.rot) + g.rot;
78
+ return parent({ x: cx - w / 2, y: cy - h / 2, w, h, rot, flipH: p.flipH !== g.flipH, flipV: p.flipV !== g.flipV });
79
+ };
80
+ }
81
+
82
+ const round2 = (v: number) => Math.round(v * 100) / 100;
83
+
84
+ /** Normalise degrees into [0, 360). */
85
+ export function normDeg(d: number): number {
86
+ const r = ((d % 360) + 360) % 360;
87
+ return round2(r === 360 ? 0 : r);
88
+ }
89
+
90
+ /** EMU rect → slide-space rect (rounded to 0.01 for byte-stable JSON). */
91
+ export function toSlide(p: Placed, scale: number): Rect {
92
+ return { x: round2(p.x * scale), y: round2(p.y * scale), w: round2(p.w * scale), h: round2(p.h * scale) };
93
+ }
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Minimal ordered XML tree over fast-xml-parser (design D1). Entities are
3
+ * never expanded by the parser (`processEntities: false`); only the five
4
+ * predefined XML entities and numeric character references are decoded here,
5
+ * which needs no DTD. DOCTYPE parts never reach this module (zip.ts rejects).
6
+ */
7
+ import { XMLParser } from "fast-xml-parser";
8
+
9
+ export interface XNode {
10
+ /** Qualified name as written (`p:sp`, `a:t`). */
11
+ name: string;
12
+ attrs: Record<string, string>;
13
+ children: XNode[];
14
+ /** Concatenated direct text content. */
15
+ text: string;
16
+ }
17
+
18
+ const parser = new XMLParser({
19
+ preserveOrder: true,
20
+ ignoreAttributes: false,
21
+ attributeNamePrefix: "",
22
+ processEntities: false,
23
+ htmlEntities: false,
24
+ trimValues: false,
25
+ parseTagValue: false,
26
+ parseAttributeValue: false,
27
+ ignoreDeclaration: true,
28
+ ignorePiTags: true,
29
+ });
30
+
31
+ const NAMED: Record<string, string> = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };
32
+
33
+ export function decodeEntities(s: string): string {
34
+ if (!s.includes("&")) return s;
35
+ return s.replace(/&(#x[0-9a-fA-F]+|#[0-9]+|amp|lt|gt|quot|apos);/g, (_m, ref: string) => {
36
+ if (ref.startsWith("#x")) return String.fromCodePoint(Number.parseInt(ref.slice(2), 16));
37
+ if (ref.startsWith("#")) return String.fromCodePoint(Number.parseInt(ref.slice(1), 10));
38
+ return NAMED[ref];
39
+ });
40
+ }
41
+
42
+ type Raw = Record<string, unknown>;
43
+
44
+ function convert(raw: Raw): XNode | null {
45
+ const name = Object.keys(raw).find((k) => k !== ":@");
46
+ if (!name || name === "#text") return null;
47
+ const attrsRaw = (raw[":@"] ?? {}) as Record<string, unknown>;
48
+ const attrs: Record<string, string> = {};
49
+ for (const [k, v] of Object.entries(attrsRaw)) attrs[k] = decodeEntities(String(v));
50
+ const children: XNode[] = [];
51
+ let text = "";
52
+ for (const c of (raw[name] as Raw[]) ?? []) {
53
+ if ("#text" in c) {
54
+ text += decodeEntities(String(c["#text"]));
55
+ continue;
56
+ }
57
+ const n = convert(c);
58
+ if (n) children.push(n);
59
+ }
60
+ return { name, attrs, children, text };
61
+ }
62
+
63
+ /** Parse an XML part; returns the document element. */
64
+ export function parseXml(input: Uint8Array | string): XNode {
65
+ const text = typeof input === "string" ? input : new TextDecoder().decode(input);
66
+ const raw = parser.parse(text) as Raw[];
67
+ for (const r of raw) {
68
+ const n = convert(r);
69
+ if (n) return n;
70
+ }
71
+ throw new Error("empty XML document");
72
+ }
73
+
74
+ /** First direct child named `name`. */
75
+ export function child(n: XNode | undefined, name: string): XNode | undefined {
76
+ return n?.children.find((c) => c.name === name);
77
+ }
78
+
79
+ /** All direct children named `name`. */
80
+ export function kids(n: XNode | undefined, name: string): XNode[] {
81
+ return n ? n.children.filter((c) => c.name === name) : [];
82
+ }
83
+
84
+ /** Walk a path of direct child names. */
85
+ export function path(n: XNode | undefined, ...names: string[]): XNode | undefined {
86
+ let cur = n;
87
+ for (const name of names) cur = child(cur, name);
88
+ return cur;
89
+ }
90
+
91
+ /** First descendant (depth-first) named `name`. */
92
+ export function descendant(n: XNode | undefined, name: string): XNode | undefined {
93
+ if (!n) return undefined;
94
+ for (const c of n.children) {
95
+ if (c.name === name) return c;
96
+ const d = descendant(c, name);
97
+ if (d) return d;
98
+ }
99
+ return undefined;
100
+ }
101
+
102
+ export function num(v: string | undefined, fallback = 0): number {
103
+ if (v === undefined) return fallback;
104
+ const n = Number(v);
105
+ return Number.isFinite(n) ? n : fallback;
106
+ }