]> git.sur5r.net Git - i3/i3/commitdiff
libi3: Implement Pango rendering
authorFernando Tarlá Cardoso Lemos <fernandotcl@gmail.com>
Sun, 5 Aug 2012 19:36:49 +0000 (21:36 +0200)
committerQuentin Glidic <sardemff7+git@sardemff7.net>
Mon, 13 Aug 2012 09:39:30 +0000 (11:39 +0200)
14 files changed:
i3-config-wizard/i3-config-wizard.mk
i3-config-wizard/main.c
i3-dump-log/i3-dump-log.mk
i3-input/i3-input.mk
i3-input/main.c
i3-msg/i3-msg.mk
i3-nagbar/i3-nagbar.mk
i3-nagbar/main.c
i3bar/i3bar.mk
i3bar/include/util.h
include/libi3.h
libi3/font.c
libi3/libi3.mk
src/i3.mk

index 526a4ecdade8cd7b47ad343f6057765c771ec2f9..7e9c4bee116b97adc9f3ee15fe2a3e6e114a1720 100644 (file)
@@ -5,8 +5,8 @@ CLEAN_TARGETS += clean-i3-config-wizard
 i3_config_wizard_SOURCES_GENERATED  = i3-config-wizard/cfgparse.tab.c i3-config-wizard/cfgparse.yy.c
 i3_config_wizard_SOURCES           := $(filter-out $(i3_config_wizard_SOURCES_GENERATED),$(wildcard i3-config-wizard/*.c))
 i3_config_wizard_HEADERS           := $(wildcard i3-config-wizard/*.h)
-i3_config_wizard_CFLAGS             = $(XCB_CFLAGS) $(XCB_KBD_CFLAGS) $(X11_CFLAGS)
-i3_config_wizard_LIBS               = $(XCB_LIBS) $(XCB_KBD_LIBS) $(X11_LIBS)
+i3_config_wizard_CFLAGS             = $(XCB_CFLAGS) $(XCB_KBD_CFLAGS) $(X11_CFLAGS) $(PANGO_CFLAGS)
+i3_config_wizard_LIBS               = $(XCB_LIBS) $(XCB_KBD_LIBS) $(X11_LIBS) $(PANGO_LIBS)
 
 i3_config_wizard_OBJECTS := $(i3_config_wizard_SOURCES_GENERATED:.c=.o) $(i3_config_wizard_SOURCES:.c=.o)
 
index 4c04a6d2662e8424f3146aec6174be9b74ed03e0..46cf8aa8d9e8d2da8fc3e163829dafa914133795 100644 (file)
@@ -69,6 +69,7 @@ enum { MOD_Mod1, MOD_Mod4 } modifier = MOD_Mod4;
 static char *config_path;
 static uint32_t xcb_numlock_mask;
 xcb_connection_t *conn;
+xcb_screen_t *root_screen;
 static xcb_get_modifier_mapping_reply_t *modmap_reply;
 static i3Font font;
 static i3Font bold_font;
@@ -456,7 +457,7 @@ int main(int argc, char *argv[]) {
     #include "atoms.xmacro"
     #undef xmacro
 
-    xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
+    root_screen = xcb_aux_get_screen(conn, screens);
     root = root_screen->root;
 
     if (!(modmap_reply = xcb_get_modifier_mapping_reply(conn, modmap_cookie, NULL)))
index 17e53a6d234aafdf409e601eda8ccc2b7f2485fe..e696546d32da9184ee2fcdd0d999a34b3d3a2180 100644 (file)
@@ -4,7 +4,7 @@ CLEAN_TARGETS += clean-i3-dump-log
 
 i3_dump_log_SOURCES := $(wildcard i3-dump-log/*.c)
 i3_dump_log_HEADERS := $(wildcard i3-dump-log/*.h)
-i3_dump_log_CFLAGS   = $(XCB_CFLAGS)
+i3_dump_log_CFLAGS   = $(XCB_CFLAGS) $(PANGO_CFLAGS)
 i3_dump_log_LIBS     = $(XCB_LIBS)
 
 i3_dump_log_OBJECTS := $(i3_dump_log_SOURCES:.c=.o)
index 98131e76e099a2a9f42ed8f5f93b603a1679866a..d31a11fffc4d562848318182d4dab90d8ae7fa5b 100644 (file)
@@ -4,8 +4,8 @@ CLEAN_TARGETS += clean-i3-input
 
 i3_input_SOURCES := $(wildcard i3-input/*.c)
 i3_input_HEADERS := $(wildcard i3-input/*.h)
-i3_input_CFLAGS   = $(XCB_CFLAGS) $(XCB_KBD_CFLAGS)
-i3_input_LIBS     = $(XCB_LIBS) $(XCB_KBD_LIBS)
+i3_input_CFLAGS   = $(XCB_CFLAGS) $(XCB_KBD_CFLAGS) $(PANGO_CFLAGS)
+i3_input_LIBS     = $(XCB_LIBS) $(XCB_KBD_LIBS) $(PANGO_LIBS)
 
 i3_input_OBJECTS := $(i3_input_SOURCES:.c=.o)
 
index 2de5a41e39e685376de803497f34f837ff0fd375..d18502cdc8058857eded7f05e5b64342056f273e 100644 (file)
@@ -54,6 +54,7 @@ static int prompt_offset = 0;
 static int limit;
 xcb_window_t root;
 xcb_connection_t *conn;
+xcb_screen_t *root_screen;
 
 /*
  * Concats the glyphs (either UCS-2 or UTF-8) to a single string, suitable for
@@ -340,7 +341,7 @@ int main(int argc, char *argv[]) {
     if (!conn || xcb_connection_has_error(conn))
         die("Cannot open display\n");
 
-    xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
+    root_screen = xcb_aux_get_screen(conn, screens);
     root = root_screen->root;
 
     symbols = xcb_key_symbols_alloc(conn);
index 01d5fc7e9790c881423151b7f437cd5f06f169e5..c3a59303974eb0e5c4a2b1b9b1ac35e7bdb10f44 100644 (file)
@@ -4,7 +4,7 @@ CLEAN_TARGETS += clean-i3-msg
 
 i3_msg_SOURCES := $(wildcard i3-msg/*.c)
 i3_msg_HEADERS := $(wildcard i3-msg/*.h)
-i3_msg_CFLAGS   = $(XCB_CFLAGS)
+i3_msg_CFLAGS   = $(XCB_CFLAGS) $(PANGO_CFLAGS)
 i3_msg_LIBS     = $(XCB_LIBS)
 
 i3_msg_OBJECTS := $(i3_msg_SOURCES:.c=.o)
index 5e5f2c4d165fc2459be1cab285966937fa4cf726..4fea16299ea7be2289770ea8e1f36146437f2cbf 100644 (file)
@@ -4,8 +4,8 @@ CLEAN_TARGETS += clean-i3-nagbar
 
 i3_nagbar_SOURCES := $(wildcard i3-nagbar/*.c)
 i3_nagbar_HEADERS := $(wildcard i3-nagbar/*.h)
-i3_nagbar_CFLAGS   = $(XCB_CFLAGS)
-i3_nagbar_LIBS     = $(XCB_LIBS)
+i3_nagbar_CFLAGS   = $(XCB_CFLAGS) $(PANGO_CFLAGS)
+i3_nagbar_LIBS     = $(XCB_LIBS) $(PANGO_LIBS)
 
 i3_nagbar_OBJECTS := $(i3_nagbar_SOURCES:.c=.o)
 
index 26a282f6521167c3d562723b965e74adc6e9da61..1588c3562065806388f8575517e7f607ccd0b3dc 100644 (file)
@@ -54,6 +54,7 @@ static uint32_t color_text;              /* color of the text */
 
 xcb_window_t root;
 xcb_connection_t *conn;
+xcb_screen_t *root_screen;
 
 /*
  * Starts the given application by passing it through a shell. We use double fork
@@ -280,7 +281,7 @@ int main(int argc, char *argv[]) {
     #include "atoms.xmacro"
     #undef xmacro
 
-    xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
+    root_screen = xcb_aux_get_screen(conn, screens);
     root = root_screen->root;
 
     if (bar_type == TYPE_ERROR) {
index 5b4ddd807a0375e27e20121a7fbd2975e231166d..c311adfe8797221ca75c3b9865083c1b2376dd0b 100644 (file)
@@ -4,8 +4,8 @@ CLEAN_TARGETS += clean-i3bar
 
 i3bar_SOURCES := $(wildcard i3bar/src/*.c)
 i3bar_HEADERS := $(wildcard i3bar/include/*.h)
-i3bar_CFLAGS   = $(XCB_CFLAGS) $(X11_CFLAGS) $(YAJL_CFLAGS) $(LIBEV_CFLAGS)
-i3bar_LIBS     = $(XCB_LIBS) $(X11_LIBS) $(YAJL_LIBS) $(LIBEV_LIBS)
+i3bar_CFLAGS   = $(XCB_CFLAGS) $(X11_CFLAGS) $(PANGO_CFLAGS) $(YAJL_CFLAGS) $(LIBEV_CFLAGS)
+i3bar_LIBS     = $(XCB_LIBS) $(X11_LIBS) $(PANGO_LIBS) $(YAJL_LIBS) $(LIBEV_LIBS)
 
 i3bar_OBJECTS := $(i3bar_SOURCES:.c=.o)
 
index 262d7750c80bc7c6ef6c70363e116d50b81e4bcb..43c56c58d3e93794d6f56becc86ae128a73d84bb 100644 (file)
@@ -11,7 +11,9 @@
 #include "queue.h"
 
 /* Get the maximum/minimum of x and y */
+#undef MAX
 #define MAX(x,y) ((x) > (y) ? (x) : (y))
+#undef MIN
 #define MIN(x,y) ((x) < (y) ? (x) : (y))
 
 /* Securely free p */
index 2c46815621c4ebfc4a05c394d6bb418b7cbc9281..7b58b96f947903f56e2f00448f2c7e773c4ab5f0 100644 (file)
 #include <xcb/xproto.h>
 #include <xcb/xcb_keysyms.h>
 
+#if PANGO_SUPPORT
+#include <pango/pango.h>
+#endif
+
 /**
  * Opaque data structure for storing strings.
  *
@@ -54,6 +58,11 @@ struct Font {
             /** Font table for this font (may be NULL) */
             xcb_charinfo_t *table;
         } xcb;
+
+#if PANGO_SUPPORT
+        /** The pango font description */
+        PangoFontDescription *pango_desc;
+#endif
     } specific;
 };
 
