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
use Result;
use compound::{HorizontalHeader, MaximumProfile};
use tape::{Tape, Value};

define_table! {
    #[doc = "Horizontal metrics."]
    pub HorizontalMetrics {
        hMetrics        (Vec<LongHorizontalMetric>),
        leftSideBearing (Vec<i16>                 ),
    }
}

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

impl HorizontalMetrics {
    /// Read the table.
    pub fn read<T: Tape>(tape: &mut T, header: &HorizontalHeader, profile: &MaximumProfile)
                         -> Result<Self> {

        let metrics = header.numberOfHMetrics as usize;
        let glyphs = profile.glyphs();
        debug_assert!(metrics <= glyphs);
        let bearings = glyphs - metrics;
        let mut table = HorizontalMetrics {
            hMetrics: Vec::with_capacity(metrics),
            leftSideBearing: Vec::with_capacity(bearings),
        };
        for _ in 0..metrics {
            table.hMetrics.push(try!(Value::read(tape)));
        }
        for _ in 0..bearings {
            table.leftSideBearing.push(try!(Value::read(tape)));
        }
        Ok(table)
    }
}