Skip to main content

f_office_fonts/
vdmx.rs

1//! Heights from the `VDMX` table, as the OpenType specification (`VDMX`) lays it out.
2
3use skrifa::raw::{FontData, TableProvider};
4use skrifa::{FontRef, Tag};
5
6use crate::{FontDb, FontId};
7
8impl FontDb {
9    /// Returns the largest height above the baseline and the largest depth below it, in
10    /// pixels and with the signs that the font stores (`yMax` and `yMin`), that a face's
11    /// `VDMX` table gives at `pels` pixels per em on a device with square pixels.
12    ///
13    /// The OpenType specification (`VDMX`) says how a ratio record matches a device: the
14    /// record's ratios, normalized to the device's x resolution, must take in its y
15    /// resolution, and a record of zeros matches every device. This reads the group of the
16    /// first record that matches the device and that is for the Windows ANSI character set
17    /// (`bCharSet` 1), in a table of either version. A group for all glyphs (`bCharSet` 0)
18    /// does not count in either version.
19    ///
20    /// Returns `None` for an unknown face, a face without such a group, or a group that does
21    /// not list `pels`.
22    #[must_use]
23    pub fn device_heights(&self, id: FontId, pels: u16) -> Option<(i16, i16)> {
24        let face = self.face(id)?;
25        let font = FontRef::from_index(face.data()?, face.index).ok()?;
26        let table = font.data_for_tag(Tag::new(b"VDMX"))?;
27        heights(table.as_bytes(), pels)
28    }
29}
30
31/// The heights of [`FontDb::device_heights`] from the bytes of a `VDMX` table.
32pub(crate) fn heights(table: &[u8], pels: u16) -> Option<(i16, i16)> {
33    let data = FontData::new(table);
34    let u16_at = |at: usize| data.read_at::<u16>(at).ok();
35    let i16_at = |at: usize| data.read_at::<i16>(at).ok();
36    let ratios = usize::from(u16_at(4)?);
37    let index = (0..ratios).find(|&index| {
38        let Some(&[char_set, x, first, last]) = table.get(6 + 4 * index..10 + 4 * index) else {
39            return false;
40        };
41        let square = (x, first, last) == (0, 0, 0) || (first <= x && x <= last);
42        square && char_set == 1
43    })?;
44    let group = usize::from(u16_at(6 + 4 * ratios + 2 * index)?);
45    let count = usize::from(u16_at(group)?);
46    (0..count).find_map(|record| {
47        let at = group + 4 + 6 * record;
48        (u16_at(at)? == pels).then_some(())?;
49        Some((i16_at(at + 2)?, i16_at(at + 4)?))
50    })
51}
52
53#[cfg(test)]
54mod tests {
55    use super::heights;
56
57    /// A `VDMX` table of `version` with one ratio record (`char_set` and `ratio`) and one
58    /// group that lists the pel heights 8 and 48.
59    fn table(version: u16, char_set: u8, ratio: [u8; 3]) -> Vec<u8> {
60        let mut bytes = Vec::new();
61        for value in [version, 1, 1] {
62            bytes.extend_from_slice(&value.to_be_bytes());
63        }
64        bytes.push(char_set);
65        bytes.extend_from_slice(&ratio);
66        bytes.extend_from_slice(&12_u16.to_be_bytes());
67        bytes.extend_from_slice(&[0, 2, 8, 48]);
68        for (pels, y_max, y_min) in [(8_u16, 7_i16, -2_i16), (48, 43, -12)] {
69            bytes.extend_from_slice(&pels.to_be_bytes());
70            bytes.extend_from_slice(&y_max.to_be_bytes());
71            bytes.extend_from_slice(&y_min.to_be_bytes());
72        }
73        bytes
74    }
75
76    #[test]
77    fn reads_the_heights_of_a_listed_pel_height() {
78        let ansi = table(0, 1, [1, 1, 1]);
79        assert_eq!(heights(&ansi, 48), Some((43, -12)));
80        assert_eq!(heights(&ansi, 8), Some((7, -2)));
81        // A pel height that the group does not list.
82        assert_eq!(heights(&ansi, 32), None);
83        // A record of zeros, and one for 1:1 through 2:1, match square pixels.
84        assert_eq!(heights(&table(0, 1, [0, 0, 0]), 48), Some((43, -12)));
85        assert_eq!(heights(&table(0, 1, [2, 1, 2]), 48), Some((43, -12)));
86    }
87
88    #[test]
89    fn ignores_groups_for_other_character_sets_and_devices() {
90        // A group for all glyphs, in either version.
91        assert_eq!(heights(&table(0, 0, [1, 1, 1]), 48), None);
92        assert_eq!(heights(&table(1, 0, [1, 1, 1]), 48), None);
93        // A group for the ANSI character set in version 1.
94        assert_eq!(heights(&table(1, 1, [1, 1, 1]), 48), Some((43, -12)));
95        // The ratio 2:1 only.
96        assert_eq!(heights(&table(0, 1, [2, 1, 1]), 48), None);
97        // A table cut short.
98        assert_eq!(heights(&table(0, 1, [1, 1, 1])[..20], 48), None);
99    }
100
101    #[test]
102    fn gives_nothing_for_a_face_without_the_table() {
103        let mut fonts = crate::FontDb::new();
104        let id = fonts.add_font(crate::tests::CARLITO.to_vec()).unwrap()[0];
105        assert_eq!(fonts.device_heights(id, 48), None);
106        assert_eq!(fonts.device_heights(crate::FontId::from_index(9), 48), None);
107    }
108}