@softure-ai/marketing-kit 0.40.0 → 0.42.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 (47) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +9 -1
  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 +10 -0
  11. package/dist/footage/draft-overlays.d.ts.map +1 -1
  12. package/dist/footage/draft-overlays.js +45 -15
  13. package/dist/footage/draft-overlays.js.map +1 -1
  14. package/dist/footage/draft-text-width.d.ts +30 -0
  15. package/dist/footage/draft-text-width.d.ts.map +1 -1
  16. package/dist/footage/draft-text-width.js +51 -1
  17. package/dist/footage/draft-text-width.js.map +1 -1
  18. package/dist/footage/draft.d.ts +22 -1
  19. package/dist/footage/draft.d.ts.map +1 -1
  20. package/dist/footage/draft.js +56 -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/overlay-schema.d.ts +6 -1
  26. package/dist/footage/overlay-schema.d.ts.map +1 -1
  27. package/dist/footage/overlay-schema.js +8 -2
  28. package/dist/footage/overlay-schema.js.map +1 -1
  29. package/dist/footage/overlays.d.ts +10 -0
  30. package/dist/footage/overlays.d.ts.map +1 -1
  31. package/dist/footage/overlays.js +12 -1
  32. package/dist/footage/overlays.js.map +1 -1
  33. package/dist/index.d.ts +5 -5
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +5 -5
  36. package/dist/index.js.map +1 -1
  37. package/package.json +1 -1
  38. package/schema/edl.schema.json +1 -1
  39. package/src/compose/timeline.ts +12 -1
  40. package/src/footage/compose.ts +3 -3
  41. package/src/footage/draft-overlays.ts +61 -15
  42. package/src/footage/draft-text-width.ts +69 -1
  43. package/src/footage/draft.ts +81 -31
  44. package/src/footage/overlay-html.ts +6 -4
  45. package/src/footage/overlay-schema.ts +13 -3
  46. package/src/footage/overlays.ts +12 -0
  47. package/src/index.ts +18 -1
