Make font selection scheme specific

Add the members font and font_descriptor to struct clr_scheme, so that
it is possible to specify a font for each scheme.

During initialization create the font descriptors for each scheme.

Instead of initially setting the font descriptor when setting up the
buffer, set the font descriptor when drawing the text.

Signed-off-by: Frank Oltmanns <frank@oltmanns.dev>
Signed-off-by: Maarten van Gompel <proycon@anaproy.nl>
This commit is contained in:
Frank Oltmanns 2023-10-23 13:59:31 +02:00 committed by Maarten van Gompel
parent 6e52be343d
commit d6439afcb9
6 changed files with 28 additions and 14 deletions

View File

@ -1,7 +1,7 @@
#ifndef config_def_h_INCLUDED
#define config_def_h_INCLUDED
static const char *default_font = "Sans 14";
#define DEFAULT_FONT "Sans 14"
static const int transparency = 255;
struct clr_scheme schemes[] = {
@ -12,6 +12,7 @@ struct clr_scheme schemes[] = {
.high = {.bgra = {100, 100, 100, transparency}},
.swipe = {.bgra = {100, 255, 100, 64}},
.text = {.color = UINT32_MAX},
.font = DEFAULT_FONT,
},
{
/* colors */
@ -20,6 +21,7 @@ struct clr_scheme schemes[] = {
.high = {.bgra = {100, 100, 100, transparency}},
.swipe = {.bgra = {100, 255, 100, 64}},
.text = {.color = UINT32_MAX},
.font = DEFAULT_FONT,
}
};

7
drw.c
View File

@ -30,11 +30,14 @@ drwsurf_flip(struct drwsurf *ds)
void
drw_draw_text(struct drwsurf *d, Color color, uint32_t x, uint32_t y,
uint32_t w, uint32_t h, uint32_t b, const char *label)
uint32_t w, uint32_t h, uint32_t b, const char *label,
PangoFontDescription *font_description)
{
cairo_save(d->cairo);
pango_layout_set_font_description(d->layout, font_description);
cairo_set_source_rgba(
d->cairo, color.bgra[2] / (double)255, color.bgra[1] / (double)255,
color.bgra[0] / (double)255, color.bgra[3] / (double)255);
@ -134,8 +137,6 @@ setup_buffer(struct drwsurf *drwsurf)
cairo_scale(drwsurf->cairo, drwsurf->scale, drwsurf->scale);
cairo_set_antialias(drwsurf->cairo, CAIRO_ANTIALIAS_NONE);
drwsurf->layout = pango_cairo_create_layout(drwsurf->cairo);
pango_layout_set_font_description(drwsurf->layout,
drwsurf->ctx->font_description);
pango_layout_set_auto_dir(drwsurf->layout, false);
cairo_save(drwsurf->cairo);

4
drw.h
View File

@ -6,7 +6,6 @@
struct drw {
struct wl_shm *shm;
PangoFontDescription *font_description;
};
struct drwsurf {
uint32_t width, height, size;
@ -41,7 +40,8 @@ void drw_over_rectangle(struct drwsurf *d, Color color, uint32_t x, uint32_t y,
uint32_t w, uint32_t h);
void drw_draw_text(struct drwsurf *d, Color color, uint32_t x, uint32_t y,
uint32_t w, uint32_t h, uint32_t b, const char *label);
uint32_t w, uint32_t h, uint32_t b, const char *label,
PangoFontDescription *font_description);
uint32_t setup_buffer(struct drwsurf *drwsurf);

View File

@ -574,7 +574,7 @@ kbd_draw_key(struct kbd *kb, struct key *k, enum key_draw_type type)
}
drw_draw_text(kb->surf, scheme->text, k->x, k->y, k->w, k->h,
KBD_KEY_BORDER, label);
KBD_KEY_BORDER, label, scheme->font_description);
wl_surface_damage(kb->surf->surf, k->x, k->y, k->w, k->h);
if (type == Press || type == Unpress) {
@ -590,7 +590,8 @@ kbd_draw_key(struct kbd *kb, struct key *k, enum key_draw_type type)
draw_inset(kb->popup_surf, k->x, kb->last_popup_y, k->w, k->h,
KBD_KEY_BORDER, scheme->high);
drw_draw_text(kb->popup_surf, scheme->text, k->x, kb->last_popup_y,
k->w, k->h, KBD_KEY_BORDER, label);
k->w, k->h, KBD_KEY_BORDER, label,
scheme->font_description);
wl_surface_damage(kb->popup_surf->surf, k->x, kb->last_popup_y, k->w,
k->h);
}

View File

@ -54,6 +54,8 @@ struct clr_scheme {
Color high;
Color swipe;
Color text;
char *font;
PangoFontDescription *font_description;
};
struct key {

20
main.c
View File

@ -24,6 +24,9 @@
fprintf(stderr, __VA_ARGS__); \
exit(1)
/* array size */
#define countof(x) (sizeof(x) / sizeof(*x))
/* client state */
static const char *namespace = "wlroots";
static struct wl_display *display;
@ -747,7 +750,7 @@ main(int argc, char **argv)
{
/* parse command line arguments */
char *layer_names_list = NULL, *landscape_layer_names_list = NULL;
const char *fc_font_pattern = NULL;
char *fc_font_pattern = NULL;
height = normal_height = KBD_PIXEL_HEIGHT;
landscape_height = KBD_PIXEL_LANDSCAPE_HEIGHT;
@ -913,8 +916,9 @@ main(int argc, char **argv)
keyboard.schemes[1].high.bgra[3] = alpha;
}
if (!fc_font_pattern) {
fc_font_pattern = default_font;
if (fc_font_pattern) {
for (i = 0; i < countof(schemes); i++)
schemes[i].font = fc_font_pattern;
}
display = wl_display_connect(NULL);
@ -959,8 +963,10 @@ main(int argc, char **argv)
kbd_init(&keyboard, (struct layout *)&layouts, layer_names_list,
landscape_layer_names_list);
draw_ctx.font_description =
pango_font_description_from_string(fc_font_pattern);
for (i = 0; i < countof(schemes); i++) {
schemes[i].font_description =
pango_font_description_from_string(schemes[i].font);
}
if (!hidden)
show();
@ -1014,8 +1020,10 @@ main(int argc, char **argv)
}
}
if (fc_font_pattern != default_font) {
if (fc_font_pattern) {
free((void *)fc_font_pattern);
for (i = 0; i < countof(schemes); i++)
schemes[i].font = NULL;
}
return 0;