Wrap native text within occurrence constraints and preserve paint bounds
Verify Tessera GUI / verify (push) Failing after 7m33s

This commit is contained in:
2026-10-22 12:24:23 +08:00
parent 25275b515c
commit 7b5d3940fd
13 changed files with 470 additions and 45 deletions
+68 -5
View File
@@ -59,6 +59,7 @@ struct TextKey {
font_size: u32,
line_height: u32,
family: TextFamily,
wrap_width: Option<u32>,
}
struct PreparedText {
@@ -114,11 +115,16 @@ impl TextEngine {
}
fn prepare(&mut self, value: &str, style: TextStyle) -> TextKey {
self.prepare_width(value, style, None)
}
fn prepare_width(&mut self, value: &str, style: TextStyle, wrap_width: Option<f32>) -> TextKey {
let key = TextKey {
value: value.to_owned(),
font_size: style.font_size.to_bits(),
line_height: style.line_height.to_bits(),
family: style.family,
wrap_width: wrap_width.map(f32::to_bits),
};
if self.buffers.contains_key(&key) {
return key;
@@ -141,8 +147,12 @@ impl TextEngine {
&mut self.fonts,
Metrics::new(style.font_size, style.line_height),
);
buffer.set_wrap(Wrap::None);
buffer.set_size(None, None);
buffer.set_wrap(if wrap_width.is_some() {
Wrap::WordOrGlyph
} else {
Wrap::None
});
buffer.set_size(wrap_width, None);
buffer.set_text(
value,
&Attrs::new().family(family),
@@ -176,10 +186,44 @@ pub fn measure(value: &str, style: TextStyle) -> TextSize {
engine.buffers[&key].size
}
/// Draw at the actual device font size; no scaling of an already rasterized bitmap.
pub fn draw(value: &str, style: TextStyle, color: Color, mut pixel: impl FnMut(i32, i32, Color)) {
pub fn measure_wrapped(value: &str, style: TextStyle, width: f32) -> TextSize {
if width <= 0.0 {
return TextSize {
width: 0.0,
height: style.line_height,
};
}
let mut engine = engine().lock().expect("text engine lock");
let key = engine.prepare(value, style);
let key = engine.prepare_width(value, style, Some(width));
engine.buffers[&key].size
}
/// Draw at the actual device font size; no scaling of an already rasterized bitmap.
pub fn draw(value: &str, style: TextStyle, color: Color, pixel: impl FnMut(i32, i32, Color)) {
draw_width(value, style, color, None, pixel)
}
pub fn draw_wrapped(
value: &str,
style: TextStyle,
color: Color,
width: f32,
pixel: impl FnMut(i32, i32, Color),
) {
if width > 0.0 {
draw_width(value, style, color, Some(width), pixel)
}
}
fn draw_width(
value: &str,
style: TextStyle,
color: Color,
width: Option<f32>,
mut pixel: impl FnMut(i32, i32, Color),
) {
let mut engine = engine().lock().expect("text engine lock");
let key = engine.prepare_width(value, style, width);
let TextEngine {
fonts,
swash,
@@ -272,6 +316,25 @@ mod tests {
use super::*;
use unicode_segmentation::UnicodeSegmentation;
#[test]
fn width_sensitive_shaping_wraps_words_and_clusters_without_changing_intrinsic_metrics() {
let style = TextStyle::ui(1);
let text = "中文段落 with words and long_unbroken_identifier é 👩‍💻 中文段落";
let intrinsic = measure(text, style);
let narrow = measure_wrapped(text, style, 70.0);
let wide = measure_wrapped(text, style, 240.0);
assert!(narrow.height > wide.height);
assert!(wide.height >= style.line_height);
assert!(narrow.width <= 70.1);
assert_eq!(measure(text, style).width, intrinsic.width);
assert_eq!(measure_wrapped(text, style, 0.0).width, 0.0);
let mut engine = engine().lock().unwrap();
let narrow_key = engine.prepare_width(text, style, Some(70.0));
let wide_key = engine.prepare_width(text, style, Some(240.0));
assert!(narrow_key != wide_key);
assert!(engine.buffers[&narrow_key].size.height > engine.buffers[&wide_key].size.height);
}
#[test]
fn proportional_metrics_monospace_and_multiline_height_come_from_the_fonts() {
let ui = TextStyle::ui(1);