File: test_bounding_boxes.py

package info (click to toggle)
fpdf2 2.8.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 114,352 kB
  • sloc: python: 50,410; sh: 133; makefile: 12
file content (750 lines) | stat: -rw-r--r-- 21,612 bytes parent folder | download
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
from pathlib import Path
from test.conftest import assert_pdf_equal

import pytest

from fpdf import FPDF
from fpdf.drawing import (
    Arc,
    BezierCurve,
    BoundingBox,
    Ellipse,
    HorizontalLine,
    Line,
    PaintedPath,
    Point,
    QuadraticBezierCurve,
    Rectangle,
    RelativeArc,
    RelativeBezierCurve,
    RelativeHorizontalLine,
    RelativeLine,
    RelativeQuadraticBezierCurve,
    RelativeVerticalLine,
    RoundedRectangle,
    Transform,
    VerticalLine,
)
from fpdf.enums import PathPaintRule

HERE = Path(__file__).resolve().parent


def rotate_around_center(bbox: BoundingBox) -> Transform:
    """
    Create a transformation that rotates the bounding box around its center.
    """
    center_x = (bbox.x0 + bbox.x1) / 2
    center_y = (bbox.y0 + bbox.y1) / 2
    return (
        Transform.translation(center_x, center_y)
        .rotate(45)
        .translate(-center_x + 150, -center_y)
    )


TRANSFORMS = {
    "identity": Transform.identity(),
    "scaled": Transform.scaling(2, 3),
    "rotated": rotate_around_center,
    "translated": Transform.translation(50, 100),
}


