Skip to content

Commit 88f1ea9

Browse files
committed
Fixed BitmapText.cleanText so it works properly with new-lines within the text.
BitmapText would crash if it tried to render a character that didn't exist in the font set. Any character that doesn't exist in the font set now renders a space character instead. BitmapText would load and parse the kerning data from the font, but would never use it when rendering. The kerning values are now applied on rendering as well (thanks @veu phaserjs#2165)
1 parent 8cf96a2 commit 88f1ea9

1 file changed

Lines changed: 28 additions & 19 deletions

File tree

src/gameobjects/BitmapText.js

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,6 @@ Phaser.BitmapText = function (game, x, y, font, text, size, align) {
145145
*/
146146
this._tint = 0xFFFFFF;
147147

148-
this._text = this.cleanText(text);
149-
150148
this.updateText();
151149

152150
/**
@@ -266,10 +264,12 @@ Phaser.BitmapText.prototype.scanLine = function (data, scale, text) {
266264

267265
var c = 0;
268266

269-
if (!charData)
267+
// If the character data isn't found in the data array
268+
// then we replace it with a blank space
269+
if (charData === undefined)
270270
{
271-
// Skip a character not found in the font data
272-
continue;
271+
charCode = 32;
272+
charData = data.chars[charCode];
273273
}
274274

275275
// Adjust for kerning from previous character to this one
@@ -289,11 +289,11 @@ Phaser.BitmapText.prototype.scanLine = function (data, scale, text) {
289289
}
290290
else
291291
{
292-
w += charData.xAdvance * scale;
292+
w += (charData.xAdvance + kerning) * scale;
293293

294-
chars.push(x + (charData.xOffset * scale));
294+
chars.push(x + (charData.xOffset + kerning) * scale);
295295

296-
x += charData.xAdvance * scale;
296+
x += (charData.xAdvance + kerning) * scale;
297297

298298
prevCharCode = charCode;
299299
}
@@ -329,27 +329,30 @@ Phaser.BitmapText.prototype.cleanText = function (text, replace) {
329329
return '';
330330
}
331331

332-
var output = '';
332+
var re = /\r\n|\n\r|\n|\r/g;
333+
var lines = text.replace(re, "\n").split("\n");
333334

334-
for (var i = 0; i < text.length; i++)
335+
for (var i = 0; i < lines.length; i++)
335336
{
336-
if (!/(?:\r\n|\r|\n)/.test(text.charAt(i)))
337-
{
338-
var charCode = text.charCodeAt(i);
339-
var charData = data.chars[charCode];
337+
var output = '';
338+
var line = lines[i];
340339

341-
if (charData)
340+
for (var c = 0; c < line.length; c++)
341+
{
342+
if (data.chars[line.charCodeAt(c)])
342343
{
343-
output = output.concat(text[i]);
344+
output = output.concat(line[c]);
344345
}
345346
else
346347
{
347-
output = output.concat(' ');
348+
output = output.concat(replace);
348349
}
349350
}
351+
352+
lines[i] = output;
350353
}
351354

352-
return output;
355+
return lines.join("\n");
353356

354357
};
355358

@@ -420,6 +423,12 @@ Phaser.BitmapText.prototype.updateText = function () {
420423
var charCode = line.text.charCodeAt(c);
421424
var charData = data.chars[charCode];
422425

426+
if (charData === undefined)
427+
{
428+
charCode = 32;
429+
charData = data.chars[charCode];
430+
}
431+
423432
var g = this._glyphs[t];
424433

425434
if (g)
@@ -623,7 +632,7 @@ Object.defineProperty(Phaser.BitmapText.prototype, 'text', {
623632

624633
if (value !== this._text)
625634
{
626-
this._text = this.cleanText(value.toString()) || '';
635+
this._text = value.toString() || '';
627636
this.updateText();
628637
}
629638

0 commit comments

Comments
 (0)