1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:47:34 +00:00

LibWeb: Start absolutizing lengths after performing the CSS cascade

Once we've performed the cascade on a set of values for an element,
we should have enough information to resolve/absolutize some lengths.

Basically, any CSS length that isn't "auto" or a percentage can be
turned into an absolute length (in pixels) as long as we have the
following information:

- The viewport rect
- The parent element's font
- The document element's font
- The element's own font

To ensure that we can absolutize lengths relative to the element's own
font, we now do a separate first pass where font-related properties are
defaulted (in the cascade spec sense of the word) and become usable.

There's a lot more work to do here, but this should open up a lot of
simplification in layout code, since it will no longer need to care
about relative lengths. Layout still needs to resolve percentages, since
we can't do that for some properties until the containing block
dimensions are known.
This commit is contained in:
Andreas Kling 2021-09-23 13:13:51 +02:00
parent 30979c1956
commit 23a08fd35a
6 changed files with 305 additions and 51 deletions

View file

@ -7,6 +7,7 @@
#pragma once
#include <AK/String.h>
#include <LibGfx/Forward.h>
#include <LibWeb/Forward.h>
namespace Web::CSS {
@ -103,10 +104,13 @@ public:
}
float raw_value() const { return m_value; }
ALWAYS_INLINE float to_px(const Layout::Node& layout_node) const
float to_px(Layout::Node const&) const;
ALWAYS_INLINE float to_px(Gfx::IntRect const& viewport_rect, Gfx::FontMetrics const& font_metrics, float root_font_size) const
{
if (is_relative())
return relative_length_to_px(layout_node);
return relative_length_to_px(viewport_rect, font_metrics, root_font_size);
constexpr float inch_pixels = 96.0f;
constexpr float centimeter_pixels = (inch_pixels / 2.54f);
switch (m_type) {
@ -153,6 +157,8 @@ public:
void set_calculated_style(CalculatedStyleValue* value) { m_calculated_style = value; }
float relative_length_to_px(Gfx::IntRect const& viewport_rect, Gfx::FontMetrics const& font_metrics, float root_font_size) const;
private:
float relative_length_to_px(const Layout::Node&) const;
float resolve_calculated_value(const Layout::Node& layout_node, float reference_for_percent) const;