use std::num::NonZeroU16;
use std::sync::Arc;
use crate::text::face::Face;
use crate::text::glyph::{FontData, GlyphID, GlyphIDType};
use crate::text::woff::{self, WoffError};
use crate::text::{GlyphAdjust, ShapedText, Shaper};
const TTF: &[u8] = include_bytes!("../../../tests/fixtures/khmer.ttf");
const WOFF: &[u8] = include_bytes!("../../../tests/fixtures/khmer.woff");
const WOFF2: &[u8] = include_bytes!("../../../tests/fixtures/khmer.woff2");
const KHMER: &str = "ខ្មែរ";
fn shape(font: &[u8]) -> (Vec<GlyphID>, Vec<GlyphAdjust>, Vec<crate::text::glyph::Glyph>) {
let type_ = GlyphIDType::face(NonZeroU16::MIN);
let shaper = Shaper::new(type_, font.to_vec());
assert!(shaper.valid());
let text: Vec<char> = KHMER.chars().collect();
let mut shaped = ShapedText::default();
shaper.shape(&text, false, &mut shaped);
let (ids, adjusts) = (shaped.glyphs, shaped.adjusts);
let glyphs = ids.iter().map(|&id| shaper.rasterize_glyph(id)).collect();
(ids, adjusts, glyphs)
}
#[test]
fn woff_and_woff2_shape_and_rasterize_as_the_ttf() {
let ttf = shape(TTF);
assert_ne!(ttf.0.len(), KHMER.chars().count());
assert!(ttf.2.iter().any(|glyph| glyph.bitmap.size.width > 0));
assert_eq!(shape(WOFF), ttf);
assert_eq!(shape(WOFF2), ttf);
}
fn tables(sfnt: &[u8]) -> Vec<([u8; 4], &[u8])> {
let u16_at = |at: usize| usize::from(u16::from_be_bytes([sfnt[at], sfnt[at + 1]]));
let u32_at = |at: usize| u32::from_be_bytes(sfnt[at..at + 4].try_into().unwrap()) as usize;
(0..u16_at(4))
.map(|i| {
let entry = 12 + i * 16;
let (offset, length) = (u32_at(entry + 8), u32_at(entry + 12));
(sfnt[entry..entry + 4].try_into().unwrap(), &sfnt[offset..offset + length])
})
.collect()
}
#[test]
fn woff_decodes_to_the_ttf_tables() {
let sfnt = woff::decode_woff(WOFF).unwrap();
assert_eq!(sfnt[..4], TTF[..4]);
assert_eq!(tables(&sfnt), tables(TTF));
assert_eq!(sfnt.len() % 4, 0);
}
#[test]
fn woff2_decodes_to_a_font() {
let sfnt = woff::decode_woff2(WOFF2).unwrap();
let glyph = |face: Face| face.font().and_then(|font| font.charmap().map_unicode('ខ'));
let decoded = glyph(Face::new(sfnt, crate::text::weight::REGULAR_WEIGHT));
assert!(decoded.is_some());
assert_eq!(decoded, glyph(Face::new(TTF.to_vec(), crate::text::weight::REGULAR_WEIGHT)));
}
#[test]
fn to_sfnt_keeps_other_data_uncopied() {
let data = FontData::from(Arc::new(TTF.to_vec()));
let sfnt = woff::to_sfnt(data.clone()).unwrap();
assert!(std::ptr::eq(sfnt.as_bytes(), data.as_bytes()));
let garbage = FontData::from(b"not a font".to_vec());
assert_eq!(woff::to_sfnt(garbage.clone()).unwrap(), garbage);
assert!(woff::is_woff(WOFF) && woff::is_woff(WOFF2) && !woff::is_woff(TTF));
}
#[test]
fn face_opens_woff_and_woff2() {
for font in [WOFF, WOFF2] {
let face = Face::new(font.to_vec(), crate::text::weight::REGULAR_WEIGHT);
let Some(skrifa::font::Source::Blob(data)) = face.font().map(skrifa::Font::source) else {
panic!("a valid face reads the font's bytes");
};
assert!(!woff::is_woff(data));
}
}
fn set_u32(data: &mut [u8], at: usize, value: u32) {
data[at..at + 4].copy_from_slice(&value.to_be_bytes());
}
#[test]
fn malformed_woff_is_an_error() {
let mut wrong_length = WOFF.to_vec();
wrong_length.push(0);
assert_eq!(woff::decode_woff(&wrong_length), Err(WoffError::InvalidWoff));
let mut huge = WOFF.to_vec();
set_u32(&mut huge, 44 + 12, 0x7FFF_FFF0);
set_u32(&mut huge, 16, 0xFFFF_FFFC);
assert_eq!(woff::decode_woff(&huge), Err(WoffError::InvalidWoff));
let mut corrupt = WOFF.to_vec();
let directory = 44;
let compressed = (0..usize::from(u16::from_be_bytes([WOFF[12], WOFF[13]])))
.map(|i| directory + i * 20)
.find(|&entry| WOFF[entry + 8..entry + 12] != WOFF[entry + 12..entry + 16])
.unwrap();
let offset = u32::from_be_bytes(WOFF[compressed + 4..compressed + 8].try_into().unwrap()) as usize;
corrupt[offset] ^= 0xFF;
assert_eq!(woff::decode_woff(&corrupt), Err(WoffError::InvalidWoff));
let mut repeated = WOFF.to_vec();
for entry in (0..usize::from(u16::from_be_bytes([WOFF[12], WOFF[13]]))).map(|i| directory + i * 20) {
repeated[entry + 4..entry + 16].copy_from_slice(&WOFF[compressed + 4..compressed + 16]);
}
assert_eq!(woff::decode_woff(&repeated), Err(WoffError::InvalidWoff));
assert_eq!(woff::decode_woff(TTF), Err(WoffError::InvalidWoff));
assert_eq!(woff::decode_woff2(b"wOF2"), Err(WoffError::InvalidWoff2));
assert_eq!(woff::to_sfnt(FontData::from(b"wOFF".to_vec())), Err(WoffError::InvalidWoff));
assert!(!Face::new(corrupt, crate::text::weight::REGULAR_WEIGHT).is_valid());
}
type Decoder = fn(&[u8]) -> Result<Vec<u8>, WoffError>;
#[test]
fn damaged_files_never_panic() {
let decoders: [(&[u8], Decoder); 2] = [(WOFF, woff::decode_woff), (WOFF2, woff::decode_woff2)];
for (font, decode) in decoders {
for len in 0..font.len() {
assert!(decode(&font[..len]).is_err(), "truncated to {len}");
}
for at in 0..font.len() {
for flip in [0x01, 0x80, 0xFF] {
let mut damaged = font.to_vec();
damaged[at] ^= flip;
if let Ok(sfnt) = woff::to_sfnt(FontData::from(damaged)) {
let _ = Face::new(sfnt, crate::text::weight::REGULAR_WEIGHT);
}
}
}
}
}