diff --git a/Cargo.lock b/Cargo.lock
index 008a2910..d3424005 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4064,14 +4064,6 @@ 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"
diff --git a/Cargo.toml b/Cargo.toml
index 34b5ffde..e6370eac 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -45,7 +45,6 @@ members = [
"examples/neon-triangle",
"examples/neon-triangle/editor/editor-bridge",
"examples/arm",
- "examples/text-width",
# maybe clean this up?
# "fframes-lyon-renderer",
diff --git a/e2e/snapshots/captions/1.5s.png b/e2e/snapshots/captions/1.5s.png
index 1bece6b3..cc011384 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 e406d3b8..bd5b28b5 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 9fba8eb8..05f022d6 100644
Binary files a/e2e/snapshots/captions/7s.png and b/e2e/snapshots/captions/7s.png differ
diff --git a/e2e/snapshots/text-width/0.5s.png b/e2e/snapshots/text-width/0.5s.png
deleted file mode 100644
index 133f1fd5..00000000
Binary files a/e2e/snapshots/text-width/0.5s.png and /dev/null differ
diff --git a/examples/text-width/Cargo.toml b/examples/text-width/Cargo.toml
deleted file mode 100644
index a38f27ce..00000000
--- a/examples/text-width/Cargo.toml
+++ /dev/null
@@ -1,24 +0,0 @@
-[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
deleted file mode 100644
index c374a56c..00000000
Binary files a/examples/text-width/media/NotoSansArabic-subset.ttf and /dev/null differ
diff --git a/examples/text-width/media/SofiaSansSemiCondensed-VariableFont_wght.ttf b/examples/text-width/media/SofiaSansSemiCondensed-VariableFont_wght.ttf
deleted file mode 100644
index 59ba155e..00000000
Binary files a/examples/text-width/media/SofiaSansSemiCondensed-VariableFont_wght.ttf and /dev/null differ
diff --git a/examples/text-width/src/lib.rs b/examples/text-width/src/lib.rs
deleted file mode 100644
index e0a2239f..00000000
--- a/examples/text-width/src/lib.rs
+++ /dev/null
@@ -1,117 +0,0 @@
-//! 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
deleted file mode 100644
index 307ce131..00000000
--- a/examples/text-width/src/main.rs
+++ /dev/null
@@ -1,25 +0,0 @@
-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 024ab610..fe16f6d6 100644
--- a/fframes-editor-controller/src/wasm_editor.rs
+++ b/fframes-editor-controller/src/wasm_editor.rs
@@ -402,8 +402,6 @@ 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 8bd05536..f6f58e87 100644
--- a/fframes-editor-controller/src/wasm_font_source.rs
+++ b/fframes-editor-controller/src/wasm_font_source.rs
@@ -47,10 +47,6 @@ 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 {
@@ -58,8 +54,6 @@ impl WasmFontSource {
Self {
static_fonts: vec![],
data: HashMap::new(),
- fontdb: fframes::usvgr::fontdb::Database::new(),
- face_ids: HashMap::new(),
}
}
@@ -87,18 +81,6 @@ 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)
}
@@ -155,10 +137,6 @@ impl<'a> fframes::FontSource<'a> for WasmFontSource {
}))
}
- fn measure_text(&self, font: &fframes::FontQuery, text: &str) -> Option {
- fframes::measure_text_width(&self.fontdb, font, text)
- }
-
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/src/font_data.rs b/fframes/src/font_data.rs
index dd5250b1..ec38fb3a 100644
--- a/fframes/src/font_data.rs
+++ b/fframes/src/font_data.rs
@@ -100,10 +100,4 @@ pub trait FontSource<'a>: Sync + Send + std::fmt::Debug {
font_style: FontStyle,
font_stretch: FontStretch,
) -> Option + 'a>>;
-
- /// Width of `text` in pixels as the renderer lays it out, see
- /// [`crate::measure_text_width`]. `None` falls back to per-character advances.
- fn measure_text(&self, _font: &crate::FontQuery, _text: &str) -> Option {
- None
- }
}
diff --git a/fframes/src/renderer/renderer_font_source.rs b/fframes/src/renderer/renderer_font_source.rs
index cacfcee2..b4edf930 100644
--- a/fframes/src/renderer/renderer_font_source.rs
+++ b/fframes/src/renderer/renderer_font_source.rs
@@ -97,157 +97,8 @@ impl<'a> crate::FontSource<'a> for RendererFontSource {
}))
}
- fn measure_text(&self, font: &crate::FontQuery, text: &str) -> Option {
- crate::measure_text_width(&self.fontdb, font, text)
- }
-
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, FontSource, 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 }
- }
-
- 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,
- );
- assert_eq!(fitted.as_deref(), Some(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).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()
- },
- )
- .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 21c9a58a..8f609cec 100644
--- a/fframes/src/text.rs
+++ b/fframes/src/text.rs
@@ -192,9 +192,6 @@ 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
@@ -213,19 +210,13 @@ impl TextCache {
NonZeroUsize::new(size).unwrap(),
))),
text_width_cache: Rc::new(RefCell::new(LruCache::new(
- NonZeroUsize::new(size.max(TEXT_WIDTH_CACHE_MIN)).unwrap(),
+ NonZeroUsize::new(size).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();
- }
}
#[derive(Debug, Clone)]
@@ -345,80 +336,18 @@ fn char_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. The text is laid out twice, from
-/// its start and from its end at `x = 0`: every glyph moves by exactly the advance width between
-/// the two, so glyph offsets such as mark positioning cancel out.
-pub fn measure_text_width(
- fontdb: &usvgr::fontdb::Database,
- font: &FontQuery,
- text: &str,
-) -> Option {
- fn escape(value: &str) -> String {
- value
- .replace('&', "&")
- .replace('<', "<")
- .replace('>', ">")
- .replace('"', """)
- }
- fn texts<'t>(group: &'t usvgr::Group, found: &mut Vec<&'t usvgr::Text>) {
- for node in group.children() {
- match node {
- usvgr::Node::Text(text) => found.push(text),
- usvgr::Node::Group(group) => texts(group, found),
- _ => {}
- }
- }
- }
- fn first_glyph_x(text: &usvgr::Text) -> Option {
- let glyph = text.layouted().first()?.positioned_glyphs.first()?;
- Some(glyph.transform().tx)
- }
-
- if text.is_empty() {
- return Some(0.0);
- }
- let style = match font.style {
- FontStyle::Normal => "normal",
- FontStyle::Italic => "italic",
- FontStyle::Oblique => "oblique",
- };
- let text = format!(
- r#"xml:space="preserve" font-family="{}" font-size="{}" font-weight="{}" font-style="{style}" font-stretch="{}">{}"#,
- escape(font.family),
- font.size,
- font.weight,
- font.stretch,
- escape(text),
- );
- let svg = format!(
- r#"