diff --git a/libi3/font.c b/libi3/font.c index 9cea83e4..4c064f2b 100644 --- a/libi3/font.c +++ b/libi3/font.c @@ -30,6 +30,21 @@ static double pango_font_red; static double pango_font_green; static double pango_font_blue; +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; + LOG("X11 root window dictates %f DPI\n", dpi); + pango_cairo_context_set_resolution(context, dpi); + layout = pango_layout_new(context); + g_object_unref(context); + + return layout; +} + /* * Loads a Pango font description into an i3Font structure. Returns true * on success, false otherwise. @@ -56,10 +71,7 @@ static bool load_pango_font(i3Font *font, const char *desc) { /* Create a dummy Pango layout to compute the font height */ cairo_surface_t *surface = cairo_xcb_surface_create(conn, root_screen->root, root_visual_type, 1, 1); cairo_t *cr = cairo_create(surface); - double ydpi = (double)root_screen->height_in_pixels * 25.4 / (double)root_screen->height_in_millimeters; - PangoContext *pc = pango_cairo_create_context(cr); - pango_cairo_context_set_resolution(pc, ydpi); - PangoLayout *layout = pango_layout_new(pc); + PangoLayout *layout = create_layout_with_dpi(cr); pango_layout_set_font_description(layout, font->specific.pango_desc); /* Get the font height */ @@ -69,7 +81,6 @@ static bool load_pango_font(i3Font *font, const char *desc) { /* Free resources */ g_object_unref(layout); - g_object_unref(pc); cairo_destroy(cr); cairo_surface_destroy(surface); @@ -89,10 +100,7 @@ static void draw_text_pango(const char *text, size_t text_len, cairo_surface_t *surface = cairo_xcb_surface_create(conn, drawable, root_visual_type, x + max_width, y + savedFont->height); cairo_t *cr = cairo_create(surface); - double ydpi = (double)root_screen->height_in_pixels * 25.4 / (double)root_screen->height_in_millimeters; - PangoContext *pc = pango_cairo_create_context(cr); - pango_cairo_context_set_resolution(pc, ydpi); - PangoLayout *layout = pango_layout_new(pc); + PangoLayout *layout = create_layout_with_dpi(cr); gint height; pango_layout_set_font_description(layout, savedFont->specific.pango_desc); @@ -111,7 +119,6 @@ static void draw_text_pango(const char *text, size_t text_len, /* Free resources */ g_object_unref(layout); - g_object_unref(pc); cairo_destroy(cr); cairo_surface_destroy(surface); } @@ -125,10 +132,7 @@ static int predict_text_width_pango(const char *text, size_t text_len) { /* root_visual_type is cached in load_pango_font */ cairo_surface_t *surface = cairo_xcb_surface_create(conn, root_screen->root, root_visual_type, 1, 1); cairo_t *cr = cairo_create(surface); - double ydpi = (double)root_screen->height_in_pixels * 25.4 / (double)root_screen->height_in_millimeters; - PangoContext *pc = pango_cairo_create_context(cr); - pango_cairo_context_set_resolution(pc, ydpi); - PangoLayout *layout = pango_layout_new(pc); + PangoLayout *layout = create_layout_with_dpi(cr); /* Get the font width */ gint width; @@ -139,7 +143,6 @@ static int predict_text_width_pango(const char *text, size_t text_len) { /* Free resources */ g_object_unref(layout); - g_object_unref(pc); cairo_destroy(cr); cairo_surface_destroy(surface);