@pytest.mark.parametrize(
    "shape, start, expected_bbox",
    [
        (
            Line(pt=Point(50, 20)),
            Point(10, 20),
            BoundingBox(10, 20, 50, 20),
        ),
        (
            Line(pt=Point(30, 80)),
            Point(30, 10),
            BoundingBox(30, 10, 30, 80),
        ),
        (
            Line(pt=Point(100, 100)),
            Point(50, 50),
            BoundingBox(50, 50, 100, 100),
        ),
        (
            Line(pt=Point(50, 50)),
            Point(100, 100),
            BoundingBox(50, 50, 100, 100),
        ),
        (
            Line(pt=Point(5, 5)),
            Point(-10, -5),
            BoundingBox(-10, -5, 5, 5),
        ),
        (Line(pt=Point(42, 42)), Point(42, 42), BoundingBox(42, 42, 42, 42)),
        (
            RelativeLine(pt=Point(40, 0)),
            Point(10, 20),
            BoundingBox(10, 20, 50, 20),
        ),
        (
            RelativeLine(pt=Point(0, 60)),
            Point(30, 10),
            BoundingBox(30, 10, 30, 70),
        ),
        (
            RelativeLine(pt=Point(100, 100)),
            Point(0, 0),
            BoundingBox(0, 0, 100, 100),
        ),
        (
            RelativeLine(pt=Point(-100, -100)),
            Point(100, 100),
            BoundingBox(0, 0, 100, 100),
        ),
        (
            RelativeLine(pt=Point(15, 10)),
            Point(-10, -5),
            BoundingBox(-10, -5, 5, 5),
        ),
        (
            RelativeLine(pt=Point(0, 0)),
            Point(42, 42),
            BoundingBox(42, 42, 42, 42),
        ),
        (HorizontalLine(x=50), Point(10, 20), BoundingBox(10, 20, 50, 20)),
        (HorizontalLine(x=20), Point(50, 15), BoundingBox(20, 15, 50, 15)),
        (
            HorizontalLine(x=42),
            Point(42, 42),
            BoundingBox(42, 42, 42, 42),
        ),
        (
            RelativeHorizontalLine(x=40),
            Point(10, 20),
            BoundingBox(10, 20, 50, 20),
        ),
        (
            RelativeHorizontalLine(x=-30),
            Point(20, 15),
            BoundingBox(-10, 15, 20, 15),
        ),
        (
            RelativeHorizontalLine(x=0),
            Point(42, 42),
            BoundingBox(42, 42, 42, 42),
        ),
        (VerticalLine(y=80), Point(30, 10), BoundingBox(30, 10, 30, 80)),
        (VerticalLine(y=20), Point(20, 50), BoundingBox(20, 20, 20, 50)),
        (VerticalLine(y=42), Point(42, 42), BoundingBox(42, 42, 42, 42)),
        (RelativeVerticalLine(y=50), Point(30, 10), BoundingBox(30, 10, 30, 60)),
        (
            RelativeVerticalLine(y=-30),
            Point(20, 50),
            BoundingBox(20, 20, 20, 50),
        ),
        (
            RelativeVerticalLine(y=0),
            Point(42, 42),
            BoundingBox(42, 42, 42, 42),
        ),
        (
            BezierCurve(c1=Point(30, 0), c2=Point(70, 0), end=Point(100, 0)),
            Point(0, 0),
            BoundingBox(0, 0, 100, 0),
        ),
        (
            BezierCurve(c1=Point(0, 30), c2=Point(0, 70), end=Point(0, 100)),
            Point(0, 0),
            BoundingBox(0, 0, 0, 100),
        ),
        (
            BezierCurve(c1=Point(100, 0), c2=Point(0, 100), end=Point(100, 100)),
            Point(0, 0),
            BoundingBox(0, 0, 100, 100),
        ),
        (
            BezierCurve(c1=Point(70, 100), c2=Point(30, 100), end=Point(0, 100)),
            Point(100, 100),
            BoundingBox(0, 100, 100, 100),
        ),
        (
            BezierCurve(c1=Point(150, 0), c2=Point(-150, 100), end=Point(100, 100)),
            Point(0, 0),
            BoundingBox(-3.808522398518157, 0.0, 100.0, 100.0),
        ),
        (
            BezierCurve(c1=Point(25, 25), c2=Point(50, 50), end=Point(75, 75)),
            Point(0, 0),
            BoundingBox(0, 0, 75, 75),
        ),
        (
            BezierCurve(c1=Point(42, 42), c2=Point(42, 42), end=Point(42, 42)),
            Point(42, 42),
            BoundingBox(42, 42, 42, 42),
        ),
        (
            RelativeBezierCurve(
                c1=Point(30, 60), c2=Point(60, 90), end=Point(100, 100)
            ),
            Point(0, 0),
            BoundingBox(0, 0, 100, 100),
        ),
        (
            RelativeBezierCurve(
                c1=Point(20, -20), c2=Point(-30, 30), end=Point(-50, -50)
            ),
            Point(25, 25),
            BoundingBox(-25.0, -25.0, 29.63395588138013, 25.0),
        ),
        (
            RelativeBezierCurve(c1=Point(0, 0), c2=Point(0, 0), end=Point(50, 50)),
            Point(0, 0),
            BoundingBox(0, 0, 50, 50),
        ),
        (
            RelativeBezierCurve(c1=Point(0, 0), c2=Point(0, 0), end=Point(0, 0)),
            Point(10, 10),
            BoundingBox(10, 10, 10, 10),
        ),
        (
            RelativeBezierCurve(c1=Point(50, 1), c2=Point(100, 2), end=Point(150, 1)),
            Point(0, 0),
            BoundingBox(0.0, 0.0, 150.0, 1.4142135623730951),
        ),
        (
            RelativeBezierCurve(c1=Point(1, 50), c2=Point(2, 100), end=Point(1, 150)),
            Point(0, 0),
            BoundingBox(0.0, 0.0, 1.4142135623730951, 150.0),
        ),
        (
            QuadraticBezierCurve(ctrl=Point(50, 100), end=Point(100, 0)),
            Point(0, 0),
            BoundingBox(0, 0, 100, 50),
        ),
        (
            QuadraticBezierCurve(ctrl=Point(50, 100), end=Point(100, 0)),
            Point(0, 0),
            BoundingBox(0, 0, 100, 50),
        ),
        (
            QuadraticBezierCurve(ctrl=Point(50, 0), end=Point(100, 0)),
            Point(0, 0),
            BoundingBox(0, 0, 100, 0),
        ),
        (
            QuadraticBezierCurve(ctrl=Point(50, 50), end=Point(100, 100)),
            Point(0, 0),
            BoundingBox(0, 0, 100, 100),
        ),
        (
            QuadraticBezierCurve(ctrl=Point(25, -100), end=Point(50, 0)),
            Point(0, 0),
            BoundingBox(0, -50, 50, 0),
        ),
        (
            QuadraticBezierCurve(ctrl=Point(42, 42), end=Point(42, 42)),
            Point(42, 42),
            BoundingBox(42, 42, 42, 42),
        ),
        (
            RelativeQuadraticBezierCurve(ctrl=Point(50, 100), end=Point(100, 0)),
            Point(0, 0),
            BoundingBox(0, 0, 100, 50),
        ),
        (
            RelativeQuadraticBezierCurve(ctrl=Point(50, 0), end=Point(100, 0)),
            Point(0, 0),
            BoundingBox(0, 0, 100, 0),
        ),
        (
            RelativeQuadraticBezierCurve(ctrl=Point(50, 50), end=Point(100, 100)),
            Point(0, 0),
            BoundingBox(0, 0, 100, 100),
        ),
        (
            RelativeQuadraticBezierCurve(ctrl=Point(25, -100), end=Point(50, 0)),
            Point(0, 0),
            BoundingBox(0, -50, 50, 0),
        ),
        (
            RelativeQuadraticBezierCurve(ctrl=Point(0, 0), end=Point(0, 0)),
            Point(42, 42),
            BoundingBox(42, 42, 42, 42),
        ),
        (
            RelativeQuadraticBezierCurve(ctrl=Point(-25, 25), end=Point(-50, -50)),
            Point(100, 100),
            BoundingBox(50, 50, 100, 106.25),
        ),
        (
            Arc(
                radii=Point(50, 50),
                rotation=0,
                large=False,
                sweep=True,
                end=Point(50, 50),
            ),
            Point(0, 0),
            BoundingBox(0, 0, 50, 50),
        ),
        (
            Arc(
                radii=Point(50, 50),
                rotation=0,
                large=True,
                sweep=False,
                end=Point(-50, 0),
            ),
            Point(50, 0),
            BoundingBox(-50, -50, 50, 0),
        ),
        (
            Arc(
                radii=Point(50, 30),
                rotation=45,
                large=False,
                sweep=True,
                end=Point(100, 0),
            ),
            Point(0, 0),
            BoundingBox(-5.405509724430832, -68.61529643011123, 99.99999989849465, 0),
        ),
        (
            Arc(
                radii=Point(0.1, 0.1),
                rotation=0,
                large=False,
                sweep=True,
                end=Point(0.1, 0),
            ),
            Point(0, 0),
            BoundingBox(0, -0.013397459621556158, 0.1, 0),
        ),
        (
            Arc(
                radii=Point(25, 40),
                rotation=0,
                large=False,
                sweep=False,
                end=Point(-40, 25),
            ),
            Point(0, 0),
            BoundingBox(
                -39.99999998902608, -8.416886842006127, 0.0, 24.999999894774596
            ),
        ),
        (
            RelativeArc(
                radii=Point(50, 50),
                rotation=0,
                large=False,
                sweep=True,
                end=Point(50, 50),
            ),
            Point(0, 0),
            BoundingBox(0, 0, 50, 50),
        ),
        (
            RelativeArc(
                radii=Point(50, 50),
                rotation=0,
                large=True,
                sweep=False,
                end=Point(-100, 0),
            ),
            Point(50, 0),
            BoundingBox(-50.0, -50.0, 50.0, 0.0),
        ),
        (
            RelativeArc(
                radii=Point(50, 30),
                rotation=45,
                large=False,
                sweep=True,
                end=Point(100, 0),
            ),
            Point(0, 0),
            BoundingBox(-5.405509724430832, -68.61529643011123, 99.99999989849465, 0),
        ),
        (
            RelativeArc(
                radii=Point(0.1, 0.1),
                rotation=0,
                large=False,
                sweep=True,
                end=Point(0.1, 0),
            ),
            Point(0, 0),
            BoundingBox(0, -0.013397459621556158, 0.1, 0),
        ),
        (
            RelativeArc(
                radii=Point(25, 40),
                rotation=0,
                large=False,
                sweep=False,
                end=Point(-40, 25),
            ),
            Point(0, 0),
            BoundingBox(-39.99999998902608, -8.416886842006127, 0, 24.999999894774596),
        ),
        (
            Rectangle(org=Point(10, 20), size=Point(80, 60)),
            None,
            BoundingBox(10, 20, 90, 80),
        ),
        (
            Rectangle(org=Point(0, 0), size=Point(100, 100)),
            None,
            BoundingBox(0, 0, 100, 100),
        ),
        (
            Rectangle(org=Point(-50, -25), size=Point(30, 45)),
            None,
            BoundingBox(-50, -25, -20, 20),
        ),
        (
            Rectangle(org=Point(42, 42), size=Point(0, 0)),
            None,
            BoundingBox(42, 42, 42, 42),
        ),
        (
            Rectangle(org=Point(5, 10), size=Point(100, 0.1)),
            None,
            BoundingBox(5, 10, 105, 10.1),
        ),
        (
            Rectangle(org=Point(5, 10), size=Point(0.1, 100)),
            None,
            BoundingBox(5, 10, 5.1, 110),
        ),
        (
            RoundedRectangle(
                org=Point(0, 0), size=Point(100, 50), corner_radii=Point(10, 10)
            ),
            None,
            BoundingBox(0, 0, 100, 50),
        ),
        (
            RoundedRectangle(
                org=Point(10, 20), size=Point(30, 40), corner_radii=Point(0, 0)
            ),
            None,
            BoundingBox(10, 20, 40, 60),
        ),
        (
            RoundedRectangle(
                org=Point(50, 50), size=Point(-30, -20), corner_radii=Point(5, 5)
            ),
            None,
            BoundingBox(20, 30, 50, 50),
        ),
        (
            RoundedRectangle(
                org=Point(0, 0), size=Point(20, 10), corner_radii=Point(30, 30)
            ),
            None,
            BoundingBox(0, 0, 20, 10),
        ),
        (
            RoundedRectangle(
                org=Point(0, 0), size=Point(0, 50), corner_radii=Point(10, 10)
            ),
            None,
            BoundingBox(0, 0, 0, 50),
        ),
        (
            RoundedRectangle(
                org=Point(0, 0), size=Point(80, 0), corner_radii=Point(10, 10)
            ),
            None,
            BoundingBox(0, 0, 80, 0),
        ),
        (
            RoundedRectangle(
                org=Point(30, 30), size=Point(-10, -10), corner_radii=Point(0, 0)
            ),
            None,
            BoundingBox(20, 20, 30, 30),
        ),
        (
            Ellipse(radii=Point(10, 10), center=Point(0, 0)),
            None,
            BoundingBox(-10, -10, 10, 10),
        ),
        (
            Ellipse(radii=Point(10, 10), center=Point(100, 200)),
            None,
            BoundingBox(90, 190, 110, 210),
        ),
        (
            Ellipse(radii=Point(25, 10), center=Point(50, 75)),
            None,
            BoundingBox(25, 65, 75, 85),
        ),
        (
            Ellipse(radii=Point(0, 20), center=Point(50, 50)),
            None,
            BoundingBox.empty(),
        ),
        (
            Ellipse(radii=Point(15, 0), center=Point(0, 0)),
            None,
            BoundingBox.empty(),
        ),
    ],
)
def test_shape_bounding_box(shape, start, expected_bbox):
    bbox, _ = shape.bounding_box(start)
    if not bbox.is_valid():
        assert not expected_bbox.is_valid()
    else:
        assert bbox == expected_bbox

    for _, tf in TRANSFORMS.items():
        transform = tf(expected_bbox) if callable(tf) else tf

        start = start if start else Point(0, 0)
        path = PaintedPath()
        path.move_to(start.x, start.y)
        path.add_path_element(shape)
        path.style.stroke_color = "#000000"
        path.style.paint_rule = PathPaintRule.FILL_NONZERO
        path.transform = transform

        bbox, _ = path.bounding_box(start=start)

        expected_scaled_bbox = expected_bbox.transformed(transform)
        if not bbox.is_valid():
            assert not expected_scaled_bbox.is_valid()
            return
        assert bbox == expected_scaled_bbox


