2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
16 #include <cairo/cairo-xcb.h>
17 #include <pango/pangocairo.h>
22 extern xcb_connection_t *conn;
23 extern xcb_screen_t *root_screen;
25 static const i3Font *savedFont = NULL;
28 static xcb_visualtype_t *root_visual_type;
29 static double pango_font_red;
30 static double pango_font_green;
31 static double pango_font_blue;
34 * Loads a Pango font description into an i3Font structure. Returns true
35 * on success, false otherwise.
38 static bool load_pango_font(i3Font *font, const char *desc) {
39 /* Load the font description */
40 font->specific.pango_desc = pango_font_description_from_string(desc);
41 if (!font->specific.pango_desc) {
42 ELOG("Could not open font %s with Pango, fallback to X font.\n", desc);
46 LOG("Using Pango font %s, size %d\n",
47 pango_font_description_get_family(font->specific.pango_desc),
48 pango_font_description_get_size(font->specific.pango_desc) / PANGO_SCALE
51 /* We cache root_visual_type here, since you must call
52 * load_pango_font before any other pango function
53 * that would need root_visual_type */
54 root_visual_type = get_visualtype(root_screen);
56 /* Create a dummy Pango layout to compute the font height */
57 cairo_surface_t *surface = cairo_xcb_surface_create(conn, root_screen->root, root_visual_type, 1, 1);
58 cairo_t *cr = cairo_create(surface);
59 PangoLayout *layout = pango_cairo_create_layout(cr);
60 pango_layout_set_font_description(layout, font->specific.pango_desc);
62 /* Get the font height */
64 pango_layout_get_pixel_size(layout, NULL, &height);
65 font->height = height;
68 g_object_unref(layout);
70 cairo_surface_destroy(surface);
72 /* Set the font type and return successfully */
73 font->type = FONT_TYPE_PANGO;
78 * Draws text using Pango rendering.
81 static void draw_text_pango(const char *text, size_t text_len,
82 xcb_drawable_t drawable, int x, int y, int max_width) {
83 /* Create the Pango layout */
84 /* root_visual_type is cached in load_pango_font */
85 cairo_surface_t *surface = cairo_xcb_surface_create(conn, drawable,
86 root_visual_type, x + max_width, y + savedFont->height);
87 cairo_t *cr = cairo_create(surface);
88 PangoLayout *layout = pango_cairo_create_layout(cr);
89 pango_layout_set_font_description(layout, savedFont->specific.pango_desc);
90 pango_layout_set_width(layout, max_width * PANGO_SCALE);
91 pango_layout_set_wrap(layout, PANGO_WRAP_CHAR);
92 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
95 cairo_set_source_rgb(cr, pango_font_red, pango_font_green, pango_font_blue);
96 cairo_move_to(cr, x, y);
97 pango_layout_set_text(layout, text, text_len);
98 pango_cairo_update_layout(cr, layout);
99 pango_cairo_show_layout(cr, layout);
102 g_object_unref(layout);
104 cairo_surface_destroy(surface);
108 * Calculate the text width using Pango rendering.
111 static int predict_text_width_pango(const char *text, size_t text_len) {
112 /* Create a dummy Pango layout */
113 /* root_visual_type is cached in load_pango_font */
114 cairo_surface_t *surface = cairo_xcb_surface_create(conn, root_screen->root, root_visual_type, 1, 1);
115 cairo_t *cr = cairo_create(surface);
116 PangoLayout *layout = pango_cairo_create_layout(cr);
118 /* Get the font width */
120 pango_layout_set_font_description(layout, savedFont->specific.pango_desc);
121 pango_layout_set_text(layout, text, text_len);
122 pango_cairo_update_layout(cr, layout);
123 pango_layout_get_pixel_size(layout, &width, NULL);
126 g_object_unref(layout);
128 cairo_surface_destroy(surface);
135 * Loads a font for usage, also getting its metrics. If fallback is true,
136 * the fonts 'fixed' or '-misc-*' will be loaded instead of exiting.
139 i3Font load_font(const char *pattern, const bool fallback) {
141 font.type = FONT_TYPE_NONE;
144 /* Try to load a pango font if specified */
145 if (strlen(pattern) > strlen("pango:") && !strncmp(pattern, "pango:", strlen("pango:"))) {
146 pattern += strlen("pango:");
147 if (load_pango_font(&font, pattern))
149 } else if (strlen(pattern) > strlen("xft:") && !strncmp(pattern, "xft:", strlen("xft:"))) {
150 pattern += strlen("xft:");
151 if (load_pango_font(&font, pattern))
156 /* Send all our requests first */
157 font.specific.xcb.id = xcb_generate_id(conn);
158 xcb_void_cookie_t font_cookie = xcb_open_font_checked(conn, font.specific.xcb.id,
159 strlen(pattern), pattern);
160 xcb_query_font_cookie_t info_cookie = xcb_query_font(conn, font.specific.xcb.id);
162 /* Check for errors. If errors, fall back to default font. */
163 xcb_generic_error_t *error;
164 error = xcb_request_check(conn, font_cookie);
166 /* If we fail to open font, fall back to 'fixed' */
167 if (fallback && error != NULL) {
168 ELOG("Could not open font %s (X error %d). Trying fallback to 'fixed'.\n",
169 pattern, error->error_code);
171 font_cookie = xcb_open_font_checked(conn, font.specific.xcb.id,
172 strlen(pattern), pattern);
173 info_cookie = xcb_query_font(conn, font.specific.xcb.id);
175 /* Check if we managed to open 'fixed' */
176 error = xcb_request_check(conn, font_cookie);
178 /* Fall back to '-misc-*' if opening 'fixed' fails. */
180 ELOG("Could not open fallback font 'fixed', trying with '-misc-*'.\n");
182 font_cookie = xcb_open_font_checked(conn, font.specific.xcb.id,
183 strlen(pattern), pattern);
184 info_cookie = xcb_query_font(conn, font.specific.xcb.id);
186 if ((error = xcb_request_check(conn, font_cookie)) != NULL)
187 errx(EXIT_FAILURE, "Could open neither requested font nor fallbacks "
188 "(fixed or -misc-*): X11 error %d", error->error_code);
192 LOG("Using X font %s\n", pattern);
194 /* Get information (height/name) for this font */
195 if (!(font.specific.xcb.info = xcb_query_font_reply(conn, info_cookie, NULL)))
196 errx(EXIT_FAILURE, "Could not load font \"%s\"", pattern);
198 /* Get the font table, if possible */
199 if (xcb_query_font_char_infos_length(font.specific.xcb.info) == 0)
200 font.specific.xcb.table = NULL;
202 font.specific.xcb.table = xcb_query_font_char_infos(font.specific.xcb.info);
204 /* Calculate the font height */
205 font.height = font.specific.xcb.info->font_ascent + font.specific.xcb.info->font_descent;
207 /* Set the font type and return successfully */
208 font.type = FONT_TYPE_XCB;
213 * Defines the font to be used for the forthcoming calls.
216 void set_font(i3Font *font) {
221 * Frees the resources taken by the current font.
224 void free_font(void) {
225 switch (savedFont->type) {
229 case FONT_TYPE_XCB: {
230 /* Close the font and free the info */
231 xcb_close_font(conn, savedFont->specific.xcb.id);
232 if (savedFont->specific.xcb.info)
233 free(savedFont->specific.xcb.info);
237 case FONT_TYPE_PANGO:
238 /* Free the font description */
239 pango_font_description_free(savedFont->specific.pango_desc);
249 * Defines the colors to be used for the forthcoming draw_text calls.
252 void set_font_colors(xcb_gcontext_t gc, uint32_t foreground, uint32_t background) {
253 assert(savedFont != NULL);
255 switch (savedFont->type) {
259 case FONT_TYPE_XCB: {
260 /* Change the font and colors in the GC */
261 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
262 uint32_t values[] = { foreground, background, savedFont->specific.xcb.id };
263 xcb_change_gc(conn, gc, mask, values);
267 case FONT_TYPE_PANGO:
268 /* Save the foreground font */
269 pango_font_red = ((foreground >> 16) & 0xff) / 255.0;
270 pango_font_green = ((foreground >> 8) & 0xff) / 255.0;
271 pango_font_blue = (foreground & 0xff) / 255.0;
280 static int predict_text_width_xcb(const xcb_char2b_t *text, size_t text_len);
282 static void draw_text_xcb(const xcb_char2b_t *text, size_t text_len, xcb_drawable_t drawable,
283 xcb_gcontext_t gc, int x, int y, int max_width) {
284 /* X11 coordinates for fonts start at the baseline */
285 int pos_y = y + savedFont->specific.xcb.info->font_ascent;
287 /* The X11 protocol limits text drawing to 255 chars, so we may need
291 /* Calculate the size of this chunk */
292 int chunk_size = (text_len > 255 ? 255 : text_len);
293 const xcb_char2b_t *chunk = text + offset;
296 xcb_image_text_16(conn, chunk_size, drawable, gc, x, pos_y, chunk);
298 /* Advance the offset and length of the text to draw */
299 offset += chunk_size;
300 text_len -= chunk_size;
302 /* Check if we're done */
306 /* Advance pos_x based on the predicted text width */
307 x += predict_text_width_xcb(chunk, chunk_size);
312 * Draws text onto the specified X drawable (normally a pixmap) at the
313 * specified coordinates (from the top left corner of the leftmost, uppermost
314 * glyph) and using the provided gc.
316 * Text must be specified as an i3String.
319 void draw_text(i3String *text, xcb_drawable_t drawable,
320 xcb_gcontext_t gc, int x, int y, int max_width) {
321 assert(savedFont != NULL);
323 switch (savedFont->type) {
328 draw_text_xcb(i3string_as_ucs2(text), i3string_get_num_glyphs(text),
329 drawable, gc, x, y, max_width);
332 case FONT_TYPE_PANGO:
333 /* Render the text using Pango */
334 draw_text_pango(i3string_as_utf8(text), i3string_get_num_bytes(text),
335 drawable, x, y, max_width);
344 * ASCII version of draw_text to print static strings.
347 void draw_text_ascii(const char *text, xcb_drawable_t drawable,
348 xcb_gcontext_t gc, int x, int y, int max_width) {
349 assert(savedFont != NULL);
351 switch (savedFont->type) {
357 size_t text_len = strlen(text);
358 if (text_len > 255) {
359 /* The text is too long to draw it directly to X */
360 i3String *str = i3string_from_utf8(text);
361 draw_text(str, drawable, gc, x, y, max_width);
364 /* X11 coordinates for fonts start at the baseline */
365 int pos_y = y + savedFont->specific.xcb.info->font_ascent;
367 xcb_image_text_8(conn, text_len, drawable, gc, x, pos_y, text);
372 case FONT_TYPE_PANGO:
373 /* Render the text using Pango */
374 draw_text_pango(text, strlen(text),
375 drawable, x, y, max_width);
383 static int xcb_query_text_width(const xcb_char2b_t *text, size_t text_len) {
384 /* Make the user know we’re using the slow path, but only once. */
385 static bool first_invocation = true;
386 if (first_invocation) {
387 fprintf(stderr, "Using slow code path for text extents\n");
388 first_invocation = false;
391 /* Query the text width */
392 xcb_generic_error_t *error;
393 xcb_query_text_extents_cookie_t cookie = xcb_query_text_extents(conn,
394 savedFont->specific.xcb.id, text_len, (xcb_char2b_t*)text);
395 xcb_query_text_extents_reply_t *reply = xcb_query_text_extents_reply(conn,
398 /* We return a safe estimate because a rendering error is better than
399 * a crash. Plus, the user will see the error in his log. */
400 fprintf(stderr, "Could not get text extents (X error code %d)\n",
402 return savedFont->specific.xcb.info->max_bounds.character_width * text_len;
405 int width = reply->overall_width;
410 static int predict_text_width_xcb(const xcb_char2b_t *input, size_t text_len) {
415 if (savedFont->specific.xcb.table == NULL) {
416 /* If we don't have a font table, fall back to querying the server */
417 width = xcb_query_text_width(input, text_len);
419 /* Save some pointers for convenience */
420 xcb_query_font_reply_t *font_info = savedFont->specific.xcb.info;
421 xcb_charinfo_t *font_table = savedFont->specific.xcb.table;
423 /* Calculate the width using the font table */
425 for (size_t i = 0; i < text_len; i++) {
426 xcb_charinfo_t *info;
427 int row = input[i].byte1;
428 int col = input[i].byte2;
430 if (row < font_info->min_byte1 ||
431 row > font_info->max_byte1 ||
432 col < font_info->min_char_or_byte2 ||
433 col > font_info->max_char_or_byte2)
436 /* Don't you ask me, how this one works… (Merovius) */
437 info = &font_table[((row - font_info->min_byte1) *
438 (font_info->max_char_or_byte2 - font_info->min_char_or_byte2 + 1)) +
439 (col - font_info->min_char_or_byte2)];
441 if (info->character_width != 0 ||
442 (info->right_side_bearing |
443 info->left_side_bearing |
445 info->descent) != 0) {
446 width += info->character_width;
455 * Predict the text width in pixels for the given text. Text must be
456 * specified as an i3String.
459 int predict_text_width(i3String *text) {
460 assert(savedFont != NULL);
462 switch (savedFont->type) {
467 return predict_text_width_xcb(i3string_as_ucs2(text), i3string_get_num_glyphs(text));
469 case FONT_TYPE_PANGO:
470 /* Calculate extents using Pango */
471 return predict_text_width_pango(i3string_as_utf8(text), i3string_get_num_bytes(text));