libsdl-org / SDL_ttf

Support for TrueType (.ttf) font files with Simple Directmedia Layer.
zlib License
403 stars 131 forks source link

TTF_MeasureString returns 1 more character than what can fit without harfbaz #429

Open ahmed-AEK opened 3 weeks ago

ahmed-AEK commented 3 weeks ago

Hello, i am really grateful for the work you are putting into SDL3_ttf, i was trying SDL3_ttf, i downloaded freetype from the linked tag, and compiled with it with SDL3_ttf (so no harfbaz), and i noticed TTF_MeasureString was returning 1 more character than could fit, an example below

I am using latest version, so commit 2554dd7 (3 days ago), also using the same version of freetype that is linked to this repository

#include <iostream>
#include <cassert>
#include <SDL3_ttf/SDL_ttf.h>
#include <string>

int main()
{
    bool init = TTF_Init();
    assert(init);

    TTF_Font* font = TTF_OpenFont("FreeSans.ttf", 16);
    assert(font);

    int measured_width;
    size_t measured_length;
    std::string str{ "ABCD" };
    bool success = TTF_MeasureString(font, str.c_str(), str.size(), 1, &measured_width, &measured_length);
    assert(success);

    std::cout << "width = " << measured_width << '\n';
    std::cout << "length = " << measured_length << '\n';
}

what is printed is width = 0 and length = 1 , for 1 pixel i expected 0 characters to be possible, i tried different fonts and got the same result.

i am using windows with MSVC verion 19.39.33523 (though i think the issue doesn't seem compiler specific)

i tried tracking down what may have caused this and this commit 40e606f seems to remove a comment with // The last character didn't fit along with the line that subtracts 1 from the result in that function, so ... maybe that's causing this issue ? i can't tell for sure.

using SDL3_TTF with harfbaz correctly produces width = 0 and length = 0 , so the issue is specific to using freetype implementation, i will use harfbaz for now so this is not a blocking issue for me.

using Roboto-Regular.ttf font also gives the same result if you want a lightweight font to test with.