#![allow(clippy::cast_possible_wrap)]
#![allow(clippy::cast_possible_truncation)]
use crate::error::ParseResult;
use crate::reader::{BinaryReader, Parse};
#[derive(Debug, Default)]
pub struct PostTable {
pub is_monospaced: bool,
pub glyph_names: Vec<String>,
}
impl PostTable {
#[must_use]
pub fn get_glyph_name(&self, index: u16) -> Option<&str> {
self.glyph_names.get(index as usize).map(String::as_str)
}
}
impl Parse for PostTable {
fn parse(reader: &mut BinaryReader) -> ParseResult<Self> {
let mut table = Self::default();
let fmt = reader.read_fixed32()?;
reader.skip_u32()?; reader.skip_u16()?; reader.skip_u16()?; table.is_monospaced = reader.read_u32()? != 0; reader.skip_u32()?; reader.skip_u32()?; reader.skip_u32()?; reader.skip_u32()?; debug_msg!(" Format = {}.{}", fmt.0, fmt.1);
match fmt {
(1, 0) => {
table.glyph_names = POST_MAC_NAMES.iter().map(ToString::to_string).collect();
}
(2, 0) => {
let num_glyphs = reader.read_u16()?;
debug_msg!(" Num glyphs = {}", num_glyphs);
let mut names = Vec::with_capacity(num_glyphs as usize);
let mut name_reader = reader.clone();
name_reader.advance_by(num_glyphs as isize * 2)?;
while !name_reader.is_eof() {
let len = name_reader.read_u8()?;
let name = name_reader.read_string(len as usize)?;
names.push(name);
}
for _ in 0..num_glyphs {
let ordinal = reader.read_u16()?;
if ordinal < POST_MAC_NAMES_LEN as u16 {
table
.glyph_names
.push(POST_MAC_NAMES[ordinal as usize].to_string());
} else {
let index = (ordinal - POST_MAC_NAMES_LEN as u16) as usize;
table.glyph_names.push(names[index].clone());
}
}
}
(2, 5) => {
let num_glyphs = reader.read_u16()?;
let mut glyph_names = Vec::new();
for i in 0..num_glyphs {
let offset = reader.read_i8()?;
let index = i.wrapping_add_signed(i16::from(offset));
glyph_names.push(POST_MAC_NAMES[index as usize].to_string());
}
}
_ => {
}
}
debug_msg!(" Found {} glyph names", table.glyph_names.len());
Ok(table)
}
}
const POST_MAC_NAMES_LEN: usize = 258;
#[rustfmt::skip]
const POST_MAC_NAMES: [&str; POST_MAC_NAMES_LEN] = [
".notdef", ".null", "nonmarkingreturn", "space", "exclam", "quotedbl", "numbersign", "dollar", "percent",
"ampersand", "quotesingle","parenleft","parenright","asterisk","plus","comma", "hyphen", "period", "slash",
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "colon", "semicolon", "less",
"equal", "greater", "question", "at", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "bracketleft", "backslash", "bracketright", "asciicircum",
"underscore", "grave", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z", "braceleft", "bar", "braceright", "asciitilde", "Adieresis", "Aring",
"Ccedilla", "Eacute", "Ntilde", "Odieresis", "Udieresis", "aacute", "agrave", "acircumflex", "adieresis",
"atilde", "aring", "ccedilla", "eacute", "egrave", "ecircumflex", "edieresis", "iacute", "igrave",
"icircumflex", "idieresis", "ntilde", "oacute", "ograve", "ocircumflex", "odieresis", "otilde", "uacute", "ugrave",
"ucircumflex", "udieresis", "dagger", "degree", "cent", "sterling", "section", "bullet", "paragraph", "germandbls",
"registered", "copyright", "trademark", "acute", "dieresis", "notequal", "AE", "Oslash", "infinity", "plusminus",
"lessequal", "greaterequal", "yen", "mu", "partialdiff", "summation", "product", "pi", "integral", "ordfeminine",
"ordmasculine", "Omega", "ae", "oslash", "questiondown", "exclamdown", "logicalnot", "radical", "florin", "approxequal",
"Delta", "guillemotleft", "guillemotright", "ellipsis", "nonbreakingspace", "Agrave", "Atilde", "Otilde", "OE", "oe",
"endash", "emdash", "quotedblleft", "quotedblright", "quoteleft", "quoteright", "divide", "lozenge", "ydieresis",
"Ydieresis", "fraction", "currency", "guilsinglleft", "guilsinglright", "fi", "fl", "daggerdbl", "periodcentered",
"quotesinglbase", "quotedblbase", "perthousand", "Acircumflex", "Ecircumflex", "Aacute", "Edieresis", "Egrave",
"Iacute", "Icircumflex", "Idieresis", "Igrave", "Oacute", "Ocircumflex", "apple", "Ograve", "Uacute", "Ucircumflex",
"Ugrave", "dotlessi", "circumflex", "tilde", "macron", "breve", "dotaccent", "ring", "cedilla", "hungarumlaut",
"ogonek", "caron", "Lslash", "lslash", "Scaron", "scaron", "Zcaron", "zcaron", "brokenbar", "Eth", "eth", "Yacute",
"yacute", "Thorn", "thorn", "minus", "multiply", "onesuperior", "twosuperior", "threesuperior", "onehalf", "onequarter",
"threequarters", "franc", "Gbreve", "gbreve", "Idotaccent", "Scedilla", "scedilla", "Cacute", "cacute", "Ccaron",
"ccaron", "dcroat"
];