Skip to main content

fleuron/style/properties/
exclusion.rs

1//! A block placed against the page rather than in the flow, and the
2//! contour the prose around it keeps clear of.
3
4use serde::Serialize;
5
6use super::edges::Edges;
7use super::value::Length;
8
9/// Whether an element sits in the flow or against the page.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
11#[serde(rename_all = "snake_case")]
12pub enum Position {
13    /// `static`: the element sits where the flow puts it.
14    Static,
15    /// `relative`: the element keeps its place in the flow, and the
16    /// engine draws it moved by its insets. Nothing around it moves.
17    Relative,
18    /// `absolute`: the element comes out of the flow and sits against
19    /// the page area, at the insets it declares.
20    Absolute,
21}
22
23/// How far one edge of a positioned box sits from the matching edge of
24/// the page area, from `top`, `right`, `bottom` and `left`.
25#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
26#[serde(rename_all = "snake_case")]
27pub enum Inset {
28    /// `auto`: the opposite edge places the box. Where that edge is
29    /// `auto` as well, the box sits at the edge of the page area.
30    Auto,
31    /// A length in points, negative where the box reaches into the
32    /// margin.
33    Points(f32),
34    /// A percentage of the page area's width, for `left` and `right`,
35    /// or of its height, for `top` and `bottom`. It stays a percentage
36    /// until layout resolves it against the page area.
37    Percent(f32),
38}
39
40impl Inset {
41    /// The inset in points across a page area `extent` wide or tall,
42    /// or `None` where it is `auto`.
43    pub fn resolve(self, extent: f32) -> Option<f32> {
44        match self {
45            Inset::Auto => None,
46            Inset::Points(points) => Some(points),
47            Inset::Percent(percent) => Some(percent / 100.0 * extent),
48        }
49    }
50}
51
52impl Edges<Inset> {
53    /// How far `position: relative` moves a box across and down a page
54    /// area of `size`. `left` outranks `right` and `top` outranks
55    /// `bottom`, as in CSS.
56    pub fn offset(self, (width, height): (f32, f32)) -> (f32, f32) {
57        let axis = |start: Inset, end: Inset, extent: f32| match (
58            start.resolve(extent),
59            end.resolve(extent),
60        ) {
61            (Some(start), _) => start,
62            (None, Some(end)) => -end,
63            (None, None) => 0.0,
64        };
65        (
66            axis(self.left, self.right, width),
67            axis(self.top, self.bottom, height),
68        )
69    }
70}
71
72/// Which side of an exclusion the prose sets on, from `wrap-flow`. An
73/// exclusion is a box that the text keeps clear of.
74#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
75#[serde(rename_all = "kebab-case")]
76pub enum WrapFlow {
77    /// `auto`: the box excludes nothing, and the text runs under it.
78    Auto,
79    /// `both`: the prose sets on either side of it.
80    Both,
81    /// `start`: the prose sets on the side the line starts at.
82    Start,
83    /// `end`: the prose sets on the side the line ends at.
84    End,
85}
86
87/// Which side of its column an image in the flow moves to, from
88/// `float`. The prose after it sets beside it.
89#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
90#[serde(rename_all = "snake_case")]
91pub enum Float {
92    /// `none`: the image takes a band of its own.
93    None,
94    /// `left`: the image stands at the left edge, and the prose sets
95    /// to its right.
96    Left,
97    /// `right`: the image stands at the right edge, and the prose sets
98    /// to its left.
99    Right,
100}
101
102/// Which floats a block starts below, from `clear`.
103#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
104#[serde(rename_all = "snake_case")]
105pub enum Clear {
106    /// `none`: the block sets beside any float.
107    None,
108    /// `left`: the block starts below a float on the left.
109    Left,
110    /// `right`: the block starts below a float on the right.
111    Right,
112    /// `both`: the block starts below every float.
113    Both,
114}
115
116impl Clear {
117    /// Whether a block that clears this way starts below a float to
118    /// `side`.
119    pub fn clears(self, side: Float) -> bool {
120        matches!(
121            (self, side),
122            (Clear::Both, Float::Left | Float::Right)
123                | (Clear::Left, Float::Left)
124                | (Clear::Right, Float::Right)
125        )
126    }
127}
128
129/// `shape-outside` as the sheet wrote it, before the cascade knows
130/// the font size its lengths are relative to.
131#[derive(Debug, Clone, PartialEq)]
132pub enum ShapeSource {
133    /// `none`.
134    None,
135    /// `auto`.
136    Auto,
137    /// `polygon(…)`, as pairs of written lengths.
138    Polygon(Vec<(Length, Length)>),
139}
140
141/// The contour prose sets around, from `shape-outside`.
142///
143/// A contour is the outline the text keeps clear of, in place of the
144/// box.
145#[derive(Debug, Clone, PartialEq, Serialize)]
146#[serde(rename_all = "snake_case")]
147pub enum ShapeOutside {
148    /// `none`: the prose keeps clear of the box itself.
149    None,
150    /// `auto`: the contour comes from the image's own alpha channel.
151    /// A format that carries no alpha contributes its box.
152    Auto,
153    /// `polygon(…)`: the points the sheet wrote, read against the
154    /// margin box.
155    Polygon(Vec<ShapePoint>),
156}
157
158/// One point of a `polygon()`, from the top left of the margin box.
159#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
160pub struct ShapePoint {
161    /// Across the box.
162    pub x: Coord,
163    /// Down the box.
164    pub y: Coord,
165}
166
167/// One coordinate of a shape: a length, or a fraction of the box the
168/// shape is read against.
169///
170/// `em` and `rem` are points by the time the cascade is done. A
171/// percentage is not, because the box it measures against is the
172/// image's, and the image is sized in the layout pass.
173#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
174#[serde(rename_all = "snake_case")]
175pub enum Coord {
176    /// An absolute length in points.
177    Points(f32),
178    /// A percentage of the box's own width or height.
179    Percent(f32),
180}
181
182impl Coord {
183    /// What the cascade makes of one written length: `em` and `rem`
184    /// against the font size in force, a percentage kept as one.
185    pub fn of(length: Length, size: f32, root: f32) -> Coord {
186        match length {
187            Length::Percent(percent) => Coord::Percent(percent),
188            other => Coord::Points(other.to_points(size, root)),
189        }
190    }
191
192    /// The coordinate in points, across a box `extent` wide or tall.
193    pub fn to_points(self, extent: f32) -> f32 {
194        match self {
195            Coord::Points(points) => points,
196            Coord::Percent(percent) => percent / 100.0 * extent,
197        }
198    }
199}