Skip to content

Commit 4435791

Browse files
committed
Updated to use current null approach
1 parent 28982fb commit 4435791

1 file changed

Lines changed: 41 additions & 27 deletions

File tree

src/gameobjects/bitmaptext/GetBitmapTextSize.js

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -242,34 +242,35 @@ var GetBitmapTextSize = function (src, round, out)
242242
src._text = text;
243243
textLength = text.length;
244244

245-
console.log(text);
246-
247245
// Recalculated in the next loop
248246
words = [];
247+
current = null;
249248
}
250249

251-
current = { word: '', x: 0, y: 0, w: 0, h: lineHeight };
252-
253250
for (i = 0; i < textLength; i++)
254251
{
255252
charCode = text.charCodeAt(i);
256253

257254
if (charCode === 10)
258255
{
259-
words.push({
260-
word: current.word,
261-
x: current.x * sx,
262-
y: current.y * sy,
263-
w: current.w * sx,
264-
h: current.h * sy
265-
});
256+
if (current !== null)
257+
{
258+
words.push({
259+
word: current.word,
260+
i: current.i,
261+
x: current.x * sx,
262+
y: current.y * sy,
263+
w: current.w * sx,
264+
h: current.h * sy
265+
});
266+
267+
current = null;
268+
}
266269

267270
xAdvance = 0;
268271
yAdvance += lineHeight;
269272
lastGlyph = null;
270273

271-
current = { word: '', x: xAdvance, y: yAdvance, w: 0, h: lineHeight };
272-
273274
lineWidths[currentLine] = currentLineWidth;
274275

275276
if (currentLineWidth > longestLine)
@@ -284,6 +285,7 @@ var GetBitmapTextSize = function (src, round, out)
284285

285286
currentLine++;
286287
currentLineWidth = 0;
288+
287289
continue;
288290
}
289291

@@ -300,6 +302,7 @@ var GetBitmapTextSize = function (src, round, out)
300302
if (lastGlyph !== null)
301303
{
302304
var kerningOffset = glyph.kerning[lastCharCode];
305+
303306
x += (kerningOffset !== undefined) ? kerningOffset : 0;
304307
}
305308

@@ -326,35 +329,46 @@ var GetBitmapTextSize = function (src, round, out)
326329
bh = gh;
327330
}
328331

329-
xAdvance += glyph.xAdvance + letterSpacing;
330-
lastGlyph = glyph;
331-
lastCharCode = charCode;
332-
currentLineWidth = gw * scale;
333-
334332
if (charCode === wordWrapCharCode)
335333
{
336-
words.push({
337-
word: current.word,
338-
x: current.x * sx,
339-
y: current.y * sy,
340-
w: current.w * sx,
341-
h: current.h * sy
342-
});
343-
344-
current = { word: '', x: xAdvance, y: yAdvance, w: 0, h: lineHeight };
334+
if (current !== null)
335+
{
336+
words.push({
337+
word: current.word,
338+
i: current.i,
339+
x: current.x * sx,
340+
y: current.y * sy,
341+
w: current.w * sx,
342+
h: current.h * sy
343+
});
344+
345+
current = null;
346+
}
345347
}
346348
else
347349
{
350+
if (current === null)
351+
{
352+
// We're starting a new word, recording the starting index, etc
353+
current = { word: '', i: i, x: xAdvance, y: yAdvance, w: 0, h: lineHeight };
354+
}
355+
348356
current.word = current.word.concat(text[i]);
349357
current.w += glyph.xOffset + glyph.xAdvance + ((kerningOffset !== undefined) ? kerningOffset : 0);
350358
}
359+
360+
xAdvance += glyph.xAdvance + letterSpacing;
361+
lastGlyph = glyph;
362+
lastCharCode = charCode;
363+
currentLineWidth = gw * scale;
351364
}
352365

353366
// Last word
354367
if (current !== null)
355368
{
356369
words.push({
357370
word: current.word,
371+
i: current.i,
358372
x: current.x * sx,
359373
y: current.y * sy,
360374
w: current.w * sx,

0 commit comments

Comments
 (0)