Skip to content

Instantly share code, notes, and snippets.

@max-verem
Forked from CallumDev/fontconfig.c
Created December 2, 2024 06:11
Show Gist options
  • Save max-verem/53c98714cba30060dd73927f480a4137 to your computer and use it in GitHub Desktop.
Save max-verem/53c98714cba30060dd73927f480a4137 to your computer and use it in GitHub Desktop.
FontConfig sample in C
//compiled gcc fonttest.c -o fonttest -lfontconfig
//Sample output: /usr/share/fonts/steam-fonts/arial.ttf
#include <stdio.h>
#include <stdlib.h>
#include <fontconfig/fontconfig.h>
int main()
{
FcConfig* config = FcInitLoadConfigAndFonts();
//make pattern from font name
FcPattern* pat = FcNameParse((const FcChar8*)"Arial");
FcConfigSubstitute(config, pat, FcMatchPattern);
FcDefaultSubstitute(pat);
char* fontFile; //this is what we'd return if this was a function
// find the font
FcResult result;
FcPattern* font = FcFontMatch(config, pat, &result);
if (font)
{
FcChar8* file = NULL;
if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch)
{
//we found the font, now print it.
//This might be a fallback font
fontFile = (char*)file;
printf("%s\n",fontFile);
}
}
FcPatternDestroy(pat);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment