Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

Paint titles, so a text clip is a picture the monitor and export draw

Jareer8a2231cff07e
diff --git a/engine/Cargo.lock b/engine/Cargo.lock
index 54a423c9..20c714bd 100644
--- a/engine/Cargo.lock
+++ b/engine/Cargo.lock
@@ -1156,6 +1156,7 @@ dependencies = [
"concat-media",
"concat-project",
"concat-render",
+ "concat-text",
"cpal",
"memmap2",
"serde",
@@ -1205,6 +1206,16 @@ dependencies = [
"whisper-rs",
]
+[[package]]
+name = "concat-text"
+version = "0.2.0"
+dependencies = [
+ "fontdb",
+ "rustybuzz",
+ "tiny-skia 0.12.0",
+ "ttf-parser",
+]
+
[[package]]
name = "concurrent-queue"
version = "2.5.0"
@@ -1926,13 +1937,24 @@ dependencies = [
"bytemuck",
]
+[[package]]
+name = "fontconfig-parser"
+version = "0.5.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbc773e24e02d4ddd8395fd30dc147524273a83e54e0f312d986ea30de5f5646"
+dependencies = [
+ "roxmltree 0.20.0",
+]
+
[[package]]
name = "fontdb"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "457e789b3d1202543297a350643cf459f836cade38934e7a4cf6a39e7cde2905"
dependencies = [
+ "fontconfig-parser",
"log",
+ "memmap2",
"slotmap",
"tinyvec",
"ttf-parser",
@@ -1953,7 +1975,7 @@ dependencies = [
"objc2-foundation 0.3.2",
"parlance",
"read-fonts 0.39.2",
- "roxmltree",
+ "roxmltree 0.21.1",
"smallvec",
"windows 0.62.2",
"windows-core 0.62.2",
@@ -5005,6 +5027,12 @@ dependencies = [
"text-size",
]
+[[package]]
+name = "roxmltree"
+version = "0.20.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97"
+
[[package]]
name = "roxmltree"
version = "0.21.1"
@@ -6218,7 +6246,7 @@ dependencies = [
"kurbo",
"log",
"pico-args",
- "roxmltree",
+ "roxmltree 0.21.1",
"rustybuzz",
"simplecss",
"siphasher",
diff --git a/engine/Cargo.toml b/engine/Cargo.toml
index 10be1346..837d49ae 100644
--- a/engine/Cargo.toml
+++ b/engine/Cargo.toml
@@ -17,6 +17,7 @@ concat-export = { path = "crates/concat-export" }
concat-host = { path = "crates/concat-host" }
concat-speech = { path = "crates/concat-speech" }
concat-effects = { path = "crates/concat-effects" }
+concat-text = { path = "crates/concat-text" }
serde_json = "1"
thiserror = "2"
diff --git a/engine/crates/concat-host/Cargo.toml b/engine/crates/concat-host/Cargo.toml
index 50f03d35..e470dbc1 100644
--- a/engine/crates/concat-host/Cargo.toml
+++ b/engine/crates/concat-host/Cargo.toml
@@ -16,6 +16,8 @@ concat-core.workspace = true
concat-media.workspace = true
concat-project.workspace = true
concat-export.workspace = true
+# Titles become pictures here; see titles.rs.
+concat-text.workspace = true
concat-render = { workspace = true, optional = true }
serde = { version = "1", features = ["derive"] }
serde_json.workspace = true
diff --git a/engine/crates/concat-host/src/lib.rs b/engine/crates/concat-host/src/lib.rs
index 5737f8a6..801d3bee 100644
--- a/engine/crates/concat-host/src/lib.rs
+++ b/engine/crates/concat-host/src/lib.rs
@@ -26,8 +26,10 @@ pub mod preview;
pub mod projects;
pub mod session;
pub mod templates;
+pub mod titles;
pub use dirs::AppDirs;
pub use jobs::{Job, SingleFlight};
pub use projects::ProjectInfo;
pub use session::{EditorView, Session, SettingsView};
+pub use titles::{TitleClip, Titles};
diff --git a/engine/crates/concat-host/src/titles.rs b/engine/crates/concat-host/src/titles.rs
new file mode 100644
index 00000000..46e0d43d
--- /dev/null
+++ b/engine/crates/concat-host/src/titles.rs
@@ -0,0 +1,306 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2026 Jareer and Concat contributors
+
+//! Titles, rendered and remembered.
+//!
+//! The flattener leaves text clips out on purpose: the compositor only knows
+//! pictures. This is where a title becomes one. Each text clip's style is
+//! painted by `concat-text` onto a frame-sized transparent PNG in the app's
+//! data directory, and the clip rejoins the flattened list as an image clip
+//! pointing at that file, carrying the text clip's timing, transform and
+//! opacity. The monitor, playback prefetch and the exporter then treat it as
+//! any other still.
+//!
+//! The file is keyed by everything that changes the pixels - the style, the
+//! frame size, the fonts the project carries - so a title that has not
+//! changed is never painted twice, across sessions included. What a title
+//! does *not* key on is where it sits or when it plays: moving one is free.
+
+use std::collections::hash_map::DefaultHasher;
+use std::collections::{HashMap, HashSet};
+use std::hash::{Hash, Hasher};
+use std::path::{Path, PathBuf};
+use std::sync::Mutex;
+
+use concat_export::chains::video_effect_chain;
+use concat_export::{ClipKind, ExportClip};
+use concat_project::model::{ClipKind as ModelClipKind, Project, TextAlign, TextStyle};
+use concat_text::{Align, Fonts, TitleStyle};
+
+use crate::dirs::AppDirs;
+
+/// One title, ready for the compositor.
+#[derive(Clone)]
+pub struct TitleClip {
+ /// The text clip this was painted from.
+ pub clip_id: String,
+ /// The image clip that stands in for it.
+ pub clip: ExportClip,
+ /// The painted block's size in frame pixels, for an outline on a monitor.
+ pub block: (u32, u32),
+}
+
+/// What one render left behind.
+#[derive(Clone, Copy, Debug)]
+struct Art {
+ block: (u32, u32),
+}
+
+/// The title painter and its cache.
+pub struct Titles {
+ dir: PathBuf,
+ /// The system's faces plus the project's files, loaded once each.
+ fonts: Mutex<Option<Fonts>>,
+ loaded_files: Mutex<HashSet<String>>,
+ /// What is known to be on disk, by key.
+ memo: Mutex<HashMap<u64, Art>>,
+}
+
+impl Titles {
+ /// A painter that caches under the app's data directory.
+ pub fn new(dirs: &AppDirs) -> Titles {
+ Titles {
+ dir: dirs.data.join("titles"),
+ fonts: Mutex::new(None),
+ loaded_files: Mutex::new(HashSet::new()),
+ memo: Mutex::new(HashMap::new()),
+ }
+ }
+
+ /// Every text clip on the active timeline, as an image clip each, for a
+ /// `width` × `height` frame. A title that fails to paint is left out and
+ /// said once on stderr; the rest of the edit still renders.
+ pub fn clips(&self, project: &Project, width: u32, height: u32) -> Vec<TitleClip> {
+ let timeline = project.active();
+ let mut out = Vec::new();
+ for clip in &timeline.clips {
+ if clip.kind != ModelClipKind::Text {
+ continue;
+ }
+ let Some(index) = timeline
+ .tracks
+ .iter()
+ .position(|track| track.id == clip.track_id)
+ else {
+ continue;
+ };
+ let track = &timeline.tracks[index];
+ let text = clip.text.clone().unwrap_or_default();
+ let (path, block) = match self.painted(project, &text, width, height) {
+ Ok(art) => art,
+ Err(error) => {
+ eprintln!("concat: title {}: {error}", clip.id);
+ continue;
+ }
+ };
+ out.push(TitleClip {
+ clip_id: clip.id.clone(),
+ clip: ExportClip {
+ path: path.to_string_lossy().into_owned(),
+ kind: ClipKind::Image,
+ start: clip.start,
+ duration: clip.duration,
+ source_start: 0.0,
+ track: index,
+ hidden: !track.visible,
+ muted: true,
+ volume: 0.0,
+ fade_in: 0.0,
+ fade_out: 0.0,
+ filter_chain: String::new(),
+ speed: 1.0,
+ preserve_pitch: true,
+ scale: clip.scale,
+ offset_x: clip.offset_x,
+ offset_y: clip.offset_y,
+ rotation: clip.rotation,
+ // The style's own opacity multiplies the clip's: a
+ // half-transparent title fades to half, not to solid.
+ opacity: (clip.opacity * text.opacity).clamp(0.0, 1.0),
+ video_filter_chain: video_effect_chain(&clip.video_effects),
+ transition: None,
+ video_fade_in: 0.0,
+ media_width: Some(width),
+ media_height: Some(height),
+ has_audio: Some(false),
+ },
+ block,
+ });
+ }
+ out
+ }
+
+ /// The PNG for one style at one frame size, painted if it is not on disk
+ /// yet, and the block it holds.
+ fn painted(
+ &self,
+ project: &Project,
+ style: &TextStyle,
+ width: u32,
+ height: u32,
+ ) -> Result<(PathBuf, (u32, u32)), String> {
+ let key = key_of(project, style, width, height);
+ let png = self.dir.join(format!("{key:016x}.png"));
+ let side = self.dir.join(format!("{key:016x}.json"));
+
+ if let Some(art) = self
+ .memo
+ .lock()
+ .unwrap_or_else(|e| e.into_inner())
+ .get(&key)
+ && png.is_file()
+ {
+ return Ok((png, art.block));
+ }
+ // On disk from an earlier session: the sidecar says how big the
+ // block was, which the PNG alone cannot.
+ if png.is_file()
+ && let Some(block) = read_block(&side)
+ {
+ self.remember(key, block);
+ return Ok((png, block));
+ }
+
+ let title = title_style(style);
+ let rendered = {
+ let mut fonts = self.fonts.lock().unwrap_or_else(|e| e.into_inner());
+ let fonts = fonts.get_or_insert_with(Fonts::new);
+ // The project's own files, each loaded once for the process.
+ let mut loaded = self.loaded_files.lock().unwrap_or_else(|e| e.into_inner());
+ for font in &project.fonts {
+ if !font.path.is_empty() && loaded.insert(font.path.clone()) {
+ fonts.add_file(Path::new(&font.path));
+ }
+ }
+ concat_text::render(fonts, &title, width, height).map_err(|error| error.to_string())?
+ };
+ std::fs::create_dir_all(&self.dir).map_err(|error| error.to_string())?;
+ std::fs::write(&png, &rendered.png).map_err(|error| error.to_string())?;
+ let block = (rendered.block_width, rendered.block_height);
+ let _ = std::fs::write(&side, format!("{{\"w\":{},\"h\":{}}}", block.0, block.1));
+ self.remember(key, block);
+ Ok((png, block))
+ }
+
+ fn remember(&self, key: u64, block: (u32, u32)) {
+ self.memo
+ .lock()
+ .unwrap_or_else(|e| e.into_inner())
+ .insert(key, Art { block });
+ }
+}
+
+/// Everything the pixels depend on, hashed. Placement and timing are left
+/// out on purpose; see the module docs.
+fn key_of(project: &Project, style: &TextStyle, width: u32, height: u32) -> u64 {
+ let mut hasher = DefaultHasher::new();
+ // The style as JSON: every field, in a stable order, with no need to
+ // keep a Hash impl in step with the struct.
+ serde_json::to_string(style)
+ .unwrap_or_default()
+ .hash(&mut hasher);
+ width.hash(&mut hasher);
+ height.hash(&mut hasher);
+ for font in &project.fonts {
+ font.family.hash(&mut hasher);
+ font.path.hash(&mut hasher);
+ }
+ // Bumped when the painter's output changes for the same input, so stale
+ // files are not mistaken for current ones.
+ 1u32.hash(&mut hasher);
+ hasher.finish()
+}
+
+fn read_block(side: &Path) -> Option<(u32, u32)> {
+ let text = std::fs::read_to_string(side).ok()?;
+ let value: serde_json::Value = serde_json::from_str(&text).ok()?;
+ Some((
+ value.get("w")?.as_u64()? as u32,
+ value.get("h")?.as_u64()? as u32,
+ ))
+}
+
+/// The document's style, field for field, in the painter's terms.
+fn title_style(style: &TextStyle) -> TitleStyle {
+ TitleStyle {
+ content: style.content.clone(),
+ font_family: style.font_family.clone(),
+ font_size: style.font_size,
+ font_weight: style.font_weight,
+ italic: style.italic,
+ color: style.color.clone(),
+ align: match style.align {
+ TextAlign::Left => Align::Left,
+ TextAlign::Center => Align::Center,
+ TextAlign::Right => Align::Right,
+ },
+ stroke_width: style.stroke_width,
+ stroke_color: style.stroke_color.clone(),
+ shadow: style.shadow,
+ background: style.background.clone(),
+ line_height: style.line_height,
+ tracking: style.tracking,
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use concat_project::{Command, Editor};
+
+ fn scratch() -> AppDirs {
+ let dir = std::env::temp_dir().join(format!("concat-titles-test-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ AppDirs {
+ config: dir.join("config"),
+ data: dir.join("data"),
+ }
+ }
+
+ /// A text clip comes back as an image clip on its own track, pointing
+ /// at a PNG on disk that is the frame's size, and a second ask for the
+ /// same title paints nothing new.
+ #[test]
+ fn a_title_rejoins_as_a_still() {
+ let dirs = scratch();
+ let mut editor = Editor::new();
+ let id = editor
+ .apply(Command::AddTextClip {
+ track_id: None,
+ start: 2.0,
+ style: None,
+ duration: Some(3.0),
+ offset_y: Some(0.3),
+ })
+ .expect("a title is added")
+ .created_id
+ .expect("with an id");
+
+ let titles = Titles::new(&dirs);
+ let out = titles.clips(editor.project(), 640, 360);
+ assert_eq!(out.len(), 1);
+ let title = &out[0];
+ assert_eq!(title.clip_id, id);
+ assert!(matches!(title.clip.kind, ClipKind::Image));
+ assert_eq!((title.clip.start, title.clip.duration), (2.0, 3.0));
+ assert_eq!(title.clip.offset_y, 0.3);
+ assert_eq!(title.clip.media_width, Some(640));
+ assert!(title.block.0 > 0 && title.block.1 > 0);
+ let painted = Path::new(&title.clip.path);
+ assert!(painted.is_file(), "the PNG is on disk");
+ let stamp = std::fs::metadata(painted).and_then(|m| m.modified()).ok();
+
+ // Same words, same frame: same file, untouched.
+ let again = titles.clips(editor.project(), 640, 360);
+ assert_eq!(again[0].clip.path, title.clip.path);
+ assert_eq!(
+ std::fs::metadata(painted).and_then(|m| m.modified()).ok(),
+ stamp
+ );
+
+ // A different frame is a different picture.
+ let wide = titles.clips(editor.project(), 1280, 720);
+ assert_ne!(wide[0].clip.path, title.clip.path);
+ let _ = std::fs::remove_dir_all(dirs.data.parent().unwrap());
+ }
+}
diff --git a/engine/crates/concat-text/Cargo.toml b/engine/crates/concat-text/Cargo.toml
new file mode 100644
index 00000000..3bcacd37
--- /dev/null
+++ b/engine/crates/concat-text/Cargo.toml
@@ -0,0 +1,22 @@
+[package]
+name = "concat-text"
+version.workspace = true
+edition.workspace = true
+rust-version.workspace = true
+license.workspace = true
+publish = false
+description = "Titles as pixels: shapes a text clip's words and rasterises them onto a frame-sized transparent canvas."
+
+[dependencies]
+# Font discovery - the system's faces plus the files a project carries.
+fontdb = "0.23"
+# Shaping: glyph ids and advances from the words, kerning and ligatures
+# included. Pure Rust HarfBuzz.
+rustybuzz = "0.20"
+# Outlines out of the font, into paths.
+ttf-parser = "0.25"
+# Fill, stroke and anti-aliasing of those paths, and the PNG encoder.
+tiny-skia = { version = "0.12", features = ["png-format"] }
+
+[lints]
+workspace = true
diff --git a/engine/crates/concat-text/src/lib.rs b/engine/crates/concat-text/src/lib.rs
new file mode 100644
index 00000000..700003d6
--- /dev/null
+++ b/engine/crates/concat-text/src/lib.rs
@@ -0,0 +1,606 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// SPDX-FileCopyrightText: 2026 Jareer and Concat contributors
+
+//! Titles as pixels.
+//!
+//! A text clip is a style and some words; the compositor wants a picture. This
+//! crate is the step between: it finds the face, shapes each line, turns the
+//! glyph outlines into paths, and paints them - plate, shadow, outline, fill -
+//! onto a canvas the size of the output frame, transparent everywhere the
+//! words are not.
+//!
+//! Frame-sized on purpose. The compositor places a picture by fitting it into
+//! the frame and then applying the clip's transform about its centre, so a
+//! canvas that *is* the frame fits at exactly one, decodes without resampling,
+//! and puts the block's centre where the clip's centre is. The clip's offset
+//! and rotation then mean the same thing for a title as for footage.
+//!
+//! Sizes in the style are fractions of the frame's height, as the document
+//! stores them, so a title looks the same at 720p and 4K. Everything here
+//! converts to pixels once, at the top.
+
+use std::fmt;
+
+use tiny_skia::{
+ Color, FillRule, LineCap, LineJoin, Paint, Path, PathBuilder, Pixmap, PixmapPaint, Rect,
+ Stroke, Transform,
+};
+
+/// How a title's lines sit within their block.
+#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
+pub enum Align {
+ /// Lines share a left edge.
+ Left,
+ /// Lines are centred on each other.
+ #[default]
+ Center,
+ /// Lines share a right edge.
+ Right,
+}
+
+/// Everything about a title's look. Mirrors the document's text style field
+/// for field so the host can copy it across; this crate does not depend on
+/// the document.
+#[derive(Clone, PartialEq, Debug)]
+pub struct TitleStyle {
+ /// The words, newlines included.
+ pub content: String,
+ /// CSS-style family name; quotes are tolerated and stripped.
+ pub font_family: String,
+ /// Em size as a fraction of frame height.
+ pub font_size: f64,
+ /// CSS-scale weight, 100..=900.
+ pub font_weight: f64,
+ /// Italic when true.
+ pub italic: bool,
+ /// Fill colour as `#rrggbb` or `#rrggbbaa`.
+ pub color: String,
+ /// Line alignment within the block.
+ pub align: Align,
+ /// Outline thickness as a fraction of frame height; zero for none.
+ pub stroke_width: f64,
+ /// Outline colour.
+ pub stroke_color: String,
+ /// A soft drop shadow behind the words.
+ pub shadow: bool,
+ /// A plate behind the block, `#rrggbb[aa]`; empty for none.
+ pub background: String,
+ /// Baseline pitch as a multiple of the em.
+ pub line_height: f64,
+ /// Extra advance after every glyph, as a fraction of frame height.
+ pub tracking: f64,
+}
+
+/// The finished title.
+#[derive(Clone, PartialEq, Debug)]
+pub struct Rendered {
+ /// The canvas, PNG-encoded, RGBA with alpha.
+ pub png: Vec<u8>,
+ /// The canvas width: the frame's.
+ pub width: u32,
+ /// The canvas height: the frame's.
+ pub height: u32,
+ /// The painted block's width in pixels, plate included - what an
+ /// outline on a monitor should be drawn around.
+ pub block_width: u32,
+ /// The painted block's height, on the same terms.
+ pub block_height: u32,
+}
+
+/// What can go wrong. Fonts fall back rather than fail, so this is short.
+#[derive(Debug)]
+pub enum Error {
+ /// The frame is zero-sized or too large for a pixmap.
+ Canvas(u32, u32),
+ /// No face at all could be found, not even a system fallback.
+ NoFont,
+ /// A face was found but its data could not be read as a font.
+ BadFont,
+ /// PNG encoding failed.
+ Encode(String),
+}
+
+impl fmt::Display for Error {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Error::Canvas(w, h) => write!(f, "cannot make a {w}×{h} canvas"),
+ Error::NoFont => write!(f, "no font found, not even a system fallback"),
+ Error::BadFont => write!(f, "the chosen font file could not be parsed"),
+ Error::Encode(why) => write!(f, "PNG encoding failed: {why}"),
+ }
+ }
+}
+
+impl std::error::Error for Error {}
+
+/// The faces available to titles: the system's, plus any files a project
+/// carries. Built once and kept; loading the system's fonts is the slow part.
+pub struct Fonts {
+ db: fontdb::Database,
+}
+
+impl Default for Fonts {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl Fonts {
+ /// The system's fonts.
+ pub fn new() -> Fonts {
+ let mut db = fontdb::Database::new();
+ db.load_system_fonts();
+ Fonts { db }
+ }
+
+ /// Adds one font file. A file that does not parse is skipped; a title
+ /// that names its family falls back to a system face.
+ pub fn add_file(&mut self, path: &std::path::Path) -> bool {
+ self.db.load_font_file(path).is_ok()
+ }
+
+ /// The best face for a style: the named family at the nearest weight and
+ /// slant, then any sans-serif, then anything at all.
+ fn pick(&self, style: &TitleStyle) -> Result<Vec<u8>, Error> {
+ let family = style
+ .font_family
+ .trim()
+ .trim_matches('"')
+ .trim_matches('\'');
+ let weight = fontdb::Weight(style.font_weight.clamp(100.0, 900.0).round() as u16);
+ let slant = if style.italic {
+ fontdb::Style::Italic
+ } else {
+ fontdb::Style::Normal
+ };
+ let mut families: Vec<fontdb::Family<'_>> = Vec::new();
+ if !family.is_empty() {
+ families.push(fontdb::Family::Name(family));
+ }
+ families.push(fontdb::Family::SansSerif);
+ let query = fontdb::Query {
+ families: &families,
+ weight,
+ stretch: fontdb::Stretch::Normal,
+ style: slant,
+ };
+ let id = self
+ .db
+ .query(&query)
+ .or_else(|| self.db.faces().next().map(|face| face.id))
+ .ok_or(Error::NoFont)?;
+ // Copied out: the shaper and the outliner both want a slice that
+ // outlives the database borrow, and a face is a few hundred KB.
+ self.db
+ .with_face_data(id, |data, index| {
+ // Multi-face collections: keep only the face that answered.
+ // rustybuzz takes the index, so the whole blob travels.
+ (data.to_vec(), index)
+ })
+ .map(|(data, index)| {
+ // Encode the index in front so the caller can hand both on.
+ let mut out = index.to_le_bytes().to_vec();
+ out.extend(data);
+ out
+ })
+ .ok_or(Error::BadFont)
+ }
+}
+
+/// One shaped line: its outline path in pixels, pen at the origin, and its
+/// advance width.
+struct Line {
+ path: Option<Path>,
+ width: f32,
+}
+
+/// A colour from `#rrggbb` or `#rrggbbaa`; anything else is `None`.
+fn colour(hex: &str) -> Option<Color> {
+ let hex = hex.trim().strip_prefix('#')?;
+ let byte = |at: usize| u8::from_str_radix(hex.get(at..at + 2)?, 16).ok();
+ match hex.len() {
+ 6 => Color::from_rgba8(byte(0)?, byte(2)?, byte(4)?, 255).into(),
+ 8 => Color::from_rgba8(byte(0)?, byte(2)?, byte(4)?, byte(6)?).into(),
+ _ => None,
+ }
+}
+
+/// Collects a glyph's outline, scaled and placed, into a path under
+/// construction. The font's y goes up; the canvas's goes down.
+struct Outliner<'a> {
+ builder: &'a mut PathBuilder,
+ scale: f32,
+ x: f32,
+ y: f32,
+}
+
+impl ttf_parser::OutlineBuilder for Outliner<'_> {
+ fn move_to(&mut self, x: f32, y: f32) {
+ self.builder
+ .move_to(self.x + x * self.scale, self.y - y * self.scale);
+ }
+ fn line_to(&mut self, x: f32, y: f32) {
+ self.builder
+ .line_to(self.x + x * self.scale, self.y - y * self.scale);
+ }
+ fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
+ self.builder.quad_to(
+ self.x + x1 * self.scale,
+ self.y - y1 * self.scale,
+ self.x + x * self.scale,
+ self.y - y * self.scale,
+ );
+ }
+ fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
+ self.builder.cubic_to(
+ self.x + x1 * self.scale,
+ self.y - y1 * self.scale,
+ self.x + x2 * self.scale,
+ self.y - y2 * self.scale,
+ self.x + x * self.scale,
+ self.y - y * self.scale,
+ );
+ }
+ fn close(&mut self) {
+ self.builder.close();
+ }
+}
+
+/// Shapes one line and outlines it, pen starting at (0, 0) on the baseline.
+fn shape_line(face: &rustybuzz::Face<'_>, text: &str, em: f32, tracking: f32) -> Line {
+ if text.is_empty() {
+ return Line {
+ path: None,
+ width: 0.0,
+ };
+ }
+ let scale = em / face.units_per_em() as f32;
+ let mut buffer = rustybuzz::UnicodeBuffer::new();
+ buffer.push_str(text);
+ let shaped = rustybuzz::shape(face, &[], buffer);
+ let mut builder = PathBuilder::new();
+ let mut pen = 0.0_f32;
+ for (info, position) in shaped
+ .glyph_infos()
+ .iter()
+ .zip(shaped.glyph_positions().iter())
+ {
+ let glyph = ttf_parser::GlyphId(info.glyph_id as u16);
+ let mut outliner = Outliner {
+ builder: &mut builder,
+ scale,
+ x: pen + position.x_offset as f32 * scale,
+ y: -(position.y_offset as f32 * scale),
+ };
+ face.outline_glyph(glyph, &mut outliner);
+ pen += position.x_advance as f32 * scale + tracking;
+ }
+ // The tracking after the last glyph is air nobody sees.
+ let width = (pen - tracking).max(0.0);
+ Line {
+ path: builder.finish(),
+ width,
+ }
+}
+
+/// A separable box blur over premultiplied RGBA, run twice for a soft
+/// falloff. Radius in pixels; zero leaves the picture alone.
+fn blur(pixmap: &mut Pixmap, radius: usize) {
+ if radius == 0 {
+ return;
+ }
+ let (width, height) = (pixmap.width() as usize, pixmap.height() as usize);
+ let data = pixmap.data_mut();
+ let mut scratch = vec![0u8; data.len()];
+ for _ in 0..2 {
+ // Horizontal, into scratch.
+ for y in 0..height {
+ let row = y * width * 4;
+ for channel in 0..4 {
+ let mut sum: u32 = 0;
+ let window = (2 * radius + 1) as u32;
+ let at = |x: isize| -> u32 {
+ let x = x.clamp(0, width as isize - 1) as usize;
+ u32::from(data[row + x * 4 + channel])
+ };
+ for x in -(radius as isize)..=(radius as isize) {
+ sum += at(x);
+ }
+ for x in 0..width {
+ scratch[row + x * 4 + channel] = (sum / window) as u8;
+ sum += at(x as isize + radius as isize + 1);
+ sum -= at(x as isize - radius as isize);
+ }
+ }
+ }
+ // Vertical, back into data.
+ for x in 0..width {
+ for channel in 0..4 {
+ let mut sum: u32 = 0;
+ let window = (2 * radius + 1) as u32;
+ let at = |y: isize| -> u32 {
+ let y = y.clamp(0, height as isize - 1) as usize;
+ u32::from(scratch[(y * width + x) * 4 + channel])
+ };
+ for y in -(radius as isize)..=(radius as isize) {
+ sum += at(y);
+ }
+ for y in 0..height {
+ data[(y * width + x) * 4 + channel] = (sum / window) as u8;
+ sum += at(y as isize + radius as isize + 1);
+ sum -= at(y as isize - radius as isize);
+ }
+ }
+ }
+ }
+}
+
+/// Paints `style` onto a `width` × `height` transparent canvas, the block
+/// centred, and returns it PNG-encoded with the block's size.
+pub fn render(
+ fonts: &Fonts,
+ style: &TitleStyle,
+ width: u32,
+ height: u32,
+) -> Result<Rendered, Error> {
+ let mut canvas = Pixmap::new(width, height).ok_or(Error::Canvas(width, height))?;
+ let frame_h = height as f32;
+ let em = (style.font_size.clamp(0.005, 1.0) as f32) * frame_h;
+ let tracking = style.tracking as f32 * frame_h;
+ let pitch = em * (style.line_height.max(0.5) as f32);
+
+ let blob = fonts.pick(style)?;
+ let (index_bytes, data) = blob.split_at(4);
+ let index = u32::from_le_bytes([
+ index_bytes[0],
+ index_bytes[1],
+ index_bytes[2],
+ index_bytes[3],
+ ]);
+ let face = rustybuzz::Face::from_slice(data, index).ok_or(Error::BadFont)?;
+ let upem = face.units_per_em() as f32;
+ let ascent = face.ascender() as f32 / upem * em;
+ let descent = -(face.descender() as f32) / upem * em;
+
+ // Shape every line with the pen at the origin; placement comes after,
+ // once the block's width is known.
+ let lines: Vec<Line> = style
+ .content
+ .lines()
+ .map(|line| shape_line(&face, line, em, tracking))
+ .collect();
+ let rows = lines.len().max(1);
+ let block_w = lines.iter().map(|line| line.width).fold(0.0, f32::max);
+ let block_h = (rows as f32 - 1.0) * pitch + ascent + descent;
+ if block_w <= 0.0 || lines.is_empty() {
+ // Nothing to paint: an empty, valid canvas.
+ let png = canvas
+ .encode_png()
+ .map_err(|error| Error::Encode(error.to_string()))?;
+ return Ok(Rendered {
+ png,
+ width,
+ height,
+ block_width: 0,
+ block_height: 0,
+ });
+ }
+
+ // The plate's padding is part of the block: it is what a monitor should
+ // outline, and what the title's neighbours should keep clear of.
+ let plate = colour(&style.background);
+ let (pad_x, pad_y) = if plate.is_some() {
+ (em * 0.35, em * 0.2)
+ } else {
+ (0.0, 0.0)
+ };
+ let outer_w = block_w + 2.0 * pad_x;
+ let outer_h = block_h + 2.0 * pad_y;
+ let left = (width as f32 - outer_w) / 2.0 + pad_x;
+ let top = (frame_h - outer_h) / 2.0 + pad_y;
+
+ // One path for all the words, placed. Each line is aligned within the
+ // block's width and sits on its own baseline.
+ let mut words = PathBuilder::new();
+ for (row, line) in lines.iter().enumerate() {
+ let Some(path) = &line.path else { continue };
+ let indent = match style.align {
+ Align::Left => 0.0,
+ Align::Center => (block_w - line.width) / 2.0,
+ Align::Right => block_w - line.width,
+ };
+ let baseline = top + ascent + row as f32 * pitch;
+ let placed = path
+ .clone()
+ .transform(Transform::from_translate(left + indent, baseline))
+ .expect("a translated glyph path stays finite");
+ words.push_path(&placed);
+ }
+ let Some(words) = words.finish() else {
+ let png = canvas
+ .encode_png()
+ .map_err(|error| Error::Encode(error.to_string()))?;
+ return Ok(Rendered {
+ png,
+ width,
+ height,
+ block_width: outer_w.round() as u32,
+ block_height: outer_h.round() as u32,
+ });
+ };
+
+ let mut paint = Paint {
+ anti_alias: true,
+ ..Paint::default()
+ };
+
+ // The plate, first and under everything.
+ if let Some(fill) = plate
+ && let Some(rect) = Rect::from_xywh(left - pad_x, top - pad_y, outer_w, outer_h)
+ {
+ paint.set_color(fill);
+ let radius = em * 0.15;
+ let mut plate_path = PathBuilder::new();
+ push_rounded_rect(&mut plate_path, rect, radius);
+ if let Some(plate_path) = plate_path.finish() {
+ canvas.fill_path(
+ &plate_path,
+ &paint,
+ FillRule::Winding,
+ Transform::identity(),
+ None,
+ );
+ }
+ }
+
+ // The shadow: the words again, offset down and right, black, blurred on
+ // a layer of their own and laid under the real words.
+ if style.shadow
+ && let Some(mut layer) = Pixmap::new(width, height)
+ {
+ let mut shade = Paint {
+ anti_alias: true,
+ ..Paint::default()
+ };
+ shade.set_color(Color::from_rgba8(0, 0, 0, 150));
+ let offset = Transform::from_translate(em * 0.05, em * 0.07);
+ layer.fill_path(&words, &shade, FillRule::Winding, offset, None);
+ blur(&mut layer, (em * 0.04).round().max(1.0) as usize);
+ canvas.draw_pixmap(
+ 0,
+ 0,
+ layer.as_ref(),
+ &PixmapPaint::default(),
+ Transform::identity(),
+ None,
+ );
+ }
+
+ // The outline, under the fill so only its outer half shows - which is
+ // why it is drawn at twice the asked-for width.
+ let stroke_w = style.stroke_width.max(0.0) as f32 * frame_h;
+ if stroke_w > 0.0
+ && let Some(edge) = colour(&style.stroke_color)
+ {
+ paint.set_color(edge);
+ let stroke = Stroke {
+ width: stroke_w * 2.0,
+ line_join: LineJoin::Round,
+ line_cap: LineCap::Round,
+ ..Stroke::default()
+ };
+ canvas.stroke_path(&words, &paint, &stroke, Transform::identity(), None);
+ }
+
+ // The words.
+ paint.set_color(colour(&style.color).unwrap_or(Color::WHITE));
+ canvas.fill_path(
+ &words,
+ &paint,
+ FillRule::Winding,
+ Transform::identity(),
+ None,
+ );
+
+ let png = canvas
+ .encode_png()
+ .map_err(|error| Error::Encode(error.to_string()))?;
+ Ok(Rendered {
+ png,
+ width,
+ height,
+ block_width: outer_w.round() as u32,
+ block_height: outer_h.round() as u32,
+ })
+}
+
+/// A rectangle with rounded corners, radius clamped to half the short side.
+fn push_rounded_rect(builder: &mut PathBuilder, rect: Rect, radius: f32) {
+ let r = radius
+ .min(rect.width() / 2.0)
+ .min(rect.height() / 2.0)
+ .max(0.0);
+ let (l, t, rgt, b) = (rect.left(), rect.top(), rect.right(), rect.bottom());
+ // Quadratic corners: close enough to circular at these sizes, and half
+ // the segments of a cubic approximation.
+ builder.move_to(l + r, t);
+ builder.line_to(rgt - r, t);
+ builder.quad_to(rgt, t, rgt, t + r);
+ builder.line_to(rgt, b - r);
+ builder.quad_to(rgt, b, rgt - r, b);
+ builder.line_to(l + r, b);
+ builder.quad_to(l, b, l, b - r);
+ builder.line_to(l, t + r);
+ builder.quad_to(l, t, l + r, t);
+ builder.close();
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn style(content: &str) -> TitleStyle {
+ TitleStyle {
+ content: content.to_owned(),
+ font_family: "\"No Such Family\"".to_owned(),
+ font_size: 0.09,
+ font_weight: 700.0,
+ italic: false,
+ color: "#ffffff".to_owned(),
+ align: Align::Center,
+ stroke_width: 0.0,
+ stroke_color: "#000000".to_owned(),
+ shadow: true,
+ background: String::new(),
+ line_height: 1.2,
+ tracking: 0.0,
+ }
+ }
+
+ fn opaque_pixels(png: &[u8]) -> usize {
+ let pixmap = Pixmap::decode_png(png).expect("our own PNG decodes");
+ pixmap.pixels().iter().filter(|p| p.alpha() > 0).count()
+ }
+
+ #[test]
+ fn colours_parse_both_lengths() {
+ assert_eq!(colour("#ff0000"), Some(Color::from_rgba8(255, 0, 0, 255)));
+ assert_eq!(colour("#00ff0080"), Some(Color::from_rgba8(0, 255, 0, 128)));
+ assert_eq!(colour(""), None);
+ assert_eq!(colour("red"), None);
+ }
+
+ /// A missing family falls back to a system face and still paints words.
+ #[test]
+ fn a_title_paints_something_with_a_fallback_face() {
+ let fonts = Fonts::new();
+ let out = render(&fonts, &style("Hello"), 640, 360).expect("renders");
+ assert_eq!((out.width, out.height), (640, 360));
+ assert!(out.block_width > 0 && out.block_height > 0);
+ assert!(out.block_width < 640);
+ assert!(opaque_pixels(&out.png) > 100);
+ }
+
+ /// The canvas is the frame; the block is not.
+ #[test]
+ fn empty_content_is_an_empty_canvas() {
+ let fonts = Fonts::new();
+ let out = render(&fonts, &style(""), 320, 180).expect("renders");
+ assert_eq!((out.block_width, out.block_height), (0, 0));
+ assert_eq!(opaque_pixels(&out.png), 0);
+ }
+
+ /// More lines, taller block; a plate grows it further.
+ #[test]
+ fn lines_and_plates_grow_the_block() {
+ let fonts = Fonts::new();
+ let one = render(&fonts, &style("One"), 640, 360).expect("renders");
+ let two = render(&fonts, &style("One\nTwo"), 640, 360).expect("renders");
+ assert!(two.block_height > one.block_height);
+ let mut plated = style("One");
+ plated.background = "#000000cc".to_owned();
+ let plated = render(&fonts, &plated, 640, 360).expect("renders");
+ assert!(plated.block_width > one.block_width);
+ assert!(plated.block_height > one.block_height);
+ }
+}
diff --git a/engine/crates/concat/src/host.rs b/engine/crates/concat/src/host.rs
index 11e4b72c..6c38590d 100644
--- a/engine/crates/concat/src/host.rs
+++ b/engine/crates/concat/src/host.rs
@@ -39,6 +39,8 @@ pub struct Host {
pub transcriber: Arc<Transcriber>,
/// Kokoro, and its model downloads.
pub speech: Arc<Speech>,
+ /// Titles painted to pictures, and the cache of them.
+ pub titles: concat_host::Titles,
}
impl Host {
@@ -50,6 +52,7 @@ impl Host {
let dirs = AppDirs::locate()?;
let _ = std::fs::create_dir_all(&dirs.config);
Ok(Host {
+ titles: concat_host::Titles::new(&dirs),
dirs,
playback: Playback::start(Arc::new(Events)),
monitor: match gpu {
diff --git a/engine/crates/concat/src/studio.rs b/engine/crates/concat/src/studio.rs
index b14af2c6..b744ae4a 100644
--- a/engine/crates/concat/src/studio.rs
+++ b/engine/crates/concat/src/studio.rs
@@ -514,6 +514,10 @@ pub struct Studio {
pub gesture: Gesture,
/// The snap lines of a stage move in flight; empty between moves.
pub stage_guides: Vec<StageGuideData>,
+ /// Each title's painted block in frame pixels, by clip id, as of the
+ /// last time the monitor asked for a frame. What the stage box for a
+ /// text clip is drawn from; see `footprint`.
+ pub title_blocks: HashMap<String, (u32, u32)>,
pub drop: Option<DropPlan>,
}
@@ -627,6 +631,7 @@ impl Studio {
divider_press: None,
gesture: Gesture::None,
stage_guides: Vec::new(),
+ title_blocks: HashMap::new(),
drop: None,
host,
};
@@ -942,8 +947,13 @@ impl Studio {
// The echo when there is one: a picture being dragged on the stage
// is drawn where the pointer has it, not where the document last
// had it. Same flattening the session does for itself.
- let clips = concat_export::flatten::flatten_timeline(self.project(), None);
+ let mut clips = concat_export::flatten::flatten_timeline(self.project(), None);
let (width, height) = self.output_size();
+ // Titles, painted to pictures and rejoined; see concat-host's titles.
+ for title in self.host.titles.clips(self.project(), width, height) {
+ self.title_blocks.insert(title.clip_id, title.block);
+ clips.push(title.clip);
+ }
let scale = match self.quality {
0 => 1.0,
1 => 0.5,
@@ -1931,11 +1941,20 @@ impl Studio {
pub fn footprint(&self, clip: &Clip) -> Footprint {
let (width, height) = self.output_size();
let (width, height) = (f64::from(width.max(1)), f64::from(height.max(1)));
- let (w, h) = if clip.kind == model::ClipKind::Text {
- // The engine does not report where a title's glyphs landed, so
- // this is a reading of the style: an em is `font_size` of the
- // frame's height, a glyph runs about six tenths of one, and a
- // little air is left around the block the way the plate would.
+ let painted = (clip.kind == model::ClipKind::Text)
+ .then(|| self.title_blocks.get(&clip.id).copied())
+ .flatten();
+ let (w, h) = if let Some((bw, bh)) = painted {
+ // The painter said how big the block came out.
+ (
+ f64::from(bw) * clip.scale / width,
+ f64::from(bh) * clip.scale / height,
+ )
+ } else if clip.kind == model::ClipKind::Text {
+ // Not painted yet: a reading of the style until it is. An em is
+ // `font_size` of the frame's height, a glyph runs about six
+ // tenths of one, and a little air is left around the block the
+ // way the plate would.
let text = clip.text.clone().unwrap_or_default();
let lines: Vec<&str> = text.content.lines().collect();
let rows = lines.len().max(1) as f64;
@@ -2617,7 +2636,15 @@ impl Studio {
crf: EXPORT_CRF[self.export.quality.min(2)],
preset: "medium".into(),
};
- let mut request = export::request(session, &spec, Vec::new());
+ let (frame_w, frame_h) = self.output_size();
+ let titles = self
+ .host
+ .titles
+ .clips(session.project(), frame_w, frame_h)
+ .into_iter()
+ .map(|title| title.clip)
+ .collect();
+ let mut request = export::request(session, &spec, titles);
let (width, height) = EXPORT_SIZES[self.export.resolution.min(3)];
let (num, den) = EXPORT_RATES[self.export.rate.min(2)];
request.width = width;