Serenity Operating System
at master 59 lines 1.5 kB view raw
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 { 14class Frequency { 15public: 16 enum class Type { 17 Calculated, 18 Hz, 19 kHz 20 }; 21 22 static Optional<Type> unit_from_name(StringView); 23 24 Frequency(int value, Type type); 25 Frequency(float value, Type type); 26 static Frequency make_calculated(NonnullRefPtr<CalculatedStyleValue>); 27 static Frequency make_hertz(float); 28 Frequency percentage_of(Percentage const&) const; 29 30 bool is_calculated() const { return m_type == Type::Calculated; } 31 NonnullRefPtr<CalculatedStyleValue> calculated_style_value() const; 32 33 ErrorOr<String> to_string() const; 34 float to_hertz() const; 35 36 bool operator==(Frequency const& other) const 37 { 38 if (is_calculated()) 39 return m_calculated_style == other.m_calculated_style; 40 return m_type == other.m_type && m_value == other.m_value; 41 } 42 43private: 44 StringView unit_name() const; 45 46 Type m_type; 47 float m_value { 0 }; 48 RefPtr<CalculatedStyleValue> m_calculated_style; 49}; 50 51} 52 53template<> 54struct AK::Formatter<Web::CSS::Frequency> : Formatter<StringView> { 55 ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Frequency const& frequency) 56 { 57 return Formatter<StringView>::format(builder, TRY(frequency.to_string())); 58 } 59};