[gtk+/wip/matthiasc/gskpango: 11/15] wip: move node creation into the renderer



commit 40ab35e808187c3fb5919e1bbb178a437bb5a210
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Aug 30 13:52:40 2017 -0400

    wip: move node creation into the renderer
    
    Create a cairo node for each show_text_glyphs call.

 gtk/gskpango.c    |  270 ++++++++++++++--------------------------------------
 gtk/gskpango.h    |    7 +-
 gtk/gtksnapshot.c |   39 +++++----
 3 files changed, 99 insertions(+), 217 deletions(-)
---
diff --git a/gtk/gskpango.c b/gtk/gskpango.c
index 643af01..ef8aab3 100644
--- a/gtk/gskpango.c
+++ b/gtk/gskpango.c
@@ -19,6 +19,7 @@
 
 #include "config.h"
 
+#include "gsk/gsk.h"
 #include "gskpango.h"
 
 #include <math.h>
@@ -49,14 +50,16 @@ struct _GskPangoRenderer
 {
   PangoRenderer parent_instance;
 
-  cairo_t *cr;
-  gboolean do_path;
+  GtkSnapshot *snapshot;
+  const GdkRGBA *fg_color;
   gboolean has_show_text_glyphs;
   double x_offset, y_offset;
 
   /* house-keeping options */
   gboolean is_cached_renderer;
   gboolean cr_had_current_point;
+
+  graphene_rect_t bounds;
 };
 
 struct _GskPangoRendererClass
