File size: 5,927 Bytes
ef28ed2
 
 
 
 
 
 
3e92b27
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24463b6
ef28ed2
336a9be
24463b6
 
 
ef28ed2
 
 
24463b6
 
 
 
 
 
 
 
 
 
 
 
 
ef28ed2
 
 
 
 
3e92b27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
336a9be
 
 
 
 
d78e3fd
 
 
 
 
 
 
070294b
 
 
 
 
6023bcd
 
 
 
 
 
 
 
 
 
 
 
d78e3fd
 
 
 
 
 
 
 
ef28ed2
 
 
 
 
 
 
 
 
 
6567a38
3e92b27
 
 
d78e3fd
24463b6
 
070294b
6023bcd
336a9be
ef28ed2
3e92b27
24463b6
 
3e92b27
 
 
d78e3fd
 
 
 
 
 
 
 
6567a38
 
d78e3fd
 
ef28ed2
 
 
 
 
6567a38
 
ef28ed2
6023bcd
ef28ed2
d78e3fd
ef28ed2
 
336a9be
 
24463b6
 
3e92b27
ef28ed2
 
 
 
 
 
 
 
 
 
 
070294b
 
ef28ed2
 
 
 
 
 
 
 
070294b
ef28ed2
 
 
 
 
 
 
24463b6
3e92b27
 
 
 
 
ef28ed2
 
 
070294b
 
24463b6
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#pragma once

#include "dom/html_parser.hpp"

#include <cstdint>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>

namespace astra {

struct Color {
    uint8_t r = 0;
    uint8_t g = 0;
    uint8_t b = 0;
    uint8_t a = 255;

    uint32_t to_argb() const {
        return (static_cast<uint32_t>(a) << 24) |
               (static_cast<uint32_t>(r) << 16) |
               (static_cast<uint32_t>(g) << 8) |
               static_cast<uint32_t>(b);
    }

    static Color from_argb(uint32_t v) {
        return Color{
            static_cast<uint8_t>((v >> 16) & 0xFF),
            static_cast<uint8_t>((v >> 8) & 0xFF),
            static_cast<uint8_t>(v & 0xFF),
            static_cast<uint8_t>((v >> 24) & 0xFF)
        };
    }
};

enum class DisplayType : uint8_t {
    Block,
    Inline,
    InlineBlock,
    Flex,
    Grid,
    Table,
    TableRow,
    TableCell,
    None
};

enum class FloatMode : uint8_t {
    None,
    Left,
    Right
};

enum class ClearMode : uint8_t {
    None,
    Left,
    Right,
    Both
};

enum class FlexDirection : uint8_t {
    Row,
    Column
};

enum class JustifyContent : uint8_t {
    FlexStart,
    Center,
    FlexEnd,
    SpaceBetween
};

enum class AlignItems : uint8_t {
    Stretch,
    FlexStart,
    Center,
    FlexEnd
};

enum class TextAlign : uint8_t {
    Left,
    Center,
    Right
};

struct GridTrack {
    float value = 1.0f;
    bool is_fr = true;
};

enum class PositionType : uint8_t {
    Static,
    Relative,
    Absolute,
    Fixed
};

enum class OverflowMode : uint8_t {
    Visible,
    Hidden
};

enum class BoxSizing : uint8_t {
    ContentBox,
    BorderBox
};

struct LinearGradient {
    bool enabled = false;
    float angle_deg = 180.0f; // CSS default: top-to-bottom (180deg)
    Color start{15, 23, 42, 255};
    Color end{30, 41, 59, 255};
};

struct BoxShadow {
    bool enabled = false;
    float offset_x = 0.0f;
    float offset_y = 6.0f;
    float blur = 12.0f;
    Color color{0, 0, 0, 110};
};

struct EdgeSizes {
    float top = 0;
    float right = 0;
    float bottom = 0;
    float left = 0;
};

struct ComputedStyle {
    DisplayType display = DisplayType::Block;
    FlexDirection flex_direction = FlexDirection::Row;
    bool flex_wrap = false;
    JustifyContent justify_content = JustifyContent::FlexStart;
    AlignItems align_items = AlignItems::Stretch;
    TextAlign text_align = TextAlign::Left;
    PositionType position = PositionType::Static;
    FloatMode float_mode = FloatMode::None;
    ClearMode clear_mode = ClearMode::None;
    OverflowMode overflow = OverflowMode::Visible;
    BoxSizing box_sizing = BoxSizing::ContentBox;
    std::vector<GridTrack> grid_columns;
    float gap = 0.0f;
    float width = -1.0f;      // < 0 means auto
    float width_pct = -1.0f;  // >= 0 means percentage of containing width
    std::string width_calc;   // non-empty means calc(...) expression resolved against containing width
    float height = -1.0f;     // < 0 means auto
    float min_width = -1.0f;
    float max_width = -1.0f;
    bool has_top = false;
    bool has_left = false;
    bool has_right = false;
    bool has_bottom = false;
    float top = 0.0f;
    float left = 0.0f;
    float right = 0.0f;
    float bottom = 0.0f;
    float translate_x = 0.0f;
    float translate_y = 0.0f;
    int z_index = 0;
    float opacity = 1.0f;
    EdgeSizes margin;
    EdgeSizes padding;
    float border_width = 0.0f;
    float border_radius = 0.0f;
    Color border_color{0, 0, 0, 0};
    float border_left_width = 0.0f;
    Color border_left_color{0, 0, 0, 0};
    Color background{0, 0, 0, 0};
    LinearGradient background_gradient;
    Color color{230, 234, 240, 255};
    BoxShadow box_shadow;
    float font_size = 14.0f;
    bool bold = false;
    bool monospace = false;
    float line_height = 1.48f;
    bool has_content = false;
    std::string content_text;
    std::unordered_map<std::string, std::string> css_vars;
};

struct CssDeclaration {
    std::string property;
    std::string value;
};

struct CssRule {
    std::vector<std::string> selectors;
    std::vector<CssDeclaration> declarations;
    size_t source_order = 0;
    float media_min_width = -1.0f;
    float media_max_width = -1.0f;
};

class CssStylesheet {
public:
    void parse(std::string_view css);
    const std::vector<CssRule>& rules() const { return rules_; }

private:
    void parse_block(std::string_view css, float min_w, float max_w);
    std::vector<CssRule> rules_;
};

class StyleEngine {
public:
    static Color parse_color(std::string_view val, Color fallback);
    static float parse_px(std::string_view val, float fallback = 0.0f);
    static float eval_calc(std::string_view val, float ref_px, float viewport_w = 1280.0f, float fallback = 0.0f);
    static std::string resolve_var_expr(std::string_view val,
                                        const std::unordered_map<std::string, std::string>& vars,
                                        int depth = 0);
    static std::unordered_map<std::string, std::string> collect_root_vars(const CssStylesheet& sheet,
                                                                          float viewport_w = 1280.0f);

    static ComputedStyle compute_style(const DomNode* node,
                                       const CssStylesheet& sheet,
                                       const ComputedStyle* parent_style,
                                       float viewport_w = 1280.0f);

    static bool compute_pseudo_style(const DomNode* node,
                                     const CssStylesheet& sheet,
                                     std::string_view pseudo_name, // "::before" or "::after"
                                     const ComputedStyle& element_style,
                                     ComputedStyle& out_style,
                                     float viewport_w = 1280.0f);
};

} // namespace astra