Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
EriKWDev opened this issue Jul 26, 2023 · 0 comments
Open

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

EriKWDev opened this issue Jul 26, 2023 · 0 comments

Comments

@EriKWDev
Copy link

EriKWDev commented Jul 26, 2023

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 EriKWDev changed the title Max width of laid out text? Bounding box of laid out text for easy shiftnig of the origin? Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant