rhwp 0.9.1

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
//! Derived recording geometry, deliberately separate from layout/hit-test bounds.

use std::collections::HashMap;

use super::paint_op::{
    ColorGlyphFormat, ColorPaintGraphNodeKind, ColorPaintGraphPayload, GlyphOutlinePayloadKind,
    LayerAffineTransform, LayerGlyphOutlinePaint, PaintBounds, PaintOp, TextDecorationKind,
};
use super::{LayerNode, LayerNodeKind, PageLayerTree, MAX_POSITIONED_CONTROL_MARKS_PER_RUN};
use crate::model::style::UnderlineType;
use crate::renderer::composer::expand_pua_render_text_in_style;
use crate::renderer::render_tree::{BoundingBox, ShapeTransform, TextRunNode};
use crate::renderer::{
    clamp_tab_leader_end_x, svg_arc_to_beziers, LineRenderType, LineStyle, PathCommand, ShadowStyle,
};

impl PaintOp {
    /// Returns logical bounds unchanged and an estimate of visible page-space ink.
    ///
    /// This is derived on every call: public mutable PaintOps cannot leave a stale
    /// recording rectangle behind. Text padding preserves the old Skia estimate;
    /// it does not replace font measurement, source geometry or authored clips.
    pub fn paint_bounds(&self) -> PaintBounds {
        let logical = self.bounds();
        let visual = match self {
            Self::PageBackground { background, .. } => {
                expand(logical, background.border_width * 0.5)
            }
            Self::TextRun { run, .. } => text_bounds(logical, run),
            Self::GlyphRun { run, .. } => {
                let size = run.paint_style.font_size.max(1.0);
                let padding = size
                    .max(run.paint_style.shadow_offset_x.abs())
                    .max(run.paint_style.shadow_offset_y.abs())
                    * 0.2;
                let mut bounds = expand(logical, padding);
                // 줄 방향을 해소한 glyph는 논리 런 밖에도 놓일 수 있다.
                for position in &run.positions {
                    if position.x.is_finite() && position.y.is_finite() {
                        bounds = union(
                            bounds,
                            affine_bounds(
                                BoundingBox::new(
                                    position.x - size * 0.2,
                                    position.y - size,
                                    size * 1.4,
                                    size * 1.4,
                                ),
                                Some(run.placement.run_to_page),
                            ),
                        );
                    }
                }
                bounds
            }
            Self::GlyphOutline { outline, .. } => {
                let padding = outline
                    .paint_style
                    .font_size
                    .max(1.0)
                    .max(outline.paint_style.shadow_offset_x.abs())
                    .max(outline.paint_style.shadow_offset_y.abs())
                    * 0.2;
                let mut bounds = expand(
                    logical,
                    padding.max(
                        outline
                            .stroke
                            .as_ref()
                            .map_or(0.0, |stroke| stroke.width * 0.5),
                    ),
                );
                if let Some(paths) = glyph_outline_bounds(outline) {
                    bounds = union(bounds, paths);
                }
                // Bitmap placement and fontEmBox are already page-space. Applying
                // run_to_page here would translate a positioned font glyph twice.
                if let Some(bitmap) = &outline.bitmap_glyph {
                    bounds = union(
                        bounds,
                        affine_bounds(bitmap.placement, bitmap.transform_to_run),
                    );
                }
                if let Some(svg) = &outline.svg_glyph {
                    bounds = union(
                        bounds,
                        affine_bounds(svg.font_em_box.unwrap_or(logical), svg.transform_to_run),
                    );
                }
                bounds
            }
            Self::CharOverlap { run, .. } => {
                if let Some(visual) = crate::renderer::char_overlap::ink_bounds(run, logical) {
                    return PaintBounds { logical, visual };
                }
                let padding = run
                    .style
                    .font_size
                    .max(1.0)
                    .max(run.style.shadow_offset_x.abs())
                    .max(run.style.shadow_offset_y.abs())
                    * 0.15;
                rotate_text(expand(logical, padding), logical, run)
            }
            Self::TextControlMark { run, .. } => control_mark_bounds(logical, run),
            Self::TabLeader { run, .. } => tab_leader_bounds(logical, run),
            Self::TextDecoration {
                run,
                kind,
                trim_trailing_spaces,
                ..
            } => {
                let (bbox, run) = self.decoration_replay().expect("decoration replay");
                union(
                    logical,
                    decoration_bounds(bbox, &run, *kind, *trim_trailing_spaces),
                )
            }
            Self::FootnoteMarker { marker, .. } => expand(logical, marker.base_font_size * 0.15),
            Self::Line { line, .. } => {
                let base = arrow_bounds(
                    union(logical, line.ink_bbox()),
                    crate::renderer::arrow::line_heads(
                        (line.x1, line.y1, line.x2, line.y2),
                        &line.style,
                    ),
                    line.style.width,
                );
                let bounds = with_shadow(
                    expand(base, line_padding(&line.style)),
                    line.style.shadow.as_ref(),
                );
                shape_bounds(bounds, logical, line.transform)
            }
            Self::Rectangle { rect, .. } => shape_bounds(
                with_shadow(
                    expand(logical, rect.style.stroke_width * 0.5),
                    rect.style.shadow.as_ref(),
                ),
                logical,
                rect.transform,
            ),
            Self::Ellipse { ellipse, .. } => shape_bounds(
                with_shadow(
                    expand(logical, ellipse.style.stroke_width * 0.5),
                    ellipse.style.shadow.as_ref(),
                ),
                logical,
                ellipse.transform,
            ),
            Self::Path { path, .. } => {
                let padding = (path.style.stroke_width * 0.5)
                    .max(path.line_style.as_ref().map_or(0.0, line_padding));
                let base = arrow_bounds(
                    logical,
                    crate::renderer::arrow::connector_heads(path),
                    path.line_style.as_ref().map_or(0.0, |style| style.width),
                );
                let bounds = with_shadow(expand(base, padding), path.style.shadow.as_ref());
                let bounds = with_shadow(
                    bounds,
                    path.line_style
                        .as_ref()
                        .and_then(|style| style.shadow.as_ref()),
                );
                shape_bounds(bounds, logical, path.transform)
            }
            Self::Image { image, .. } => {
                let effective = image.transformed_paint_bbox(&logical);
                shape_bounds(effective, effective, image.transform)
            }
            Self::ControlLabel { .. }
            | Self::Equation { .. }
            | Self::FormObject { .. }
            | Self::Placeholder { .. }
            | Self::RawSvg { .. } => logical,
        };
        let visual = affine_bounds(visual, self.glyph_paint_transform());
        PaintBounds { logical, visual }
    }