index 7f8c7bb1e7e4c6ff1b370b04102ff2bfc9713f3a..d9683e547dcf9e9118496469011d9ad5f88b2bd2 100644 (file)
 #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;
+static double pango_font_blue;
+
+/*
+ * Loads a Pango font description into an i3Font structure. Returns true
+ * on success, false otherwise.
+ *
+ */
+static bool load_pango_font(i3Font *font, const char *desc) {
+    /* Load the font description */
+    font->specific.pango_desc = pango_font_description_from_string(desc);
+    if (!font->specific.pango_desc)
+        return false;
+
+    /* We cache root_visual_type here, since you must call
+     * load_pango_font before any other pango function
+     * that would need root_visual_type */
+    root_visual_type = get_visualtype(root_screen);
+
+    /* 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);
+    pango_layout_set_font_description(layout, font->specific.pango_desc);
+
+    /* Get the font height */
+    gint height;
+    pango_layout_get_pixel_size(layout, NULL, &height);
+    font->height = height;
+
+    /* Free resources */
+    g_object_unref(layout);
+    cairo_destroy(cr);
+    cairo_surface_destroy(surface);
+
+    /* Set the font type and return successfully */
+    font->type = FONT_TYPE_PANGO;
+    return true;
+}
+
+/*
+ * Draws text using Pango rendering.
+ *
+ */
+static void draw_text_pango(const char *text, size_t text_len,
+        xcb_drawable_t drawable, int x, int y, int max_width) {
+    /* 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);
+    cairo_t *cr = cairo_create(surface);
+    PangoLayout *layout = pango_cairo_create_layout(cr);
+    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);
+
+    /* 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_cairo_show_layout(cr, layout);
+
+    /* Free resources */
+    g_object_unref(layout);
+    cairo_destroy(cr);
+    cairo_surface_destroy(surface);
+}
+
+/*
+ * Calculate the text width using Pango rendering.
+ *
+ */
+static int predict_text_width_pango(const char *text, size_t text_len) {
+    /* 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);
+    cairo_t *cr = cairo_create(surface);
+    PangoLayout *layout = pango_cairo_create_layout(cr);
+
+    /* Get the font width */
+    gint width;
+    pango_layout_set_font_description(layout, savedFont->specific.pango_desc);
+    pango_layout_set_text(layout, text, text_len);
+    pango_cairo_update_layout(cr, layout);
+    pango_layout_get_pixel_size(layout, &width, NULL);
+
+    /* Free resources */
+    g_object_unref(layout);
+    cairo_destroy(cr);
+    cairo_surface_destroy(surface);
+
+    return width;
+}
+#endif
+
 /*
  * Loads a font for usage, also getting its metrics. If fallback is true,
  * the fonts 'fixed' or '-misc-*' will be loaded instead of exiting.
@@ -26,6 +133,14 @@ i3Font load_font(const char *pattern, const bool fallback) {
     i3Font font;
     font.type = FONT_TYPE_NONE;
 
+#if PANGO_SUPPORT
+    /* Try to load a pango font if specified */
+    if (strlen(pattern) > strlen("xft:") && !strncmp(pattern, "xft:", strlen("xft:"))) {
+        pattern += strlen("xft:");
+        if (load_pango_font(&font, pattern))
+            return font;
+    }
+#endif
 
     /* Send all our requests first */
     font.specific.xcb.id = xcb_generate_id(conn);
