X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libi3%2Ffont.c;h=7be84ee07775474a1efe9b18aafaa44e04475b15;hb=50880db2c554ad73d5524ef21d376fab25129a5d;hp=9e72849acd5d5ccc298912193ce5483fa1e137f4;hpb=98875fda762142419d557e7d99dde39eaa52f4bf;p=i3%2Fi3 diff --git a/libi3/font.c b/libi3/font.c index 9e72849a..7be84ee0 100644 --- a/libi3/font.c +++ b/libi3/font.c @@ -5,6 +5,8 @@ * © 2009 Michael Stapelberg and contributors (see also: LICENSE) * */ +#include "libi3.h" + #include #include #include @@ -13,38 +15,21 @@ #include #include -#if PANGO_SUPPORT #include -#endif - -#include "libi3.h" static const i3Font *savedFont = NULL; -#if PANGO_SUPPORT static xcb_visualtype_t *root_visual_type; static double pango_font_red; static double pango_font_green; static double pango_font_blue; -/* Necessary to track whether the dpi changes and trigger a LOG() message, - * which is more easily visible to users. */ -static double logged_dpi = 0.0; - static PangoLayout *create_layout_with_dpi(cairo_t *cr) { PangoLayout *layout; PangoContext *context; context = pango_cairo_create_context(cr); - const double dpi = (double)root_screen->height_in_pixels * 25.4 / - (double)root_screen->height_in_millimeters; - if (logged_dpi != dpi) { - logged_dpi = dpi; - LOG("X11 root window dictates %f DPI\n", dpi); - } else { - DLOG("X11 root window dictates %f DPI\n", dpi); - } - pango_cairo_context_set_resolution(context, dpi); + pango_cairo_context_set_resolution(context, get_dpi_value()); layout = pango_layout_new(context); g_object_unref(context); @@ -126,7 +111,7 @@ static void draw_text_pango(const char *text, size_t text_len, pango_layout_get_pixel_size(layout, NULL, &height); /* Center the piece of text vertically if its height is smaller than the * cached font height, and just let "high" symbols fall out otherwise. */ - int yoffset = (height < savedFont->height ? 0.5 : 1) * (height - savedFont->height); + int yoffset = abs(height - savedFont->height) / 2; cairo_move_to(cr, x, y - yoffset); pango_cairo_show_layout(cr, layout); @@ -166,7 +151,6 @@ static int predict_text_width_pango(const char *text, size_t text_len, bool pang return width; } -#endif /* * Loads a font for usage, also getting its metrics. If fallback is true, @@ -187,7 +171,6 @@ i3Font load_font(const char *pattern, const bool fallback) { return font; } -#if PANGO_SUPPORT /* Try to load a pango font if specified */ if (strlen(pattern) > strlen("pango:") && !strncmp(pattern, "pango:", strlen("pango:"))) { const char *font_pattern = pattern + strlen("pango:"); @@ -202,7 +185,6 @@ i3Font load_font(const char *pattern, const bool fallback) { return font; } } -#endif /* Send all our requests first */ font.specific.xcb.id = xcb_generate_id(conn); @@ -224,6 +206,7 @@ i3Font load_font(const char *pattern, const bool fallback) { info_cookie = xcb_query_font(conn, font.specific.xcb.id); /* Check if we managed to open 'fixed' */ + free(error); error = xcb_request_check(conn, font_cookie); /* Fall back to '-misc-*' if opening 'fixed' fails. */ @@ -234,12 +217,14 @@ i3Font load_font(const char *pattern, const bool fallback) { strlen(pattern), pattern); info_cookie = xcb_query_font(conn, font.specific.xcb.id); + free(error); if ((error = xcb_request_check(conn, font_cookie)) != NULL) errx(EXIT_FAILURE, "Could open neither requested font nor fallbacks " "(fixed or -misc-*): X11 error %d", error->error_code); } } + free(error); font.pattern = sstrdup(pattern); LOG("Using X font %s\n", pattern); @@ -288,19 +273,13 @@ void free_font(void) { case FONT_TYPE_XCB: { /* Close the font and free the info */ xcb_close_font(conn, savedFont->specific.xcb.id); - if (savedFont->specific.xcb.info) - free(savedFont->specific.xcb.info); + free(savedFont->specific.xcb.info); break; } -#if PANGO_SUPPORT case FONT_TYPE_PANGO: /* Free the font description */ pango_font_description_free(savedFont->specific.pango_desc); break; -#endif - default: - assert(false); - break; } savedFont = NULL; @@ -324,17 +303,12 @@ void set_font_colors(xcb_gcontext_t gc, color_t foreground, color_t background) xcb_change_gc(conn, gc, mask, values); break; } -#if PANGO_SUPPORT case FONT_TYPE_PANGO: /* Save the foreground font */ pango_font_red = foreground.red; pango_font_green = foreground.green; pango_font_blue = foreground.blue; break; -#endif - default: - assert(false); - break; } } @@ -343,11 +317,7 @@ void set_font_colors(xcb_gcontext_t gc, color_t foreground, color_t background) * */ bool font_is_pango(void) { -#if PANGO_SUPPORT return savedFont->type == FONT_TYPE_PANGO; -#else - return false; -#endif } static int predict_text_width_xcb(const xcb_char2b_t *text, size_t text_len); @@ -404,15 +374,11 @@ void draw_text(i3String *text, xcb_drawable_t drawable, xcb_gcontext_t gc, draw_text_xcb(i3string_as_ucs2(text), i3string_get_num_glyphs(text), drawable, gc, x, y, max_width); break; -#if PANGO_SUPPORT case FONT_TYPE_PANGO: /* Render the text using Pango */ draw_text_pango(i3string_as_utf8(text), i3string_get_num_bytes(text), drawable, visual, x, y, max_width, i3string_is_markup(text)); return; -#endif - default: - assert(false); } } @@ -443,15 +409,11 @@ void draw_text_ascii(const char *text, xcb_drawable_t drawable, } break; } -#if PANGO_SUPPORT case FONT_TYPE_PANGO: /* Render the text using Pango */ draw_text_pango(text, strlen(text), drawable, root_visual_type, x, y, max_width, false); return; -#endif - default: - assert(false); } } @@ -540,14 +502,10 @@ int predict_text_width(i3String *text) { return 0; case FONT_TYPE_XCB: return predict_text_width_xcb(i3string_as_ucs2(text), i3string_get_num_glyphs(text)); -#if PANGO_SUPPORT case FONT_TYPE_PANGO: /* Calculate extents using Pango */ return predict_text_width_pango(i3string_as_utf8(text), i3string_get_num_bytes(text), i3string_is_markup(text)); -#endif - default: - assert(false); - return 0; } + assert(false); }