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
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
use super::*;
use crate::layout::assets::{
BorderPaint, ResolvedBackgroundImagePaint, has_opaque_square_normal_border,
};
use crate::layout::block::children::state::RenderedLegendBorderExclusion;
/// One visible destination slice of a continuously decorated fragmented box.
///
/// `source_border_rect` is the border box of the unfragmented (for
/// `box-decoration-break: slice`) box. `destination_border_rect` is solely
/// the visible fragment's paint and clipping geometry. The owner supplies
/// the already-projected translation between those coordinate spaces, which
/// keeps physical page coordinates out of the fragmentation contract.
///
/// <https://www.w3.org/TR/css-break-3/#break-decoration>
/// <https://www.w3.org/TR/css-backgrounds-3/#background-position>
#[derive(Debug, Clone, Copy)]
pub(in crate::layout) struct FragmentedDecorationSlice {
source_border_rect: PaintRect,
destination_border_rect: PaintRect,
source_to_destination: PaintTranslation,
owns_block_start: bool,
owns_block_end: bool,
}
impl FragmentedDecorationSlice {
pub(in crate::layout) fn new(
source_border_rect: PaintRect,
destination_border_rect: PaintRect,
source_to_destination: PaintTranslation,
owns_block_start: bool,
owns_block_end: bool,
) -> Self {
Self {
source_border_rect,
destination_border_rect,
source_to_destination,
owns_block_start,
owns_block_end,
}
}
/// The area used to resolve `background-position` and `background-size`.
/// The destination rectangle deliberately remains a clip: it never
/// becomes the positioning area for a sliced decoration.
pub(in crate::layout) fn positioning_border_rect(
self,
decoration_break: css::BoxDecorationBreak,
) -> PaintRect {
if decoration_break == css::BoxDecorationBreak::Clone {
self.destination_border_rect
} else {
self.source_to_destination
.transform_rect(&self.source_border_rect)
}
}
pub(in crate::layout) fn owns_block_start(self) -> bool {
self.owns_block_start
}
pub(in crate::layout) fn owns_block_end(self) -> bool {
self.owns_block_end
}
}
/// Builds CSS outline paint primitives for a box border area.
///
/// CSS UI paints outlines outside the border edge and excludes them from box
/// sizing. The renderer models this as a synthetic border expanded by the
/// used outline offset and outline width, then emits it in CSS 2.2 Appendix E's
/// final outline paint band:
/// <https://www.w3.org/TR/css-ui-3/#outline-props> and
/// <https://www.w3.org/TR/CSS22/zindex.html>.
pub(in crate::layout) fn outline_primitives_for_border_rect(
border_rect: PaintRect,
style: &ComputedStyle,
) -> Vec<PaintPrimitive> {
if style.outline_width <= 0.0 || style.outline_style.suppresses_used_width() {
return Vec::new();
}
if border_rect.size.width <= 0.0 || border_rect.size.height <= 0.0 {
return Vec::new();
}
if let Some(paths) = border_shape_outline_paths(border_rect, style) {
return paths.into_iter().map(PaintPrimitive::Path).collect();
}
let mut outline_style = style.clone();
outline_style.background.background_color = css::BackgroundColor::TRANSPARENT;
outline_style.background.background_image = css::ComputedImage::None;
outline_style.background.background_layers.clear();
outline_style.border_image = css::BorderImage::initial();
// CSS outlines are their own final paint step. Reusing the decoration
// helper must not replay the element's box shadows around the synthetic
// outline border.
// <https://www.w3.org/TR/css-ui-3/#outline-props>
outline_style.box_shadow.clear();
outline_style.border_width = style.outline_width;
outline_style.border_widths = css::Edges {
top: style.outline_width,
right: style.outline_width,
bottom: style.outline_width,
left: style.outline_width,
};
outline_style.border_width_values = css::PhysicalEdges::all(
css::ComputedLengthPercentage::from_points(style.outline_width),
);
outline_style.border_color = style.outline_color;
outline_style.border_colors = css::BorderColors {
top: style.outline_color,
right: style.outline_color,
bottom: style.outline_color,
left: style.outline_color,
};
outline_style.border_styles = css::BorderStyles {
top: style.outline_style,
right: style.outline_style,
bottom: style.outline_style,
left: style.outline_style,
};
let outset = style.outline_offset.length_points() + style.outline_width;
let (rects, rounded_rects, paths, strokes) = block_paint_ops(
expanded_outline_paint_rect(border_rect, outset),
&outline_style,
);
let mut primitives = Vec::new();
primitives.extend(rects.into_iter().map(PaintPrimitive::Rect));
primitives.extend(rounded_rects.into_iter().map(PaintPrimitive::RoundedRect));
primitives.extend(paths.into_iter().map(PaintPrimitive::Path));
primitives.extend(strokes.into_iter().map(PaintPrimitive::Stroke));
primitives
}
fn expanded_outline_paint_rect(border_rect: PaintRect, outset: f32) -> PaintRect {
paint_space_rect(
border_rect.origin.x - outset,
border_rect.origin.y - outset,
border_rect.size.width + outset * 2.0,
border_rect.size.height + outset * 2.0,
)
}
impl<'a> LayoutBuilder<'a> {
/// Collect positioned descendants emitted after `start_index` for one page.
///
/// Real stacking contexts capture their positioned descendants into the
/// scoped paint tree, while pseudo contexts such as inline-block painting
/// can intentionally let those descendants escape:
/// <https://www.w3.org/TR/css-position-3/#painting-order> and
/// <https://www.w3.org/TR/CSS22/zindex.html>.
pub(super) fn positioned_child_contexts_since(
&mut self,
start_index: usize,
page_index: usize,
policy: &StackingContextPolicy,
) -> Vec<PaintStackingContext> {
if !policy.captures_positioned_descendants || start_index >= self.positioned_layers.len() {
return Vec::new();
}
self.positioned_layers
.split_off(start_index)
.into_iter()
.filter(|layer| layer.page_index == page_index)
.map(|layer| layer.context.with_links(layer.links))
.collect()
}
/// Replace current-page paint emitted since `checkpoint` with one scoped
/// paint-tree context in the requested parent band.
///
/// CSS 2.2 Appendix E paints formatting units through ordered bands inside
/// stacking contexts. This helper preserves the captured fragment's local
/// bands while making the unit durable in the page paint tree:
/// <https://www.w3.org/TR/CSS22/zindex.html>.
pub(super) fn scope_current_page_paint_since(
&mut self,
checkpoint: &PaintCheckpoint,
band: PaintBand,
bounds: PaintClip,
child_contexts: Vec<PaintStackingContext>,
effects: PaintEffects,
) -> bool {
// A formatting context can have emitted only primitive operations so
// far; in that case no retained page paint tree exists yet. Capture
// through the durable checkpoint rather than requiring a tree, so an
// atomic effect (notably a containment clip) owns both primitive-only
// and already-retained descendants.
let fragment = self
.current_page
.take_paint_fragment_since(checkpoint.clone());
if fragment.is_empty() && child_contexts.is_empty() {
return false;
}
let context = PaintStackingContext::from_banded_fragment(fragment, child_contexts)
.with_source_order(self.next_paint_source_order())
.with_effects(effects)
.with_bounds(bounds);
self.current_page
.replace_paint_tree_since_with_context(checkpoint, band, context);
true
}
/// Replace current-page paint emitted since `checkpoint` using a full
/// stacking-context policy.
///
/// Flex items and table fragments derive their parent band, stack level,
/// captured descendant behavior, and effects from `StackingContextPolicy`;
/// using this helper keeps those policy decisions applied consistently:
/// <https://www.w3.org/TR/css-position-3/#painting-order> and
/// <https://www.w3.org/TR/CSS22/zindex.html>.
pub(super) fn scope_current_page_paint_since_with_policy(
&mut self,
checkpoint: &PaintCheckpoint,
policy: StackingContextPolicy,
bounds: PaintClip,
child_contexts: Vec<PaintStackingContext>,
) -> bool {
let fragment = self
.current_page
.take_paint_fragment_since(checkpoint.clone());
self.scope_current_page_fragment_with_policy(
checkpoint,
policy,
bounds,
fragment,
child_contexts,
)
}
/// Replace current-page paint since `checkpoint` with a prepared fragment
/// wrapped according to a stacking-context policy.
///
/// Table fragmentation may need to augment the captured fragment with
/// collapsed borders before it can be scoped, while flex item fragments can
/// scope the raw checkpoint capture. Both still use the same CSS stacking
/// policy fields when replacing page paint:
/// <https://www.w3.org/TR/css-position-3/#painting-order> and
/// <https://www.w3.org/TR/CSS22/zindex.html>.
pub(super) fn scope_current_page_fragment_with_policy(
&mut self,
checkpoint: &PaintCheckpoint,
policy: StackingContextPolicy,
bounds: PaintClip,
fragment: PaintFragment,
child_contexts: Vec<PaintStackingContext>,
) -> bool {
let source_order = self.next_paint_source_order();
self.scope_current_page_fragment_with_policy_and_source_order(
checkpoint,
policy,
bounds,
fragment,
child_contexts,
source_order,
)
}
/// Scope a prepared paint fragment with the source-order token captured
/// before its descendants were replayed.
///
/// A pseudo stacking context can export positioned descendants to its
/// parent. Those descendants still sort after the owning box in tree
/// order, so callers that replay descendants before scoping their owner
/// must reserve the owner's token at the replay checkpoint:
/// <https://www.w3.org/TR/CSS22/zindex.html>.
pub(super) fn scope_current_page_fragment_with_policy_and_source_order(
&mut self,
checkpoint: &PaintCheckpoint,
policy: StackingContextPolicy,
bounds: PaintClip,
fragment: PaintFragment,
child_contexts: Vec<PaintStackingContext>,
source_order: usize,
) -> bool {
if fragment.is_empty() && child_contexts.is_empty() {
return false;
}
let context = PaintStackingContext::from_banded_fragment_with_stack_level(
policy.stack_level,
fragment,
child_contexts,
)
.with_source_order(source_order)
.with_effects(policy.effects)
.with_bounds(bounds);
self.current_page.replace_paint_tree_since_with_context(
checkpoint,
policy.parent_band,
context,
);
true
}
/// Scope paint emitted since `checkpoint` as an atomic/effect box.
///
/// CSS Transforms, CSS CssColor opacity, CSS Overflow clipping, replaced
/// elements, inline-blocks, and table fragments all require descendants to
/// paint as one isolated unit in the parent stacking order. This helper
/// centralizes effect resolution so table/replaced/atomic callers do not
/// accidentally drop transforms, opacity groups, overflow clips, links, or
/// child stacking contexts:
/// <https://www.w3.org/TR/css-transforms-1/#transform-rendering>,
/// <https://www.w3.org/TR/css-color-4/#transparency>, and
/// <https://www.w3.org/TR/css-overflow-3/#overflow-clip-edge>.
pub(super) fn scope_current_page_atomic_paint_since(
&mut self,
checkpoint: &PaintCheckpoint,
band: PaintBand,
bounds: PaintClip,
style: &ComputedStyle,
child_contexts: Vec<PaintStackingContext>,
) -> bool {
let policy = StackingContextPolicy::for_atomic(style, band, bounds);
self.scope_current_page_paint_since_with_policy(checkpoint, policy, bounds, child_contexts)
}
/// Scope a replaced principal box as one monolithic paint subtree.
///
/// CSS Fragmentation treats replaced elements as monolithic. Their own
/// background/border and replaced content therefore have to be sliced or
/// moved as one unit; leaving only the replaced-content primitive atomic
/// can duplicate the background across columns.
/// <https://www.w3.org/TR/css-break-3/#monolithic>
pub(super) fn scope_current_page_replaced_paint_since(
&mut self,
checkpoint: &PaintCheckpoint,
band: PaintBand,
bounds: PaintClip,
style: &ComputedStyle,
) -> bool {
if self.float_paint_capture_depth > 0 {
// The enclosing float promotes this complete subtree into the
// Float paint band after layout. Scoping it here would remove it
// from that capture and leave the replaced image behind in the
// parent in-flow band.
return false;
}
// `border-image-outset` is ink overflow, not a layout adjustment.
// The monolithic-fragmentation bound chooses which replacement-box
// paint survives a positioned or fragmented replay, so retaining
// only the border box would silently drop all outset-only tiles.
// <https://drafts.csswg.org/css-backgrounds-3/#border-image-outset>
let bounds = border_image_ink_overflow_bounds(bounds, style);
let fragment = self
.current_page
.paint_tree_fragment_since(checkpoint)
.with_monolithic_fragmentation_scope(bounds);
let mut policy = StackingContextPolicy::for_atomic(style, band, bounds);
// Overflow clips a box's contents, not its own background and border.
// Replaced-element painters establish the concrete-object crop while
// producing their content primitive, so wrapping the whole atomic
// fragment here would incorrectly clip the principal decoration too.
// <https://www.w3.org/TR/css-overflow-3/#overflow-clipping>
policy.effects.clear_overflow_clip_effects();
self.scope_current_page_fragment_with_policy(
checkpoint,
policy,
bounds,
fragment,
Vec::new(),
)
}
/// Scope an embedded SVG root as its required isolated SVG stacking
/// context.
///
/// This captures the CSS box decoration emitted before the SVG scene and
/// the scene itself in one atomic group. The root's SVG viewport clipping
/// remains on the scene primitives, so the principal box decoration is
/// not incorrectly clipped by CSS overflow.
/// <https://www.w3.org/TR/SVG2/render.html#EstablishingANewStackingContext>
/// <https://www.w3.org/TR/SVG2/render.html#ParentCompositing>
pub(super) fn scope_current_page_inline_svg_root_paint_since(
&mut self,
checkpoint: &PaintCheckpoint,
band: PaintBand,
bounds: PaintClip,
fragmentation_bounds: PaintClip,
style: &ComputedStyle,
) -> bool {
if self.float_paint_capture_depth > 0 {
return false;
}
let fragment = self
.current_page
.paint_tree_fragment_since(checkpoint)
.with_monolithic_fragmentation_scope(fragmentation_bounds);
let mut policy = StackingContextPolicy::for_inline_svg_root_with_geometry(
style,
band,
assets::PrincipalPaintGeometry::with_transform_box(
fragmentation_bounds,
assets::TransformReferenceBox::css_layout(bounds.paint_rect()),
),
);
// Overflow clips a box's contents, not its own background and border.
// The inline SVG scene owns its viewport clip before it enters this
// root compositing group.
policy.effects.clear_overflow_clip_effects();
self.scope_current_page_fragment_with_policy(
checkpoint,
policy,
fragmentation_bounds,
fragment,
Vec::new(),
)
}
pub(super) fn push_rect_in_band(&mut self, band: PaintBand, rect: RenderedRect) {
if let Some(rect) = self.clip_rendered_rect(rect) {
self.current_page.push_rect_in_band(band, rect);
}
}
pub(super) fn push_rounded_rect_in_band(&mut self, band: PaintBand, rect: RenderedRoundedRect) {
self.current_page.push_rounded_rect_in_band(band, rect);
}
pub(super) fn push_path_in_band(&mut self, band: PaintBand, path: RenderedPath) {
self.current_page.push_path_in_band(band, path);
}
pub(super) fn push_svg_group_in_band(
&mut self,
band: PaintBand,
group: crate::svg::SvgPaintGroup,
) {
self.current_page.push_svg_group_in_band(band, group);
}
pub(super) fn box_background_primitives(
&self,
border_rect: PaintRect,
style: &ComputedStyle,
) -> Vec<PaintPrimitive> {
self.box_background_primitives_with_background_image_areas(border_rect, border_rect, style)
}
/// Paint a fragment's decoration while resolving its image layers from a
/// possibly different source positioning area.
///
/// CSS Backgrounds slices a fragmented box's background image from the
/// unfragmented positioning area, while the fragment itself remains the
/// image's clip and border-paint area. Generated pseudo-floats use this
/// same path as principal boxes, so their vector SVG paths and patterns
/// retain their normal background ordering when float paint is captured.
///
/// <https://www.w3.org/TR/css-backgrounds-3/#background-position>
/// <https://www.w3.org/TR/css-backgrounds-3/#box-decoration-break>
/// <https://drafts.csswg.org/css-pseudo-4/#generated-content>
/// <https://www.w3.org/TR/CSS22/visuren.html#floats>
pub(super) fn box_background_primitives_with_background_image_areas(
&self,
border_rect: PaintRect,
positioning_border_rect: PaintRect,
style: &ComputedStyle,
) -> Vec<PaintPrimitive> {
let background_images = self.resolved_background_image_paint_with_paint_areas(
positioning_border_rect,
border_rect,
style,
);
self.box_background_primitives_with_resolved_images(border_rect, style, background_images)
}
fn box_background_primitives_with_resolved_images(
&self,
border_rect: PaintRect,
style: &ComputedStyle,
background_images: ResolvedBackgroundImagePaint,
) -> Vec<PaintPrimitive> {
let border_paint = self.border_image_paint(border_rect, style);
let mut normal_border_style = style.clone();
// Normal and replacement borders are emitted in their own final
// phase. The shared decoration phases must never let an authored
// border-image affect their background or shadow primitives.
normal_border_style.border_image.source = css::ComputedImage::None;
let border_insets = used_border_widths(&normal_border_style);
let defer_border_disjoint_images = background_images.border_disjoint_tile_geometry
&& matches!(&border_paint, BorderPaint::UseNormalBorder)
&& has_opaque_square_normal_border(style)
&& !style.box_shadow.iter().any(|shadow| shadow.inset);
let mut background_images = background_images.primitives;
let mut primitives = Vec::new();
self.append_box_paint_phase(
&mut primitives,
block_paint_ops_with_phases(
border_rect,
&normal_border_style,
border_insets,
true,
true,
false,
false,
),
);
if !defer_border_disjoint_images {
primitives.append(&mut background_images);
}
self.append_box_paint_phase(
&mut primitives,
block_paint_ops_with_phases(
border_rect,
&normal_border_style,
border_insets,
false,
false,
true,
false,
),
);
match border_paint {
BorderPaint::UseNormalBorder => self.append_box_paint_phase(
&mut primitives,
block_paint_ops_with_phases(
border_rect,
&normal_border_style,
border_insets,
false,
false,
false,
true,
),
),
BorderPaint::ReplaceNormalBorder {
primitives: border_primitives,
} => primitives.extend(border_primitives),
}
if defer_border_disjoint_images {
primitives.append(&mut background_images);
}
primitives
}
/// Split a box with a normal CSS border into background and border phases.
/// This lets HTML's rendered-legend model subtract its margin rectangle
/// from border primitives without clipping backgrounds beneath the legend.
/// Border-image replacement stays on its dedicated asset path.
/// <https://html.spec.whatwg.org/multipage/rendering.html#the-fieldset-and-legend-elements>
pub(super) fn split_background_and_normal_border_primitives(
&self,
border_rect: PaintRect,
style: &ComputedStyle,
) -> Option<(
Vec<PaintPrimitive>,
Vec<PaintPrimitive>,
Vec<PaintPrimitive>,
)> {
self.split_background_and_normal_border_primitives_with_background_image_areas(
border_rect,
border_rect,
style,
)
}
/// Split a fragment decoration into background and normal-border phases,
/// retaining a distinct source positioning area for sliced backgrounds.
///
/// Fieldset legend exclusion applies only to the border phase, while CSS
/// Backgrounds still resolves `box-decoration-break: slice` images from
/// the unfragmented positioning area.
/// <https://html.spec.whatwg.org/multipage/rendering.html#the-fieldset-and-legend-elements>
/// <https://www.w3.org/TR/css-backgrounds-3/#background-position>
/// <https://www.w3.org/TR/css-break-3/#break-decoration>
pub(super) fn split_background_and_normal_border_primitives_with_background_image_areas(
&self,
border_rect: PaintRect,
positioning_border_rect: PaintRect,
style: &ComputedStyle,
) -> Option<(
Vec<PaintPrimitive>,
Vec<PaintPrimitive>,
Vec<PaintPrimitive>,
)> {
if style.border_image.source.is_image()
|| matches!(
self.border_image_paint(border_rect, style),
BorderPaint::ReplaceNormalBorder { .. }
)
{
return None;
}
let mut normal_border_style = style.clone();
normal_border_style.border_image.source = css::ComputedImage::None;
let insets = used_border_widths(&normal_border_style);
let background_images = self.resolved_background_image_paint_with_paint_areas(
positioning_border_rect,
border_rect,
style,
);
let defer_border_disjoint_images = background_images.border_disjoint_tile_geometry
&& has_opaque_square_normal_border(style)
&& !style.box_shadow.iter().any(|shadow| shadow.inset);
let mut background_images = background_images.primitives;
let mut backgrounds = Vec::new();
let background_phase = block_paint_ops_with_phases(
border_rect,
&normal_border_style,
insets,
true,
true,
false,
false,
);
self.append_box_paint_phase(&mut backgrounds, background_phase);
if !defer_border_disjoint_images {
backgrounds.append(&mut background_images);
}
let inset_shadow_phase = block_paint_ops_with_phases(
border_rect,
&normal_border_style,
insets,
false,
false,
true,
false,
);
self.append_box_paint_phase(&mut backgrounds, inset_shadow_phase);
let mut borders = Vec::new();
let border_phase = block_paint_ops_with_phases(
border_rect,
&normal_border_style,
insets,
false,
false,
false,
true,
);
self.append_box_paint_phase(&mut borders, border_phase);
let deferred_images = if defer_border_disjoint_images {
background_images
} else {
Vec::new()
};
Some((backgrounds, borders, deferred_images))
}
/// Paint one box fragment while excluding a rendered fieldset legend from
/// its normal border phase only.
///
/// The caller decides whether the legend belongs to this destination
/// fragmentainer. This preserves a complete cloned border on later
/// continuations, which have no rendered legend.
/// <https://html.spec.whatwg.org/multipage/rendering.html#the-fieldset-and-legend-elements>
pub(super) fn box_background_primitives_with_legend_border_exclusion(
&self,
border_rect: PaintRect,
positioning_border_rect: PaintRect,
style: &ComputedStyle,
legend_exclusion: Option<&RenderedLegendBorderExclusion>,
) -> Vec<PaintPrimitive> {
let Some(exclusion) = legend_exclusion else {
return self.box_background_primitives_with_background_image_areas(
border_rect,
positioning_border_rect,
style,
);
};
let Some((mut backgrounds, borders, mut deferred_images)) = self
.split_background_and_normal_border_primitives_with_background_image_areas(
border_rect,
positioning_border_rect,
style,
)
else {
return self.box_background_primitives_with_background_image_areas(
border_rect,
positioning_border_rect,
style,
);
};
backgrounds
.extend(self.clip_rectangular_border_primitives(borders, &exclusion.visible_regions));
backgrounds.append(&mut deferred_images);
backgrounds
}
/// Clip rectangular border primitives to the fieldset regions visible
/// around a rendered legend. Non-rectangular primitives retain their
/// dedicated paint paths; those paths are handled by the corresponding
/// rounded, patterned, and border-image exclusions as they acquire the
/// same phase boundary.
pub(super) fn clip_rectangular_border_primitives(
&self,
primitives: Vec<PaintPrimitive>,
visible_regions: &[PaintRect],
) -> Vec<PaintPrimitive> {
let mut clipped = Vec::new();
for primitive in primitives {
match primitive {
PaintPrimitive::Rect(rect) => {
for region in visible_regions {
if let Some(intersection) = rect.paint_rect().intersection(region) {
let mut part = rect.clone();
part.set_paint_rect(intersection);
clipped.push(PaintPrimitive::Rect(part));
}
}
}
primitive => clipped.push(primitive),
}
}
clipped
}
fn append_box_paint_phase(
&self,
primitives: &mut Vec<PaintPrimitive>,
(rects, rounded_rects, paths, strokes): (
Vec<RenderedRect>,
Vec<RenderedRoundedRect>,
Vec<RenderedPath>,
Vec<RenderedStroke>,
),
) {
primitives.extend(
rects
.into_iter()
.filter_map(|rect| self.clip_rendered_rect(rect))
.map(PaintPrimitive::Rect),
);
primitives.extend(rounded_rects.into_iter().map(PaintPrimitive::RoundedRect));
primitives.extend(paths.into_iter().map(PaintPrimitive::Path));
primitives.extend(strokes.into_iter().map(PaintPrimitive::Stroke));
}
pub(super) fn box_outline_primitives(
&self,
border_rect: PaintRect,
style: &ComputedStyle,
) -> Vec<PaintPrimitive> {
outline_primitives_for_border_rect(border_rect, style)
}
pub(super) fn push_primitive_in_band(&mut self, band: PaintBand, primitive: PaintPrimitive) {
match primitive {
PaintPrimitive::Rect(rect) => self.push_rect_in_band(band, rect),
PaintPrimitive::RoundedRect(rect) => self.push_rounded_rect_in_band(band, rect),
PaintPrimitive::Path(path) => self.push_path_in_band(band, path),
PaintPrimitive::Stroke(stroke) => self.push_stroke_in_band(band, stroke),
PaintPrimitive::Image(image) => self.push_image_in_band(band, image),
PaintPrimitive::ImagePattern(pattern) => self.push_image_pattern_in_band(band, pattern),
PaintPrimitive::ProjectiveRaster(_) => {
unreachable!("projective raster lowering happens in the PDF backend")
}
PaintPrimitive::GradientPattern(pattern) => {
self.current_page
.push_gradient_pattern_in_band(band, pattern);
}
PaintPrimitive::SvgPattern(pattern) => {
self.current_page.push_svg_pattern_in_band(band, pattern);
}
PaintPrimitive::Line(line) => self.push_line_in_band(band, line),
PaintPrimitive::OpaqueTextCoverage { line, paths } => {
self.current_page
.push_opaque_text_coverage_in_band(band, line, paths);
}
PaintPrimitive::SvgTextOutline {
content,
actual_text,
} => {
self.current_page
.push_svg_text_outline_scope_in_band(band, *content, actual_text);
}
}
}
pub(super) fn push_stroke_in_band(&mut self, band: PaintBand, stroke: RenderedStroke) {
self.current_page.push_stroke_in_band(band, stroke);
}
pub(super) fn push_image(&mut self, image: RenderedImage) {
self.push_image_in_band(PaintBand::InFlowBlock, image);
}
pub(super) fn push_image_in_band(&mut self, band: PaintBand, image: RenderedImage) {
self.current_page.push_image_in_band(band, image);
}
pub(super) fn push_image_pattern_in_band(
&mut self,
band: PaintBand,
pattern: RenderedImagePattern,
) {
self.current_page.push_image_pattern_in_band(band, pattern);
}
pub(super) fn push_line_in_band(&mut self, band: PaintBand, line: RenderedLine) {
match self.rendered_line_clip(&line) {
Some(Some(clip)) => {
self.current_page
.push_line_clipped_in_band(band, line, clip);
}
Some(None) => {
self.current_page.push_line_in_band(band, line);
}
None => {}
}
}
pub(super) fn push_overflow_clip(&mut self, clip: OverflowClip) {
// An empty used scrollport is still an active overflow clip: it
// suppresses every descendant rather than becoming `overflow:
// visible`. Retaining it also keeps push/pop scope depth balanced;
// dropping it would let `pop_overflow_clip` remove an ancestor clip.
// <https://www.w3.org/TR/css-overflow-3/#overflow-clipping>
self.overflow_clips.push(clip);
}
/// Push a CSS overflow clip for a box's padding box.
///
/// CSS Overflow clips non-visible overflow to the overflow clip edge, whose
/// default position is the padding box. This helper is shared by formatting
/// contexts that know their used content height before painting children:
/// <https://www.w3.org/TR/css-overflow-3/#overflow-clip-edge>.
#[allow(clippy::too_many_arguments)]
pub(super) fn push_padding_box_overflow_clip(
&mut self,
element: &Element,
style: &ComputedStyle,
scrollbar_reservation: Option<ScrollbarGutterReservation>,
outer_x: f32,
block_top: f32,
border_widths: css::Edges,
content_width: f32,
content_height: f32,
) -> bool {
let containment = used_property_containment(element, style);
if !style_clips_overflow(style) && !containment.clips_descendant_paint() {
return false;
}
let used_overflow = UsedOverflowAxes::from_style(style);
let clip_height = content_height + style.padding.top + style.padding.bottom;
let padding_box = PageTopRect::new(
outer_x + border_widths.left,
block_top - border_widths.top,
content_width + style.padding.left + style.padding.right,
clip_height,
)
.paint_clip();
let scrollport = scrollbar_reservation.map_or_else(
|| ScrollportGeometry::for_padding_box(padding_box, style, used_overflow, false, false),
|reservation| {
ScrollportGeometry::for_padding_box_with_reservation(padding_box, reservation)
},
);
let border_box = PageTopRect::new(
outer_x,
block_top,
content_width
+ style.padding.left
+ style.padding.right
+ border_widths.left
+ border_widths.right,
content_height
+ style.padding.top
+ style.padding.bottom
+ border_widths.top
+ border_widths.bottom,
)
.paint_rect();
let edge = resolve_overflow_clip_edge(
border_box,
style,
border_widths,
used_overflow,
containment.clips_descendant_paint(),
(used_overflow.horizontal.is_scroll_container()
|| used_overflow.vertical.is_scroll_container())
.then_some(scrollport.scrollport),
)
.expect("overflow or paint containment requires a resolved clip edge");
self.push_overflow_clip(edge.eager_clip());
true
}
pub(super) fn pop_overflow_clip(&mut self, active: bool) {
if active {
self.overflow_clips.pop();
}
}
/// Clip a filled rectangle to active CSS overflow clipping rectangles.
///
/// CSS Overflow clips visual overflow to the overflow clip edge. The first
/// implemented clip primitive is an axis-aligned rectangle, which covers
/// block padding-box clipping for `overflow: hidden`:
/// <https://www.w3.org/TR/css-overflow-3/#overflow-clip-edge>.
fn clip_rendered_rect(&self, mut rect: RenderedRect) -> Option<RenderedRect> {
for clip in &self.overflow_clips {
let clip_rect = clip.paint_rect();
let left = if clip.clips_x {
rect.x().max(clip_rect.origin.x)
} else {
rect.x()
};
let right = if clip.clips_x {
(rect.x() + rect.width()).min(clip_rect.origin.x + clip_rect.size.width)
} else {
rect.x() + rect.width()
};
let bottom = if clip.clips_y {
rect.y().max(clip_rect.origin.y)
} else {
rect.y()
};
let top = if clip.clips_y {
(rect.y() + rect.height()).min(clip_rect.origin.y + clip_rect.size.height)
} else {
rect.y() + rect.height()
};
if right <= left || top <= bottom {
return None;
}
rect.set_paint_rect(paint_space_rect(left, bottom, right - left, top - bottom));
}
Some(rect)
}
/// Return the effective PDF clip for a text line, if it remains visible.
///
/// CSS Tables 3 clips cell content portions that belong to collapsed
/// tracks. PDF text is emitted as whole shaped lines, so a partial
/// intersection needs an in-band graphics-state clip rather than line
/// filtering; otherwise the glyph ink outside the collapsed cell leaks:
/// <https://drafts.csswg.org/css-tables-3/#visibility-collapse-cell-rendering>.
fn rendered_line_clip(&self, line: &RenderedLine) -> Option<Option<PaintClip>> {
rendered_line_clip_for_overflow_clips(line, &self.overflow_clips)
}
}
fn border_image_ink_overflow_bounds(bounds: PaintClip, style: &ComputedStyle) -> PaintClip {
if !style.border_image.source.is_image() {
return bounds;
}
let outsets = used_border_image_outsets(style, computed_border_widths(style));
PaintClip::new(
bounds.x() - outsets.left,
bounds.y() - outsets.bottom,
bounds.width() + outsets.left + outsets.right,
bounds.height() + outsets.top + outsets.bottom,
)
}
fn rendered_line_clip_for_overflow_clips(
line: &RenderedLine,
overflow_clips: &[OverflowClip],
) -> Option<Option<PaintClip>> {
if overflow_clips.is_empty() {
return Some(None);
}
let line_clip = line.glyph_ink_bounds.unwrap_or_else(|| {
let width = rendered_line_width(line);
PaintClip::new(
line.x(),
line.y() - line.font_size,
width,
line.font_size * 1.35,
)
});
let mut clipped = line_clip;
for clip in overflow_clips {
let clip_rect = clip.paint_rect();
let left = if clip.clips_x {
clipped.x().max(clip_rect.origin.x)
} else {
clipped.x()
};
let right = if clip.clips_x {
(clipped.x() + clipped.width()).min(clip_rect.origin.x + clip_rect.size.width)
} else {
clipped.x() + clipped.width()
};
let bottom = if clip.clips_y {
clipped.y().max(clip_rect.origin.y)
} else {
clipped.y()
};
let top = if clip.clips_y {
(clipped.y() + clipped.height()).min(clip_rect.origin.y + clip_rect.size.height)
} else {
clipped.y() + clipped.height()
};
if right <= left || top <= bottom {
return None;
}
clipped = PaintClip::new(left, bottom, right - left, top - bottom);
}
Some((clipped != line_clip).then_some(clipped))
}
fn rendered_line_width(line: &RenderedLine) -> f32 {
line.runs.iter().fold(0.0_f32, |width, run| {
let run_width = if run.text_matrix.is_identity() {
run.glyphs
.as_ref()
.map(|glyphs| glyphs.iter().map(|glyph| glyph.x_advance).sum::<f32>())
.unwrap_or_else(|| run.text.chars().count() as f32 * run.font_size * 0.5)
} else {
run.font_size
};
width.max(run.x_offset + run_width)
})
}
#[cfg(test)]
mod tests {
use std::rc::Rc;
use super::*;
#[test]
fn expanded_outline_rect_preserves_nonzero_paint_origin() {
let border_rect = paint_space_rect(13.0, 29.0, 20.0, 40.0);
assert_eq!(
expanded_outline_paint_rect(border_rect, 5.0),
paint_space_rect(8.0, 24.0, 30.0, 50.0)
);
}
#[test]
fn fragmented_decoration_keeps_source_positioning_separate_from_destination_clip() {
let source = paint_space_rect(10.0, 20.0, 100.0, 200.0);
let destination = paint_space_rect(40.0, 80.0, 100.0, 60.0);
let slice = FragmentedDecorationSlice::new(
source,
destination,
PaintTranslation::new(30.0, 60.0),
false,
true,
);
assert_eq!(
slice.positioning_border_rect(css::BoxDecorationBreak::Slice),
paint_space_rect(40.0, 80.0, 100.0, 200.0)
);
assert_eq!(
slice.positioning_border_rect(css::BoxDecorationBreak::Clone),
destination
);
assert!(!slice.owns_block_start());
assert!(slice.owns_block_end());
}
#[test]
fn partially_intersecting_line_receives_pdf_clip_scope() {
let line = RenderedLine::new(
"shouldbeclipped".to_string(),
10.0,
20.0,
10.0,
None,
CssColor::BLACK,
vec![RenderedTextRun {
text: Rc::from("shouldbeclipped"),
actual_text: None,
x_offset: 0.0,
y_offset: 0.0,
text_matrix: RenderedTextMatrix::IDENTITY,
font_size: 10.0,
font_id: None,
font_palette: crate::css::FontPalette::Normal,
glyphs: None,
glyph_source_ranges: None,
}],
);
let clip = OverflowClip::from_paint_rect(paint_space_rect(10.0, 0.0, 30.0, 40.0));
assert_eq!(
rendered_line_clip_for_overflow_clips(&line, &[clip]),
Some(Some(PaintClip::new(10.0, 10.0, 30.0, 13.5)))
);
}
#[test]
fn glyph_ink_wholly_inside_fragment_edge_does_not_receive_synthetic_clip() {
let mut line = RenderedLine::new(
"ink fits".to_string(),
10.0,
20.0,
10.0,
None,
CssColor::BLACK,
Vec::new(),
);
// The line's conservative em box reaches y=10, but the selected glyph
// outlines remain above the fragment edge at y=12. A PDF clip here
// would introduce an antialiased edge without clipping any CSS ink.
line.glyph_ink_bounds = Some(PaintClip::new(10.0, 13.0, 30.0, 7.0));
let clip = OverflowClip::from_paint_rect(paint_space_rect(10.0, 12.0, 30.0, 28.0));
assert_eq!(
rendered_line_clip_for_overflow_clips(&line, &[clip]),
Some(None)
);
}
}