@softure-ai/marketing-kit 0.41.0 → 0.43.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.
Files changed (41) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/README.md +13 -0
  3. package/dist/compose/timeline.d.ts +6 -0
  4. package/dist/compose/timeline.d.ts.map +1 -1
  5. package/dist/compose/timeline.js +9 -1
  6. package/dist/compose/timeline.js.map +1 -1
  7. package/dist/footage/compose.d.ts.map +1 -1
  8. package/dist/footage/compose.js +3 -3
  9. package/dist/footage/compose.js.map +1 -1
  10. package/dist/footage/draft-overlays.d.ts +21 -1
  11. package/dist/footage/draft-overlays.d.ts.map +1 -1
  12. package/dist/footage/draft-overlays.js +180 -26
  13. package/dist/footage/draft-overlays.js.map +1 -1
  14. package/dist/footage/draft-text-width.d.ts +43 -0
  15. package/dist/footage/draft-text-width.d.ts.map +1 -1
  16. package/dist/footage/draft-text-width.js +62 -1
  17. package/dist/footage/draft-text-width.js.map +1 -1
  18. package/dist/footage/draft.d.ts +29 -2
  19. package/dist/footage/draft.d.ts.map +1 -1
  20. package/dist/footage/draft.js +64 -29
  21. package/dist/footage/draft.js.map +1 -1
  22. package/dist/footage/overlay-html.d.ts.map +1 -1
  23. package/dist/footage/overlay-html.js +6 -5
  24. package/dist/footage/overlay-html.js.map +1 -1
  25. package/dist/footage/overlays.d.ts +74 -2
  26. package/dist/footage/overlays.d.ts.map +1 -1
  27. package/dist/footage/overlays.js +115 -2
  28. package/dist/footage/overlays.js.map +1 -1
  29. package/dist/index.d.ts +4 -4
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +4 -4
  32. package/dist/index.js.map +1 -1
  33. package/package.json +1 -1
  34. package/src/compose/timeline.ts +12 -1
  35. package/src/footage/compose.ts +3 -3
  36. package/src/footage/draft-overlays.ts +226 -27
  37. package/src/footage/draft-text-width.ts +90 -1
  38. package/src/footage/draft.ts +91 -32
  39. package/src/footage/overlay-html.ts +6 -4
  40. package/src/footage/overlays.ts +135 -2
  41. package/src/index.ts +29 -1
@@ -4,6 +4,7 @@ import { fileURLToPath } from "node:url";
4
4
 
5
5
  import type { CaptionLayout, LayoutOverride } from "../compose/timeline.js";
6
6
  import type { BrandColors } from "../config/colors.js";
7
+ import type { BrandFont } from "../config/config.js";
7
8
  import { err, ok } from "./failure.js";
8
9
  import type { TimedWord } from "../voice/voiceover.js";
9
10
  import { CAPTION_HIGHLIGHT_BOX_PADDING_EM, CAPTION_HIGHLIGHT_SECONDS, CAPTION_UNDERLINE_EM, getCaptionHighlight, type CaptionHighlightLook } from "./caption-highlight.js";
@@ -15,12 +16,12 @@ import { toAssColor } from "./draft-color.js";
15
16
  import { buildDraftOverlayEvents, getDraftOverlayStyles, getRoundedRectDrawing } from "./draft-overlays.js";
16
17
  import { getCaptionCornerRadius, getCornerRadiusPx, getStylePadding, type BrandStyle } from "./brand-style.js";
17
18
  import { getDraftBoxEvents } from "./draft-boxes.js";
18
- import { createDraftTextMeasure, wrapDraftLine, type DraftTextMeasure } from "./draft-text-width.js";
19
- import { CAPTION_PILL_PADDING } from "../compose/timeline.js";
19
+ import { createDraftFont, getSpacingWidth, toDraftAssSize, toDraftSpacing, withLetterSpacing, wrapDraftLine, type DraftFont, type DraftTextMeasure } from "./draft-text-width.js";
20
+ import { CAPTION_LETTER_SPACING_EM, CAPTION_PILL_PADDING, CAPTION_RIBBON_LETTER_SPACING_EM } from "../compose/timeline.js";
20
21
  import { estimateTextWidth, isHandFontNeeded } from "./overlays.js";
21
22
  import { HAND_FONT } from "./hand-font.js";
22
23
  import type { OverlayColors } from "./overlay-html.js";
23
- import { resolveEdlOverlays, type OverlayFontRole, type ResolvedOverlay } from "./overlays.js";
24
+ import { resolveEdlOverlays, type OverlayFontRole, type OverlayTextMeasure, type ResolvedOverlay } from "./overlays.js";
24
25
  import { chooseFootageDraftFont } from "./draft-font.js";
25
26
  import { getEdlDuration, getSegmentStarts, remapWords, type Edl } from "./edl.js";
26
27
  import { declickSpeech, describeDeclick, type DeclickReport } from "./declick.js";
@@ -69,6 +70,46 @@ export const FOOTAGE_DRAFT_FONT = {
69
70
  file: join(dirname(fileURLToPath(import.meta.url)), "../../fonts/DejaVuSans-Bold-Latin.ttf"),
70
71
  } as const;
71
72
 
