1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:17:44 +00:00

LibWeb: Start fleshing out support for relative CSS units

This patch introduces support for more than just "absolute px" units in
our Length class. It now also supports "em" and "rem", which are units
relative to the font-size of the current layout node and the <html>
element's layout node respectively.
This commit is contained in:
Andreas Kling 2020-06-07 17:55:46 +02:00
parent d93bf78346
commit 731685468a
16 changed files with 163 additions and 92 deletions

View file

@ -0,0 +1,64 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibWeb/CSS/Length.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/HTMLHtmlElement.h>
namespace Web {
float Length::to_px(const LayoutNode& layout_node) const
{
switch (m_type) {
case Type::Auto:
return 0;
case Type::Px:
return m_value;
case Type::Em:
return m_value * layout_node.font_size();
case Type::Rem:
return m_value * layout_node.document().document_element()->layout_node()->font_size();
default:
ASSERT_NOT_REACHED();
}
}
const char* Length::unit_name() const
{
switch (m_type) {
case Type::Px:
return "px";
case Type::Em:
return "em";
case Type::Rem:
return "rem";
case Type::Auto:
return "auto";
}
ASSERT_NOT_REACHED();
}
}

View file

@ -27,6 +27,7 @@
#pragma once
#include <AK/String.h>
#include <LibWeb/Forward.h>
namespace Web {
@ -34,10 +35,12 @@ class Length {
public:
enum class Type {
Auto,
Absolute,
Px,
Em,
Rem,
};
Length() {}
Length() { }
Length(int value, Type type)
: m_type(type)
, m_value(value)
@ -48,28 +51,24 @@ public:
, m_value(value)
{
}
~Length() {}
bool is_auto() const { return m_type == Type::Auto; }
bool is_absolute() const { return m_type == Type::Absolute; }
bool is_absolute() const { return m_type == Type::Px; }
bool is_relative() const { return m_type == Type::Em || m_type == Type::Rem; }
float value() const { return m_value; }
float raw_value() const { return m_value; }
float to_px(const LayoutNode&) const;
String to_string() const
{
if (is_auto())
return "[Length/auto]";
return String::format("%g [Length/px]", m_value);
}
float to_px() const
{
if (is_auto())
return 0;
return m_value;
return "[auto]";
return String::format("[%g %s]", m_value, unit_name());
}
private:
const char* unit_name() const;
Type m_type { Type::Auto };
float m_value { 0 };
};

View file

@ -172,11 +172,11 @@ void StyleProperties::load_font() const
return;
}
float StyleProperties::line_height() const
float StyleProperties::line_height(const LayoutNode& layout_node) const
{
auto line_height_length = length_or_fallback(CSS::PropertyID::LineHeight, {});
if (line_height_length.is_absolute())
return (float)line_height_length.to_px();
return (float)line_height_length.to_px(layout_node);
return (float)font().glyph_height() * 1.4f;
}

View file

@ -66,7 +66,7 @@ public:
return *m_font;
}
float line_height() const;
float line_height(const LayoutNode&) const;
bool operator==(const StyleProperties&) const;
bool operator!=(const StyleProperties& other) const { return !(*this == other); }

View file

@ -190,7 +190,7 @@ static void set_property_expanding_shorthands(StyleProperties& style, CSS::Prope
if (auto value = parse_line_style(parts[0])) {
set_property_border_style(style, value.release_nonnull());
set_property_border_color(style, ColorStyleValue::create(Gfx::Color::Black));
set_property_border_width(style, LengthStyleValue::create(Length(3, Length::Type::Absolute)));
set_property_border_width(style, LengthStyleValue::create(Length(3, Length::Type::Px)));
return;
}
}

View file

@ -212,7 +212,7 @@ public:
virtual String to_string() const override { return String::format("%g%%", m_percentage); }
Length to_length(float reference) const { return Length((m_percentage / 100.0f) * reference, Length::Type::Absolute); }
Length to_length(float reference) const { return Length((m_percentage / 100.0f) * reference, Length::Type::Px); }
private:
virtual Length to_length() const override { return {}; }