diff --git a/src/text/BitmapText.js b/src/text/BitmapText.js index d2db02f..b3ca9e4 100644 --- a/src/text/BitmapText.js +++ b/src/text/BitmapText.js @@ -68,6 +68,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} @@ -181,10 +189,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))) { @@ -198,6 +208,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)