ttf_parser/tables/
vorg.rs

1//! A [Vertical Origin Table](
2//! https://docs.microsoft.com/en-us/typography/opentype/spec/vorg) implementation.
3
4use crate::GlyphId;
5use crate::parser::{Stream, FromData, LazyArray16};
6
7/// Vertical origin metrics for the
8/// [Vertical Origin Table](https://docs.microsoft.com/en-us/typography/opentype/spec/vorg).
9#[derive(Clone, Copy, Debug)]
10pub struct VerticalOriginMetrics {
11    /// Glyph ID.
12    pub glyph_id: GlyphId,
13    /// Y coordinate, in the font's design coordinate system, of the vertical origin.
14    pub y: i16,
15}
16
17impl FromData for VerticalOriginMetrics {
18    const SIZE: usize = 4;
19
20    #[inline]
21    fn parse(data: &[u8]) -> Option<Self> {
22        let mut s = Stream::new(data);
23        Some(VerticalOriginMetrics {
24            glyph_id: s.read::<GlyphId>()?,
25            y: s.read::<i16>()?,
26        })
27    }
28}
29
30
31/// A [Vertical Origin Table](https://docs.microsoft.com/en-us/typography/opentype/spec/vorg).
32#[derive(Clone, Copy, Debug)]
33pub struct Table<'a> {
34    /// Default origin.
35    pub default_y: i16,
36    /// A list of metrics for each glyph.
37    ///
38    /// Ordered by `glyph_id`.
39    pub metrics: LazyArray16<'a, VerticalOriginMetrics>,
40}
41
42impl<'a> Table<'a> {
43    /// Parses a table from raw data.
44    pub fn parse(data: &'a [u8]) -> Option<Self> {
45        let mut s = Stream::new(data);
46
47        let version = s.read::<u32>()?;
48        if version != 0x00010000 {
49            return None;
50        }
51
52        let default_y = s.read::<i16>()?;
53        let count = s.read::<u16>()?;
54        let metrics = s.read_array16::<VerticalOriginMetrics>(count)?;
55
56        Some(Table {
57            default_y,
58            metrics,
59        })
60    }
61
62    /// Returns glyph's Y origin.
63    pub fn glyph_y_origin(&self, glyph_id: GlyphId) -> i16 {
64        self.metrics.binary_search_by(|m| m.glyph_id.cmp(&glyph_id))
65            .map(|(_, m)| m.y)
66            .unwrap_or(self.default_y)
67    }
68}