dustnet-client 0.2.1

Dustnet client library: transport, viewer, compositor, and AML/WASM runtime
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//! `NodeKind::Text` scene-native layout.
//!
//! Covers every TextSource variant (Text, Pre, Heading, Art,
//! ElementDef, TextAnimate). Single-run / no-children content takes
//! the direct wrap path; multi-run / node-bearing-inline-children
//! content routes through `super::try_collect_inline_segments` and the
//! inline wrap/render primitives from `engine`.

use crate::compositor::layout::Rect;
use crate::compositor::layout::border::draw_hr;
use crate::compositor::layout::cell::{CellBuffer, CellStyle};
use crate::compositor::layout::engine::{
    self, LayoutCtx, Placement, resolve_or_inherit, try_temp_vec_from_slice,
};
use crate::compositor::scene::{NodeId, NodeKind, Scene, TextContent, TextSource};
use crate::parser::ast::{Alignment, HrStyle};

pub(crate) fn layout(
    buf: &mut CellBuffer,
    ctx: &mut LayoutCtx,
    scene: &mut Scene,
    node_id: NodeId,
) -> Placement {
    // Snapshot: TextContent + child ids.
    let (data, children) = {
        let Some(node) = scene.get(node_id) else {
            return Placement::empty_at(ctx.x, ctx.y);
        };
        let NodeKind::Text(d) = node.kind() else {
            return Placement::empty_at(ctx.x, ctx.y);
        };
        let Some(children) = try_temp_vec_from_slice(node.children(), ctx.governor.as_ref()) else {
            buf.record_allocation_failure();
            scene.record_resource_error();
            return Placement::empty_at(ctx.x, ctx.y);
        };
        (d.clone(), children)
    };

    let placement = match data.source {
        TextSource::Pre => layout_pre(buf, ctx, scene, node_id, &data),
        TextSource::Art => layout_art(buf, ctx, scene, node_id, &data),
        TextSource::ElementDef => layout_element_def(buf, ctx, scene, node_id, &data),
        TextSource::TextAnimate => layout_text_animate(buf, ctx, scene, node_id, &data),
        TextSource::Heading(level) => {
            layout_heading(buf, ctx, scene, node_id, &data, level, &children)
        }
        TextSource::Text => layout_text_plain(buf, ctx, scene, &data, node_id, &children),
    };
    scene.update_placement(node_id, placement);
    placement
}

fn layout_pre(
    buf: &mut CellBuffer,
    ctx: &mut LayoutCtx,
    scene: &mut Scene,
    node_id: NodeId,
    data: &TextContent,
) -> Placement {
    let start_x = ctx.x;
    let start_y = ctx.y;
    let width = ctx.width;

    let run = data.runs.first();
    let content = run.map(|r| r.text.as_str()).unwrap_or("");

    let style = CellStyle {
        fg: resolve_or_inherit(
            run.and_then(|r| r.fg.as_ref()),
            ctx.style.fg,
            ctx.color_support,
        ),
        bg: resolve_or_inherit(
            run.and_then(|r| r.bg.as_ref()),
            ctx.style.bg,
            ctx.color_support,
        ),
        ..ctx.style.clone()
    };

    let block_offset = match data.align {
        Alignment::Left => 0u16,
        Alignment::Center | Alignment::Right => {
            let max_w = content
                .split('\n')
                .map(|l| crate::compositor::layout::text::display_width(l, ctx.wcfg) as u16)
                .max()
                .unwrap_or(0);
            match data.align {
                Alignment::Center => ctx.width.saturating_sub(max_w) / 2,
                Alignment::Right => ctx.width.saturating_sub(max_w),
                _ => 0,
            }
        }
    };

    scene.allocate_buffer(node_id, width.max(1), 1);
    let line_count = content.split('\n').count() as u16;
    let mut local_y: u16 = 0;
    if let Some(text_buf) = scene.layout_buffer_mut(node_id) {
        text_buf.ensure_height(line_count.max(1));
        for line in content.split('\n') {
            text_buf.ensure_height(local_y.saturating_add(1));
            engine::put_str_clipped(
                text_buf,
                block_offset,
                local_y,
                line,
                &style,
                width,
                ctx.wcfg,
            );
            local_y = local_y.saturating_add(1);
        }
    }

    let h = local_y;
    ctx.y = start_y.saturating_add(h);
    buf.ensure_height(ctx.y);
    let rect = Rect::new(start_x, start_y, width, h);
    Placement {
        rect,
        flow_advance: h,
        bbox: rect,
    }
}

