You are not logged in.

#1 2021-02-24 01:55:02

GhostlyCoding
Member
Registered: 2020-06-11
Posts: 8

Text won't load in any graphics libraries

I've tested 2 graphics libraries, X11 and SDL2, both in C/C++, both causing a segmentation fault at the line of loading the font.
My code for X11: https://pastebin.com/EHuLDrc7
My code for SDL2: https://pastebin.com/FipXkwU6

I have looked on the internet to see if anybody else has had this issue before, but I found nothing.

Offline

#2 2021-02-24 02:36:47

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: Text won't load in any graphics libraries

There are likely a number of problems with the X11 version.  First, I cannot replicate a segfault on the font loading - but rather it segfaults on using "font".  At least in my case, this is due to XLoadQueryFont returning a null pointer which you should 1) test for before you use it and 2) should be done outside the event loop.  Does XLoadQueryFont even return a valid pointer to a structure for you?

For the SDL version, you are using TTF_OpenFont which expects a font file, and you are passing just "Sans".  Do you have a font file in the current working directory called "Sans"?  Wouldn't it at least be "Sans.ttf"?  In any case, the issue is about the same: the load fails, and returns a null pointer which your code does not check, but just tries to use.  Dereferencing a null pointer will cause a segfault.

I gather you just want to be able to draw text.  Do you need a specific font, or would any generic available font be suitable?  If so, you can use the X11 drawing functions without explicitly setting a font.  If you want to be able to load fonts by names like "sans", then you'd want to use fontconfig, or some other library that in turn uses fontconfig (e.g., cairo is pretty easy to get started with).

Last edited by Trilby (2021-02-24 02:46:29)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2021-03-13 23:56:06

GhostlyCoding
Member
Registered: 2020-06-11
Posts: 8

Re: Text won't load in any graphics libraries

I would like to be able to load a specific font. I tried checking if XLoadQueryFont returns null on Helvetica but it doesn't. The font also loads fine.

Last edited by GhostlyCoding (2021-03-14 00:12:27)

Offline

Board footer

Powered by FluxBB