| 1 |
use std::num::NonZeroU16; |
| 2 |
use ttf_parser::GlyphId; |
| 3 |
use ttf_parser::apple_layout::Lookup; |
| 4 |
use crate::{convert, Unit::*}; |
| 5 |
|
| 6 |
mod format0 { |
| 7 |
use super::*; |
| 8 |
|
| 9 |
#[test] |
| 10 |
fn single() { |
| 11 |
let data = convert(&[ |
| 12 |
UInt16(0), // format |
| 13 |
UInt16(10), // value |
| 14 |
]); |
| 15 |
|
| 16 |
let table = Lookup::parse(NonZeroU16::new(1).unwrap(), &data).unwrap(); |
| 17 |
assert_eq!(table.value(GlyphId(0)).unwrap(), 10); |
| 18 |
assert!(table.value(GlyphId(1)).is_none()); |
| 19 |
} |
| 20 |
|
| 21 |
#[test] |
| 22 |
fn not_enough_glyphs() { |
| 23 |
let data = convert(&[ |
| 24 |
UInt16(0), // format |
| 25 |
UInt16(10), // value |
| 26 |
]); |
| 27 |
|
| 28 |
assert!(Lookup::parse(NonZeroU16::new(2).unwrap(), &data).is_none()); |
| 29 |
} |
| 30 |
|
| 31 |
#[test] |
| 32 |
fn too_many_glyphs() { |
| 33 |
let data = convert(&[ |
| 34 |
UInt16(0), // format |
| 35 |
UInt16(10), // value |
| 36 |
UInt16(11), // value <-- will be ignored |
| 37 |
]); |
| 38 |
|
| 39 |
let table = Lookup::parse(NonZeroU16::new(1).unwrap(), &data).unwrap(); |
| 40 |
assert_eq!(table.value(GlyphId(0)).unwrap(), 10); |
| 41 |
assert!(table.value(GlyphId(1)).is_none()); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
mod format2 { |
| 46 |
use super::*; |
| 47 |
|
| 48 |
#[test] |
| 49 |
fn single() { |
| 50 |
let data = convert(&[ |
| 51 |
UInt16(2), // format |
| 52 |
|
| 53 |
// Binary Search Table |
| 54 |
UInt16(6), // segment size |
| 55 |
UInt16(1), // number of segments |
| 56 |
UInt16(0), // search range: we don't use it |
| 57 |
UInt16(0), // entry selector: we don't use it |
| 58 |
UInt16(0), // range shift: we don't use it |
| 59 |
|
| 60 |
// Segment [0] |
| 61 |
UInt16(118), // last glyph |
| 62 |
UInt16(118), // first glyph |
| 63 |
UInt16(10), // value |
| 64 |
]); |
| 65 |
|
| 66 |
let table = Lookup::parse(NonZeroU16::new(1).unwrap(), &data).unwrap(); |
| 67 |
assert_eq!(table.value(GlyphId(118)).unwrap(), 10); |
| 68 |
assert!(table.value(GlyphId(1)).is_none()); |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn range() { |
| 73 |
let data = convert(&[ |
| 74 |
UInt16(2), // format |
| 75 |
|
| 76 |
// Binary Search Table |
| 77 |
UInt16(6), // segment size |
| 78 |
UInt16(1), // number of segments |
| 79 |
UInt16(0), // search range: we don't use it |
| 80 |
UInt16(0), // entry selector: we don't use it |
| 81 |
UInt16(0), // range shift: we don't use it |
| 82 |
|
| 83 |
// Segment [0] |
| 84 |
UInt16(7), // last glyph |
| 85 |
UInt16(5), // first glyph |
| 86 |
UInt16(18), // offset |
| 87 |
]); |
| 88 |
|
| 89 |
let table = Lookup::parse(NonZeroU16::new(1).unwrap(), &data).unwrap(); |
| 90 |
assert!(table.value(GlyphId(4)).is_none()); |
| 91 |
assert_eq!(table.value(GlyphId(5)).unwrap(), 18); |
| 92 |
assert_eq!(table.value(GlyphId(6)).unwrap(), 18); |
| 93 |
assert_eq!(table.value(GlyphId(7)).unwrap(), 18); |
| 94 |
assert!(table.value(GlyphId(8)).is_none()); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
mod format4 { |
| 99 |
use super::*; |
| 100 |
|
| 101 |
#[test] |
| 102 |
fn single() { |
| 103 |
let data = convert(&[ |
| 104 |
UInt16(4), // format |
| 105 |
|
| 106 |
// Binary Search Table |
| 107 |
UInt16(6), // segment size |
| 108 |
UInt16(1), // number of segments |
| 109 |
UInt16(0), // search range: we don't use it |
| 110 |
UInt16(0), // entry selector: we don't use it |
| 111 |
UInt16(0), // range shift: we don't use it |
| 112 |
|
| 113 |
// Segment [0] |
| 114 |
UInt16(118), // last glyph |
| 115 |
UInt16(118), // first glyph |
| 116 |
UInt16(18), // offset |
| 117 |
|
| 118 |
// Values [0] |
| 119 |
UInt16(10), // value [0] |
| 120 |
]); |
| 121 |
|
| 122 |
let table = Lookup::parse(NonZeroU16::new(1).unwrap(), &data).unwrap(); |
| 123 |
assert_eq!(table.value(GlyphId(118)).unwrap(), 10); |
| 124 |
assert!(table.value(GlyphId(1)).is_none()); |
| 125 |
} |
| 126 |
|
| 127 |
#[test] |
| 128 |
fn range() { |
| 129 |
let data = convert(&[ |
| 130 |
UInt16(4), // format |
| 131 |
|
| 132 |
// Binary Search Table |
| 133 |
UInt16(6), // segment size |
| 134 |
UInt16(1), // number of segments |
| 135 |
UInt16(0), // search range: we don't use it |
| 136 |
UInt16(0), // entry selector: we don't use it |
| 137 |
UInt16(0), // range shift: we don't use it |
| 138 |
|
| 139 |
// Segment [0] |
| 140 |
UInt16(7), // last glyph |
| 141 |
UInt16(5), // first glyph |
| 142 |
UInt16(18), // offset |
| 143 |
|
| 144 |
// Values [0] |
| 145 |
UInt16(10), // value [0] |
| 146 |
UInt16(11), // value [1] |
| 147 |
UInt16(12), // value [2] |
| 148 |
]); |
| 149 |
|
| 150 |
let table = Lookup::parse(NonZeroU16::new(1).unwrap(), &data).unwrap(); |
| 151 |
assert!(table.value(GlyphId(4)).is_none()); |
| 152 |
assert_eq!(table.value(GlyphId(5)).unwrap(), 10); |
| 153 |
assert_eq!(table.value(GlyphId(6)).unwrap(), 11); |
| 154 |
assert_eq!(table.value(GlyphId(7)).unwrap(), 12); |
| 155 |
assert!(table.value(GlyphId(8)).is_none()); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
mod format6 { |
| 160 |
use super::*; |
| 161 |
|
| 162 |
#[test] |
| 163 |
fn single() { |
| 164 |
let data = convert(&[ |
| 165 |
UInt16(6), // format |
| 166 |
|
| 167 |
// Binary Search Table |
| 168 |
UInt16(4), // segment size |
| 169 |
UInt16(1), // number of segments |
| 170 |
UInt16(0), // search range: we don't use it |
| 171 |
UInt16(0), // entry selector: we don't use it |
| 172 |
UInt16(0), // range shift: we don't use it |
| 173 |
|
| 174 |
// Segment [0] |
| 175 |
UInt16(0), // glyph |
| 176 |
UInt16(10), // value |
| 177 |
]); |
| 178 |
|
| 179 |
let table = Lookup::parse(NonZeroU16::new(1).unwrap(), &data).unwrap(); |
| 180 |
assert_eq!(table.value(GlyphId(0)).unwrap(), 10); |
| 181 |
assert!(table.value(GlyphId(1)).is_none()); |
| 182 |
} |
| 183 |
|
| 184 |
#[test] |
| 185 |
fn multiple() { |
| 186 |
let data = convert(&[ |
| 187 |
UInt16(6), // format |
| 188 |
|
| 189 |
// Binary Search Table |
| 190 |
UInt16(4), // segment size |
| 191 |
UInt16(3), // number of segments |
| 192 |
UInt16(0), // search range: we don't use it |
| 193 |
UInt16(0), // entry selector: we don't use it |
| 194 |
UInt16(0), // range shift: we don't use it |
| 195 |
|
| 196 |
// Segment [0] |
| 197 |
UInt16(0), // glyph |
| 198 |
UInt16(10), // value |
| 199 |
// Segment [1] |
| 200 |
UInt16(5), // glyph |
| 201 |
UInt16(20), // value |
| 202 |
// Segment [2] |
| 203 |
UInt16(10), // glyph |
| 204 |
UInt16(30), // value |
| 205 |
]); |
| 206 |
|
| 207 |
let table = Lookup::parse(NonZeroU16::new(1).unwrap(), &data).unwrap(); |
| 208 |
assert_eq!(table.value(GlyphId(0)).unwrap(), 10); |
| 209 |
assert_eq!(table.value(GlyphId(5)).unwrap(), 20); |
| 210 |
assert_eq!(table.value(GlyphId(10)).unwrap(), 30); |
| 211 |
assert!(table.value(GlyphId(1)).is_none()); |
| 212 |
} |
| 213 |
|
| 214 |
// Tests below are indirectly testing BinarySearchTable. |
| 215 |
|
| 216 |
#[test] |
| 217 |
fn no_segments() { |
| 218 |
let data = convert(&[ |
| 219 |
UInt16(6), // format |
| 220 |
|
| 221 |
// Binary Search Table |
| 222 |
UInt16(4), // segment size |
| 223 |
UInt16(0), // number of segments |
| 224 |
UInt16(0), // search range: we don't use it |
| 225 |
UInt16(0), // entry selector: we don't use it |
| 226 |
UInt16(0), // range shift: we don't use it |
| 227 |
]); |
| 228 |
|
| 229 |
assert!(Lookup::parse(NonZeroU16::new(1).unwrap(), &data).is_none()); |
| 230 |
} |
| 231 |
|
| 232 |
#[test] |
| 233 |
fn ignore_termination() { |
| 234 |
let data = convert(&[ |
| 235 |
UInt16(6), // format |
| 236 |
|
| 237 |
// Binary Search Table |
| 238 |
UInt16(4), // segment size |
| 239 |
UInt16(2), // number of segments |
| 240 |
UInt16(0), // search range: we don't use it |
| 241 |
UInt16(0), // entry selector: we don't use it |
| 242 |
UInt16(0), // range shift: we don't use it |
| 243 |
|
| 244 |
// Segment [0] |
| 245 |
UInt16(0), // glyph |
| 246 |
UInt16(10), // value |
| 247 |
// Segment [1] |
| 248 |
UInt16(0xFFFF), // glyph |
| 249 |
UInt16(0xFFFF), // value |
| 250 |
]); |
| 251 |
|
| 252 |
let table = Lookup::parse(NonZeroU16::new(1).unwrap(), &data).unwrap(); |
| 253 |
assert!(table.value(GlyphId(0xFFFF)).is_none()); |
| 254 |
} |
| 255 |
|
| 256 |
#[test] |
| 257 |
fn only_termination() { |
| 258 |
let data = convert(&[ |
| 259 |
UInt16(6), // format |
| 260 |
|
| 261 |
// Binary Search Table |
| 262 |
UInt16(4), // segment size |
| 263 |
UInt16(1), // number of segments |
| 264 |
UInt16(0), // search range: we don't use it |
| 265 |
UInt16(0), // entry selector: we don't use it |
| 266 |
UInt16(0), // range shift: we don't use it |
| 267 |
|
| 268 |
// Segment [0] |
| 269 |
UInt16(0xFFFF), // glyph |
| 270 |
UInt16(0xFFFF), // value |
| 271 |
]); |
| 272 |
|
| 273 |
assert!(Lookup::parse(NonZeroU16::new(1).unwrap(), &data).is_none()); |
| 274 |
} |
| 275 |
|
| 276 |
#[test] |
| 277 |
fn invalid_segment_size() { |
| 278 |
let data = convert(&[ |
| 279 |
UInt16(6), // format |
| 280 |
|
| 281 |
// Binary Search Table |
| 282 |
UInt16(8), // segment size <-- must be 4 |
| 283 |
UInt16(1), // number of segments |
| 284 |
UInt16(0), // search range: we don't use it |
| 285 |
UInt16(0), // entry selector: we don't use it |
| 286 |
UInt16(0), // range shift: we don't use it |
| 287 |
|
| 288 |
// Segment [0] |
| 289 |
UInt16(0), // glyph |
| 290 |
UInt16(10), // value |
| 291 |
]); |
| 292 |
|
| 293 |
assert!(Lookup::parse(NonZeroU16::new(1).unwrap(), &data).is_none()); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
mod format8 { |
| 298 |
use super::*; |
| 299 |
|
| 300 |
#[test] |
| 301 |
fn single() { |
| 302 |
let data = convert(&[ |
| 303 |
UInt16(8), // format |
| 304 |
UInt16(0), // first glyph |
| 305 |
UInt16(1), // glyphs count |
| 306 |
UInt16(2), // value [0] |
| 307 |
]); |
| 308 |
|
| 309 |
let table = Lookup::parse(NonZeroU16::new(1).unwrap(), &data).unwrap(); |
| 310 |
assert_eq!(table.value(GlyphId(0)).unwrap(), 2); |
| 311 |
assert!(table.value(GlyphId(1)).is_none()); |
| 312 |
} |
| 313 |
|
| 314 |
#[test] |
| 315 |
fn non_zero_first() { |
| 316 |
let data = convert(&[ |
| 317 |
UInt16(8), // format |
| 318 |
UInt16(5), // first glyph |
| 319 |
UInt16(1), // glyphs count |
| 320 |
UInt16(2), // value [0] |
| 321 |
]); |
| 322 |
|
| 323 |
let table = Lookup::parse(NonZeroU16::new(1).unwrap(), &data).unwrap(); |
| 324 |
assert_eq!(table.value(GlyphId(5)).unwrap(), 2); |
| 325 |
assert!(table.value(GlyphId(1)).is_none()); |
| 326 |
assert!(table.value(GlyphId(6)).is_none()); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
mod format10 { |
| 331 |
use super::*; |
| 332 |
|
| 333 |
#[test] |
| 334 |
fn single() { |
| 335 |
let data = convert(&[ |
| 336 |
UInt16(10), // format |
| 337 |
UInt16(1), // value size: u8 |
| 338 |
UInt16(0), // first glyph |
| 339 |
UInt16(1), // glyphs count |
| 340 |
UInt8(2), // value [0] |
| 341 |
]); |
| 342 |
|
| 343 |
let table = Lookup::parse(NonZeroU16::new(1).unwrap(), &data).unwrap(); |
| 344 |
assert_eq!(table.value(GlyphId(0)).unwrap(), 2); |
| 345 |
assert!(table.value(GlyphId(1)).is_none()); |
| 346 |
} |
| 347 |
|
| 348 |
#[test] |
| 349 |
fn invalid_value_size() { |
| 350 |
let data = convert(&[ |
| 351 |
UInt16(10), // format |
| 352 |
UInt16(50), // value size <-- invalid |
| 353 |
UInt16(0), // first glyph |
| 354 |
UInt16(1), // glyphs count |
| 355 |
UInt8(2), // value [0] |
| 356 |
]); |
| 357 |
|
| 358 |
let table = Lookup::parse(NonZeroU16::new(1).unwrap(), &data).unwrap(); |
| 359 |
assert!(table.value(GlyphId(0)).is_none()); |
| 360 |
} |
| 361 |
|
| 362 |
#[test] |
| 363 |
fn unsupported_value_size() { |
| 364 |
let data = convert(&[ |
| 365 |
UInt16(10), // format |
| 366 |
UInt16(8), // value size <-- we do not support u64 |
| 367 |
UInt16(0), // first glyph |
| 368 |
UInt16(1), // glyphs count |
| 369 |
Raw(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02]), // value [0] |
| 370 |
]); |
| 371 |
|
| 372 |
let table = Lookup::parse(NonZeroU16::new(1).unwrap(), &data).unwrap(); |
| 373 |
assert!(table.value(GlyphId(0)).is_none()); |
| 374 |
} |
| 375 |
|
| 376 |
#[test] |
| 377 |
fn u32_value_size() { |
| 378 |
let data = convert(&[ |
| 379 |
UInt16(10), // format |
| 380 |
UInt16(4), // value size |
| 381 |
UInt16(0), // first glyph |
| 382 |
UInt16(1), // glyphs count |
| 383 |
UInt32(0xFFFF + 10), // value [0] <-- will be truncated |
| 384 |
]); |
| 385 |
|
| 386 |
let table = Lookup::parse(NonZeroU16::new(1).unwrap(), &data).unwrap(); |
| 387 |
assert_eq!(table.value(GlyphId(0)).unwrap(), 9); |
| 388 |
} |
| 389 |
} |