| 1 |
#[rustfmt::skip] mod aat; |
| 2 |
#[rustfmt::skip] mod ankr; |
| 3 |
#[rustfmt::skip] mod cff1; |
| 4 |
#[rustfmt::skip] mod cmap; |
| 5 |
#[rustfmt::skip] mod colr; |
| 6 |
#[rustfmt::skip] mod feat; |
| 7 |
#[rustfmt::skip] mod glyf; |
| 8 |
#[rustfmt::skip] mod hmtx; |
| 9 |
#[rustfmt::skip] mod maxp; |
| 10 |
#[rustfmt::skip] mod sbix; |
| 11 |
#[rustfmt::skip] mod trak; |
| 12 |
|
| 13 |
use ttf_parser::{fonts_in_collection, Face, FaceParsingError}; |
| 14 |
|
| 15 |
#[allow(dead_code)] |
| 16 |
#[derive(Clone, Copy)] |
| 17 |
pub enum Unit { |
| 18 |
Raw(&'static [u8]), |
| 19 |
Int8(i8), |
| 20 |
UInt8(u8), |
| 21 |
Int16(i16), |
| 22 |
UInt16(u16), |
| 23 |
Int32(i32), |
| 24 |
UInt32(u32), |
| 25 |
Fixed(f32), |
| 26 |
} |
| 27 |
|
| 28 |
pub fn convert(units: &[Unit]) -> Vec<u8> { |
| 29 |
let mut data = Vec::with_capacity(256); |
| 30 |
for v in units { |
| 31 |
convert_unit(*v, &mut data); |
| 32 |
} |
| 33 |
|
| 34 |
data |
| 35 |
} |
| 36 |
|
| 37 |
fn convert_unit(unit: Unit, data: &mut Vec<u8>) { |
| 38 |
match unit { |
| 39 |
Unit::Raw(bytes) => { |
| 40 |
data.extend_from_slice(bytes); |
| 41 |
} |
| 42 |
Unit::Int8(n) => { |
| 43 |
data.extend_from_slice(&i8::to_be_bytes(n)); |
| 44 |
} |
| 45 |
Unit::UInt8(n) => { |
| 46 |
data.extend_from_slice(&u8::to_be_bytes(n)); |
| 47 |
} |
| 48 |
Unit::Int16(n) => { |
| 49 |
data.extend_from_slice(&i16::to_be_bytes(n)); |
| 50 |
} |
| 51 |
Unit::UInt16(n) => { |
| 52 |
data.extend_from_slice(&u16::to_be_bytes(n)); |
| 53 |
} |
| 54 |
Unit::Int32(n) => { |
| 55 |
data.extend_from_slice(&i32::to_be_bytes(n)); |
| 56 |
} |
| 57 |
Unit::UInt32(n) => { |
| 58 |
data.extend_from_slice(&u32::to_be_bytes(n)); |
| 59 |
} |
| 60 |
Unit::Fixed(n) => { |
| 61 |
data.extend_from_slice(&i32::to_be_bytes((n * 65536.0) as i32)); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
#[test] |
| 67 |
fn empty_font() { |
| 68 |
assert_eq!( |
| 69 |
Face::parse(&[], 0).unwrap_err(), |
| 70 |
FaceParsingError::UnknownMagic |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
#[test] |
| 75 |
fn zero_tables() { |
| 76 |
use Unit::*; |
| 77 |
let data = convert(&[ |
| 78 |
Raw(&[0x00, 0x01, 0x00, 0x00]), // magic |
| 79 |
UInt16(0), // numTables |
| 80 |
UInt16(0), // searchRange |
| 81 |
UInt16(0), // entrySelector |
| 82 |
UInt16(0), // rangeShift |
| 83 |
]); |
| 84 |
|
| 85 |
assert_eq!( |
| 86 |
Face::parse(&data, 0).unwrap_err(), |
| 87 |
FaceParsingError::NoHeadTable |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
#[test] |
| 92 |
fn tables_count_overflow() { |
| 93 |
use Unit::*; |
| 94 |
let data = convert(&[ |
| 95 |
Raw(&[0x00, 0x01, 0x00, 0x00]), // magic |
| 96 |
UInt16(u16::MAX), // numTables |
| 97 |
UInt16(0), // searchRange |
| 98 |
UInt16(0), // entrySelector |
| 99 |
UInt16(0), // rangeShift |
| 100 |
]); |
| 101 |
|
| 102 |
assert_eq!( |
| 103 |
Face::parse(&data, 0).unwrap_err(), |
| 104 |
FaceParsingError::MalformedFont |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
#[test] |
| 109 |
fn empty_font_collection() { |
| 110 |
use Unit::*; |
| 111 |
let data = convert(&[ |
| 112 |
Raw(&[0x74, 0x74, 0x63, 0x66]), // magic |
| 113 |
UInt16(0), // majorVersion |
| 114 |
UInt16(0), // minorVersion |
| 115 |
UInt32(0), // numFonts |
| 116 |
]); |
| 117 |
|
| 118 |
assert_eq!(fonts_in_collection(&data), Some(0)); |
| 119 |
assert_eq!( |
| 120 |
Face::parse(&data, 0).unwrap_err(), |
| 121 |
FaceParsingError::FaceIndexOutOfBounds |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
#[test] |
| 126 |
fn font_collection_num_fonts_overflow_1() { |
| 127 |
use Unit::*; |
| 128 |
let data = convert(&[ |
| 129 |
Raw(&[0x74, 0x74, 0x63, 0x66]), // magic |
| 130 |
UInt16(0), // majorVersion |
| 131 |
UInt16(0), // minorVersion |
| 132 |
UInt32(u32::MAX), // numFonts |
| 133 |
]); |
| 134 |
|
| 135 |
assert_eq!(fonts_in_collection(&data), Some(u32::MAX)); |
| 136 |
} |
| 137 |
|
| 138 |
#[test] |
| 139 |
#[should_panic] |
| 140 |
fn font_collection_num_fonts_overflow_2() { |
| 141 |
use Unit::*; |
| 142 |
let data = convert(&[ |
| 143 |
Raw(&[0x74, 0x74, 0x63, 0x66]), // magic |
| 144 |
UInt16(0), // majorVersion |
| 145 |
UInt16(0), // minorVersion |
| 146 |
UInt32(u32::MAX), // numFonts |
| 147 |
]); |
| 148 |
|
| 149 |
assert_eq!( |
| 150 |
Face::parse(&data, 0).unwrap_err(), |
| 151 |
FaceParsingError::MalformedFont |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
#[test] |
| 156 |
fn font_index_overflow() { |
| 157 |
use Unit::*; |
| 158 |
let data = convert(&[ |
| 159 |
Raw(&[0x74, 0x74, 0x63, 0x66]), // magic |
| 160 |
UInt16(0), // majorVersion |
| 161 |
UInt16(0), // minorVersion |
| 162 |
UInt32(1), // numFonts |
| 163 |
UInt32(12), // offset [0] |
| 164 |
]); |
| 165 |
|
| 166 |
assert_eq!(fonts_in_collection(&data), Some(1)); |
| 167 |
assert_eq!( |
| 168 |
Face::parse(&data, u32::MAX).unwrap_err(), |
| 169 |
FaceParsingError::FaceIndexOutOfBounds |
| 170 |
); |
| 171 |
} |
| 172 |
|
| 173 |
#[test] |
| 174 |
fn font_index_overflow_on_regular_font() { |
| 175 |
use Unit::*; |
| 176 |
let data = convert(&[ |
| 177 |
Raw(&[0x00, 0x01, 0x00, 0x00]), // magic |
| 178 |
UInt16(0), // numTables |
| 179 |
UInt16(0), // searchRange |
| 180 |
UInt16(0), // entrySelector |
| 181 |
UInt16(0), // rangeShift |
| 182 |
]); |
| 183 |
|
| 184 |
assert_eq!(fonts_in_collection(&data), None); |
| 185 |
assert_eq!( |
| 186 |
Face::parse(&data, 1).unwrap_err(), |
| 187 |
FaceParsingError::FaceIndexOutOfBounds |
| 188 |
); |
| 189 |
} |