fn layout_art(
    buf: &mut CellBuffer,
    ctx: &mut LayoutCtx,
    scene: &mut Scene,
    node_id: NodeId,
    data: &TextContent,
) -> Placement {
    let start_x = ctx.x;
    let start_y = ctx.y;
    let ctx_width = ctx.width;

    let content = data.runs.first().map(|r| r.text.as_str()).unwrap_or("");

    scene.allocate_buffer(node_id, ctx_width.max(1), 1);
    let line_count = content.split('\n').count() as u16;
    let mut local_y: u16 = 0;
    let style = ctx.style.clone();
    if let Some(text_buf) = scene.layout_buffer_mut(node_id) {
        text_buf.ensure_height(line_count.max(1));
        for line in content.split('\n') {
            text_buf.ensure_height(local_y.saturating_add(1));
            engine::put_str_clipped(text_buf, 0, local_y, line, &style, ctx_width, ctx.wcfg);
            local_y = local_y.saturating_add(1);
        }
    }

    let h = local_y;
    ctx.y = start_y.saturating_add(h);
    buf.ensure_height(ctx.y);
    let rect = Rect::new(start_x, start_y, ctx_width, h);
    Placement {
        rect,
        flow_advance: h,
        bbox: rect,
    }
}

fn layout_element_def(
    buf: &mut CellBuffer,
    ctx: &mut LayoutCtx,
    scene: &mut Scene,
    node_id: NodeId,
    data: &TextContent,
) -> Placement {
    let start_x = ctx.x;
    let start_y = ctx.y;
    let ctx_width = ctx.width;

    let run = data.runs.first();
    let content = run.map(|r| r.text.as_str()).unwrap_or("").trim();
    let style = CellStyle {
        fg: resolve_or_inherit(
            run.and_then(|r| r.fg.as_ref()),
            ctx.style.fg,
            ctx.color_support,
        ),
        ..ctx.style.clone()
    };

    let content_w = (crate::compositor::layout::text::display_width(content, ctx.wcfg)
        .min(ctx_width as usize)) as u16;

    // ElementDef writes past ctx.width (it uses buf.width as the clip
    // boundary in the pre-pivot code). Mirror that by sizing the buffer
    // to the full content width.
    let buf_w = content_w.max(ctx_width).max(1);
    scene.allocate_buffer(node_id, buf_w, 1);
    if let Some(text_buf) = scene.layout_buffer_mut(node_id) {
        engine::put_str_clipped(text_buf, 0, 0, content, &style, buf_w, ctx.wcfg);
    }

    ctx.y = start_y.saturating_add(1);
    buf.ensure_height(ctx.y);

    let rect = Rect::new(start_x, start_y, ctx_width, 1);
    let bbox_rect = Rect::new(start_x, start_y, content_w.max(1), 1);
    Placement {
        rect,
        flow_advance: 1,
        bbox: rect.union(bbox_rect),
    }
}

fn layout_text_animate(
    buf: &mut CellBuffer,
    ctx: &mut LayoutCtx,
    scene: &mut Scene,
    node_id: NodeId,
    data: &TextContent,
) -> Placement {
    let start_x = ctx.x;
    let start_y = ctx.y;
    let ctx_width = ctx.width;

    let content = data.runs.first().map(|r| r.text.as_str()).unwrap_or("");

    scene.allocate_buffer(node_id, ctx_width.max(1), 1);
    let style = ctx.style.clone();
    if let Some(text_buf) = scene.layout_buffer_mut(node_id) {
        engine::put_str_clipped(text_buf, 0, 0, content, &style, ctx_width, ctx.wcfg);
    }

    ctx.y = start_y.saturating_add(1);
    buf.ensure_height(ctx.y);

    let rect = Rect::new(start_x, start_y, ctx_width, 1);
    Placement {
        rect,
        flow_advance: 1,
        bbox: rect,
    }
}

