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
use super::annotations::RenderedLink;
use super::display_list::{PaintBand, PaintBandList, recorded_context_paint_bounds};
use super::effects::PaintEffects;
use super::fragments::PaintFragment;
use super::geometry::{PaintClip, PaintTransform, PaintTranslation};
use super::page::PaintPrimitive;
use crate::document::Page;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum StackLevel {
Auto,
Integer(i32),
}
impl StackLevel {
pub(crate) fn from_z_index(z_index: i32) -> Self {
Self::Integer(z_index)
}
pub(crate) fn from_optional_z_index(z_index: Option<i32>) -> Self {
z_index.map_or(Self::Auto, Self::Integer)
}
pub(crate) fn paint_band(self) -> PaintBand {
match self {
Self::Integer(value) if value < 0 => PaintBand::NegativeZ,
Self::Integer(value) if value > 0 => PaintBand::PositiveZ,
Self::Auto | Self::Integer(0) => PaintBand::AutoZeroZ,
Self::Integer(_) => PaintBand::AutoZeroZ,
}
}
pub(crate) fn sort_key(self) -> (i32, i32) {
match self {
Self::Integer(value) => (value, 0),
Self::Auto => (0, 0),
}
}
}
/// Effects applied to a whole stacking context before PDF emission.
///
/// CSS Transforms, CSS CssColor opacity, and CSS Overflow act on stacking-context
/// contents as a group. The current flattening path keeps these defaults until
/// PDF group and matrix emission are wired through page streams:
/// <https://www.w3.org/TR/css-transforms-1/>,
/// <https://www.w3.org/TR/css-color-4/#transparency>, and
/// <https://www.w3.org/TR/css-overflow-3/>.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct PaintStackingContext {
pub(crate) source_order: usize,
pub(crate) stack_level: StackLevel,
/// Keep this context's materialized PDF paint separate from adjacent
/// contexts even when their CSS paint values happen to match. Fragment
/// replay uses the boundary so independently clipped continuations retain
/// their own device-pixel edge coverage.
pub(crate) pdf_paint_boundary: bool,
pub(crate) bands: PaintBandList,
pub(crate) effects: PaintEffects,
pub(crate) bounds: Option<PaintClip>,
}
impl PaintStackingContext {
pub(in crate::document) fn root() -> Self {
Self {
source_order: 0,
stack_level: StackLevel::Auto,
pdf_paint_boundary: false,
bands: PaintBandList::default(),
effects: PaintEffects::default(),
bounds: None,
}
}
/// Build a stacking-context node for an independently painted fragment.
///
/// CSS 2.2 Appendix E requires descendant stacking contexts to be painted
/// atomically inside the parent stack level:
/// <https://www.w3.org/TR/CSS22/zindex.html>.
pub(crate) fn new(
z_index: i32,
content: PaintFragment,
child_contexts: Vec<PaintStackingContext>,
) -> Self {
Self::new_with_stack_level(StackLevel::from_z_index(z_index), content, child_contexts)
}
pub(crate) fn new_with_stack_level(
stack_level: StackLevel,
content: PaintFragment,
child_contexts: Vec<PaintStackingContext>,
) -> Self {
let mut bands = PaintBandList::default();
bands.extend_band(
PaintBand::BackgroundBorder,
content.display_list.bands.into_items_in_order(),
);
for context in child_contexts {
bands.push_context(context);
}
bands.sort_stacking_contexts();
Self::with_bands(stack_level, bands)
}
/// Build a stacking-context node for an independently painted fragment
/// while preserving the fragment's internal CSS paint bands.
///
/// CSS Positioned Layout assigns the outer stack level in the parent
/// context, but CSS 2.2 Appendix E still applies recursively inside that
/// positioned context:
/// <https://www.w3.org/TR/css-position-3/#painting-order> and
/// <https://www.w3.org/TR/CSS22/zindex.html>.
pub(crate) fn from_banded_fragment_with_stack_level(
stack_level: StackLevel,
content: PaintFragment,
child_contexts: Vec<PaintStackingContext>,
) -> Self {
let mut bands = content.display_list.bands;
if matches!(stack_level, StackLevel::Integer(_)) {
let in_flow = std::mem::take(&mut bands.bands[PaintBand::InFlowBlock.index()]);
bands
.bands
.get_mut(PaintBand::BackgroundBorder.index())
.expect("background band index should exist")
.extend(in_flow);
}
for link in content.links {
bands.push_link(PaintBand::Inline, link);
}
for context in child_contexts {
bands.push_context(context);
}
bands.sort_stacking_contexts();
Self::with_bands(stack_level, bands)
}
/// Build an atomic stacking-context node while preserving the fragment's
/// existing paint-band structure.
///
/// CSS Transforms and CSS CssColor opacity create stacking contexts whose
/// descendants still follow CSS 2.2 Appendix E inside the isolated group:
/// <https://www.w3.org/TR/css-transforms-1/#transform-rendering> and
/// <https://www.w3.org/TR/css-color-4/#transparency>.
pub(crate) fn from_banded_fragment(
content: PaintFragment,
child_contexts: Vec<PaintStackingContext>,
) -> Self {
let mut bands = content.display_list.bands;
for link in content.links {
bands.push_link(PaintBand::Inline, link);
}
for context in child_contexts {
bands.push_context(context);
}
bands.sort_stacking_contexts();
Self::with_bands(StackLevel::Auto, bands)
}
pub(in crate::document) fn with_bands(stack_level: StackLevel, bands: PaintBandList) -> Self {
Self {
source_order: 0,
stack_level,
pdf_paint_boundary: false,
bands,
effects: PaintEffects::default(),
bounds: None,
}
}
pub(crate) fn with_source_order(mut self, source_order: usize) -> Self {
self.source_order = source_order;
self
}
pub(crate) fn with_effects(mut self, effects: PaintEffects) -> Self {
self.effects = effects;
self
}
pub(crate) fn with_bounds(mut self, bounds: PaintClip) -> Self {
self.bounds = Some(bounds);
self
}
pub(crate) fn with_links(mut self, links: Vec<RenderedLink>) -> Self {
for link in links {
self.bands.push_link(PaintBand::Inline, link);
}
self
}
/// Return bounds after context-level clipping and transforms.
///
/// CSS applies overflow/absolute clipping in the context's local coordinate
/// space and then maps the painted result through transforms. PDF opacity
/// groups need a Form XObject `/BBox` covering that composed output:
/// <https://www.w3.org/TR/css-overflow-3/#overflow-clip-edge>,
/// <https://www.w3.org/TR/css-transforms-1/#transform-rendering>, and
/// ISO 32000-1:2008 §11.6.6.
pub(crate) fn effect_bounds(&self, fallback: PaintClip) -> PaintClip {
let mut bounds = self.bounds.unwrap_or(fallback);
for clip in [
self.effects.absolute_clip,
self.effects.overflow_clip_bounds(),
]
.into_iter()
.flatten()
{
bounds =
bounds
.intersect(clip)
.unwrap_or(PaintClip::new(bounds.x(), bounds.y(), 0.0, 0.0));
}
if let Some(transform) = self.effects.transform {
bounds = transform.apply_clip_to_aabb(bounds);
}
bounds
}
/// Return the recorded ink bounds when every nested paint effect is a
/// rectangular, axis-aligned operation.
///
/// Fragmentainer replay uses this only to reject source slices that cannot
/// contain any ink. Effects whose bounds cannot be established exactly are
/// intentionally reported as indeterminate so callers conservatively keep
/// every candidate.
/// <https://www.w3.org/TR/css-break-3/#fragmentation-model>
pub(crate) fn recorded_paint_bounds(
&self,
page: &Page,
) -> std::result::Result<Option<PaintClip>, ()> {
recorded_context_paint_bounds(
page,
&self.effects,
PaintBand::ORDER
.into_iter()
.flat_map(|band| self.bands.bands[band.index()].iter()),
)
}
pub(in crate::document) fn push_flattened_primitives(
&self,
primitives: &mut Vec<PaintPrimitive>,
) {
self.bands.push_flattened_primitives(primitives);
}
pub(crate) fn for_each_flattened_primitive<'a>(
&'a self,
f: &mut impl FnMut(&'a PaintPrimitive),
) {
self.bands.for_each_flattened_primitive(f);
}
pub(crate) fn translated(self, offset: PaintTranslation) -> Self {
Self {
source_order: self.source_order,
stack_level: self.stack_level,
pdf_paint_boundary: self.pdf_paint_boundary,
bands: self.bands.translated(offset),
// Effects are expressed in the same page-top coordinate space as
// the band's primitives. Keeping them in place would detach an
// overflow or containment clip from an escaped inline fragment.
effects: self.effects.translated(offset),
bounds: self.bounds.map(|bounds| bounds.translated(offset)),
}
}
/// Trim materialized primitive geometry to a fragmentation projection
/// without flattening the stacking-context tree.
///
/// The accompanying rectangular effect remains necessary for paths,
/// images, glyphs, and deferred page operations whose exact ink cannot be
/// reduced to a rectangle here. Rectangular primitives are nevertheless
/// trimmed before PDF serialization so a fragmentainer edge has the same
/// coverage as an independently painted fragment rather than a PDF clip
/// seam.
/// <https://www.w3.org/TR/css-break-3/#fragmentation-model>
pub(crate) fn sliced_primitives_to_fragmentainer_rect(mut self, clip: PaintClip) -> Self {
self.bands = self.bands.sliced_primitives_to_fragmentainer_rect(clip);
self.bounds = self.bounds.and_then(|bounds| bounds.intersect(clip));
self
}
/// Whether all retained items have concrete rectangular bounds within a
/// clip. Callers may elide an additional PDF clip only in this narrow
/// case: deferred operations and nested effect scopes intentionally keep
/// the clip because their final ink is not known here.
pub(crate) fn has_only_items_wholly_contained_by_rect(&self, clip: PaintClip) -> bool {
let mut has_item = false;
for band in PaintBand::ORDER {
for item in &self.bands.bands[band.index()] {
has_item = true;
if !item.is_wholly_contained_by_rect(clip) {
return false;
}
}
}
has_item
}
/// Whether a rectangular overflow clip can be omitted after all retained
/// primitive geometry has been cut to its effective bounds.
///
/// This deliberately rejects every other effect. A transform, rounded
/// clip, mask, filter, or nested effect scope can change final ink after
/// local rectangle inspection, so its overflow clip remains authoritative.
pub(crate) fn can_elide_overflow_clip_after_materialization(
&self,
effective_clip: PaintClip,
) -> bool {
let mut effects_without_overflow_clip = self.effects.clone();
effects_without_overflow_clip.overflow_clip_effect = None;
effects_without_overflow_clip
== PaintEffects {
opacity: self.effects.opacity,
..PaintEffects::default()
}
&& self.has_only_items_wholly_contained_by_rect(effective_clip)
}
pub(in crate::document) fn into_recorded_nodes(self, page: &mut Page) -> Self {
Self {
source_order: self.source_order,
stack_level: self.stack_level,
pdf_paint_boundary: self.pdf_paint_boundary,
bands: self.bands.into_recorded_nodes(page),
effects: self.effects.clone(),
bounds: self.bounds,
}
}
pub(crate) fn into_primitive_nodes(self, page: &Page) -> Self {
Self {
source_order: self.source_order,
stack_level: self.stack_level,
pdf_paint_boundary: self.pdf_paint_boundary,
bands: self.bands.primitive_node_copy(page),
effects: self.effects,
bounds: self.bounds,
}
}
pub(in crate::document) fn primitive_node_copy(&self, page: &Page) -> Self {
Self {
source_order: self.source_order,
stack_level: self.stack_level,
pdf_paint_boundary: self.pdf_paint_boundary,
bands: self.bands.primitive_node_copy(page),
effects: self.effects.clone(),
bounds: self.bounds,
}
}
pub(in crate::document) fn push_transformed_links(
&self,
parent_transform: PaintTransform,
clip: Option<PaintClip>,
links: &mut Vec<RenderedLink>,
) {
if self.effects.suppresses_paint() {
return;
}
let transform = if let Some(transform) = self.effects.transform {
parent_transform.multiply(transform)
} else {
parent_transform
};
let clip = match (
clip,
self.effects
.scene_plane_clip
.map(|clip| transform.apply_clip_to_aabb(clip.bounds())),
) {
(Some(left), Some(right)) => left.intersect(right),
(Some(clip), None) | (None, Some(clip)) => Some(clip),
(None, None) => None,
};
self.bands.push_transformed_links(transform, clip, links);
}
}