use std::collections::BTreeMap;
use f_office_fonts::{SymbolFont, plain_device_width};
use super::{SymbolFonts, SymbolWidths, character};
use crate::profile::Profile;
use crate::profile::fonts::{SymbolFileMetrics, SymbolFontEntry};
fn entry() -> SymbolFontEntry {
SymbolFontEntry {
postscript_name: Some("Face-Regular".to_owned()),
version: Some("Version 1.00".to_owned()),
units_per_em: Some(2048),
measured_sizes: Some(800),
heights: vec![[8, 7, -2], [9, 8, -2]],
metrics: Some(SymbolFileMetrics {
hhea: [1800, -400, 0],
win: [1800, 400],
panose: [5, 0, 0, 0, 0, 0, 0, 0, 0, 0],
..SymbolFileMetrics::default()
}),
advance: [("F020".to_owned(), 512), ("F041".to_owned(), 1479)].into(),
written: [("F041".to_owned(), 700)].into(),
wider: [("F041".to_owned(), vec![57, 900])].into(),
narrower: [("F041".to_owned(), vec![58])].into(),
}
}
#[test]
fn takes_a_code_by_its_private_use_form_or_its_byte_when_the_file_maps_it() {
let widths = SymbolWidths::of(&entry()).unwrap();
assert_eq!(widths.code('\u{F041}'), Some(0x41));
assert_eq!(widths.code('A'), Some(0x41));
assert_eq!(widths.code(' '), Some(0x20));
for ch in [
'B', '\u{F042}', '\u{F010}', '\u{10}', '\u{2022}', '\u{F100}',
] {
assert_eq!(widths.code(ch), None, "{ch:?}");
}
assert_eq!(widths.advance(0x41), Some(1479));
assert_eq!(widths.advance(0x42), None);
}
#[test]
fn keeps_the_widths_of_the_missing_glyph_at_glyph_0_which_no_character_takes() {
let missing = SymbolFontEntry::MISSING_GLYPH.to_owned();
let mut entry = entry();
entry.advance.insert(missing.clone(), 1229);
entry.narrower.insert(missing, vec![57]);
let widths = SymbolWidths::of(&entry).unwrap();
let numbers = (
widths.advance(0),
widths.device_width(0, 57),
widths.written(0),
);
let expected = (
Some(1229),
Some(plain_device_width(1229, 2048, 57) - 1),
Some(600),
);
assert_eq!(numbers, expected);
assert_eq!((widths.code('\u{0}'), widths.code('B')), (None, None));
}
#[test]
fn gives_the_plain_device_width_except_at_the_measured_sizes_of_a_code() {
let widths = SymbolWidths::of(&entry()).unwrap();
let plain = |ppem| plain_device_width(1479, 2048, ppem);
assert_eq!(widths.device_width(0x41, 56), Some(plain(56)));
assert_eq!(widths.device_width(0x41, 57), Some(plain(57) + 1));
assert_eq!(widths.device_width(0x41, 58), Some(plain(58) - 1));
assert_eq!(widths.device_width(0x41, 900), Some(plain(900)));
assert_eq!(
widths.device_width(0x20, 57),
Some(plain_device_width(512, 2048, 57))
);
assert_eq!(widths.device_width(0x42, 57), None);
}
#[test]
fn writes_the_rounded_advance_unless_the_table_gives_another_width() {
let widths = SymbolWidths::of(&entry()).unwrap();
assert_eq!(widths.written(0x20), Some(250));
assert_eq!(widths.written(0x41), Some(700));
assert_eq!(widths.written(0x42), None);
}
#[test]
fn gives_device_heights_only_at_the_sizes_that_the_file_lists() {
let widths = SymbolWidths::of(&entry()).unwrap();
assert_eq!(widths.heights(8), Some((7, -2)));
assert_eq!(widths.heights(9), Some((8, -2)));
assert_eq!(widths.heights(10), None);
assert_eq!(widths.heights(7), None);
}
#[test]
fn takes_the_files_metrics_and_classification() {
let widths = SymbolWidths::of(&entry()).unwrap();
assert_eq!(widths.units_per_em(), 2048);
assert_eq!(widths.metrics.hhea.ascender, 1800);
let os2 = widths.metrics.os2.unwrap();
assert_eq!((os2.win_ascent, os2.win_descent), (1800, 400));
assert_eq!(widths.classification.panose[0], 5);
assert_eq!(
(widths.postscript_name.as_str(), widths.version.as_str()),
("Face-Regular", "Version 1.00")
);
}
fn refused(change: impl FnOnce(&mut SymbolFontEntry)) -> Option<&'static str> {
let mut entry = entry();
change(&mut entry);
SymbolWidths::of(&entry).err()
}
#[test]
fn refuses_an_entry_that_lacks_a_value() {
let errors = [
refused(|entry| entry.units_per_em = Some(0)),
refused(|entry| entry.metrics = None),
refused(|entry| entry.version = None),
];
assert_eq!(
errors,
[Some("units_per_em"), Some("metrics"), Some("version")]
);
}
#[test]
fn refuses_an_entry_that_holds_a_wrong_code_or_size() {
let codes = ["F01F", "F100", "0041", "G041", "0000"]
.map(|key| refused(|entry| entry.advance = [(key.to_owned(), 1)].into()));
assert_eq!(codes, [Some("advance"); 5]);
let errors = [
refused(|entry| entry.written = [("F042".to_owned(), 1)].into()),
refused(|entry| entry.wider = [("F041".to_owned(), vec![58, 57])].into()),
refused(|entry| entry.heights = vec![[9, 8, -2], [8, 7, -2]]),
];
assert_eq!(errors, [Some("written"), Some("wider"), Some("heights")]);
}
#[test]
fn finds_a_symbol_font_by_its_family_or_the_postscript_name_of_its_file() {
let fonts = SymbolFonts::of(&[("Wingdings 2".to_owned(), entry())].into()).unwrap();
let place = |name| fonts.named(name).map(|(at, _)| at);
let places = ["wingdings 2", "FACE-REGULAR", "Face", "Symbol"].map(place);
assert_eq!(places, [Some(0), Some(0), None, None]);
}
#[test]
fn reads_the_symbol_fonts_by_family_and_refuses_another_family() {
let fonts = SymbolFonts::of(&[("Wingdings 2".to_owned(), entry())].into()).unwrap();
assert_eq!(fonts.get(SymbolFont::Wingdings2).map(|(at, _)| at), Some(0));
assert!(fonts.get(SymbolFont::Symbol).is_none());
assert_eq!(
fonts.at(0).map(|(font, _)| font),
Some(SymbolFont::Wingdings2)
);
let other = BTreeMap::from([("Arial".to_owned(), entry())]);
assert_eq!(SymbolFonts::of(&other).err(), Some("symbol_font"));
}
#[test]
fn reads_the_widths_of_the_symbol_fonts_of_each_targets_environment() {
for target in crate::Target::all() {
let profile = Profile::of(target).unwrap();
let fonts = profile.symbol_fonts();
for font in SymbolFont::ALL {
let Some((_, widths)) = fonts.get(font) else {
continue;
};
let unmapped: Vec<u32> = (0x20..=0xFF)
.filter(|&code| widths.advance(code).is_some() && character(font, code).is_none())
.collect();
let expected: &[u32] = if font == SymbolFont::Wingdings {
&[0xFF]
} else {
&[]
};
assert_eq!(unmapped, expected, "{target:?} {font:?}");
}
}
}