]> git.sur5r.net Git - i3/i3/blobdiff - libi3/font.c
Ensure all *.[ch] files include config.h
[i3/i3] / libi3 / font.c
index cb92d52fbd29bc8281cb9beabcc269f2c5fca93b..fa8484813b472c3b9a28c7473da9f229effb2bb4 100644 (file)
@@ -2,9 +2,11 @@
  * 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 "libi3.h"
+
 #include <assert.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <stdbool.h>
 #include <err.h>
 
-#if PANGO_SUPPORT
 #include <cairo/cairo-xcb.h>
 #include <pango/pangocairo.h>
-#endif
-
-#include "libi3.h"
-
-extern xcb_connection_t *conn;
-extern xcb_screen_t *root_screen;
 
 static const i3Font *savedFont = NULL;
 
-#if PANGO_SUPPORT
 static xcb_visualtype_t *root_visual_type;
 static double pango_font_red;
 static double pango_font_green;
@@ -102,12 +96,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,
-                            int max_width, bool is_markup) {
+                            xcb_drawable_t drawable, xcb_visualtype_t *visual, int x, int y,
+                            int max_width, bool pango_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;
@@ -117,16 +111,20 @@ static void draw_text_pango(const char *text, size_t text_len,
     pango_layout_set_wrap(layout, PANGO_WRAP_CHAR);
     pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
 
-    if (is_markup)
+    if (pango_markup)
         pango_layout_set_markup(layout, text, text_len);
     else
         pango_layout_set_text(layout, text, text_len);
 
     /* Do the drawing */
+    cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
     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 */
@@ -139,7 +137,7 @@ static void draw_text_pango(const char *text, size_t text_len,
  * Calculate the text width using Pango rendering.
  *
  */
-static int predict_text_width_pango(const char *text, size_t text_len, bool is_markup) {
+static int predict_text_width_pango(const char *text, size_t text_len, bool pango_markup) {
     /* Create a dummy Pango layout */
     /* 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);
@@ -150,7 +148,7 @@ static int predict_text_width_pango(const char *text, size_t text_len, bool is_m
     gint width;
     pango_layout_set_font_description(layout, savedFont->specific.pango_desc);
 
-    if (is_markup)
+    if (pango_markup)
         pango_layout_set_markup(layout, text, text_len);
     else
         pango_layout_set_text(layout, text, text_len);
@@ -165,7 +163,6 @@ static int predict_text_width_pango(const char *text, size_t text_len, bool is_m
 
     return width;
 }
-#endif
 
 /*
  * Loads a font for usage, also getting its metrics. If fallback is true,
@@ -186,7 +183,6 @@ i3Font load_font(const char *pattern, const bool fallback) {
         return font;
     }
 
-#if PANGO_SUPPORT
     /* Try to load a pango font if specified */
     if (strlen(pattern) > strlen("pango:") && !strncmp(pattern, "pango:", strlen("pango:"))) {
         const char *font_pattern = pattern + strlen("pango:");
@@ -201,7 +197,6 @@ i3Font load_font(const char *pattern, const bool fallback) {
             return font;
         }
     }
-#endif
 
     /* Send all our requests first */
     font.specific.xcb.id = xcb_generate_id(conn);
@@ -223,6 +218,7 @@ i3Font load_font(const char *pattern, const bool fallback) {
         info_cookie = xcb_query_font(conn, font.specific.xcb.id);
 
         /* Check if we managed to open 'fixed' */
+        free(error);
         error = xcb_request_check(conn, font_cookie);
 
         /* Fall back to '-misc-*' if opening 'fixed' fails. */
@@ -233,12 +229,16 @@ i3Font load_font(const char *pattern, const bool fallback) {
                                                 strlen(pattern), pattern);
             info_cookie = xcb_query_font(conn, font.specific.xcb.id);
 
+            free(error);
             if ((error = xcb_request_check(conn, font_cookie)) != NULL)
                 errx(EXIT_FAILURE, "Could open neither requested font nor fallbacks "
                                    "(fixed or -misc-*): X11 error %d",
                      error->error_code);
         }
     }
+    if (error != NULL) {
+        free(error);
+    }
 
     font.pattern = sstrdup(pattern);
     LOG("Using X font %s\n", pattern);
@@ -291,12 +291,10 @@ void free_font(void) {
                 free(savedFont->specific.xcb.info);
             break;
         }
-#if PANGO_SUPPORT
         case FONT_TYPE_PANGO:
             /* Free the font description */
             pango_font_description_free(savedFont->specific.pango_desc);
             break;
-#endif
         default:
             assert(false);
             break;
@@ -309,7 +307,7 @@ void free_font(void) {
  * Defines the colors to be used for the forthcoming draw_text calls.
  *
  */
-void set_font_colors(xcb_gcontext_t gc, uint32_t foreground, uint32_t background) {
+void set_font_colors(xcb_gcontext_t gc, color_t foreground, color_t background) {
     assert(savedFont != NULL);
 
     switch (savedFont->type) {
@@ -319,24 +317,30 @@ void set_font_colors(xcb_gcontext_t gc, uint32_t foreground, uint32_t background
         case FONT_TYPE_XCB: {
             /* Change the font and colors in the GC */
             uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
-            uint32_t values[] = {foreground, background, savedFont->specific.xcb.id};
+            uint32_t values[] = {foreground.colorpixel, background.colorpixel, savedFont->specific.xcb.id};
             xcb_change_gc(conn, gc, mask, values);
             break;
         }
-#if PANGO_SUPPORT
         case FONT_TYPE_PANGO:
             /* Save the foreground font */
-            pango_font_red = ((foreground >> 16) & 0xff) / 255.0;
-            pango_font_green = ((foreground >> 8) & 0xff) / 255.0;
-            pango_font_blue = (foreground & 0xff) / 255.0;
+            pango_font_red = foreground.red;
+            pango_font_green = foreground.green;
+            pango_font_blue = foreground.blue;
             break;
-#endif
         default:
             assert(false);
             break;
     }
 }
 
+/*
+ * Returns true if and only if the current font is a pango font.
+ *
+ */
+bool font_is_pango(void) {
+    return savedFont->type == FONT_TYPE_PANGO;
+}
+
 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 +380,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:
@@ -388,13 +395,11 @@ void draw_text(i3String *text, xcb_drawable_t drawable,
             draw_text_xcb(i3string_as_ucs2(text), i3string_get_num_glyphs(text),
                           drawable, gc, x, y, max_width);
             break;
-#if PANGO_SUPPORT
         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:
             assert(false);
     }
@@ -417,7 +422,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 */
@@ -427,13 +432,11 @@ void draw_text_ascii(const char *text, xcb_drawable_t drawable,
             }
             break;
         }
-#if PANGO_SUPPORT
         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:
             assert(false);
     }
@@ -524,12 +527,10 @@ int predict_text_width(i3String *text) {
             return 0;
         case FONT_TYPE_XCB:
             return predict_text_width_xcb(i3string_as_ucs2(text), i3string_get_num_glyphs(text));
-#if PANGO_SUPPORT
         case FONT_TYPE_PANGO:
             /* Calculate extents using Pango */
             return predict_text_width_pango(i3string_as_utf8(text), i3string_get_num_bytes(text),
                                             i3string_is_markup(text));
-#endif
         default:
             assert(false);
             return 0;