Merge pull request #98 from flannelhead/font-selection

Font selection
This commit is contained in:
Giuseppe 2015-01-21 20:12:05 +01:00
commit 78dc0d0d21
2 changed files with 33 additions and 7 deletions

View File

@ -84,6 +84,10 @@ Set the text background color. The parameter I<color> can be I<-> or a color in
Set the text foreground color. The parameter I<color> can be I<-> or a color in one of the formats mentioned before. The special value I<-> resets the color to the default one. Set the text foreground color. The parameter I<color> can be I<-> or a color in one of the formats mentioned before. The special value I<-> resets the color to the default one.
=item B<T>I<index>
Set the font used to draw the following text. The parameter I<index> is a 1-based index of the font list supplied to bar. Any other value (for example I<->) resets bar to normal behaviour (matching the first font that can be used for that character). If the selected font can't be used to draw a character, bar will fall back to normal behaviour for that character.
=item B<U>I<color> =item B<U>I<color>
Set the text underline color. The parameter I<color> can be I<-> or a color in one of the formats mentioned before. The special value I<-> resets the color to the default one. Set the text underline color. The parameter I<color> can be I<-> or a color in one of the formats mentioned before. The special value I<-> resets the color to the default one.

28
bar.c
View File

@ -77,6 +77,8 @@ static monitor_t *monhead, *montail;
static font_t *font_list[MAX_FONT_COUNT]; static font_t *font_list[MAX_FONT_COUNT];
static char *font_names[MAX_FONT_COUNT]; static char *font_names[MAX_FONT_COUNT];
static font_t *font_cache[FONT_CACHE_SIZE]; static font_t *font_cache[FONT_CACHE_SIZE];
static int font_count = 0;
static int font_index = -1;
static uint32_t attrs = 0; static uint32_t attrs = 0;
static bool dock = false; static bool dock = false;
static bool topbar = true; static bool topbar = true;
@ -320,17 +322,27 @@ area_add (char *str, const char *optend, char **end, monitor_t *mon, const int x
return true; return true;
} }
bool
font_has_glyph (font_t *font, const uint16_t c)
{
return c >= font->char_min && c <= font->char_max &&
font->width_lut[c - font->char_min].character_width != 0;
}
/* returns NULL if character cannot be printed */ /* returns NULL if character cannot be printed */
font_t * font_t *
select_drawable_font (const uint16_t c) select_drawable_font (const uint16_t c)
{ {
/* If the user has specified a font to use, try that first. */
if (font_index != -1 && font_has_glyph(font_list[font_index - 1], c))
return font_list[font_index - 1];
/* if the end is reached without finding an apropriate font, return NULL. /* if the end is reached without finding an apropriate font, return NULL.
* If the font can draw the character, return it. * If the font can draw the character, return it.
*/ */
for (int i = 0; font_list[i] != NULL; i++) { for (int i = 0; font_list[i] != NULL; i++) {
font_t *font = font_list[i]; font_t *font = font_list[i];
if (c >= font->char_min && c <= font->char_max && if (font_has_glyph(font, c))
font->width_lut[c - font->char_min].character_width != 0)
return font; return font;
} }
return NULL; return NULL;
@ -422,6 +434,13 @@ parse (char *text)
pos_x = 0; pos_x = 0;
break; break;
case 'T':
font_index = (int)strtoul(p, NULL, 10);
if (!font_index || font_index >= font_count)
font_index = -1;
p = end;
break;
/* In case of error keep parsing after the closing } */ /* In case of error keep parsing after the closing } */
default: default:
p = end; p = end;
@ -851,12 +870,15 @@ init (void)
/* Load the fonts */ /* Load the fonts */
for (int i = 0; font_names[i]; i++) { for (int i = 0; font_names[i]; i++) {
font_list[i] = font_load(font_names[i]); font_list[i] = font_load(font_names[i]);
font_count++;
if (!font_list[i]) if (!font_list[i])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (!font_list[0]) if (!font_list[0]) {
font_list[0] = font_load("fixed"); font_list[0] = font_load("fixed");
font_count++;
}
if (!font_list[0]) if (!font_list[0])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);