mirror of
https://github.com/photonstorm/phaser
synced 2024-11-27 15:12:18 +00:00
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 #2165)
This commit is contained in:
parent
8cf96a2f78
commit
88f1ea95d2
1 changed files with 28 additions and 19 deletions
|
@ -145,8 +145,6 @@ Phaser.BitmapText = function (game, x, y, font, text, size, align) {
|
|||
*/
|
||||
this._tint = 0xFFFFFF;
|
||||
|
||||
this._text = this.cleanText(text);
|
||||
|
||||
this.updateText();
|
||||
|
||||
/**
|
||||
|
@ -266,10 +264,12 @@ Phaser.BitmapText.prototype.scanLine = function (data, scale, text) {
|
|||
|
||||
var c = 0;
|
||||
|
||||
if (!charData)
|
||||
// If the character data isn't found in the data array
|
||||
// then we replace it with a blank space
|
||||
if (charData === undefined)
|
||||
{
|
||||
// Skip a character not found in the font data
|
||||
continue;
|
||||
charCode = 32;
|
||||
charData = data.chars[charCode];
|
||||
}
|
||||
|
||||
// Adjust for kerning from previous character to this one
|
||||
|
@ -289,11 +289,11 @@ Phaser.BitmapText.prototype.scanLine = function (data, scale, text) {
|
|||
}
|
||||
else
|
||||
{
|
||||
w += charData.xAdvance * scale;
|
||||
w += (charData.xAdvance + kerning) * scale;
|
||||
|
||||
chars.push(x + (charData.xOffset * scale));
|
||||
chars.push(x + (charData.xOffset + kerning) * scale);
|
||||
|
||||
x += charData.xAdvance * scale;
|
||||
x += (charData.xAdvance + kerning) * scale;
|
||||
|
||||
prevCharCode = charCode;
|
||||
}
|
||||
|
@ -329,27 +329,30 @@ Phaser.BitmapText.prototype.cleanText = function (text, replace) {
|
|||
return '';
|
||||
}
|
||||
|
||||
var output = '';
|
||||
var re = /\r\n|\n\r|\n|\r/g;
|
||||
var lines = text.replace(re, "\n").split("\n");
|
||||
|
||||
for (var i = 0; i < text.length; i++)
|
||||
for (var i = 0; i < lines.length; i++)
|
||||
{
|
||||
if (!/(?:\r\n|\r|\n)/.test(text.charAt(i)))
|
||||
{
|
||||
var charCode = text.charCodeAt(i);
|
||||
var charData = data.chars[charCode];
|
||||
var output = '';
|
||||
var line = lines[i];
|
||||
|
||||
if (charData)
|
||||
for (var c = 0; c < line.length; c++)
|
||||
{
|
||||
if (data.chars[line.charCodeAt(c)])
|
||||
{
|
||||
output = output.concat(text[i]);
|
||||
output = output.concat(line[c]);
|
||||
}
|
||||
else
|
||||
{
|
||||
output = output.concat(' ');
|
||||
output = output.concat(replace);
|
||||
}
|
||||
}
|
||||
|
||||
lines[i] = output;
|
||||
}
|
||||
|
||||
return output;
|
||||
return lines.join("\n");
|
||||
|
||||
};
|
||||
|
||||
|
@ -420,6 +423,12 @@ Phaser.BitmapText.prototype.updateText = function () {
|
|||
var charCode = line.text.charCodeAt(c);
|
||||
var charData = data.chars[charCode];
|
||||
|
||||
if (charData === undefined)
|
||||
{
|
||||
charCode = 32;
|
||||
charData = data.chars[charCode];
|
||||
}
|
||||
|
||||
var g = this._glyphs[t];
|
||||
|
||||
if (g)
|
||||
|
@ -623,7 +632,7 @@ Object.defineProperty(Phaser.BitmapText.prototype, 'text', {
|
|||
|
||||
if (value !== this._text)
|
||||
{
|
||||
this._text = this.cleanText(value.toString()) || '';
|
||||
this._text = value.toString() || '';
|
||||
this.updateText();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue