gamnit: compute a more precise `TextSprites::height`
authorAlexis Laferrière <alexis.laf@xymus.net>
Fri, 15 Sep 2017 12:17:09 +0000 (08:17 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Mon, 25 Sep 2017 15:11:12 +0000 (11:11 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/gamnit/bmfont.nit

index e34e2e7..cdeef2d 100644 (file)
@@ -367,6 +367,10 @@ class BMFontAsset
                var dy = 0.0
                var text_width = 0.0
                var line_sprites = new Array[Sprite]
+               var height = 0.0
+
+               # Has the current line height been added to `height`?
+               var line_height_counted = false
 
                # TextSprite customization
                var max_width = text_sprites.max_width
@@ -397,14 +401,21 @@ class BMFontAsset
                                dy -= line_height
                                if max_height != null and max_height < -dy + line_height then break
                                dx = 0.0
+                               if not line_height_counted then
+                                       # Force to account for empty lines
+                                       height += line_height
+                               end
+                               line_height_counted = false
                                prev_char = null
                                continue
                        else if c == pld then
                                dy -= partial_line_skip
+                               height += partial_line_skip
                                word_break = true
                                continue
                        else if c == plu then
                                dy += partial_line_skip
+                               height -= partial_line_skip # We could keep two heights and return the max
                                word_break = true
                                continue
                        else if c.is_whitespace then
@@ -494,6 +505,7 @@ class BMFontAsset
                                                dy -= line_height
                                                if max_height != null and max_height < -dy + line_height then break
                                                dx = 0.0
+                                               line_height_counted = false
 
                                                if not text_sprites.wrap then
                                                        # Cut short, skip everything until the next new line
@@ -533,6 +545,12 @@ class BMFontAsset
                        prev_char = c
 
                        text_width = text_width.max(dx)
+
+                       if not line_height_counted then
+                               # Increase `height` only once per line iff there's a caracter
+                               line_height_counted = true
+                               height += line_height
+                       end
                end
 
                justify(line_sprites, text_sprites.align, dx)
@@ -544,7 +562,7 @@ class BMFontAsset
                end
 
                text_sprites.width = text_width.max(dx)
-               text_sprites.height = dy + line_height
+               text_sprites.height = height
        end
 
        # Character replacing other characters missing from the font