    pub fn visual_bounds(&self) -> BoundingBox {
        self.paint_bounds().visual
    }
}

impl LayerNode {
    /// Unions descendant paint estimates without changing this node's layout box.
    /// `clip_enabled` applies the producer's exact clips, with no inferred slop.
    /// An empty/fully clipped subtree has a zero-size visual extent.
    pub fn paint_bounds(&self, clip_enabled: bool) -> PaintBounds {
        PaintBounds {
            logical: self.bounds,
            visual: self.visual_bounds(clip_enabled),
        }
    }

    pub fn visual_bounds(&self, clip_enabled: bool) -> BoundingBox {
        collect_visual_bounds(self, clip_enabled, &mut |_, _| {}).unwrap_or_default()
    }
}

impl PageLayerTree {
    /// Uses this export's clip option, not an implicit backend clip policy.
    pub fn paint_bounds(&self) -> PaintBounds {
        self.root.paint_bounds(self.output_options.clip_enabled)
    }

    pub fn visual_bounds(&self) -> BoundingBox {
        self.paint_bounds().visual
    }

    /// Build once per export. Recomputing each group recursively while writing
    /// its children would be quadratic for nested groups. Pointer keys are valid
    /// only for this immutable borrow and are never serialized or retained.
    pub(super) fn visual_bounds_index(&self) -> HashMap<*const LayerNode, BoundingBox> {
        let mut bounds = HashMap::new();
        collect_visual_bounds(
            &self.root,
            self.output_options.clip_enabled,
            &mut |node, bbox| {
                if let Some(bbox) = bbox.filter(|bbox| bbox.width > 0.0 && bbox.height > 0.0) {
                    bounds.insert(node, bbox);
                }
            },
        );
        bounds
    }
}

