From 48eda6bd7d8761366d10799235f09be855200e0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sun, 23 Nov 2025 06:22:24 +0100 Subject: [PATCH] Draft metrics hinting support --- benches/layout.rs | 2 +- benches/text_shaping_benchmarks.rs | 10 +++++----- examples/editor-test/src/main.rs | 2 +- examples/editor/src/main.rs | 1 + examples/multiview/src/main.rs | 2 +- examples/rich-text/src/main.rs | 2 +- examples/terminal/src/main.rs | 2 +- src/buffer.rs | 15 ++++++++++----- src/buffer_line.rs | 2 ++ src/lib.rs | 2 +- src/shape.rs | 10 ++++++++++ tests/common/mod.rs | 2 +- tests/editor_modified_state.rs | 2 +- tests/wrap_stability.rs | 7 ++++--- tests/wrap_word_fallback.rs | 2 +- 15 files changed, 41 insertions(+), 22 deletions(-) diff --git a/benches/layout.rs b/benches/layout.rs index d41dc46..2d23b8d 100644 --- a/benches/layout.rs +++ b/benches/layout.rs @@ -9,7 +9,7 @@ fn load_font_system(c: &mut Criterion) { fn layout(c: &mut Criterion) { let mut fs = ct::FontSystem::new(); - let mut buffer = ct::Buffer::new(&mut fs, ct::Metrics::new(10.0, 10.0)); + let mut buffer = ct::Buffer::new(&mut fs, ct::Metrics::new(10.0, 10.0), false); buffer.set_size(&mut fs, Some(80.0), None); for (wrap_name, wrap) in &[ diff --git a/benches/text_shaping_benchmarks.rs b/benches/text_shaping_benchmarks.rs index 097b552..38bbc6e 100644 --- a/benches/text_shaping_benchmarks.rs +++ b/benches/text_shaping_benchmarks.rs @@ -4,7 +4,7 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; fn bench_ascii_fast_path(c: &mut Criterion) { let mut fs = ct::FontSystem::new(); - let mut buffer = ct::Buffer::new(&mut fs, ct::Metrics::new(14.0, 20.0)); + let mut buffer = ct::Buffer::new(&mut fs, ct::Metrics::new(14.0, 20.0), false); buffer.set_size(&mut fs, Some(500.0), None); let ascii_text = "Pure ASCII text for BidiParagraphs optimization testing.\n".repeat(50); @@ -25,7 +25,7 @@ fn bench_ascii_fast_path(c: &mut Criterion) { fn bench_bidi_processing(c: &mut Criterion) { let mut fs = ct::FontSystem::new(); - let mut buffer = ct::Buffer::new(&mut fs, ct::Metrics::new(14.0, 20.0)); + let mut buffer = ct::Buffer::new(&mut fs, ct::Metrics::new(14.0, 20.0), false); buffer.set_size(&mut fs, Some(500.0), None); let bidi_text = "Mixed English and العربية النص العربي text for BiDi testing.\nThis tests adjust_levels and combined BiDi optimizations.\n".repeat(30); @@ -46,7 +46,7 @@ fn bench_bidi_processing(c: &mut Criterion) { fn bench_lang_mixed(c: &mut Criterion) { let mut fs = ct::FontSystem::new(); - let mut buffer = ct::Buffer::new(&mut fs, ct::Metrics::new(14.0, 20.0)); + let mut buffer = ct::Buffer::new(&mut fs, ct::Metrics::new(14.0, 20.0), false); buffer.set_size(&mut fs, Some(500.0), None); let bidi_text = include_str!("../sample/hello.txt"); @@ -69,7 +69,7 @@ fn bench_lang_mixed(c: &mut Criterion) { fn bench_layout_heavy(c: &mut Criterion) { let mut fs = ct::FontSystem::new(); - let mut buffer = ct::Buffer::new(&mut fs, ct::Metrics::new(14.0, 20.0)); + let mut buffer = ct::Buffer::new(&mut fs, ct::Metrics::new(14.0, 20.0), false); buffer.set_size(&mut fs, Some(500.0), None); let layout_text = "This is a very long line that will wrap multiple times and stress the reorder optimization through intensive layout processing with comprehensive buffer reuse testing. ".repeat(30); @@ -90,7 +90,7 @@ fn bench_layout_heavy(c: &mut Criterion) { fn bench_combined_stress(c: &mut Criterion) { let mut fs = ct::FontSystem::new(); - let mut buffer = ct::Buffer::new(&mut fs, ct::Metrics::new(14.0, 20.0)); + let mut buffer = ct::Buffer::new(&mut fs, ct::Metrics::new(14.0, 20.0), false); buffer.set_size(&mut fs, Some(500.0), None); let stress_text = format!("{}\n{}\n{}\n{}\n", diff --git a/examples/editor-test/src/main.rs b/examples/editor-test/src/main.rs index 85bae2b..4ce62ec 100644 --- a/examples/editor-test/src/main.rs +++ b/examples/editor-test/src/main.rs @@ -71,7 +71,7 @@ fn main() { ]; let font_size_default = 1; // Body - let mut buffer = Buffer::new(&mut font_system, font_sizes[font_size_default]); + let mut buffer = Buffer::new(&mut font_system, font_sizes[font_size_default], false); buffer .borrow_with(&mut font_system) .set_size(Some(window.width() as f32), Some(window.height() as f32)); diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index d0e5ff0..b429d1b 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -45,6 +45,7 @@ fn main() { Buffer::new( &mut font_system, font_sizes[font_size_i].scale(display_scale), + false, ), &syntax_system, "base16-eighties.dark", diff --git a/examples/multiview/src/main.rs b/examples/multiview/src/main.rs index e292fe9..fdab7f8 100644 --- a/examples/multiview/src/main.rs +++ b/examples/multiview/src/main.rs @@ -25,7 +25,7 @@ fn main() { let mut swash_cache = SwashCache::new(); - let mut buffer = Buffer::new_empty(Metrics::new(14.0, 20.0)); + let mut buffer = Buffer::new_empty(Metrics::new(14.0, 20.0), false); let mut buffer = buffer.borrow_with(&mut font_system); diff --git a/examples/rich-text/src/main.rs b/examples/rich-text/src/main.rs index 9d51bc0..d185978 100644 --- a/examples/rich-text/src/main.rs +++ b/examples/rich-text/src/main.rs @@ -172,7 +172,7 @@ fn main() { let mut display_scale = window.scale_factor() as f32; let metrics = Metrics::new(32.0, 44.0); - let mut editor = Editor::new(Buffer::new_empty(metrics.scale(display_scale))); + let mut editor = Editor::new(Buffer::new_empty(metrics.scale(display_scale), false)); let mut editor = editor.borrow_with(&mut font_system); editor.with_buffer_mut(|buffer| { buffer.set_size( diff --git a/examples/terminal/src/main.rs b/examples/terminal/src/main.rs index 08596e7..4251a45 100644 --- a/examples/terminal/src/main.rs +++ b/examples/terminal/src/main.rs @@ -20,7 +20,7 @@ fn main() { let metrics = Metrics::new(FONT_SIZE, LINE_HEIGHT); // A Buffer provides shaping and layout for a UTF-8 string, create one per text widget - let mut buffer = Buffer::new(&mut font_system, metrics); + let mut buffer = Buffer::new(&mut font_system, metrics, false); let mut buffer = buffer.borrow_with(&mut font_system); diff --git a/src/buffer.rs b/src/buffer.rs index e4aa575..a82339a 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -216,6 +216,7 @@ pub struct Buffer { wrap: Wrap, monospace_width: Option, tab_width: u16, + hint: bool, } impl Clone for Buffer { @@ -230,6 +231,7 @@ impl Clone for Buffer { wrap: self.wrap, monospace_width: self.monospace_width, tab_width: self.tab_width, + hint: self.hint, } } } @@ -246,7 +248,7 @@ impl Buffer { /// # Panics /// /// Will panic if `metrics.line_height` is zero. - pub fn new_empty(metrics: Metrics) -> Self { + pub fn new_empty(metrics: Metrics, hint: bool) -> Self { assert_ne!(metrics.line_height, 0.0, "line height cannot be 0"); Self { lines: Vec::new(), @@ -258,6 +260,7 @@ impl Buffer { wrap: Wrap::WordOrGlyph, monospace_width: None, tab_width: 8, + hint, } } @@ -266,8 +269,8 @@ impl Buffer { /// # Panics /// /// Will panic if `metrics.line_height` is zero. - pub fn new(font_system: &mut FontSystem, metrics: Metrics) -> Self { - let mut buffer = Self::new_empty(metrics); + pub fn new(font_system: &mut FontSystem, metrics: Metrics, hint: bool) -> Self { + let mut buffer = Self::new_empty(metrics, hint); buffer.set_text(font_system, "", &Attrs::new(), Shaping::Advanced, None); buffer } @@ -297,6 +300,7 @@ impl Buffer { self.wrap, self.monospace_width, self.tab_width, + self.hint, ); } } @@ -540,6 +544,7 @@ impl Buffer { self.wrap, self.monospace_width, self.tab_width, + self.hint, )) } @@ -719,7 +724,7 @@ impl Buffer { /// ``` /// # use cosmic_text::{Attrs, Buffer, Family, FontSystem, Metrics, Shaping}; /// # let mut font_system = FontSystem::new(); - /// let mut buffer = Buffer::new_empty(Metrics::new(32.0, 44.0)); + /// let mut buffer = Buffer::new_empty(Metrics::new(32.0, 44.0), false); /// let attrs = Attrs::new().family(Family::Serif); /// buffer.set_rich_text( /// &mut font_system, @@ -1448,7 +1453,7 @@ impl BorrowedWithFontSystem<'_, Buffer> { /// ``` /// # use cosmic_text::{Attrs, Buffer, Family, FontSystem, Metrics, Shaping}; /// # let mut font_system = FontSystem::new(); - /// let mut buffer = Buffer::new_empty(Metrics::new(32.0, 44.0)); + /// let mut buffer = Buffer::new_empty(Metrics::new(32.0, 44.0), false); /// let attrs = Attrs::new().family(Family::Serif); /// buffer.set_rich_text( /// &mut font_system, diff --git a/src/buffer_line.rs b/src/buffer_line.rs index d8689fd..37d21d9 100644 --- a/src/buffer_line.rs +++ b/src/buffer_line.rs @@ -243,6 +243,7 @@ impl BufferLine { wrap: Wrap, match_mono_width: Option, tab_width: u16, + hint: bool, ) -> &[LayoutLine] { if self.layout_opt.is_unused() { let align = self.align; @@ -259,6 +260,7 @@ impl BufferLine { align, &mut layout, match_mono_width, + hint, ); self.layout_opt.set_used(layout); } diff --git a/src/lib.rs b/src/lib.rs index 0cad691..682e779 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,7 +24,7 @@ //! let metrics = Metrics::new(14.0, 20.0); //! //! // A Buffer provides shaping and layout for a UTF-8 string, create one per text widget -//! let mut buffer = Buffer::new(&mut font_system, metrics); +//! let mut buffer = Buffer::new(&mut font_system, metrics, false); //! //! // Borrow buffer together with the font system for more convenient method calls //! let mut buffer = buffer.borrow_with(&mut font_system); diff --git a/src/shape.rs b/src/shape.rs index 9216592..0399964 100644 --- a/src/shape.rs +++ b/src/shape.rs @@ -1152,6 +1152,7 @@ impl ShapeLine { wrap: Wrap, align: Option, match_mono_width: Option, + hint: bool, ) -> Vec { let mut lines = Vec::with_capacity(1); self.layout_to_buffer( @@ -1162,6 +1163,7 @@ impl ShapeLine { align, &mut lines, match_mono_width, + hint, ); lines } @@ -1175,6 +1177,7 @@ impl ShapeLine { align: Option, layout_lines: &mut Vec, match_mono_width: Option, + hint: bool, ) { fn add_to_visual_line( vl: &mut VisualLine, @@ -1550,6 +1553,10 @@ impl ShapeLine { x += alignment_correction; } + if hint { + x = x.round(); + } + // TODO: Only certain `is_whitespace` chars are typically expanded but this is what is // currently used to compute `visual_line.spaces`. // @@ -1628,6 +1635,9 @@ impl ShapeLine { // Round to nearest monospace width x_advance = ((x_advance / match_em_width).round()) * match_em_width; } + if hint { + x_advance = x_advance.round(); + } if self.rtl { x -= x_advance; } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 6dae78b..3181fdb 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -85,7 +85,7 @@ impl DrawTestCfg { let mut font_system = FontSystem::new_with_locale_and_db("En-US".into(), font_db); let mut swash_cache = SwashCache::new(); let metrics = Metrics::new(self.font_size, self.line_height); - let mut buffer = Buffer::new(&mut font_system, metrics); + let mut buffer = Buffer::new(&mut font_system, metrics, false); let mut buffer = buffer.borrow_with(&mut font_system); let margins = 5; buffer.set_size( diff --git a/tests/editor_modified_state.rs b/tests/editor_modified_state.rs index b3f9e07..d8be8d2 100644 --- a/tests/editor_modified_state.rs +++ b/tests/editor_modified_state.rs @@ -13,7 +13,7 @@ fn editor() -> ViEditor<'static, 'static> { let line_height = (font_size * 1.4).ceil(); let metrics = Metrics::new(font_size, line_height); - let buffer = Buffer::new_empty(metrics); + let buffer = Buffer::new_empty(metrics, false); let editor = SyntaxEditor::new( buffer, SYNTAX_SYSTEM.get_or_init(SyntaxSystem::new), diff --git a/tests/wrap_stability.rs b/tests/wrap_stability.rs index 781e357..aed22a0 100644 --- a/tests/wrap_stability.rs +++ b/tests/wrap_stability.rs @@ -23,14 +23,15 @@ fn stable_wrap() { let mut check_wrap = |text: &_, wrap, align_opt, start_width_opt| { let line = ShapeLine::new(&mut font_system, text, &attrs, Shaping::Advanced, 8); - let layout_unbounded = line.layout(font_size, start_width_opt, wrap, align_opt, None); + let layout_unbounded = + line.layout(font_size, start_width_opt, wrap, align_opt, None, false); let max_width = layout_unbounded.iter().map(|l| l.w).fold(0.0, f32::max); let new_limit = match start_width_opt { Some(start_width) => f32::min(start_width, max_width), None => max_width, }; - let layout_bounded = line.layout(font_size, Some(new_limit), wrap, align_opt, None); + let layout_bounded = line.layout(font_size, Some(new_limit), wrap, align_opt, None, false); let bounded_max_width = layout_bounded.iter().map(|l| l.w).fold(0.0, f32::max); // For debugging: @@ -99,7 +100,7 @@ fn wrap_extra_line() { let mut font_system = FontSystem::new(); let metrics = Metrics::new(14.0, 20.0); - let mut buffer = Buffer::new(&mut font_system, metrics); + let mut buffer = Buffer::new(&mut font_system, metrics, false); let mut buffer = buffer.borrow_with(&mut font_system); diff --git a/tests/wrap_word_fallback.rs b/tests/wrap_word_fallback.rs index db7f80a..8493bf9 100644 --- a/tests/wrap_word_fallback.rs +++ b/tests/wrap_word_fallback.rs @@ -10,7 +10,7 @@ fn wrap_word_fallback() { font_system.db_mut().load_font_data(font); let metrics = Metrics::new(14.0, 20.0); - let mut buffer = Buffer::new(&mut font_system, metrics); + let mut buffer = Buffer::new(&mut font_system, metrics, false); let mut buffer = buffer.borrow_with(&mut font_system);