[gtk/wip/otte/lottie: 13/17] gsk: Add GskPathMeasure




commit 1732a3d88d007ca532c2c5e0d1650383bb54f11f
Author: Benjamin Otte <otte redhat com>
Date:   Thu Nov 12 05:09:47 2020 +0100

    gsk: Add GskPathMeasure
    
    An object to do measuring operations on paths - determining their
    length, cutting off subpaths, things like that.

 gsk/gsk-autocleanup.h |   1 +
 gsk/gsk.h             |   1 +
 gsk/gskpath.c         | 117 ++++++++++++++++++++++++++++++++++--
 gsk/gskpathmeasure.c  | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++
 gsk/gskpathmeasure.h  |  49 +++++++++++++++
 gsk/gskpathprivate.h  |  40 +++++++++++++
 gsk/gsktypes.h        |   1 +
 gsk/meson.build       |   2 +
 8 files changed, 369 insertions(+), 5 deletions(-)
---
diff --git a/gsk/gsk-autocleanup.h b/gsk/gsk-autocleanup.h
index 6ca7b00d19..515ca713ec 100644
--- a/gsk/gsk-autocleanup.h
+++ b/gsk/gsk-autocleanup.h
@@ -23,6 +23,7 @@
 #ifndef __GI_SCANNER__
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GskPath, gsk_path_unref)
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(GskPathMeasure, gsk_path_measure_unref)
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GskRenderer, g_object_unref)
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GskRenderNode, gsk_render_node_unref)
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GskTransform, gsk_transform_unref)
diff --git a/gsk/gsk.h b/gsk/gsk.h
index a5b530f291..9b9ddaceae 100644
--- a/gsk/gsk.h
+++ b/gsk/gsk.h
@@ -23,6 +23,7 @@
 #include <gsk/gskenums.h>
 #include <gsk/gskglshader.h>
 #include <gsk/gskpath.h>
+#include <gsk/gskpathmeasure.h>
 #include <gsk/gskrenderer.h>
 #include <gsk/gskrendernode.h>
 #include <gsk/gskroundedrect.h>
diff --git a/gsk/gskpath.c b/gsk/gskpath.c
index b446f1fd69..f17a5965d3 100644
--- a/gsk/gskpath.c
+++ b/gsk/gskpath.c
@@ -19,7 +19,7 @@
 
 #include "config.h"
 