fn collect_visual_bounds(
    node: &LayerNode,
    clip_enabled: bool,
    visit: &mut impl FnMut(*const LayerNode, Option<BoundingBox>),
) -> Option<BoundingBox> {
    let bounds = match &node.kind {
        LayerNodeKind::Leaf { ops } => ops
            .iter()
            .map(PaintOp::visual_bounds)
            .filter(|bounds| valid(*bounds))
            .reduce(union),
        LayerNodeKind::Group {
            children,
            group_kind,
            ..
        } => {
            let bounds = children
                .iter()
                .filter_map(|child| collect_visual_bounds(child, clip_enabled, visit))
                .reduce(union);
            bounds.map(|bounds| {
                let transform = match group_kind {
                    super::layer_tree::GroupKind::Group(g) => g
                        .paint_transform
                        .map(|[a, b, c, d, e, f]| LayerAffineTransform { a, b, c, d, e, f }),
                    _ => None,
                };
                affine_bounds(bounds, transform)
            })
        }
        // A conservative visual hull must not pretend that a curved clip is a rectangle.
        LayerNodeKind::ClipPath { child, .. } => collect_visual_bounds(child, clip_enabled, visit),
        LayerNodeKind::ClipRect { clip, child, .. } => {
            let child = collect_visual_bounds(child, clip_enabled, visit);
            if clip_enabled {
                child.and_then(|child| intersection(child, *clip))
            } else {
                child
            }
        }
    };
    visit(node, bounds);
    bounds
}

fn valid(bounds: BoundingBox) -> bool {
    [
        bounds.x,
        bounds.y,
        bounds.width,
        bounds.height,
        bounds.x + bounds.width,
        bounds.y + bounds.height,
    ]
    .into_iter()
    .all(f64::is_finite)
        && bounds.width >= 0.0
        && bounds.height >= 0.0
}

fn expand(bounds: BoundingBox, amount: f64) -> BoundingBox {
    if !amount.is_finite() || amount <= 0.0 {
        return bounds;
    }
    let expanded = BoundingBox::new(
        bounds.x - amount,
        bounds.y - amount,
        bounds.width + amount * 2.0,
        bounds.height + amount * 2.0,
    );
    if valid(expanded) {
        expanded
    } else {
        bounds
    }
}

fn union(a: BoundingBox, b: BoundingBox) -> BoundingBox {
    if !valid(a) {
        return b;
    }
    if !valid(b) {
        return a;
    }
    let x = a.x.min(b.x);
    let y = a.y.min(b.y);
    let result = BoundingBox::new(
        x,
        y,
        (a.x + a.width).max(b.x + b.width) - x,
        (a.y + a.height).max(b.y + b.height) - y,
    );
    if valid(result) {
        result
    } else {
        a
    }
}

fn intersection(a: BoundingBox, b: BoundingBox) -> Option<BoundingBox> {
    if !valid(a) || !valid(b) {
        return None;
    }
    let x = a.x.max(b.x);
    let y = a.y.max(b.y);
    let right = (a.x + a.width).min(b.x + b.width);
    let bottom = (a.y + a.height).min(b.y + b.height);
    (right > x && bottom > y).then(|| BoundingBox::new(x, y, right - x, bottom - y))
}

fn with_shadow(bounds: BoundingBox, shadow: Option<&ShadowStyle>) -> BoundingBox {
    let Some(shadow) = shadow else {
        return bounds;
    };
    let offset = BoundingBox::new(
        bounds.x + shadow.offset_x,
        bounds.y + shadow.offset_y,
        bounds.width,
        bounds.height,
    );
    union(
        bounds,
        expand(offset, shadow.offset_x.abs().max(shadow.offset_y.abs())),
    )
}

fn line_padding(style: &LineStyle) -> f64 {
    let mut padding = style.width.max(0.0) * 0.5;
    if style.line_type != LineRenderType::Single {
        padding = padding.max(style.width.max(1.0) * 2.0);
    }
    padding
}

// Control points contain the cubic outline's convex hull. Reuse the same
// positioned arrow geometry as each backend rather than estimating by size code.
fn arrow_bounds(
    mut bounds: BoundingBox,
    heads: Vec<crate::renderer::arrow::ArrowHead>,
    stroke: f64,
) -> BoundingBox {
    for head in heads {
        for command in head.commands {
            let mut include = |x, y| {
                bounds = union(
                    bounds,
                    expand(BoundingBox::new(x, y, 0.0, 0.0), stroke.max(0.5) * 0.5),
                );
            };
            match command {
                PathCommand::MoveTo(x, y) | PathCommand::LineTo(x, y) => include(x, y),
                PathCommand::CurveTo(x1, y1, x2, y2, x, y) => {
                    include(x1, y1);
                    include(x2, y2);
                    include(x, y);
                }
                PathCommand::ClosePath => {}
                PathCommand::ArcTo(..) => unreachable!("shared arrows use cubic outlines"),
            }
        }
    }
    bounds
}

