use super::*;
use crate::layout::assets::background_image_primitives_for_style;
pub(super) fn replay_page_margin_box_fragments(
page: &mut Page,
mut boxes: Vec<PageMarginPaintedBox>,
) {
const POST_DOCUMENT_SOURCE_ORDER: usize = usize::MAX - 16;
boxes.sort_by_key(|box_| (box_.z_index, box_.order));
for box_ in boxes {
let context = PaintStackingContext::new(box_.z_index, box_.fragment, Vec::new())
.with_effects(box_.effects)
.with_bounds(box_.bounds)
.with_source_order(POST_DOCUMENT_SOURCE_ORDER + box_.order);
if box_.z_index < 0 {
let fragment =
PaintFragment::from_stacking_context_in_band(PaintBand::PageBackground, context);
page.prepend_paint_fragment_owned(fragment, PaintTranslation::identity());
} else {
let fragment = PaintFragment::from_stacking_context(context);
page.append_paint_fragment_owned(fragment, PaintTranslation::identity());
}
}
page.sort_paint_tree_stacking_contexts();
}
pub(super) fn page_margin_box_paint_order(name: &str) -> usize {
const PAINT_ORDER: &[&str] = &[
"top-left-corner",
"top-left",
"top-center",
"top-right",
"top-right-corner",
"right-top",
"right-middle",
"right-bottom",
"bottom-right-corner",
"bottom-right",
"bottom-center",
"bottom-left",
"bottom-left-corner",
"left-bottom",
"left-middle",
"left-top",
];
PAINT_ORDER
.iter()
.position(|candidate| *candidate == name)
.unwrap_or(PAINT_ORDER.len())
}
pub(in crate::layout) fn paint_page_margin_box(
page: &mut Page,
layout: &PageMarginBoxLayout<'_>,
context: PageMarginPaintContext<'_>,
) {
let style = &layout.spec.style;
if style.visibility != Visibility::Visible {
return;
}
let (rects, rounded_rects, paths, strokes) = block_paint_ops(layout.border_rect, style);
for rect in rects {
page.push_rect_in_band(PaintBand::BackgroundBorder, rect);
}
for rect in rounded_rects {
page.push_rounded_rect_in_band(PaintBand::BackgroundBorder, rect);
}
for path in paths {
page.push_path_in_band(PaintBand::BackgroundBorder, path);
}
for stroke in strokes {
page.push_stroke_in_band(PaintBand::BackgroundBorder, stroke);
}
for primitive in background_image_primitives_for_style(
PaintBackgroundArea::from_paint_rect(layout.border_rect),
style,
context.base_url,
context.root_url,
context.resource_cache,
) {
push_page_margin_primitive(page, PaintBand::BackgroundBorder, primitive);
}
for primitive in page_margin_box_outline_primitives(layout, style) {
push_page_margin_primitive(page, PaintBand::Outline, primitive);
}
}
pub(in crate::layout) fn push_page_margin_primitive(
page: &mut Page,
band: PaintBand,
primitive: PaintPrimitive,
) {
match primitive {
PaintPrimitive::Rect(rect) => page.push_rect_in_band(band, rect),
PaintPrimitive::RoundedRect(rect) => page.push_rounded_rect_in_band(band, rect),
PaintPrimitive::Path(path) => page.push_path_in_band(band, path),
PaintPrimitive::Stroke(stroke) => page.push_stroke_in_band(band, stroke),
PaintPrimitive::Image(image) => page.push_image_in_band(band, image),
PaintPrimitive::ImagePattern(pattern) => page.push_image_pattern_in_band(band, pattern),
PaintPrimitive::ProjectiveRaster(_) => {
unreachable!("projective raster lowering happens in the PDF backend")
}
PaintPrimitive::GradientPattern(pattern) => {
page.push_gradient_pattern_in_band(band, pattern)
}
PaintPrimitive::SvgPattern(pattern) => page.push_svg_pattern_in_band(band, pattern),
PaintPrimitive::Line(line) => page.push_line_in_band(band, line),
PaintPrimitive::OpaqueTextCoverage { line, paths } => {
page.push_opaque_text_coverage_in_band(band, line, paths)
}
PaintPrimitive::SvgTextOutline {
content,
actual_text,
} => page.push_svg_text_outline_scope_in_band(band, *content, actual_text),
};
}
pub(in crate::layout) fn page_margin_box_outline_primitives(
layout: &PageMarginBoxLayout<'_>,
style: &ComputedStyle,
) -> Vec<PaintPrimitive> {
crate::layout::paint_ops::outline_primitives_for_border_rect(layout.border_rect, style)
}
pub(in crate::layout) fn page_margin_text_stack_top(
layout: &PageMarginBoxLayout<'_>,
vertical_align: VerticalAlign,
total_height: f32,
) -> f32 {
if matches!(vertical_align.baseline_shift, BaselineShift::Bottom)
|| matches!(
vertical_align.alignment_baseline,
AlignmentBaseline::Metric(BaselineMetric::TextBottom)
)
{
layout.content_y() + total_height
} else if matches!(vertical_align.baseline_shift, BaselineShift::Top)
|| matches!(
vertical_align.alignment_baseline,
AlignmentBaseline::Metric(BaselineMetric::TextTop)
)
{
layout.content_y() + layout.content_height()
} else {
layout.content_y() + ((layout.content_height() + total_height) / 2.0)
}
}