i3 - improved tiling WM


libi3/font: Set DPI for the pango context

Patch status: merged

Patch by Bas Pape

Long description:

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

To apply this patch, use:
curl http://cr.i3wm.org/patch/312/raw.patch | git am

b/libi3/font.c

19
@@ -56,7 +56,10 @@ static bool load_pango_font(i3Font *font, const char *desc) {
20
     /* Create a dummy Pango layout to compute the font height */
21
     cairo_surface_t *surface = cairo_xcb_surface_create(conn, root_screen->root, root_visual_type, 1, 1);
22
     cairo_t *cr = cairo_create(surface);
23
-    PangoLayout *layout = pango_cairo_create_layout(cr);
24
+    double ydpi = (double)root_screen->height_in_pixels * 25.4 / (double)root_screen->height_in_millimeters;
25
+    PangoContext *pc = pango_cairo_create_context(cr);
26
+    pango_cairo_context_set_resolution(pc, ydpi);
27
+    PangoLayout *layout = pango_layout_new(pc);
28
     pango_layout_set_font_description(layout, font->specific.pango_desc);
29
 
30
     /* Get the font height */
31
@@ -66,6 +69,7 @@ static bool load_pango_font(i3Font *font, const char *desc) {
32
 
33
     /* Free resources */
34
     g_object_unref(layout);
35
+    g_object_unref(pc);
36
     cairo_destroy(cr);
37
     cairo_surface_destroy(surface);
38
 
39
@@ -85,7 +89,10 @@ static void draw_text_pango(const char *text, size_t text_len,
40
     cairo_surface_t *surface = cairo_xcb_surface_create(conn, drawable,
41
             root_visual_type, x + max_width, y + savedFont->height);
42
     cairo_t *cr = cairo_create(surface);
43
-    PangoLayout *layout = pango_cairo_create_layout(cr);
44
+    double ydpi = (double)root_screen->height_in_pixels * 25.4 / (double)root_screen->height_in_millimeters;
45
+    PangoContext *pc = pango_cairo_create_context(cr);
46
+    pango_cairo_context_set_resolution(pc, ydpi);
47
+    PangoLayout *layout = pango_layout_new(pc);
48
     gint height;
49
 
50
     pango_layout_set_font_description(layout, savedFont->specific.pango_desc);
51
@@ -104,6 +111,7 @@ static void draw_text_pango(const char *text, size_t text_len,
52
 
53
     /* Free resources */
54
     g_object_unref(layout);
55
+    g_object_unref(pc);
56
     cairo_destroy(cr);
57
     cairo_surface_destroy(surface);
58
 }
59
@@ -117,7 +125,10 @@ static int predict_text_width_pango(const char *text, size_t text_len) {
60
     /* root_visual_type is cached in load_pango_font */
61
     cairo_surface_t *surface = cairo_xcb_surface_create(conn, root_screen->root, root_visual_type, 1, 1);
62
     cairo_t *cr = cairo_create(surface);
63
-    PangoLayout *layout = pango_cairo_create_layout(cr);
64
+    double ydpi = (double)root_screen->height_in_pixels * 25.4 / (double)root_screen->height_in_millimeters;
65
+    PangoContext *pc = pango_cairo_create_context(cr);
66
+    pango_cairo_context_set_resolution(pc, ydpi);
67
+    PangoLayout *layout = pango_layout_new(pc);
68
 
69
     /* Get the font width */
70
     gint width;
71
@@ -128,6 +139,7 @@ static int predict_text_width_pango(const char *text, size_t text_len) {
72
 
73
     /* Free resources */
74
     g_object_unref(layout);
75
+    g_object_unref(pc);
76
     cairo_destroy(cr);
77
     cairo_surface_destroy(surface);
78