fn affine_bounds(bounds: BoundingBox, transform: Option<LayerAffineTransform>) -> BoundingBox {
    let Some(t) = transform else {
        return bounds;
    };
    checked_affine_bounds(bounds, t).unwrap_or(bounds)
}

fn checked_affine_bounds(bounds: BoundingBox, t: LayerAffineTransform) -> Option<BoundingBox> {
    let (mut left, mut top) = (f64::INFINITY, f64::INFINITY);
    let (mut right, mut bottom) = (f64::NEG_INFINITY, f64::NEG_INFINITY);
    for (x, y) in [
        (bounds.x, bounds.y),
        (bounds.x + bounds.width, bounds.y),
        (bounds.x, bounds.y + bounds.height),
        (bounds.x + bounds.width, bounds.y + bounds.height),
    ] {
        let point = BoundingBox::new(t.a * x + t.c * y + t.e, t.b * x + t.d * y + t.f, 0.0, 0.0);
        if !valid(point) {
            return None;
        }
        left = left.min(point.x);
        top = top.min(point.y);
        right = right.max(point.x);
        bottom = bottom.max(point.y);
    }
    let result = BoundingBox::new(left, top, right - left, bottom - top);
    valid(result).then_some(result)
}

// Canvas concatenation applies the child matrix before its enclosing matrix.
fn concat_affine(
    parent: LayerAffineTransform,
    child: LayerAffineTransform,
) -> LayerAffineTransform {
    LayerAffineTransform {
        a: parent.a * child.a + parent.c * child.b,
        b: parent.b * child.a + parent.d * child.b,
        c: parent.a * child.c + parent.c * child.d,
        d: parent.b * child.c + parent.d * child.d,
        e: parent.a * child.e + parent.c * child.f + parent.e,
        f: parent.b * child.e + parent.d * child.f + parent.f,
    }
}

/// Outward-rounded f32 arithmetic also encloses fused multiply/add and wider
/// intermediates. Keep ranges through concatenation: rounding only the final
/// point misses cancellation between large, individually rounded matrices.
#[derive(Clone, Copy)]
struct F32Range {
    min: f64,
    max: f64,
}

impl F32Range {
    fn scalar(value: f64) -> Self {
        let value = f64::from(value as f32);
        Self {
            min: value,
            max: value,
        }
    }

    fn rounded(min: f64, max: f64) -> Self {
        let mut lower = min as f32;
        let mut upper = max as f32;
        if !min.is_finite() || !max.is_finite() || !lower.is_finite() || !upper.is_finite() {
            return Self {
                min: f64::NAN,
                max: f64::NAN,
            };
        }
        if f64::from(lower) > min {
            lower = lower.next_down();
        }
        if f64::from(upper) < max {
            upper = upper.next_up();
        }
        Self {
            min: f64::from(lower),
            max: f64::from(upper),
        }
    }

    fn add(self, other: Self) -> Self {
        Self::rounded(self.min + other.min, self.max + other.max)
    }

    fn mul(self, other: Self) -> Self {
        let products = [
            self.min * other.min,
            self.min * other.max,
            self.max * other.min,
            self.max * other.max,
        ];
        if products.iter().any(|value| !value.is_finite()) {
            return Self {
                min: f64::NAN,
                max: f64::NAN,
            };
        }
        Self::rounded(
            products.into_iter().fold(f64::INFINITY, f64::min),
            products.into_iter().fold(f64::NEG_INFINITY, f64::max),
        )
    }
}

type F32Affine = [[F32Range; 3]; 2];

fn f32_affine(t: LayerAffineTransform) -> F32Affine {
    [[t.a, t.c, t.e], [t.b, t.d, t.f]].map(|row| row.map(F32Range::scalar))
}

fn concat_f32_affine(parent: F32Affine, child: F32Affine) -> F32Affine {
    std::array::from_fn(|row| {
        std::array::from_fn(|column| {
            let value = parent[row][0]
                .mul(child[0][column])
                .add(parent[row][1].mul(child[1][column]));
            if column == 2 {
                value.add(parent[row][2])
            } else {
                value
            }
        })
    })
}

