changeset 886:cf4000cc6286

fix: word-wrapping working correctly now
author Sam <sam@basx.dev>
date Sat, 03 Feb 2024 21:38:34 +0700
parents b9ed52c6cc93
children 604f1e7be026
files semicongine/text.nim
diffstat 1 files changed, 10 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/semicongine/text.nim	Sat Feb 03 19:33:57 2024 +0700
+++ b/semicongine/text.nim	Sat Feb 03 21:38:34 2024 +0700
@@ -83,17 +83,19 @@
   # pre-calculate text-width
   var width = 0'f32
   var lineWidths: seq[float32]
-  for i in 0 ..< min(text.processedText.len, text.maxLen):
+  for i in 0 ..< text.processedText.len:
     if text.processedText[i] == NEWLINE:
       lineWidths.add width
       width = 0'f32
     else:
-      width += text.font.glyphs[text.processedText[i]].advance
+      if not (i == text.processedText.len - 1 and text.processedText[i].isWhiteSpace):
+        width += text.font.glyphs[text.processedText[i]].advance
       if i < text.processedText.len - 1:
         width += text.font.kerning[(text.processedText[i], text.processedText[i + 1])]
   lineWidths.add width
-  let
-    height = float32(lineWidths.len) * text.font.lineAdvance
+  var height = float32(lineWidths.len) * text.font.lineAdvance
+  if lineWidths[^1] == 0 and lineWidths.len > 1:
+    height -= 1
 
   let anchorY = (case text.verticalAlignment
     of Top: 0'f32
@@ -200,7 +202,10 @@
         remaining.add currentWord[subWord.len .. ^1] # process rest of the word in next iteration
       else:
         if (currentLine & SPACE & currentWord).width(font) <= maxWidth:
-          currentLine = currentLine & SPACE & currentWord
+          if currentLine.len == 0:
+            currentLine = currentWord
+          else:
+            currentLine = currentLine & SPACE & currentWord
         else:
           result.add currentLine & NEWLINE
           remaining.add currentWord
@@ -220,9 +225,6 @@
   text.processedText = text.text
   if text.maxWidth > 0:
     text.processedText = text.processedText.wordWrapped(text.font, text.maxWidth / text.scale)
-    echo "--------------------------"
-    echo text.processedText
-    echo "##########################"
 
 proc `text=`*(text: var Text, newText: string) =
   `text=`(text, newText.toRunes)