c++qtembedded-resourcelibharu

How to use Qt resource for ttf font in libharu?


I'm creating a Qt application that writes a pdf file using libharu. The true-type-font that is used in the pdf file shall be embedded into the binary file, so it is available on all platforms. For this, I want to use the Qt resource system.

The font file should be used as follows

const char* fontName = HPDF_LoadTTFontFromFile(doc,"path/to/myfont.ttf",HPDF_TRUE);
documentFont = HPDF_GetFont(doc,fontName,"ISO8859-2");

where I want the filename and path to be replaced by a resource name (e.g. :/fonts/myfont.ttf). Is there a way to achieve this?

The options that I think of:

Are there any other more simple solutions for this? Thanks.

Solution:

Actually the second option was easier than expected. With the answer by robin.thoni and this question, I obtained this pretty simple solution:

std::string fontFile = std::tmpnam(nullptr);
QFile::copy(":/fonts/myfont.ttf",QString::fromStdString(fontFile));
const char* fontName = HPDF_LoadTTFontFromFile(doc,fontFile.c_str(),HPDF_TRUE);

Solution

  • As you can see here How to access resource image with fopen?, there's no way to 'open' a Qt resource file using standard open/fopen, so you won't be able to craft a valid path to it. You will need to use your second option. You could use std::tmpnam to achieve it.