diff --git a/Cargo.lock b/Cargo.lock
index d3424005..d67a9023 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3941,8 +3941,7 @@ dependencies = [
[[package]]
name = "svgr"
version = "0.50.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1c4ca5d70fab22333f7ea7de99b5a7e273cf4b29bd9d8b28e3977b3daac7fc8f"
+source = "git+https://github.com/silouanwright/svgr?branch=feat%2Fshaped-text-measure#23cf2f5052ff14d50ad6e9af9591767a13c46b6b"
dependencies = [
"bytemuck",
"gif",
@@ -3972,8 +3971,7 @@ dependencies = [
[[package]]
name = "svgrtypes"
version = "0.50.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d01e129db840881357043393e2b2ec41ed2f0f012409d27c324e25346091f070"
+source = "git+https://github.com/silouanwright/svgr?branch=feat%2Fshaped-text-measure#23cf2f5052ff14d50ad6e9af9591767a13c46b6b"
dependencies = [
"kurbo",
"proc-macro2",
@@ -4064,6 +4062,14 @@ dependencies = [
"fframes_skia_renderer",
]
+[[package]]
+name = "text-width-example"
+version = "1.4.1"
+dependencies = [
+ "fframes",
+ "fframes_skia_renderer",
+]
+
[[package]]
name = "thingbuf"
version = "0.1.6"
@@ -4425,8 +4431,7 @@ checksum = "81e544489bf3d8ef66c953931f56617f423cd4b5494be343d9b9d3dda037b9a3"
[[package]]
name = "usvgr"
version = "0.50.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "157a8545c1764f266393e38b1b87450db31aac1edbe82ef32411c20a9c50731a"
+source = "git+https://github.com/silouanwright/svgr?branch=feat%2Fshaped-text-measure#23cf2f5052ff14d50ad6e9af9591767a13c46b6b"
dependencies = [
"ahash",
"base64 0.22.1",
diff --git a/Cargo.toml b/Cargo.toml
index e6370eac..1209c685 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -45,6 +45,7 @@ members = [
"examples/neon-triangle",
"examples/neon-triangle/editor/editor-bridge",
"examples/arm",
+ "examples/text-width",
# maybe clean this up?
# "fframes-lyon-renderer",
@@ -105,8 +106,9 @@ float_cmp = "allow"
# into the parent directory of fframes (as a sibling) and uncomment the following lines:
# svgr = { path = "../svgr/crates/svgr" }
# usvgr = { path = "../svgr/crates/usvgr" }
-svgr = "0.50.3"
-usvgr = "0.50.3"
+# Until https://github.com/dmtrKovalenko/svgr/pull/26 is released:
+svgr = { git = "https://github.com/silouanwright/svgr", branch = "feat/shaped-text-measure" }
+usvgr = { git = "https://github.com/silouanwright/svgr", branch = "feat/shaped-text-measure" }
# ffmpeg-sys-next with prebuilt FFmpeg binaries: https://github.com/dmtrKovalenko/rust-ffmpeg-sys
ffmpeg-sys-fframes = "9.0.0"
wasm-bindgen = "0.2.100"
diff --git a/e2e/snapshots/captions/1.5s.png b/e2e/snapshots/captions/1.5s.png
index cc011384..1bece6b3 100644
Binary files a/e2e/snapshots/captions/1.5s.png and b/e2e/snapshots/captions/1.5s.png differ
diff --git a/e2e/snapshots/captions/5.2s.png b/e2e/snapshots/captions/5.2s.png
index bd5b28b5..e406d3b8 100644
Binary files a/e2e/snapshots/captions/5.2s.png and b/e2e/snapshots/captions/5.2s.png differ
diff --git a/e2e/snapshots/captions/7s.png b/e2e/snapshots/captions/7s.png
index 05f022d6..9fba8eb8 100644
Binary files a/e2e/snapshots/captions/7s.png and b/e2e/snapshots/captions/7s.png differ
diff --git a/e2e/snapshots/fframes-intro/outroscene_7s.png b/e2e/snapshots/fframes-intro/outroscene_7s.png
index a767555e..bbfb0679 100644
Binary files a/e2e/snapshots/fframes-intro/outroscene_7s.png and b/e2e/snapshots/fframes-intro/outroscene_7s.png differ
diff --git a/e2e/snapshots/text-width/0.5s.png b/e2e/snapshots/text-width/0.5s.png
new file mode 100644
index 00000000..133f1fd5
Binary files /dev/null and b/e2e/snapshots/text-width/0.5s.png differ
diff --git a/examples/text-width/Cargo.toml b/examples/text-width/Cargo.toml
new file mode 100644
index 00000000..a38f27ce
--- /dev/null
+++ b/examples/text-width/Cargo.toml
@@ -0,0 +1,24 @@
+[package]
+name = "text-width-example"
+version = "1.4.1"
+edition = "2024"
+publish = false
+
+[dependencies]
+fframes = { workspace = true, features = ["cli", "compile-time-svgtree"] }
+
+[features]
+default = ["skia"]
+skia = ["dep:fframes_skia_renderer"]
+
+[target.'cfg(target_os = "macos")'.dependencies]
+fframes_skia_renderer = { workspace = true, optional = true, features = ["cli", "metal"] }
+
+[target.'cfg(not(any(target_os = "macos", target_arch = "wasm32")))'.dependencies]
+fframes_skia_renderer = { workspace = true, optional = true, features = ["cli", "vulkan"] }
+
+[target.'cfg(not(any(windows, target_arch = "wasm32")))'.dependencies]
+fframes = { workspace = true, features = ["h264", "libav-agree-gpl", "build-portable"] }
+
+[lints]
+workspace = true
diff --git a/examples/text-width/media/NotoSansArabic-subset.ttf b/examples/text-width/media/NotoSansArabic-subset.ttf
new file mode 100644
index 00000000..c374a56c
Binary files /dev/null and b/examples/text-width/media/NotoSansArabic-subset.ttf differ
diff --git a/examples/text-width/media/SofiaSansSemiCondensed-VariableFont_wght.ttf b/examples/text-width/media/SofiaSansSemiCondensed-VariableFont_wght.ttf
new file mode 100644
index 00000000..59ba155e
Binary files /dev/null and b/examples/text-width/media/SofiaSansSemiCondensed-VariableFont_wght.ttf differ
diff --git a/examples/text-width/src/lib.rs b/examples/text-width/src/lib.rs
new file mode 100644
index 00000000..e0a2239f
--- /dev/null
+++ b/examples/text-width/src/lib.rs
@@ -0,0 +1,117 @@
+//! Boxes sized by `text_width`, a box filled by `text_fit` and a paragraph wrapped by
+//! `text_break_lines`: each box should hug its glyphs, and no wrapped line should cross the
+//! paragraph's right edge. The right column covers a variable font at two weights and Arabic
+//! laid out with a fallback font inside Latin text.
+use fframes::{
+ AudioMap, BreakLinesOpts, Duration, FFramesContext, FontQuery, Frame, Svgr, TextAlign,
+ TextOverflow, Video, include_media_dir, svgr,
+};
+
+include_media_dir!(pub struct InterMedia, "examples/arm/media");
+mod own {
+ // Sofia Sans Semi Condensed (variable weight) and Noto Sans Arabic cut down to "سلام".
+ fframes::include_media_dir!(pub struct TextWidthMedia, "examples/text-width/media");
+}
+pub use own::TextWidthMedia;
+
+const INTER: &str = "Inter 18pt";
+const SOFIA: &str = "Sofia Sans Semi Condensed";
+const PARAGRAPH_WIDTH: usize = 760;
+const FIT_WIDTH: usize = 550;
+
+pub struct TextWidthVideo;
+
+fn font(family: &str, size: usize, weight: u16) -> FontQuery<'_> {
+ FontQuery {
+ family,
+ size,
+ weight,
+ ..Default::default()
+ }
+}
+
+fn measured<'a>(
+ frame: &mut Frame,
+ ctx: &FFramesContext<'a, '_>,
+ font: FontQuery<'a>,
+ text: &'a str,
+ (x, y): (f64, f64),
+) -> Svgr<'a> {
+ let width = frame.text_width(ctx, font, text).unwrap_or(0);
+ let size = font.size as f64;
+ svgr!(
+
+ {text}
+ )
+}
+
+impl Video for TextWidthVideo {
+ const FPS: usize = 30;
+ const WIDTH: usize = 1920;
+ const HEIGHT: usize = 1080;
+
+ fn duration(&self) -> Duration<'_> {
+ Duration::Seconds(1.0)
+ }
+
+ fn audio(&self) -> AudioMap<'_> {
+ AudioMap::none()
+ }
+
+ fn render_frame<'a>(&'a self, mut frame: Frame, ctx: &FFramesContext<'a, '_>) -> Svgr<'a> {
+ let rows = [
+ (font(INTER, 100, 800), "AVAVAVAV WAVE", (120.0, 260.0)),
+ (
+ font(INTER, 46, 800),
+ "δεν είναι ανατολική ρωμαϊκή αυτοκρατορία",
+ (120.0, 420.0),
+ ),
+ (
+ font(INTER, 46, 800),
+ "The Byzantine Greeks call Eastern Rome",
+ (120.0, 520.0),
+ ),
+ (font(SOFIA, 64, 100), "Hamburgefonts WAVE", (1040.0, 660.0)),
+ (font(SOFIA, 64, 900), "Hamburgefonts WAVE", (1040.0, 760.0)),
+ (font(INTER, 64, 800), "AV سلام WAVE", (1040.0, 860.0)),
+ ]
+ .into_iter()
+ .map(|(font, text, at)| measured(&mut frame, ctx, font, text, at))
+ .collect::>();
+ let fitted = frame
+ .text_fit(
+ ctx,
+ font(INTER, 64, 800),
+ "Eastern Rome, Byzantium",
+ FIT_WIDTH,
+ TextOverflow::Ellipsis,
+ )
+ .unwrap_or_default()
+ .into_owned();
+ let paragraph = frame.text_break_lines(
+ ctx,
+ "Η Ανατολική Ρωμαϊκή Αυτοκρατορία ονομάστηκε αργότερα Βυζάντιο. Later historians named the Eastern Roman Empire Byzantium.",
+ BreakLinesOpts {
+ font: font(INTER, 40, 800),
+ width: PARAGRAPH_WIDTH,
+ line_height: 1.25,
+ x: 120,
+ y: 660,
+ align: TextAlign::Left,
+ fill: "#f4f7fb",
+ ..Default::default()
+ },
+ );
+ svgr!(
+
+ )
+ }
+}
diff --git a/examples/text-width/src/main.rs b/examples/text-width/src/main.rs
new file mode 100644
index 00000000..307ce131
--- /dev/null
+++ b/examples/text-width/src/main.rs
@@ -0,0 +1,25 @@
+use fframes::{CombinedMediaProvider, MediaProvider, RenderOptions, cli};
+use std::process::ExitCode;
+use text_width_example::{InterMedia, TextWidthMedia, TextWidthVideo};
+
+fn main() -> ExitCode {
+ let (inter, own) = match (InterMedia::new(), TextWidthMedia::new()) {
+ (Ok(inter), Ok(own)) => (inter, own),
+ (Err(error), _) | (_, Err(error)) => {
+ eprintln!("{error}");
+ return ExitCode::FAILURE;
+ }
+ };
+ let media = CombinedMediaProvider::from([&inter as &dyn MediaProvider, &own]);
+ let video = TextWidthVideo;
+ let runner = cli::new(
+ &video,
+ RenderOptions {
+ media: Some(&media),
+ ..Default::default()
+ },
+ );
+ #[cfg(feature = "skia")]
+ let runner = runner.renderers(fframes_skia_renderer::cli::SkiaRenderers::default());
+ runner.run()
+}
diff --git a/fframes-editor-controller/src/wasm_editor.rs b/fframes-editor-controller/src/wasm_editor.rs
index fe16f6d6..024ab610 100644
--- a/fframes-editor-controller/src/wasm_editor.rs
+++ b/fframes-editor-controller/src/wasm_editor.rs
@@ -402,6 +402,8 @@ impl + 'static> WasmEditor JsValue {
let mut fonts = self.fonts.lock().unwrap();
let face_info = fonts.insert_font(slice.to_vec().into(), None);
+ // Cache keys name the font, not its bytes, so a replaced font would keep old widths.
+ self.break_lines_cache.clear();
serde_wasm_bindgen::to_value(&face_info).unwrap()
}
diff --git a/fframes-editor-controller/src/wasm_font_source.rs b/fframes-editor-controller/src/wasm_font_source.rs
index f6f58e87..1ef4a71d 100644
--- a/fframes-editor-controller/src/wasm_font_source.rs
+++ b/fframes-editor-controller/src/wasm_font_source.rs
@@ -47,6 +47,10 @@ impl StaticFontFace {
pub struct WasmFontSource {
pub static_fonts: Vec,
pub data: HashMap>,
+ /// The same fonts for measuring text the way the native renderer lays it out.
+ pub fontdb: fframes::usvgr::fontdb::Database,
+ /// Faces each font added to `fontdb`, replaced when the font is inserted again.
+ face_ids: HashMap>,
}
impl WasmFontSource {
@@ -54,6 +58,8 @@ impl WasmFontSource {
Self {
static_fonts: vec![],
data: HashMap::new(),
+ fontdb: fframes::usvgr::fontdb::Database::new(),
+ face_ids: HashMap::new(),
}
}
@@ -81,6 +87,18 @@ impl WasmFontSource {
})
}
+ // Static fonts are shared with fontdb, not copied.
+ let shared: Arc + Send + Sync> = match &data {
+ Cow::Borrowed(bytes) => Arc::new(*bytes),
+ Cow::Owned(bytes) => Arc::new(bytes.clone()),
+ };
+ for id in self.face_ids.remove(&face_info).into_iter().flatten() {
+ self.fontdb.remove_face(id);
+ }
+ let ids = self
+ .fontdb
+ .load_font_source(fframes::usvgr::fontdb::Source::Binary(shared));
+ self.face_ids.insert(face_info.clone(), ids.to_vec());
self.data.insert(face_info.clone(), data);
Some(face_info)
}
@@ -137,6 +155,10 @@ impl<'a> fframes::FontSource<'a> for WasmFontSource {
}))
}
+ fn fontdb(&self) -> Option<&fframes::usvgr::fontdb::Database> {
+ Some(&self.fontdb)
+ }
+
fn add_font(&mut self, _filename: String, _font_data: Arc + Sync + Send>) {
unimplemented!(
"Adding fonts for wasm font source must be done through WasmFontSource::insert_font api"
diff --git a/fframes-skia-renderer/src/instant_rendering.rs b/fframes-skia-renderer/src/instant_rendering.rs
index 778f719f..29639b68 100644
--- a/fframes-skia-renderer/src/instant_rendering.rs
+++ b/fframes-skia-renderer/src/instant_rendering.rs
@@ -164,6 +164,13 @@ impl SkiaFFramesRenderer<'_, TBackend> {
fframes_ctx.video_decoders.clone(),
);
+ if native_ctx.converter_cache.usvgr_shape_cache.is_none() {
+ native_ctx.converter_cache.usvgr_shape_cache = fframes_ctx
+ .break_lines_cache
+ .as_ref()
+ .map(TextCache::shapes);
+ }
+
let tree = fframes::render_frame_guarded(video, fframe, &ctx)?.into_svg_tree(
&fframes_ctx.usvg_options,
&mut native_ctx.converter_cache,
diff --git a/fframes-skia-renderer/src/skia_pipeline.rs b/fframes-skia-renderer/src/skia_pipeline.rs
index dff99f91..2ac2c20c 100644
--- a/fframes-skia-renderer/src/skia_pipeline.rs
+++ b/fframes-skia-renderer/src/skia_pipeline.rs
@@ -359,7 +359,8 @@ fn generate_frames<'a, 'media: 'a, TVideo: Video + Sync + Send>(
#[cfg(feature = "debug")] metrics: Arc,
) -> FFramesRendererResult<()> {
let break_lines_cache = TextCache::new(10);
- let mut converter_cache = usvgr::Cache::new_with_text_cache(text_cache_capacity);
+ let mut converter_cache =
+ TextCache::converter_cache(break_lines_cache.as_ref(), text_cache_capacity);
// x2 because sometimes we might need to decode 2 frames at once
let video_decoders_worker = VideoDecodersWorker::new(queue_size * 2);
diff --git a/fframes/src/captions/render.rs b/fframes/src/captions/render.rs
index 886f073e..6e5a6ac1 100644
--- a/fframes/src/captions/render.rs
+++ b/fframes/src/captions/render.rs
@@ -355,7 +355,7 @@ impl Captions {
fn prepare(
&self,
track: &CaptionTrack,
- _frame: &mut Frame,
+ frame: &mut Frame,
ctx: &FFramesContext<'_, '_>,
) -> Result {
if self.font_size == 0 || !self.line_height.is_finite() || self.line_height < 1.0 {
@@ -402,7 +402,7 @@ impl Captions {
CaptionError::invalid("layout.font", "caption layout needs a font source")
})?;
let measure = |text: &str| {
- crate::text_get_width_impl(self.font_query(), text, font_source)
+ crate::text_get_width_impl(self.font_query(), text, font_source, frame.text_shapes())
.map(|width| width as f64)
.ok_or_else(|| {
CaptionError::invalid(
diff --git a/fframes/src/font_data.rs b/fframes/src/font_data.rs
index ec38fb3a..696f1646 100644
--- a/fframes/src/font_data.rs
+++ b/fframes/src/font_data.rs
@@ -100,4 +100,11 @@ pub trait FontSource<'a>: Sync + Send + std::fmt::Debug {
font_style: FontStyle,
font_stretch: FontStretch,
) -> Option + 'a>>;
+
+ /// The database the renderer takes these fonts from. With it text is measured the way the
+ /// renderer lays it out (kerning, fallback fonts, font variations); `None` falls back to
+ /// per-character advances.
+ fn fontdb(&self) -> Option<&usvgr::fontdb::Database> {
+ None
+ }
}
diff --git a/fframes/src/frame.rs b/fframes/src/frame.rs
index fddb71cb..a7e0870d 100644
--- a/fframes/src/frame.rs
+++ b/fframes/src/frame.rs
@@ -279,7 +279,7 @@ impl Frame {
) -> Option {
let font_source = ctx.font_source?;
let Some(text_cache) = self.text_cache.as_ref() else {
- return text_wrap_impl(opts.hash_with_value(value), value, font_source, opts);
+ return text_wrap_impl(opts.hash_with_value(value), value, font_source, opts, None);
};
let hash = opts.hash_with_value(value);
@@ -288,7 +288,7 @@ impl Frame {
}
// See `text_width`: unresolved fonts are retried on the next frame.
- let structure = text_wrap_impl(hash, value, font_source, opts)?;
+ let structure = text_wrap_impl(hash, value, font_source, opts, Some(&text_cache.shapes))?;
text_cache
.breaks_cache
.borrow_mut()
@@ -296,6 +296,10 @@ impl Frame {
Some(structure)
}
+ pub(crate) fn text_shapes(&self) -> Option<&usvgr::UsvgrTextShapeCache> {
+ self.text_cache.as_ref().map(|cache| &cache.shapes)
+ }
+
/// Wraps the text string into the lines according to the provided content area width.
/// Outputs the ready to use svgr text element. If font can not be resolved returns `None`.
///
@@ -347,7 +351,7 @@ impl Frame {
) -> Option {
let font_source = ctx.font_source?;
let Some(text_cache) = self.text_cache.as_ref() else {
- return text_get_width_impl(font_query, value, font_source);
+ return text_get_width_impl(font_query, value, font_source, None);
};
let hash = font_query.hash_with_value(value);
@@ -357,7 +361,7 @@ impl Frame {
// A font that can not be measured yet (the editor is still loading
// fonts) must not be remembered, or it never gets measured at all.
- let width = text_get_width_impl(font_query, value, font_source)?;
+ let width = text_get_width_impl(font_query, value, font_source, Some(&text_cache.shapes))?;
text_cache.text_width_cache.borrow_mut().put(hash, width);
Some(width)
}
@@ -387,7 +391,14 @@ impl Frame {
overflow: TextOverflow,
) -> Option> {
let font_source = ctx.font_source?;
- text_fit_impl(font_query, value, max_width, overflow, font_source)
+ text_fit_impl(
+ font_query,
+ value,
+ max_width,
+ overflow,
+ font_source,
+ self.text_shapes(),
+ )
}
/// Returns a phrase that must be rendered by time in this frame.
diff --git a/fframes/src/renderer/cpu.rs b/fframes/src/renderer/cpu.rs
index 724aeb34..3ff1e016 100644
--- a/fframes/src/renderer/cpu.rs
+++ b/fframes/src/renderer/cpu.rs
@@ -216,7 +216,8 @@ impl FFramesRenderBackend for CpuRenderingBackend {
let render_worker = |worker: usize| -> FFramesRendererResult<()> {
let worker_local_decoders = VideoDecodersWorker::new(1);
let break_lines_cache = TextCache::new(self.text_cache_capacity);
- let mut converter_cache = usvgr::Cache::new_with_text_cache(self.text_cache_capacity);
+ let mut converter_cache =
+ TextCache::converter_cache(break_lines_cache.as_ref(), self.text_cache_capacity);
let mut renderer = self.encoder_frame_renderer(
writer.encoder_input(),
video_size.width as u32,
diff --git a/fframes/src/renderer/preview.rs b/fframes/src/renderer/preview.rs
index ca5ae72d..65c89377 100644
--- a/fframes/src/renderer/preview.rs
+++ b/fframes/src/renderer/preview.rs
@@ -538,7 +538,8 @@ impl<'a, 'media: 'a, TVideo: Video> Previewer<'a, 'media, TVideo> {
if let Some(capacity) = renderer.svg_text_cache_capacity()
&& capacity != self.converter_text_cache_capacity
{
- self.worker.converter_cache = usvgr::Cache::new_with_text_cache(capacity);
+ self.worker.converter_cache =
+ TextCache::converter_cache(self.worker.text_cache.as_ref(), capacity);
self.converter_text_cache_capacity = capacity;
}
}
@@ -693,10 +694,14 @@ struct Worker {
impl Worker {
fn new(converter_text_cache_capacity: usize) -> Self {
+ let text_cache = TextCache::new(10);
Self {
- text_cache: TextCache::new(10),
+ converter_cache: TextCache::converter_cache(
+ text_cache.as_ref(),
+ converter_text_cache_capacity,
+ ),
+ text_cache,
decoders: VideoDecodersWorker::new(2),
- converter_cache: usvgr::Cache::new_with_text_cache(converter_text_cache_capacity),
}
}
}
diff --git a/fframes/src/renderer/renderer_font_source.rs b/fframes/src/renderer/renderer_font_source.rs
index b4edf930..bf40e9c9 100644
--- a/fframes/src/renderer/renderer_font_source.rs
+++ b/fframes/src/renderer/renderer_font_source.rs
@@ -97,8 +97,201 @@ impl<'a> crate::FontSource<'a> for RendererFontSource {
}))
}
+ fn fontdb(&self) -> Option<&fontdb::Database> {
+ Some(&self.fontdb)
+ }
+
fn add_font(&mut self, _filename: String, font_data: Arc + Sync + Send>) {
self.fontdb
.load_font_source(fontdb::Source::Binary(font_data.clone()));
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::text::{text_fit_impl, text_wrap_impl};
+ use crate::{BreakLinesOpts, FontQuery, TextOverflow};
+
+ const INTER: &[u8] = include_bytes!("../../../examples/arm/media/Inter_18pt-ExtraBold.ttf");
+ const SOFIA: &[u8] = include_bytes!(
+ "../../../examples/teej-podcast/dynamic_media/SofiaSansSemiCondensed-VariableFont_wght.ttf"
+ );
+ // Noto Sans Arabic (OFL) cut down to the glyphs of "سلام".
+ const ARABIC: &[u8] =
+ include_bytes!("../../../examples/text-width/media/NotoSansArabic-subset.ttf");
+
+ fn fonts(files: &[&[u8]]) -> RendererFontSource {
+ let mut fontdb = fontdb::Database::new();
+ for data in files {
+ fontdb.load_font_data(data.to_vec());
+ }
+ RendererFontSource { fontdb }
+ }
+
+ impl RendererFontSource {
+ fn measure_text(&self, font: &FontQuery, text: &str) -> Option {
+ crate::text::shaped_text_width(&self.fontdb, font, text, None)
+ }
+ }
+
+ fn font(family: &str, size: usize, weight: u16) -> FontQuery<'_> {
+ FontQuery {
+ family,
+ size,
+ weight,
+ ..Default::default()
+ }
+ }
+
+ fn assert_near(actual: f32, expected: f32) {
+ assert!(
+ (actual - expected).abs() < 0.5,
+ "measured {actual}, expected {expected}"
+ );
+ }
+
+ #[test]
+ fn text_fit_keeps_kerned_text_that_fits() {
+ let fonts = fonts(&[INTER]);
+ let text = "AVAVAVAV";
+ assert_near(
+ fonts
+ .measure_text(&font("Inter 18pt", 100, 800), text)
+ .unwrap(),
+ 546.0,
+ );
+ let fitted = text_fit_impl(
+ font("Inter 18pt", 100, 800),
+ text,
+ 550,
+ TextOverflow::Ellipsis,
+ &fonts,
+ None,
+ );
+ assert_eq!(fitted.as_deref(), Some(text));
+ }
+
+ #[test]
+ fn rendering_reuses_measured_shapes() {
+ let fonts = fonts(&[INTER]);
+ let text_cache = crate::TextCache::new(10).unwrap();
+ let shapes = text_cache.shapes();
+ let query = font("Inter 18pt", 100, 800);
+ crate::text::shaped_text_width(&fonts.fontdb, &query, "AVAVAVAV", Some(&shapes)).unwrap();
+ assert_eq!(shapes.len(), 1);
+
+ let svg = r#""#;
+ let doc = usvgr::roxmltree::Document::parse(svg).unwrap();
+ let mut converter_cache = crate::TextCache::converter_cache(Some(&text_cache), 10);
+ usvgr::Tree::from_xmltree_with_cache(
+ &doc,
+ &usvgr::Options::default(),
+ &mut converter_cache,
+ &fonts.fontdb,
+ )
+ .unwrap();
+ assert_eq!(shapes.len(), 1, "the renderer shaped measured text again");
+ }
+
+ #[test]
+ fn tabs_and_line_breaks_measure_as_spaces() {
+ let fonts = fonts(&[INTER]);
+ let query = font("Inter 18pt", 46, 800);
+ let spaced = fonts.measure_text(&query, "a b").unwrap();
+ for text in ["a\tb", "a\nb", "a\r\nb"] {
+ assert_eq!(
+ fonts.measure_text(&query, text).unwrap(),
+ spaced,
+ "{text:?}"
+ );
+ }
+ }
+
+ #[test]
+ fn text_fit_truncates_text_wider_than_its_box() {
+ let fonts = fonts(&[INTER]);
+ let query = font("Inter 18pt", 46, 800);
+ let text = "The Byzantine Greeks call Eastern Rome";
+ assert!(fonts.measure_text(&query, text).unwrap() > 885.0);
+ let fitted = text_fit_impl(query, text, 885, TextOverflow::Ellipsis, &fonts, None).unwrap();
+ assert!(fitted.ends_with('…'), "{fitted}");
+ assert!(fonts.measure_text(&query, &fitted).unwrap() <= 885.0);
+ }
+
+ #[test]
+ fn wrapped_lines_are_measured_joined() {
+ let fonts = fonts(&[INTER]);
+ let query = font("Inter 18pt", 16, 800);
+ let wrapped = text_wrap_impl(
+ 0,
+ "a a a a a a a a a a",
+ &fonts,
+ BreakLinesOpts:: {
+ font: query,
+ width: 117,
+ ..Default::default()
+ },
+ None,
+ )
+ .unwrap();
+ assert!(wrapped.lines.len() > 1);
+ for line in &wrapped.lines {
+ let rendered = fonts.measure_text(&query, &line.words.join(" ")).unwrap();
+ assert!(rendered <= 117.0, "{:?} renders {rendered} px", line.words);
+ assert_eq!(line.width, rendered.round() as usize);
+ }
+ }
+
+ #[test]
+ fn measuring_follows_the_weight_axis() {
+ let fonts = fonts(&[SOFIA]);
+ let family = "Sofia Sans Semi Condensed";
+ assert_near(
+ fonts
+ .measure_text(&font(family, 40, 100), "Hamburgefonts")
+ .unwrap(),
+ 247.9,
+ );
+ assert_near(
+ fonts
+ .measure_text(&font(family, 40, 900), "Hamburgefonts")
+ .unwrap(),
+ 266.5,
+ );
+ }
+
+ #[test]
+ fn missing_glyphs_are_measured_in_the_fallback_font() {
+ let fonts = fonts(&[INTER, ARABIC]);
+ let fallback = fonts
+ .measure_text(&font("Inter 18pt", 40, 800), "سلام")
+ .unwrap();
+ let direct = fonts
+ .measure_text(&font("Noto Sans Arabic", 40, 400), "سلام")
+ .unwrap();
+ assert_near(fallback, direct);
+ }
+
+ #[test]
+ fn marks_do_not_widen_text() {
+ let fonts = fonts(&[ARABIC]);
+ let arabic = font("Noto Sans Arabic", 40, 400);
+ assert_near(
+ fonts.measure_text(&arabic, "اَ").unwrap(),
+ fonts.measure_text(&arabic, "ا").unwrap(),
+ );
+ }
+
+ #[test]
+ fn mixed_direction_text_is_measured_per_run() {
+ let fonts = fonts(&[INTER, ARABIC]);
+ let query = font("Inter 18pt", 40, 800);
+ let latin = fonts.measure_text(&query, "AV ").unwrap();
+ let arabic = fonts.measure_text(&query, "سلام").unwrap();
+ assert_near(
+ fonts.measure_text(&query, "AV سلام").unwrap(),
+ latin + arabic,
+ );
+ }
+}
diff --git a/fframes/src/text.rs b/fframes/src/text.rs
index 8f609cec..dab017bb 100644
--- a/fframes/src/text.rs
+++ b/fframes/src/text.rs
@@ -192,6 +192,9 @@ where
}
}
+/// Widths are tiny and a frame often measures every word.
+const TEXT_WIDTH_CACHE_MIN: usize = 1024;
+
/// This is only fframes cache for text related processing, the actual
/// glyph aliasing, text outlining happens in the rendering backend and
/// can have its own cache
@@ -200,23 +203,45 @@ where
pub struct TextCache {
pub(crate) breaks_cache: Rc>>,
pub(crate) text_width_cache: Rc>>,
+ pub(crate) shapes: usvgr::UsvgrTextShapeCache,
}
impl TextCache {
pub fn new(size: usize) -> Option {
if size > 0 {
Some(Self {
+ shapes: usvgr::UsvgrTextShapeCache::new(size.max(TEXT_WIDTH_CACHE_MIN))?,
breaks_cache: Rc::new(RefCell::new(LruCache::new(
NonZeroUsize::new(size).unwrap(),
))),
text_width_cache: Rc::new(RefCell::new(LruCache::new(
- NonZeroUsize::new(size).unwrap(),
+ NonZeroUsize::new(size.max(TEXT_WIDTH_CACHE_MIN)).unwrap(),
))),
})
} else {
None
}
}
+
+ /// Forgets every measured width and wrapped text, for when a font changes.
+ pub fn clear(&self) {
+ self.breaks_cache.borrow_mut().clear();
+ self.text_width_cache.borrow_mut().clear();
+ self.shapes.clear();
+ }
+
+ /// The shaped glyphs of measured text. A converter cache that holds them lays that text
+ /// out without shaping it again, see [`TextCache::converter_cache`].
+ pub fn shapes(&self) -> usvgr::UsvgrTextShapeCache {
+ self.shapes.clone()
+ }
+
+ /// A converter cache for the frames that measure text through `text_cache`.
+ pub fn converter_cache(text_cache: Option<&Self>, text_cache_capacity: usize) -> usvgr::Cache {
+ let mut cache = usvgr::Cache::new_with_text_cache(text_cache_capacity);
+ cache.usvgr_shape_cache = text_cache.map(Self::shapes);
+ cache
+ }
}
#[derive(Debug, Clone)]
@@ -336,18 +361,74 @@ fn char_width(
}
}
-fn calc_text_width(
+/// Width of `text` in pixels as usvgr lays it out when rendering: shaped per bidi run, with
+/// fallback fonts and the variation axes the renderer applies. Only shaping runs, and the
+/// shaped glyphs go into `shapes`, where the renderer finds them when it lays the text out.
+pub(crate) fn shaped_text_width(
+ fontdb: &usvgr::fontdb::Database,
+ font: &FontQuery,
text: &str,
+ shapes: Option<&usvgr::UsvgrTextShapeCache>,
+) -> Option {
+ let style = match font.style {
+ FontStyle::Normal => usvgr::FontStyle::Normal,
+ FontStyle::Italic => usvgr::FontStyle::Italic,
+ FontStyle::Oblique => usvgr::FontStyle::Oblique,
+ };
+ let stretch = match font.stretch {
+ FontStretch::UltraCondensed => usvgr::FontStretch::UltraCondensed,
+ FontStretch::ExtraCondensed => usvgr::FontStretch::ExtraCondensed,
+ FontStretch::Condensed => usvgr::FontStretch::Condensed,
+ FontStretch::SemiCondensed => usvgr::FontStretch::SemiCondensed,
+ FontStretch::Normal => usvgr::FontStretch::Normal,
+ FontStretch::SemiExpanded => usvgr::FontStretch::SemiExpanded,
+ FontStretch::Expanded => usvgr::FontStretch::Expanded,
+ FontStretch::ExtraExpanded => usvgr::FontStretch::ExtraExpanded,
+ FontStretch::UltraExpanded => usvgr::FontStretch::UltraExpanded,
+ };
+ let usvgr_font = usvgr::Font::new(
+ vec![usvgr::FontFamily::Named(font.family.to_owned())],
+ style,
+ stretch,
+ font.weight,
+ Vec::new(),
+ );
+ // The SVG parser turns tabs and line breaks into spaces before the renderer shapes the text.
+ let text = if text.contains(['\t', '\n', '\r']) {
+ Cow::Owned(text.replace("\r\n", " ").replace(['\t', '\n', '\r'], " "))
+ } else {
+ Cow::Borrowed(text)
+ };
+ usvgr::text_advance(
+ &text,
+ &usvgr_font,
+ font.size as f32,
+ &usvgr::FontResolver::default(),
+ fontdb,
+ shapes,
+ )
+}
+
+/// The renderer's width when the font source has a font database, per-character advances
+/// otherwise.
+fn text_width_px(
+ text: &str,
+ font: &FontQuery,
+ font_source: &dyn FontSource,
font_face: &dyn FontFace,
- font_size: usize,
font_variant: FontVariant,
-) -> usize {
+ shapes: Option<&usvgr::UsvgrTextShapeCache>,
+) -> f32 {
match text {
- "" | "\n" | "\r" => 0,
- text => text
- .chars()
- .map(|char| char_width(char, font_face, font_size, font_variant))
- .sum(),
+ "" | "\n" | "\r" => 0.0,
+ text => font_source
+ .fontdb()
+ .and_then(|fontdb| shaped_text_width(fontdb, font, text, shapes))
+ .unwrap_or_else(|| {
+ text.chars()
+ .map(|char| char_width(char, font_face, font.size, font_variant))
+ .sum::() as f32
+ }),
}
}
@@ -359,6 +440,7 @@ pub(crate) fn text_fit_impl<'a>(
max_width: usize,
overflow: TextOverflow,
font_source: &'a (dyn FontSource<'a> + 'a),
+ shapes: Option<&usvgr::UsvgrTextShapeCache>,
) -> Option> {
let font_face = font_source.resolve_font(
font_query.family,
@@ -367,32 +449,52 @@ pub(crate) fn text_fit_impl<'a>(
font_query.stretch,
)?;
let font_variant = font_face.font_variant(font_query.size)?;
- let width =
- |text: &str| calc_text_width(text, font_face.as_ref(), font_query.size, font_variant);
-
- let marker = overflow.marker();
- let marker_width = width(marker);
-
- // One pass over the glyphs: `fits_end` is the end of the longest prefix
- // that leaves room for the marker, `total` the width of the whole text.
- let mut total = 0;
- let mut fits_end = 0;
- for (index, char) in value.char_indices() {
- total += char_width(char, font_face.as_ref(), font_query.size, font_variant);
- if total + marker_width <= max_width {
- fits_end = index + char.len_utf8();
- }
- }
+ let max_width = max_width as f32;
+ let width = |text: &str| {
+ text_width_px(
+ text,
+ &font_query,
+ font_source,
+ font_face.as_ref(),
+ font_variant,
+ shapes,
+ )
+ };
- if total <= max_width {
+ if width(value) <= max_width {
return Some(Cow::Borrowed(value));
}
- if marker_width > max_width {
+ let marker = overflow.marker();
+ if width(marker) > max_width {
// Not even the marker fits.
return Some(Cow::Borrowed(""));
}
- let kept = &value[..fits_end];
+ // The kept prefix and the marker are measured together, as they render. Binary search over
+ // the number of kept characters: measuring is a layout, so each probe counts.
+ let ends: Vec = value
+ .char_indices()
+ .map(|(i, c)| i + c.len_utf8())
+ .collect();
+ let fits = |end: usize| {
+ let kept = &value[..end];
+ if marker.is_empty() {
+ width(kept) <= max_width
+ } else {
+ width(&format!("{}{marker}", kept.trim_end())) <= max_width
+ }
+ };
+ let (mut low, mut high) = (0, ends.len());
+ while low < high {
+ let mid = (low + high).div_ceil(2);
+ if fits(ends[mid - 1]) {
+ low = mid;
+ } else {
+ high = mid - 1;
+ }
+ }
+
+ let kept = &value[..low.checked_sub(1).map_or(0, |i| ends[i])];
if marker.is_empty() {
return Some(Cow::Borrowed(kept));
}
@@ -406,6 +508,7 @@ pub(crate) fn text_get_width_impl<'a>(
font_query: FontQuery,
text: &str,
font_source: &'a (dyn FontSource<'a> + 'a),
+ shapes: Option<&usvgr::UsvgrTextShapeCache>,
) -> Option {
let font_face = font_source.resolve_font(
font_query.family,
@@ -415,12 +518,17 @@ pub(crate) fn text_get_width_impl<'a>(
)?;
let font_variant = font_face.font_variant(font_query.size)?;
- Some(calc_text_width(
- text,
- font_face.as_ref(),
- font_query.size,
- font_variant,
- ))
+ Some(
+ text_width_px(
+ text,
+ &font_query,
+ font_source,
+ font_face.as_ref(),
+ font_variant,
+ shapes,
+ )
+ .round() as usize,
+ )
}
pub(crate) fn text_wrap_impl<
@@ -433,6 +541,7 @@ pub(crate) fn text_wrap_impl<
value: &'b str,
font_source: &'a (dyn FontSource<'a> + 'a),
options: BreakLinesOpts<'a, TX, TY>,
+ shapes: Option<&usvgr::UsvgrTextShapeCache>,
) -> Option {
let BreakLinesOpts {
width,
@@ -452,46 +561,49 @@ pub(crate) fn text_wrap_impl<
};
let font_variant = font_face.font_variant(font.size)?;
- let space_width = match font_variant {
- crate::FontVariant::Monospaced(mono_width) => mono_width,
- crate::FontVariant::Variable => font_face.resolve_char_width(font.size, ' ')?,
+ let max_width = width as f32;
+ let measure = |text: &str| {
+ text_width_px(
+ text,
+ &font,
+ font_source,
+ font_face.as_ref(),
+ font_variant,
+ shapes,
+ )
};
- let mut structure: Vec<(Vec, usize)> = vec![(vec![], 0usize)];
+ // Lines are measured joined, as they render, so spaces and kerning across words count.
+ let mut structure: Vec<(Vec, f32)> = vec![(vec![], 0.0)];
let line_height_in_px = line_height * font.size as f32;
for line in value.split('\n') {
let mut is_first_word_in_line = true;
for word in line.split_whitespace() {
- let word_width = calc_text_width(word, font_face.as_ref(), font.size, font_variant);
let (last_line, last_line_width) = structure.last_mut()?;
-
- // The first word of a line is never preceded by a space.
- let separator_width = if *last_line_width == 0 {
- 0
+ let joined = if last_line.is_empty() {
+ word.to_owned()
} else {
- space_width
+ format!("{} {word}", last_line.join(" "))
};
+ let joined_width = measure(&joined);
- if *last_line_width + separator_width + word_width > width && !last_line.is_empty() {
- structure.push((vec![word.to_owned()], word_width));
+ if joined_width > max_width && !last_line.is_empty() {
+ structure.push((vec![word.to_owned()], measure(word)));
} else {
- *last_line_width += separator_width + word_width;
+ *last_line_width = joined_width;
last_line.push(word.to_owned());
}
is_first_word_in_line = false;
}
if !is_first_word_in_line {
- structure.push((vec![], 0usize));
+ structure.push((vec![], 0.0));
}
}
- if let Some((last_words, last_width)) = structure.last()
- && last_words.is_empty()
- && *last_width == 0
- {
+ if structure.last().is_some_and(|(words, _)| words.is_empty()) {
structure.pop();
}
@@ -499,6 +611,7 @@ pub(crate) fn text_wrap_impl<
.into_iter()
.enumerate()
.map(|(index, (words, line_width))| {
+ let line_width = line_width.round() as usize;
let dx = match align {
TextAlign::Left => 0,
TextAlign::Center => width.saturating_sub(line_width) / 2,
@@ -547,17 +660,18 @@ pub fn estimate_text_width<'a>(
font_stretch,
} = options;
- let font_face =
- ctx.font_source?
- .resolve_font(font_family, font_weight, font_style, font_stretch)?;
-
- let font_variant = font_face.font_variant(font_size)?;
- Some(calc_text_width(
+ text_get_width_impl(
+ FontQuery {
+ family: font_family,
+ size: font_size,
+ weight: font_weight,
+ style: font_style,
+ stretch: font_stretch,
+ },
text,
- font_face.as_ref(),
- font_size,
- font_variant,
- ))
+ ctx.font_source?,
+ None,
+ )
}
#[cfg(test)]
@@ -614,8 +728,15 @@ mod tests {
max_width: usize,
overflow: TextOverflow,
) -> Cow<'a, str> {
- text_fit_impl(FontQuery::default(), value, max_width, overflow, fonts)
- .expect("the fixed width font always resolves")
+ text_fit_impl(
+ FontQuery::default(),
+ value,
+ max_width,
+ overflow,
+ fonts,
+ None,
+ )
+ .expect("the fixed width font always resolves")
}
#[test]
@@ -711,7 +832,7 @@ mod tests {
"Ünïcö…"
);
assert_eq!(
- text_get_width_impl(FontQuery::default(), "Ünïcödé", &MONOSPACED),
+ text_get_width_impl(FontQuery::default(), "Ünïcödé", &MONOSPACED, None),
Some(7 * CHAR_WIDTH)
);
}
diff --git a/scripts/e2e.mjs b/scripts/e2e.mjs
index 937410a0..6067e9a5 100644
--- a/scripts/e2e.mjs
+++ b/scripts/e2e.mjs
@@ -63,6 +63,8 @@ const allExamples = [
{ dir: "podcast", pkg: "podcast-example", bin: "podcast_example_bin", frames: "10s,40s" },
{ dir: "shaders", pkg: "shaders-example", bin: "shaders_example_bin", frames: "2s,6s", skia: true },
{ dir: "signal-lab", pkg: "signal-lab", bin: "signal-lab", frames: "ProductScene@3s,DataScene@3s,SystemScene@3s" },
+ // Boxes sized by text_width and a paragraph wrapped by text_break_lines, in Greek and kerned Latin.
+ { dir: "text-width", pkg: "text-width-example", bin: "text-width-example", frames: "0.5s" },
{ dir: "teej-podcast", pkg: "teej_podcast_example", bin: "teej_podcast_example_bin", frames: "10s,2:00" },
{ dir: "tiktok", pkg: "tiktok-example", bin: "tiktok_example_bin", frames: "10s,30s" },
];