diff --git a/Userland/Libraries/LibWeb/FontCache.cpp b/Userland/Libraries/LibWeb/FontCache.cpp index b37bf8e553..889ffe5699 100644 --- a/Userland/Libraries/LibWeb/FontCache.cpp +++ b/Userland/Libraries/LibWeb/FontCache.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include @@ -21,6 +22,22 @@ RefPtr FontCache::get(FontSelector const& font_selector) const return nullptr; } +NonnullRefPtr FontCache::scaled_font(Gfx::Font const& font, float scale_factor) +{ + auto device_font_pt_size = font.point_size() * scale_factor; + FontSelector font_selector = { FlyString::from_deprecated_fly_string(font.family()).release_value_but_fixme_should_propagate_errors(), device_font_pt_size, 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(device_font_pt_size)) { + set(font_selector, *font_with_device_pt_size); + return font_with_device_pt_size.release_nonnull(); + } + + return font; +} + void FontCache::set(FontSelector const& font_selector, NonnullRefPtr font) { m_fonts.set(font_selector, move(font)); diff --git a/Userland/Libraries/LibWeb/FontCache.h b/Userland/Libraries/LibWeb/FontCache.h index daf725305f..a0dccdf394 100644 --- a/Userland/Libraries/LibWeb/FontCache.h +++ b/Userland/Libraries/LibWeb/FontCache.h @@ -37,6 +37,8 @@ public: RefPtr get(FontSelector const&) const; void set(FontSelector const&, NonnullRefPtr); + NonnullRefPtr scaled_font(Gfx::Font const&, float scale_factor); + private: FontCache() = default; mutable HashMap> m_fonts; diff --git a/Userland/Libraries/LibWeb/Painting/ButtonPaintable.cpp b/Userland/Libraries/LibWeb/Painting/ButtonPaintable.cpp index 60a0f03596..c0e58aad25 100644 --- a/Userland/Libraries/LibWeb/Painting/ButtonPaintable.cpp +++ b/Userland/Libraries/LibWeb/Painting/ButtonPaintable.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -47,7 +48,12 @@ void ButtonPaintable::paint(PaintContext& context, PaintPhase phase) const auto offset = context.rounded_device_pixels(1); text_rect.translate_by(offset, offset); } - context.painter().draw_text(text_rect.to_type(), static_cast(dom_node).value(), layout_box().font(), Gfx::TextAlignment::Center, computed_values().color()); + context.painter().draw_text( + text_rect.to_type(), + static_cast(dom_node).value(), + FontCache::the().scaled_font(layout_box().font(), context.device_pixels_per_css_pixel()), + Gfx::TextAlignment::Center, + computed_values().color()); } } diff --git a/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp b/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp index c625efbec0..43b2d8e41c 100644 --- a/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp +++ b/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp @@ -525,30 +525,16 @@ 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()) }; - auto& font = fragment.layout_node().font(); - auto scaled_font = [&]() -> RefPtr { - auto device_font_pt_size = font.point_size() * context.device_pixels_per_css_pixel(); - FontSelector font_selector = { FlyString::from_deprecated_fly_string(font.family()).release_value_but_fixme_should_propagate_errors(), device_font_pt_size, font.weight(), font.width(), font.slope() }; - if (auto cached_font = FontCache::the().get(font_selector)) { - return cached_font; - } + auto scaled_font = FontCache::the().scaled_font(fragment.layout_node().font(), context.device_pixels_per_css_pixel()); - if (auto font_with_device_pt_size = font.with_size(device_font_pt_size)) { - 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()); + painter.draw_text_run(baseline_start.to_type(), view, *scaled_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, scaled_font ? *scaled_font : font, context.palette().selection_text()); + painter.draw_text_run(baseline_start.to_type(), view, *scaled_font, context.palette().selection_text()); } paint_text_decoration(context, painter, text_node, fragment);