1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:27:43 +00:00

LibWeb: Resolve Lengths to CSSPixels

This commit is contained in:
Sam Atkins 2022-11-08 17:29:52 +00:00 committed by Linus Groh
parent 7d40e3eb0d
commit 8cc0bdf777
13 changed files with 44 additions and 44 deletions

View file

@ -15,7 +15,6 @@
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/HTMLHtmlElement.h>
#include <LibWeb/PixelUnits.h>
namespace Web::CSS {
@ -71,7 +70,7 @@ Length Length::resolved(Layout::Node const& layout_node) const
return *this;
}
float Length::relative_length_to_px(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float root_font_size) const
CSSPixels Length::relative_length_to_px(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size) const
{
switch (m_type) {
case Type::Ex:
@ -96,7 +95,7 @@ float Length::relative_length_to_px(Gfx::IntRect const& viewport_rect, Gfx::Font
}
}
float Length::to_px(Layout::Node const& layout_node) const
CSSPixels Length::to_px(Layout::Node const& layout_node) const
{
if (is_calculated())
return m_calculated_style->resolve_length(layout_node)->to_px(layout_node);

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -80,9 +81,9 @@ public:
float raw_value() const { return m_value; }
NonnullRefPtr<CalculatedStyleValue> calculated_style_value() const;
float to_px(Layout::Node const&) const;
CSSPixels to_px(Layout::Node const&) const;
ALWAYS_INLINE float to_px(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float root_font_size) const
ALWAYS_INLINE CSSPixels to_px(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size) const
{
if (is_auto())
return 0;
@ -93,7 +94,7 @@ public:
return absolute_length_to_px();
}
ALWAYS_INLINE float absolute_length_to_px() const
ALWAYS_INLINE CSSPixels absolute_length_to_px() const
{
constexpr float inch_pixels = 96.0f;
constexpr float centimeter_pixels = (inch_pixels / 2.54f);
@ -123,7 +124,7 @@ public:
// this file already. To break the cyclic dependency, we must move all method definitions out.
bool operator==(Length const& other) const;
float relative_length_to_px(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float root_font_size) const;
CSSPixels relative_length_to_px(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size) const;
private:
char const* unit_name() const;

View file

@ -156,8 +156,8 @@ bool MediaFeature::compare(HTML::Window const& window, MediaFeatureValue left, C
}
if (left.is_length()) {
float left_px;
float right_px;
CSSPixels left_px;
CSSPixels right_px;
// Save ourselves some work if neither side is a relative length.
if (left.length().is_absolute() && right.length().is_absolute()) {
left_px = left.length().absolute_length_to_px();

View file

@ -947,7 +947,7 @@ void StyleComputer::compute_defaulted_values(StyleProperties& style, DOM::Elemen
}
}
float StyleComputer::root_element_font_size() const
CSSPixels StyleComputer::root_element_font_size() const
{
constexpr float default_root_element_font_size = 16;
@ -1048,7 +1048,7 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
font_size_in_px *= multiplier;
} else {
float root_font_size = root_element_font_size();
auto root_font_size = root_element_font_size();
Gfx::FontPixelMetrics font_metrics;
if (parent_element && parent_element->computed_css_values())
@ -1056,7 +1056,7 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
else
font_metrics = Platform::FontPlugin::the().default_font().pixel_metrics();
auto parent_font_size = [&]() -> float {
auto parent_font_size = [&]() -> CSSPixels {
if (!parent_element || !parent_element->computed_css_values())
return font_size_in_px;
auto value = parent_element->computed_css_values()->property(CSS::PropertyID::FontSize);
@ -1083,7 +1083,7 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
// FIXME: Support font-size: calc(...)
// Theoretically we can do this now, but to resolve it we need a layout_node which we might not have. :^(
if (!maybe_length->is_calculated()) {
auto px = maybe_length.value().to_px(viewport_rect(), font_metrics, parent_font_size(), root_font_size);
auto px = maybe_length.value().to_px(viewport_rect(), font_metrics, parent_font_size(), root_font_size).value();
if (px != 0)
font_size_in_px = px;
}
@ -1207,14 +1207,14 @@ Gfx::Font const& StyleComputer::initial_font() const
void StyleComputer::absolutize_values(StyleProperties& style, DOM::Element const*, Optional<CSS::Selector::PseudoElement>) const
{
auto font_metrics = style.computed_font().pixel_metrics();
float root_font_size = root_element_font_size();
float font_size = style.property(CSS::PropertyID::FontSize)->to_length().to_px(viewport_rect(), font_metrics, root_font_size, root_font_size);
auto root_font_size = root_element_font_size();
auto font_size = style.property(CSS::PropertyID::FontSize)->to_length().to_px(viewport_rect(), font_metrics, root_font_size, root_font_size);
for (size_t i = 0; i < style.m_property_values.size(); ++i) {
auto& value_slot = style.m_property_values[i];
if (!value_slot)
continue;
value_slot = value_slot->absolutized(viewport_rect(), font_metrics, font_size, root_font_size);
value_slot = value_slot->absolutized(viewport_rect(), font_metrics, font_size.value(), root_font_size.value());
}
}

View file

@ -94,7 +94,7 @@ private:
void for_each_stylesheet(CascadeOrigin, Callback) const;
Gfx::IntRect viewport_rect() const;
float root_element_font_size() const;
CSSPixels root_element_font_size() const;
struct MatchingRuleSet {
Vector<MatchingRule> user_agent_rules;

View file

@ -893,10 +893,10 @@ Gfx::FloatRect EdgeRect::resolved(Layout::Node const& layout_node, Gfx::FloatRec
// widths for <bottom>, and the same as the used value of the width plus the sum of the
// horizontal padding and border widths for <right>, such that four 'auto' values result in the
// clipping region being the same as the element's border box).
auto left = border_box.left() + (left_edge.is_auto() ? 0 : left_edge.to_px(layout_node));
auto top = border_box.top() + (top_edge.is_auto() ? 0 : top_edge.to_px(layout_node));
auto right = border_box.left() + (right_edge.is_auto() ? border_box.width() : right_edge.to_px(layout_node));
auto bottom = border_box.top() + (bottom_edge.is_auto() ? border_box.height() : bottom_edge.to_px(layout_node));
auto left = border_box.left() + (left_edge.is_auto() ? 0 : left_edge.to_px(layout_node)).value();
auto top = border_box.top() + (top_edge.is_auto() ? 0 : top_edge.to_px(layout_node)).value();
auto right = border_box.left() + (right_edge.is_auto() ? border_box.width() : right_edge.to_px(layout_node)).value();
auto bottom = border_box.top() + (bottom_edge.is_auto() ? border_box.height() : bottom_edge.to_px(layout_node)).value();
return Gfx::FloatRect {
left,
top,
@ -1187,7 +1187,7 @@ float Filter::Blur::resolved_radius(Layout::Node const& node) const
// Default value when omitted is 0px.
auto sigma = 0;
if (radius.has_value())
sigma = radius->resolved(node).to_px(node);
sigma = radius->resolved(node).to_px(node).value();
// Note: The radius/sigma of the blur needs to be doubled for LibGfx's blur functions.
return sigma * 2;
}
@ -1197,9 +1197,9 @@ Filter::DropShadow::Resolved Filter::DropShadow::resolved(Layout::Node const& no
// The default value for omitted values is missing length values set to 0
// and the missing used color is taken from the color property.
return Resolved {
offset_x.resolved(node).to_px(node),
offset_y.resolved(node).to_px(node),
radius.has_value() ? radius->resolved(node).to_px(node) : 0.0f,
offset_x.resolved(node).to_px(node).value(),
offset_y.resolved(node).to_px(node).value(),
radius.has_value() ? radius->resolved(node).to_px(node).value() : 0.0f,
color.has_value() ? *color : node.computed_values().color()
};
}