Skip to content

Open source CapCut alternative.

TypeScript

feat(cut): caption font and size controls in the subtitles panel

David Le380532fa095a
diff --git a/site/src/cut/components/SubtitlesPanel.tsx b/site/src/cut/components/SubtitlesPanel.tsx
index 7a5d253e..2dd19dc9 100644
--- a/site/src/cut/components/SubtitlesPanel.tsx
+++ b/site/src/cut/components/SubtitlesPanel.tsx
@@ -3,7 +3,16 @@
import React, { memo, useEffect, useRef, useState } from "react";
import { AlertCircle, Captions, ChevronDown, Languages, Loader2, RefreshCw } from "lucide-react";
import { Button } from "@/components/ui/button";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select";
+import { Slider } from "@/components/ui/slider";
import { Switch } from "@/components/ui/switch";
+import { parseNumberInput, ScrubValue } from "@/cut/components/ScrubValue";
import { GenerateSubtitlesAudio } from "@/cut/components/VoicePicker";
import {
CAPTION_STYLES,
@@ -18,7 +27,9 @@ import { useElapsed } from "@/cut/hooks/useElapsed";
import { TIMELINE_H_MIN, useEditor } from "@/cut/lib/store";
import { PLATE_PAD_X, PLATE_PAD_Y, PLATE_RADIUS, plateFill } from "@/cut/lib/textRender";
import {
+ FONTS,
fontStack,
+ type FontId,
type SubtitleCue,
type SubtitlesBlock,
type WordAccentMode,
@@ -191,6 +202,50 @@ function OptionsTab() {
}}
/>
</label>
+ <div className="flex min-h-8 items-center justify-between text-xs font-medium">
+ Font
+ <Select
+ value={subtitles.font ?? captionStyle(subtitles.style).font}
+ items={Object.fromEntries(FONTS.map((f) => [f.id, f.label]))}
+ onValueChange={(v) => useEditor.getState().setSubtitlesView({ font: v as FontId })}
+ >
+ <SelectTrigger size="sm" className="sub-font w-28">
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent>
+ {FONTS.map((f) => (
+ <SelectItem key={f.id} value={f.id}>
+ <span style={{ fontFamily: fontStack(f.id) }}>{f.label}</span>
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ </div>
+ <div className="flex min-h-8 items-center justify-between text-xs font-medium">
+ Size
+ <div className="sub-size flex items-center gap-2">
+ <Slider
+ className="data-horizontal:w-24"
+ min={24}
+ max={120}
+ step={1}
+ value={subtitles.size ?? captionStyle(subtitles.style).size}
+ onValueChange={(v) => useEditor.getState().setSubtitlesView({ size: Number(v) })}
+ />
+ <ScrubValue
+ label="Caption size"
+ className="w-7 text-muted-foreground"
+ value={subtitles.size ?? captionStyle(subtitles.style).size}
+ min={24}
+ max={120}
+ step={1}
+ format={(v) => String(Math.round(v))}
+ parse={parseNumberInput}
+ onScrub={(v) => useEditor.getState().setSubtitlesView({ size: v })}
+ onCommit={(v) => useEditor.getState().setSubtitlesView({ size: v })}
+ />
+ </div>
+ </div>
<label className="flex min-h-8 items-center justify-between text-xs font-medium">
Highlight spoken word
<Switch
@@ -302,7 +357,9 @@ function StylesTab() {
style === cs.id ? "bg-primary/10 ring-1 ring-primary" : "hover:bg-muted"
)}
aria-pressed={style === cs.id}
- onClick={() => useEditor.getState().setSubtitlesView({ style: cs.id })}
+ // Picking a style hands the font back to the preset; the last
+ // choice — custom font or style — wins.
+ onClick={() => useEditor.getState().setSubtitlesView({ style: cs.id, font: undefined })}
>
<div className="grid h-12 place-items-center overflow-hidden rounded-md">
<span
diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.ts
index 5660e02e..24e485c9 100644
--- a/site/src/cut/lib/store.ts
+++ b/site/src/cut/lib/store.ts
@@ -262,7 +262,7 @@ export interface EditorState {
* track's language. Timings copy over; word timings don't survive
* translation, so the new cues carry none. */
translateSubtitleTrack: (fromLane: number) => Promise<void>;
- setSubtitlesView: (patch: Partial<Pick<SubtitlesBlock, "showOnVideo" | "showOnTimeline" | "locale" | "style" | "x" | "y" | "wordHighlight" | "accentMode" | "accentColor">>) => void;
+ setSubtitlesView: (patch: Partial<Pick<SubtitlesBlock, "showOnVideo" | "showOnTimeline" | "locale" | "style" | "size" | "font" | "x" | "y" | "wordHighlight" | "accentMode" | "accentColor">>) => void;
/** The subtitle track (row) the panel edits and generation writes to. */
subtitleLane: number;
setSubtitleLane: (lane: number) => void;
diff --git a/site/src/cut/lib/subtitles.ts b/site/src/cut/lib/subtitles.ts
index 67692f6e..96c8be67 100644
--- a/site/src/cut/lib/subtitles.ts
+++ b/site/src/cut/lib/subtitles.ts
@@ -31,19 +31,22 @@ export function trackLocale(subs: SubtitlesBlock, lane: number): string {
return subs.tracks?.[lane]?.locale ?? (lane === 0 ? subs.locale : undefined) ?? "en-US";
}
-/** A track's effective caption anchor plus the block's karaoke overrides —
- * the `pos` argument `cueOverlay` takes. The track's own dragged spot wins,
- * then the block-level legacy spot (first track only), then the style's
- * default stacked upward per track so simultaneous languages never overlap. */
+/** A track's effective caption anchor plus the block's look overrides
+ * (karaoke accent, font size) — the `pos` argument `cueOverlay` takes. The
+ * track's own dragged spot wins, then the block-level legacy spot (first
+ * track only), then the style's default stacked upward per track so
+ * simultaneous languages never overlap. */
export function trackPos(
subs: SubtitlesBlock,
style: CaptionStyle,
lane: number
-): { x: number; y: number; accentMode?: WordAccentMode; accentColor?: string } {
+): { x: number; y: number; size?: number; font?: FontId; accentMode?: WordAccentMode; accentColor?: string } {
const meta = subs.tracks?.[lane];
return {
x: meta?.x ?? (lane === 0 ? subs.x : undefined) ?? style.x,
y: meta?.y ?? (lane === 0 ? subs.y : undefined) ?? Math.max(0.08, style.y - lane * 0.1),
+ size: subs.size,
+ font: subs.font,
accentMode: subs.accentMode,
accentColor: subs.accentColor,
};
@@ -332,18 +335,21 @@ export function cueWordWindows(cue: SubtitleCue): { start: number; end: number }
}
/** A cue as a synthetic overlay, so captions ride the title pipeline. The style
- * preset drives the look; the opening cue can render bigger for a punchy hook;
- * a dragged caption position (frame fractions) overrides the preset's spot;
- * a word index marks that word for the karaoke accent. */
+ * preset drives the look; a user font or font size overrides the preset's
+ * (the opener emphasis still multiplies the size); a dragged caption position
+ * (frame fractions) overrides the preset's spot; a word index marks that word
+ * for the karaoke accent. */
export function cueOverlay(
cue: SubtitleCue,
style: CaptionStyle = CAPTION_STYLES.clean,
isOpener = false,
- pos?: { x?: number; y?: number; accentMode?: WordAccentMode; accentColor?: string },
+ pos?: { x?: number; y?: number; size?: number; font?: FontId; accentMode?: WordAccentMode; accentColor?: string },
wordIndex?: number
): TextOverlay {
const kl = wordIndex !== undefined ? karaokeLook(style, pos) : null;
- const size = Math.round(style.size * (isOpener && style.openerScale ? style.openerScale : 1));
+ const size = Math.round(
+ (pos?.size ?? style.size) * (isOpener && style.openerScale ? style.openerScale : 1)
+ );
return {
id: `sub-${cue.id}`,
text: wrapCaptionForSize(cue.text, size),
@@ -352,7 +358,7 @@ export function cueOverlay(
x: pos?.x ?? style.x,
y: pos?.y ?? style.y,
size,
- font: style.font,
+ font: pos?.font ?? style.font,
weight: style.weight,
color: style.color,
shadow: style.shadow,
diff --git a/site/src/cut/lib/types.ts b/site/src/cut/lib/types.ts
index f3060b39..91177219 100644
--- a/site/src/cut/lib/types.ts
+++ b/site/src/cut/lib/types.ts
@@ -607,6 +607,12 @@ export interface SubtitlesBlock {
generatedAt?: number;
/** Caption look preset; absent = the plain "clean" subtitle style. */
style?: CaptionStyleId;
+ /** Caption font size (px at a 1080-wide frame); absent = the style
+ * preset's size. Applies to every track. */
+ size?: number;
+ /** Caption font override; absent = the style preset's font. Picking a
+ * style preset clears it, so the last choice — font or style — wins. */
+ font?: FontId;
/** Legacy caption anchor for the first track; dragging now writes the
* per-track anchor in `tracks`. Read as the lane-0 fallback. */
x?: number;