Skip to content

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

fix(text): a title's box and letters are no longer cut off at the frame's edges

Jareer268cd6013ff2
diff --git a/src/crates/concat-core/src/timeline.rs b/src/crates/concat-core/src/timeline.rs
index 0d91eee0..b23d70e2 100644
--- a/src/crates/concat-core/src/timeline.rs
+++ b/src/crates/concat-core/src/timeline.rs
@@ -104,6 +104,12 @@ pub struct Clip {
/// The shape that keeps part of the picture, when one does; its
/// numbers ride the animation's mask tracks.
pub mask: Option<ShapeMask>,
+ /// How many times the frame the picture is drawn for, along the axis
+ /// it is most: one - every picture but a title whose ink runs past the
+ /// frame's edges, painted on a canvas grown to hold it. A picture past
+ /// one is drawn at one of its pixels to one of the frame's, centred on
+ /// the clip's centre, where any other is fitted inside the frame.
+ pub overscan: f64,
}
/// How a layer's colour meets what is beneath it.
@@ -227,6 +233,7 @@ impl Clip {
blend: Blend::Normal,
hold: false,
mask: None,
+ overscan: 1.0,
}
}
diff --git a/src/crates/concat-export/src/lib.rs b/src/crates/concat-export/src/lib.rs
index f1c07efb..3f40e27a 100644
--- a/src/crates/concat-export/src/lib.rs
+++ b/src/crates/concat-export/src/lib.rs
@@ -206,6 +206,12 @@ pub struct ExportClip {
/// The source's pixel height; see `media_width`.
#[serde(default)]
pub media_height: Option<u32>,
+ /// How many times the frame the source is, for a picture drawn for
+ /// the frame at one pixel to one rather than fitted into it: a title
+ /// whose canvas grew past the frame to hold its ink. One for every
+ /// other clip. See `concat_core::timeline::Clip::overscan`.
+ #[serde(default = "unity")]
+ pub overscan: f64,
/// The source's length in seconds, when the UI knows it: how much
/// footage lies past the clip's out-point for a transition to run the
/// clip on into. Absent, a transition holds the clip's last frame
@@ -350,6 +356,7 @@ impl ExportClip {
hold: false,
media_width: None,
media_height: None,
+ overscan: 1.0,
media_duration: None,
has_audio: None,
cutout: None,
diff --git a/src/crates/concat-export/src/resolve.rs b/src/crates/concat-export/src/resolve.rs
index 6d723240..7ade9bc2 100644
--- a/src/crates/concat-export/src/resolve.rs
+++ b/src/crates/concat-export/src/resolve.rs
@@ -326,6 +326,7 @@ pub(crate) fn build_timeline(
engine_clip.animation = animation_of(&clip.animation);
engine_clip.blend = concat_core::timeline::Blend::parse(&clip.blend);
engine_clip.mask = clip.mask.map(|mask| mask.engine());
+ engine_clip.overscan = overscan_of(clip);
engine_clip.transform = Transform {
scale: clip.scale,
offset_x: clip.offset_x,
@@ -556,7 +557,8 @@ pub(crate) fn fitted_size(
};
let fit = (f64::from(request.width) / f64::from(media_width))
- .min(f64::from(request.height) / f64::from(media_height));
+ .min(f64::from(request.height) / f64::from(media_height))
+ * overscan_of(clip);
let (fit_width, fit_height) = if whole {
(source_width, source_height)
} else {
@@ -569,6 +571,16 @@ pub(crate) fn fitted_size(
Some((width & !1, height & !1))
}
+/// The clip's overscan, held to what it can mean: one, or more for a
+/// picture several frames across; a number that is not one is one.
+fn overscan_of(clip: &ExportClip) -> f64 {
+ if clip.overscan.is_finite() {
+ clip.overscan.max(1.0)
+ } else {
+ 1.0
+ }
+}
+
pub(crate) fn quantise(seconds: f64, rate: FrameRate) -> Rational {
rate.time_of_frame((seconds * rate.fps().as_f64()).round().max(0.0) as i64)
}
diff --git a/src/crates/concat-host/src/cards.rs b/src/crates/concat-host/src/cards.rs
index f816578e..eb93d48a 100644
--- a/src/crates/concat-host/src/cards.rs
+++ b/src/crates/concat-host/src/cards.rs
@@ -298,8 +298,11 @@ impl Painter {
Drawing::Title(pass) => {
let (title, reveal) = self.title(width, height)?;
let ground = PlannedLayer::picture(detached_clip(), Arc::clone(&self.still));
+ let overscan = (title.width() as f32 / width as f32)
+ .max(title.height() as f32 / height as f32);
let mut words = PlannedLayer::picture(detached_clip(), title);
words.track = 1;
+ words.overscan = overscan;
let mut pass = pass.clone();
pass.reveal_map = Some(reveal);
words.effects = vec![pass];
@@ -364,7 +367,13 @@ impl Painter {
.iter()
.map(|word| (word.x, word.y, word.width, word.height))
.collect();
- let reveal = Arc::new(RevealMap::from_rects(width, height, &rects));
+ // The canvas's size, which is the still's unless the words ran
+ // past its edges.
+ let reveal = Arc::new(RevealMap::from_rects(
+ rendered.width,
+ rendered.height,
+ &rects,
+ ));
let frame = Arc::new(
Frame::from_rgba(rendered.width, rendered.height, rendered.rgba)
.ok_or("the title came back the wrong size")?,
diff --git a/src/crates/concat-host/src/titles.rs b/src/crates/concat-host/src/titles.rs
index 56d6efb0..574ba403 100644
--- a/src/crates/concat-host/src/titles.rs
+++ b/src/crates/concat-host/src/titles.rs
@@ -66,6 +66,10 @@ enum Painting<'a> {
struct Art {
block: (u32, u32),
offset: (i32, i32),
+ /// The canvas the painting is on: the frame, or larger by the same
+ /// margin either side where the title's ink runs past the frame's
+ /// edges. See `concat_text::Rendered::overscan`.
+ canvas: (u32, u32),
/// The title's per-word reveal order, baked once here rather than on
/// every clip build - see `concat_core::RevealMap`.
reveal: Arc<RevealMap>,
@@ -308,8 +312,14 @@ impl Titles {
export_base(clip, KeyProperty::Opacity).clamp(0.0, 1.0)
}
},
- media_width: Some(width),
- media_height: Some(height),
+ // The canvas, which is the frame's size unless the
+ // title's ink runs past the frame - a box pulled wider,
+ // a word too long for a line - when it is larger and
+ // drawn at one to one, so the ink is shown rather than
+ // cut where the frame ends.
+ media_width: Some(art.canvas.0),
+ media_height: Some(art.canvas.1),
+ overscan: overscan_of(art.canvas, width, height),
has_audio: Some(false),
// A shape has no words to reveal one at a time.
reveal_map: match painting {
@@ -356,7 +366,8 @@ impl Titles {
let art = Art {
block: (rendered.block_width, rendered.block_height),
offset: (rendered.block_dx, rendered.block_dy),
- reveal: reveal_of(&rendered.words, width, height),
+ canvas: (rendered.width, rendered.height),
+ reveal: reveal_of(&rendered.words, rendered.width, rendered.height),
};
let frame = Arc::new(
Frame::from_rgba(rendered.width, rendered.height, rendered.rgba)
@@ -418,7 +429,8 @@ impl Titles {
let art = Art {
block: (rendered.block_width, rendered.block_height),
offset: (rendered.block_dx, rendered.block_dy),
- reveal: reveal_of(&rendered.words, width, height),
+ canvas: (rendered.width, rendered.height),
+ reveal: reveal_of(&rendered.words, rendered.width, rendered.height),
};
let words: Vec<String> = rendered
.words
@@ -428,11 +440,13 @@ impl Titles {
let _ = std::fs::write(
&side,
format!(
- "{{\"w\":{},\"h\":{},\"x\":{},\"y\":{},\"words\":[{}]}}",
+ "{{\"w\":{},\"h\":{},\"x\":{},\"y\":{},\"cw\":{},\"ch\":{},\"words\":[{}]}}",
art.block.0,
art.block.1,
art.offset.0,
art.offset.1,
+ art.canvas.0,
+ art.canvas.1,
words.join(",")
),
);
@@ -495,12 +509,14 @@ fn key_of(project: &Project, painting: Painting<'_>, width: u32, height: u32) ->
// blocks moved to their anchors. 4: the sidecar gained each word's box,
// for a per-word reveal effect to read back. 5: a title's lines are as
// tall as their biggest word, and each look is painted on its own.
- bytes.extend_from_slice(&5u32.to_le_bytes());
+ // 6: a canvas grows past the frame to hold ink that runs past it, and
+ // the sidecar says how big it is.
+ bytes.extend_from_slice(&6u32.to_le_bytes());
crate::media::fnv1a(&bytes)
}
-/// How many painted titles the directory keeps. Each is a frame-sized PNG,
-/// mostly transparent, so this is tens of megabytes at most; above it the
+/// How many painted titles the directory keeps. Each is a frame-sized PNG -
+/// larger for a title whose ink runs past the frame - mostly transparent, so this is tens of megabytes at most; above it the
/// oldest go, and a title that is still in use paints again.
const TITLES_KEPT: usize = 400;
@@ -548,16 +564,33 @@ fn read_block(side: &Path, width: u32, height: u32) -> Option<Art> {
))
})
.collect();
+ // The canvas's size, which is the frame's unless it is said.
+ let side_of = |name: &str, frame: u32| {
+ value
+ .get(name)
+ .and_then(|v| v.as_u64())
+ .map_or(frame, |v| v as u32)
+ };
+ let canvas = (side_of("cw", width), side_of("ch", height));
Some(Art {
block: (
value.get("w")?.as_u64()? as u32,
value.get("h")?.as_u64()? as u32,
),
offset: (offset("x"), offset("y")),
- reveal: Arc::new(RevealMap::from_rects(width, height, &rects)),
+ canvas,
+ reveal: Arc::new(RevealMap::from_rects(canvas.0, canvas.1, &rects)),
})
}
+/// How many times a `width` by `height` frame a painting's canvas is,
+/// along the axis it grew the most: one for a canvas that is the frame.
+fn overscan_of(canvas: (u32, u32), width: u32, height: u32) -> f64 {
+ (f64::from(canvas.0) / f64::from(width.max(1)))
+ .max(f64::from(canvas.1) / f64::from(height.max(1)))
+ .max(1.0)
+}
+
/// A title's per-word reveal map, baked from the boxes its layout left
/// behind - a byproduct `concat-text` already computed, not new work.
fn reveal_of(words: &[WordRect], width: u32, height: u32) -> Arc<RevealMap> {
@@ -890,6 +923,60 @@ mod tests {
let _ = std::fs::remove_dir_all(dirs.data.parent().unwrap());
}
+ /// A title whose box is pulled wider than the frame rejoins on a canvas
+ /// grown to hold it, said to be that many frames across so it is drawn
+ /// one to one; its reveal map is the canvas's size, and a painter
+ /// reading the sidecar cold, and the live painter, say the same.
+ #[test]
+ fn a_title_wider_than_the_frame_rejoins_on_its_grown_canvas() {
+ let dirs = scratch("a_title_wide");
+ let mut editor = Editor::new();
+ let style = concat_project::model::TextStyle {
+ content: "a box wider than the frame".to_owned(),
+ background: "#2040a0".to_owned(),
+ max_width: 1.5,
+ ..Default::default()
+ };
+ editor
+ .apply(Command::AddTextClip {
+ above: false,
+ track_id: None,
+ start: 0.0,
+ style: Some(style),
+ duration: Some(3.0),
+ offset_y: None,
+ })
+ .expect("a title is added");
+
+ let titles = Titles::new(&dirs);
+ let out = titles.clips(editor.project(), 640, 360);
+ let clip = &out[0].clip;
+ let (canvas_w, canvas_h) = (
+ clip.media_width.expect("a width"),
+ clip.media_height.expect("a height"),
+ );
+ assert!(canvas_w >= 960, "the box and its shadow: {canvas_w}");
+ assert_eq!((canvas_w - 640) % 2, 0, "the same margin either side");
+ let overscan = (f64::from(canvas_w) / 640.0).max(f64::from(canvas_h) / 360.0);
+ assert_eq!(clip.overscan, overscan);
+ let reveal = clip.reveal_map.as_ref().expect("a title reveals");
+ assert_eq!((reveal.width, reveal.height), (canvas_w, canvas_h));
+ assert_eq!(out[0].block.0, 960, "the block is the box, in frame pixels");
+
+ let cold = Titles::new(&dirs).clips(editor.project(), 640, 360);
+ assert_eq!(cold[0].clip.media_width, clip.media_width);
+ assert_eq!(cold[0].clip.overscan, clip.overscan);
+ assert_eq!(
+ cold[0].clip.reveal_map.as_ref().map(|r| (r.width, r.height)),
+ Some((canvas_w, canvas_h))
+ );
+ let live = titles.clips_live(editor.project(), 640, 360);
+ let frame = live[0].frame.as_ref().expect("painted live");
+ assert_eq!((frame.width(), frame.height()), (canvas_w, canvas_h));
+ assert_eq!(live[0].clip.overscan, clip.overscan);
+ let _ = std::fs::remove_dir_all(dirs.data.parent().unwrap());
+ }
+
/// More titles than the byte budget holds are all kept while the
/// timeline shows them: a second live pass paints none of them again.
/// Kept first in, first out, each was thrown out just before it was
@@ -941,6 +1028,7 @@ mod tests {
let art = || Art {
block: (1, 1),
offset: (0, 0),
+ canvas: (4, 4),
reveal: Arc::new(RevealMap::from_rects(1, 1, &[])),
};
let frame = || Arc::new(Frame::transparent(4, 4));
diff --git a/src/crates/concat-render/src/plan.rs b/src/crates/concat-render/src/plan.rs
index 69786aba..843b9289 100644
--- a/src/crates/concat-render/src/plan.rs
+++ b/src/crates/concat-render/src/plan.rs
@@ -294,6 +294,12 @@ pub struct PlannedLayer {
pub blend: Blend,
/// Blend strength over everything beneath, in `0.0..=1.0`.
pub opacity: f32,
+ /// How many times the frame the picture is, along the axis it is most:
+ /// one for every picture but a title painted on a canvas grown past the
+ /// frame to hold its ink. It is fitted this many times larger than the
+ /// frame would fit it, which for such a title is one of its pixels to
+ /// one of the frame's. See [`Geometry::of`].
+ pub overscan: f32,
}
impl PlannedLayer {
@@ -321,6 +327,7 @@ impl PlannedLayer {
transitions: Vec::new(),
blend: Blend::Normal,
opacity: 1.0,
+ overscan: 1.0,
}
}
@@ -612,7 +619,16 @@ impl Geometry {
) -> Geometry {
let source_rect = layer.crop.rect(source_width, source_height);
let [_, _, cw, ch] = source_rect;
- let fit = (width as f32 / cw).min(height as f32 / ch);
+ // A picture that is the frame several times over - a title whose
+ // canvas grew to hold its ink - fits that many times larger: at one
+ // to one, where the plain fit would shrink it into the frame. One,
+ // or anything that is not a number past it, is the plain fit.
+ let overscan = if layer.overscan.is_finite() {
+ layer.overscan.max(1.0)
+ } else {
+ 1.0
+ };
+ let fit = (width as f32 / cw).min(height as f32 / ch) * overscan;
let fitted = (
((cw * fit).round() as u32).max(1),
((ch * fit).round() as u32).max(1),
@@ -841,6 +857,7 @@ pub fn plan_frame(timeline: &Timeline, time: Rational) -> FramePlan {
// The fade ramp multiplies in here, so the compositor only ever
// sees a per-frame opacity - it has no idea fades exist.
opacity: (clip.opacity_at(time) * clip.video_fade_factor(time)).clamp(0.0, 1.0),
+ overscan: clip.overscan as f32,
});
}
@@ -1028,6 +1045,30 @@ mod tests {
assert_eq!(cropped.uv_of(1.0, 1.0, false, false), (0.75, 1.0));
}
+ /// A title's canvas grown past the frame is drawn at one to one,
+ /// centred where the frame's centre is - so the frame-sized middle of it
+ /// lands exactly where a frame-sized canvas would - and it keeps that
+ /// placement at any output size; one that did not grow fits as before.
+ #[test]
+ fn an_overscanned_picture_is_drawn_one_to_one_about_the_centre() {
+ // A 100 by 50 frame's canvas grown by 20 either side across and 5
+ // above and below: 140 by 60, 1.4 frames across.
+ let mut layer = PlannedLayer::picture(a_clip(), Arc::new(Frame::black(140, 60)));
+ layer.overscan = 1.4;
+ let own = Geometry::of(&layer, 140, 60, 100, 50);
+ assert_eq!(own.fitted, (140, 60));
+ assert_eq!(own.centre, (50.0, 25.0));
+ assert_eq!(own.corner(), (-20, -5));
+ assert!(own.fits_source() && own.is_aligned());
+ // At twice the size it is twice as big, still about the centre.
+ let doubled = Geometry::of(&layer, 140, 60, 200, 100);
+ assert_eq!(doubled.fitted, (280, 120));
+ assert_eq!(doubled.centre, (100.0, 50.0));
+ // A number that is not one is the plain fit, not a broken frame.
+ layer.overscan = f32::NAN;
+ assert_eq!(Geometry::of(&layer, 140, 60, 100, 50).fitted, (100, 43));
+ }
+
/// A picture covers the frame only drawn whole and solid over every
/// pixel of it: a wide picture in a wide frame does, in a tall one only
/// once it is scaled to fill, and never at half strength, turned, with
diff --git a/src/crates/concat-text/src/lib.rs b/src/crates/concat-text/src/lib.rs
index 7e1b0107..5ed06fdd 100644
--- a/src/crates/concat-text/src/lib.rs
+++ b/src/crates/concat-text/src/lib.rs
@@ -15,6 +15,15 @@
//! and puts the canvas'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.
//!
+//! Frame-sized at the least, that is. Ink past the frame's edges - a box
+//! pulled wider than the frame, a word too long for any line, the outline,
+//! shadow or lean of letters on a line as wide as the frame - would be cut
+//! off at the canvas's edge, and a title scaled down, moved or turned
+//! showed the cut. The canvas grows instead, by the same margin either
+//! side, so its centre stays the clip's centre; [`Rendered::overscan`] says
+//! by how much, for the compositor to draw it at one pixel to one rather
+//! than fit it into the frame.
+//!
//! Where the block sits on that canvas is the alignment's to say. A centred
//! title has its block centred, so the clip's position is the block's middle.
//! A left-aligned one has its block's left edge on the canvas's centre, a
@@ -162,10 +171,12 @@ pub struct WordRect {
pub struct RenderedFrame {
/// The canvas, `width` by `height` RGBA, alpha straight.
pub rgba: Vec<u8>,
- /// The canvas width: the frame's.
+ /// See [`Rendered::width`].
pub width: u32,
- /// The canvas height: the frame's.
+ /// See [`Rendered::height`].
pub height: u32,
+ /// See [`Rendered::overscan`].
+ pub overscan: f64,
/// See [`Rendered::block_width`].
pub block_width: u32,
/// See [`Rendered::block_height`].
@@ -183,10 +194,18 @@ pub struct RenderedFrame {
pub struct Rendered {
/// The canvas, PNG-encoded, RGBA with alpha.
pub png: Vec<u8>,
- /// The canvas width: the frame's.
+ /// The canvas width: the frame's, and more where the title's ink runs
+ /// past the frame's left or right edge - the same margin either side.
pub width: u32,
- /// The canvas height: the frame's.
+ /// The canvas height: the frame's, grown the same way for ink past the
+ /// top or the bottom.
pub height: u32,
+ /// How many times the frame the canvas is, along the axis it grew the
+ /// most: one for a title inside the frame, which is nearly every title.
+ /// A picture this many times the frame is drawn at one canvas pixel to
+ /// one frame pixel, where the compositor would otherwise fit the whole
+ /// canvas into the frame.
+ pub overscan: f64,
/// The painted block's width in pixels, plate included - what an
/// outline on a monitor should be drawn around.
pub block_width: u32,
@@ -1484,8 +1503,9 @@ pub fn render(
.map_err(|error| Error::Encode(error.to_string()))?;
Ok(Rendered {
png,
- width,
- height,
+ width: canvas.width(),
+ height: canvas.height(),
+ overscan: overscan_of(&canvas, width, height),
block_width: block.0,
block_height: block.1,
block_dx: block.2,
@@ -1507,8 +1527,9 @@ pub fn render_frame(
let (canvas, block, words) = paint(fonts, style, width, height)?;
Ok(RenderedFrame {
rgba: straight_rgba(&canvas),
- width,
- height,
+ width: canvas.width(),
+ height: canvas.height(),
+ overscan: overscan_of(&canvas, width, height),
block_width: block.0,
block_height: block.1,
block_dx: block.2,
@@ -1520,6 +1541,62 @@ pub fn render_frame(
/// A painted block: (width, height, dx, dy), on [`Rendered`]'s terms.
type Block = (u32, u32, i32, i32);
+/// The longest side a canvas may grow to: the largest texture every GPU
+/// takes, `concat_core::frame::MAX_SIDE`. Ink past it is cut as all ink
+/// past the frame once was, which takes a box several frames wide.
+const MAX_SIDE: u32 = 8192;
+
+/// How many times a `width` by `height` frame `canvas` is, along the axis
+/// it grew the most; see [`Rendered::overscan`].
+fn overscan_of(canvas: &Pixmap, width: u32, height: u32) -> f64 {
+ (f64::from(canvas.width()) / f64::from(width.max(1)))
+ .max(f64::from(canvas.height()) / f64::from(height.max(1)))
+ .max(1.0)
+}
+
+/// Where ink lands, as left, top, right and bottom edges in pixels: what
+/// the canvas has to hold. Empty until something is added.
+#[derive(Clone, Copy)]
+struct Ink(Option<[f32; 4]>);
+
+impl Ink {
+ /// Takes in `rect`, moved by `(dx, dy)` and grown by `by` on every side.
+ fn add(&mut self, rect: Rect, (dx, dy): (f32, f32), by: f32) {
+ let edges = [
+ rect.left() + dx - by,
+ rect.top() + dy - by,
+ rect.right() + dx + by,
+ rect.bottom() + dy + by,
+ ];
+ self.0 = Some(match self.0 {
+ None => edges,
+ Some([l, t, r, b]) => [
+ l.min(edges[0]),
+ t.min(edges[1]),
+ r.max(edges[2]),
+ b.max(edges[3]),
+ ],
+ });
+ }
+
+ /// The margin a `width` by `height` canvas needs on each side to hold
+ /// the ink, x and y, in whole pixels; nothing for ink inside it. The
+ /// same either side, so the canvas's centre does not move, and held so
+ /// the canvas grows no larger than [`MAX_SIDE`].
+ fn margins(&self, width: u32, height: u32) -> (u32, u32) {
+ let Some([left, top, right, bottom]) = self.0 else {
+ return (0, 0);
+ };
+ let past = |low: f32, high: f32, side: u32| -> u32 {
+ let over = (-low).max(high - side as f32).max(0.0);
+ // A hair over is anti-aliasing's guess at an edge, not ink.
+ let over = if over < 0.01 { 0 } else { over.ceil() as u32 };
+ over.min(MAX_SIDE.saturating_sub(side) / 2)
+ };
+ (past(left, right, width), past(top, bottom, height))
+ }
+}
+
/// The canvas with the title on it, the block, and every word's box on the
/// canvas in reading order.
fn paint(
@@ -1528,7 +1605,9 @@ fn paint(
width: u32,
height: u32,
) -> Result<(Pixmap, Block, Vec<WordRect>), Error> {
- let mut canvas = Pixmap::new(width, height).ok_or(Error::Canvas(width, height))?;
+ // The frame's canvas, for a title with nothing to paint; one with ink
+ // gets a canvas as big as its ink needs, below.
+ let empty = 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;
@@ -1630,7 +1709,7 @@ fn paint(
let words_w = lines.iter().map(|line| line.width).fold(0.0, f32::max);
if words_w <= 0.0 || lines.is_empty() {
// Nothing to paint: an empty, valid canvas.
- return Ok((canvas, (0, 0, 0, 0), Vec::new()));
+ return Ok((empty, (0, 0, 0, 0), Vec::new()));
}
// Each baseline below the first by the pitch of the bigger of the two
// lines it sits between, so a line with a big word in it pushes its
@@ -1763,6 +1842,75 @@ fn paint(
.filter_map(|(look, builder)| Some((look, builder.finish()?)))
.collect();
+ // Everything about to be painted, in the frame's pixels: the plate,
+ // the words with the outer half of their outline, their shadow and
+ // its blur, and the glyphs that paint their own colours. The words'
+ // advances are not their ink - a letter's outline, an italic's lean, a
+ // side bearing below zero all reach past them - so the outlines are
+ // measured, not the line. Whatever reaches past the frame's edge
+ // grows the canvas by that much on both sides, so it is painted
+ // rather than cut, and the canvas's centre stays the frame's.
+ let stroke_w = style.stroke_width.max(0.0) as f32 * frame_h;
+ let shadow_offset = (em * 0.05, em * 0.07);
+ let shadow_blur = (em * 0.04).round().max(1.0) as usize;
+ let plate_rect = plate
+ .and_then(|_| Rect::from_xywh(outer_left, (frame_h - outer_h) / 2.0, outer_w, outer_h));
+ // A pixel more than the edges, for the anti-aliasing either side.
+ let mut ink = Ink(None);
+ if let Some(rect) = plate_rect {
+ ink.add(rect, (0.0, 0.0), 1.0);
+ }
+ for (look, path) in &words {
+ let edge = if stroke_w > 0.0 && looks[*look].edge.is_some() {
+ stroke_w
+ } else {
+ 0.0
+ };
+ ink.add(path.bounds(), (0.0, 0.0), edge + 1.0);
+ if style.shadow {
+ // The blur is a box run twice, so it spreads twice its radius.
+ ink.add(path.bounds(), shadow_offset, 2.0 * shadow_blur as f32 + 1.0);
+ }
+ }
+ for (row, line) in lines.iter().enumerate() {
+ let at = (left + indent_of(line), baseline_of(row));
+ for mark in &line.marks {
+ match mark {
+ Mark::Bitmap {
+ x,
+ y,
+ width,
+ height,
+ ..
+ } => {
+ if let Some(rect) = Rect::from_xywh(*x, *y, *width, *height) {
+ ink.add(rect, at, 1.0);
+ }
+ }
+ Mark::Layers(layers) => {
+ for (path, _) in layers {
+ ink.add(path.bounds(), at, 1.0);
+ }
+ }
+ }
+ }
+ }
+ let (margin_x, margin_y) = ink.margins(width, height);
+ let mut canvas = if (margin_x, margin_y) == (0, 0) {
+ empty
+ } else {
+ let (grown_w, grown_h) = (width + 2 * margin_x, height + 2 * margin_y);
+ Pixmap::new(grown_w, grown_h).ok_or(Error::Canvas(grown_w, grown_h))?
+ };
+ // Everything below is laid out in the frame's pixels and painted
+ // through this, onto the canvas the frame sits in the middle of.
+ let (shift_x, shift_y) = (margin_x as f32, margin_y as f32);
+ let shift = Transform::from_translate(shift_x, shift_y);
+ for word in &mut word_rects {
+ word.x += margin_x as i32;
+ word.y += margin_y as i32;
+ }
+
let mut paint = Paint {
anti_alias: true,
..Paint::default()
@@ -1770,20 +1918,14 @@ fn paint(
// The plate, first and under everything: the whole box.
if let Some(fill) = plate
- && let Some(rect) = Rect::from_xywh(outer_left, (frame_h - outer_h) / 2.0, outer_w, outer_h)
+ && let Some(rect) = plate_rect
{
paint.set_color(fill);
let radius = (style.background_radius.max(0.0) as f32) * frame_h;
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,
- );
+ canvas.fill_path(&plate_path, &paint, FillRule::Winding, shift, None);
}
}
@@ -1792,18 +1934,19 @@ fn paint(
// under all of them, one shadow for the title.
if style.shadow
&& !words.is_empty()
- && let Some(mut layer) = Pixmap::new(width, height)
+ && let Some(mut layer) = Pixmap::new(canvas.width(), canvas.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);
+ let offset =
+ Transform::from_translate(shadow_offset.0 + shift_x, shadow_offset.1 + shift_y);
for (_, path) in &words {
layer.fill_path(path, &shade, FillRule::Winding, offset, None);
}
- blur(&mut layer, (em * 0.04).round().max(1.0) as usize);
+ blur(&mut layer, shadow_blur);
canvas.draw_pixmap(
0,
0,
@@ -1818,7 +1961,6 @@ fn paint(
// why it is drawn at twice the asked-for width. Every look's outline
// goes down before any fill, so one word's outline never covers its
// neighbour's letters.
- let stroke_w = style.stroke_width.max(0.0) as f32 * frame_h;
if stroke_w > 0.0 {
let stroke = Stroke {
width: stroke_w * 2.0,
@@ -1829,7 +1971,7 @@ fn paint(
for (look, path) in &words {
if let Some(edge) = looks[*look].edge {
paint.set_color(edge);
- canvas.stroke_path(path, &paint, &stroke, Transform::identity(), None);
+ canvas.stroke_path(path, &paint, &stroke, shift, None);
}
}
}
@@ -1837,7 +1979,7 @@ fn paint(
// The words, each look in its own colour.
for (look, path) in &words {
paint.set_color(looks[*look].fill);
- canvas.fill_path(path, &paint, FillRule::Winding, Transform::identity(), None);
+ canvas.fill_path(path, &paint, FillRule::Winding, shift, None);
}
// The glyphs that paint their own colours - an emoji's picture, a
@@ -1851,7 +1993,7 @@ fn paint(
}
let indent = indent_of(line);
let baseline = baseline_of(row);
- let placed = Transform::from_translate(left + indent, baseline);
+ let placed = Transform::from_translate(left + indent + shift_x, baseline + shift_y);
for mark in &line.marks {
match mark {
Mark::Bitmap {
@@ -1878,8 +2020,8 @@ fn paint(
0.0,
0.0,
scale_y,
- left + indent + *x,
- baseline + *y,
+ left + indent + *x + shift_x,
+ baseline + *y + shift_y,
),
None,
);
@@ -1990,6 +2132,8 @@ pub fn render_shape(style: &ShapeStyle, width: u32, height: u32) -> Result<Rende
png,
width,
height,
+ // A shape is painted on the frame alone, as it always was.
+ overscan: 1.0,
block_width: block.0,
block_height: block.1,
block_dx: block.2,
@@ -2010,6 +2154,8 @@ pub fn render_shape_frame(
rgba: straight_rgba(&canvas),
width,
height,
+ // A shape is painted on the frame alone, as it always was.
+ overscan: 1.0,
block_width: block.0,
block_height: block.1,
block_dx: block.2,
@@ -2868,6 +3014,122 @@ mod tests {
}
}
+ /// Whether any paint touches the canvas's outermost column or row: ink
+ /// that reached the edge, and so was cut there.
+ fn touches_an_edge(png: &[u8]) -> bool {
+ let pixmap = Pixmap::decode_png(png).expect("our own PNG decodes");
+ let (width, height) = (pixmap.width(), pixmap.height());
+ pixmap.pixels().iter().enumerate().any(|(index, pixel)| {
+ let (x, y) = (index as u32 % width, index as u32 / width);
+ pixel.alpha() > 0 && (x == 0 || y == 0 || x == width - 1 || y == height - 1)
+ })
+ }
+
+ /// A title inside the frame is painted on the frame, as every title
+ /// was: nothing about it changes for the canvas being able to grow.
+ #[test]
+ fn a_title_inside_the_frame_is_painted_on_the_frame() {
+ let fonts = Fonts::new();
+ let mut outlined = style("Your text");
+ outlined.stroke_width = 0.01;
+ outlined.background = "#000000".to_owned();
+ let out = render(&fonts, &outlined, 640, 360).expect("renders");
+ assert_eq!((out.width, out.height), (640, 360));
+ assert_eq!(out.overscan, 1.0);
+ assert!(!touches_an_edge(&out.png));
+ }
+
+ /// The situations that cut a title's sides off - its box pulled wider
+ /// than the frame with the side grips, a word too long for any line, a
+ /// line as wide as the frame with an outline, a shadow and an italic's
+ /// lean past its last letter - all paint whole: the canvas grows by
+ /// the same margin either side, so nothing reaches its edge and its
+ /// centre is still the frame's. Scaled down, moved or turned, such a
+ /// title showed the cut as a straight edge through its plate or its
+ /// letters.
+ #[test]
+ fn ink_past_the_frame_grows_the_canvas_rather_than_being_cut() {
+ let fonts = Fonts::new();
+ let (width, height) = (640_u32, 360_u32);
+ let mut boxed = style("a box wider than the frame");
+ boxed.background = "#2040a0".to_owned();
+ boxed.max_width = 1.6;
+ let mut long = style("Unbelievably");
+ long.font_family = BUNDLED_FAMILY.to_owned();
+ long.font_size = 0.4;
+ long.stroke_width = 0.02;
+ // A word sized so its letters' advances fill the frame but for two
+ // pixels a side: its outline, shadow and lean run past the edge.
+ let mut edge = style("Overhang");
+ edge.font_family = BUNDLED_FAMILY.to_owned();
+ edge.italic = true;
+ edge.shadow = false;
+ let natural = render(&fonts, &edge, width, height).expect("renders");
+ edge.font_size *= f64::from(width - 4) / f64::from(natural.block_width);
+ let fitted = render(&fonts, &edge, width, height).expect("renders");
+ assert!(
+ fitted.block_width <= width && fitted.block_width >= width - 8,
+ "{}",
+ fitted.block_width
+ );
+ edge.shadow = true;
+ edge.stroke_width = 0.02;
+ let mut left = boxed.clone();
+ left.align = Align::Left;
+ for (name, title) in [
+ ("box", boxed),
+ ("word", long),
+ ("edge", edge),
+ ("left", left),
+ ] {
+ let out = render(&fonts, &title, width, height).expect("renders");
+ assert!(out.width > width, "{name}: {} wide", out.width);
+ assert_eq!(
+ (out.width - width) % 2,
+ 0,
+ "{name}: the same margin either side"
+ );
+ assert_eq!((out.height - height) % 2, 0, "{name}");
+ let expected = (f64::from(out.width) / f64::from(width))
+ .max(f64::from(out.height) / f64::from(height));
+ assert_eq!(out.overscan, expected, "{name}");
+ assert!(!touches_an_edge(&out.png), "{name}: ink was cut");
+ for word in &out.words {
+ assert!(
+ word.x >= 0 && word.x as u32 + word.width <= out.width,
+ "{name}: a word at {} leaves the canvas",
+ word.x
+ );
+ }
+ // The live painting is the same canvas, pixel for pixel.
+ let live = render_frame(&fonts, &title, width, height).expect("renders");
+ assert_eq!((live.width, live.height), (out.width, out.height), "{name}");
+ assert_eq!(live.words, out.words, "{name}");
+ }
+ }
+
+ /// A box wider than the frame keeps its plate whole and centred on
+ /// the canvas: the canvas's centre is still where the clip's is.
+ #[test]
+ fn a_box_wider_than_the_frame_is_centred_on_the_grown_canvas() {
+ let fonts = Fonts::new();
+ let mut boxed = style("wide");
+ boxed.background = "#2040a0".to_owned();
+ boxed.shadow = false;
+ boxed.max_width = 1.5;
+ let out = render(&fonts, &boxed, 640, 360).expect("renders");
+ assert_eq!(out.block_width, 960);
+ assert_eq!(out.block_dx, 0);
+ let (left, right) = painted_span(&out.png);
+ let middle = (left + right) as f32 / 2.0;
+ assert!(
+ (middle - out.width as f32 / 2.0).abs() <= 1.0,
+ "{left}..{right} in {}",
+ out.width
+ );
+ assert!(right - left + 1 >= 959, "the plate whole: {left}..{right}");
+ }
+
// ── scripts the style's face lacks ──
/// A character the style's face has no glyph for is set in a face that