def simple_path_absolute_relative():
    path = PaintedPath()
    path.move_to(10, 10)
    path.line_to(100, 10)
    path.line_relative(0, 40)
    path.line_to(10, 50)
    path.close()
    return path


def circle_and_rectangle():
    path = PaintedPath()
    path.circle(60, 60, 30)
    path.rectangle(20, 20, 40, 40)
    return path


def mixed_curves_and_lines():
    path = PaintedPath()
    path.move_to(0, 0)
    path.curve_to(30, 60, 60, 90, 100, 100)
    path.quadratic_curve_relative(20, -100, 50, 0)
    path.line_to(0, 0)
    path.close()
    return path


def arc_and_line():
    path = PaintedPath()
    path.move_to(50, 50)
    path.arc_relative(25, 25, 0, False, True, 50, 50)
    path.line_to(25, 75)
    return path


def rect_base():
    p = PaintedPath()
    p.style.fill_color = "#88aaee"
    p.rectangle(10, 10, 90, 40)  # bbox: (10,10)-(100,50)
    return p


def fill_only_rect_nonzero():
    p = rect_base()
    p.style.fill_color = "#88aaee"
    p.style.stroke_color = None
    p.style.paint_rule = PathPaintRule.FILL_NONZERO
    return p  # FILL_NONZERO, no stroke expansion


