@clypra-studio/engine 1.4.0 → 1.5.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/dist/{chunk-E6O6LZME.js → chunk-7ORA5BPW.js} +14 -299
- package/dist/chunk-7ORA5BPW.js.map +1 -0
- package/dist/chunk-NG4F55V7.js +300 -0
- package/dist/chunk-NG4F55V7.js.map +1 -0
- package/dist/chunk-Q7XYDP4X.js +1898 -0
- package/dist/chunk-Q7XYDP4X.js.map +1 -0
- package/dist/{chunk-3UEOLDJJ.js → chunk-SH7HOGVX.js} +1 -1
- package/dist/chunk-SH7HOGVX.js.map +1 -0
- package/dist/index-B1lFl4p2.d.ts +122 -0
- package/dist/index-Cpps1DKP.d.ts +489 -0
- package/dist/index-DrzRbR7G.d.cts +122 -0
- package/dist/index-WsvrOpAy.d.cts +489 -0
- package/dist/index.cjs +3131 -482
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +185 -103
- package/dist/index.d.ts +185 -103
- package/dist/index.js +1599 -792
- package/dist/index.js.map +1 -1
- package/dist/rendering/index.cjs +3694 -0
- package/dist/rendering/index.cjs.map +1 -0
- package/dist/rendering/index.d.cts +2 -0
- package/dist/rendering/index.d.ts +2 -0
- package/dist/rendering/index.js +21 -0
- package/dist/rendering/index.js.map +1 -0
- package/dist/textEffects/index.cjs +13 -5
- package/dist/textEffects/index.cjs.map +1 -1
- package/dist/textEffects/index.d.cts +2 -1
- package/dist/textEffects/index.d.ts +2 -1
- package/dist/textEffects/index.js +6 -4
- package/dist/{index-C18SQ-y6.d.cts → types-IYwXGYkB.d.cts} +141 -129
- package/dist/{index-C18SQ-y6.d.ts → types-IYwXGYkB.d.ts} +141 -129
- package/dist/videoEffects/index.cjs.map +1 -1
- package/dist/videoEffects/index.js +1 -1
- package/package.json +6 -1
- package/dist/chunk-3UEOLDJJ.js.map +0 -1
- package/dist/chunk-E6O6LZME.js.map +0 -1
|
@@ -0,0 +1,1898 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SCENE_SCHEMA_VERSION,
|
|
3
|
+
_buildConfig,
|
|
4
|
+
applyTimelineAtTime,
|
|
5
|
+
evaluateConfig,
|
|
6
|
+
evaluateScene,
|
|
7
|
+
sceneToConfig,
|
|
8
|
+
textEffectConfigToScene
|
|
9
|
+
} from "./chunk-7ORA5BPW.js";
|
|
10
|
+
|
|
11
|
+
// src/templates/keyframes.ts
|
|
12
|
+
function cubicBezier(x1, y1, x2, y2) {
|
|
13
|
+
return function(t) {
|
|
14
|
+
if (t <= 0) return 0;
|
|
15
|
+
if (t >= 1) return 1;
|
|
16
|
+
let tApprox = t;
|
|
17
|
+
for (let i = 0; i < 8; i++) {
|
|
18
|
+
const currentX = 3 * (1 - tApprox) * (1 - tApprox) * tApprox * x1 + 3 * (1 - tApprox) * tApprox * tApprox * x2 + tApprox * tApprox * tApprox;
|
|
19
|
+
const slope = 3 * (1 - tApprox) * (1 - tApprox) * x1 + 6 * (1 - tApprox) * tApprox * (x2 - x1) + 3 * tApprox * tApprox * (1 - x2);
|
|
20
|
+
if (Math.abs(slope) < 1e-6) break;
|
|
21
|
+
tApprox -= (currentX - t) / slope;
|
|
22
|
+
}
|
|
23
|
+
tApprox = Math.max(0, Math.min(1, tApprox));
|
|
24
|
+
return 3 * (1 - tApprox) * (1 - tApprox) * tApprox * y1 + 3 * (1 - tApprox) * tApprox * tApprox * y2 + tApprox * tApprox * tApprox;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function solveSpring(t, params) {
|
|
28
|
+
const mass = params?.mass ?? 1;
|
|
29
|
+
const stiffness = params?.stiffness ?? 100;
|
|
30
|
+
const damping = params?.damping ?? 10;
|
|
31
|
+
const omega0 = Math.sqrt(stiffness / mass);
|
|
32
|
+
const zeta = damping / (2 * Math.sqrt(stiffness * mass));
|
|
33
|
+
if (zeta < 1) {
|
|
34
|
+
const omegaD = omega0 * Math.sqrt(1 - zeta * zeta);
|
|
35
|
+
const envelope = Math.exp(-zeta * omega0 * t);
|
|
36
|
+
return 1 - envelope * (Math.cos(omegaD * t) + zeta / Math.sqrt(1 - zeta * zeta) * Math.sin(omegaD * t));
|
|
37
|
+
} else {
|
|
38
|
+
return 1 - (1 + omega0 * t) * Math.exp(-omega0 * t);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function easeOutBounce(t) {
|
|
42
|
+
const n1 = 7.5625;
|
|
43
|
+
const d1 = 2.75;
|
|
44
|
+
if (t < 1 / d1) {
|
|
45
|
+
return n1 * t * t;
|
|
46
|
+
} else if (t < 2 / d1) {
|
|
47
|
+
let tVal = t - 1.5 / d1;
|
|
48
|
+
return n1 * tVal * tVal + 0.75;
|
|
49
|
+
} else if (t < 2.5 / d1) {
|
|
50
|
+
let tVal = t - 2.25 / d1;
|
|
51
|
+
return n1 * tVal * tVal + 0.9375;
|
|
52
|
+
} else {
|
|
53
|
+
let tVal = t - 2.625 / d1;
|
|
54
|
+
return n1 * tVal * tVal + 0.984375;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function isKeyframed(value) {
|
|
58
|
+
return typeof value === "object" && value !== null && "keyframes" in value && Array.isArray(value.keyframes);
|
|
59
|
+
}
|
|
60
|
+
function evaluateAnimatable(value, time, templateDuration) {
|
|
61
|
+
if (!isKeyframed(value)) {
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
const { keyframes } = value;
|
|
65
|
+
if (keyframes.length === 0) {
|
|
66
|
+
throw new Error("Keyframes array cannot be empty");
|
|
67
|
+
}
|
|
68
|
+
if (keyframes.length === 1) {
|
|
69
|
+
return keyframes[0].value;
|
|
70
|
+
}
|
|
71
|
+
const sorted = [...keyframes].sort((a, b) => a.time - b.time);
|
|
72
|
+
if (time <= sorted[0].time) {
|
|
73
|
+
return sorted[0].value;
|
|
74
|
+
}
|
|
75
|
+
if (time >= sorted[sorted.length - 1].time) {
|
|
76
|
+
return sorted[sorted.length - 1].value;
|
|
77
|
+
}
|
|
78
|
+
let leftIdx = 0;
|
|
79
|
+
for (let i = 0; i < sorted.length - 1; i++) {
|
|
80
|
+
if (time >= sorted[i].time && time <= sorted[i + 1].time) {
|
|
81
|
+
leftIdx = i;
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const left = sorted[leftIdx];
|
|
86
|
+
const right = sorted[leftIdx + 1];
|
|
87
|
+
const range = right.time - left.time;
|
|
88
|
+
const t = range === 0 ? 0 : (time - left.time) / range;
|
|
89
|
+
const easedT = applyEasing(t, right.easing || "linear", right.bezier, right.spring);
|
|
90
|
+
return interpolateValue(left.value, right.value, easedT);
|
|
91
|
+
}
|
|
92
|
+
function applyEasing(t, easing, bezier, spring) {
|
|
93
|
+
switch (easing) {
|
|
94
|
+
case "linear":
|
|
95
|
+
return t;
|
|
96
|
+
case "ease-in":
|
|
97
|
+
return t * t;
|
|
98
|
+
case "ease-out":
|
|
99
|
+
return t * (2 - t);
|
|
100
|
+
case "ease-in-out":
|
|
101
|
+
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
|
|
102
|
+
case "ease":
|
|
103
|
+
return cubicBezier(0.25, 0.1, 0.25, 1)(t);
|
|
104
|
+
case "cubic-bezier":
|
|
105
|
+
if (bezier) {
|
|
106
|
+
return cubicBezier(bezier.x1, bezier.y1, bezier.x2, bezier.y2)(t);
|
|
107
|
+
}
|
|
108
|
+
return cubicBezier(0.4, 0, 0.2, 1)(t);
|
|
109
|
+
case "spring":
|
|
110
|
+
return solveSpring(t, spring);
|
|
111
|
+
case "bounce":
|
|
112
|
+
return easeOutBounce(t);
|
|
113
|
+
default:
|
|
114
|
+
return t;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function interpolateValue(from, to, t) {
|
|
118
|
+
if (typeof from === "number" && typeof to === "number") {
|
|
119
|
+
return from + (to - from) * t;
|
|
120
|
+
}
|
|
121
|
+
if (typeof from === "string" && typeof to === "string") {
|
|
122
|
+
const isFromColor = from.startsWith("#") || from.startsWith("rgb");
|
|
123
|
+
const isToColor = to.startsWith("#") || to.startsWith("rgb");
|
|
124
|
+
if (isFromColor && isToColor) {
|
|
125
|
+
return interpolateColorString(from, to, t);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return t < 0.5 ? from : to;
|
|
129
|
+
}
|
|
130
|
+
function parseColor(color) {
|
|
131
|
+
if (!color) return null;
|
|
132
|
+
const trimmed = color.trim().toLowerCase();
|
|
133
|
+
if (trimmed.startsWith("#")) {
|
|
134
|
+
const hex = trimmed.slice(1);
|
|
135
|
+
if (hex.length === 3) {
|
|
136
|
+
return {
|
|
137
|
+
r: parseInt(hex[0] + hex[0], 16),
|
|
138
|
+
g: parseInt(hex[1] + hex[1], 16),
|
|
139
|
+
b: parseInt(hex[2] + hex[2], 16),
|
|
140
|
+
a: 1
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
if (hex.length === 4) {
|
|
144
|
+
return {
|
|
145
|
+
r: parseInt(hex[0] + hex[0], 16),
|
|
146
|
+
g: parseInt(hex[1] + hex[1], 16),
|
|
147
|
+
b: parseInt(hex[2] + hex[2], 16),
|
|
148
|
+
a: parseInt(hex[3] + hex[3], 16) / 255
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
if (hex.length === 6) {
|
|
152
|
+
return {
|
|
153
|
+
r: parseInt(hex.slice(0, 2), 16),
|
|
154
|
+
g: parseInt(hex.slice(2, 4), 16),
|
|
155
|
+
b: parseInt(hex.slice(4, 6), 16),
|
|
156
|
+
a: 1
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
if (hex.length === 8) {
|
|
160
|
+
return {
|
|
161
|
+
r: parseInt(hex.slice(0, 2), 16),
|
|
162
|
+
g: parseInt(hex.slice(2, 4), 16),
|
|
163
|
+
b: parseInt(hex.slice(4, 6), 16),
|
|
164
|
+
a: parseInt(hex.slice(6, 8), 16) / 255
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const match = trimmed.match(/^rgba?\s*\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)(?:\s*,\s*([\d.]+))?\s*\)$/);
|
|
169
|
+
if (match) {
|
|
170
|
+
return {
|
|
171
|
+
r: Math.min(255, Math.max(0, parseFloat(match[1]))),
|
|
172
|
+
g: Math.min(255, Math.max(0, parseFloat(match[2]))),
|
|
173
|
+
b: Math.min(255, Math.max(0, parseFloat(match[3]))),
|
|
174
|
+
a: match[4] !== void 0 ? Math.min(1, Math.max(0, parseFloat(match[4]))) : 1
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
function interpolateColorString(from, to, t) {
|
|
180
|
+
const c1 = parseColor(from);
|
|
181
|
+
const c2 = parseColor(to);
|
|
182
|
+
if (!c1 || !c2) {
|
|
183
|
+
return t < 0.5 ? from : to;
|
|
184
|
+
}
|
|
185
|
+
const r = Math.round(c1.r + (c2.r - c1.r) * t);
|
|
186
|
+
const g = Math.round(c1.g + (c2.g - c1.g) * t);
|
|
187
|
+
const b = Math.round(c1.b + (c2.b - c1.b) * t);
|
|
188
|
+
const a = Math.round((c1.a + (c2.a - c1.a) * t) * 100) / 100;
|
|
189
|
+
if (a >= 1) {
|
|
190
|
+
return `#${r.toString(16).padStart(2, "0")}${g.toString(16).padStart(2, "0")}${b.toString(16).padStart(2, "0")}`;
|
|
191
|
+
}
|
|
192
|
+
return `rgba(${r}, ${g}, ${b}, ${a})`;
|
|
193
|
+
}
|
|
194
|
+
function createKeyframed(keyframes) {
|
|
195
|
+
return { keyframes };
|
|
196
|
+
}
|
|
197
|
+
function addKeyframe(value, time, newValue, easing = "ease-in-out", bezier, spring) {
|
|
198
|
+
const existing = isKeyframed(value) ? value.keyframes : [{ time: 0, value, easing: "linear" }];
|
|
199
|
+
const existingIndex = existing.findIndex((kf) => Math.abs(kf.time - time) < 0.01);
|
|
200
|
+
const newKf = { time, value: newValue, easing, bezier, spring };
|
|
201
|
+
if (existingIndex >= 0) {
|
|
202
|
+
const updated = [...existing];
|
|
203
|
+
updated[existingIndex] = newKf;
|
|
204
|
+
return { keyframes: updated.sort((a, b) => a.time - b.time) };
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
keyframes: [...existing, newKf].sort((a, b) => a.time - b.time)
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
function removeTemplateKeyframe(value, time) {
|
|
211
|
+
if (!isKeyframed(value)) {
|
|
212
|
+
return value;
|
|
213
|
+
}
|
|
214
|
+
const filtered = value.keyframes.filter((kf) => Math.abs(kf.time - time) >= 0.01);
|
|
215
|
+
if (filtered.length === 0) {
|
|
216
|
+
throw new Error("Cannot remove all keyframes");
|
|
217
|
+
}
|
|
218
|
+
if (filtered.length === 1) {
|
|
219
|
+
return filtered[0].value;
|
|
220
|
+
}
|
|
221
|
+
return { keyframes: filtered };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// src/contracts.ts
|
|
225
|
+
var CANONICAL_SCHEMA_VERSION = 2;
|
|
226
|
+
var CANONICAL_RENDERER_VERSION = "1.4.0";
|
|
227
|
+
function clone(value) {
|
|
228
|
+
return JSON.parse(JSON.stringify(value));
|
|
229
|
+
}
|
|
230
|
+
function stableStringify(value) {
|
|
231
|
+
if (value === null || typeof value !== "object") return JSON.stringify(value);
|
|
232
|
+
if (Array.isArray(value)) return `[${value.map(stableStringify).join(",")}]`;
|
|
233
|
+
const record = value;
|
|
234
|
+
return `{${Object.keys(record).sort().map((key) => `${JSON.stringify(key)}:${stableStringify(record[key])}`).join(",")}}`;
|
|
235
|
+
}
|
|
236
|
+
function canonicalContentHash(value) {
|
|
237
|
+
const input = stableStringify(value);
|
|
238
|
+
let hash = 2166136261;
|
|
239
|
+
for (let i = 0; i < input.length; i += 1) {
|
|
240
|
+
hash ^= input.charCodeAt(i);
|
|
241
|
+
hash = Math.imul(hash, 16777619);
|
|
242
|
+
}
|
|
243
|
+
return `fnv1a-${(hash >>> 0).toString(16).padStart(8, "0")}`;
|
|
244
|
+
}
|
|
245
|
+
function stableLayerId(layer, index, counts) {
|
|
246
|
+
if (layer.id && !/^layer-[a-z0-9-]+$/i.test(layer.id)) return layer.id;
|
|
247
|
+
const count = (counts.get(layer.type) ?? 0) + 1;
|
|
248
|
+
counts.set(layer.type, count);
|
|
249
|
+
return count === 1 ? layer.type : `${layer.type}-${count}`;
|
|
250
|
+
}
|
|
251
|
+
function canonicalizeSceneDocument(input) {
|
|
252
|
+
const doc = clone(input);
|
|
253
|
+
const legacyDoc = doc;
|
|
254
|
+
delete legacyDoc.customEngineId;
|
|
255
|
+
delete legacyDoc.engineParams;
|
|
256
|
+
if (doc.legacyConfig) {
|
|
257
|
+
const legacyConfig = doc.legacyConfig;
|
|
258
|
+
for (const key of [
|
|
259
|
+
"customRenderer",
|
|
260
|
+
"inkColor",
|
|
261
|
+
"bristleDensity",
|
|
262
|
+
"bristleSkipRate",
|
|
263
|
+
"dripRate",
|
|
264
|
+
"dripMaxLength",
|
|
265
|
+
"grainDensity",
|
|
266
|
+
"skewX"
|
|
267
|
+
])
|
|
268
|
+
delete legacyConfig[key];
|
|
269
|
+
}
|
|
270
|
+
const idMap = /* @__PURE__ */ new Map();
|
|
271
|
+
const counts = /* @__PURE__ */ new Map();
|
|
272
|
+
const usedIds = /* @__PURE__ */ new Set();
|
|
273
|
+
const effectLayers = doc.effectLayers.filter((layer) => layer.type !== "customEngine").map((layer) => {
|
|
274
|
+
let id = stableLayerId(layer, 0, counts);
|
|
275
|
+
let suffix = 2;
|
|
276
|
+
while (usedIds.has(id)) id = `${id}-${suffix++}`;
|
|
277
|
+
usedIds.add(id);
|
|
278
|
+
const effectiveEnabled = layer.enabled !== false && layer.params.enabled !== false;
|
|
279
|
+
idMap.set(layer.id, id);
|
|
280
|
+
return {
|
|
281
|
+
...layer,
|
|
282
|
+
id,
|
|
283
|
+
enabled: effectiveEnabled,
|
|
284
|
+
opacity: Number.isFinite(layer.opacity) ? layer.opacity : 1,
|
|
285
|
+
params: { ...layer.params, enabled: effectiveEnabled }
|
|
286
|
+
};
|
|
287
|
+
});
|
|
288
|
+
const timeline = {
|
|
289
|
+
...doc.timeline,
|
|
290
|
+
tracks: doc.timeline.tracks.map((track) => ({
|
|
291
|
+
...track,
|
|
292
|
+
layerId: idMap.get(track.layerId) ?? track.layerId
|
|
293
|
+
}))
|
|
294
|
+
};
|
|
295
|
+
return {
|
|
296
|
+
...doc,
|
|
297
|
+
schemaVersion: SCENE_SCHEMA_VERSION,
|
|
298
|
+
effectLayers,
|
|
299
|
+
timeline
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
function effectDefinitionToConfig(input) {
|
|
303
|
+
const raw = input;
|
|
304
|
+
const fontSize = Number(raw.fontSize ?? 100);
|
|
305
|
+
return {
|
|
306
|
+
..._buildConfig(
|
|
307
|
+
input,
|
|
308
|
+
raw.text ?? "CLYPRA",
|
|
309
|
+
fontSize,
|
|
310
|
+
Number(raw.canvasWidth ?? 800),
|
|
311
|
+
Number(raw.canvasHeight ?? 200)
|
|
312
|
+
),
|
|
313
|
+
effectName: input.name,
|
|
314
|
+
textPosX: "center",
|
|
315
|
+
textPosY: "middle"
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
function normalizeTextEffect(input, metadata) {
|
|
319
|
+
if (!input || typeof input !== "object")
|
|
320
|
+
throw new Error("Invalid text effect document");
|
|
321
|
+
const raw = input;
|
|
322
|
+
const scene = Array.isArray(raw.effectLayers) ? canonicalizeSceneDocument(raw) : canonicalizeSceneDocument(
|
|
323
|
+
textEffectConfigToScene(
|
|
324
|
+
raw.font && Array.isArray(raw.fills) ? effectDefinitionToConfig(raw) : raw
|
|
325
|
+
)
|
|
326
|
+
);
|
|
327
|
+
const content = { ...scene, revision: void 0 };
|
|
328
|
+
if (metadata?.assetId) {
|
|
329
|
+
scene.revision = {
|
|
330
|
+
assetId: metadata.assetId,
|
|
331
|
+
revisionId: metadata.revisionId ?? "draft",
|
|
332
|
+
schemaVersion: CANONICAL_SCHEMA_VERSION,
|
|
333
|
+
contentHash: metadata.contentHash ?? canonicalContentHash(content),
|
|
334
|
+
rendererVersion: metadata.rendererVersion ?? CANONICAL_RENDERER_VERSION,
|
|
335
|
+
createdAt: metadata.createdAt ?? (/* @__PURE__ */ new Date()).toISOString()
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
return scene;
|
|
339
|
+
}
|
|
340
|
+
function normalizeTemplateLayer(layer, index) {
|
|
341
|
+
const normalized = clone(layer);
|
|
342
|
+
normalized.id = normalized.id || `${normalized.kind}-${index + 1}`;
|
|
343
|
+
if (normalized.kind === "text") {
|
|
344
|
+
const text = normalized;
|
|
345
|
+
if (!text.styleRef && text.styleDefinition) {
|
|
346
|
+
const snapshot = normalizeTextEffect(text.styleDefinition, {
|
|
347
|
+
assetId: text.styleId ?? "legacy-effect"
|
|
348
|
+
});
|
|
349
|
+
text.styleRef = {
|
|
350
|
+
effectId: text.styleId ?? snapshot.effectName,
|
|
351
|
+
revisionId: String(text.styleVersion ?? "legacy"),
|
|
352
|
+
contentHash: canonicalContentHash({ ...snapshot, revision: void 0 }),
|
|
353
|
+
snapshot
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return normalized;
|
|
358
|
+
}
|
|
359
|
+
function normalizeTemplateElement(element, index) {
|
|
360
|
+
const animation = element.animation ?? {
|
|
361
|
+
in: "none",
|
|
362
|
+
out: "none",
|
|
363
|
+
inDuration: 0,
|
|
364
|
+
outDuration: 0,
|
|
365
|
+
hold: "full"
|
|
366
|
+
};
|
|
367
|
+
const base = {
|
|
368
|
+
id: element.id || `${element.kind}-${index + 1}`,
|
|
369
|
+
x: element.x ?? element.relativePosition?.x ?? 0,
|
|
370
|
+
y: element.y ?? element.relativePosition?.y ?? 0,
|
|
371
|
+
width: element.width ?? "auto",
|
|
372
|
+
height: element.height ?? "auto",
|
|
373
|
+
animation
|
|
374
|
+
};
|
|
375
|
+
if (element.kind === "text") {
|
|
376
|
+
const props2 = element.textProperties ?? element;
|
|
377
|
+
return {
|
|
378
|
+
kind: "text",
|
|
379
|
+
...base,
|
|
380
|
+
content: props2.text ?? props2.content ?? "Text",
|
|
381
|
+
fontFamily: props2.fontFamily ?? "Inter Variable",
|
|
382
|
+
fontSize: props2.fontSize ?? 48,
|
|
383
|
+
fontWeight: props2.fontWeight ?? 400,
|
|
384
|
+
color: props2.color ?? "#FFFFFF",
|
|
385
|
+
align: props2.align ?? "center",
|
|
386
|
+
role: props2.role ?? "none",
|
|
387
|
+
verticalAlign: props2.verticalAlign ?? "middle",
|
|
388
|
+
styleRef: props2.styleRef
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
if (element.kind === "solid" || element.kind === "shape") {
|
|
392
|
+
const props2 = element.solidProperties ?? element;
|
|
393
|
+
return {
|
|
394
|
+
kind: "shape",
|
|
395
|
+
...base,
|
|
396
|
+
shape: props2.shape ?? "rect",
|
|
397
|
+
fill: props2.color ?? props2.fill ?? "#000000",
|
|
398
|
+
opacity: props2.opacity ?? 1
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
const props = element.imageProperties ?? element;
|
|
402
|
+
return {
|
|
403
|
+
kind: "image",
|
|
404
|
+
...base,
|
|
405
|
+
url: props.url ?? "",
|
|
406
|
+
opacity: props.opacity ?? 1
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
function normalizeTextTemplate(input, metadata) {
|
|
410
|
+
if (!input || typeof input !== "object")
|
|
411
|
+
throw new Error("Invalid text template document");
|
|
412
|
+
const raw = input;
|
|
413
|
+
const sourceLayers = Array.isArray(raw.layers) ? raw.layers : Array.isArray(raw.elements) ? raw.elements.map(normalizeTemplateElement) : [];
|
|
414
|
+
const layers = sourceLayers.map(normalizeTemplateLayer);
|
|
415
|
+
const dependencies = layers.map((layer) => layer.kind === "text" ? layer.styleRef : void 0).filter((ref) => !!ref).filter(
|
|
416
|
+
(ref, index, all) => all.findIndex(
|
|
417
|
+
(other) => other.effectId === ref.effectId && other.revisionId === ref.revisionId
|
|
418
|
+
) === index
|
|
419
|
+
);
|
|
420
|
+
const document = {
|
|
421
|
+
schemaVersion: CANONICAL_SCHEMA_VERSION,
|
|
422
|
+
id: String(raw.id),
|
|
423
|
+
label: String(raw.label ?? raw.name ?? raw.displayName ?? raw.id),
|
|
424
|
+
category: raw.category ?? "title-card",
|
|
425
|
+
description: String(raw.description ?? ""),
|
|
426
|
+
tags: Array.isArray(raw.tags) ? raw.tags.map(String) : [],
|
|
427
|
+
duration: Number(raw.duration ?? raw.defaultDuration ?? 4),
|
|
428
|
+
fps: Number(raw.fps ?? 30),
|
|
429
|
+
canvasWidth: Number(raw.canvasWidth ?? raw.width ?? 1920),
|
|
430
|
+
canvasHeight: Number(raw.canvasHeight ?? raw.height ?? 1080),
|
|
431
|
+
layers,
|
|
432
|
+
dependencies,
|
|
433
|
+
thumbnail: raw.thumbnail ?? raw.thumbnailUrl,
|
|
434
|
+
preview: raw.preview ?? raw.previewVideoUrl,
|
|
435
|
+
published: raw.published,
|
|
436
|
+
creatorName: raw.creatorName,
|
|
437
|
+
creatorLink: raw.creatorLink
|
|
438
|
+
};
|
|
439
|
+
if (!document.id || !Number.isFinite(document.duration) || document.duration <= 0) {
|
|
440
|
+
throw new Error("Text template must have a valid id and positive duration");
|
|
441
|
+
}
|
|
442
|
+
if (metadata?.assetId) {
|
|
443
|
+
document.revision = {
|
|
444
|
+
assetId: metadata.assetId,
|
|
445
|
+
revisionId: metadata.revisionId ?? "draft",
|
|
446
|
+
schemaVersion: CANONICAL_SCHEMA_VERSION,
|
|
447
|
+
contentHash: metadata.contentHash ?? canonicalContentHash({ ...document, revision: void 0 }),
|
|
448
|
+
rendererVersion: metadata.rendererVersion ?? CANONICAL_RENDERER_VERSION,
|
|
449
|
+
createdAt: metadata.createdAt ?? (/* @__PURE__ */ new Date()).toISOString()
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
return document;
|
|
453
|
+
}
|
|
454
|
+
function activeEffectDiagnostics(scene) {
|
|
455
|
+
const config = sceneToConfig(scene);
|
|
456
|
+
const glowLayers = scene.effectLayers.filter(
|
|
457
|
+
(layer) => layer.type === "glow"
|
|
458
|
+
);
|
|
459
|
+
return scene.effectLayers.map((layer) => {
|
|
460
|
+
const glowIndex = glowLayers.indexOf(layer);
|
|
461
|
+
const semanticEnabled = (() => {
|
|
462
|
+
switch (layer.type) {
|
|
463
|
+
case "panel":
|
|
464
|
+
return config.panelEnabled;
|
|
465
|
+
case "glow":
|
|
466
|
+
return config.glowLayers[glowIndex]?.enabled === true;
|
|
467
|
+
case "shadow":
|
|
468
|
+
return config.shadowEnabled;
|
|
469
|
+
case "extrusion":
|
|
470
|
+
return config.bevelEnabled;
|
|
471
|
+
case "duplicateStack":
|
|
472
|
+
return config.stackEnabled;
|
|
473
|
+
case "stroke":
|
|
474
|
+
return config.strokeEnabled;
|
|
475
|
+
case "fill":
|
|
476
|
+
return config.fillType !== "none";
|
|
477
|
+
default:
|
|
478
|
+
return true;
|
|
479
|
+
}
|
|
480
|
+
})();
|
|
481
|
+
const contributes = layer.enabled === true && layer.opacity > 0 && layer.params.enabled !== false && semanticEnabled === true;
|
|
482
|
+
return {
|
|
483
|
+
layerId: layer.id,
|
|
484
|
+
layerType: layer.type,
|
|
485
|
+
enabled: contributes,
|
|
486
|
+
contribution: contributes ? "active" : "disabled",
|
|
487
|
+
reason: contributes ? `${layer.name} is enabled` : `${layer.name} is explicitly disabled`
|
|
488
|
+
};
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
function capabilities(target, unsupportedFeatures = []) {
|
|
492
|
+
const status = unsupportedFeatures.length > 0 ? target === "native" ? "rasterized" : "unsupported" : "exact";
|
|
493
|
+
return { target, status, unsupportedFeatures, approximations: [] };
|
|
494
|
+
}
|
|
495
|
+
function evaluateTextEffect(document, time = 0, _overrides = {}) {
|
|
496
|
+
const scene = normalizeTextEffect(document);
|
|
497
|
+
const evaluated = canonicalizeSceneDocument(applyTimelineAtTime(scene, time));
|
|
498
|
+
const diagnostics = activeEffectDiagnostics(evaluated);
|
|
499
|
+
const unsupported = [];
|
|
500
|
+
return {
|
|
501
|
+
kind: "text-effect",
|
|
502
|
+
width: evaluated.canvas.width,
|
|
503
|
+
height: evaluated.canvas.height,
|
|
504
|
+
duration: evaluated.timeline.duration,
|
|
505
|
+
time,
|
|
506
|
+
effect: evaluated,
|
|
507
|
+
diagnostics,
|
|
508
|
+
capabilities: {
|
|
509
|
+
canvas2d: capabilities("canvas2d"),
|
|
510
|
+
native: capabilities("native", unsupported),
|
|
511
|
+
export: capabilities("export", unsupported)
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
function evaluateTextTemplate(document, time = 0, overrides = {}) {
|
|
516
|
+
const template = normalizeTextTemplate(document);
|
|
517
|
+
const diagnostics = template.layers.map((layer) => {
|
|
518
|
+
const enabled = evaluateAnimatable(layer.opacity ?? 1, time, template.duration) > 0;
|
|
519
|
+
return {
|
|
520
|
+
layerId: layer.id,
|
|
521
|
+
layerType: layer.kind,
|
|
522
|
+
enabled,
|
|
523
|
+
contribution: enabled ? "active" : "disabled",
|
|
524
|
+
reason: enabled ? `${layer.kind} layer contributes at ${time.toFixed(3)}s` : "Layer opacity is zero"
|
|
525
|
+
};
|
|
526
|
+
});
|
|
527
|
+
void overrides;
|
|
528
|
+
return {
|
|
529
|
+
kind: "text-template",
|
|
530
|
+
width: template.canvasWidth,
|
|
531
|
+
height: template.canvasHeight,
|
|
532
|
+
duration: template.duration,
|
|
533
|
+
time,
|
|
534
|
+
template,
|
|
535
|
+
diagnostics,
|
|
536
|
+
capabilities: {
|
|
537
|
+
canvas2d: capabilities("canvas2d"),
|
|
538
|
+
native: capabilities("native"),
|
|
539
|
+
export: capabilities("export")
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
function getRenderCapabilities(plan, target) {
|
|
544
|
+
return plan.capabilities[target];
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// src/textTemplates/contract.ts
|
|
548
|
+
var TEXT_TEMPLATE_KIND = "text-template";
|
|
549
|
+
var TEXT_TEMPLATE_SCHEMA_VERSION = 4;
|
|
550
|
+
var TEXT_TEMPLATE_RENDERER_VERSION = "1.5.0";
|
|
551
|
+
|
|
552
|
+
// src/rendering/textEffectRenderer.ts
|
|
553
|
+
function isScene(value) {
|
|
554
|
+
return Boolean(value && typeof value === "object" && Array.isArray(value.effectLayers) && value.canvas);
|
|
555
|
+
}
|
|
556
|
+
function isConfig(value) {
|
|
557
|
+
return Boolean(value && typeof value === "object" && typeof value.text === "string" && typeof value.fontFamily === "string");
|
|
558
|
+
}
|
|
559
|
+
function renderTextEffectToCanvas(ctx, input) {
|
|
560
|
+
const source = input.source;
|
|
561
|
+
const scene = isScene(source) ? source : isScene(source?.scene) ? source.scene : isConfig(source?.config) ? textEffectConfigToScene(source.config) : isConfig(source) ? textEffectConfigToScene(source) : null;
|
|
562
|
+
if (!scene) {
|
|
563
|
+
return {
|
|
564
|
+
capability: "text-effect",
|
|
565
|
+
rendererVersion: CANONICAL_RENDERER_VERSION,
|
|
566
|
+
diagnostics: [{ level: "error", code: "effect-source-invalid", message: "Text effect source is not a scene or supported configuration.", capability: "text-effect" }],
|
|
567
|
+
usedDependencies: []
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
if (isConfig(source)) evaluateConfig(source, input.context.time, ctx);
|
|
571
|
+
else evaluateScene(scene, input.context.time, ctx);
|
|
572
|
+
return {
|
|
573
|
+
capability: "text-effect",
|
|
574
|
+
revisionId: source?.revisionId,
|
|
575
|
+
contentHash: source?.contentHash,
|
|
576
|
+
rendererVersion: source?.rendererVersion ?? CANONICAL_RENDERER_VERSION,
|
|
577
|
+
diagnostics: [],
|
|
578
|
+
usedDependencies: []
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// src/textTemplates/validator.ts
|
|
583
|
+
var MAX_NODES = 500;
|
|
584
|
+
var MAX_CONTROLS = 100;
|
|
585
|
+
function validateTextTemplateArtifact(artifact) {
|
|
586
|
+
const diagnostics = [];
|
|
587
|
+
if (!artifact || artifact.kind !== "text-template") {
|
|
588
|
+
return [{ severity: "error", code: "TEMPLATE_KIND", message: "Artifact kind must be text-template" }];
|
|
589
|
+
}
|
|
590
|
+
const { document, timing, controls } = artifact;
|
|
591
|
+
if (!document || !document.id) {
|
|
592
|
+
diagnostics.push({ severity: "error", code: "TEMPLATE_ID", message: "Template ID is required" });
|
|
593
|
+
}
|
|
594
|
+
if (!Number.isFinite(document.canvas.width) || document.canvas.width <= 0 || !Number.isFinite(document.canvas.height) || document.canvas.height <= 0) {
|
|
595
|
+
diagnostics.push({ severity: "error", code: "CANVAS_SIZE", message: "Canvas dimensions must be positive finite numbers" });
|
|
596
|
+
}
|
|
597
|
+
if (!Number.isFinite(timing.duration) || timing.duration <= 0) {
|
|
598
|
+
diagnostics.push({ severity: "error", code: "DURATION", message: "Duration must be positive" });
|
|
599
|
+
}
|
|
600
|
+
if (!Number.isFinite(timing.fps) || timing.fps <= 0 || timing.fps > 240) {
|
|
601
|
+
diagnostics.push({ severity: "error", code: "FPS", message: "FPS must be between 0 and 240" });
|
|
602
|
+
}
|
|
603
|
+
const ids = /* @__PURE__ */ new Set();
|
|
604
|
+
let nodeCount = 0;
|
|
605
|
+
const visit = (node) => {
|
|
606
|
+
nodeCount += 1;
|
|
607
|
+
if (!node.id) {
|
|
608
|
+
diagnostics.push({ severity: "error", code: "NODE_ID", message: "Every node needs a stable ID" });
|
|
609
|
+
} else if (ids.has(node.id)) {
|
|
610
|
+
diagnostics.push({ severity: "error", code: "DUPLICATE_NODE_ID", message: `Duplicate node ID: ${node.id}`, nodeId: node.id });
|
|
611
|
+
} else {
|
|
612
|
+
ids.add(node.id);
|
|
613
|
+
}
|
|
614
|
+
for (const value of [node.x, node.y]) {
|
|
615
|
+
if (!Number.isFinite(value)) {
|
|
616
|
+
diagnostics.push({ severity: "error", code: "NODE_GEOMETRY", message: `Node ${node.id} has non-finite position`, nodeId: node.id });
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
for (const [axis, value] of [["width", node.width], ["height", node.height]]) {
|
|
620
|
+
if (value === "auto") {
|
|
621
|
+
if (node.type !== "text" && node.type !== "container") {
|
|
622
|
+
diagnostics.push({
|
|
623
|
+
severity: "error",
|
|
624
|
+
code: "NODE_GEOMETRY_AUTO_NOT_SUPPORTED",
|
|
625
|
+
message: `Node ${node.id} (${node.type}) does not support auto ${axis} \u2014 only text and container nodes can use content-driven sizing`,
|
|
626
|
+
nodeId: node.id
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
} else if (!Number.isFinite(value)) {
|
|
630
|
+
diagnostics.push({ severity: "error", code: "NODE_GEOMETRY", message: `Node ${node.id} has non-finite geometry`, nodeId: node.id });
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
if (node.type === "text" && !node.text) {
|
|
634
|
+
diagnostics.push({ severity: "warning", code: "EMPTY_TEXT", message: `Text node ${node.id} is empty`, nodeId: node.id });
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
(document.nodes || []).forEach(visit);
|
|
638
|
+
if (nodeCount > MAX_NODES) {
|
|
639
|
+
diagnostics.push({ severity: "error", code: "NODE_LIMIT", message: `Template exceeds the ${MAX_NODES}-node limit` });
|
|
640
|
+
}
|
|
641
|
+
if ((controls || []).length > MAX_CONTROLS) {
|
|
642
|
+
diagnostics.push({ severity: "error", code: "CONTROL_LIMIT", message: `Template exceeds the ${MAX_CONTROLS}-control limit` });
|
|
643
|
+
}
|
|
644
|
+
const controlIds = /* @__PURE__ */ new Set();
|
|
645
|
+
for (const control of controls || []) {
|
|
646
|
+
if (controlIds.has(control.id)) {
|
|
647
|
+
diagnostics.push({ severity: "error", code: "DUPLICATE_CONTROL_ID", message: `Duplicate control ID: ${control.id}` });
|
|
648
|
+
}
|
|
649
|
+
controlIds.add(control.id);
|
|
650
|
+
if (!ids.has(control.target.nodeId)) {
|
|
651
|
+
diagnostics.push({ severity: "error", code: "CONTROL_TARGET", message: `Control ${control.id} targets missing node ${control.target.nodeId}` });
|
|
652
|
+
}
|
|
653
|
+
if (control.target.propertyPath.includes("__proto__") || control.target.propertyPath.includes("constructor") || control.target.propertyPath.includes("prototype")) {
|
|
654
|
+
diagnostics.push({ severity: "error", code: "UNSAFE_TARGET", message: `Control ${control.id} has an unsafe target path` });
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
const regions = [timing.intro, timing.outro, timing.loop].filter(Boolean);
|
|
658
|
+
regions.forEach((region) => {
|
|
659
|
+
if (!Number.isFinite(region.start) || !Number.isFinite(region.end) || region.start < 0 || region.end <= region.start || region.end > timing.duration) {
|
|
660
|
+
diagnostics.push({ severity: "error", code: "TIMING_REGION", message: "Timing regions must fit within the template duration" });
|
|
661
|
+
}
|
|
662
|
+
});
|
|
663
|
+
return diagnostics;
|
|
664
|
+
}
|
|
665
|
+
function assertValidTextTemplateArtifact(artifact) {
|
|
666
|
+
const errors = validateTextTemplateArtifact(artifact).filter((diagnostic) => diagnostic.severity === "error");
|
|
667
|
+
if (errors.length) {
|
|
668
|
+
throw new Error(errors.map((diagnostic) => diagnostic.message).join("; "));
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
// src/textTemplates/normalize.ts
|
|
673
|
+
function clone2(value) {
|
|
674
|
+
return JSON.parse(JSON.stringify(value));
|
|
675
|
+
}
|
|
676
|
+
function stableStringify2(value) {
|
|
677
|
+
if (value === null || typeof value !== "object") return JSON.stringify(value);
|
|
678
|
+
if (Array.isArray(value)) return `[${value.map(stableStringify2).join(",")}]`;
|
|
679
|
+
const object = value;
|
|
680
|
+
return `{${Object.keys(object).sort().map((key) => `${JSON.stringify(key)}:${stableStringify2(object[key])}`).join(",")}}`;
|
|
681
|
+
}
|
|
682
|
+
function canonicalTemplateHash(value) {
|
|
683
|
+
const input = stableStringify2(value);
|
|
684
|
+
let hash = 2166136261;
|
|
685
|
+
for (let index = 0; index < input.length; index += 1) {
|
|
686
|
+
hash ^= input.charCodeAt(index);
|
|
687
|
+
hash = Math.imul(hash, 16777619);
|
|
688
|
+
}
|
|
689
|
+
return `fnv1a-${(hash >>> 0).toString(16).padStart(8, "0")}`;
|
|
690
|
+
}
|
|
691
|
+
function now() {
|
|
692
|
+
return (/* @__PURE__ */ new Date()).toISOString();
|
|
693
|
+
}
|
|
694
|
+
function resolveAnimatableValue(raw, fallback) {
|
|
695
|
+
if (raw == null) return fallback;
|
|
696
|
+
if (typeof raw === "object" && raw !== null && "keyframes" in raw && Array.isArray(raw.keyframes)) {
|
|
697
|
+
const kfs = raw.keyframes;
|
|
698
|
+
if (kfs.length > 0 && kfs[0]?.value !== void 0) {
|
|
699
|
+
return kfs[0].value;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
return raw;
|
|
703
|
+
}
|
|
704
|
+
function resolveNumericProperty(raw, fallback) {
|
|
705
|
+
const unwrapped = resolveAnimatableValue(raw, fallback);
|
|
706
|
+
const n = Number(unwrapped);
|
|
707
|
+
return Number.isFinite(n) ? n : fallback;
|
|
708
|
+
}
|
|
709
|
+
function resolveStringProperty(raw, fallback) {
|
|
710
|
+
const unwrapped = resolveAnimatableValue(raw, fallback);
|
|
711
|
+
return unwrapped != null ? String(unwrapped) : fallback;
|
|
712
|
+
}
|
|
713
|
+
function resolveDimension(raw, fallback) {
|
|
714
|
+
if (raw === "auto") return "auto";
|
|
715
|
+
const unwrapped = resolveAnimatableValue(raw, fallback);
|
|
716
|
+
if (unwrapped === "auto") return "auto";
|
|
717
|
+
const n = Number(unwrapped);
|
|
718
|
+
return Number.isFinite(n) ? n : fallback;
|
|
719
|
+
}
|
|
720
|
+
function propertyKeyframes(layer) {
|
|
721
|
+
const result = {};
|
|
722
|
+
for (const property of ["x", "y", "width", "height", "opacity", "fontSize", "fontWeight", "letterSpacing"]) {
|
|
723
|
+
const value = layer?.[property];
|
|
724
|
+
if (value && typeof value === "object" && Array.isArray(value.keyframes) && value.keyframes.length > 0) {
|
|
725
|
+
result[property] = { keyframes: clone2(value.keyframes) };
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
return Object.keys(result).length ? result : void 0;
|
|
729
|
+
}
|
|
730
|
+
function nodeAnimation(layer) {
|
|
731
|
+
const propertyFrames = propertyKeyframes(layer);
|
|
732
|
+
if (!propertyFrames) return layer?.animation;
|
|
733
|
+
return {
|
|
734
|
+
...layer?.animation || { in: "none", out: "none", inDuration: 0, outDuration: 0, hold: "full" },
|
|
735
|
+
propertyKeyframes: propertyFrames
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
function legacyLayerToNode(layer, index) {
|
|
739
|
+
const id = String(layer?.id || `${layer?.kind || "node"}-${index + 1}`);
|
|
740
|
+
const x = resolveNumericProperty(layer?.x ?? layer?.relativePosition?.x, 0);
|
|
741
|
+
const y = resolveNumericProperty(layer?.y ?? layer?.relativePosition?.y, 0);
|
|
742
|
+
const width = resolveDimension(layer?.width, 400);
|
|
743
|
+
const height = resolveDimension(layer?.height, 120);
|
|
744
|
+
const animation = nodeAnimation(layer);
|
|
745
|
+
const anchor = layer?.anchor;
|
|
746
|
+
const parentId = layer?.parentId;
|
|
747
|
+
const visible = layer?.visible !== false;
|
|
748
|
+
if (layer?.kind === "container" || layer?.kind === "group") {
|
|
749
|
+
const container = layer?.containerProperties || layer;
|
|
750
|
+
const layout = container?.layout || {};
|
|
751
|
+
return {
|
|
752
|
+
id,
|
|
753
|
+
name: String(layer?.name || id),
|
|
754
|
+
type: "container",
|
|
755
|
+
layout: {
|
|
756
|
+
type: layout.type === "absolute" ? "absolute" : "flex",
|
|
757
|
+
direction: layout.direction === "column" ? "column" : "row",
|
|
758
|
+
gap: resolveNumericProperty(layout.gap, 0),
|
|
759
|
+
alignItems: ["start", "center", "end", "stretch"].includes(
|
|
760
|
+
layout.alignItems
|
|
761
|
+
) ? layout.alignItems : "center",
|
|
762
|
+
justifyContent: [
|
|
763
|
+
"start",
|
|
764
|
+
"center",
|
|
765
|
+
"end",
|
|
766
|
+
"space-between",
|
|
767
|
+
"space-around"
|
|
768
|
+
].includes(layout.justifyContent) ? layout.justifyContent : "start",
|
|
769
|
+
paddingTop: resolveNumericProperty(
|
|
770
|
+
layout.paddingTop ?? container.paddingTop,
|
|
771
|
+
0
|
|
772
|
+
),
|
|
773
|
+
paddingRight: resolveNumericProperty(
|
|
774
|
+
layout.paddingRight ?? container.paddingRight,
|
|
775
|
+
0
|
|
776
|
+
),
|
|
777
|
+
paddingBottom: resolveNumericProperty(
|
|
778
|
+
layout.paddingBottom ?? container.paddingBottom,
|
|
779
|
+
0
|
|
780
|
+
),
|
|
781
|
+
paddingLeft: resolveNumericProperty(
|
|
782
|
+
layout.paddingLeft ?? container.paddingLeft,
|
|
783
|
+
0
|
|
784
|
+
)
|
|
785
|
+
},
|
|
786
|
+
x,
|
|
787
|
+
y,
|
|
788
|
+
width,
|
|
789
|
+
height,
|
|
790
|
+
visible,
|
|
791
|
+
style: {
|
|
792
|
+
backgroundColor: container?.backgroundColor ? resolveStringProperty(container.backgroundColor, "") : void 0,
|
|
793
|
+
backgroundOpacity: resolveNumericProperty(
|
|
794
|
+
container?.backgroundOpacity,
|
|
795
|
+
1
|
|
796
|
+
),
|
|
797
|
+
borderRadius: resolveNumericProperty(
|
|
798
|
+
container?.backgroundRadius ?? container?.borderRadius,
|
|
799
|
+
0
|
|
800
|
+
),
|
|
801
|
+
borderColor: container?.backgroundBorderColor ?? container?.borderColor ? resolveStringProperty(
|
|
802
|
+
container.backgroundBorderColor ?? container.borderColor,
|
|
803
|
+
""
|
|
804
|
+
) : void 0,
|
|
805
|
+
borderWidth: resolveNumericProperty(
|
|
806
|
+
container?.backgroundBorderWidth ?? container?.borderWidth,
|
|
807
|
+
0
|
|
808
|
+
),
|
|
809
|
+
opacity: resolveNumericProperty(container?.opacity, 1)
|
|
810
|
+
},
|
|
811
|
+
animation,
|
|
812
|
+
anchor,
|
|
813
|
+
parentId
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
if (layer?.kind === "shape" || layer?.kind === "solid") {
|
|
817
|
+
const shape = layer?.solidProperties || layer;
|
|
818
|
+
return {
|
|
819
|
+
id,
|
|
820
|
+
name: String(layer?.name || id),
|
|
821
|
+
type: "shape",
|
|
822
|
+
shapeType: shape?.shape === "circle" ? "circle" : shape?.shape === "line" ? "line" : "rectangle",
|
|
823
|
+
x,
|
|
824
|
+
y,
|
|
825
|
+
width: typeof width === "number" ? width : 400,
|
|
826
|
+
height: typeof height === "number" ? height : 200,
|
|
827
|
+
visible,
|
|
828
|
+
style: {
|
|
829
|
+
fillColor: resolveStringProperty(
|
|
830
|
+
shape?.fill ?? shape?.color,
|
|
831
|
+
"#000000"
|
|
832
|
+
),
|
|
833
|
+
fillOpacity: resolveNumericProperty(
|
|
834
|
+
shape?.opacity ?? shape?.fillOpacity,
|
|
835
|
+
1
|
|
836
|
+
),
|
|
837
|
+
strokeColor: shape?.stroke?.color ? resolveStringProperty(shape.stroke.color, "#000000") : void 0,
|
|
838
|
+
strokeWidth: shape?.stroke?.width ? resolveNumericProperty(shape.stroke.width, 0) : void 0
|
|
839
|
+
},
|
|
840
|
+
animation,
|
|
841
|
+
anchor,
|
|
842
|
+
parentId
|
|
843
|
+
};
|
|
844
|
+
}
|
|
845
|
+
if (layer?.kind === "image") {
|
|
846
|
+
const image = layer?.imageProperties || layer;
|
|
847
|
+
return {
|
|
848
|
+
id,
|
|
849
|
+
name: String(layer?.name || id),
|
|
850
|
+
type: "media",
|
|
851
|
+
mediaType: "image",
|
|
852
|
+
x,
|
|
853
|
+
y,
|
|
854
|
+
width: typeof width === "number" ? width : 400,
|
|
855
|
+
height: typeof height === "number" ? height : 300,
|
|
856
|
+
visible,
|
|
857
|
+
assetId: image?.assetId,
|
|
858
|
+
src: image?.url,
|
|
859
|
+
style: { opacity: resolveNumericProperty(image?.opacity, 1) },
|
|
860
|
+
animation,
|
|
861
|
+
anchor,
|
|
862
|
+
parentId
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
const text = layer?.textProperties || layer;
|
|
866
|
+
return {
|
|
867
|
+
id,
|
|
868
|
+
name: String(layer?.name || id),
|
|
869
|
+
type: "text",
|
|
870
|
+
text: resolveStringProperty(text?.content ?? text?.text, "Text"),
|
|
871
|
+
x,
|
|
872
|
+
y,
|
|
873
|
+
width,
|
|
874
|
+
height,
|
|
875
|
+
visible,
|
|
876
|
+
animation,
|
|
877
|
+
anchor,
|
|
878
|
+
parentId,
|
|
879
|
+
style: {
|
|
880
|
+
fontFamily: resolveStringProperty(text?.fontFamily, "Inter Variable"),
|
|
881
|
+
fontSize: resolveNumericProperty(text?.fontSize, 48),
|
|
882
|
+
fontWeight: resolveStringProperty(text?.fontWeight, "400"),
|
|
883
|
+
textColor: resolveStringProperty(
|
|
884
|
+
text?.color ?? text?.textColor,
|
|
885
|
+
"#FFFFFF"
|
|
886
|
+
),
|
|
887
|
+
textAlign: text?.align || text?.textAlign || "center",
|
|
888
|
+
lineHeight: resolveNumericProperty(text?.lineHeight, 1.2),
|
|
889
|
+
letterSpacing: resolveNumericProperty(text?.letterSpacing, 0),
|
|
890
|
+
overflow: text?.overflow,
|
|
891
|
+
verticalAlign: text?.verticalAlign
|
|
892
|
+
},
|
|
893
|
+
backgroundPanel: text?.backgroundColor || text?.backgroundPanel ? {
|
|
894
|
+
color: text?.backgroundPanel?.color ?? text?.backgroundColor ? resolveStringProperty(
|
|
895
|
+
text?.backgroundPanel?.color ?? text?.backgroundColor,
|
|
896
|
+
"#000000"
|
|
897
|
+
) : void 0,
|
|
898
|
+
opacity: resolveNumericProperty(
|
|
899
|
+
text?.backgroundPanel?.opacity ?? text?.backgroundOpacity,
|
|
900
|
+
1
|
|
901
|
+
),
|
|
902
|
+
paddingTop: resolveNumericProperty(
|
|
903
|
+
text?.backgroundPanel?.paddingTop ?? text?.paddingTop,
|
|
904
|
+
0
|
|
905
|
+
),
|
|
906
|
+
paddingRight: resolveNumericProperty(
|
|
907
|
+
text?.backgroundPanel?.paddingRight ?? text?.paddingRight,
|
|
908
|
+
0
|
|
909
|
+
),
|
|
910
|
+
paddingBottom: resolveNumericProperty(
|
|
911
|
+
text?.backgroundPanel?.paddingBottom ?? text?.paddingBottom,
|
|
912
|
+
0
|
|
913
|
+
),
|
|
914
|
+
paddingLeft: resolveNumericProperty(
|
|
915
|
+
text?.backgroundPanel?.paddingLeft ?? text?.paddingLeft,
|
|
916
|
+
0
|
|
917
|
+
),
|
|
918
|
+
borderRadius: resolveNumericProperty(
|
|
919
|
+
text?.backgroundPanel?.borderRadius ?? text?.backgroundRadius,
|
|
920
|
+
0
|
|
921
|
+
),
|
|
922
|
+
borderColor: text?.backgroundPanel?.borderColor ?? text?.backgroundBorderColor ? resolveStringProperty(
|
|
923
|
+
text?.backgroundPanel?.borderColor ?? text?.backgroundBorderColor,
|
|
924
|
+
"#000000"
|
|
925
|
+
) : void 0,
|
|
926
|
+
borderWidth: resolveNumericProperty(
|
|
927
|
+
text?.backgroundPanel?.borderWidth ?? text?.backgroundBorderWidth,
|
|
928
|
+
0
|
|
929
|
+
)
|
|
930
|
+
} : void 0,
|
|
931
|
+
spans: Array.isArray(text?.spans) ? clone2(text.spans) : void 0,
|
|
932
|
+
perCharFillEnabled: Boolean(text?.perCharFillEnabled),
|
|
933
|
+
charFillColors: Array.isArray(text?.charFillColors) ? clone2(text.charFillColors) : void 0,
|
|
934
|
+
splitAnimator: text?.splitAnimator ? clone2(text.splitAnimator) : void 0,
|
|
935
|
+
textEffectRef: text?.styleRef ? clone2(text.styleRef) : void 0,
|
|
936
|
+
// Kept as optional metadata for compatibility with role-based editor
|
|
937
|
+
// customizations. The canonical renderer does not depend on roles.
|
|
938
|
+
...layer?.role ? { role: String(layer.role) } : {}
|
|
939
|
+
};
|
|
940
|
+
}
|
|
941
|
+
function inferControls(nodes) {
|
|
942
|
+
const controls = [];
|
|
943
|
+
for (const node of nodes) {
|
|
944
|
+
if (node.type === "text") {
|
|
945
|
+
controls.push({
|
|
946
|
+
id: `text-${node.id}`,
|
|
947
|
+
label: node.name || "Text",
|
|
948
|
+
type: "text",
|
|
949
|
+
defaultValue: node.text,
|
|
950
|
+
constraints: { multiline: true, maxLength: 500 },
|
|
951
|
+
target: { nodeId: node.id, propertyPath: "text" }
|
|
952
|
+
});
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
return controls;
|
|
956
|
+
}
|
|
957
|
+
function collectDependencies(nodes) {
|
|
958
|
+
const assets = [];
|
|
959
|
+
const fonts = [];
|
|
960
|
+
const textEffects = [];
|
|
961
|
+
const seenAssets = /* @__PURE__ */ new Set();
|
|
962
|
+
const seenFonts = /* @__PURE__ */ new Set();
|
|
963
|
+
const seenEffects = /* @__PURE__ */ new Set();
|
|
964
|
+
for (const node of nodes) {
|
|
965
|
+
if (node.type === "media" && node.assetId && !seenAssets.has(node.assetId)) {
|
|
966
|
+
assets.push({
|
|
967
|
+
id: node.assetId,
|
|
968
|
+
type: "image",
|
|
969
|
+
mimeType: "image/png",
|
|
970
|
+
uri: node.src || "",
|
|
971
|
+
contentHash: node.assetId
|
|
972
|
+
});
|
|
973
|
+
seenAssets.add(node.assetId);
|
|
974
|
+
}
|
|
975
|
+
if (node.type === "text" && node.style?.fontFamily) {
|
|
976
|
+
const key = `${node.style.fontFamily}:${node.style.fontWeight || 400}`;
|
|
977
|
+
if (!seenFonts.has(key)) {
|
|
978
|
+
fonts.push({
|
|
979
|
+
family: node.style.fontFamily,
|
|
980
|
+
weight: Number(node.style.fontWeight || 400),
|
|
981
|
+
style: "normal"
|
|
982
|
+
});
|
|
983
|
+
seenFonts.add(key);
|
|
984
|
+
}
|
|
985
|
+
if (node.textEffectRef) {
|
|
986
|
+
const effectKey = `${node.textEffectRef.effectId}:${node.textEffectRef.revisionId}`;
|
|
987
|
+
if (!seenEffects.has(effectKey)) {
|
|
988
|
+
textEffects.push(clone2(node.textEffectRef));
|
|
989
|
+
seenEffects.add(effectKey);
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
return { assets, fonts, textEffects };
|
|
995
|
+
}
|
|
996
|
+
function normalizeTextTemplateArtifact(input) {
|
|
997
|
+
if (!input || typeof input !== "object")
|
|
998
|
+
throw new Error("Invalid text template artifact");
|
|
999
|
+
const raw = input;
|
|
1000
|
+
const source = raw.document && typeof raw.document === "object" ? raw.document : raw;
|
|
1001
|
+
const rawLayers = Array.isArray(source.layers) ? source.layers : Array.isArray(source.elements) ? source.elements : [];
|
|
1002
|
+
const nodes = Array.isArray(source.nodes) ? clone2(source.nodes) : rawLayers.map(legacyLayerToNode);
|
|
1003
|
+
const id = String(
|
|
1004
|
+
raw.metadata?.id ?? source.id ?? raw.id ?? "untitled-text-template"
|
|
1005
|
+
);
|
|
1006
|
+
const label = String(
|
|
1007
|
+
raw.metadata?.label ?? source.title ?? raw.label ?? raw.name ?? id
|
|
1008
|
+
);
|
|
1009
|
+
const duration = Number(
|
|
1010
|
+
raw.timing?.duration ?? source.duration ?? raw.duration ?? 4
|
|
1011
|
+
);
|
|
1012
|
+
const fps = Number(raw.timing?.fps ?? source.fps ?? raw.fps ?? 30);
|
|
1013
|
+
const width = Number(
|
|
1014
|
+
source.canvas?.width ?? source.canvasWidth ?? raw.canvasWidth ?? raw.width ?? 1920
|
|
1015
|
+
);
|
|
1016
|
+
const height = Number(
|
|
1017
|
+
source.canvas?.height ?? source.canvasHeight ?? raw.canvasHeight ?? raw.height ?? 1080
|
|
1018
|
+
);
|
|
1019
|
+
const createdAt = String(
|
|
1020
|
+
raw.revision?.createdAt ?? source.createdAt ?? now()
|
|
1021
|
+
);
|
|
1022
|
+
const document = {
|
|
1023
|
+
id,
|
|
1024
|
+
kind: TEXT_TEMPLATE_KIND,
|
|
1025
|
+
schemaVersion: TEXT_TEMPLATE_SCHEMA_VERSION,
|
|
1026
|
+
templateVersion: 1,
|
|
1027
|
+
canvas: {
|
|
1028
|
+
width,
|
|
1029
|
+
height,
|
|
1030
|
+
fps,
|
|
1031
|
+
backgroundColor: source.canvas?.backgroundColor ?? raw.backgroundColor
|
|
1032
|
+
},
|
|
1033
|
+
nodes,
|
|
1034
|
+
variables: source.variables && typeof source.variables === "object" ? clone2(source.variables) : void 0
|
|
1035
|
+
};
|
|
1036
|
+
const controls = Array.isArray(raw.controls) ? clone2(raw.controls) : inferControls(nodes);
|
|
1037
|
+
const timing = {
|
|
1038
|
+
duration,
|
|
1039
|
+
fps,
|
|
1040
|
+
durationPolicy: raw.timing?.durationPolicy || "stretch",
|
|
1041
|
+
intro: raw.timing?.intro ? clone2(raw.timing.intro) : void 0,
|
|
1042
|
+
outro: raw.timing?.outro ? clone2(raw.timing.outro) : void 0,
|
|
1043
|
+
loop: raw.timing?.loop ? clone2(raw.timing.loop) : void 0,
|
|
1044
|
+
markers: Array.isArray(raw.timing?.markers) ? clone2(raw.timing.markers) : void 0
|
|
1045
|
+
};
|
|
1046
|
+
const dependencies = raw.dependencies?.assets ? clone2(raw.dependencies) : collectDependencies(nodes);
|
|
1047
|
+
const content = { document, controls, timing, dependencies };
|
|
1048
|
+
const contentHash = String(
|
|
1049
|
+
raw.revision?.contentHash || canonicalTemplateHash(content)
|
|
1050
|
+
);
|
|
1051
|
+
const revision = {
|
|
1052
|
+
revisionId: String(
|
|
1053
|
+
raw.revision?.revisionId ?? raw.revisionId ?? `rev-${contentHash}`
|
|
1054
|
+
),
|
|
1055
|
+
contentHash,
|
|
1056
|
+
schemaVersion: TEXT_TEMPLATE_SCHEMA_VERSION,
|
|
1057
|
+
rendererVersion: String(
|
|
1058
|
+
raw.revision?.rendererVersion ?? raw.rendererVersion ?? TEXT_TEMPLATE_RENDERER_VERSION
|
|
1059
|
+
),
|
|
1060
|
+
createdAt,
|
|
1061
|
+
status: raw.revision?.status
|
|
1062
|
+
};
|
|
1063
|
+
const metadata = {
|
|
1064
|
+
id,
|
|
1065
|
+
label,
|
|
1066
|
+
category: String(
|
|
1067
|
+
raw.metadata?.category ?? source.category ?? raw.category ?? "title-card"
|
|
1068
|
+
),
|
|
1069
|
+
description: String(
|
|
1070
|
+
raw.metadata?.description ?? source.description ?? raw.description ?? ""
|
|
1071
|
+
),
|
|
1072
|
+
tags: Array.isArray(raw.metadata?.tags) ? clone2(raw.metadata.tags) : Array.isArray(raw.tags) ? raw.tags.map(String) : [],
|
|
1073
|
+
creatorName: raw.metadata?.creatorName ?? raw.creatorName,
|
|
1074
|
+
creatorLink: raw.metadata?.creatorLink ?? raw.creatorLink,
|
|
1075
|
+
thumbnailUrl: raw.previews?.thumbnailUrl ?? raw.thumbnail ?? raw.thumbnailUrl,
|
|
1076
|
+
previewVideoUrl: raw.previews?.previewVideoUrl ?? raw.preview ?? raw.previewVideoUrl
|
|
1077
|
+
};
|
|
1078
|
+
return {
|
|
1079
|
+
kind: TEXT_TEMPLATE_KIND,
|
|
1080
|
+
schemaVersion: TEXT_TEMPLATE_SCHEMA_VERSION,
|
|
1081
|
+
metadata,
|
|
1082
|
+
document,
|
|
1083
|
+
controls,
|
|
1084
|
+
timing,
|
|
1085
|
+
dependencies,
|
|
1086
|
+
revision,
|
|
1087
|
+
previews: raw.previews || metadata.thumbnailUrl || metadata.previewVideoUrl ? {
|
|
1088
|
+
thumbnailUrl: metadata.thumbnailUrl,
|
|
1089
|
+
previewVideoUrl: metadata.previewVideoUrl
|
|
1090
|
+
} : void 0
|
|
1091
|
+
};
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
// src/textTemplates/compiler.ts
|
|
1095
|
+
function clone3(value) {
|
|
1096
|
+
return JSON.parse(JSON.stringify(value));
|
|
1097
|
+
}
|
|
1098
|
+
function findNode(nodes, id) {
|
|
1099
|
+
for (const node of nodes) {
|
|
1100
|
+
if (node.id === id) return node;
|
|
1101
|
+
}
|
|
1102
|
+
return void 0;
|
|
1103
|
+
}
|
|
1104
|
+
function setPath(target, path, value) {
|
|
1105
|
+
const parts = path.split(".").filter(Boolean);
|
|
1106
|
+
if (!parts.length || parts.some((part) => part === "__proto__" || part === "constructor" || part === "prototype")) {
|
|
1107
|
+
return;
|
|
1108
|
+
}
|
|
1109
|
+
let cursor = target;
|
|
1110
|
+
for (let index = 0; index < parts.length - 1; index += 1) {
|
|
1111
|
+
const part = parts[index];
|
|
1112
|
+
if (!cursor[part] || typeof cursor[part] !== "object") cursor[part] = {};
|
|
1113
|
+
cursor = cursor[part];
|
|
1114
|
+
}
|
|
1115
|
+
cursor[parts[parts.length - 1]] = value;
|
|
1116
|
+
}
|
|
1117
|
+
function finite(value, fallback) {
|
|
1118
|
+
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
1119
|
+
}
|
|
1120
|
+
function fallbackTextWidth(text, style) {
|
|
1121
|
+
const fontSize = Math.max(1, finite(style.fontSize, 48));
|
|
1122
|
+
const letterSpacing = finite(style.letterSpacing, 0);
|
|
1123
|
+
return Array.from(text).reduce((total) => total + fontSize * 0.55 + letterSpacing, 0);
|
|
1124
|
+
}
|
|
1125
|
+
function measureTextNode(node, runtime) {
|
|
1126
|
+
const style = node.style;
|
|
1127
|
+
const lines = String(node.text ?? "").split("\n");
|
|
1128
|
+
const measure = runtime?.measureText;
|
|
1129
|
+
const widths = lines.map((line) => {
|
|
1130
|
+
const measured = measure?.(line, style)?.width;
|
|
1131
|
+
return Number.isFinite(measured) ? Math.max(0, measured) : fallbackTextWidth(line, style);
|
|
1132
|
+
});
|
|
1133
|
+
const lineHeight = Math.max(1, finite(style.fontSize, 48) * finite(style.lineHeight, 1.2));
|
|
1134
|
+
return {
|
|
1135
|
+
width: Math.max(1, ...widths),
|
|
1136
|
+
height: Math.max(1, lines.length * lineHeight)
|
|
1137
|
+
};
|
|
1138
|
+
}
|
|
1139
|
+
function resolveNodeSize(node, runtime) {
|
|
1140
|
+
if (node.type === "text") {
|
|
1141
|
+
const measured = measureTextNode(node, runtime);
|
|
1142
|
+
return {
|
|
1143
|
+
width: node.width === "auto" ? measured.width : Math.max(1, finite(node.width, measured.width)),
|
|
1144
|
+
height: node.height === "auto" ? measured.height : Math.max(1, finite(node.height, measured.height))
|
|
1145
|
+
};
|
|
1146
|
+
}
|
|
1147
|
+
return {
|
|
1148
|
+
width: node.width === "auto" ? 0 : Math.max(1, finite(node.width, 1)),
|
|
1149
|
+
height: node.height === "auto" ? 0 : Math.max(1, finite(node.height, 1))
|
|
1150
|
+
};
|
|
1151
|
+
}
|
|
1152
|
+
function resolveControls(controls, values) {
|
|
1153
|
+
const resolved = {};
|
|
1154
|
+
for (const control of controls || []) {
|
|
1155
|
+
const value = values && Object.prototype.hasOwnProperty.call(values, control.id) ? values[control.id] : control.defaultValue;
|
|
1156
|
+
if (control.type === "number") {
|
|
1157
|
+
const numeric = Number(value);
|
|
1158
|
+
resolved[control.id] = Number.isFinite(numeric) ? Math.min(
|
|
1159
|
+
control.constraints?.max ?? numeric,
|
|
1160
|
+
Math.max(control.constraints?.min ?? numeric, numeric)
|
|
1161
|
+
) : control.defaultValue;
|
|
1162
|
+
} else if (control.type === "boolean") {
|
|
1163
|
+
resolved[control.id] = value === true;
|
|
1164
|
+
} else {
|
|
1165
|
+
resolved[control.id] = value;
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
return resolved;
|
|
1169
|
+
}
|
|
1170
|
+
function mapTime(time, artifact, clipDuration) {
|
|
1171
|
+
const duration = artifact.timing.duration;
|
|
1172
|
+
const targetDuration = clipDuration && clipDuration > 0 ? clipDuration : duration;
|
|
1173
|
+
const localTime = Math.max(0, Math.min(time, targetDuration));
|
|
1174
|
+
if (artifact.timing.durationPolicy === "stretch" && targetDuration !== duration) {
|
|
1175
|
+
const introEnd = artifact.timing.intro?.end ?? 0;
|
|
1176
|
+
const outroDuration = duration - (artifact.timing.outro?.start ?? duration);
|
|
1177
|
+
const outroStartInTarget = targetDuration - outroDuration;
|
|
1178
|
+
if (localTime <= introEnd) {
|
|
1179
|
+
return localTime;
|
|
1180
|
+
} else if (localTime >= outroStartInTarget) {
|
|
1181
|
+
const outroProgress = outroDuration > 0 ? (localTime - outroStartInTarget) / outroDuration : 1;
|
|
1182
|
+
return (artifact.timing.outro?.start ?? duration) + outroProgress * outroDuration;
|
|
1183
|
+
} else {
|
|
1184
|
+
const flexibleTarget = Math.max(1e-3, outroStartInTarget - introEnd);
|
|
1185
|
+
const flexibleAuthored = Math.max(0, (artifact.timing.outro?.start ?? duration) - introEnd);
|
|
1186
|
+
const progress = (localTime - introEnd) / flexibleTarget;
|
|
1187
|
+
return introEnd + progress * flexibleAuthored;
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
if (artifact.timing.durationPolicy === "loop") {
|
|
1191
|
+
const loop = artifact.timing.loop;
|
|
1192
|
+
if (loop && loop.end > loop.start && localTime > loop.start) {
|
|
1193
|
+
return loop.start + (localTime - loop.start) % (loop.end - loop.start);
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
return Math.max(0, Math.min(localTime, duration));
|
|
1197
|
+
}
|
|
1198
|
+
function easeBezier(t) {
|
|
1199
|
+
const clamped = Math.max(0, Math.min(1, t));
|
|
1200
|
+
return cubicBezier(0.4, 0, 0.2, 1)(clamped);
|
|
1201
|
+
}
|
|
1202
|
+
function applyAnimationPreset(preset, t, direction) {
|
|
1203
|
+
const ease = easeBezier(t);
|
|
1204
|
+
const clamped = Math.max(0, Math.min(1, t));
|
|
1205
|
+
const p = direction === "in" ? ease : 1 - ease;
|
|
1206
|
+
let opacity = 1;
|
|
1207
|
+
let x = 0;
|
|
1208
|
+
let y = 0;
|
|
1209
|
+
let scale = 1;
|
|
1210
|
+
let blur = 0;
|
|
1211
|
+
let typewriterProgress = 1;
|
|
1212
|
+
switch (preset) {
|
|
1213
|
+
case "fade":
|
|
1214
|
+
opacity = p;
|
|
1215
|
+
break;
|
|
1216
|
+
case "typewriter":
|
|
1217
|
+
typewriterProgress = direction === "in" ? clamped : 1 - clamped;
|
|
1218
|
+
break;
|
|
1219
|
+
case "slide-up":
|
|
1220
|
+
opacity = p;
|
|
1221
|
+
y = (1 - p) * 40;
|
|
1222
|
+
break;
|
|
1223
|
+
case "slide-down":
|
|
1224
|
+
opacity = p;
|
|
1225
|
+
y = (p - 1) * 40;
|
|
1226
|
+
break;
|
|
1227
|
+
case "slide-left":
|
|
1228
|
+
opacity = p;
|
|
1229
|
+
x = (1 - p) * 40;
|
|
1230
|
+
break;
|
|
1231
|
+
case "slide-right":
|
|
1232
|
+
opacity = p;
|
|
1233
|
+
x = (p - 1) * 40;
|
|
1234
|
+
break;
|
|
1235
|
+
case "scale-in":
|
|
1236
|
+
opacity = p;
|
|
1237
|
+
scale = 0.8 + p * 0.2;
|
|
1238
|
+
break;
|
|
1239
|
+
case "scale-out":
|
|
1240
|
+
opacity = p;
|
|
1241
|
+
scale = 1.2 - (1 - p) * 0.2;
|
|
1242
|
+
break;
|
|
1243
|
+
case "blur-in":
|
|
1244
|
+
case "blur-out":
|
|
1245
|
+
opacity = p;
|
|
1246
|
+
blur = (1 - p) * 15;
|
|
1247
|
+
break;
|
|
1248
|
+
case "3d-flip":
|
|
1249
|
+
opacity = p;
|
|
1250
|
+
scale = 0.6 + p * 0.4;
|
|
1251
|
+
y = (1 - p) * 25;
|
|
1252
|
+
break;
|
|
1253
|
+
case "scale-pop":
|
|
1254
|
+
opacity = p;
|
|
1255
|
+
scale = p < 0.7 ? p / 0.7 * 1.15 : 1.15 - (p - 0.7) / 0.3 * 0.15;
|
|
1256
|
+
break;
|
|
1257
|
+
case "wave":
|
|
1258
|
+
opacity = p;
|
|
1259
|
+
y = Math.sin(p * Math.PI) * -15;
|
|
1260
|
+
break;
|
|
1261
|
+
case "track-in":
|
|
1262
|
+
opacity = p;
|
|
1263
|
+
blur = (1 - p) * 8;
|
|
1264
|
+
break;
|
|
1265
|
+
case "glitch":
|
|
1266
|
+
opacity = p > 0.05 ? 1 : 0;
|
|
1267
|
+
x = Math.sin(p * 25) * 6 * (1 - p);
|
|
1268
|
+
break;
|
|
1269
|
+
case "none":
|
|
1270
|
+
default:
|
|
1271
|
+
break;
|
|
1272
|
+
}
|
|
1273
|
+
return { opacity, x, y, scale, blur, typewriterProgress };
|
|
1274
|
+
}
|
|
1275
|
+
function computeNodeAnimationState(animation, time, duration) {
|
|
1276
|
+
if (!animation) return { opacity: 1, x: 0, y: 0, scale: 1, blur: 0, typewriterProgress: 1 };
|
|
1277
|
+
const inEnd = Number(animation.inDuration ?? 0);
|
|
1278
|
+
const outStart = duration - Number(animation.outDuration ?? 0);
|
|
1279
|
+
if (time < inEnd && inEnd > 0) {
|
|
1280
|
+
const t = time / inEnd;
|
|
1281
|
+
return applyAnimationPreset(animation.in || "fade", t, "in");
|
|
1282
|
+
} else if (time > outStart && Number(animation.outDuration ?? 0) > 0) {
|
|
1283
|
+
const t = (time - outStart) / Number(animation.outDuration ?? 0);
|
|
1284
|
+
return applyAnimationPreset(animation.out || "fade", t, "out");
|
|
1285
|
+
}
|
|
1286
|
+
return { opacity: 1, x: 0, y: 0, scale: 1, blur: 0, typewriterProgress: 1 };
|
|
1287
|
+
}
|
|
1288
|
+
function evaluateNodeProperty(node, property, fallback, time, duration) {
|
|
1289
|
+
const frames = node.animation?.propertyKeyframes?.[property];
|
|
1290
|
+
if (!frames?.keyframes?.length) return fallback;
|
|
1291
|
+
return evaluateAnimatable(frames, time, duration);
|
|
1292
|
+
}
|
|
1293
|
+
function compileTextTemplate(artifact, context) {
|
|
1294
|
+
assertValidTextTemplateArtifact(artifact);
|
|
1295
|
+
const resolvedControls = resolveControls(artifact.controls, context.controlValues);
|
|
1296
|
+
const document = clone3(artifact.document);
|
|
1297
|
+
for (const control of artifact.controls || []) {
|
|
1298
|
+
const node = findNode(document.nodes, control.target.nodeId);
|
|
1299
|
+
if (node) setPath(node, control.target.propertyPath, resolvedControls[control.id]);
|
|
1300
|
+
}
|
|
1301
|
+
const time = mapTime(context.time, artifact, context.clipDuration);
|
|
1302
|
+
const flexPositions = /* @__PURE__ */ new Map();
|
|
1303
|
+
const containers = (document.nodes || []).filter((n) => n.type === "container");
|
|
1304
|
+
for (const container of containers) {
|
|
1305
|
+
const children = (document.nodes || []).filter((n) => n.parentId === container.id && n.visible !== false);
|
|
1306
|
+
const layout = container.layout || { type: "flex", direction: "column", gap: 0, alignItems: "start", justifyContent: "start" };
|
|
1307
|
+
const direction = layout.direction || "column";
|
|
1308
|
+
const gap = layout.gap || 0;
|
|
1309
|
+
const padTop = layout.paddingTop || 0;
|
|
1310
|
+
const padRight = layout.paddingRight || 0;
|
|
1311
|
+
const padBottom = layout.paddingBottom || 0;
|
|
1312
|
+
const padLeft = layout.paddingLeft || 0;
|
|
1313
|
+
const alignItems = layout.alignItems || "center";
|
|
1314
|
+
const containerX = container.x;
|
|
1315
|
+
const containerY = container.y;
|
|
1316
|
+
if (direction === "column") {
|
|
1317
|
+
let totalChildHeight = 0;
|
|
1318
|
+
let maxChildWidth = 0;
|
|
1319
|
+
const childSizes = [];
|
|
1320
|
+
for (const child of children) {
|
|
1321
|
+
const measured = resolveNodeSize(child, context.runtime);
|
|
1322
|
+
const cw = measured.width;
|
|
1323
|
+
const ch = measured.height;
|
|
1324
|
+
childSizes.push({ id: child.id, width: cw, height: ch });
|
|
1325
|
+
totalChildHeight += ch;
|
|
1326
|
+
maxChildWidth = Math.max(maxChildWidth, cw);
|
|
1327
|
+
}
|
|
1328
|
+
if (children.length > 1) {
|
|
1329
|
+
totalChildHeight += (children.length - 1) * gap;
|
|
1330
|
+
}
|
|
1331
|
+
const containerW = container.width === "auto" ? maxChildWidth + padLeft + padRight : Number(container.width);
|
|
1332
|
+
const containerH = container.height === "auto" ? totalChildHeight + padTop + padBottom : Number(container.height);
|
|
1333
|
+
flexPositions.set(container.id, { x: containerX, y: containerY, width: containerW, height: containerH });
|
|
1334
|
+
let currentY = containerY + padTop;
|
|
1335
|
+
const innerW = Math.max(0, containerW - padLeft - padRight);
|
|
1336
|
+
for (const child of childSizes) {
|
|
1337
|
+
let childX = containerX + padLeft;
|
|
1338
|
+
if (alignItems === "center") {
|
|
1339
|
+
childX = containerX + padLeft + (innerW - child.width) / 2;
|
|
1340
|
+
} else if (alignItems === "end") {
|
|
1341
|
+
childX = containerX + padLeft + (innerW - child.width);
|
|
1342
|
+
} else if (alignItems === "stretch") {
|
|
1343
|
+
child.width = innerW;
|
|
1344
|
+
}
|
|
1345
|
+
flexPositions.set(child.id, { x: childX, y: currentY, width: child.width, height: child.height });
|
|
1346
|
+
currentY += child.height + gap;
|
|
1347
|
+
}
|
|
1348
|
+
} else {
|
|
1349
|
+
let totalChildWidth = 0;
|
|
1350
|
+
let maxChildHeight = 0;
|
|
1351
|
+
const childSizes = [];
|
|
1352
|
+
for (const child of children) {
|
|
1353
|
+
const measured = resolveNodeSize(child, context.runtime);
|
|
1354
|
+
const cw = measured.width;
|
|
1355
|
+
const ch = measured.height;
|
|
1356
|
+
childSizes.push({ id: child.id, width: cw, height: ch });
|
|
1357
|
+
totalChildWidth += cw;
|
|
1358
|
+
maxChildHeight = Math.max(maxChildHeight, ch);
|
|
1359
|
+
}
|
|
1360
|
+
if (children.length > 1) {
|
|
1361
|
+
totalChildWidth += (children.length - 1) * gap;
|
|
1362
|
+
}
|
|
1363
|
+
const containerW = container.width === "auto" ? totalChildWidth + padLeft + padRight : Number(container.width);
|
|
1364
|
+
const containerH = container.height === "auto" ? maxChildHeight + padTop + padBottom : Number(container.height);
|
|
1365
|
+
flexPositions.set(container.id, { x: containerX, y: containerY, width: containerW, height: containerH });
|
|
1366
|
+
let currentX = containerX + padLeft;
|
|
1367
|
+
const innerH = Math.max(0, containerH - padTop - padBottom);
|
|
1368
|
+
for (const child of childSizes) {
|
|
1369
|
+
let childY = containerY + padTop;
|
|
1370
|
+
if (alignItems === "center") {
|
|
1371
|
+
childY = containerY + padTop + (innerH - child.height) / 2;
|
|
1372
|
+
} else if (alignItems === "end") {
|
|
1373
|
+
childY = containerY + padTop + (innerH - child.height);
|
|
1374
|
+
} else if (alignItems === "stretch") {
|
|
1375
|
+
child.height = innerH;
|
|
1376
|
+
}
|
|
1377
|
+
flexPositions.set(child.id, { x: currentX, y: childY, width: child.width, height: child.height });
|
|
1378
|
+
currentX += child.width + gap;
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
const layers = (document.nodes || []).map((node) => {
|
|
1383
|
+
const animState = computeNodeAnimationState(node.animation, time, artifact.timing.duration);
|
|
1384
|
+
const fullText = node.type === "text" ? node.text : void 0;
|
|
1385
|
+
const visibleText = fullText === void 0 ? void 0 : Array.from(fullText).slice(0, Math.floor(fullText.length * animState.typewriterProgress)).join("");
|
|
1386
|
+
const flexPos = flexPositions.get(node.id);
|
|
1387
|
+
const baseX = flexPos?.x ?? evaluateNodeProperty(node, "x", Number(node.x), time, artifact.timing.duration);
|
|
1388
|
+
const baseY = flexPos?.y ?? evaluateNodeProperty(node, "y", Number(node.y), time, artifact.timing.duration);
|
|
1389
|
+
const measuredSize = node.type === "text" ? measureTextNode(node, context.runtime) : void 0;
|
|
1390
|
+
const resolvedWidth = flexPos?.width ?? (node.width === "auto" ? measuredSize?.width ?? 1 : evaluateNodeProperty(node, "width", Number(node.width), time, artifact.timing.duration));
|
|
1391
|
+
const resolvedHeight = flexPos?.height ?? (node.height === "auto" ? measuredSize?.height ?? 1 : evaluateNodeProperty(node, "height", Number(node.height), time, artifact.timing.duration));
|
|
1392
|
+
const baseOpacity = node.type === "shape" ? node.style.fillOpacity ?? 1 : node.type === "media" ? node.style?.opacity ?? 1 : node.type === "container" ? node.style?.opacity ?? 1 : 1;
|
|
1393
|
+
return {
|
|
1394
|
+
id: node.id,
|
|
1395
|
+
type: node.type,
|
|
1396
|
+
parentId: node.parentId,
|
|
1397
|
+
x: baseX + animState.x,
|
|
1398
|
+
y: baseY + animState.y,
|
|
1399
|
+
width: resolvedWidth * animState.scale,
|
|
1400
|
+
height: resolvedHeight * animState.scale,
|
|
1401
|
+
rotation: 0,
|
|
1402
|
+
opacity: node.visible === false ? 0 : animState.opacity * baseOpacity,
|
|
1403
|
+
visible: node.visible !== false && animState.opacity > 0,
|
|
1404
|
+
text: visibleText,
|
|
1405
|
+
assetId: node.type === "media" ? node.assetId : void 0,
|
|
1406
|
+
mediaUrl: node.type === "media" ? node.src : void 0,
|
|
1407
|
+
style: node.style,
|
|
1408
|
+
content: node.type === "text" ? {
|
|
1409
|
+
text: node.text,
|
|
1410
|
+
backgroundPanel: node.backgroundPanel,
|
|
1411
|
+
splitAnimator: node.splitAnimator,
|
|
1412
|
+
typewriterProgress: animState.typewriterProgress,
|
|
1413
|
+
splitProgress: Number(node.animation?.inDuration ?? 0) > 0 ? Math.max(0, Math.min(1, time / Number(node.animation?.inDuration))) : 1
|
|
1414
|
+
} : void 0
|
|
1415
|
+
};
|
|
1416
|
+
});
|
|
1417
|
+
const capabilities2 = {
|
|
1418
|
+
studio: { status: "exact", unsupportedFeatures: [], warnings: [] },
|
|
1419
|
+
editor: { status: "exact", unsupportedFeatures: [], warnings: [] },
|
|
1420
|
+
export: { status: "exact", unsupportedFeatures: [], warnings: [] }
|
|
1421
|
+
};
|
|
1422
|
+
return {
|
|
1423
|
+
kind: "text-template",
|
|
1424
|
+
templateId: artifact.metadata.id,
|
|
1425
|
+
revisionId: artifact.revision.revisionId,
|
|
1426
|
+
contentHash: artifact.revision.contentHash || canonicalTemplateHash(artifact),
|
|
1427
|
+
width: artifact.document.canvas.width,
|
|
1428
|
+
height: artifact.document.canvas.height,
|
|
1429
|
+
duration: artifact.timing.duration,
|
|
1430
|
+
fps: artifact.timing.fps,
|
|
1431
|
+
time,
|
|
1432
|
+
evaluatedScene: { nodes: layers, diagnostics: [] },
|
|
1433
|
+
resolvedControls,
|
|
1434
|
+
layers,
|
|
1435
|
+
dependencies: artifact.dependencies,
|
|
1436
|
+
capabilities: capabilities2,
|
|
1437
|
+
diagnostics: []
|
|
1438
|
+
};
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
// src/templates/textSplitter.ts
|
|
1442
|
+
function splitTextContent(text, mode) {
|
|
1443
|
+
if (!text) return [];
|
|
1444
|
+
const units = [];
|
|
1445
|
+
if (mode === "character") {
|
|
1446
|
+
const chars = Array.from(text);
|
|
1447
|
+
let charPos = 0;
|
|
1448
|
+
chars.forEach((char, idx) => {
|
|
1449
|
+
units.push({
|
|
1450
|
+
text: char,
|
|
1451
|
+
index: idx,
|
|
1452
|
+
totalUnits: chars.length,
|
|
1453
|
+
charStartIndex: charPos,
|
|
1454
|
+
charEndIndex: charPos + char.length,
|
|
1455
|
+
isWhitespace: char === " " || char === " " || char === "\n"
|
|
1456
|
+
});
|
|
1457
|
+
charPos += char.length;
|
|
1458
|
+
});
|
|
1459
|
+
} else if (mode === "word") {
|
|
1460
|
+
const tokens = text.match(/\S+|\s+/g) || [text];
|
|
1461
|
+
let wordIdx = 0;
|
|
1462
|
+
let charPos = 0;
|
|
1463
|
+
tokens.forEach((token) => {
|
|
1464
|
+
const isWhitespace = /^\s+$/.test(token);
|
|
1465
|
+
units.push({
|
|
1466
|
+
text: token,
|
|
1467
|
+
index: isWhitespace ? -1 : wordIdx++,
|
|
1468
|
+
totalUnits: 0,
|
|
1469
|
+
// Assigned below
|
|
1470
|
+
charStartIndex: charPos,
|
|
1471
|
+
charEndIndex: charPos + token.length,
|
|
1472
|
+
isWhitespace
|
|
1473
|
+
});
|
|
1474
|
+
charPos += token.length;
|
|
1475
|
+
});
|
|
1476
|
+
const totalWords = wordIdx;
|
|
1477
|
+
units.forEach((u) => {
|
|
1478
|
+
u.totalUnits = totalWords;
|
|
1479
|
+
});
|
|
1480
|
+
} else if (mode === "line") {
|
|
1481
|
+
const lines = text.split("\n");
|
|
1482
|
+
let charPos = 0;
|
|
1483
|
+
lines.forEach((line, idx) => {
|
|
1484
|
+
units.push({
|
|
1485
|
+
text: line,
|
|
1486
|
+
index: idx,
|
|
1487
|
+
totalUnits: lines.length,
|
|
1488
|
+
charStartIndex: charPos,
|
|
1489
|
+
charEndIndex: charPos + line.length,
|
|
1490
|
+
isWhitespace: line.length === 0
|
|
1491
|
+
});
|
|
1492
|
+
charPos += line.length + 1;
|
|
1493
|
+
});
|
|
1494
|
+
}
|
|
1495
|
+
return units;
|
|
1496
|
+
}
|
|
1497
|
+
function getStaggerRank(index, totalUnits, direction, seed = 42) {
|
|
1498
|
+
if (totalUnits <= 1) return 0;
|
|
1499
|
+
switch (direction) {
|
|
1500
|
+
case "start-to-end":
|
|
1501
|
+
return index;
|
|
1502
|
+
case "end-to-start":
|
|
1503
|
+
return totalUnits - 1 - index;
|
|
1504
|
+
case "center-out": {
|
|
1505
|
+
const center = (totalUnits - 1) / 2;
|
|
1506
|
+
return Math.abs(index - center);
|
|
1507
|
+
}
|
|
1508
|
+
case "edges-in": {
|
|
1509
|
+
const center = (totalUnits - 1) / 2;
|
|
1510
|
+
return center - Math.abs(index - center);
|
|
1511
|
+
}
|
|
1512
|
+
case "random": {
|
|
1513
|
+
let t = (index + 1) * (seed + 1831565813);
|
|
1514
|
+
t = Math.imul(t ^ t >>> 15, t | 1);
|
|
1515
|
+
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
|
|
1516
|
+
const rand = ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
1517
|
+
return rand * totalUnits;
|
|
1518
|
+
}
|
|
1519
|
+
default:
|
|
1520
|
+
return index;
|
|
1521
|
+
}
|
|
1522
|
+
}
|
|
1523
|
+
function evaluateSplitTextTransforms(units, animator, progress, direction = "in") {
|
|
1524
|
+
const {
|
|
1525
|
+
direction: staggerDir,
|
|
1526
|
+
delayPerUnit,
|
|
1527
|
+
overlap,
|
|
1528
|
+
randomSeed = 1234,
|
|
1529
|
+
initialTransform,
|
|
1530
|
+
easing = "ease",
|
|
1531
|
+
bezier,
|
|
1532
|
+
durationPerUnit = 0.4
|
|
1533
|
+
} = animator;
|
|
1534
|
+
const validUnits = units.filter((u) => !u.isWhitespace || animator.splitBy === "character");
|
|
1535
|
+
const totalUnits = validUnits.length;
|
|
1536
|
+
if (totalUnits === 0) return [];
|
|
1537
|
+
const maxRank = Math.max(
|
|
1538
|
+
...validUnits.map((u) => getStaggerRank(u.index >= 0 ? u.index : 0, totalUnits, staggerDir, randomSeed))
|
|
1539
|
+
);
|
|
1540
|
+
return units.map((unit) => {
|
|
1541
|
+
if (unit.isWhitespace && animator.splitBy !== "character") {
|
|
1542
|
+
return {
|
|
1543
|
+
unit,
|
|
1544
|
+
progress: 1,
|
|
1545
|
+
x: 0,
|
|
1546
|
+
y: 0,
|
|
1547
|
+
z: 0,
|
|
1548
|
+
scale: 1,
|
|
1549
|
+
scaleX: 1,
|
|
1550
|
+
scaleY: 1,
|
|
1551
|
+
rotation: 0,
|
|
1552
|
+
rotateX: 0,
|
|
1553
|
+
rotateY: 0,
|
|
1554
|
+
opacity: 1,
|
|
1555
|
+
blur: 0,
|
|
1556
|
+
letterSpacingOffset: 0
|
|
1557
|
+
};
|
|
1558
|
+
}
|
|
1559
|
+
const rank = getStaggerRank(unit.index >= 0 ? unit.index : 0, totalUnits, staggerDir, randomSeed);
|
|
1560
|
+
const staggerNormalized = maxRank > 0 ? rank / maxRank * (1 - overlap) : 0;
|
|
1561
|
+
const unitTimeSpan = 1 - staggerNormalized;
|
|
1562
|
+
let unitProgress = unitTimeSpan > 0 ? Math.max(0, Math.min(1, (progress - staggerNormalized) / unitTimeSpan)) : 1;
|
|
1563
|
+
const easedT = applyEasing(unitProgress, easing, bezier);
|
|
1564
|
+
const p = direction === "in" ? easedT : 1 - easedT;
|
|
1565
|
+
const init = initialTransform;
|
|
1566
|
+
const x = (init.x ?? 0) * (1 - p);
|
|
1567
|
+
const y = (init.y ?? 0) * (1 - p);
|
|
1568
|
+
const z = (init.z ?? 0) * (1 - p);
|
|
1569
|
+
const scale = (init.scale ?? 1) * (1 - p) + 1 * p;
|
|
1570
|
+
const scaleX = (init.scaleX ?? 1) * (1 - p) + 1 * p;
|
|
1571
|
+
const scaleY = (init.scaleY ?? 1) * (1 - p) + 1 * p;
|
|
1572
|
+
const rotation = (init.rotation ?? 0) * (1 - p);
|
|
1573
|
+
const rotateX = (init.rotateX ?? 0) * (1 - p);
|
|
1574
|
+
const rotateY = (init.rotateY ?? 0) * (1 - p);
|
|
1575
|
+
const opacity = (init.opacity ?? 0) * (1 - p) + 1 * p;
|
|
1576
|
+
const blur = (init.blur ?? 0) * (1 - p);
|
|
1577
|
+
const letterSpacingOffset = (init.letterSpacingOffset ?? 0) * (1 - p);
|
|
1578
|
+
return {
|
|
1579
|
+
unit,
|
|
1580
|
+
progress: p,
|
|
1581
|
+
x,
|
|
1582
|
+
y,
|
|
1583
|
+
z,
|
|
1584
|
+
scale,
|
|
1585
|
+
scaleX,
|
|
1586
|
+
scaleY,
|
|
1587
|
+
rotation,
|
|
1588
|
+
rotateX,
|
|
1589
|
+
rotateY,
|
|
1590
|
+
opacity,
|
|
1591
|
+
blur,
|
|
1592
|
+
letterSpacingOffset
|
|
1593
|
+
};
|
|
1594
|
+
});
|
|
1595
|
+
}
|
|
1596
|
+
function getSpanForCharIndex(spans, charIndex) {
|
|
1597
|
+
if (!spans || spans.length === 0) return void 0;
|
|
1598
|
+
return spans.find((s) => charIndex >= s.start && charIndex < s.end);
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
// src/rendering/textTemplateRenderer.ts
|
|
1602
|
+
function isObject(value) {
|
|
1603
|
+
return Boolean(value && typeof value === "object");
|
|
1604
|
+
}
|
|
1605
|
+
function isCanonicalArtifact(value) {
|
|
1606
|
+
return isObject(value) && value.kind === "text-template" && isObject(value.document) && Array.isArray(value.document.nodes);
|
|
1607
|
+
}
|
|
1608
|
+
function hasLegacyLayers(value) {
|
|
1609
|
+
return isObject(value) && (Array.isArray(value.layers) || Array.isArray(value.elements));
|
|
1610
|
+
}
|
|
1611
|
+
function resolveTextTemplateArtifact(input, options = {}) {
|
|
1612
|
+
if (!isObject(input)) return null;
|
|
1613
|
+
if (isCanonicalArtifact(input)) return normalizeTextTemplateArtifact(input);
|
|
1614
|
+
for (const key of ["templateData", "injectedData", "templateDefinition", "lottieData", "artifact", "snapshot"]) {
|
|
1615
|
+
const nested = input[key];
|
|
1616
|
+
if (nested && nested !== input) {
|
|
1617
|
+
const resolved = resolveTextTemplateArtifact(nested, options);
|
|
1618
|
+
if (resolved) return resolved;
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
if (options.allowLegacy !== false && hasLegacyLayers(input)) return normalizeTextTemplateArtifact(input);
|
|
1622
|
+
return null;
|
|
1623
|
+
}
|
|
1624
|
+
function getTextTemplateControls(input) {
|
|
1625
|
+
return resolveTextTemplateArtifact(input)?.controls ?? [];
|
|
1626
|
+
}
|
|
1627
|
+
function finite2(value, fallback) {
|
|
1628
|
+
const number = Number(value);
|
|
1629
|
+
return Number.isFinite(number) ? number : fallback;
|
|
1630
|
+
}
|
|
1631
|
+
function cssColor(value, fallback) {
|
|
1632
|
+
return typeof value === "string" && value.trim() ? value : fallback;
|
|
1633
|
+
}
|
|
1634
|
+
function roundedRect(ctx, x, y, width, height, radius) {
|
|
1635
|
+
const r = Math.max(0, Math.min(radius, Math.min(Math.abs(width), Math.abs(height)) / 2));
|
|
1636
|
+
if (r <= 0) {
|
|
1637
|
+
ctx.rect(x, y, width, height);
|
|
1638
|
+
return;
|
|
1639
|
+
}
|
|
1640
|
+
ctx.moveTo(x + r, y);
|
|
1641
|
+
ctx.arcTo(x + width, y, x + width, y + height, r);
|
|
1642
|
+
ctx.arcTo(x + width, y + height, x, y + height, r);
|
|
1643
|
+
ctx.arcTo(x, y + height, x, y, r);
|
|
1644
|
+
ctx.arcTo(x, y, x + width, y, r);
|
|
1645
|
+
ctx.closePath();
|
|
1646
|
+
}
|
|
1647
|
+
function drawPanel(ctx, panel, x, y, width, height) {
|
|
1648
|
+
ctx.save();
|
|
1649
|
+
const left = finite2(panel.paddingLeft, 0);
|
|
1650
|
+
const right = finite2(panel.paddingRight, left);
|
|
1651
|
+
const top = finite2(panel.paddingTop, 0);
|
|
1652
|
+
const bottom = finite2(panel.paddingBottom, top);
|
|
1653
|
+
const px = x - left;
|
|
1654
|
+
const py = y - top;
|
|
1655
|
+
const pw = width + left + right;
|
|
1656
|
+
const ph = height + top + bottom;
|
|
1657
|
+
ctx.beginPath();
|
|
1658
|
+
roundedRect(ctx, px, py, pw, ph, finite2(panel.borderRadius, 0));
|
|
1659
|
+
ctx.fillStyle = cssColor(panel.color, "#000000");
|
|
1660
|
+
ctx.globalAlpha *= Math.max(0, Math.min(1, finite2(panel.opacity, 1)));
|
|
1661
|
+
ctx.fill();
|
|
1662
|
+
if (finite2(panel.borderWidth, 0) > 0 && panel.borderColor) {
|
|
1663
|
+
ctx.lineWidth = finite2(panel.borderWidth, 1);
|
|
1664
|
+
ctx.strokeStyle = cssColor(panel.borderColor, "#000000");
|
|
1665
|
+
ctx.stroke();
|
|
1666
|
+
}
|
|
1667
|
+
ctx.restore();
|
|
1668
|
+
}
|
|
1669
|
+
function letterSpacedText(ctx, text, x, y, maxWidth, letterSpacing) {
|
|
1670
|
+
if (!letterSpacing) {
|
|
1671
|
+
ctx.fillText(text, x, y, maxWidth);
|
|
1672
|
+
return;
|
|
1673
|
+
}
|
|
1674
|
+
const glyphs = Array.from(text);
|
|
1675
|
+
const widths = glyphs.map((glyph) => ctx.measureText(glyph).width);
|
|
1676
|
+
const total = widths.reduce((sum, value) => sum + value, 0) + Math.max(0, glyphs.length - 1) * letterSpacing;
|
|
1677
|
+
const align = ctx.textAlign;
|
|
1678
|
+
let cursor = x;
|
|
1679
|
+
if (align === "center") cursor -= total / 2;
|
|
1680
|
+
if (align === "right") cursor -= total;
|
|
1681
|
+
ctx.textAlign = "left";
|
|
1682
|
+
for (let index = 0; index < glyphs.length; index += 1) {
|
|
1683
|
+
ctx.fillText(glyphs[index], cursor, y);
|
|
1684
|
+
cursor += widths[index] + letterSpacing;
|
|
1685
|
+
}
|
|
1686
|
+
ctx.textAlign = align;
|
|
1687
|
+
}
|
|
1688
|
+
function drawTextLayer(ctx, layer, style) {
|
|
1689
|
+
const text = layer.text ?? "";
|
|
1690
|
+
const fontSize = finite2(style.fontSize, 48);
|
|
1691
|
+
const fontWeight = style.fontWeight ?? 400;
|
|
1692
|
+
const fontStyle = "normal";
|
|
1693
|
+
const fontFamily = String(style.fontFamily || "sans-serif").replace(/"/g, "");
|
|
1694
|
+
ctx.font = `${fontStyle} ${fontWeight} ${fontSize}px "${fontFamily}"`;
|
|
1695
|
+
ctx.fillStyle = cssColor(style.textColor, "#FFFFFF");
|
|
1696
|
+
ctx.textAlign = style.textAlign ?? "left";
|
|
1697
|
+
ctx.textBaseline = style.verticalAlign === "top" ? "top" : style.verticalAlign === "bottom" ? "bottom" : "middle";
|
|
1698
|
+
const lineHeight = fontSize * finite2(style.lineHeight, 1.2);
|
|
1699
|
+
const maxWidth = Math.max(1, layer.width);
|
|
1700
|
+
const lines = style.overflow === "wrap" ? wrapText(ctx, text, maxWidth) : [text];
|
|
1701
|
+
const blockHeight = lines.length * lineHeight;
|
|
1702
|
+
const startY = style.verticalAlign === "top" ? layer.y : style.verticalAlign === "bottom" ? layer.y + layer.height - blockHeight + lineHeight : layer.y + (layer.height - blockHeight) / 2 + lineHeight / 2;
|
|
1703
|
+
const anchorX = style.textAlign === "center" ? layer.x + layer.width / 2 : style.textAlign === "right" ? layer.x + layer.width : layer.x;
|
|
1704
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
1705
|
+
letterSpacedText(ctx, lines[index], anchorX, startY + index * lineHeight, maxWidth, finite2(style.letterSpacing, 0));
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
function drawSplitTextLayer(ctx, layer, style, animator, progress) {
|
|
1709
|
+
const text = layer.text ?? "";
|
|
1710
|
+
const fontSize = finite2(style.fontSize, 48);
|
|
1711
|
+
const fontFamily = String(style.fontFamily || "sans-serif").replace(/"/g, "");
|
|
1712
|
+
ctx.font = `normal ${style.fontWeight ?? 400} ${fontSize}px "${fontFamily}"`;
|
|
1713
|
+
ctx.textBaseline = "middle";
|
|
1714
|
+
ctx.textAlign = "left";
|
|
1715
|
+
ctx.fillStyle = cssColor(style.textColor, "#FFFFFF");
|
|
1716
|
+
const units = splitTextContent(text, animator.splitBy);
|
|
1717
|
+
const transforms = evaluateSplitTextTransforms(units, animator, progress, "in");
|
|
1718
|
+
const lineHeight = fontSize * finite2(style.lineHeight, 1.2);
|
|
1719
|
+
const fullWidth = ctx.measureText(text.split("\n")[0] ?? "").width;
|
|
1720
|
+
let cursorX = style.textAlign === "center" ? layer.x + (layer.width - fullWidth) / 2 : style.textAlign === "right" ? layer.x + layer.width - fullWidth : layer.x;
|
|
1721
|
+
let cursorY = layer.y + layer.height / 2;
|
|
1722
|
+
for (const item of transforms) {
|
|
1723
|
+
if (item.unit.text.includes("\n")) {
|
|
1724
|
+
cursorX = layer.x;
|
|
1725
|
+
cursorY += lineHeight;
|
|
1726
|
+
continue;
|
|
1727
|
+
}
|
|
1728
|
+
const advance = ctx.measureText(item.unit.text).width;
|
|
1729
|
+
ctx.save();
|
|
1730
|
+
ctx.globalAlpha *= Math.max(0, Math.min(1, item.opacity));
|
|
1731
|
+
if (item.blur > 0) ctx.filter = `blur(${item.blur}px)`;
|
|
1732
|
+
ctx.translate(cursorX + item.x, cursorY + item.y);
|
|
1733
|
+
ctx.rotate(item.rotation * Math.PI / 180);
|
|
1734
|
+
ctx.scale(item.scaleX, item.scaleY);
|
|
1735
|
+
ctx.fillText(item.unit.text, 0, 0);
|
|
1736
|
+
ctx.restore();
|
|
1737
|
+
cursorX += advance + item.letterSpacingOffset;
|
|
1738
|
+
}
|
|
1739
|
+
}
|
|
1740
|
+
function wrapText(ctx, text, maxWidth) {
|
|
1741
|
+
const words = text.split(/\s+/);
|
|
1742
|
+
const lines = [];
|
|
1743
|
+
let line = "";
|
|
1744
|
+
for (const word of words) {
|
|
1745
|
+
const candidate = line ? `${line} ${word}` : word;
|
|
1746
|
+
if (line && ctx.measureText(candidate).width > maxWidth) {
|
|
1747
|
+
lines.push(line);
|
|
1748
|
+
line = word;
|
|
1749
|
+
} else {
|
|
1750
|
+
line = candidate;
|
|
1751
|
+
}
|
|
1752
|
+
}
|
|
1753
|
+
if (line || !lines.length) lines.push(line);
|
|
1754
|
+
return lines;
|
|
1755
|
+
}
|
|
1756
|
+
function drawLayer(ctx, layer) {
|
|
1757
|
+
if (!layer.visible || layer.opacity <= 0) return;
|
|
1758
|
+
ctx.save();
|
|
1759
|
+
ctx.globalAlpha *= Math.max(0, Math.min(1, layer.opacity));
|
|
1760
|
+
if (layer.type === "shape") {
|
|
1761
|
+
const style = layer.style ?? {};
|
|
1762
|
+
ctx.beginPath();
|
|
1763
|
+
roundedRect(ctx, layer.x, layer.y, layer.width, layer.height, finite2(style.borderRadius, 0));
|
|
1764
|
+
ctx.fillStyle = cssColor(style.fillColor, "#000000");
|
|
1765
|
+
ctx.globalAlpha *= Math.max(0, Math.min(1, finite2(style.fillOpacity, 1)));
|
|
1766
|
+
ctx.fill();
|
|
1767
|
+
if (finite2(style.strokeWidth, 0) > 0 && style.strokeColor) {
|
|
1768
|
+
ctx.lineWidth = finite2(style.strokeWidth, 1);
|
|
1769
|
+
ctx.strokeStyle = cssColor(style.strokeColor, "#000000");
|
|
1770
|
+
ctx.stroke();
|
|
1771
|
+
}
|
|
1772
|
+
} else if (layer.type === "container") {
|
|
1773
|
+
const style = layer.style ?? {};
|
|
1774
|
+
if (style.backgroundColor) {
|
|
1775
|
+
ctx.beginPath();
|
|
1776
|
+
roundedRect(ctx, layer.x, layer.y, layer.width, layer.height, finite2(style.borderRadius, 0));
|
|
1777
|
+
ctx.fillStyle = cssColor(style.backgroundColor, "#000000");
|
|
1778
|
+
ctx.globalAlpha *= Math.max(0, Math.min(1, finite2(style.backgroundOpacity, 1)));
|
|
1779
|
+
ctx.fill();
|
|
1780
|
+
}
|
|
1781
|
+
} else if (layer.type === "text" || layer.type === "rich-text") {
|
|
1782
|
+
const style = layer.style ?? {};
|
|
1783
|
+
if (style && layer.content?.backgroundPanel) {
|
|
1784
|
+
drawPanel(ctx, layer.content.backgroundPanel, layer.x, layer.y, layer.width, layer.height);
|
|
1785
|
+
}
|
|
1786
|
+
const splitAnimator = layer.content?.splitAnimator;
|
|
1787
|
+
if (splitAnimator) {
|
|
1788
|
+
drawSplitTextLayer(ctx, layer, style, splitAnimator, finite2(layer.content?.splitProgress, 1));
|
|
1789
|
+
} else {
|
|
1790
|
+
drawTextLayer(ctx, layer, style);
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
ctx.restore();
|
|
1794
|
+
}
|
|
1795
|
+
function renderTextTemplateToCanvas(ctx, input) {
|
|
1796
|
+
const artifact = input.artifact;
|
|
1797
|
+
const runtime = {
|
|
1798
|
+
...input.context.runtime || {},
|
|
1799
|
+
// Measure with the exact Canvas context that will draw the layer. This
|
|
1800
|
+
// makes auto-sized nodes use loaded font metrics instead of a fixed box.
|
|
1801
|
+
measureText: (text, style) => {
|
|
1802
|
+
const family = String(style.fontFamily || "sans-serif").replace(/"/g, "");
|
|
1803
|
+
ctx.save();
|
|
1804
|
+
ctx.font = `${style.fontWeight ?? 400} ${finite2(style.fontSize, 48)}px "${family}"`;
|
|
1805
|
+
const width = ctx.measureText(text).width + Math.max(0, text.length - 1) * finite2(style.letterSpacing, 0);
|
|
1806
|
+
ctx.restore();
|
|
1807
|
+
return { width };
|
|
1808
|
+
}
|
|
1809
|
+
};
|
|
1810
|
+
const compiled = compileTextTemplate(artifact, {
|
|
1811
|
+
time: input.context.time,
|
|
1812
|
+
target: input.context.environment === "preview" || input.context.environment === "thumbnail" ? "editor" : input.context.environment,
|
|
1813
|
+
clipDuration: input.context.clipDuration,
|
|
1814
|
+
controlValues: input.context.controlValues,
|
|
1815
|
+
runtime
|
|
1816
|
+
});
|
|
1817
|
+
const outputWidth = finite2(input.context.width, compiled.width);
|
|
1818
|
+
const outputHeight = finite2(input.context.height, compiled.height);
|
|
1819
|
+
const scaleX = outputWidth / compiled.width;
|
|
1820
|
+
const scaleY = outputHeight / compiled.height;
|
|
1821
|
+
ctx.save();
|
|
1822
|
+
ctx.scale(scaleX, scaleY);
|
|
1823
|
+
if (artifact.document.canvas.backgroundColor) {
|
|
1824
|
+
ctx.fillStyle = artifact.document.canvas.backgroundColor;
|
|
1825
|
+
ctx.fillRect(0, 0, compiled.width, compiled.height);
|
|
1826
|
+
}
|
|
1827
|
+
for (const layer of compiled.layers) drawLayer(ctx, layer);
|
|
1828
|
+
ctx.restore();
|
|
1829
|
+
return {
|
|
1830
|
+
capability: "text-template",
|
|
1831
|
+
revisionId: compiled.revisionId,
|
|
1832
|
+
contentHash: compiled.contentHash,
|
|
1833
|
+
rendererVersion: artifact.revision.rendererVersion || TEXT_TEMPLATE_RENDERER_VERSION,
|
|
1834
|
+
diagnostics: compiled.diagnostics.map((diagnostic) => ({ ...diagnostic, capability: "text-template" })),
|
|
1835
|
+
usedDependencies: [
|
|
1836
|
+
...artifact.dependencies.fonts.map((font) => `font:${font.family}:${font.weight ?? "normal"}`),
|
|
1837
|
+
...artifact.dependencies.assets.map((asset) => `asset:${asset.id}:${asset.contentHash}`),
|
|
1838
|
+
...artifact.dependencies.textEffects.map((effect) => `text-effect:${effect.effectId}:${effect.revisionId}:${effect.contentHash}`)
|
|
1839
|
+
],
|
|
1840
|
+
compiledTemplate: compiled
|
|
1841
|
+
};
|
|
1842
|
+
}
|
|
1843
|
+
|
|
1844
|
+
// src/rendering/index.ts
|
|
1845
|
+
function renderCanonicalToCanvas(ctx, input) {
|
|
1846
|
+
if (input.capability === "text-template") {
|
|
1847
|
+
const artifact = resolveTextTemplateArtifact(input.artifact);
|
|
1848
|
+
if (!artifact) {
|
|
1849
|
+
return {
|
|
1850
|
+
capability: "text-template",
|
|
1851
|
+
rendererVersion: TEXT_TEMPLATE_RENDERER_VERSION,
|
|
1852
|
+
diagnostics: [{ level: "error", code: "template-artifact-missing", message: "A full text-template artifact is required for rendering.", capability: "text-template" }],
|
|
1853
|
+
usedDependencies: []
|
|
1854
|
+
};
|
|
1855
|
+
}
|
|
1856
|
+
return renderTextTemplateToCanvas(ctx, { artifact, context: input.context });
|
|
1857
|
+
}
|
|
1858
|
+
return renderTextEffectToCanvas(ctx, { source: input.source, context: input.context });
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1861
|
+
export {
|
|
1862
|
+
cubicBezier,
|
|
1863
|
+
solveSpring,
|
|
1864
|
+
isKeyframed,
|
|
1865
|
+
evaluateAnimatable,
|
|
1866
|
+
applyEasing,
|
|
1867
|
+
interpolateValue,
|
|
1868
|
+
interpolateColorString,
|
|
1869
|
+
createKeyframed,
|
|
1870
|
+
addKeyframe,
|
|
1871
|
+
removeTemplateKeyframe,
|
|
1872
|
+
CANONICAL_SCHEMA_VERSION,
|
|
1873
|
+
CANONICAL_RENDERER_VERSION,
|
|
1874
|
+
canonicalContentHash,
|
|
1875
|
+
canonicalizeSceneDocument,
|
|
1876
|
+
normalizeTextEffect,
|
|
1877
|
+
normalizeTextTemplate,
|
|
1878
|
+
evaluateTextEffect,
|
|
1879
|
+
evaluateTextTemplate,
|
|
1880
|
+
getRenderCapabilities,
|
|
1881
|
+
splitTextContent,
|
|
1882
|
+
evaluateSplitTextTransforms,
|
|
1883
|
+
getSpanForCharIndex,
|
|
1884
|
+
TEXT_TEMPLATE_KIND,
|
|
1885
|
+
TEXT_TEMPLATE_SCHEMA_VERSION,
|
|
1886
|
+
TEXT_TEMPLATE_RENDERER_VERSION,
|
|
1887
|
+
canonicalTemplateHash,
|
|
1888
|
+
normalizeTextTemplateArtifact,
|
|
1889
|
+
validateTextTemplateArtifact,
|
|
1890
|
+
assertValidTextTemplateArtifact,
|
|
1891
|
+
compileTextTemplate,
|
|
1892
|
+
renderTextEffectToCanvas,
|
|
1893
|
+
resolveTextTemplateArtifact,
|
|
1894
|
+
getTextTemplateControls,
|
|
1895
|
+
renderTextTemplateToCanvas,
|
|
1896
|
+
renderCanonicalToCanvas
|
|
1897
|
+
};
|
|
1898
|
+
//# sourceMappingURL=chunk-Q7XYDP4X.js.map
|