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
252
//! Where a centered or a right-aligned line starts on a device that starts it on a dot.
use f_office_units::Length;
use super::Origin;
use crate::grid::ColumnEdges;
use crate::layout::{Layout, Within, decisions};
use crate::lines::{Line, Piece};
use crate::params;
use crate::props::Alignment;
impl Layout<'_> {
/// The dot of the column's left edge. The text edge of a table cell is the sum of the page
/// column's edge, the cell's edge past it and the cell's left margin, as
/// `Device::parts_dot` adds them; in a centered table that keeps its outer edge in
/// fixed-point points, the sum of that edge, the text edge's distance from it less the
/// margin, and the margin (`Device::parts_dot_from`). A later column of the page whose edge goes by its parts
/// ([`Self::column_parts`]) is the sum of those parts. Another column of the page and a
/// turned cell take the dot of their edge as one length, with the halves of
/// `Device::line_start`: an edge of 1,134 twips, 472.5 dots, lies on dot 472 where that
/// quirk is fixed point.
pub(in crate::layout) fn column_dot(&self) -> Length {
match self.column.within {
Within::Cell(parts) => {
let inner = parts.inner;
// quirk: CenteredTablePlace::FixedPointHalf (src/quirks.rs)
if let Some(outer) = parts.outer {
return self
.device
.parts_dot_from(outer, [self.column.left - inner - outer.exact, inner]);
}
let origin = self.column.origin;
self.device
.parts_dot([origin, self.column.left - inner - origin, inner])
}
Within::Page => match self.column_parts() {
Some(parts) => self.device.parts_dot(parts),
None => self.device.snap(self.device.line_start(self.column.left)),
},
Within::TurnedFrame(..) => self.device.snap(self.device.line_start(self.column.left)),
}
}
/// The parts of the left edge of the current column of the page, when it is a later column
/// of the section's columns and the quirk `ColumnEdgeHalves::ColumnParts` takes its edge by
/// its parts in the document's mode: the left margin, the first column's edge, and each
/// earlier column's width with the space after it as one length, the distance from its
/// edge to the next column's edge.
fn column_parts(&self) -> Option<Vec<Length>> {
if !self.settings.mode_rules.takes_column_edge_by_parts() {
return None;
}
decisions::column_edge_parts(&self.context.quirks, || self.edge_parts())
}
/// The parts of the left edge of the current column of the page that [`Self::column_parts`]
/// gives, on every target.
fn edge_parts(&self) -> Option<Vec<Length>> {
let edges = self.columns.get(..=self.column_index)?;
let own = edges
.last()
.is_some_and(|&(left, _)| left == self.column.left);
if edges.len() < 2 || !own {
return None;
}
let steps = edges.windows(2).filter_map(|pair| match pair {
[(before, _), (after, _)] => Some(*after - *before),
_ => None,
});
Some(
edges
.first()
.map(|&(margin, _)| margin)
.into_iter()
.chain(steps)
.collect(),
)
}
/// Draws `line` from the start that [`Self::aligned_start`] gives it, and returns what
/// lies before each glyph that it draws, with the glyph's character spacing.
pub(super) fn emit_aligned(
&mut self,
line: &Line,
origin: Origin,
moved: (Alignment, Length),
width: Length,
) -> super::draw::DrawnSteps {
let origin = self.aligned_start(line, origin, moved, width);
self.emit_visual(line, origin).0
}
/// The edges of the column of a line that starts at `origin` in a space `width` wide, a
/// column of the page or the text area of an upright table cell, from which the space's
/// right edge can take its dot; a turned cell, whose lines run down or up the page, gives
/// none.
fn column_edges(&self, origin: Origin, width: Length) -> Option<ColumnEdges> {
(!matches!(self.column.within, Within::TurnedFrame(..))).then(|| ColumnEdges {
left: self.column_dot(),
width: self.column.width,
inset: self.column.width - origin.start - width,
})
}
/// The exact left edge of `line`, a centered line that ends its right-to-left paragraph, that
/// alignment moved `offset` right of its start at its indent to `origin`, in a space `width`
/// wide, where the device puts it on dots ([`crate::grid::Device::right_to_left_center`]):
/// its space's left edge lies on the dot of the line's start at its indent, and its right
/// edge on the dot that a centered line of a left-to-right paragraph moves up to.
fn right_to_left_center(
&self,
line: &Line,
(origin, offset): (Origin, Length),
width: Length,
) -> Option<Length> {
let start = self.device.snap(origin.x - offset);
let edge = self
.device
.aligned_edge((start, width), self.column_edges(origin, width));
self.device
.right_to_left_center((start, edge), line.width())
}
/// `origin` with the start of `line`, which `alignment` moved `offset` right of its start at
/// its indent to `origin`, in a space `width` wide. A centered or a right-aligned line of
/// a target with the quirk `AlignedEnds::OnDots` takes its glyph places from the
/// dot of its start at its indent, and moves right by the free whole dots between the dot
/// where its written text then ends and the dot of the space's right edge
/// (`Device::aligned_edge`), or by half of them, rounded down. With the quirk
/// `AlignedLineSegments::FromExactStart` the text after a tab or a list label's gap counts
/// its segment from the exact start at its indent (`Origin::segment_rest`), so that it lands
/// where it lands in a start-aligned line, before the line moves. So does a right-aligned
/// line of a right-to-left paragraph that does not end the paragraph, on a target with
/// `RightToLeftLineStart::WholeDots`: its steps count without the shift of
/// `Origin::right_to_left`, its space's right edge is the one of
/// [`crate::grid::Device::right_to_left_edge`], and its glyphs then lie
/// [`params::RIGHT_TO_LEFT_LINE_DOTS`] dots right of them. A
/// right-to-left line whose spaces stretch or shrink, on a target with
/// `RightToLeftJustifiedLine::EndsAtEdge`, moves by the dots between the dot where its steps
/// end and that edge's dot, also left when its steps end past it, and its glyphs then lie as
/// those of such a start-aligned line. A line that ends with a tab that no glyph follows
/// ends where the quirk `AlignedTabEnd` says. Every other line keeps `origin`, and so does
/// a line without a written end or with an inline drawing. A centered line that ends its
/// right-to-left paragraph lies where [`Self::right_to_left_center`] puts it.
fn aligned_start(
&mut self,
line: &Line,
origin: Origin,
(alignment, offset): (Alignment, Length),
width: Length,
) -> Origin {
let centered = alignment == Alignment::Center;
let moves = centered || alignment == Alignment::End;
let right_to_left = origin.right_to_left.is_some();
let whole_dots = right_to_left
&& !centered
&& !origin.mark
&& self.device.starts_right_to_left_on_dots();
let ends_at_edge = right_to_left
&& origin.space_extra != Length::ZERO
&& self.device.ends_right_to_left_at_edge();
let steps_alone = whole_dots || ends_at_edge;
// A line with an inline drawing keeps its exact start: the drawing's own text is laid
// out when the line is drawn, which the line cannot do twice.
let boxed = line.placed.iter().any(|placed| {
matches!(
placed.piece,
Piece::Object {
drawing: Some(_),
..
}
)
});
if right_to_left && centered && origin.mark && !boxed {
// quirk: RightToLeftCenteredLine (src/quirks.rs)
let x = self.right_to_left_center(line, (origin, offset), width);
return Origin {
x: x.unwrap_or(origin.x),
..origin
};
}
if !(moves || ends_at_edge)
|| boxed
|| !self.device.aligns_on_dots()
|| (right_to_left && !steps_alone)
{
return origin;
}
let stepped = if steps_alone {
Origin {
right_to_left: Some(Length::ZERO),
..origin
}
} else {
origin
};
let exact_start = origin.x - offset;
let start = self.device.snap(exact_start);
// The glyphs take their places from the dot of the start. The text after a tab or a
// list label's gap can count its segment from the exact start, so that it lands on the
// dot of its exact place in the line before it moves, as in a start-aligned line.
// quirk: AlignedLineSegments (src/quirks.rs)
let segment_rest = self
.device
.aligned_segment_rest((exact_start, start), steps_alone);
let column = self.column_edges(origin, width);
let edge = if steps_alone {
self.device
.right_to_left_edge((start, width), column, self.column.left)
} else {
self.device.aligned_edge((start, width), column)
};
// The line's written end comes from the steps of its glyphs, so the line is drawn once
// from the dot of its start, and what that drew is dropped.
let drawn = self.items.len();
let (_, end) = self.emit_visual(
line,
Origin {
x: start,
segment_rest,
..stepped
},
);
self.items.cut(drawn);
if !steps_alone {
self.aligned_parts = end.map(|end| crate::layout::AlignedParts {
start,
end,
edge,
indent: origin.start,
width,
});
}
let shift = if ends_at_edge {
end.map(|end| edge - end)
} else {
end.and_then(|end| self.device.aligned_shift(centered, end, edge))
};
let x = shift.map_or(origin.x, |shift| start + shift);
let right_to_left = if steps_alone {
Some(self.device.dots(params::RIGHT_TO_LEFT_LINE_DOTS))
} else {
origin.right_to_left
};
Origin {
x,
right_to_left,
segment_rest: shift.map_or(Length::ZERO, |_| segment_rest),
..origin
}
}
}