1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 10:37:45 +00:00

LibWeb: Convert Paintable coordinates to new pixel units

This fixes a few sizing issues too. The page size is now correct in most
cases! \o/

We get to remove some of the `to_type<>()` shenanigans, though it
reappears in some other places.
This commit is contained in:
Sam Atkins 2022-10-31 19:46:55 +00:00 committed by Linus Groh
parent 57a69f15ff
commit ab49dbf137
39 changed files with 200 additions and 179 deletions

View file

@ -42,10 +42,12 @@ void ButtonPaintable::paint(PaintContext& context, PaintPhase phase) const
auto const& dom_node = layout_box().dom_node();
if (is<HTML::HTMLInputElement>(dom_node) && phase == PaintPhase::Foreground) {
auto text_rect = enclosing_int_rect(absolute_rect());
if (being_pressed())
text_rect.translate_by(1, 1);
context.painter().draw_text(text_rect, static_cast<HTML::HTMLInputElement const&>(dom_node).value(), layout_box().font(), Gfx::TextAlignment::Center, computed_values().color());
auto text_rect = context.enclosing_device_rect(absolute_rect());
if (being_pressed()) {
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());
}
}

View file

@ -31,7 +31,7 @@ void CanvasPaintable::paint(PaintContext& context, PaintPhase phase) const
PaintableBox::paint(context, phase);
if (phase == PaintPhase::Foreground) {
auto canvas_rect = context.rounded_device_rect(absolute_rect().to_type<CSSPixels>());
auto canvas_rect = context.rounded_device_rect(absolute_rect());
ScopedCornerRadiusClip corner_clip { context, context.painter(), canvas_rect, normalized_border_radii_data(ShrinkRadiiForBorders::Yes) };
// FIXME: This should be done at a different level.

View file

@ -43,7 +43,7 @@ void CheckBoxPaintable::paint(PaintContext& context, PaintPhase phase) const
auto const& checkbox = static_cast<HTML::HTMLInputElement const&>(layout_box().dom_node());
if (phase == PaintPhase::Foreground)
Gfx::StylePainter::paint_check_box(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), layout_box().dom_node().enabled(), checkbox.checked(), being_pressed());
Gfx::StylePainter::paint_check_box(context.painter(), context.enclosing_device_rect(absolute_rect()).to_type<int>(), context.palette(), layout_box().dom_node().enabled(), checkbox.checked(), being_pressed());
}
}

View file

@ -42,14 +42,15 @@ void ImagePaintable::paint(PaintContext& context, PaintPhase phase) const
if (phase == PaintPhase::Foreground) {
if (layout_box().renders_as_alt_text()) {
auto& image_element = verify_cast<HTML::HTMLImageElement>(*dom_node());
auto enclosing_rect = context.enclosing_device_rect(absolute_rect()).to_type<int>();
context.painter().set_font(Platform::FontPlugin::the().default_font());
Gfx::StylePainter::paint_frame(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
Gfx::StylePainter::paint_frame(context.painter(), enclosing_rect, context.palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
auto alt = image_element.alt();
if (alt.is_empty())
alt = image_element.src();
context.painter().draw_text(enclosing_int_rect(absolute_rect()), alt, Gfx::TextAlignment::Center, computed_values().color(), Gfx::TextElision::Right);
context.painter().draw_text(enclosing_rect, alt, Gfx::TextAlignment::Center, computed_values().color(), Gfx::TextElision::Right);
} else if (auto bitmap = layout_box().image_loader().bitmap(layout_box().image_loader().current_frame_index())) {
auto image_rect = context.rounded_device_rect(absolute_rect().to_type<CSSPixels>());
auto image_rect = context.rounded_device_rect(absolute_rect());
ScopedCornerRadiusClip corner_clip { context, context.painter(), image_rect, normalized_border_radii_data(ShrinkRadiiForBorders::Yes) };
context.painter().draw_scaled_bitmap(image_rect.to_type<int>(), *bitmap, bitmap->rect(), 1.0f, to_gfx_scaling_mode(computed_values().image_rendering()));
}

View file

@ -41,7 +41,7 @@ void InlinePaintable::paint(PaintContext& context, Painting::PaintPhase phase) c
auto containing_block_position_in_absolute_coordinates = containing_block()->paint_box()->absolute_position();
for_each_fragment([&](auto const& fragment, bool is_first_fragment, bool is_last_fragment) {
Gfx::FloatRect absolute_fragment_rect { containing_block_position_in_absolute_coordinates.translated(fragment.offset()), fragment.size() };
CSSPixelRect absolute_fragment_rect { containing_block_position_in_absolute_coordinates.translated(fragment.offset()), fragment.size() };
if (is_first_fragment) {
float extra_start_width = box_model().padding.left;
@ -54,8 +54,8 @@ void InlinePaintable::paint(PaintContext& context, Painting::PaintPhase phase) c
absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_end_width);
}
auto border_radii_data = Painting::normalized_border_radii_data(layout_node(), absolute_fragment_rect.to_type<CSSPixels>(), top_left_border_radius, top_right_border_radius, bottom_right_border_radius, bottom_left_border_radius);
Painting::paint_background(context, layout_node(), absolute_fragment_rect.to_type<CSSPixels>(), computed_values().background_color(), computed_values().image_rendering(), &computed_values().background_layers(), border_radii_data);
auto border_radii_data = Painting::normalized_border_radii_data(layout_node(), absolute_fragment_rect, top_left_border_radius, top_right_border_radius, bottom_right_border_radius, bottom_left_border_radius);
Painting::paint_background(context, layout_node(), absolute_fragment_rect, computed_values().background_color(), computed_values().image_rendering(), &computed_values().background_layers(), border_radii_data);
if (auto computed_box_shadow = computed_values().box_shadow(); !computed_box_shadow.is_empty()) {
Vector<Painting::ShadowData> resolved_box_shadow_data;
@ -69,7 +69,7 @@ void InlinePaintable::paint(PaintContext& context, Painting::PaintPhase phase) c
layer.spread_distance.to_px(layout_node()),
layer.placement == CSS::ShadowPlacement::Outer ? Painting::ShadowPlacement::Outer : Painting::ShadowPlacement::Inner);
}
Painting::paint_box_shadow(context, absolute_fragment_rect.to_type<CSSPixels>(), border_radii_data, resolved_box_shadow_data);
Painting::paint_box_shadow(context, absolute_fragment_rect, border_radii_data, resolved_box_shadow_data);
}
return IterationDecision::Continue;
@ -121,7 +121,7 @@ void InlinePaintable::paint(PaintContext& context, Painting::PaintPhase phase) c
// would be none. Once we implement non-rectangular outlines for the `outline` CSS
// property, we can use that here instead.
for_each_fragment([&](auto const& fragment, bool, bool) {
painter.draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Magenta);
painter.draw_rect(context.enclosing_device_rect(fragment.absolute_rect()).template to_type<int>(), Color::Magenta);
return IterationDecision::Continue;
});
}

