config: implement font fallback

Fonts in the configuration may now be a comma separated list of
fonts (all using the fontconfig format). The first font is the primary
font, and the rest are fallback fonts that will be searched, in order.
This commit is contained in:
Daniel Eklöf 2022-02-10 18:30:19 +01:00
parent 2cfe45ee81
commit 6ac046dec3
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
3 changed files with 43 additions and 3 deletions

View file

@ -4,6 +4,7 @@
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <dlfcn.h>
@ -66,7 +67,38 @@ conf_to_color(const struct yml_node *node)
struct fcft_font *
conf_to_font(const struct yml_node *node)
{
return fcft_from_name(1, &(const char *){yml_value_as_string(node)}, NULL);
const char *font_spec = yml_value_as_string(node);
size_t count = 0;
size_t size = 0;
const char **fonts = NULL;
char *copy = strdup(font_spec);
for (const char *font = strtok(copy, ",");
font != NULL;
font = strtok(NULL, ","))
{
/* Trim spaces, strictly speaking not necessary, but looks nice :) */
while (isspace(font[0]))
font++;
if (font[0] == '\0')
continue;
if (count + 1 > size) {
size += 4;
fonts = realloc(fonts, size * sizeof(fonts[0]));
}
assert(count + 1 <= size);
fonts[count++] = font;
}
struct fcft_font *ret = fcft_from_name(count, fonts, NULL);
free(fonts);
free(copy);
return ret;
}
struct deco *