browser_engine 0.1.0

A modular, GPU-accelerated, security-first web browser engine built from scratch in Rust.
Documentation
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Batch 3 & Production Pillar 1 — Layout geometry pass
//
// Implements modern CSS layout: Block formatting context, Flexbox (row,
// column, justify-content, align-items), margin: auto horizontal centering,
// and real line-wrapping text flow.

use std::collections::HashMap;

use crate::style::ComputedStyle;

use super::box_model::Dimensions;
use super::tree::{BoxType, LayoutBox};

/// Rough text metrics stand-in until real font shaping exists.
pub const AVG_CHAR_WIDTH_PX: f32 = 8.0;
pub const LINE_HEIGHT_PX: f32 = 18.0;

pub fn layout_tree(
    root: &mut LayoutBox,
    viewport_width: f32,
    styles: &HashMap<usize, ComputedStyle>,
) {
    let containing_block = Dimensions {
        content: super::box_model::Rect {
            x: 0.0,
            y: 0.0,
            width: viewport_width,
            height: 0.0,
        },
        ..Default::default()
    };
    layout_box(root, containing_block, styles);
}

fn layout_box(
    layout_box_ref: &mut LayoutBox,
    containing_block: Dimensions,
    styles: &HashMap<usize, ComputedStyle>,
) {
    match layout_box_ref.box_type {
        BoxType::Block(node_idx) => layout_block(layout_box_ref, containing_block, styles, node_idx),
        BoxType::Anonymous => layout_anonymous_text(layout_box_ref, containing_block),
    }
}

fn style_for<'a>(
    styles: &'a HashMap<usize, ComputedStyle>,
    node_idx: usize,
) -> Option<&'a ComputedStyle> {
    styles.get(&node_idx)
}

/// Parses a CSS length value ("10px", "50%", "0") into pixels relative to
/// `containing`. Unknown units or garbage values fall back to 0.0.
pub fn parse_length(value: &str, containing: f32) -> f32 {
    let v = value.trim();
    if let Some(stripped) = v.strip_suffix("px") {
        stripped.trim().parse().unwrap_or(0.0)
    } else if let Some(stripped) = v.strip_suffix('%') {
        stripped
            .trim()
            .parse::<f32>()
            .map(|p| p / 100.0 * containing)
            .unwrap_or(0.0)
    } else {
        v.parse().unwrap_or(0.0)
    }
}

/// Reads a box-edge property (margin/padding/border-width) honoring both shorthand and longhands.
fn edge_sizes(
    style: Option<&ComputedStyle>,
    prefix: &str,
    containing_width: f32,
) -> super::box_model::EdgeSizes {
    let mut edges = super::box_model::EdgeSizes::default();
    let Some(style) = style else { return edges };

    if let Some(shorthand) = style.get(prefix) {
        let v = parse_length(shorthand, containing_width);
        edges = super::box_model::EdgeSizes {
            top: v,
            right: v,
            bottom: v,
            left: v,
        };
    }
    if let Some(v) = style.get(&format!("{prefix}-top")) {
        edges.top = parse_length(v, containing_width);
    }
    if let Some(v) = style.get(&format!("{prefix}-right")) {
        edges.right = parse_length(v, containing_width);
    }
    if let Some(v) = style.get(&format!("{prefix}-bottom")) {
        edges.bottom = parse_length(v, containing_width);
    }
    if let Some(v) = style.get(&format!("{prefix}-left")) {
        edges.left = parse_length(v, containing_width);
    }
    edges
}

/// Breaks text by whitespace into lines wrapped to `max_width`.
pub fn wrap_text(text: &str, max_width: f32, char_width: f32) -> Vec<String> {
    let mut lines = Vec::new();
    let max_chars = ((max_width / char_width).floor() as usize).max(1);

    for raw_line in text.lines() {
        let trimmed = raw_line.trim();
        if trimmed.is_empty() {
            continue;
        }
        let words = trimmed.split_whitespace();
        let mut current_line = String::new();

        for word in words {
            if current_line.is_empty() {
                current_line.push_str(word);
            } else if current_line.chars().count() + 1 + word.chars().count() <= max_chars {
                current_line.push(' ');
                current_line.push_str(word);
            } else {
                lines.push(current_line);
                current_line = word.to_string();
            }
        }
        if !current_line.is_empty() {
            lines.push(current_line);
        }
    }

    if lines.is_empty() {
        lines.push(String::new());
    }
    lines
}

fn layout_block(
    lb: &mut LayoutBox,
    containing_block: Dimensions,
    styles: &HashMap<usize, ComputedStyle>,
    node_idx: usize,
) {
    let style = style_for(styles, node_idx);
    let containing_width = containing_block.content.width;

    lb.dimensions.margin = edge_sizes(style, "margin", containing_width);
    lb.dimensions.border = edge_sizes(style, "border-width", containing_width);
    lb.dimensions.padding = edge_sizes(style, "padding", containing_width);

    let explicit_width = style
        .and_then(|s| s.get("width"))
        .filter(|w| w.as_str() != "auto")
        .map(|w| parse_length(w, containing_width));

    let used_width = explicit_width.unwrap_or_else(|| {
        (containing_width
            - lb.dimensions.margin.left
            - lb.dimensions.margin.right
            - lb.dimensions.border.left
            - lb.dimensions.border.right
            - lb.dimensions.padding.left
            - lb.dimensions.padding.right)
            .max(0.0)
    });
    lb.dimensions.content.width = used_width;

    // Check for margin: auto centering
    let margin_left_auto = style
        .and_then(|s| s.get("margin-left"))
        .map(|m| m.as_str() == "auto")
        .unwrap_or(false)
        || style
            .and_then(|s| s.get("margin"))
            .map(|m| m.contains("auto"))
            .unwrap_or(false);
    let margin_right_auto = style
        .and_then(|s| s.get("margin-right"))
        .map(|m| m.as_str() == "auto")
        .unwrap_or(false)
        || style
            .and_then(|s| s.get("margin"))
            .map(|m| m.contains("auto"))
            .unwrap_or(false);

    if margin_left_auto && margin_right_auto && explicit_width.is_some() {
        let free_space = (containing_width
            - used_width
            - lb.dimensions.border.left
            - lb.dimensions.border.right
            - lb.dimensions.padding.left
            - lb.dimensions.padding.right)
            .max(0.0);
        let half = free_space / 2.0;
        lb.dimensions.margin.left = half;
        lb.dimensions.margin.right = half;
    }

    // Normal flow placement relative to containing block
    lb.dimensions.content.x = containing_block.content.x
        + lb.dimensions.margin.left
        + lb.dimensions.border.left
        + lb.dimensions.padding.left;
    lb.dimensions.content.y = containing_block.content.y
        + containing_block.content.height
        + lb.dimensions.margin.top
        + lb.dimensions.border.top
        + lb.dimensions.padding.top;

    let position_mode = style
        .and_then(|s| s.get("position"))
        .map(|p| p.trim().to_ascii_lowercase())
        .unwrap_or_else(|| "static".to_string());

    let top_offset = style
        .and_then(|s| s.get("top"))
        .map(|v| parse_length(v, containing_block.content.height));
    let left_offset = style
        .and_then(|s| s.get("left"))
        .map(|v| parse_length(v, containing_width));
    let right_offset = style
        .and_then(|s| s.get("right"))
        .map(|v| parse_length(v, containing_width));

    match position_mode.as_str() {
        "fixed" => {
            let x = left_offset.unwrap_or_else(|| {
                if let Some(r) = right_offset {
                    (containing_width - used_width - r).max(0.0)
                } else {
                    0.0
                }
            });
            let y = top_offset.unwrap_or(0.0);
            lb.dimensions.content.x = x + lb.dimensions.margin.left;
            lb.dimensions.content.y = y + lb.dimensions.margin.top;
        }
        "absolute" => {
            let x = left_offset.unwrap_or_else(|| {
                if let Some(r) = right_offset {
                    (containing_width - used_width - r).max(0.0)
                } else {
                    0.0
                }
            });
            let y = top_offset.unwrap_or(0.0);
            lb.dimensions.content.x = containing_block.content.x + x + lb.dimensions.margin.left;
            lb.dimensions.content.y = containing_block.content.y + y + lb.dimensions.margin.top;
        }
        "relative" => {
            lb.dimensions.content.x += left_offset.unwrap_or(0.0);
            lb.dimensions.content.y += top_offset.unwrap_or(0.0);
        }
        _ => {}
    }

    let display = style
        .and_then(|s| s.get("display"))
        .map(|d| d.trim().to_ascii_lowercase());
    let is_flex = display.as_deref() == Some("flex") || display.as_deref() == Some("inline-flex");

    let children_height = if is_flex {
        layout_flex_children(lb, style, styles)
    } else {
        layout_block_children(lb, styles)
    };

    let explicit_height = style
        .and_then(|s| s.get("height"))
        .filter(|h| h.as_str() != "auto")
        .map(|h| parse_length(h, containing_width));

    lb.dimensions.content.height = explicit_height.unwrap_or(children_height);
}

