1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:47:44 +00:00

LibWeb: Use the new Gfx::Painter::draw_text_run() API for drawing text

This avoids a bunch of unnecessary work in Painter which not only took
time, but sometimes also led to alignment issues. draw_text_run() will
draw the text where we tell it, and that's it.
This commit is contained in:
Andreas Kling 2022-03-28 21:05:51 +02:00
parent dd940dfa85
commit 1c88536298
4 changed files with 16 additions and 9 deletions

View file

@ -369,8 +369,6 @@ static void paint_text_fragment(PaintContext& context, Layout::TextNode const& t
if (phase == Painting::PaintPhase::Foreground) {
auto fragment_absolute_rect = fragment.absolute_rect();
painter.set_font(text_node.font());
if (text_node.document().inspected_node() == &text_node.dom_node())
context.painter().draw_rect(enclosing_int_rect(fragment_absolute_rect), Color::Magenta);
@ -382,17 +380,17 @@ static void paint_text_fragment(PaintContext& context, Layout::TextNode const& t
if (text_transform == CSS::TextTransform::Lowercase)
text = text_node.text_for_rendering().to_lowercase();
// FIXME: This is a hack to prevent text clipping when painting a bitmap font into a too-small box.
auto draw_rect = enclosing_int_rect(fragment_absolute_rect);
draw_rect.set_height(max(draw_rect.height(), text_node.font().pixel_size()));
painter.draw_text(draw_rect, text.substring_view(fragment.start(), fragment.length()), Gfx::TextAlignment::CenterLeft, text_node.computed_values().color());
Gfx::FloatPoint baseline_start { fragment_absolute_rect.x(), fragment_absolute_rect.y() + fragment.baseline() };
Utf8View view { text.substring_view(fragment.start(), fragment.length()) };
painter.draw_text_run(baseline_start, view, fragment.layout_node().font(), text_node.computed_values().color());
auto selection_rect = fragment.selection_rect(text_node.font());
if (!selection_rect.is_empty()) {
painter.fill_rect(enclosing_int_rect(selection_rect), context.palette().selection());
Gfx::PainterStateSaver saver(painter);
painter.add_clip_rect(enclosing_int_rect(selection_rect));
painter.draw_text(enclosing_int_rect(fragment_absolute_rect), text.substring_view(fragment.start(), fragment.length()), Gfx::TextAlignment::CenterLeft, context.palette().selection_text());
painter.draw_text_run(baseline_start, view, fragment.layout_node().font(), context.palette().selection_text());
}
paint_text_decoration(painter, text_node, fragment);