fn layout_heading(
    buf: &mut CellBuffer,
    ctx: &mut LayoutCtx,
    scene: &mut Scene,
    node_id: NodeId,
    data: &TextContent,
    level: u8,
    children: &[NodeId],
) -> Placement {
    let start_x = ctx.x;
    let start_y = ctx.y;
    let width = ctx.width;

    let run = data.runs.first();
    let content = run.map(|r| r.text.as_str().trim()).unwrap_or("");

    let style = CellStyle {
        fg: resolve_or_inherit(
            run.and_then(|r| r.fg.as_ref()),
            ctx.style.fg,
            ctx.color_support,
        ),
        bold: true,
        ..ctx.style.clone()
    };

    let inline_lines = if children.is_empty() {
        None
    } else {
        let Some(segments) = super::try_collect_inline_segments(
            children,
            scene,
            &style,
            ctx.color_support,
            None,
            ctx.governor.as_ref(),
        ) else {
            buf.record_allocation_failure();
            scene.record_resource_error();
            return Placement::empty_at(start_x, start_y);
        };
        if segments.is_empty() {
            None
        } else {
            let Some(lines) = engine::try_wrap_inline_segments(
                &segments,
                width as usize,
                ctx.wcfg,
                ctx.governor.as_ref(),
            ) else {
                buf.record_allocation_failure();
                scene.record_resource_error();
                return Placement::empty_at(start_x, start_y);
            };
            Some(lines)
        }
    };

    scene.allocate_buffer(node_id, width.max(1), 1);

    let mut local_ctx = LayoutCtx {
        x: 0,
        y: 0,
        width,
        viewport_height: ctx.viewport_height,
        color_support: ctx.color_support,
        wcfg: ctx.wcfg,
        style: style.clone(),
        governor: ctx.governor.clone(),
    };

    let wrapped = match scene.layout_buffer_mut(node_id) {
        Some(text_buf) if !content.is_empty() => {
            engine::render_wrapped_text(text_buf, &mut local_ctx, content, &style, Alignment::Left)
        }
        _ => true,
    };
    if !wrapped {
        buf.record_allocation_failure();
        scene.record_resource_error();
        return Placement::empty_at(start_x, start_y);
    }

    if let Some(lines) = inline_lines {
        let placeholder = CellBuffer::new(1, 1);
        let mut text_buf = match scene.layout_buffer_mut(node_id) {
            Some(b) => std::mem::replace(b, placeholder),
            None => placeholder,
        };
        engine::render_inline_lines_with_origin(
            &mut text_buf,
            &mut local_ctx,
            scene,
            &lines,
            Alignment::Left,
            (start_x, start_y),
        );
        if let Some(b) = scene.layout_buffer_mut(node_id) {
            *b = text_buf;
        }
    }

    if level == 1 && local_ctx.y > 0 {
        if let Some(text_buf) = scene.layout_buffer_mut(node_id) {
            text_buf.ensure_height(local_ctx.y.saturating_add(1));
            draw_hr(text_buf, local_ctx.y, 0, width, HrStyle::Heavy, &style);
        }
        local_ctx.y = local_ctx.y.saturating_add(1);
    }

    let h = local_ctx.y;
    ctx.y = start_y.saturating_add(h);
    buf.ensure_height(ctx.y);
    let rect = Rect::new(start_x, start_y, width, h);
    Placement {
        rect,
        flow_advance: h,
        bbox: rect,
    }
}

