mooman219 / fontdue

The fastest font renderer in the world, written in pure rust.
Apache License 2.0
1.44k stars 71 forks source link

Unable to write a font bitmap to a pixel buffer #80

Closed snakedye closed 3 years ago

snakedye commented 3 years ago

I noticed that the format of the pixmap doesn't really seem to match what I usually expect. There seems to be no pixel since dimensions of the pixmap are not always multiples of 4.

I am probably missing something. Am I using fontdue correctly?

        let mut index = ((x + (y * width as u32)) * 4) as usize;
        let font = include_bytes!("/home/bryan/.local/share/fonts/TerminusTTF-Bold.ttf") as &[u8];
        // Parse it into the font type.
        let font = fontdue::Font::from_bytes(font, fontdue::FontSettings::default()).unwrap();
        for c in self.text.chars() {
            let (metrics, bitmap) = font.rasterize(c, self.font_size);
            for i in (0..metrics.width * metrics.height).into_iter().step_by(metrics.width) {
                if index >= canvas.len() {
                    break;
                } else {
                    let mut writer = &mut canvas[index..];
                    writer.write(&bitmap[i..i+metrics.width]).unwrap();
                    writer.flush().unwrap();
                    index += width as usize * 4;
                }
            }
        }

This is supposed to be the letter 'm'. image

snakedye commented 3 years ago

I was just misinterpreting the bitmap.