From 87b14ea2858dda3b8121a02ee3c30ae8f7e0ffc5 Mon Sep 17 00:00:00 2001 From: max-ishere <47008271+max-ishere@users.noreply.github.com> Date: Wed, 7 Feb 2024 21:22:24 +0200 Subject: [PATCH] fix(pff2): Changed codepoint from char to u32 GRUB's default font (unicode.pf2) contains some invalid unicode codepoints that are likely used in some special way that is not yet implemented. Instead of discarding those codepoints, we simply keep them as u32 --- examples/pff2.rs | 5 ++--- src/parser/pff2.rs | 19 ++++++------------- 2 files changed, 8 insertions(+), 16 deletions(-) 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, }