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

[Renderers/Raylib] Odin measure text function for Raylib doesn't support variable font size #308

Open
tralph3 opened this issue Mar 4, 2025 · 5 comments

Comments

@tralph3
Copy link

tralph3 commented Mar 4, 2025

I found the measure text function provided in the example for the raylib renderer in the odin bindings to give wrong results. Here's how it looks like:

Image

Luckily, raylib has its own function to measure text, and it works perfectly.

This is the revised function:

measureText :: proc "c" (text: StringSlice, config: ^TextElementConfig, userData: rawptr) -> Dimensions {
    context = runtime.default_context()

    cloned := strings.clone_to_cstring(string(text.chars[:text.length]), context.temp_allocator)
    result := raylib.MeasureTextEx(
        raylibFonts[config.fontId].font,
        cloned,
        f32(config.fontSize),
        f32(config.letterSpacing))

    return {
        width = result.x,
        height = result.y
    }
}

And this is the result:

Image

@nicbarker
Copy link
Owner

Hello, thanks for opening this issue. Just for me to understand a little better, are you loading your raylib font (with LoadFont or LoadFontEx) at a different font size to the size you're using to render it?

@nicbarker nicbarker changed the title Odin measure text function for Raylib gives wrongs results [Renderers/Raylib] Odin measure text function for Raylib gives wrongs results Mar 4, 2025
@tralph3
Copy link
Author

tralph3 commented Mar 4, 2025

Hi. Yes, I'm loading it with fontSize = 24, but rendering the title at size 20, and the subtitle at size 14.

I guess this means the proper way to do it is to load a separate font for every size I'd need. While certainly doable once I have a more defined style, in these prototyping phases, it's easier for me to just change the size.

Plus, what's the problem with using raylib's internal font measuring, if we're using raylib for rendering anyway? If it gives better results... then we might as well.

@nicbarker
Copy link
Owner

@tralph3 Yep you're totally right, it's a problem that our shim text measurement function doesn't support rendering at a different size from the loaded one. Will get that fixed up 🙂
Just for context, the reason that we provide an alternative to the MeasureTextEx function is that it does a linear scan through all the loaded glyphs for every character, which can be very slow for long text and large glyph counts.

@tralph3
Copy link
Author

tralph3 commented Mar 4, 2025

Ah... I wasn't aware of that. Well thankfully my program isn't too text intensive, but I'll take it into account. Thanks!

@nicbarker nicbarker changed the title [Renderers/Raylib] Odin measure text function for Raylib gives wrongs results [Renderers/Raylib] Odin measure text function for Raylib doesn't support variable font size Mar 5, 2025
@johan0A
Copy link
Contributor

johan0A commented Mar 6, 2025

I thought the rewritten MeasureTextEx function was because MeasureTextEx only supports null terminated strings. For my zig bindings I just rewrote MeasureTextEx directly. https://github.com/johan0A/clay-zig-bindings/blob/c26ef9d4b6b591caeb1750869abcdc50446b33c1/examples/clay-official-website/src/raylib_render_clay.zig#L195

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

3 participants