Updated to use current null approach

This commit is contained in:
Richard Davey 2019-11-22 13:54:07 +00:00
parent 28982fb7fc
commit 4435791cc1

View file

@ -242,34 +242,35 @@ var GetBitmapTextSize = function (src, round, out)
src._text = text; src._text = text;
textLength = text.length; textLength = text.length;
console.log(text);
// Recalculated in the next loop // Recalculated in the next loop
words = []; words = [];
current = null;
} }
current = { word: '', x: 0, y: 0, w: 0, h: lineHeight };
for (i = 0; i < textLength; i++) for (i = 0; i < textLength; i++)
{ {
charCode = text.charCodeAt(i); charCode = text.charCodeAt(i);
if (charCode === 10) if (charCode === 10)
{
if (current !== null)
{ {
words.push({ words.push({
word: current.word, word: current.word,
i: current.i,
x: current.x * sx, x: current.x * sx,
y: current.y * sy, y: current.y * sy,
w: current.w * sx, w: current.w * sx,
h: current.h * sy h: current.h * sy
}); });
current = null;
}
xAdvance = 0; xAdvance = 0;
yAdvance += lineHeight; yAdvance += lineHeight;
lastGlyph = null; lastGlyph = null;
current = { word: '', x: xAdvance, y: yAdvance, w: 0, h: lineHeight };
lineWidths[currentLine] = currentLineWidth; lineWidths[currentLine] = currentLineWidth;
if (currentLineWidth > longestLine) if (currentLineWidth > longestLine)
@ -284,6 +285,7 @@ var GetBitmapTextSize = function (src, round, out)
currentLine++; currentLine++;
currentLineWidth = 0; currentLineWidth = 0;
continue; continue;
} }
@ -300,6 +302,7 @@ var GetBitmapTextSize = function (src, round, out)
if (lastGlyph !== null) if (lastGlyph !== null)
{ {
var kerningOffset = glyph.kerning[lastCharCode]; var kerningOffset = glyph.kerning[lastCharCode];
x += (kerningOffset !== undefined) ? kerningOffset : 0; x += (kerningOffset !== undefined) ? kerningOffset : 0;
} }
@ -326,28 +329,38 @@ var GetBitmapTextSize = function (src, round, out)
bh = gh; bh = gh;
} }
xAdvance += glyph.xAdvance + letterSpacing;
lastGlyph = glyph;
lastCharCode = charCode;
currentLineWidth = gw * scale;
if (charCode === wordWrapCharCode) if (charCode === wordWrapCharCode)
{
if (current !== null)
{ {
words.push({ words.push({
word: current.word, word: current.word,
i: current.i,
x: current.x * sx, x: current.x * sx,
y: current.y * sy, y: current.y * sy,
w: current.w * sx, w: current.w * sx,
h: current.h * sy h: current.h * sy
}); });
current = { word: '', x: xAdvance, y: yAdvance, w: 0, h: lineHeight }; current = null;
}
} }
else 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.word = current.word.concat(text[i]);
current.w += glyph.xOffset + glyph.xAdvance + ((kerningOffset !== undefined) ? kerningOffset : 0); current.w += glyph.xOffset + glyph.xAdvance + ((kerningOffset !== undefined) ? kerningOffset : 0);
} }
xAdvance += glyph.xAdvance + letterSpacing;
lastGlyph = glyph;
lastCharCode = charCode;
currentLineWidth = gw * scale;
} }
// Last word // Last word
@ -355,6 +368,7 @@ var GetBitmapTextSize = function (src, round, out)
{ {
words.push({ words.push({
word: current.word, word: current.word,
i: current.i,
x: current.x * sx, x: current.x * sx,
y: current.y * sy, y: current.y * sy,
w: current.w * sx, w: current.w * sx,