[gtk/wip/otte/transform: 8/37] snapshot: Refactor text rendering



commit 5baa40e604f18e2e9600449df7f96fdf6ea8df90
Author: Benjamin Otte <otte redhat com>
Date:   Thu Feb 21 03:07:14 2019 +0100

    snapshot: Refactor text rendering
    
    The code didn't change, it was just shuffled around to make the
    with_bounds() versions of the text rendering unnecessary and instead
    pass through the generic append_node() path.

 gsk/gskrendernode.h        |  4 ++--
 gsk/gskrendernodeimpl.c    | 46 +++++++---------------------------------------
 gsk/gskrendernodeprivate.h |  7 -------
 gtk/gskpango.c             | 37 ++++++-------------------------------
 gtk/gtksnapshot.c          | 42 ++++++++++++++++++++++++++++++++++++++++++
 gtk/gtksnapshotprivate.h   |  6 ++++++
 6 files changed, 63 insertions(+), 79 deletions(-)
---
diff --git a/gsk/gskrendernode.h b/gsk/gskrendernode.h
index 3099569471..4ab51853da 100644
--- a/gsk/gskrendernode.h
+++ b/gsk/gskrendernode.h
@@ -287,8 +287,8 @@ GDK_AVAILABLE_IN_ALL
 GskRenderNode *         gsk_text_node_new                       (PangoFont                *font,
                                                                  PangoGlyphString         *glyphs,
                                                                  const GdkRGBA            *color,
-                                                                 double                    x,
-                                                                 double                    y);
+                                                                 float                     x,
+                                                                 float                     y);
 GDK_AVAILABLE_IN_ALL
 const PangoFont *       gsk_text_node_peek_font                 (GskRenderNode            *node);
 GDK_AVAILABLE_IN_ALL