fn layout_text_plain(
    buf: &mut CellBuffer,
    ctx: &mut LayoutCtx,
    scene: &mut Scene,
    data: &TextContent,
    node_id: NodeId,
    children: &[NodeId],
) -> Placement {
    let has_children = !children.is_empty();
    let multi_run = data.runs.len() > 1;
    if has_children || multi_run {
        // Phase 1 pivot: multi-run / inline-children text allocates its
        // own buffer and writes at local (0, 0). Focusable rects inside
        // (Links, Buttons) are written via render_inline_lines_with_origin
        // with the node's global origin so hit-testing reads screen coords.
        let start_x = ctx.x;
        let start_y = ctx.y;
        let width = ctx.width;
        let Some(segments) = super::try_collect_inline_segments(
            &[node_id],
            scene,
            &ctx.style,
            ctx.color_support,
            None,
            ctx.governor.as_ref(),
        ) else {
            buf.record_allocation_failure();
            scene.record_resource_error();
            return Placement::empty_at(start_x, start_y);
        };
        let lines = if segments.is_empty() {
            None
        } else {
            let Some(lines) = engine::try_wrap_inline_segments(
                &segments,
                width as usize,
                ctx.wcfg,
                ctx.governor.as_ref(),
            ) else {
                buf.record_allocation_failure();
                scene.record_resource_error();
                return Placement::empty_at(start_x, start_y);
            };
            Some(lines)
        };
        drop(segments);

        scene.allocate_buffer(node_id, width.max(1), 1);

        let mut local_ctx = LayoutCtx {
            x: 0,
            y: 0,
            width,
            viewport_height: ctx.viewport_height,
            color_support: ctx.color_support,
            wcfg: ctx.wcfg,
            style: ctx.style.clone(),
            governor: ctx.governor.clone(),
        };

        if let Some(lines) = lines {
            // We need a mutable borrow of text_buf AND scene in the
            // render call. Take the buffer by raw pointer dance? No — we
            // can pass scene through render_inline_lines_with_origin which
            // takes both; the node's buffer is accessed via
            // scene.layout_buffer_mut inside each call. Simpler: pull the
            // buffer out, render, don't touch scene inside the call, then
            // write focusable rects separately. But render_inline_lines
            // already interleaves cell writes and focusable_rect writes.
            // Use a scope to reborrow: take the text buffer via a
            // "swap-out / swap-in" — replace with an empty placeholder
            // buffer during the render so scene is free to be mutated.
            let placeholder = crate::compositor::layout::cell::CellBuffer::new(1, 1);
            let mut text_buf = match scene.layout_buffer_mut(node_id) {
                Some(b) => std::mem::replace(b, placeholder),
                None => placeholder,
            };
            engine::render_inline_lines_with_origin(
                &mut text_buf,
                &mut local_ctx,
                scene,
                &lines,
                data.align,
                (start_x, start_y),
            );
            if let Some(b) = scene.layout_buffer_mut(node_id) {
                *b = text_buf;
            }
        }

        let h = local_ctx.y;
        ctx.y = start_y.saturating_add(h);
        buf.ensure_height(ctx.y);
        let rect = Rect::new(start_x, start_y, width, h);
        return Placement {
            rect,
            flow_advance: h,
            bbox: rect,
        };
    }

    // Phase 1 pivot (per-node-buffer migration, Strategy D): single-run /
    // no-children text allocates its own `CellBuffer` and writes at LOCAL
    // coordinates (0, 0) inside it. The dispatcher translates the local
    // extent to a global `placement.rect` so the composite walk blits the
    // text buffer at the right screen position — no writes to `buf`
    // (page_buf) happen on this path. The companion pivot of
    // `layout_box_node` (Phase 2) keeps paint order correct when text
    // sits beside or inside a box, by making the box chrome a separate
    // per-node buffer that blits in tree order.

    let start_x = ctx.x;
    let start_y = ctx.y;
    let width = ctx.width;

    let run = data.runs.first();
    let content = run.map(|r| r.text.as_str()).unwrap_or("");

    let style = run
        .map(|r| CellStyle {
            fg: resolve_or_inherit(r.fg.as_ref(), ctx.style.fg, ctx.color_support),
            bg: resolve_or_inherit(r.bg.as_ref(), ctx.style.bg, ctx.color_support),
            bold: r.bold || ctx.style.bold,
            italic: r.italic || ctx.style.italic,
            underline: r.underline || ctx.style.underline,
            strikethrough: r.strikethrough || ctx.style.strikethrough,
            dim: r.dim || ctx.style.dim,
            blink: r.blink || ctx.style.blink,
        })
        .unwrap_or_else(|| ctx.style.clone());

    scene.allocate_buffer(node_id, width.max(1), 1);

    let mut local_ctx = LayoutCtx {
        x: 0,
        y: 0,
        width,
        viewport_height: ctx.viewport_height,
        color_support: ctx.color_support,
        wcfg: ctx.wcfg,
        style: ctx.style.clone(),
        governor: ctx.governor.clone(),
    };

    let wrapped = if content.is_empty() {
        true
    } else {
        let text = content.trim_matches(|c: char| c == '\t' || c == ' ');
        match scene.layout_buffer_mut(node_id) {
            Some(text_buf) if !text.is_empty() => {
                engine::render_wrapped_text(text_buf, &mut local_ctx, text, &style, data.align)
            }
            _ => true,
        }
    };
    if !wrapped {
        buf.record_allocation_failure();
        scene.record_resource_error();
        return Placement::empty_at(start_x, start_y);
    }

    let h = local_ctx.y;
    ctx.y = start_y.saturating_add(h);

    // Keep page_buf tall enough for downstream consumers (document-mode
    // scroll height, `Compositor` dimensions). Pre-pivot, writes to `buf`
    // grew it via `ensure_height`; post-pivot, content-height bookkeeping
    // still has to happen here until `page.buf` retires in Phase 6.
    buf.ensure_height(ctx.y);

    let rect = Rect::new(start_x, start_y, width, h);
    Placement {
        rect,
        flow_advance: h,
        bbox: rect,
    }
}