2011-11-13 20:19:42 +01:00
|
|
|
|
/*
|
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
|
*
|
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
2013-01-26 09:55:38 +01:00
|
|
|
|
* © 2009-2013 Michael Stapelberg and contributors (see also: LICENSE)
|
2011-11-13 20:19:42 +01:00
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <err.h>
|
|
|
|
|
|
2012-08-05 21:36:49 +02:00
|
|
|
|
#if PANGO_SUPPORT
|
|
|
|
|
#include <cairo/cairo-xcb.h>
|
|
|
|
|
#include <pango/pangocairo.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-11-13 20:19:42 +01:00
|
|
|
|
#include "libi3.h"
|
|
|
|
|
|
|
|
|
|
extern xcb_connection_t *conn;
|
2012-08-05 21:36:49 +02:00
|
|
|
|
extern xcb_screen_t *root_screen;
|
|
|
|
|
|
2011-11-13 20:19:42 +01:00
|
|
|
|
static const i3Font *savedFont = NULL;
|
|
|
|
|
|
2012-08-05 21:36:49 +02:00
|
|
|
|
#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;
|
|
|
|
|
|
2013-12-24 10:35:56 +01:00
|
|
|
|
/* Necessary to track whether the dpi changes and trigger a LOG() message,
|
|
|
|
|
* which is more easily visible to users. */
|
|
|
|
|
static double logged_dpi = 0.0;
|
|
|
|
|
|
2013-11-18 23:17:44 +01:00
|
|
|
|
static PangoLayout *create_layout_with_dpi(cairo_t *cr) {
|
|
|
|
|
PangoLayout *layout;
|
|
|
|
|
PangoContext *context;
|
|
|
|
|
|
|
|
|
|
context = pango_cairo_create_context(cr);
|
|
|
|
|
const double dpi = (double)root_screen->height_in_pixels * 25.4 /
|
|
|
|
|
(double)root_screen->height_in_millimeters;
|
2013-12-24 10:35:56 +01:00
|
|
|
|
if (logged_dpi != dpi) {
|
|
|
|
|
logged_dpi = dpi;
|
|
|
|
|
LOG("X11 root window dictates %f DPI\n", dpi);
|
|
|
|
|
} else {
|
|
|
|
|
DLOG("X11 root window dictates %f DPI\n", dpi);
|
|
|
|
|
}
|
2013-11-18 23:17:44 +01:00
|
|
|
|
pango_cairo_context_set_resolution(context, dpi);
|
|
|
|
|
layout = pango_layout_new(context);
|
|
|
|
|
g_object_unref(context);
|
|
|
|
|
|
|
|
|
|
return layout;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-05 21:36:49 +02:00
|
|
|
|
/*
|
|
|
|
|
* 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);
|
2012-08-13 13:27:00 +02:00
|
|
|
|
if (!font->specific.pango_desc) {
|
2012-08-12 12:20:51 +02:00
|
|
|
|
ELOG("Could not open font %s with Pango, fallback to X font.\n", desc);
|
2012-08-05 21:36:49 +02:00
|
|
|
|
return false;
|
2012-08-12 12:20:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-13 13:27:00 +02:00
|
|
|
|
LOG("Using Pango font %s, size %d\n",
|
2012-08-12 12:20:51 +02:00
|
|
|
|
pango_font_description_get_family(font->specific.pango_desc),
|
2014-06-15 19:07:02 +02:00
|
|
|
|
pango_font_description_get_size(font->specific.pango_desc) / PANGO_SCALE);
|
2012-08-05 21:36:49 +02:00
|
|
|
|
|
|
|
|
|
/* 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);
|
2013-11-18 23:17:44 +01:00
|
|
|
|
PangoLayout *layout = create_layout_with_dpi(cr);
|
2012-08-05 21:36:49 +02:00
|
|
|
|
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,
|
2015-02-12 20:45:34 +01:00
|
|
|
|
xcb_drawable_t drawable, int x, int y,
|
|
|
|
|
int max_width, bool is_markup) {
|
2012-08-05 21:36:49 +02:00
|
|
|
|
/* Create the Pango layout */
|
|
|
|
|
/* root_visual_type is cached in load_pango_font */
|
|
|
|
|
cairo_surface_t *surface = cairo_xcb_surface_create(conn, drawable,
|
2014-06-15 19:07:02 +02:00
|
|
|
|
root_visual_type, x + max_width, y + savedFont->height);
|
2012-08-05 21:36:49 +02:00
|
|
|
|
cairo_t *cr = cairo_create(surface);
|
2013-11-18 23:17:44 +01:00
|
|
|
|
PangoLayout *layout = create_layout_with_dpi(cr);
|
2013-09-27 15:55:49 +02:00
|
|
|
|
gint height;
|
|
|
|
|
|
2012-08-05 21:36:49 +02:00
|
|
|
|
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);
|
|
|
|
|
|
2015-02-12 20:45:34 +01:00
|
|
|
|
if (is_markup)
|
|
|
|
|
pango_layout_set_markup(layout, text, text_len);
|
|
|
|
|
else
|
|
|
|
|
pango_layout_set_text(layout, text, text_len);
|
2013-09-27 15:55:49 +02:00
|
|
|
|
|
2012-08-05 21:36:49 +02:00
|
|
|
|
/* Do the drawing */
|
|
|
|
|
cairo_set_source_rgb(cr, pango_font_red, pango_font_green, pango_font_blue);
|
|
|
|
|
pango_cairo_update_layout(cr, layout);
|
2013-09-27 15:55:49 +02:00
|
|
|
|
pango_layout_get_pixel_size(layout, NULL, &height);
|
2015-03-15 08:32:17 +01:00
|
|
|
|
/* Center the piece of text vertically if its height is smaller than the
|
|
|
|
|
* cached font height, and just let "high" symbols fall out otherwise. */
|
|
|
|
|
int yoffset = (height < savedFont->height ? 0.5 : 1) * (height - savedFont->height);
|
|
|
|
|
cairo_move_to(cr, x, y - yoffset);
|
2012-08-05 21:36:49 +02:00
|
|
|
|
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.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2015-02-12 20:45:34 +01:00
|
|
|
|
static int predict_text_width_pango(const char *text, size_t text_len, bool is_markup) {
|
2012-08-05 21:36:49 +02:00
|
|
|
|
/* 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);
|
2013-11-18 23:17:44 +01:00
|
|
|
|
PangoLayout *layout = create_layout_with_dpi(cr);
|
2012-08-05 21:36:49 +02:00
|
|
|
|
|
|
|
|
|
/* Get the font width */
|
|
|
|
|
gint width;
|
|
|
|
|
pango_layout_set_font_description(layout, savedFont->specific.pango_desc);
|
2015-02-12 20:45:34 +01:00
|
|
|
|
|
|
|
|
|
if (is_markup)
|
|
|
|
|
pango_layout_set_markup(layout, text, text_len);
|
|
|
|
|
else
|
|
|
|
|
pango_layout_set_text(layout, text, text_len);
|
|
|
|
|
|
2012-08-05 21:36:49 +02:00
|
|
|
|
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
|
|
|
|
|
|
2011-11-13 20:19:42 +01:00
|
|
|
|
/*
|
|
|
|
|
* Loads a font for usage, also getting its metrics. If fallback is true,
|
2015-02-10 21:11:40 +01:00
|
|
|
|
* the fonts 'fixed' or '-misc-*' will be loaded instead of exiting. If any
|
|
|
|
|
* font was previously loaded, it will be freed.
|
2011-11-13 20:19:42 +01:00
|
|
|
|
*
|
|
|
|
|
*/
|
2011-11-21 22:48:24 +01:00
|
|
|
|
i3Font load_font(const char *pattern, const bool fallback) {
|
2015-02-10 21:11:40 +01:00
|
|
|
|
/* if any font was previously loaded, free it now */
|
|
|
|
|
free_font();
|
|
|
|
|
|
2011-11-13 20:19:42 +01:00
|
|
|
|
i3Font font;
|
2011-11-15 00:39:03 +01:00
|
|
|
|
font.type = FONT_TYPE_NONE;
|
|
|
|
|
|
2014-08-02 07:01:15 +02:00
|
|
|
|
/* No XCB connction, return early because we're just validating the
|
|
|
|
|
* configuration file. */
|
|
|
|
|
if (conn == NULL) {
|
|
|
|
|
return font;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-05 21:36:49 +02:00
|
|
|
|
#if PANGO_SUPPORT
|
|
|
|
|
/* Try to load a pango font if specified */
|
2012-11-07 09:54:17 +01:00
|
|
|
|
if (strlen(pattern) > strlen("pango:") && !strncmp(pattern, "pango:", strlen("pango:"))) {
|
2013-01-26 09:55:38 +01:00
|
|
|
|
const char *font_pattern = pattern + strlen("pango:");
|
|
|
|
|
if (load_pango_font(&font, font_pattern)) {
|
|
|
|
|
font.pattern = sstrdup(pattern);
|
2012-11-07 09:54:17 +01:00
|
|
|
|
return font;
|
2013-01-26 09:55:38 +01:00
|
|
|
|
}
|
2012-11-07 09:54:17 +01:00
|
|
|
|
} else if (strlen(pattern) > strlen("xft:") && !strncmp(pattern, "xft:", strlen("xft:"))) {
|
2013-01-26 09:55:38 +01:00
|
|
|
|
const char *font_pattern = pattern + strlen("xft:");
|
|
|
|
|
if (load_pango_font(&font, font_pattern)) {
|
|
|
|
|
font.pattern = sstrdup(pattern);
|
2012-08-05 21:36:49 +02:00
|
|
|
|
return font;
|
2013-01-26 09:55:38 +01:00
|
|
|
|
}
|
2012-08-05 21:36:49 +02:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2011-11-13 20:19:42 +01:00
|
|
|
|
|
|
|
|
|
/* Send all our requests first */
|
2011-11-15 00:39:03 +01:00
|
|
|
|
font.specific.xcb.id = xcb_generate_id(conn);
|
|
|
|
|
xcb_void_cookie_t font_cookie = xcb_open_font_checked(conn, font.specific.xcb.id,
|
2014-06-15 19:07:02 +02:00
|
|
|
|
strlen(pattern), pattern);
|
2011-11-15 00:39:03 +01:00
|
|
|
|
xcb_query_font_cookie_t info_cookie = xcb_query_font(conn, font.specific.xcb.id);
|
2011-11-13 20:19:42 +01:00
|
|
|
|
|
|
|
|
|
/* Check for errors. If errors, fall back to default font. */
|
|
|
|
|
xcb_generic_error_t *error;
|
|
|
|
|
error = xcb_request_check(conn, font_cookie);
|
|
|
|
|
|
|
|
|
|
/* If we fail to open font, fall back to 'fixed' */
|
|
|
|
|
if (fallback && error != NULL) {
|
|
|
|
|
ELOG("Could not open font %s (X error %d). Trying fallback to 'fixed'.\n",
|
|
|
|
|
pattern, error->error_code);
|
|
|
|
|
pattern = "fixed";
|
2011-11-15 00:39:03 +01:00
|
|
|
|
font_cookie = xcb_open_font_checked(conn, font.specific.xcb.id,
|
2014-06-15 19:07:02 +02:00
|
|
|
|
strlen(pattern), pattern);
|
2011-11-15 00:39:03 +01:00
|
|
|
|
info_cookie = xcb_query_font(conn, font.specific.xcb.id);
|
2011-11-13 20:19:42 +01:00
|
|
|
|
|
|
|
|
|
/* Check if we managed to open 'fixed' */
|
|
|
|
|
error = xcb_request_check(conn, font_cookie);
|
|
|
|
|
|
|
|
|
|
/* Fall back to '-misc-*' if opening 'fixed' fails. */
|
|
|
|
|
if (error != NULL) {
|
|
|
|
|
ELOG("Could not open fallback font 'fixed', trying with '-misc-*'.\n");
|
|
|
|
|
pattern = "-misc-*";
|
2011-11-15 00:39:03 +01:00
|
|
|
|
font_cookie = xcb_open_font_checked(conn, font.specific.xcb.id,
|
2014-06-15 19:07:02 +02:00
|
|
|
|
strlen(pattern), pattern);
|
2011-11-15 00:39:03 +01:00
|
|
|
|
info_cookie = xcb_query_font(conn, font.specific.xcb.id);
|
2011-11-13 20:19:42 +01:00
|
|
|
|
|
|
|
|
|
if ((error = xcb_request_check(conn, font_cookie)) != NULL)
|
|
|
|
|
errx(EXIT_FAILURE, "Could open neither requested font nor fallbacks "
|
2014-06-15 19:07:02 +02:00
|
|
|
|
"(fixed or -misc-*): X11 error %d",
|
|
|
|
|
error->error_code);
|
2011-11-13 20:19:42 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-26 09:55:38 +01:00
|
|
|
|
font.pattern = sstrdup(pattern);
|
2012-08-13 13:27:00 +02:00
|
|
|
|
LOG("Using X font %s\n", pattern);
|
2012-08-12 12:20:51 +02:00
|
|
|
|
|
2011-11-13 20:19:42 +01:00
|
|
|
|
/* Get information (height/name) for this font */
|
2011-11-15 00:39:03 +01:00
|
|
|
|
if (!(font.specific.xcb.info = xcb_query_font_reply(conn, info_cookie, NULL)))
|
2011-11-13 20:19:42 +01:00
|
|
|
|
errx(EXIT_FAILURE, "Could not load font \"%s\"", pattern);
|
|
|
|
|
|
|
|
|
|
/* Get the font table, if possible */
|
2011-11-15 00:39:03 +01:00
|
|
|
|
if (xcb_query_font_char_infos_length(font.specific.xcb.info) == 0)
|
|
|
|
|
font.specific.xcb.table = NULL;
|
2011-11-27 20:45:24 +01:00
|
|
|
|
else
|
2011-11-15 00:39:03 +01:00
|
|
|
|
font.specific.xcb.table = xcb_query_font_char_infos(font.specific.xcb.info);
|
2011-11-13 20:19:42 +01:00
|
|
|
|
|
|
|
|
|
/* Calculate the font height */
|
2011-11-15 00:39:03 +01:00
|
|
|
|
font.height = font.specific.xcb.info->font_ascent + font.specific.xcb.info->font_descent;
|
2011-11-13 20:19:42 +01:00
|
|
|
|
|
2011-11-15 00:39:03 +01:00
|
|
|
|
/* Set the font type and return successfully */
|
|
|
|
|
font.type = FONT_TYPE_XCB;
|
2011-11-13 20:19:42 +01:00
|
|
|
|
return font;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2011-11-24 00:12:36 +01:00
|
|
|
|
* Defines the font to be used for the forthcoming calls.
|
2011-11-13 20:19:42 +01:00
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void set_font(i3Font *font) {
|
|
|
|
|
savedFont = font;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-24 00:12:36 +01:00
|
|
|
|
/*
|
2015-02-10 21:11:40 +01:00
|
|
|
|
* Frees the resources taken by the current font. If no font was previously
|
|
|
|
|
* loaded, it simply returns.
|
2011-11-24 00:12:36 +01:00
|
|
|
|
*
|
|
|
|
|
*/
|
2012-03-31 10:53:04 +02:00
|
|
|
|
void free_font(void) {
|
2015-02-10 21:11:40 +01:00
|
|
|
|
/* if there is no saved font, simply return */
|
|
|
|
|
if (savedFont == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-01-26 09:55:38 +01:00
|
|
|
|
free(savedFont->pattern);
|
2011-11-15 00:39:03 +01:00
|
|
|
|
switch (savedFont->type) {
|
|
|
|
|
case FONT_TYPE_NONE:
|
|
|
|
|
/* Nothing to do */
|
|
|
|
|
break;
|
|
|
|
|
case FONT_TYPE_XCB: {
|
|
|
|
|
/* Close the font and free the info */
|
|
|
|
|
xcb_close_font(conn, savedFont->specific.xcb.id);
|
|
|
|
|
if (savedFont->specific.xcb.info)
|
|
|
|
|
free(savedFont->specific.xcb.info);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-08-05 21:36:49 +02:00
|
|
|
|
#if PANGO_SUPPORT
|
|
|
|
|
case FONT_TYPE_PANGO:
|
|
|
|
|
/* Free the font description */
|
|
|
|
|
pango_font_description_free(savedFont->specific.pango_desc);
|
|
|
|
|
break;
|
|
|
|
|
#endif
|
2011-11-15 00:39:03 +01:00
|
|
|
|
default:
|
|
|
|
|
assert(false);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-02-10 21:11:40 +01:00
|
|
|
|
|
|
|
|
|
savedFont = NULL;
|
2011-11-24 00:12:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 23:20:18 +01:00
|
|
|
|
/*
|
|
|
|
|
* Defines the colors to be used for the forthcoming draw_text calls.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void set_font_colors(xcb_gcontext_t gc, uint32_t foreground, uint32_t background) {
|
|
|
|
|
assert(savedFont != NULL);
|
2011-11-15 00:39:03 +01:00
|
|
|
|
|
|
|
|
|
switch (savedFont->type) {
|
|
|
|
|
case FONT_TYPE_NONE:
|
|
|
|
|
/* Nothing to do */
|
|
|
|
|
break;
|
|
|
|
|
case FONT_TYPE_XCB: {
|
|
|
|
|
/* Change the font and colors in the GC */
|
|
|
|
|
uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
|
2014-06-15 19:07:02 +02:00
|
|
|
|
uint32_t values[] = {foreground, background, savedFont->specific.xcb.id};
|
2011-11-15 00:39:03 +01:00
|
|
|
|
xcb_change_gc(conn, gc, mask, values);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-08-05 21:36:49 +02:00
|
|
|
|
#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
|
2011-11-15 00:39:03 +01:00
|
|
|
|
default:
|
|
|
|
|
assert(false);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-11-14 23:20:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-07 22:30:37 +02:00
|
|
|
|
static int predict_text_width_xcb(const xcb_char2b_t *text, size_t text_len);
|
|
|
|
|
|
2012-08-07 20:28:39 +02:00
|
|
|
|
static void draw_text_xcb(const xcb_char2b_t *text, size_t text_len, xcb_drawable_t drawable,
|
2014-06-15 19:07:02 +02:00
|
|
|
|
xcb_gcontext_t gc, int x, int y, int max_width) {
|
2011-11-13 20:19:42 +01:00
|
|
|
|
/* X11 coordinates for fonts start at the baseline */
|
2011-11-15 00:39:03 +01:00
|
|
|
|
int pos_y = y + savedFont->specific.xcb.info->font_ascent;
|
2011-11-13 20:19:42 +01:00
|
|
|
|
|
|
|
|
|
/* The X11 protocol limits text drawing to 255 chars, so we may need
|
|
|
|
|
* multiple calls */
|
|
|
|
|
int offset = 0;
|
|
|
|
|
for (;;) {
|
|
|
|
|
/* Calculate the size of this chunk */
|
2011-11-21 22:48:24 +01:00
|
|
|
|
int chunk_size = (text_len > 255 ? 255 : text_len);
|
2012-08-07 20:28:39 +02:00
|
|
|
|
const xcb_char2b_t *chunk = text + offset;
|
2011-11-13 20:19:42 +01:00
|
|
|
|
|
|
|
|
|
/* Draw it */
|
2012-08-07 20:28:39 +02:00
|
|
|
|
xcb_image_text_16(conn, chunk_size, drawable, gc, x, pos_y, chunk);
|
2011-11-13 20:19:42 +01:00
|
|
|
|
|
|
|
|
|
/* Advance the offset and length of the text to draw */
|
|
|
|
|
offset += chunk_size;
|
|
|
|
|
text_len -= chunk_size;
|
|
|
|
|
|
|
|
|
|
/* Check if we're done */
|
|
|
|
|
if (text_len == 0)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* Advance pos_x based on the predicted text width */
|
2012-08-07 22:30:37 +02:00
|
|
|
|
x += predict_text_width_xcb(chunk, chunk_size);
|
2011-11-13 20:19:42 +01:00
|
|
|
|
}
|
2012-08-07 20:28:39 +02:00
|
|
|
|
}
|
2011-11-13 20:19:42 +01:00
|
|
|
|
|
2012-08-07 20:28:39 +02:00
|
|
|
|
/*
|
|
|
|
|
* Draws text onto the specified X drawable (normally a pixmap) at the
|
|
|
|
|
* specified coordinates (from the top left corner of the leftmost, uppermost
|
|
|
|
|
* glyph) and using the provided gc.
|
|
|
|
|
*
|
|
|
|
|
* Text must be specified as an i3String.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void draw_text(i3String *text, xcb_drawable_t drawable,
|
|
|
|
|
xcb_gcontext_t gc, int x, int y, int max_width) {
|
|
|
|
|
assert(savedFont != NULL);
|
|
|
|
|
|
2011-11-15 00:39:03 +01:00
|
|
|
|
switch (savedFont->type) {
|
|
|
|
|
case FONT_TYPE_NONE:
|
|
|
|
|
/* Nothing to do */
|
|
|
|
|
return;
|
|
|
|
|
case FONT_TYPE_XCB:
|
|
|
|
|
draw_text_xcb(i3string_as_ucs2(text), i3string_get_num_glyphs(text),
|
2014-06-15 19:07:02 +02:00
|
|
|
|
drawable, gc, x, y, max_width);
|
2011-11-15 00:39:03 +01:00
|
|
|
|
break;
|
2012-08-05 21:36:49 +02:00
|
|
|
|
#if PANGO_SUPPORT
|
|
|
|
|
case FONT_TYPE_PANGO:
|
|
|
|
|
/* Render the text using Pango */
|
|
|
|
|
draw_text_pango(i3string_as_utf8(text), i3string_get_num_bytes(text),
|
2015-02-12 20:45:34 +01:00
|
|
|
|
drawable, x, y, max_width, i3string_is_markup(text));
|
2012-08-05 21:36:49 +02:00
|
|
|
|
return;
|
|
|
|
|
#endif
|
2011-11-15 00:39:03 +01:00
|
|
|
|
default:
|
|
|
|
|
assert(false);
|
|
|
|
|
}
|
2012-08-07 20:28:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* ASCII version of draw_text to print static strings.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
void draw_text_ascii(const char *text, xcb_drawable_t drawable,
|
2014-06-15 19:07:02 +02:00
|
|
|
|
xcb_gcontext_t gc, int x, int y, int max_width) {
|
2012-08-07 20:28:39 +02:00
|
|
|
|
assert(savedFont != NULL);
|
|
|
|
|
|
2011-11-15 00:39:03 +01:00
|
|
|
|
switch (savedFont->type) {
|
|
|
|
|
case FONT_TYPE_NONE:
|
|
|
|
|
/* Nothing to do */
|
|
|
|
|
return;
|
2014-06-15 19:07:02 +02:00
|
|
|
|
case FONT_TYPE_XCB: {
|
2011-11-15 00:39:03 +01:00
|
|
|
|
size_t text_len = strlen(text);
|
|
|
|
|
if (text_len > 255) {
|
|
|
|
|
/* The text is too long to draw it directly to X */
|
|
|
|
|
i3String *str = i3string_from_utf8(text);
|
|
|
|
|
draw_text(str, drawable, gc, x, y, max_width);
|
|
|
|
|
i3string_free(str);
|
|
|
|
|
} else {
|
|
|
|
|
/* X11 coordinates for fonts start at the baseline */
|
|
|
|
|
int pos_y = y + savedFont->specific.xcb.info->font_ascent;
|
|
|
|
|
|
|
|
|
|
xcb_image_text_8(conn, text_len, drawable, gc, x, pos_y, text);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-08-05 21:36:49 +02:00
|
|
|
|
#if PANGO_SUPPORT
|
|
|
|
|
case FONT_TYPE_PANGO:
|
|
|
|
|
/* Render the text using Pango */
|
|
|
|
|
draw_text_pango(text, strlen(text),
|
2015-02-12 20:45:34 +01:00
|
|
|
|
drawable, x, y, max_width, false);
|
2012-08-05 21:36:49 +02:00
|
|
|
|
return;
|
|
|
|
|
#endif
|
2011-11-15 00:39:03 +01:00
|
|
|
|
default:
|
|
|
|
|
assert(false);
|
2012-08-07 20:28:39 +02:00
|
|
|
|
}
|
2011-11-13 20:19:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-07 22:30:37 +02:00
|
|
|
|
static int xcb_query_text_width(const xcb_char2b_t *text, size_t text_len) {
|
2011-11-21 22:48:24 +01:00
|
|
|
|
/* Make the user know we’re using the slow path, but only once. */
|
|
|
|
|
static bool first_invocation = true;
|
|
|
|
|
if (first_invocation) {
|
2011-11-13 20:19:42 +01:00
|
|
|
|
fprintf(stderr, "Using slow code path for text extents\n");
|
2011-11-21 22:48:24 +01:00
|
|
|
|
first_invocation = false;
|
2011-11-13 20:19:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Query the text width */
|
|
|
|
|
xcb_generic_error_t *error;
|
|
|
|
|
xcb_query_text_extents_cookie_t cookie = xcb_query_text_extents(conn,
|
2014-06-15 19:07:02 +02:00
|
|
|
|
savedFont->specific.xcb.id, text_len, (xcb_char2b_t *)text);
|
2011-11-13 20:19:42 +01:00
|
|
|
|
xcb_query_text_extents_reply_t *reply = xcb_query_text_extents_reply(conn,
|
2014-06-15 19:07:02 +02:00
|
|
|
|
cookie, &error);
|
2011-11-13 20:19:42 +01:00
|
|
|
|
if (reply == NULL) {
|
|
|
|
|
/* We return a safe estimate because a rendering error is better than
|
2015-03-12 05:41:43 +01:00
|
|
|
|
* a crash. Plus, the user will see the error in their log. */
|
2011-11-13 20:19:42 +01:00
|
|
|
|
fprintf(stderr, "Could not get text extents (X error code %d)\n",
|
|
|
|
|
error->error_code);
|
2011-11-15 00:39:03 +01:00
|
|
|
|
return savedFont->specific.xcb.info->max_bounds.character_width * text_len;
|
2011-11-13 20:19:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int width = reply->overall_width;
|
|
|
|
|
free(reply);
|
|
|
|
|
return width;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-07 22:30:37 +02:00
|
|
|
|
static int predict_text_width_xcb(const xcb_char2b_t *input, size_t text_len) {
|
|
|
|
|
if (text_len == 0)
|
|
|
|
|
return 0;
|
2011-11-13 20:19:42 +01:00
|
|
|
|
|
|
|
|
|
int width;
|
2011-11-15 00:39:03 +01:00
|
|
|
|
if (savedFont->specific.xcb.table == NULL) {
|
2011-11-13 20:19:42 +01:00
|
|
|
|
/* If we don't have a font table, fall back to querying the server */
|
|
|
|
|
width = xcb_query_text_width(input, text_len);
|
2011-11-21 22:48:24 +01:00
|
|
|
|
} else {
|
2011-11-13 20:19:42 +01:00
|
|
|
|
/* Save some pointers for convenience */
|
2011-11-15 00:39:03 +01:00
|
|
|
|
xcb_query_font_reply_t *font_info = savedFont->specific.xcb.info;
|
|
|
|
|
xcb_charinfo_t *font_table = savedFont->specific.xcb.table;
|
2011-11-13 20:19:42 +01:00
|
|
|
|
|
|
|
|
|
/* Calculate the width using the font table */
|
|
|
|
|
width = 0;
|
|
|
|
|
for (size_t i = 0; i < text_len; i++) {
|
|
|
|
|
xcb_charinfo_t *info;
|
|
|
|
|
int row = input[i].byte1;
|
|
|
|
|
int col = input[i].byte2;
|
|
|
|
|
|
2011-11-21 22:48:24 +01:00
|
|
|
|
if (row < font_info->min_byte1 ||
|
|
|
|
|
row > font_info->max_byte1 ||
|
|
|
|
|
col < font_info->min_char_or_byte2 ||
|
|
|
|
|
col > font_info->max_char_or_byte2)
|
2011-11-13 20:19:42 +01:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
/* Don't you ask me, how this one works… (Merovius) */
|
|
|
|
|
info = &font_table[((row - font_info->min_byte1) *
|
2014-06-15 19:07:02 +02:00
|
|
|
|
(font_info->max_char_or_byte2 - font_info->min_char_or_byte2 + 1)) +
|
|
|
|
|
(col - font_info->min_char_or_byte2)];
|
2011-11-13 20:19:42 +01:00
|
|
|
|
|
|
|
|
|
if (info->character_width != 0 ||
|
2014-06-15 19:07:02 +02:00
|
|
|
|
(info->right_side_bearing |
|
|
|
|
|
info->left_side_bearing |
|
|
|
|
|
info->ascent |
|
|
|
|
|
info->descent) != 0) {
|
2011-11-13 20:19:42 +01:00
|
|
|
|
width += info->character_width;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return width;
|
|
|
|
|
}
|
2012-08-07 22:30:37 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Predict the text width in pixels for the given text. Text must be
|
|
|
|
|
* specified as an i3String.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
int predict_text_width(i3String *text) {
|
2011-11-15 00:39:03 +01:00
|
|
|
|
assert(savedFont != NULL);
|
|
|
|
|
|
|
|
|
|
switch (savedFont->type) {
|
|
|
|
|
case FONT_TYPE_NONE:
|
|
|
|
|
/* Nothing to do */
|
|
|
|
|
return 0;
|
|
|
|
|
case FONT_TYPE_XCB:
|
|
|
|
|
return predict_text_width_xcb(i3string_as_ucs2(text), i3string_get_num_glyphs(text));
|
2012-08-05 21:36:49 +02:00
|
|
|
|
#if PANGO_SUPPORT
|
|
|
|
|
case FONT_TYPE_PANGO:
|
|
|
|
|
/* Calculate extents using Pango */
|
2015-02-12 20:45:34 +01:00
|
|
|
|
return predict_text_width_pango(i3string_as_utf8(text), i3string_get_num_bytes(text),
|
|
|
|
|
i3string_is_markup(text));
|
2012-08-05 21:36:49 +02:00
|
|
|
|
#endif
|
2011-11-15 00:39:03 +01:00
|
|
|
|
default:
|
|
|
|
|
assert(false);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2012-08-07 22:30:37 +02:00
|
|
|
|
}
|