The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL_ttf 2.0.14 - TTF_SetFontKerning not working as expected
imapersonman


Joined: 18 May 2016
Posts: 1
According to the documentation, a simple call to TTF_SetFontKerning(TTF_Font *font, int allowed) will turn on Kerning for a specific font when allowed is non-zero, and will turn off Kerning for a specific font when allowed is zero. When using this function in a project I didn't see a difference between fonts with kerning on and fonts with kerning off. I wrote a test app to look at it more closely. Here is an image of the output.

http://i.imgur.com/jaA5O99.png?1

The text in green has kerning turned on, and the text in blue has kerning turned off. Kerning is still being applied to both strings.

Here is the source code for the test application.

Code:

#include <iostream>
#include <SDL.h>
#include <SDL_ttf.h>

int main(int argc, const char * argv[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Window *Window = SDL_CreateWindow("Kerning", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 400, 400, 0);
    SDL_Renderer *Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_PRESENTVSYNC);

    TTF_Init();
    TTF_Font *Font = TTF_OpenFontRW(SDL_RWFromFile("Resources/Fonts/Apple Chancery.ttf", "rb"), 1, 144);
    if (!Font)
    {
        std::cout << "No Font, Exiting." << std::endl;
        std::cout << SDL_GetError() << std::endl;
        return(-1);
    }

    TTF_SetFontKerning(Font, 1);
    SDL_Surface *FontKerningSurface = TTF_RenderText_Solid(Font, "NA", {0, 200, 0});
    SDL_Texture *FontKerningTexture = SDL_CreateTextureFromSurface(Renderer, FontKerningSurface);
    TTF_SetFontKerning(Font, 0);
    SDL_Surface *FontSurface = TTF_RenderText_Solid(Font, "NA", {0, 0, 200});
    SDL_Texture *FontTexture = SDL_CreateTextureFromSurface(Renderer, FontSurface);

    bool Running = true;
    SDL_Event Event;

    while (Running)
    {
        while (SDL_PollEvent(&Event))
        {
            switch (Event.type)
            {
                case SDL_QUIT:
                {
                    Running = false;
                } break;
            }
        }

        SDL_SetRenderDrawColor(Renderer, 255, 255, 255, 255);
        SDL_RenderClear(Renderer);

        {
            int Width, Height;
            SDL_QueryTexture(FontKerningTexture, NULL, NULL, &Width, &Height);
            SDL_Rect DestRect = { 10, 10, Width, Height };
            SDL_RenderCopy(Renderer, FontKerningTexture, NULL, &DestRect);

            SDL_SetRenderDrawColor(Renderer, 0, 0, 0, 255);
            SDL_RenderDrawLine(Renderer, 18, 0, 18, 400);
            SDL_RenderDrawLine(Renderer, 10 + Width, 0, 10 + Width, 400);
            SDL_SetRenderDrawColor(Renderer, 255, 0, 0, 255);
            SDL_RenderDrawLine(Renderer, 177, 0, 177, 400);
            SDL_RenderDrawLine(Renderer, 145, 0, 145, 400);
        }

        {
            int Width, Height;
            SDL_QueryTexture(FontTexture, NULL, NULL, &Width, &Height);
            SDL_Rect DestRect = { 10, 150, Width, Height };
            SDL_RenderCopy(Renderer, FontTexture, NULL, &DestRect);
        }
       
        SDL_RenderPresent(Renderer);
    }

    return(0);
}


Am I doing something wrong or is this a bug?