1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:17:36 +00:00

LibGfx: BitmapFont: Handle '\r' and '\n' when calculating text width

Previously calculating multiline text width would return invalid value,
this change makes it so that we are returning the longest line width.

We are now also reusing same width() implementation for both UTF-8 and
UTF-32 strings.
This commit is contained in:
LuK1337 2021-07-06 12:08:06 +02:00 committed by Andreas Kling
parent 6386c2d880
commit 6319796f58
2 changed files with 23 additions and 9 deletions

View file

@ -263,29 +263,40 @@ int BitmapFont::width(const StringView& string) const
return width(utf8);
}
int BitmapFont::width(const Utf8View& utf8) const
template<typename T>
int BitmapFont::unicode_view_width(T const& view) const
{
if (view.is_empty())
return 0;
bool first = true;
int width = 0;
int longest_width = 0;
for (u32 code_point : utf8) {
for (u32 code_point : view) {
if (code_point == '\n' || code_point == '\r') {
first = true;
width = 0;
continue;
}
if (!first)
width += glyph_spacing();
first = false;
width += glyph_or_emoji_width(code_point);
if (width > longest_width)
longest_width = width;
}
return width;
return longest_width;
}
int BitmapFont::width(const Utf8View& utf8) const
{
return unicode_view_width(utf8);
}
int BitmapFont::width(const Utf32View& view) const
{
if (view.length() == 0)
return 0;
int width = (view.length() - 1) * glyph_spacing();
for (size_t i = 0; i < view.length(); ++i)
width += glyph_or_emoji_width(view.code_points()[i]);
return width;
return unicode_view_width(view);
}
void BitmapFont::set_type(FontTypes type)