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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#![allow(non_snake_case)]

use input::Read;
use Result;

/// A 16-bit unsigned integer.
pub type USHORT = u16;

/// A 16-bit signed integer.
pub type SHORT = i16;

/// A 32-bit unsigned integer.
pub type ULONG = u32;

/// A 32-bit signed fixed-point number (16.16).
#[derive(Clone, Copy, Default, Eq, PartialEq)]
pub struct Fixed(u32);

/// A date represented in number of seconds since 12:00 midnight, January 1,
/// 1904. The value is represented as a signed 64-bit integer.
pub type LONGDATETIME = i64;

/// A vector of `USHORT`.
pub type VecUSHORT = Vec<USHORT>;

/// A vector of `SHORT`.
pub type VecSHORT = Vec<SHORT>;

pub const CFF_FORMAT_TAG: [u8; 4] = [b'O', b'T', b'T', b'O'];

pub const CHAR_MAPPING_TAG: [u8; 4] = [b'c', b'm', b'a', b'p'];
pub const CHAR_MAPPING_HEADER_VERSION_0_0: USHORT = 0;

pub const FONT_HEADER_TAG: [u8; 4] = [b'h', b'e', b'a', b'd'];
pub const FONT_HEADER_VERSION_1_0: Fixed = Fixed(0x00010000);
pub const FONT_HEADER_MAGIC_NUMBER: ULONG = 0x5F0F3CF5;

pub const MAXIMAL_PROFILE_TAG: [u8; 4] = [b'm', b'a', b'x', b'p'];
pub const MAXIMAL_PROFILE_VERSION_0_5: Fixed = Fixed(0x00005000);

pub trait Table {
    fn read<R: Read>(&mut self, reader: &mut R) -> Result<()>;
}

macro_rules! define(
    ($name:ident: $($class:ident $field:ident,)+) => (
        #[derive(Default)]
        pub struct $name { $(pub $field: $class,)+ }
        implement!($name: $($field as $class,)+);
    )
);

macro_rules! implement(
    ($name:ident: $($field:ident as $class:ident,)+) => (
        impl Table for $name {
            fn read<R: Read>(&mut self, reader: &mut R) -> Result<()> {
                $(self.$field = read!(reader as $class);)+
                Ok(())
            }
        }
    )
);

macro_rules! read(
    ($reader:ident as USHORT) => (try!($reader.read_u16()));
    ($reader:ident as SHORT) => (try!($reader.read_i16()));
    ($reader:ident as ULONG) => (try!($reader.read_u32()));
    ($reader:ident as Fixed) => (Fixed(try!($reader.read_u32())));
    ($reader:ident as LONGDATETIME) => (try!($reader.read_i64()));
    ($reader:ident as VecUSHORT) => ({
        vec![]
    });
    ($reader:ident as VecSHORT) => ({
        vec![]
    });
);

define!(
    OffsetTable:

    Fixed version,
    USHORT numTables,
    USHORT searchRange,
    USHORT entrySelector,
    USHORT rangeShift,
);

define!(
    TableRecord:

    ULONG tag,
    ULONG checkSum,
    ULONG offset,
    ULONG length,
);

define!(
    CharMappingHeader:

    USHORT version,
    USHORT numTables,
);

define!(
    EncodingRecord:

    USHORT platformID,
    USHORT encodingID,
    ULONG offset,
);

define!(
    CharMappingFormat:

    USHORT version,
);

define!(
    CharMappingFormat4:

    USHORT format,
    USHORT length,
    USHORT language,
    USHORT segCountX2,
    USHORT searchRange,
    USHORT entrySelector,
    USHORT rangeShift,
    VecUSHORT endCount,
    USHORT reservedPad,
    VecUSHORT startCount,
    VecSHORT idDelta,
    VecUSHORT idRangeOffset,
    VecUSHORT glyphIdArray,
);

define!(
    CharMappingFormat6:

    USHORT format,
    USHORT length,
    USHORT language,
    USHORT firstCode,
    USHORT entryCount,
    VecUSHORT glyphIdArray,
);

define!(
    FontHeader:

    Fixed version,
    Fixed fontRevision,
    ULONG checkSumAdjustment,
    ULONG magicNumber,
    USHORT flags,
    USHORT unitsPerEm,
    LONGDATETIME created,
    LONGDATETIME modified,
    SHORT xMin,
    SHORT yMin,
    SHORT xMax,
    SHORT yMax,
    USHORT macStyle,
    USHORT lowestRecPPEM,
    SHORT fontDirectionHint,
    SHORT indexToLocFormat,
    SHORT glyphDataFormat,
);

define!(
    MaximumProfile:

    Fixed version,
    USHORT numGlyphs,
);

impl Fixed {
    /// Convert `Fixed` into `f32`.
    #[inline]
    pub fn to_f32(&self) -> f32 {
        use num::Float;
        ((self.0 as f32) * 0.0000152587890625 * 1000.0).round() / 1000.0
    }
}

#[cfg(test)]
mod tests {
    use std::default::Default;

    use input::Reader;
    use spec::Table;

    #[test]
    fn offset_table_read() {
        use spec::OffsetTable;

        let mut file = ::tests::open("SourceSerifPro-Regular.otf");
        let mut reader = Reader::new(&mut file);

        let mut table: OffsetTable = Default::default();
        assert_ok!(table.read(&mut reader));
        assert_eq!(table.version.0, 0x4f54544f);
        assert_eq!(table.numTables, 12);
        assert_eq!(table.searchRange, 8 * 16);
        assert_eq!(table.entrySelector, 3);
        assert_eq!(table.rangeShift, table.numTables * 16 - table.searchRange);
    }

    #[test]
    fn char_mapping_read() {
        use std::io::{Seek, SeekFrom};
        use spec::{CharMappingHeader, EncodingRecord};

        let mut file = ::tests::open("SourceSerifPro-Regular.otf");
        assert_ok!(file.seek(SeekFrom::Start(15668)));
        let mut reader = Reader::new(&mut file);

        let mut table: CharMappingHeader = Default::default();
        assert_ok!(table.read(&mut reader));
        assert_eq!(table.version, 0);
        assert_eq!(table.numTables, 3);

        let (platforms, encodings) = ([0, 1, 3], [3, 0, 1]);
        for i in 0..3 {
            let mut table: EncodingRecord = Default::default();
            assert_ok!(table.read(&mut reader));
            assert_eq!(table.platformID, platforms[i]);
            assert_eq!(table.encodingID, encodings[i]);
        }
    }
}