-#include "gskpath.h"
+#include "gskpathprivate.h"
 
 /**
  * SECTION:gskpath
@@ -55,6 +55,10 @@ struct _GskContourClass
                                                  cairo_t                *cr);
   gboolean              (* get_bounds)          (const GskContour       *contour,
                                                  graphene_rect_t        *bounds);
+  gpointer              (* init_measure)        (const GskContour       *contour,
+                                                 float                  *out_length);
+  void                  (* free_measure)        (const GskContour       *contour,
+                                                 gpointer                measure_data);
 };
 
 struct _GskPath
@@ -131,6 +135,23 @@ gsk_rect_contour_get_bounds (const GskContour *contour,
   return TRUE;
 }
 
+static gpointer
+gsk_rect_contour_init_measure (const GskContour *contour,
+                               float            *out_length)
+{
+  const GskRectContour *self = (const GskRectContour *) contour;
+
+  *out_length = 2 * ABS (self->width) + 2 * ABS (self->height);
+
+  return NULL;
+}
+
+static void
+gsk_rect_contour_free_measure (const GskContour *contour,
+                               gpointer          data)
+{
+}
+
 static const GskContourClass GSK_RECT_CONTOUR_CLASS =
 {
   sizeof (GskRectContour),
@@ -138,7 +159,9 @@ static const GskContourClass GSK_RECT_CONTOUR_CLASS =
   gsk_contour_get_size_default,
   gsk_rect_contour_print,
   gsk_rect_contour_to_cairo,
-  gsk_rect_contour_get_bounds
+  gsk_rect_contour_get_bounds,
+  gsk_rect_contour_init_measure,
+  gsk_rect_contour_free_measure
 };
 
 static void
@@ -211,7 +234,7 @@ gsk_standard_contour_print (const GskContour *contour,
 
   for (i = 0; i < self->n_ops; i ++)
     {
-      graphene_point_t *pt = &self->points [self->ops[i].point];
+      graphene_point_t *pt = &self->points[self->ops[i].point];
 
       switch (self->ops[i].op)
       {
@@ -252,7 +275,7 @@ gsk_standard_contour_to_cairo (const GskContour *contour,
 
   for (i = 0; i < self->n_ops; i ++)
     {
-      graphene_point_t *pt = &self->points [self->ops[i].point];
+      graphene_point_t *pt = &self->points[self->ops[i].point];
 
       switch (self->ops[i].op)
       {
@@ -326,6 +349,52 @@ gsk_standard_contour_get_bounds (const GskContour *contour,
   return bounds->size.width > 0 && bounds->size.height > 0;
 }
 
+static gpointer
+gsk_standard_contour_init_measure (const GskContour *contour,
+                                   float            *out_length)
+{
+  const GskStandardContour *self = (const GskStandardContour *) contour;
+  gsize i;
+  float length;
+
+  length = 0;
+
+  for (i = 1; i < self->n_ops; i ++)
+    {
+      graphene_point_t *pt = &self->points[self->ops[i].point];
+
+      switch (self->ops[i].op)
+      {
+        case GSK_PATH_MOVE:
+          break;
+
+        case GSK_PATH_CLOSE:
+        case GSK_PATH_LINE:
+          length += graphene_point_distance (&pt[0], &pt[1], NULL, NULL);
+          break;
+
+        case GSK_PATH_CURVE:
+          g_warning ("i'm not fat!");
+          length += graphene_point_distance (&pt[0], &pt[3], NULL, NULL);
+          break;
+
+        default:
+          g_assert_not_reached();
+          return NULL;
+      }
+    }
+
+  *out_length = length;
+
+  return NULL;
+}
+
+static void
+gsk_standard_contour_free_measure (const GskContour *contour,
+                                   gpointer          data)
+{
+}
+
 static const GskContourClass GSK_STANDARD_CONTOUR_CLASS =
 {
   sizeof (GskStandardContour),
@@ -333,7 +402,9 @@ static const GskContourClass GSK_STANDARD_CONTOUR_CLASS =
   gsk_standard_contour_get_size,
   gsk_standard_contour_print,
   gsk_standard_contour_to_cairo,
-  gsk_standard_contour_get_bounds
+  gsk_standard_contour_get_bounds,
+  gsk_standard_contour_init_measure,
+  gsk_standard_contour_free_measure
 };
 
 /* You must ensure the contour has enough size allocated,
@@ -357,6 +428,28 @@ gsk_standard_contour_init (GskContour *contour,
   memcpy (self->points, points, sizeof (graphene_point_t) * n_points);
 }
 
+/* CONTOUR */
+
+gpointer
+gsk_contour_init_measure (GskPath *path,
+                          gsize    i,
+                          float   *out_length)
+{
+  GskContour *self = path->contours[i];
+
+  return self->klass->init_measure (self, out_length);
+}
+
+void
+gsk_contour_free_measure (GskPath  *path,
+                          gsize     i,
+                          gpointer  data)
+{
+  GskContour *self = path->contours[i];
+
+  self->klass->free_measure (self, data);
+}
+
 /* PATH */
 
 static GskPath *
@@ -588,6 +681,20 @@ gsk_path_to_cairo (GskPath *self,
     }
 }
 
