LibGfx+LibWeb: Use harfbuzz for text shaping

This replaces glyph positioning system with harfbuzz's shaping
algorithm. Adding support for bidirectional encoded text.
This commit is contained in:
BenJilks
2024-08-13 17:05:43 +01:00
committed by Alexander Kalenik
parent 0d05ab2ad0
commit 0d63269cb7
48 changed files with 365 additions and 333 deletions

View File

@@ -7,51 +7,82 @@
#include "TextLayout.h"
#include "Font/Emoji.h"
#include <AK/Debug.h>
#include <LibUnicode/CharacterTypes.h>
#include <AK/TypeCasts.h>
#include <LibGfx/Font/ScaledFont.h>
#include <LibUnicode/Emoji.h>
#include <harfbuzz/hb.h>
namespace Gfx {
DrawGlyphOrEmoji prepare_draw_glyph_or_emoji(FloatPoint point, Utf8CodePointIterator& it, Font const& font)
static DrawGlyphOrEmoji construct_glyph_or_emoji(size_t index, FloatPoint const& position, Gfx::Font const& font, Span<hb_glyph_info_t const> glyph_info, Span<hb_glyph_info_t const> input_glyph_info)
{
u32 code_point = *it;
auto next_code_point = it.peek(1);
if (font.has_color_bitmaps()) {
auto cluster_start = glyph_info[index].cluster;
auto cluster_end = [&]() -> u32 {
if (index + 1 < glyph_info.size())
return glyph_info[index + 1].cluster;
return input_glyph_info.last().cluster + 1;
}();
ScopeGuard consume_variation_selector = [&, initial_it = it] {
// If we advanced the iterator to consume an emoji sequence, don't look for another variation selector.
if (initial_it != it)
return;
Vector<u32> cluster;
for (size_t j = 0; j < input_glyph_info.size(); ++j) {
auto const& glyph = input_glyph_info[j];
if (glyph.cluster >= cluster_end)
break;
if (glyph.cluster >= cluster_start)
cluster.append(glyph.codepoint);
}
// Otherwise, discard one code point if it's a variation selector.
if (next_code_point.has_value() && Unicode::code_point_has_variation_selector_property(*next_code_point))
++it;
};
// NOTE: We don't check for emoji
auto font_contains_glyph = font.contains_glyph(code_point);
auto check_for_emoji = !font.has_color_bitmaps() && Unicode::could_be_start_of_emoji_sequence(it, font_contains_glyph ? Unicode::SequenceType::EmojiPresentation : Unicode::SequenceType::Any);
// If the font contains the glyph, and we know it's not the start of an emoji, draw a text glyph.
if (font_contains_glyph && !check_for_emoji) {
return DrawGlyph {
.position = point,
.code_point = code_point,
};
}
// If we didn't find a text glyph, or have an emoji variation selector or regional indicator, try to draw an emoji glyph.
if (auto const* emoji = Emoji::emoji_for_code_point_iterator(it)) {
return DrawEmoji {
.position = point,
.emoji = emoji,
};
if (auto const* emoji = Emoji::emoji_for_code_points(cluster)) {
return DrawEmoji {
.position = position,
.emoji = emoji,
};
}
}
return DrawGlyph {
.position = point,
.code_point = code_point,
.position = position,
.glyph_id = glyph_info[index].codepoint,
};
}
void for_each_glyph_position(FloatPoint baseline_start, Utf8View string, Gfx::Font const& font, Function<void(DrawGlyphOrEmoji const&)> callback, IncludeLeftBearing include_left_bearing, Optional<float&> width)
{
hb_buffer_t* buffer = hb_buffer_create();
ScopeGuard destroy_buffer = [&]() { hb_buffer_destroy(buffer); };
hb_buffer_add_utf8(buffer, reinterpret_cast<char const*>(string.bytes()), string.byte_length(), 0, -1);
hb_buffer_guess_segment_properties(buffer);
u32 glyph_count;
auto* glyph_info = hb_buffer_get_glyph_infos(buffer, &glyph_count);
Vector<hb_glyph_info_t> const input_glyph_info({ glyph_info, glyph_count });
if (input_glyph_info.is_empty())
return;
auto* hb_font = font.harfbuzz_font();
hb_shape(hb_font, buffer, nullptr, 0);
glyph_info = hb_buffer_get_glyph_infos(buffer, &glyph_count);
auto* positions = hb_buffer_get_glyph_positions(buffer, &glyph_count);
FloatPoint point = baseline_start;
for (size_t i = 0; i < glyph_count; ++i) {
auto position = point
- FloatPoint { 0, font.pixel_metrics().ascent }
+ FloatPoint { positions[i].x_offset, positions[i].y_offset } / text_shaping_resolution;
if (include_left_bearing == IncludeLeftBearing::Yes) {
VERIFY(is<Gfx::ScaledFont>(font));
auto bearing = static_cast<Gfx::ScaledFont const&>(font).glyph_metrics(glyph_info[i].codepoint).left_side_bearing;
position += FloatPoint { bearing, 0 };
}
callback(construct_glyph_or_emoji(i, position, font, { glyph_info, glyph_count }, input_glyph_info.span()));
point += FloatPoint { positions[i].x_advance, positions[i].y_advance } / text_shaping_resolution;
}
if (width.has_value())
*width = point.x();
}
}