mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-20 01:37:39 +03:00
LibWeb: Scale the font when painting the text on buttons
Button text was staying the same size at all zoom levels. :^)
This commit is contained in:
parent
20132da88d
commit
5146b9b35e
Notes:
sideshowbarker
2024-07-16 23:59:28 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/5146b9b35e
@ -4,6 +4,7 @@
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/DeprecatedFlyString.h>
|
||||
#include <LibGfx/Font/Font.h>
|
||||
#include <LibWeb/FontCache.h>
|
||||
|
||||
@ -21,6 +22,22 @@ RefPtr<Gfx::Font const> FontCache::get(FontSelector const& font_selector) const
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
NonnullRefPtr<Gfx::Font const> 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<Gfx::Font const> font)
|
||||
{
|
||||
m_fonts.set(font_selector, move(font));
|
||||
|
@ -37,6 +37,8 @@ public:
|
||||
RefPtr<Gfx::Font const> get(FontSelector const&) const;
|
||||
void set(FontSelector const&, NonnullRefPtr<Gfx::Font const>);
|
||||
|
||||
NonnullRefPtr<Gfx::Font const> scaled_font(Gfx::Font const&, float scale_factor);
|
||||
|
||||
private:
|
||||
FontCache() = default;
|
||||
mutable HashMap<FontSelector, NonnullRefPtr<Gfx::Font const>> m_fonts;
|
||||
|
@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
#include <LibGUI/Event.h>
|
||||
#include <LibWeb/FontCache.h>
|
||||
#include <LibWeb/HTML/BrowsingContext.h>
|
||||
#include <LibWeb/HTML/HTMLImageElement.h>
|
||||
#include <LibWeb/Layout/ButtonBox.h>
|
||||
@ -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<int>(), static_cast<HTML::HTMLInputElement const&>(dom_node).value(), layout_box().font(), Gfx::TextAlignment::Center, computed_values().color());
|
||||
context.painter().draw_text(
|
||||
text_rect.to_type<int>(),
|
||||
static_cast<HTML::HTMLInputElement const&>(dom_node).value(),
|
||||
FontCache::the().scaled_font(layout_box().font(), context.device_pixels_per_css_pixel()),
|
||||
Gfx::TextAlignment::Center,
|
||||
computed_values().color());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<Gfx::Font const> {
|
||||
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<int>(), view, scaled_font ? *scaled_font : font, text_node.computed_values().color());
|
||||
painter.draw_text_run(baseline_start.to_type<int>(), view, *scaled_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, scaled_font ? *scaled_font : font, context.palette().selection_text());
|
||||
painter.draw_text_run(baseline_start.to_type<int>(), view, *scaled_font, context.palette().selection_text());
|
||||
}
|
||||
|
||||
paint_text_decoration(context, painter, text_node, fragment);
|
||||
|
Loading…
Reference in New Issue
Block a user