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
//! The device's decisions on where the lines and the cell text of a centered table, or of a
//! table whose outer edge sits at its place, land on the device's dots.
use f_office_units::Length;
use super::{Device, FixedPlace, LineRounding, fixed_points};
use crate::quirks::{
CenteredTableLines, CenteredTableLinesBefore15, CenteredTablePlace, CenteredTableTextBefore15,
CompoundTableLines, EdgeTableText, TableEdgeHalves, TableLines,
};
impl Device {
/// The outer edge of a centered table in fixed-point points, under
/// `CenteredTablePlace::FixedPointHalf`, in a text column whose left edge lies `left` past
/// the page column's edge `origin` and that is `width` wide, with a grid `total` wide and a
/// left border whose half is `half_border`: the column's edge as those two parts, plus half
/// of the column's width less the grid's width less the border, each in fixed-point points,
/// the half taken toward zero. `None` under the other variant, and on a device that does not
/// take the start of a line through fixed-point points.
#[must_use]
pub fn centered_outer_edge(
self,
(origin, left): (Length, Length),
(width, total): (Length, Length),
half_border: Length,
) -> Option<FixedPlace> {
// quirk: CenteredTablePlace::FixedPointHalf (src/quirks.rs)
if self.quirks.centered_table_place != CenteredTablePlace::FixedPointHalf
|| !self.starts_in_fixed_point()
{
return None;
}
let free = fixed_points(width) - fixed_points(total) - 2 * fixed_points(half_border);
let exact = left + (width - total).div_toward_zero(2) - half_border;
Some(FixedPlace {
exact,
fixed: fixed_points(origin) + fixed_points(left - origin) + free / 2,
})
}
/// The dot from which a centered table whose outer edge sits at its place takes its lines,
/// under `CenteredTableLines::FromOuterEdge`: its outer edge `outer` rounded to the nearest
/// dot, and for an outer edge on exactly half a dot the dot of its fixed-point place
/// `place` ([`Self::centered_outer_edge`]). `None` for an outer edge on half a dot without
/// a place, which keeps the exact grid lines, and under the other variant.
#[must_use]
pub fn outer_edge_dot(self, outer: Length, place: Option<FixedPlace>) -> Option<Length> {
if self.quirks.centered_table_lines != CenteredTableLines::FromOuterEdge {
return None;
}
// quirk: CenteredTableLines::FromOuterEdge (src/quirks.rs)
match (self.snap(outer), place) {
(rounded, _) if rounded == self.snap_half_down(outer) => Some(rounded),
// quirk: CenteredTablePlace::FixedPointHalf (src/quirks.rs)
(_, Some(place)) => Some(self.fixed_place_dot(place)),
(_, None) => None,
}
}
/// The dot from which a centered table before mode 15 takes its lines, under
/// `CenteredTableLinesBefore15::FromColumnEdge`: the left edge `left` of its text column
/// rounded to the nearest dot, and on exactly half a dot the dot of
/// [`Self::column_edge_half`], for an edge that takes it (`halves`). `None`
/// for an edge on exactly half a dot without that dot, which keeps the exact grid lines,
/// and under the other variant.
#[must_use]
pub fn column_edge_dot(self, left: Length, halves: bool) -> Option<Length> {
if self.quirks.centered_table_lines_before_15 != CenteredTableLinesBefore15::FromColumnEdge
{
return None;
}
// quirk: CenteredTableLinesBefore15::FromColumnEdge (src/quirks.rs)
let rounded = self.snap(left);
if rounded == self.snap_half_down(left) {
Some(rounded)
} else {
self.column_edge_half(left, halves)
}
}
/// The dot of a table's column edge `left` on exactly half a dot, for an edge that takes it
/// (`halves`: the edge of the page's text column, or the place of a floating table placed
/// at an offset across), under `TableEdgeHalves::FixedPoint`: the edge in fixed-point
/// points, a half up, and those to the nearest dot, a half up. `None` under the other
/// variant, and for another edge, such as the place of a centered floating table or a
/// cell's text edge, the column of a nested table.
fn column_edge_half(self, left: Length, halves: bool) -> Option<Length> {
if !halves {
return None;
}
match self.quirks.table_edge_halves {
TableEdgeHalves::BelowOrExact => None,
// quirk: TableEdgeHalves::FixedPoint (src/quirks.rs)
TableEdgeHalves::FixedPoint => Some(self.dots(self.fixed_point_snap(left))),
}
}
/// What the text of a centered table's cells before mode 15 loses when the table's first
/// grid line is put on a whole twip past the left edge of its text column, whose half twip
/// is `rest`: all of it under `CenteredTableTextBefore15::WholeTwip`, and nothing under the
/// other variant.
#[must_use]
pub fn centered_text_loss(self, rest: Length) -> Length {
match self.quirks.centered_table_text_before_15 {
// quirk: CenteredTableTextBefore15::WholeTwip (src/quirks.rs)
CenteredTableTextBefore15::WholeTwip => rest,
CenteredTableTextBefore15::Exact => Length::ZERO,
}
}
/// What the upright text of the cells of a table whose outer edge sits at its place gains
/// when half its left border, `shift`, by which its first grid line lies right of its outer
/// edge, is rounded up to whole twips: that rest under `EdgeTableText::WholeTwipBorder`, and
/// nothing under the other variant.
#[must_use]
pub fn edge_text_gain(self, shift: Length) -> Length {
match self.quirks.edge_table_text {
// quirk: EdgeTableText::WholeTwipBorder (src/quirks.rs)
EdgeTableText::WholeTwipBorder => shift.up_to_whole(Length::UNITS_PER_TWIP) - shift,
EdgeTableText::Exact => Length::ZERO,
}
}
}
impl Device {
/// The rounded column edge `left` from which the lines of a left-aligned table are drawn:
/// the nearest dot, a half up under `TableLines::RoundsStarts`, and under
/// `TableLines::RoundsCenters` a half to the dot of [`Self::column_edge_half`] for an edge
/// that takes it (`halves`), or else down.
#[must_use]
pub fn table_line_edge(self, left: Length, halves: bool) -> Length {
match self.quirks.table_lines {
TableLines::RoundsStarts => self.snap(left),
// quirk: TableLines::RoundsCenters (src/quirks.rs)
TableLines::RoundsCenters => {
let below = self.snap_half_down(left);
if below == self.snap(left) {
below
} else {
self.column_edge_half(left, halves).unwrap_or(below)
}
}
}
}
/// How wide a table's line of `thickness` half-EMUs is on the device: rounded to the
/// nearest dot under `TableLines::RoundsStarts`, rounded down to whole dots under
/// `TableLines::RoundsCenters`, and at least one dot ([`Device::line_thickness`]).
#[must_use]
pub fn table_line_thickness(self, thickness: Length) -> Length {
let rounding = match self.quirks.table_lines {
TableLines::RoundsStarts => LineRounding::Nearest,
// quirk: TableLines::RoundsCenters (src/quirks.rs)
TableLines::RoundsCenters => LineRounding::Down,
};
self.line_thickness(thickness, rounding)
}
/// How a table's border `border` lies on the device as one line: the thickness on the
/// device of each of its parts, its lines and the gaps between them in order. That is where
/// the target places a border of more than one line as one line of all its parts
/// (`CompoundTableLines::WholeBorder`), each part as thick as a line of its size, for a
/// border whose lines do not touch ([`crate::props::Border::touches`]); `None` otherwise.
#[must_use]
pub fn whole_table_border(self, border: &crate::props::Border) -> Option<Vec<Length>> {
match self.quirks.compound_table_lines {
CompoundTableLines::EachLine => None,
// quirk: CompoundTableLines::WholeBorder (src/quirks.rs)
CompoundTableLines::WholeBorder => {
let sizes = border.parts.sizes();
(sizes.len() > 1 && !border.touches()).then(|| {
sizes
.iter()
.map(|size| self.table_line_thickness(*size))
.collect()
})
}
}
}
/// The place of a table's line that `centered` gives from the rounded center of the line,
/// where the device rounds a table's lines by their centers (`TableLines::RoundsCenters`).
/// `None` where it rounds their starts.
pub fn centered_table_line<T>(self, centered: impl FnOnce() -> T) -> Option<T> {
match self.quirks.table_lines {
TableLines::RoundsStarts => None,
// quirk: TableLines::RoundsCenters (src/quirks.rs)
TableLines::RoundsCenters => Some(centered()),
}
}
}