From 8d645d0de6f02de4bf0dac701892f7bd6d87e5f1 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Fri, 5 Mar 2021 11:36:09 +0100 Subject: [PATCH 1/2] Remove draw_text_ascii() This function is unused since commit fa488d721dfd1e1 from 2017. Signed-off-by: Uli Schlachter --- include/libi3.h | 7 ------- libi3/font.c | 35 ----------------------------------- 2 files changed, 42 deletions(-) diff --git a/include/libi3.h b/include/libi3.h index e693c3ad4..7945134ce 100644 --- a/include/libi3.h +++ b/include/libi3.h @@ -451,13 +451,6 @@ bool font_is_pango(void); void draw_text(i3String *text, xcb_drawable_t drawable, xcb_gcontext_t gc, xcb_visualtype_t *visual, int x, int y, int max_width); -/** - * ASCII version of draw_text to print static strings. - * - */ -void draw_text_ascii(const char *text, xcb_drawable_t drawable, - xcb_gcontext_t gc, int x, int y, int max_width); - /** * Predict the text width in pixels for the given text. Text must be * specified as an i3String. diff --git a/libi3/font.c b/libi3/font.c index 5ed58b8d7..26e90e44a 100644 --- a/libi3/font.c +++ b/libi3/font.c @@ -380,41 +380,6 @@ void draw_text(i3String *text, xcb_drawable_t drawable, xcb_gcontext_t gc, } } -/* - * ASCII version of draw_text to print static strings. - * - */ -void draw_text_ascii(const char *text, xcb_drawable_t drawable, - xcb_gcontext_t gc, int x, int y, int max_width) { - assert(savedFont != NULL); - - switch (savedFont->type) { - case FONT_TYPE_NONE: - /* Nothing to do */ - return; - case FONT_TYPE_XCB: { - 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, NULL, 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; - } - case FONT_TYPE_PANGO: - /* Render the text using Pango */ - draw_text_pango(text, strlen(text), - drawable, root_visual_type, x, y, max_width, false); - return; - } -} - static int xcb_query_text_width(const xcb_char2b_t *text, size_t text_len) { /* Make the user know we’re using the slow path, but only once. */ static bool first_invocation = true; From b23c8875f70c5c142ba499d4704b51802138d071 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Fri, 5 Mar 2021 11:37:03 +0100 Subject: [PATCH 2/2] font: Get rid of temporary cairo surface i3 actually manages to have two different cairo surfaces referring to the same drawable. One comes from the code in draw_util. The second is temporarily created while rendering text via draw_text(). No idea how well cairo handles this case. This commit instead changes the code to pass the already existing cairo surface from the caller through. This might or might not fix https://github.com/i3/i3/pull/4357. My thinking here is that cairo now knows the actual size of the drawable and thus does not clip the drawing to a smaller size. Signed-off-by: Uli Schlachter --- include/libi3.h | 4 +++- libi3/draw_util.c | 2 +- libi3/font.c | 14 ++++---------- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/include/libi3.h b/include/libi3.h index 7945134ce..d17892f8b 100644 --- a/include/libi3.h +++ b/include/libi3.h @@ -445,11 +445,13 @@ bool font_is_pango(void); * specified coordinates (from the top left corner of the leftmost, uppermost * glyph) and using the provided gc. * + * The given cairo surface must refer to the specified X drawable. + * * Text must be specified as an i3String. * */ void draw_text(i3String *text, xcb_drawable_t drawable, xcb_gcontext_t gc, - xcb_visualtype_t *visual, int x, int y, int max_width); + cairo_surface_t *surface, int x, int y, int max_width); /** * Predict the text width in pixels for the given text. Text must be diff --git a/libi3/draw_util.c b/libi3/draw_util.c index 313dc29ac..18cd0459c 100644 --- a/libi3/draw_util.c +++ b/libi3/draw_util.c @@ -133,7 +133,7 @@ void draw_util_text(i3String *text, surface_t *surface, color_t fg_color, color_ CAIRO_SURFACE_FLUSH(surface->surface); set_font_colors(surface->gc, fg_color, bg_color); - draw_text(text, surface->id, surface->gc, surface->visual_type, x, y, max_width); + draw_text(text, surface->id, surface->gc, surface->surface, x, y, max_width); /* Notify cairo that we (possibly) used another way to draw on the surface. */ cairo_surface_mark_dirty(surface->surface); diff --git a/libi3/font.c b/libi3/font.c index 26e90e44a..514328f62 100644 --- a/libi3/font.c +++ b/libi3/font.c @@ -82,12 +82,10 @@ static bool load_pango_font(i3Font *font, const char *desc) { * */ static void draw_text_pango(const char *text, size_t text_len, - xcb_drawable_t drawable, xcb_visualtype_t *visual, int x, int y, - int max_width, bool pango_markup) { + xcb_drawable_t drawable, cairo_surface_t *surface, + int x, int y, int max_width, bool pango_markup) { /* Create the Pango layout */ /* root_visual_type is cached in load_pango_font */ - cairo_surface_t *surface = cairo_xcb_surface_create(conn, drawable, - visual, x + max_width, y + savedFont->height); cairo_t *cr = cairo_create(surface); PangoLayout *layout = create_layout_with_dpi(cr); gint height; @@ -115,7 +113,6 @@ static void draw_text_pango(const char *text, size_t text_len, /* Free resources */ g_object_unref(layout); cairo_destroy(cr); - cairo_surface_destroy(surface); } /* @@ -358,11 +355,8 @@ static void draw_text_xcb(const xcb_char2b_t *text, size_t text_len, xcb_drawabl * */ void draw_text(i3String *text, xcb_drawable_t drawable, xcb_gcontext_t gc, - xcb_visualtype_t *visual, int x, int y, int max_width) { + cairo_surface_t *surface, int x, int y, int max_width) { assert(savedFont != NULL); - if (visual == NULL) { - visual = root_visual_type; - } switch (savedFont->type) { case FONT_TYPE_NONE: @@ -375,7 +369,7 @@ void draw_text(i3String *text, xcb_drawable_t drawable, xcb_gcontext_t gc, case FONT_TYPE_PANGO: /* Render the text using Pango */ draw_text_pango(i3string_as_utf8(text), i3string_get_num_bytes(text), - drawable, visual, x, y, max_width, i3string_is_markup(text)); + drawable, surface, x, y, max_width, i3string_is_markup(text)); return; } }