@clypra-studio/engine 1.10.2 → 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/captions/index.cjs +543 -0
- package/dist/captions/index.cjs.map +1 -0
- package/dist/captions/index.d.cts +180 -0
- package/dist/captions/index.d.ts +180 -0
- package/dist/captions/index.js +35 -0
- package/dist/captions/index.js.map +1 -0
- package/dist/chunk-MVUSVC4T.js +504 -0
- package/dist/chunk-MVUSVC4T.js.map +1 -0
- package/dist/index.cjs +516 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +44 -43
- package/dist/index.d.ts +44 -43
- package/dist/index.js +48 -16
- package/dist/index.js.map +1 -1
- package/package.json +6 -1
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,
|
|
@@ -271,6 +283,7 @@ __export(index_exports, {
|
|
|
271
283
|
getAnimatableParamsForLayer: () => getAnimatableParamsForLayer,
|
|
272
284
|
getCanvasTemplatePreset: () => getCanvasTemplatePreset,
|
|
273
285
|
getCanvasTemplatesByCategory: () => getCanvasTemplatesByCategory,
|
|
286
|
+
getCaptionPreset: () => getCaptionPreset,
|
|
274
287
|
getClipLayout: () => getClipLayout,
|
|
275
288
|
getDefaultText: () => getDefaultText,
|
|
276
289
|
getEffectByIdForApi: () => getEffectByIdForApi,
|
|
@@ -299,6 +312,7 @@ __export(index_exports, {
|
|
|
299
312
|
getTransitionsByCategory: () => getTransitionsByCategory,
|
|
300
313
|
getTransitionsByTags: () => getTransitionsByTags,
|
|
301
314
|
getWebMFrameCount: () => getWebMFrameCount,
|
|
315
|
+
hashCaptionPreset: () => hashCaptionPreset,
|
|
302
316
|
hexToLottieColor: () => hexToLottieColor,
|
|
303
317
|
hexToLottieRgb: () => hexToLottieRgb,
|
|
304
318
|
hexToRgba: () => hexToRgba,
|
|
@@ -356,6 +370,7 @@ __export(index_exports, {
|
|
|
356
370
|
renderBubbleBackground: () => renderBubbleBackground,
|
|
357
371
|
renderBubblePresetBackground: () => renderBubblePresetBackground,
|
|
358
372
|
renderCanonicalToCanvas: () => renderCanonicalToCanvas,
|
|
373
|
+
renderCaptionToCanvas: () => renderCaptionToCanvas,
|
|
359
374
|
renderEvaluatedSceneToCanvas: () => renderEvaluatedSceneToCanvas,
|
|
360
375
|
renderPngSequence: () => renderPngSequence,
|
|
361
376
|
renderSceneWebM: () => renderSceneWebM,
|
|
@@ -25283,6 +25298,492 @@ async function calculateSha256(data) {
|
|
|
25283
25298
|
}
|
|
25284
25299
|
}
|
|
25285
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
|
+
|
|
25286
25787
|
// src/index.ts
|
|
25287
25788
|
var VERSION = "1.4.0";
|
|
25288
25789
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -25295,11 +25796,19 @@ var VERSION = "1.4.0";
|
|
|
25295
25796
|
AnimationValidator,
|
|
25296
25797
|
AssetRegistry,
|
|
25297
25798
|
BUILTIN_CANVAS_TEMPLATES,
|
|
25799
|
+
BUILT_IN_CAPTION_PRESETS,
|
|
25298
25800
|
BoundingBoxSchema,
|
|
25299
25801
|
CANONICAL_RENDERER_VERSION,
|
|
25300
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,
|
|
25301
25809
|
COMPOSITION_PRESETS,
|
|
25302
25810
|
CanvasDevice,
|
|
25811
|
+
CaptionEngine,
|
|
25303
25812
|
CommandBufferBuilder,
|
|
25304
25813
|
CommandExecutor,
|
|
25305
25814
|
CommandHistory,
|
|
@@ -25308,6 +25817,9 @@ var VERSION = "1.4.0";
|
|
|
25308
25817
|
DEFAULT_DURATION,
|
|
25309
25818
|
DEFAULT_FONT_SIZE,
|
|
25310
25819
|
DEFAULT_FPS,
|
|
25820
|
+
DEFAULT_MAX_LINES,
|
|
25821
|
+
DEFAULT_MAX_WORDS_PER_LINE,
|
|
25822
|
+
DEFAULT_PAUSE_BREAK_MS,
|
|
25311
25823
|
DEFAULT_TEXT_STYLE,
|
|
25312
25824
|
DataBindingAuthoringEngine,
|
|
25313
25825
|
DataBindingEngine,
|
|
@@ -25360,6 +25872,7 @@ var VERSION = "1.4.0";
|
|
|
25360
25872
|
Mp4VideoEncoder,
|
|
25361
25873
|
NodeRegistry,
|
|
25362
25874
|
NullBackend,
|
|
25875
|
+
PAUSE_RELEASE_THRESHOLD_MS,
|
|
25363
25876
|
PngSequenceEncoder,
|
|
25364
25877
|
PrimitiveRegistry,
|
|
25365
25878
|
ProResVideoEncoder,
|
|
@@ -25529,6 +26042,7 @@ var VERSION = "1.4.0";
|
|
|
25529
26042
|
getAnimatableParamsForLayer,
|
|
25530
26043
|
getCanvasTemplatePreset,
|
|
25531
26044
|
getCanvasTemplatesByCategory,
|
|
26045
|
+
getCaptionPreset,
|
|
25532
26046
|
getClipLayout,
|
|
25533
26047
|
getDefaultText,
|
|
25534
26048
|
getEffectByIdForApi,
|
|
@@ -25557,6 +26071,7 @@ var VERSION = "1.4.0";
|
|
|
25557
26071
|
getTransitionsByCategory,
|
|
25558
26072
|
getTransitionsByTags,
|
|
25559
26073
|
getWebMFrameCount,
|
|
26074
|
+
hashCaptionPreset,
|
|
25560
26075
|
hexToLottieColor,
|
|
25561
26076
|
hexToLottieRgb,
|
|
25562
26077
|
hexToRgba,
|
|
@@ -25614,6 +26129,7 @@ var VERSION = "1.4.0";
|
|
|
25614
26129
|
renderBubbleBackground,
|
|
25615
26130
|
renderBubblePresetBackground,
|
|
25616
26131
|
renderCanonicalToCanvas,
|
|
26132
|
+
renderCaptionToCanvas,
|
|
25617
26133
|
renderEvaluatedSceneToCanvas,
|
|
25618
26134
|
renderPngSequence,
|
|
25619
26135
|
renderSceneWebM,
|