| 1 |
use std::num::NonZeroU16; |
| 2 |
use ttf_parser::{GlyphId, RasterImageFormat}; |
| 3 |
use ttf_parser::sbix::Table; |
| 4 |
use crate::{convert, Unit::*}; |
| 5 |
|
| 6 |
#[test] |
| 7 |
fn single_glyph() { |
| 8 |
let data = convert(&[ |
| 9 |
UInt16(1), // version |
| 10 |
UInt16(0), // flags |
| 11 |
UInt32(1), // number of strikes |
| 12 |
UInt32(12), // strike offset [0] |
| 13 |
|
| 14 |
// Strike [0] |
| 15 |
UInt16(20), // pixels_per_em |
| 16 |
UInt16(72), // ppi |
| 17 |
UInt32(12), // glyph data offset [0] |
| 18 |
UInt32(44), // glyph data offset [1] |
| 19 |
|
| 20 |
// Glyph Data [0] |
| 21 |
UInt16(1), // x |
| 22 |
UInt16(2), // y |
| 23 |
Raw(b"png "), // type tag |
| 24 |
// PNG data, just the part we need |
| 25 |
Raw(&[0x89, 0x50, 0x4E, 0x47]), |
| 26 |
Raw(&[0x0D, 0x0A, 0x1A, 0x0A]), |
| 27 |
Raw(&[0x00, 0x00, 0x00, 0x0D]), |
| 28 |
Raw(&[0x49, 0x48, 0x44, 0x52]), |
| 29 |
UInt32(20), // width |
| 30 |
UInt32(30), // height |
| 31 |
]); |
| 32 |
|
| 33 |
let table = Table::parse(NonZeroU16::new(1).unwrap(), &data).unwrap(); |
| 34 |
assert_eq!(table.strikes.len(), 1); |
| 35 |
|
| 36 |
let strike = table.strikes.get(0).unwrap(); |
| 37 |
assert_eq!(strike.pixels_per_em, 20); |
| 38 |
assert_eq!(strike.ppi, 72); |
| 39 |
assert_eq!(strike.len(), 1); |
| 40 |
|
| 41 |
let glyph_data = strike.get(GlyphId(0)).unwrap(); |
| 42 |
assert_eq!(glyph_data.x, 1); |
| 43 |
assert_eq!(glyph_data.y, 2); |
| 44 |
assert_eq!(glyph_data.width, 20); |
| 45 |
assert_eq!(glyph_data.height, 30); |
| 46 |
assert_eq!(glyph_data.pixels_per_em, 20); |
| 47 |
assert_eq!(glyph_data.format, RasterImageFormat::PNG); |
| 48 |
assert_eq!(glyph_data.data.len(), 24); |
| 49 |
} |
| 50 |
|
| 51 |
#[test] |
| 52 |
fn duplicate_glyph() { |
| 53 |
let data = convert(&[ |
| 54 |
UInt16(1), // version |
| 55 |
UInt16(0), // flags |
| 56 |
UInt32(1), // number of strikes |
| 57 |
UInt32(12), // strike offset [0] |
| 58 |
|
| 59 |
// Strike [0] |
| 60 |
UInt16(20), // pixels_per_em |
| 61 |
UInt16(72), // ppi |
| 62 |
UInt32(16), // glyph data offset [0] |
| 63 |
UInt32(48), // glyph data offset [1] |
| 64 |
UInt32(58), // glyph data offset [2] |
| 65 |
|
| 66 |
// Glyph Data [0] |
| 67 |
UInt16(1), // x |
| 68 |
UInt16(2), // y |
| 69 |
Raw(b"png "), // type tag |
| 70 |
// PNG data, just the part we need |
| 71 |
Raw(&[0x89, 0x50, 0x4E, 0x47]), |
| 72 |
Raw(&[0x0D, 0x0A, 0x1A, 0x0A]), |
| 73 |
Raw(&[0x00, 0x00, 0x00, 0x0D]), |
| 74 |
Raw(&[0x49, 0x48, 0x44, 0x52]), |
| 75 |
UInt32(20), // width |
| 76 |
UInt32(30), // height |
| 77 |
|
| 78 |
// Glyph Data [1] |
| 79 |
UInt16(3), // x |
| 80 |
UInt16(4), // y |
| 81 |
Raw(b"dupe"), // type tag |
| 82 |
UInt16(0), // glyph id |
| 83 |
]); |
| 84 |
|
| 85 |
let table = Table::parse(NonZeroU16::new(2).unwrap(), &data).unwrap(); |
| 86 |
assert_eq!(table.strikes.len(), 1); |
| 87 |
|
| 88 |
let strike = table.strikes.get(0).unwrap(); |
| 89 |
assert_eq!(strike.pixels_per_em, 20); |
| 90 |
assert_eq!(strike.ppi, 72); |
| 91 |
assert_eq!(strike.len(), 2); |
| 92 |
|
| 93 |
let glyph_data = strike.get(GlyphId(1)).unwrap(); |
| 94 |
assert_eq!(glyph_data.x, 1); |
| 95 |
assert_eq!(glyph_data.y, 2); |
| 96 |
assert_eq!(glyph_data.width, 20); |
| 97 |
assert_eq!(glyph_data.height, 30); |
| 98 |
assert_eq!(glyph_data.pixels_per_em, 20); |
| 99 |
assert_eq!(glyph_data.format, RasterImageFormat::PNG); |
| 100 |
assert_eq!(glyph_data.data.len(), 24); |
| 101 |
} |
| 102 |
|
| 103 |
#[test] |
| 104 |
fn recursive() { |
| 105 |
let data = convert(&[ |
| 106 |
UInt16(1), // version |
| 107 |
UInt16(0), // flags |
| 108 |
UInt32(1), // number of strikes |
| 109 |
UInt32(12), // strike offset [0] |
| 110 |
|
| 111 |
// Strike [0] |
| 112 |
UInt16(20), // pixels_per_em |
| 113 |
UInt16(72), // ppi |
| 114 |
UInt32(16), // glyph data offset [0] |
| 115 |
UInt32(26), // glyph data offset [1] |
| 116 |
UInt32(36), // glyph data offset [2] |
| 117 |
|
| 118 |
// Glyph Data [0] |
| 119 |
UInt16(1), // x |
| 120 |
UInt16(2), // y |
| 121 |
Raw(b"dupe"), // type tag |
| 122 |
UInt16(0), // glyph id |
| 123 |
|
| 124 |
// Glyph Data [1] |
| 125 |
UInt16(1), // x |
| 126 |
UInt16(2), // y |
| 127 |
Raw(b"dupe"), // type tag |
| 128 |
UInt16(0), // glyph id |
| 129 |
]); |
| 130 |
|
| 131 |
let table = Table::parse(NonZeroU16::new(2).unwrap(), &data).unwrap(); |
| 132 |
let strike = table.strikes.get(0).unwrap(); |
| 133 |
assert!(strike.get(GlyphId(0)).is_none()); |
| 134 |
assert!(strike.get(GlyphId(1)).is_none()); |
| 135 |
} |