phoboslab / Impact

HTML5 Game Engine
MIT License
2k stars 201 forks source link

ig.Font.widthForString returns (slightly) incorrect value #45

Closed Joncom closed 5 years ago

Joncom commented 5 years ago

Screen Shot 2019-07-25 at 7 46 53 PM

// Pre-patch:
ig.game.font.widthForString('HELLO'); // 22
// Post-patch:
ig.game.font.widthForString('HELLO'); // 21
diff --git a/lib/impact/font.js b/lib/impact/font.js
index 638e91b..5d085c0 100644
--- a/lib/impact/font.js
+++ b/lib/impact/font.js
@@ -42,7 +42,10 @@ ig.Font = ig.Image.extend({
        _widthForLine: function( text ) {
                var width = 0;
                for( var i = 0; i < text.length; i++ ) {
-                       width += this.widthMap[text.charCodeAt(i) - this.firstChar] + this.letterSpacing;
+                       width += this.widthMap[text.charCodeAt(i) - this.firstChar];
+               }
+               if( text.length > 0 ) {
+                       width += this.letterSpacing * (text.length - 1);
                }
                return width;
        },