Skip to content

Commit

Permalink
fix(pff2): Changed codepoint from char to u32
Browse files Browse the repository at this point in the history
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
  • Loading branch information
max-ishere committed Feb 7, 2024
1 parent 5acf0d7 commit 87b14ea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
5 changes: 2 additions & 3 deletions examples/pff2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
Expand All @@ -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(())
Expand Down
19 changes: 6 additions & 13 deletions src/parser/pff2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct Pff2<T: FontValidation> {
#[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,
Expand Down Expand Up @@ -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(),
})
})
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -307,7 +300,7 @@ impl Parser {
}

impl<T: FontValidation> Pff2<T> {
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])
Expand Down Expand Up @@ -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,
}
Expand Down

0 comments on commit 87b14ea

Please sign in to comment.