Coverage Report

Created: 2025-03-04 07:22

/src/serenity/Userland/Libraries/LibWeb/SVG/SVGDecodedImageData.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#pragma once
8
9
#include <LibWeb/DOM/Document.h>
10
#include <LibWeb/HTML/DecodedImageData.h>
11
12
namespace Web::SVG {
13
14
class SVGDecodedImageData final : public HTML::DecodedImageData {
15
    JS_CELL(SVGDecodedImageData, HTML::DecodedImageData);
16
    JS_DECLARE_ALLOCATOR(SVGDecodedImageData);
17
18
public:
19
    class SVGPageClient;
20
    static ErrorOr<JS::NonnullGCPtr<SVGDecodedImageData>> create(JS::Realm&, JS::NonnullGCPtr<Page>, URL::URL const&, ByteBuffer encoded_svg);
21
    virtual ~SVGDecodedImageData() override;
22
23
    virtual RefPtr<Gfx::ImmutableBitmap> bitmap(size_t frame_index, Gfx::IntSize) const override;
24
25
    virtual Optional<CSSPixels> intrinsic_width() const override;
26
    virtual Optional<CSSPixels> intrinsic_height() const override;
27
    virtual Optional<CSSPixelFraction> intrinsic_aspect_ratio() const override;
28
29
    // FIXME: Support SVG animations. :^)
30
0
    virtual int frame_duration(size_t) const override { return 0; }
31
0
    virtual size_t frame_count() const override { return 1; }
32
0
    virtual size_t loop_count() const override { return 0; }
33
0
    virtual bool is_animated() const override { return false; }
34
35
0
    DOM::Document const& svg_document() const { return *m_document; }
36
37
    virtual void visit_edges(Cell::Visitor& visitor) override;
38
39
private:
40
    SVGDecodedImageData(JS::NonnullGCPtr<Page>, JS::NonnullGCPtr<SVGPageClient>, JS::NonnullGCPtr<DOM::Document>, JS::NonnullGCPtr<SVG::SVGSVGElement>);
41
42
    RefPtr<Gfx::Bitmap> render(Gfx::IntSize) const;
43
44
    mutable HashMap<Gfx::IntSize, NonnullRefPtr<Gfx::ImmutableBitmap>> m_cached_rendered_bitmaps;
45
46
    JS::NonnullGCPtr<Page> m_page;
47
    JS::NonnullGCPtr<SVGPageClient> m_page_client;
48
49
    JS::NonnullGCPtr<DOM::Document> m_document;
50
    JS::NonnullGCPtr<SVG::SVGSVGElement> m_root_element;
51
};
52
53
class SVGDecodedImageData::SVGPageClient final : public PageClient {
54
    JS_CELL(SVGDecodedImageData::SVGPageClient, PageClient);
55
    JS_DECLARE_ALLOCATOR(SVGDecodedImageData::SVGPageClient);
56
57
public:
58
    static JS::NonnullGCPtr<SVGPageClient> create(JS::VM& vm, Page& page)
59
0
    {
60
0
        return vm.heap().allocate_without_realm<SVGPageClient>(page);
61
0
    }
62
63
    virtual ~SVGPageClient() override = default;
64
65
    JS::NonnullGCPtr<Page> m_host_page;
66
    JS::GCPtr<Page> m_svg_page;
67
68
0
    virtual Page& page() override { return *m_svg_page; }
69
0
    virtual Page const& page() const override { return *m_svg_page; }
70
0
    virtual bool is_connection_open() const override { return false; }
71
0
    virtual Gfx::Palette palette() const override { return m_host_page->client().palette(); }
72
0
    virtual DevicePixelRect screen_rect() const override { return {}; }
73
0
    virtual double device_pixels_per_css_pixel() const override { return 1.0; }
74
0
    virtual CSS::PreferredColorScheme preferred_color_scheme() const override { return m_host_page->client().preferred_color_scheme(); }
75
0
    virtual CSS::PreferredContrast preferred_contrast() const override { return m_host_page->client().preferred_contrast(); }
76
0
    virtual CSS::PreferredMotion preferred_motion() const override { return m_host_page->client().preferred_motion(); }
77
0
    virtual void request_file(FileRequest) override { }
78
0
    virtual void paint_next_frame() override { }
79
0
    virtual void paint(DevicePixelRect const&, Gfx::Bitmap&, Web::PaintOptions = {}) override { }
80
0
    virtual void schedule_repaint() override { }
81
0
    virtual bool is_ready_to_paint() const override { return true; }
82
83
0
    virtual DisplayListPlayerType display_list_player_type() const override { return m_host_page->client().display_list_player_type(); }
84
85
private:
86
    explicit SVGPageClient(Page& host_page)
87
0
        : m_host_page(host_page)
88
0
    {
89
0
    }
90
91
    virtual void visit_edges(Visitor&) override;
92
};
93
94
}