1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-17 00:35:08 +00:00
serenity/Userland/Libraries/LibWeb/Painting/ButtonPaintable.cpp
Andreas Kling f900957d26 LibGfx+LibWeb: Move Gfx::ScaledFont caching from LibWeb into LibGfx
Before this change, we would only cache and reuse Gfx::ScaledFont
instances for downloaded CSS fonts.

By moving it into Gfx::VectorFont, we get caching for all vector fonts,
including local system TTFs etc.

This avoids a *lot* of style invalidations in LibWeb, since we now vend
the same Gfx::Font pointer for the same font when used repeatedly.
2023-12-26 18:15:55 +01:00

74 lines
2.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGUI/Event.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/Layout/ButtonBox.h>
#include <LibWeb/Layout/Label.h>
#include <LibWeb/Painting/ButtonPaintable.h>
namespace Web::Painting {
JS::NonnullGCPtr<ButtonPaintable> ButtonPaintable::create(Layout::ButtonBox const& layout_box)
{
return layout_box.heap().allocate_without_realm<ButtonPaintable>(layout_box);
}
ButtonPaintable::ButtonPaintable(Layout::ButtonBox const& layout_box)
: LabelablePaintable(layout_box)
{
}
Layout::ButtonBox const& ButtonPaintable::layout_box() const
{
return static_cast<Layout::ButtonBox const&>(layout_node());
}
Layout::ButtonBox& ButtonPaintable::layout_box()
{
return static_cast<Layout::ButtonBox&>(layout_node());
}
void ButtonPaintable::paint(PaintContext& context, PaintPhase phase) const
{
if (!is_visible())
return;
PaintableBox::paint(context, phase);
auto const& dom_node = layout_box().dom_node();
if (is<HTML::HTMLInputElement>(dom_node) && phase == PaintPhase::Foreground) {
auto button_rect = context.enclosing_device_rect(absolute_rect());
auto text_rect = button_rect;
// Apply CSS text-indent property to text rect
// FIXME: The second parameter to to_px() needs to be the block containers own inline-axis inner size:
// https://drafts.csswg.org/css-text-3/#propdef-text-indent
auto text_indent = computed_values().text_indent().to_px(layout_box(), CSSPixels());
text_rect.translate_by(context.rounded_device_pixels(text_indent), 0);
// Apply button pressed state offset
if (being_pressed()) {
auto offset = context.rounded_device_pixels(1);
text_rect.translate_by(offset, offset);
}
// Paint button text clipped to button rect
auto& painter = context.recording_painter();
painter.save();
painter.add_clip_rect(button_rect.to_type<int>());
painter.draw_text(
text_rect.to_type<int>(),
static_cast<HTML::HTMLInputElement const&>(dom_node).value(),
layout_box().scaled_font(context.device_pixels_per_css_pixel()),
Gfx::TextAlignment::Center,
computed_values().color());
painter.restore();
}
}
}