mod dimensions;
use crate::document::{BlockBox, CounterSnapshot, PageBox, Paragraph, RunningStringAssignment};
use crate::graphics::{BoxDecoration, BoxPaint, DisplayItem, FlowPaint, ImagePaint, Rect};
use crate::layout::measure::{MeasuredBlock, Measurer};
use crate::layout::{LayoutError, TextLine};
use crate::style::{BoxSizing, MarginLocation, PageMargin, Size, VerticalAlign};
use crate::text::Line;
use dimensions::{Variable, variable};
const HORIZONTAL: usize = 0;
const VERTICAL: usize = 1;
const START: usize = 0;
const CENTER: usize = 1;
const END: usize = 2;
const GROUPS: [[MarginLocation; 3]; 4] = [
[
MarginLocation::TopLeft,
MarginLocation::TopCenter,
MarginLocation::TopRight,
],
[
MarginLocation::BottomLeft,
MarginLocation::BottomCenter,
MarginLocation::BottomRight,
],
[
MarginLocation::LeftTop,
MarginLocation::LeftMiddle,
MarginLocation::LeftBottom,
],
[
MarginLocation::RightTop,
MarginLocation::RightMiddle,
MarginLocation::RightBottom,
],
];
struct Prepared<'a, 't> {
margin: &'a PageMargin,
decoration: &'a BoxDecoration,
paragraph: Option<Paragraph>,
images: (Vec<[f32; 2]>, Vec<crate::text::AtomicMetrics>),
replacement: Option<std::sync::Arc<crate::images::Image>>,
element: Option<&'t BlockBox>,
measured: Option<MeasuredBlock<'t>>,
padding: [f32; 4],
borders: [f32; 4],
margins: [Option<f32>; 4],
specified: [Option<f32>; 2],
start: [f32; 2],
content: [f32; 2],
lines: Vec<Line>,
}
impl<'t> Prepared<'_, 't> {
fn inline_axis(&self) -> usize {
if self.margin.style.used_writing_mode().vertical() {
VERTICAL
} else {
HORIZONTAL
}
}
fn block_axis(&self) -> usize {
1 - self.inline_axis()
}
fn sides(axis: usize) -> [usize; 2] {
if axis == HORIZONTAL { [3, 1] } else { [0, 2] }
}
fn edges(&self, axis: usize) -> f32 {
Self::sides(axis)
.iter()
.map(|&i| self.padding[i] + self.borders[i])
.sum()
}
fn outer_edges(&self, axis: usize) -> f32 {
self.edges(axis)
+ Self::sides(axis)
.iter()
.map(|&i| self.margins[i].unwrap_or(0.0))
.sum::<f32>()
}
fn fixed(&mut self, axis: usize, available: f32) {
let [start, end] = Self::sides(axis).map(|i| self.margins[i].unwrap_or(0.0));
self.start[axis] = start;
self.content[axis] = available - self.edges(axis) - start - end;
}
fn variable(&mut self, axis: usize, available: f32, outer: f32, position: usize) {
self.content[axis] = (outer - self.outer_edges(axis)).max(0.0);
let style = &self.margin.style;
if axis == HORIZONTAL && *style.width.basis() == Size::Auto {
self.content[axis] = crate::style::SizeConstraints::new(
[&style.width, &Size::Auto, &Size::Auto],
Some(available),
style.box_sizing,
self.edges(axis),
)
.preferred(self.content[axis]);
}
let offset = match position {
START => 0.0,
CENTER => (available - outer) / 2.0,
END => available - outer,
_ => unreachable!(),
};
self.start[axis] = offset + self.margins[Self::sides(axis)[0]].unwrap_or(0.0);
}
fn validate(&self) -> Result<(), LayoutError> {
let values = self
.padding
.iter()
.chain(self.borders.iter())
.chain(self.margins.iter().flatten())
.chain(self.specified.iter().flatten())
.chain(self.start.iter())
.chain(self.content.iter());
if values.into_iter().any(|value| !value.is_finite())
|| [HORIZONTAL, VERTICAL]
.iter()
.any(|&axis| !self.outer_edges(axis).is_finite())
{
return Err(LayoutError::NonFinitePageMarginGeometry {
location: self.margin.location,
});
}
Ok(())
}
fn layout(&mut self, measurer: &mut Measurer<'t>) -> Result<(), LayoutError> {
self.validate()?;
let inline = self.content[self.inline_axis()];
if let Some(element) = self.element {
self.measured = Some(measurer.block(
element,
0.0,
inline,
self.margin.style.used_rtl(),
false,
None,
)?);
return Ok(());
}
self.lines = self
.paragraph
.as_ref()
.map(|p| measurer.text_lines(p, inline, &self.images.1))
.transpose()?
.unwrap_or_default();
Ok(())
}
fn intrinsic(
&self,
measurer: &mut Measurer<'t>,
available: f32,
) -> Result<[f32; 2], LayoutError> {
if let Some(element) = self.element {
let widths = measurer
.run(|measurer| crate::layout::intrinsic::measure(element, measurer, available))?;
return Ok([widths.min, widths.max]);
}
if self.replacement.is_some() {
let inline = self.inline_axis();
let width = self.replaced_extent(inline, self.content[1 - inline]);
return Ok([width; 2]);
}
let atomics: Vec<_> = self
.images
.0
.iter()
.map(|&[width, _]| crate::text::IntrinsicWidths {
min: width,
max: width,
})
.collect();
let intrinsic = self
.paragraph
.as_ref()
.map(|p| measurer.intrinsic_widths(p, &atomics))
.transpose()?
.unwrap_or_default();
Ok([intrinsic.min, intrinsic.max])
}
fn block_extent(&self) -> f32 {
if let Some(measured) = &self.measured {
return if measured.through {
let mut margins = measured.before.clone();
margins.merge(measured.after.clone());
margins.value()
} else {
measured.before.value() + measured.height + measured.after.value()
};
}
if self.replacement.is_some() {
let inline = self.inline_axis();
return self.replaced_extent(1 - inline, self.content[inline]);
}
self.lines.iter().map(|line| line.height).sum()
}
fn replaced_extent(&self, axis: usize, other: f32) -> f32 {
let image = self
.replacement
.as_ref()
.expect("only a replaced margin box has replaced content");
let natural = crate::paint::natural_points(
image,
crate::paint::INITIAL_IMAGE_SCALE,
self.margin.style.effective_zoom,
);
let mut size = [
crate::paint::SizeComponent::Auto,
crate::paint::SizeComponent::Auto,
];
size[1 - axis] =
crate::paint::SizeComponent::Value(crate::paint::LengthPercentage::Length(other));
crate::paint::image_size(
&crate::paint::BackgroundSize::Explicit(size),
natural,
crate::images::DEFAULT_OBJECT_SIZE
.map(|extent| extent * crate::style::POINTS_PER_PIXEL),
)[axis]
}
fn paint(
mut self,
origin: [f32; 2],
index: usize,
output: &mut MarginOutput<'_>,
measurer: &mut Measurer<'t>,
destinations: usize,
) -> Result<(), LayoutError> {
self.validate()?;
let block_axis = self.block_axis();
let mut used = 0.0;
let mut retained = 0;
for line in &self.lines {
if self.content[block_axis] < 0.0
|| (retained > 0 && used + line.height > self.content[block_axis])
{
break;
}
used += line.height;
retained += 1;
}
self.lines.truncate(retained);
let rect = Rect {
x: origin[0] + self.start[0],
y: origin[1] + self.start[1],
width: self.content[0] + self.edges(0),
height: self.content[1] + self.edges(1),
};
if [
rect.x,
rect.y,
rect.width,
rect.height,
rect.x + rect.width,
rect.y + rect.height,
]
.iter()
.any(|value| !value.is_finite())
{
return Err(LayoutError::NonFinitePageMarginGeometry {
location: self.margin.location,
});
}
let paint = BoxPaint {
padding: self.padding,
..BoxPaint::whole(rect, self.decoration.clone())
};
let outline = paint.outline();
let replaced = self
.replacement
.take()
.map(|image| self.replaced(image, &paint));
let clips_to_text = self.decoration.backgrounds.clips_to_text();
let text = clips_to_text || output.page_text.is_some();
let painted = self.decoration.is_visible().then(|| {
output.display.push(DisplayItem::Box(paint));
output.display.len() - 1
});
let content = output.display.len();
let extent = self.block_extent();
let text_area = if let Some(measured) = self.measured.take() {
self.paint_element(
measured,
extent,
rect,
output,
measurer,
(destinations, text),
)?
} else if let Some(image) = replaced {
output.display.push(DisplayItem::Image(image));
text.then(|| text_flow(&[], &[], rect))
} else {
self.paint_lines(rect, index, output.lines, output.display);
text.then(|| text_flow(&output.display[content..], output.lines, rect))
};
if let (Some(page_text), Some(area)) = (&mut output.page_text, &text_area) {
page_text
.items
.push(DisplayItem::Frame(Box::new(area.clone())));
}
if clips_to_text
&& let (Some(slot), Some(area)) = (painted, text_area)
&& let DisplayItem::Box(paint) = &mut output.display[slot]
{
paint.text_area = Some(std::sync::Arc::new(area));
}
output.display.extend(outline.map(DisplayItem::Box));
Ok(())
}
fn replaced(
&self,
image: std::sync::Arc<crate::images::Image>,
paint: &BoxPaint,
) -> ImagePaint {
let style = &self.margin.style;
let clip = paint.inner_clip(self.padding, [true; 2]);
let content = clip.rect;
let box_size = [content.width, content.height];
let [width, height] = if image.natural_size() == Default::default() {
box_size
} else {
let scale = crate::style::POINTS_PER_PIXEL * style.effective_zoom;
let natural = image
.pixel_size(content.width / scale)
.map(|extent| extent * scale);
style.object_fit.size(natural, box_size)
};
ImagePaint {
image,
rect: Rect {
x: content.x + style.object_position[0].resolve(content.width - width),
y: content.y + style.object_position[1].resolve(content.height - height),
width,
height,
},
clip: Some(clip),
source_scale: style.effective_zoom,
}
}
fn vertical_offset(&self, physical_height: f32) -> f32 {
let extra = self.content[VERTICAL] - physical_height;
match self.margin.style.table.vertical_align {
VerticalAlign::Middle => extra / 2.0,
VerticalAlign::Bottom => extra,
VerticalAlign::Top
| VerticalAlign::Baseline
| VerticalAlign::BaselineMiddle
| VerticalAlign::Sub
| VerticalAlign::Super
| VerticalAlign::TextTop
| VerticalAlign::TextBottom
| VerticalAlign::Length(_) => 0.0,
}
}
fn content_origin(&self, rect: Rect, physical_height: f32) -> [f32; 2] {
[
rect.x + self.padding[3] + self.borders[3],
rect.y + self.padding[0] + self.borders[0] + self.vertical_offset(physical_height),
]
}
fn paint_element(
&self,
measured: MeasuredBlock<'t>,
extent: f32,
rect: Rect,
output: &mut MarginOutput<'_>,
measurer: &mut Measurer<'t>,
(destinations, text_area): (usize, bool),
) -> Result<Option<FlowPaint>, LayoutError> {
let vertical = self.inline_axis() == VERTICAL;
let logical = [self.content[self.inline_axis()], extent];
let size = if vertical {
[logical[1], logical[0]]
} else {
logical
};
let flow = crate::layout::pagination::Paginator::standalone(
measurer,
measured,
Rect {
x: 0.0,
y: 0.0,
width: logical[0],
height: logical[1],
},
destinations,
text_area,
)?;
let margin_flow = crate::style::logical::Flow::used(&self.margin.style);
let frame = crate::layout::writing::Frame::physical(margin_flow, size);
measurer
.element_targets
.displayed
.borrow_mut()
.adopt(flow.renderings.physical(margin_flow));
let mut items = flow.items;
let mut text_area = flow.text_area;
let mut navigation = flow.navigation.into_links();
if vertical {
frame.items(&mut items);
if let Some(area) = &mut text_area {
frame.items(&mut area.items);
}
for area in &mut navigation {
match &mut area.shape {
crate::layout::LinkShape::Rect(rect) => *rect = frame.rect(*rect),
crate::layout::LinkShape::Quads(quads) => {
for point in quads.iter_mut().flatten() {
*point = frame.point(*point);
}
}
}
}
}
let origin = self.content_origin(rect, size[1]);
for mut area in navigation {
match &mut area.shape {
crate::layout::LinkShape::Rect(rect) => {
rect.x += origin[0];
rect.y += origin[1];
}
crate::layout::LinkShape::Quads(quads) => {
for point in quads.iter_mut().flatten() {
point[0] += origin[0];
point[1] += origin[1];
}
}
}
output.links.push(area);
}
let matrix = [1.0, 0.0, 0.0, 1.0, origin[0], origin[1]];
let area = Rect {
x: 0.0,
y: 0.0,
width: size[0],
height: size[1],
};
output.display.push(DisplayItem::Frame(Box::new(FlowPaint {
matrix,
area,
items,
lines: flow.lines,
})));
Ok(text_area.map(|text| FlowPaint {
matrix,
area,
..text
}))
}
fn paint_lines(
self,
rect: Rect,
index: usize,
page: &mut Vec<TextLine>,
display: &mut Vec<DisplayItem>,
) {
let style = &self.margin.style;
let vertical = self.inline_axis() == VERTICAL;
let inline_extent = if vertical {
self.lines.iter().map(|line| line.width).fold(0.0, f32::max)
} else {
self.content[HORIZONTAL]
};
let physical_height = if vertical {
inline_extent
} else {
self.block_extent()
};
let content_origin = self.content_origin(rect, physical_height);
let mut frame_lines = Vec::new();
let mut frame_items = Vec::new();
let (text_lines, text_items) = if vertical {
(&mut frame_lines, &mut frame_items)
} else {
(&mut *page, &mut *display)
};
let mut y = if vertical { 0.0 } else { content_origin[1] };
for line in self.lines {
let x = if vertical { 0.0 } else { content_origin[0] }
+ line.alignment_offset(inline_extent, style.used_rtl());
let baseline = y + line.ascent;
let [below, above] =
crate::layout::inline::decoration_items(&line.decorations, x, baseline);
text_items.extend(below);
text_items.push(DisplayItem::Text {
line: text_lines.len(),
});
text_items.extend(above);
let paragraph = self
.paragraph
.as_ref()
.expect("a margin box's lines lay out its paragraph");
text_items.extend(
crate::layout::markers::content_image_paints(paragraph, &line, &self.images.0)
.into_iter()
.map(|mut image| {
image.rect.x += x;
image.rect.y += baseline;
DisplayItem::Image(image)
}),
);
text_lines.push(TextLine {
paragraph: index,
text: line.text,
x,
baseline,
width: line.width,
height: line.height,
runs: line.runs,
font_size: style.size,
color: style.color,
});
y += line.height;
}
if vertical {
let size = [self.content[HORIZONTAL], inline_extent];
crate::layout::writing::Frame::physical(crate::style::logical::Flow::used(style), size)
.items(&mut frame_items);
display.push(DisplayItem::Frame(Box::new(FlowPaint {
matrix: [1.0, 0.0, 0.0, 1.0, content_origin[0], content_origin[1]],
area: Rect {
x: 0.0,
y: 0.0,
width: size[0],
height: size[1],
},
items: frame_items,
lines: frame_lines,
})));
}
}
}
fn text_flow(items: &[DisplayItem], lines: &[TextLine], area: Rect) -> FlowPaint {
let mut flow = FlowPaint {
matrix: [1.0, 0.0, 0.0, 1.0, 0.0, 0.0],
area,
items: Vec::new(),
lines: Vec::new(),
};
for item in items {
match item {
DisplayItem::Text { line } => {
flow.lines.push(lines[*line].clone().opaque());
flow.items.push(DisplayItem::Text {
line: flow.lines.len() - 1,
});
}
DisplayItem::Decoration(decoration) => {
flow.items
.push(DisplayItem::Decoration(decoration.opaque()));
}
DisplayItem::Frame(frame) => {
flow.items.push(DisplayItem::Frame(Box::new(FlowPaint {
matrix: frame.matrix,
..text_flow(&frame.items, &frame.lines, frame.area)
})));
}
DisplayItem::PushTransform(_) | DisplayItem::Pop => flow.items.push(item.clone()),
DisplayItem::Image(_) => {}
DisplayItem::Box(_)
| DisplayItem::TextShadow(_)
| DisplayItem::PushClip(_)
| DisplayItem::PushOpacity(_)
| DisplayItem::PushBlend(_)
| DisplayItem::PushFilter(_)
| DisplayItem::PushMask(_)
| DisplayItem::PushBackdropRoot
| DisplayItem::Backdrop(_) => {
unreachable!("margin lines paint text, decorations, images and their transforms")
}
}
}
flow
}
struct MarginOutput<'p> {
lines: &'p mut Vec<TextLine>,
display: &'p mut Vec<DisplayItem>,
links: &'p mut Vec<crate::layout::LinkArea>,
page_text: Option<&'p mut FlowPaint>,
}
pub(super) struct MarginContext<'s, 't> {
pub resolver: &'s dyn crate::resources::ResourceResolver,
pub strings: &'s crate::layout::strings::PageStrings,
pub running: &'t [RunningStringAssignment],
pub destinations: usize,
pub counters: &'s crate::layout::page_counters::PageCounters,
pub document: &'s crate::document::StyledDocument,
}
pub(super) fn running(
page_boxes: &[std::sync::Arc<PageBox>],
running: &[RunningStringAssignment],
strings: &crate::layout::strings::PageStrings,
counters: &crate::layout::page_counters::PageCounters,
) -> Vec<(crate::style::PageStyleTarget, i128)> {
page_boxes
.iter()
.enumerate()
.flat_map(|(page, page_box)| {
page_box.margins.iter().filter_map(move |margin| {
let request = margin.margin.content.running_element()?;
let element = running[strings.element(page, request)?].element.as_ref()?;
Some((
element.target,
counters.page(page, Some(margin.margin.location)),
))
})
})
.collect()
}
fn prepare<'a, 't>(
margin: &'a PageMargin,
decoration: &'a BoxDecoration,
containing: [f32; 2],
page: usize,
pages: usize,
context: &MarginContext<'_, 't>,
) -> Result<Prepared<'a, 't>, LayoutError> {
let style = &margin.style;
let bases = [containing[1], containing[0], containing[1], containing[0]];
let padding = std::array::from_fn(|i| style.padding[i].resolve(bases[i]));
let borders = style
.decoration
.borders
.map(crate::graphics::Border::used_width);
let margins = std::array::from_fn(|i| style.margin[i].resolve(bases[i]));
let mut specified = [style.width.resolve(containing[0]), None];
if style.box_sizing == BoxSizing::BorderBox {
for axis in [HORIZONTAL, VERTICAL] {
let edges = Prepared::sides(axis)
.iter()
.map(|&i| padding[i] + borders[i])
.sum::<f32>();
specified[axis] = specified[axis].map(|size| (size - edges).max(0.0));
}
}
let mut replacement = None;
let (paragraph, element) = if let Some(request) = margin.content.running_element() {
(
None,
context
.strings
.element(page, request)
.and_then(|owner| context.running[owner].element.as_ref())
.map(|element| {
element.block(context.counters.page(page, Some(margin.location)), pages)
}),
)
} else if let Some(image) = margin.content.replacement() {
replacement = Some(
context
.document
.margin_replacement(image, style, context.resolver),
);
(None, None)
} else {
let output = margin.content.page_output(
context.counters.page(page, Some(margin.location)),
pages,
crate::document::MAX_INLINE_TEXT_BYTES,
&style.quotes,
&mut |request| Ok(context.strings.text(page, request)),
&mut |request| {
context.counters.text(
page,
margin.location,
request,
context
.document
.page_counters(page)
.unwrap_or(&CounterSnapshot::default()),
)
},
)?;
let images = context
.document
.margin_images(&output, style, context.resolver);
(Paragraph::generated_output(&output, &images, style)?, None)
};
let images = paragraph
.as_ref()
.map(crate::layout::markers::content_image_atomics)
.unwrap_or_default();
Ok(Prepared {
margin,
decoration,
paragraph,
images,
replacement,
element,
measured: None,
padding,
borders,
margins,
specified,
start: [0.0; 2],
content: [0.0; 2],
lines: Vec::new(),
})
}
pub(super) fn render<'t>(
page_boxes: &[std::sync::Arc<PageBox>],
context: &MarginContext<'_, 't>,
measurer: &mut Measurer<'t>,
first_index: usize,
(pages, display, links, text_areas): crate::layout::PageOutputs<'_>,
) -> Result<(), LayoutError> {
let total = pages.len();
for (number, (((page_lines, display), links), page_text)) in pages
.iter_mut()
.zip(display)
.zip(links.iter_mut())
.zip(text_areas.iter_mut())
.enumerate()
{
let page = &page_boxes[number];
let margins: Vec<_> = page
.margins
.iter()
.map(|margin| (&margin.margin, &margin.decoration))
.collect();
if margins.is_empty() {
continue;
}
let page_box = page.geometry.margin_box();
let [width, height] = [page_box.width, page_box.height];
let [top, right, bottom, left] = page.geometry.margin;
let groups = [
([left, 0.0], [width - left - right, top], HORIZONTAL),
(
[left, height - bottom],
[width - left - right, bottom],
HORIZONTAL,
),
([0.0, top], [left, height - top - bottom], VERTICAL),
(
[width - right, top],
[right, height - top - bottom],
VERTICAL,
),
];
let page = page_lines;
for (locations, (origin, containing, axis)) in GROUPS.iter().zip(groups) {
let mut boxes: [Option<Prepared<'_, '_>>; 3] = [None, None, None];
let mut dimensions = [Variable::default(); 3];
for (position, location) in locations.iter().enumerate() {
let Some((margin, decoration)) = margins
.iter()
.find(|(margin, _)| margin.location == *location)
else {
continue;
};
let mut prepared = prepare(margin, decoration, containing, number, total, context)?;
prepared.validate()?;
let fixed_axis = 1 - axis;
prepared.fixed(fixed_axis, containing[fixed_axis]);
let bounds = if axis == prepared.inline_axis() {
prepared.intrinsic(measurer, containing[axis])?
} else {
prepared.layout(measurer)?;
[prepared.block_extent(); 2]
};
if axis == HORIZONTAL && prepared.margin.style.width.intrinsic().is_some() {
let available = containing[axis] - prepared.outer_edges(axis);
let style = &prepared.margin.style;
let sizes = [&style.width, &Size::Auto, &Size::Auto];
prepared.specified[axis] = crate::style::SizeConstraints::new(
sizes,
Some(containing[axis]),
style.box_sizing,
prepared.edges(axis),
)
.with_intrinsic(
sizes,
crate::style::IntrinsicWidths {
min: bounds[0],
max: bounds[1],
},
Some(available.max(0.0)),
)
.specified;
}
let edges = prepared.outer_edges(axis);
dimensions[position] = Variable {
min: bounds[0] + edges,
max: bounds[1] + edges,
specified: prepared.specified[axis].map(|size| size + edges),
generated: true,
};
boxes[position] = Some(prepared);
}
let sizes = variable(dimensions, containing[axis]);
for (position, prepared) in boxes.into_iter().enumerate() {
if let Some(mut prepared) = prepared {
prepared.variable(axis, containing[axis], sizes[position], position);
prepared.layout(measurer)?;
let index = first_index + prepared.margin.location as usize;
prepared.paint(
origin,
index,
&mut MarginOutput {
lines: page,
display,
links,
page_text: page_text.as_mut(),
},
measurer,
context.destinations,
)?;
}
}
}
for (location, origin, containing) in [
(MarginLocation::TopLeftCorner, [0.0, 0.0], [left, top]),
(
MarginLocation::TopRightCorner,
[width - right, 0.0],
[right, top],
),
(
MarginLocation::BottomLeftCorner,
[0.0, height - bottom],
[left, bottom],
),
(
MarginLocation::BottomRightCorner,
[width - right, height - bottom],
[right, bottom],
),
] {
if let Some((margin, decoration)) = margins
.iter()
.find(|(margin, _)| margin.location == location)
{
let mut prepared = prepare(margin, decoration, containing, number, total, context)?;
prepared.validate()?;
for axis in [HORIZONTAL, VERTICAL] {
prepared.fixed(axis, containing[axis]);
}
prepared.layout(measurer)?;
prepared.paint(
origin,
first_index + location as usize,
&mut MarginOutput {
lines: page,
display,
links,
page_text: page_text.as_mut(),
},
measurer,
context.destinations,
)?;
}
}
}
Ok(())
}