fontdue icon indicating copy to clipboard operation
fontdue copied to clipboard

Bounding box of laid out text for easy shiftnig of the origin?

Open EriKWDev opened this issue 11 months ago • 0 comments

I'm trying to shift the origin of the laid out text for which I need the total width and height.

let (xw, yw) = (0.5, 0.5);
let offset_x = -total_width * xw;
let offset_y = total_height * yw;

let glyphs = layout.glyphs();
layout.lines().unwrap().for_each(|line| {
    for i in line.glyph_start..=line.glyph_end {
        let g = &glyphs[i];
        // TODO: Shift glyph position by `offset_x` and `offset_y` 
    }
});

I found Layout::height which seems to be what I need for total_height for calculating offset_y up-front, but I've found no equivalent Layout::width?

I found the private tracking_x which seems to have been useful here maybe since it's used for the horizontal alignment in the finalize stage..

Is there something I've missed in the API or do I have to calculate the total_width manually like I do currently which seems to work:

let total_height = layout.height();
let total_width = {
    let mut max_width: f32 = f32::MIN;
    lines.iter().for_each(|line| {
        let gs = &glyphs[line.glyph_start];
        let ge = &glyphs[line.glyph_end];

        let min = gs.x;
        let max = ge.x + ge.width as f32;

        max_width = max_width.max(max - min + line.padding);
    });
    max_width
};

If I'm totally wrong here, how would you calculate the bounding box of the laid out text?


Thanks btw for making this crate as dependency free and self-standing as possible. I really appreciate that kind of effort :)

EriKWDev avatar Jul 26 '23 09:07 EriKWDev