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
//! Font-wide metrics, read as the font stores them.
use skrifa::font::Tables;
use skrifa::raw::TableProvider;
use skrifa::raw::tables::os2::{Os2, SelectionFlags};
use crate::FontError;
/// Font-wide metrics in font units, with the values and signs that the font's tables store.
///
/// A font has three sets of line metrics, in `hhea`, in the typographic fields of `OS/2` and in
/// the Windows fields of `OS/2`, and they often differ. This type reports all of them and
/// chooses none, because each Office app has its own rule for which one it uses.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FontMetrics {
/// Font units in one em, from `head`. Never zero.
pub units_per_em: u16,
/// Line metrics from `hhea`.
pub hhea: LineMetrics,
/// Metrics from `OS/2`, or `None` when the font has no readable `OS/2` table.
pub os2: Option<Os2Metrics>,
/// The underline from `post`, or `None` when the font has no readable `post` table.
pub underline: Option<Decoration>,
}
/// An ascender, a descender and a line gap.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LineMetrics {
/// Height above the baseline.
pub ascender: i16,
/// Depth below the baseline, which fonts normally store as a negative value.
pub descender: i16,
/// Extra space between lines.
pub line_gap: i16,
}
/// Metrics from the `OS/2` table.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Os2Metrics {
/// `xAvgCharWidth`, the average width of the font's characters.
pub average_width: i16,
/// `sTypoAscender`, `sTypoDescender` and `sTypoLineGap`.
pub typo: LineMetrics,
/// `usWinAscent`, the height above the baseline.
pub win_ascent: u16,
/// `usWinDescent`, the depth below the baseline as a positive value.
pub win_descent: u16,
/// The `USE_TYPO_METRICS` bit of `fsSelection`, as stored. The specification defines the bit
/// from version 4 of the table.
pub use_typo_metrics: bool,
/// `sxHeight`, or `None` before version 2 of the table.
pub x_height: Option<i16>,
/// `sCapHeight`, or `None` before version 2 of the table.
pub cap_height: Option<i16>,
/// `yStrikeoutPosition` and `yStrikeoutSize`.
pub strikeout: Decoration,
/// The `ySubscript` fields.
pub subscript: ScriptMetrics,
/// The `ySuperscript` fields.
pub superscript: ScriptMetrics,
/// Whether `ulCodePageRange1` names a Japanese, Chinese or Korean code page (bits 17 to
/// 21), as fonts made for East Asian text do.
pub east_asian: bool,
}
/// The bits of `ulCodePageRange1` for the code pages 932, 936, 949, 950 and 1361.
const EAST_ASIAN_CODE_PAGES: u32 = 0b1_1111 << 17;
/// The position and thickness of a line drawn with text, such as an underline.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Decoration {
/// Position of the top of the line, measured upwards from the baseline. A negative value is
/// below the baseline.
pub position: i16,
/// Thickness of the line.
pub thickness: i16,
}
/// The size and offset that a font recommends for subscripts or superscripts.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ScriptMetrics {
/// Horizontal size of the em.
pub x_size: i16,
/// Vertical size of the em.
pub y_size: i16,
/// Horizontal offset.
pub x_offset: i16,
/// Vertical offset. For subscripts, a positive value moves the text down.
pub y_offset: i16,
}
impl FontMetrics {
/// Reads the metrics, or fails when `head` or `hhea` is missing or malformed, or when the
/// units per em are zero.
pub(crate) fn read(tables: &Tables) -> Result<Self, FontError> {
let head = tables.head().map_err(|_| FontError::BadTable("head"))?;
let units_per_em = head.units_per_em();
if units_per_em == 0 {
return Err(FontError::BadTable("head"));
}
let hhea = tables.hhea().map_err(|_| FontError::BadTable("hhea"))?;
Ok(Self {
units_per_em,
hhea: LineMetrics {
ascender: hhea.ascender().to_i16(),
descender: hhea.descender().to_i16(),
line_gap: hhea.line_gap().to_i16(),
},
os2: tables.os2().ok().map(|os2| Os2Metrics::read(&os2)),
underline: tables.post().ok().map(|post| Decoration {
position: post.underline_position().to_i16(),
thickness: post.underline_thickness().to_i16(),
}),
})
}
}
impl Os2Metrics {
fn read(os2: &Os2<'_>) -> Self {
Self {
average_width: os2.x_avg_char_width(),
typo: LineMetrics {
ascender: os2.s_typo_ascender(),
descender: os2.s_typo_descender(),
line_gap: os2.s_typo_line_gap(),
},
win_ascent: os2.us_win_ascent(),
win_descent: os2.us_win_descent(),
use_typo_metrics: os2
.fs_selection()
.contains(SelectionFlags::USE_TYPO_METRICS),
x_height: os2.sx_height(),
cap_height: os2.s_cap_height(),
strikeout: Decoration {
position: os2.y_strikeout_position(),
thickness: os2.y_strikeout_size(),
},
subscript: ScriptMetrics {
x_size: os2.y_subscript_x_size(),
y_size: os2.y_subscript_y_size(),
x_offset: os2.y_subscript_x_offset(),
y_offset: os2.y_subscript_y_offset(),
},
superscript: ScriptMetrics {
x_size: os2.y_superscript_x_size(),
y_size: os2.y_superscript_y_size(),
x_offset: os2.y_superscript_x_offset(),
y_offset: os2.y_superscript_y_offset(),
},
east_asian: os2
.ul_code_page_range_1()
.is_some_and(|range| range & EAST_ASIAN_CODE_PAGES != 0),
}
}
}