Serenity Operating System
1/*
2 * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/RefPtr.h>
10#include <AK/String.h>
11#include <LibWeb/Forward.h>
12
13namespace Web::CSS {
14
15class Angle {
16public:
17 enum class Type {
18 Calculated,
19 Deg,
20 Grad,
21 Rad,
22 Turn,
23 };
24
25 static Optional<Type> unit_from_name(StringView);
26
27 Angle(int value, Type type);
28 Angle(float value, Type type);
29 static Angle make_calculated(NonnullRefPtr<CalculatedStyleValue>);
30 static Angle make_degrees(float);
31 Angle percentage_of(Percentage const&) const;
32
33 bool is_calculated() const { return m_type == Type::Calculated; }
34 NonnullRefPtr<CalculatedStyleValue> calculated_style_value() const;
35
36 ErrorOr<String> to_string() const;
37 float to_degrees() const;
38
39 bool operator==(Angle const& other) const
40 {
41 if (is_calculated())
42 return m_calculated_style == other.m_calculated_style;
43 return m_type == other.m_type && m_value == other.m_value;
44 }
45
46private:
47 StringView unit_name() const;
48
49 Type m_type;
50 float m_value { 0 };
51 RefPtr<CalculatedStyleValue> m_calculated_style;
52};
53
54}
55
56template<>
57struct AK::Formatter<Web::CSS::Angle> : Formatter<StringView> {
58 ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Angle const& angle)
59 {
60 return Formatter<StringView>::format(builder, TRY(angle.to_string()));
61 }
62};