@@ -62,6 +62,17 @@ export interface CaptionLayout extends TextBox {
62
62
  /** The caption text's line height, in font sizes (the pill and the phrase alike). */
63
63
  export const CAPTION_LINE_HEIGHT = 1.18;
64
64
 
65
+ /** The caption text's letter spacing, in em (the pill and the phrase alike); a ribbon's capitals are spaced by `CAPTION_RIBBON_LETTER_SPACING_EM`. */
66
+ export const CAPTION_LETTER_SPACING_EM = -0.01;
67
+
68
+ /** A ribbon caption's letter spacing, in em. */
69
+ export const CAPTION_RIBBON_LETTER_SPACING_EM = 0.01;
70
+
71
+ /** A letter spacing in em as the render's CSS writes it (`-0.015` → `-.015em`). */
72
+ export function toCssEm(em: number): string {
73
+ return `${String(em).replace(/^(-?)0\./, "$1.")}em`;
74
+ }
75
+
65
76
  /** The highlight pill's padding around its text, in px. */
66
77
  export const CAPTION_PILL_PADDING = { y: 16, x: 28 } as const;
67
78
 
@@ -72,7 +83,7 @@ export function getCaptionPositionCss(caption: CaptionLayout): string {
72
83
 
73
84
  /** The highlight pill's text CSS: weight, size, line height and padding (the colours are the caller's). */
74
85
  export function getCaptionPillCss(fontSize: number): string {
75
- return `font-weight:650;font-size:${fontSize}px;line-height:${CAPTION_LINE_HEIGHT};letter-spacing:-.01em;text-align:center;padding:${CAPTION_PILL_PADDING.y}px ${CAPTION_PILL_PADDING.x}px`;
86
+ return `font-weight:650;font-size:${fontSize}px;line-height:${CAPTION_LINE_HEIGHT};letter-spacing:${toCssEm(CAPTION_LETTER_SPACING_EM)};text-align:center;padding:${CAPTION_PILL_PADDING.y}px ${CAPTION_PILL_PADDING.x}px`;
76
87
  }
77
88
 
78
89
  export interface EndCardLayout extends TextBox {
@@ -2,7 +2,7 @@ import type { BrandColors } from "../config/colors.js";
2
2
  import type { SfxEvent } from "../config/schema.js";
3
3
  import type { ComposeFont } from "../compose/compose.js";
4
4
  import { assertLocalReferences, escapeHtml, fontFaces, fontStack } from "../compose/html.js";
5
- import { CAPTION_LINE_HEIGHT, getCaptionPillCss, getCaptionPositionCss, type CaptionLayout } from "../compose/timeline.js";
5
+ import { CAPTION_LETTER_SPACING_EM, CAPTION_LINE_HEIGHT, CAPTION_RIBBON_LETTER_SPACING_EM, getCaptionPillCss, getCaptionPositionCss, toCssEm, type CaptionLayout } from "../compose/timeline.js";
6
6
  import type { TimedWord } from "../voice/voiceover.js";
7
7
  import { CAPTION_HIGHLIGHT_BOX_PADDING_EM, CAPTION_HIGHLIGHT_BOX_RADIUS, CAPTION_HIGHLIGHT_SECONDS, CAPTION_UNDERLINE_EM, getCaptionEntranceTween, getCaptionHighlight } from "./caption-highlight.js";
8
8
  import { CAPTION_SAFE_ZONES, getCaptionChunkLayouts, getCaptionRibbonTilt, getCaptionSpans, getFootageCaptionChunks, isSameCaptionPlace, toCaptionDisplayText, type CaptionSpan } from "./captions.js";
@@ -346,7 +346,7 @@ ${isBox ? `.pill{position:relative;z-index:0}
346
346
  .hl{position:absolute;top:0;bottom:0;left:-${CAPTION_HIGHLIGHT_BOX_PADDING_EM}em;right:-${CAPTION_HIGHLIGHT_BOX_PADDING_EM}em;z-index:-1;border-radius:${CAPTION_HIGHLIGHT_BOX_RADIUS}px;background:${look.box};opacity:0}
347
347
  ` : ""}${isUnderline ? `.pill span{position:relative}
348
348
  .ul{position:absolute;left:0;right:0;top:${CAPTION_UNDERLINE_EM.top}em;height:${CAPTION_UNDERLINE_EM.thickness}em;border-radius:${CAPTION_UNDERLINE_EM.thickness / 2}em;background:${look.color};opacity:0}
349
- ` : ""}${isRibbon ? `${getRibbonCaptionCss(captionBox.fontSize, getCaptionRibbonTilt(film), { background: c.captionBackground, text: c.captionText })}\n` : ""}.phrase{font-weight:650;font-size:${captionBox.fontSize}px;line-height:${CAPTION_LINE_HEIGHT};letter-spacing:-.01em;text-align:center;color:#fff;text-shadow:0 2px 6px rgba(0,0,0,.55),0 0 18px rgba(0,0,0,.35)}
349
+ ` : ""}${isRibbon ? `${getRibbonCaptionCss(captionBox.fontSize, getCaptionRibbonTilt(film), { background: c.captionBackground, text: c.captionText })}\n` : ""}.phrase{font-weight:650;font-size:${captionBox.fontSize}px;line-height:${CAPTION_LINE_HEIGHT};letter-spacing:${toCssEm(CAPTION_LETTER_SPACING_EM)};text-align:center;color:#fff;text-shadow:0 2px 6px rgba(0,0,0,.55),0 0 18px rgba(0,0,0,.35)}
350
350
  .endcard{position:absolute;inset:0;display:flex;flex-direction:column;justify-content:center;align-items:center;padding:0 80px;text-align:center}
351
351
  .brand{display:flex;justify-content:center;align-items:center;gap:18px;font-family:${headingFont};font-size:46px}
352
352
  .logo{height:60px;width:auto}
@@ -410,7 +410,7 @@ export function getRibbonCaptionCss(fontSize: number, tilt: number, colors: { ba
410
410
  const { lineHeight, padding, radius, weight } = CAPTION_RIBBON;
411
411
  return [
412
412
  `.caption.turned{transform:rotate(${String(-tilt)}deg);transform-origin:50% 50%}`,
413
- `.ribbon{font-weight:${String(weight)};font-size:${String(fontSize)}px;line-height:${String(lineHeight)};letter-spacing:.01em;text-align:center;color:${colors.text}}`,
413
+ `.ribbon{font-weight:${String(weight)};font-size:${String(fontSize)}px;line-height:${String(lineHeight)};letter-spacing:${toCssEm(CAPTION_RIBBON_LETTER_SPACING_EM)};text-align:center;color:${colors.text}}`,
414
414
  `.rb{background:${colors.background};padding:${String(padding.y)}em ${String(padding.x)}em;border-radius:${String(radius)}px;-webkit-box-decoration-break:clone;box-decoration-break:clone}`,
415
415
  ].join("\n");
416
416
  }
@@ -1,6 +1,7 @@
1
1
  import { getCornerRadiusPx, type BrandStyle, type CornerRadius } from "./brand-style.js";
2
2
  import { getDraftBoxEvents } from "./draft-boxes.js";
3
3
  import { toAssColor } from "./draft-color.js";
4
+ import { getSpacingWidth, toDraftAssSize, toDraftSpacing, wrapDraftLine, type DraftFont } from "./draft-text-width.js";
4
5
  import { HAND_FONT, getHandFontAssSize } from "./hand-font.js";
5
6
  import { getCounterSeconds, getOverlayColors, getPlaceCardTimes, getTypingTimes, type OverlayColors } from "./overlay-html.js";
6
7
  import {
@@ -26,6 +27,7 @@ import {
26
27
  OVERLAY_FONT_ROLES,
27
28
  OVERLAY_LINE_HEIGHT,
28
29
  OVERLAY_LIST_LINE_HEIGHT,
30
+ OVERLAY_LETTER_SPACING_EM,
29
31
  OVERLAY_TITLE_LINE_HEIGHT,
30
32
  SEARCH_BAR_ICON_EM,
31
33
  SEARCH_BAR_PADDING_EM,
@@ -166,12 +168,24 @@ export function getGlassDrawing(at: OverlayPoint, size: number): string {
166
168
  return `${ring} ${handle}`;
167
169
  }
168
170
 
171
+ /** The font an overlay's text is drawn in: the body font (the styles' own) or an overlay role's. */
172
+ export type DraftFontRole = "body" | OverlayFontRole;
173
+
174
+ /** A character that stands for a line break in an overlay's text until it is written as ASS (`\\N`): one character, as the space it replaces. */
175
+ const LINE_BREAK = "\uE000";
176
+
169
177
  interface DraftContext {
170
178
  brand: OverlayColors;
171
179
  scale: number;
172
180
  locale: string;
173
181
  /** The font family per role, when it is not the styles' own. */
174
182
  fonts: Partial<Record<OverlayFontRole, string>>;
183
+ /** The ASS size of a CSS px size in the font of `role` (`body`: the styles' own font, which a role without its own family draws in). */
184
+ toAssSize: (role: DraftFontRole, cssPx: number) => number;
185
+ /** A line's width in CSS px in the font of `role`, its letters spaced `spacingEm` (the render's `letter-spacing`). */
186
+ measure: (role: DraftFontRole, text: string, cssPx: number, spacingEm: number) => number;
187
+ /** The `\\fsp` tag that spaces letters `spacingEm` at a CSS size as the render does; empty without the draft's fonts (as before). */
188
+ spacing: (spacingEm: number, cssPx: number) => string;
175
189
  /** The brand's style: the cards' radius, shadow and border; absent: each kind's own corner, no shadow, no border. */
176
190
  style?: BrandStyle;
177
191
  }
@@ -491,23 +505,29 @@ function getSideTextEvents(overlay: Extract<ResolvedOverlay, { kind: "keyword" |
491
505
  const card = getDrawnOverlayCard(overlay) as OverlayBox;
492
506
  const { x: padX, y: padY } = getOverlayPadding(overlay);
493
507
  const isCentred = overlay.kind === "keyword" && overlay.isCentred === true;
494
- const isRight = !isCentred && side === "right";
495
- const x = isCentred ? card.left + card.width / 2 : isRight ? card.left + card.width - padX : card.left + padX;
508
+ const role = OVERLAY_FONT_ROLES[overlay.kind];
509
+ // The render's card wraps its text inside the box (`max-width: 100%`): the same breaks, by the font's own widths.
510
+ const wrapped = getWrappedText(overlay.text, overlay.box.width - 2 * padX, fontSize, (line, size) => context.measure(role, line, size, OVERLAY_LETTER_SPACING_EM.card));
511
+ // A wrapped card fills its box and its lines start at its left on either side, as the render's do (`text-align` start).
512
+ const isWrapped = wrapped.includes(LINE_BREAK);
513
+ const isRight = !isCentred && side === "right" && !isWrapped;
514
+ const x = isCentred ? card.left + card.width / 2 : isRight ? card.left + card.width - padX : (isWrapped ? overlay.box.left : card.left) + padX;
496
515
  const isFoot = overlay.kind === "lower_third";
497
516
  const y = isFoot ? card.top + card.height - padY : card.top + padY;
498
517
  // \an: 7 top left, 8 top centre, 9 top right, 1 bottom left, 3 bottom right.
499
518
  const align = isFoot ? (isRight ? 3 : 1) : isCentred ? 8 : isRight ? 9 : 7;
500
519
  const motion = getDraftMotion(overlay, overlay.entrance === null || overlay.entrance === "type" ? "fade" : overlay.entrance, card, context);
501
520
  const span = { start: overlay.start, end: overlay.end };
502
- const tags = `\\an${align}\\fs${fontSize}${getTextTags(overlay, colors, context, OVERLAY_FONT_ROLES[overlay.kind])}`;
503
- const text = (pos: OverlayPoint, steps: TextStep[], own = motion) => getTextEvents({ layer: 4, style: "OverlayPlain", pos, tags, steps, motion: own });
521
+ const tags = `\\an${align}\\fs${context.toAssSize(role, fontSize)}${context.spacing(OVERLAY_LETTER_SPACING_EM.card, fontSize)}${getTextTags(overlay, colors, context, role)}`;
522
+ const text = (pos: OverlayPoint, steps: TextStep[], own = motion) =>
523
+ getTextEvents({ layer: 4, style: "OverlayPlain", pos, tags, steps: steps.map((step) => ({ ...step, text: step.text.replaceAll(LINE_BREAK, "\\N") })), motion: own });
504
524
  const isMarked = overlay.kind === "keyword" && overlay.backdrop === "highlight";
505
525
  const shape = isMarked
506
526
  ? getBackdropMarkEvents({ text: overlay.text, x, align: isCentred ? "center" : isRight ? "right" : "left", top: y, fontSize, colour: colors.background, motion, span, typing: overlay.typing })
507
527
  : getCardShapeEvents(overlay, card, fontSize, colors, motion, context);
508
- if (overlay.entrance === "drop") return [...shape, ...text({ x, y }, [{ from: span.start, to: span.end, text: getDropText(overlay.text, getDropLetterTimes(span.start, overlay.text), span.start) }], getPlainMotion(motion))];
528
+ if (overlay.entrance === "drop") return [...shape, ...text({ x, y }, [{ from: span.start, to: span.end, text: getDropText(wrapped, getDropLetterTimes(span.start, overlay.text), span.start) }], getPlainMotion(motion))];
509
529
  // A keyword or a lower third types in only when its entrance says so.
510
- const steps = overlay.entrance === null ? [{ from: span.start, to: span.end, text: toAssText(overlay.text) }] : getTypedSteps(overlay.text, span, overlay.typing, isCentred);
530
+ const steps = overlay.entrance === null ? [{ from: span.start, to: span.end, text: toAssText(wrapped) }] : getTypedSteps(wrapped, span, overlay.typing, isCentred);
511
531
  return [...shape, ...text({ x, y }, steps), ...(overlay.kind === "keyword" ? getUnderlineEvent(overlay, card, colors.accent, motion) : [])];
512
532
  }
513
533
 
@@ -566,7 +586,7 @@ function getListEvents(overlay: Extract<ResolvedOverlay, { kind: "list" }>, cont
566
586
  const textTags = getTextTags(overlay, colors, context, OVERLAY_FONT_ROLES.list);
567
587
  const lines = getSideTextLines(overlay);
568
588
  const write = (pos: OverlayPoint, align: number, steps: TextStep[], motion: DraftMotion, clip = "") =>
569
- getTextEvents({ layer: 4, style: "OverlayPlain", pos, tags: `\\an${align}\\fs${fontSize}${textTags}${clip}`, steps, motion });
589
+ getTextEvents({ layer: 4, style: "OverlayPlain", pos, tags: `\\an${align}\\fs${context.toAssSize(OVERLAY_FONT_ROLES.list, fontSize)}${context.spacing(OVERLAY_LETTER_SPACING_EM.card, fontSize)}${textTags}${clip}`, steps, motion });
570
590
  if (overlay.chips !== undefined) {
571
591
  const covered = getChipCoveredTimes(overlay);
572
592
  return overlay.chips.flatMap((_chip, index) => {
@@ -587,7 +607,7 @@ function getListEvents(overlay: Extract<ResolvedOverlay, { kind: "list" }>, cont
587
607
  return [
588
608
  ...getCardShapeEvents(overlay, place.box, fontSize, colors, motion, context, span),
589
609
  ...write({ x: parts.textX, y: place.box.top + pad.y }, 8, steps, motion),
590
- ...getChipBadgeEvents({ index, span, until, parts, colors, motion }),
610
+ ...getChipBadgeEvents({ index, span, until, parts, colors, motion, toAssSize: (cssPx) => context.toAssSize("body", cssPx) }),
591
611
  ];
592
612
  });
593
613
  });
@@ -633,6 +653,8 @@ function getChipBadgeEvents(input: {
633
653
  parts: ReturnType<typeof getChipParts>;
634
654
  colors: OverlayColors;
635
655
  motion: DraftMotion;
656
+ /** The ASS size of the number's CSS size (it is drawn in the styles' own font). */
657
+ toAssSize: (cssPx: number) => number;
636
658
  }): string[] {
637
659
  const { span, until, parts, colors, motion } = input;
638
660
  const { centre, radius } = parts.badge;
@@ -645,7 +667,7 @@ function getChipBadgeEvents(input: {
645
667
  layer: 6,
646
668
  style: "OverlayPlain",
647
669
  pos: centre,
648
- tags: `\\an5\\fs${numberSize}\\b1\\1c${toTagColor(colors.background)}`,
670
+ tags: `\\an5\\fs${input.toAssSize(numberSize)}\\b1\\1c${toTagColor(colors.background)}`,
649
671
  steps: [{ from: span.start, to: numberEnd, text: String(input.index + 1) }],
650
672
  motion: numberEnd === span.end ? motion : { ...motion, exit: { ...motion.exit, kind: "none" } },
651
673
  })
@@ -744,8 +766,11 @@ function getCardEvents(overlay: CardOverlay, context: DraftContext): string[] {
744
766
  const { x: padX, y: padY } = getOverlayPadding(overlay);
745
767
  const centerX = box.left + box.width / 2;
746
768
  const tagsFor = (role: OverlayFontRole) => getTextTags(overlay, colors, context, role);
769
+ // The render spaces a title's heading lines tighter than the rest of a card's text, and a search query not at all.
770
+ const spacingFor = (role: OverlayFontRole): number =>
771
+ overlay.kind === "search_bar" ? 0 : role !== "heading" ? OVERLAY_LETTER_SPACING_EM.card : overlay.kind === "hook_title" ? OVERLAY_LETTER_SPACING_EM.hookTitle : overlay.kind === "place_card" ? OVERLAY_LETTER_SPACING_EM.placeTitle : OVERLAY_LETTER_SPACING_EM.card;
747
772
  const text = (pos: OverlayPoint, align: number, size: number, role: OverlayFontRole, steps: TextStep[], own: DraftMotion = motion) =>
748
- getTextEvents({ layer: 4, style: "OverlayPlain", pos, tags: `\\an${align}\\fs${r0(size)}${tagsFor(role)}`, steps, motion: own });
773
+ getTextEvents({ layer: 4, style: "OverlayPlain", pos, tags: `\\an${align}\\fs${context.toAssSize(role, r0(size))}${context.spacing(spacingFor(role), r0(size))}${tagsFor(role)}`, steps, motion: own });
749
774
  const plain = { ...motion, name: "none" as const, entrance: { kind: "none" as const } };
750
775
  if (overlay.kind === "hook_title" || overlay.kind === "place_card") {
751
776
  // The text block sits in the card's middle; centred lines on its axis, left ones from its left edge (RV-131).
@@ -820,7 +845,7 @@ function getCardEvents(overlay: CardOverlay, context: DraftContext): string[] {
820
845
  return events;
821
846
  }
822
847
  if (overlay.kind === "counter") {
823
- const unit = overlay.unit === null ? "" : `{\\fs${overlay.unitFontSize}} ${toAssText(overlay.unit)}`;
848
+ const unit = overlay.unit === null ? "" : `{\\fs${context.toAssSize("heading", overlay.unitFontSize)}} ${toAssText(overlay.unit)}`;
824
849
  const count = getCounterSeconds(overlay);
825
850
  const steps: TextStep[] = [];
826
851
  const ticks = Math.max(1, Math.round(count.duration / COUNT_STEP));
@@ -927,8 +952,8 @@ function getNoteEvents(overlay: Extract<ResolvedOverlay, { kind: "note" }>, cont
927
952
  const font = isStrip ? `${context.fonts.heading === undefined ? "" : `\\fn${context.fonts.heading}`}\\b1` : `\\fn${HAND_FONT.family}\\b0`;
928
953
  const textMotion = getPlainMotion(motion);
929
954
  // libass sizes the hand font by its ascent and descent: its \\fs is scaled so the letters match the render's.
930
- const assSize = isStrip ? overlay.fontSize : getHandFontAssSize(overlay.fontSize);
931
- const tags = `\\an7\\fs${assSize}${font}\\1c${toTagColor(colors.ink)}${turn}`;
955
+ const assSize = isStrip ? context.toAssSize("heading", overlay.fontSize) : getHandFontAssSize(overlay.fontSize);
956
+ const tags = `\\an7\\fs${assSize}${isStrip ? context.spacing(OVERLAY_LETTER_SPACING_EM.strip, overlay.fontSize) : ""}${font}\\1c${toTagColor(colors.ink)}${turn}`;
932
957
  getNoteWordsByLine(overlay).forEach((words, index) => {
933
958
  const pos = { x: textBox.left, y: textBox.top + index * lineHeight };
934
959
  const line = overlay.lines[index] as string;
@@ -972,6 +997,17 @@ function getEndWordEvents(overlay: Extract<ResolvedOverlay, { kind: "end_word" }
972
997
  return getTextEvents({ layer: 4, style: "OverlayPlain", pos, tags, steps: [{ from: span.start, to: span.end, text: toAssText(overlay.text) }], motion });
973
998
  }
974
999
 
1000
+ /**
1001
+ * `text` with the spaces it breaks at turned into `LINE_BREAK`, greedily as a browser breaks a line that does not fit
1002
+ * `width` (a word wider than a line stands alone); the text as it is when it fits on one line.
1003
+ */
1004
+ function getWrappedText(text: string, width: number, fontSize: number, measure: (line: string, size: number) => number): string {
1005
+ const words = text.replace(/\s+/g, " ").trim().split(" ");
1006
+ return wrapDraftLine(words, width, fontSize, measure)
1007
+ .map((line) => line.join(" "))
1008
+ .join(LINE_BREAK);
1009
+ }
1010
+
975
1011
  /**
976
1012
  * The overlays as ASS events, layers 3 and 4. `fonts` names the heading and interface families where they are not the
977
1013
  * styles' own (the captions' font); `locale` groups a counter's number as the render does.
@@ -980,9 +1016,19 @@ export function buildDraftOverlayEvents(
980
1016
  overlays: readonly ResolvedOverlay[],
981
1017
  colors: OverlayColors,
982
1018
  frame: { width: number; height: number },
983
- options: { fonts?: Partial<Record<OverlayFontRole, string>>; locale?: string; style?: BrandStyle } = {},
1019
+ options: { fonts?: Partial<Record<OverlayFontRole, string>>; locale?: string; style?: BrandStyle; drawnFonts?: Record<DraftFontRole, DraftFont> } = {},
984
1020
  ): string[] {
985
- const context: DraftContext = { brand: colors, scale: frame.width / 1080, locale: options.locale ?? "en-US", fonts: options.fonts ?? {}, ...(options.style === undefined ? {} : { style: options.style }) };
1021
+ const drawn = options.drawnFonts;
1022
+ const context: DraftContext = {
1023
+ brand: colors,
1024
+ scale: frame.width / 1080,
1025
+ locale: options.locale ?? "en-US",
1026
+ fonts: options.fonts ?? {},
1027
+ toAssSize: (role, cssPx) => (drawn === undefined ? cssPx : toDraftAssSize(cssPx, drawn[role].assScale)),
1028
+ measure: (role, text, cssPx, spacingEm) => (drawn === undefined ? estimateTextWidth(text, cssPx) : drawn[role].measure(text, cssPx) + getSpacingWidth(text, spacingEm, cssPx)),
1029
+ spacing: (spacingEm, cssPx) => (drawn === undefined || spacingEm === 0 ? "" : `\\fsp${String(toDraftSpacing(spacingEm, cssPx))}`),
1030
+ ...(options.style === undefined ? {} : { style: options.style }),
1031
+ };
986
1032
  return overlays.flatMap((overlay) => {
987
1033
  if (overlay.kind === "punch_in") return [];
988
1034
  if (overlay.kind === "arrow" || overlay.kind === "highlight") return getMarkEvents(overlay, context);
@@ -13,6 +13,8 @@ export type DraftTextMeasure = (text: string, fontSize: number) => number;
13
13
  interface Metrics {
14
14
  /** The font's units per libass font size: `usWinAscent + usWinDescent`. */
15
15
  unitsPerSize: number;
16
+ /** The font's units per em (`head`), what a browser sizes it by; 0 when the font has no `head`. */
17
+ unitsPerEm: number;
16
18
  advances: (glyph: number) => number;
17
19
  }
18
20
 
@@ -27,8 +29,10 @@ function readMetrics(font: Buffer): Metrics | null {
27
29
  const unitsPerSize = winSum > 0 ? winSum : readInt16(hhea, 4) - readInt16(hhea, 6);
28
30
  const count = readUint16(hhea, 34);
29
31
  if (unitsPerSize <= 0 || count === 0 || hmtx.length < count * 4) return null;
32
+ const head = tables.value.get("head");
33
+ const unitsPerEm = head === undefined ? 0 : readUint16(head, 18);
30
34
  // Glyphs past `numberOfHMetrics` share the last advance (OpenType `hmtx`).
31
- return { unitsPerSize, advances: (glyph) => readUint16(hmtx, Math.min(glyph, count - 1) * 4) };
35
+ return { unitsPerSize, unitsPerEm, advances: (glyph) => readUint16(hmtx, Math.min(glyph, count - 1) * 4) };
32
36
  }
33
37
 
34
38
  /**
@@ -52,6 +56,70 @@ export function createDraftTextMeasure(font: Buffer): DraftTextMeasure | null {
52
56
  }
53
57
  }
54
58
 
59
+ /**
60
+ * A font of a footage draft as the render sizes it. Every size in the draft is a CSS px size, as in the render; libass
61
+ * sizes a font by `usWinAscent + usWinDescent`, so an ASS size is written through `toAssSize`, and the text is as large
62
+ * as the browser draws it (an em of `cssPx`). `measure` gives a line's width at a CSS size, as the browser lays it.
63
+ */
64
+ export interface DraftFont {
65
+ /** The family libass knows the file by (its `name` table). */
66
+ family: string;
67
+ file: string;
68
+ /** The libass size that draws one CSS px: `(usWinAscent + usWinDescent) / unitsPerEm`. */
69
+ assScale: number;
70
+ /** A line's width in CSS px at a CSS font size (advances per em; kerning not read). */
71
+ measure: DraftTextMeasure;
72
+ }
73
+
74
+ /** The ASS size (`Fontsize`, `\fs`) that draws a font of `assScale` as large as the render's `cssPx`, to 0.01. */
75
+ export function toDraftAssSize(cssPx: number, assScale: number): number {
76
+ return Math.round(cssPx * assScale * 100) / 100;
77
+ }
78
+
79
+ /** The ASS letter spacing (`\\fsp`, `Spacing`, in the frame's px) of the render's `letter-spacing` of `spacingEm` at `cssPx`, to 0.01. */
80
+ export function toDraftSpacing(spacingEm: number, cssPx: number): number {
81
+ return Math.round(spacingEm * cssPx * 100) / 100;
82
+ }
83
+
84
+ /** What a letter spacing of `spacingEm` adds to a line at `cssPx`: once after every character, as a browser spaces them. */
85
+ export function getSpacingWidth(text: string, spacingEm: number, cssPx: number): number {
86
+ // `+ 0` turns an empty line's -0 into 0.
87
+ return [...text].length * spacingEm * cssPx + 0;
88
+ }
89
+
90
+ /** `measure` with the letters spaced `spacingEm`, as the render's `letter-spacing` spaces them. */
91
+ export function withLetterSpacing(measure: DraftTextMeasure, spacingEm: number): DraftTextMeasure {
92
+ return (text, fontSize) => Math.ceil(measure(text, fontSize) + getSpacingWidth(text, spacingEm, fontSize) - 1e-9);
93
+ }
94
+
95
+ /**
96
+ * The draft font of `data` (a TrueType or OpenType file) known to libass as `family`, or null when its tables cannot be
97
+ * read (no `hhea`, `hmtx`, `head` or `cmap`). A character the font lacks is measured as its `.notdef` glyph.
98
+ */
99
+ export function createDraftFont(source: { family: string; file: string }, data: Buffer): DraftFont | null {
100
+ try {
101
+ const metrics = readMetrics(data);
102
+ const characters = readCharacterMap(data);
103
+ if (metrics === null || metrics.unitsPerEm === 0 || !characters.ok) return null;
104
+ const map = characters.value;
105
+ const { unitsPerEm } = metrics;
106
+ return {
107
+ family: source.family,
108
+ file: source.file,
109
+ assScale: metrics.unitsPerSize / unitsPerEm,
110
+ measure: (text, fontSize) => {
111
+ let units = 0;
112
+ for (const character of text) units += metrics.advances(map.getGlyph(character.codePointAt(0) ?? 0));
113
+ // Float noise must not round a whole width up; `+ 0` turns an empty line's -0 into 0.
114
+ return Math.ceil((units * fontSize) / unitsPerEm - 1e-9) + 0;
115
+ },
116
+ };
117
+ } catch (error) {
118
+ if (error instanceof OutOfBounds) return null;
119
+ throw error;
120
+ }
121
+ }
122
+
55
123
  /**
56
124
  * Words broken into lines that fit `width` by `measure`, greedily as a browser breaks the render's pill: each line is
57
125
  * its words, and a word wider than a line stands alone on its own.
@@ -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,8 +16,8 @@ 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, 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";
@@ -69,6 +70,37 @@ 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
+
72
104
  /** How far a segment marked `kenburns` zooms when the film's `kenBurnsPercent` is 0, as in the render. */
73
105
  const HAND_KEN_BURNS_PERCENT = 4;
74
106
  const DRAFT_CRF = 28;
@@ -154,6 +186,18 @@ export interface FootageDraftCaptionsInput {
154
186
  style?: BrandStyle;
155
187
  /** The width of a line in the captions' font (`createDraftTextMeasure`); absent or null: the `OVERLAY_CHAR_EM` estimate. */
156
188
  measure?: DraftTextMeasure | null;
189
+ /**
190
+ * The draft's fonts (`loadFootageDraftFonts`): every text is drawn as large as the render draws it, its lines broken
191
+ * by the font's own widths in CSS px, as the render's are. It takes the place of `fontFamily`, `overlayFonts` and
192
+ * `measure`. Absent: those fields, and every size goes to libass as it is (libass then draws a brand font smaller
193
+ * than the render does, by its `usWinAscent + usWinDescent` over its em).
194
+ */
195
+ fonts?: FootageDraftFonts;
196
+ }
197
+
198
+ /** The overlays' heading and interface families where they are not the body's (the styles' own). */
199
+ function getOverlayFamilies(fonts: FootageDraftFonts): Partial<Record<OverlayFontRole, string>> {
200
+ return Object.fromEntries((["heading", "interface"] as const).flatMap((role) => (fonts[role].family === fonts.body.family ? [] : [[role, fonts[role].family.replace(/,/g, " ")]])));
157
201
  }
158
202
 
159
203
  /** 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 +272,8 @@ interface DrawnWord {
228
272
  left: number;
229
273
  width: number;
230
274
  lineTop: number;
275
+ /** How tall libass draws the word's line. */
276
+ lineHeight: number;
231
277
  }
232
278
 
233
279
  /** 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 +290,8 @@ interface DrawnPillInput {
244
290
  frame: Frame;
245
291
  style: BrandStyle;
246
292
  measure: DraftTextMeasure;
293
+ /** How tall libass draws a line of the caption font, per CSS px of its size (`DraftFont.assScale`). */
294
+ lineScale: number;
247
295
  colors: { background: string; accent: string };
248
296
  }
249
297
 
@@ -255,6 +303,7 @@ interface DrawnPillInput {
255
303
  function getDrawnPill(input: DrawnPillInput): DrawnPill {
256
304
  const { place, frame, style, measure } = input;
257
305
  const scale = frame.width / 1080;
306
+ const lineHeight = place.fontSize * input.lineScale;
258
307
  const pad = getStylePadding(CAPTION_PILL_PADDING, style.padding);
259
308
  const areaWidth = frame.width - place.left - place.right;
260
309
  const lines = wrapDraftLine(input.words, areaWidth - 2 * pad.x, place.fontSize, measure);
@@ -265,8 +314,8 @@ function getDrawnPill(input: DrawnPillInput): DrawnPill {
265
314
  breaks.add(counted - 1);
266
315
  }
267
316
  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;
317
+ const height = lines.length * lineHeight + 2 * pad.y;
318
+ const textTop = place.bottom === undefined ? place.top : frame.height - place.bottom - lines.length * lineHeight;
270
319
  const box = { left: place.left + (areaWidth - width) / 2, top: textTop - pad.y, width, height };
271
320
  const at = `${toAssTime(input.span.start)},${toAssTime(input.span.end)}`;
272
321
  const radius = getCornerRadiusPx(getCaptionCornerRadius(style), height, scale);
@@ -275,7 +324,7 @@ function getDrawnPill(input: DrawnPillInput): DrawnPill {
275
324
  const lineLeft = place.left + (areaWidth - measure(line.join(" "), place.fontSize)) / 2;
276
325
  return line.map((word, index) => {
277
326
  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 };
327
+ return { left: lineLeft + measure(line.slice(0, index + 1).join(" "), place.fontSize) - width, width, lineTop: textTop + lineIndex * lineHeight, lineHeight };
279
328
  });
280
329
  });
281
330
  return { breaks, events: getDraftBoxEvents({ layer: 0, at, styleName: "CaptionShape", box, radius, color: input.colors.background, accent: input.colors.accent, style, scale }), words };
@@ -288,6 +337,8 @@ interface RibbonInput {
288
337
  frame: Frame;
289
338
  tilt: number;
290
339
  measure: DraftTextMeasure;
340
+ /** How tall libass draws a line of the caption font, per CSS px of its size. */
341
+ lineScale: number;
291
342
  colors: { background: string; text: string };
292
343
  }
293
344
 
@@ -304,6 +355,7 @@ function getRibbonEvents(input: RibbonInput): string[] {
304
355
  const areaWidth = frame.width - place.left - place.right;
305
356
  const pad = { x: padding.x * size, y: padding.y * size };
306
357
  const lines = wrapDraftLine(input.text.split(" "), areaWidth - 2 * pad.x, size, measure).map((line) => line.join(" "));
358
+ const drawn = size * input.lineScale;
307
359
  const step = lineHeight * size;
308
360
  const blockTop = place.bottom === undefined ? place.top : frame.height - place.bottom - lines.length * step;
309
361
  const centre = { x: place.left + areaWidth / 2, y: blockTop + (lines.length * step) / 2 };
@@ -313,8 +365,8 @@ function getRibbonEvents(input: RibbonInput): string[] {
313
365
  return lines.flatMap((line, index) => {
314
366
  const width = measure(line, size) + 2 * pad.x;
315
367
  // 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 };
368
+ const textTop = blockTop + index * step + (step - drawn) / 2;
369
+ const strip = { left: centre.x - width / 2, top: textTop - pad.y, width, height: drawn + 2 * pad.y };
318
370
  return [
319
371
  ...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
372
  `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 +386,7 @@ function buildUnderlineEvents(chunk: { words: TimedWord[] }, span: { start: numb
334
386
  return getHighlightPieces(chunk, span).flatMap((piece) =>
335
387
  words.flatMap((word, index) => {
336
388
  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 };
389
+ const bar = { left: word.left, top: word.lineTop + Math.round(word.lineHeight * DRAFT_UNDERLINE_TOP_EM), width: word.width, height: thickness };
338
390
  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
391
  }),
340
392
  );
@@ -371,7 +423,13 @@ export function buildFootageDraftCaptions(input: FootageDraftCaptionsInput): str
371
423
  if (chunks.length === 0 && overlays.length === 0) return null;
372
424
  const spans = getCaptionSpans(chunks, getEdlDuration(input.edl));
373
425
  const { caption, frame } = input;
374
- const family = (input.fontFamily ?? FOOTAGE_DRAFT_FONT.family).replace(/,/g, " ");
426
+ const fonts = input.fonts;
427
+ const family = (fonts?.body.family ?? input.fontFamily ?? FOOTAGE_DRAFT_FONT.family).replace(/,/g, " ");
428
+ // Every size below is the render's (CSS px); an ASS size is written through the body font's scale (1 without `fonts`).
429
+ const lineScale = fonts?.body.assScale ?? 1;
430
+ const styleSize = toDraftAssSize(caption.fontSize, lineScale);
431
+ // With the draft's fonts the letters are spaced as the render spaces them (`letter-spacing`); without, as before.
432
+ const spacing = fonts === undefined ? 0 : toDraftSpacing(isRibbon ? CAPTION_RIBBON_LETTER_SPACING_EM : CAPTION_LETTER_SPACING_EM, caption.fontSize);
375
433
  const colors = input.colors ?? FOOTAGE_DRAFT_CAPTION_COLORS;
376
434
  const text = toAssColor(colors.captionText);
377
435
  const box = toAssColor(colors.captionBackground);
@@ -389,7 +447,7 @@ export function buildFootageDraftCaptions(input: FootageDraftCaptionsInput): str
389
447
  const toStyle = (name: string, place: CaptionLayout): string => [
390
448
  name,
391
449
  family,
392
- caption.fontSize,
450
+ styleSize,
393
451
  look.primary,
394
452
  look.primary,
395
453
  look.outlineColor,
@@ -400,7 +458,7 @@ export function buildFootageDraftCaptions(input: FootageDraftCaptionsInput): str
400
458
  0,
401
459
  100,
402
460
  100,
403
- 0,
461
+ spacing,
404
462
  0,
405
463
  look.borderStyle,
406
464
  look.outline,
@@ -431,17 +489,18 @@ export function buildFootageDraftCaptions(input: FootageDraftCaptionsInput): str
431
489
  const boxPadding = Math.max(1, Math.round(caption.fontSize * CAPTION_HIGHLIGHT_BOX_PADDING_EM));
432
490
  // With the brand's style a `highlight` chunk stands on a drawn rounded pill (its lines broken here), not libass's box.
433
491
  const style = isHighlight ? input.style : undefined;
434
- const measure = input.measure ?? ((line: string, size: number) => estimateTextWidth(line, size));
492
+ const measure =
493
+ 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
494
  const pillColors = { background: colors.captionBackground, accent: lit.style === "box" ? lit.box : colors.captionHighlight };
436
495
  const events = chunks.flatMap((chunk, index) => {
437
496
  const span = spans[index] as { start: number; end: number };
438
497
  if (isRibbon) {
439
498
  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 } });
499
+ return getRibbonEvents({ text: ribbonText, span, place: places[index] ?? caption, frame, tilt: getCaptionRibbonTilt(input.film), measure, lineScale, colors: { background: colors.captionBackground, text: colors.captionText } });
441
500
  }
442
501
  const styleName = styleNames[index] ?? "Caption";
443
502
  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 });
503
+ 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
504
  const layout: ChunkLayout = pill === null ? BOXLESS_LAYOUT : { breaks: pill.breaks, hasPill: true };
446
505
  const drawn = pill?.events ?? [];
447
506
  if (isHighlight && lit.style === "underline") {
@@ -465,14 +524,14 @@ export function buildFootageDraftCaptions(input: FootageDraftCaptionsInput): str
465
524
  "[V4+ Styles]",
466
525
  "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
526
  ...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`]),
527
+ ...((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
528
  ...(overlays.length === 0 ? [] : getDraftOverlayStyles(family, input.overlayColors ?? FOOTAGE_DRAFT_OVERLAY_COLORS)),
470
529
  "",
471
530
  "[Events]",
472
531
  "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text",
473
532
  ...events,
474
533
  ...buildDraftOverlayEvents(overlays, input.overlayColors ?? FOOTAGE_DRAFT_OVERLAY_COLORS, input.frame, {
475
- ...(input.overlayFonts === undefined ? {} : { fonts: input.overlayFonts }),
534
+ ...(fonts === undefined ? (input.overlayFonts === undefined ? {} : { fonts: input.overlayFonts }) : { fonts: getOverlayFamilies(fonts), drawnFonts: fonts }),
476
535
  ...(input.locale === undefined ? {} : { locale: input.locale }),
477
536
  ...(input.style === undefined ? {} : { style: input.style }),
478
537
  }),
@@ -1118,34 +1177,25 @@ async function runRenderFootageDraft(
1118
1177
  ? { background: brandColors.background, foreground: brandColors.foreground, accent: brandColors.accent }
1119
1178
  : FOOTAGE_DRAFT_OVERLAY_COLORS;
1120
1179
  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 }>>;
1180
+ // Every font sized and measured as the render sizes it; the overlays' roles only when there are overlays to draw.
1181
+ const fonts = loadFootageDraftFonts(overlays.length === 0 ? { body: options.brand?.fonts.body ?? null } : options.brand?.fonts);
1129
1182
  const captions = buildFootageDraftCaptions({
1130
1183
  film,
1131
1184
  edl,
1132
1185
  words,
1133
1186
  caption: getFootageCaptionLayout(film, options.layout ?? {}),
1134
1187
  frame,
1135
- fontFamily: font.family,
1188
+ fonts,
1136
1189
  ...(options.brand === undefined ? {} : { colors: options.brand.colors }),
1137
1190
  ...(overlays.length === 0 ? {} : { overlays, overlayColors }),
1138
- ...(Object.keys(roleFonts).length === 0 ? {} : { overlayFonts: Object.fromEntries(Object.entries(roleFonts).map(([role, chosen]) => [role, chosen.family])) }),
1139
1191
  ...(options.brand?.locale === undefined ? {} : { locale: options.brand.locale }),
1140
1192
  ...(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
1193
  });
1144
1194
  if (captions !== null) {
1145
1195
  writeFileSync(join(draftDir, "captions.ass"), captions);
1146
1196
  mkdirSync(join(draftDir, "fonts"));
1147
1197
  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)));
1198
+ for (const file of new Set([fonts.body.file, fonts.heading.file, fonts.interface.file, ...handFont])) copyFileSync(file, join(draftDir, "fonts", basename(file)));
1149
1199
  if (brandFont === null && options.brand?.fonts.body != null) {
1150
1200
  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
1201
  }
@@ -1223,12 +1273,12 @@ async function runRenderFootageDraft(
1223
1273
  const draft = getFootageDraftFrame(frame, options.width);
1224
1274
  const bytes = statSync(output).size;
1225
1275
  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"];
1276
+ const notes = [captions === null ? "no captions" : `captions in ${fonts.body.family}`, draftMusic === "played" ? "music" : draftMusic === "none" ? "no music" : "music not cached"];
1227
1277
  log(`draft: ${output}, ${draft.width}×${draft.height}, ${seconds.toFixed(1)} s, ${bytes} bytes, ${notes.join(", ")}.`);
1228
1278
  return ok({
1229
1279
  output,
1230
1280
  music: draftMusic,
1231
- captionFont: captions === null ? null : font.family,
1281
+ captionFont: captions === null ? null : fonts.body.family,
1232
1282
  seconds,
1233
1283
  assembly,
1234
1284
  pieces: pieceCount,