fn f32_affine_bounds(bounds: BoundingBox, transform: F32Affine) -> Option<BoundingBox> {
    let x = F32Range::rounded(bounds.x, bounds.x + bounds.width);
    let y = F32Range::rounded(bounds.y, bounds.y + bounds.height);
    let [x, y] = transform.map(|row| row[0].mul(x).add(row[1].mul(y)).add(row[2]));
    let result = BoundingBox::new(x.min, y.min, x.max - x.min, y.max - y.min);
    valid(result).then_some(result)
}

fn glyph_transformed_bounds(
    bounds: BoundingBox,
    transforms: &[LayerAffineTransform],
    clip_path: bool,
) -> Option<BoundingBox> {
    let (&first, rest) = transforms.split_first()?;
    let exact = rest.iter().copied().fold(first, concat_affine);
    let native = if clip_path {
        // PreparedGlyph clips are transformed paths, not a concatenated canvas
        // matrix. Enclose rounding after each reversed path transformation too.
        transforms
            .iter()
            .rev()
            .try_fold(bounds, |bounds, &transform| {
                f32_affine_bounds(bounds, f32_affine(transform))
            })?
    } else {
        let transform = rest
            .iter()
            .copied()
            .map(f32_affine)
            .fold(f32_affine(first), concat_f32_affine);
        f32_affine_bounds(bounds, transform)?
    };
    Some(union(checked_affine_bounds(bounds, exact)?, native))
}

fn glyph_outline_bounds(outline: &LayerGlyphOutlinePaint) -> Option<BoundingBox> {
    let placement = outline.placement.run_to_page;
    match outline.payload_kind {
        GlyphOutlinePayloadKind::MonochromeFill | GlyphOutlinePayloadKind::MonochromeFillStroke => {
            if outline.paths.len() > 4096 {
                return None;
            }
            let stroke_padding =
                if outline.payload_kind == GlyphOutlinePayloadKind::MonochromeFillStroke {
                    let stroke = outline.stroke.as_ref()?;
                    if !stroke.is_strict_subset() {
                        return None;
                    }
                    // Miter length is bounded in path space, before scale/skew.
                    (stroke.width * 0.5 * stroke.miter_limit).max(
                        F32Range::scalar(stroke.width)
                            .mul(F32Range::scalar(0.5))
                            .mul(F32Range::scalar(stroke.miter_limit))
                            .max,
                    )
                } else {
                    0.0
                };
            let mut bounds = None;
            for path in &outline.paths {
                let local = expand(glyph_path_bounds(&path.commands)?, stroke_padding);
                let path = glyph_transformed_bounds(local, &[placement], false)?;
                bounds = Some(bounds.map_or(path, |prior| union(prior, path)));
            }
            bounds
        }
        GlyphOutlinePayloadKind::ColorLayers => {
            let payload = outline.color_layers.as_ref()?;
            if payload.color_format == ColorGlyphFormat::ColrV0
                && payload.has_colrv0_resolved_layer_contract()
            {
                let mut bounds = None;
                for layer in &payload.layers {
                    let mut transforms = vec![placement];
                    transforms.extend(layer.transform_to_run);
                    let path = glyph_transformed_bounds(
                        glyph_path_bounds(layer.commands.as_deref()?)?,
                        &transforms,
                        false,
                    )?;
                    bounds = Some(bounds.map_or(path, |prior| union(prior, path)));
                }
                bounds
            } else if payload.has_colrv1_supported_graph_contract() {
                color_graph_bounds(payload.paint_graph.as_ref()?, placement)
            } else {
                None
            }
        }
        // These payloads carry page-space placements and are handled separately.
        GlyphOutlinePayloadKind::BitmapGlyph | GlyphOutlinePayloadKind::SvgGlyph => None,
    }
}

