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
//! A [Vertical Origin Table](
//! https://docs.microsoft.com/en-us/typography/opentype/spec/vorg) implementation.

use crate::parser::{FromData, LazyArray16, Stream};
use crate::GlyphId;

/// Vertical origin metrics for the
/// [Vertical Origin Table](https://docs.microsoft.com/en-us/typography/opentype/spec/vorg).
#[derive(Clone, Copy, Debug)]
pub struct VerticalOriginMetrics {
    /// Glyph ID.
    pub glyph_id: GlyphId,
    /// Y coordinate, in the font's design coordinate system, of the vertical origin.
    pub y: i16,
}

impl FromData for VerticalOriginMetrics {
    const SIZE: usize = 4;

    #[inline]
    fn parse(data: &[u8]) -> Option<Self> {
        let mut s = Stream::new(data);
        Some(VerticalOriginMetrics {
            glyph_id: s.read::<GlyphId>()?,
            y: s.read::<i16>()?,
        })
    }
}

/// A [Vertical Origin Table](https://docs.microsoft.com/en-us/typography/opentype/spec/vorg).
#[derive(Clone, Copy, Debug)]
pub struct Table<'a> {
    /// Default origin.
    pub default_y: i16,
    /// A list of metrics for each glyph.
    ///
    /// Ordered by `glyph_id`.
    pub metrics: LazyArray16<'a, VerticalOriginMetrics>,
}

impl<'a> Table<'a> {
    /// Parses a table from raw data.
    pub fn parse(data: &'a [u8]) -> Option<Self> {
        let mut s = Stream::new(data);

        let version = s.read::<u32>()?;
        if version != 0x00010000 {
            return None;
        }

        let default_y = s.read::<i16>()?;
        let count = s.read::<u16>()?;
        let metrics = s.read_array16::<VerticalOriginMetrics>(count)?;

        Some(Table { default_y, metrics })
    }

    /// Returns glyph's Y origin.
    pub fn glyph_y_origin(&self, glyph_id: GlyphId) -> i16 {
        self.metrics
            .binary_search_by(|m| m.glyph_id.cmp(&glyph_id))
            .map(|(_, m)| m.y)
            .unwrap_or(self.default_y)
    }
}