@@ -105,6 +220,12 @@ 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;
@@ -129,6 +250,14 @@ void set_font_colors(xcb_gcontext_t gc, uint32_t foreground, uint32_t background
             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;
+            break;
+#endif
         default:
             assert(false);
             break;
@@ -186,6 +315,13 @@ 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);
+            return;
+#endif
         default:
             assert(false);
     }
@@ -219,6 +355,13 @@ 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);
+            return;
+#endif
         default:
             assert(false);
     }
@@ -309,6 +452,11 @@ 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));
+#endif
         default:
             assert(false);
             return 0;
index 6f6bf506ba92d7978c1752b661b87cc4c1e56793..b6a90995a78de421ad128e6bf118bda3509a74d6 100644 (file)
@@ -2,7 +2,7 @@ CLEAN_TARGETS += clean-libi3
 
 libi3_SOURCES := $(wildcard libi3/*.c)
 libi3_HEADERS := $(wildcard libi3/*.h)
-libi3_CFLAGS   =
+libi3_CFLAGS   = $(PANGO_CFLAGS)
 libi3_LIBS     =
 
 libi3_OBJECTS := $(libi3_SOURCES:.c=.o)
index 9591e178ebc238710942eb2fd7263ec2483b2456..bc9eabe81f40e4b17b461de3cfdbe384111833d7 100644 (file)
--- a/src/i3.mk
+++ b/src/i3.mk
@@ -6,8 +6,8 @@ i3_SOURCES_GENERATED  = src/cfgparse.tab.c src/cfgparse.yy.c
 i3_SOURCES           := $(filter-out $(i3_SOURCES_GENERATED),$(wildcard src/*.c))
 i3_HEADERS_CMDPARSER := $(wildcard include/GENERATED_*.h)
 i3_HEADERS           := $(filter-out $(i3_HEADERS_CMDPARSER),$(wildcard include/*.h))
-i3_CFLAGS             = $(XCB_CFLAGS) $(XCB_KBD_CFLAGS) $(XCB_WM_CFLAGS) $(X11_CFLAGS) $(XCURSOR_CFLAGS) $(YAJL_CFLAGS) $(LIBEV_CFLAGS) $(PCRE_CFLAGS) $(LIBSN_CFLAGS)
-i3_LIBS               = $(XCB_LIBS) $(XCB_KBD_LIBS) $(XCB_WM_LIBS) $(X11_LIBS) $(XCURSOR_LIBS) $(YAJL_LIBS) $(LIBEV_LIBS) $(PCRE_LIBS) $(LIBSN_LIBS) -lm -lpthread
+i3_CFLAGS             = $(XCB_CFLAGS) $(XCB_KBD_CFLAGS) $(XCB_WM_CFLAGS) $(X11_CFLAGS) $(XCURSOR_CFLAGS) $(PANGO_CFLAGS) $(YAJL_CFLAGS) $(LIBEV_CFLAGS) $(PCRE_CFLAGS) $(LIBSN_CFLAGS)
+i3_LIBS               = $(XCB_LIBS) $(XCB_KBD_LIBS) $(XCB_WM_LIBS) $(X11_LIBS) $(XCURSOR_LIBS) $(PANGO_LIBS) $(YAJL_LIBS) $(LIBEV_LIBS) $(PCRE_LIBS) $(LIBSN_LIBS) -lm -lpthread
 
 # When using clang, we use pre-compiled headers to speed up the build. With
 # gcc, this actually makes the build slower.