libsdl-org/SDL_ttf

Couldn't load font file

sd-evo opened this issue · 14 comments

sd-evo commented

Hello friends!

I'm trying to open a ttf file, which is a valid font file, but I'm getting an error message: "Couldn't load font file". I've tried downloading different fonts from various websites, but to no avail. I've also checked the permissions on the font files, and they are accessible with full permissions.

Here's my configuration:
SDL3
SDL3_image
SDL3_ttf
Ubuntu 22.04
Cmake 3.26
C++20

Cmake configuration:

cmake_minimum_required(VERSION 3.26)
project(MyTest)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -Werror")

add_executable(MyTest
        // cpp files
)

include(FindPkgConfig)

pkg_search_module(SDL3 REQUIRED sdl3)
pkg_search_module(SDL3IMAGE REQUIRED SDL2_image >= 2.0.0)
pkg_search_module(SDL3TTF REQUIRED SDL2_ttf >= 2.0.0)

include_directories(include ${SDL3_INCLUDE_DIRS} ${SDL3IMAGE_INCLUDE_DIRS} ${SDL3TTF_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${SDL3_LIBRARIES} ${SDL3IMAGE_LIBRARIES} ${SDL3TTF_LIBRARIES})

Code:

#include <SDL3/SDL.h>
#include <SDL3/SDL_render.h>
#include <SDL3_ttf/SDL_ttf.h>

int main()
{
    if (SDL_Init(SDL_INIT_VIDEO) != 0 {
        std::cout << "SDL init failed" << std::endl;
        return 0;
    }

    if (TTF_Init() != 0) {
        std::cout << "TTF init failed" << std::endl;

        SDL_Quit();

        return 0;
    }

    SDL_Window *window = SDL_CreateWindow("Button Example", WINDOW_WIDTH, WINDOW_HEIGHT, 0);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, nullptr, SDL_RENDERER_ACCELERATED);

    auto font = TTF_OpenFont("data/fonts/Ubuntu-Regular.ttf", 16);
    if (nullptr == font) {
        std::cout << TTF_GetError() << std::endl;

        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        TTF_Quit();
        SDL_Quit();

        return 0;
    }
    
    // Some code...
}
madebr commented

I can reproduce.
SDL_ttf is a thin wrapper around libfreetype, which I think does not support this file type.
At least, this was the case 8 years ago.
I grep'ed libfreetype's source code, and could not find a single match referring to the .eot file format.
These are the only eot reference I could find on their mailing list, and they don't bode well:
https://lists.nongnu.org/archive/html/freetype/2003-04/msg00064.html
https://lists.nongnu.org/archive/cgi-bin/namazu.cgi?query=eot&submit=Search%21&idxname=freetype-devel&max=20&result=normal&sort=score

sd-evo commented

@madebr Sorry, that was me testing. But of course, I only try files in ttf format.
Thank you, I edited the message.

madebr commented

All Ubuntu ttf variants downloadable from https://fonts.google.com/specimen/Ubuntu, seem to work for me.

sd-evo commented

@madebr I also downloaded from this website, but the font doesn't open for me, it gives the same error...

madebr commented

Ubuntu 22.04 has libfreetype 2.11. Can you try the latest 2.13?
The easiest way to do this is to build SDL3_ttf with vendored dependencies: fetch SDL3_ttf's git submodules, configure with -DSDL3TTF_VENDORED=TRUE, and rebuild.
If that does not fix it, I'm out of options.

sd-evo commented

@madebr Unfortunately, in Ubuntu 22.04, the libfreetype 2.13 package is missing. I tried the 32-bit version (i386), fetched the git submodules, and updated them. Then, I rebuilt SDL_ttf with the DSDL3TTF_VENDORED=TRUE flag, but it didn’t help :(

madebr commented

The error message comes from here, because of a failing FT_Open_Face:

SDL_ttf/src/SDL_ttf.c

Lines 1811 to 1816 in f9b636f

error = FT_Open_Face(library, &font->args, index, &font->face);
if (error || font->face == NULL) {
TTF_SetFTError("Couldn't load font file", error);
TTF_CloseFont(font);
return NULL;
}

Since I can't reproduce, perhaps you can find out where inside libttf it starts failing through some debugging?
The vendored libttf should make it easier to do this.

sd-evo commented

@madebr I tried running the build SDL_ttf from github, passing one of the fonts that I couldn’t use in my application. The SDL_ttf application started and was able to load the required font. Could this indicate that SDL_ttf is not properly connected in my CMake or that one of the SDL3 libraries is incorrectly installed?

madebr commented

All I know Couldn't load font file means libttf failed to load your font. So you need to find out what is different between the 2 libttf calls.

Since you don't have link issues and your app appears to start, I don't think your cmake is wrong.

sd-evo commented

@madebr My code works if I use it within the downloaded repository of your SDL_ttf project, but it doesn’t work in my own folder. I think there must be something missing in my project. I will investigate. Thank you for your efforts to help!

1bsyl commented

@sd-evo you can enable USE_FREETYPE_ERRORS in SDL_ttf.c code so that you have more accurate error message (eg FT errors)

sd-evo commented

@madebr Should the current SDL_ttf library be cloned into your project or installed using cmake?

madebr commented

@madebr Should the current SDL_ttf library be cloned into your project or installed using cmake?

The SDL_ttf cmake script supports both, so whatever works for your project.

sd-evo commented

In the end, I performed a search for the SDL3_ttf library using find_package instead of pkg_search_module, and also explicitly specified the paths for include_directories and target_link_libraries using set in CMake. That's the only way it worked for me.