/src/serenity/Userland/Libraries/LibWeb/CSS/Angle.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include "Angle.h" |
8 | | #include <AK/Math.h> |
9 | | #include <LibWeb/CSS/Percentage.h> |
10 | | #include <LibWeb/CSS/StyleValues/CSSMathValue.h> |
11 | | |
12 | | namespace Web::CSS { |
13 | | |
14 | | Angle::Angle(double value, Type type) |
15 | 0 | : m_type(type) |
16 | 0 | , m_value(value) |
17 | 0 | { |
18 | 0 | } |
19 | | |
20 | | Angle Angle::make_degrees(double value) |
21 | 0 | { |
22 | 0 | return { value, Type::Deg }; |
23 | 0 | } |
24 | | |
25 | | Angle Angle::percentage_of(Percentage const& percentage) const |
26 | 0 | { |
27 | 0 | return Angle { percentage.as_fraction() * m_value, m_type }; |
28 | 0 | } |
29 | | |
30 | | String Angle::to_string() const |
31 | 0 | { |
32 | 0 | return MUST(String::formatted("{}deg", to_degrees())); |
33 | 0 | } |
34 | | |
35 | | double Angle::to_degrees() const |
36 | 0 | { |
37 | 0 | switch (m_type) { |
38 | 0 | case Type::Deg: |
39 | 0 | return m_value; |
40 | 0 | case Type::Grad: |
41 | 0 | return m_value * (360.0 / 400.0); |
42 | 0 | case Type::Rad: |
43 | 0 | return AK::to_degrees(m_value); |
44 | 0 | case Type::Turn: |
45 | 0 | return m_value * 360.0; |
46 | 0 | } |
47 | 0 | VERIFY_NOT_REACHED(); |
48 | 0 | } |
49 | | |
50 | | double Angle::to_radians() const |
51 | 0 | { |
52 | 0 | return AK::to_radians(to_degrees()); |
53 | 0 | } |
54 | | |
55 | | StringView Angle::unit_name() const |
56 | 0 | { |
57 | 0 | switch (m_type) { |
58 | 0 | case Type::Deg: |
59 | 0 | return "deg"sv; |
60 | 0 | case Type::Grad: |
61 | 0 | return "grad"sv; |
62 | 0 | case Type::Rad: |
63 | 0 | return "rad"sv; |
64 | 0 | case Type::Turn: |
65 | 0 | return "turn"sv; |
66 | 0 | } |
67 | 0 | VERIFY_NOT_REACHED(); |
68 | 0 | } |
69 | | |
70 | | Optional<Angle::Type> Angle::unit_from_name(StringView name) |
71 | 0 | { |
72 | 0 | if (name.equals_ignoring_ascii_case("deg"sv)) { |
73 | 0 | return Type::Deg; |
74 | 0 | } |
75 | 0 | if (name.equals_ignoring_ascii_case("grad"sv)) { |
76 | 0 | return Type::Grad; |
77 | 0 | } |
78 | 0 | if (name.equals_ignoring_ascii_case("rad"sv)) { |
79 | 0 | return Type::Rad; |
80 | 0 | } |
81 | 0 | if (name.equals_ignoring_ascii_case("turn"sv)) { |
82 | 0 | return Type::Turn; |
83 | 0 | } |
84 | 0 | return {}; |
85 | 0 | } |
86 | | |
87 | | Angle Angle::resolve_calculated(NonnullRefPtr<CSSMathValue> const& calculated, Layout::Node const&, Angle const& reference_value) |
88 | 0 | { |
89 | 0 | return calculated->resolve_angle_percentage(reference_value).value(); |
90 | 0 | } |
91 | | |
92 | | } |