File size: 2,636 Bytes
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
336a9be
d78e3fd
070294b
6023bcd
 
6567a38
 
ef28ed2
 
 
 
 
 
6023bcd
 
ef28ed2
 
 
d78e3fd
ef28ed2
336a9be
 
d78e3fd
070294b
 
ef28ed2
 
 
 
 
 
 
336a9be
070294b
 
 
ef28ed2
 
 
 
 
d78e3fd
 
070294b
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#pragma once

#include "core/arena.hpp"
#include "dom/html_parser.hpp"
#include "style/css_engine.hpp"

#include <cstdint>
#include <string>
#include <vector>

namespace astra {

struct Rect {
    float x = 0;
    float y = 0;
    float w = 0;
    float h = 0;
};

struct LayoutFrame {
    const DomNode* node = nullptr;
    ComputedStyle style;
    Rect border_box;
    Rect content_box;
    std::vector<std::string> wrapped_lines;
    std::vector<LayoutFrame*> children;
};

enum class DisplayItemType : uint32_t {
    FillRect = 1,
    StrokeRect = 2,
    DrawText = 3,
    Embed3D = 4,
    EmbedVideo = 5,
    DrawImage = 6,
    DrawInput = 7,
    DropShadow = 8,
    EmbedAudio = 9,
    FillGradient = 10,
    EmbedCanvas = 11,
    EmbedSvg = 12
};

struct DisplayItem {
    DisplayItemType type = DisplayItemType::FillRect;
    Rect rect;
    uint32_t color_argb = 0xFFFFFFFF;
    uint32_t color2_argb = 0xFFFFFFFF;
    float angle_deg = 180.0f;
    float radius = 0.0f;
    float thickness = 1.0f;
    float font_size = 14.0f;
    int32_t z_index = 0;
    bool bold = false;
    bool monospace = false;
    bool focused = false;
    bool fixed_viewport = false;
    bool has_clip = false;
    Rect clip_rect;
    std::string text;
};

enum class HitRegionKind : uint32_t {
    Link = 1,
    Button = 2,
    Scene3D = 3,
    Video = 4,
    Input = 5,
    Audio = 6,
    Download = 7
};

struct HitRegion {
    HitRegionKind kind = HitRegionKind::Link;
    Rect rect;
    int32_t z_index = 0;
    bool fixed_viewport = false;
    std::string target; // href for Link/Download, id/selector for Button/Input, src for Video/Audio/3D
};

struct DisplayList {
    uint32_t viewport_width = 1280;
    uint32_t viewport_height = 720;
    float content_height = 0.0f;
    uint32_t clear_color_argb = 0xFF0D1118;
    std::vector<DisplayItem> items;
    std::vector<HitRegion> hit_regions;

    const HitRegion* hit_test(float x, float y, float scroll_y = 0.0f) const;

    std::vector<uint8_t> serialize() const;
    static bool deserialize(const std::vector<uint8_t>& blob, DisplayList& out);
};

class LayoutEngine {
public:
    static LayoutFrame* build_layout(SlabArena& arena,
                                     const HtmlDocument& doc,
                                     const CssStylesheet& sheet,
                                     float viewport_w,
                                     float viewport_h);

    static DisplayList build_display_list(const LayoutFrame* root,
                                          uint32_t viewport_w,
                                          uint32_t viewport_h);
};

} // namespace astra