Skip to content

Commit c1a2970

Browse files
committed
Fixing issue phaserjs#2496: Wrapped BitmapText not centering
1 parent 027725e commit c1a2970

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

src/gameobjects/BitmapText.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ Phaser.BitmapText.prototype.scanLine = function (data, scale, text) {
244244
var x = 0;
245245
var w = 0;
246246
var lastSpace = -1;
247+
var wrappedWidth = 0;
247248
var prevCharCode = null;
248249
var maxWidth = (this._maxWidth > 0) ? this._maxWidth : null;
249250
var chars = [];
@@ -275,17 +276,20 @@ Phaser.BitmapText.prototype.scanLine = function (data, scale, text) {
275276
// Adjust for kerning from previous character to this one
276277
var kerning = (prevCharCode && charData.kerning[prevCharCode]) ? charData.kerning[prevCharCode] : 0;
277278

278-
// Record the last space in the string
279-
lastSpace = /(\s)/.test(text.charAt(i)) ? i : lastSpace;
280-
279+
// Record the last space in the string and the current width
280+
if (/(\s)/.test(text.charAt(i))) {
281+
lastSpace = i;
282+
wrappedWidth = w;
283+
}
284+
281285
// What will the line width be if we add this character to it?
282286
c = (kerning + charData.texture.width + charData.xOffset) * scale;
283287

284288
// Do we need to line-wrap?
285289
if (maxWidth && ((w + c) >= maxWidth) && lastSpace > -1)
286290
{
287291
// The last space was at "lastSpace" which was "i - lastSpace" characters ago
288-
return { width: w, text: text.substr(0, i - (i - lastSpace)), end: end, chars: chars };
292+
return { width: wrappedWidth || w, text: text.substr(0, i - (i - lastSpace)), end: end, chars: chars };
289293
}
290294
else
291295
{

0 commit comments

Comments
 (0)