Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: Fontconfig error: Cannot load default config file

I have the following problem:

I included a static library of FontConfig libfontconfig.a into my iPhone project.

The application is building and i can run it on the device but when I do a call to a method that needs some functions of this library the application crashes with the following error

Fontconfig error: Cannot load default config file

terminate called throwing an exception

I read that this error normally occurs when FontConfig cannot find the fonts.conf file that is normally located in /etc/fonts.

But I cannot move that file to this directory of the iPhone. Has anyone a solution? Just copying the file to the application bundle did not help, as well.

I would appreciate any help.

like image 791
el flex Avatar asked Oct 08 '22 05:10

el flex


1 Answers

I finally found a way to solve this problem. I set an Environment Variable FONTCONFIG_FILE and FONTCONFIG_PATH that define the locations of the fonts.conf file and the path that was before /etc/fonts. So I can add the fonts directory to the project and define the path @runtime.

The Environment Variables have to be set in your code before you call a method that needs a functionality of FontConfig. I set it after starting my Application in the AppDelegate.

Here is the code I added:

//Get the directory of your Application
NSString *bundlePath = [[NSBundle mainBundle] resourcePath];

//Create a String with the location of your fonts.conf file
NSString *fontconfigFile= [bundlePath stringByAppendingString:
                             [NSString stringWithFormat:@"/fonts/fonts.conf"]];

//Create a String with the path of the FontConfig configuration path
NSString *fontconfigPath= [bundlePath stringByAppendingString:
                             [NSString stringWithFormat:@"/fonts"]];

//Set the Environment Variables
setenv("FONTCONFIG_FILE", [fontconfigFile UTF8String], 0); 
setenv("FONTCONFIG_PATH", [fontconfigPath UTF8String], 0); 

In a second step I had then to modify my fonts.conf file. I added the fonts directory to FC/fonts and the cache directory to FC/cache of my project and adjusted the fonts.conf file on two parts.

I changed the first part:

<!-- Font directory list -->

<dir>/usr/share/fonts</dir>
<dir>/usr/X11R6/lib/X11/fonts</dir>
<dir>~/.fonts</dir>

to:

<!-- Font directory list -->

<dir>FC/fonts</dir>

and the second part:

<!-- Font cache directory list -->

<cachedir>/opt/local/fc1407cd/var/cache/fontconfig</cachedir>
<cachedir>~/.fontconfig</cachedir>

to:

<!-- Font cache directory list -->

<cachedir>FC/cache</cachedir>

Then it did not crash anymore.

like image 196
el flex Avatar answered Oct 13 '22 10:10

el flex