@oim-media/core 1.27.9 → 1.27.10

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 (48) hide show
  1. package/README.md +26 -1
  2. package/dist/compatibility.d.ts +1 -1
  3. package/dist/compatibility.js +2 -1
  4. package/dist/v2/geometry-outline.d.ts +23 -0
  5. package/dist/v2/geometry-outline.js +35 -0
  6. package/dist/v2/index.d.ts +10 -0
  7. package/dist/v2/index.js +11 -0
  8. package/dist/v2/layer-blocks.d.ts +11 -0
  9. package/dist/v2/layer-blocks.js +34 -0
  10. package/dist/v2/paint-css.d.ts +46 -0
  11. package/dist/v2/paint-css.js +109 -0
  12. package/dist/v2/schema.d.ts +1180 -0
  13. package/dist/v2/schema.js +433 -0
  14. package/dist/v2/upgrade/animations.d.ts +7 -0
  15. package/dist/v2/upgrade/animations.js +255 -0
  16. package/dist/v2/upgrade/box.d.ts +10 -0
  17. package/dist/v2/upgrade/box.js +119 -0
  18. package/dist/v2/upgrade/context.d.ts +42 -0
  19. package/dist/v2/upgrade/context.js +54 -0
  20. package/dist/v2/upgrade/effects.d.ts +25 -0
  21. package/dist/v2/upgrade/effects.js +204 -0
  22. package/dist/v2/upgrade/geometry.d.ts +9 -0
  23. package/dist/v2/upgrade/geometry.js +162 -0
  24. package/dist/v2/upgrade/gradient.d.ts +14 -0
  25. package/dist/v2/upgrade/gradient.js +79 -0
  26. package/dist/v2/upgrade/keys.d.ts +9 -0
  27. package/dist/v2/upgrade/keys.js +31 -0
  28. package/dist/v2/upgrade/mask.d.ts +7 -0
  29. package/dist/v2/upgrade/mask.js +64 -0
  30. package/dist/v2/upgrade/paints.d.ts +21 -0
  31. package/dist/v2/upgrade/paints.js +258 -0
  32. package/dist/v2/upgrade/root.d.ts +21 -0
  33. package/dist/v2/upgrade/root.js +94 -0
  34. package/dist/v2/upgrade/svg-paths.d.ts +38 -0
  35. package/dist/v2/upgrade/svg-paths.js +225 -0
  36. package/dist/v2/upgrade/text.d.ts +7 -0
  37. package/dist/v2/upgrade/text.js +132 -0
  38. package/dist/v2/upgrade/tree.d.ts +15 -0
  39. package/dist/v2/upgrade/tree.js +49 -0
  40. package/dist/v2/upgrade/variables.d.ts +8 -0
  41. package/dist/v2/upgrade/variables.js +24 -0
  42. package/dist/v2/upgrade/zones.d.ts +12 -0
  43. package/dist/v2/upgrade/zones.js +63 -0
  44. package/dist/v2/upgrade.d.ts +17 -0
  45. package/dist/v2/upgrade.js +143 -0
  46. package/dist/v2/version.d.ts +6 -0
  47. package/dist/v2/version.js +7 -0
  48. package/package.json +5 -1
