LibGfx+LibWeb: Remove DrawEmoji variant in GlyphRun

It was needed to support bitmap emoji rendering but no longer used after
switching to Skia+HarfBuzz for text rendering.
This commit is contained in:
Aliaksandr Kalenik 2024-09-06 01:19:53 +02:00 committed by Tim Flynn
parent 22b8380e37
commit ec315667f0
Notes: github-actions[bot] 2024-09-06 12:31:34 +00:00
8 changed files with 37 additions and 72 deletions

View file

@ -14,15 +14,7 @@
namespace Gfx {
static DrawGlyphOrEmoji construct_glyph_or_emoji(size_t index, FloatPoint const& position, Gfx::Font const&, Span<hb_glyph_info_t const> glyph_info, Span<hb_glyph_info_t const>)
{
return DrawGlyph {
.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, Optional<float&> width)
void for_each_glyph_position(FloatPoint baseline_start, Utf8View string, Gfx::Font const& font, Function<void(DrawGlyph const&)> callback, Optional<float&> width)
{
hb_buffer_t* buffer = hb_buffer_create();
ScopeGuard destroy_buffer = [&]() { hb_buffer_destroy(buffer); };
@ -46,7 +38,10 @@ void for_each_glyph_position(FloatPoint baseline_start, Utf8View string, Gfx::Fo
auto position = point
- FloatPoint { 0, font.pixel_metrics().ascent }
+ FloatPoint { positions[i].x_offset, positions[i].y_offset } / text_shaping_resolution;
callback(construct_glyph_or_emoji(i, position, font, { glyph_info, glyph_count }, input_glyph_info.span()));
callback(DrawGlyph {
.position = position,
.glyph_id = glyph_info[i].codepoint,
});
point += FloatPoint { positions[i].x_advance, positions[i].y_advance } / text_shaping_resolution;
}
@ -57,7 +52,7 @@ void for_each_glyph_position(FloatPoint baseline_start, Utf8View string, Gfx::Fo
float measure_text_width(Utf8View const& string, Gfx::Font const& font)
{
float width = 0;
for_each_glyph_position({}, string, font, [&](DrawGlyphOrEmoji const&) {}, width);
for_each_glyph_position({}, string, font, [&](DrawGlyph const&) {}, width);
return width;
}

View file

@ -30,18 +30,6 @@ struct DrawGlyph {
}
};
struct DrawEmoji {
FloatPoint position;
Gfx::Bitmap const* emoji;
void translate_by(FloatPoint const& delta)
{
position.translate_by(delta);
}
};
using DrawGlyphOrEmoji = Variant<DrawGlyph, DrawEmoji>;
class GlyphRun : public RefCounted<GlyphRun> {
public:
enum class TextType {
@ -52,7 +40,7 @@ public:
Rtl,
};
GlyphRun(Vector<Gfx::DrawGlyphOrEmoji>&& glyphs, NonnullRefPtr<Font> font, TextType text_type)
GlyphRun(Vector<DrawGlyph>&& glyphs, NonnullRefPtr<Font> font, TextType text_type)
: m_glyphs(move(glyphs))
, m_font(move(font))
, m_text_type(text_type)
@ -61,19 +49,19 @@ public:
[[nodiscard]] Font const& font() const { return m_font; }
[[nodiscard]] TextType text_type() const { return m_text_type; }
[[nodiscard]] Vector<Gfx::DrawGlyphOrEmoji> const& glyphs() const { return m_glyphs; }
[[nodiscard]] Vector<Gfx::DrawGlyphOrEmoji>& glyphs() { return m_glyphs; }
[[nodiscard]] Vector<DrawGlyph> const& glyphs() const { return m_glyphs; }
[[nodiscard]] Vector<DrawGlyph>& glyphs() { return m_glyphs; }
[[nodiscard]] bool is_empty() const { return m_glyphs.is_empty(); }
void append(Gfx::DrawGlyphOrEmoji glyph) { m_glyphs.append(glyph); }
void append(DrawGlyph glyph) { m_glyphs.append(glyph); }
private:
Vector<Gfx::DrawGlyphOrEmoji> m_glyphs;
Vector<DrawGlyph> m_glyphs;
NonnullRefPtr<Font> m_font;
TextType m_text_type;
};
void for_each_glyph_position(FloatPoint baseline_start, Utf8View string, Gfx::Font const& font, Function<void(DrawGlyphOrEmoji const&)> callback, Optional<float&> width = {});
void for_each_glyph_position(FloatPoint baseline_start, Utf8View string, Gfx::Font const& font, Function<void(DrawGlyph const&)> callback, Optional<float&> width = {});
float measure_text_width(Utf8View const& string, Gfx::Font const& font);
}

View file

@ -516,8 +516,8 @@ CanvasRenderingContext2D::PreparedText CanvasRenderingContext2D::prepare_text(By
auto glyph_run = adopt_ref(*new Gfx::GlyphRun({}, *font, Gfx::GlyphRun::TextType::Ltr));
float glyph_run_width = 0;
Gfx::for_each_glyph_position(
anchor, replaced_text.code_points(), *font, [&](Gfx::DrawGlyphOrEmoji const& glyph_or_emoji) {
glyph_run->append(glyph_or_emoji);
anchor, replaced_text.code_points(), *font, [&](Gfx::DrawGlyph const& glyph) {
glyph_run->append(glyph);
},
glyph_run_width);

View file

@ -356,20 +356,11 @@ void InlineFormattingContext::generate_line_boxes(LayoutMode layout_mode)
size_t last_glyph_index = 0;
auto last_glyph_position = Gfx::FloatPoint();
for (auto const& glyph_or_emoji : glyphs) {
auto this_position = Gfx::FloatPoint();
glyph_or_emoji.visit(
[&](Gfx::DrawGlyph glyph) {
this_position = glyph.position;
},
[&](Gfx::DrawEmoji emoji) {
this_position = emoji.position;
});
if (this_position.x() > max_text_width)
for (auto const& glyph : glyphs) {
if (glyph.position.x() > max_text_width)
break;
last_glyph_index++;
last_glyph_position = this_position;
last_glyph_position = glyph.position;
}
if (last_glyph_index > 1) {

View file

@ -239,11 +239,11 @@ Optional<InlineLevelIterator::Item> InlineLevelIterator::next_without_lookahead(
};
}
Vector<Gfx::DrawGlyphOrEmoji> glyph_run;
Vector<Gfx::DrawGlyph> glyph_run;
float glyph_run_width = 0;
Gfx::for_each_glyph_position(
{ 0, 0 }, chunk.view, chunk.font, [&](Gfx::DrawGlyphOrEmoji const& glyph_or_emoji) {
glyph_run.append(glyph_or_emoji);
{ 0, 0 }, chunk.view, chunk.font, [&](Gfx::DrawGlyph const& glyph) {
glyph_run.append(glyph);
},
glyph_run_width);

View file

@ -103,19 +103,17 @@ void LineBoxFragment::append_glyph_run_ltr(RefPtr<Gfx::GlyphRun> const& glyph_ru
switch (run_direction) {
case CSS::Direction::Ltr:
for (auto& glyph : glyph_run->glyphs()) {
glyph.visit([&](auto& glyph) { glyph.position.translate_by(width().to_float(), 0); });
glyph.position.translate_by(width().to_float(), 0);
m_glyph_run->append(glyph);
}
break;
case CSS::Direction::Rtl:
for (auto& glyph : m_glyph_run->glyphs()) {
glyph.visit([&](auto& glyph) {
if (glyph.position.x() >= m_insert_position)
glyph.position.translate_by(run_width.to_float(), 0);
});
if (glyph.position.x() >= m_insert_position)
glyph.position.translate_by(run_width.to_float(), 0);
}
for (auto& glyph : glyph_run->glyphs()) {
glyph.visit([&](auto& glyph) { glyph.position.translate_by(m_insert_position, 0); });
glyph.position.translate_by(m_insert_position, 0);
m_glyph_run->append(glyph);
}
break;
@ -137,20 +135,18 @@ void LineBoxFragment::append_glyph_run_rtl(RefPtr<Gfx::GlyphRun> const& glyph_ru
switch (run_direction) {
case CSS::Direction::Ltr:
for (auto& glyph : m_glyph_run->glyphs()) {
glyph.visit([&](auto& glyph) {
if (glyph.position.x() >= m_insert_position)
glyph.position.translate_by(run_width.to_float(), 0);
});
if (glyph.position.x() >= m_insert_position)
glyph.position.translate_by(run_width.to_float(), 0);
}
for (auto& glyph : glyph_run->glyphs()) {
glyph.visit([&](auto& glyph) { glyph.position.translate_by(m_insert_position, 0); });
glyph.position.translate_by(m_insert_position, 0);
m_glyph_run->append(glyph);
}
break;
case CSS::Direction::Rtl:
if (glyph_run->text_type() != Gfx::GlyphRun::TextType::EndPadding) {
for (auto& glyph : m_glyph_run->glyphs()) {
glyph.visit([&](auto& glyph) { glyph.position.translate_by(run_width.to_float(), 0); });
glyph.position.translate_by(run_width.to_float(), 0);
}
}
for (auto& glyph : glyph_run->glyphs()) {

View file

@ -344,18 +344,13 @@ void DisplayListPlayerSkia::draw_glyph_run(DrawGlyphRun const& command)
Vector<SkPoint> positions;
positions.ensure_capacity(glyph_count);
auto font_ascent = gfx_font.pixel_metrics().ascent;
for (auto const& glyph_or_emoji : command.glyph_run->glyphs()) {
auto transformed_glyph = glyph_or_emoji;
transformed_glyph.visit([&](auto& glyph) {
glyph.position.set_y(glyph.position.y() + font_ascent);
glyph.position = glyph.position.scaled(command.scale);
});
if (transformed_glyph.has<Gfx::DrawGlyph>()) {
auto& glyph = transformed_glyph.get<Gfx::DrawGlyph>();
auto const& point = glyph.position;
glyphs.append(glyph.glyph_id);
positions.append(to_skia_point(point));
}
for (auto const& glyph : command.glyph_run->glyphs()) {
auto transformed_glyph = glyph;
transformed_glyph.position.set_y(glyph.position.y() + font_ascent);
transformed_glyph.position = transformed_glyph.position.scaled(command.scale);
auto const& point = transformed_glyph.position;
glyphs.append(transformed_glyph.glyph_id);
positions.append(to_skia_point(point));
}
SkPaint paint;

View file

@ -236,8 +236,8 @@ void DisplayListRecorder::draw_text(Gfx::IntRect const& rect, String raw_text, G
auto glyph_run = adopt_ref(*new Gfx::GlyphRun({}, font, Gfx::GlyphRun::TextType::Ltr));
float glyph_run_width = 0;
Gfx::for_each_glyph_position(
{ 0, 0 }, raw_text.code_points(), font, [&](Gfx::DrawGlyphOrEmoji const& glyph_or_emoji) {
glyph_run->append(glyph_or_emoji);
{ 0, 0 }, raw_text.code_points(), font, [&](Gfx::DrawGlyph const& glyph) {
glyph_run->append(glyph);
},
glyph_run_width);