I noticed that the TTF_GetGlyphKerning should give me a kerning value between two characters of a font, but it seems not to work. There isn’t any documentation on it and my research (chatgpt) says that it does not exist, even though it is in the header files. Am I missing something important?
ChatGPT doesn’t actually know anything. It doesn’t understand what you’re asking or what it’s telling you, which is why its output is often nonsense and not something you should trust. It’s a fancy word association + text prediction system.
Anyway, does calling TTF_GetGlyphKerning return an error? Does it return NULL for the last parameter (*kerning)?
AFAIK kerning data is something fonts have to provide on a glyph pair by glyph pair basis, so the font’s creator can tweak it to look how they want. If the font doesn’t have a kerning value for the given glyph pair then it would make sense this function wouldn’t return anything in the *kerning parameter.
I have to say that ChatGPT is a double-edged tool. It is good for simple stuff, especially when errors occur with gcc regarding missing headers and libs.
For example, an error message like this is entered in ChatGPT. $ gcc test.c test.c: In function ‘main’: test.c:3:3: warning: implicit declaration of function ‘SDL_Init’ [-Wimplicit-function-declaration] 3 | SDL_Init(0); | ^~~~~~~~ /usr/bin/ld: /tmp/ccwrttuF.o: in function main’:
test.c:(.text+0x1e): undefined reference to SDL_Init' collect2: error: ld returned 1 exit status
And now I have the answer: https://www.perplexity.ai/search/gcc-test-c-test-c-in-function-HpnjOcHRSCSfpC8Ujg_Q.A
Thanks to ChatGPT I managed to build the latest DLLs for GTK4. I wouldn’t have managed it without this help.
It works well for older things. You don’t have to expect any help with the new SDL3 (yet).
Without trying to be a jerk: it’s obvious from the error message alone that you have a missing header and aren’t linking to SDL.
And, again, ChatGPT and other generative “AI” don’t actually understand anything. They don’t know if what they’re telling you is correct, and sometimes the output is wrong. Like when Google’s AI was telling people to eat rocks after scraping a joke article from The Onion, or when a lawyer used ChatGPT to write a brief and got in trouble with the judge because whoops! it was citing cases that don’t exist as a basis for a legal argument.
IIRC SDL_TTF does it for you when you use it to create a texture surface. I needed an atlas for my use case and I had to use FreeType. But I didn’t need kerning. I looked at the source once and it seems like it does a binary search every time. IIRC you can get kerning information from the GPOS section of a font file and from a KERN table. If anyone writes code to parse that I’d be interested in having a look. It wasn’t obvious to me how to properly scale the values to a specific font size. But I barely spent any time looking, just enough to see it was a binary search and not a full table scan
Sorry, that was in response to the poster above that post, not to you. Will edit that post to clarify.
If you draw them with SDL_ttf using that font and those particular glyphs, does it look like they’re getting rendered with kerning compared to using them in some outside program?
I’m not familiar enough with SDL_ttf to know what function, if any, you have to call to get the right glyph index from a given character for passing to TTF_GetGlyphKerning(). Maybe check the SDL_ttf source and see if it’s used anywhere.
Or maybe it’s a bug!
If you’re building a font atlas it can still be a good idea to store the kerning data with it, so you get nicer text rendering.
For font atlases I just use stb_truetype from here. Its only dependency is stb_rect_pack (available from the same link), and since they’re both single-header libraries they’re a lot easier to include and build than SDL_ttf.
After testing, there is a pixel offset applied to the ‘k’ and ‘e’ when rendering it as a texture.
It could be!
After looking at the sdl_ttf source code, It seems like I should be getting a kerning value since the function returns true and the two characters I input (k, e) have kerning when paired. I also tried other pairs like ‘W’ and ‘A’ with no luck.
I even replaced the return value from int to float and recompiled it just to be sure that it isn’t a lack of precision (which it isn’t). Who knows, maybe freetype itself is having trouble ¯\_(ツ)_/¯
I did compile it with -DSDLTTF_VENDORED=ON and -DSDLTTF_HARFBUZZ=ON, but it still doesn’t see a kerning value. (Roboto font size 48, TTF_GetGlyphKerning(font, (int)'A', (int)'V', &kerning);)
(P.S. I was wondering btw if the kerning could return as an int? You strictly set it to an int before returning it, is there a reason why removing that and returning a float doesn’t work?)