From cc75bb66b40794702ebc9fc9e30b1b29d7438bb5 Mon Sep 17 00:00:00 2001 From: dtzxporter Date: Thu, 1 Feb 2024 21:51:36 -0500 Subject: [PATCH] Adds a separate wrapping option to allow word to glyph fallback. --- src/layout.rs | 5 ++++- src/shape.rs | 18 ++++++++++++------ tests/images/a_hebrew_word.png | 4 ++-- tests/images/an_arabic_word.png | 4 ++-- tests/shaping_and_rendering.rs | 4 ++-- tests/wrap_word_fallback.rs | 2 +- 6 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/layout.rs b/src/layout.rs index bcc20ef94d..5d158167d8 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -104,8 +104,10 @@ pub enum Wrap { None, /// Wraps at a glyph level Glyph, - /// Word Wrapping + /// Wraps at the word level Word, + /// Wraps at the word level, or fallback to glyph level if a word can't fit on a line by itself + WordOrGlyph, } impl Display for Wrap { @@ -113,6 +115,7 @@ impl Display for Wrap { match self { Self::None => write!(f, "No Wrap"), Self::Word => write!(f, "Word Wrap"), + Self::WordOrGlyph => write!(f, "Word Wrap or Character"), Self::Glyph => write!(f, "Character"), } } diff --git a/src/shape.rs b/src/shape.rs index a5dece1596..4420cc15bc 100644 --- a/src/shape.rs +++ b/src/shape.rs @@ -994,10 +994,13 @@ impl ShapeLine { continue; } else if wrap == Wrap::Glyph // Make sure that the word is able to fit on it's own line, if not, fall back to Glyph wrapping. - || word_width > line_width + || (wrap == Wrap::WordOrGlyph && word_width > line_width) { // Commit the current line so that the word starts on the next line. - if word_range_width > 0. && word_width > line_width { + if word_range_width > 0. + && wrap == Wrap::WordOrGlyph + && word_width > line_width + { add_to_visual_line( &mut current_visual_line, span_index, @@ -1041,7 +1044,7 @@ impl ShapeLine { } } } else { - // Wrap::Word + // Wrap::Word, Wrap::WordOrGlyph // TODO: What if the previous span ended with whitespace and the next // span wraps a new line? Is that possible? @@ -1117,10 +1120,13 @@ impl ShapeLine { continue; } else if wrap == Wrap::Glyph // Make sure that the word is able to fit on it's own line, if not, fall back to Glyph wrapping. - || word_width > line_width + || (wrap == Wrap::WordOrGlyph && word_width > line_width) { // Commit the current line so that the word starts on the next line. - if word_range_width > 0. && word_width > line_width { + if word_range_width > 0. + && wrap == Wrap::WordOrGlyph + && word_width > line_width + { add_to_visual_line( &mut current_visual_line, span_index, @@ -1164,7 +1170,7 @@ impl ShapeLine { } } } else { - // Wrap::Word + // Wrap::Word, Wrap::WordOrGlyph // Current word causing a wrap is not whitespace, so we ignore the // previous word if it's a whitespace diff --git a/tests/images/a_hebrew_word.png b/tests/images/a_hebrew_word.png index bfb4c902cf..1159d485a7 100644 --- a/tests/images/a_hebrew_word.png +++ b/tests/images/a_hebrew_word.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d412b55f4c5e3ac27c1cf4ebd87adc08bf079a7afa8d385320b75f52ed34072f -size 3522 +oid sha256:95bbe8f4db74914f6a124547f024463a3e434b3c7be3f86c1464af71ed39bba0 +size 3512 diff --git a/tests/images/an_arabic_word.png b/tests/images/an_arabic_word.png index 7a4a7659fe..d40901b1c2 100644 --- a/tests/images/an_arabic_word.png +++ b/tests/images/an_arabic_word.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:cd2cc96243d6e550162a5a51837d95598bcf57690b0851d59cf92e2e3bb9e157 -size 3864 +oid sha256:ba0219c8e226f4bd79e0681a3189013ed035459fd3ed90405ec53d595e70ab81 +size 3851 diff --git a/tests/shaping_and_rendering.rs b/tests/shaping_and_rendering.rs index cb90ff09fd..e70bcbe6d7 100644 --- a/tests/shaping_and_rendering.rs +++ b/tests/shaping_and_rendering.rs @@ -11,7 +11,7 @@ fn test_hebrew_word_rendering() { .font_size(36., 40.) .font_attrs(attrs) .text("בדיקה") - .canvas(120, 60) + .canvas(100, 60) .validate_text_rendering(); } @@ -46,7 +46,7 @@ fn test_arabic_word_rendering() { .font_size(36., 40.) .font_attrs(attrs) .text("خالصة") - .canvas(120, 60) + .canvas(100, 60) .validate_text_rendering(); } diff --git a/tests/wrap_word_fallback.rs b/tests/wrap_word_fallback.rs index d32f2b534c..c3a824b7aa 100644 --- a/tests/wrap_word_fallback.rs +++ b/tests/wrap_word_fallback.rs @@ -11,7 +11,7 @@ fn wrap_word_fallback() { let mut buffer = buffer.borrow_with(&mut font_system); - buffer.set_wrap(Wrap::Word); + buffer.set_wrap(Wrap::WordOrGlyph); buffer.set_text("Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat.", Attrs::new().family(cosmic_text::Family::Name("Inter")), Shaping::Advanced); buffer.set_size(50.0, 1000.0);