From ac440e6c0e5daded6dd709ea78224bd73f0db6e1 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Thu, 9 Feb 2023 12:05:19 +0300 Subject: LibWeb: Scale font size to device pixels --- Userland/Libraries/LibWeb/Painting/PaintableBox.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'Userland/Libraries/LibWeb/Painting/PaintableBox.cpp') diff --git a/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp b/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp index a041696188..111a46766c 100644 --- a/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp +++ b/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -511,14 +512,30 @@ static void paint_text_fragment(PaintContext& context, Layout::TextNode const& t DevicePixelPoint baseline_start { fragment_absolute_device_rect.x(), fragment_absolute_device_rect.y() + context.rounded_device_pixels(fragment.baseline()) }; Utf8View view { text.substring_view(fragment.start(), fragment.length()) }; - painter.draw_text_run(baseline_start.to_type(), view, fragment.layout_node().font(), text_node.computed_values().color()); + auto& font = fragment.layout_node().font(); + auto scaled_font = [&]() -> RefPtr { + auto device_font_pt_size = context.enclosing_device_pixels(font.presentation_size()); + FontSelector font_selector = { font.family(), static_cast(device_font_pt_size.value()), font.weight(), font.width(), font.slope() }; + if (auto cached_font = FontCache::the().get(font_selector)) { + return cached_font; + } + + if (auto font_with_device_pt_size = font.with_size(static_cast(device_font_pt_size.value()))) { + FontCache::the().set(font_selector, *font_with_device_pt_size); + return font_with_device_pt_size; + } + + return {}; + }(); + + painter.draw_text_run(baseline_start.to_type(), view, scaled_font ? *scaled_font : font, text_node.computed_values().color()); auto selection_rect = context.enclosing_device_rect(fragment.selection_rect(text_node.font())).to_type(); if (!selection_rect.is_empty()) { painter.fill_rect(selection_rect, context.palette().selection()); Gfx::PainterStateSaver saver(painter); painter.add_clip_rect(selection_rect); - painter.draw_text_run(baseline_start.to_type(), view, fragment.layout_node().font(), context.palette().selection_text()); + painter.draw_text_run(baseline_start.to_type(), view, scaled_font ? *scaled_font : font, context.palette().selection_text()); } paint_text_decoration(context, painter, text_node, fragment); -- cgit v1.2.3