Skip to content

Commit

Permalink
Adds a separate wrapping option to allow word to glyph fallback.
Browse files Browse the repository at this point in the history
  • Loading branch information
dtzxporter authored and jackpot51 committed Feb 2, 2024
1 parent b7f1506 commit cc75bb6
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
5 changes: 4 additions & 1 deletion src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,18 @@ 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 {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
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"),
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/images/a_hebrew_word.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tests/images/an_arabic_word.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tests/shaping_and_rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/wrap_word_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit cc75bb6

Please sign in to comment.