Serenity Operating System
at master 67 lines 1.4 kB view raw
1/* 2 * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include "Resolution.h" 8#include <LibWeb/CSS/StyleValue.h> 9 10namespace Web::CSS { 11 12Resolution::Resolution(int value, Type type) 13 : m_type(type) 14 , m_value(value) 15{ 16} 17 18Resolution::Resolution(float value, Type type) 19 : m_type(type) 20 , m_value(value) 21{ 22} 23 24ErrorOr<String> Resolution::to_string() const 25{ 26 return String::formatted("{}{}", m_value, unit_name()); 27} 28 29float Resolution::to_dots_per_pixel() const 30{ 31 switch (m_type) { 32 case Type::Dpi: 33 return m_value * 96; // 1in = 2.54cm = 96px 34 case Type::Dpcm: 35 return m_value * (96.0f / 2.54f); // 1cm = 96px/2.54 36 case Type::Dppx: 37 return m_value; 38 } 39 VERIFY_NOT_REACHED(); 40} 41 42StringView Resolution::unit_name() const 43{ 44 switch (m_type) { 45 case Type::Dpi: 46 return "dpi"sv; 47 case Type::Dpcm: 48 return "dpcm"sv; 49 case Type::Dppx: 50 return "dppx"sv; 51 } 52 VERIFY_NOT_REACHED(); 53} 54 55Optional<Resolution::Type> Resolution::unit_from_name(StringView name) 56{ 57 if (name.equals_ignoring_ascii_case("dpi"sv)) { 58 return Type::Dpi; 59 } else if (name.equals_ignoring_ascii_case("dpcm"sv)) { 60 return Type::Dpcm; 61 } else if (name.equals_ignoring_ascii_case("dppx"sv)) { 62 return Type::Dppx; 63 } 64 return {}; 65} 66 67}