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
//! The [horizontal metrics][1].
//!
//! [1]: https://www.microsoft.com/typography/otspec/hmtx.htm

use {HorizontalHeader, MaximumProfile, Result, Tape, Walue};

table! {
    @define
    #[doc = "Horizontal metrics."]
    pub HorizontalMetrics {
        records            (Vec<Record>), // hMetrics
        left_side_bearings (Vec<i16>   ), // leftSideBearing
    }
}

table! {
    #[doc = "A record of horizontal metrics."]
    #[derive(Copy)]
    pub Record { // longHorMetric
        advance_width     (u16), // advanceWidth
        left_side_bearing (i16), // lsb
    }
}

impl HorizontalMetrics {
    /// Return the advance width and left side bearing.
    pub fn get(&self, mut index: usize) -> (u16, i16) {
        let longs = self.records.len();
        if index < longs {
            (self.records[index].advance_width, self.records[index].left_side_bearing)
        } else {
            let shorts = self.left_side_bearings.len();
            index -= longs;
            if index < shorts {
                (self.records[longs - 1].advance_width, self.left_side_bearings[index])
            } else {
                (self.records[longs - 1].advance_width, self.left_side_bearings[shorts - 1])
            }
        }
    }
}

impl<'l> Walue<(&'l HorizontalHeader, &'l MaximumProfile)> for HorizontalMetrics {
    fn read<T: Tape>(tape: &mut T, (header, profile): (&HorizontalHeader, &MaximumProfile))
                     -> Result<Self> {

        let metric_count = header.horizontal_metric_count as usize;
        let glyph_count = profile.glyph_count();
        if metric_count == 0 || metric_count > glyph_count {
            raise!("found a malformed horizontal header");
        }
        let bearing_count = glyph_count - metric_count;
        let mut table = HorizontalMetrics {
            records: Vec::with_capacity(metric_count),
            left_side_bearings: Vec::with_capacity(bearing_count),
        };
        for _ in 0..metric_count {
            table.records.push(try!(tape.take()));
        }
        for _ in 0..bearing_count {
            table.left_side_bearings.push(try!(tape.take()));
        }
        Ok(table)
    }
}