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 /* Necessary to track whether the dpi changes and trigger a LOG() message,
34 * which is more easily visible to users. */
35 static double logged_dpi = 0.0;
37 static PangoLayout *create_layout_with_dpi(cairo_t *cr) {
39 PangoContext *context;
41 context = pango_cairo_create_context(cr);
42 const double dpi = (double)root_screen->height_in_pixels * 25.4 /
43 (double)root_screen->height_in_millimeters;
44 if (logged_dpi != dpi) {
46 LOG("X11 root window dictates %f DPI\n", dpi);
48 DLOG("X11 root window dictates %f DPI\n", dpi);
50 pango_cairo_context_set_resolution(context, dpi);
51 layout = pango_layout_new(context);
52 g_object_unref(context);
58 * Loads a Pango font description into an i3Font structure. Returns true
59 * on success, false otherwise.
62 static bool load_pango_font(i3Font *font, const char *desc) {
63 /* Load the font description */
64 font->specific.pango_desc = pango_font_description_from_string(desc);
65 if (!font->specific.pango_desc) {
66 ELOG("Could not open font %s with Pango, fallback to X font.\n", desc);
70 LOG("Using Pango font %s, size %d\n",
71 pango_font_description_get_family(font->specific.pango_desc),
72 pango_font_description_get_size(font->specific.pango_desc) / PANGO_SCALE);
74 /* We cache root_visual_type here, since you must call
75 * load_pango_font before any other pango function
76 * that would need root_visual_type */
77 root_visual_type = get_visualtype(root_screen);
79 /* Create a dummy Pango layout to compute the font height */
80 cairo_surface_t *surface = cairo_xcb_surface_create(conn, root_screen->root, root_visual_type, 1, 1);
81 cairo_t *cr = cairo_create(surface);
82 PangoLayout *layout = create_layout_with_dpi(cr);
83 pango_layout_set_font_description(layout, font->specific.pango_desc);
85 /* Get the font height */
87 pango_layout_get_pixel_size(layout, NULL, &height);
88 font->height = height;
91 g_object_unref(layout);
93 cairo_surface_destroy(surface);
95 /* Set the font type and return successfully */
96 font->type = FONT_TYPE_PANGO;
101 * Draws text using Pango rendering.
104 static void draw_text_pango(const char *text, size_t text_len,
105 xcb_drawable_t drawable, int x, int y, int max_width) {
106 /* Create the Pango layout */
107 /* root_visual_type is cached in load_pango_font */
108 cairo_surface_t *surface = cairo_xcb_surface_create(conn, drawable,
109 root_visual_type, x + max_width, y + savedFont->height);
110 cairo_t *cr = cairo_create(surface);
111 PangoLayout *layout = create_layout_with_dpi(cr);
114 pango_layout_set_font_description(layout, savedFont->specific.pango_desc);
115 pango_layout_set_width(layout, max_width * PANGO_SCALE);
116 pango_layout_set_wrap(layout, PANGO_WRAP_CHAR);
117 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
119 pango_layout_set_text(layout, text, text_len);
122 cairo_set_source_rgb(cr, pango_font_red, pango_font_green, pango_font_blue);
123 pango_cairo_update_layout(cr, layout);
124 pango_layout_get_pixel_size(layout, NULL, &height);
125 cairo_move_to(cr, x, y - (height - savedFont->height));
126 pango_cairo_show_layout(cr, layout);
129 g_object_unref(layout);
131 cairo_surface_destroy(surface);
135 * Calculate the text width using Pango rendering.
138 static int predict_text_width_pango(const char *text, size_t text_len) {
139 /* Create a dummy Pango layout */
140 /* root_visual_type is cached in load_pango_font */
141 cairo_surface_t *surface = cairo_xcb_surface_create(conn, root_screen->root, root_visual_type, 1, 1);
142 cairo_t *cr = cairo_create(surface);
143 PangoLayout *layout = create_layout_with_dpi(cr);
145 /* Get the font width */
147 pango_layout_set_font_description(layout, savedFont->specific.pango_desc);
148 pango_layout_set_text(layout, text, text_len);
149 pango_cairo_update_layout(cr, layout);
150 pango_layout_get_pixel_size(layout, &width, NULL);
153 g_object_unref(layout);
155 cairo_surface_destroy(surface);
162 * Loads a font for usage, also getting its metrics. If fallback is true,
163 * the fonts 'fixed' or '-misc-*' will be loaded instead of exiting.
166 i3Font load_font(const char *pattern, const bool fallback) {
168 font.type = FONT_TYPE_NONE;
171 /* Try to load a pango font if specified */
172 if (strlen(pattern) > strlen("pango:") && !strncmp(pattern, "pango:", strlen("pango:"))) {
173 const char *font_pattern = pattern + strlen("pango:");
174 if (load_pango_font(&font, font_pattern)) {
175 font.pattern = sstrdup(pattern);
178 } else if (strlen(pattern) > strlen("xft:") && !strncmp(pattern, "xft:", strlen("xft:"))) {
179 const char *font_pattern = pattern + strlen("xft:");
180 if (load_pango_font(&font, font_pattern)) {
181 font.pattern = sstrdup(pattern);
187 /* Send all our requests first */
188 font.specific.xcb.id = xcb_generate_id(conn);
189 xcb_void_cookie_t font_cookie = xcb_open_font_checked(conn, font.specific.xcb.id,
190 strlen(pattern), pattern);
191 xcb_query_font_cookie_t info_cookie = xcb_query_font(conn, font.specific.xcb.id);
193 /* Check for errors. If errors, fall back to default font. */
194 xcb_generic_error_t *error;
195 error = xcb_request_check(conn, font_cookie);
197 /* If we fail to open font, fall back to 'fixed' */
198 if (fallback && error != NULL) {
199 ELOG("Could not open font %s (X error %d). Trying fallback to 'fixed'.\n",
200 pattern, error->error_code);
202 font_cookie = xcb_open_font_checked(conn, font.specific.xcb.id,
203 strlen(pattern), pattern);
204 info_cookie = xcb_query_font(conn, font.specific.xcb.id);
206 /* Check if we managed to open 'fixed' */
207 error = xcb_request_check(conn, font_cookie);
209 /* Fall back to '-misc-*' if opening 'fixed' fails. */
211 ELOG("Could not open fallback font 'fixed', trying with '-misc-*'.\n");
213 font_cookie = xcb_open_font_checked(conn, font.specific.xcb.id,
214 strlen(pattern), pattern);
215 info_cookie = xcb_query_font(conn, font.specific.xcb.id);
217 if ((error = xcb_request_check(conn, font_cookie)) != NULL)
218 errx(EXIT_FAILURE, "Could open neither requested font nor fallbacks "
219 "(fixed or -misc-*): X11 error %d",
224 font.pattern = sstrdup(pattern);
225 LOG("Using X font %s\n", pattern);
227 /* Get information (height/name) for this font */
228 if (!(font.specific.xcb.info = xcb_query_font_reply(conn, info_cookie, NULL)))
229 errx(EXIT_FAILURE, "Could not load font \"%s\"", pattern);
231 /* Get the font table, if possible */
232 if (xcb_query_font_char_infos_length(font.specific.xcb.info) == 0)
233 font.specific.xcb.table = NULL;
235 font.specific.xcb.table = xcb_query_font_char_infos(font.specific.xcb.info);
237 /* Calculate the font height */
238 font.height = font.specific.xcb.info->font_ascent + font.specific.xcb.info->font_descent;
240 /* Set the font type and return successfully */
241 font.type = FONT_TYPE_XCB;
246 * Defines the font to be used for the forthcoming calls.
249 void set_font(i3Font *font) {
254 * Frees the resources taken by the current font.
257 void free_font(void) {
258 free(savedFont->pattern);
259 switch (savedFont->type) {
263 case FONT_TYPE_XCB: {
264 /* Close the font and free the info */
265 xcb_close_font(conn, savedFont->specific.xcb.id);
266 if (savedFont->specific.xcb.info)
267 free(savedFont->specific.xcb.info);
271 case FONT_TYPE_PANGO:
272 /* Free the font description */
273 pango_font_description_free(savedFont->specific.pango_desc);
283 * Defines the colors to be used for the forthcoming draw_text calls.
286 void set_font_colors(xcb_gcontext_t gc, uint32_t foreground, uint32_t background) {
287 assert(savedFont != NULL);
289 switch (savedFont->type) {
293 case FONT_TYPE_XCB: {
294 /* Change the font and colors in the GC */
295 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
296 uint32_t values[] = {foreground, background, savedFont->specific.xcb.id};
297 xcb_change_gc(conn, gc, mask, values);
301 case FONT_TYPE_PANGO:
302 /* Save the foreground font */
303 pango_font_red = ((foreground >> 16) & 0xff) / 255.0;
304 pango_font_green = ((foreground >> 8) & 0xff) / 255.0;
305 pango_font_blue = (foreground & 0xff) / 255.0;
314 static int predict_text_width_xcb(const xcb_char2b_t *text, size_t text_len);
316 static void draw_text_xcb(const xcb_char2b_t *text, size_t text_len, xcb_drawable_t drawable,
317 xcb_gcontext_t gc, int x, int y, int max_width) {
318 /* X11 coordinates for fonts start at the baseline */
319 int pos_y = y + savedFont->specific.xcb.info->font_ascent;
321 /* The X11 protocol limits text drawing to 255 chars, so we may need
325 /* Calculate the size of this chunk */
326 int chunk_size = (text_len > 255 ? 255 : text_len);
327 const xcb_char2b_t *chunk = text + offset;
330 xcb_image_text_16(conn, chunk_size, drawable, gc, x, pos_y, chunk);
332 /* Advance the offset and length of the text to draw */
333 offset += chunk_size;
334 text_len -= chunk_size;
336 /* Check if we're done */
340 /* Advance pos_x based on the predicted text width */
341 x += predict_text_width_xcb(chunk, chunk_size);
346 * Draws text onto the specified X drawable (normally a pixmap) at the
347 * specified coordinates (from the top left corner of the leftmost, uppermost
348 * glyph) and using the provided gc.
350 * Text must be specified as an i3String.
353 void draw_text(i3String *text, xcb_drawable_t drawable,
354 xcb_gcontext_t gc, int x, int y, int max_width) {
355 assert(savedFont != NULL);
357 switch (savedFont->type) {
362 draw_text_xcb(i3string_as_ucs2(text), i3string_get_num_glyphs(text),
363 drawable, gc, x, y, max_width);
366 case FONT_TYPE_PANGO:
367 /* Render the text using Pango */
368 draw_text_pango(i3string_as_utf8(text), i3string_get_num_bytes(text),
369 drawable, x, y, max_width);
378 * ASCII version of draw_text to print static strings.
381 void draw_text_ascii(const char *text, xcb_drawable_t drawable,
382 xcb_gcontext_t gc, int x, int y, int max_width) {
383 assert(savedFont != NULL);
385 switch (savedFont->type) {
389 case FONT_TYPE_XCB: {
390 size_t text_len = strlen(text);
391 if (text_len > 255) {
392 /* The text is too long to draw it directly to X */
393 i3String *str = i3string_from_utf8(text);
394 draw_text(str, drawable, gc, x, y, max_width);
397 /* X11 coordinates for fonts start at the baseline */
398 int pos_y = y + savedFont->specific.xcb.info->font_ascent;
400 xcb_image_text_8(conn, text_len, drawable, gc, x, pos_y, text);
405 case FONT_TYPE_PANGO:
406 /* Render the text using Pango */
407 draw_text_pango(text, strlen(text),
408 drawable, x, y, max_width);
416 static int xcb_query_text_width(const xcb_char2b_t *text, size_t text_len) {
417 /* Make the user know we’re using the slow path, but only once. */
418 static bool first_invocation = true;
419 if (first_invocation) {
420 fprintf(stderr, "Using slow code path for text extents\n");
421 first_invocation = false;
424 /* Query the text width */
425 xcb_generic_error_t *error;
426 xcb_query_text_extents_cookie_t cookie = xcb_query_text_extents(conn,
427 savedFont->specific.xcb.id, text_len, (xcb_char2b_t *)text);
428 xcb_query_text_extents_reply_t *reply = xcb_query_text_extents_reply(conn,
431 /* We return a safe estimate because a rendering error is better than
432 * a crash. Plus, the user will see the error in his log. */
433 fprintf(stderr, "Could not get text extents (X error code %d)\n",
435 return savedFont->specific.xcb.info->max_bounds.character_width * text_len;
438 int width = reply->overall_width;
443 static int predict_text_width_xcb(const xcb_char2b_t *input, size_t text_len) {
448 if (savedFont->specific.xcb.table == NULL) {
449 /* If we don't have a font table, fall back to querying the server */
450 width = xcb_query_text_width(input, text_len);
452 /* Save some pointers for convenience */
453 xcb_query_font_reply_t *font_info = savedFont->specific.xcb.info;
454 xcb_charinfo_t *font_table = savedFont->specific.xcb.table;
456 /* Calculate the width using the font table */
458 for (size_t i = 0; i < text_len; i++) {
459 xcb_charinfo_t *info;
460 int row = input[i].byte1;
461 int col = input[i].byte2;
463 if (row < font_info->min_byte1 ||
464 row > font_info->max_byte1 ||
465 col < font_info->min_char_or_byte2 ||
466 col > font_info->max_char_or_byte2)
469 /* Don't you ask me, how this one works… (Merovius) */
470 info = &font_table[((row - font_info->min_byte1) *
471 (font_info->max_char_or_byte2 - font_info->min_char_or_byte2 + 1)) +
472 (col - font_info->min_char_or_byte2)];
474 if (info->character_width != 0 ||
475 (info->right_side_bearing |
476 info->left_side_bearing |
478 info->descent) != 0) {
479 width += info->character_width;
488 * Predict the text width in pixels for the given text. Text must be
489 * specified as an i3String.
492 int predict_text_width(i3String *text) {
493 assert(savedFont != NULL);
495 switch (savedFont->type) {
500 return predict_text_width_xcb(i3string_as_ucs2(text), i3string_get_num_glyphs(text));
502 case FONT_TYPE_PANGO:
503 /* Calculate extents using Pango */
504 return predict_text_width_pango(i3string_as_utf8(text), i3string_get_num_bytes(text));