2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-07-01 12:17:32 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-09-06 10:34:26 -03:00
|
|
|
#include <AK/String.h>
|
2021-09-23 08:13:51 -03:00
|
|
|
#include <LibGfx/Forward.h>
|
2020-06-07 12:55:46 -03:00
|
|
|
#include <LibWeb/Forward.h>
|
2019-07-03 02:55:22 -03:00
|
|
|
|
2020-07-26 15:01:35 -03:00
|
|
|
namespace Web::CSS {
|
2020-03-07 06:27:02 -03:00
|
|
|
|
2019-07-01 12:17:32 -03:00
|
|
|
class Length {
|
|
|
|
|
public:
|
|
|
|
|
enum class Type {
|
2021-06-11 19:03:15 -03:00
|
|
|
Calculated,
|
2019-07-01 12:17:32 -03:00
|
|
|
Auto,
|
2020-09-29 15:06:58 -03:00
|
|
|
Cm,
|
|
|
|
|
In,
|
|
|
|
|
Mm,
|
|
|
|
|
Q,
|
2020-06-07 12:55:46 -03:00
|
|
|
Px,
|
2020-06-28 10:25:32 -03:00
|
|
|
Pt,
|
2020-10-05 12:18:07 -03:00
|
|
|
Pc,
|
2020-08-07 15:30:27 -03:00
|
|
|
Ex,
|
2020-06-07 12:55:46 -03:00
|
|
|
Em,
|
2021-08-05 11:31:15 -03:00
|
|
|
Ch,
|
2020-06-07 12:55:46 -03:00
|
|
|
Rem,
|
2020-09-08 15:39:09 -03:00
|
|
|
Vh,
|
|
|
|
|
Vw,
|
|
|
|
|
Vmax,
|
|
|
|
|
Vmin,
|
2019-07-01 12:17:32 -03:00
|
|
|
};
|
|
|
|
|
|
2022-02-22 09:10:48 -03:00
|
|
|
static Optional<Type> unit_from_name(StringView);
|
|
|
|
|
|
2021-09-30 17:57:35 -03:00
|
|
|
// We have a RefPtr<CalculatedStyleValue> member, but can't include the header StyleValue.h as it includes
|
|
|
|
|
// this file already. To break the cyclic dependency, we must move all method definitions out.
|
|
|
|
|
Length(int value, Type type);
|
|
|
|
|
Length(float value, Type type);
|
2022-09-13 12:42:39 -03:00
|
|
|
~Length();
|
2019-07-01 12:17:32 -03:00
|
|
|
|
2021-09-30 17:57:35 -03:00
|
|
|
static Length make_auto();
|
|
|
|
|
static Length make_px(float value);
|
2022-01-25 09:11:23 -03:00
|
|
|
static Length make_calculated(NonnullRefPtr<CalculatedStyleValue>);
|
2022-01-14 09:23:54 -03:00
|
|
|
Length percentage_of(Percentage const&) const;
|
2020-06-24 06:08:46 -03:00
|
|
|
|
2022-02-18 13:17:27 -03:00
|
|
|
Length resolved(Layout::Node const& layout_node) const;
|
2020-06-24 12:45:42 -03:00
|
|
|
|
2019-07-01 12:17:32 -03:00
|
|
|
bool is_auto() const { return m_type == Type::Auto; }
|
2021-06-11 19:03:15 -03:00
|
|
|
bool is_calculated() const { return m_type == Type::Calculated; }
|
2022-02-18 16:26:09 -03:00
|
|
|
bool is_px() const { return m_type == Type::Px; }
|
2020-09-29 15:06:58 -03:00
|
|
|
|
|
|
|
|
bool is_absolute() const
|
|
|
|
|
{
|
|
|
|
|
return m_type == Type::Cm
|
|
|
|
|
|| m_type == Type::In
|
|
|
|
|
|| m_type == Type::Mm
|
|
|
|
|
|| m_type == Type::Px
|
|
|
|
|
|| m_type == Type::Pt
|
2020-10-05 12:18:07 -03:00
|
|
|
|| m_type == Type::Pc
|
2020-09-29 15:06:58 -03:00
|
|
|
|| m_type == Type::Q;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 15:39:09 -03:00
|
|
|
bool is_relative() const
|
|
|
|
|
{
|
|
|
|
|
return m_type == Type::Ex
|
|
|
|
|
|| m_type == Type::Em
|
2021-08-05 11:31:15 -03:00
|
|
|
|| m_type == Type::Ch
|
2020-09-08 15:39:09 -03:00
|
|
|
|| m_type == Type::Rem
|
|
|
|
|
|| m_type == Type::Vh
|
|
|
|
|
|| m_type == Type::Vw
|
|
|
|
|
|| m_type == Type::Vmax
|
|
|
|
|
|| m_type == Type::Vmin;
|
|
|
|
|
}
|
2019-07-01 12:17:32 -03:00
|
|
|
|
2020-06-07 12:55:46 -03:00
|
|
|
float raw_value() const { return m_value; }
|
2022-07-27 09:45:05 -03:00
|
|
|
NonnullRefPtr<CalculatedStyleValue> calculated_style_value() const;
|
2021-09-23 08:13:51 -03:00
|
|
|
|
|
|
|
|
float to_px(Layout::Node const&) const;
|
|
|
|
|
|
2022-03-28 07:03:44 -03:00
|
|
|
ALWAYS_INLINE float to_px(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float root_font_size) const
|
2020-06-23 18:21:58 -03:00
|
|
|
{
|
2021-10-05 11:39:40 -03:00
|
|
|
if (is_auto())
|
|
|
|
|
return 0;
|
2020-06-23 18:21:58 -03:00
|
|
|
if (is_relative())
|
2022-02-21 12:24:12 -03:00
|
|
|
return relative_length_to_px(viewport_rect, font_metrics, font_size, root_font_size);
|
2022-02-18 13:20:16 -03:00
|
|
|
if (is_calculated())
|
|
|
|
|
VERIFY_NOT_REACHED(); // We can't resolve a calculated length from here. :^(
|
2021-10-05 11:39:40 -03:00
|
|
|
return absolute_length_to_px();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ALWAYS_INLINE float absolute_length_to_px() const
|
|
|
|
|
{
|
2020-10-05 12:18:07 -03:00
|
|
|
constexpr float inch_pixels = 96.0f;
|
|
|
|
|
constexpr float centimeter_pixels = (inch_pixels / 2.54f);
|
2020-06-23 18:21:58 -03:00
|
|
|
switch (m_type) {
|
2020-09-29 15:06:58 -03:00
|
|
|
case Type::Cm:
|
2020-10-05 12:18:07 -03:00
|
|
|
return m_value * centimeter_pixels; // 1cm = 96px/2.54
|
2020-09-29 15:06:58 -03:00
|
|
|
case Type::In:
|
2020-10-05 12:18:07 -03:00
|
|
|
return m_value * inch_pixels; // 1in = 2.54 cm = 96px
|
2020-06-23 18:21:58 -03:00
|
|
|
case Type::Px:
|
2020-09-29 15:06:58 -03:00
|
|
|
return m_value; // 1px = 1/96th of 1in
|
2020-06-28 10:25:32 -03:00
|
|
|
case Type::Pt:
|
2020-10-05 12:18:07 -03:00
|
|
|
return m_value * ((1.0f / 72.0f) * inch_pixels); // 1pt = 1/72th of 1in
|
|
|
|
|
case Type::Pc:
|
|
|
|
|
return m_value * ((1.0f / 6.0f) * inch_pixels); // 1pc = 1/6th of 1in
|
2020-09-29 15:06:58 -03:00
|
|
|
case Type::Mm:
|
2020-10-05 12:18:07 -03:00
|
|
|
return m_value * ((1.0f / 10.0f) * centimeter_pixels); // 1mm = 1/10th of 1cm
|
2020-09-29 15:06:58 -03:00
|
|
|
case Type::Q:
|
2020-10-05 12:18:07 -03:00
|
|
|
return m_value * ((1.0f / 40.0f) * centimeter_pixels); // 1Q = 1/40th of 1cm
|
2020-06-23 18:21:58 -03:00
|
|
|
default:
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-23 18:21:58 -03:00
|
|
|
}
|
|
|
|
|
}
|
2019-07-01 12:17:32 -03:00
|
|
|
|
2022-02-21 14:35:52 -03:00
|
|
|
String to_string() const;
|
2019-07-24 02:34:07 -03:00
|
|
|
|
2022-09-13 12:42:39 -03:00
|
|
|
// We have a RefPtr<CalculatedStyleValue> member, but can't include the header StyleValue.h as it includes
|
|
|
|
|
// this file already. To break the cyclic dependency, we must move all method definitions out.
|
|
|
|
|
bool operator==(Length const& other) const;
|
2022-04-01 14:58:27 -03:00
|
|
|
bool operator!=(Length const& other) const
|
2020-12-15 15:39:33 -03:00
|
|
|
{
|
|
|
|
|
return !(*this == other);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-28 07:03:44 -03:00
|
|
|
float relative_length_to_px(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float root_font_size) const;
|
2021-09-23 08:13:51 -03:00
|
|
|
|
2019-07-01 12:17:32 -03:00
|
|
|
private:
|
2022-04-01 14:58:27 -03:00
|
|
|
char const* unit_name() const;
|
2020-06-07 12:55:46 -03:00
|
|
|
|
2022-02-18 13:50:17 -03:00
|
|
|
Type m_type;
|
2019-11-18 12:25:38 -03:00
|
|
|
float m_value { 0 };
|
2021-06-11 19:03:15 -03:00
|
|
|
|
2021-09-21 06:49:47 -03:00
|
|
|
RefPtr<CalculatedStyleValue> m_calculated_style;
|
2019-07-01 12:17:32 -03:00
|
|
|
};
|
2019-08-18 03:09:56 -03:00
|
|
|
|
2020-03-07 06:27:02 -03:00
|
|
|
}
|
2022-01-19 14:30:29 -03:00
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
struct AK::Formatter<Web::CSS::Length> : Formatter<StringView> {
|
|
|
|
|
ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Length const& length)
|
|
|
|
|
{
|
|
|
|
|
return Formatter<StringView>::format(builder, length.to_string());
|
|
|
|
|
}
|
|
|
|
|
};
|