diff --git a/gsk/gskrendernodeimpl.c b/gsk/gskrendernodeimpl.c
index aa72646065..c20b941d88 100644
--- a/gsk/gskrendernodeimpl.c
+++ b/gsk/gskrendernodeimpl.c
@@ -4647,8 +4647,8 @@ GskRenderNode *
 gsk_text_node_new (PangoFont        *font,
                    PangoGlyphString *glyphs,
                    const GdkRGBA    *color,
-                   double            x,
-                   double            y)
+                   float             x,
+                   float             y)
 {
   GskTextNode *self;
   PangoRectangle ink_rect;
@@ -4660,42 +4660,6 @@ gsk_text_node_new (PangoFont        *font,
   if (ink_rect.width == 0 || ink_rect.height == 0)
     return NULL;
 
-  self = (GskTextNode *)gsk_text_node_new_with_bounds (font, glyphs, color, x, y,
-                                                       &GRAPHENE_RECT_INIT (x,
-                                                                            y + ink_rect.y,
-                                                                            ink_rect.x + ink_rect.width,
-                                                                            ink_rect.height));
-
-  return &self->render_node;
-}
-
-/**
- * gsk_text_node_new_with_bounds:
- * @font: the #PangoFont containing the glyphs
- * @glyphs: the #PangoGlyphString to render
- * @color: the foreground color to render with
- * @x: the x coordinate at which to put the baseline
- * @y: the y coordinate at wihch to put the baseline
- * @bounds: the node bounds
- *
- * Creates a render node that renders the given glyphs,
- * Note that @color may not be used if the font contains
- * color glyphs.
- *
- * This function will not do any text measuring, contrary to gsk_text_node_new().
- *
- * Returns: (nullable): a new text node, or %NULL
- */
-GskRenderNode *
-gsk_text_node_new_with_bounds (PangoFont             *font,
-                               PangoGlyphString      *glyphs,
-                               const GdkRGBA         *color,
-                               double                 x,
-                               double                 y,
-                               const graphene_rect_t *bounds)
-{
-  GskTextNode *self;
-
   self = (GskTextNode *) gsk_render_node_new (&GSK_TEXT_NODE_CLASS, sizeof (PangoGlyphInfo) * 
glyphs->num_glyphs);
 
   self->font = g_object_ref (font);
@@ -4705,7 +4669,11 @@ gsk_text_node_new_with_bounds (PangoFont             *font,
   self->num_glyphs = glyphs->num_glyphs;
   memcpy (self->glyphs, glyphs->glyphs, sizeof (PangoGlyphInfo) * glyphs->num_glyphs);
 
-  graphene_rect_init_from_rect (&self->render_node.bounds, bounds);
+  graphene_rect_init (&self->render_node.bounds,
+                      x,
+                      y + ink_rect.y,
+-                     ink_rect.x + ink_rect.width,
+-                     ink_rect.height);
 
   return &self->render_node;
 }
diff --git a/gsk/gskrendernodeprivate.h b/gsk/gskrendernodeprivate.h
index 1f0565cfab..62f34ba3db 100644
--- a/gsk/gskrendernodeprivate.h
+++ b/gsk/gskrendernodeprivate.h
@@ -101,13 +101,6 @@ GskRenderNode *         gsk_transform_node_new_with_category    (GskRenderNode
                                                                  GskMatrixCategory               category);
 GskMatrixCategory       gsk_transform_node_get_category         (GskRenderNode                  *node);
 
-GskRenderNode * gsk_text_node_new_with_bounds     (PangoFont                *font,
-                                                   PangoGlyphString         *glyphs,
-                                                   const GdkRGBA            *color,
-                                                   double                    x,
-                                                   double                    y,
-                                                   const graphene_rect_t    *bounds);
-
 
 G_END_DECLS
 
diff --git a/gtk/gskpango.c b/gtk/gskpango.c
index ae4a6a81fe..8e1c2e3f70 100644
--- a/gtk/gskpango.c
+++ b/gtk/gskpango.c
@@ -113,41 +113,16 @@ gsk_pango_renderer_show_text_glyphs (PangoRenderer        *renderer,
                                      int                   y)
 {
   GskPangoRenderer *crenderer = (GskPangoRenderer *) (renderer);
-  int x_offset, y_offset;
-  GskRenderNode *node;
   GdkRGBA color;
-  graphene_rect_t node_bounds;
-  PangoRectangle ink_rect;
-
-  pango_glyph_string_extents (glyphs, font, &ink_rect, NULL);
-  pango_extents_to_pixels (&ink_rect, NULL);
-
-  /* Don't create empty nodes */
-  if (ink_rect.width == 0 || ink_rect.height == 0)
-    return;
-
-  graphene_rect_init (&node_bounds,
-                      (float)x/PANGO_SCALE - 1.0,
-                      (float)y/PANGO_SCALE + ink_rect.y - 1.0,
-                      ink_rect.x + ink_rect.width + 2.0,
-                      ink_rect.height + 2.0);
-
-  gtk_snapshot_get_offset (crenderer->snapshot, &x_offset, &y_offset);
-  graphene_rect_offset (&node_bounds, x_offset, y_offset);
 
   get_color (crenderer, PANGO_RENDER_PART_FOREGROUND, &color);
 
-  node = gsk_text_node_new_with_bounds (font,
-                                        glyphs,
-                                        &color,
-                                        x_offset + (double)x/PANGO_SCALE,
-                                        y_offset + (double)y/PANGO_SCALE,
-                                        &node_bounds);
-  if (node == NULL)
-    return;
-
-  gtk_snapshot_append_node_internal (crenderer->snapshot, node);
-  gsk_render_node_unref (node);
+  gtk_snapshot_append_text (crenderer->snapshot,
+                            font,
+                            glyphs,
+                            &color,
+                            (float) x / PANGO_SCALE,
+                            (float) y / PANGO_SCALE);
 }
 
 static void
diff --git a/gtk/gtksnapshot.c b/gtk/gtksnapshot.c
index 1be41ad138..eb666dc5ad 100644
--- a/gtk/gtksnapshot.c
+++ b/gtk/gtksnapshot.c
@@ -606,6 +606,23 @@ gtk_snapshot_ensure_affine (GtkSnapshot *snapshot,
   *dx = *dy = 0;
 }
 
+static void
+gtk_snapshot_ensure_translate (GtkSnapshot *snapshot,
+                               float       *dx,
+                               float       *dy)
+{
+  const GtkSnapshotState *current_state = gtk_snapshot_get_current_state (snapshot);
+  float scale_x, scale_y;
+
+  if (gtk_transform_to_affine (current_state->transform, &scale_x, &scale_y, dx, dy) &&
+      scale_x == 1.0f && scale_y == 1.0f)
+    return;
+
+  gtk_snapshot_autopush_transform (snapshot);
+
+  *dx = *dy = 0;
+}
+
 static void
 gtk_snapshot_ensure_identity (GtkSnapshot *snapshot)
 {
@@ -1463,6 +1480,31 @@ gtk_snapshot_render_layout (GtkSnapshot     *snapshot,
   gtk_snapshot_offset (snapshot, -x, -y);
 }
 
+void
+gtk_snapshot_append_text (GtkSnapshot           *snapshot,
+                          PangoFont             *font,
+                          PangoGlyphString      *glyphs,
+                          const GdkRGBA         *color,
+                          float                  x,
+                          float                  y)
+{
+  GskRenderNode *node;
+  float dx, dy;
+
+  gtk_snapshot_ensure_translate (snapshot, &dx, &dy);
+
+  node = gsk_text_node_new (font,
+                            glyphs,
+                            color,
+                            x + dx,
+                            y + dy);
+  if (node == NULL)
+    return;
+
+  gtk_snapshot_append_node_internal (snapshot, node);
+  gsk_render_node_unref (node);
+}
+
 /**
  * gtk_snapshot_append_linear_gradient:
  * @snapshot: a #GtkSnapshot
diff --git a/gtk/gtksnapshotprivate.h b/gtk/gtksnapshotprivate.h
index e19d0b2a20..7750cf5dbf 100644
--- a/gtk/gtksnapshotprivate.h
+++ b/gtk/gtksnapshotprivate.h
@@ -108,6 +108,12 @@ GtkSnapshot *           gtk_snapshot_new_with_parent            (GtkSnapshot
 void                    gtk_snapshot_push_transform_with_category (GtkSnapshot          *snapshot,
                                                                  const graphene_matrix_t*transform,
                                                                  GskMatrixCategory       category);
+void                    gtk_snapshot_append_text                (GtkSnapshot            *snapshot,
+                                                                 PangoFont              *font,
+                                                                 PangoGlyphString       *glyphs,
+                                                                 const GdkRGBA          *color,
+                                                                 float                   x,
+                                                                 float                   y);
 G_END_DECLS
 
 #endif /* __GTK_SNAPSHOT_PRIVATE_H__ */


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