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

@ -11,6 +11,9 @@
## Unreleased
### Added
* Support for custom font fallbacks
(https://codeberg.org/dnkl/yambar/issues/153).
### Changed
### Deprecated
### Removed

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 *

View file

@ -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