@clypra-studio/engine 1.10.1 → 2.0.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/index.cjs CHANGED
@@ -37,11 +37,19 @@ __export(index_exports, {
37
37
  AnimationValidator: () => AnimationValidator,
38
38
  AssetRegistry: () => AssetRegistry,
39
39
  BUILTIN_CANVAS_TEMPLATES: () => BUILTIN_CANVAS_TEMPLATES,
40
+ BUILT_IN_CAPTION_PRESETS: () => BUILT_IN_CAPTION_PRESETS,
40
41
  BoundingBoxSchema: () => BoundingBoxSchema,
41
42
  CANONICAL_RENDERER_VERSION: () => CANONICAL_RENDERER_VERSION,
42
43
  CANONICAL_SCHEMA_VERSION: () => CANONICAL_SCHEMA_VERSION,
44
+ CAPTION_PRESET_BOLD_PUNCH: () => CAPTION_PRESET_BOLD_PUNCH,
45
+ CAPTION_PRESET_CINEMATIC_GOLD: () => CAPTION_PRESET_CINEMATIC_GOLD,
46
+ CAPTION_PRESET_CLASSIC_SUBTITLE: () => CAPTION_PRESET_CLASSIC_SUBTITLE,
47
+ CAPTION_PRESET_CLEAN_MINIMAL: () => CAPTION_PRESET_CLEAN_MINIMAL,
48
+ CAPTION_PRESET_KARAOKE_POP: () => CAPTION_PRESET_KARAOKE_POP,
49
+ CAPTION_PRESET_NEON_GLOW: () => CAPTION_PRESET_NEON_GLOW,
43
50
  COMPOSITION_PRESETS: () => COMPOSITION_PRESETS,
44
51
  CanvasDevice: () => CanvasDevice,
52
+ CaptionEngine: () => CaptionEngine,
45
53
  CommandBufferBuilder: () => CommandBufferBuilder,
46
54
  CommandExecutor: () => CommandExecutor,
47
55
  CommandHistory: () => CommandHistory,
@@ -50,6 +58,9 @@ __export(index_exports, {
50
58
  DEFAULT_DURATION: () => DEFAULT_DURATION,
51
59
  DEFAULT_FONT_SIZE: () => DEFAULT_FONT_SIZE,
52
60
  DEFAULT_FPS: () => DEFAULT_FPS,
61
+ DEFAULT_MAX_LINES: () => DEFAULT_MAX_LINES,
62
+ DEFAULT_MAX_WORDS_PER_LINE: () => DEFAULT_MAX_WORDS_PER_LINE,
63
+ DEFAULT_PAUSE_BREAK_MS: () => DEFAULT_PAUSE_BREAK_MS,
53
64
  DEFAULT_TEXT_STYLE: () => DEFAULT_TEXT_STYLE,
54
65
  DataBindingAuthoringEngine: () => DataBindingAuthoringEngine,
55
66
  DataBindingEngine: () => DataBindingEngine,
@@ -102,6 +113,7 @@ __export(index_exports, {
102
113
  Mp4VideoEncoder: () => Mp4VideoEncoder,
103
114
  NodeRegistry: () => NodeRegistry,
104
115
  NullBackend: () => NullBackend,
116
+ PAUSE_RELEASE_THRESHOLD_MS: () => PAUSE_RELEASE_THRESHOLD_MS,
105
117
  PngSequenceEncoder: () => PngSequenceEncoder,
106
118
  PrimitiveRegistry: () => PrimitiveRegistry,
107
119
  ProResVideoEncoder: () => ProResVideoEncoder,
@@ -198,6 +210,8 @@ __export(index_exports, {
198
210
  clearRecipeCache: () => clearRecipeCache,
199
211
  cloneSceneWithNewIds: () => cloneSceneWithNewIds,
200
212
  commandExecutor: () => commandExecutor,
213
+ compileBubblePath: () => compileBubblePath,
214
+ compileBubbleSvg: () => compileBubbleSvg,
201
215
  compileSemanticOverlay: () => compileSemanticOverlay,
202
216
  compileTextTemplate: () => compileTextTemplate,
203
217
  componentRegistry: () => componentRegistry,
@@ -209,10 +223,12 @@ __export(index_exports, {
209
223
  createBlankLottie: () => createBlankLottie,
210
224
  createBlankScratchTemplate: () => createBlankScratchTemplate,
211
225
  createBlankTextEffectConfig: () => createBlankTextEffectConfig,
226
+ createBubbleAssetDefinition: () => createBubbleAssetDefinition,
212
227
  createCanvas: () => createCanvas,
213
228
  createDefaultRevealTrack: () => createDefaultRevealTrack,
214
229
  createDefaultScratchBlock: () => createDefaultScratchBlock,
215
230
  createEmptyScene: () => createEmptyScene,
231
+ createFreeformBubbleAssetDefinition: () => createFreeformBubbleAssetDefinition,
216
232
  createKeyframed: () => createKeyframed,
217
233
  createPulseOpacityTrack: () => createPulseOpacityTrack,
218
234
  cubicBezier: () => cubicBezier,
@@ -228,6 +244,7 @@ __export(index_exports, {
228
244
  downloadLottieMp4: () => downloadLottieMp4,
229
245
  downloadPngSequenceZip: () => downloadPngSequenceZip,
230
246
  downloadSceneWebM: () => downloadSceneWebM,
247
+ drawChalkSpeechBubble: () => drawChalkSpeechBubble,
231
248
  drawComicBurst: () => drawComicBurst,
232
249
  drawPerCharText: () => drawPerCharText,
233
250
  drawRibbonBanner: () => drawRibbonBanner,
@@ -266,6 +283,7 @@ __export(index_exports, {
266
283
  getAnimatableParamsForLayer: () => getAnimatableParamsForLayer,
267
284
  getCanvasTemplatePreset: () => getCanvasTemplatePreset,
268
285
  getCanvasTemplatesByCategory: () => getCanvasTemplatesByCategory,
286
+ getCaptionPreset: () => getCaptionPreset,
269
287
  getClipLayout: () => getClipLayout,
270
288
  getDefaultText: () => getDefaultText,
271
289
  getEffectByIdForApi: () => getEffectByIdForApi,
@@ -294,6 +312,7 @@ __export(index_exports, {
294
312
  getTransitionsByCategory: () => getTransitionsByCategory,
295
313
  getTransitionsByTags: () => getTransitionsByTags,
296
314
  getWebMFrameCount: () => getWebMFrameCount,
315
+ hashCaptionPreset: () => hashCaptionPreset,
297
316
  hexToLottieColor: () => hexToLottieColor,
298
317
  hexToLottieRgb: () => hexToLottieRgb,
299
318
  hexToRgba: () => hexToRgba,
@@ -347,8 +366,11 @@ __export(index_exports, {
347
366
  removeKeyframe: () => removeKeyframe,
348
367
  removeTemplateKeyframe: () => removeTemplateKeyframe,
349
368
  removeTrack: () => removeTrack,
369
+ renderBubbleAssetDefinition: () => renderBubbleAssetDefinition,
350
370
  renderBubbleBackground: () => renderBubbleBackground,
371
+ renderBubblePresetBackground: () => renderBubblePresetBackground,
351
372
  renderCanonicalToCanvas: () => renderCanonicalToCanvas,
373
+ renderCaptionToCanvas: () => renderCaptionToCanvas,
352
374
  renderEvaluatedSceneToCanvas: () => renderEvaluatedSceneToCanvas,
353
375
  renderPngSequence: () => renderPngSequence,
354
376
  renderSceneWebM: () => renderSceneWebM,
@@ -419,6 +441,7 @@ __export(index_exports, {
419
441
  updateTrack: () => updateTrack,
420
442
  updateTrackMatte: () => updateTrackMatte,
421
443
  upsertKeyframe: () => upsertKeyframe,
444
+ validateBubbleAssetDefinition: () => validateBubbleAssetDefinition,
422
445
  validateEffectDefinition: () => validateEffectDefinition,
423
446
  validateEffectDefinitionStrict: () => validateEffectDefinitionStrict,
424
447
  validateTextEffectDefinition: () => validateTextEffectDefinition,
@@ -13929,6 +13952,170 @@ function drawSpeechBubble(ctx, x, y, width, height, fillColor = "#ffffff", strok
13929
13952
  ctx.fill();
13930
13953
  ctx.restore();
13931
13954
  }
13955
+ function drawConfigurableSpeechBubble(ctx, x, y, width, height, appearance) {
13956
+ const tailHeight = Math.min(appearance.tailSize, height * 0.38);
13957
+ const bodyHeight = height - tailHeight;
13958
+ const radius = Math.min(appearance.cornerRadius, bodyHeight / 2);
13959
+ const tailCenter = appearance.tailPosition === "bottom-left" ? x + width * 0.2 : appearance.tailPosition === "bottom-right" ? x + width * 0.8 : x + width * 0.5;
13960
+ const tailWidth = Math.min(Math.max(18, appearance.tailSize * 1.5), width * 0.28);
13961
+ ctx.save();
13962
+ ctx.globalAlpha = Math.max(0, Math.min(1, appearance.fillOpacity));
13963
+ ctx.fillStyle = appearance.fillColor;
13964
+ ctx.strokeStyle = appearance.strokeColor;
13965
+ ctx.lineWidth = Math.max(0, appearance.strokeWidth);
13966
+ ctx.beginPath();
13967
+ if (typeof ctx.roundRect === "function") {
13968
+ ctx.roundRect(x, y, width, bodyHeight, radius);
13969
+ } else {
13970
+ ctx.rect(x, y, width, bodyHeight);
13971
+ }
13972
+ ctx.fill();
13973
+ if (appearance.strokeWidth > 0) ctx.stroke();
13974
+ ctx.beginPath();
13975
+ ctx.moveTo(tailCenter - tailWidth / 2, y + bodyHeight - 1);
13976
+ ctx.quadraticCurveTo(tailCenter - tailWidth * 0.15, y + bodyHeight + tailHeight * 0.7, tailCenter, y + height);
13977
+ ctx.quadraticCurveTo(tailCenter + tailWidth * 0.18, y + bodyHeight + tailHeight * 0.55, tailCenter + tailWidth / 2, y + bodyHeight - 1);
13978
+ ctx.closePath();
13979
+ ctx.fill();
13980
+ if (appearance.strokeWidth > 0) ctx.stroke();
13981
+ ctx.restore();
13982
+ }
13983
+ function renderBubblePresetBackground(ctx, preset, x, y, width, height) {
13984
+ const appearance = preset.appearance;
13985
+ if (!appearance) return renderBubbleBackground(ctx, preset.id, x, y, width, height);
13986
+ ctx.save();
13987
+ switch (appearance.shape) {
13988
+ case "chalk":
13989
+ drawChalkSpeechBubble(ctx, x, y, width, height);
13990
+ break;
13991
+ case "cloud":
13992
+ drawThoughtCloud(ctx, x, y, width, height, appearance.fillColor, appearance.strokeColor, appearance.strokeWidth);
13993
+ break;
13994
+ case "comic":
13995
+ drawComicBurst(ctx, x, y, width, height, appearance.fillColor, appearance.strokeColor, appearance.strokeWidth);
13996
+ break;
13997
+ case "paper":
13998
+ drawTornPaper(ctx, x, y, width, height, appearance.fillColor, true);
13999
+ break;
14000
+ case "ribbon":
14001
+ drawRibbonBanner(ctx, x, y, width, height, appearance.fillColor, appearance.strokeColor, appearance.strokeWidth);
14002
+ break;
14003
+ case "speech":
14004
+ drawConfigurableSpeechBubble(ctx, x, y, width, height, appearance);
14005
+ break;
14006
+ case "rounded": {
14007
+ ctx.globalAlpha = Math.max(0, Math.min(1, appearance.fillOpacity));
14008
+ ctx.fillStyle = appearance.fillColor;
14009
+ ctx.strokeStyle = appearance.strokeColor;
14010
+ ctx.lineWidth = Math.max(0, appearance.strokeWidth);
14011
+ ctx.beginPath();
14012
+ if (typeof ctx.roundRect === "function") {
14013
+ ctx.roundRect(x, y, width, height, Math.min(appearance.cornerRadius, height / 2));
14014
+ } else {
14015
+ ctx.rect(x, y, width, height);
14016
+ }
14017
+ ctx.fill();
14018
+ if (appearance.strokeWidth > 0) ctx.stroke();
14019
+ break;
14020
+ }
14021
+ }
14022
+ if (appearance.texture === "chalk") {
14023
+ ctx.globalAlpha = 0.55;
14024
+ ctx.strokeStyle = appearance.strokeColor;
14025
+ ctx.setLineDash([1, 3]);
14026
+ ctx.lineWidth = Math.max(1, appearance.strokeWidth * 0.7);
14027
+ ctx.strokeRect(x + 1, y + 1, width - 2, height - 2);
14028
+ } else if (appearance.texture === "hatch") {
14029
+ ctx.globalAlpha = 0.22;
14030
+ ctx.strokeStyle = appearance.strokeColor;
14031
+ ctx.lineWidth = 1;
14032
+ for (let offset = -height; offset < width; offset += 9) {
14033
+ ctx.beginPath();
14034
+ ctx.moveTo(x + offset, y + height);
14035
+ ctx.lineTo(x + offset + height, y);
14036
+ ctx.stroke();
14037
+ }
14038
+ }
14039
+ ctx.restore();
14040
+ return true;
14041
+ }
14042
+ function drawChalkSpeechBubble(ctx, x, y, width, height) {
14043
+ ctx.save();
14044
+ const tailHeight = Math.min(46, height * 0.3);
14045
+ const bodyHeight = height - tailHeight;
14046
+ const radius = Math.min(36, bodyHeight * 0.25);
14047
+ const tailLeft = x + width * 0.1;
14048
+ const tailRight = x + width * 0.19;
14049
+ const tailTipX = x + width * 0.14;
14050
+ const tailTipY = y + height;
14051
+ const balloonPath = () => {
14052
+ ctx.beginPath();
14053
+ ctx.moveTo(x + radius, y);
14054
+ ctx.lineTo(x + width - radius, y);
14055
+ ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
14056
+ ctx.lineTo(x + width, y + bodyHeight - radius);
14057
+ ctx.quadraticCurveTo(x + width, y + bodyHeight, x + width - radius, y + bodyHeight);
14058
+ ctx.lineTo(tailRight, y + bodyHeight);
14059
+ ctx.quadraticCurveTo(tailRight - width * 0.02, y + bodyHeight + tailHeight * 0.35, tailTipX, tailTipY);
14060
+ ctx.quadraticCurveTo(tailLeft + width * 5e-3, y + bodyHeight + tailHeight * 0.32, tailLeft, y + bodyHeight);
14061
+ ctx.lineTo(x + radius, y + bodyHeight);
14062
+ ctx.quadraticCurveTo(x, y + bodyHeight, x, y + bodyHeight - radius);
14063
+ ctx.lineTo(x, y + radius);
14064
+ ctx.quadraticCurveTo(x, y, x + radius, y);
14065
+ ctx.closePath();
14066
+ };
14067
+ ctx.fillStyle = "rgba(184, 122, 132, 0.58)";
14068
+ balloonPath();
14069
+ ctx.fill();
14070
+ ctx.save();
14071
+ ctx.beginPath();
14072
+ ctx.moveTo(x + width * 0.2, y + bodyHeight - tailHeight * 0.13);
14073
+ ctx.quadraticCurveTo(x + width * 0.48, y + bodyHeight + tailHeight * 0.37, x + width * 0.91, y + bodyHeight - tailHeight * 0.02);
14074
+ ctx.quadraticCurveTo(x + width * 0.66, y + bodyHeight + tailHeight * 0.72, x + width * 0.24, y + bodyHeight + tailHeight * 0.44);
14075
+ ctx.closePath();
14076
+ ctx.clip();
14077
+ ctx.shadowColor = "rgba(222, 228, 105, 0.32)";
14078
+ ctx.shadowBlur = 3;
14079
+ ctx.strokeStyle = "rgba(203, 211, 76, 0.52)";
14080
+ ctx.lineWidth = 1.25;
14081
+ for (let offset = -height; offset < width; offset += 9) {
14082
+ ctx.beginPath();
14083
+ ctx.moveTo(x + offset, y + height);
14084
+ ctx.lineTo(x + offset + height, y);
14085
+ ctx.stroke();
14086
+ }
14087
+ ctx.shadowBlur = 0;
14088
+ ctx.strokeStyle = "rgba(255, 250, 196, 0.48)";
14089
+ ctx.lineWidth = 0.8;
14090
+ for (let offset = 0; offset < width + height; offset += 12) {
14091
+ ctx.beginPath();
14092
+ ctx.moveTo(x + offset, y);
14093
+ ctx.lineTo(x + offset - height, y + height);
14094
+ ctx.stroke();
14095
+ }
14096
+ ctx.restore();
14097
+ ctx.strokeStyle = "rgba(255, 253, 246, 0.82)";
14098
+ ctx.lineCap = "round";
14099
+ ctx.lineWidth = 1.35;
14100
+ balloonPath();
14101
+ ctx.stroke();
14102
+ const edgeSegments = 66;
14103
+ for (let index = 0; index < edgeSegments; index++) {
14104
+ const pointX = x + width * (index / edgeSegments);
14105
+ const topNoise = (pseudoNoise(index + 91) - 0.5) * 4;
14106
+ const strokeLength = 0.8 + pseudoNoise(index + 143) * 2.1;
14107
+ ctx.lineWidth = 0.6 + pseudoNoise(index + 211) * 1.1;
14108
+ ctx.beginPath();
14109
+ ctx.moveTo(pointX, y + topNoise);
14110
+ ctx.lineTo(pointX + strokeLength, y + topNoise + (pseudoNoise(index + 19) - 0.5) * 2);
14111
+ ctx.stroke();
14112
+ ctx.beginPath();
14113
+ ctx.moveTo(pointX, y + bodyHeight + topNoise);
14114
+ ctx.lineTo(pointX + strokeLength, y + bodyHeight + topNoise + (pseudoNoise(index + 37) - 0.5) * 2);
14115
+ ctx.stroke();
14116
+ }
14117
+ ctx.restore();
14118
+ }
13932
14119
  function drawThoughtCloud(ctx, x, y, width, height, fillColor = "#ffffff", strokeColor = "#000000", strokeWidth = 2) {
13933
14120
  ctx.save();
13934
14121
  ctx.fillStyle = fillColor;
@@ -14017,6 +14204,11 @@ function renderBubbleBackground(ctx, presetId, x, y, width, height, options) {
14017
14204
  ctx.restore();
14018
14205
  return true;
14019
14206
  }
14207
+ if (presetId === "bubble-speech-chalk-rose") {
14208
+ drawChalkSpeechBubble(ctx, x, y, width, height);
14209
+ ctx.restore();
14210
+ return true;
14211
+ }
14020
14212
  if (presetId === "bubble-cloud-dotted" || presetId === "bubble-cloud-morning" || presetId === "bubble-cloud-yellow") {
14021
14213
  drawThoughtCloud(ctx, x, y, width, height, color, "#000000", 2);
14022
14214
  ctx.restore();
@@ -14050,6 +14242,213 @@ function renderBubbleBackground(ctx, presetId, x, y, width, height, options) {
14050
14242
  return true;
14051
14243
  }
14052
14244
 
14245
+ // src/rendering/bubbleAssets.ts
14246
+ var createBubbleAssetDefinition = () => ({
14247
+ schemaVersion: 1,
14248
+ type: "clypra.textBubble",
14249
+ coordinateSpace: "normalized",
14250
+ geometry: { kind: "parametric-path", body: { cornerRadius: 0.12 }, tail: { enabled: true, side: "bottom", anchor: 0.22, width: 0.18, depth: 0.2, curvature: 0.55 } },
14251
+ layers: { fill: { color: "#7c3aed", opacity: 0.9 }, outline: { color: "#ffffff", width: 0.018, roughness: 0, seed: 1 } },
14252
+ textLayout: { padding: [0.1, 0.1, 0.18, 0.1], alignment: "center", overflow: "grow" }
14253
+ });
14254
+ var createFreeformBubbleAssetDefinition = () => ({
14255
+ ...createBubbleAssetDefinition(),
14256
+ geometry: {
14257
+ kind: "freeform-path",
14258
+ closed: true,
14259
+ commands: [
14260
+ { kind: "move", to: [0.12, 0.04] },
14261
+ { kind: "line", to: [0.88, 0.04] },
14262
+ { kind: "quadratic", control: [0.98, 0.04], to: [0.98, 0.16] },
14263
+ { kind: "line", to: [0.98, 0.7] },
14264
+ { kind: "quadratic", control: [0.98, 0.82], to: [0.86, 0.82] },
14265
+ { kind: "line", to: [0.38, 0.82] },
14266
+ { kind: "line", to: [0.22, 1] },
14267
+ { kind: "line", to: [0.27, 0.82] },
14268
+ { kind: "line", to: [0.12, 0.82] },
14269
+ { kind: "quadratic", control: [0.02, 0.82], to: [0.02, 0.7] },
14270
+ { kind: "line", to: [0.02, 0.16] },
14271
+ { kind: "quadratic", control: [0.02, 0.04], to: [0.12, 0.04] },
14272
+ { kind: "close" }
14273
+ ]
14274
+ }
14275
+ });
14276
+ var clamp = (value) => Math.max(0, Math.min(1, value));
14277
+ function compileBubblePath(definition) {
14278
+ if (definition.geometry.kind === "freeform-path") {
14279
+ return definition.geometry.commands.map((command) => {
14280
+ if (command.kind === "close") return "Z";
14281
+ if (command.kind === "move") return `M ${command.to[0]} ${command.to[1]}`;
14282
+ if (command.kind === "line") return `L ${command.to[0]} ${command.to[1]}`;
14283
+ if (command.kind === "quadratic") return `Q ${command.control[0]} ${command.control[1]} ${command.to[0]} ${command.to[1]}`;
14284
+ if (command.kind === "cubic") return `C ${command.control1[0]} ${command.control1[1]} ${command.control2[0]} ${command.control2[1]} ${command.to[0]} ${command.to[1]}`;
14285
+ return "";
14286
+ }).join(" ");
14287
+ }
14288
+ const { body, tail } = definition.geometry;
14289
+ const r = clamp(body.cornerRadius) * 0.5;
14290
+ const anchor = clamp(tail.anchor);
14291
+ const tailWidth = clamp(tail.width) * 0.5;
14292
+ const depth = clamp(tail.depth);
14293
+ const left = clamp(anchor - tailWidth);
14294
+ const right = clamp(anchor + tailWidth);
14295
+ const bodyPath = `M ${r} 0 H ${1 - r} Q 1 0 1 ${r} V ${1 - r} Q 1 1 ${1 - r} 1 H ${r} Q 0 1 0 ${1 - r} V ${r} Q 0 0 ${r} 0 Z`;
14296
+ if (!tail.enabled) return bodyPath;
14297
+ if (tail.side === "bottom") {
14298
+ return `M ${r} 0 H ${1 - r} Q 1 0 1 ${r} V ${1 - r} Q 1 1 ${1 - r} 1 H ${right} Q ${anchor + tailWidth * 0.35} ${1 + depth * tail.curvature} ${anchor} ${1 + depth} Q ${anchor - tailWidth * 0.35} ${1 + depth * tail.curvature} ${left} 1 H ${r} Q 0 1 0 ${1 - r} V ${r} Q 0 0 ${r} 0 Z`;
14299
+ }
14300
+ if (tail.side === "top") {
14301
+ return `M ${r} 0 H ${left} Q ${anchor - tailWidth * 0.35} ${-depth * tail.curvature} ${anchor} ${-depth} Q ${anchor + tailWidth * 0.35} ${-depth * tail.curvature} ${right} 0 H ${1 - r} Q 1 0 1 ${r} V ${1 - r} Q 1 1 ${1 - r} 1 H ${r} Q 0 1 0 ${1 - r} V ${r} Q 0 0 ${r} 0 Z`;
14302
+ }
14303
+ if (tail.side === "left") {
14304
+ return `M ${r} 0 H ${1 - r} Q 1 0 1 ${r} V ${1 - r} Q 1 1 ${1 - r} 1 H ${r} Q 0 1 0 ${1 - r} V ${right} Q ${-depth * tail.curvature} ${anchor + tailWidth * 0.35} ${-depth} ${anchor} Q ${-depth * tail.curvature} ${anchor - tailWidth * 0.35} 0 ${left} V ${r} Q 0 0 ${r} 0 Z`;
14305
+ }
14306
+ return `M ${r} 0 H ${1 - r} Q 1 0 1 ${r} V ${left} Q ${1 + depth * tail.curvature} ${anchor - tailWidth * 0.35} ${1 + depth} ${anchor} Q ${1 + depth * tail.curvature} ${anchor + tailWidth * 0.35} 1 ${right} V ${1 - r} Q 1 1 ${1 - r} 1 H ${r} Q 0 1 0 ${1 - r} V ${r} Q 0 0 ${r} 0 Z`;
14307
+ }
14308
+ function compileBubbleSvg(definition, width = 1e3, height = 700) {
14309
+ const { fill, outline, accent } = definition.layers;
14310
+ const texture = accent?.texture === "hatch" ? `<pattern id="hatch" width="24" height="24" patternUnits="userSpaceOnUse" patternTransform="rotate(45)"><line x1="0" y1="0" x2="0" y2="24" stroke="${accent.color}" stroke-opacity="${accent.opacity}" stroke-width="3" /></pattern>` : "";
14311
+ const textureOverlay = accent?.texture === "hatch" ? `<path d="${compileBubblePath(definition)}" fill="url(#hatch)" />` : "";
14312
+ return `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="-0.25 -0.25 1.5 1.5"><defs>${texture}</defs><path d="${compileBubblePath(definition)}" fill="${fill.color}" fill-opacity="${fill.opacity}" stroke="${outline.color}" stroke-width="${outline.width}" vector-effect="non-scaling-stroke" />${textureOverlay}</svg>`;
14313
+ }
14314
+ function traceBubbleSilhouette(ctx, x, y, width, height, radius, tail, tailDepth) {
14315
+ const r = Math.min(radius, width / 2, height / 2);
14316
+ const anchor = clamp(tail.anchor);
14317
+ const halfTail = clamp(tail.width) * (tail.side === "left" || tail.side === "right" ? height : width) / 2;
14318
+ const along = tail.side === "left" || tail.side === "right" ? y + height * anchor : x + width * anchor;
14319
+ ctx.beginPath();
14320
+ ctx.moveTo(x + r, y);
14321
+ if (tail.enabled && tail.side === "top") {
14322
+ ctx.lineTo(along - halfTail, y);
14323
+ ctx.quadraticCurveTo(along - halfTail * 0.35, y - tailDepth * tail.curvature, along, y - tailDepth);
14324
+ ctx.quadraticCurveTo(along + halfTail * 0.35, y - tailDepth * tail.curvature, along + halfTail, y);
14325
+ }
14326
+ ctx.lineTo(x + width - r, y);
14327
+ ctx.quadraticCurveTo(x + width, y, x + width, y + r);
14328
+ if (tail.enabled && tail.side === "right") {
14329
+ ctx.lineTo(x + width, along - halfTail);
14330
+ ctx.quadraticCurveTo(x + width + tailDepth * tail.curvature, along - halfTail * 0.35, x + width + tailDepth, along);
14331
+ ctx.quadraticCurveTo(x + width + tailDepth * tail.curvature, along + halfTail * 0.35, x + width, along + halfTail);
14332
+ }
14333
+ ctx.lineTo(x + width, y + height - r);
14334
+ ctx.quadraticCurveTo(x + width, y + height, x + width - r, y + height);
14335
+ if (tail.enabled && tail.side === "bottom") {
14336
+ ctx.lineTo(along + halfTail, y + height);
14337
+ ctx.quadraticCurveTo(along + halfTail * 0.35, y + height + tailDepth * tail.curvature, along, y + height + tailDepth);
14338
+ ctx.quadraticCurveTo(along - halfTail * 0.35, y + height + tailDepth * tail.curvature, along - halfTail, y + height);
14339
+ }
14340
+ ctx.lineTo(x + r, y + height);
14341
+ ctx.quadraticCurveTo(x, y + height, x, y + height - r);
14342
+ if (tail.enabled && tail.side === "left") {
14343
+ ctx.lineTo(x, along + halfTail);
14344
+ ctx.quadraticCurveTo(x - tailDepth * tail.curvature, along + halfTail * 0.35, x - tailDepth, along);
14345
+ ctx.quadraticCurveTo(x - tailDepth * tail.curvature, along - halfTail * 0.35, x, along - halfTail);
14346
+ }
14347
+ ctx.lineTo(x, y + r);
14348
+ ctx.quadraticCurveTo(x, y, x + r, y);
14349
+ ctx.closePath();
14350
+ }
14351
+ function renderBubbleAssetDefinition(ctx, definition, x, y, width, height) {
14352
+ if (definition.geometry.kind === "freeform-path") {
14353
+ renderFreeformBubbleAssetDefinition(ctx, definition, x, y, width, height);
14354
+ return;
14355
+ }
14356
+ const { tail, body } = definition.geometry;
14357
+ const tailDepth = tail.enabled ? clamp(tail.depth) * (tail.side === "left" || tail.side === "right" ? width : height) : 0;
14358
+ const bodyX = x + (tail.side === "left" ? tailDepth : 0);
14359
+ const bodyY = y + (tail.side === "top" ? tailDepth : 0);
14360
+ const bodyWidth = width - (tail.side === "left" || tail.side === "right" ? tailDepth : 0);
14361
+ const bodyHeight = height - (tail.side === "top" || tail.side === "bottom" ? tailDepth : 0);
14362
+ const radius = clamp(body.cornerRadius) * Math.min(bodyWidth, bodyHeight) / 2;
14363
+ ctx.save();
14364
+ traceBubbleSilhouette(ctx, bodyX, bodyY, bodyWidth, bodyHeight, radius, tail, tailDepth);
14365
+ ctx.globalAlpha = definition.layers.fill.opacity;
14366
+ ctx.fillStyle = definition.layers.fill.color;
14367
+ ctx.fill();
14368
+ const accent = definition.layers.accent;
14369
+ if (accent && accent.texture !== "none") {
14370
+ ctx.save();
14371
+ ctx.clip();
14372
+ ctx.globalAlpha = accent.opacity;
14373
+ ctx.strokeStyle = accent.color;
14374
+ ctx.lineWidth = Math.max(1, Math.min(width, height) * 6e-3);
14375
+ if (accent.texture === "hatch") {
14376
+ const spacing = Math.max(5, Math.min(width, height) * 0.09);
14377
+ for (let offset = -height; offset < width + height; offset += spacing) {
14378
+ ctx.beginPath();
14379
+ ctx.moveTo(x + offset, y + height);
14380
+ ctx.lineTo(x + offset + height, y);
14381
+ ctx.stroke();
14382
+ }
14383
+ } else {
14384
+ let state = definition.layers.outline.seed >>> 0;
14385
+ const nextRandom = () => {
14386
+ state = state * 1664525 + 1013904223 >>> 0;
14387
+ return state / 4294967296;
14388
+ };
14389
+ const count = Math.max(18, Math.round(width * height / 700));
14390
+ for (let index = 0; index < count; index += 1) {
14391
+ const px = x + nextRandom() * width;
14392
+ const py = y + nextRandom() * height;
14393
+ const length = 1 + nextRandom() * Math.min(width, height) * 0.025;
14394
+ ctx.beginPath();
14395
+ ctx.moveTo(px, py);
14396
+ ctx.lineTo(px + length, py);
14397
+ ctx.stroke();
14398
+ }
14399
+ }
14400
+ ctx.restore();
14401
+ }
14402
+ if (definition.layers.outline.width > 0) {
14403
+ ctx.globalAlpha = 1;
14404
+ ctx.strokeStyle = definition.layers.outline.color;
14405
+ ctx.lineWidth = definition.layers.outline.width * Math.min(width, height);
14406
+ ctx.stroke();
14407
+ }
14408
+ ctx.restore();
14409
+ }
14410
+ function traceFreeformPath(ctx, definition, x, y, width, height) {
14411
+ ctx.beginPath();
14412
+ for (const command of definition.geometry.commands) {
14413
+ if (command.kind === "close") ctx.closePath();
14414
+ else if (command.kind === "move") ctx.moveTo(x + command.to[0] * width, y + command.to[1] * height);
14415
+ else if (command.kind === "line") ctx.lineTo(x + command.to[0] * width, y + command.to[1] * height);
14416
+ else if (command.kind === "quadratic") ctx.quadraticCurveTo(x + command.control[0] * width, y + command.control[1] * height, x + command.to[0] * width, y + command.to[1] * height);
14417
+ else if (command.kind === "cubic") ctx.bezierCurveTo(x + command.control1[0] * width, y + command.control1[1] * height, x + command.control2[0] * width, y + command.control2[1] * height, x + command.to[0] * width, y + command.to[1] * height);
14418
+ }
14419
+ }
14420
+ function renderFreeformBubbleAssetDefinition(ctx, definition, x, y, width, height) {
14421
+ ctx.save();
14422
+ traceFreeformPath(ctx, definition, x, y, width, height);
14423
+ ctx.globalAlpha = definition.layers.fill.opacity;
14424
+ ctx.fillStyle = definition.layers.fill.color;
14425
+ ctx.fill();
14426
+ if (definition.layers.outline.width > 0) {
14427
+ ctx.globalAlpha = 1;
14428
+ ctx.strokeStyle = definition.layers.outline.color;
14429
+ ctx.lineWidth = definition.layers.outline.width * Math.min(width, height);
14430
+ ctx.stroke();
14431
+ }
14432
+ ctx.restore();
14433
+ }
14434
+ function validateBubbleAssetDefinition(definition) {
14435
+ const errors = [];
14436
+ if (definition.schemaVersion !== 1 || definition.type !== "clypra.textBubble") errors.push("Unsupported bubble asset schema.");
14437
+ if (!/^#[0-9a-f]{6}$/i.test(definition.layers.fill.color)) errors.push("Fill color must be a six-digit hex value.");
14438
+ if (definition.layers.fill.opacity < 0 || definition.layers.fill.opacity > 1) errors.push("Fill opacity must be between 0 and 1.");
14439
+ if (!/^#[0-9a-f]{6}$/i.test(definition.layers.outline.color)) errors.push("Outline color must be a six-digit hex value.");
14440
+ if (definition.layers.outline.width < 0 || definition.layers.outline.width > 0.1) errors.push("Outline width must be normalized between 0 and 0.1.");
14441
+ if (definition.geometry.kind === "parametric-path") {
14442
+ if (definition.geometry.body.cornerRadius < 0 || definition.geometry.body.cornerRadius > 1) errors.push("Corner radius must be normalized.");
14443
+ if (definition.geometry.tail.anchor < 0 || definition.geometry.tail.anchor > 1) errors.push("Tail anchor must be normalized.");
14444
+ if (definition.geometry.tail.width < 0 || definition.geometry.tail.width > 1 || definition.geometry.tail.depth < 0 || definition.geometry.tail.depth > 1) errors.push("Tail width and depth must be normalized.");
14445
+ } else if (definition.geometry.commands.length < 4 || !definition.geometry.commands.some((command) => command.kind === "close")) {
14446
+ errors.push("Freeform bubble paths must contain a closed silhouette.");
14447
+ }
14448
+ if (definition.textLayout.padding.some((value) => value < 0 || value > 0.5)) errors.push("Text padding must be normalized between 0 and 0.5.");
14449
+ return errors;
14450
+ }
14451
+
14053
14452
  // src/rendering/index.ts
14054
14453
  function renderCanonicalToCanvas(ctx, input) {
14055
14454
  if (input.capability === "text-template") {
@@ -24899,6 +25298,492 @@ async function calculateSha256(data) {
24899
25298
  }
24900
25299
  }
24901
25300
 
25301
+ // src/captions/presets.ts
25302
+ var CAPTION_PRESET_CLASSIC_SUBTITLE = {
25303
+ id: "classic-subtitle",
25304
+ name: "Classic Subtitle",
25305
+ fontFamily: "Inter Variable",
25306
+ fontWeight: 600,
25307
+ fontStyle: "normal",
25308
+ fontSize: 48,
25309
+ letterSpacing: 0,
25310
+ lineHeight: 1.25,
25311
+ textTransform: "none",
25312
+ textAlign: "center",
25313
+ verticalAlign: "bottom",
25314
+ inactiveColor: "#FFFFFF",
25315
+ inactiveOpacity: 0.95,
25316
+ activeColor: "#FFFFFF",
25317
+ activeScale: 1,
25318
+ strokeEnabled: true,
25319
+ strokeColor: "#000000",
25320
+ strokeWidth: 4,
25321
+ shadowEnabled: true,
25322
+ shadowColor: "rgba(0, 0, 0, 0.75)",
25323
+ shadowBlur: 6,
25324
+ shadowOffsetX: 0,
25325
+ shadowOffsetY: 2,
25326
+ backgroundEnabled: false
25327
+ };
25328
+ var CAPTION_PRESET_KARAOKE_POP = {
25329
+ id: "karaoke-pop",
25330
+ name: "Karaoke Pop",
25331
+ fontFamily: "Outfit Variable",
25332
+ fontWeight: 700,
25333
+ fontStyle: "normal",
25334
+ fontSize: 54,
25335
+ letterSpacing: 0.5,
25336
+ lineHeight: 1.3,
25337
+ textTransform: "none",
25338
+ textAlign: "center",
25339
+ verticalAlign: "bottom",
25340
+ inactiveColor: "#FFFFFF",
25341
+ inactiveOpacity: 0.55,
25342
+ activeColor: "#38BDF8",
25343
+ activeScale: 1.12,
25344
+ activeWordAnim: "pop",
25345
+ strokeEnabled: true,
25346
+ strokeColor: "#000000",
25347
+ strokeWidth: 5,
25348
+ shadowEnabled: true,
25349
+ shadowColor: "rgba(0, 0, 0, 0.8)",
25350
+ shadowBlur: 8,
25351
+ shadowOffsetX: 0,
25352
+ shadowOffsetY: 3,
25353
+ backgroundEnabled: false
25354
+ };
25355
+ var CAPTION_PRESET_BOLD_PUNCH = {
25356
+ id: "bold-punch",
25357
+ name: "Bold Punch",
25358
+ fontFamily: "Montserrat Variable",
25359
+ fontWeight: 900,
25360
+ fontStyle: "normal",
25361
+ fontSize: 56,
25362
+ letterSpacing: 1,
25363
+ lineHeight: 1.2,
25364
+ textTransform: "uppercase",
25365
+ textAlign: "center",
25366
+ verticalAlign: "bottom",
25367
+ inactiveColor: "#FFFFFF",
25368
+ inactiveOpacity: 0.6,
25369
+ activeColor: "#E11D48",
25370
+ activeScale: 1.08,
25371
+ activeWordAnim: "pop",
25372
+ strokeEnabled: true,
25373
+ strokeColor: "#000000",
25374
+ strokeWidth: 6,
25375
+ shadowEnabled: true,
25376
+ shadowColor: "rgba(0, 0, 0, 0.9)",
25377
+ shadowBlur: 10,
25378
+ shadowOffsetX: 3,
25379
+ shadowOffsetY: 4,
25380
+ backgroundEnabled: false
25381
+ };
25382
+ var CAPTION_PRESET_CLEAN_MINIMAL = {
25383
+ id: "clean-minimal",
25384
+ name: "Clean Minimal",
25385
+ fontFamily: "Inter Variable",
25386
+ fontWeight: 500,
25387
+ fontStyle: "normal",
25388
+ fontSize: 42,
25389
+ letterSpacing: 0,
25390
+ lineHeight: 1.3,
25391
+ textTransform: "none",
25392
+ textAlign: "center",
25393
+ verticalAlign: "bottom",
25394
+ inactiveColor: "#E2E8F0",
25395
+ inactiveOpacity: 0.7,
25396
+ activeColor: "#FFFFFF",
25397
+ activeScale: 1,
25398
+ strokeEnabled: false,
25399
+ shadowEnabled: false,
25400
+ backgroundEnabled: true,
25401
+ backgroundColor: "rgba(15, 23, 42, 0.75)",
25402
+ backgroundPaddingX: 20,
25403
+ backgroundPaddingY: 10,
25404
+ backgroundBorderRadius: 14
25405
+ };
25406
+ var CAPTION_PRESET_CINEMATIC_GOLD = {
25407
+ id: "cinematic-gold",
25408
+ name: "Cinematic Gold",
25409
+ fontFamily: "Outfit Variable",
25410
+ fontWeight: 600,
25411
+ fontStyle: "normal",
25412
+ fontSize: 48,
25413
+ letterSpacing: 2,
25414
+ lineHeight: 1.35,
25415
+ textTransform: "none",
25416
+ textAlign: "center",
25417
+ verticalAlign: "bottom",
25418
+ inactiveColor: "#E2E8F0",
25419
+ inactiveOpacity: 0.55,
25420
+ activeColor: "#FBBF24",
25421
+ activeScale: 1.05,
25422
+ strokeEnabled: true,
25423
+ strokeColor: "#000000",
25424
+ strokeWidth: 3.5,
25425
+ shadowEnabled: true,
25426
+ shadowColor: "rgba(251, 191, 36, 0.35)",
25427
+ shadowBlur: 14,
25428
+ shadowOffsetX: 0,
25429
+ shadowOffsetY: 2,
25430
+ backgroundEnabled: false
25431
+ };
25432
+ var CAPTION_PRESET_NEON_GLOW = {
25433
+ id: "neon-glow",
25434
+ name: "Neon Glow",
25435
+ fontFamily: "Montserrat Variable",
25436
+ fontWeight: 800,
25437
+ fontStyle: "normal",
25438
+ fontSize: 52,
25439
+ letterSpacing: 1,
25440
+ lineHeight: 1.25,
25441
+ textTransform: "none",
25442
+ textAlign: "center",
25443
+ verticalAlign: "bottom",
25444
+ inactiveColor: "#94A3B8",
25445
+ inactiveOpacity: 0.5,
25446
+ activeColor: "#22D3EE",
25447
+ activeScale: 1.06,
25448
+ activeWordAnim: "glow",
25449
+ strokeEnabled: true,
25450
+ strokeColor: "#0891B2",
25451
+ strokeWidth: 4,
25452
+ glowEnabled: true,
25453
+ glowColor: "#06B6D4",
25454
+ glowBlur: 16,
25455
+ shadowEnabled: true,
25456
+ shadowColor: "rgba(0, 0, 0, 0.8)",
25457
+ shadowBlur: 8,
25458
+ shadowOffsetX: 0,
25459
+ shadowOffsetY: 2,
25460
+ backgroundEnabled: false
25461
+ };
25462
+ var BUILT_IN_CAPTION_PRESETS = {
25463
+ "classic-subtitle": CAPTION_PRESET_CLASSIC_SUBTITLE,
25464
+ "karaoke-pop": CAPTION_PRESET_KARAOKE_POP,
25465
+ "bold-punch": CAPTION_PRESET_BOLD_PUNCH,
25466
+ "clean-minimal": CAPTION_PRESET_CLEAN_MINIMAL,
25467
+ "cinematic-gold": CAPTION_PRESET_CINEMATIC_GOLD,
25468
+ "neon-glow": CAPTION_PRESET_NEON_GLOW
25469
+ };
25470
+ function getCaptionPreset(idOrPreset) {
25471
+ if (!idOrPreset) return CAPTION_PRESET_CLASSIC_SUBTITLE;
25472
+ if (typeof idOrPreset === "object") return idOrPreset;
25473
+ const match = BUILT_IN_CAPTION_PRESETS[idOrPreset];
25474
+ return match ?? CAPTION_PRESET_CLASSIC_SUBTITLE;
25475
+ }
25476
+ function hashCaptionPreset(preset) {
25477
+ const str = `${preset.id}:${preset.fontFamily}:${preset.fontWeight}:${preset.fontSize}:${preset.activeColor}:${preset.inactiveColor}:${preset.activeScale ?? 1}:${preset.strokeEnabled ? preset.strokeWidth : 0}:${preset.shadowEnabled ? preset.shadowBlur : 0}:${preset.backgroundEnabled ? preset.backgroundColor : ""}:${preset.glowEnabled ? preset.glowBlur : 0}`;
25478
+ let hash = 2166136261;
25479
+ for (let i = 0; i < str.length; i++) {
25480
+ hash ^= str.charCodeAt(i);
25481
+ hash = Math.imul(hash, 16777619);
25482
+ }
25483
+ return (hash >>> 0).toString(16);
25484
+ }
25485
+
25486
+ // src/captions/engine.ts
25487
+ var PAUSE_RELEASE_THRESHOLD_MS = 250;
25488
+ var DEFAULT_MAX_WORDS_PER_LINE = 5;
25489
+ var DEFAULT_MAX_LINES = 2;
25490
+ var DEFAULT_PAUSE_BREAK_MS = 350;
25491
+ var CaptionEngine = class {
25492
+ /**
25493
+ * Paginates a flat stream of words into structured caption cues.
25494
+ * Enforces max words per line, max lines, pause thresholds, and punctuation breaks.
25495
+ */
25496
+ static paginateWords(words, config = {}) {
25497
+ if (!words.length) return [];
25498
+ const maxWordsPerLine = config.maxWordsPerLine ?? DEFAULT_MAX_WORDS_PER_LINE;
25499
+ const maxLines = config.maxLines ?? DEFAULT_MAX_LINES;
25500
+ const maxWordsPerCue = maxWordsPerLine * maxLines;
25501
+ const pauseBreakMs = config.pauseBreakMs ?? DEFAULT_PAUSE_BREAK_MS;
25502
+ const cues = [];
25503
+ let currentCueWords = [];
25504
+ for (let i = 0; i < words.length; i++) {
25505
+ const currentWord = words[i];
25506
+ const nextWord = words[i + 1];
25507
+ currentCueWords.push(currentWord);
25508
+ const hasReachedMaxWords = currentCueWords.length >= maxWordsPerCue;
25509
+ const isTerminalPunctuation = /[.!?]$/.test(currentWord.text.trim());
25510
+ const hasPauseBreak = nextWord && nextWord.startMs - currentWord.endMs >= pauseBreakMs;
25511
+ const isLastWord = i === words.length - 1;
25512
+ if (hasReachedMaxWords || isTerminalPunctuation || hasPauseBreak || isLastWord) {
25513
+ const text = currentCueWords.map((w) => w.text).join(" ");
25514
+ const startMs = currentCueWords[0].startMs;
25515
+ const endMs = currentCueWords[currentCueWords.length - 1].endMs;
25516
+ const id = `cue_${startMs}_${endMs}`;
25517
+ cues.push({
25518
+ id,
25519
+ words: [...currentCueWords],
25520
+ text,
25521
+ startMs,
25522
+ endMs
25523
+ });
25524
+ currentCueWords = [];
25525
+ }
25526
+ }
25527
+ return cues;
25528
+ }
25529
+ /**
25530
+ * Resolves the active word index within a cue for a given playback time in milliseconds.
25531
+ * Returns -1 if no word is active or during the pause release window (> 250ms after speech ends).
25532
+ */
25533
+ static resolveActiveWordIndex(cue, currentTimeMs) {
25534
+ if (!cue.words.length) return -1;
25535
+ if (currentTimeMs < cue.words[0].startMs) return -1;
25536
+ for (let i = 0; i < cue.words.length; i++) {
25537
+ const w = cue.words[i];
25538
+ const nextWord = cue.words[i + 1];
25539
+ if (currentTimeMs >= w.startMs && currentTimeMs < w.endMs) {
25540
+ return i;
25541
+ }
25542
+ if (currentTimeMs >= w.endMs && nextWord && currentTimeMs < nextWord.startMs) {
25543
+ const gap = nextWord.startMs - w.endMs;
25544
+ if (gap <= PAUSE_RELEASE_THRESHOLD_MS) {
25545
+ return i;
25546
+ }
25547
+ if (currentTimeMs < w.endMs + PAUSE_RELEASE_THRESHOLD_MS) {
25548
+ return i;
25549
+ }
25550
+ return -1;
25551
+ }
25552
+ }
25553
+ const lastWord = cue.words[cue.words.length - 1];
25554
+ if (currentTimeMs >= lastWord.endMs) {
25555
+ if (currentTimeMs < lastWord.endMs + PAUSE_RELEASE_THRESHOLD_MS) {
25556
+ return cue.words.length - 1;
25557
+ }
25558
+ return -1;
25559
+ }
25560
+ return -1;
25561
+ }
25562
+ /**
25563
+ * Builds the canonical raster cache key for a caption frame.
25564
+ * Format: `caption:${clipId}:${segmentId}:${activeWordIndex}:${presetHash}:${fontLoaded}`
25565
+ */
25566
+ static buildCacheKey(clipId, segmentId, activeWordIndex, preset, fontLoaded) {
25567
+ const presetHash = hashCaptionPreset(preset);
25568
+ return `caption:${clipId}:${segmentId}:${activeWordIndex}:${presetHash}:${fontLoaded ? 1 : 0}`;
25569
+ }
25570
+ /**
25571
+ * Computes multi-line word layout and bounding boxes for a cue.
25572
+ */
25573
+ static computeLayout(ctx, cue, activeWordIndex, preset, canvasWidth, canvasHeight, scale = 1, positionYRatio = 0.85) {
25574
+ const fontSize = Math.round(preset.fontSize * scale);
25575
+ const lineHeight = fontSize * preset.lineHeight;
25576
+ const fontStr = `${preset.fontStyle} ${preset.fontWeight} ${fontSize}px "${preset.fontFamily}", sans-serif`;
25577
+ ctx.font = fontStr;
25578
+ const maxLineWidth = Math.max(200, canvasWidth * 0.85);
25579
+ const spaceWidth = ctx.measureText(" ").width;
25580
+ const lines = [];
25581
+ let curLineWords = [];
25582
+ let curLineIndices = [];
25583
+ let curLineWidths = [];
25584
+ let curLineWidth = 0;
25585
+ for (let i = 0; i < cue.words.length; i++) {
25586
+ const word = cue.words[i];
25587
+ const displayText = preset.textTransform === "uppercase" ? word.text.toUpperCase() : preset.textTransform === "lowercase" ? word.text.toLowerCase() : word.text;
25588
+ const wWidth = ctx.measureText(displayText).width;
25589
+ const neededWidth = curLineWidth === 0 ? wWidth : curLineWidth + spaceWidth + wWidth;
25590
+ if (curLineWords.length > 0 && (neededWidth > maxLineWidth || curLineWords.length >= DEFAULT_MAX_WORDS_PER_LINE)) {
25591
+ lines.push({
25592
+ words: curLineWords,
25593
+ indices: curLineIndices,
25594
+ widths: curLineWidths
25595
+ });
25596
+ curLineWords = [word];
25597
+ curLineIndices = [i];
25598
+ curLineWidths = [wWidth];
25599
+ curLineWidth = wWidth;
25600
+ } else {
25601
+ curLineWords.push(word);
25602
+ curLineIndices.push(i);
25603
+ curLineWidths.push(wWidth);
25604
+ curLineWidth = neededWidth;
25605
+ }
25606
+ }
25607
+ if (curLineWords.length > 0) {
25608
+ lines.push({
25609
+ words: curLineWords,
25610
+ indices: curLineIndices,
25611
+ widths: curLineWidths
25612
+ });
25613
+ }
25614
+ const totalHeight = lines.length * lineHeight;
25615
+ const anchorCenterY = canvasHeight * positionYRatio;
25616
+ const startY = anchorCenterY - totalHeight / 2;
25617
+ const allWordBoxes = [];
25618
+ const lineLayouts = [];
25619
+ let overallMinX = Infinity;
25620
+ let overallMinY = Infinity;
25621
+ let overallMaxX = -Infinity;
25622
+ let overallMaxY = -Infinity;
25623
+ for (let l = 0; l < lines.length; l++) {
25624
+ const line = lines[l];
25625
+ const lineWidth = line.widths.reduce((a, b) => a + b, 0) + (line.words.length - 1) * spaceWidth;
25626
+ const lineY = startY + l * lineHeight;
25627
+ const baselineY = lineY + fontSize * 0.82;
25628
+ let startX = (canvasWidth - lineWidth) / 2;
25629
+ if (preset.textAlign === "left") {
25630
+ startX = canvasWidth * 0.08;
25631
+ } else if (preset.textAlign === "right") {
25632
+ startX = canvasWidth * 0.92 - lineWidth;
25633
+ }
25634
+ let curX = startX;
25635
+ const lineBoxes = [];
25636
+ for (let w = 0; w < line.words.length; w++) {
25637
+ const word = line.words[w];
25638
+ const wordIdx = line.indices[w];
25639
+ const wWidth = line.widths[w];
25640
+ const isActive = wordIdx === activeWordIndex;
25641
+ const box = {
25642
+ wordIndex: wordIdx,
25643
+ word,
25644
+ x: curX,
25645
+ y: lineY,
25646
+ width: wWidth,
25647
+ height: lineHeight,
25648
+ baselineY,
25649
+ isActive
25650
+ };
25651
+ lineBoxes.push(box);
25652
+ allWordBoxes.push(box);
25653
+ overallMinX = Math.min(overallMinX, curX);
25654
+ overallMinY = Math.min(overallMinY, lineY);
25655
+ overallMaxX = Math.max(overallMaxX, curX + wWidth);
25656
+ overallMaxY = Math.max(overallMaxY, lineY + lineHeight);
25657
+ curX += wWidth + spaceWidth;
25658
+ }
25659
+ lineLayouts.push({
25660
+ lineIndex: l,
25661
+ text: line.words.map((w) => w.text).join(" "),
25662
+ words: lineBoxes,
25663
+ width: lineWidth,
25664
+ height: lineHeight,
25665
+ x: startX,
25666
+ y: lineY
25667
+ });
25668
+ }
25669
+ const strokeBleed = preset.strokeEnabled ? (preset.strokeWidth ?? 4) * scale : 0;
25670
+ const shadowBleed = preset.shadowEnabled ? (preset.shadowBlur ?? 8) * scale : 0;
25671
+ const glowBleed = preset.glowEnabled ? (preset.glowBlur ?? 16) * scale : 0;
25672
+ const bgPadX = preset.backgroundEnabled ? (preset.backgroundPaddingX ?? 20) * scale : 0;
25673
+ const bgPadY = preset.backgroundEnabled ? (preset.backgroundPaddingY ?? 10) * scale : 0;
25674
+ const activeScaleBleed = fontSize * 0.25;
25675
+ const padX = Math.ceil(Math.max(strokeBleed + shadowBleed + glowBleed + activeScaleBleed, bgPadX));
25676
+ const padY = Math.ceil(Math.max(strokeBleed + shadowBleed + glowBleed + activeScaleBleed, bgPadY));
25677
+ const bounds = {
25678
+ x: Math.max(0, Math.floor(overallMinX - padX)),
25679
+ y: Math.max(0, Math.floor(overallMinY - padY)),
25680
+ width: Math.min(canvasWidth, Math.ceil(overallMaxX - overallMinX + padX * 2)),
25681
+ height: Math.min(canvasHeight, Math.ceil(overallMaxY - overallMinY + padY * 2))
25682
+ };
25683
+ return { bounds, wordBoxes: allWordBoxes, lines: lineLayouts };
25684
+ }
25685
+ };
25686
+
25687
+ // src/captions/renderer.ts
25688
+ function renderCaptionToCanvas(ctx, options) {
25689
+ const {
25690
+ cue,
25691
+ activeWordIndex,
25692
+ preset,
25693
+ canvasWidth,
25694
+ canvasHeight,
25695
+ scale = 1,
25696
+ positionYRatio = 0.85
25697
+ } = options;
25698
+ if (!cue.words || cue.words.length === 0) {
25699
+ return {
25700
+ bounds: { x: 0, y: 0, width: 0, height: 0 },
25701
+ wordBoxes: [],
25702
+ lines: []
25703
+ };
25704
+ }
25705
+ const layout = CaptionEngine.computeLayout(
25706
+ ctx,
25707
+ cue,
25708
+ activeWordIndex,
25709
+ preset,
25710
+ canvasWidth,
25711
+ canvasHeight,
25712
+ scale,
25713
+ positionYRatio
25714
+ );
25715
+ const fontSize = Math.round(preset.fontSize * scale);
25716
+ const fontStr = `${preset.fontStyle} ${preset.fontWeight} ${fontSize}px "${preset.fontFamily}", sans-serif`;
25717
+ ctx.font = fontStr;
25718
+ ctx.textBaseline = "alphabetic";
25719
+ if (preset.backgroundEnabled && preset.backgroundColor) {
25720
+ ctx.save();
25721
+ ctx.fillStyle = preset.backgroundColor;
25722
+ const padX = (preset.backgroundPaddingX ?? 18) * scale;
25723
+ const padY = (preset.backgroundPaddingY ?? 8) * scale;
25724
+ const radius = (preset.backgroundBorderRadius ?? 12) * scale;
25725
+ for (const line of layout.lines) {
25726
+ const bgX = line.x - padX;
25727
+ const bgY = line.y - padY / 2;
25728
+ const bgW = line.width + padX * 2;
25729
+ const bgH = line.height + padY;
25730
+ ctx.beginPath();
25731
+ if (typeof ctx.roundRect === "function") {
25732
+ ctx.roundRect(bgX, bgY, bgW, bgH, radius);
25733
+ } else {
25734
+ ctx.rect(bgX, bgY, bgW, bgH);
25735
+ }
25736
+ ctx.fill();
25737
+ }
25738
+ ctx.restore();
25739
+ }
25740
+ for (const box of layout.wordBoxes) {
25741
+ ctx.save();
25742
+ const displayText = preset.textTransform === "uppercase" ? box.word.text.toUpperCase() : preset.textTransform === "lowercase" ? box.word.text.toLowerCase() : box.word.text;
25743
+ const isActive = box.isActive;
25744
+ if (isActive && preset.activeScale && preset.activeScale !== 1) {
25745
+ const centerX = box.x + box.width / 2;
25746
+ const centerY = box.y + box.height / 2;
25747
+ ctx.translate(centerX, centerY);
25748
+ ctx.scale(preset.activeScale, preset.activeScale);
25749
+ ctx.translate(-centerX, -centerY);
25750
+ }
25751
+ if (isActive && preset.glowEnabled && preset.glowColor) {
25752
+ ctx.shadowColor = preset.glowColor;
25753
+ ctx.shadowBlur = (preset.glowBlur ?? 16) * scale;
25754
+ ctx.shadowOffsetX = 0;
25755
+ ctx.shadowOffsetY = 0;
25756
+ } else if (preset.shadowEnabled && preset.shadowColor) {
25757
+ ctx.shadowColor = preset.shadowColor;
25758
+ ctx.shadowBlur = (preset.shadowBlur ?? 6) * scale;
25759
+ ctx.shadowOffsetX = (preset.shadowOffsetX ?? 0) * scale;
25760
+ ctx.shadowOffsetY = (preset.shadowOffsetY ?? 2) * scale;
25761
+ } else {
25762
+ ctx.shadowColor = "transparent";
25763
+ ctx.shadowBlur = 0;
25764
+ ctx.shadowOffsetX = 0;
25765
+ ctx.shadowOffsetY = 0;
25766
+ }
25767
+ if (preset.strokeEnabled) {
25768
+ ctx.lineWidth = (isActive && preset.activeStrokeWidth || preset.strokeWidth || 4) * scale;
25769
+ ctx.strokeStyle = isActive && preset.activeStrokeColor || preset.strokeColor || "#000000";
25770
+ ctx.lineJoin = "round";
25771
+ ctx.miterLimit = 2;
25772
+ ctx.strokeText(displayText, box.x, box.baselineY);
25773
+ }
25774
+ if (isActive) {
25775
+ ctx.fillStyle = preset.activeColor;
25776
+ ctx.globalAlpha = 1;
25777
+ } else {
25778
+ ctx.fillStyle = preset.inactiveColor;
25779
+ ctx.globalAlpha = preset.inactiveOpacity;
25780
+ }
25781
+ ctx.fillText(displayText, box.x, box.baselineY);
25782
+ ctx.restore();
25783
+ }
25784
+ return layout;
25785
+ }
25786
+
24902
25787
  // src/index.ts
24903
25788
  var VERSION = "1.4.0";
24904
25789
  // Annotate the CommonJS export names for ESM import in node:
@@ -24911,11 +25796,19 @@ var VERSION = "1.4.0";
24911
25796
  AnimationValidator,
24912
25797
  AssetRegistry,
24913
25798
  BUILTIN_CANVAS_TEMPLATES,
25799
+ BUILT_IN_CAPTION_PRESETS,
24914
25800
  BoundingBoxSchema,
24915
25801
  CANONICAL_RENDERER_VERSION,
24916
25802
  CANONICAL_SCHEMA_VERSION,
25803
+ CAPTION_PRESET_BOLD_PUNCH,
25804
+ CAPTION_PRESET_CINEMATIC_GOLD,
25805
+ CAPTION_PRESET_CLASSIC_SUBTITLE,
25806
+ CAPTION_PRESET_CLEAN_MINIMAL,
25807
+ CAPTION_PRESET_KARAOKE_POP,
25808
+ CAPTION_PRESET_NEON_GLOW,
24917
25809
  COMPOSITION_PRESETS,
24918
25810
  CanvasDevice,
25811
+ CaptionEngine,
24919
25812
  CommandBufferBuilder,
24920
25813
  CommandExecutor,
24921
25814
  CommandHistory,
@@ -24924,6 +25817,9 @@ var VERSION = "1.4.0";
24924
25817
  DEFAULT_DURATION,
24925
25818
  DEFAULT_FONT_SIZE,
24926
25819
  DEFAULT_FPS,
25820
+ DEFAULT_MAX_LINES,
25821
+ DEFAULT_MAX_WORDS_PER_LINE,
25822
+ DEFAULT_PAUSE_BREAK_MS,
24927
25823
  DEFAULT_TEXT_STYLE,
24928
25824
  DataBindingAuthoringEngine,
24929
25825
  DataBindingEngine,
@@ -24976,6 +25872,7 @@ var VERSION = "1.4.0";
24976
25872
  Mp4VideoEncoder,
24977
25873
  NodeRegistry,
24978
25874
  NullBackend,
25875
+ PAUSE_RELEASE_THRESHOLD_MS,
24979
25876
  PngSequenceEncoder,
24980
25877
  PrimitiveRegistry,
24981
25878
  ProResVideoEncoder,
@@ -25072,6 +25969,8 @@ var VERSION = "1.4.0";
25072
25969
  clearRecipeCache,
25073
25970
  cloneSceneWithNewIds,
25074
25971
  commandExecutor,
25972
+ compileBubblePath,
25973
+ compileBubbleSvg,
25075
25974
  compileSemanticOverlay,
25076
25975
  compileTextTemplate,
25077
25976
  componentRegistry,
@@ -25083,10 +25982,12 @@ var VERSION = "1.4.0";
25083
25982
  createBlankLottie,
25084
25983
  createBlankScratchTemplate,
25085
25984
  createBlankTextEffectConfig,
25985
+ createBubbleAssetDefinition,
25086
25986
  createCanvas,
25087
25987
  createDefaultRevealTrack,
25088
25988
  createDefaultScratchBlock,
25089
25989
  createEmptyScene,
25990
+ createFreeformBubbleAssetDefinition,
25090
25991
  createKeyframed,
25091
25992
  createPulseOpacityTrack,
25092
25993
  cubicBezier,
@@ -25102,6 +26003,7 @@ var VERSION = "1.4.0";
25102
26003
  downloadLottieMp4,
25103
26004
  downloadPngSequenceZip,
25104
26005
  downloadSceneWebM,
26006
+ drawChalkSpeechBubble,
25105
26007
  drawComicBurst,
25106
26008
  drawPerCharText,
25107
26009
  drawRibbonBanner,
@@ -25140,6 +26042,7 @@ var VERSION = "1.4.0";
25140
26042
  getAnimatableParamsForLayer,
25141
26043
  getCanvasTemplatePreset,
25142
26044
  getCanvasTemplatesByCategory,
26045
+ getCaptionPreset,
25143
26046
  getClipLayout,
25144
26047
  getDefaultText,
25145
26048
  getEffectByIdForApi,
@@ -25168,6 +26071,7 @@ var VERSION = "1.4.0";
25168
26071
  getTransitionsByCategory,
25169
26072
  getTransitionsByTags,
25170
26073
  getWebMFrameCount,
26074
+ hashCaptionPreset,
25171
26075
  hexToLottieColor,
25172
26076
  hexToLottieRgb,
25173
26077
  hexToRgba,
@@ -25221,8 +26125,11 @@ var VERSION = "1.4.0";
25221
26125
  removeKeyframe,
25222
26126
  removeTemplateKeyframe,
25223
26127
  removeTrack,
26128
+ renderBubbleAssetDefinition,
25224
26129
  renderBubbleBackground,
26130
+ renderBubblePresetBackground,
25225
26131
  renderCanonicalToCanvas,
26132
+ renderCaptionToCanvas,
25226
26133
  renderEvaluatedSceneToCanvas,
25227
26134
  renderPngSequence,
25228
26135
  renderSceneWebM,
@@ -25293,6 +26200,7 @@ var VERSION = "1.4.0";
25293
26200
  updateTrack,
25294
26201
  updateTrackMatte,
25295
26202
  upsertKeyframe,
26203
+ validateBubbleAssetDefinition,
25296
26204
  validateEffectDefinition,
25297
26205
  validateEffectDefinitionStrict,
25298
26206
  validateTextEffectDefinition,