summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Painting/PaintContext.cpp
blob: c418d0fe5e60536700c129313b0edc57d9ddb1c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibWeb/Painting/PaintContext.h>

namespace Web {

PaintContext::PaintContext(Gfx::Painter& painter, Palette const& palette, float device_pixels_per_css_pixel)
    : m_painter(painter)
    , m_palette(palette)
    , m_device_pixels_per_css_pixel(device_pixels_per_css_pixel)
{
}

SVGContext& PaintContext::svg_context()
{
    // FIXME: This is a total hack to avoid crashing on content that has SVG elements embedded directly in HTML without an <svg> element.
    if (!m_svg_context.has_value())
        m_svg_context = SVGContext { {} };
    return m_svg_context.value();
}

void PaintContext::set_svg_context(SVGContext context)
{
    m_svg_context = move(context);
}

void PaintContext::clear_svg_context()
{
    m_svg_context.clear();
}

CSSPixelRect PaintContext::css_viewport_rect() const
{
    return {
        m_device_viewport_rect.x().value() / m_device_pixels_per_css_pixel,
        m_device_viewport_rect.y().value() / m_device_pixels_per_css_pixel,
        m_device_viewport_rect.width().value() / m_device_pixels_per_css_pixel,
        m_device_viewport_rect.height().value() / m_device_pixels_per_css_pixel
    };
}

DevicePixels PaintContext::rounded_device_pixels(CSSPixels css_pixels) const
{
    return roundf(css_pixels.value() * m_device_pixels_per_css_pixel);
}

DevicePixels PaintContext::enclosing_device_pixels(CSSPixels css_pixels) const
{
    return ceilf(css_pixels.value() * m_device_pixels_per_css_pixel);
}

DevicePixels PaintContext::floored_device_pixels(CSSPixels css_pixels) const
{
    return floorf(css_pixels.value() * m_device_pixels_per_css_pixel);
}

DevicePixelPoint PaintContext::rounded_device_point(CSSPixelPoint point) const
{
    return {
        roundf(point.x().value() * m_device_pixels_per_css_pixel),
        roundf(point.y().value() * m_device_pixels_per_css_pixel)
    };
}

DevicePixelRect PaintContext::enclosing_device_rect(CSSPixelRect rect) const
{
    return {
        floorf(rect.x().value() * m_device_pixels_per_css_pixel),
        floorf(rect.y().value() * m_device_pixels_per_css_pixel),
        ceilf(rect.width().value() * m_device_pixels_per_css_pixel),
        ceilf(rect.height().value() * m_device_pixels_per_css_pixel)
    };
}

DevicePixelRect PaintContext::rounded_device_rect(CSSPixelRect rect) const
{
    return {
        roundf(rect.x().value() * m_device_pixels_per_css_pixel),
        roundf(rect.y().value() * m_device_pixels_per_css_pixel),
        roundf(rect.width().value() * m_device_pixels_per_css_pixel),
        roundf(rect.height().value() * m_device_pixels_per_css_pixel)
    };
}

DevicePixelSize PaintContext::enclosing_device_size(CSSPixelSize size) const
{
    return {
        ceilf(size.width().value() * m_device_pixels_per_css_pixel),
        ceilf(size.height().value() * m_device_pixels_per_css_pixel)
    };
}

DevicePixelSize PaintContext::rounded_device_size(CSSPixelSize size) const
{
    return {
        roundf(size.width().value() * m_device_pixels_per_css_pixel),
        roundf(size.height().value() * m_device_pixels_per_css_pixel)
    };
}

CSSPixels PaintContext::scale_to_css_pixels(DevicePixels device_pixels) const
{
    return device_pixels.value() / m_device_pixels_per_css_pixel;
}

CSSPixelPoint PaintContext::scale_to_css_point(DevicePixelPoint point) const
{
    return {
        scale_to_css_pixels(point.x()),
        scale_to_css_pixels(point.y())
    };
}

}