Does TTF_OpenFont works with Ubuntu? = Yes with the good compilation flag

Hello,

I’m trying to load fonts with SDL_ttf and fail with the message “Error writing to datastream” on my main program. So I tried a simpler program to identify the cause, but get the message “Couldn’t load font file” this time. I added some features to this second program like testing different fonts, a different way to load font (load file, then open it) to try to identify the problem is. SDL read the file but cannot open it as a font.

So, I tried the “showfont” program from SDL2_ttf-2.0.15 source code and this one shows me all the fonts I tried. I tried an old program I made years ago and it worked well until I compile it again. :disappointed_relieved: I even tried to look into SDL source code and saw that these message are very generic, that doesn’t helped me :grinning: I tried to install FreeType 2 but useless…

I think the problem may comes from dependencies in my makefile, but I don’t see any. Maybe from compatibility with Linux (Ubuntu 18.04), but I don’t know how to identify and solve this. Can someone help me ?

— Makefile output

rm -rf *.o
g++ -c test_ttf.cpp -o test_ttf.o `pkg-config --cflags --libs sdl2` -lSDL_ttf -pedantic
(rm -rf ../test_ttf) && (g++ -o ../test_ttf *.o `pkg-config --cflags --libs sdl2` -lSDL_ttf )

— Program: test_ttf.cpp

#include "SDL.h"
#include "SDL_ttf.h"
#include < iostream> // remove the space after "<" to work
#define err(msg) ( cout << __FILE__ << "\t" << __LINE__ << "\t" << msg << "\n", fflush(stdout), 0)

using namespace std;

const char* font_list[] = {
	"font/NoFile",
	"font/RedFive.otf",
	"font/DejaVuSans.ttf",
	"font/UbuntuMono-R.ttf",
	NULL
};

int font_test1(const char* file) {
	cout << "- TTF_OpenFont " << file << ":\t";
	TTF_Font* font = TTF_OpenFont(file, 16);
	if(font == NULL) return err(TTF_GetError()); else cout << "ok\n";

	TTF_CloseFont(font);
	return 1;
}

int font_test2(const char* file) {
	cout << "- SDL_RWFromFile " << file << ":\t";
	SDL_RWops* rw = SDL_RWFromFile(file, "r");
	if(rw == NULL) return err("SDL_RWFromFile"); else cout << "ok\n";

	cout << "- TTF_OpenFontIndexRW rw: ";
	TTF_Font* font = TTF_OpenFontRW(rw, 1, 15);
	if(font == NULL) return err(TTF_GetError()); else cout << "ok\n";

	TTF_CloseFont(font);
	return 1;
}

int main(int,char *[]) {
	if(SDL_Init(SDL_INIT_EVERYTHING) != 0) return err(SDL_GetError());
	if(TTF_Init() != 0) return err(TTF_GetError());

	const char** font = font_list;
	while(*font != NULL) {
		if(font_test1(*font)) cout << "OK\n"; else  cout << "FAIL\n";
		if(font_test2(*font)) cout << "OK\n"; else  cout << "FAIL\n";
		font++;
	}

	TTF_Quit();
	SDL_Quit();
	return 0;
}

— Program output

  • TTF_OpenFont font/NoFile: test_ttf.cpp 20 Couldn’t open font/NoFile
    FAIL
  • SDL_RWFromFile font/NoFile: test_ttf.cpp 29 SDL_RWFromFile
    FAIL
  • TTF_OpenFont font/RedFive.otf: test_ttf.cpp 20 Couldn’t load font file
    FAIL
  • SDL_RWFromFile font/RedFive.otf: ok
  • TTF_OpenFontIndexRW rw: test_ttf.cpp 33 Couldn’t load font file
    FAIL
  • TTF_OpenFont font/DejaVuSans.ttf: test_ttf.cpp 20 Couldn’t load font file
    FAIL
  • SDL_RWFromFile font/DejaVuSans.ttf: ok
  • TTF_OpenFontIndexRW rw: test_ttf.cpp 33 Couldn’t load font file
    FAIL
  • TTF_OpenFont font/UbuntuMono-R.ttf: test_ttf.cpp 20 Couldn’t load font file
    FAIL
  • SDL_RWFromFile font/UbuntuMono-R.ttf: ok
  • TTF_OpenFontIndexRW rw: test_ttf.cpp 33 Couldn’t load font file
    FAIL

The program seems to read the files (SDL_RWFromFile OK), but doesn’t seem to be able to open it as a font (TTF_OpenFontIndexRW KO).

I’m using SDL2_ttf with no problem on Ubuntu 18.04, so it should work…

If you compile the testime from the SDL source, the make command is

gcc -o testime testime.c -g -O2 -D_REENTRANT -I/usr/include/SDL2 -DHAVE_OPENGLES2 -DHAVE_OPENGL -DHAVE_SDL_TTF -g -lSDL2_test -lSDL2 -lSDL2_ttf

you could try to adapt this

change this to -lSDL2_ttf and it will work :wink:

1 Like

change this to -lSDL2_ttf and it will work

lol thanks a lot !