| 1 |
use std::num::NonZeroU16; |
| 2 |
use ttf_parser::GlyphId; |
| 3 |
use ttf_parser::hmtx::Table; |
| 4 |
use crate::{convert, Unit::*}; |
| 5 |
|
| 6 |
macro_rules! nzu16 { |
| 7 |
($n:expr) => { NonZeroU16::new($n).unwrap() }; |
| 8 |
} |
| 9 |
|
| 10 |
#[test] |
| 11 |
fn simple_case() { |
| 12 |
let data = convert(&[ |
| 13 |
UInt16(1), // advance width [0] |
| 14 |
Int16(2), // side bearing [0] |
| 15 |
]); |
| 16 |
|
| 17 |
let table = Table::parse(1, nzu16!(1), &data).unwrap(); |
| 18 |
assert_eq!(table.advance(GlyphId(0)), Some(1)); |
| 19 |
assert_eq!(table.side_bearing(GlyphId(0)), Some(2)); |
| 20 |
} |
| 21 |
|
| 22 |
#[test] |
| 23 |
fn empty() { |
| 24 |
assert!(Table::parse(1, nzu16!(1), &[]).is_none()); |
| 25 |
} |
| 26 |
|
| 27 |
#[test] |
| 28 |
fn zero_metrics() { |
| 29 |
let data = convert(&[ |
| 30 |
UInt16(1), // advance width [0] |
| 31 |
Int16(2), // side bearing [0] |
| 32 |
]); |
| 33 |
|
| 34 |
assert!(Table::parse(0, nzu16!(1), &data).is_none()); |
| 35 |
} |
| 36 |
|
| 37 |
#[test] |
| 38 |
fn smaller_than_glyphs_count() { |
| 39 |
let data = convert(&[ |
| 40 |
UInt16(1), // advance width [0] |
| 41 |
Int16(2), // side bearing [0] |
| 42 |
|
| 43 |
Int16(3), // side bearing [1] |
| 44 |
]); |
| 45 |
|
| 46 |
let table = Table::parse(1, nzu16!(2), &data).unwrap(); |
| 47 |
assert_eq!(table.advance(GlyphId(0)), Some(1)); |
| 48 |
assert_eq!(table.side_bearing(GlyphId(0)), Some(2)); |
| 49 |
assert_eq!(table.advance(GlyphId(1)), Some(1)); |
| 50 |
assert_eq!(table.side_bearing(GlyphId(1)), Some(3)); |
| 51 |
} |
| 52 |
|
| 53 |
#[test] |
| 54 |
fn no_additional_side_bearings() { |
| 55 |
let data = convert(&[ |
| 56 |
UInt16(1), // advance width [0] |
| 57 |
Int16(2), // side bearing [0] |
| 58 |
|
| 59 |
// A single side bearing should be present here. |
| 60 |
// We should simply ignore it and not return None during Table parsing. |
| 61 |
]); |
| 62 |
|
| 63 |
let table = Table::parse(1, nzu16!(2), &data).unwrap(); |
| 64 |
assert_eq!(table.advance(GlyphId(0)), Some(1)); |
| 65 |
assert_eq!(table.side_bearing(GlyphId(0)), Some(2)); |
| 66 |
} |
| 67 |
|
| 68 |
#[test] |
| 69 |
fn less_metrics_than_glyphs() { |
| 70 |
let data = convert(&[ |
| 71 |
UInt16(1), // advance width [0] |
| 72 |
Int16(2), // side bearing [0] |
| 73 |
|
| 74 |
UInt16(3), // advance width [1] |
| 75 |
Int16(4), // side bearing [1] |
| 76 |
|
| 77 |
Int16(5), // side bearing [2] |
| 78 |
]); |
| 79 |
|
| 80 |
let table = Table::parse(2, nzu16!(1), &data).unwrap(); |
| 81 |
assert_eq!(table.side_bearing(GlyphId(0)), Some(2)); |
| 82 |
assert_eq!(table.side_bearing(GlyphId(1)), Some(4)); |
| 83 |
assert_eq!(table.side_bearing(GlyphId(2)), None); |
| 84 |
} |
| 85 |
|
| 86 |
#[test] |
| 87 |
fn glyph_out_of_bounds_0() { |
| 88 |
let data = convert(&[ |
| 89 |
UInt16(1), // advance width [0] |
| 90 |
Int16(2), // side bearing [0] |
| 91 |
]); |
| 92 |
|
| 93 |
let table = Table::parse(1, nzu16!(1), &data).unwrap(); |
| 94 |
assert_eq!(table.advance(GlyphId(0)), Some(1)); |
| 95 |
assert_eq!(table.side_bearing(GlyphId(0)), Some(2)); |
| 96 |
assert_eq!(table.advance(GlyphId(1)), None); |
| 97 |
assert_eq!(table.side_bearing(GlyphId(1)), None); |
| 98 |
} |
| 99 |
|
| 100 |
#[test] |
| 101 |
fn glyph_out_of_bounds_1() { |
| 102 |
let data = convert(&[ |
| 103 |
UInt16(1), // advance width [0] |
| 104 |
Int16(2), // side bearing [0] |
| 105 |
|
| 106 |
Int16(3), // side bearing [1] |
| 107 |
]); |
| 108 |
|
| 109 |
let table = Table::parse(1, nzu16!(2), &data).unwrap(); |
| 110 |
assert_eq!(table.advance(GlyphId(1)), Some(1)); |
| 111 |
assert_eq!(table.side_bearing(GlyphId(1)), Some(3)); |
| 112 |
assert_eq!(table.advance(GlyphId(2)), None); |
| 113 |
assert_eq!(table.side_bearing(GlyphId(2)), None); |
| 114 |
} |