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

Fix MeasureTextEx() height calculation #3770

Merged
merged 1 commit into from
Feb 1, 2024
Merged

Conversation

marrony
Copy link
Contributor

@marrony marrony commented Feb 1, 2024

For multiline strings MeasureTextEx() is returning a much higher value than the actual text height.

Pretty much like DrawTextEx() this patch just increment textLineSpacing in the height when it sees \n and don't apply scaleFactor at the end.

Small program to simulate the issue:

#include <stdlib.h>
#include "raylib.h"

int main(void) {
  InitWindow(200, 100, "MeasureTextEx");

  Font font = GetFontDefault();

  while (!WindowShouldClose()) {
    BeginDrawing();
    ClearBackground(WHITE);

    const char *text = "Some\nMultiline\nText";
    float fontSize = 20;
    float spacing = 5;

    SetTextLineSpacing(fontSize + 4);
    DrawTextEx(font, text, (Vector2){10, 10}, fontSize, spacing, BLACK);

    // draw red box around text
    Vector2 m = MeasureTextEx(font, text, fontSize, spacing);
    DrawRectangleLinesEx((Rectangle){10, 10, m.x, m.y}, 1, RED);

    EndDrawing();
  }

  CloseWindow();
  return 0;
}

Without the fix
Screenshot 2024-01-31 at 5 40 00 PM

With the fix
Screenshot 2024-01-31 at 5 39 25 PM

@raysan5 raysan5 merged commit c0b081f into raysan5:master Feb 1, 2024
@raysan5
Copy link
Owner

raysan5 commented Feb 1, 2024

@marrony thanks for the review! It should fix issue #3745

gstaaij added a commit to gstaaij/geometryfoss that referenced this pull request Feb 9, 2024
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

Successfully merging this pull request may close these issues.

2 participants