Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
font: Get rid of temporary cairo surface
Browse files Browse the repository at this point in the history
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 i3/i3#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 <psychon@znc.in>
  • Loading branch information
psychon committed Mar 5, 2021
1 parent 8d645d0 commit b23c887
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
4 changes: 3 additions & 1 deletion include/libi3.h
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libi3/draw_util.c
Expand Up @@ -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);
Expand Down
14 changes: 4 additions & 10 deletions libi3/font.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/*
Expand Down Expand Up @@ -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:
Expand All @@ -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;
}
}
Expand Down

0 comments on commit b23c887

Please sign in to comment.