@@ -0,0 +1,258 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2026 Xue Studios LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ // Every way 1.x paints something -fill, text colour, pattern, background,
7
+ // stroke, text outline- becomes one of two lists: `fills` and `strokes`.
8
+ import { admits } from "../layer-blocks.js";
9
+ import { asObject } from "./context.js";
10
+ import { parseCssGradient } from "./gradient.js";
11
+ import { drawsMarkup } from "./svg-paths.js";
12
+ export const isColor = (value) => typeof value === "string" && value !== "" && value !== "transparent" && value !== "none";
13
+ /**
14
+ * A colour and its role. A known role (a palette's, or one of the dynamic roles
15
+ * a host page can set) becomes a binding that keeps the literal as its fallback,
16
+ * which is what 1.x painted: `var(--oim-color-<role>, <literal>)`.
17
+ */
18
+ export function colorOf(ctx, owner, key, roleKey) {
19
+ const literal = ctx.take(owner, key);
20
+ const role = roleKey ? ctx.take(owner, roleKey) : undefined;
21
+ if (typeof role === "string" && ctx.hasRole(role))
22
+ return isColor(literal) ? { var: role, fallback: literal } : { var: role };
23
+ return isColor(literal) ? literal : undefined;
24
+ }
25
+ const numbers = (value) => Array.isArray(value) && value.length > 0 && value.every((n) => typeof n === "number") ? value : undefined;
26
+ /** The pattern fields that share a prefix: `fillPattern*` on a layer, `backgroundPattern*` on a layer or the canvas. */
27
+ function patternOf(ctx, owner, prefix, cssAnimation) {
28
+ const preset = ctx.take(owner, `${prefix}Type`);
29
+ const single = colorOf(ctx, owner, `${prefix}Color`, `${prefix}ColorRole`);
30
+ const bg = colorOf(ctx, owner, `${prefix}BgColor`, `${prefix}BgColorRole`);
31
+ const list = ctx.take(owner, `${prefix}Colors`);
32
+ const scale = ctx.take(owner, `${prefix}Scale`);
33
+ const speed = ctx.take(owner, `${prefix}Speed`);
34
+ const direction = ctx.take(owner, `${prefix}Direction`);
35
+ const svgs = ctx.take(owner, `${prefix}Svgs`);
36
+ const sizes = numbers(ctx.take(owner, `${prefix}Sizes`));
37
+ // The canvas wrote the singular, the layer the plural: the same list.
38
+ const counts = numbers(ctx.take(owner, `${prefix}Counts`)) ?? numbers(ctx.take(owner, `${prefix}Count`));
39
+ const dispersions = numbers(ctx.take(owner, `${prefix}Dispersions`)) ?? numbers(ctx.take(owner, `${prefix}Dispersion`));
40
+ const colors = Array.isArray(list) ? list.filter(isColor) : [];
41
+ // A raw SVG tile carries no preset: only the colours it is recoloured with, its fit and its motion.
42
+ const tile = prefix === "backgroundPattern" && typeof owner.svgRawXml === "string" && owner.svgRawXml !== "";
43
+ if ((typeof preset !== "string" || !preset) && !tile && single === undefined && bg === undefined && colors.length === 0)
44
+ return undefined;
45
+ const pattern = typeof preset === "string" && preset ? { preset } : {};
46
+ // The first colour of the list is the one the role tints.
47
+ if (colors.length > 0 && single !== undefined && typeof single === "object")
48
+ colors[0] = single;
49
+ if (colors.length > 0)
50
+ pattern.colors = colors;
51
+ else if (single !== undefined)
52
+ pattern.colors = [single];
53
+ if (bg !== undefined)
54
+ pattern.bg = bg;
55
+ if (typeof scale === "number" && scale !== 1)
56
+ pattern.scale = scale;
57
+ if (sizes)
58
+ pattern.sizes = sizes;
59
+ if (counts)
60
+ pattern.counts = counts;
61
+ if (dispersions)
62
+ pattern.dispersions = dispersions;
63
+ if (Array.isArray(svgs) && svgs.length > 0)
64
+ pattern.svgs = svgs.filter((s) => typeof s === "string");
65
+ // 1.x said how the pattern moves twice: in these fields and, on the canvas
66
+ // background, as a CSS animation string. Either one is enough.
67
+ const css = typeof cssAnimation === "string" ? /^background-animation-([a-z-]+)\s+([\d.]+)s\b/.exec(cssAnimation) : null;
68
+ const way = typeof direction === "string" && direction !== "none" ? direction : css?.[1];
69
+ // The canvas moved a direction given without a speed at 8 s a cycle.
70
+ const period = typeof speed === "number" && speed > 0 ? speed : css ? Number(css[2]) : prefix === "backgroundPattern" && way ? 8 : undefined;
71
+ if (way && MOTION.has(way) && period)
72
+ pattern.motion = { direction: way, period };
73
+ return { kind: "pattern", pattern };
74
+ }
75
+ const MOTION = new Set(["right", "left", "up", "down", "zoom-in", "zoom-out", "pulse"]);
76
+ function gradientOf(ctx, owner, key, label) {
77
+ const css = ctx.take(owner, key);
78
+ if (css === undefined || css === null || css === "")
79
+ return undefined;
80
+ const gradient = parseCssGradient(css);
81
+ if (!gradient) {
82
+ ctx.note(`${label}.${key}: gradient not understood`);
83
+ return undefined;
84
+ }
85
+ return { kind: "gradient", gradient };
86
+ }
87
+ function imageOf(ctx, style) {
88
+ const src = ctx.take(style, "fillImage");
89
+ const crop = asObject(ctx.take(style, "fillImageCrop"));
90
+ const flipX = ctx.take(style, "fillImageFlipX");
91
+ const flipY = ctx.take(style, "fillImageFlipY");
92
+ if (typeof src !== "string" || !src)
93
+ return undefined;
94
+ const paint = { kind: "image", src };
95
+ if (crop)
96
+ paint.crop = crop;
97
+ if (flipX === true)
98
+ paint.flipX = true;
99
+ if (flipY === true)
100
+ paint.flipY = true;
101
+ return paint;
102
+ }
103
+ const TEXT_GRADIENT_PRESETS = new Set(["metallic-chrome", "sunset-gold", "neon-glow", "linear"]);
104
+ function fillOf(ctx, slot) {
105
+ const { style } = slot;
106
+ const fillType = ctx.take(style, "fillType");
107
+ const backgroundType = ctx.take(style, "backgroundType");
108
+ // Everything is taken even when it does not win: 1.x writes the fields of
109
+ // every kind of fill and only `fillType` says which one counts.
110
+ // 1.x painted fillColor under `fillColorRole || colorRole`; `color` itself was
111
+ // retired in 1.5.0 and paints nothing, so it is only taken.
112
+ const roleKey = typeof style.fillColorRole === "string" && style.fillColorRole ? "fillColorRole" : "colorRole";
113
+ const color = colorOf(ctx, style, "fillColor", roleKey);
114
+ ctx.take(style, "fillColorRole");
115
+ ctx.take(style, "colorRole");
116
+ ctx.take(style, "color");
117
+ const gradient = gradientOf(ctx, style, "fillGradient", "style");
118
+ const image = imageOf(ctx, style);
119
+ const cssAnimation = style.cssAnimation;
120
+ const fillPattern = patternOf(ctx, style, "fillPattern", cssAnimation);
121
+ const backgroundPattern = patternOf(ctx, style, "backgroundPattern", cssAnimation);
122
+ const generative = asObject(ctx.take(style, "fillGenerative")) ?? asObject(ctx.take(style, "backgroundGenerative"));
123
+ const backgroundFit = ctx.take(style, "backgroundFit");
124
+ const textGradient = ctx.take(style, "textGradientType");
125
+ // A background tile could be stretched over the canvas instead of repeated.
126
+ if (backgroundPattern && backgroundFit === "cover" && "pattern" in backgroundPattern)
127
+ backgroundPattern.pattern.fit = "cover";
128
+ const pattern = fillPattern ?? backgroundPattern;
129
+ if (pattern && typeof cssAnimation === "string" && /^background-animation-/.test(cssAnimation))
130
+ ctx.take(style, "cssAnimation");
131
+ const solid = () => {
132
+ return color === undefined ? undefined : { kind: "color", value: color };
133
+ };
134
+ const kind = typeof fillType === "string" ? fillType : typeof backgroundType === "string" ? backgroundType : slot.type === "pattern" || backgroundPattern ? "pattern" : "color";
135
+ if (kind === "none" || kind === "transparent")
136
+ return undefined;
137
+ if (kind === "gradient")
138
+ return gradient ?? solid();
139
+ // 1.x kept fillColor under the photo: it shows when the photo does not load.
140
+ if (kind === "image")
141
+ return image ? (color === undefined ? image : [image, solid()]) : solid();
142
+ if (kind === "pattern")
143
+ return pattern ?? solid();
144
+ if (kind === "generative")
145
+ return generative ? { kind: "generative", generative: generative } : solid();
146
+ if ((slot.type === "text" || slot.type === "artistic-text") && typeof textGradient === "string" && TEXT_GRADIENT_PRESETS.has(textGradient) && textGradient !== "linear") {
147
+ return { kind: "gradient", preset: textGradient };
148
+ }
149
+ return solid();
150
+ }
151
+ function strokeOf(ctx, style, colorKey, widthKey, extra = {}, roleKey) {
152
+ const color = colorOf(ctx, style, colorKey, roleKey);
153
+ const width = ctx.take(style, widthKey);
154
+ // A colour with no width paints nothing: 1.x writes the default colour on every layer.
155
+ if (typeof width !== "number" || !(width > 0) || color === undefined)
156
+ return undefined;
157
+ return { paint: { kind: "color", value: color }, width, ...extra };
158
+ }
159
+ /** 1.x painted a text stroke black when it had no colour, under `textStrokeColorRole || strokeColorRole`. */
160
+ function textStrokeOf(ctx, style) {
161
+ const roleKey = typeof style.textStrokeColorRole === "string" && style.textStrokeColorRole ? "textStrokeColorRole" : "strokeColorRole";
162
+ const width = style.textStrokeWidth;
163
+ const color = colorOf(ctx, style, "textStrokeColor", roleKey);
164
+ ctx.take(style, "textStrokeColorRole");
165
+ ctx.take(style, "textStrokeWidth");
166
+ if (typeof width !== "number" || !(width > 0))
167
+ return undefined;
168
+ return { paint: { kind: "color", value: color ?? "#000000" }, width };
169
+ }
170
+ export function convertPaints(ctx, slot, layer) {
171
+ const { style } = slot;
172
+ const fills = fillOf(ctx, slot);
173
+ const fill = Array.isArray(fills) ? fills[0] : fills;
174
+ // 1.x painted no colour inside a custom path (only a photo, a gradient or a
175
+ // pattern) nor inside the scribble, an open stroke.
176
+ const shape = style.geoShape;
177
+ const customPath = typeof style.svgPath === "string" && style.svgPath !== "" && (shape === undefined || shape === "custom");
178
+ const colourless = slot.type === "geo" && (customPath || shape === "scribble") && !!fill && "kind" in fill && fill.kind === "color";
179
+ if (fill && !colourless && admits(layer.type, "fills"))
180
+ layer.fills = Array.isArray(fills) ? fills : [fill];
181
+ const strokeStyle = ctx.take(style, "strokeStyle");
182
+ const dashed = strokeStyle === "dashed" || strokeStyle === "dotted" ? { style: strokeStyle } : {};
183
+ // 1.x passed borderColor and borderWidth to CSS without a border-style, so
184
+ // they painted nothing unless the file also carried a visible borderStyle.
185
+ const borderStyle = ctx.take(style, "borderStyle");
186
+ const border = strokeOf(ctx, style, "borderColor", "borderWidth", borderStyle === "dashed" || borderStyle === "dotted" ? { style: borderStyle } : {});
187
+ // An SVG layer: 1.x tinted every `currentColor` of the drawing with strokeColor, at any
188
+ // width. In 2.0 a path's `currentColor` stroke is the layer's first stroke.
189
+ const markup = drawsMarkup(slot);
190
+ const tint = markup ? colorOf(ctx, style, "strokeColor", "strokeColorRole") : undefined;
191
+ if (markup)
192
+ ctx.take(style, "strokeWidth");
193
+ let outline = tint !== undefined ? { paint: { kind: "color", value: tint }, width: 0 }
194
+ : strokeOf(ctx, style, "strokeColor", "strokeWidth", dashed, "strokeColorRole");
195
+ // The scribble is an open stroke with no fill: 1.x gave it a stroke of its own when it had
196
+ // none, 2 px wide and in its stroke colour, else its fill colour, else blue.
197
+ if (!outline && style.geoShape === "scribble") {
198
+ const width = typeof style.strokeWidth === "number" && style.strokeWidth > 0 ? style.strokeWidth : 2;
199
+ const color = colorOf(ctx, style, "strokeColor", "strokeColorRole") ?? colorOf(ctx, style, "fillColor", "fillColorRole") ?? "#2563EB";
200
+ outline = { paint: { kind: "color", value: color }, width, ...dashed };
201
+ }
202
+ const glyphs = textStrokeOf(ctx, style);
203
+ // On a text the stroke is the outline of the letters. The box border 1.x drew
204
+ // when a text carried strokeColor has no 2.0 meaning and is dropped.
205
+ const outer = strokeOf(ctx, style, "outerStrokeColor", "outerStrokeWidth", { align: "outside" });
206
+ const inner = strokeOf(ctx, style, "innerStrokeColor", "innerStrokeWidth", { align: "inside" });
207
+ const strokes = (layer.type === "text" ? [glyphs, outer, inner] : [
208
+ outline,
209
+ borderStyle === "solid" || borderStyle === "dashed" || borderStyle === "dotted" ? border : undefined,
210
+ glyphs,
211
+ outer,
212
+ inner,
213
+ ]).filter((s) => s !== undefined);
214
+ if (strokes.length > 0 && admits(layer.type, "strokes"))
215
+ layer.strokes = strokes;
216
+ }
217
+ const TEXTURE_KEYS = [
218
+ ["tipo", "type"],
219
+ ["color", "color"],
220
+ ["angulo", "angle"],
221
+ ["cruzada", "crossed"],
222
+ ["grosor", "thickness"],
223
+ ["tamano", "size"],
224
+ ];
225
+ /** The background of the canvas, which 1.x kept in some twenty undeclared root keys. */
226
+ export function canvasFills(ctx, meta) {
227
+ const type = ctx.take(meta, "backgroundType");
228
+ const color = colorOf(ctx, meta, "backgroundColor");
229
+ const gradient = gradientOf(ctx, meta, "backgroundGradient", "root");
230
+ // The gradient string already says all of this.
231
+ for (const key of ["backgroundGradientAngle", "backgroundGradientStart", "backgroundGradientEnd", "backgroundGradientType"]) {
232
+ ctx.take(meta, key);
233
+ }
234
+ const pattern = patternOf(ctx, meta, "backgroundPattern");
235
+ const generative = asObject(ctx.take(meta, "backgroundGenerative"));
236
+ const texture = asObject(ctx.take(meta, "backgroundTexture"));
237
+ const fills = [];
238
+ const solid = color === undefined ? undefined : { kind: "color", value: color };
239
+ if (type === "gradient")
240
+ fills.push(...[gradient ?? solid].filter((p) => !!p));
241
+ else if (type === "pattern")
242
+ fills.push(...[pattern ?? solid].filter((p) => !!p));
243
+ else if (type === "generative" && generative)
244
+ fills.push({ kind: "generative", generative: generative });
245
+ else if (type !== "transparent" && solid)
246
+ fills.push(solid);
247
+ if (texture && typeof texture.tipo === "string") {
248
+ const drawn = {};
249
+ for (const [spanish, english] of TEXTURE_KEYS)
250
+ if (texture[spanish] !== undefined)
251
+ drawn[english] = texture[spanish];
252
+ const paint = { kind: "texture", texture: drawn };
253
+ if (typeof texture.opacidad === "number")
254
+ paint.opacity = Math.min(1, Math.max(0, texture.opacidad));
255
+ fills.push(paint);
256
+ }
257
+ return fills;
258
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2026 Xue Studios LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import type { OimDocument } from "../schema.js";
7
+ import { type UpgradeContext, type V1Object } from "./context.js";
8
+ export declare const TRANSPORT_KEYS: string[];
9
+ export declare const RETIRED_ROOT_KEYS: string[];
10
+ export declare function canvasOf(meta: V1Object): {
11
+ width: number;
12
+ height: number;
13
+ };
14
+ export declare function languagesOf(meta: V1Object): string[];
15
+ /** Every colour role some palette of the document defines. */
16
+ export declare function rolesOf(meta: V1Object): Set<string>;
17
+ /** The 2.0 document without its layers, and the 1.x list the layers come from. */
18
+ export declare function convertRoot(ctx: UpgradeContext, meta: V1Object): {
19
+ document: OimDocument;
20
+ slots: unknown[];
21
+ };
@@ -0,0 +1,94 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2026 Xue Studios LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import { permissionOf } from "../../reeditPermission.js";
7
+ import { DOCUMENT_VERSION } from "../version.js";
8
+ import { asObject } from "./context.js";
9
+ /** Fallback when a 1.x file declares no canvas: the base the core has always assumed. */
10
+ const DEFAULT_CANVAS = 800;
11
+ // A converted document is not a signed one: it is signed again as 2.0, so the
12
+ // signature of the 1.x bytes and everything that travelled with it is dropped.
13
+ export const TRANSPORT_KEYS = ["signature", "key_version", "signed_at", "timestamp", "image_hash", "image_hash_alg"];
14
+ // Root keys with no place in 2.0: superseded, never read, or about the video format.
15
+ export const RETIRED_ROOT_KEYS = [
16
+ "version",
17
+ "min_player",
18
+ "image",
19
+ "media_type",
20
+ "allowed_domains",
21
+ "terms_accepted",
22
+ "redirect_membership_verified",
23
+ "capabilities",
24
+ "capabilities_required",
25
+ ];
26
+ export function canvasOf(meta) {
27
+ const canvas = asObject(meta.canvas) ?? {};
28
+ const positive = (value) => (typeof value === "number" && value > 0 ? value : undefined);
29
+ // The player's own chain: the top-level sizes first, then the canvas object.
30
+ const width = positive(meta.artboardWidth) ?? positive(meta.canvasWidth) ?? positive(meta.canvas_width)
31
+ ?? positive(canvas.natural_width) ?? positive(canvas.width) ?? DEFAULT_CANVAS;
32
+ const height = positive(meta.artboardHeight) ?? positive(meta.canvasHeight) ?? positive(meta.canvas_height)
33
+ ?? positive(canvas.natural_height) ?? positive(canvas.height) ?? width;
34
+ return { width, height };
35
+ }
36
+ export function languagesOf(meta) {
37
+ for (const key of ["languages", "lang"]) {
38
+ const list = meta[key];
39
+ if (Array.isArray(list)) {
40
+ const clean = list.filter((l) => typeof l === "string" && l.length > 0);
41
+ if (clean.length > 0)
42
+ return [...new Set(clean)];
43
+ }
44
+ }
45
+ return ["es"];
46
+ }
47
+ /** Every colour role some palette of the document defines. */
48
+ export function rolesOf(meta) {
49
+ const roles = new Set();
50
+ for (const palette of Object.values(asObject(meta.palettes) ?? {})) {
51
+ for (const role of Object.keys(asObject(palette) ?? {}))
52
+ roles.add(role);
53
+ }
54
+ return roles;
55
+ }
56
+ /** The 2.0 document without its layers, and the 1.x list the layers come from. */
57
+ export function convertRoot(ctx, meta) {
58
+ const document = {
59
+ oim: DOCUMENT_VERSION,
60
+ canvas: ctx.canvas,
61
+ languages: languagesOf(meta),
62
+ layers: [],
63
+ };
64
+ for (const key of ["oim", "canvas", "languages", "lang"])
65
+ ctx.take(meta, key);
66
+ const id = ctx.take(meta, "oim_id");
67
+ if (typeof id === "string" && id)
68
+ document.id = id;
69
+ const name = ctx.take(meta, "name");
70
+ if (typeof name === "string" && name)
71
+ document.name = name;
72
+ // `reedit` wins over the lock; an unknown value never loosens the permission.
73
+ if (meta.reedit !== undefined || meta.immutable_lock !== undefined)
74
+ document.reedit = permissionOf(meta);
75
+ ctx.take(meta, "reedit");
76
+ ctx.take(meta, "immutable_lock");
77
+ const description = asObject(ctx.take(meta, "description"));
78
+ if (description && Object.keys(description).length > 0)
79
+ document.description = description;
80
+ const fonts = ctx.take(meta, "fonts");
81
+ if (Array.isArray(fonts) && fonts.length > 0)
82
+ document.fonts = fonts;
83
+ for (const key of TRANSPORT_KEYS)
84
+ ctx.retire(meta, key);
85
+ for (const key of RETIRED_ROOT_KEYS)
86
+ ctx.retire(meta, key);
87
+ let slots = [];
88
+ for (const key of ["slots", "elements", "layers"]) {
89
+ const list = ctx.take(meta, key);
90
+ if (slots.length === 0 && Array.isArray(list))
91
+ slots = list;
92
+ }
93
+ return { document, slots };
94
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2026 Xue Studios LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ export type PathEntry = {
7
+ d: string;
8
+ fillRule?: "evenodd";
9
+ fill?: string;
10
+ stroke?: string;
11
+ strokeWidth?: number;
12
+ cap?: "round" | "square";
13
+ join?: "round" | "bevel";
14
+ opacity?: number;
15
+ transform?: number[];
16
+ };
17
+ export type SvgPaths = {
18
+ viewBox: number[];
19
+ paths: PathEntry[];
20
+ };
21
+ export type SvgResult = {
22
+ ok: true;
23
+ value: SvgPaths;
24
+ } | {
25
+ ok: false;
26
+ reason: string;
27
+ };
28
+ /**
29
+ * A layer 1.x drew from SVG markup: the `svg` type, or an old library icon created
30
+ * as `geo` with the SVG in its content. Its strokeColor tints the drawing.
31
+ */
32
+ export declare function drawsMarkup(slot: {
33
+ type?: unknown;
34
+ raw: {
35
+ content?: unknown;
36
+ };
37
+ }): boolean;
38
+ export declare function svgToPaths(xml: unknown): SvgResult;
@@ -0,0 +1,225 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2026 Xue Studios LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ const IDENTITY = [1, 0, 0, 1, 0, 0];
7
+ const SHAPES = new Set(["path", "rect", "circle", "ellipse", "line", "polyline", "polygon"]);
8
+ const CONTAINERS = new Set(["svg", "g"]);
9
+ /** Text for people, with nothing to draw. Its content is skipped. */
10
+ const SILENT = new Set(["title", "desc", "metadata"]);
11
+ function multiply(a, b) {
12
+ return [
13
+ a[0] * b[0] + a[2] * b[1],
14
+ a[1] * b[0] + a[3] * b[1],
15
+ a[0] * b[2] + a[2] * b[3],
16
+ a[1] * b[2] + a[3] * b[3],
17
+ a[0] * b[4] + a[2] * b[5] + a[4],
18
+ a[1] * b[4] + a[3] * b[5] + a[5],
19
+ ];
20
+ }
21
+ const numbersIn = (text) => (text.match(/-?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/gi) ?? []).map(Number);
22
+ function parseTransform(text) {
23
+ let matrix = IDENTITY;
24
+ const calls = [...text.matchAll(/([a-zA-Z]+)\s*\(([^)]*)\)/g)];
25
+ if (calls.length === 0 && text.trim() !== "")
26
+ return null;
27
+ for (const [, name, args] of calls) {
28
+ const n = numbersIn(args);
29
+ let next;
30
+ if (name === "matrix" && n.length === 6)
31
+ next = n;
32
+ else if (name === "translate" && n.length >= 1)
33
+ next = [1, 0, 0, 1, n[0], n[1] ?? 0];
34
+ else if (name === "scale" && n.length >= 1)
35
+ next = [n[0], 0, 0, n[1] ?? n[0], 0, 0];
36
+ else if (name === "rotate" && n.length >= 1) {
37
+ const r = (n[0] * Math.PI) / 180;
38
+ const turn = [Math.cos(r), Math.sin(r), -Math.sin(r), Math.cos(r), 0, 0];
39
+ next = n.length >= 3 ? multiply(multiply([1, 0, 0, 1, n[1], n[2]], turn), [1, 0, 0, 1, -n[1], -n[2]]) : turn;
40
+ }
41
+ else if (name === "skewX" && n.length === 1)
42
+ next = [1, 0, Math.tan((n[0] * Math.PI) / 180), 1, 0, 0];
43
+ else if (name === "skewY" && n.length === 1)
44
+ next = [1, Math.tan((n[0] * Math.PI) / 180), 0, 1, 0, 0];
45
+ else
46
+ return null;
47
+ matrix = multiply(matrix, next);
48
+ }
49
+ return matrix;
50
+ }
51
+ function attributesOf(text) {
52
+ const attrs = {};
53
+ for (const [, name, , double, single] of text.matchAll(/([a-zA-Z_:][\w:.-]*)\s*=\s*("([^"]*)"|'([^']*)')/g)) {
54
+ attrs[name] = double ?? single ?? "";
55
+ }
56
+ return attrs;
57
+ }
58
+ const HORIZONTAL = new Set(["x", "width", "cx", "rx", "x1", "x2"]);
59
+ const VERTICAL = new Set(["y", "height", "cy", "ry", "y1", "y2"]);
60
+ /** A length; a percentage is of the viewBox, as SVG resolves it (r: of its normalised diagonal). */
61
+ const numIn = (attrs, name, viewBox) => {
62
+ const raw = attrs[name] ?? "0";
63
+ const value = Number.parseFloat(raw);
64
+ if (!Number.isFinite(value))
65
+ return 0;
66
+ if (!raw.trim().endsWith("%") || !viewBox)
67
+ return value;
68
+ const [w, h] = [viewBox[2], viewBox[3]];
69
+ const ref = HORIZONTAL.has(name) ? w : VERTICAL.has(name) ? h : Math.sqrt((w * w + h * h) / 2);
70
+ return (value / 100) * ref;
71
+ };
72
+ /**
73
+ * A layer 1.x drew from SVG markup: the `svg` type, or an old library icon created
74
+ * as `geo` with the SVG in its content. Its strokeColor tints the drawing.
75
+ */
76
+ export function drawsMarkup(slot) {
77
+ if (slot.type === "svg")
78
+ return true;
79
+ const content = slot.raw.content;
80
+ const values = typeof content === "string" ? [content] : content && typeof content === "object" ? Object.values(content) : [];
81
+ return values.some((v) => typeof v === "string" && /<svg/i.test(v));
82
+ }
83
+ /** The path data of a basic shape. */
84
+ function shapeToPath(tag, a, vb) {
85
+ const num = (attrs, name) => numIn(attrs, name, vb);
86
+ if (tag === "path")
87
+ return a.d?.trim() || null;
88
+ if (tag === "rect") {
89
+ const [x, y, w, h] = [num(a, "x"), num(a, "y"), num(a, "width"), num(a, "height")];
90
+ if (!(w > 0) || !(h > 0))
91
+ return null;
92
+ let rx = a.rx !== undefined ? num(a, "rx") : a.ry !== undefined ? num(a, "ry") : 0;
93
+ let ry = a.ry !== undefined ? num(a, "ry") : rx;
94
+ rx = Math.min(rx, w / 2);
95
+ ry = Math.min(ry, h / 2);
96
+ if (!(rx > 0) || !(ry > 0))
97
+ return `M${x} ${y}H${x + w}V${y + h}H${x}Z`;
98
+ return (`M${x + rx} ${y}H${x + w - rx}A${rx} ${ry} 0 0 1 ${x + w} ${y + ry}V${y + h - ry}` +
99
+ `A${rx} ${ry} 0 0 1 ${x + w - rx} ${y + h}H${x + rx}A${rx} ${ry} 0 0 1 ${x} ${y + h - ry}V${y + ry}` +
100
+ `A${rx} ${ry} 0 0 1 ${x + rx} ${y}Z`);
101
+ }
102
+ if (tag === "circle" || tag === "ellipse") {
103
+ const [cx, cy] = [num(a, "cx"), num(a, "cy")];
104
+ const rx = tag === "circle" ? num(a, "r") : num(a, "rx");
105
+ const ry = tag === "circle" ? rx : num(a, "ry");
106
+ if (!(rx > 0) || !(ry > 0))
107
+ return null;
108
+ return `M${cx - rx} ${cy}A${rx} ${ry} 0 1 0 ${cx + rx} ${cy}A${rx} ${ry} 0 1 0 ${cx - rx} ${cy}Z`;
109
+ }
110
+ if (tag === "line")
111
+ return `M${num(a, "x1")} ${num(a, "y1")}L${num(a, "x2")} ${num(a, "y2")}`;
112
+ const points = numbersIn(a.points ?? "");
113
+ if (points.length < 4 || points.length % 2 !== 0)
114
+ return null;
115
+ let d = `M${points[0]} ${points[1]}`;
116
+ for (let i = 2; i < points.length; i += 2)
117
+ d += `L${points[i]} ${points[i + 1]}`;
118
+ return tag === "polygon" ? `${d}Z` : d;
119
+ }
120
+ // Presentation attributes that a child inherits from its group.
121
+ const INHERITED = ["fill", "stroke", "stroke-width", "stroke-linecap", "stroke-linejoin", "fill-rule"];
122
+ function refusal(name, attrs) {
123
+ for (const key of Object.keys(attrs)) {
124
+ const lower = key.toLowerCase();
125
+ if (lower === "style")
126
+ return `<${name}> carries a style attribute`;
127
+ if (lower.startsWith("on"))
128
+ return `<${name}> carries an event handler (${key})`;
129
+ if (lower === "href" || lower.endsWith(":href"))
130
+ return `<${name}> carries a reference (${key})`;
131
+ }
132
+ for (const key of ["fill", "stroke"]) {
133
+ if (/url\s*\(/i.test(attrs[key] ?? ""))
134
+ return `<${name}> paints with a reference (${key})`;
135
+ }
136
+ return null;
137
+ }
138
+ export function svgToPaths(xml) {
139
+ if (typeof xml !== "string" || xml.trim() === "")
140
+ return { ok: false, reason: "not an SVG string" };
141
+ const source = xml.replace(/<!--[\s\S]*?-->/g, "").replace(/<\?[\s\S]*?\?>/g, "").replace(/<!DOCTYPE[^>]*>/gi, "");
142
+ const paths = [];
143
+ const stack = [];
144
+ let viewBox;
145
+ let silent = 0;
146
+ let sawSvg = false;
147
+ for (const [, closing, name, rest, selfClosing] of source.matchAll(/<(\/?)([a-zA-Z][\w:-]*)([^>]*?)(\/?)>/g)) {
148
+ if (SILENT.has(name)) {
149
+ if (closing)
150
+ silent--;
151
+ else if (!selfClosing)
152
+ silent++;
153
+ continue;
154
+ }
155
+ if (silent > 0)
156
+ continue;
157
+ if (closing) {
158
+ stack.pop();
159
+ continue;
160
+ }
161
+ if (!CONTAINERS.has(name) && !SHAPES.has(name))
162
+ return { ok: false, reason: `<${name}> is outside the static profile` };
163
+ const attrs = attributesOf(rest);
164
+ const refused = refusal(name, attrs);
165
+ if (refused)
166
+ return { ok: false, reason: refused };
167
+ const parent = stack[stack.length - 1];
168
+ const own = attrs.transform === undefined ? IDENTITY : parseTransform(attrs.transform);
169
+ if (!own)
170
+ return { ok: false, reason: `<${name}> has a transform that cannot be read` };
171
+ const matrix = parent ? multiply(parent.matrix, own) : own;
172
+ const inherited = { ...(parent?.attrs ?? {}) };
173
+ for (const key of INHERITED)
174
+ if (attrs[key] !== undefined)
175
+ inherited[key] = attrs[key];
176
+ if (name === "svg" && !sawSvg) {
177
+ sawSvg = true;
178
+ const box = numbersIn(attrs.viewBox ?? "");
179
+ const [w, h] = [Number.parseFloat(attrs.width ?? ""), Number.parseFloat(attrs.height ?? "")];
180
+ if (box.length === 4)
181
+ viewBox = box;
182
+ else if (w > 0 && h > 0 && !/%/.test(`${attrs.width}${attrs.height}`))
183
+ viewBox = [0, 0, w, h];
184
+ }
185
+ if (SHAPES.has(name)) {
186
+ const d = shapeToPath(name, attrs, viewBox);
187
+ if (!d)
188
+ continue;
189
+ const entry = { d };
190
+ // SVG fills in black when nothing says otherwise; `none` is written out.
191
+ const fill = inherited.fill ?? "black";
192
+ if (fill !== "none")
193
+ entry.fill = fill;
194
+ const stroke = inherited.stroke;
195
+ if (stroke && stroke !== "none") {
196
+ entry.stroke = stroke;
197
+ const width = Number.parseFloat(inherited["stroke-width"] ?? "1");
198
+ entry.strokeWidth = Number.isFinite(width) ? width : 1;
199
+ const cap = inherited["stroke-linecap"];
200
+ const join = inherited["stroke-linejoin"];
201
+ if (cap === "round" || cap === "square")
202
+ entry.cap = cap;
203
+ if (join === "round" || join === "bevel")
204
+ entry.join = join;
205
+ }
206
+ if (inherited["fill-rule"] === "evenodd")
207
+ entry.fillRule = "evenodd";
208
+ const opacity = Number.parseFloat(attrs.opacity ?? "");
209
+ if (Number.isFinite(opacity) && opacity < 1)
210
+ entry.opacity = Math.max(0, opacity);
211
+ if (matrix.some((v, i) => Math.abs(v - IDENTITY[i]) > 1e-9))
212
+ entry.transform = matrix.map((v) => Number(v.toFixed(6)));
213
+ paths.push(entry);
214
+ }
215
+ if (CONTAINERS.has(name) && !selfClosing)
216
+ stack.push({ name, matrix, attrs: inherited });
217
+ }
218
+ if (!sawSvg)
219
+ return { ok: false, reason: "no <svg> element" };
220
+ if (!viewBox)
221
+ return { ok: false, reason: "the <svg> declares no viewBox nor a size in units" };
222
+ if (paths.length === 0)
223
+ return { ok: false, reason: "nothing to draw" };
224
+ return { ok: true, value: { viewBox, paths } };
225
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2026 Xue Studios LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import { type DraftLayer, type UpgradeContext, type V1Slot } from "./context.js";
7
+ export declare function convertText(ctx: UpgradeContext, slot: V1Slot, layer: DraftLayer): void;