Add libi3/load_font, use it everywhere
…except for i3bar, which needs slightly more information about the font
This commit is contained in:
@ -59,11 +59,9 @@ enum { MOD_Mod1, MOD_Mod4 } modifier = MOD_Mod4;
|
||||
static char *config_path;
|
||||
xcb_connection_t *conn;
|
||||
static xcb_get_modifier_mapping_reply_t *modmap_reply;
|
||||
static uint32_t font_id;
|
||||
static uint32_t font_bold_id;
|
||||
static i3Font font;
|
||||
static i3Font bold_font;
|
||||
static char *socket_path;
|
||||
static int font_height;
|
||||
static int font_bold_height;
|
||||
static xcb_window_t win;
|
||||
static xcb_pixmap_t pixmap;
|
||||
static xcb_gcontext_t pixmap_gc;
|
||||
@ -112,13 +110,13 @@ static char *resolve_tilde(const char *path) {
|
||||
*/
|
||||
static int handle_expose() {
|
||||
/* re-draw the background */
|
||||
xcb_rectangle_t border = {0, 0, 300, (15*font_height) + 8};
|
||||
xcb_rectangle_t border = {0, 0, 300, (15 * font.height) + 8};
|
||||
xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel("#000000") });
|
||||
xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
|
||||
|
||||
xcb_change_gc(conn, pixmap_gc, XCB_GC_FONT, (uint32_t[]){ font_id });
|
||||
xcb_change_gc(conn, pixmap_gc, XCB_GC_FONT, (uint32_t[]){ font.id });
|
||||
|
||||
#define txt(x, row, text) xcb_image_text_8(conn, strlen(text), pixmap, pixmap_gc, x, (row * font_height) + 2, text)
|
||||
#define txt(x, row, text) xcb_image_text_8(conn, strlen(text), pixmap, pixmap_gc, x, (row * font.height) + 2, text)
|
||||
|
||||
if (current_step == STEP_WELCOME) {
|
||||
/* restore font color */
|
||||
@ -154,14 +152,14 @@ static int handle_expose() {
|
||||
else txt(31, 4, "<Win>");
|
||||
|
||||
/* the selected modifier */
|
||||
xcb_change_gc(conn, pixmap_gc, XCB_GC_FONT, (uint32_t[]){ font_bold_id });
|
||||
xcb_change_gc(conn, pixmap_gc, XCB_GC_FONT, (uint32_t[]){ bold_font.id });
|
||||
if (modifier == MOD_Mod4)
|
||||
txt(31, 4, "<Win>");
|
||||
else txt(31, 5, "<Alt>");
|
||||
|
||||
/* green */
|
||||
xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND | XCB_GC_FONT,
|
||||
(uint32_t[]) { get_colorpixel("#00FF00"), font_id });
|
||||
(uint32_t[]) { get_colorpixel("#00FF00"), font.id });
|
||||
|
||||
txt(25, 9, "<Enter>");
|
||||
|
||||
@ -440,8 +438,8 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
xcb_numlock_mask = get_mod_mask_for(XCB_NUM_LOCK, symbols, modmap_reply);
|
||||
|
||||
font_id = get_font_id(conn, pattern, &font_height);
|
||||
font_bold_id = get_font_id(conn, patternbold, &font_bold_height);
|
||||
font = load_font(pattern, true);
|
||||
bold_font = load_font(patternbold, true);
|
||||
|
||||
/* Open an input window */
|
||||
win = open_input_window(conn, 300, 205);
|
||||
|
@ -73,34 +73,3 @@ xcb_window_t open_input_window(xcb_connection_t *conn, uint32_t width, uint32_t
|
||||
|
||||
return win;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the ID of the font matching the given pattern and stores the height
|
||||
* of the font (in pixels) in *font_height. die()s if no font matches.
|
||||
*
|
||||
*/
|
||||
int get_font_id(xcb_connection_t *conn, char *pattern, int *font_height) {
|
||||
xcb_void_cookie_t font_cookie;
|
||||
xcb_list_fonts_with_info_cookie_t info_cookie;
|
||||
|
||||
/* Send all our requests first */
|
||||
int result;
|
||||
result = xcb_generate_id(conn);
|
||||
font_cookie = xcb_open_font_checked(conn, result, strlen(pattern), pattern);
|
||||
info_cookie = xcb_list_fonts_with_info(conn, 1, strlen(pattern), pattern);
|
||||
|
||||
xcb_generic_error_t *error = xcb_request_check(conn, font_cookie);
|
||||
if (error != NULL) {
|
||||
fprintf(stderr, "ERROR: Could not open font: %d\n", error->error_code);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Get information (height/name) for this font */
|
||||
xcb_list_fonts_with_info_reply_t *reply = xcb_list_fonts_with_info_reply(conn, info_cookie, NULL);
|
||||
if (reply == NULL)
|
||||
errx(1, "Could not load font \"%s\"\n", pattern);
|
||||
|
||||
*font_height = reply->font_ascent + reply->font_descent;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -11,6 +11,5 @@
|
||||
extern unsigned int xcb_numlock_mask;
|
||||
|
||||
xcb_window_t open_input_window(xcb_connection_t *conn, uint32_t width, uint32_t height);
|
||||
int get_font_id(xcb_connection_t *conn, char *pattern, int *font_height);
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user