azul-layout 0.0.7

Layout solver + font and image loader the Azul GUI framework
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/// Test inline and inline-block elements with gradient backgrounds and borders
/// Verifies that gradient backgrounds and borders are correctly generated in the display list
use azul_core::dom::{Dom, DomId};
use azul_core::geom::{LogicalPosition, LogicalRect, LogicalSize};
use azul_core::resources::RendererResources;
use azul_layout::font::loading::build_font_cache;
use azul_layout::font_traits::{FontManager, TextLayoutCache};
use azul_layout::paged::FragmentationContext;
use azul_layout::solver3::display_list::DisplayListItem;
use azul_layout::solver3::paged_layout::layout_document_paged_with_config;
use azul_layout::solver3::pagination::FakePageConfig;
use azul_layout::text3::default::PathLoader;
use azul_layout::xml::DomXmlExt;
use azul_layout::Solver3LayoutCache;
use std::collections::BTreeMap;

/// Helper function to run layout and return display list items
fn run_layout(html: &str) -> Vec<DisplayListItem> {
    let styled_dom = Dom::from_xml_string(html);

    let fc_cache = build_font_cache();
    let mut font_manager = FontManager::new(fc_cache).expect("Failed to create font manager");

    let mut layout_cache = Solver3LayoutCache {
        tree: None,
        calculated_positions: BTreeMap::new(),
        viewport: None,
        scroll_ids: BTreeMap::new(),
        scroll_id_to_node_id: BTreeMap::new(),
        counters: BTreeMap::new(),
        float_cache: BTreeMap::new(),
        cache_map: Default::default(),
    };
    let mut text_cache = TextLayoutCache::new();

    let content_size = LogicalSize::new(800.0, 600.0);
    let fragmentation_context = FragmentationContext::new_paged(content_size);

    let viewport = LogicalRect {
        origin: LogicalPosition::zero(),
        size: content_size,
    };

    let renderer_resources = RendererResources::default();
    let mut debug_messages = Some(Vec::new());

    let loader = PathLoader::new();
    let font_loader = |bytes: &[u8], index: usize| loader.load_font(bytes, index);
    let page_config = FakePageConfig::new();

    let display_lists = layout_document_paged_with_config(
        &mut layout_cache,
        &mut text_cache,
        fragmentation_context,
        styled_dom,
        viewport,
        &mut font_manager,
        &BTreeMap::new(),
        &BTreeMap::new(),
        &mut debug_messages,
        None,
        &renderer_resources,
        azul_core::resources::IdNamespace(0),
        DomId::ROOT_ID,
        font_loader,
        page_config,
        azul_core::task::GetSystemTimeCallback { cb: azul_core::task::get_system_time_libstd },
    )
    .expect("Layout should succeed");

    display_lists
        .into_iter()
        .flat_map(|dl| dl.items.into_iter())
        .collect()
}

#[test]
fn test_inline_block_with_gradient_background() {
    let html = r#"
    <html>
        <head>
            <style>
                .gradient-box {
                    display: inline-block;
                    width: 200px;
                    height: 100px;
                    background: linear-gradient(to right, red, blue);
                }
            </style>
        </head>
        <body>
            <div class="gradient-box">Gradient content</div>
        </body>
    </html>
    "#;

    let items = run_layout(html);

    // Look for gradient items in the display list
    let gradient_count = items
        .iter()
        .filter(|item| {
            matches!(
                item,
                DisplayListItem::LinearGradient { .. }
                    | DisplayListItem::RadialGradient { .. }
                    | DisplayListItem::ConicGradient { .. }
            )
        })
        .count();

    println!("Display list items:");
    for (i, item) in items.iter().enumerate() {
        println!("  [{}] {:?}", i, std::mem::discriminant(item));
        match item {
            DisplayListItem::LinearGradient { bounds, .. } => {
                println!(
                    "      LinearGradient bounds: {}x{} @ ({}, {})",
                    bounds.size.width, bounds.size.height, bounds.origin.x, bounds.origin.y
                );
            }
            DisplayListItem::Rect { bounds, color, .. } => {
                println!(
                    "      Rect bounds: {}x{} @ ({}, {}), color: rgba({},{},{},{})",
                    bounds.size.width,
                    bounds.size.height,
                    bounds.origin.x,
                    bounds.origin.y,
                    color.r,
                    color.g,
                    color.b,
                    color.a
                );
            }
            _ => {}
        }
    }

    assert!(
        gradient_count >= 1,
        "Should have at least 1 gradient item for the inline-block element, got {}",
        gradient_count
    );
}

#[test]
fn test_inline_block_with_border() {
    let html = r#"
    <html>
        <head>
            <style>
                .bordered-box {
                    display: inline-block;
                    width: 200px;
                    height: 100px;
                    border: 5px solid green;
                    background: white;
                }
            </style>
        </head>
        <body>
            <div class="bordered-box">Bordered content</div>
        </body>
    </html>
    "#;

    let items = run_layout(html);

    // Look for border items in the display list
    let border_count = items
        .iter()
        .filter(|item| matches!(item, DisplayListItem::Border { .. }))
        .count();

    println!("Display list items:");
    for (i, item) in items.iter().enumerate() {
        println!("  [{}] {:?}", i, std::mem::discriminant(item));
        match item {
            DisplayListItem::Border {
                bounds,
                widths,
                colors,
                ..
            } => {
                println!(
                    "      Border bounds: {}x{} @ ({}, {})",
                    bounds.size.width, bounds.size.height, bounds.origin.x, bounds.origin.y
                );
                println!("      Border widths: {:?}", widths);
                println!("      Border colors: {:?}", colors);
            }
            _ => {}
        }
    }

    assert!(
        border_count >= 1,
        "Should have at least 1 border item for the inline-block element, got {}",
        border_count
    );
}

#[test]
fn test_inline_block_with_gradient_and_border() {
    let html = r#"
    <html>
        <head>
            <style>
                .fancy-box {
                    display: inline-block;
                    width: 200px;
                    height: 100px;
                    background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
                    border: 3px solid #333;
                    border-radius: 10px;
                }
            </style>
        </head>
        <body>
            <div class="fancy-box">Fancy content</div>
        </body>
    </html>
    "#;

    let items = run_layout(html);

    let gradient_count = items
        .iter()
        .filter(|item| {
            matches!(
                item,
                DisplayListItem::LinearGradient { .. }
                    | DisplayListItem::RadialGradient { .. }
                    | DisplayListItem::ConicGradient { .. }
            )
        })
        .count();

    let border_count = items
        .iter()
        .filter(|item| matches!(item, DisplayListItem::Border { .. }))
        .count();

    println!("Display list items:");
    for (i, item) in items.iter().enumerate() {
        println!("  [{}] {:?}", i, std::mem::discriminant(item));
    }

    println!("\nGradient items: {}", gradient_count);
    println!("Border items: {}", border_count);

    assert!(
        gradient_count >= 1,
        "Should have at least 1 gradient item, got {}",
        gradient_count
    );

    assert!(
        border_count >= 1,
        "Should have at least 1 border item, got {}",
        border_count
    );
}

#[test]
fn test_inline_element_with_gradient_background() {
    let html = r#"
    <html>
        <head>
            <style>
                .inline-gradient {
                    display: inline;
                    background: linear-gradient(to bottom, yellow, orange);
                    padding: 5px 10px;
                }
            </style>
        </head>
        <body>
            <p>This is <span class="inline-gradient">highlighted text</span> in a paragraph.</p>
        </body>
    </html>
    "#;

    let items = run_layout(html);

    let gradient_count = items
        .iter()
        .filter(|item| {
            matches!(
                item,
                DisplayListItem::LinearGradient { .. }
                    | DisplayListItem::RadialGradient { .. }
                    | DisplayListItem::ConicGradient { .. }
            )
        })
        .count();

    println!("Display list items for inline element with gradient:");
    for (i, item) in items.iter().enumerate() {
        println!("  [{}] {:?}", i, std::mem::discriminant(item));
        match item {
            DisplayListItem::LinearGradient { bounds, .. } => {
                println!(
                    "      LinearGradient bounds: {}x{} @ ({}, {})",
                    bounds.size.width, bounds.size.height, bounds.origin.x, bounds.origin.y
                );
            }
            _ => {}
        }
    }

    assert!(
        gradient_count >= 1,
        "Should have at least 1 gradient item for the inline span element, got {}",
        gradient_count
    );
}

#[test]
fn test_inline_element_with_border() {
    let html = r#"
    <html>
        <head>
            <style>
                .inline-bordered {
                    display: inline;
                    border: 2px dashed purple;
                    padding: 2px 8px;
                }
            </style>
        </head>
        <body>
            <p>This is <span class="inline-bordered">bordered text</span> in a paragraph.</p>
        </body>
    </html>
    "#;

    let items = run_layout(html);

    let border_count = items
        .iter()
        .filter(|item| matches!(item, DisplayListItem::Border { .. }))
        .count();

    println!("Display list items for inline element with border:");
    for (i, item) in items.iter().enumerate() {
        println!("  [{}] {:?}", i, std::mem::discriminant(item));
        match item {
            DisplayListItem::Border {
                bounds,
                widths,
                styles,
                ..
            } => {
                println!(
                    "      Border bounds: {}x{} @ ({}, {})",
                    bounds.size.width, bounds.size.height, bounds.origin.x, bounds.origin.y
                );
                println!("      Border widths: {:?}", widths);
                println!("      Border styles: {:?}", styles);
            }
            _ => {}
        }
    }

    assert!(
        border_count >= 1,
        "Should have at least 1 border item for the inline span element, got {}",
        border_count
    );
}