fn color_graph_bounds(
    graph: &ColorPaintGraphPayload,
    placement: LayerAffineTransform,
) -> Option<BoundingBox> {
    // The caller checks the graph's bounded, acyclic replay contract. Do not
    // deduplicate shared leaves: each parent can supply a different transform.
    let mut pending = vec![(graph.root_node_id, vec![placement], None::<BoundingBox>)];
    let mut bounds = None;
    while let Some((id, mut transforms, clip)) = pending.pop() {
        let node = graph.nodes.iter().find(|node| node.node_id == id)?;
        let commands = match node.kind {
            ColorPaintGraphNodeKind::Transform => {
                let child = node.transform.as_ref()?;
                transforms.push(child.transform);
                pending.push((child.child_node_id, transforms, clip));
                continue;
            }
            ColorPaintGraphNodeKind::Composite => {
                let composite = node.composite.as_ref()?;
                pending.push((composite.source_node_id, transforms.clone(), clip));
                pending.push((composite.backdrop_node_id, transforms, clip));
                continue;
            }
            ColorPaintGraphNodeKind::Clip => {
                let child = node.clip.as_ref()?;
                let own_clip = glyph_transformed_bounds(
                    glyph_path_bounds(&child.clip_commands)?,
                    &transforms,
                    true,
                )?;
                let clipped = match clip {
                    Some(clip) => intersection(clip, own_clip),
                    None => Some(own_clip),
                };
                if let Some(clipped) = clipped {
                    // A later child transform must not move this enclosing clip.
                    pending.push((child.child_node_id, transforms, Some(clipped)));
                }
                continue;
            }
            ColorPaintGraphNodeKind::SolidPath => &node.solid_path.as_ref()?.commands,
            ColorPaintGraphNodeKind::LinearGradientPath => {
                &node.linear_gradient_path.as_ref()?.commands
            }
            ColorPaintGraphNodeKind::RadialGradientPath => {
                &node.radial_gradient_path.as_ref()?.commands
            }
            ColorPaintGraphNodeKind::SweepGradientPath => {
                &node.sweep_gradient_path.as_ref()?.commands
            }
        };
        let path = glyph_transformed_bounds(glyph_path_bounds(commands)?, &transforms, false)?;
        let path = match clip {
            Some(clip) => intersection(path, clip),
            None => Some(path),
        };
        if let Some(path) = path {
            bounds = Some(bounds.map_or(path, |prior| union(prior, path)));
        }
    }
    bounds
}

fn glyph_path_bounds(commands: &[PathCommand]) -> Option<BoundingBox> {
    if !super::paint_op::path_commands_are_finite(commands) {
        return None;
    }
    let mut bounds = None;
    let mut include = |(x, y)| {
        // PathBuilder receives f32 endpoints/control points, including the
        // cubic points produced by the shared f64 arc conversion.
        for (x, y) in [(x, y), (f64::from(x as f32), f64::from(y as f32))] {
            let point = BoundingBox::new(x, y, 0.0, 0.0);
            if !valid(point) {
                return None;
            }
            bounds = Some(bounds.map_or(point, |prior| union(prior, point)));
        }
        Some(())
    };
    let mut current = (0.0, 0.0);
    let mut origin = current;
    for command in commands {
        match *command {
            PathCommand::MoveTo(x, y) => {
                include((x, y))?;
                origin = (x, y);
                current = origin;
            }
            PathCommand::LineTo(x, y) => {
                include(current)?;
                include((x, y))?;
                current = (x, y);
            }
            PathCommand::CurveTo(x1, y1, x2, y2, x, y) => {
                // A cubic lies within its endpoints/control-point convex hull.
                for point in [current, (x1, y1), (x2, y2), (x, y)] {
                    include(point)?;
                }
                current = (x, y);
            }
            PathCommand::ArcTo(rx, ry, rotation, large, sweep, x, y) => {
                include(current)?;
                include((x, y))?;
                if rx != 0.0 && ry != 0.0 {
                    // Native replay draws these same cubic approximations.
                    for segment in svg_arc_to_beziers(
                        current.0, current.1, rx, ry, rotation, large, sweep, x, y,
                    ) {
                        if let PathCommand::CurveTo(x1, y1, x2, y2, x, y) = segment {
                            for point in [(x1, y1), (x2, y2), (x, y)] {
                                include(point)?;
                            }
                        }
                    }
                }
                current = (x, y);
            }
            PathCommand::ClosePath => current = origin,
        }
    }
    bounds
}

