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)
17 extern xcb_connection_t *conn;
18 static const i3Font *savedFont = NULL;
21 * Loads a font for usage, also getting its metrics. If fallback is true,
22 * the fonts 'fixed' or '-misc-*' will be loaded instead of exiting.
25 i3Font load_font(const char *pattern, const bool fallback) {
28 /* Send all our requests first */
29 font.id = xcb_generate_id(conn);
30 xcb_void_cookie_t font_cookie = xcb_open_font_checked(conn, font.id,
31 strlen(pattern), pattern);
32 xcb_query_font_cookie_t info_cookie = xcb_query_font(conn, font.id);
34 /* Check for errors. If errors, fall back to default font. */
35 xcb_generic_error_t *error;
36 error = xcb_request_check(conn, font_cookie);
38 /* If we fail to open font, fall back to 'fixed' */
39 if (fallback && error != NULL) {
40 ELOG("Could not open font %s (X error %d). Trying fallback to 'fixed'.\n",
41 pattern, error->error_code);
43 font_cookie = xcb_open_font_checked(conn, font.id, strlen(pattern), pattern);
44 info_cookie = xcb_query_font(conn, font.id);
46 /* Check if we managed to open 'fixed' */
47 error = xcb_request_check(conn, font_cookie);
49 /* Fall back to '-misc-*' if opening 'fixed' fails. */
51 ELOG("Could not open fallback font 'fixed', trying with '-misc-*'.\n");
53 font_cookie = xcb_open_font_checked(conn, font.id, strlen(pattern), pattern);
54 info_cookie = xcb_query_font(conn, font.id);
56 if ((error = xcb_request_check(conn, font_cookie)) != NULL)
57 errx(EXIT_FAILURE, "Could open neither requested font nor fallbacks "
58 "(fixed or -misc-*): X11 error %d", error->error_code);
62 /* Get information (height/name) for this font */
63 if (!(font.info = xcb_query_font_reply(conn, info_cookie, NULL)))
64 errx(EXIT_FAILURE, "Could not load font \"%s\"", pattern);
66 /* Get the font table, if possible */
67 if (xcb_query_font_char_infos_length(font.info) == 0)
70 font.table = xcb_query_font_char_infos(font.info);
72 /* Calculate the font height */
73 font.height = font.info->font_ascent + font.info->font_descent;
79 * Defines the font to be used for the forthcoming calls.
82 void set_font(i3Font *font) {
87 * Frees the resources taken by the current font.
90 void free_font(void) {
91 /* Close the font and free the info */
92 xcb_close_font(conn, savedFont->id);
94 free(savedFont->info);
98 * Defines the colors to be used for the forthcoming draw_text calls.
101 void set_font_colors(xcb_gcontext_t gc, uint32_t foreground, uint32_t background) {
102 assert(savedFont != NULL);
103 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
104 uint32_t values[] = { foreground, background, savedFont->id };
105 xcb_change_gc(conn, gc, mask, values);
108 static void draw_text_xcb(const xcb_char2b_t *text, size_t text_len, xcb_drawable_t drawable,
109 xcb_gcontext_t gc, int x, int y, int max_width) {
110 /* X11 coordinates for fonts start at the baseline */
111 int pos_y = y + savedFont->info->font_ascent;
113 /* The X11 protocol limits text drawing to 255 chars, so we may need
117 /* Calculate the size of this chunk */
118 int chunk_size = (text_len > 255 ? 255 : text_len);
119 const xcb_char2b_t *chunk = text + offset;
122 xcb_image_text_16(conn, chunk_size, drawable, gc, x, pos_y, chunk);
124 /* Advance the offset and length of the text to draw */
125 offset += chunk_size;
126 text_len -= chunk_size;
128 /* Check if we're done */
132 /* Advance pos_x based on the predicted text width */
133 x += predict_text_width((char*)chunk, chunk_size, true);
138 * Draws text onto the specified X drawable (normally a pixmap) at the
139 * specified coordinates (from the top left corner of the leftmost, uppermost
140 * glyph) and using the provided gc.
142 * Text must be specified as an i3String.
145 void draw_text(i3String *text, xcb_drawable_t drawable,
146 xcb_gcontext_t gc, int x, int y, int max_width) {
147 assert(savedFont != NULL);
149 draw_text_xcb(i3string_as_ucs2(text), i3string_get_num_glyphs(text),
150 drawable, gc, x, y, max_width);
154 * ASCII version of draw_text to print static strings.
157 void draw_text_ascii(const char *text, xcb_drawable_t drawable,
158 xcb_gcontext_t gc, int x, int y, int max_width) {
159 assert(savedFont != NULL);
161 size_t text_len = strlen(text);
162 if (text_len > 255) {
163 /* The text is too long to draw it directly to X */
164 i3String *str = i3string_from_utf8(text);
165 draw_text(str, drawable, gc, x, y, max_width);
168 /* X11 coordinates for fonts start at the baseline */
169 int pos_y = y + savedFont->info->font_ascent;
171 xcb_image_text_8(conn, text_len, drawable, gc, x, pos_y, text);
175 static int xcb_query_text_width(xcb_char2b_t *text, size_t text_len) {
176 /* Make the user know we’re using the slow path, but only once. */
177 static bool first_invocation = true;
178 if (first_invocation) {
179 fprintf(stderr, "Using slow code path for text extents\n");
180 first_invocation = false;
183 /* Query the text width */
184 xcb_generic_error_t *error;
185 xcb_query_text_extents_cookie_t cookie = xcb_query_text_extents(conn,
186 savedFont->id, text_len, (xcb_char2b_t*)text);
187 xcb_query_text_extents_reply_t *reply = xcb_query_text_extents_reply(conn,
190 /* We return a safe estimate because a rendering error is better than
191 * a crash. Plus, the user will see the error in his log. */
192 fprintf(stderr, "Could not get text extents (X error code %d)\n",
194 return savedFont->info->max_bounds.character_width * text_len;
197 int width = reply->overall_width;
203 * Predict the text width in pixels for the given text. Text can be specified
207 int predict_text_width(char *text, size_t text_len, bool is_ucs2) {
208 /* Convert the text into UTF-16 so we can do basic pointer math */
211 input = (xcb_char2b_t*)text;
213 input = convert_utf8_to_ucs2(text, &text_len);
216 if (savedFont->table == NULL) {
217 /* If we don't have a font table, fall back to querying the server */
218 width = xcb_query_text_width(input, text_len);
220 /* Save some pointers for convenience */
221 xcb_query_font_reply_t *font_info = savedFont->info;
222 xcb_charinfo_t *font_table = savedFont->table;
224 /* Calculate the width using the font table */
226 for (size_t i = 0; i < text_len; i++) {
227 xcb_charinfo_t *info;
228 int row = input[i].byte1;
229 int col = input[i].byte2;
231 if (row < font_info->min_byte1 ||
232 row > font_info->max_byte1 ||
233 col < font_info->min_char_or_byte2 ||
234 col > font_info->max_char_or_byte2)
237 /* Don't you ask me, how this one works… (Merovius) */
238 info = &font_table[((row - font_info->min_byte1) *
239 (font_info->max_char_or_byte2 - font_info->min_char_or_byte2 + 1)) +
240 (col - font_info->min_char_or_byte2)];
242 if (info->character_width != 0 ||
243 (info->right_side_bearing |
244 info->left_side_bearing |
246 info->descent) != 0) {
247 width += info->character_width;
252 /* If we had to convert, free the converted string */