diff --git a/gdk/gdkcairo.c b/gdk/gdkcairo.c index f9730d995..046bc5de7 100644 --- a/gdk/gdkcairo.c +++ b/gdk/gdkcairo.c @@ -33,6 +33,8 @@ gdk_ensure_surface_flush (gpointer surface) cairo_surface_destroy (surface); } +static const cairo_user_data_key_t surface_flush_key; + /** * gdk_cairo_create: * @drawable: a #GdkDrawable @@ -53,7 +55,6 @@ gdk_ensure_surface_flush (gpointer surface) cairo_t * gdk_cairo_create (GdkDrawable *drawable) { - static const cairo_user_data_key_t key; cairo_surface_t *surface; cairo_t *cr; @@ -70,7 +71,29 @@ gdk_cairo_create (GdkDrawable *drawable) * on X11 (where flushing is a no-op). For details, see * https://bugzilla.gnome.org/show_bug.cgi?id=628291 */ - cairo_set_user_data (cr, &key, surface, gdk_ensure_surface_flush); + cairo_set_user_data (cr, &surface_flush_key, surface, gdk_ensure_surface_flush); + + return cr; +} + +/* gdk_cairo_create() for drawing that stays within @area (in the + * coordinates of @drawable): for a window whose clip has many + * rectangles, only those parts of it within @area make the clip */ +cairo_t * +_gdk_cairo_create_in_area (GdkDrawable *drawable, + const GdkRectangle *area) +{ + cairo_surface_t *surface; + cairo_t *cr; + + if (!GDK_IS_WINDOW (drawable) || + _gdk_window_get_cairo_clip_n_rects ((GdkWindow *) drawable) <= 2) + return gdk_cairo_create (drawable); + + surface = _gdk_drawable_ref_cairo_surface (drawable); + cr = cairo_create (surface); + _gdk_window_set_cairo_clip_in_area ((GdkWindow *) drawable, cr, area); + cairo_set_user_data (cr, &surface_flush_key, surface, gdk_ensure_surface_flush); return cr; } diff --git a/gdk/gdkinternals.h b/gdk/gdkinternals.h index 82ce0d71e..f2b41bc58 100644 --- a/gdk/gdkinternals.h +++ b/gdk/gdkinternals.h @@ -406,6 +406,13 @@ gboolean _gdk_rgb_composite (GdkColormap *colormap, gboolean _gdk_rgb_prefers_own_blending (GdkColormap *colormap, GdkRgbDither dither); +gint _gdk_window_get_cairo_clip_n_rects (GdkWindow *window); +void _gdk_window_set_cairo_clip_in_area (GdkWindow *window, + cairo_t *cr, + const GdkRectangle *area); +cairo_t *_gdk_cairo_create_in_area (GdkDrawable *drawable, + const GdkRectangle *area); + void _gdk_gc_update_context (GdkGC *gc, cairo_t *cr, const GdkColor *override_foreground, diff --git a/gdk/gdkpango.c b/gdk/gdkpango.c index 756507847..5b36602f7 100644 --- a/gdk/gdkpango.c +++ b/gdk/gdkpango.c @@ -59,6 +59,12 @@ struct _GdkPangoRendererPrivate GdkGC *base_gc; gboolean gc_changed; + + /* what gdk_draw_layout[_line]_with_colors() draws, where, for the + * clip (see get_cairo_context()) */ + PangoLayout *area_layout; + PangoLayoutLine *area_line; + gint area_x, area_y; }; static PangoAttrType gdk_pango_attr_stipple_type; @@ -155,6 +161,41 @@ color_equal (const PangoColor *c1, const PangoColor *c2) return FALSE; } +/* Where gdk_draw_layout[_line]_with_colors() draws: the ink and the + * logical extents, with room around for the emboss and for pixels of + * glyphs that antialiasing puts a little outside their ink. Only asked + * for when the clip of the window has more than a few rectangles. */ +#define DRAW_AREA_MARGIN 4 + +static gboolean +get_draw_area (GdkPangoRendererPrivate *priv, + GdkRectangle *area) +{ + PangoRectangle ink, logical; + + if (priv->area_layout) + pango_layout_get_pixel_extents (priv->area_layout, &ink, &logical); + else if (priv->area_line) + pango_layout_line_get_pixel_extents (priv->area_line, &ink, &logical); + else + return FALSE; + + if (ink.width <= 0 || ink.height <= 0) + ink = logical; + + area->x = MIN (ink.x, logical.x); + area->y = MIN (ink.y, logical.y); + area->width = MAX (ink.x + ink.width, logical.x + logical.width) - area->x; + area->height = MAX (ink.y + ink.height, logical.y + logical.height) - area->y; + + area->x += priv->area_x - DRAW_AREA_MARGIN; + area->y += priv->area_y - DRAW_AREA_MARGIN; + area->width += 2 * DRAW_AREA_MARGIN; + area->height += 2 * DRAW_AREA_MARGIN; + + return TRUE; +} + static cairo_t * get_cairo_context (GdkPangoRenderer *gdk_renderer, PangoRenderPart part) @@ -166,8 +207,15 @@ get_cairo_context (GdkPangoRenderer *gdk_renderer, if (!priv->cr) { const PangoMatrix *matrix; + GdkRectangle area; - priv->cr = gdk_cairo_create (priv->drawable); + if ((priv->area_layout || priv->area_line) && + GDK_IS_WINDOW (priv->drawable) && + _gdk_window_get_cairo_clip_n_rects ((GdkWindow *) priv->drawable) > 2 && + get_draw_area (priv, &area)) + priv->cr = _gdk_cairo_create_in_area (priv->drawable, &area); + else + priv->cr = gdk_cairo_create (priv->drawable); cr_is_new = TRUE; matrix = pango_renderer_get_matrix (renderer); @@ -842,6 +890,9 @@ release_renderer (PangoRenderer *renderer) gdk_pango_renderer_set_drawable (gdk_renderer, NULL); gdk_pango_renderer_set_gc (gdk_renderer, NULL); + + gdk_renderer->priv->area_layout = NULL; + gdk_renderer->priv->area_line = NULL; } /** @@ -914,7 +965,16 @@ gdk_draw_layout_line_with_colors (GdkDrawable *drawable, y = 0; } else - pango_renderer_set_matrix (renderer, NULL); + { + GdkPangoRendererPrivate *priv = GDK_PANGO_RENDERER (renderer)->priv; + + pango_renderer_set_matrix (renderer, NULL); + + /* without a matrix, the extents of the line say where it goes */ + priv->area_line = line; + priv->area_x = x; + priv->area_y = y; + } pango_renderer_draw_layout_line (renderer, line, x * PANGO_SCALE, y * PANGO_SCALE); @@ -994,7 +1054,16 @@ gdk_draw_layout_with_colors (GdkDrawable *drawable, y = 0; } else - pango_renderer_set_matrix (renderer, NULL); + { + GdkPangoRendererPrivate *priv = GDK_PANGO_RENDERER (renderer)->priv; + + pango_renderer_set_matrix (renderer, NULL); + + /* without a matrix, the extents of the layout say where it goes */ + priv->area_layout = layout; + priv->area_x = x; + priv->area_y = y; + } pango_renderer_draw_layout (renderer, layout, x * PANGO_SCALE, y * PANGO_SCALE); diff --git a/gdk/gdkwindow.c b/gdk/gdkwindow.c index e75aebb07..580366e47 100644 --- a/gdk/gdkwindow.c +++ b/gdk/gdkwindow.c @@ -34,6 +34,7 @@ #include "gdkdrawable.h" #include "gdkintl.h" #include "gdkscreen.h" +#include "gdkregion-generic.h" #include "gdkmarshalers.h" #include "gdkalias.h" @@ -5249,6 +5250,71 @@ gdk_window_set_cairo_clip (GdkDrawable *drawable, } } +/* The region gdk_window_set_cairo_clip() clips to, or %NULL if it + * sets no clip */ +static const GdkRegion * +gdk_window_get_cairo_clip_region (GdkWindowObject *private) +{ + GdkWindowPaint *paint; + + if (!private->paint_stack) + return private->clip_region_with_children; + + paint = private->paint_stack->data; + return paint->uses_implicit ? paint->region : NULL; +} + +/* The number of rectangles of the clip a context made with + * gdk_cairo_create() for @window gets, 0 if it gets none */ +gint +_gdk_window_get_cairo_clip_n_rects (GdkWindow *window) +{ + const GdkRegion *region; + + region = gdk_window_get_cairo_clip_region ((GdkWindowObject *) window); + + return region ? region->numRects : 0; +} + +/* Clips @cr as gdk_window_set_cairo_clip() does, for drawing that + * stays within @area: only the parts of the clip region inside @area + * are made into the clip, which saves building a path out of every + * rectangle of a region with many (the parts of a window around its + * child windows, or an expose of many separate areas). */ +void +_gdk_window_set_cairo_clip_in_area (GdkWindow *window, + cairo_t *cr, + const GdkRectangle *area) +{ + const GdkRegion *region; + gint i; + + region = gdk_window_get_cairo_clip_region ((GdkWindowObject *) window); + + cairo_reset_clip (cr); + if (!region) + return; + + cairo_save (cr); + cairo_identity_matrix (cr); + cairo_new_path (cr); + + for (i = 0; i < region->numRects; i++) + { + const GdkRegionBox *box = ®ion->rects[i]; + gint x1 = MAX (box->x1, area->x); + gint y1 = MAX (box->y1, area->y); + gint x2 = MIN (box->x2, area->x + area->width); + gint y2 = MIN (box->y2, area->y + area->height); + + if (x1 < x2 && y1 < y2) + cairo_rectangle (cr, x1, y1, x2 - x1, y2 - y1); + } + + cairo_restore (cr); + cairo_clip (cr); +} + /* Code for dirty-region queueing */ static GSList *update_windows = NULL;