PDA

View Full Version : how to add .ttf file into resource file



vinothrajendran
5th March 2015, 10:37
-> I want to add arialbd.ttf file (part of FTGL library) into my project using resource file(.qrc)

-> I have added the file into .qrc file which is shown below.

<RCC>
<qresource prefix="/">
<file>arialbd.ttf</file>
</qresource>
</RCC>

-> But i get the following error msg at runtime,
FT_Stream_Open: could not open `://arialbd.ttf'

-> Here is where i am adding the path in code
ftgl_pixFont = new FTGLPixmapFont((const char*)"://arialbd.ttf");

like to hear suggestions

anda_skoa
5th March 2015, 11:11
FTGLPixmapFont is not a Qt API, it doesn't know about Qt resource paths, only about paths in the file system.

You can use QFile::copy() to copy into a permanent file on disk or QTemporaryFile::createNativeFile() to do it with a temporary file.

Cheers,
_

vinothrajendran
5th March 2015, 12:46
QFile::copy() worked for me.....Thanks

Radek
5th March 2015, 15:25
You may attempt to bypass copying the font file from the executable to a disk and rereading the file from the disk. FTGL can read the font file from a buffer (in fact, you have tried it but mistakenly). Therefore:
(1) open() the file in the resources (you get a QFile)
(2) readAll() the file into a QByteArray
(3) pass FTGL the QByteArray data(). It's char *, perhaps you would need to cast to unsigned char *.
This way, you avoid copying on the disk.

Another possibility is not placing the font in resources at all. Put the font in the same directory as you exe, get exe dir and read the font from there. Your app need not consist of a single exe.