From 6ac046dec31353cce847fb7a6b1c4cc99aedd84f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 10 Feb 2022 18:30:19 +0100 Subject: [PATCH] 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. --- CHANGELOG.md | 3 +++ config.c | 34 +++++++++++++++++++++++++++++++++- doc/yambar.5.scd | 9 +++++++-- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58b6346..43c95a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ ## Unreleased ### Added + +* Support for custom font fallbacks + (https://codeberg.org/dnkl/yambar/issues/153). ### Changed ### Deprecated ### Removed diff --git a/config.c b/config.c index 7f9583e..580781b 100644 --- a/config.c +++ b/config.c @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -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 * diff --git a/doc/yambar.5.scd b/doc/yambar.5.scd index d3a585f..4e70ef2 100644 --- a/doc/yambar.5.scd +++ b/doc/yambar.5.scd @@ -12,7 +12,8 @@ and reference them using anchors. Besides the normal yaml types, there are a couple of yambar specific types that are frequently used: -- *font*: this is a string in _fontconfig_ format. Example of valid values: +- *font*: this is a comma separated list of fonts in _fontconfig_ + format. Example of valid values: - Font Awesome 5 Brands - Font Awesome 5 Free:style=solid - Dina:pixelsize=10:slant=italic @@ -124,7 +125,11 @@ types that are frequently used: | font : font : no -: Default font to use in modules and particles +: Default font to use in modules and particles. May also be a comma + separated list of several fonts, in which case the first font is + the primary font, and the rest fallback fonts. These are yambar + custom fallback fonts that will be searched before the fontconfig + provided fallback list. | foreground : color : no