Repository navigation
fix(text): measure shaped text width instead of summing rounded advances #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dmtrKovalenko
merged 6 commits into
dmtrKovalenko:main
from
silouanwright:fix/shaped-text-width
Nov 15, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3f0d82c
fix(text): measure shaped text width instead of summing rounded advances
silouanwright ea6e8be
fix(text): measure with the renderer's own text layout
silouanwright 28df544
fix(editor): share static font bytes with fontdb and replace faces on…
silouanwright 0472e03
fix(editor): clear text caches when a font is ingested
silouanwright 2f5c769
fix(text): measure between start- and end-anchored layouts so glyph o…
silouanwright c7d2b40
test(text): use a left-shifted fatha so the mark test catches the old…
silouanwright File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
Binary file not shown.
Binary file added
BIN
+337 KB
examples/text-width/media/SofiaSansSemiCondensed-VariableFont_wght.ttf
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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!(<g> | ||
| <rect x={x} y={y - size * 0.8} width={width} height={size} fill="#1f3a5f" /> | ||
| <text x={x} y={y} font-family={font.family} font-weight={font.weight} font-size={size} fill="#f4f7fb">{text}</text> | ||
| </g>) | ||
| } | ||
|
|
||
| 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::<Vec<_>>(); | ||
| 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!( | ||
| <svg xmlns="http://www.w3.org/2000/svg" width="1920" height="1080" viewBox="0 0 1920 1080"> | ||
| <rect width="1920" height="1080" fill="#0c1423" /> | ||
| <text x="120" y="120" font-family={INTER} font-weight="800" font-size="40" fill="#91a3b9">"text_width, text_fit and text_break_lines"</text> | ||
| {rows} | ||
| <rect x="1040" y="209" width={FIT_WIDTH} height="64" fill="none" stroke="#e76f51" stroke-width="2" /> | ||
| <text x="1040" y="260" font-family={INTER} font-weight="800" font-size="64" fill="#f4f7fb">{fitted}</text> | ||
| <path d={format!("M {} 600 V 1000", 120 + PARAGRAPH_WIDTH)} stroke="#e76f51" stroke-width="2" /> | ||
| {paragraph} | ||
| </svg> | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.