1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//! The device's decisions on the height and the baseline of a line: how the parts of a line's
//! box round to the device's dots, and what the box holds.
use f_office_units::Length;
use f_office_units::scale::LINE;
use super::Device;
use crate::props::LineSpacing;
use crate::quirks::{
AtLeastSpaceAfter, CellSpaceAfter, DroppedSpaceBox, ExactBaseline, ExactBoxLength,
ExactFifthHalves, ExactSpacedBottom, LineHeightUnit, LowMultipleBaseline, MultipleBaseline,
SpaceBeforeAscent, TwipLengthUnit,
};
/// Where the baseline of a line below single spacing goes, from a dot of its box
/// ([`Device::low_multiple_baseline`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LowBaseline {
/// This offset below the line's rounded top.
BelowTop(Length),
/// This offset above the line's rounded bottom.
AboveBottom(Length),
}
impl Device {
/// The ascent part of the baseline of a first line whose box holds its space `before`, below
/// the box's rounded top: the space and the line's `ascent` rounded together, or each on its
/// own under `SpaceBeforeAscent::RoundedApart`.
#[must_use]
pub fn ascent_part(self, before: Length, ascent: Length) -> Length {
match self.quirks.space_before_ascent {
SpaceBeforeAscent::RoundedTogether => self.snap(before + ascent),
// quirk: SpaceBeforeAscent::RoundedApart (src/quirks.rs)
SpaceBeforeAscent::RoundedApart => self.snap(before) + self.snap(ascent),
}
}
/// The rounded part `below` the baseline of a line that the page places up from its
/// bottom. It rounds as a top on half a dot does ([`Self::top_dot`]), except the part of a
/// line of exact spacing of whole twips that lies exactly on half a dot under
/// `ExactFifthHalves::Entered`, whose `entered` rest is that part's share of the entered
/// height past `below`: the part takes the shorter dot when the rest lies below 0, and the
/// longer one otherwise. `entered` is `None` for another line.
#[must_use]
pub fn exact_below(self, below: Length, entered: Option<i64>) -> Length {
let on_half = self.half_dot_side(below).is_eq();
match (self.quirks.exact_fifth_halves, entered) {
// quirk: ExactFifthHalves::Entered (src/quirks.rs)
(ExactFifthHalves::Entered, Some(rest)) if on_half => {
if rest < 0 {
self.snap_half_down(below)
} else {
self.snap(below)
}
}
// The part below the baseline, a fifth of the line, of whole twips on half a dot goes
// to the dot of its fixed-point value where the target rounds such tops so.
// quirk: TopHalves::FixedPoint (src/quirks.rs)
(ExactFifthHalves::AsTops | ExactFifthHalves::Entered, _) => self.top_dot(below),
}
}
/// The offset of the baseline from the rounded top of the box of an exact line of `height`,
/// whose baseline lies `baseline` below its top and whose part below the baseline rounds to
/// `below` ([`Self::exact_below`]), with `before` of space before that the box holds above
/// the line: `None` unless the device places such baselines from the box
/// (`ExactBaseline::BoxForms`) and the box rounds down to the device grid.
///
/// The box's exact length decides whether it rounds down, except under
/// `ExactBoxLength::Entered` for a box that lies exactly on half a dot whose two lengths
/// have the twips rests `rests`: it rounds down when they sum below 0, so that the length
/// that the target enters lies below the half. Under `ExactBoxLength::Entered`, on the first
/// line of a paragraph that sets its own space before (`below_space`), the box's rounded
/// length is its shorter dot, also when its exact length lies on half a dot, and the
/// baseline sits `below` above that rounded bottom. Otherwise
/// [`Self::rounded_down_baseline`] places it, with the baseline four fifths down the line.
#[must_use]
pub fn exact_box_baseline(
self,
before: Length,
(height, baseline, below): (Length, Length, Length),
rests: (Option<i64>, Option<i64>),
below_space: bool,
) -> Option<Length> {
if self.quirks.exact_baseline != ExactBaseline::BoxForms {
return None;
}
// quirk: ExactBaseline::BoxForms (src/quirks.rs)
let boxed = before + height;
let on_half = self.half_dot_side(boxed).is_eq();
let rounds_down = match (self.quirks.exact_box_length, rests) {
// quirk: ExactBoxLength::Entered (src/quirks.rs)
(ExactBoxLength::Entered, (Some(above), Some(own))) if on_half => above + own < 0,
_ => self.snap(boxed) < boxed,
};
if !rounds_down {
return None;
}
Some(match self.quirks.exact_box_length {
// quirk: ExactBoxLength::Entered (src/quirks.rs)
ExactBoxLength::Entered if below_space => self.snap_half_down(boxed) - below,
ExactBoxLength::Entered | ExactBoxLength::Exact => self.rounded_down_baseline(
(before + baseline, height - baseline),
boxed,
below_space,
),
})
}
/// The offset of the baseline from the rounded top of an exact line's box that holds the
/// room `before` above the line and the room `after` below it, of the exact `height` with
/// the baseline `baseline` below the line's own top. The two rooms round on their own, the
/// line sits in the middle of the room left between them, and its baseline rounds up.
#[must_use]
pub fn centred_room_baseline(
self,
(before, after): (Length, Length),
(height, baseline): (Length, Length),
) -> Length {
let room = self.snap(before + height + after) - self.snap(before) - self.snap(after);
self.snap(before) + self.ceil(baseline + (room - height).div_toward_zero(2))
}
/// Where the baseline of a line below single spacing of `height` goes, whose exact
/// baseline lies `baseline` below its top, under `LowMultipleBaseline::HeightFraction`; `None`
/// under the other variant, which rounds the exact baseline once. Where the line's height
/// lies less than half a dot past a whole dot, the top rounds, and the baseline sits the
/// exact offset of the baseline less half that part of a dot, rounded, below it. Otherwise
/// the line's bottom rounds, and the baseline sits the part of the line below it, rounded,
/// above that bottom. On a device whose dot is no whole number of half-EMUs, that part of a
/// dot is itself rounded to a half-EMU.
#[must_use]
pub fn low_multiple_baseline(self, height: Length, baseline: Length) -> Option<LowBaseline> {
if self.quirks.low_multiple_baseline != LowMultipleBaseline::HeightFraction {
return None;
}
// quirk: LowMultipleBaseline::HeightFraction (src/quirks.rs)
let part = height - self.floor(height);
Some(if self.half_dot_side(height).is_lt() {
LowBaseline::BelowTop(self.snap(baseline - part.div_toward_zero(2)))
} else {
LowBaseline::AboveBottom(self.snap(height - baseline))
})
}
/// The space after of a paragraph, `after` (with the room of a bottom border that the box
/// holds as space after), that the box of a line of the spacing `rule` holds, when that space
/// follows the line (`spaced`) and no taller object or document grid sizes the line: all of
/// it at automatic spacing at or above single spacing under
/// `MultipleBaseline::DescentAboveExtra`, and at at-least spacing under
/// `AtLeastSpaceAfter::Held`, whether the minimum sets the line's height or, at most the
/// single line, leaves the line the box of single spacing; none otherwise. A line in a table
/// cell (`in_cell`) holds it only under `CellSpaceAfter::Held`.
#[must_use]
pub fn held_after(
self,
after: Length,
rule: LineSpacing,
spaced: bool,
in_cell: bool,
) -> Length {
// quirk: CellSpaceAfter::Held (src/quirks.rs)
let spaced = spaced && (!in_cell || self.quirks.cell_space_after == CellSpaceAfter::Held);
// quirk: MultipleBaseline::DescentAboveExtra (src/quirks.rs)
let auto = matches!(rule, LineSpacing::Auto(n) if n >= LINE)
&& self.quirks.multiple_baseline == MultipleBaseline::DescentAboveExtra;
// quirk: AtLeastSpaceAfter::Held (src/quirks.rs)
let at_least = matches!(rule, LineSpacing::AtLeast(_))
&& self.quirks.at_least_space_after == AtLeastSpaceAfter::Held;
if spaced && (auto || at_least) {
after
} else {
Length::ZERO
}
}
}
impl Device {
/// The length of `twips` whole twips in whole parts of `params::HEIGHT_PARTS_PER_POINT` of a
/// point, the nearest a half up, where the device adds a length of whole twips down the page
/// in those parts (`TwipLengthUnit::Nearest4096ths`). `None` where it adds the half-EMUs.
#[must_use]
pub fn twip_parts(self, twips: i64) -> Option<i64> {
let twips_per_point = Length::UNITS_PER_POINT / Length::UNITS_PER_TWIP;
self.in_twip_parts(twips).map(|twips| {
f_office_units::div_round(
twips * crate::params::HEIGHT_PARTS_PER_POINT,
twips_per_point,
)
})
}
/// The value `value` of a length of whole twips, or of a place that such lengths reach,
/// where the device adds those lengths down the page in whole parts of
/// `params::HEIGHT_PARTS_PER_POINT` of a point (`TwipLengthUnit::Nearest4096ths`). `None`
/// where it adds the half-EMUs.
#[must_use]
pub fn in_twip_parts<T>(self, value: T) -> Option<T> {
match self.quirks.twip_length_unit {
TwipLengthUnit::HalfEmus => None,
// quirk: TwipLengthUnit::Nearest4096ths (src/quirks.rs)
TwipLengthUnit::Nearest4096ths => Some(value),
}
}
/// The kind `kind` of a line of exact height that ends its paragraph with its space after
/// following it, where the page rounds the parts of such a line's bottom on their own
/// (`ExactSpacedBottom::PartsRounded`). `None` where it rounds their sum.
#[must_use]
pub fn spaced_exact_bottom<T>(self, kind: T) -> Option<T> {
match self.quirks.exact_spaced_bottom {
ExactSpacedBottom::SumRounded => None,
// quirk: ExactSpacedBottom::PartsRounded (src/quirks.rs)
ExactSpacedBottom::PartsRounded => Some(kind),
}
}
/// The height of a line in whole parts of `params::HEIGHT_PARTS_PER_POINT` of a point, which
/// `height` gives, where the target holds the heights of lines of automatic spacing in them
/// (`LineHeightUnit::Whole4096ths`). `None` where it holds half-EMUs.
pub fn whole_line_height<T>(self, height: impl FnOnce() -> Option<T>) -> Option<T> {
match self.quirks.line_height_unit {
LineHeightUnit::HalfEmus => None,
// quirk: LineHeightUnit::Whole4096ths (src/quirks.rs)
LineHeightUnit::Whole4096ths => height(),
}
}
/// The space `dropped` before a first line at the body's top on a page, which a natural
/// break dropped, that the line's box holds above the page's top for the rounding of its
/// baseline (`DroppedSpaceBox::HeldAbove`). `None` where the box leaves it out.
#[must_use]
pub fn held_dropped_space<T>(self, dropped: T) -> Option<T> {
match self.quirks.dropped_space_box {
DroppedSpaceBox::LeftOut => None,
// quirk: DroppedSpaceBox::HeldAbove (src/quirks.rs)
DroppedSpaceBox::HeldAbove => Some(dropped),
}
}
/// The result of `above` for a line of multiple spacing whose descent the device puts above
/// the extra spacing (`MultipleBaseline::DescentAboveExtra`), such as its baseline or whether
/// its box holds its spaces. `None` where the device rounds the ascent of such a line.
pub fn descent_above_extra<T>(self, above: impl FnOnce() -> T) -> Option<T> {
match self.quirks.multiple_baseline {
MultipleBaseline::RoundedAscent => None,
// quirk: MultipleBaseline::DescentAboveExtra (src/quirks.rs)
MultipleBaseline::DescentAboveExtra => Some(above()),
}
}
}