fleuron/style/properties/page.rs
1//! The page box: its trim, its margins, the columns it divides
2//! into, and the margin boxes around it.
3
4use serde::Serialize;
5
6use super::edges::{BorderStyle, Edges, MEDIUM};
7
8/// The rule painted down a page's gutters, from `column-rule-width`
9/// and `column-rule-style`. It takes no room: the gutter is the room
10/// it has.
11#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
12pub struct ColumnRule {
13 /// How the rule is drawn.
14 pub style: BorderStyle,
15 /// Thickness in points, whether or not it is drawn.
16 pub width: f32,
17}
18
19impl ColumnRule {
20 /// The initial rule: `medium` wide, and not drawn.
21 pub const NONE: ColumnRule = ColumnRule {
22 style: BorderStyle::None,
23 width: MEDIUM,
24 };
25
26 /// The thickness the rule paints at: nothing unless it is drawn.
27 pub fn used(self) -> f32 {
28 match self.style {
29 BorderStyle::None => 0.0,
30 BorderStyle::Solid => self.width.max(0.0),
31 }
32 }
33}
34
35/// How a page's content box divides, from `column-count`,
36/// `column-width` and `column-gap`.
37///
38/// Both `column-count` and `column-width` are what the author asked
39/// for rather than what the page does: a content box only so wide
40/// takes only so many columns of a given width, and
41/// [`PageGeometry::column_count`] is where the two meet.
42#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
43pub struct Columns {
44 /// `column-count`, or `None` for `auto`.
45 #[serde(skip_serializing_if = "Option::is_none")]
46 pub count: Option<u32>,
47 /// `column-width`: the width a column is asked to have, or
48 /// `None` for `auto`.
49 #[serde(skip_serializing_if = "Option::is_none")]
50 pub width: Option<f32>,
51 /// `column-gap`: the gutter between two columns.
52 pub gap: f32,
53 /// What is painted down each gutter.
54 pub rule: ColumnRule,
55}
56
57impl Columns {
58 /// One column, the whole content box: what a page that declares
59 /// no column property divides into.
60 pub const fn undivided(gap: f32) -> Columns {
61 Columns {
62 count: None,
63 width: None,
64 gap,
65 rule: ColumnRule::NONE,
66 }
67 }
68
69 /// Whether the page asked for no division at all. A page box
70 /// that divides into nothing serializes without a `columns`
71 /// field.
72 pub fn single(&self) -> bool {
73 self.count.is_none() && self.width.is_none()
74 }
75}
76
77/// Where a page's content sits down its content box, from
78/// `align-content` on `@page`.
79#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
80#[serde(rename_all = "snake_case")]
81pub enum AlignContent {
82 /// `start`: against the top of the content box.
83 Start,
84 /// `center`: the same distance from the top and the bottom.
85 Center,
86 /// `end`: against the bottom of the content box.
87 End,
88}
89
90impl AlignContent {
91 /// Whether the content sits at the top, which is where it sits
92 /// when a page names nothing.
93 pub fn is_start(&self) -> bool {
94 *self == AlignContent::Start
95 }
96}
97
98/// The marks a printer cuts and registers by, from `marks` on
99/// `@page`. They paint outside the bleed, in the slug.
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize)]
101pub struct Marks {
102 /// `crop`: a short line off each corner of the trim, along each
103 /// edge that meets there.
104 pub crop: bool,
105 /// `cross`: a registration target off the middle of each edge.
106 pub cross: bool,
107}
108
109impl Marks {
110 /// `none`: no mark at all.
111 pub const NONE: Marks = Marks {
112 crop: false,
113 cross: false,
114 };
115
116 /// Whether no mark is drawn.
117 pub fn is_none(&self) -> bool {
118 *self == Marks::NONE
119 }
120}
121
122/// How far past the trim `bleed: auto` reaches where the page has crop
123/// marks: 6pt, the figure CSS gives.
124const AUTO_BLEED: f32 = 6.0;
125
126/// How far past the bleed the room for marks reaches, in points.
127pub(crate) const SLUG: f32 = 24.0;
128
129/// Page trim, margins and columns, in points: the resolved `@page`
130/// box.
131#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
132pub struct PageGeometry {
133 /// Trimmed page width.
134 pub width: f32,
135 /// Trimmed page height.
136 pub height: f32,
137 /// Margins, resolved from the page's own `margin`. Mirroring
138 /// across the spread is `@page :left` and `@page :right` saying
139 /// different things, not a property of its own.
140 pub margin: Edges,
141 /// How the content box divides.
142 #[serde(skip_serializing_if = "Columns::single")]
143 pub columns: Columns,
144 /// Where the content of a page that ends short of the foot of its
145 /// content box sits.
146 #[serde(skip_serializing_if = "AlignContent::is_start")]
147 pub align_content: AlignContent,
148 /// `bleed`: how far past the trim the page background reaches, in
149 /// points, or `None` for `auto`. [`PageGeometry::bleed`] resolves
150 /// `auto`.
151 #[serde(skip_serializing_if = "Option::is_none")]
152 pub bleed: Option<f32>,
153 /// `marks`: what a printer cuts and registers by.
154 #[serde(skip_serializing_if = "Marks::is_none")]
155 pub marks: Marks,
156}
157
158impl PageGeometry {
159 /// How far past the trim the page background reaches, in points.
160 /// `auto` is 6pt on a page with crop marks and nothing on one
161 /// without.
162 pub fn bleed(self) -> f32 {
163 match self.bleed {
164 Some(bleed) => bleed.max(0.0),
165 None if self.marks.crop => AUTO_BLEED,
166 None => 0.0,
167 }
168 }
169
170 /// How far past the bleed the room for marks reaches, in points:
171 /// 24pt on a page with marks and nothing on one without.
172 pub fn slug(self) -> f32 {
173 if self.marks.is_none() { 0.0 } else { SLUG }
174 }
175
176 /// Origin (top-left) of the content box, in page coordinates.
177 pub fn content_origin(self) -> (f32, f32) {
178 (self.margin.left, self.margin.top)
179 }
180
181 /// Size of the content box.
182 pub fn content_size(self) -> (f32, f32) {
183 (
184 self.width - self.margin.left - self.margin.right,
185 self.height - self.margin.top - self.margin.bottom,
186 )
187 }
188
189 /// How many columns the content box divides into.
190 ///
191 /// A declared `column-width` is a preference: as many columns of
192 /// that width as fit, and where `column-count` was declared too
193 /// it is the ceiling on that. A box too narrow for one column of
194 /// that width still divides into one.
195 pub fn column_count(self) -> u32 {
196 let available = self.content_size().0;
197 let fitting = self.columns.width.map(|width| {
198 let pitch = width.max(0.0) + self.columns.gap;
199 if pitch <= 0.0 {
200 return 1;
201 }
202 (((available + self.columns.gap) / pitch).floor() as i32).max(1) as u32
203 });
204 match (self.columns.count, fitting) {
205 (Some(count), Some(fitting)) => count.max(1).min(fitting),
206 (Some(count), None) => count.max(1),
207 (None, Some(fitting)) => fitting,
208 (None, None) => 1,
209 }
210 }
211
212 /// The measure line layout breaks to: one column's width, which
213 /// on an undivided page is the content box's own.
214 pub fn measure(self) -> f32 {
215 let count = self.column_count() as f32;
216 let gutters = self.columns.gap * (count - 1.0);
217 ((self.content_size().0 - gutters) / count).max(0.0)
218 }
219
220 /// Origin (top-left) of one column, in page coordinates.
221 pub fn column_origin(self, index: u32) -> (f32, f32) {
222 let (x, y) = self.content_origin();
223 (x + index as f32 * (self.measure() + self.columns.gap), y)
224 }
225}
226
227/// A page margin box, named as CSS names them.
228#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
229#[serde(rename_all = "kebab-case")]
230pub enum MarginBox {
231 /// `@top-left-corner`
232 TopLeftCorner,
233 /// `@top-left`
234 TopLeft,
235 /// `@top-center`
236 TopCenter,
237 /// `@top-right`
238 TopRight,
239 /// `@top-right-corner`
240 TopRightCorner,
241 /// `@left-top`
242 LeftTop,
243 /// `@left-middle`
244 LeftMiddle,
245 /// `@left-bottom`
246 LeftBottom,
247 /// `@right-top`
248 RightTop,
249 /// `@right-middle`
250 RightMiddle,
251 /// `@right-bottom`
252 RightBottom,
253 /// `@bottom-left-corner`
254 BottomLeftCorner,
255 /// `@bottom-left`
256 BottomLeft,
257 /// `@bottom-center`
258 BottomCenter,
259 /// `@bottom-right`
260 BottomRight,
261 /// `@bottom-right-corner`
262 BottomRightCorner,
263}
264
265impl MarginBox {
266 /// The at-rule name, without the `@`.
267 pub fn keyword(self) -> &'static str {
268 match self {
269 MarginBox::TopLeftCorner => "top-left-corner",
270 MarginBox::TopLeft => "top-left",
271 MarginBox::TopCenter => "top-center",
272 MarginBox::TopRight => "top-right",
273 MarginBox::TopRightCorner => "top-right-corner",
274 MarginBox::LeftTop => "left-top",
275 MarginBox::LeftMiddle => "left-middle",
276 MarginBox::LeftBottom => "left-bottom",
277 MarginBox::RightTop => "right-top",
278 MarginBox::RightMiddle => "right-middle",
279 MarginBox::RightBottom => "right-bottom",
280 MarginBox::BottomLeftCorner => "bottom-left-corner",
281 MarginBox::BottomLeft => "bottom-left",
282 MarginBox::BottomCenter => "bottom-center",
283 MarginBox::BottomRight => "bottom-right",
284 MarginBox::BottomRightCorner => "bottom-right-corner",
285 }
286 }
287
288 /// Parses an at-rule name inside `@page`.
289 pub fn parse(keyword: &str) -> Option<MarginBox> {
290 Self::ALL
291 .into_iter()
292 .find(|candidate| candidate.keyword().eq_ignore_ascii_case(keyword))
293 }
294
295 /// Every margin box CSS defines.
296 pub const ALL: [MarginBox; 16] = [
297 MarginBox::TopLeftCorner,
298 MarginBox::TopLeft,
299 MarginBox::TopCenter,
300 MarginBox::TopRight,
301 MarginBox::TopRightCorner,
302 MarginBox::LeftTop,
303 MarginBox::LeftMiddle,
304 MarginBox::LeftBottom,
305 MarginBox::RightTop,
306 MarginBox::RightMiddle,
307 MarginBox::RightBottom,
308 MarginBox::BottomLeftCorner,
309 MarginBox::BottomLeft,
310 MarginBox::BottomCenter,
311 MarginBox::BottomRight,
312 MarginBox::BottomRightCorner,
313 ];
314
315 /// The margin the box sits in, and where in it: `None` for the
316 /// boxes the engine parses but does not paint.
317 pub fn band(self) -> Option<(Band, Align)> {
318 match self {
319 MarginBox::TopLeft => Some((Band::Top, Align::Start)),
320 MarginBox::TopCenter => Some((Band::Top, Align::Center)),
321 MarginBox::TopRight => Some((Band::Top, Align::End)),
322 MarginBox::BottomLeft => Some((Band::Bottom, Align::Start)),
323 MarginBox::BottomCenter => Some((Band::Bottom, Align::Center)),
324 MarginBox::BottomRight => Some((Band::Bottom, Align::End)),
325 _ => None,
326 }
327 }
328}
329
330/// Which margin a painted margin box lives in.
331#[derive(Debug, Clone, Copy, PartialEq, Eq)]
332pub enum Band {
333 /// The top margin: running heads.
334 Top,
335 /// The bottom margin: folios and running feet.
336 Bottom,
337}
338
339/// Where in its band a margin box's content sits. `Center` centres on
340/// the trim rather than on the content box: a folio belongs on the
341/// page's axis, and mirrored margins put the content box off it.
342#[derive(Debug, Clone, Copy, PartialEq, Eq)]
343pub enum Align {
344 /// The content box's leading edge.
345 Start,
346 /// The trim's axis.
347 Center,
348 /// The content box's trailing edge.
349 End,
350}