diff --git a/src/text/BitmapText.js b/src/text/BitmapText.js index 5363b3c..a8e69ec 100644 --- a/src/text/BitmapText.js +++ b/src/text/BitmapText.js @@ -71,6 +71,14 @@ this._text = text; /** + * The max width of this bitmap text in pixels. If the text provided is longer than the value provided, line breaks will be automatically inserted in the last whitespace. + * Disable by setting value to 0 + * + * @member {number} + */ + this.maxWidth = 0; + + /** * The dirty state of this object. * * @member {boolean} @@ -189,10 +197,12 @@ var lineWidths = []; var line = 0; var scale = this.fontSize / data.size; + var lastSpace = -1; for (var i = 0; i < this.text.length; i++) { var charCode = this.text.charCodeAt(i); + lastSpace = /(\s)/.test(this.text.charAt(i)) ? i : lastSpace; if (/(?:\r\n|\r|\n)/.test(this.text.charAt(i))) { @@ -206,6 +216,22 @@ continue; } + if (lastSpace !== -1 && this.maxWidth > 0 && pos.x * scale > this.maxWidth) + { + chars.splice(lastSpace, i - lastSpace); + i = lastSpace; + lastSpace = -1; + + lineWidths.push(lastLineWidth); + maxLineWidth = Math.max(maxLineWidth, lastLineWidth); + line++; + + pos.x = 0; + pos.y += data.lineHeight; + prevCharCode = null; + continue; + } + var charData = data.chars[charCode]; if (!charData)