View file

@ -51,7 +51,7 @@ LabelablePaintable::DispatchEventOfSameName LabelablePaintable::handle_mouseup(B
if (!m_tracking_mouse || button != GUI::MouseButton::Primary || !layout_box().dom_node().enabled())
return DispatchEventOfSameName::No;
bool is_inside_node_or_label = absolute_rect().to_type<CSSPixels>().contains(position);
bool is_inside_node_or_label = absolute_rect().contains(position);
if (!is_inside_node_or_label)
is_inside_node_or_label = Layout::Label::is_inside_associated_label(layout_box(), position);
@ -66,7 +66,7 @@ LabelablePaintable::DispatchEventOfSameName LabelablePaintable::handle_mousemove
if (!m_tracking_mouse || !layout_box().dom_node().enabled())
return DispatchEventOfSameName::No;
bool is_inside_node_or_label = absolute_rect().to_type<CSSPixels>().contains(position);
bool is_inside_node_or_label = absolute_rect().contains(position);
if (!is_inside_node_or_label)
is_inside_node_or_label = Layout::Label::is_inside_associated_label(layout_box(), position);

View file

@ -31,7 +31,8 @@ void MarkerPaintable::paint(PaintContext& context, PaintPhase phase) const
if (phase != PaintPhase::Foreground)
return;
CSSPixelRect enclosing = absolute_rect().to_rounded<float>().to_type<CSSPixels>();
// FIXME: All this does is round to the nearest whole CSS pixel, but it's goofy.
CSSPixelRect enclosing = absolute_rect().to_type<float>().to_rounded<float>().to_type<CSSPixels>();
auto device_enclosing = context.enclosing_device_rect(enclosing);
CSSPixels marker_width = enclosing.height() / 2.0f;

View file

@ -36,7 +36,8 @@ void NestedBrowsingContextPaintable::paint(PaintContext& context, PaintPhase pha
PaintableBox::paint(context, phase);
if (phase == PaintPhase::Foreground) {
auto clip_rect = context.rounded_device_rect(absolute_rect().to_type<CSSPixels>());
auto absolute_rect = this->absolute_rect();
auto clip_rect = context.rounded_device_rect(absolute_rect);
ScopedCornerRadiusClip corner_clip { context, context.painter(), clip_rect, normalized_border_radii_data(ShrinkRadiiForBorders::Yes) };
auto* hosted_document = layout_box().dom_node().content_document_without_origin_check();
@ -50,7 +51,7 @@ void NestedBrowsingContextPaintable::paint(PaintContext& context, PaintPhase pha
auto old_viewport_rect = context.device_viewport_rect();
context.painter().add_clip_rect(clip_rect.to_type<int>());
context.painter().translate(absolute_x(), absolute_y());
context.painter().translate(absolute_rect.x().value(), absolute_rect.y().value());
context.set_device_viewport_rect({ {}, layout_box().dom_node().nested_browsing_context()->size() });
const_cast<Layout::InitialContainingBlock*>(hosted_layout_tree)->paint_all_phases(context);
@ -60,7 +61,7 @@ void NestedBrowsingContextPaintable::paint(PaintContext& context, PaintPhase pha
if constexpr (HIGHLIGHT_FOCUSED_FRAME_DEBUG) {
if (layout_box().dom_node().nested_browsing_context()->is_focused_context()) {
context.painter().draw_rect(absolute_rect().to_type<int>(), Color::Cyan);
context.painter().draw_rect(clip_rect.to_type<int>(), Color::Cyan);
}
}
}

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -39,7 +40,8 @@ void PaintableBox::invalidate_stacking_context()
bool PaintableBox::is_out_of_view(PaintContext& context) const
{
return !enclosing_int_rect(absolute_paint_rect())
return !context.enclosing_device_rect(absolute_paint_rect())
.to_type<int>()
.translated(context.painter().translation())
.intersects(context.painter().clip_rect());
}
@ -53,23 +55,23 @@ PaintableWithLines::~PaintableWithLines()
{
}
void PaintableBox::set_offset(Gfx::FloatPoint offset)
void PaintableBox::set_offset(CSSPixelPoint offset)
{
m_offset = offset;
// FIXME: This const_cast is gross.
const_cast<Layout::Box&>(layout_box()).did_set_rect();
}
void PaintableBox::set_content_size(Gfx::FloatSize size)
void PaintableBox::set_content_size(CSSPixelSize size)
{
m_content_size = size;
// FIXME: This const_cast is gross.
const_cast<Layout::Box&>(layout_box()).did_set_rect();
}
Gfx::FloatPoint PaintableBox::effective_offset() const
CSSPixelPoint PaintableBox::effective_offset() const
{
Gfx::FloatPoint offset;
CSSPixelPoint offset;
if (m_containing_line_box_fragment.has_value()) {
// FIXME: This is a hack to deal with situations where the layout tree has been garbage collected.
@ -89,22 +91,22 @@ Gfx::FloatPoint PaintableBox::effective_offset() const
return offset;
}
Gfx::FloatRect PaintableBox::compute_absolute_rect() const
CSSPixelRect PaintableBox::compute_absolute_rect() const
{
Gfx::FloatRect rect { effective_offset(), content_size() };
CSSPixelRect rect { effective_offset(), content_size() };
for (auto const* block = containing_block(); block && block->paintable(); block = block->paintable()->containing_block())
rect.translate_by(block->paint_box()->effective_offset());
return rect;
}
Gfx::FloatRect PaintableBox::absolute_rect() const
CSSPixelRect PaintableBox::absolute_rect() const
{
if (!m_absolute_rect.has_value())
m_absolute_rect = compute_absolute_rect();
return *m_absolute_rect;
}
Gfx::FloatRect PaintableBox::compute_absolute_paint_rect() const
CSSPixelRect PaintableBox::compute_absolute_paint_rect() const
{
// FIXME: This likely incomplete:
auto rect = absolute_border_box_rect();
@ -112,14 +114,14 @@ Gfx::FloatRect PaintableBox::compute_absolute_paint_rect() const
for (auto const& shadow : resolved_box_shadow_data) {
if (shadow.placement == ShadowPlacement::Inner)
continue;
auto inflate = shadow.spread_distance.value() + shadow.blur_radius.value();
auto shadow_rect = rect.inflated(inflate, inflate, inflate, inflate).translated(shadow.offset_x.value(), shadow.offset_y.value());
auto inflate = shadow.spread_distance + shadow.blur_radius;
auto shadow_rect = rect.inflated(inflate, inflate, inflate, inflate).translated(shadow.offset_x, shadow.offset_y);
rect = rect.united(shadow_rect);
}
return rect;
}
Gfx::FloatRect PaintableBox::absolute_paint_rect() const
CSSPixelRect PaintableBox::absolute_paint_rect() const
{
if (!m_absolute_paint_rect.has_value())
m_absolute_paint_rect = compute_absolute_paint_rect();
@ -156,7 +158,7 @@ void PaintableBox::paint(PaintContext& context, PaintPhase phase) const
if (should_clip_rect) {
context.painter().save();
auto border_box = absolute_border_box_rect();
context.painter().add_clip_rect(context.rounded_device_rect(clip_rect.to_rect().resolved(Paintable::layout_node(), border_box).to_type<CSSPixels>()).to_type<int>());
context.painter().add_clip_rect(context.rounded_device_rect(clip_rect.to_rect().resolved(Paintable::layout_node(), border_box.to_type<float>()).to_type<CSSPixels>()).to_type<int>());
}
paint_backdrop_filter(context);
paint_background(context);
@ -171,7 +173,7 @@ void PaintableBox::paint(PaintContext& context, PaintPhase phase) const
context.painter().restore();
if (phase == PaintPhase::Overlay && layout_box().dom_node() && layout_box().document().inspected_node() == layout_box().dom_node()) {
auto content_rect = absolute_rect().to_type<CSSPixels>();
auto content_rect = absolute_rect();
auto margin_box = box_model().margin_box();
CSSPixelRect margin_rect;
@ -180,8 +182,8 @@ void PaintableBox::paint(PaintContext& context, PaintPhase phase) const
margin_rect.set_y(absolute_y() - margin_box.top);
margin_rect.set_height(content_height() + margin_box.top + margin_box.bottom);
auto border_rect = absolute_border_box_rect().to_type<CSSPixels>();
auto padding_rect = absolute_padding_box_rect().to_type<CSSPixels>();
auto border_rect = absolute_border_box_rect();
auto padding_rect = absolute_padding_box_rect();
auto paint_inspector_rect = [&](CSSPixelRect const& rect, Color color) {
auto device_rect = context.enclosing_device_rect(rect).to_type<int>();
@ -216,7 +218,7 @@ void PaintableBox::paint(PaintContext& context, PaintPhase phase) const
if (phase == PaintPhase::FocusOutline && layout_box().dom_node() && layout_box().dom_node()->is_element() && verify_cast<DOM::Element>(*layout_box().dom_node()).is_focused()) {
// FIXME: Implement this as `outline` using :focus-visible in the default UA stylesheet to make it possible to override/disable.
auto focus_outline_rect = context.enclosing_device_rect(absolute_border_box_rect().to_type<CSSPixels>()).inflated(4, 4);
auto focus_outline_rect = context.enclosing_device_rect(absolute_border_box_rect()).inflated(4, 4);
context.painter().draw_focus_rect(focus_outline_rect.to_type<int>(), context.palette().focus_outline());
}
}
@ -229,14 +231,14 @@ void PaintableBox::paint_border(PaintContext& context) const
.bottom = computed_values().border_bottom(),
.left = computed_values().border_left(),
};
paint_all_borders(context, absolute_border_box_rect().to_type<CSSPixels>(), normalized_border_radii_data(), borders_data);
paint_all_borders(context, absolute_border_box_rect(), normalized_border_radii_data(), borders_data);
}
void PaintableBox::paint_backdrop_filter(PaintContext& context) const
{
auto& backdrop_filter = computed_values().backdrop_filter();
if (!backdrop_filter.is_none())
Painting::apply_backdrop_filter(context, layout_node(), absolute_border_box_rect().to_type<CSSPixels>(), normalized_border_radii_data(), backdrop_filter);
Painting::apply_backdrop_filter(context, layout_node(), absolute_border_box_rect(), normalized_border_radii_data(), backdrop_filter);
}
void PaintableBox::paint_background(PaintContext& context) const
@ -260,13 +262,13 @@ void PaintableBox::paint_background(PaintContext& context) const
background_color = document().background_color(context.palette());
}
} else {
background_rect = absolute_padding_box_rect().to_type<CSSPixels>();
background_rect = absolute_padding_box_rect();
}
// HACK: If the Box has a border, use the bordered_rect to paint the background.
// This way if we have a border-radius there will be no gap between the filling and actual border.
if (computed_values().border_top().width || computed_values().border_right().width || computed_values().border_bottom().width || computed_values().border_left().width)
background_rect = absolute_border_box_rect().to_type<CSSPixels>();
background_rect = absolute_border_box_rect();
Painting::paint_background(context, layout_box(), background_rect, background_color, computed_values().image_rendering(), background_layers, normalized_border_radii_data());
}
@ -297,13 +299,13 @@ void PaintableBox::paint_box_shadow(PaintContext& context) const
auto resolved_box_shadow_data = resolve_box_shadow_data();
if (resolved_box_shadow_data.is_empty())
return;
Painting::paint_box_shadow(context, absolute_border_box_rect().to_type<CSSPixels>(), normalized_border_radii_data(), resolved_box_shadow_data);
Painting::paint_box_shadow(context, absolute_border_box_rect(), normalized_border_radii_data(), resolved_box_shadow_data);
}
BorderRadiiData PaintableBox::normalized_border_radii_data(ShrinkRadiiForBorders shrink) const
{
auto border_radius_data = Painting::normalized_border_radii_data(layout_box(),
absolute_border_box_rect().to_type<CSSPixels>(),
absolute_border_box_rect(),
computed_values().border_top_left_radius(),
computed_values().border_top_right_radius(),
computed_values().border_bottom_right_radius(),
@ -324,9 +326,9 @@ Optional<Gfx::IntRect> PaintableBox::clip_rect() const
if (overflow_x == CSS::Overflow::Hidden && overflow_y == CSS::Overflow::Hidden) {
if (m_clip_rect.has_value()) {
m_clip_rect->intersect(absolute_padding_box_rect().to_rounded<int>());
m_clip_rect->intersect(absolute_padding_box_rect().to_type<float>().to_rounded<int>());
} else {
m_clip_rect = absolute_padding_box_rect().to_rounded<int>();
m_clip_rect = absolute_padding_box_rect().to_type<float>().to_rounded<int>();
}
}
}
@ -407,7 +409,7 @@ static void paint_cursor_if_needed(PaintContext& context, Layout::TextNode const
if (!fragment.layout_node().dom_node() || !fragment.layout_node().dom_node()->is_editable())
return;
auto fragment_rect = fragment.absolute_rect().to_type<CSSPixels>();
auto fragment_rect = fragment.absolute_rect();
CSSPixelRect cursor_rect {
fragment_rect.x() + text_node.font().width(fragment.text().substring_view(0, text_node.browsing_context().cursor_position().offset() - fragment.start())),
@ -424,7 +426,7 @@ static void paint_cursor_if_needed(PaintContext& context, Layout::TextNode const
static void paint_text_decoration(PaintContext& context, Gfx::Painter& painter, Layout::Node const& text_node, Layout::LineBoxFragment const& fragment)
{
auto& font = fragment.layout_node().font();
auto fragment_box = fragment.absolute_rect().to_type<CSSPixels>();
auto fragment_box = fragment.absolute_rect();
CSSPixels glyph_height = font.pixel_size();
auto baseline = fragment_box.height() / 2 - (glyph_height + 4) / 2 + glyph_height;
@ -507,7 +509,7 @@ static void paint_text_fragment(PaintContext& context, Layout::TextNode const& t
auto& painter = context.painter();
if (phase == Painting::PaintPhase::Foreground) {
auto fragment_absolute_rect = fragment.absolute_rect().to_type<CSSPixels>();
auto fragment_absolute_rect = fragment.absolute_rect();
auto fragment_absolute_device_rect = context.enclosing_device_rect(fragment_absolute_rect);
if (text_node.document().inspected_node() == &text_node.dom_node())
@ -521,16 +523,16 @@ static void paint_text_fragment(PaintContext& context, Layout::TextNode const& t
if (text_transform == CSS::TextTransform::Lowercase)
text = Unicode::to_unicode_lowercase_full(text_node.text_for_rendering());
DevicePixelPoint baseline_start { fragment_absolute_device_rect.x(), fragment_absolute_device_rect.y() + fragment.baseline() };
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 selection_rect = fragment.selection_rect(text_node.font());
auto selection_rect = context.enclosing_device_rect(fragment.selection_rect(text_node.font())).to_type<int>();
if (!selection_rect.is_empty()) {
painter.fill_rect(enclosing_int_rect(selection_rect), context.palette().selection());
painter.fill_rect(selection_rect, context.palette().selection());
Gfx::PainterStateSaver saver(painter);
painter.add_clip_rect(enclosing_int_rect(selection_rect));
painter.add_clip_rect(selection_rect);
painter.draw_text_run(baseline_start.to_type<int>(), view, fragment.layout_node().font(), context.palette().selection_text());
}
@ -555,7 +557,7 @@ void PaintableWithLines::paint(PaintContext& context, PaintPhase phase) const
if (should_clip_overflow) {
context.painter().save();
// FIXME: Handle overflow-x and overflow-y being different values.
auto clip_box = context.rounded_device_rect(absolute_padding_box_rect().to_type<CSSPixels>());
auto clip_box = context.rounded_device_rect(absolute_padding_box_rect());
context.painter().add_clip_rect(clip_box.to_type<int>());
auto scroll_offset = static_cast<Layout::BlockContainer const&>(layout_box()).scroll_offset();
context.painter().translate(-scroll_offset.to_type<int>());
@ -603,10 +605,10 @@ void PaintableWithLines::paint(PaintContext& context, PaintPhase phase) const
for (auto& fragment : line_box.fragments()) {
if (context.should_show_line_box_borders()) {
auto fragment_absolute_rect = fragment.absolute_rect();
context.painter().draw_rect(enclosing_int_rect(fragment_absolute_rect), Color::Green);
context.painter().draw_rect(context.enclosing_device_rect(fragment_absolute_rect).to_type<int>(), Color::Green);
context.painter().draw_line(
fragment_absolute_rect.top_left().translated(0, fragment.baseline()).to_rounded<int>(),
fragment_absolute_rect.top_right().translated(0, fragment.baseline()).to_rounded<int>(), Color::Red);
context.rounded_device_point(fragment_absolute_rect.top_left().translated(0, fragment.baseline())).to_type<int>(),
context.rounded_device_point(fragment_absolute_rect.top_right().translated(0, fragment.baseline())).to_type<int>(), Color::Red);
}
if (is<Layout::TextNode>(fragment.layout_node()))
paint_text_fragment(context, static_cast<Layout::TextNode const&>(fragment.layout_node()), fragment, phase);
@ -631,7 +633,7 @@ void PaintableWithLines::paint(PaintContext& context, PaintPhase phase) const
continue;
if (parent->is_focused()) {
// FIXME: Implement this as `outline` using :focus-visible in the default UA stylesheet to make it possible to override/disable.
auto focus_outline_rect = enclosing_int_rect(fragment.absolute_rect()).inflated(4, 4);
auto focus_outline_rect = context.enclosing_device_rect(fragment.absolute_rect()).to_type<int>().inflated(4, 4);
context.painter().draw_focus_rect(focus_outline_rect, context.palette().focus_outline());
}
}
@ -674,7 +676,7 @@ Optional<HitTestResult> PaintableBox::hit_test(CSSPixelPoint position, HitTestTy
return stacking_context()->hit_test(position, type);
}
if (!absolute_border_box_rect().contains(position.x().value(), position.y().value()))
if (!absolute_border_box_rect().contains(position.x(), position.y()))
return {};
for (auto* child = first_child(); child; child = child->next_sibling()) {
@ -702,11 +704,11 @@ Optional<HitTestResult> PaintableWithLines::hit_test(CSSPixelPoint position, Hit
dbgln("FIXME: PaintableWithLines::hit_test(): Missing containing block on {}", fragment.layout_node().debug_description());
continue;
}
auto fragment_absolute_rect = fragment.absolute_rect().to_type<CSSPixels>();
auto fragment_absolute_rect = fragment.absolute_rect();
if (fragment_absolute_rect.contains(position)) {
if (is<Layout::BlockContainer>(fragment.layout_node()) && fragment.layout_node().paintable())
return fragment.layout_node().paintable()->hit_test(position, type);
return HitTestResult { *fragment.layout_node().paintable(), fragment.text_index_at(position.x().value()) };
return HitTestResult { *fragment.layout_node().paintable(), fragment.text_index_at(position.x()) };
}
// If we reached this point, the position is not within the fragment. However, the fragment start or end might be the place to place the cursor.
@ -730,7 +732,7 @@ Optional<HitTestResult> PaintableWithLines::hit_test(CSSPixelPoint position, Hit
if (type == HitTestType::TextCursor && last_good_candidate.has_value())
return last_good_candidate;
if (is_visible() && absolute_border_box_rect().contains(position.x().value(), position.y().value()))
if (is_visible() && absolute_border_box_rect().contains(position.x(), position.y()))
return HitTestResult { *this };
return {};
}

View file

@ -28,29 +28,29 @@ public:
auto const& box_model() const { return layout_box().box_model(); }
struct OverflowData {
Gfx::FloatRect scrollable_overflow_rect;
Gfx::FloatPoint scroll_offset;
CSSPixelRect scrollable_overflow_rect;
CSSPixelPoint scroll_offset;
};
Gfx::FloatRect absolute_rect() const;
Gfx::FloatPoint effective_offset() const;
CSSPixelRect absolute_rect() const;
CSSPixelPoint effective_offset() const;
void set_offset(Gfx::FloatPoint);
void set_offset(CSSPixelPoint);
void set_offset(float x, float y) { set_offset({ x, y }); }
Gfx::FloatSize content_size() const { return m_content_size; }
void set_content_size(Gfx::FloatSize);
void set_content_size(float width, float height) { set_content_size({ width, height }); }
CSSPixelSize const& content_size() const { return m_content_size; }
void set_content_size(CSSPixelSize);
void set_content_size(CSSPixels width, CSSPixels height) { set_content_size({ width, height }); }
void set_content_width(float width) { set_content_size(width, content_height()); }
void set_content_height(float height) { set_content_size(content_width(), height); }
float content_width() const { return m_content_size.width(); }
float content_height() const { return m_content_size.height(); }
void set_content_width(CSSPixels width) { set_content_size(width, content_height()); }
void set_content_height(CSSPixels height) { set_content_size(content_width(), height); }
CSSPixels content_width() const { return m_content_size.width(); }
CSSPixels content_height() const { return m_content_size.height(); }
Gfx::FloatRect absolute_padding_box_rect() const
CSSPixelRect absolute_padding_box_rect() const
{
auto absolute_rect = this->absolute_rect();
Gfx::FloatRect rect;
CSSPixelRect rect;
rect.set_x(absolute_rect.x() - box_model().padding.left);
rect.set_width(content_width() + box_model().padding.left + box_model().padding.right);
rect.set_y(absolute_rect.y() - box_model().padding.top);
@ -58,10 +58,10 @@ public:
return rect;
}
Gfx::FloatRect absolute_border_box_rect() const
CSSPixelRect absolute_border_box_rect() const
{
auto padded_rect = this->absolute_padding_box_rect();
Gfx::FloatRect rect;
CSSPixelRect rect;
rect.set_x(padded_rect.x() - box_model().border.left);
rect.set_width(padded_rect.width() + box_model().border.left + box_model().border.right);
rect.set_y(padded_rect.y() - box_model().border.top);
@ -69,27 +69,27 @@ public:
return rect;
}
Gfx::FloatRect absolute_paint_rect() const;
CSSPixelRect absolute_paint_rect() const;
float border_box_width() const
CSSPixels border_box_width() const
{
auto border_box = box_model().border_box();
return content_width() + border_box.left + border_box.right;
}
float border_box_height() const
CSSPixels border_box_height() const
{
auto border_box = box_model().border_box();
return content_height() + border_box.top + border_box.bottom;
}
float absolute_x() const { return absolute_rect().x(); }
float absolute_y() const { return absolute_rect().y(); }
Gfx::FloatPoint absolute_position() const { return absolute_rect().location(); }
CSSPixels absolute_x() const { return absolute_rect().x(); }
CSSPixels absolute_y() const { return absolute_rect().y(); }
CSSPixelPoint absolute_position() const { return absolute_rect().location(); }
bool has_overflow() const { return m_overflow_data.has_value(); }
Optional<Gfx::FloatRect> scrollable_overflow_rect() const
Optional<CSSPixelRect> scrollable_overflow_rect() const
{
if (!m_overflow_data.has_value())
return {};
@ -129,8 +129,8 @@ protected:
virtual void paint_background(PaintContext&) const;
virtual void paint_box_shadow(PaintContext&) const;
virtual Gfx::FloatRect compute_absolute_rect() const;
virtual Gfx::FloatRect compute_absolute_paint_rect() const;
virtual CSSPixelRect compute_absolute_rect() const;
virtual CSSPixelRect compute_absolute_paint_rect() const;
enum class ShrinkRadiiForBorders {
Yes,
@ -144,16 +144,16 @@ protected:
private:
Optional<OverflowData> m_overflow_data;
Gfx::FloatPoint m_offset;
Gfx::FloatSize m_content_size;
CSSPixelPoint m_offset;
CSSPixelSize m_content_size;
// Some boxes hang off of line box fragments. (inline-block, inline-table, replaced, etc)
Optional<Layout::LineBoxFragmentCoordinate> m_containing_line_box_fragment;
OwnPtr<Painting::StackingContext> m_stacking_context;
Optional<Gfx::FloatRect> mutable m_absolute_rect;
Optional<Gfx::FloatRect> mutable m_absolute_paint_rect;
Optional<CSSPixelRect> mutable m_absolute_rect;
Optional<CSSPixelRect> mutable m_absolute_paint_rect;
Optional<Gfx::IntRect> mutable m_clip_rect;

View file

@ -30,10 +30,13 @@ void ProgressPaintable::paint(PaintContext& context, PaintPhase phase) const
return;
if (phase == PaintPhase::Foreground) {
auto progress_rect = absolute_rect().to_rounded<int>();
auto frame_thickness = min(min(progress_rect.width(), progress_rect.height()) / 6, 3);
Gfx::StylePainter::paint_progressbar(context.painter(), progress_rect.shrunken(frame_thickness, frame_thickness), context.palette(), 0, round_to<int>(layout_box().dom_node().max()), round_to<int>(layout_box().dom_node().value()), ""sv);
Gfx::StylePainter::paint_frame(context.painter(), progress_rect, context.palette(), Gfx::FrameShape::Box, Gfx::FrameShadow::Raised, frame_thickness);
auto progress_rect = context.rounded_device_rect(absolute_rect());
auto min_frame_thickness = context.rounded_device_pixels(3);
auto frame_thickness = min(min(progress_rect.width(), progress_rect.height()) / 6, min_frame_thickness);
Gfx::StylePainter::paint_progressbar(context.painter(), progress_rect.shrunken(frame_thickness, frame_thickness).to_type<int>(), context.palette(), 0, round_to<int>(layout_box().dom_node().max()), round_to<int>(layout_box().dom_node().value()), ""sv);
Gfx::StylePainter::paint_frame(context.painter(), progress_rect.to_type<int>(), context.palette(), Gfx::FrameShape::Box, Gfx::FrameShadow::Raised, frame_thickness.value());
}
}

View file

@ -34,7 +34,7 @@ void RadioButtonPaintable::paint(PaintContext& context, PaintPhase phase) const
auto const& radio_box = static_cast<HTML::HTMLInputElement const&>(layout_box().dom_node());
if (phase == PaintPhase::Foreground)
Gfx::StylePainter::paint_radio_button(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), radio_box.checked(), being_pressed());
Gfx::StylePainter::paint_radio_button(context.painter(), context.enclosing_device_rect(absolute_rect()).to_type<int>(), context.palette(), radio_box.checked(), being_pressed());
}
}

View file

@ -47,7 +47,7 @@ void SVGGeometryPaintable::paint(PaintContext& context, PaintPhase phase) const
auto const* svg_element = geometry_element.first_ancestor_of_type<SVG::SVGSVGElement>();
auto maybe_view_box = svg_element->view_box();
context.painter().add_clip_rect(enclosing_int_rect(absolute_rect()));
context.painter().add_clip_rect(context.enclosing_device_rect(absolute_rect()).to_type<int>());
Gfx::Path path = const_cast<SVG::SVGGeometryElement&>(geometry_element).get_path();

View file

@ -36,10 +36,10 @@ void SVGPaintable::after_children_paint(PaintContext& context, PaintPhase phase)
context.svg_context().restore();
}
Gfx::FloatRect SVGPaintable::compute_absolute_rect() const
CSSPixelRect SVGPaintable::compute_absolute_rect() const
{
if (auto* svg_svg_box = layout_box().first_ancestor_of_type<Layout::SVGSVGBox>()) {
Gfx::FloatRect rect { effective_offset(), content_size() };
CSSPixelRect rect { effective_offset(), content_size() };
for (Layout::Box const* ancestor = svg_svg_box; ancestor && ancestor->paintable(); ancestor = ancestor->paintable()->containing_block())
rect.translate_by(ancestor->paint_box()->effective_offset());
return rect;

View file

@ -21,7 +21,7 @@ public:
protected:
SVGPaintable(Layout::SVGBox const&);
virtual Gfx::FloatRect compute_absolute_rect() const override;
virtual CSSPixelRect compute_absolute_rect() const override;
};
}

View file

@ -30,7 +30,7 @@ void SVGSVGPaintable::before_children_paint(PaintContext& context, PaintPhase ph
return;
if (!context.has_svg_context())
context.set_svg_context(SVGContext(absolute_rect()));
context.set_svg_context(SVGContext(absolute_rect().to_type<float>()));
PaintableBox::before_children_paint(context, phase);
}

View file

@ -371,14 +371,14 @@ void paint_text_shadow(PaintContext& context, Layout::LineBoxFragment const& fra
Gfx::Painter shadow_painter { *shadow_bitmap };
shadow_painter.set_font(context.painter().font());
// FIXME: "Spread" the shadow somehow.
DevicePixelPoint baseline_start(text_rect.x(), text_rect.y() + fragment.baseline());
DevicePixelPoint baseline_start(text_rect.x(), text_rect.y() + context.rounded_device_pixels(fragment.baseline()));
shadow_painter.draw_text_run(baseline_start.to_type<int>(), Utf8View(fragment.text()), context.painter().font(), layer.color);
// Blur
Gfx::StackBlurFilter filter(*shadow_bitmap);
filter.process_rgba(blur_radius.value(), layer.color);
auto draw_rect = context.enclosing_device_rect(fragment.absolute_rect().to_type<CSSPixels>());
auto draw_rect = context.enclosing_device_rect(fragment.absolute_rect());
DevicePixelPoint draw_location {
draw_rect.x() + offset_x - margin,
draw_rect.y() + offset_y - margin

View file

@ -357,7 +357,7 @@ void StackingContext::paint(PaintContext& context) const
if (opacity < 1.0f || !affine_transform.is_identity_or_translation()) {
auto transform_origin = this->transform_origin();
auto source_rect = paintable().absolute_paint_rect().translated(-transform_origin);
auto source_rect = paintable().absolute_paint_rect().to_type<float>().translated(-transform_origin);
auto transformed_destination_rect = affine_transform.map(source_rect).translated(transform_origin);
auto destination_rect = transformed_destination_rect.to_rounded<int>();
@ -366,12 +366,12 @@ void StackingContext::paint(PaintContext& context) const
// to the size of the source (which could add some artefacts, though just scaling the bitmap already does that).
// We need to copy the background at the destination because a bunch of our rendering effects now rely on
// being able to sample the painter (see border radii, shadows, filters, etc).
Gfx::FloatPoint destination_clipped_fixup {};
CSSPixelPoint destination_clipped_fixup {};
auto try_get_scaled_destination_bitmap = [&]() -> ErrorOr<NonnullRefPtr<Gfx::Bitmap>> {
Gfx::IntRect actual_destination_rect;
auto bitmap = TRY(context.painter().get_region_bitmap(destination_rect, Gfx::BitmapFormat::BGRA8888, actual_destination_rect));
// get_region_bitmap() may clip to a smaller region if the requested rect goes outside the painter, so we need to account for that.
destination_clipped_fixup = Gfx::FloatPoint { destination_rect.location() - actual_destination_rect.location() };
destination_clipped_fixup = CSSPixelPoint { destination_rect.location() - actual_destination_rect.location() };
destination_rect = actual_destination_rect;
if (source_rect.size() != transformed_destination_rect.size()) {
auto sx = static_cast<float>(source_rect.width()) / transformed_destination_rect.width();
@ -387,7 +387,7 @@ void StackingContext::paint(PaintContext& context) const
return;
auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
Gfx::Painter painter(bitmap);
painter.translate((-paintable().absolute_paint_rect().location() + destination_clipped_fixup).to_rounded<int>());
painter.translate(context.rounded_device_point(-paintable().absolute_paint_rect().location() + destination_clipped_fixup).to_type<int>());
auto paint_context = context.clone(painter);
paint_internal(paint_context);