2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2013 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;
33 static PangoLayout *create_layout_with_dpi(cairo_t *cr) {
35 PangoContext *context;
37 context = pango_cairo_create_context(cr);
38 const double dpi = (double)root_screen->height_in_pixels * 25.4 /
39 (double)root_screen->height_in_millimeters;
40 LOG("X11 root window dictates %f DPI\n", dpi);
41 pango_cairo_context_set_resolution(context, dpi);
42 layout = pango_layout_new(context);
43 g_object_unref(context);
49 * Loads a Pango font description into an i3Font structure. Returns true
50 * on success, false otherwise.
53 static bool load_pango_font(i3Font *font, const char *desc) {
54 /* Load the font description */
55 font->specific.pango_desc = pango_font_description_from_string(desc);
56 if (!font->specific.pango_desc) {
57 ELOG("Could not open font %s with Pango, fallback to X font.\n", desc);
61 LOG("Using Pango font %s, size %d\n",
62 pango_font_description_get_family(font->specific.pango_desc),
63 pango_font_description_get_size(font->specific.pango_desc) / PANGO_SCALE
66 /* We cache root_visual_type here, since you must call
67 * load_pango_font before any other pango function
68 * that would need root_visual_type */
69 root_visual_type = get_visualtype(root_screen);
71 /* Create a dummy Pango layout to compute the font height */
72 cairo_surface_t *surface = cairo_xcb_surface_create(conn, root_screen->root, root_visual_type, 1, 1);
73 cairo_t *cr = cairo_create(surface);
74 PangoLayout *layout = create_layout_with_dpi(cr);
75 pango_layout_set_font_description(layout, font->specific.pango_desc);
77 /* Get the font height */
79 pango_layout_get_pixel_size(layout, NULL, &height);
80 font->height = height;
83 g_object_unref(layout);
85 cairo_surface_destroy(surface);
87 /* Set the font type and return successfully */
88 font->type = FONT_TYPE_PANGO;
93 * Draws text using Pango rendering.
96 static void draw_text_pango(const char *text, size_t text_len,
97 xcb_drawable_t drawable, int x, int y, int max_width) {
98 /* Create the Pango layout */
99 /* root_visual_type is cached in load_pango_font */
100 cairo_surface_t *surface = cairo_xcb_surface_create(conn, drawable,
101 root_visual_type, x + max_width, y + savedFont->height);
102 cairo_t *cr = cairo_create(surface);
103 PangoLayout *layout = create_layout_with_dpi(cr);
106 pango_layout_set_font_description(layout, savedFont->specific.pango_desc);
107 pango_layout_set_width(layout, max_width * PANGO_SCALE);
108 pango_layout_set_wrap(layout, PANGO_WRAP_CHAR);
109 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
111 pango_layout_set_text(layout, text, text_len);
114 cairo_set_source_rgb(cr, pango_font_red, pango_font_green, pango_font_blue);
115 pango_cairo_update_layout(cr, layout);
116 pango_layout_get_pixel_size(layout, NULL, &height);
117 cairo_move_to(cr, x, y - (height - savedFont->height));
118 pango_cairo_show_layout(cr, layout);
121 g_object_unref(layout);
123 cairo_surface_destroy(surface);
127 * Calculate the text width using Pango rendering.
130 static int predict_text_width_pango(const char *text, size_t text_len) {
131 /* Create a dummy Pango layout */
132 /* root_visual_type is cached in load_pango_font */
133 cairo_surface_t *surface = cairo_xcb_surface_create(conn, root_screen->root, root_visual_type, 1, 1);
134 cairo_t *cr = cairo_create(surface);
135 PangoLayout *layout = create_layout_with_dpi(cr);
137 /* Get the font width */
139 pango_layout_set_font_description(layout, savedFont->specific.pango_desc);
140 pango_layout_set_text(layout, text, text_len);
141 pango_cairo_update_layout(cr, layout);
142 pango_layout_get_pixel_size(layout, &width, NULL);
145 g_object_unref(layout);
147 cairo_surface_destroy(surface);
154 * Loads a font for usage, also getting its metrics. If fallback is true,
155 * the fonts 'fixed' or '-misc-*' will be loaded instead of exiting.
158 i3Font load_font(const char *pattern, const bool fallback) {
160 font.type = FONT_TYPE_NONE;
163 /* Try to load a pango font if specified */
164 if (strlen(pattern) > strlen("pango:") && !strncmp(pattern, "pango:", strlen("pango:"))) {
165 const char *font_pattern = pattern + strlen("pango:");
166 if (load_pango_font(&font, font_pattern)) {
167 font.pattern = sstrdup(pattern);
170 } else if (strlen(pattern) > strlen("xft:") && !strncmp(pattern, "xft:", strlen("xft:"))) {
171 const char *font_pattern = pattern + strlen("xft:");
172 if (load_pango_font(&font, font_pattern)) {
173 font.pattern = sstrdup(pattern);
179 /* Send all our requests first */
180 font.specific.xcb.id = xcb_generate_id(conn);
181 xcb_void_cookie_t font_cookie = xcb_open_font_checked(conn, font.specific.xcb.id,
182 strlen(pattern), pattern);
183 xcb_query_font_cookie_t info_cookie = xcb_query_font(conn, font.specific.xcb.id);
185 /* Check for errors. If errors, fall back to default font. */
186 xcb_generic_error_t *error;
187 error = xcb_request_check(conn, font_cookie);
189 /* If we fail to open font, fall back to 'fixed' */
190 if (fallback && error != NULL) {
191 ELOG("Could not open font %s (X error %d). Trying fallback to 'fixed'.\n",
192 pattern, error->error_code);
194 font_cookie = xcb_open_font_checked(conn, font.specific.xcb.id,
195 strlen(pattern), pattern);
196 info_cookie = xcb_query_font(conn, font.specific.xcb.id);
198 /* Check if we managed to open 'fixed' */
199 error = xcb_request_check(conn, font_cookie);
201 /* Fall back to '-misc-*' if opening 'fixed' fails. */
203 ELOG("Could not open fallback font 'fixed', trying with '-misc-*'.\n");
205 font_cookie = xcb_open_font_checked(conn, font.specific.xcb.id,
206 strlen(pattern), pattern);
207 info_cookie = xcb_query_font(conn, font.specific.xcb.id);
209 if ((error = xcb_request_check(conn, font_cookie)) != NULL)
210 errx(EXIT_FAILURE, "Could open neither requested font nor fallbacks "
211 "(fixed or -misc-*): X11 error %d", error->error_code);
215 font.pattern = sstrdup(pattern);
216 LOG("Using X font %s\n", pattern);
218 /* Get information (height/name) for this font */
219 if (!(font.specific.xcb.info = xcb_query_font_reply(conn, info_cookie, NULL)))
220 errx(EXIT_FAILURE, "Could not load font \"%s\"", pattern);
222 /* Get the font table, if possible */
223 if (xcb_query_font_char_infos_length(font.specific.xcb.info) == 0)
224 font.specific.xcb.table = NULL;
226 font.specific.xcb.table = xcb_query_font_char_infos(font.specific.xcb.info);
228 /* Calculate the font height */
229 font.height = font.specific.xcb.info->font_ascent + font.specific.xcb.info->font_descent;
231 /* Set the font type and return successfully */
232 font.type = FONT_TYPE_XCB;
237 * Defines the font to be used for the forthcoming calls.
240 void set_font(i3Font *font) {
245 * Frees the resources taken by the current font.
248 void free_font(void) {
249 free(savedFont->pattern);
250 switch (savedFont->type) {
254 case FONT_TYPE_XCB: {
255 /* Close the font and free the info */
256 xcb_close_font(conn, savedFont->specific.xcb.id);
257 if (savedFont->specific.xcb.info)
258 free(savedFont->specific.xcb.info);
262 case FONT_TYPE_PANGO:
263 /* Free the font description */
264 pango_font_description_free(savedFont->specific.pango_desc);
274 * Defines the colors to be used for the forthcoming draw_text calls.
277 void set_font_colors(xcb_gcontext_t gc, uint32_t foreground, uint32_t background) {
278 assert(savedFont != NULL);
280 switch (savedFont->type) {
284 case FONT_TYPE_XCB: {
285 /* Change the font and colors in the GC */
286 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
287 uint32_t values[] = { foreground, background, savedFont->specific.xcb.id };
288 xcb_change_gc(conn, gc, mask, values);
292 case FONT_TYPE_PANGO:
293 /* Save the foreground font */
294 pango_font_red = ((foreground >> 16) & 0xff) / 255.0;
295 pango_font_green = ((foreground >> 8) & 0xff) / 255.0;
296 pango_font_blue = (foreground & 0xff) / 255.0;
305 static int predict_text_width_xcb(const xcb_char2b_t *text, size_t text_len);
307 static void draw_text_xcb(const xcb_char2b_t *text, size_t text_len, xcb_drawable_t drawable,
308 xcb_gcontext_t gc, int x, int y, int max_width) {
309 /* X11 coordinates for fonts start at the baseline */
310 int pos_y = y + savedFont->specific.xcb.info->font_ascent;
312 /* The X11 protocol limits text drawing to 255 chars, so we may need
316 /* Calculate the size of this chunk */
317 int chunk_size = (text_len > 255 ? 255 : text_len);
318 const xcb_char2b_t *chunk = text + offset;
321 xcb_image_text_16(conn, chunk_size, drawable, gc, x, pos_y, chunk);
323 /* Advance the offset and length of the text to draw */
324 offset += chunk_size;
325 text_len -= chunk_size;
327 /* Check if we're done */
331 /* Advance pos_x based on the predicted text width */
332 x += predict_text_width_xcb(chunk, chunk_size);
337 * Draws text onto the specified X drawable (normally a pixmap) at the
338 * specified coordinates (from the top left corner of the leftmost, uppermost
339 * glyph) and using the provided gc.
341 * Text must be specified as an i3String.
344 void draw_text(i3String *text, xcb_drawable_t drawable,
345 xcb_gcontext_t gc, int x, int y, int max_width) {
346 assert(savedFont != NULL);
348 switch (savedFont->type) {
353 draw_text_xcb(i3string_as_ucs2(text), i3string_get_num_glyphs(text),
354 drawable, gc, x, y, max_width);
357 case FONT_TYPE_PANGO:
358 /* Render the text using Pango */
359 draw_text_pango(i3string_as_utf8(text), i3string_get_num_bytes(text),
360 drawable, x, y, max_width);
369 * ASCII version of draw_text to print static strings.
372 void draw_text_ascii(const char *text, xcb_drawable_t drawable,
373 xcb_gcontext_t gc, int x, int y, int max_width) {
374 assert(savedFont != NULL);
376 switch (savedFont->type) {
382 size_t text_len = strlen(text);
383 if (text_len > 255) {
384 /* The text is too long to draw it directly to X */
385 i3String *str = i3string_from_utf8(text);
386 draw_text(str, drawable, gc, x, y, max_width);
389 /* X11 coordinates for fonts start at the baseline */
390 int pos_y = y + savedFont->specific.xcb.info->font_ascent;
392 xcb_image_text_8(conn, text_len, drawable, gc, x, pos_y, text);
397 case FONT_TYPE_PANGO:
398 /* Render the text using Pango */
399 draw_text_pango(text, strlen(text),
400 drawable, x, y, max_width);
408 static int xcb_query_text_width(const xcb_char2b_t *text, size_t text_len) {
409 /* Make the user know we’re using the slow path, but only once. */
410 static bool first_invocation = true;
411 if (first_invocation) {
412 fprintf(stderr, "Using slow code path for text extents\n");
413 first_invocation = false;
416 /* Query the text width */
417 xcb_generic_error_t *error;
418 xcb_query_text_extents_cookie_t cookie = xcb_query_text_extents(conn,
419 savedFont->specific.xcb.id, text_len, (xcb_char2b_t*)text);
420 xcb_query_text_extents_reply_t *reply = xcb_query_text_extents_reply(conn,
423 /* We return a safe estimate because a rendering error is better than
424 * a crash. Plus, the user will see the error in his log. */
425 fprintf(stderr, "Could not get text extents (X error code %d)\n",
427 return savedFont->specific.xcb.info->max_bounds.character_width * text_len;
430 int width = reply->overall_width;
435 static int predict_text_width_xcb(const xcb_char2b_t *input, size_t text_len) {
440 if (savedFont->specific.xcb.table == NULL) {
441 /* If we don't have a font table, fall back to querying the server */
442 width = xcb_query_text_width(input, text_len);
444 /* Save some pointers for convenience */
445 xcb_query_font_reply_t *font_info = savedFont->specific.xcb.info;
446 xcb_charinfo_t *font_table = savedFont->specific.xcb.table;
448 /* Calculate the width using the font table */
450 for (size_t i = 0; i < text_len; i++) {
451 xcb_charinfo_t *info;
452 int row = input[i].byte1;
453 int col = input[i].byte2;
455 if (row < font_info->min_byte1 ||
456 row > font_info->max_byte1 ||
457 col < font_info->min_char_or_byte2 ||
458 col > font_info->max_char_or_byte2)
461 /* Don't you ask me, how this one works… (Merovius) */
462 info = &font_table[((row - font_info->min_byte1) *
463 (font_info->max_char_or_byte2 - font_info->min_char_or_byte2 + 1)) +
464 (col - font_info->min_char_or_byte2)];
466 if (info->character_width != 0 ||
467 (info->right_side_bearing |
468 info->left_side_bearing |
470 info->descent) != 0) {
471 width += info->character_width;
480 * Predict the text width in pixels for the given text. Text must be
481 * specified as an i3String.
484 int predict_text_width(i3String *text) {
485 assert(savedFont != NULL);
487 switch (savedFont->type) {
492 return predict_text_width_xcb(i3string_as_ucs2(text), i3string_get_num_glyphs(text));
494 case FONT_TYPE_PANGO:
495 /* Calculate extents using Pango */
496 return predict_text_width_pango(i3string_as_utf8(text), i3string_get_num_bytes(text));