Skip to main content

fleuron/layout/
fragment.rs

1//! What the flow can move: one line, one image, one ornament,
2//! and what the blocks around it paint.
3
4use std::sync::Arc;
5
6use crate::content::NodeId;
7use crate::lines::Line;
8use crate::pages::{DrawItem, PageBox};
9use crate::style::{
10    BlendMode, BorderRadius, BoxDecorationBreak, Break, Clear, Color, ComputedStyle, Edges, Float,
11};
12
13use super::background::Backdrop;
14
15use super::build::Reflow;
16use super::note::Note;
17
18/// Whether a page may end above a fragment.
19///
20/// Everything the cascade says about fragmentation — `break-before`,
21/// `break-after`, `break-inside`, `orphans`, `widows` — reaches the
22/// flow as one of these, decided while the fragments are built.
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum BreakPoint {
25    /// A page break must fall here, on the side the break names.
26    Forced(Break),
27    /// A break may fall here.
28    Allowed,
29    /// A break may not fall here: this fragment moves with the one
30    /// above it.
31    Forbidden,
32}
33
34/// What one fragment paints.
35#[derive(Debug, Clone)]
36pub enum Piece {
37    /// A laid-out line, and the initial letter sunk beside it.
38    Line {
39        /// The line itself, shaped and measured.
40        line: Line,
41        /// The drop cap set beside this line, on the first line of a
42        /// paragraph that has one. Boxed: a book has thousands of
43        /// lines and a handful of initial letters.
44        cap: Option<Box<DropCap>>,
45    },
46    /// A placed image, sized against the content box.
47    Image {
48        /// Width in points, after any scaling.
49        width: f32,
50        /// Height in points, after any scaling.
51        height: f32,
52        /// Index into the asset table.
53        asset: u32,
54        /// How the image is mixed with what is under it.
55        blend: BlendMode,
56    },
57    /// Space with nothing in it: what a thematic break set in space
58    /// rather than in an ornament comes to.
59    Blank,
60    /// Where an image the sheet lifted out of the flow was written.
61    /// It takes no space and paints nothing. The page the flow
62    /// reaches here is the page that places the image.
63    Anchor(NodeId),
64    /// One row of a table, set whole.
65    Row(Box<TableRow>),
66    /// An image floated to one side of its column. It takes no height
67    /// in the column, and the prose after it sets beside it.
68    Float(Box<Floated>),
69}
70
71/// An image floated to one side of its column. The fragment's `x` is
72/// the leading edge of its margin box and its height is the height of
73/// that box, which is what the prose beside it keeps clear of.
74#[derive(Debug, Clone)]
75pub struct Floated {
76    /// The image's own node.
77    pub node: NodeId,
78    /// The side of the column it stands at.
79    pub side: Float,
80    /// Index into the asset table.
81    pub asset: u32,
82    /// Leading edge of the image, from the margin box's own.
83    pub x: f32,
84    /// Top of the image, from the margin box's own.
85    pub y: f32,
86    /// Width of the image in points, after any scaling.
87    pub width: f32,
88    /// Height of the image in points, after any scaling.
89    pub height: f32,
90    /// Width of the margin box.
91    pub outer: f32,
92    /// How the image mixes with what is behind it.
93    pub blend: BlendMode,
94}
95
96/// One row of a table, set whole: what it paints, and what the flow
97/// needs to set the table's header rows again on a new page.
98#[derive(Debug, Clone)]
99pub struct TableRow {
100    /// What the row paints, from the top of the row and the leading
101    /// edge of the content box.
102    pub items: Vec<DrawItem>,
103    /// The border boxes of the row, its cells, and the blocks inside
104    /// them, from the top of the row and the leading edge of the
105    /// content box.
106    pub boxes: Vec<(NodeId, PageBox)>,
107    /// Whether this is the first row of its table.
108    pub opens: bool,
109    /// Whether this is a header row, which is set again at the top of
110    /// every page or column the table continues onto.
111    pub head: bool,
112    /// Whether the header rows are set above this row when it is the
113    /// first thing on a page or in a column.
114    pub repeats: bool,
115}
116
117/// An initial letter sunk beside the lines that follow it.
118#[derive(Debug, Clone)]
119pub struct DropCap {
120    /// The letter, shaped at the size the sink works out to.
121    pub line: Line,
122    /// Leading edge, from the content box's own.
123    pub x: f32,
124    /// How far the cap's baseline sits below its line's.
125    pub drop: f32,
126}
127
128/// The marker of a list item, set to the left of the item on the
129/// baseline of its first line.
130#[derive(Debug, Clone)]
131pub struct Marker {
132    /// The marker and the space after it, shaped in the item's style.
133    pub line: Line,
134    /// Leading edge, from the content box's own.
135    pub x: f32,
136}
137
138/// One thing the flow can place: a line, an image, an ornament.
139///
140/// Everything horizontal is settled here — indentation, alignment,
141/// the measure a drop cap left — so the flow only stacks.
142#[derive(Debug, Clone)]
143pub struct Fragment {
144    /// Leading edge, from the content box's own.
145    pub x: f32,
146    /// Space above, from the margins around it. A page that opens on
147    /// this fragment drops it.
148    pub lead: f32,
149    /// Space above that a page break does not drop and no margin
150    /// collapses through: the borders and padding between this
151    /// fragment and whatever is above it.
152    pub fixed: f32,
153    /// The fragment's own height.
154    pub height: f32,
155    /// Whether a page may end above it.
156    pub break_before: BreakPoint,
157    /// What it paints.
158    pub piece: Piece,
159    /// What it tells the page furniture when it lands. Boxed because
160    /// a book has thousands of fragments and a handful of chapter
161    /// headings.
162    pub marks: Option<Box<Marks>>,
163    /// The decorated blocks this fragment opens and closes. Boxed
164    /// for the same reason: most fragments decorate nothing.
165    pub decorations: Option<Box<Decorations>>,
166    /// The markers of the list items that open on this fragment,
167    /// outermost first. Boxed, because most fragments open no item.
168    pub markers: Option<Box<Vec<Marker>>>,
169    /// The notes whose references were set on this fragment, in the
170    /// order they were written. They are set at the foot of whichever
171    /// page the fragment lands on. Boxed: a book has thousands of
172    /// lines and a handful of notes.
173    pub notes: Option<Box<Vec<Arc<Note>>>>,
174    /// The paragraph this line came out of, shared by every line of
175    /// it, and `None` on everything else. The flow reads it where an
176    /// image narrows the bands the paragraph is set in. A book that
177    /// anchors nothing keeps none of this.
178    pub reflow: Option<Arc<Reflow>>,
179    /// Whether it came out of a block that spans every column, from
180    /// `column-span: all`. Its leading edge and its measure are then
181    /// the whole content box's rather than one column's.
182    pub spanning: bool,
183    /// The layer the fragment's own items paint in, from the
184    /// `z-index` of the block it came out of. A row carries the
185    /// layers of the table it is part of on its items, so this is not
186    /// read for one.
187    pub layer: i32,
188    /// How far `position: relative` moves what the fragment paints,
189    /// across and down, from the blocks it came out of. Nothing around
190    /// the fragment moves with it.
191    pub offset: (f32, f32),
192    /// How much of what the fragment paints shows, from 0 to 1: the
193    /// `opacity` of the blocks it came out of, multiplied together.
194    pub opacity: f32,
195    /// Which floats it starts below, from the `clear` of the block
196    /// whose first fragment it is.
197    pub clear: Clear,
198}
199
200impl Fragment {
201    /// A fragment with nothing above it: what a block emits before
202    /// the margins and breaks around it are folded in.
203    pub(super) fn plain(x: f32, height: f32, piece: Piece) -> Fragment {
204        Fragment {
205            x,
206            lead: 0.0,
207            fixed: 0.0,
208            height,
209            break_before: BreakPoint::Allowed,
210            piece,
211            marks: None,
212            decorations: None,
213            markers: None,
214            notes: None,
215            reflow: None,
216            spanning: false,
217            layer: 0,
218            offset: (0.0, 0.0),
219            opacity: 1.0,
220            clear: Clear::None,
221        }
222    }
223
224    /// Whether it stands outside the height of the column: an anchor,
225    /// or a float.
226    pub(super) fn aside(&self) -> bool {
227        matches!(self.piece, Piece::Anchor(_) | Piece::Float(_))
228    }
229}
230
231/// What one fragment does to the blocks decorated around it.
232///
233/// A decoration spans a range of fragments. The range is settled
234/// while the flow is built and the geometry is not: the paginator
235/// places fragments one at a time and moves what it has already
236/// painted when one carries to the next page. So the range travels on
237/// the fragments at its ends, and the paginator resolves it, per
238/// page, into a border box over the fragments that landed there.
239#[derive(Debug, Clone, Default)]
240pub struct Decorations {
241    /// The blocks whose first fragment this is, outermost first.
242    pub opens: Vec<Decoration>,
243    /// How many of the open blocks end with this fragment,
244    /// innermost first.
245    pub closes: u32,
246}
247
248/// One block: what it paints, and where it sits around the fragments
249/// it spans.
250#[derive(Debug, Clone)]
251pub struct Decoration {
252    /// The content node the block stands for.
253    pub node: NodeId,
254    /// Whether the block paints a background or a border. A block that
255    /// paints neither still has a border box to answer for.
256    pub paints: bool,
257    /// Leading edge of the border box, from the content box's own.
258    pub x: f32,
259    /// Width of the border box.
260    pub width: f32,
261    /// Distance from the top of the block's first fragment up to the
262    /// top of its border box.
263    pub above: f32,
264    /// Distance from the bottom of its last fragment down to the
265    /// bottom of its border box.
266    pub below: f32,
267    /// Border widths, zero on an edge that is not drawn.
268    pub border: Edges,
269    /// What each edge is painted in, `currentColor` resolved.
270    pub colors: Edges<Color>,
271    /// How far each corner of the border box is rounded, before the
272    /// box's own size is known.
273    pub radius: BorderRadius,
274    /// What is painted behind the whole border box: the tint, and
275    /// the image over it.
276    pub(super) backdrop: Backdrop,
277    /// Whether `box-decoration-break: clone` closes the two edges a
278    /// page break cuts.
279    pub cloned: bool,
280    /// The layer the border box paints in, from the block's
281    /// `z-index`.
282    pub layer: i32,
283    /// How far `position: relative` moves the border box, across and
284    /// down.
285    pub offset: (f32, f32),
286    /// How much of what the border box paints shows, from 0 to 1: the
287    /// `opacity` of the block and the blocks around it, multiplied
288    /// together.
289    pub opacity: f32,
290    /// How what the border box paints is mixed with what is under it.
291    pub blend: BlendMode,
292}
293
294/// What a fragment tells the page it lands on: the running strings
295/// its element set, and the folio its page restarts at.
296///
297/// Both are captured from the content flow, so both are answers only
298/// pagination has: which page a heading fell on is not known until it
299/// falls there.
300#[derive(Debug, Clone, Default, PartialEq)]
301pub struct Marks {
302    /// Named strings, resolved from the element's own text, in the
303    /// order the cascade gave them.
304    pub strings: Vec<(String, String)>,
305    /// The folio of the page this fragment lands on.
306    pub page_number: Option<u32>,
307    /// The nodes a link can reach that open on this fragment. The page
308    /// it lands on is the page a reference to one of them prints.
309    pub targets: Vec<crate::content::NodeId>,
310}
311
312/// Whether a block paints anything behind or around its content.
313pub(super) fn decorated(style: &ComputedStyle) -> bool {
314    style.background.paints() || style.border.paints()
315}
316
317/// The border box of one block, and what it paints there. `x` and
318/// `measure` are what the block was laid out against; the border box
319/// takes its margins off them. `backdrop` is what the cascade put
320/// behind it, resolved against the asset table. `offset` is the sum of
321/// the moves of the relative blocks around it, itself included, and
322/// `opacity` the product of their `opacity`, beside it.
323pub(super) fn decoration(
324    node: NodeId,
325    style: &ComputedStyle,
326    x: f32,
327    measure: f32,
328    backdrop: Backdrop,
329    (offset, opacity): ((f32, f32), f32),
330) -> Decoration {
331    let (left, width) = style.border_box(x, measure);
332    let border = style.border.widths();
333    let ink = |edge: crate::style::Border| edge.color.unwrap_or(style.color);
334    Decoration {
335        node,
336        paints: decorated(style),
337        x: left,
338        width,
339        above: 0.0,
340        below: 0.0,
341        border,
342        colors: Edges {
343            top: ink(style.border.top),
344            right: ink(style.border.right),
345            bottom: ink(style.border.bottom),
346            left: ink(style.border.left),
347        },
348        radius: style.border_radius,
349        backdrop,
350        cloned: style.box_decoration_break == BoxDecorationBreak::Clone,
351        layer: style.z_index,
352        offset,
353        opacity,
354        blend: style.blend(),
355    }
356}