ironpress 1.4.4

Pure Rust HTML/CSS/Markdown to PDF converter with layout engine, LaTeX math, tables, images, custom fonts, and streaming output. No browser, no system dependencies.
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
use super::*;
use crate::layout::elements::Container;
use crate::layout::engine::StackingContext;
use crate::render::pdf::affine_solids::AffineSolidGroup;

fn push_nested_background_clip(
    content: &mut String,
    clip: RoundedRect,
    force_rectangular_clip: bool,
) -> bool {
    if force_rectangular_clip {
        clip.push_clip(content);
        true
    } else {
        clip.push_rounded_clip(content)
    }
}

pub(super) fn render_nested_container(
    content: &mut String,
    child: &Container,
    child_index: usize,
    flow: &ContainerFlowContext<'_>,
    position: FlowPosition,
    abs_origins: &mut HashMap<usize, PdfPoint>,
    ctx: &mut PageRenderContext<'_>,
) -> FlowPosition {
    let x = flow.frame.content_origin.x;
    let width = flow.frame.width();
    let self_pad_origin = flow.frame.padding_origin;
    let container_top_y = flow.container_top_y;
    let flow_top_by_index = flow.flow_top_by_index;
    let float_top_by_index = flow.float_top_by_index;
    let left_float_bottom = flow.left_float_bottom;
    let right_float_bottom = flow.right_float_bottom;
    let device_space_available = flow.device_space_available;
    let phase = flow.paint_phase;
    let FlowPosition {
        y: _,
        mut cursor_y,
        previous_margin_bottom: mut prev_margin_bottom,
    } = position;
    let mut y;
    let nested_kids = &child.children;
    let background_color = &child.paint.background.color;
    let background_gradient = &child.paint.background.layers.gradient;
    let background_radial_gradient = &child.paint.background.layers.radial_gradient;
    let background_conic_gradient = &child.paint.background.layers.conic_gradient;
    let border = &child.box_model.border;
    let padding = &child.box_model.padding;
    let margin_top = &child.box_model.margins.start;
    let margin_bottom = &child.box_model.margins.end;
    let block_width = &child.box_model.size.width;
    let nk_block_height = &child.box_model.size.height;
    let nk_stacking_context = &child.paint.group.effects.stacking_context;
    let nk_bg_blend = &child.paint.background.blend_mode;
    let nk_visible = &child.paint.visible;
    let nk_float = &child.flow.float;
    let nk_clear = &child.flow.clear;
    let overflow = &child.overflow.combined;
    let nk_overflow_x = &child.overflow.x;
    let nk_overflow_y = &child.overflow.y;
    let nk_position = &child.positioning.scheme;
    let nk_offset_top = &child.positioning.insets.top;
    let nk_offset_left = &child.positioning.insets.left;
    let nk_box_transform = &child.paint.group.transform;
    let nk_transform = &nk_box_transform.value;
    let nk_box_shadow = &child.paint.shadows;
    let nk_bg_svg = &child.paint.background.layers.svg;
    let nk_bg_size = &child.paint.background.layers.size;
    let nk_bg_position = &child.paint.background.layers.position;
    let nk_bg_repeat = &child.paint.background.layers.repeat;
    let nk_bg_origin = &child.paint.background.layers.origin;
    let nk_bg_clip = &child.paint.background.layers.clip;
    let nk_bg_blur = &child.paint.background.layers.blur_radius;
    let nk_outline_width = &child.paint.outline.width;
    let nk_outline_color = &child.paint.outline.color;
    let nk_outline_offset = &child.paint.outline.offset;
    let cont_radii = &child.paint.border_radii;
    let nk_positioned_depth = &child.positioning.containing_block_depth;
    let nk_containing_block = &child.positioning.containing_block;
    // Absolute-positioned containers (e.g. an empty position:absolute
    // div) must render at their inset offset from the containing
    // block's padding box, mirroring the TextBlock abspos arm — not
    // in normal flow. Without this, nested abspos boxes rendered at
    // the parent's content-box origin (top/left silently dropped).
    let nk_is_abs = nk_position.is_absolute();
    // In-flow containers collapse their margin-top against the
    // previous in-flow sibling's margin-bottom; floats and absolutes
    // are out of flow and take their margin-top in full.
    let nk_is_float = !nk_is_abs && *nk_float != Float::None;
    let nk_in_flow = !nk_is_abs && !nk_is_float;
    let planned_flow_top = flow_top_by_index.get(&child_index).copied();
    if let Some(top) = planned_flow_top {
        y = top;
    } else if nk_in_flow {
        if *nk_clear != Clear::None {
            cursor_y = clear_cursor(
                cursor_y,
                *nk_clear,
                left_float_bottom,
                right_float_bottom,
                &mut prev_margin_bottom,
            );
        }
        cursor_y -= collapsed_margin_top_extra(*margin_top, prev_margin_bottom);
        y = cursor_y;
    } else if nk_is_float {
        // Floated container: pinned at its precomputed top (the flow
        // cursor at its source position); does not advance the cursor.
        let rel_top = float_top_by_index.get(&child_index).copied().unwrap_or(0.0);
        y = container_top_y - rel_top;
    } else {
        // Absolute: positioned from the container top below.
        y = cursor_y;
    }
    let nk_w = block_width.resolve(width);
    let nk_children_h: f32 = collapsed_children_height(nested_kids);
    let nk_content_h = padding.vertical() + nk_children_h + border.vertical_width();
    let nk_total_h = nk_block_height.resolve(nk_content_h);
    // Absolute Containers anchor to their containing block's padding
    // box (resolved by depth, skipping static intermediates).
    let nk_anchor = abs_child_anchor(nk_containing_block, abs_origins, self_pad_origin);
    let normal_inline_offset = match nk_float {
        Float::Right => width - nk_w,
        _ => 0.0,
    };
    let flow_origin = child.positioning.resolve_in_flow_origin(
        crate::types::Point::new(normal_inline_offset, container_top_y - y),
        crate::types::Size::new(nk_w, nk_total_h),
        flow.frame.size,
    );
    let (nk_x, nk_top_y) = if nk_is_abs {
        (nk_anchor.x + nk_offset_left, nk_anchor.y - nk_offset_top)
    } else {
        (x + flow_origin.x, container_top_y - flow_origin.y)
    };
    // A definite `block_height` (set only for an explicit `height`)
    // is a hard border-box size: per CSS, oversized content overflows
    // the box (clipped or visible per `overflow`) rather than growing
    // it. Honour the declared height directly regardless of `overflow`
    // — only an auto height (`None`) expands to fit children. (The old
    // `content_h.max(h)` for non-hidden overflow wrongly inflated the
    // box to the child height, e.g. an `overflow:visible` box grew to
    // its oversized child instead of letting the child spill out.)
    let nk_geometry = LayoutBoxGeometry::from_layout(
        PdfRect::from_top(nk_x, nk_top_y, nk_w, nk_total_h),
        border,
        *padding,
        child.paint.border_image.as_ref(),
    );
    let nk_box_geometry = ctx.text.pdf_writer.resolve_box_geometry(nk_geometry);
    let nk_paint_geometry = nk_box_geometry.painting();
    let nk_fragment_geometry = nk_box_geometry.fragment(child.fragmentation);
    let nk_border_box = nk_paint_geometry.rounded_border_box(*cont_radii);
    let background_geometry =
        nk_fragment_geometry.background(*nk_bg_origin, *nk_bg_clip, *cont_radii);
    let nk_gradient_reference = background_geometry.positioning_area.generated_image_box();
    let nk_image_reference = background_geometry.positioning_area.intrinsic_image_box();
    let nk_background_clip = background_geometry.painting_box;
    let nk_gradient_area = LayerPaintArea::new(
        nk_gradient_reference,
        background_geometry.image_destination_box,
    );
    let force_background_clip = child.fragmentation.reference_slice.is_some()
        || *nk_bg_clip != BackgroundClip::Border
        || nk_background_clip != nk_border_box;

    // CSS `filter: blur()` on a solid box (css-filter-effects-1 §4.1):
    // rasterize this empty container's bg fill + border, gaussian-blur
    // it, and embed it overflowing the border box. Restricted to a
    // plain solid box (no children, no gradient/SVG bg, no
    // transform/opacity/clip/mask wrapper, square corners) so the
    // vector paint path is byte-unchanged for everything else.
    if phase == ElementPaintPhase::All
        && *nk_visible
        && *nk_bg_blur > 0.0
        && nested_kids.is_empty()
        && background_gradient.is_none()
        && background_radial_gradient.is_none()
        && background_conic_gradient.is_none()
        && nk_bg_svg.is_none()
        && nk_transform.is_none()
        && cont_radii.is_zero()
        && *nk_outline_width == 0.0
        && let Some(blurred) = crate::render::blur::blur_box(
            nk_paint_geometry.border_box.width,
            nk_paint_geometry.border_box.height,
            *background_color,
            border,
            *nk_bg_blur,
            ctx.text.pdf_writer.opts.raster_quality.filter_dpi,
        )
    {
        let group = PaintGroupScope::begin(content, child, nk_fragment_geometry, ctx);
        let ov = blurred.overflow_pt;
        let img_obj_id = ctx.text.pdf_writer.add_image_object(
            &blurred.asset.data,
            blurred.asset.source_width,
            blurred.asset.source_height,
            blurred.asset.format,
            blurred.asset.png_metadata.as_ref(),
        );
        let img_name = format!("Im{img_obj_id}");
        content.push_str(&format!(
            "q\n{w} 0 0 {h} {ix} {iy} cm\n/{name} Do\nQ\n",
            w = nk_paint_geometry.border_box.width + 2.0 * ov,
            h = nk_paint_geometry.border_box.height + 2.0 * ov,
            ix = nk_paint_geometry.border_box.left - ov,
            iy = nk_paint_geometry.border_box.bottom - ov,
            name = img_name,
        ));
        ctx.text.page_images.push(ImageRef {
            name: img_name,
            obj_id: img_obj_id,
        });
        group.finish(content, ctx);
        // Advance the flow cursor exactly as the normal container path
        // (the filter does not change layout).
        if nk_is_float {
            prev_margin_bottom = 0.0;
        } else if planned_flow_top.is_some() {
            prev_margin_bottom = *margin_bottom;
        } else if !nk_is_abs {
            cursor_y -= nk_total_h + margin_bottom;
            y = cursor_y;
            prev_margin_bottom = *margin_bottom;
        }
        return FlowPosition::new(y, cursor_y, prev_margin_bottom);
    }

    // `visibility: hidden` keeps the box's space (cursor still
    // advances below). Per CSS2 §11.2 it suppresses only THIS box's own
    // painting — a `visibility: visible` descendant must still render —
    // so the subtree (wrappers + children) is always emitted; the
    // box's own decoration is gated on `nk_visible` further down.
    {
        if phase == ElementPaintPhase::All
            && *nk_visible
            && device_space_available
            && let Some(t) = nk_transform
            && !is_projected_transform(t)
            && background_gradient.is_none()
            && background_radial_gradient.is_none()
            && background_conic_gradient.is_none()
            && nk_bg_svg.is_none()
            && *nk_bg_blur == 0.0
            && cont_radii.is_zero()
            && *nk_outline_width == 0.0
            && nk_box_shadow.is_empty()
            && child.paint.group.effects.is_identity()
            && !overflow.clips()
            && {
                render_affine_solid_group(
                    content,
                    ctx.text.pdf_writer,
                    ctx.text.page_images,
                    AffineSolidGroup {
                        transform: nk_box_transform,
                        geometry: nk_paint_geometry,
                        background: *background_color,
                        border,
                        children: nested_kids,
                    },
                )
            }
        {
            if nk_is_float {
                prev_margin_bottom = 0.0;
            } else if planned_flow_top.is_some() {
                prev_margin_bottom = *margin_bottom;
            } else if !nk_is_abs {
                cursor_y -= nk_total_h + margin_bottom;
                y = cursor_y;
                prev_margin_bottom = *margin_bottom;
            }
            return FlowPosition::new(y, cursor_y, prev_margin_bottom);
        }
        if phase == ElementPaintPhase::All
            && *nk_visible
            && device_space_available
            && let Some(t) = nk_transform
            && !is_projected_transform(t)
            && projected_solid_children_are_empty(nested_kids)
            && background_gradient.is_none()
            && background_radial_gradient.is_none()
            && background_conic_gradient.is_none()
            && nk_bg_svg.is_none()
            && *nk_bg_blur == 0.0
            && cont_radii.is_zero()
            && *nk_outline_width == 0.0
            && nk_box_shadow.is_empty()
            && child.paint.group.effects.is_identity()
            && !overflow.clips()
            && {
                render_affine_solid_box(
                    content,
                    ctx.text.pdf_writer,
                    ctx.text.page_images,
                    nk_box_transform,
                    nk_paint_geometry,
                    *background_color,
                    border,
                )
            }
        {
            if nk_is_float {
                prev_margin_bottom = 0.0;
            } else if planned_flow_top.is_some() {
                prev_margin_bottom = *margin_bottom;
            } else if !nk_is_abs {
                cursor_y -= nk_total_h + margin_bottom;
                y = cursor_y;
                prev_margin_bottom = *margin_bottom;
            }
            return FlowPosition::new(y, cursor_y, prev_margin_bottom);
        }
        if phase == ElementPaintPhase::All
            && *nk_visible
            && let Some(t) = nk_transform
            && is_projected_transform(t)
            && projected_solid_children_are_empty(nested_kids)
            && background_gradient.is_none()
            && background_radial_gradient.is_none()
            && background_conic_gradient.is_none()
            && nk_bg_svg.is_none()
            && *nk_bg_blur == 0.0
            && cont_radii.is_zero()
            && *nk_outline_width == 0.0
            && nk_box_shadow.is_empty()
            && child.paint.group.effects.is_identity()
            && *nk_bg_blend == crate::style::computed::BlendMode::Normal
            && *nk_bg_clip == BackgroundClip::Border
            && !overflow.clips()
        {
            render_projected_solid_box(
                content,
                ctx.text.pdf_writer.page_content_transform,
                nk_box_transform,
                nk_paint_geometry,
                *background_color,
                border,
            );
            if nk_is_float {
                prev_margin_bottom = 0.0;
            } else if planned_flow_top.is_some() {
                prev_margin_bottom = *margin_bottom;
            } else if !nk_is_abs {
                cursor_y -= nk_total_h + margin_bottom;
                y = cursor_y;
                prev_margin_bottom = *margin_bottom;
            }
            return FlowPosition::new(y, cursor_y, prev_margin_bottom);
        }
        let nk_group = PaintGroupScope::begin(content, child, nk_fragment_geometry, ctx);

        // CSS2 §11.2: self-decoration (background / border / outline /
        // shadow) is suppressed when this box is `visibility: hidden`,
        // but the opacity/transform/clip wrappers and the children
        // (which may override back to `visible`) are still emitted.
        if phase.paints_decoration() && *nk_visible {
            // Draw outset box-shadow (before the background, so it sits
            // behind the element). Nested containers previously dropped
            // box-shadow entirely; the top-level Container arm handles it
            // the same way.
            render_box_shadows(
                content,
                nk_box_shadow,
                nk_fragment_geometry,
                *cont_radii,
                ctx.page_ext_gstates,
                ctx.bg_alpha_counter,
                ctx.text.pdf_writer,
            );

            // Draw the solid layer through the same resolved background
            // geometry used by gradients and images. In particular, this
            // preserves the paint-only bleed inset below opaque rounded
            // borders for nested boxes instead of reverting to the raw
            // border box.
            if let Some(color) = background_color {
                paint_solid_background(
                    content,
                    *color,
                    nk_background_clip,
                    ctx.page_ext_gstates,
                    ctx.bg_alpha_counter,
                );
            }

            // `background-blend-mode`: the background image layers (gradient /
            // SVG) blend against the background color painted above. Scope the
            // blend gstate to a `q`..`Q` around each background-image paint.
            let nk_bg_blend_mode = nk_bg_blend.background_layer(0);
            let nk_bg_blended = nk_bg_blend_mode != crate::style::computed::BlendMode::Normal;
            let nk_layer_box = background_layer_box(*nk_bg_size, *nk_bg_position, *nk_bg_repeat);

            // Draw linear gradient
            if let Some(gradient) = background_gradient {
                let gradient = linear_with_background_layer(gradient, nk_layer_box);
                if nk_bg_blended {
                    content.push_str("q\n");
                    begin_blend_mode(content, ctx.page_ext_gstates, nk_bg_blend_mode);
                }
                let rounded_clip =
                    push_nested_background_clip(content, nk_background_clip, force_background_clip);
                render_linear_gradient(
                    content,
                    &gradient,
                    GradientBackdrop::isolated_linear_layer(
                        *background_color,
                        background_radial_gradient.is_some()
                            || background_conic_gradient.is_some()
                            || nk_bg_svg.is_some(),
                        nk_bg_blend_mode,
                    ),
                    nk_gradient_area,
                    ctx.shadings,
                    ctx.shading_counter,
                    ctx.text.pdf_writer,
                    ctx.text.page_images,
                );
                if rounded_clip {
                    content.push_str("Q\n");
                }
                if nk_bg_blended {
                    content.push_str("Q\n");
                }
            }

            // Draw radial gradient
            if let Some(gradient) = background_radial_gradient {
                let gradient = radial_with_background_layer(gradient, nk_layer_box);
                if nk_bg_blended {
                    content.push_str("q\n");
                    begin_blend_mode(content, ctx.page_ext_gstates, nk_bg_blend_mode);
                }
                let rounded_clip =
                    push_nested_background_clip(content, nk_background_clip, force_background_clip);
                render_radial_gradient(
                    content,
                    &gradient,
                    nk_gradient_area,
                    ctx.shadings,
                    ctx.shading_counter,
                    ctx.text.pdf_writer,
                    ctx.text.page_images,
                );
                if rounded_clip {
                    content.push_str("Q\n");
                }
                if nk_bg_blended {
                    content.push_str("Q\n");
                }
            }

            // Draw conic gradient
            if let Some(gradient) = background_conic_gradient {
                let gradient = conic_with_background_layer(gradient, nk_layer_box);
                if nk_bg_blended {
                    content.push_str("q\n");
                    begin_blend_mode(content, ctx.page_ext_gstates, nk_bg_blend_mode);
                }
                let rounded_clip =
                    push_nested_background_clip(content, nk_background_clip, force_background_clip);
                render_conic_gradient(
                    content,
                    &gradient,
                    nk_gradient_area,
                    ctx.text.pdf_writer,
                    ctx.text.page_images,
                );
                if rounded_clip {
                    content.push_str("Q\n");
                }
                if nk_bg_blended {
                    content.push_str("Q\n");
                }
            }

            // Draw SVG background image if specified
            if let Some(svg_tree) = nk_bg_svg {
                if nk_bg_blended {
                    content.push_str("q\n");
                    begin_blend_mode(content, ctx.page_ext_gstates, nk_bg_blend_mode);
                }
                render_svg_background(
                    content,
                    svg_tree,
                    PdfBackgroundResources::new(
                        ctx.text.pdf_writer,
                        ctx.text.page_images,
                        ctx.shadings,
                        ctx.shading_counter,
                        Some(ctx.page_ext_gstates),
                    ),
                    PdfBackgroundPaintContext::local(BackgroundPaintContext::new(
                        nk_image_reference.into(),
                        background_geometry.image_destination_box.into(),
                        nk_background_clip.radii,
                        *nk_bg_blur,
                        *nk_bg_size,
                        *nk_bg_position,
                        *nk_bg_repeat,
                    )),
                );
                if nk_bg_blended {
                    content.push_str("Q\n");
                }
            }

            // Draw inset box-shadow (after the backgrounds, before the
            // borders/content) so it paints over the element fill.
            render_box_shadows_inset(
                content,
                nk_box_shadow,
                nk_fragment_geometry,
                *cont_radii,
                ctx.page_ext_gstates,
                ctx.bg_alpha_counter,
                ctx.text.pdf_writer,
            );

            if border.has_visible() || child.paint.border_image.is_some() {
                paint_box_decoration(
                    content,
                    nk_fragment_geometry,
                    border,
                    *cont_radii,
                    child.paint.border_image.as_ref(),
                    BorderPaintResources::from_page(ctx),
                );
            }

            // Draw outline if specified (a uniform stroke outside the
            // border box). `outline-offset` widens the gap between the
            // border edge and the outline; the stroke centerline sits half
            // the outline width beyond the offset edge so the outline stays
            // entirely outside the box. Mirrors the TextBlock outline arm.
            if *nk_outline_width > 0.0 {
                let gap = *nk_outline_offset + *nk_outline_width / 2.0;
                let (or, og, ob) = nk_outline_color
                    .unwrap_or(crate::types::Color::BLACK)
                    .to_f32_rgb();
                content.push_str(&format!(
                    "{or} {og} {ob} RG\n{ow} w\n",
                    ow = nk_outline_width
                ));
                content.push_str(
                    &nk_paint_geometry
                        .border_box
                        .outset_uniform(gap)
                        .rounded(cont_radii.grow(gap))
                        .path_or_rect(),
                );
                content.push_str("S\n");
            }
        } // end `if *nk_visible` — nested container self-decoration

        // Decide print scrollbars (css-overflow-3): a `scroll` axis
        // always reserves a gutter and paints a (non-interactive)
        // scrollbar; an `auto` axis does so only when its content
        // overflows. Chrome renders these in print, insetting the
        // content clip by the gutter on each scrolling axis.
        let padding_box = nk_geometry.padding_box();
        let paint_padding_box = nk_paint_geometry.padding_box();
        let content_box = nk_geometry.content_box();
        let content_avail_w = content_box.width;
        let content_avail_h = content_box.height;
        let (over_w, over_h) = children_overflow_extent(nested_kids);
        let over_ratio_h = if content_avail_w > 0.0 {
            over_w / content_avail_w
        } else {
            0.0
        };
        let over_ratio_v = if content_avail_h > 0.0 {
            over_h / content_avail_h
        } else {
            0.0
        };
        // No rounded scrollbars: a rounded box clips its scrollbar
        // chrome away, so only paint on square scroll containers.
        let scroll_ok = cont_radii.is_zero();
        let has_v = scroll_ok
            && match nk_overflow_y {
                Overflow::Scroll => true,
                Overflow::Auto => over_ratio_v > 1.001,
                _ => false,
            };
        let has_h = scroll_ok
            && match nk_overflow_x {
                Overflow::Scroll => true,
                Overflow::Auto => over_ratio_h > 1.001,
                _ => false,
            };
        let sb = SCROLLBAR_THICKNESS_PT;
        let v_gutter = if has_v { sb } else { 0.0 };
        let h_gutter = if has_h { sb } else { 0.0 };
        let scrollport_w = (content_avail_w - v_gutter).max(0.0);
        let scrollport_h = (content_avail_h - h_gutter).max(0.0);
        let thumb_ratio_h = if scrollport_w > 0.0 {
            over_w / scrollport_w
        } else {
            over_ratio_h
        };
        let thumb_ratio_v = if scrollport_h > 0.0 {
            over_h / scrollport_h
        } else {
            over_ratio_v
        };

        // Clip if overflow clips (hidden/clip/scroll/auto). CSS clips
        // at the PADDING box (border box inset by the border widths)
        // and follows the rounded corners when border-radius is set.
        // Scroll containers inset the clip by the reserved gutter so
        // content does not paint under the scrollbar.
        let clip = overflow.clips();
        let content_clip = clip.then(|| {
            let path = if has_v || has_h {
                // Rectangular clip inset by the per-side border and the
                // reserved gutter (right gutter for vertical, bottom for
                // horizontal — matching the LTR/top-anchored UA layout).
                let scroll_clip = paint_padding_box.inset(EdgeSizes {
                    right: v_gutter,
                    bottom: h_gutter,
                    ..Default::default()
                });
                scroll_clip.rect_path()
            } else {
                nk_paint_geometry
                    .rounded_padding_box(*cont_radii)
                    .path_or_rect()
            };
            ContentClip::from_path(path)
        });
        if let Some(clip) = &content_clip {
            clip.begin(content, &mut ctx.stacking);
        }

        // Recurse into nested children
        let inner_x = content_box.left;
        let inner_w = content_box.width;
        let inner_y = content_box.top();
        // Record this box's padding-box origin keyed by its
        // positioned depth so absolutely-positioned descendants nested
        // inside static intermediates anchor here (their CB), not to
        // the static container they are physically nested in.
        if *nk_positioned_depth > 0
            && (nk_position.is_positioned()
                || nk_transform.is_some()
                || *nk_stacking_context == StackingContext::Filter)
        {
            abs_origins.insert(
                *nk_positioned_depth,
                PdfPoint::new(padding_box.left, padding_box.top()),
            );
        }
        render_container_children(
            content,
            nested_kids,
            ContainerFrame::new(
                PdfPoint::new(inner_x, inner_y),
                crate::types::Size::new(inner_w, content_box.height),
                PdfPoint::new(padding_box.left, padding_box.top()),
            ),
            abs_origins,
            ctx,
            ContainerRenderOptions {
                device_space_available: device_space_available && nk_transform.is_none(),
                paint_phase: phase,
                stacking_scope: StackingScope::for_element(child),
            },
        );

        if let Some(clip) = &content_clip {
            clip.finish(content, &mut ctx.stacking);
        }

        // Paint the print scrollbar chrome in the reserved gutter,
        // AFTER the content clip is closed (the gutter lies outside
        // the inset content clip) but inside the box decoration group.
        if phase.paints_decoration() && (has_v || has_h) {
            paint_scrollbars(
                content,
                paint_padding_box.left,
                paint_padding_box.bottom,
                paint_padding_box.width,
                paint_padding_box.height,
                has_v,
                has_h,
                thumb_ratio_v.max(1.0),
                thumb_ratio_h.max(1.0),
            );
        }
        nk_group.finish(content, ctx);
    } // end nested-container subtree (wrappers + children)
    // Out-of-flow containers (absolute / float) don't advance the
    // flow cursor. A float's bottom is tracked via the simulator for
    // later `clear` siblings; it breaks the margin-collapse chain.
    if nk_is_float {
        prev_margin_bottom = 0.0;
    } else if planned_flow_top.is_some() {
        prev_margin_bottom = *margin_bottom;
    } else if !nk_is_abs {
        cursor_y -= nk_total_h + margin_bottom;
        y = cursor_y;
        // Remember this in-flow block's margin-bottom so the next
        // sibling collapses against it; floats don't collapse.
        prev_margin_bottom = *margin_bottom;
    }

    FlowPosition::new(y, cursor_y, prev_margin_bottom)
}