Updated maxWidth calculations

This commit is contained in:
Zeke Chan 2024-07-12 15:34:48 +08:00
parent e9f92fab64
commit 7931e181a2

View file

@ -104,36 +104,35 @@ var GetBitmapTextSize = function (src, round, updateOrigin, out)
var characters = [];
var current = null;
var getCharWidth = function (char)
{
var charCode = char.charCodeAt(0);
var charGlyph = chars[charCode];
return charGlyph ? charGlyph.xOffset + charGlyph.xAdvance : 0;
};
// Scan for breach of maxWidth and insert carriage-returns
if (maxWidth > 0)
{
for (i = 0; i < textLength; i++)
var formattedText = '';
var word = '';
var wordWidth = 0;
for (i = 0; i < text.length; i++)
{
charCode = text.charCodeAt(i);
if (charCode === 10)
var isSpace = charCode === wordWrapCharCode;
var isLineFeed = charCode === 10;
// If the character is a space or the end of the text, check the word's width
if (isLineFeed)
{
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,
cr: true
});
current = null;
}
formattedText += word + '\n';
xAdvance = 0;
yAdvance += lineHeight + lineSpacing;
lastGlyph = null;
continue;
word = '';
wordWidth = 0;
}
glyph = chars[charCode];
@ -143,130 +142,36 @@ var GetBitmapTextSize = function (src, round, updateOrigin, out)
continue;
}
if (lastGlyph !== null)
if (isSpace || i === text.length - 1)
{
var glyphKerningOffset = glyph.kerning[lastCharCode];
}
if (charCode === wordWrapCharCode)
{
if (current !== null)
// Add the last character if it's not a space
if (!isSpace)
{
words.push({
word: current.word,
i: current.i,
x: current.x * sx,
y: current.y * sy,
w: current.w * sx,
h: current.h * sy,
cr: false
});
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, cr: false };
word += text[i];
}
current.word = current.word.concat(text[i]);
current.w += glyph.xOffset + glyph.xAdvance + ((glyphKerningOffset !== undefined) ? glyphKerningOffset : 0);
}
xAdvance += glyph.xAdvance + letterSpacing;
lastGlyph = glyph;
lastCharCode = charCode;
}
// Last word
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,
cr: false
});
}
// Reset for the next loop
xAdvance = 0;
yAdvance = 0;
lastGlyph = null;
lastCharCode = 0;
// Loop through the words array and see if we've got any > maxWidth
var prev;
var offset = 0;
var crs = [];
var currentY = words.length ? words[0].y : 0;
for (i = 0; i < words.length; i++)
{
var entry = words[i];
var left = entry.x;
var right = entry.x + entry.w;
if (prev)
{
var diff = left - (prev.x + prev.w);
offset = left - (diff + prev.w);
prev = null;
}
var checkLeft = left - offset;
var checkRight = right - offset;
if (checkLeft > maxWidth || checkRight > maxWidth)
{
crs.push(entry.i - 1);
if (entry.cr)
// Check if adding this word exceeds the maxWidth
if (xAdvance + wordWidth > maxWidth)
{
crs.push(entry.i + entry.word.length);
offset = 0;
prev = null;
formattedText += '\n';
xAdvance = 0;
yAdvance += lineHeight + lineSpacing;
}
else
{
prev = entry;
}
}
else if (entry.cr)
{
crs.push(entry.i + entry.word.length);
offset = 0;
prev = null;
formattedText += word + (isSpace ? ' ' : '');
xAdvance += wordWidth + (isSpace ? letterSpacing * sx : 0);
word = '';
wordWidth = 0;
}
else if (entry.y > currentY)
else // If the character is not a space or line feed, add it to the current word
{
offset = 0;
currentY = entry.y;
word += text[i];
wordWidth += getCharWidth(text[i]) * sx;
}
}
var stringInsert = function (str, index, value)
{
return str.substr(0, index) + value + str.substr(index + 1);
};
for (i = crs.length - 1; i >= 0; i--)
{
// eslint-disable-next-line quotes
text = stringInsert(text, crs[i], "\n");
}
text = formattedText;
out.wrappedText = text;
textLength = text.length;
@ -274,6 +179,10 @@ var GetBitmapTextSize = function (src, round, updateOrigin, out)
// Recalculated in the next loop
words = [];
current = null;
xAdvance = 0;
yAdvance = 0;
lastGlyph = null;
lastCharCode = 0;
}
var charIndex = 0;