fn shape_bounds(
    bounds: BoundingBox,
    logical: BoundingBox,
    transform: ShapeTransform,
) -> BoundingBox {
    let radians = transform.rotation.to_radians();
    let (sin, cos) = radians.sin_cos();
    let sx = if transform.horz_flip { -1.0 } else { 1.0 };
    let sy = if transform.vert_flip { -1.0 } else { 1.0 };
    let (a, b, c, d) = (cos * sx, sin * sx, -sin * sy, cos * sy);
    let cx = logical.x + logical.width * 0.5;
    let cy = logical.y + logical.height * 0.5;
    affine_bounds(
        bounds,
        Some(LayerAffineTransform {
            a,
            b,
            c,
            d,
            e: cx - a * cx - c * cy,
            f: cy - b * cx - d * cy,
        }),
    )
}

fn rotate_text(bounds: BoundingBox, logical: BoundingBox, run: &TextRunNode) -> BoundingBox {
    shape_bounds(
        bounds,
        logical,
        ShapeTransform {
            rotation: run.rotation,
            ..Default::default()
        },
    )
}

fn text_bounds(logical: BoundingBox, run: &TextRunNode) -> BoundingBox {
    let (font_size, baseline) = effective_text_font_size_and_baseline(run);
    // Character position moves ink, not the line's logical flow box. Keep both
    // extents in the conservative paint bounds so clipping/cache invalidation
    // cannot drop a raised/lowered glyph. Rotation still uses the original box.
    let position_offset = run.style.effective_font_size()
        * f64::from(run.style.font_position.unwrap_or_default())
        / 100.0;
    let positioned = if position_offset == 0.0 {
        logical
    } else {
        union(
            logical,
            BoundingBox::new(
                logical.x,
                logical.y + position_offset,
                logical.width,
                logical.height,
            ),
        )
    };
    let mut padding: f64 = 0.0;
    if run.style.underline != UnderlineType::None || run.style.strikethrough {
        padding = font_size * 0.2;
    }
    if run.style.outline_type != 0 || run.style.emboss || run.style.engrave {
        padding = padding.max(font_size * 0.15);
    }
    if let Some(p) = run.style.text_paint {
        let width = if p.width == 0.0 { 1.0 } else { p.width };
        padding = padding.max(width * f64::from(p.paint.miter_limit_milli) / 2000.0);
    }
    if run.style.emphasis_dot != 0 {
        padding = padding.max(font_size * 0.35);
    }
    let text = run.display_or_text();
    let positions = run.replay_positions_for(text);
    let mut painted = positioned;
    if let Some(visual) = crate::renderer::text_direction::VisualTextPositions::for_style(
        text, &positions, &run.style,
    ) {
        for (left, right) in visual.segments(text.chars().count()) {
            painted = union(
                painted,
                BoundingBox::new(
                    logical.x + left,
                    positioned.y,
                    right - left,
                    positioned.height,
                ),
            );
        }
    }
    let mut bounds = expand(painted, padding);
    if let Some(ink) = crate::renderer::text_ink::zero_advance_ink_bbox(logical, run) {
        bounds = union(bounds, ink);
    }
    if let Some(ink) = crate::renderer::emphasis::geometry(text, &positions, &run.style, font_size)
        .and_then(|g| g.ink_bounds())
    {
        bounds = union(
            bounds,
            BoundingBox::new(
                logical.x + ink.x,
                logical.y + baseline + ink.y,
                ink.width,
                ink.height,
            ),
        );
    }
    if run.style.shadow_type != 0 {
        let offset = BoundingBox::new(
            painted.x + run.style.shadow_offset_x,
            painted.y + run.style.shadow_offset_y,
            painted.width,
            painted.height,
        );
        bounds = union(
            bounds,
            expand(
                offset,
                run.style
                    .shadow_offset_x
                    .abs()
                    .max(run.style.shadow_offset_y.abs())
                    .max(font_size * 0.1),
            ),
        );
    }
    rotate_text(bounds, logical, run)
}

fn control_mark_bounds(logical: BoundingBox, run: &TextRunNode) -> BoundingBox {
    let (text, _) = bounded_text_prefix(&run.text);
    let positions = run.replay_positions_prefix_for(&run.text, &text);
    let font_size = if run.style.font_size > 0.0 {
        run.style.font_size
    } else {
        12.0
    };
    let mut bounds = logical;
    for x in positions
        .iter()
        .copied()
        .chain((run.is_para_end || run.is_line_break_end).then_some(logical.width))
    {
        bounds = union(
            bounds,
            BoundingBox::new(
                logical.x + x - font_size * 0.25,
                logical.y + run.baseline - font_size,
                font_size * 1.5,
                font_size * 1.2,
            ),
        );
    }
    rotate_text(bounds, logical, run)
}

