Added support for greater than two fonts.

This commit is contained in:
Grayson MacKenzie 2014-07-22 14:47:41 -04:00
parent 5e7b44dce9
commit f3e9df4a97
1 changed files with 59 additions and 31 deletions

90
bar.c
View File

@ -70,13 +70,14 @@ static xcb_gcontext_t gc[GC_MAX];
static xcb_visualid_t visual; static xcb_visualid_t visual;
static xcb_colormap_t colormap; static xcb_colormap_t colormap;
static monitor_t *monhead, *montail; static monitor_t *monhead, *montail;
static font_t *main_font, *alt_font; #define MAX_FONT_COUNT 5
static font_t *all_fonts[MAX_FONT_COUNT];
static char *all_fonts_str[MAX_FONT_COUNT];
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;
static int bw = -1, bh = -1, bx = 0, by = 0; static int bw = -1, bh = -1, bx = 0, by = 0;
static int bu = 1; /* Underline height */ static int bu = 1; /* Underline height */
static char *mfont, *afont;
static uint32_t fgc, bgc, ugc; static uint32_t fgc, bgc, ugc;
static uint32_t dfgc, dbgc; static uint32_t dfgc, dbgc;
static area_stack_t astack; static area_stack_t astack;
@ -98,10 +99,6 @@ fill_rect (xcb_drawable_t d, xcb_gcontext_t gc, int x, int y, int width, int hei
int int
draw_char (monitor_t *mon, font_t *cur_font, int x, int align, uint16_t ch) draw_char (monitor_t *mon, font_t *cur_font, int x, int align, uint16_t ch)
{ {
/* In the unlikely case that the font doesn't have the glyph wanted just do nothing */
if (ch < cur_font->char_min || ch > cur_font->char_max)
return 0;
int ch_width = cur_font->width_lut[ch - cur_font->char_min].character_width; int ch_width = cur_font->width_lut[ch - cur_font->char_min].character_width;
switch (align) { switch (align) {
@ -295,6 +292,17 @@ area_add (char *str, const char *optend, char **end, monitor_t *mon, const int x
return true; return true;
} }
/* returns NULL if character cannot be printed */
font_t *
select_drawable_font (uint16_t c)
{
int i = 0;
while (all_fonts[i] != NULL && (c < all_fonts[i]->char_min || c > all_fonts[i]->char_max))
i++;
return all_fonts[i];
}
void void
parse (char *text) parse (char *text)
{ {
@ -399,8 +407,9 @@ parse (char *text)
p += 1; p += 1;
} }
/* If the character is outside the main font charset use the alternate font */ cur_font = select_drawable_font(ucs);
cur_font = (ucs < main_font->char_min || ucs > main_font->char_max) ? alt_font : main_font; if (cur_font == NULL)
continue;
xcb_change_gc(c, gc[GC_DRAW] , XCB_GC_FONT, (const uint32_t []){ cur_font->ptr }); xcb_change_gc(c, gc[GC_DRAW] , XCB_GC_FONT, (const uint32_t []){ cur_font->ptr });
@ -590,11 +599,13 @@ monitor_create_chain (xcb_rectangle_t *rects, const int num)
height = h; height = h;
} }
/* TODO replace constants here and in init() with #defines */
if (bw < 0) if (bw < 0)
bw = width - bx; bw = width - bx;
if (bh < 0 || bh > height) if (bh < 0 || bh > height)
bh = main_font->height + bu + 2; bh = all_fonts[0]->height + bu + 2;
/* Check the geometry */ /* Check the geometry */
if (bx + bw > width || by + bh > height) { if (bx + bw > width || by + bh > height) {
@ -799,16 +810,30 @@ void
init (void) init (void)
{ {
/* Load the fonts */ /* Load the fonts */
main_font = font_load(mfont ? mfont : "fixed"); int i = 0;
if (!main_font) while (all_fonts_str[i]) {
all_fonts[i] = font_load(all_fonts_str[i]);
if (!all_fonts[i])
exit(EXIT_FAILURE);
i++;
}
if (all_fonts[0] == NULL)
all_fonts[0] = font_load("fixed");
if (!all_fonts[0])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
alt_font = font_load(afont ? afont : "fixed"); /* To make the alignment uniform, find maximum height */
if (!alt_font) int maxh = all_fonts[0]->height;
exit(EXIT_FAILURE); i = 0;
while (all_fonts[++i])
maxh = max(maxh, all_fonts[i]->height);
/* To make the alignment uniform */ /* Set maximum height to all fonts */
main_font->height = alt_font->height = max(main_font->height, alt_font->height); i = 0;
while (all_fonts[i])
all_fonts[i++]->height = maxh;
/* Generate a list of screens */ /* Generate a list of screens */
const xcb_query_extension_reply_t *qe_reply; const xcb_query_extension_reply_t *qe_reply;
@ -837,13 +862,15 @@ init (void)
} }
if (!monhead) { if (!monhead) {
/* TODO replace constants here and in monitor_create_chain() with #defines */
/* If I fits I sits */ /* If I fits I sits */
if (bw < 0) if (bw < 0)
bw = scr->width_in_pixels - bx; bw = scr->width_in_pixels - bx;
/* Adjust the height */ /* Adjust the height */
if (bh < 0 || bh > scr->height_in_pixels) if (bh < 0 || bh > scr->height_in_pixels)
bh = main_font->height + bu + 2; bh = maxh + bu + 2;
/* Check the geometry */ /* Check the geometry */
if (bx + bw > scr->width_in_pixels || by + bh > scr->height_in_pixels) { if (bx + bw > scr->width_in_pixels || by + bh > scr->height_in_pixels) {
@ -883,14 +910,11 @@ init (void)
void void
cleanup (void) cleanup (void)
{ {
if (main_font) { int i = 0;
xcb_close_font(c, main_font->ptr); while (all_fonts[i]) {
free(main_font); xcb_close_font(c, all_fonts[i]->ptr);
} free(all_fonts[i]);
all_fonts[i++] = NULL;
if (alt_font) {
xcb_close_font(c, alt_font->ptr);
free(alt_font);
} }
while (monhead) { while (monhead) {
@ -980,13 +1004,17 @@ parse_font_list (char *str)
return; return;
tok = strtok(str, ","); tok = strtok(str, ",");
if (tok)
mfont = tok;
tok = strtok(NULL, ",");
if (tok)
afont = tok;
return; int i = 0;
while (tok) {
if (i >= MAX_FONT_COUNT) {
fprintf(stderr, "Too many fonts; maximum %i\n", MAX_FONT_COUNT);
return;
}
all_fonts_str[i++] = tok;
tok = strtok(NULL, ",");
}
} }
int int