From 36d9c8d673180fad8070e937dac0620e77694a36 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Wed, 13 Jan 2016 15:10:45 +0000 Subject: [PATCH] Changed from a space to '' and added 'replace' argument. --- src/gameobjects/BitmapText.js | 51 ++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/gameobjects/BitmapText.js b/src/gameobjects/BitmapText.js index 2a1e48fc4..853718ff0 100644 --- a/src/gameobjects/BitmapText.js +++ b/src/gameobjects/BitmapText.js @@ -302,6 +302,55 @@ Phaser.BitmapText.prototype.scanLine = function (data, scale, text) { }; +/** +* Given a text string this will scan each character in the string to ensure it exists +* in the BitmapText font data. If it doesn't the character is removed, or replaced with the `replace` argument. +* +* If no font data has been loaded at all this returns an empty string. +* +* @method Phaser.BitmapText.prototype.cleanText +* @param {string} text - The text to parse. +* @param {string} [replace=''] - The replacement string for any missing characters. +* @return {string} The cleaned text string. +*/ +Phaser.BitmapText.prototype.cleanText = function (text, replace) { + + if (replace === undefined) + { + replace = ''; + } + + var data = this._data.font; + + if (!data) + { + return ''; + } + + var output = ''; + + for (var i = 0; i < text.length; i++) + { + if (!/(?:\r\n|\r|\n)/.test(text.charAt(i))) + { + var charCode = text.charCodeAt(i); + var charData = data.chars[charCode]; + + if (charData) + { + output = output.concat(text[i]) + } + else + { + output = output.concat(' '); + } + } + } + + return output; + +}; + /** * Renders text and updates it when needed. * @@ -572,7 +621,7 @@ Object.defineProperty(Phaser.BitmapText.prototype, 'text', { if (value !== this._text) { - this._text = value.toString() || ''; + this._text = this.cleanText(value.toString()) || ''; this.updateText(); }