tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 709HTML 662Markdown 12TOML 62348 Definitions

fix: wordwrap calculation in measure example (#629)

Fixes an off-by-one error in the wordwrap calculation

Fixes: #628

Co-authored-by: Nico Burns <nico@nicoburns.com>

b3ee315ccec414bee6689fb7f2f50fbb6fbdd54c · by Josh McKinney · (2 years ago) · parent dc9862a93 · browse the tree at this commit

Semantic delta · 1

examples/common/text.rs

1 files · +10 −5

diff --git a/examples/common/text.rs b/examples/common/text.rs
index cde02c4cd..09a92dc45 100644
--- a/examples/common/text.rs
+++ b/examples/common/text.rs
@@ -51,13 +51,18 @@ pub fn text_measure_function(
         let mut line_count = 1;
         let mut current_line_length = 0;
         for word in &words {
-            if current_line_length + word.len() > inline_line_length {
-                if current_line_length > 0 {
-                    line_count += 1
-                };
+            if current_line_length == 0 {
+                // first word
+                current_line_length = word.len();
+            } else if current_line_length + word.len() + 1 > inline_line_length {
+                // every word past the first needs to check for line length including the space between words
+                // note: a real implementation of this should handle whitespace characters other than ' '
+                // and do something more sophisticated for long words
+                line_count += 1;
                 current_line_length = word.len();
             } else {
-                current_line_length += word.len();
+                // add the word and a space
+                current_line_length += word.len() + 1;
             };
         }
         (line_count as f32) * font_metrics.char_height