Coverage Report

Created: 2026-07-25 07:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibWeb/CSS/Resolution.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
3
 * Copyright (c) 2024, Glenn Skrzypczak <glenn.skrzypczak@gmail.com>
4
 *
5
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#include "Resolution.h"
9
10
namespace Web::CSS {
11
12
Resolution::Resolution(double value, Type type)
13
0
    : m_type(type)
14
0
    , m_value(value)
15
0
{
16
0
}
17
18
Resolution Resolution::make_dots_per_pixel(double value)
19
0
{
20
0
    return { value, Type::Dppx };
21
0
}
22
23
String Resolution::to_string() const
24
0
{
25
0
    return MUST(String::formatted("{}dppx", to_dots_per_pixel()));
26
0
}
27
28
double Resolution::to_dots_per_pixel() const
29
0
{
30
0
    switch (m_type) {
31
0
    case Type::Dpi:
32
0
        return m_value / 96; // 1in = 2.54cm = 96px
33
0
    case Type::Dpcm:
34
0
        return m_value / (96.0 / 2.54); // 1cm = 96px/2.54
35
0
    case Type::Dppx:
36
0
        return m_value;
37
0
    }
38
0
    VERIFY_NOT_REACHED();
39
0
}
40
41
StringView Resolution::unit_name() const
42
0
{
43
0
    switch (m_type) {
44
0
    case Type::Dpi:
45
0
        return "dpi"sv;
46
0
    case Type::Dpcm:
47
0
        return "dpcm"sv;
48
0
    case Type::Dppx:
49
0
        return "dppx"sv;
50
0
    }
51
0
    VERIFY_NOT_REACHED();
52
0
}
53
54
Optional<Resolution::Type> Resolution::unit_from_name(StringView name)
55
0
{
56
0
    if (name.equals_ignoring_ascii_case("dpi"sv)) {
57
0
        return Type::Dpi;
58
0
    } else if (name.equals_ignoring_ascii_case("dpcm"sv)) {
59
0
        return Type::Dpcm;
60
0
    } else if (name.equals_ignoring_ascii_case("dppx"sv) || name.equals_ignoring_ascii_case("x"sv)) {
61
0
        return Type::Dppx;
62
0
    }
63
0
    return {};
64
0
}
65
66
}