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
//! Where a script of an equation starts after its base, and the space after the scripts, from
//! the `MATH` table of the math font: the italic correction and the corner kerns of the base's
//! last glyph, the corner kerns of the script's first glyph, and `SpaceAfterScript`.
use f_office_fonts::MathCorner;
use f_office_units::Length;
use super::{Face, FaceIndex, Glyph, Shaper};
use crate::body::ScriptSide;
impl Shaper<'_> {
/// How far right of the end of the base's last glyph `base` a script whose first glyph is
/// `script` starts, on the side `side`; negative when it starts to the left. `ink` is the
/// lowest and the highest point of the whole script's ink above the baseline of `script`,
/// none when the script has no ink.
///
/// A superscript starts after the base glyph's italic correction, and moves by the kern of
/// the base glyph's top right corner and of the script glyph's bottom left corner, the
/// larger of their sums at two heights: the bottom of the script's ink and the top of the
/// base's ink. A subscript moves by the kern of the base glyph's bottom right corner and of
/// the script glyph's top left corner, the larger of their sums at the top of the script's
/// ink and the bottom of the base's ink. A corner that the `MATH` table does not list kerns
/// by nothing, and so do the corners of a base or a script without ink.
#[must_use]
pub fn script_lead(
&self,
base: &Glyph,
(script, ink): (&Glyph, Option<(Length, Length)>),
side: ScriptSide,
) -> Length {
let faces = self.faces();
let (Some(base_face), Some(script_face)) = (faces.get(base.face), faces.get(script.face))
else {
return Length::ZERO;
};
let italic = match side {
ScriptSide::Sup => self.glyph_italic_correction(base).unwrap_or(Length::ZERO),
ScriptSide::Sub => Length::ZERO,
};
let (Some((base_low, base_high)), Some((low, high))) = (self.ink(base), ink) else {
return italic;
};
let rise = script_face.rise - base_face.rise;
let (base_corner, script_corner, heights) = match side {
ScriptSide::Sup => (
MathCorner::TopRight,
MathCorner::BottomLeft,
[rise + low, base_high],
),
ScriptSide::Sub => (
MathCorner::BottomRight,
MathCorner::TopLeft,
[rise + high, base_low],
),
};
let kern = |height: Length| {
self.corner_kern((base_face, base), base_corner, height)
+ self.corner_kern((script_face, script), script_corner, height - rise)
};
let [first, second] = heights.map(kern);
italic + first.max(second)
}
/// The kern of the corner `corner` of `glyph` of `face` at `height` from the glyph's
/// baseline, at the face's size; nothing when the `MATH` table does not list the corner.
/// The height counts in font units rounded down, so a height just below a correction
/// height takes the band below it.
fn corner_kern(
&self,
(face, glyph): (&Face, &Glyph),
corner: MathCorner,
height: Length,
) -> Length {
let em = i64::from(face.metrics.units_per_em.max(1));
let units = (height.units() * em).div_euclid(face.size.units().max(1));
i32::try_from(units)
.ok()
.and_then(|units| self.fonts.math_kern(face.font, glyph.id, corner, units))
.map_or(Length::ZERO, |kern| face.scale(i64::from(kern)))
}
/// The space after the scripts of a base of the face `face`, from the `MATH` table of its
/// font (`SpaceAfterScript`); nothing for a font without the table.
#[must_use]
pub fn space_after_script(&self, face: FaceIndex) -> Length {
let Some(face) = self.faces().get(face) else {
return Length::ZERO;
};
self.fonts
.space_after_script(face.font)
.map_or(Length::ZERO, |units| face.scale(i64::from(units)))
}
/// Moves the glyphs after `glyph` by `by`, to the right when it is positive, as the space
/// between a base and its script or after a script does: the change goes into the glyph's
/// advance, which its written width keeps apart from.
pub fn widen_glyph(&self, glyph: &mut Glyph, by: Length) {
self.kern_glyph(glyph, by);
}
}