diff --git a/examples/pff2.rs b/examples/pff2.rs index c3c66c6..3a770ee 100644 --- a/examples/pff2.rs +++ b/examples/pff2.rs @@ -11,7 +11,7 @@ struct Args { pub font_file: PathBuf, #[clap(long = "char", short)] - pub character: char, + pub codepoint: u32, } const SQUARE_BLOCK: &str = "\u{2588}\u{2588}"; @@ -24,8 +24,7 @@ fn main() -> anyhow::Result<()> { println!("{}", font.name); - let glyph = font.glyph(args.character).unwrap(); - + let glyph = font.glyph(args.codepoint).unwrap(); render_glyph(glyph); Ok(()) diff --git a/src/parser/pff2.rs b/src/parser/pff2.rs index 5fc8ccc..3a345d1 100644 --- a/src/parser/pff2.rs +++ b/src/parser/pff2.rs @@ -63,7 +63,7 @@ pub struct Pff2 { #[derive(Debug, Clone, PartialEq, Eq, Default)] pub struct Glyph { /// The UTF codepoint of the character - pub code: char, + pub code: u32, pub x_offset: isize, pub y_offset: isize, @@ -199,9 +199,8 @@ impl Parser { ); } - // TODO: Investigate why unicode.pf2 contains invalid codepoint 0x40000626 and deal with it. Ok::<_, ParserError>(CharIndex { - code: char::from_u32(codepoint).ok_or(ParserError::InvalidCodepoint(codepoint))?, + code: codepoint, offset: u32::from_be_bytes([chunk[5], chunk[6], chunk[7], chunk[8]]).to_usize(), }) }) @@ -218,10 +217,7 @@ impl Parser { // make sure there are enough bytes to read the bitmap dimentions if offset + 4 > input.len() { - warn!( - "Insufficient data to load a glyph for codepoint {}", - index.code.escape_unicode(), - ); + warn!("Insufficient data to load a glyph for codepoint {:x}", index.code,); continue; } @@ -232,10 +228,7 @@ impl Parser { // make sure there are enough bytes to read the bitmap and the rest of the fields if offset + 10 + bitmap_len > input.len() { - warn!( - "Insufficient data to load a glyph for codepoint {}", - index.code.escape_unicode() - ); + warn!("Insufficient data to load a glyph for codepoint {:x}", index.code); continue; } @@ -307,7 +300,7 @@ impl Parser { } impl Pff2 { - pub fn glyph(&self, c: char) -> Option<&Glyph> { + pub fn glyph(&self, c: u32) -> Option<&Glyph> { self.glyphs .binary_search_by(|g| (g.code as u32).cmp(&(c as u32))) .map(|i| &self.glyphs[i]) @@ -372,7 +365,7 @@ enum Section { /// An intermediate structure used for reading glyphs from a font file. This is discarded after the glyphs are read. struct CharIndex { /// The UCS-4 codepoint - pub code: char, + pub code: u32, /// A file-level (absolute) offset to the glyph data pub offset: usize, }