From ef7c2c6275b08b6c1d9c5027c425f2cb1112dbe0 Mon Sep 17 00:00:00 2001 From: slash Date: Sun, 3 Jan 2016 16:33:33 -0500 Subject: [PATCH 1/2] Alow precalculating the results of text wrapping. This may be useful for example if the developer wants to control pagination, in which case he may know in advance the total number of lines the text will wrap to, and then set it on batches. --- src/gameobjects/Text.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/gameobjects/Text.js b/src/gameobjects/Text.js index 2eddbc409..085b817a1 100644 --- a/src/gameobjects/Text.js +++ b/src/gameobjects/Text.js @@ -805,6 +805,20 @@ Phaser.Text.prototype.addFontWeight = function (weight, position) { }; +/** +* Precalculates word wrap for a given text based on the Text object configuration. +* +* It may be useful is the developer wants to control pagination on long pieces of content. +* +* @method Phaser.Text#precalculateWordWrap +* @param {string} text - The text for which the wrapping will be precalculated. +* @return {array} An array of strings with the pieces of wrapped text. +*/ +Phaser.Text.prototype.precalculateWordWrap = function (text) { + var wrappedLines = this.runWordWrap(text); + return wrappedLines.split(/(?:\r\n|\r|\n)/); +}; + /** * Greedy wrapping algorithm that will wrap words as the line grows longer than its horizontal bounds. * From 5cc6b5962b63167aa8e189e7b62c231b859b723d Mon Sep 17 00:00:00 2001 From: slash Date: Sun, 3 Jan 2016 16:54:27 -0500 Subject: [PATCH 2/2] Add missing assignations required for wrapping calculations to work correctly --- src/gameobjects/Text.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/gameobjects/Text.js b/src/gameobjects/Text.js index 085b817a1..473abfe7c 100644 --- a/src/gameobjects/Text.js +++ b/src/gameobjects/Text.js @@ -815,6 +815,8 @@ Phaser.Text.prototype.addFontWeight = function (weight, position) { * @return {array} An array of strings with the pieces of wrapped text. */ Phaser.Text.prototype.precalculateWordWrap = function (text) { + this.texture.baseTexture.resolution = this._res; + this.context.font = this.style.font; var wrappedLines = this.runWordWrap(text); return wrappedLines.split(/(?:\r\n|\r|\n)/); };