def stroke_only_rect_w4():
    p = rect_base()
    p.style.fill_color = None
    p.style.stroke_color = "#000000"
    p.style.stroke_width = 4  # expand by 2 in x/y
    return p  # STROKE


def stroke_fill_rect_w2():
    p = rect_base()
    p.style.fill_color = "#cccccc"
    p.style.stroke_color = "#000000"
    p.style.stroke_width = 2  # expand by 1 in x/y
    p.style.paint_rule = PathPaintRule.STROKE_FILL_NONZERO
    return p  # STROKE_FILL_NONZERO


def evenodd_fill_with_hole():
    p = PaintedPath()
    # outer rect (20,20)-(90,90)
    p.rectangle(20, 20, 70, 70)
    # inner rect (40,40)-(70,70) => hole
    p.rectangle(40, 40, 30, 30)
    p.style.fill_color = "#88aaee"
    p.style.stroke_color = None
    p.style.paint_rule = PathPaintRule.FILL_EVENODD
    return p  # FILL_EVENODD, bbox is outer rect only


def scaled_stroke_rect_w2():
    p = rect_base()
    p.transform = Transform.scaling(2.0, 0.5)
    p.style.fill_color = None
    p.style.stroke_color = "#000000"
    p.style.stroke_width = 2
    p.style.paint_rule = PathPaintRule.STROKE
    return p
    # Geom scales to (20,5)-(200,25); row_norms=(2,0.5); pad=(2,0.5)


