Skip to content

TTF_GetTextSize gives incorrect height when text position y is not zero #626

Description

@mefesto

SDL3_ttf v3.2.2

While using TTF_SetTextPosition, I was noticing odd behavior in my app. After closer look it appears that TTF_GetTextSize function return their height with the TexPos.y coordinate added to it.

Below are two example outputs. The first has the text position at the origin (0, 0). The second places the text at (100, 100). I would expect the width and height of the text to remain the same since the actual content didn't change (only its position).

    TTF_Font *font = TTF_OpenFont("assets/fonts/mono.ttf", 24);
    TTF_Text *text = TTF_CreateText(engine, font, "hello world", 0);

    // example 1
    {
        TTF_SetTextPosition(text, 0, 0);

        int tw, th;
        TTF_GetTextSize(text, &tw, &th);
        printf("example [1]: width=%d; height=%d\n", tw, th);
    }

    // example 2
    {
        TTF_SetTextPosition(text, 100, 100);

        int tw, th;
        TTF_GetTextSize(text, &tw, &th);
        printf("example [2]: width=%d; height=%d\n", tw, th);
    }

Output:

example [1]: width=154; height=32
example [2]: width=154; height=132

Notice how the width remains the same (as expected) while the height changed by the same amount as TexPos.y.

Glancing at the code, my guess is that its L4514 of the LayoutText function[1] which adds the text y position to the height variable then at the bottom of the function that same height variable gets written back to text->internal->h on L4625.

I'm new to this library so I could be misunderstanding something. My assumption is that if you have a box of width w and height h, those values don't change if you merely move position of the box.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions