image-rs / imageproc

Image processing operations
MIT License
758 stars 149 forks source link

Text rendered at wrong Y position #687

Open SpriteOvO opened 2 months ago

SpriteOvO commented 2 months ago

Minimal reproducible

use ab_glyph::FontRef;
use imageproc::{
    drawing::*,
    image::{Rgb, RgbImage},
    rect::Rect,
};

fn main() {
    let mut image = RgbImage::new(200, 200);

    let font = FontRef::try_from_slice(include_bytes!("../DejaVuSans.ttf")).unwrap();
    let white = Rgb([255, 255, 255]);

    let scale = 64.;
    let text = "hello";
    let (w, h) = text_size(scale, &font, text);

    draw_text_mut(&mut image, white, 0, 0, scale, &font, text);
    draw_hollow_rect_mut(&mut image, Rect::at(0, 0).of_size(w, h), white);

    image.save("./output.png").unwrap();
}

Rendered

image

This issue would be more obvious if we used other fonts or languages.

For example, NotoSansCJK-Regular.ttc

image image

cospectrum commented 1 month ago

I think It's not the wrong Y position, it's the wrong text_size

cospectrum commented 1 month ago

Thats probably glyph "bearing" at the top. But text_size is broken for sure!