def translated_fill_only():
    p = rect_base()
    p.transform = Transform.translation(15, -5)
    p.style.fill_color = "#88aaee"
    p.style.stroke_color = None
    p.style.paint_rule = PathPaintRule.FILL_NONZERO
    return p  # FILL_NONZERO, just shifted


def nested_scaled_circle_stroke2():
    p = PaintedPath()
    p.transform = Transform.scaling(1.5, 1.5)
    p.circle(20, 20, 10)  # base bbox (10,10)-(30,30) -> scaled to (15,15)-(45,45)
    p.style.fill_color = None
    p.style.stroke_color = "#000000"
    p.style.stroke_width = 2  # row_norms ~ (1.5,1.5); pad=(1.5,1.5)
    return p


def hairline_stroke_rect():
    p = rect_base()
    p.style.fill_color = None
    p.style.stroke_color = "#000000"
    p.style.stroke_width = 0  # hairline: no bbox expansion in user space
    return p


def zero_opacity_stroke_rect():
    p = rect_base()
    p.style.fill_color = None
    p.style.stroke_color = "#000000"
    p.style.stroke_width = 6
    p.style.stroke_opacity = 0.0  # invisible stroke => no expansion
    return p


def dont_paint_rect():
    p = rect_base()
    p.style.paint_rule = PathPaintRule.DONT_PAINT
    # bbox should remain purely geometric
    return p