fn layout_block_children(lb: &mut LayoutBox, styles: &HashMap<usize, ComputedStyle>) -> f32 {
    let mut cursor = lb.dimensions;
    cursor.content.height = 0.0;
    for child in &mut lb.children {
        layout_box(child, cursor, styles);
        let is_out_of_flow = match child.box_type {
            BoxType::Block(idx) => styles
                .get(&idx)
                .and_then(|s| s.get("position"))
                .map(|p| {
                    let s = p.trim().to_ascii_lowercase();
                    s == "absolute" || s == "fixed"
                })
                .unwrap_or(false),
            _ => false,
        };
        if !is_out_of_flow {
            cursor.content.height += child.dimensions.margin_box().height;
        }
    }
    cursor.content.height
}

fn layout_flex_children(
    lb: &mut LayoutBox,
    style: Option<&ComputedStyle>,
    styles: &HashMap<usize, ComputedStyle>,
) -> f32 {
    let flex_direction = style
        .and_then(|s| s.get("flex-direction"))
        .map(|d| d.trim().to_ascii_lowercase())
        .unwrap_or_else(|| "row".to_string());
    let is_row = flex_direction != "column";

    let justify_content = style
        .and_then(|s| s.get("justify-content"))
        .map(|d| d.trim().to_ascii_lowercase())
        .unwrap_or_else(|| "flex-start".to_string());

    let align_items = style
        .and_then(|s| s.get("align-items"))
        .map(|d| d.trim().to_ascii_lowercase())
        .unwrap_or_else(|| "stretch".to_string());

    if lb.children.is_empty() {
        return 0.0;
    }

    // First layout pass to measure children natural sizes
    let probe_cursor = Dimensions {
        content: super::box_model::Rect {
            x: lb.dimensions.content.x,
            y: lb.dimensions.content.y,
            width: lb.dimensions.content.width,
            height: 0.0,
        },
        ..Default::default()
    };

    for child in &mut lb.children {
        layout_box(child, probe_cursor, styles);
    }

    if is_row {
        let total_child_width: f32 = lb.children.iter().map(|c| c.dimensions.margin_box().width).sum();
        let free_space = (lb.dimensions.content.width - total_child_width).max(0.0);

        let (start_x, gap) = match justify_content.as_str() {
            "center" => (free_space / 2.0, 0.0),
            "flex-end" => (free_space, 0.0),
            "space-between" => {
                if lb.children.len() > 1 {
                    (0.0, free_space / (lb.children.len() - 1) as f32)
                } else {
                    (0.0, 0.0)
                }
            }
            "space-around" => {
                let space = free_space / lb.children.len() as f32;
                (space / 2.0, space)
            }
            _ => (0.0, 0.0), // "flex-start" default
        };

        let mut max_height: f32 = 0.0;
        for child in &lb.children {
            max_height = max_height.max(child.dimensions.margin_box().height);
        }

        let mut current_x = lb.dimensions.content.x + start_x;
        for child in &mut lb.children {
            child.dimensions.content.x = current_x + child.dimensions.margin.left;

            // Cross-axis alignment (align-items)
            let child_mb_height = child.dimensions.margin_box().height;
            let align_y_offset = match align_items.as_str() {
                "center" => (max_height - child_mb_height) / 2.0,
                "flex-end" => max_height - child_mb_height,
                _ => 0.0, // "flex-start", "stretch"
            };

            child.dimensions.content.y = lb.dimensions.content.y
                + align_y_offset
                + child.dimensions.margin.top;

            current_x += child.dimensions.margin_box().width + gap;
        }

        max_height
    } else {
        // flex-direction: column
        let mut cursor_y = lb.dimensions.content.y;
        for child in &mut lb.children {
            child.dimensions.content.y = cursor_y + child.dimensions.margin.top;
            cursor_y += child.dimensions.margin_box().height;
        }
        cursor_y - lb.dimensions.content.y
    }
}

fn layout_anonymous_text(lb: &mut LayoutBox, containing_block: Dimensions) {
    lb.dimensions.content.x = containing_block.content.x;
    lb.dimensions.content.y = containing_block.content.y + containing_block.content.height;
    lb.dimensions.content.width = containing_block.content.width;

    let text = lb.text_content.as_deref().unwrap_or("");
    let lines = wrap_text(text, containing_block.content.width, AVG_CHAR_WIDTH_PX);
    lb.dimensions.content.height = (lines.len() as f32 * LINE_HEIGHT_PX).max(LINE_HEIGHT_PX);
}