The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.
Rust ffmpegfree-video-editormodel-context-protocolnon-linear-editoroffline-firstopen-source-video-editorself-hostedvideo-editingvideo-editing-automationvideo-editing-softwarevideo-editing-toolsvideo-editorvoice-cloningyoutube-automationyoutube-shorts
feat(text): a title is a box, sized by width and height, and never stretched
Jareerc486fe7a1f3c
diff --git a/src/crates/concat-host/src/titles.rs b/src/crates/concat-host/src/titles.rsindex 27e55c2e..2fe6d315 100644--- a/src/crates/concat-host/src/titles.rs+++ b/src/crates/concat-host/src/titles.rs@@ -155,8 +155,14 @@ impl Titles {offset_x: clip.offset_x,offset_y: clip.offset_y,rotation: clip.rotation,- stretch_x: clip.stretch_x,- stretch_y: clip.stretch_y,+ // A title is never pulled along an axis: its box is+ // sized by the style, and its glyphs keep their shape.+ // 0.2.2's side grips wrote a stretch onto title clips+ // to make a long caption fit, and honouring it here+ // kept those captions squashed after the wrap arrived.+ // https://github.com/jub0t/Concat/issues/119+ stretch_x: 1.0,+ stretch_y: 1.0,// 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),@@ -387,6 +393,7 @@ fn title_style(style: &TextStyle) -> TitleStyle {background: style.background.clone(),line_height: style.line_height,max_width: style.max_width,+ max_height: style.max_height,tracking: style.tracking,}}@@ -405,6 +412,52 @@ mod tests {}}+ /// A title clip carrying a stretch - what 0.2.2's side grips wrote to+ /// make a long caption fit - comes back unstretched: the words wrap+ /// now, and a squashed glyph was never wanted.+ /// https://github.com/jub0t/Concat/issues/119+ #[test]+ fn a_title_is_never_stretched_whatever_its_clip_says() {+ let dirs = scratch();+ let mut editor = Editor::new();+ let id = editor+ .apply(Command::AddTextClip {+ above: false,+ track_id: None,+ start: 0.0,+ style: None,+ duration: Some(3.0),+ offset_y: None,+ })+ .expect("a title is added")+ .created_id+ .expect("with an id");+ editor+ .apply(Command::SetClipTransform {+ clip_id: id.clone(),+ scale: Some(1.03),+ offset_x: None,+ offset_y: None,+ rotation: None,+ stretch_x: Some(0.46),+ stretch_y: Some(2.2),+ })+ .expect("the stretch a 0.2.2 grip left behind");+ let clip = editor.project().active().clip(&id).expect("kept");+ assert_eq!(+ (clip.stretch_x, clip.stretch_y),+ (0.46, 2.2),+ "it is on the clip"+ );++ let out = Titles::new(&dirs).clips(editor.project(), 640, 360);+ assert_eq!((out[0].clip.stretch_x, out[0].clip.stretch_y), (1.0, 1.0));+ assert_eq!(+ out[0].clip.scale, 1.03,+ "the scale is honoured; only the stretch is not"+ );+ }+/// 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.diff --git a/src/crates/concat-project/src/commands/mod.rs b/src/crates/concat-project/src/commands/mod.rsindex d5c5a675..dda39cde 100644--- a/src/crates/concat-project/src/commands/mod.rs+++ b/src/crates/concat-project/src/commands/mod.rs@@ -853,6 +853,7 @@ impl Command {style.line_height,style.tracking,style.max_width,+ style.max_height,])})}@@ -907,6 +908,7 @@ impl Command {style.line_height,style.tracking,style.max_width,+ style.max_height,])})}diff --git a/src/crates/concat-project/src/lib.rs b/src/crates/concat-project/src/lib.rsindex 5375c425..1cc980b8 100644--- a/src/crates/concat-project/src/lib.rs+++ b/src/crates/concat-project/src/lib.rs@@ -580,6 +580,28 @@ mod tests {}}+ /// A document from before the box had a height reads as no height,+ /// and a hand-edited one cannot make it negative or not a number.+ /// https://github.com/jub0t/Concat/issues/119+ #[test]+ fn a_text_style_without_a_box_height_reads_as_the_words_own() {+ let style: TextStyle =+ serde_json::from_value(json!({ "content": "Hello" })).expect("parses");+ assert_eq!(style.max_height, 0.0);+ let odd = TextStyle {+ max_height: -3.0,+ ..TextStyle::default()+ }+ .tidy();+ assert_eq!(odd.max_height, 0.0);+ let nan = TextStyle {+ max_height: f64::NAN,+ ..TextStyle::default()+ }+ .tidy();+ assert_eq!(nan.max_height, 0.0);+ }+#[test]fn a_new_project_has_one_timeline_and_four_lanes() {let editor = Editor::new();diff --git a/src/crates/concat-project/src/model.rs b/src/crates/concat-project/src/model.rsindex dc9c24a0..26497c48 100644--- a/src/crates/concat-project/src/model.rs+++ b/src/crates/concat-project/src/model.rs@@ -882,8 +882,17 @@ pub struct TextStyle {pub tracking: f64,/// The widest a line may run, as a fraction of frame width, before its/// words wrap onto the next; the stage's side grips set it. Zero - the- /// default - is no limit: a line is as long as its words.+ /// default - is no limit: a line is as long as its words. With a limit+ /// the block is a box exactly that wide, plate included, and the lines+ /// align inside it.pub max_width: f64,+ /// The box's height as a fraction of frame height; the stage's top and+ /// bottom grips set it, and the inspector's Height field. Zero - the+ /// default - is the words' own height. With a height the block is a+ /// box exactly that tall, the words centred in it, so a lower third+ /// can be a band of a fixed size whatever is written on it.+ /// https://github.com/jub0t/Concat/issues/119+ pub max_height: f64,}impl TextStyle {@@ -902,6 +911,7 @@ impl TextStyle {self.line_height = finite(self.line_height, base.line_height).max(0.5);self.tracking = finite(self.tracking, base.tracking);self.max_width = finite(self.max_width, base.max_width).max(0.0);+ self.max_height = finite(self.max_height, base.max_height).max(0.0);self}}@@ -924,6 +934,7 @@ impl Default for TextStyle {line_height: 1.2,tracking: 0.0,max_width: 0.0,+ max_height: 0.0,}}}diff --git a/src/crates/concat-text/src/lib.rs b/src/crates/concat-text/src/lib.rsindex a5357e24..16e3e36e 100644--- a/src/crates/concat-text/src/lib.rs+++ b/src/crates/concat-text/src/lib.rs@@ -82,8 +82,14 @@ pub struct TitleStyle {/// Extra advance after every glyph, as a fraction of frame height.pub tracking: f64,/// The widest a line may run, as a fraction of frame width, before its- /// words wrap; zero for no limit.+ /// words wrap; zero for no limit. With a limit the block is a box that+ /// wide - a word no line can hold still widens it - and the lines+ /// align inside the box rather than against each other.pub max_width: f64,+ /// The box's height as a fraction of frame height; zero for the words'+ /// own. With a height the block is a box that tall, at least, and the+ /// words sit centred in it.+ pub max_height: f64,}/// The finished title as pixels: the canvas, RGBA with straight alpha,@@ -536,23 +542,44 @@ fn paint(.flat_map(|line| wrap_line(&face, line, em, tracking, max_w)).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() {+ let words_w = lines.iter().map(|line| line.width).fold(0.0, f32::max);+ let words_h = (rows as f32 - 1.0) * pitch + ascent + descent;+ if words_w <= 0.0 || lines.is_empty() {// Nothing to paint: an empty, valid canvas.return Ok((canvas, (0, 0, 0, 0)));}+ // The box the words sit in. Sized by the style where the style says,+ // and by the words where it does not; never smaller than the words,+ // so a word no line can hold is still whole. A sized axis is exact -+ // a box asked for at 400 by 120 is 400 by 120, plate and all - so the+ // plate's air is only added on an axis the words size themselves.+ let max_h = (style.max_height as f32) * frame_h;+ let box_w = if max_w > 0.0 {+ max_w.max(words_w)+ } else {+ words_w+ };+ let box_h = if max_h > 0.0 {+ max_h.max(words_h)+ } else {+ words_h+ };// 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)+ let pad_x = if plate.is_some() && max_w <= 0.0 {+ em * 0.35+ } else {+ 0.0+ };+ let pad_y = if plate.is_some() && max_h <= 0.0 {+ em * 0.2} else {- (0.0, 0.0)+ 0.0};- let outer_w = block_w + 2.0 * pad_x;- let outer_h = block_h + 2.0 * pad_y;+ let outer_w = box_w + 2.0 * pad_x;+ let outer_h = box_h + 2.0 * pad_y;// The anchor is the canvas's centre - where the clip's position lands -// and the alignment says which edge of the block sits on it, plate and// all. Growing words then push the far edge and leave the anchored one@@ -564,18 +591,19 @@ fn paint(Align::Right => anchor_x - outer_w,};let left = outer_left + pad_x;- let top = (frame_h - outer_h) / 2.0 + pad_y;+ // The words are centred in a box taller than they are.+ let top = (frame_h - outer_h) / 2.0 + pad_y + (box_h - words_h) / 2.0;let block_dx = (outer_left + outer_w / 2.0 - anchor_x).round() as i32;// One path for all the words, placed. Each line is aligned within the- // block's width and sits on its own baseline.+ // box'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,+ Align::Center => (box_w - line.width) / 2.0,+ Align::Right => box_w - line.width,};let baseline = top + ascent + row as f32 * pitch;let placed = path@@ -596,9 +624,9 @@ fn paint(..Paint::default()};- // The plate, first and under everything.+ // The plate, first and under everything: the whole box.if let Some(fill) = plate- && let Some(rect) = Rect::from_xywh(left - pad_x, top - pad_y, outer_w, outer_h)+ && let Some(rect) = Rect::from_xywh(outer_left, (frame_h - outer_h) / 2.0, outer_w, outer_h){paint.set_color(fill);let radius = em * 0.15;@@ -711,6 +739,7 @@ mod tests {line_height: 1.2,tracking: 0.0,max_width: 0.0,+ max_height: 0.0,}}@@ -763,6 +792,105 @@ mod tests {(left, right)}+ /// The topmost and bottommost rows holding any paint.+ fn painted_rows(png: &[u8]) -> (u32, u32) {+ let pixmap = Pixmap::decode_png(png).expect("our own PNG decodes");+ let width = pixmap.width();+ let mut top = pixmap.height();+ let mut bottom = 0;+ for (index, pixel) in pixmap.pixels().iter().enumerate() {+ if pixel.alpha() > 0 {+ let y = index as u32 / width;+ top = top.min(y);+ bottom = bottom.max(y);+ }+ }+ (top, bottom)+ }++ // ── the box: https://github.com/jub0t/Concat/issues/119 ──++ /// A box sized by the style is exactly that size, plate and all, and+ /// the words sit centred inside it.+ #[test]+ fn a_sized_box_is_exactly_that_size_with_the_words_centred_in_it() {+ let fonts = Fonts::new();+ let mut plated = style("hi");+ plated.max_width = 0.5;+ plated.max_height = 0.4;+ plated.background = "#000000ff".to_owned();+ plated.shadow = false;+ let rendered = render(&fonts, &plated, 640, 360).expect("renders");+ assert_eq!((rendered.block_width, rendered.block_height), (320, 144));+ // The plate is the box: half the frame wide, centred, no air added.+ let (left, right) = painted_span(&rendered.png);+ assert_eq!((left, right), (160, 479), "the plate's columns");+ let (top, bottom) = painted_rows(&rendered.png);+ assert_eq!((top, bottom), (108, 251), "the plate's rows");++ // Without the plate the box is the same size, and the words are in+ // the middle of it: narrower than it, and centred on its centre.+ let mut bare = plated.clone();+ bare.background = String::new();+ let rendered = render(&fonts, &bare, 640, 360).expect("renders");+ assert_eq!((rendered.block_width, rendered.block_height), (320, 144));+ let (left, right) = painted_span(&rendered.png);+ assert!(+ left > 160 && right < 479,+ "{left}..{right} is inside the box"+ );+ let (top, bottom) = painted_rows(&rendered.png);+ let middle = f64::from(top + bottom) / 2.0;+ assert!(+ (middle - 180.0).abs() < 8.0,+ "the words are centred in the box: rows {top}..{bottom}"+ );+ assert!(top > 108 && bottom < 251, "and inside it");+ }++ /// A box narrower or shorter than the words grows to hold them: a word+ /// no line can hold is never cut, and never overflows the plate.+ #[test]+ fn a_box_smaller_than_the_words_grows_to_hold_them() {+ let fonts = Fonts::new();+ let mut tiny = style("unbreakable");+ tiny.max_width = 0.01;+ tiny.max_height = 0.01;+ tiny.background = "#000000ff".to_owned();+ tiny.shadow = false;+ let rendered = render(&fonts, &tiny, 640, 360).expect("renders");+ assert!(rendered.block_width > 6, "{}", rendered.block_width);+ assert!(rendered.block_height > 4, "{}", rendered.block_height);+ let (left, right) = painted_span(&rendered.png);+ assert!(+ right - left <= rendered.block_width,+ "nothing paints outside the box: {left}..{right} in {}",+ rendered.block_width+ );+ }++ /// A left-aligned box keeps its left edge on the anchor whatever its+ /// width, and a right-aligned one its right edge: sizing the box must+ /// not walk the words.+ #[test]+ fn a_sized_box_keeps_its_anchored_edge() {+ let fonts = Fonts::new();+ for (align, dx) in [(Align::Left, 160), (Align::Right, -160)] {+ let mut sized = style("hi");+ sized.align = align;+ sized.max_width = 0.5;+ sized.shadow = false;+ let rendered = render(&fonts, &sized, 640, 360).expect("renders");+ assert_eq!(rendered.block_width, 320);+ assert_eq!(rendered.block_dx, dx, "{align:?}");+ let (left, right) = painted_span(&rendered.png);+ match align {+ Align::Left => assert!(left >= 320 && right < 640, "{left}..{right}"),+ _ => assert!(right <= 320 && left > 0, "{left}..{right}"),+ }+ }+ }+#[test]fn colours_parse_both_lengths() {assert_eq!(colour("#ff0000"), Some(Color::from_rgba8(255, 0, 0, 255)));diff --git a/src/crates/concat/src/format.rs b/src/crates/concat/src/format.rsindex f9e5156f..47be38a9 100644--- a/src/crates/concat/src/format.rs+++ b/src/crates/concat/src/format.rs@@ -253,6 +253,17 @@ pub fn colour_of(hex: &str) -> slint::Color {}}+/// "#rrggbb" when opaque, else "#rrggbbaa" - at zero too. For a colour+/// whose alpha is a dial of its own, like a stroke's: an opacity turned+/// down to nothing must not take the colour with it, or turning it back+/// up brings back black.+pub fn hex_rgba(colour: slint::Color) -> String {+ match colour.alpha() {+ 255 => hex_of(colour),+ alpha => format!("{}{alpha:02x}", hex_of(colour)),+ }+}+/// The inverse of [`colour_of`]: "#rrggbb", "#rrggbbaa" when translucent,/// and an empty string for fully transparent.pub fn hex_with_alpha(colour: slint::Color) -> String {diff --git a/src/crates/concat/src/presets.rs b/src/crates/concat/src/presets.rsindex 66b70ed7..821f91b6 100644--- a/src/crates/concat/src/presets.rs+++ b/src/crates/concat/src/presets.rs@@ -84,6 +84,7 @@ struct PresetStyle {line_height: Option<f64>,tracking: Option<f64>,max_width: Option<f64>,+ max_height: Option<f64>,}impl PresetStyle {@@ -114,6 +115,7 @@ impl PresetStyle {line_height: self.line_height.unwrap_or(base.line_height).max(0.5),tracking: self.tracking.unwrap_or(0.0),max_width: self.max_width.unwrap_or(0.0).max(0.0),+ max_height: self.max_height.unwrap_or(0.0).max(0.0),}}}diff --git a/src/crates/concat/src/studio.rs b/src/crates/concat/src/studio.rsindex 48da4035..2848f8a1 100644--- a/src/crates/concat/src/studio.rs+++ b/src/crates/concat/src/studio.rs@@ -47,7 +47,8 @@ use crate::dock::{Dock, DockLayout, SEAT_GAP, default_dock, lay_out, nearest_row, row_at, row_top,};use crate::format::{- colour_of, frames_timecode, hex_of, hex_with_alpha, wave_columns, wave_path, when_phrase,+ colour_of, frames_timecode, hex_of, hex_rgba, hex_with_alpha, wave_columns, wave_path,+ when_phrase,};use crate::host::{CachedStrip, Host, MediaArt, WindowArt, cached_media_art, cached_window_art, image_at,@@ -293,6 +294,16 @@ pub enum Gesture {/// The title's turn, to project the pointer onto its axis.rotation: f64,},+ /// The top or bottom grip on a title pulls the height of its box, on+ /// the same terms as [`Gesture::TextWidth`] pulls the width. Before+ /// this the two grips fell through to a stretch, and a caption pulled+ /// to fit came out with its glyphs squashed.+ /// https://github.com/jub0t/Concat/issues/119+ TextHeight {+ clip: String,+ centre: (f64, f64),+ rotation: f64,+ },/// The rotation grip dragged: the picture turns by the angle the pointer/// has swept about the centre since the press.StageRotate {@@ -2815,6 +2826,7 @@ impl Studio {| Gesture::StageRotate { .. }| Gesture::StageStretch { .. }| Gesture::TextWidth { .. }+ | Gesture::TextHeight { .. }| Gesture::Paint { .. } => {}}self.gesture = gesture;@@ -2881,6 +2893,7 @@ impl Studio {| Gesture::StageRotate { .. }| Gesture::StageStretch { .. }| Gesture::TextWidth { .. }+ | Gesture::TextHeight { .. }| Gesture::Paint { .. }) => {self.gesture = other;}@@ -3030,6 +3043,17 @@ impl Studio {ClipField::LineHeight => text.line_height = value.clamp(0.7, 2.5),ClipField::Tracking => text.tracking = value.clamp(-0.05, 0.3),ClipField::TextWidth => text.max_width = value.clamp(0.0, 2.0),+ ClipField::TextHeight => text.max_height = value.clamp(0.0, 2.0),+ // The stroke's opacity is its colour's alpha; see `hex_rgba`.+ ClipField::StrokeOpacity => {+ let edge = colour_of(&text.stroke_color);+ text.stroke_color = hex_rgba(slint::Color::from_argb_u8(+ (value.clamp(0.0, 1.0) * 255.0).round() as u8,+ edge.red(),+ edge.green(),+ edge.blue(),+ ));+ }}// A media clip has no text; the placeholder must not linger.if clip.kind != model::ClipKind::Text {@@ -3083,7 +3107,7 @@ impl Studio {let text = clip.text.get_or_insert_with(TextStyle::default);match field {ClipTextField::Color => text.color = hex_of(value),- ClipTextField::StrokeColor => text.stroke_color = hex_of(value),+ ClipTextField::StrokeColor => text.stroke_color = hex_rgba(value),ClipTextField::Background => text.background = hex_with_alpha(value),_ => {}}@@ -3348,8 +3372,15 @@ impl Studio {None => (clip.scale, clip.scale),}};- // Then pulled along each axis, as the compositor pulls it.- let (w, h) = (w * clip.stretch_x, h * clip.stretch_y);+ // Then pulled along each axis, as the compositor pulls it - a+ // picture, that is. A title's box is its style's, and the compositor+ // never stretches one; see titles.rs.+ // https://github.com/jub0t/Concat/issues/119+ let (w, h) = if clip.kind == model::ClipKind::Text {+ (w, h)+ } else {+ (w * clip.stretch_x, h * clip.stretch_y)+ };// The placement at the playhead: the clip's own, moved by its// animation, so the box follows a slide or a spin.let base = concat_core::timeline::Transform {@@ -3566,6 +3597,14 @@ impl Studio {centre,rotation: clip.rotation,}+ } else if (grip == 5 || grip == 7) && clip.kind == model::ClipKind::Text {+ // And the top and bottom grips size its box, never its glyphs.+ // https://github.com/jub0t/Concat/issues/119+ Gesture::TextHeight {+ clip: id.to_owned(),+ centre,+ rotation: clip.rotation,+ }} else if grip >= 5 {// 5 top, 6 right, 7 bottom, 8 left: the pointer's offset from// the centre, turned back into the box's own frame.@@ -3807,6 +3846,24 @@ impl Studio {clip.text.get_or_insert_with(TextStyle::default).max_width = next;}}+ Gesture::TextHeight {+ clip,+ centre,+ rotation,+ } => {+ let dx = x * f64::from(width) - centre.0;+ let dy = y * f64::from(height) - centre.1;+ let (sin, cos) = rotation.to_radians().sin_cos();+ // The pointer's reach across the box's own axis.+ let across = (-dx * sin + dy * cos).abs();+ let mut next = (2.0 * across / f64::from(height)).clamp(0.03, 2.0);+ if snap {+ next = (next * 20.0).round() / 20.0;+ }+ if let Some(clip) = self.echo_clip_mut(clip) {+ clip.text.get_or_insert_with(TextStyle::default).max_height = next;+ }+ }Gesture::StageRotate {clip,rotation,@@ -5354,6 +5411,7 @@ impl Studio {stroke_width: text.stroke_width as f32,stroke,stroke_hex: hex_of(stroke).into(),+ stroke_opacity: f32::from(stroke.alpha()) / 255.0,shadow: text.shadow,plate,plate_hex: hex_of(plate).into(),@@ -5361,6 +5419,7 @@ impl Studio {line_height: text.line_height as f32,tracking: text.tracking as f32,text_width: text.max_width as f32,+ text_height: text.max_height as f32,cutout: match &clip.cutout {None => 0,Some(cutout) if cutout.mode == model::CutoutMode::Auto => 1,diff --git a/src/crates/concat/ui/timeline/model.slint b/src/crates/concat/ui/timeline/model.slintindex e02fb956..3a56aa7f 100644--- a/src/crates/concat/ui/timeline/model.slint+++ b/src/crates/concat/ui/timeline/model.slint@@ -154,7 +154,8 @@ export enum ClipField {anim-in, anim-out, anim-combo, anim-in-duration, anim-out-duration,flip-h, flip-v, blend, crop-left, crop-top, crop-right, crop-bottom,font-size, font-weight, italic, text-opacity, align,- stroke-width, shadow, line-height, tracking, text-width,+ stroke-width, shadow, line-height, tracking, text-width, text-height,+ stroke-opacity,cutout-feather,/// which of the media's audio tracks the clip plays, as a row of/// Editor.audio-tracks@@ -322,6 +323,8 @@ export struct SelectedClipData {stroke-width: float,stroke: color,stroke-hex: string,+ /// the stroke colour's alpha, 0..1, as its own dial+ stroke-opacity: float,shadow: bool,/// the plate behind the words; fully transparent means no plateplate: color,@@ -332,6 +335,8 @@ export struct SelectedClipData {/// the widest a line runs before wrapping, as a fraction of the frame;/// zero for no limittext-width: float,+ /// the box's height as a fraction of the frame's; zero is the words' own+ text-height: float,}/// One picture under the playhead, as the monitor places it.