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
//! The [hmtx](https://docs.microsoft.com/en-us/typography/opentype/spec/hmtx)
//! table parsing primitives.

use crate::stream::Stream;
use crate::Font;


/// A horizontal metrics of a glyph.
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct HorizontalMetrics {
    /// A horizontal advance.
    pub advance: u16,

    /// Left side bearing.
    pub left_side_bearing: i16,
}

impl<'a> Font<'a> {
    /// Returns glyph's horizontal metrics.
    ///
    /// Returns `None` when font doesn't have such `glyph_id`.
    pub fn glyph_hor_metrics(&self, glyph_id: u16) -> Option<HorizontalMetrics> {
        const HOR_METRIC_RECORD_SIZE: usize = 4;
        const U16_SIZE: usize = 2;

        if glyph_id >= self.number_of_glyphs {
            return None;
        }

        let number_of_hmetrics = self.number_of_hmetrics();
        let data = &self.data[self.hmtx.range()];

        if glyph_id < number_of_hmetrics {
            // Records are indexed by glyph ID.
            let data = &data[glyph_id as usize * HOR_METRIC_RECORD_SIZE..];
            let mut s = Stream::new(data);
            Some(HorizontalMetrics {
                advance: s.read_u16(),
                left_side_bearing: s.read_i16(),
            })
        } else {
            assert!(number_of_hmetrics > 0);

            let left_side_bearings_offset = number_of_hmetrics as usize * HOR_METRIC_RECORD_SIZE;

            // 'As an optimization, the number of records can be less than the number of glyphs,
            // in which case the advance width value of the last record applies
            // to all remaining glyph IDs.'
            let advance = Stream::read_at(
                data, left_side_bearings_offset - HOR_METRIC_RECORD_SIZE
            );

            let left_side_bearing = Stream::read_at(
                data, left_side_bearings_offset + (glyph_id - number_of_hmetrics) as usize * U16_SIZE
            );

            Some(HorizontalMetrics {
                advance,
                left_side_bearing,
            })
        }
    }
}