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
//! The root sign of a radical (`m:rad`): the lengths of the radical's parts from the `MATH`
//! table of its math font, and the glyph and place of its sign once its element is measured.
use f_office_units::Length;
use super::{Glyph, MathRoom, Shaper};
use crate::props::RunProps;
/// The lengths of a radical's parts at the size of the run of its marks and root sign, from the
/// `MATH` table of its math font.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RadicalLengths {
/// The gap between the top of the element and the bar (`RadicalVerticalGap`), in a line of
/// text and in a display alike.
pub gap: Length,
/// The thickness of the bar (`RadicalRuleThickness`).
pub rule: Length,
/// The room of the line above the bar: `RadicalExtraAscender`, and the `MathLeading` at the
/// size of the equation.
pub room_above: Length,
/// The white space above the bar that the radical's box holds (`RadicalExtraAscender`).
pub extra_ascender: Length,
/// The space before the degree (`RadicalKernBeforeDegree`).
pub kern_before_degree: Length,
/// The space after the degree, before the root sign (`RadicalKernAfterDegree`).
pub kern_after_degree: Length,
/// The height of the degree's baseline above the bottom of the root sign's ink, in percent
/// of the sign's ink height (`RadicalDegreeBottomRaisePercent`).
pub degree_raise_percent: i64,
/// The least height of the element's top above the radical's baseline
/// (`FlattenedAccentBaseHeight`).
pub least_top: Length,
}
/// A root sign as drawn: the bottom and the top of its ink above the line's baseline, and the
/// height of its bar's top above the sign's own baseline, on a dot.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RootSign {
/// The bottom of the sign's ink above the line's baseline.
pub low: Length,
/// The top of the sign's ink above the line's baseline.
pub high: Length,
/// The top of the bar above the sign's baseline, on a dot.
pub bar_top: Length,
}
impl Shaper<'_> {
/// The lengths of the parts of a radical whose marks have the properties `props`, at their
/// size; none for a font without the radical's constants of a `MATH` table.
pub fn radical_lengths(&mut self, props: &RunProps) -> Option<RadicalLengths> {
let face = self.face(props, true)?;
let f = self.faces().get(face).copied()?;
let radical = self.fonts.math_radical(f.font)?;
let em = i64::from(f.metrics.units_per_em.max(1));
let at = |units: i32| f.scale(i64::from(units));
let leading = self.math_leading(props, f.font, em)?;
Some(RadicalLengths {
gap: at(radical.vertical_gap),
rule: at(radical.rule_thickness),
room_above: at(radical.extra_ascender) + leading,
extra_ascender: at(radical.extra_ascender),
kern_before_degree: at(radical.kern_before_degree),
kern_after_degree: at(radical.kern_after_degree),
degree_raise_percent: i64::from(radical.degree_bottom_raise_percent),
least_top: at(radical.flattened_accent_base_height),
})
}
/// Draws `glyph`, the root sign of a radical whose element's ink lies from the first to the
/// second of `ink` above the line's baseline, or nowhere for an element without ink. The
/// element's height runs from the bottom of its ink to its top, or to `least_top` above the
/// radical's baseline when its ink is lower. The sign draws the first vertical variant of
/// its glyph in the font's `MATH` table whose height reaches that height with the gap and
/// the bar's thickness, or else the tallest one, with the middle of its ink the bar's
/// thickness above the middle of the element's height, in whole dots, and the room of its
/// ink with `room_above` over it. The element starts at the end of the variant's advance.
/// Returns the sign's ink and its bar's top; none when the glyph's font has no vertical
/// variants of it.
pub fn root_sign(
&mut self,
glyph: &mut Glyph,
ink: Option<(Length, Length)>,
lengths: &RadicalLengths,
) -> Option<RootSign> {
let f = self.faces().get(glyph.face).copied()?;
let base = self.fonts.glyph(f.font, glyph.ch)?;
let em = i64::from(f.metrics.units_per_em.max(1));
let dot = self.settings.dot;
let baseline = dot.snap(f.rise);
let (low, high) = ink.unwrap_or((baseline, baseline));
let top = high.max(baseline + lengths.least_top);
let need = top - low + lengths.gap + lengths.rule;
// The least height in font units, up: the variant's height is at least the need.
let size = f.size.units().max(1);
let least = (need.units() * em + size - 1).div_euclid(size);
let least = i32::try_from(least.max(0)).unwrap_or(i32::MAX);
let (variant, _) = self.fonts.vertical_variant(f.font, base, least)?;
let (bottom, summit) = self.font_ink(f.font, variant)?;
let at = |units: i64| (f.size * units).div_round(em);
let middle = (low + top).div_round(2) + lengths.rule;
let own_middle = (f.size * (i64::from(bottom) + i64::from(summit))).div_round(2 * em);
let rise = dot.snap(middle - own_middle);
let mut drawn = f;
drawn.rise = rise;
drawn.raised = rise;
drawn.math.room = MathRoom::Ink {
leading: lengths.room_above,
};
self.draw_variant(glyph, (f, drawn), variant);
Some(RootSign {
low: rise + at(i64::from(bottom)),
high: rise + at(i64::from(summit)),
bar_top: dot.snap(at(i64::from(summit))),
})
}
}