Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using FontConfig to pick a default font.
/* WARNING -- THE CODE IN THIS FILE DOESN'T DO ANY ERROR HANDLING! */
static const char *TXDI_DEFAULT_FAMILY_NAMES[] = {
"monospace",
"sans",
"serif"
};
TXD_API TxdFont *txd_opendefaultfont(int family, int style, float height, float slant, float outline) {
FcPattern *pat, *match;
FcChar8 *path;
TxdFont *font = NULL;
FcResult result;
TXDI_ASSERT(family >= 0 && family < TXD_COUNT_DEFAULT);
TXDI_ASSERT(height > 0.0f);
pat = FcPatternCreate();
FcPatternAddString(pat, "family", (const FcChar8*)TXDI_DEFAULT_FAMILY_NAMES[family]);
if (style & TXD_STYLE_BOLD) { FcPatternAddInteger(pat, "weight", FC_WEIGHT_BOLD); }
if (style & TXD_STYLE_ITALIC) { FcPatternAddInteger(pat, "slant", FC_SLANT_ITALIC); }
FcPatternAddDouble(pat, "dpi", 72.0); /* 72 dpi = 1 pixel per 'point' */
FcPatternAddDouble(pat, "scale", 1.0);
FcPatternAddDouble(pat, "size", height);
FcDefaultSubstitute(pat); /* fill in other expected pattern fields */
FcConfigSubstitute(0, pat, FcMatchPattern); /* apply any system/user config rules */
match = FcFontMatch(0, pat, &result); /* find 'best' matching font */
if (result != FcResultMatch || !match) {
/* FIXME: better error reporting/handling here...
* want to minimise the situations where opendefaultfont gives you *nothing* */
return NULL;
}
FcPatternGetString(match, "file", 0, &path);
font = txd_openfont((const char*)path, height, slant, outline);
FcPatternDestroy(match);
FcPatternDestroy(pat);
return font;
}
/* On my system, this finds:
mono: /usr/share/fonts/TTF/DejaVuSansMono.ttf
mono, bold: /usr/share/fonts/TTF/DejaVuSansMono-Bold.ttf
mono, italic: /usr/share/fonts/TTF/DejaVuSansMono-Oblique.ttf
mono, bold-italic: /usr/share/fonts/TTF/DejaVuSansMono-BoldOblique.ttf
sans: /usr/share/fonts/TTF/DejaVuSans.ttf
sans, bold: /usr/share/fonts/TTF/DejaVuSans-Bold.ttf
sans, italic: /usr/share/fonts/TTF/DejaVuSans-Oblique.ttf
sans, bold-italic: /usr/share/fonts/TTF/DejaVuSans-BoldOblique.ttf
serif: /usr/share/fonts/TTF/DejaVuSerif.ttf
serif, bold: /usr/share/fonts/TTF/DejaVuSerif-Bold.ttf
serif, italic: /usr/share/fonts/TTF/DejaVuSerif-Italic.ttf
serif, bold-italic: /usr/share/fonts/TTF/DejaVuSerif-BoldItalic.ttf
Note that the sans-serif fonts have 'oblique' styles, while the serif font has 'italic' styles.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment