openglrustfreetypefreetype2

Advance x is incorrect for multiple characters Freetype/OpenGL?


This is what I got when I render text using opengl (mine is on the right)

enter image description here

The letter "j" is quite close to the letter "k"

Here's how I measure the sizes and positions of the characters

fn main() {
    let library = ft::Library::init().unwrap();
    let face = library.new_face(font, 0).unwrap();

    face.set_pixel_sizes(0, 50);

    let string = String::from("jkl");

    let mut font_measure: HashMap<char, FontSize> = HashMap::new();

    for c in string.chars() {
        face.load_char(c as usize, ft::face::LoadFlag::DEFAULT)
            .unwrap();

        let glyph = face.glyph();
        let metrics = glyph.metrics();
        let width = metrics.width >> 6;
        let bbox_ymax = face.raw().bbox.yMax >> 6;
        let face_height = (face.raw().height >> 6) as f64;

        font_measure.insert(
            c,
            FontSize {
                width: metrics.width >> 6,
                height: metrics.height >> 6,
                advance: glyph.advance().x + width / 2 >> 6,
                y_offset: (bbox_ymax - metrics.horiBearingY >> 6) as f64 + face_height,
            },
        );
    }
}
fn main() {
    let mut pen_x = 0.0;
    let mut pen_y = 0.0;
    for character in line.text.chars() {
        let character_props = &k.get(&character).unwrap();
        
        // ....
        // vertex_buffer.splice(start..end, character_vertices.iter().cloned());

        // move drawing position in the x for advance.x
        pen_x += character_props.advance;
    }
}

Solution

  • Solved. I wasn't taking the horizontal bearing into consideration