#[test]
fn test_radial_gradient_on_inline_block() {
    let html = r#"
    <html>
        <head>
            <style>
                .radial-box {
                    display: inline-block;
                    width: 150px;
                    height: 150px;
                    background: radial-gradient(circle, white, black);
                }
            </style>
        </head>
        <body>
            <div class="radial-box">Radial</div>
        </body>
    </html>
    "#;

    let items = run_layout(html);

    let radial_gradient_count = items
        .iter()
        .filter(|item| matches!(item, DisplayListItem::RadialGradient { .. }))
        .count();

    let any_gradient_count = items
        .iter()
        .filter(|item| {
            matches!(
                item,
                DisplayListItem::LinearGradient { .. }
                    | DisplayListItem::RadialGradient { .. }
                    | DisplayListItem::ConicGradient { .. }
            )
        })
        .count();

    println!("Display list items for radial gradient:");
    for (i, item) in items.iter().enumerate() {
        println!("  [{}] {:?}", i, std::mem::discriminant(item));
    }

    println!("\nRadial gradient items: {}", radial_gradient_count);
    println!("Any gradient items: {}", any_gradient_count);

    assert!(
        any_gradient_count >= 1 || radial_gradient_count >= 1,
        "Should have at least 1 gradient item for the radial gradient, got radial={}, any={}",
        radial_gradient_count,
        any_gradient_count
    );
}

#[test]
fn test_multiple_inline_blocks_with_different_borders() {
    let html = r#"
    <html>
        <head>
            <style>
                .box1 {
                    display: inline-block;
                    width: 100px;
                    height: 50px;
                    border: 2px solid red;
                    margin-right: 10px;
                }
                .box2 {
                    display: inline-block;
                    width: 100px;
                    height: 50px;
                    border: 4px dotted blue;
                    margin-right: 10px;
                }
                .box3 {
                    display: inline-block;
                    width: 100px;
                    height: 50px;
                    border: 3px double green;
                }
            </style>
        </head>
        <body>
            <div class="box1">Box 1</div>
            <div class="box2">Box 2</div>
            <div class="box3">Box 3</div>
        </body>
    </html>
    "#;

    let items = run_layout(html);

    let border_count = items
        .iter()
        .filter(|item| matches!(item, DisplayListItem::Border { .. }))
        .count();

    println!("Display list items for multiple bordered boxes:");
    for (i, item) in items.iter().enumerate() {
        match item {
            DisplayListItem::Border {
                bounds,
                widths,
                colors,
                styles,
                ..
            } => {
                println!(
                    "  [{}] Border: {}x{} @ ({}, {}), widths={:?}, styles={:?}, colors={:?}",
                    i,
                    bounds.size.width,
                    bounds.size.height,
                    bounds.origin.x,
                    bounds.origin.y,
                    widths,
                    styles,
                    colors
                );
            }
            _ => {}
        }
    }

    assert!(
        border_count >= 3,
        "Should have at least 3 border items for the 3 boxes, got {}",
        border_count
    );
}

#[test]
fn test_inline_block_gradient_bounds_match_element_size() {
    let html = r#"
    <html>
        <head>
            <style>
                .sized-gradient {
                    display: inline-block;
                    width: 250px;
                    height: 120px;
                    background: linear-gradient(to right, #000, #fff);
                }
            </style>
        </head>
        <body>
            <div class="sized-gradient">Test</div>
        </body>
    </html>
    "#;

    let items = run_layout(html);

    // Find the gradient item
    let gradient_item = items.iter().find(|item| {
        matches!(
            item,
            DisplayListItem::LinearGradient { .. }
                | DisplayListItem::RadialGradient { .. }
                | DisplayListItem::ConicGradient { .. }
        )
    });

    println!("Display list items:");
    for (i, item) in items.iter().enumerate() {
        println!("  [{}] {:?}", i, std::mem::discriminant(item));
        match item {
            DisplayListItem::LinearGradient { bounds, .. } => {
                println!(
                    "      LinearGradient bounds: {}x{}",
                    bounds.size.width, bounds.size.height
                );
            }
            DisplayListItem::Rect { bounds, .. } => {
                println!(
                    "      Rect bounds: {}x{}",
                    bounds.size.width, bounds.size.height
                );
            }
            _ => {}
        }
    }

    if let Some(DisplayListItem::LinearGradient { bounds, .. }) = gradient_item {
        // Gradient bounds should match the element size (250x120)
        assert!(
            (bounds.size.width - 250.0).abs() < 1.0,
            "Gradient width should be 250px, got {}",
            bounds.size.width
        );
        assert!(
            (bounds.size.height - 120.0).abs() < 1.0,
            "Gradient height should be 120px, got {}",
            bounds.size.height
        );
        println!(
            "SUCCESS: Gradient bounds match element size: {}x{}",
            bounds.size.width, bounds.size.height
        );
    } else {
        panic!("Should have a gradient item in the display list");
    }
}