diff options
author | Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com> | 2023-02-09 12:05:19 +0300 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-02-11 20:59:13 +0000 |
commit | ac440e6c0e5daded6dd709ea78224bd73f0db6e1 (patch) | |
tree | c3689e9f2564d93cd0ad43e3c801254a47d36ef5 /Userland/Libraries/LibWeb/Painting/PaintableBox.cpp | |
parent | 1cc8895e4bca13f7b65cb7d290b2138b49b6bcfe (diff) | |
download | serenity-ac440e6c0e5daded6dd709ea78224bd73f0db6e1.zip |
LibWeb: Scale font size to device pixels
Diffstat (limited to 'Userland/Libraries/LibWeb/Painting/PaintableBox.cpp')
-rw-r--r-- | Userland/Libraries/LibWeb/Painting/PaintableBox.cpp | 21 |
1 files changed, 19 insertions, 2 deletions
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 <AK/GenericShorthands.h> #include <LibUnicode/CharacterTypes.h> #include <LibWeb/DOM/Document.h> +#include <LibWeb/FontCache.h> #include <LibWeb/HTML/HTMLHtmlElement.h> #include <LibWeb/Layout/BlockContainer.h> #include <LibWeb/Layout/InitialContainingBlock.h> @@ -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<int>(), view, fragment.layout_node().font(), text_node.computed_values().color()); + auto& font = fragment.layout_node().font(); + auto scaled_font = [&]() -> RefPtr<Gfx::Font> { + auto device_font_pt_size = context.enclosing_device_pixels(font.presentation_size()); + FontSelector font_selector = { font.family(), static_cast<float>(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<float>(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<int>(), 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<int>(); 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<int>(), view, fragment.layout_node().font(), context.palette().selection_text()); + painter.draw_text_run(baseline_start.to_type<int>(), view, scaled_font ? *scaled_font : font, context.palette().selection_text()); } paint_text_decoration(context, painter, text_node, fragment); |