]> git.sur5r.net Git - i3/i3/blobdiff - libi3/font.c
Use 32-bit visuals for i3bar when possible and allow RGBA colors.
[i3/i3] / libi3 / font.c
index cb92d52fbd29bc8281cb9beabcc269f2c5fca93b..8bdf3d600fd0ab95210012dd26d324413a05d553 100644 (file)
@@ -2,7 +2,7 @@
  * vim:ts=4:sw=4:expandtab
  *
  * i3 - an improved dynamic tiling window manager
- * © 2009-2013 Michael Stapelberg and contributors (see also: LICENSE)
+ * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
  *
  */
 #include <assert.h>
@@ -12,8 +12,8 @@
 #include <stdbool.h>
 #include <err.h>
 
-#if PANGO_SUPPORT
 #include <cairo/cairo-xcb.h>
+#if PANGO_SUPPORT
 #include <pango/pangocairo.h>
 #endif
 
@@ -102,12 +102,12 @@ static bool load_pango_font(i3Font *font, const char *desc) {
  *
  */
 static void draw_text_pango(const char *text, size_t text_len,
-                            xcb_drawable_t drawable, int x, int y,
+                            xcb_drawable_t drawable, xcb_visualtype_t *visual, int x, int y,
                             int max_width, bool is_markup) {
     /* Create the Pango layout */
     /* root_visual_type is cached in load_pango_font */
     cairo_surface_t *surface = cairo_xcb_surface_create(conn, drawable,
-                                                        root_visual_type, x + max_width, y + savedFont->height);
+                                                        visual, x + max_width, y + savedFont->height);
     cairo_t *cr = cairo_create(surface);
     PangoLayout *layout = create_layout_with_dpi(cr);
     gint height;
@@ -126,7 +126,10 @@ static void draw_text_pango(const char *text, size_t text_len,
     cairo_set_source_rgb(cr, pango_font_red, pango_font_green, pango_font_blue);
     pango_cairo_update_layout(cr, layout);
     pango_layout_get_pixel_size(layout, NULL, &height);
-    cairo_move_to(cr, x, y - 0.5 * (height - savedFont->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);
+    cairo_move_to(cr, x, y - yoffset);
     pango_cairo_show_layout(cr, layout);
 
     /* Free resources */
@@ -337,6 +340,18 @@ void set_font_colors(xcb_gcontext_t gc, uint32_t foreground, uint32_t background
     }
 }
 
+/*
+ * Returns true if and only if the current font is a pango font.
+ *
+ */
+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);
 
 static void draw_text_xcb(const xcb_char2b_t *text, size_t text_len, xcb_drawable_t drawable,
@@ -376,9 +391,12 @@ static void draw_text_xcb(const xcb_char2b_t *text, size_t text_len, xcb_drawabl
  * Text must be specified as an i3String.
  *
  */
-void draw_text(i3String *text, xcb_drawable_t drawable,
-               xcb_gcontext_t gc, int x, int y, int max_width) {
+void draw_text(i3String *text, xcb_drawable_t drawable, xcb_gcontext_t gc,
+               xcb_visualtype_t *visual, int x, int y, int max_width) {
     assert(savedFont != NULL);
+    if (visual == NULL) {
+        visual = root_visual_type;
+    }
 
     switch (savedFont->type) {
         case FONT_TYPE_NONE:
@@ -392,7 +410,7 @@ void draw_text(i3String *text, xcb_drawable_t drawable,
         case FONT_TYPE_PANGO:
             /* Render the text using Pango */
             draw_text_pango(i3string_as_utf8(text), i3string_get_num_bytes(text),
-                            drawable, x, y, max_width, i3string_is_markup(text));
+                            drawable, visual, x, y, max_width, i3string_is_markup(text));
             return;
 #endif
         default:
@@ -417,7 +435,7 @@ void draw_text_ascii(const char *text, xcb_drawable_t drawable,
             if (text_len > 255) {
                 /* The text is too long to draw it directly to X */
                 i3String *str = i3string_from_utf8(text);
-                draw_text(str, drawable, gc, x, y, max_width);
+                draw_text(str, drawable, gc, NULL, x, y, max_width);
                 i3string_free(str);
             } else {
                 /* X11 coordinates for fonts start at the baseline */
@@ -431,7 +449,7 @@ void draw_text_ascii(const char *text, xcb_drawable_t drawable,
         case FONT_TYPE_PANGO:
             /* Render the text using Pango */
             draw_text_pango(text, strlen(text),
-                            drawable, x, y, max_width, false);
+                            drawable, root_visual_type, x, y, max_width, false);
             return;
 #endif
         default: