]> git.sur5r.net Git - i3/i3/blobdiff - libi3/font.c
libi3/font: Draw the text at the expected place
[i3/i3] / libi3 / font.c
index a2162c47cea1e0b55fd7a0b287d56157e44f0a44..c57009c062a686682c8f55e542cdcdbdd531d750 100644 (file)
@@ -2,7 +2,7 @@
  * vim:ts=4:sw=4:expandtab
  *
  * i3 - an improved dynamic tiling window manager
- * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
+ * © 2009-2013 Michael Stapelberg and contributors (see also: LICENSE)
  *
  */
 #include <assert.h>
@@ -86,16 +86,20 @@ static void draw_text_pango(const char *text, size_t text_len,
             root_visual_type, x + max_width, y + savedFont->height);
     cairo_t *cr = cairo_create(surface);
     PangoLayout *layout = pango_cairo_create_layout(cr);
+    gint height;
+
     pango_layout_set_font_description(layout, savedFont->specific.pango_desc);
     pango_layout_set_width(layout, max_width * PANGO_SCALE);
     pango_layout_set_wrap(layout, PANGO_WRAP_CHAR);
     pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
 
+    pango_layout_set_text(layout, text, text_len);
+
     /* Do the drawing */
     cairo_set_source_rgb(cr, pango_font_red, pango_font_green, pango_font_blue);
-    cairo_move_to(cr, x, y);
-    pango_layout_set_text(layout, text, text_len);
     pango_cairo_update_layout(cr, layout);
+    pango_layout_get_pixel_size(layout, NULL, &height);
+    cairo_move_to(cr, x, y - (height - savedFont->height));
     pango_cairo_show_layout(cr, layout);
 
     /* Free resources */
@@ -143,13 +147,17 @@ i3Font load_font(const char *pattern, const bool fallback) {
 #if PANGO_SUPPORT
     /* Try to load a pango font if specified */
     if (strlen(pattern) > strlen("pango:") && !strncmp(pattern, "pango:", strlen("pango:"))) {
-        pattern += strlen("pango:");
-        if (load_pango_font(&font, pattern))
+        const char *font_pattern = pattern + strlen("pango:");
+        if (load_pango_font(&font, font_pattern)) {
+            font.pattern = sstrdup(pattern);
             return font;
+        }
     } else if (strlen(pattern) > strlen("xft:") && !strncmp(pattern, "xft:", strlen("xft:"))) {
-        pattern += strlen("xft:");
-        if (load_pango_font(&font, pattern))
+        const char *font_pattern = pattern + strlen("xft:");
+        if (load_pango_font(&font, font_pattern)) {
+            font.pattern = sstrdup(pattern);
             return font;
+        }
     }
 #endif
 
@@ -189,6 +197,7 @@ i3Font load_font(const char *pattern, const bool fallback) {
         }
     }
 
+    font.pattern = sstrdup(pattern);
     LOG("Using X font %s\n", pattern);
 
     /* Get information (height/name) for this font */
@@ -222,6 +231,7 @@ void set_font(i3Font *font) {
  *
  */
 void free_font(void) {
+    free(savedFont->pattern);
     switch (savedFont->type) {
         case FONT_TYPE_NONE:
             /* Nothing to do */