@pytest.mark.parametrize(
    "test_id, path_builder, expected_bbox",
    [
        (
            "simple_path_absolute_relative",
            simple_path_absolute_relative,
            BoundingBox(9.5, 9.5, 100.5, 50.5),
        ),
        (
            "circle_and_rectangle",
            circle_and_rectangle,
            BoundingBox(19.5, 19.5, 90.5, 90.5),
        ),
        (
            "mixed_curves_and_lines",
            mixed_curves_and_lines,
            BoundingBox(-0.5, -0.5, 150.5, 100.5),
        ),
        (
            "arc_and_line",
            arc_and_line,
            BoundingBox(
                24.5, 39.14466094591647, 110.85533905932736, 100.50000004195078
            ),
        ),
        # paint-rule variations (no transforms)
        (
            "fill_only_nonzero",
            fill_only_rect_nonzero,
            BoundingBox(10.0, 10.0, 100.0, 50.0),
        ),
        ("stroke_only_w4", stroke_only_rect_w4, BoundingBox(8.0, 8.0, 102.0, 52.0)),
        ("stroke_fill_w2", stroke_fill_rect_w2, BoundingBox(9.0, 9.0, 101.0, 51.0)),
        (
            "evenodd_fill_with_hole",
            evenodd_fill_with_hole,
            BoundingBox(20.0, 20.0, 90.0, 90.0),
        ),
        # transforms
        (
            "scaled_stroke_rect_w2",
            scaled_stroke_rect_w2,
            BoundingBox(18.0, 4.5, 202.0, 25.5),
        ),
        (
            "translated_fill_only",
            translated_fill_only,
            BoundingBox(25.0, 5.0, 115.0, 45.0),
        ),
        (
            "nested_scaled_circle_stroke2",
            nested_scaled_circle_stroke2,
            BoundingBox(13.5, 13.5, 46.5, 46.5),
        ),
        # special stroke cases
        (
            "hairline_stroke_rect",
            hairline_stroke_rect,
            BoundingBox(10.0, 10.0, 100.0, 50.0),
        ),
        (
            "zero_opacity_stroke_rect",
            zero_opacity_stroke_rect,
            BoundingBox(10.0, 10.0, 100.0, 50.0),
        ),
        # explicit DONT_PAINT (no stroke expansion)
        ("dont_paint_rect", dont_paint_rect, BoundingBox(10.0, 10.0, 100.0, 50.0)),
    ],
)
def test_composite_painted_path(test_id, path_builder, expected_bbox, tmp_path):
    path = path_builder()
    bbox, _ = path.bounding_box(Point(0, 0))
    print(bbox, expected_bbox)
    assert bbox == expected_bbox

    pdf = FPDF()
    pdf.add_page()

    with pdf.drawing_context() as gc:
        gc.add_item(path)

    with pdf.drawing_context() as gc:
        # Draw bounding box in red
        bbox_path = PaintedPath()
        bbox_path.rectangle(bbox.x0, bbox.y0, bbox.x1 - bbox.x0, bbox.y1 - bbox.y0)
        bbox_path.style.stroke_color = "#ff0000"
        bbox_path.style.stroke_width = 0.1
        bbox_path.style.fill_color = None
        gc.add_item(bbox_path)

    assert_pdf_equal(
        pdf,
        HERE / f"generated_pdf/bounding_box_painted_path_{test_id}.pdf",
        tmp_path,
    )