@@ -68,7 +71,8 @@ G_DEFINE_TYPE (GskPangoRenderer, gsk_pango_renderer, PANGO_TYPE_RENDERER)
 
 static void
 set_color (GskPangoRenderer *crenderer,
-           PangoRenderPart     part)
+           cairo_t          *cr,
+           PangoRenderPart   part)
 {
   PangoColor *color = pango_renderer_get_color ((PangoRenderer *) (crenderer), part);
   guint16 a = pango_renderer_get_alpha ((PangoRenderer *) (crenderer), part);
@@ -86,7 +90,7 @@ set_color (GskPangoRenderer *crenderer,
     }
   else
     {
-      cairo_pattern_t *pattern = cairo_get_source (crenderer->cr);
+      cairo_pattern_t *pattern = cairo_get_source (cr);
 
       if (pattern && cairo_pattern_get_type (pattern) == CAIRO_PATTERN_TYPE_SOLID)
         cairo_pattern_get_rgba (pattern, &red, &green, &blue, &alpha);
@@ -102,7 +106,7 @@ set_color (GskPangoRenderer *crenderer,
   if (a)
     alpha = a / 65535.;
 
-  cairo_set_source_rgba (crenderer->cr, red, green, blue, alpha);
+  cairo_set_source_rgba (cr, red, green, blue, alpha);
 }
 
 #if 0
@@ -118,81 +122,6 @@ gsk_pango_renderer_draw_frame (GskPangoRenderer *crenderer,
 {
   cairo_t *cr = crenderer->cr;
 
-  if (crenderer->do_path)
-    {
-      double d2 = line_width * .5, d = line_width;
-
-      /* we draw an outer box in one winding direction and an inner one in the
-       * opposite direction.  This works for both cairo windings rules.
-       *
-       * what we really want is cairo_stroke_to_path(), but that's not
-       * implemented in cairo yet.
-       */
-
-      /* outer */
-      cairo_rectangle (cr, x-d2, y-d2, width+d, height+d);
-
-      /* inner */
-      if (invalid)
-        {
-          /* delicacies of computing the joint... this is REALLY slow */
-
-          double alpha, tan_alpha2, cos_alpha;
-          double sx, sy;
-
-          alpha = atan2 (height, width);
-
-          tan_alpha2 = tan (alpha * .5);
-          if (tan_alpha2 < 1e-5 || (sx = d2 / tan_alpha2, 2. * sx > width - d))
-            sx = (width - d) * .5;
-
-          cos_alpha = cos (alpha);
-          if (cos_alpha < 1e-5 || (sy = d2 / cos_alpha, 2. * sy > height - d))
-            sy = (height - d) * .5;
-
-          /* top triangle */
-          cairo_new_sub_path (cr);
-          cairo_line_to (cr, x+width-sx, y+d2);
-          cairo_line_to (cr, x+sx, y+d2);
-          cairo_line_to (cr, x+.5*width, y+.5*height-sy);
-          cairo_close_path (cr);
-
-          /* bottom triangle */
-          cairo_new_sub_path (cr);
-          cairo_line_to (cr, x+width-sx, y+height-d2);
-          cairo_line_to (cr, x+.5*width, y+.5*height+sy);
-          cairo_line_to (cr, x+sx, y+height-d2);
-          cairo_close_path (cr);
-
-
-          alpha = G_PI_2 - alpha;
-          tan_alpha2 = tan (alpha * .5);
-          if (tan_alpha2 < 1e-5 || (sy = d2 / tan_alpha2, 2. * sy > height - d))
-            sy = (height - d) * .5;
-
-          cos_alpha = cos (alpha);
-          if (cos_alpha < 1e-5 || (sx = d2 / cos_alpha, 2. * sx > width - d))
-            sx = (width - d) * .5;
-
-          /* left triangle */
-          cairo_new_sub_path (cr);
-          cairo_line_to (cr, x+d2, y+sy);
-          cairo_line_to (cr, x+d2, y+height-sy);
-          cairo_line_to (cr, x+.5*width-sx, y+.5*height);
-          cairo_close_path (cr);
-
-          /* right triangle */
-          cairo_new_sub_path (cr);
-          cairo_line_to (cr, x+width-d2, y+sy);
-          cairo_line_to (cr, x+.5*width+sx, y+.5*height);
-          cairo_line_to (cr, x+width-d2, y+height-sy);
-          cairo_close_path (cr);
-        }
-      else
-        cairo_rectangle (cr, x+width-d2, y+d2, - (width-d), height-d);
-    }
-  else
-    {
       cairo_rectangle (cr, x, y, width, height);
 
       if (invalid)
@@ -214,7 +143,6 @@ gsk_pango_renderer_draw_frame (GskPangoRenderer *crenderer,
       cairo_set_line_join (cr, CAIRO_LINE_JOIN_MITER);
       cairo_set_miter_limit (cr, 2.);
       cairo_stroke (cr);
-    }
 }
 
 static void
@@ -322,11 +250,7 @@ gsk_pango_renderer_draw_unknown_glyph (GskPangoRenderer *crenderer,
 
           hexbox_string[0] = buf[row * cols + col];
 
-          if (crenderer->do_path)
-              cairo_text_path (crenderer->cr, hexbox_string);
-          else
-              cairo_show_text (crenderer->cr, hexbox_string);
-        }
+          cairo_show_text (crenderer->cr, hexbox_string);
     }
 
 done:
@@ -354,6 +278,7 @@ gsk_pango_renderer_show_text_glyphs (PangoRenderer        *renderer,
 {
   GskPangoRenderer *crenderer = (GskPangoRenderer *) (renderer);
 
+  cairo_t *cr;
   int i, count;
   int x_position = 0;
   cairo_glyph_t *cairo_glyphs;
@@ -361,11 +286,14 @@ gsk_pango_renderer_show_text_glyphs (PangoRenderer        *renderer,
   double base_x = crenderer->x_offset + (double)x / PANGO_SCALE;
   double base_y = crenderer->y_offset + (double)y / PANGO_SCALE;
 
-  cairo_save (crenderer->cr);
-  if (!crenderer->do_path)
-    set_color (crenderer, PANGO_RENDER_PART_FOREGROUND);
 
-  if (!_pango_cairo_font_install (font, crenderer->cr))
+  cr = gtk_snapshot_append_cairo (crenderer->snapshot, &crenderer->bounds, "Text<%dglyphs>", 
glyphs->num_glyphs);
+
+  gdk_cairo_set_source_rgba (cr, crenderer->fg_color);
+
+  set_color (crenderer, cr, PANGO_RENDER_PART_FOREGROUND);
+
+  if (!_pango_cairo_font_install (font, cr))
     {
       for (i = 0; i < glyphs->num_glyphs; i++)
         {
@@ -416,23 +344,20 @@ gsk_pango_renderer_show_text_glyphs (PangoRenderer        *renderer,
       x_position += gi->geometry.width;
     }
 
-  if (G_UNLIKELY (crenderer->do_path))
-    cairo_glyph_path (crenderer->cr, cairo_glyphs, count);
-  else
-    if (G_UNLIKELY (clusters))
-      cairo_show_text_glyphs (crenderer->cr,
+  if (G_UNLIKELY (clusters))
+      cairo_show_text_glyphs (cr,
                               text, text_len,
                               cairo_glyphs, count,
                               clusters, num_clusters,
                               backward ? CAIRO_TEXT_CLUSTER_FLAG_BACKWARD : 0);
-    else
-      cairo_show_glyphs (crenderer->cr, cairo_glyphs, count);
+  else
+      cairo_show_glyphs (cr, cairo_glyphs, count);
 
   if (cairo_glyphs != stack_glyphs)
     g_free (cairo_glyphs);
 
 done:
-  cairo_restore (crenderer->cr);
+  cairo_destroy (cr);
 }
 
 static void
@@ -442,13 +367,7 @@ gsk_pango_renderer_draw_glyphs (PangoRenderer     *renderer,
                                 int                x,
                                 int                y)
 {
-  gsk_pango_renderer_show_text_glyphs (renderer,
-                                       NULL, 0,
-                                       glyphs,
-                                       NULL, 0,
-                                       FALSE,
-                                       font,
-                                       x, y);
+  gsk_pango_renderer_show_text_glyphs (renderer, NULL, 0, glyphs, NULL, 0, FALSE, font, x, y);
 }
 
 static void
@@ -459,25 +378,19 @@ gsk_pango_renderer_draw_glyph_item (PangoRenderer  *renderer,
                                     int             y)
 {
   GskPangoRenderer *crenderer = (GskPangoRenderer *) (renderer);
-  PangoFont          *font      = glyph_item->item->analysis.font;
-  PangoGlyphString   *glyphs    = glyph_item->glyphs;
-  PangoItem          *item      = glyph_item->item;
-  gboolean            backward  = (item->analysis.level & 1) != 0;
+  PangoFont *font = glyph_item->item->analysis.font;
+  PangoGlyphString *glyphs = glyph_item->glyphs;
+  PangoItem *item = glyph_item->item;
+  gboolean backward = (item->analysis.level & 1) != 0;
 
-  PangoGlyphItemIter   iter;
+  PangoGlyphItemIter iter;
   cairo_text_cluster_t *cairo_clusters;
   cairo_text_cluster_t stack_clusters[STACK_ARRAY_LENGTH (cairo_text_cluster_t)];
   int num_clusters;
 
-  if (!crenderer->has_show_text_glyphs || crenderer->do_path)
+  if (!crenderer->has_show_text_glyphs)
     {
-      gsk_pango_renderer_show_text_glyphs (renderer,
-                                           NULL, 0,
-                                           glyphs,
-                                           NULL, 0,
-                                           FALSE,
-                                           font,
-                                           x, y);
+      gsk_pango_renderer_show_text_glyphs (renderer, NULL, 0, glyphs, NULL, 0, FALSE, font, x, y);
       return;
     }
 
@@ -519,12 +432,12 @@ gsk_pango_renderer_draw_glyph_item (PangoRenderer  *renderer,
     }
 
   gsk_pango_renderer_show_text_glyphs (renderer,
-                                         text + item->offset, item->length,
-                                         glyphs,
-                                         cairo_clusters, num_clusters,
-                                         backward,
-                                         font,
-                                         x, y);
+                                       text + item->offset, item->length,
+                                       glyphs,
+                                       cairo_clusters, num_clusters,
+                                       backward,
+                                       font,
+                                       x, y);
 
   if (cairo_clusters != stack_clusters)
     g_free (cairo_clusters);
@@ -538,26 +451,20 @@ gsk_pango_renderer_draw_rectangle (PangoRenderer     *renderer,
                                      int                width,
                                      int                height)
 {
+#if 0
   GskPangoRenderer *crenderer = (GskPangoRenderer *) (renderer);
 
-  if (!crenderer->do_path)
-    {
-      cairo_save (crenderer->cr);
-
-      set_color (crenderer, part);
-    }
+  cairo_save (crenderer->cr);
+  set_color (crenderer, part);
 
   cairo_rectangle (crenderer->cr,
                    crenderer->x_offset + (double)x / PANGO_SCALE,
                    crenderer->y_offset + (double)y / PANGO_SCALE,
                    (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
 
-  if (!crenderer->do_path)
-    {
-      cairo_fill (crenderer->cr);
-
-      cairo_restore (crenderer->cr);
-    }
+  cairo_fill (crenderer->cr);
+  cairo_restore (crenderer->cr);
+#endif
 }
 
 static void
@@ -570,6 +477,7 @@ gsk_pango_renderer_draw_trapezoid (PangoRenderer     *renderer,
                                      double             x12,
                                      double             x22)
 {
+#if 0
   GskPangoRenderer *crenderer = (GskPangoRenderer *) (renderer);
   cairo_t *cr;
   double x, y;
@@ -578,8 +486,7 @@ gsk_pango_renderer_draw_trapezoid (PangoRenderer     *renderer,
 
   cairo_save (cr);
 
-  if (!crenderer->do_path)
-    set_color (crenderer, part);
+  set_color (crenderer, part);
 
   x = crenderer->x_offset,
   y = crenderer->y_offset;
@@ -593,10 +500,10 @@ gsk_pango_renderer_draw_trapezoid (PangoRenderer     *renderer,
   cairo_line_to (cr, x12, y2);
   cairo_close_path (cr);
 
-  if (!crenderer->do_path)
-    cairo_fill (cr);
+  cairo_fill (cr);
 
   cairo_restore (cr);
+#endif
 }
 
 /* Draws an error underline that looks like one of:
@@ -679,29 +586,25 @@ gsk_pango_renderer_draw_error_underline (PangoRenderer *renderer,
                                            int            width,
                                            int            height)
 {
+#if 0
   GskPangoRenderer *crenderer = (GskPangoRenderer *) (renderer);
   cairo_t *cr = crenderer->cr;
 
-  if (!crenderer->do_path)
-    {
-      cairo_save (cr);
+  cairo_save (cr);
 
-      set_color (crenderer, PANGO_RENDER_PART_UNDERLINE);
+  set_color (crenderer, PANGO_RENDER_PART_UNDERLINE);
 
-      cairo_new_path (cr);
-    }
+  cairo_new_path (cr);
 
   draw_error_underline (cr,
                         crenderer->x_offset + (double)x / PANGO_SCALE,
                         crenderer->y_offset + (double)y / PANGO_SCALE,
                         (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
 
-  if (!crenderer->do_path)
-    {
-      cairo_fill (cr);
+  cairo_fill (cr);
 
-      cairo_restore (cr);
-    }
+  cairo_restore (cr);
+#endif
 }
 
 static void
@@ -733,12 +636,11 @@ gsk_pango_renderer_draw_shape (PangoRenderer  *renderer,
   base_y = crenderer->y_offset + (double)y / PANGO_SCALE;
 
   cairo_save (cr);
-  if (!crenderer->do_path)
-    set_color (crenderer, PANGO_RENDER_PART_FOREGROUND);
+  set_color (crenderer, PANGO_RENDER_PART_FOREGROUND);
 
   cairo_move_to (cr, base_x, base_y);
 
-  shape_renderer (cr, attr, crenderer->do_path, shape_renderer_data);
+  shape_renderer (cr, attr, FALSE, shape_renderer_data);
 
   cairo_restore (cr);
 #endif
@@ -793,8 +695,7 @@ release_renderer (GskPangoRenderer *renderer)
 {
   if (G_LIKELY (renderer->is_cached_renderer))
     {
-      renderer->cr = NULL;
-      renderer->do_path = FALSE;
+      renderer->snapshot = NULL;
       renderer->has_show_text_glyphs = FALSE;
       renderer->x_offset = 0.;
       renderer->y_offset = 0.;
@@ -805,54 +706,29 @@ release_renderer (GskPangoRenderer *renderer)
     g_object_unref (renderer);
 }
 
-static void
-save_current_point (GskPangoRenderer *renderer)
-{
-  renderer->cr_had_current_point = cairo_has_current_point (renderer->cr);
-  cairo_get_current_point (renderer->cr, &renderer->x_offset, &renderer->y_offset);
-
-  /* abuse save_current_point() to cache cairo_has_show_text_glyphs() result */
-  renderer->has_show_text_glyphs = cairo_surface_has_show_text_glyphs (cairo_get_target (renderer->cr));
-}
-
-static void
-restore_current_point (GskPangoRenderer *renderer)
-{
-  if (renderer->cr_had_current_point)
-    /* XXX should do cairo_set_current_point() when we have that function */
-    cairo_move_to (renderer->cr, renderer->x_offset, renderer->y_offset);
-  else
-    cairo_new_sub_path (renderer->cr);
-}
-
-
 /* convenience wrappers using the default renderer */
 
-static void
-gsk_pango_do_layout (cairo_t     *cr,
-                     PangoLayout *layout,
-                     gboolean     do_path)
+void
+gsk_pango_show_layout (GtkSnapshot   *snapshot,
+                       const GdkRGBA *fg_color,
+                       PangoLayout   *layout)
 {
-  GskPangoRenderer *crenderer = acquire_renderer ();
-  PangoRenderer *renderer = (PangoRenderer *) crenderer;
+  GskPangoRenderer *crenderer;
+  PangoRectangle ink_rect;
 
-  crenderer->cr = cr;
-  crenderer->do_path = do_path;
-  save_current_point (crenderer);
+  g_return_if_fail (snapshot != NULL);
+  g_return_if_fail (PANGO_IS_LAYOUT (layout));
 
-  pango_renderer_draw_layout (renderer, layout, 0, 0);
+  crenderer = acquire_renderer ();
 
-  restore_current_point (crenderer);
+  crenderer->snapshot = snapshot;
+  crenderer->fg_color = fg_color;
+  crenderer->x_offset = crenderer->y_offset = 0;
 
-  release_renderer (crenderer);
-}
+  pango_layout_get_pixel_extents (layout, &ink_rect, NULL);
+  graphene_rect_init (&crenderer->bounds, ink_rect.x, ink_rect.y, ink_rect.width, ink_rect.height);
 
-void
-gsk_pango_show_layout (cairo_t     *cr,
-                       PangoLayout *layout)
-{
-  g_return_if_fail (cr != NULL);
-  g_return_if_fail (PANGO_IS_LAYOUT (layout));
+  pango_renderer_draw_layout (PANGO_RENDERER (crenderer), layout, 0, 0);
 
-  gsk_pango_do_layout (cr, layout, FALSE);
+  release_renderer (crenderer);
 }
diff --git a/gtk/gskpango.h b/gtk/gskpango.h
index 75a54d8..1fe7a6c 100644
--- a/gtk/gskpango.h
+++ b/gtk/gskpango.h
@@ -19,7 +19,7 @@
 #ifndef __GSK_PANGO_H__
 #define __GSK_PANGO_H__
 
-#include <gsk/gsk.h>
+#include "gtk/gtksnapshot.h"
 
 G_BEGIN_DECLS
 
@@ -35,8 +35,9 @@ GDK_AVAILABLE_IN_3_92
 GType gsk_pango_renderer_get_type (void) G_GNUC_CONST;
 
 GDK_AVAILABLE_IN_3_92
-void gsk_pango_show_layout (cairo_t     *cr,
-                            PangoLayout *layout);
+void gsk_pango_show_layout (GtkSnapshot   *snapshot,
+                            const GdkRGBA *fg_color,
+                            PangoLayout   *layout);
 
 G_END_DECLS
 
diff --git a/gtk/gtksnapshot.c b/gtk/gtksnapshot.c
index d46c0a0..11e594e 100644
--- a/gtk/gtksnapshot.c
+++ b/gtk/gtksnapshot.c
@@ -1332,37 +1332,42 @@ gtk_snapshot_render_layout (GtkSnapshot     *snapshot,
                             PangoLayout     *layout)
 {
   const GdkRGBA *fg_color;
-  graphene_rect_t bounds;
-  GtkBorder shadow_extents;
-  PangoRectangle ink_rect;
   GtkCssValue *shadow;
-  cairo_t *cr;
 
   g_return_if_fail (snapshot != NULL);
   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
   g_return_if_fail (PANGO_IS_LAYOUT (layout));
 
+  gtk_snapshot_offset (snapshot, x, y);
+
   fg_color = _gtk_css_rgba_value_get_rgba (_gtk_style_context_peek_property (context, 
GTK_CSS_PROPERTY_COLOR));
+
   shadow = _gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_TEXT_SHADOW);
-  pango_layout_get_pixel_extents (layout, &ink_rect, NULL);
-  _gtk_css_shadows_value_get_extents (shadow, &shadow_extents);
-  graphene_rect_init (&bounds,
-                      ink_rect.x - shadow_extents.left,
-                      ink_rect.y - shadow_extents.top,
-                      ink_rect.width + shadow_extents.left + shadow_extents.right,
-                      ink_rect.height + shadow_extents.top + shadow_extents.bottom);
+  if (!_gtk_css_shadows_value_is_none (shadow))
+    {
+      PangoRectangle ink_rect;
+      graphene_rect_t bounds;
+      GtkBorder shadow_extents = { 0, };
+      cairo_t *cr;
 
-  gtk_snapshot_offset (snapshot, x, y);
+      pango_layout_get_pixel_extents (layout, &ink_rect, NULL);
+      _gtk_css_shadows_value_get_extents (shadow, &shadow_extents);
+      graphene_rect_init (&bounds,
+                          ink_rect.x - shadow_extents.left,
+                          ink_rect.y - shadow_extents.top,
+                          ink_rect.width + shadow_extents.left + shadow_extents.right,
+                          ink_rect.height + shadow_extents.top + shadow_extents.bottom);
 
-  cr = gtk_snapshot_append_cairo (snapshot, &bounds, "Text<%dchars>", pango_layout_get_character_count 
(layout));
+      cr = gtk_snapshot_append_cairo (snapshot, &bounds, "Text<%dchars>", pango_layout_get_character_count 
(layout));
 
-  _gtk_css_shadows_value_paint_layout (shadow, cr, layout);
+      gdk_cairo_set_source_rgba (cr, fg_color);
 
-  gdk_cairo_set_source_rgba (cr, fg_color);
+      _gtk_css_shadows_value_paint_layout (shadow, cr, layout);
+      cairo_destroy (cr);
+    }
 
-  gsk_pango_show_layout (cr, layout);
+  gsk_pango_show_layout (snapshot, fg_color, layout);
 
-  cairo_destroy (cr);
   gtk_snapshot_offset (snapshot, -x, -y);
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]