libi3: Implement Pango rendering

This commit is contained in:
Fernando Tarlá Cardoso Lemos 2012-08-05 21:36:49 +02:00 committed by Quentin Glidic
parent f06161b58a
commit 6ff3f7abad
14 changed files with 178 additions and 16 deletions

View 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_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_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_HEADERS := $(wildcard i3-config-wizard/*.h)
i3_config_wizard_CFLAGS = $(XCB_CFLAGS) $(XCB_KBD_CFLAGS) $(X11_CFLAGS) i3_config_wizard_CFLAGS = $(XCB_CFLAGS) $(XCB_KBD_CFLAGS) $(X11_CFLAGS) $(PANGO_CFLAGS)
i3_config_wizard_LIBS = $(XCB_LIBS) $(XCB_KBD_LIBS) $(X11_LIBS) 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) i3_config_wizard_OBJECTS := $(i3_config_wizard_SOURCES_GENERATED:.c=.o) $(i3_config_wizard_SOURCES:.c=.o)

View File

@ -69,6 +69,7 @@ enum { MOD_Mod1, MOD_Mod4 } modifier = MOD_Mod4;
static char *config_path; static char *config_path;
static uint32_t xcb_numlock_mask; static uint32_t xcb_numlock_mask;
xcb_connection_t *conn; xcb_connection_t *conn;
xcb_screen_t *root_screen;
static xcb_get_modifier_mapping_reply_t *modmap_reply; static xcb_get_modifier_mapping_reply_t *modmap_reply;
static i3Font font; static i3Font font;
static i3Font bold_font; static i3Font bold_font;
@ -456,7 +457,7 @@ int main(int argc, char *argv[]) {
#include "atoms.xmacro" #include "atoms.xmacro"
#undef 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; root = root_screen->root;
if (!(modmap_reply = xcb_get_modifier_mapping_reply(conn, modmap_cookie, NULL))) if (!(modmap_reply = xcb_get_modifier_mapping_reply(conn, modmap_cookie, NULL)))

View File

@ -4,7 +4,7 @@ CLEAN_TARGETS += clean-i3-dump-log
i3_dump_log_SOURCES := $(wildcard i3-dump-log/*.c) i3_dump_log_SOURCES := $(wildcard i3-dump-log/*.c)
i3_dump_log_HEADERS := $(wildcard i3-dump-log/*.h) 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_LIBS = $(XCB_LIBS)
i3_dump_log_OBJECTS := $(i3_dump_log_SOURCES:.c=.o) i3_dump_log_OBJECTS := $(i3_dump_log_SOURCES:.c=.o)

View File

@ -4,8 +4,8 @@ CLEAN_TARGETS += clean-i3-input
i3_input_SOURCES := $(wildcard i3-input/*.c) i3_input_SOURCES := $(wildcard i3-input/*.c)
i3_input_HEADERS := $(wildcard i3-input/*.h) i3_input_HEADERS := $(wildcard i3-input/*.h)
i3_input_CFLAGS = $(XCB_CFLAGS) $(XCB_KBD_CFLAGS) i3_input_CFLAGS = $(XCB_CFLAGS) $(XCB_KBD_CFLAGS) $(PANGO_CFLAGS)
i3_input_LIBS = $(XCB_LIBS) $(XCB_KBD_LIBS) i3_input_LIBS = $(XCB_LIBS) $(XCB_KBD_LIBS) $(PANGO_LIBS)
i3_input_OBJECTS := $(i3_input_SOURCES:.c=.o) i3_input_OBJECTS := $(i3_input_SOURCES:.c=.o)

View File

@ -54,6 +54,7 @@ static int prompt_offset = 0;
static int limit; static int limit;
xcb_window_t root; xcb_window_t root;
xcb_connection_t *conn; xcb_connection_t *conn;
xcb_screen_t *root_screen;
/* /*
* Concats the glyphs (either UCS-2 or UTF-8) to a single string, suitable for * 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)) if (!conn || xcb_connection_has_error(conn))
die("Cannot open display\n"); 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; root = root_screen->root;
symbols = xcb_key_symbols_alloc(conn); symbols = xcb_key_symbols_alloc(conn);

View File

@ -4,7 +4,7 @@ CLEAN_TARGETS += clean-i3-msg
i3_msg_SOURCES := $(wildcard i3-msg/*.c) i3_msg_SOURCES := $(wildcard i3-msg/*.c)
i3_msg_HEADERS := $(wildcard i3-msg/*.h) 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_LIBS = $(XCB_LIBS)
i3_msg_OBJECTS := $(i3_msg_SOURCES:.c=.o) i3_msg_OBJECTS := $(i3_msg_SOURCES:.c=.o)

View File

@ -4,8 +4,8 @@ CLEAN_TARGETS += clean-i3-nagbar
i3_nagbar_SOURCES := $(wildcard i3-nagbar/*.c) i3_nagbar_SOURCES := $(wildcard i3-nagbar/*.c)
i3_nagbar_HEADERS := $(wildcard i3-nagbar/*.h) i3_nagbar_HEADERS := $(wildcard i3-nagbar/*.h)
i3_nagbar_CFLAGS = $(XCB_CFLAGS) i3_nagbar_CFLAGS = $(XCB_CFLAGS) $(PANGO_CFLAGS)
i3_nagbar_LIBS = $(XCB_LIBS) i3_nagbar_LIBS = $(XCB_LIBS) $(PANGO_LIBS)
i3_nagbar_OBJECTS := $(i3_nagbar_SOURCES:.c=.o) i3_nagbar_OBJECTS := $(i3_nagbar_SOURCES:.c=.o)

View File

@ -54,6 +54,7 @@ static uint32_t color_text; /* color of the text */
xcb_window_t root; xcb_window_t root;
xcb_connection_t *conn; xcb_connection_t *conn;
xcb_screen_t *root_screen;
/* /*
* Starts the given application by passing it through a shell. We use double fork * 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" #include "atoms.xmacro"
#undef 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; root = root_screen->root;
if (bar_type == TYPE_ERROR) { if (bar_type == TYPE_ERROR) {

View File

@ -4,8 +4,8 @@ CLEAN_TARGETS += clean-i3bar
i3bar_SOURCES := $(wildcard i3bar/src/*.c) i3bar_SOURCES := $(wildcard i3bar/src/*.c)
i3bar_HEADERS := $(wildcard i3bar/include/*.h) i3bar_HEADERS := $(wildcard i3bar/include/*.h)
i3bar_CFLAGS = $(XCB_CFLAGS) $(X11_CFLAGS) $(YAJL_CFLAGS) $(LIBEV_CFLAGS) i3bar_CFLAGS = $(XCB_CFLAGS) $(X11_CFLAGS) $(PANGO_CFLAGS) $(YAJL_CFLAGS) $(LIBEV_CFLAGS)
i3bar_LIBS = $(XCB_LIBS) $(X11_LIBS) $(YAJL_LIBS) $(LIBEV_LIBS) i3bar_LIBS = $(XCB_LIBS) $(X11_LIBS) $(PANGO_LIBS) $(YAJL_LIBS) $(LIBEV_LIBS)
i3bar_OBJECTS := $(i3bar_SOURCES:.c=.o) i3bar_OBJECTS := $(i3bar_SOURCES:.c=.o)

View File

@ -11,7 +11,9 @@
#include "queue.h" #include "queue.h"
/* Get the maximum/minimum of x and y */ /* Get the maximum/minimum of x and y */
#undef MAX
#define MAX(x,y) ((x) > (y) ? (x) : (y)) #define MAX(x,y) ((x) > (y) ? (x) : (y))
#undef MIN
#define MIN(x,y) ((x) < (y) ? (x) : (y)) #define MIN(x,y) ((x) < (y) ? (x) : (y))
/* Securely free p */ /* Securely free p */

View File

@ -18,6 +18,10 @@
#include <xcb/xproto.h> #include <xcb/xproto.h>
#include <xcb/xcb_keysyms.h> #include <xcb/xcb_keysyms.h>
#if PANGO_SUPPORT
#include <pango/pango.h>
#endif
/** /**
* Opaque data structure for storing strings. * Opaque data structure for storing strings.
* *
@ -54,6 +58,11 @@ struct Font {
/** Font table for this font (may be NULL) */ /** Font table for this font (may be NULL) */
xcb_charinfo_t *table; xcb_charinfo_t *table;
} xcb; } xcb;
#if PANGO_SUPPORT
/** The pango font description */
PangoFontDescription *pango_desc;
#endif
} specific; } specific;
}; };

View File

@ -12,11 +12,118 @@
#include <stdbool.h> #include <stdbool.h>
#include <err.h> #include <err.h>
#if PANGO_SUPPORT
#include <cairo/cairo-xcb.h>
#include <pango/pangocairo.h>
#endif
#include "libi3.h" #include "libi3.h"
extern xcb_connection_t *conn; extern xcb_connection_t *conn;
extern xcb_screen_t *root_screen;
static const i3Font *savedFont = NULL; 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, * Loads a font for usage, also getting its metrics. If fallback is true,
* the fonts 'fixed' or '-misc-*' will be loaded instead of exiting. * 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; i3Font font;
font.type = FONT_TYPE_NONE; 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 */ /* Send all our requests first */
font.specific.xcb.id = xcb_generate_id(conn); font.specific.xcb.id = xcb_generate_id(conn);
@ -105,6 +220,12 @@ void free_font(void) {
free(savedFont->specific.xcb.info); free(savedFont->specific.xcb.info);
break; break;
} }
#if PANGO_SUPPORT
case FONT_TYPE_PANGO:
/* Free the font description */
pango_font_description_free(savedFont->specific.pango_desc);
break;
#endif
default: default:
assert(false); assert(false);
break; 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); xcb_change_gc(conn, gc, mask, values);
break; 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: default:
assert(false); assert(false);
break; 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), draw_text_xcb(i3string_as_ucs2(text), i3string_get_num_glyphs(text),
drawable, gc, x, y, max_width); drawable, gc, x, y, max_width);
break; 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: default:
assert(false); assert(false);
} }
@ -219,6 +355,13 @@ void draw_text_ascii(const char *text, xcb_drawable_t drawable,
} }
break; 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: default:
assert(false); assert(false);
} }
@ -309,6 +452,11 @@ int predict_text_width(i3String *text) {
return 0; return 0;
case FONT_TYPE_XCB: case FONT_TYPE_XCB:
return predict_text_width_xcb(i3string_as_ucs2(text), i3string_get_num_glyphs(text)); 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: default:
assert(false); assert(false);
return 0; return 0;

View File

@ -2,7 +2,7 @@ CLEAN_TARGETS += clean-libi3
libi3_SOURCES := $(wildcard libi3/*.c) libi3_SOURCES := $(wildcard libi3/*.c)
libi3_HEADERS := $(wildcard libi3/*.h) libi3_HEADERS := $(wildcard libi3/*.h)
libi3_CFLAGS = libi3_CFLAGS = $(PANGO_CFLAGS)
libi3_LIBS = libi3_LIBS =
libi3_OBJECTS := $(libi3_SOURCES:.c=.o) libi3_OBJECTS := $(libi3_SOURCES:.c=.o)

View File

@ -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_SOURCES := $(filter-out $(i3_SOURCES_GENERATED),$(wildcard src/*.c))
i3_HEADERS_CMDPARSER := $(wildcard include/GENERATED_*.h) i3_HEADERS_CMDPARSER := $(wildcard include/GENERATED_*.h)
i3_HEADERS := $(filter-out $(i3_HEADERS_CMDPARSER),$(wildcard include/*.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_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) $(YAJL_LIBS) $(LIBEV_LIBS) $(PCRE_LIBS) $(LIBSN_LIBS) -lm -lpthread 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 # When using clang, we use pre-compiled headers to speed up the build. With
# gcc, this actually makes the build slower. # gcc, this actually makes the build slower.