LibGfx: Replace ad-hoc text width calculation with harfbuzz
Since harfbuzz is already used to calculate glyph positions, let's also use it to measure width.
This commit is contained in:
parent
a541a0792c
commit
4786604f8b
Notes:
github-actions[bot]
2024-09-04 11:15:29 +00:00
Author: https://github.com/kalenikaliaksandr Commit: https://github.com/LadybirdBrowser/ladybird/commit/4786604f8bb Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1271
4 changed files with 11 additions and 31 deletions
|
@ -7,6 +7,7 @@
|
|||
#include <AK/Utf8View.h>
|
||||
#include <LibGfx/Font/Emoji.h>
|
||||
#include <LibGfx/Font/ScaledFont.h>
|
||||
#include <LibGfx/TextLayout.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
|
@ -34,34 +35,8 @@ ScaledFont::ScaledFont(NonnullRefPtr<Typeface> typeface, float point_width, floa
|
|||
};
|
||||
}
|
||||
|
||||
float ScaledFont::width(StringView view) const { return unicode_view_width(Utf8View(view)); }
|
||||
float ScaledFont::width(Utf8View const& view) const { return unicode_view_width(view); }
|
||||
|
||||
template<typename T>
|
||||
ALWAYS_INLINE float ScaledFont::unicode_view_width(T const& view) const
|
||||
{
|
||||
if (view.is_empty())
|
||||
return 0;
|
||||
float width = 0;
|
||||
float longest_width = 0;
|
||||
u32 last_code_point = 0;
|
||||
|
||||
for (auto it = view.begin(); it != view.end(); last_code_point = *it, ++it) {
|
||||
auto code_point = *it;
|
||||
|
||||
if (code_point == '\n' || code_point == '\r') {
|
||||
longest_width = max(width, longest_width);
|
||||
width = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
auto kerning = glyphs_horizontal_kerning(last_code_point, code_point);
|
||||
width += kerning + glyph_or_emoji_width(it);
|
||||
}
|
||||
|
||||
longest_width = max(width, longest_width);
|
||||
return longest_width;
|
||||
}
|
||||
float ScaledFont::width(StringView view) const { return measure_text_width(Utf8View(view), *this); }
|
||||
float ScaledFont::width(Utf8View const& view) const { return measure_text_width(view, *this); }
|
||||
|
||||
float ScaledFont::glyph_width(u32 code_point) const
|
||||
{
|
||||
|
|
|
@ -59,9 +59,6 @@ private:
|
|||
|
||||
float m_pixel_size { 0.0f };
|
||||
int m_pixel_size_rounded_up { 0 };
|
||||
|
||||
template<typename T>
|
||||
float unicode_view_width(T const& view) const;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -54,4 +54,11 @@ void for_each_glyph_position(FloatPoint baseline_start, Utf8View string, Gfx::Fo
|
|||
*width = point.x();
|
||||
}
|
||||
|
||||
float measure_text_width(Utf8View const& string, Gfx::Font const& font)
|
||||
{
|
||||
float width = 0;
|
||||
for_each_glyph_position({}, string, font, [&](DrawGlyphOrEmoji const&) {}, width);
|
||||
return width;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -74,5 +74,6 @@ private:
|
|||
};
|
||||
|
||||
void for_each_glyph_position(FloatPoint baseline_start, Utf8View string, Gfx::Font const& font, Function<void(DrawGlyphOrEmoji const&)> callback, Optional<float&> width = {});
|
||||
float measure_text_width(Utf8View const& string, Gfx::Font const& font);
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue