]> git.sur5r.net Git - i3/i3/commitdiff
libi3/font: Set DPI for the pango context
authorBas Pape <baspape@gmail.com>
Wed, 13 Nov 2013 19:23:35 +0000 (20:23 +0100)
committerMichael Stapelberg <michael@stapelberg.de>
Mon, 18 Nov 2013 22:12:08 +0000 (23:12 +0100)
The pango font specification accepts a font size in points, but pango
defaults to a DPI of 96. Create a default PangoContext (which
internally creates a default PangoCairoFontMap as usual) and set the
DPI to the value of the root Screen manually.

Fixes #1115

libi3/font.c

index c57009c062a686682c8f55e542cdcdbdd531d750..9cea83e42cf1aedd1ff07b7e50190dff755cc072 100644 (file)
@@ -56,7 +56,10 @@ 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);
-    PangoLayout *layout = pango_cairo_create_layout(cr);
+    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);
     pango_layout_set_font_description(layout, font->specific.pango_desc);
 
     /* Get the font height */
@@ -66,6 +69,7 @@ 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);
 
@@ -85,7 +89,10 @@ 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);
-    PangoLayout *layout = pango_cairo_create_layout(cr);
+    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);
     gint height;
 
     pango_layout_set_font_description(layout, savedFont->specific.pango_desc);
@@ -104,6 +111,7 @@ 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);
 }
@@ -117,7 +125,10 @@ 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);
-    PangoLayout *layout = pango_cairo_create_layout(cr);
+    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);
 
     /* Get the font width */
     gint width;
@@ -128,6 +139,7 @@ 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);