|
@@ -12,7 +12,7 @@
|
|
|
|
|
|
namespace Gfx {
|
|
namespace Gfx {
|
|
|
|
|
|
-void for_each_glyph_position(FloatPoint baseline_start, Utf8View string, Gfx::Font const& font, Function<void(DrawGlyph const&)> callback, Optional<float&> width)
|
|
|
|
|
|
+RefPtr<GlyphRun> shape_text(FloatPoint baseline_start, Utf8View string, Gfx::Font const& font, GlyphRun::TextType text_type)
|
|
{
|
|
{
|
|
hb_buffer_t* buffer = hb_buffer_create();
|
|
hb_buffer_t* buffer = hb_buffer_create();
|
|
ScopeGuard destroy_buffer = [&]() { hb_buffer_destroy(buffer); };
|
|
ScopeGuard destroy_buffer = [&]() { hb_buffer_destroy(buffer); };
|
|
@@ -22,8 +22,6 @@ void for_each_glyph_position(FloatPoint baseline_start, Utf8View string, Gfx::Fo
|
|
u32 glyph_count;
|
|
u32 glyph_count;
|
|
auto* glyph_info = hb_buffer_get_glyph_infos(buffer, &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 });
|
|
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();
|
|
auto* hb_font = font.harfbuzz_font();
|
|
hb_shape(hb_font, buffer, nullptr, 0);
|
|
hb_shape(hb_font, buffer, nullptr, 0);
|
|
@@ -31,27 +29,23 @@ void for_each_glyph_position(FloatPoint baseline_start, Utf8View string, Gfx::Fo
|
|
glyph_info = hb_buffer_get_glyph_infos(buffer, &glyph_count);
|
|
glyph_info = hb_buffer_get_glyph_infos(buffer, &glyph_count);
|
|
auto* positions = hb_buffer_get_glyph_positions(buffer, &glyph_count);
|
|
auto* positions = hb_buffer_get_glyph_positions(buffer, &glyph_count);
|
|
|
|
|
|
|
|
+ Vector<Gfx::DrawGlyph> glyph_run;
|
|
FloatPoint point = baseline_start;
|
|
FloatPoint point = baseline_start;
|
|
for (size_t i = 0; i < glyph_count; ++i) {
|
|
for (size_t i = 0; i < glyph_count; ++i) {
|
|
auto position = point
|
|
auto position = point
|
|
- FloatPoint { 0, font.pixel_metrics().ascent }
|
|
- FloatPoint { 0, font.pixel_metrics().ascent }
|
|
+ FloatPoint { positions[i].x_offset, positions[i].y_offset } / text_shaping_resolution;
|
|
+ FloatPoint { positions[i].x_offset, positions[i].y_offset } / text_shaping_resolution;
|
|
- callback(DrawGlyph {
|
|
|
|
- .position = position,
|
|
|
|
- .glyph_id = glyph_info[i].codepoint,
|
|
|
|
- });
|
|
|
|
|
|
+ glyph_run.append({ position, glyph_info[i].codepoint });
|
|
point += FloatPoint { positions[i].x_advance, positions[i].y_advance } / text_shaping_resolution;
|
|
point += FloatPoint { positions[i].x_advance, positions[i].y_advance } / text_shaping_resolution;
|
|
}
|
|
}
|
|
|
|
|
|
- if (width.has_value())
|
|
|
|
- *width = point.x();
|
|
|
|
|
|
+ return adopt_ref(*new Gfx::GlyphRun(move(glyph_run), font, text_type, point.x()));
|
|
}
|
|
}
|
|
|
|
|
|
float measure_text_width(Utf8View const& string, Gfx::Font const& font)
|
|
float measure_text_width(Utf8View const& string, Gfx::Font const& font)
|
|
{
|
|
{
|
|
- float width = 0;
|
|
|
|
- for_each_glyph_position({}, string, font, [&](DrawGlyph const&) {}, width);
|
|
|
|
- return width;
|
|
|
|
|
|
+ auto glyph_run = shape_text({}, string, font, GlyphRun::TextType::Common);
|
|
|
|
+ return glyph_run->width();
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|