Skip to content

Commit 36d9c8d

Browse files
committed
Changed from a space to '' and added 'replace' argument.
1 parent 172f972 commit 36d9c8d

1 file changed

Lines changed: 50 additions & 1 deletion

File tree

src/gameobjects/BitmapText.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,55 @@ Phaser.BitmapText.prototype.scanLine = function (data, scale, text) {
302302

303303
};
304304

305+
/**
306+
* Given a text string this will scan each character in the string to ensure it exists
307+
* in the BitmapText font data. If it doesn't the character is removed, or replaced with the `replace` argument.
308+
*
309+
* If no font data has been loaded at all this returns an empty string.
310+
*
311+
* @method Phaser.BitmapText.prototype.cleanText
312+
* @param {string} text - The text to parse.
313+
* @param {string} [replace=''] - The replacement string for any missing characters.
314+
* @return {string} The cleaned text string.
315+
*/
316+
Phaser.BitmapText.prototype.cleanText = function (text, replace) {
317+
318+
if (replace === undefined)
319+
{
320+
replace = '';
321+
}
322+
323+
var data = this._data.font;
324+
325+
if (!data)
326+
{
327+
return '';
328+
}
329+
330+
var output = '';
331+
332+
for (var i = 0; i < text.length; i++)
333+
{
334+
if (!/(?:\r\n|\r|\n)/.test(text.charAt(i)))
335+
{
336+
var charCode = text.charCodeAt(i);
337+
var charData = data.chars[charCode];
338+
339+
if (charData)
340+
{
341+
output = output.concat(text[i])
342+
}
343+
else
344+
{
345+
output = output.concat(' ');
346+
}
347+
}
348+
}
349+
350+
return output;
351+
352+
};
353+
305354
/**
306355
* Renders text and updates it when needed.
307356
*
@@ -572,7 +621,7 @@ Object.defineProperty(Phaser.BitmapText.prototype, 'text', {
572621

573622
if (value !== this._text)
574623
{
575-
this._text = value.toString() || '';
624+
this._text = this.cleanText(value.toString()) || '';
576625
this.updateText();
577626
}
578627

0 commit comments

Comments
 (0)