fn tab_leader_bounds(logical: BoundingBox, run: &TextRunNode) -> BoundingBox {
    let (text, _) = bounded_display_text_for_run(run);
    let positions = run.replay_positions_prefix_for(run.display_or_text(), &text);
    let (font_size, baseline) = effective_text_font_size_and_baseline(run);
    let mut bounds = logical;
    for leader in run
        .style
        .tab_leaders
        .iter()
        .take(MAX_POSITIONED_CONTROL_MARKS_PER_RUN)
    {
        let end = clamp_tab_leader_end_x(&text, &positions, leader, font_size);
        bounds = union(
            bounds,
            BoundingBox::new(
                logical.x + leader.start_x.min(end),
                logical.y + baseline - font_size * 0.4,
                (end - leader.start_x).abs(),
                font_size * 0.5,
            ),
        );
    }
    rotate_text(bounds, logical, run)
}

fn decoration_bounds(
    logical: BoundingBox,
    run: &TextRunNode,
    kind: TextDecorationKind,
    trim: usize,
) -> BoundingBox {
    let (text, _) = bounded_display_text_for_run(run);
    let positions = run.replay_positions_prefix_for(run.display_or_text(), &text);
    let trailing = text.chars().rev().take_while(|ch| *ch == ' ').count();
    let end = positions.len().saturating_sub(trim.min(trailing));
    let width = positions.get(end.saturating_sub(1)).copied().unwrap_or(0.0);
    let (font_size, baseline) = effective_text_font_size_and_baseline(run);
    if kind == TextDecorationKind::EmphasisDot {
        let ink = crate::renderer::emphasis::geometry(&text, &positions, &run.style, font_size)
            .and_then(|g| g.ink_bounds());
        return rotate_text(
            ink.map_or(logical, |ink| {
                union(
                    logical,
                    BoundingBox::new(
                        logical.x + ink.x,
                        logical.y + baseline + ink.y,
                        ink.width,
                        ink.height,
                    ),
                )
            }),
            logical,
            run,
        );
    }
    let top_geometry = (kind == TextDecorationKind::Underline)
        .then(|| crate::renderer::text_decoration::underline_geometry(&run.style, font_size))
        .flatten();
    let end_pad = top_geometry
        .as_ref()
        .and_then(|g| g.dots.as_ref())
        .map_or(0.0, |d| d.radius);
    let (y, height) = match kind {
        TextDecorationKind::Underline => {
            if let Some(top) = &top_geometry {
                let (top, bottom) = top.vertical_bounds();
                (baseline + top, bottom - top)
            } else {
                (
                    if run.style.underline == UnderlineType::Top {
                        baseline - font_size + 1.0
                    } else {
                        baseline
                            + font_size * crate::renderer::text_decoration::UNDERLINE_BASELINE_RATIO
                    },
                    1.0,
                )
            }
        }
        TextDecorationKind::Strikethrough => (baseline - font_size * 0.3, 1.0),
        TextDecorationKind::EmphasisDot => (baseline, 0.0),
    };
    rotate_text(
        union(
            logical,
            expand(
                BoundingBox::new(
                    logical.x - end_pad,
                    logical.y + y,
                    width + 2.0 * end_pad,
                    height,
                ),
                2.0,
            ),
        ),
        logical,
        run,
    )
}

// The JSON writer and recording estimates must use the same bounded display
// projection, script metrics and producer positions for explicit text visuals.
pub(super) fn bounded_text_prefix(text: &str) -> (String, bool) {
    let mut chars = text.chars();
    let prefix = chars
        .by_ref()
        .take(MAX_POSITIONED_CONTROL_MARKS_PER_RUN)
        .collect();
    (prefix, chars.next().is_none())
}

pub(super) fn bounded_display_text_for_run(run: &TextRunNode) -> (String, bool) {
    let (source_prefix, source_complete) = bounded_text_prefix(run.display_or_text());
    let display_text = expand_pua_render_text_in_style(&source_prefix, &run.style);
    let (display_prefix, display_complete) = bounded_text_prefix(&display_text);
    (display_prefix, source_complete && display_complete)
}

pub(super) fn effective_text_font_size_and_baseline(run: &TextRunNode) -> (f64, f64) {
    let base_font_size = run.style.effective_font_size();
    run.style.script_draw_metrics(base_font_size, run.baseline)
}