+/*
+ * gsk_path_get_n_contours:
+ * @path: a #GskPath
+ *
+ * Gets the nnumber of contours @path is composed out of.
+ *
+ * Returns: the number of contours in @path
+ **/
+gsize
+gsk_path_get_n_contours (GskPath *path)
+{
+  return path->n_contours;
+}
+
 /**
  * gsk_path_is_empty:
  * @path: a #GskPath
diff --git a/gsk/gskpathmeasure.c b/gsk/gskpathmeasure.c
new file mode 100644
index 0000000000..60b8727564
--- /dev/null
+++ b/gsk/gskpathmeasure.c
@@ -0,0 +1,163 @@
+/*
+ * Copyright © 2020 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#include "config.h"
+
+#include "gskpathmeasure.h"
+
+#include "gskpathprivate.h"
+
+/**
+ * SECTION:gskpathmeasure
+ * @Title: PathMeasure
+ * @Short_description: Measuring operations on paths
+ * @See_also: #GskPath
+ *
+ * #GskPathMeasure is an object that allows measuring operations on #GskPaths.
+ * These operations are useful when implementing animations.
+ */
+
+typedef struct _GskContourMeasure GskContourMeasure;
+
+struct _GskContourMeasure
+{
+  float length;
+  gpointer contour_data;
+};
+
+struct _GskPathMeasure
+{
+  /*< private >*/
+  guint ref_count;
+
+  GskPath *path;
+
+  float length;
+  gsize n_contours;
+  GskContourMeasure measures[];
+};
+
+/**
+ * GskPathMeasure:
+ *
+ * A #GskPathMeasure struct is a reference counted struct
+ * and should be treated as opaque.
+ */
+
+G_DEFINE_BOXED_TYPE (GskPathMeasure, gsk_path_measure,
+                     gsk_path_measure_ref,
+                     gsk_path_measure_unref)
+
+/**
+ * gsk_path_measure_new:
+ * @path: the path to measure
+ *
+ * Creates a measure object for the given @path.
+ *
+ * Returns: a new #GskPathMeasure representing @path
+ **/
+GskPathMeasure *
+gsk_path_measure_new (GskPath *path)
+{
+  GskPathMeasure *self;
+  gsize i, n_contours;
+
+  g_return_val_if_fail (path != NULL, NULL);
+
+  n_contours = gsk_path_get_n_contours (path);
+
+  self = g_malloc0 (sizeof (GskPathMeasure) + n_contours * sizeof (GskContourMeasure));
+
+  self->ref_count = 1;
+  self->path = gsk_path_ref (path);
+  self->n_contours = n_contours;
+
+  for (i = 0; i < n_contours; i++)
+    {
+      self->measures[i].contour_data = gsk_contour_init_measure (path, i, &self->measures[i].length);
+      self->length += self->measures[i].length;
+    }
+
+  return self;
+}
+
+/**
+ * gsk_path_measure_ref:
+ * @self: a #GskPathMeasure
+ *
+ * Increases the reference count of a #GskPathMeasure by one.
+ *
+ * Returns: the passed in #GskPathMeasure.
+ **/
+GskPathMeasure *
+gsk_path_measure_ref (GskPathMeasure *self)
+{
+  g_return_val_if_fail (self != NULL, NULL);
+
+  self->ref_count++;
+
+  return self;
+}
+
+/**
+ * gsk_path_measure_unref:
+ * @self: a #GskPathMeasure
+ *
+ * Decreases the reference count of a #GskPathMeasure by one.
+ * If the resulting reference count is zero, frees the path_measure.
+ **/
+void
+gsk_path_measure_unref (GskPathMeasure *self)
+{
+  gsize i;
+
+  g_return_if_fail (self != NULL);
+  g_return_if_fail (self->ref_count > 0);
+
+  self->ref_count--;
+  if (self->ref_count > 0)
+    return;
+
+  for (i = 0; i < self->n_contours; i++)
+    {
+      gsk_contour_free_measure (self->path, i, self->measures[i].contour_data);
+    }
+
+  gsk_path_unref (self->path);
+  g_free (self);
+}
+
+/**
+ * gsk_path_measure_get_length:
+ * @self: a #GskPathMeasure
+ *
+ * Gets the length of the path being measured.
+ *
+ * The length is cached, so this function does not do any work.
+ *
+ * Returns: The length of the path measured by @self
+ **/
+float
+gsk_path_measure_get_length (GskPathMeasure *self)
+{
+  g_return_val_if_fail (self != NULL, 0);
+
+  return self->length;
+}
+
diff --git a/gsk/gskpathmeasure.h b/gsk/gskpathmeasure.h
new file mode 100644
index 0000000000..2941176e60
--- /dev/null
+++ b/gsk/gskpathmeasure.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright © 2020 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#ifndef __GSK_PATH_MEASURE_H__
+#define __GSK_PATH_MEASURE_H__
+
+#if !defined (__GSK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gsk/gsk.h> can be included directly."
+#endif
+
+
+#include <gsk/gsktypes.h>
+
+G_BEGIN_DECLS
+
+#define GSK_TYPE_PATH_MEASURE (gsk_path_measure_get_type ())
+
+GDK_AVAILABLE_IN_ALL
+GType                   gsk_path_measure_get_type               (void) G_GNUC_CONST;
+GDK_AVAILABLE_IN_ALL
+GskPathMeasure *        gsk_path_measure_new                    (GskPath                *path);
+
+GDK_AVAILABLE_IN_ALL
+GskPathMeasure *        gsk_path_measure_ref                    (GskPathMeasure         *self);
+GDK_AVAILABLE_IN_ALL
+void                    gsk_path_measure_unref                  (GskPathMeasure         *self);
+
+GDK_AVAILABLE_IN_ALL
+float                   gsk_path_measure_get_length             (GskPathMeasure         *self);
+
+G_END_DECLS
+
+#endif /* __GSK_PATH_MEASURE_H__ */
diff --git a/gsk/gskpathprivate.h b/gsk/gskpathprivate.h
new file mode 100644
index 0000000000..5551734411
--- /dev/null
+++ b/gsk/gskpathprivate.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright © 2020 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+
+#ifndef __GSK_PATH_PRIVATE_H__
+#define __GSK_PATH_PRIVATE_H__
+
+#include "gskpath.h"
+
+G_BEGIN_DECLS
+
+gsize                   gsk_path_get_n_contours                 (GskPath              *path);
+
+gpointer                gsk_contour_init_measure                (GskPath              *path,
+                                                                 gsize                 i,
+                                                                 float                *out_length);
+void                    gsk_contour_free_measure                (GskPath              *path,
+                                                                 gsize                 i,
+                                                                 gpointer              data);
+
+G_END_DECLS
+
+#endif /* __GSK_PATH_PRIVATE_H__ */
+
diff --git a/gsk/gsktypes.h b/gsk/gsktypes.h
index fdf06b5ffc..d31e5d9be4 100644
--- a/gsk/gsktypes.h
+++ b/gsk/gsktypes.h
@@ -27,6 +27,7 @@
 #include <gsk/gskenums.h>
 
 typedef struct _GskPath                 GskPath;
+typedef struct _GskPathMeasure          GskPathMeasure;
 typedef struct _GskRenderer             GskRenderer;
 typedef struct _GskStroke               GskStroke;
 typedef struct _GskTransform            GskTransform;
diff --git a/gsk/meson.build b/gsk/meson.build
index 5be795d0c3..a5ee24fb70 100644
--- a/gsk/meson.build
+++ b/gsk/meson.build
@@ -24,6 +24,7 @@ gsk_public_sources = files([
   'gskcairorenderer.c',
   'gskglshader.c',
   'gskpath.c',
+  'gskpathmeasure.c',
   'gskrenderer.c',
   'gskrendernode.c',
   'gskrendernodeimpl.c',
@@ -56,6 +57,7 @@ gsk_public_headers = files([
   'gskenums.h',
   'gskglshader.h',
   'gskpath.h',
+  'gskpathmeasure.h',
   'gskrenderer.h',
   'gskrendernode.h',
   'gskroundedrect.h',


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