From 4435791cc1b56645e50dfc96be2b50e3a60c8e17 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Fri, 22 Nov 2019 13:54:07 +0000 Subject: [PATCH] Updated to use current null approach --- .../bitmaptext/GetBitmapTextSize.js | 76 +++++++++++-------- 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/src/gameobjects/bitmaptext/GetBitmapTextSize.js b/src/gameobjects/bitmaptext/GetBitmapTextSize.js index 2e5ea892f..cbcbeceae 100644 --- a/src/gameobjects/bitmaptext/GetBitmapTextSize.js +++ b/src/gameobjects/bitmaptext/GetBitmapTextSize.js @@ -242,34 +242,35 @@ var GetBitmapTextSize = function (src, round, out) src._text = text; textLength = text.length; - console.log(text); - // Recalculated in the next loop words = []; + current = null; } - current = { word: '', x: 0, y: 0, w: 0, h: lineHeight }; - for (i = 0; i < textLength; i++) { charCode = text.charCodeAt(i); if (charCode === 10) { - words.push({ - word: current.word, - x: current.x * sx, - y: current.y * sy, - w: current.w * sx, - h: current.h * sy - }); + if (current !== null) + { + words.push({ + word: current.word, + i: current.i, + x: current.x * sx, + y: current.y * sy, + w: current.w * sx, + h: current.h * sy + }); + + current = null; + } xAdvance = 0; yAdvance += lineHeight; lastGlyph = null; - current = { word: '', x: xAdvance, y: yAdvance, w: 0, h: lineHeight }; - lineWidths[currentLine] = currentLineWidth; if (currentLineWidth > longestLine) @@ -284,6 +285,7 @@ var GetBitmapTextSize = function (src, round, out) currentLine++; currentLineWidth = 0; + continue; } @@ -300,6 +302,7 @@ var GetBitmapTextSize = function (src, round, out) if (lastGlyph !== null) { var kerningOffset = glyph.kerning[lastCharCode]; + x += (kerningOffset !== undefined) ? kerningOffset : 0; } @@ -326,28 +329,38 @@ var GetBitmapTextSize = function (src, round, out) bh = gh; } + if (charCode === wordWrapCharCode) + { + if (current !== null) + { + words.push({ + word: current.word, + i: current.i, + x: current.x * sx, + y: current.y * sy, + w: current.w * sx, + h: current.h * sy + }); + + current = null; + } + } + else + { + if (current === null) + { + // We're starting a new word, recording the starting index, etc + current = { word: '', i: i, x: xAdvance, y: yAdvance, w: 0, h: lineHeight }; + } + + current.word = current.word.concat(text[i]); + current.w += glyph.xOffset + glyph.xAdvance + ((kerningOffset !== undefined) ? kerningOffset : 0); + } + xAdvance += glyph.xAdvance + letterSpacing; lastGlyph = glyph; lastCharCode = charCode; currentLineWidth = gw * scale; - - if (charCode === wordWrapCharCode) - { - words.push({ - word: current.word, - x: current.x * sx, - y: current.y * sy, - w: current.w * sx, - h: current.h * sy - }); - - current = { word: '', x: xAdvance, y: yAdvance, w: 0, h: lineHeight }; - } - else - { - current.word = current.word.concat(text[i]); - current.w += glyph.xOffset + glyph.xAdvance + ((kerningOffset !== undefined) ? kerningOffset : 0); - } } // Last word @@ -355,6 +368,7 @@ var GetBitmapTextSize = function (src, round, out) { words.push({ word: current.word, + i: current.i, x: current.x * sx, y: current.y * sy, w: current.w * sx,