73
+ /** The draft's fonts by role: the captions' (`body`) and the overlays' heading and interface fonts (the body's where the brand has none). */
74
+ export interface FootageDraftFonts {
75
+ body: DraftFont;
76
+ heading: DraftFont;
77
+ interface: DraftFont;
78
+ }
79
+
80
+ /**
81
+ * The draft font of a chosen file, or the file's family at libass's own size (`assScale` 1) measured by the
82
+ * `OVERLAY_CHAR_EM` estimate when its metrics cannot be read: the draft is still drawn, as before this measure existed.
83
+ */
84
+ function readDraftFont(source: { family: string; file: string }, read: (path: string) => Buffer): DraftFont {
85
+ return createDraftFont(source, read(source.file)) ?? { family: source.family, file: source.file, assScale: 1, measure: estimateTextWidth };
86
+ }
87
+
88
+ /**
89
+ * The fonts a footage draft draws with, sized as the render sizes them (`DraftFont`): the brand's body font (its file
90
+ * closest to the caption weight, `chooseFootageDraftFont`), else `FOOTAGE_DRAFT_FONT`; its heading and interface fonts
91
+ * where it has a TrueType or OpenType file of another family for them, else the body's. The same set serves a draft and any other
92
+ * drawing an app makes with `buildFootageDraftCaptions` (a preset preview), so they all draw the render's sizes.
93
+ */
94
+ export function loadFootageDraftFonts(fonts: Partial<Record<"body" | OverlayFontRole, BrandFont | null>> | undefined, read: (path: string) => Buffer = (path) => readFileSync(/* turbopackIgnore: true */ path)): FootageDraftFonts {
95
+ const body = readDraftFont(chooseFootageDraftFont(fonts?.body ?? null, read) ?? FOOTAGE_DRAFT_FONT, read);
96
+ const forRole = (role: OverlayFontRole): DraftFont => {
97
+ const chosen = chooseFootageDraftFont(fonts?.[role] ?? null, read);
98
+ // libass finds a font by its family: a second file of the body's family would compete with the body's, so a role of that family draws with the body's file.
99
+ return chosen === null || chosen.family === body.family ? body : readDraftFont(chosen, read);
100
+ };
101
+ return { body, heading: forRole("heading"), interface: forRole("interface") };
102
+ }
103
+
104
+ /**
105
+ * The render's measure of overlay text (`OverlayTextMeasure`) from the draft's fonts: a line's width in CSS px in the
106
+ * font of its role, its letters spaced as the render spaces them. With it `getDrawnOverlayCard` answers the card the
107
+ * render lays out, as the draft draws it, so an app reports where a card really stands.
108
+ */
109
+ export function createOverlayTextMeasure(fonts: FootageDraftFonts): OverlayTextMeasure {
110
+ return (role, text, cssPx, spacingEm) => fonts[role].measure(text, cssPx) + getSpacingWidth(text, spacingEm, cssPx);
111
+ }
112
+
72
113
  /** How far a segment marked `kenburns` zooms when the film's `kenBurnsPercent` is 0, as in the render. */
73
114
  const HAND_KEN_BURNS_PERCENT = 4;
74
115
  const DRAFT_CRF = 28;
@@ -154,6 +195,18 @@ export interface FootageDraftCaptionsInput {
154
195
  style?: BrandStyle;
155
196
  /** The width of a line in the captions' font (`createDraftTextMeasure`); absent or null: the `OVERLAY_CHAR_EM` estimate. */
156
197
  measure?: DraftTextMeasure | null;
198
+ /**
199
+ * The draft's fonts (`loadFootageDraftFonts`): every text is drawn as large as the render draws it, its lines broken
200
+ * by the font's own widths in CSS px, as the render's are. It takes the place of `fontFamily`, `overlayFonts` and
201
+ * `measure`. Absent: those fields, and every size goes to libass as it is (libass then draws a brand font smaller
202
+ * than the render does, by its `usWinAscent + usWinDescent` over its em).
203
+ */
204
+ fonts?: FootageDraftFonts;
205
+ }
206
+
207
+ /** The overlays' heading and interface families where they are not the body's (the styles' own). */
208
+ function getOverlayFamilies(fonts: FootageDraftFonts): Partial<Record<OverlayFontRole, string>> {
209
+ return Object.fromEntries((["heading", "interface"] as const).flatMap((role) => (fonts[role].family === fonts.body.family ? [] : [[role, fonts[role].family.replace(/,/g, " ")]])));
157
210
  }
158
211
 
159
212
  /** The pieces of a `highlight` chunk's span: cut wherever a word lights up or goes out (in centiseconds), with the words lit in each. */
@@ -228,6 +281,8 @@ interface DrawnWord {
228
281
  left: number;
229
282
  width: number;
230
283
  lineTop: number;
284
+ /** How tall libass draws the word's line. */
285
+ lineHeight: number;
231
286
  }
232
287
 
233
288
  /** The pill of a chunk drawn in the brand's style: its lines (broken by width), its shadow, box and border events, and each word's place. */
@@ -244,6 +299,8 @@ interface DrawnPillInput {
244
299
  frame: Frame;
245
300
  style: BrandStyle;
246
301
  measure: DraftTextMeasure;
302
+ /** How tall libass draws a line of the caption font, per CSS px of its size (`DraftFont.assScale`). */
303
+ lineScale: number;
247
304
  colors: { background: string; accent: string };
248
305
  }
249
306
 
