use super::annotations::RenderedLink;
use super::contours::{OverflowClipEffect, ResolvedBoxContentClip};
use super::display_list::{PaintBand, PaintBandList, PaintDisplayItem, PaintDisplayList};
use super::effects::{PaintEffectScope, PaintEffects};
use super::geometry::{AxisSelectivePaintClip, PaintBounds, PaintClip, PaintTranslation};
use super::page::PaintPrimitive;
use super::stacking::PaintStackingContext;
use crate::css;
use crate::document::Page;
pub(crate) struct RecordedPaintFragment {
pub(in crate::document) display_list: PaintDisplayList,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct PaintFragment {
pub(in crate::document) display_list: PaintDisplayList,
pub links: Vec<RenderedLink>,
}
impl PaintFragment {
pub(crate) fn from_primitives(
primitives: Vec<PaintPrimitive>,
links: Vec<RenderedLink>,
) -> Self {
Self {
display_list: PaintDisplayList::from_primitives(primitives),
links,
}
}
pub(crate) fn from_stacking_context(context: PaintStackingContext) -> Self {
Self::from_stacking_context_in_band(context.stack_level.paint_band(), context)
}
pub(crate) fn from_stacking_context_in_band(
band: PaintBand,
context: PaintStackingContext,
) -> Self {
Self {
display_list: PaintDisplayList {
bands: {
let mut bands = PaintBandList::default();
bands.push_context_in_band(band, context);
bands
},
},
links: Vec::new(),
}
}
pub(crate) fn flattened_primitives(&self) -> Vec<PaintPrimitive> {
self.display_list.flattened_primitives()
}
pub(crate) fn contains_affine_3d_transform(&self) -> bool {
self.display_list.bands.contains_affine_3d_transform()
}
pub(in crate::document) fn for_each_flattened_primitive<'a>(
&'a self,
f: &mut impl FnMut(&'a PaintPrimitive),
) {
self.display_list.for_each_flattened_primitive(f);
}
pub(crate) fn prepend_primitives_in_band(
&mut self,
band: PaintBand,
primitives: impl IntoIterator<Item = PaintPrimitive>,
) {
let items = primitives
.into_iter()
.map(PaintDisplayItem::Primitive)
.collect::<Vec<_>>();
if items.is_empty() {
return;
}
self.display_list.bands.bands[band.index()].splice(0..0, items);
}
pub(crate) fn append_primitives_in_band(
&mut self,
band: PaintBand,
primitives: impl IntoIterator<Item = PaintPrimitive>,
) {
self.display_list.bands.extend_band(
band,
primitives.into_iter().map(PaintDisplayItem::Primitive),
);
}
pub(crate) fn prepend_monolithic_primitives_in_band(
&mut self,
band: PaintBand,
bounds: PaintClip,
primitives: impl IntoIterator<Item = PaintPrimitive>,
) {
let items = primitives
.into_iter()
.map(PaintDisplayItem::Primitive)
.collect::<Vec<_>>();
if items.is_empty() {
return;
}
self.display_list.bands.bands[band.index()].insert(
0,
PaintDisplayItem::EffectScope(PaintEffectScope::monolithic(bounds, items)),
);
}
pub(crate) fn append_monolithic_primitives_in_band(
&mut self,
band: PaintBand,
bounds: PaintClip,
primitives: impl IntoIterator<Item = PaintPrimitive>,
) {
let items = primitives
.into_iter()
.map(PaintDisplayItem::Primitive)
.collect::<Vec<_>>();
if items.is_empty() {
return;
}
self.display_list.bands.bands[band.index()].push(PaintDisplayItem::EffectScope(
PaintEffectScope::monolithic(bounds, items),
));
}
pub(crate) fn promote_background_border_to_in_flow_block(&mut self) {
let background_items =
std::mem::take(&mut self.display_list.bands.bands[PaintBand::BackgroundBorder.index()]);
if background_items.is_empty() {
return;
}
let in_flow = &mut self.display_list.bands.bands[PaintBand::InFlowBlock.index()];
in_flow.splice(0..0, background_items);
}
pub(crate) fn promote_outline_to_in_flow_outline(&mut self) {
let outline_items =
std::mem::take(&mut self.display_list.bands.bands[PaintBand::Outline.index()]);
if outline_items.is_empty() {
return;
}
self.display_list.bands.bands[PaintBand::InFlowOutline.index()].extend(outline_items);
}
#[allow(dead_code)]
pub(crate) fn promote_background_border_after_floats(&mut self) {
let background_items =
std::mem::take(&mut self.display_list.bands.bands[PaintBand::BackgroundBorder.index()]);
if background_items.is_empty() {
return;
}
self.display_list.bands.bands[PaintBand::Inline.index()].extend(background_items);
}
pub(crate) fn promote_page_background_to_in_flow_block(&mut self) {
let page_background =
std::mem::take(&mut self.display_list.bands.bands[PaintBand::PageBackground.index()]);
if page_background.is_empty() {
return;
}
let in_flow = &mut self.display_list.bands.bands[PaintBand::InFlowBlock.index()];
in_flow.splice(0..0, page_background);
}
pub(crate) fn take_positioned_stacking_contexts(&mut self) -> Vec<PaintStackingContext> {
let mut contexts = Vec::new();
for band in [
PaintBand::NegativeZ,
PaintBand::AutoZeroZ,
PaintBand::PositiveZ,
] {
let items = std::mem::take(&mut self.display_list.bands.bands[band.index()]);
let retained = items
.into_iter()
.filter_map(|item| match item {
PaintDisplayItem::StackingContext(context) => {
contexts.push(context);
None
}
item => Some(item),
})
.collect();
self.display_list.bands.bands[band.index()] = retained;
}
contexts
}
pub(crate) fn restore_positioned_stacking_contexts(
&mut self,
contexts: impl IntoIterator<Item = PaintStackingContext>,
) {
for context in contexts {
self.display_list.bands.push_context(context);
}
self.display_list.bands.sort_stacking_contexts();
}
pub(crate) fn is_empty(&self) -> bool {
self.display_list.is_empty() && self.links.is_empty()
}
pub(crate) fn first_line_y(&self) -> Option<f32> {
let mut first_line_y = None;
self.for_each_flattened_primitive(&mut |primitive| {
if first_line_y.is_none() {
match primitive {
PaintPrimitive::Line(line)
| PaintPrimitive::OpaqueTextCoverage { line, .. } => {
first_line_y = Some(line.y());
}
_ => {}
}
}
});
first_line_y
}
pub(crate) fn bounds(&self) -> Option<PaintClip> {
let mut bounds: Option<PaintBounds> = None;
self.for_each_flattened_primitive(&mut |primitive| {
let Some(primitive_bounds) = primitive.bounds() else {
return;
};
match &mut bounds {
Some(bounds) => bounds.include_paint_rect(primitive_bounds.paint_rect()),
None => bounds = Some(PaintBounds::from_paint_rect(primitive_bounds.paint_rect())),
}
});
for link in &self.links {
match &mut bounds {
Some(bounds) => bounds.include_paint_rect(link.paint_rect()),
None => bounds = Some(PaintBounds::from_paint_rect(link.paint_rect())),
}
}
bounds.map(PaintBounds::into_paint_clip)
}
pub(crate) fn with_contents_effect_scoped_to_rect_if_needed(
self,
page: &Page,
clip: PaintClip,
) -> Self {
if self.display_list.bands.contains_unresolved_3d_transform() {
return self.with_contents_effect_scoped_to_rect(clip);
}
let mut bounds: Option<PaintBounds> = None;
for band in PaintBand::ORDER {
for item in &self.display_list.bands.bands[band.index()] {
let Ok(item_bounds) = item.recorded_paint_bounds(page) else {
return self.with_contents_effect_scoped_to_rect(clip);
};
let Some(item_bounds) = item_bounds else {
continue;
};
match &mut bounds {
Some(bounds) => bounds.include_paint_rect(item_bounds.paint_rect()),
None => bounds = Some(PaintBounds::from_paint_rect(item_bounds.paint_rect())),
}
}
}
for link in &self.links {
match &mut bounds {
Some(bounds) => bounds.include_paint_rect(link.paint_rect()),
None => bounds = Some(PaintBounds::from_paint_rect(link.paint_rect())),
}
}
if bounds
.map(PaintBounds::into_paint_clip)
.is_some_and(|bounds| clip.contains(bounds))
{
self
} else {
self.with_contents_effect_scoped_to_rect(clip)
}
}
pub(crate) fn translated(mut self, offset: PaintTranslation) -> Self {
self.display_list = self.display_list.translated(offset);
self.links = self
.links
.into_iter()
.map(|link| link.translated(offset))
.collect();
self
}
pub(crate) fn with_contents_effect_scoped_to_rect(self, clip: PaintClip) -> Self {
self.with_contents_effect_scoped_to_clip(clip, false, None, None, false)
}
pub(crate) fn with_contents_effect_scoped_to_axis_selective_rect(
self,
clip: AxisSelectivePaintClip,
) -> Self {
self.with_contents_effect_scoped_to_clip(clip.bounds(), false, Some(clip), None, false)
}
pub(crate) fn with_descendant_paint_effect_scoped_to_rect(self, clip: PaintClip) -> Self {
self.with_contents_effect_scoped_to_clip(clip, false, None, None, true)
}
pub(crate) fn with_descendant_paint_effect_scoped_to_axis_selective_rect(
self,
clip: AxisSelectivePaintClip,
) -> Self {
self.with_contents_effect_scoped_to_clip(clip.bounds(), false, Some(clip), None, true)
}
pub(crate) fn with_contents_effect_scoped_to_rect_preserving_contained_ink(
self,
page: &Page,
clip: PaintClip,
) -> Self {
self.with_contents_effect_scoped_to_clip_with_recorded_ink(page, clip)
}
pub(crate) fn with_paint_containment_contents_effect_scoped_to_rect(
mut self,
page: &Page,
clip: PaintClip,
) -> Self {
for band in PaintBand::ORDER {
let items = std::mem::take(&mut self.display_list.bands.bands[band.index()]);
self.display_list.bands.bands[band.index()] = items
.into_iter()
.filter_map(|item| item.into_primitive_node(page))
.filter(|item| {
item.recorded_paint_bounds(page).map_or(true, |bounds| {
bounds.is_none_or(|bounds| bounds.intersects_with_positive_area(clip))
})
})
.collect();
}
self.with_contents_effect_scoped_to_clip(clip, true, None, None, false)
}
pub(crate) fn with_contents_effect_scoped_to_rect_and_child_contexts(
mut self,
clip: PaintClip,
child_contexts: Vec<PaintStackingContext>,
) -> Self {
for context in child_contexts {
self.display_list.bands.push_context(context);
}
self.display_list.bands.sort_stacking_contexts();
self.with_contents_effect_scoped_to_rect(clip)
}
pub(crate) fn with_contents_effect_scoped_to_rect_and_child_contexts_if_needed(
mut self,
page: &Page,
clip: PaintClip,
child_contexts: Vec<PaintStackingContext>,
) -> Self {
for context in child_contexts {
self.display_list.bands.push_context(context);
}
self.display_list.bands.sort_stacking_contexts();
self.with_contents_effect_scoped_to_rect_if_needed(page, clip)
}
pub(crate) fn with_contents_effect_scoped_to_box_content_contour(
self,
contour: ResolvedBoxContentClip,
) -> Self {
self.with_contents_effect_scoped_to_clip(contour.bounds, false, None, Some(contour), false)
}
pub(crate) fn with_descendant_paint_effect_scoped_to_box_content_contour(
self,
contour: ResolvedBoxContentClip,
) -> Self {
self.with_contents_effect_scoped_to_clip(contour.bounds, false, None, Some(contour), true)
}
pub(crate) fn with_contents_effect_scoped_to_box_content_contour_and_child_contexts(
mut self,
contour: ResolvedBoxContentClip,
child_contexts: Vec<PaintStackingContext>,
) -> Self {
for context in child_contexts {
self.display_list.bands.push_context(context);
}
self.display_list.bands.sort_stacking_contexts();
self.with_contents_effect_scoped_to_box_content_contour(contour)
}
fn with_contents_effect_scoped_to_clip(
self,
clip: PaintClip,
elide_covered_rectangular_clip: bool,
axis_selective_clip: Option<AxisSelectivePaintClip>,
contoured_overflow_clip: Option<ResolvedBoxContentClip>,
include_decoration_bands: bool,
) -> Self {
let mut bands = self.display_list.bands;
let overflow_clip_effect = contoured_overflow_clip
.map(OverflowClipEffect::Contoured)
.or_else(|| axis_selective_clip.map(OverflowClipEffect::AxisSelective))
.or(Some(OverflowClipEffect::Rect(clip)));
let effects = PaintEffects::transparent_overflow_scope(
overflow_clip_effect.expect("overflow scoping always has a clip effect"),
);
let mut emitted_scope = false;
for band in PaintBand::ORDER {
if !include_decoration_bands
&& matches!(
band,
PaintBand::BackgroundBorder
| PaintBand::TableCellBorder
| PaintBand::TableCollapsedBorder
| PaintBand::InFlowOutline
| PaintBand::Outline
)
{
continue;
}
let items = std::mem::take(&mut bands.bands[band.index()]);
if items.is_empty() {
continue;
}
let (inside, overflowing): (Vec<_>, Vec<_>) = if elide_covered_rectangular_clip {
items
.into_iter()
.partition(|item| item.is_wholly_contained_by_rect(clip))
} else {
(Vec::new(), items)
};
for item in inside {
bands.bands[band.index()].push(item);
}
if !overflowing.is_empty() {
bands.push_effect_scope_in_band(
band,
PaintEffectScope::new(effects.clone(), Some(clip), overflowing),
);
emitted_scope = true;
}
}
let content_links = self
.links
.into_iter()
.filter_map(|mut link| {
if axis_selective_clip.is_some() {
return Some(PaintDisplayItem::Link(link));
}
let clipped = PaintClip::from_paint_rect(link.paint_rect()).intersect(clip)?;
link.rect = clipped.paint_rect();
Some(PaintDisplayItem::Link(link))
})
.collect::<Vec<_>>();
if !content_links.is_empty() {
bands.push_effect_scope_in_band(
PaintBand::Inline,
PaintEffectScope::new(effects.clone(), Some(clip), content_links),
);
emitted_scope = true;
}
if elide_covered_rectangular_clip
&& !emitted_scope
&& (clip.width() <= 0.0 || clip.height() <= 0.0)
{
bands.push_effect_scope_in_band(
PaintBand::InFlowBlock,
PaintEffectScope::new(effects, Some(clip), Vec::new()),
);
}
Self {
display_list: PaintDisplayList { bands },
links: Vec::new(),
}
}
fn with_contents_effect_scoped_to_clip_with_recorded_ink(
self,
_page: &Page,
clip: PaintClip,
) -> Self {
let mut bands = self.display_list.bands;
let effects = PaintEffects::transparent_overflow_scope(OverflowClipEffect::Rect(clip));
let mut emitted_scope = false;
for band in PaintBand::ORDER {
if matches!(
band,
PaintBand::BackgroundBorder
| PaintBand::TableCellBorder
| PaintBand::TableCollapsedBorder
| PaintBand::InFlowOutline
| PaintBand::Outline
) {
continue;
}
let items = std::mem::take(&mut bands.bands[band.index()]);
if !items.is_empty() {
bands.bands[band.index()].push(PaintDisplayItem::EffectScope(
PaintEffectScope::new(effects.clone(), Some(clip), items),
));
emitted_scope = true;
}
}
let content_links = self
.links
.into_iter()
.filter_map(|mut link| {
let clipped = PaintClip::from_paint_rect(link.paint_rect()).intersect(clip)?;
link.rect = clipped.paint_rect();
Some(PaintDisplayItem::Link(link))
})
.collect::<Vec<_>>();
if !content_links.is_empty() {
bands.push_effect_scope_in_band(
PaintBand::Inline,
PaintEffectScope::new(effects.clone(), Some(clip), content_links),
);
emitted_scope = true;
}
if !emitted_scope && (clip.width() <= 0.0 || clip.height() <= 0.0) {
bands.push_effect_scope_in_band(
PaintBand::InFlowBlock,
PaintEffectScope::new(effects, Some(clip), Vec::new()),
);
}
Self {
display_list: PaintDisplayList { bands },
links: Vec::new(),
}
}
pub(crate) fn with_effect_scoped_to_rect_all_bands(mut self, clip: PaintClip) -> Self {
let effects = PaintEffects::transparent_overflow_scope(OverflowClipEffect::Rect(clip));
for band in PaintBand::ORDER {
let items = std::mem::take(&mut self.display_list.bands.bands[band.index()]);
if items.is_empty() {
continue;
}
self.display_list.bands.push_effect_scope_in_band(
band,
PaintEffectScope::new(effects.clone(), Some(clip), items),
);
}
let links = std::mem::take(&mut self.links)
.into_iter()
.filter_map(|mut link| {
let clipped = PaintClip::from_paint_rect(link.paint_rect()).intersect(clip)?;
link.rect = clipped.paint_rect();
Some(PaintDisplayItem::Link(link))
})
.collect::<Vec<_>>();
if !links.is_empty() {
self.display_list.bands.push_effect_scope_in_band(
PaintBand::Inline,
PaintEffectScope::new(effects, Some(clip), links),
);
}
self
}
pub(crate) fn top_level_contents_overflow_clip(&self) -> Option<PaintClip> {
PaintBand::ORDER
.into_iter()
.filter(|band| {
!matches!(
band,
PaintBand::BackgroundBorder
| PaintBand::TableCellBorder
| PaintBand::TableCollapsedBorder
| PaintBand::InFlowOutline
| PaintBand::Outline
)
})
.flat_map(|band| &self.display_list.bands.bands[band.index()])
.find_map(|item| match item {
PaintDisplayItem::EffectScope(scope) => scope.effects.overflow_clip_bounds(),
_ => None,
})
}
pub(crate) fn contains_overflow_clip(&self) -> bool {
self.display_list.bands.contains_overflow_clip()
}
pub(crate) fn into_fragmentable_source_canvas(mut self) -> Self {
self.display_list.bands.make_fragmentable_source_canvas();
self
}
pub(crate) fn with_primitives_sliced_to_fragmentainer_rect_preserving_structure(
mut self,
clip: PaintClip,
) -> Self {
self.display_list.bands = self
.display_list
.bands
.sliced_primitives_to_fragmentainer_rect(clip);
self.links = self
.links
.into_iter()
.filter_map(|mut link| {
let clipped = PaintClip::from_paint_rect(link.paint_rect()).intersect(clip)?;
link.rect = clipped.paint_rect();
Some(link)
})
.collect();
self
}
#[allow(dead_code)]
pub(crate) fn with_primitives_clipped_to_physical_block_range_preserving_inline_overflow(
self,
block_clip: PaintClip,
clip_crossing_ink: bool,
) -> Self {
self.with_primitives_clipped_to_physical_axis_range_preserving_cross_axis_overflow(
css::PhysicalAxis::Vertical,
block_clip,
clip_crossing_ink,
)
}
pub(crate) fn with_primitives_clipped_to_physical_axis_range_preserving_cross_axis_overflow(
self,
block_axis: css::PhysicalAxis,
block_clip: PaintClip,
clip_crossing_ink: bool,
) -> Self {
let Some(bounds) = self.bounds() else {
return self;
};
let inline_guard = 1.0;
let clip = match block_axis {
css::PhysicalAxis::Vertical => PaintClip::new(
bounds.x() - inline_guard,
block_clip.y(),
bounds.width() + inline_guard * 2.0,
block_clip.height(),
),
css::PhysicalAxis::Horizontal => PaintClip::new(
block_clip.x(),
bounds.y() - inline_guard,
block_clip.width(),
bounds.height() + inline_guard * 2.0,
),
};
let mut fragment = self;
fragment.display_list.bands = fragment
.display_list
.bands
.sliced_primitives_to_fragmentainer_rect(clip);
if clip_crossing_ink
&& !fragment
.display_list
.bands
.contains_monolithic_fragmentation()
{
let effects = PaintEffects::transparent_overflow_scope(OverflowClipEffect::Rect(clip));
for band in PaintBand::ORDER {
let items = std::mem::take(&mut fragment.display_list.bands.bands[band.index()]);
if items.is_empty() {
continue;
}
fragment.display_list.bands.push_effect_scope_in_band(
band,
PaintEffectScope::new(effects.clone(), Some(clip), items),
);
}
}
fragment.links = fragment
.links
.into_iter()
.filter_map(|mut link| {
let clipped = PaintClip::from_paint_rect(link.paint_rect()).intersect(clip)?;
link.rect = clipped.paint_rect();
Some(link)
})
.collect();
fragment
}
pub(crate) fn with_monolithic_fragmentation_scope(mut self, bounds: PaintClip) -> Self {
for band in PaintBand::ORDER {
let items = std::mem::take(&mut self.display_list.bands.bands[band.index()]);
if items.is_empty() {
continue;
}
self.display_list
.bands
.push_effect_scope_in_band(band, PaintEffectScope::monolithic(bounds, items));
}
if !self.links.is_empty() {
let links = std::mem::take(&mut self.links)
.into_iter()
.map(PaintDisplayItem::Link)
.collect();
self.display_list.bands.push_effect_scope_in_band(
PaintBand::Inline,
PaintEffectScope::monolithic(bounds, links),
);
}
self
}
pub(crate) fn with_contents_clipped_to_rect(
self,
clip: PaintClip,
child_contexts: Vec<PaintStackingContext>,
) -> Self {
let mut content_bands = self.display_list.bands;
let mut decoration_bands = PaintBandList::default();
decoration_bands.bands[PaintBand::BackgroundBorder.index()] =
std::mem::take(&mut content_bands.bands[PaintBand::BackgroundBorder.index()]);
decoration_bands.bands[PaintBand::TableCellBorder.index()] =
std::mem::take(&mut content_bands.bands[PaintBand::TableCellBorder.index()]);
decoration_bands.bands[PaintBand::TableCollapsedBorder.index()] =
std::mem::take(&mut content_bands.bands[PaintBand::TableCollapsedBorder.index()]);
decoration_bands.bands[PaintBand::InFlowOutline.index()] =
std::mem::take(&mut content_bands.bands[PaintBand::InFlowOutline.index()]);
decoration_bands.bands[PaintBand::Outline.index()] =
std::mem::take(&mut content_bands.bands[PaintBand::Outline.index()]);
let content_links = self
.links
.into_iter()
.filter_map(|mut link| {
let clipped = PaintClip::from_paint_rect(link.paint_rect()).intersect(clip)?;
link.rect = clipped.paint_rect();
Some(link)
})
.collect::<Vec<_>>();
let content_fragment = Self {
display_list: PaintDisplayList {
bands: content_bands,
},
links: content_links,
};
if !content_fragment.is_empty() || !child_contexts.is_empty() {
let content_context =
PaintStackingContext::from_banded_fragment(content_fragment, child_contexts)
.with_effects(PaintEffects::transparent_overflow_scope(
OverflowClipEffect::Rect(clip),
))
.with_bounds(clip);
decoration_bands.push_context_in_band(PaintBand::InFlowBlock, content_context);
}
Self {
display_list: PaintDisplayList {
bands: decoration_bands,
},
links: Vec::new(),
}
}
pub(crate) fn clipped_to_rect(self, clip: PaintClip) -> Self {
let primitives = self
.flattened_primitives()
.into_iter()
.filter_map(|primitive| primitive.clipped_to_rect(clip))
.collect::<Vec<_>>();
let links = self
.links
.into_iter()
.filter_map(|mut link| {
let clipped = PaintClip::from_paint_rect(link.paint_rect()).intersect(clip)?;
link.rect = clipped.paint_rect();
Some(link)
})
.collect::<Vec<_>>();
Self::from_primitives(primitives, links)
}
}
#[cfg(test)]
mod tests {
use std::rc::Rc;
use super::{PaintFragment, PaintStackingContext};
use crate::CssColor;
use crate::document::paint::annotations::RenderedLink;
use crate::document::paint::display_list::{
PaintBand, PaintBandList, PaintDisplayItem, PaintDisplayList,
};
use crate::document::paint::effects::PaintEffects;
use crate::document::paint::geometry::{
Affine3dPaintTransform, PaintClip, PaintPoint, PaintRect, PaintSize, PaintStrokeWidth,
PaintTranslation,
};
use crate::document::paint::page::PaintPrimitive;
use crate::document::paint::paths::{
RenderedPath, RenderedPathClip, RenderedPathCommand, RenderedPathFillRule,
};
use crate::document::paint::patterns::{PaintPatternTiling, RenderedImagePattern};
use crate::document::paint::shapes::RenderedRect;
use crate::document::paint::stacking::StackLevel;
use crate::document::paint::text::{
RenderedGlyph, RenderedGlyphKind, RenderedGlyphs, RenderedLine, RenderedTextMatrix,
RenderedTextRun,
};
fn test_rect(x: f32) -> PaintPrimitive {
PaintPrimitive::Rect(RenderedRect::from_paint_rect(
PaintRect::new(PaintPoint::new(x, 0.0), PaintSize::new(1.0, 1.0)),
Some(CssColor::BLACK),
))
}
fn rect_x(primitive: &PaintPrimitive) -> f32 {
let PaintPrimitive::Rect(rect) = primitive else {
panic!("expected test rectangle");
};
rect.paint_rect().min_x()
}
#[test]
fn promoted_in_flow_outline_precedes_auto_zero_positioned_paint() {
let mut fragment = PaintFragment::from_primitives(Vec::new(), Vec::new());
fragment.append_primitives_in_band(PaintBand::Inline, [test_rect(1.0)]);
fragment.append_primitives_in_band(PaintBand::Outline, [test_rect(2.0)]);
fragment.append_primitives_in_band(PaintBand::AutoZeroZ, [test_rect(3.0)]);
fragment.promote_outline_to_in_flow_outline();
assert!(fragment.display_list.bands.bands[PaintBand::Outline.index()].is_empty());
assert_eq!(
fragment
.flattened_primitives()
.iter()
.map(rect_x)
.collect::<Vec<_>>(),
vec![1.0, 2.0, 3.0]
);
}
#[test]
fn owner_inline_marker_precedes_owner_inline_and_auto_zero_child_context() {
let mut positioned_bands = PaintBandList::default();
positioned_bands.extend_band(
PaintBand::InFlowBlock,
[PaintDisplayItem::Primitive(test_rect(3.0))],
);
let positioned_child = PaintStackingContext::with_bands(StackLevel::Auto, positioned_bands);
let mut owner = PaintFragment::from_primitives(Vec::new(), Vec::new());
owner.append_primitives_in_band(PaintBand::Inline, [test_rect(1.0)]);
owner.append_primitives_in_band(PaintBand::Inline, [test_rect(2.0)]);
owner
.display_list
.bands
.push_context_in_band(PaintBand::AutoZeroZ, positioned_child);
assert_eq!(
owner
.flattened_primitives()
.iter()
.map(rect_x)
.collect::<Vec<_>>(),
vec![1.0, 2.0, 3.0]
);
}
#[test]
fn promotion_does_not_move_an_atomic_contexts_final_outline() {
let mut local_bands = PaintBandList::default();
local_bands.extend_band(
PaintBand::Outline,
[PaintDisplayItem::Primitive(test_rect(3.0))],
);
let local_context = PaintStackingContext::with_bands(StackLevel::Auto, local_bands);
let mut fragment = PaintFragment::from_primitives(Vec::new(), Vec::new());
fragment.append_primitives_in_band(PaintBand::Outline, [test_rect(2.0)]);
fragment
.display_list
.bands
.push_context_in_band(PaintBand::AutoZeroZ, local_context);
fragment.promote_outline_to_in_flow_outline();
let [PaintDisplayItem::StackingContext(context)] =
fragment.display_list.bands.bands[PaintBand::AutoZeroZ.index()].as_slice()
else {
panic!("expected positioned local context");
};
assert!(context.bands.bands[PaintBand::InFlowOutline.index()].is_empty());
assert!(matches!(
context.bands.bands[PaintBand::Outline.index()].as_slice(),
[PaintDisplayItem::Primitive(_)]
));
assert_eq!(
fragment
.flattened_primitives()
.iter()
.map(rect_x)
.collect::<Vec<_>>(),
vec![2.0, 3.0]
);
}
#[test]
fn overflow_and_fragmentation_keep_both_outline_phases_as_decorations() {
let mut fragment = PaintFragment::from_primitives(Vec::new(), Vec::new());
fragment.append_primitives_in_band(PaintBand::InFlowBlock, [test_rect(1.0)]);
fragment.append_primitives_in_band(PaintBand::InFlowOutline, [test_rect(2.0)]);
fragment.append_primitives_in_band(PaintBand::Outline, [test_rect(3.0)]);
let clipped = fragment
.clone()
.with_contents_clipped_to_rect(PaintClip::new(0.0, 0.0, 10.0, 10.0), Vec::new());
assert!(matches!(
clipped.display_list.bands.bands[PaintBand::InFlowOutline.index()].as_slice(),
[PaintDisplayItem::Primitive(_)]
));
assert!(matches!(
clipped.display_list.bands.bands[PaintBand::Outline.index()].as_slice(),
[PaintDisplayItem::Primitive(_)]
));
let fragmented =
fragment.with_monolithic_fragmentation_scope(PaintClip::new(0.0, 0.0, 10.0, 10.0));
assert!(matches!(
fragmented.display_list.bands.bands[PaintBand::InFlowOutline.index()].as_slice(),
[PaintDisplayItem::EffectScope(_)]
));
assert!(matches!(
fragmented.display_list.bands.bands[PaintBand::Outline.index()].as_slice(),
[PaintDisplayItem::EffectScope(_)]
));
}
#[test]
fn translated_paint_fragment_preserves_shared_rendered_glyph_storage() {
let glyphs: RenderedGlyphs = vec![RenderedGlyph {
kind: RenderedGlyphKind::Paint(42),
x_advance: 7.0,
nominal_x_advance: 7.0,
x_offset: 0.0,
y_offset: 0.0,
unicode: "A".to_string(),
}]
.into();
let line = RenderedLine::from_paint_origin(
"A".to_string(),
PaintPoint::new(10.0, 20.0),
12.0,
Some(0),
CssColor::BLACK,
vec![RenderedTextRun {
text: Rc::from("A"),
actual_text: None,
x_offset: 0.0,
y_offset: 0.0,
text_matrix: RenderedTextMatrix::IDENTITY,
font_size: 12.0,
font_id: Some(0),
font_palette: crate::css::FontPalette::Normal,
glyphs: Some(glyphs.clone()),
glyph_source_ranges: None,
}],
);
let fragment = PaintFragment::from_primitives(vec![PaintPrimitive::Line(line)], Vec::new());
let translated = fragment.translated(PaintTranslation::new(3.0, -4.0));
let primitives = translated.flattened_primitives();
let PaintPrimitive::Line(translated_line) = &primitives[0] else {
panic!("expected translated line primitive");
};
assert_eq!(translated_line.origin(), PaintPoint::new(13.0, 16.0));
assert!(glyphs.ptr_eq(translated_line.runs[0].glyphs.as_ref().unwrap()));
}
#[test]
fn descendant_overflow_scope_keeps_parent_owned_dotted_border_unclipped() {
let clip = PaintClip::new(0.0, 0.0, 20.0, 20.0);
let dotted_border = PaintPrimitive::Rect(RenderedRect::from_paint_rect(
PaintRect::new(PaintPoint::new(-1.0, -1.0), PaintSize::new(22.0, 22.0)),
Some(CssColor::BLACK),
));
let descendant = PaintPrimitive::Rect(RenderedRect::from_paint_rect(
PaintRect::new(PaintPoint::new(10.0, 10.0), PaintSize::new(30.0, 30.0)),
Some(CssColor::BLACK),
));
let mut principal = PaintFragment::from_primitives(Vec::new(), Vec::new());
principal.append_primitives_in_band(PaintBand::BackgroundBorder, [dotted_border]);
let mut descendants = PaintFragment::from_primitives(Vec::new(), Vec::new());
descendants.append_primitives_in_band(PaintBand::BackgroundBorder, [descendant]);
let clipped_descendants = descendants.with_descendant_paint_effect_scoped_to_rect(clip);
let decoration = &principal.display_list.bands.bands[PaintBand::BackgroundBorder.index()];
let clipped_descendant_background =
&clipped_descendants.display_list.bands.bands[PaintBand::BackgroundBorder.index()];
assert!(matches!(
decoration.as_slice(),
[PaintDisplayItem::Primitive(_)]
));
assert!(
matches!(clipped_descendant_background.as_slice(), [PaintDisplayItem::EffectScope(scope)] if scope.effects.overflow_clip_effect.is_some())
);
}
#[test]
fn paint_fragment_translation_moves_paths_patterns_and_links_together() {
let path = RenderedPath::new(
vec![RenderedPathCommand::move_to(PaintPoint::new(1.0, 2.0))],
Some(CssColor::BLACK),
RenderedPathFillRule::NonZero,
None,
PaintStrokeWidth::ZERO,
Some(RenderedPathClip::new(
vec![RenderedPathCommand::line_to(PaintPoint::new(3.0, 4.0))],
RenderedPathFillRule::NonZero,
Vec::new(),
)),
);
let pattern = RenderedImagePattern::from_paint_rect(
PaintRect::new(PaintPoint::new(5.0, 6.0), PaintSize::new(7.0, 8.0)),
true,
PaintPatternTiling::new(
PaintSize::new(7.0, 8.0),
PaintSize::new(7.0, 8.0),
PaintPoint::new(9.0, 10.0),
),
1,
1,
true,
Rc::<[u8]>::from(vec![0, 0, 0]),
None,
);
let link = RenderedLink::from_paint_rect(
PaintRect::new(PaintPoint::new(11.0, 12.0), PaintSize::new(13.0, 14.0)),
"https://example.com",
);
let translated = PaintFragment::from_primitives(
vec![
PaintPrimitive::Path(path),
PaintPrimitive::ImagePattern(pattern),
],
vec![link],
)
.translated(PaintTranslation::new(20.0, -30.0));
let primitives = translated.flattened_primitives();
let [
PaintPrimitive::Path(path),
PaintPrimitive::ImagePattern(pattern),
] = primitives.as_slice()
else {
panic!("expected translated path and image pattern");
};
assert_eq!(
path.commands,
vec![RenderedPathCommand::move_to(PaintPoint::new(21.0, -28.0))]
);
assert_eq!(
path.clip.as_ref().unwrap().commands,
vec![RenderedPathCommand::line_to(PaintPoint::new(23.0, -26.0))]
);
assert_eq!(
pattern.paint_rect(),
PaintRect::new(PaintPoint::new(25.0, -24.0), PaintSize::new(7.0, 8.0))
);
assert_eq!(pattern.tiling.origin, PaintPoint::new(29.0, -20.0));
assert_eq!(pattern.tiling.tile_size, PaintSize::new(7.0, 8.0));
assert_eq!(pattern.tiling.step, PaintSize::new(7.0, 8.0));
assert_eq!(
translated.links[0].paint_rect(),
PaintRect::new(PaintPoint::new(31.0, -18.0), PaintSize::new(13.0, 14.0))
);
}
#[test]
fn fragmentainer_slice_keeps_monolithic_paint_whole_at_its_block_start() {
let bounds = PaintClip::new(0.0, 0.0, 100.0, 100.0);
let fragment = PaintFragment::from_primitives(
vec![PaintPrimitive::Rect(RenderedRect::from_paint_rect(
bounds.paint_rect(),
Some(CssColor::BLACK),
))],
Vec::new(),
)
.with_monolithic_fragmentation_scope(bounds);
let sliced = fragment
.with_primitives_clipped_to_physical_block_range_preserving_inline_overflow(
PaintClip::new(0.0, 80.0, 100.0, 20.0),
true,
);
let primitives = sliced.flattened_primitives();
let [PaintPrimitive::Rect(rect)] = primitives.as_slice() else {
panic!("expected one retained monolithic rectangle");
};
assert_eq!(rect.paint_rect(), bounds.paint_rect());
}
#[test]
fn later_fragmentainer_slice_does_not_replay_monolithic_paint() {
let bounds = PaintClip::new(0.0, 0.0, 100.0, 100.0);
let fragment = PaintFragment::from_primitives(
vec![PaintPrimitive::Rect(RenderedRect::from_paint_rect(
bounds.paint_rect(),
Some(CssColor::BLACK),
))],
Vec::new(),
)
.with_monolithic_fragmentation_scope(bounds);
let sliced = fragment
.with_primitives_clipped_to_physical_block_range_preserving_inline_overflow(
PaintClip::new(0.0, 60.0, 100.0, 20.0),
true,
);
assert!(sliced.flattened_primitives().is_empty());
}
#[test]
fn overflow_scope_is_not_elided_for_unresolved_3d_paint() {
let clip = PaintClip::new(0.0, 0.0, 100.0, 100.0);
let child = PaintStackingContext::with_bands(StackLevel::Auto, PaintBandList::default())
.with_effects(PaintEffects {
affine_3d_transform: Some(Affine3dPaintTransform::identity()),
..PaintEffects::default()
})
.with_bounds(PaintClip::new(10.0, 10.0, 10.0, 10.0));
let mut bands = PaintBandList::default();
bands.extend_band(
PaintBand::InFlowBlock,
[PaintDisplayItem::StackingContext(child)],
);
let fragment = PaintFragment {
display_list: PaintDisplayList { bands },
links: Vec::new(),
};
let clipped = fragment.with_contents_effect_scoped_to_rect_if_needed(
&crate::document::Page::new(100.0, 100.0),
clip,
);
assert!(clipped.display_list.bands.contains_overflow_clip());
}
}