@guilhermebkel/scenec 0.0.0-stage → 0.1.2
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/LICENSE +21 -0
- package/README.md +242 -2
- package/dist/catalog.d.ts +21 -0
- package/dist/catalog.js +35 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +118 -0
- package/dist/data/europe.json +1 -0
- package/dist/drawing/assets.d.ts +8 -0
- package/dist/drawing/assets.js +612 -0
- package/dist/drawing/kits.d.ts +7 -0
- package/dist/drawing/kits.js +92 -0
- package/dist/drawing/person.d.ts +18 -0
- package/dist/drawing/person.js +287 -0
- package/dist/drawing/themes.d.ts +4 -0
- package/dist/drawing/themes.js +45 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +2 -0
- package/dist/modes/map.d.ts +5 -0
- package/dist/modes/map.js +365 -0
- package/dist/modes/plane.d.ts +5 -0
- package/dist/modes/plane.js +716 -0
- package/dist/pipeline/compile.d.ts +2 -0
- package/dist/pipeline/compile.js +581 -0
- package/dist/pipeline/custom.d.ts +2 -0
- package/dist/pipeline/custom.js +120 -0
- package/dist/pipeline/groups.d.ts +2 -0
- package/dist/pipeline/groups.js +189 -0
- package/dist/pipeline/index.d.ts +16 -0
- package/dist/pipeline/index.js +7 -0
- package/dist/pipeline/parse.d.ts +10 -0
- package/dist/pipeline/parse.js +80 -0
- package/dist/protocols/options.d.ts +14 -0
- package/dist/protocols/options.js +1 -0
- package/dist/protocols/scene.d.ts +299 -0
- package/dist/protocols/scene.js +118 -0
- package/dist/protocols/theme.d.ts +25 -0
- package/dist/protocols/theme.js +1 -0
- package/dist/render/check.d.ts +6 -0
- package/dist/render/check.js +10 -0
- package/dist/render/critic.d.ts +1 -0
- package/dist/render/critic.js +235 -0
- package/dist/render/files.d.ts +2 -0
- package/dist/render/files.js +16 -0
- package/dist/render/fonts.d.ts +2 -0
- package/dist/render/fonts.js +4 -0
- package/dist/render/png.d.ts +9 -0
- package/dist/render/png.js +13 -0
- package/dist/render.d.ts +5 -0
- package/dist/render.js +3 -0
- package/dist/schema.d.ts +3 -0
- package/dist/schema.js +6 -0
- package/dist/utils/color.d.ts +5 -0
- package/dist/utils/color.js +35 -0
- package/dist/utils/recolor.d.ts +1 -0
- package/dist/utils/recolor.js +56 -0
- package/dist/utils/sanitize.d.ts +5 -0
- package/dist/utils/sanitize.js +169 -0
- package/dist/utils/suggest.d.ts +1 -0
- package/dist/utils/suggest.js +22 -0
- package/dist/utils/svg.d.ts +2 -0
- package/dist/utils/svg.js +11 -0
- package/fonts/OFL.txt +50 -0
- package/fonts/Poppins-Bold.ttf +0 -0
- package/fonts/Poppins-Medium.ttf +0 -0
- package/fonts/Poppins-Regular.ttf +0 -0
- package/package.json +96 -4
- package/skills/scenec/SKILL.md +46 -0
- package/skills/scenec/recipes/anuncios-variacoes.yaml +58 -0
- package/skills/scenec/recipes/carrossel.yaml +58 -0
- package/skills/scenec/recipes/elearning.yaml +64 -0
- package/skills/scenec/recipes/historia-ilustrada.yaml +48 -0
- package/skills/scenec/recipes/ilustracoes-produto.yaml +44 -0
- package/skills/scenec/recipes/mapas-jornadas.yaml +27 -0
- package/skills/scenec/recipes/reels-explicativo.yaml +41 -0
- package/skills/scenec/recipes/wrapped-personalizado.yaml +64 -0
- package/skills/scenec/references/catalog.md +125 -0
- package/skills/scenec/references/dsl.md +111 -0
- package/skills/scenec/references/fontes.md +8 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { makeRecolor } from "../utils/recolor.js";
|
|
2
|
+
import { sanitizeSvg } from "../utils/sanitize.js";
|
|
3
|
+
const KINDS = ["ground", "surface", "any", "wall", "sky", "ceiling", "floor", "water"];
|
|
4
|
+
const DEFAULT_H = { ground: 60, surface: 18, any: 40, wall: 50, sky: 50, ceiling: 30, floor: 10, water: 60 };
|
|
5
|
+
// Prefixa ids internos para não colidir com outros objetos da cena.
|
|
6
|
+
function prefixIds(svg, pre) {
|
|
7
|
+
const ids = [...svg.matchAll(/\sid="([^"]+)"/g)].map((m) => m[1]);
|
|
8
|
+
for (const id of ids) {
|
|
9
|
+
const esc = id.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
10
|
+
svg = svg.replace(new RegExp(`id="${esc}"`, "g"), `id="${pre}${id}"`)
|
|
11
|
+
.replace(new RegExp(`url\\(#${esc}\\)`, "g"), `url(#${pre}${id})`)
|
|
12
|
+
.replace(new RegExp(`href="#${esc}"`, "g"), `href="#${pre}${id}"`);
|
|
13
|
+
}
|
|
14
|
+
return svg;
|
|
15
|
+
}
|
|
16
|
+
function extname(file) {
|
|
17
|
+
const dot = file.lastIndexOf(".");
|
|
18
|
+
return dot < 0 ? "" : file.slice(dot).toLowerCase();
|
|
19
|
+
}
|
|
20
|
+
function readText(io, file) {
|
|
21
|
+
if (!io.files)
|
|
22
|
+
throw new Error("esta chamada não lê arquivos; passe o SVG inline ou use a CLI");
|
|
23
|
+
return io.files.readText(file);
|
|
24
|
+
}
|
|
25
|
+
function readBytes(io, file) {
|
|
26
|
+
if (!io.files)
|
|
27
|
+
throw new Error("esta chamada não lê arquivos; passe a imagem por um leitor de arquivos ou use a CLI");
|
|
28
|
+
return io.files.readBytes(file);
|
|
29
|
+
}
|
|
30
|
+
function toBase64(bytes) {
|
|
31
|
+
let binary = "";
|
|
32
|
+
for (let i = 0; i < bytes.length; i += 0x8000)
|
|
33
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
|
|
34
|
+
return btoa(binary);
|
|
35
|
+
}
|
|
36
|
+
function rasterSize(buf, ext) {
|
|
37
|
+
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
38
|
+
if (ext === ".png")
|
|
39
|
+
return [view.getUint32(16), view.getUint32(20)];
|
|
40
|
+
if (ext === ".jpg" || ext === ".jpeg") {
|
|
41
|
+
let i = 2;
|
|
42
|
+
while (i < buf.length) {
|
|
43
|
+
const m = buf[i + 1], len = view.getUint16(i + 2);
|
|
44
|
+
if (m >= 0xc0 && m <= 0xc3)
|
|
45
|
+
return [view.getUint16(i + 7), view.getUint16(i + 5)];
|
|
46
|
+
i += 2 + len;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
export function loadCustomAssets(defs = {}, io = {}, warnings = [], failed = new Set()) {
|
|
52
|
+
const out = {};
|
|
53
|
+
let n = 0;
|
|
54
|
+
for (const [id, d] of Object.entries(defs)) {
|
|
55
|
+
n++;
|
|
56
|
+
const kind = d.kind || "any";
|
|
57
|
+
if (!KINDS.includes(kind)) {
|
|
58
|
+
warnings.push(`asset custom '${id}': kind '${kind}' inválido (use ${KINDS.join(", ")})`);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const height = d.height ?? DEFAULT_H[kind];
|
|
62
|
+
let inner, box;
|
|
63
|
+
try {
|
|
64
|
+
if (d.svg) {
|
|
65
|
+
let src = d.svg.trim().startsWith("<") ? d.svg : readText(io, d.svg);
|
|
66
|
+
src = sanitizeSvg(src);
|
|
67
|
+
const open = src.match(/<svg[^>]*>/i);
|
|
68
|
+
if (!open)
|
|
69
|
+
throw new Error("não parece um SVG");
|
|
70
|
+
const vb = open[0].match(/viewBox="([^"]+)"/i);
|
|
71
|
+
const wAttr = open[0].match(/\swidth="([\d.]+)/), hAttr = open[0].match(/\sheight="([\d.]+)/);
|
|
72
|
+
const view = vb ? vb[1].split(/[\s,]+/).map(Number) : [0, 0, +(wAttr?.[1] || 100), +(hAttr?.[1] || 100)];
|
|
73
|
+
inner = prefixIds(src.slice(src.indexOf(open[0]) + open[0].length, src.lastIndexOf("</svg>")), `c${n}-`);
|
|
74
|
+
const probe = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="${view.join(" ")}" width="${view[2]}" height="${view[3]}">${inner}</svg>`;
|
|
75
|
+
const b = io.measure ? io.measure(probe) : { x: view[0], y: view[1], width: view[2], height: view[3] };
|
|
76
|
+
if (!b?.width)
|
|
77
|
+
throw new Error("desenho vazio");
|
|
78
|
+
box = d.trim === false ? { x: view[0], y: view[1], width: view[2], height: view[3] } : b;
|
|
79
|
+
}
|
|
80
|
+
else if (d.image) {
|
|
81
|
+
const ext = extname(d.image);
|
|
82
|
+
const buf = readBytes(io, d.image);
|
|
83
|
+
const mime = { ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".webp": "image/webp" }[ext];
|
|
84
|
+
if (!mime)
|
|
85
|
+
throw new Error(`formato ${ext} não suportado (use png, jpg ou webp)`);
|
|
86
|
+
const size = (d.size_px && [d.size_px[0], d.size_px[1]]) || rasterSize(buf, ext);
|
|
87
|
+
if (!size)
|
|
88
|
+
throw new Error("não consegui ler as dimensões; informe size_px: [largura, altura]");
|
|
89
|
+
inner = `<image width="${size[0]}" height="${size[1]}" href="data:${mime};base64,${toBase64(buf)}"/>`;
|
|
90
|
+
box = { x: 0, y: 0, width: size[0], height: size[1] };
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
warnings.push(`asset custom '${id}': informe 'svg' (arquivo ou inline) ou 'image'`);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
catch (e) {
|
|
98
|
+
const msg = e.code === "ENOENT" ? `arquivo '${d.svg || d.image}' não encontrado (caminho relativo ao arquivo da cena)` : e.message;
|
|
99
|
+
warnings.push(`asset custom '${id}': ${msg}`);
|
|
100
|
+
failed.add(id);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
const s = height / box.height;
|
|
104
|
+
const w = box.width * s;
|
|
105
|
+
const art = (p, t) => (d.recolor ? makeRecolor(t, d.recolor === true ? 0.75 : d.recolor)(inner) : inner);
|
|
106
|
+
out[id] = {
|
|
107
|
+
kind, w, h: height, custom: true,
|
|
108
|
+
top: d.top, seat: d.seat, elevation: d.elevation ?? (kind === "wall" ? 80 : undefined),
|
|
109
|
+
tags: d.tags || [],
|
|
110
|
+
draw: (p, t) => {
|
|
111
|
+
const flip = p.facing === "left" && d.facing !== "left" || p.facing === "right" && d.facing === "left" ? -1 : 1;
|
|
112
|
+
const shadow = ["ground", "any"].includes(kind) && d.shadow !== false && p.shadow !== false
|
|
113
|
+
? `<ellipse cx="0" cy="0" rx="${(w * 0.45).toFixed(1)}" ry="${Math.min(4, w * 0.05).toFixed(1)}" fill="${t.shadow}"/>`
|
|
114
|
+
: "";
|
|
115
|
+
return shadow + `<g transform="scale(${flip} 1) translate(${(-(box.x + box.width / 2) * s).toFixed(3)} ${(-(box.y + box.height) * s).toFixed(3)}) scale(${s.toFixed(5)})">${art(p, t)}</g>`;
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
return out;
|
|
120
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// Grupos prontos: um objeto `group: <nome>` vira várias peças já arrumadas entre si.
|
|
2
|
+
// Os ids gerados são <id>_<peça> (ex.: canto_pessoa), e podem ser referenciados de fora (look, on, left_of...).
|
|
3
|
+
// Parâmetros comuns: at, depth, size, flip (espelha), person/people (sobrescreve personagens),
|
|
4
|
+
// parts: { peça: {...campos} | false } para ajustar ou remover peças, color (cor principal do móvel).
|
|
5
|
+
const REL = ["left_of", "right_of", "behind", "in_front_of", "on", "under", "above", "holds", "look"];
|
|
6
|
+
export const GROUPS = {
|
|
7
|
+
// canto de trabalho: mesa, cadeira, pessoa digitando, tela, luminária, caneca e planta
|
|
8
|
+
workspace: {
|
|
9
|
+
anchor: "mesa", desc: "mesa de trabalho com pessoa digitando, tela, luminária, caneca e planta",
|
|
10
|
+
build: (g) => ({
|
|
11
|
+
mesa: { asset: "desk", at: g.at, color: g.color },
|
|
12
|
+
cadeira: { asset: "chair", left_of: "mesa", gap: -0.04, facing: "right" },
|
|
13
|
+
pessoa: { asset: "person", pose: "typing", on: "cadeira", facing: "right", ...g.person },
|
|
14
|
+
tela: g.screen === "monitor" ? { asset: "monitor", on: "mesa", align: -0.1, size: 0.8 } : { asset: "laptop", on: "mesa", align: "left", facing: "left" },
|
|
15
|
+
luminaria: { asset: "lamp", on: "mesa", align: "right" },
|
|
16
|
+
caneca: { asset: "cup", on: "mesa", align: 0.12, color: "accent" },
|
|
17
|
+
planta: { asset: "big_plant", right_of: "mesa", gap: 0.015, size: "small" },
|
|
18
|
+
}),
|
|
19
|
+
},
|
|
20
|
+
// canto de leitura: sofá, pessoa lendo, abajur de chão e prateleira acima
|
|
21
|
+
reading_corner: {
|
|
22
|
+
anchor: "sofa", desc: "sofá com pessoa lendo, abajur de chão e prateleira",
|
|
23
|
+
build: (g) => ({
|
|
24
|
+
sofa: { asset: "sofa", at: g.at, color: g.color || "secondary" },
|
|
25
|
+
pessoa: { asset: "person", pose: "reading", on: "sofa", align: "left", facing: "right", pants: "metal", ...g.person },
|
|
26
|
+
abajur: { asset: "floor_lamp", right_of: "sofa", gap: 0.005 },
|
|
27
|
+
prateleira: { asset: "shelf_wall", above: "sofa", offset: 0.18, elevation: 45 },
|
|
28
|
+
}),
|
|
29
|
+
},
|
|
30
|
+
// mesa de refeição: duas pessoas sentadas frente a frente, olhando uma para a outra
|
|
31
|
+
dining: {
|
|
32
|
+
anchor: "mesa", desc: "mesa com duas pessoas sentadas conversando, comida e bebidas",
|
|
33
|
+
build: (g) => {
|
|
34
|
+
const [a = {}, b = {}] = g.people || [];
|
|
35
|
+
return {
|
|
36
|
+
mesa: { asset: "table", at: g.at, color: g.color },
|
|
37
|
+
cadeira1: { asset: "chair", left_of: "mesa", gap: -0.004, facing: "right" },
|
|
38
|
+
cadeira2: { asset: "chair", right_of: "mesa", gap: -0.004, facing: "left" },
|
|
39
|
+
pessoa1: { asset: "person", pose: "sitting", on: "cadeira1", facing: "right", look: "pessoa2", ...a },
|
|
40
|
+
pessoa2: { asset: "person", pose: "drinking", on: "cadeira2", facing: "left", look: "pessoa1", ...b },
|
|
41
|
+
prato: { asset: g.food || "pizza", on: "mesa", align: 0 },
|
|
42
|
+
copo1: { asset: "cup", on: "mesa", align: -0.32, steam: false },
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
// conversa em pé: um fala (apresentando), o outro escuta; os dois se olham
|
|
47
|
+
conversation: {
|
|
48
|
+
anchor: "pessoa1", desc: "duas pessoas em pé conversando, olhando uma para a outra",
|
|
49
|
+
build: (g) => {
|
|
50
|
+
const [a = {}, b = {}] = g.people || [];
|
|
51
|
+
return {
|
|
52
|
+
pessoa1: { asset: "person", at: g.at, pose: "presenting", facing: "right", look: "pessoa2", expr: "happy", ...a },
|
|
53
|
+
pessoa2: { asset: "person", right_of: "pessoa1", gap: g.gap ?? 0.04, pose: "standing", facing: "left", look: "pessoa1", expr: "neutral", ...b },
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
// banco de praça: banco, árvore, pessoa sentada lendo e cachorro
|
|
58
|
+
park_bench: {
|
|
59
|
+
anchor: "banco", desc: "banco de praça com árvore, pessoa sentada e cachorro",
|
|
60
|
+
build: (g) => ({
|
|
61
|
+
banco: { asset: "bench", at: g.at, color: g.color },
|
|
62
|
+
arvore: { asset: "tree", right_of: "banco", gap: -0.02, depth: "mid" },
|
|
63
|
+
pessoa: { asset: "person", pose: "reading", on: "banco", align: "left", facing: "right", ...g.person },
|
|
64
|
+
cachorro: { asset: "dog", left_of: "banco", gap: 0.01, facing: "right", look: "pessoa" },
|
|
65
|
+
}),
|
|
66
|
+
},
|
|
67
|
+
// comemoração: mesa com bolo, balões e duas pessoas festejando
|
|
68
|
+
celebration: {
|
|
69
|
+
anchor: "mesa", desc: "mesa com bolo, balões e duas pessoas comemorando",
|
|
70
|
+
build: (g) => {
|
|
71
|
+
const [a = {}, b = {}] = g.people || [];
|
|
72
|
+
return {
|
|
73
|
+
mesa: { asset: "table", at: g.at, color: g.color },
|
|
74
|
+
bolo: { asset: "cake", on: "mesa", size: 1.4 },
|
|
75
|
+
baloes: { asset: "balloons", right_of: "mesa", gap: 0.01 },
|
|
76
|
+
pessoa1: { asset: "person", left_of: "mesa", gap: 0.01, pose: "celebrating", expr: "laughing", look: "bolo", ...a },
|
|
77
|
+
pessoa2: { asset: "person", right_of: "baloes", gap: 0.0, pose: "waving", expr: "happy", facing: "left", look: "pessoa1", ...b },
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
// reunião: quadro na parede, uma pessoa apresenta e duas assistem sentadas
|
|
82
|
+
meeting: {
|
|
83
|
+
anchor: "mesa", desc: "mesa de reunião com quadro, uma pessoa apresentando e duas sentadas",
|
|
84
|
+
build: (g) => {
|
|
85
|
+
const [a = {}, b = {}, c = {}] = g.people || [];
|
|
86
|
+
return {
|
|
87
|
+
quadro: { asset: "board", above: "mesa", offset: 0.1, elevation: 70 },
|
|
88
|
+
mesa: { asset: "desk", at: g.at, color: g.color },
|
|
89
|
+
cadeira1: { asset: "chair", left_of: "mesa", gap: -0.03, facing: "right" },
|
|
90
|
+
cadeira2: { asset: "chair", right_of: "mesa", gap: -0.03, facing: "left" },
|
|
91
|
+
pessoa1: { asset: "person", pose: "sitting", on: "cadeira1", facing: "right", look: "quadro", ...a },
|
|
92
|
+
pessoa2: { asset: "person", behind: "mesa", offset: 0.05, layer: "back", pose: "presenting", look: "pessoa1", ...b },
|
|
93
|
+
pessoa3: { asset: "person", pose: "sitting", on: "cadeira2", facing: "left", look: "pessoa2", ...c },
|
|
94
|
+
notebook: { asset: "laptop", on: "mesa", align: "left", facing: "left" },
|
|
95
|
+
};
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
const SIZES = { tiny: 0.5, small: 0.75, medium: 1, large: 1.3, huge: 1.7 };
|
|
100
|
+
const flipSide = (v) => (v === "left" ? "right" : v === "right" ? "left" : v);
|
|
101
|
+
export function expandGroups(objs, warnings) {
|
|
102
|
+
const out = {}, alias = {};
|
|
103
|
+
for (const [id, o] of Object.entries(objs || {})) {
|
|
104
|
+
if (!o?.group) {
|
|
105
|
+
out[id] = o;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const G = GROUPS[o.group];
|
|
109
|
+
if (!G) {
|
|
110
|
+
warnings.push(`grupo '${o.group}' (em '${id}') não existe; disponíveis: ${Object.keys(GROUPS).join(", ")}`);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const parts = G.build({ ...o, at: o.at ?? 0.5 });
|
|
114
|
+
const removed = new Set();
|
|
115
|
+
for (const [k, v] of Object.entries(o.parts || {})) {
|
|
116
|
+
if (v === false) {
|
|
117
|
+
delete parts[k];
|
|
118
|
+
removed.add(k);
|
|
119
|
+
}
|
|
120
|
+
else if (parts[k]) {
|
|
121
|
+
parts[k] = { ...parts[k], ...v };
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
parts[k] = v;
|
|
125
|
+
} // peça extra
|
|
126
|
+
}
|
|
127
|
+
const pre = (ref) => (typeof ref === "string" && parts[ref] ? `${id}_${ref}` : removed.has(ref) ? undefined : ref);
|
|
128
|
+
for (const [k, p] of Object.entries(parts)) {
|
|
129
|
+
const q = { ...p };
|
|
130
|
+
for (const r of REL) {
|
|
131
|
+
if (q[r] != null) {
|
|
132
|
+
q[r] = Array.isArray(q[r]) ? q[r].map(pre) : pre(q[r]);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
for (const key of Object.keys(q)) {
|
|
136
|
+
if (q[key] === undefined) {
|
|
137
|
+
delete q[key];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (o.depth && !q.on && !q.depth) {
|
|
141
|
+
q.depth = o.depth;
|
|
142
|
+
}
|
|
143
|
+
// tamanho do grupo: só para peças que não herdam escala (as apoiadas/seguradas herdam do suporte)
|
|
144
|
+
if (o.size != null && !q.on && !q.holds) {
|
|
145
|
+
const k0 = typeof q.size === "number" ? q.size : SIZES[q.size] ?? 1;
|
|
146
|
+
q.size = k0 * (typeof o.size === "number" ? o.size : SIZES[o.size] ?? 1);
|
|
147
|
+
}
|
|
148
|
+
if (o.flip) {
|
|
149
|
+
if (q.left_of || q.right_of) {
|
|
150
|
+
const l = q.left_of;
|
|
151
|
+
q.left_of = q.right_of;
|
|
152
|
+
q.right_of = l;
|
|
153
|
+
if (q.left_of == null) {
|
|
154
|
+
delete q.left_of;
|
|
155
|
+
}
|
|
156
|
+
if (q.right_of == null) {
|
|
157
|
+
delete q.right_of;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
q.facing = flipSide(q.facing);
|
|
161
|
+
if (typeof q.align === "number") {
|
|
162
|
+
q.align = -q.align;
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
q.align = flipSide(q.align);
|
|
166
|
+
}
|
|
167
|
+
if (typeof q.offset === "number") {
|
|
168
|
+
q.offset = -q.offset;
|
|
169
|
+
}
|
|
170
|
+
for (const key of ["facing", "align"]) {
|
|
171
|
+
if (q[key] == null) {
|
|
172
|
+
delete q[key];
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
out[`${id}_${k}`] = q;
|
|
177
|
+
}
|
|
178
|
+
alias[id] = `${id}_${G.anchor}`;
|
|
179
|
+
}
|
|
180
|
+
// referências ao id do grupo apontam para a peça âncora
|
|
181
|
+
for (const o of Object.values(out)) {
|
|
182
|
+
for (const r of REL) {
|
|
183
|
+
if (typeof o[r] === "string" && alias[o[r]]) {
|
|
184
|
+
o[r] = alias[o[r]];
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return out;
|
|
189
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CompileOptions } from "../protocols/options.ts";
|
|
2
|
+
export interface LayoutEntry {
|
|
3
|
+
id: string;
|
|
4
|
+
asset: string;
|
|
5
|
+
x: number;
|
|
6
|
+
base: number;
|
|
7
|
+
w: number;
|
|
8
|
+
h: number;
|
|
9
|
+
}
|
|
10
|
+
export interface CompileResult {
|
|
11
|
+
svg: string;
|
|
12
|
+
warnings: string[];
|
|
13
|
+
description: string;
|
|
14
|
+
layout?: LayoutEntry[];
|
|
15
|
+
}
|
|
16
|
+
export declare function compile(input: unknown, options?: CompileOptions): CompileResult;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { compileInternal } from "./compile.js";
|
|
2
|
+
import { parseScene } from "./parse.js";
|
|
3
|
+
export function compile(input, options = {}) {
|
|
4
|
+
const { scene, warnings } = parseScene(input);
|
|
5
|
+
const { critiqueInput: _ignored, ...result } = compileInternal(scene, options);
|
|
6
|
+
return { ...result, warnings: [...warnings, ...result.warnings] };
|
|
7
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Scene } from "../protocols/scene.ts";
|
|
2
|
+
export declare class SceneError extends Error {
|
|
3
|
+
readonly errors: string[];
|
|
4
|
+
constructor(errors: string[]);
|
|
5
|
+
}
|
|
6
|
+
export interface ParsedScene {
|
|
7
|
+
scene: Scene;
|
|
8
|
+
warnings: string[];
|
|
9
|
+
}
|
|
10
|
+
export declare function parseScene(input: unknown): ParsedScene;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import * as yaml from "js-yaml";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { canvasSchema, objectSchema, OBJECT_PARAM_KEYS, MAX_OBJECTS, SIZE_NAMES, SLOT_NAMES, sceneSchema, textSchema } from "../protocols/scene.js";
|
|
4
|
+
import { closest } from "../utils/suggest.js";
|
|
5
|
+
export class SceneError extends Error {
|
|
6
|
+
errors;
|
|
7
|
+
constructor(errors) {
|
|
8
|
+
super(errors.join("\n"));
|
|
9
|
+
this.name = "SceneError";
|
|
10
|
+
this.errors = errors;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function shapeKeys(schema) {
|
|
14
|
+
return Object.keys(schema.shape);
|
|
15
|
+
}
|
|
16
|
+
function unknownKeys(value, known, where) {
|
|
17
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
return Object.keys(value).filter((key) => !known.includes(key)).map((key) => {
|
|
21
|
+
const [first] = closest(key, known, 1);
|
|
22
|
+
return first
|
|
23
|
+
? `campo '${key}' desconhecido em ${where}; você quis dizer '${first}'?`
|
|
24
|
+
: `campo '${key}' desconhecido em ${where}; ele será ignorado`;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
const UNION_FIELD_TO_NAMES = {
|
|
28
|
+
size: SIZE_NAMES,
|
|
29
|
+
at: SLOT_NAMES,
|
|
30
|
+
frame: ["left", "center", "right"],
|
|
31
|
+
};
|
|
32
|
+
const OBJECT_KEYS = [...shapeKeys(objectSchema), ...OBJECT_PARAM_KEYS];
|
|
33
|
+
function checkKnownKeys(scene) {
|
|
34
|
+
const warnings = unknownKeys(scene.canvas, shapeKeys(canvasSchema), "canvas");
|
|
35
|
+
for (const [id, object] of Object.entries(scene.objects ?? {})) {
|
|
36
|
+
warnings.push(...unknownKeys(object, OBJECT_KEYS, `objects.${id}`));
|
|
37
|
+
}
|
|
38
|
+
(scene.text ?? []).forEach((text, index) => {
|
|
39
|
+
warnings.push(...unknownKeys(text, shapeKeys(textSchema), `text[${index}]`));
|
|
40
|
+
});
|
|
41
|
+
return warnings;
|
|
42
|
+
}
|
|
43
|
+
function valueAt(root, path) {
|
|
44
|
+
let current = root;
|
|
45
|
+
for (const key of path) {
|
|
46
|
+
current = typeof current === "object" && current !== null ? current[key] : undefined;
|
|
47
|
+
}
|
|
48
|
+
return current;
|
|
49
|
+
}
|
|
50
|
+
function describeIssue(path, message, values, received) {
|
|
51
|
+
const where = path.length ? path.map(String).join(".") : "(cena)";
|
|
52
|
+
const options = values?.map(String) ?? [];
|
|
53
|
+
const [suggestion] = typeof received === "string" ? closest(received, options, 1) : [];
|
|
54
|
+
const hint = suggestion ? `; você quis dizer '${suggestion}'?` : options.length ? `; valores aceitos: ${options.join(", ")}` : "";
|
|
55
|
+
return `${where}: ${message}${hint}`;
|
|
56
|
+
}
|
|
57
|
+
export function parseScene(input) {
|
|
58
|
+
let raw = input;
|
|
59
|
+
if (typeof input === "string") {
|
|
60
|
+
try {
|
|
61
|
+
raw = yaml.load(input);
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
throw new SceneError([`YAML inválido: ${error instanceof Error ? error.message : String(error)}`]);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const result = sceneSchema.safeParse(raw, { error: z.locales.pt().localeError });
|
|
68
|
+
if (!result.success) {
|
|
69
|
+
throw new SceneError(result.error.issues.map((issue) => {
|
|
70
|
+
const field = String(issue.path.at(-1));
|
|
71
|
+
const values = issue.code === "invalid_union" ? UNION_FIELD_TO_NAMES[field] : "values" in issue ? issue.values : undefined;
|
|
72
|
+
return describeIssue(issue.path, issue.message, values, valueAt(raw, issue.path));
|
|
73
|
+
}));
|
|
74
|
+
}
|
|
75
|
+
const count = Object.keys(result.data.objects ?? {}).length;
|
|
76
|
+
if (count > MAX_OBJECTS) {
|
|
77
|
+
throw new SceneError([`objects: ${count} objetos; o limite é ${MAX_OBJECTS}`]);
|
|
78
|
+
}
|
|
79
|
+
return { scene: result.data, warnings: checkKnownKeys(result.data) };
|
|
80
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface FileReader {
|
|
2
|
+
readText: (file: string) => string;
|
|
3
|
+
readBytes: (file: string) => Uint8Array;
|
|
4
|
+
}
|
|
5
|
+
export interface BBox {
|
|
6
|
+
x: number;
|
|
7
|
+
y: number;
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
}
|
|
11
|
+
export interface CompileOptions {
|
|
12
|
+
files?: FileReader;
|
|
13
|
+
measure?: (svg: string) => BBox | null;
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|