@@ -255,6 +312,7 @@ interface DrawnPillInput {
255
312
  function getDrawnPill(input: DrawnPillInput): DrawnPill {
256
313
  const { place, frame, style, measure } = input;
257
314
  const scale = frame.width / 1080;
315
+ const lineHeight = place.fontSize * input.lineScale;
258
316
  const pad = getStylePadding(CAPTION_PILL_PADDING, style.padding);
259
317
  const areaWidth = frame.width - place.left - place.right;
260
318
  const lines = wrapDraftLine(input.words, areaWidth - 2 * pad.x, place.fontSize, measure);
@@ -265,8 +323,8 @@ function getDrawnPill(input: DrawnPillInput): DrawnPill {
265
323
  breaks.add(counted - 1);
266
324
  }
267
325
  const width = Math.min(areaWidth, Math.max(...lines.map((line) => measure(line.join(" "), place.fontSize))) + 2 * pad.x);
268
- const height = lines.length * place.fontSize + 2 * pad.y;
269
- const textTop = place.bottom === undefined ? place.top : frame.height - place.bottom - lines.length * place.fontSize;
326
+ const height = lines.length * lineHeight + 2 * pad.y;
327
+ const textTop = place.bottom === undefined ? place.top : frame.height - place.bottom - lines.length * lineHeight;
270
328
  const box = { left: place.left + (areaWidth - width) / 2, top: textTop - pad.y, width, height };
271
329
  const at = `${toAssTime(input.span.start)},${toAssTime(input.span.end)}`;
272
330
  const radius = getCornerRadiusPx(getCaptionCornerRadius(style), height, scale);
@@ -275,7 +333,7 @@ function getDrawnPill(input: DrawnPillInput): DrawnPill {
275
333
  const lineLeft = place.left + (areaWidth - measure(line.join(" "), place.fontSize)) / 2;
276
334
  return line.map((word, index) => {
277
335
  const width = measure(word, place.fontSize);
278
- return { left: lineLeft + measure(line.slice(0, index + 1).join(" "), place.fontSize) - width, width, lineTop: textTop + lineIndex * place.fontSize };
336
+ return { left: lineLeft + measure(line.slice(0, index + 1).join(" "), place.fontSize) - width, width, lineTop: textTop + lineIndex * lineHeight, lineHeight };
279
337
  });
280
338
  });
281
339
  return { breaks, events: getDraftBoxEvents({ layer: 0, at, styleName: "CaptionShape", box, radius, color: input.colors.background, accent: input.colors.accent, style, scale }), words };
@@ -288,6 +346,8 @@ interface RibbonInput {
288
346
  frame: Frame;
289
347
  tilt: number;
290
348
  measure: DraftTextMeasure;
349
+ /** How tall libass draws a line of the caption font, per CSS px of its size. */
350
+ lineScale: number;
291
351
  colors: { background: string; text: string };
292
352
  }
293
353
 
@@ -304,6 +364,7 @@ function getRibbonEvents(input: RibbonInput): string[] {
304
364
  const areaWidth = frame.width - place.left - place.right;
305
365
  const pad = { x: padding.x * size, y: padding.y * size };
306
366
  const lines = wrapDraftLine(input.text.split(" "), areaWidth - 2 * pad.x, size, measure).map((line) => line.join(" "));
367
+ const drawn = size * input.lineScale;
307
368
  const step = lineHeight * size;
308
369
  const blockTop = place.bottom === undefined ? place.top : frame.height - place.bottom - lines.length * step;
309
370
  const centre = { x: place.left + areaWidth / 2, y: blockTop + (lines.length * step) / 2 };
@@ -313,8 +374,8 @@ function getRibbonEvents(input: RibbonInput): string[] {
313
374
  return lines.flatMap((line, index) => {
314
375
  const width = measure(line, size) + 2 * pad.x;
315
376
  // libass draws a line one font size tall: the strip is that plus its padding, both centred in the line's step.
316
- const textTop = blockTop + index * step + (step - size) / 2;
317
- const strip = { left: centre.x - width / 2, top: textTop - pad.y, width, height: size + 2 * pad.y };
377
+ const textTop = blockTop + index * step + (step - drawn) / 2;
378
+ const strip = { left: centre.x - width / 2, top: textTop - pad.y, width, height: drawn + 2 * pad.y };
318
379
  return [
319
380
  ...getDraftBoxEvents({ layer: 0, at, styleName: "CaptionShape", box: strip, radius: radius * scale, color: input.colors.background, accent: input.colors.background, style: { shadow: "none", border: "none" }, scale, motion: turn }),
320
381
  `Dialogue: 1,${at},CaptionShape,,0,0,0,,{\\an8\\pos(${String(Math.round(centre.x))},${String(Math.round(textTop))})${turn}\\b1\\1c&H${text.slice(4)}&\\1a&H${text.slice(2, 4)}&\\bord0\\shad0}${toAssText(line)}`,
@@ -334,7 +395,7 @@ function buildUnderlineEvents(chunk: { words: TimedWord[] }, span: { start: numb
334
395
  return getHighlightPieces(chunk, span).flatMap((piece) =>
335
396
  words.flatMap((word, index) => {
336
397
  if (piece.lit[index] !== true) return [];
337
- const bar = { left: word.left, top: word.lineTop + Math.round(fontSize * DRAFT_UNDERLINE_TOP_EM), width: word.width, height: thickness };
398
+ const bar = { left: word.left, top: word.lineTop + Math.round(word.lineHeight * DRAFT_UNDERLINE_TOP_EM), width: word.width, height: thickness };
338
399
  return [`Dialogue: 0,${toAssTime(piece.from / 100)},${toAssTime(piece.to / 100)},CaptionShape,,0,0,0,,{\\an7\\pos(0,0)${toUnderlineColorTags(color)}\\bord0\\shad0\\p1}${getRoundedRectDrawing(bar, thickness / 2)}{\\p0}`];
339
400
  }),
340
401
  );
@@ -371,7 +432,13 @@ export function buildFootageDraftCaptions(input: FootageDraftCaptionsInput): str
371
432
  if (chunks.length === 0 && overlays.length === 0) return null;
372
433
  const spans = getCaptionSpans(chunks, getEdlDuration(input.edl));
373
434
  const { caption, frame } = input;
374
- const family = (input.fontFamily ?? FOOTAGE_DRAFT_FONT.family).replace(/,/g, " ");
435
+ const fonts = input.fonts;
436
+ const family = (fonts?.body.family ?? input.fontFamily ?? FOOTAGE_DRAFT_FONT.family).replace(/,/g, " ");
437
+ // Every size below is the render's (CSS px); an ASS size is written through the body font's scale (1 without `fonts`).
438
+ const lineScale = fonts?.body.assScale ?? 1;
439
+ const styleSize = toDraftAssSize(caption.fontSize, lineScale);
440
+ // With the draft's fonts the letters are spaced as the render spaces them (`letter-spacing`); without, as before.
441
+ const spacing = fonts === undefined ? 0 : toDraftSpacing(isRibbon ? CAPTION_RIBBON_LETTER_SPACING_EM : CAPTION_LETTER_SPACING_EM, caption.fontSize);
375
442
  const colors = input.colors ?? FOOTAGE_DRAFT_CAPTION_COLORS;
376
443
  const text = toAssColor(colors.captionText);
377
444
  const box = toAssColor(colors.captionBackground);
@@ -389,7 +456,7 @@ export function buildFootageDraftCaptions(input: FootageDraftCaptionsInput): str
389
456
  const toStyle = (name: string, place: CaptionLayout): string => [
390
457
  name,
391
458
  family,
392
- caption.fontSize,
459
+ styleSize,
393
460
  look.primary,
394
461
  look.primary,
395
462
  look.outlineColor,
@@ -400,7 +467,7 @@ export function buildFootageDraftCaptions(input: FootageDraftCaptionsInput): str
400
467
  0,
401
468
  100,
402
469
  100,
403
- 0,
470
+ spacing,
404
471
  0,
405
472
  look.borderStyle,
406
473
  look.outline,
@@ -431,17 +498,18 @@ export function buildFootageDraftCaptions(input: FootageDraftCaptionsInput): str
431
498
  const boxPadding = Math.max(1, Math.round(caption.fontSize * CAPTION_HIGHLIGHT_BOX_PADDING_EM));
432
499
  // With the brand's style a `highlight` chunk stands on a drawn rounded pill (its lines broken here), not libass's box.
433
500
  const style = isHighlight ? input.style : undefined;
434
- const measure = input.measure ?? ((line: string, size: number) => estimateTextWidth(line, size));
501
+ const measure =
502
+ fonts === undefined ? (input.measure ?? ((line: string, size: number) => estimateTextWidth(line, size))) : withLetterSpacing(fonts.body.measure, isRibbon ? CAPTION_RIBBON_LETTER_SPACING_EM : CAPTION_LETTER_SPACING_EM);
435
503
  const pillColors = { background: colors.captionBackground, accent: lit.style === "box" ? lit.box : colors.captionHighlight };
436
504
  const events = chunks.flatMap((chunk, index) => {
437
505
  const span = spans[index] as { start: number; end: number };
438
506
  if (isRibbon) {
439
507
  const ribbonText = toCaptionDisplayText(input.film, chunk.words.map((word) => word.text.replace(/\u00a0/g, " ")).join(" "), input.locale ?? "en-US");
440
- return getRibbonEvents({ text: ribbonText, span, place: places[index] ?? caption, frame, tilt: getCaptionRibbonTilt(input.film), measure, colors: { background: colors.captionBackground, text: colors.captionText } });
508
+ return getRibbonEvents({ text: ribbonText, span, place: places[index] ?? caption, frame, tilt: getCaptionRibbonTilt(input.film), measure, lineScale, colors: { background: colors.captionBackground, text: colors.captionText } });
441
509
  }
442
510
  const styleName = styleNames[index] ?? "Caption";
443
511
  const pill =
444
- style === undefined ? null : getDrawnPill({ words: chunk.words.map((word) => word.text.replace(/\u00a0/g, " ")), span, place: places[index] ?? caption, frame, style, measure, colors: pillColors });
512
+ style === undefined ? null : getDrawnPill({ words: chunk.words.map((word) => word.text.replace(/\u00a0/g, " ")), span, place: places[index] ?? caption, frame, style, measure, lineScale, colors: pillColors });
445
513
  const layout: ChunkLayout = pill === null ? BOXLESS_LAYOUT : { breaks: pill.breaks, hasPill: true };
446
514
  const drawn = pill?.events ?? [];
447
515
  if (isHighlight && lit.style === "underline") {
@@ -465,14 +533,14 @@ export function buildFootageDraftCaptions(input: FootageDraftCaptionsInput): str
465
533
  "[V4+ Styles]",
466
534
  "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding",
467
535
  ...styles.map((line) => `Style: ${line}`),
468
- ...((style === undefined && !isRibbon) || chunks.length === 0 ? [] : [`Style: CaptionShape,${family},${String(caption.fontSize)},&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,0,0,7,0,0,0,1`]),
536
+ ...((style === undefined && !isRibbon) || chunks.length === 0 ? [] : [`Style: CaptionShape,${family},${String(styleSize)},&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,0,0,0,0,100,100,${String(spacing)},0,1,0,0,7,0,0,0,1`]),
469
537
  ...(overlays.length === 0 ? [] : getDraftOverlayStyles(family, input.overlayColors ?? FOOTAGE_DRAFT_OVERLAY_COLORS)),
470
538
  "",
471
539
  "[Events]",
472
540
  "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text",
473
541
  ...events,
474
542
  ...buildDraftOverlayEvents(overlays, input.overlayColors ?? FOOTAGE_DRAFT_OVERLAY_COLORS, input.frame, {
475
- ...(input.overlayFonts === undefined ? {} : { fonts: input.overlayFonts }),
543
+ ...(fonts === undefined ? (input.overlayFonts === undefined ? {} : { fonts: input.overlayFonts }) : { fonts: getOverlayFamilies(fonts), drawnFonts: fonts }),
476
544
  ...(input.locale === undefined ? {} : { locale: input.locale }),
477
545
  ...(input.style === undefined ? {} : { style: input.style }),
478
546
  }),
@@ -1118,34 +1186,25 @@ async function runRenderFootageDraft(
1118
1186
  ? { background: brandColors.background, foreground: brandColors.foreground, accent: brandColors.accent }
1119
1187
  : FOOTAGE_DRAFT_OVERLAY_COLORS;
1120
1188
  const brandFont = chooseFootageDraftFont(options.brand?.fonts.body ?? null);
1121
- const font = brandFont ?? FOOTAGE_DRAFT_FONT;
1122
- // The overlays' heading and interface fonts, where the brand has a TrueType or OpenType file for them.
1123
- const roleFonts = Object.fromEntries(
1124
- (["heading", "interface"] as const).flatMap((role) => {
1125
- const chosen = overlays.length === 0 ? null : chooseFootageDraftFont(options.brand?.fonts[role] ?? null);
1126
- return chosen === null || chosen.family === font.family ? [] : [[role, chosen]];
1127
- }),
1128
- ) as Partial<Record<OverlayFontRole, { family: string; file: string }>>;
1189
+ // Every font sized and measured as the render sizes it; the overlays' roles only when there are overlays to draw.
1190
+ const fonts = loadFootageDraftFonts(overlays.length === 0 ? { body: options.brand?.fonts.body ?? null } : options.brand?.fonts);
1129
1191
  const captions = buildFootageDraftCaptions({
1130
1192
  film,
1131
1193
  edl,
1132
1194
  words,
1133
1195
  caption: getFootageCaptionLayout(film, options.layout ?? {}),
1134
1196
  frame,
1135
- fontFamily: font.family,
1197
+ fonts,
1136
1198
  ...(options.brand === undefined ? {} : { colors: options.brand.colors }),
1137
1199
  ...(overlays.length === 0 ? {} : { overlays, overlayColors }),
1138
- ...(Object.keys(roleFonts).length === 0 ? {} : { overlayFonts: Object.fromEntries(Object.entries(roleFonts).map(([role, chosen]) => [role, chosen.family])) }),
1139
1200
  ...(options.brand?.locale === undefined ? {} : { locale: options.brand.locale }),
1140
1201
  ...(options.brand?.style === undefined ? {} : { style: options.brand.style }),
1141
- // A drawn pill and a ribbon break their lines by the font's own widths.
1142
- ...(options.brand?.style !== undefined || film.captionStyle === "ribbon" ? { measure: createDraftTextMeasure(readFileSync(/* turbopackIgnore: true */ font.file)) } : {}),
1143
1202
  });
1144
1203
  if (captions !== null) {
1145
1204
  writeFileSync(join(draftDir, "captions.ass"), captions);
1146
1205
  mkdirSync(join(draftDir, "fonts"));
1147
1206
  const handFont = isHandFontNeeded(overlays) ? [HAND_FONT.file] : [];
1148
- for (const file of new Set([font.file, ...Object.values(roleFonts).map((chosen) => chosen.file), ...handFont])) copyFileSync(file, join(draftDir, "fonts", basename(file)));
1207
+ for (const file of new Set([fonts.body.file, fonts.heading.file, fonts.interface.file, ...handFont])) copyFileSync(file, join(draftDir, "fonts", basename(file)));
1149
1208
  if (brandFont === null && options.brand?.fonts.body != null) {
1150
1209
  log(`draft: the brand's body font ${options.brand.fonts.body.family} has no TrueType or OpenType file ffmpeg can read; the captions use ${FOOTAGE_DRAFT_FONT.family}.`);
1151
1210
  }
@@ -1223,12 +1282,12 @@ async function runRenderFootageDraft(
1223
1282
  const draft = getFootageDraftFrame(frame, options.width);
1224
1283
  const bytes = statSync(output).size;
1225
1284
  const draftMusic: FootageDraftMusic = music.value.kind === "file" ? "played" : music.value.kind;
1226
- const notes = [captions === null ? "no captions" : `captions in ${font.family}`, draftMusic === "played" ? "music" : draftMusic === "none" ? "no music" : "music not cached"];
1285
+ const notes = [captions === null ? "no captions" : `captions in ${fonts.body.family}`, draftMusic === "played" ? "music" : draftMusic === "none" ? "no music" : "music not cached"];
1227
1286
  log(`draft: ${output}, ${draft.width}×${draft.height}, ${seconds.toFixed(1)} s, ${bytes} bytes, ${notes.join(", ")}.`);
1228
1287
  return ok({
1229
1288
  output,
1230
1289
  music: draftMusic,
1231
- captionFont: captions === null ? null : font.family,
1290
+ captionFont: captions === null ? null : fonts.body.family,
1232
1291
  seconds,
1233
1292
  assembly,
1234
1293
  pieces: pieceCount,
@@ -1,5 +1,6 @@
1
1
  import type { BrandColors } from "../config/colors.js";
2
2
  import { escapeHtml } from "../compose/html.js";
3
+ import { toCssEm } from "../compose/timeline.js";
3
4
  import { getCornerRadiusCss, getStyleBorderCss, getStyleShadowCss, type BrandStyle } from "./brand-style.js";
4
5
  import type { OverlayEntrance } from "./overlay-schema.js";
5
6
  import { HAND_FONT } from "./hand-font.js";
@@ -24,6 +25,7 @@ import {
24
25
  } from "./overlay-motion.js";
25
26
  import {
26
27
  NOTE_LINE_HEIGHT,
28
+ OVERLAY_LETTER_SPACING_EM,
27
29
  OVERLAY_LINE_HEIGHT,
28
30
  OVERLAY_LIST_LINE_HEIGHT,
29
31
  OVERLAY_PADDING_EM,
@@ -108,7 +110,7 @@ function getOverlayCss(colors: OverlayColors, fonts: { heading: string; interfac
108
110
  `.ov{position:absolute;display:flex;pointer-events:none}`,
109
111
  `.ov.right{justify-content:flex-end}`,
110
112
  `.ov.center{justify-content:center}`,
111
- `.ov-card{display:flex;align-items:center;gap:.45em;padding:${OVERLAY_PADDING_EM.y}em ${OVERLAY_PADDING_EM.x}em;border-radius:.42em;background:${colors.background};color:${colors.foreground};box-shadow:0 14px 44px rgba(0,0,0,.35);line-height:${OVERLAY_LINE_HEIGHT};font-family:${fonts.interface};font-weight:700;letter-spacing:-.015em;max-width:100%;overflow-wrap:anywhere;transform-origin:0% 100%}`,
113
+ `.ov-card{display:flex;align-items:center;gap:.45em;padding:${OVERLAY_PADDING_EM.y}em ${OVERLAY_PADDING_EM.x}em;border-radius:.42em;background:${colors.background};color:${colors.foreground};box-shadow:0 14px 44px rgba(0,0,0,.35);line-height:${OVERLAY_LINE_HEIGHT};font-family:${fonts.interface};font-weight:700;letter-spacing:${toCssEm(OVERLAY_LETTER_SPACING_EM.card)};max-width:100%;overflow-wrap:anywhere;transform-origin:0% 100%}`,
112
114
  `.ov.right .ov-card{transform-origin:100% 100%}`,
113
115
  `.ov-keyword .ov-card{font-family:${fonts.heading};border-bottom:.12em solid ${colors.accent}}`,
114
116
  `.ov-dot{flex:none;width:.36em;height:.36em;border-radius:50%;background:${colors.accent}}`,
@@ -137,10 +139,10 @@ function getOverlayCss(colors: OverlayColors, fonts: { heading: string; interfac
137
139
  `.ov-stack.left .ov-place{justify-content:flex-start}`,
138
140
  `.ov-line{min-height:1lh}`,
139
141
  `.ty .ov-stack:not(.left) .ch{display:none}`,
140
- `.ov-hook-title .ov-line{font-family:${fonts.heading};font-weight:800;letter-spacing:-.02em}`,
142
+ `.ov-hook-title .ov-line{font-family:${fonts.heading};font-weight:800;letter-spacing:${toCssEm(OVERLAY_LETTER_SPACING_EM.hookTitle)}}`,
141
143
  `.ov-place{display:flex;align-items:center;justify-content:center;gap:.25em;font-weight:650;opacity:.92;margin-bottom:.12em}`,
142
144
  `.ov-pin{flex:none;width:1.05em;height:1.05em}`,
143
- `.ov-place-card .ov-line{font-family:${fonts.heading};font-weight:800;line-height:${OVERLAY_TITLE_LINE_HEIGHT};letter-spacing:-.025em}`,
145
+ `.ov-place-card .ov-line{font-family:${fonts.heading};font-weight:800;line-height:${OVERLAY_TITLE_LINE_HEIGHT};letter-spacing:${toCssEm(OVERLAY_LETTER_SPACING_EM.placeTitle)}}`,
144
146
  `.ov-sub{font-weight:600;opacity:.88;margin-top:.12em}`,
145
147
  `.ov-search{width:100%;border-radius:999px;padding:${SEARCH_BAR_PADDING_EM.y}em ${SEARCH_BAR_PADDING_EM.x}em;gap:.5em;font-weight:600;letter-spacing:0;justify-content:flex-start;transform-origin:50% 50%}`,
146
148
  `.ov-glass{flex:none;width:1em;height:1em}`,
@@ -172,7 +174,7 @@ function getOverlayCss(colors: OverlayColors, fonts: { heading: string; interfac
172
174
  `.ov-note .nt{position:absolute;white-space:nowrap;line-height:${NOTE_LINE_HEIGHT}}`,
173
175
  `.ov-note .nw{opacity:0}`,
174
176
  `.ov-note .tick{position:absolute;inset:0;overflow:visible}`,
175
- `.ov-strip .nt{font-family:${fonts.heading};font-weight:800;letter-spacing:.02em}`,
177
+ `.ov-strip .nt{font-family:${fonts.heading};font-weight:800;letter-spacing:${toCssEm(OVERLAY_LETTER_SPACING_EM.strip)}}`,
176
178
  `.ov-hand{font-family:"${HAND_FONT.family}",cursive;font-weight:400}`,
177
179
  `.ov-endword{white-space:nowrap;line-height:${OVERLAY_LINE_HEIGHT};text-shadow:0 3px 10px rgba(0,0,0,.45)}`,
178
180
  // The brand's style (after the kinds' own rules, so it wins over them; the search bar stays a pill).
@@ -107,6 +107,11 @@ export const LIST_CHIP_MIN_FONT_SIZE = 36;
107
107
  export const LIST_CHIP_CENTRED_MIN_FONT_SIZE = 32;
108
108
  /** A place card title's line height, tighter for its big type. */
109
109
  export const OVERLAY_TITLE_LINE_HEIGHT = 1.05;
110
+ /**
111
+ * The render's letter spacing, in em: every card's text (`card`), a hook title's and a place card's title lines, and a
112
+ * paper strip's capitals. The draft spaces its letters the same (`\\fsp`) and measures its lines with it.
113
+ */
114
+ export const OVERLAY_LETTER_SPACING_EM = { card: -0.015, hookTitle: -0.02, placeTitle: -0.025, strip: 0.02 } as const;
110
115
  export const OVERLAY_PADDING_EM = { x: 0.6, y: 0.35 } as const;
111
116
  /** The search field's padding round its query, in font sizes (its height is the line plus twice this). */
112
117
  export const SEARCH_BAR_PADDING_EM = { x: 0.7, y: 0.55 } as const;
@@ -431,9 +436,20 @@ export function getSideTextLines(overlay: SideTextOverlay): string[] {
431
436
  * The card a drawn overlay shows, in px of the frame, or null for a `punch_in`. A keyword, a lower third and a list
432
437
  * are placed in the whole width they may take (`box`); their card fits their widest line (by `estimateTextWidth`)
433
438
  * plus its padding, on its side, as tall as the box. Every other kind's card is its box (`getOverlayBounds`). The
434
- * draft draws this card, so a report of where an overlay sits (does it cover a face?) reads the same box.
439
+ * draft draws this card, so a report of where an overlay sits (does it cover a face?) reads the same box. With the
440
+ * brand fonts' `measure` (`createOverlayTextMeasure`), a keyword's, a lower third's and a counter's card is the one the
441
+ * render lays out (`getSideTextLayout`, `getCounterCard`, the counter at its final value), as the draft then draws it;
442
+ * a highlighted keyword or hook title (no card of its own) answers where its marks show.
435
443
  */
436
- export function getDrawnOverlayCard(overlay: ResolvedOverlay): OverlayBox | null {
444
+ export function getDrawnOverlayCard(overlay: ResolvedOverlay, measure?: OverlayTextMeasure): OverlayBox | null {
445
+ if (measure !== undefined && overlay.kind === "keyword" && overlay.backdrop === "highlight") {
446
+ const layout = getSideTextLayout(overlay, measure);
447
+ return getKeywordMarkBlock(overlay, layout, layout.lines, measure);
448
+ }
449
+ if (measure !== undefined && (overlay.kind === "keyword" || overlay.kind === "lower_third")) return getSideTextLayout(overlay, measure).card;
450
+ if (measure !== undefined && overlay.kind === "hook_title" && overlay.backdrop === "highlight") return getMarkedTitleBounds(overlay, measure) ?? overlay.box;
451
+ // The counter's widest number: its final value, grouped as en-US groups it (as `measureCounter` sizes its box).
452
+ if (measure !== undefined && overlay.kind === "counter") return getCounterCard(overlay, formatOverlayNumber(overlay.value, overlay.decimals, "en-US"), measure);
437
453
  if (overlay.kind !== "keyword" && overlay.kind !== "lower_third" && overlay.kind !== "list") return getOverlayBounds(overlay);
438
454
  if (overlay.kind === "list" && overlay.chips !== undefined) return getBoxesUnion(overlay.chips);
439
455
  return getSideTextCard(overlay, getSideTextLines(overlay), overlay.box.height);
@@ -450,6 +466,123 @@ function getSideTextCard(overlay: SideTextOverlay, lines: readonly string[], hei
450
466
  return { left, top: box.top, width, height };
451
467
  }
452
468
 
469
+ /**
470
+ * A line's width in CSS px in the brand font of `role` at `cssPx`, its letters spaced `spacingEm` (the render's
471
+ * `letter-spacing`): the render's own measure, read from the font files (`createOverlayTextMeasure`).
472
+ */
473
+ export type OverlayTextMeasure = (role: OverlayFontRole, text: string, cssPx: number, spacingEm: number) => number;
474
+
475
+ /** A lower third's accent dot and the gap after it, in ems of its font size (the render's `.ov-dot` and card gap). */
476
+ export const OVERLAY_DOT_EM = { size: 0.36, gap: 0.45 } as const;
477
+ /** A keyword card's accent edge under its text, in ems (the render's `border-bottom`). */
478
+ export const KEYWORD_EDGE_EM = 0.12;
479
+ /** The gap between a counter's number and its unit, in ems of the number's size. */
480
+ export const COUNTER_UNIT_GAP_EM = 0.3;
481
+ /** A highlight mark's padding round its text and its corner, in ems (the render's `.ov-mark`). */
482
+ export const MARK_PADDING_EM = { x: 0.22, y: 0.04 } as const;
483
+ export const MARK_RADIUS_EM = 0.14;
484
+
485
+ /** A keyword's or a lower third's card as the render lays it out: its text's lines, the card, where the text starts and its dot. */
486
+ export interface SideTextLayout {
487
+ /** The text broken as the render's card breaks it, one entry a line. */
488
+ lines: string[];
489
+ card: OverlayBox;
490
+ /** Where the first letter of every line starts (a centred keyword: the card's middle). */
491
+ textX: number;
492
+ /** The top of the first line (inside a highlight mark's padding). */
493
+ textTop: number;
494
+ /** A lower third's accent dot; null for a keyword. */
495
+ dot: { centre: OverlayPoint; radius: number } | null;
496
+ }
497
+
498
+ /** `words` broken greedily into lines no wider than `width` by `measureLine`, as a browser breaks a line (a word wider than a line stands alone). */
499
+ function breakLines(words: readonly string[], width: number, measureLine: (line: string) => number): string[] {
500
+ const lines: string[] = [];
501
+ for (const word of words) {
502
+ const last = lines[lines.length - 1];
503
+ if (last !== undefined && measureLine(`${last} ${word}`) <= width) lines[lines.length - 1] = `${last} ${word}`;
504
+ else lines.push(word);
505
+ }
506
+ return lines;
507
+ }
508
+
509
+ /**
510
+ * A keyword's or a lower third's card as the render lays it out (`.ov-card` in its box): its text broken inside the
511
+ * box less the padding (and a highlight mark's padding, a lower third's dot), the card as wide as its widest line (the
512
+ * whole box once it wraps), as tall as its lines plus padding and a keyword's accent edge; a keyword's card hangs from
513
+ * the box's top, a lower third's stands on its foot. `measure` reads the brand font, as the browser does.
514
+ */
515
+ export function getSideTextLayout(overlay: Extract<ResolvedOverlay, { kind: "keyword" | "lower_third" }>, measure: OverlayTextMeasure): SideTextLayout {
516
+ const { box, fontSize, side } = overlay;
517
+ const pad = getOverlayPadding(overlay);
518
+ const role = OVERLAY_FONT_ROLES[overlay.kind];
519
+ const isMarked = overlay.kind === "keyword" && overlay.backdrop === "highlight";
520
+ const markPad = isMarked ? fontSize * MARK_PADDING_EM.x : 0;
521
+ const dotRoom = overlay.kind === "lower_third" ? fontSize * (OVERLAY_DOT_EM.size + OVERLAY_DOT_EM.gap) : 0;
522
+ const measureLine = (line: string): number => measure(role, line, fontSize, OVERLAY_LETTER_SPACING_EM.card);
523
+ const words = overlay.text.replace(/\s+/g, " ").trim().split(" ");
524
+ const lines = breakLines(words, box.width - 2 * pad.x - dotRoom - 2 * markPad, measureLine);
525
+ const textWidth = Math.max(0, ...lines.map(measureLine)) + 2 * markPad + dotRoom;
526
+ const width = lines.length > 1 ? box.width : Math.min(box.width, Math.ceil(textWidth + 2 * pad.x));
527
+ // A highlighted keyword's card draws no border; an underlined one keeps its edge's room, transparent.
528
+ const edge = overlay.kind === "keyword" && !isMarked ? Math.round(fontSize * KEYWORD_EDGE_EM) : 0;
529
+ // A highlighted keyword's text is one block (a flex item) on its mark, padded on every side.
530
+ const markPadY = isMarked ? fontSize * MARK_PADDING_EM.y : 0;
531
+ const height = Math.ceil(lines.length * fontSize * OVERLAY_LINE_HEIGHT + 2 * markPadY + 2 * pad.y + edge);
532
+ const isCentred = overlay.kind === "keyword" && overlay.isCentred === true;
533
+ const left = isCentred ? box.left + Math.round((box.width - width) / 2) : side === "right" ? box.left + box.width - width : box.left;
534
+ const top = overlay.kind === "lower_third" ? box.top + box.height - height : box.top;
535
+ const card = { left, top, width, height };
536
+ const textTop = top + pad.y + markPadY;
537
+ const dot =
538
+ overlay.kind === "lower_third" ? { centre: { x: left + pad.x + (fontSize * OVERLAY_DOT_EM.size) / 2, y: top + height / 2 }, radius: (fontSize * OVERLAY_DOT_EM.size) / 2 } : null;
539
+ return { lines, card, textX: isCentred ? left + width / 2 : left + pad.x + dotRoom + markPad, textTop, dot };
540
+ }
541
+
542
+ /**
543
+ * A highlighted keyword's mark as the render draws it (`.ov-mark` is a flex item of the card, so one block): round
544
+ * `lines` (the whole text's, or the letters typed so far on theirs), as wide as the widest line, or all the card's
545
+ * room once the text wraps, as tall as the lines, padded by `MARK_PADDING_EM`.
546
+ */
547
+ export function getKeywordMarkBlock(overlay: Extract<ResolvedOverlay, { kind: "keyword" }>, layout: SideTextLayout, lines: readonly string[], measure: OverlayTextMeasure): OverlayBox {
548
+ const { fontSize } = overlay;
549
+ const pad = { x: fontSize * MARK_PADDING_EM.x, y: fontSize * MARK_PADDING_EM.y };
550
+ const measureLine = (line: string): number => measure(OVERLAY_FONT_ROLES.keyword, line, fontSize, OVERLAY_LETTER_SPACING_EM.card);
551
+ const width = lines.length > 1 ? layout.card.width - 2 * getOverlayPadding(overlay).x : Math.max(0, ...lines.map(measureLine)) + 2 * pad.x;
552
+ const height = lines.length * fontSize * OVERLAY_LINE_HEIGHT + 2 * pad.y;
553
+ const left = overlay.isCentred === true ? layout.textX - width / 2 : layout.textX - pad.x;
554
+ return { left, top: layout.textTop - pad.y, width, height };
555
+ }
556
+
557
+ /**
558
+ * Where a highlighted hook title shows (its card is transparent): round its lines' marks, as wide as the widest line
559
+ * and its marks' padding, as tall as its lines; null when it names a place (its row is not a mark).
560
+ */
561
+ function getMarkedTitleBounds(overlay: Extract<ResolvedOverlay, { kind: "hook_title" }>, measure: OverlayTextMeasure): OverlayBox | null {
562
+ if (overlay.place !== null) return null;
563
+ const { fontSize } = overlay;
564
+ const content = getTitleContentBox(overlay);
565
+ const padX = fontSize * MARK_PADDING_EM.x;
566
+ const width = Math.max(0, ...overlay.lines.map((line) => measure("heading", line, fontSize, OVERLAY_LETTER_SPACING_EM.hookTitle))) + 2 * padX;
567
+ const left = overlay.align === "left" ? content.left : content.left + content.width / 2 - width / 2;
568
+ return { left, top: content.top, width, height: content.height };
569
+ }
570
+
571
+ /**
572
+ * A counter's card as the render lays it out while it shows `text` (its number, as formatted): as wide as the number,
573
+ * its unit and the gap between, plus padding; one line tall; in the middle of its box, from its top.
574
+ */
575
+ export function getCounterCard(overlay: Extract<ResolvedOverlay, { kind: "counter" }>, text: string, measure: OverlayTextMeasure): OverlayBox {
576
+ const { box, fontSize, unit, unitFontSize } = overlay;
577
+ const pad = getOverlayPadding(overlay);
578
+ const role = OVERLAY_FONT_ROLES.counter;
579
+ // The card's letter spacing is set in ems of the card's size and inherited as px by the unit.
580
+ const unitWidth = unit === null ? 0 : fontSize * COUNTER_UNIT_GAP_EM + measure(role, unit, unitFontSize, (OVERLAY_LETTER_SPACING_EM.card * fontSize) / unitFontSize);
581
+ const width = Math.ceil(measure(role, text, fontSize, OVERLAY_LETTER_SPACING_EM.card) + unitWidth + 2 * pad.x);
582
+ const height = Math.ceil(fontSize * OVERLAY_LINE_HEIGHT + 2 * pad.y);
583
+ return { left: box.left + Math.round((box.width - width) / 2), top: box.top, width, height };
584
+ }
585
+
453
586
  /** The smallest box round every box of a non-empty list. */
454
587
  function getBoxesUnion(boxes: readonly OverlayBox[]): OverlayBox {
455
588
  const left = Math.min(...boxes.map((box) => box.left));
package/src/index.ts CHANGED
@@ -157,11 +157,23 @@ export {
157
157
  type StylePadding,
158
158
  type StyleShadow,
159
159
  } from "./footage/brand-style.js";
160
- export { createDraftTextMeasure, wrapDraftLine, type DraftTextMeasure } from "./footage/draft-text-width.js";
160
+ export {
161
+ DEFAULT_CONTENT_AREA,
162
+ createDraftFont,
163
+ createDraftTextMeasure,
164
+ getSpacingWidth,
165
+ toDraftAssSize,
166
+ toDraftSpacing,
167
+ withLetterSpacing,
168
+ wrapDraftLine,
169
+ type DraftFont,
170
+ type DraftTextMeasure,
171
+ } from "./footage/draft-text-width.js";
161
172
  export { HAND_FONT } from "./footage/hand-font.js";
162
173
  export {
163
174
  OVERLAY_FONT_ROLES,
164
175
  OVERLAY_FONT_SIZES,
176
+ OVERLAY_LETTER_SPACING_EM,
165
177
  OVERLAY_SUBTITLE_SCALES,
166
178
  MIN_OVERLAY_SUBTITLE_PX,
167
179
  OVERLAY_TITLE_SHRINK,
@@ -179,7 +191,17 @@ export {
179
191
  getChipCoveredTimes,
180
192
  getChipParts,
181
193
  getChipTickPoints,
194
+ getCounterCard,
182
195
  getDrawnOverlayCard,
196
+ getKeywordMarkBlock,
197
+ getSideTextLayout,
198
+ COUNTER_UNIT_GAP_EM,
199
+ KEYWORD_EDGE_EM,
200
+ MARK_PADDING_EM,
201
+ MARK_RADIUS_EM,
202
+ OVERLAY_DOT_EM,
203
+ type OverlayTextMeasure,
204
+ type SideTextLayout,
183
205
  getListCardsByItem,
184
206
  getListItemTimes,
185
207
  getListStops,
@@ -278,7 +300,10 @@ export { describeVoiceoverBatch, produceVoiceovers, type VoiceoverBatchItem, typ
278
300
  export { getPlaceholderKey, isPlaceholderKey, makePlaceholderWords, writePlaceholderVoiceover, type PlaceholderPace, type WritePlaceholderOptions } from "./voice/placeholder.js";
279
301
  export {
280
302
  BROWSER_BAR_HEIGHT,
303
+ CAPTION_LETTER_SPACING_EM,
281
304
  CAPTION_LINE_HEIGHT,
305
+ CAPTION_RIBBON_LETTER_SPACING_EM,
306
+ toCssEm,
282
307
  CAPTION_PILL_PADDING,
283
308
  DEVICE_KINDS,
284
309
  LAYOUTS,
@@ -687,6 +712,9 @@ export {
687
712
  FOOTAGE_DRAFT_OVERLAY_COLORS,
688
713
  FOOTAGE_DRAFT_FONT,
689
714
  FOOTAGE_DRAFT_WIDTH,
715
+ loadFootageDraftFonts,
716
+ createOverlayTextMeasure,
717
+ type FootageDraftFonts,
690
718
  buildFootageDraftArgs,
691
719
  buildFootageDraftCaptions,
692
720
  buildFootageDraftJoinArgs,