[gtk/wip/otte/canvas: 28/36] canvas: Rename GtkCanvasVec2 to GtkCanvasVector




commit 7f36e3619dd0b4e28121667f56db11c80f947011
Author: Benjamin Otte <otte redhat com>
Date:   Tue Jul 5 07:38:27 2022 +0200

    canvas: Rename GtkCanvasVec2 to GtkCanvasVector
    
    I want to export it as public API, so better choose a nice name.
    
    I also want to split the public API from the implementation details, so
    this file goes to gtkcanvasvectorimpl.c for now.

 gtk/gtkcanvas.c              |  10 +-
 gtk/gtkcanvasbox.c           |  46 +++---
 gtk/gtkcanvasboxprivate.h    |  10 +-
 gtk/gtkcanvasitem.c          |  34 ++---
 gtk/gtkcanvasitemprivate.h   |   4 +-
 gtk/gtkcanvaspoint.c         |  14 +-
 gtk/gtkcanvassize.c          |  18 +--
 gtk/gtkcanvasvec2.c          | 349 -------------------------------------------
 gtk/gtkcanvasvec2private.h   | 101 -------------
 gtk/gtkcanvasvectorimpl.c    | 348 ++++++++++++++++++++++++++++++++++++++++++
 gtk/gtkcanvasvectorprivate.h | 101 +++++++++++++
 gtk/meson.build              |   2 +-
 12 files changed, 518 insertions(+), 519 deletions(-)
---
diff --git a/gtk/gtkcanvas.c b/gtk/gtkcanvas.c
index b85b7a4c48..2b15c21c74 100644
--- a/gtk/gtkcanvas.c
+++ b/gtk/gtkcanvas.c
@@ -24,7 +24,7 @@
 #include "gtkcanvasbox.h"
 #include "gtkcanvasitemprivate.h"
 #include "gtkcanvassize.h"
-#include "gtkcanvasvec2private.h"
+#include "gtkcanvasvectorprivate.h"
 #include "gtkintl.h"
 #include "gtklistitemfactory.h"
 #include "gtkwidgetprivate.h"
@@ -54,7 +54,7 @@ struct _GtkCanvas
   GtkCanvasItems items;
   GHashTable *item_lookup;
 
-  GtkCanvasVec2 viewport_size;
+  GtkCanvasVector viewport_size;
 };
 
 enum
@@ -165,7 +165,7 @@ gtk_canvas_finalize (GObject *object)
   GtkCanvas *self = GTK_CANVAS (object);
 
   g_hash_table_unref (self->item_lookup);
-  gtk_canvas_vec2_finish (&self->viewport_size);
+  gtk_canvas_vector_finish (&self->viewport_size);
 
   G_OBJECT_CLASS (gtk_canvas_parent_class)->finalize (object);
 }
@@ -243,7 +243,7 @@ gtk_canvas_allocate (GtkWidget *widget,
 
   gtk_canvas_validate_variables (self);
 
-  gtk_canvas_vec2_init_constant (gtk_canvas_vec2_get_variable (&self->viewport_size), width, height);
+  gtk_canvas_vector_init_constant (gtk_canvas_vector_get_variable (&self->viewport_size), width, height);
 
   force = FALSE;
   do
@@ -371,7 +371,7 @@ gtk_canvas_init (GtkCanvas *self)
 {
   self->item_lookup = g_hash_table_new (g_direct_hash, g_direct_equal);
 
-  gtk_canvas_vec2_init_variable (&self->viewport_size);
+  gtk_canvas_vector_init_variable (&self->viewport_size);
 }
 
 /**
diff --git a/gtk/gtkcanvasbox.c b/gtk/gtkcanvasbox.c
index 715985ee31..ac76fce336 100644
--- a/gtk/gtkcanvasbox.c
+++ b/gtk/gtkcanvasbox.c
@@ -39,12 +39,12 @@ G_DEFINE_BOXED_TYPE (GtkCanvasBox, gtk_canvas_box,
 
 void
 gtk_canvas_box_init (GtkCanvasBox          *self,
-                     const GtkCanvasVec2   *point,
-                     const GtkCanvasVec2   *size,
+                     const GtkCanvasVector *point,
+                     const GtkCanvasVector *size,
                      const graphene_vec2_t *origin)
 {
-  gtk_canvas_vec2_init_copy (&self->point, point);
-  gtk_canvas_vec2_init_copy (&self->size, size);
+  gtk_canvas_vector_init_copy (&self->point, point);
+  gtk_canvas_vector_init_copy (&self->size, size);
   graphene_vec2_init_from_vec2 (&self->origin, origin);
 }
 
@@ -58,15 +58,15 @@ gtk_canvas_box_init_copy (GtkCanvasBox       *self,
 void
 gtk_canvas_box_finish (GtkCanvasBox *self)
 {
-  gtk_canvas_vec2_finish (&self->point);
-  gtk_canvas_vec2_finish (&self->size);
+  gtk_canvas_vector_finish (&self->point);
+  gtk_canvas_vector_finish (&self->size);
 }
 
 void
 gtk_canvas_box_init_variable (GtkCanvasBox *self)
 {
-  gtk_canvas_vec2_init_variable (&self->point);
-  gtk_canvas_vec2_init_variable (&self->size);
+  gtk_canvas_vector_init_variable (&self->point);
+  gtk_canvas_vector_init_variable (&self->size);
   graphene_vec2_init (&self->origin, 0, 0);
 }
 
@@ -74,8 +74,8 @@ void
 gtk_canvas_box_update_variable (GtkCanvasBox       *self,
                                 const GtkCanvasBox *other)
 {
-  gtk_canvas_vec2_init_copy (gtk_canvas_vec2_get_variable (&self->point), &other->point);
-  gtk_canvas_vec2_init_copy (gtk_canvas_vec2_get_variable (&self->size), &other->size);
+  gtk_canvas_vector_init_copy (gtk_canvas_vector_get_variable (&self->point), &other->point);
+  gtk_canvas_vector_init_copy (gtk_canvas_vector_get_variable (&self->size), &other->size);
   graphene_vec2_init_from_vec2 (&self->origin, &other->origin);
 }
 
@@ -93,7 +93,7 @@ GtkCanvasBox *
 gtk_canvas_box_new_points (const GtkCanvasPoint *point1,
                            const GtkCanvasPoint *point2)
 {
-  GtkCanvasVec2 size;
+  GtkCanvasVector size;
   GtkCanvasBox *result;
   graphene_vec2_t minus_one;
 
@@ -101,15 +101,15 @@ gtk_canvas_box_new_points (const GtkCanvasPoint *point1,
   g_return_val_if_fail (point2 != NULL, NULL);
 
   graphene_vec2_init (&minus_one, -1.f, -1.f);
-  gtk_canvas_vec2_init_sum (&size,
-                            graphene_vec2_one (),
-                            point2,
-                            &minus_one,
-                            point1,
-                            NULL);
+  gtk_canvas_vector_init_sum (&size,
+                              graphene_vec2_one (),
+                              point2,
+                              &minus_one,
+                              point1,
+                              NULL);
   result = g_slice_new (GtkCanvasBox);
-  gtk_canvas_box_init (result, (GtkCanvasVec2 *) point1, &size, graphene_vec2_zero ());
-  gtk_canvas_vec2_finish (&size);
+  gtk_canvas_box_init (result, (GtkCanvasVector *) point1, &size, graphene_vec2_zero ());
+  gtk_canvas_vector_finish (&size);
 
   return result;
 }
@@ -144,8 +144,8 @@ gtk_canvas_box_new (const GtkCanvasPoint *point,
 
   self = g_slice_new (GtkCanvasBox);
   gtk_canvas_box_init (self,
-                       (GtkCanvasVec2 *) point,
-                       (GtkCanvasVec2 *) size,
+                       (GtkCanvasVector *) point,
+                       (GtkCanvasVector *) size,
                        &origin);
 
   return self;
@@ -181,8 +181,8 @@ gtk_canvas_box_eval (const GtkCanvasBox *self,
   g_return_val_if_fail (self != NULL, FALSE);
   g_return_val_if_fail (rect != NULL, FALSE);
 
-  if (!gtk_canvas_vec2_eval (&self->point, &point) ||
-      !gtk_canvas_vec2_eval (&self->size, &size))
+  if (!gtk_canvas_vector_eval (&self->point, &point) ||
+      !gtk_canvas_vector_eval (&self->size, &size))
     {
       *rect = *graphene_rect_zero ();
       return FALSE;
diff --git a/gtk/gtkcanvasboxprivate.h b/gtk/gtkcanvasboxprivate.h
index 0660f8761f..e93a9dac28 100644
--- a/gtk/gtkcanvasboxprivate.h
+++ b/gtk/gtkcanvasboxprivate.h
@@ -3,21 +3,21 @@
 
 #include "gtkcanvasbox.h"
 
-#include "gtkcanvasvec2private.h"
+#include "gtkcanvasvectorprivate.h"
 
 G_BEGIN_DECLS
 
 struct _GtkCanvasBox
 {
-  GtkCanvasVec2 point;
-  GtkCanvasVec2 size;
+  GtkCanvasVector point;
+  GtkCanvasVector size;
   graphene_vec2_t origin;
 };
 
 
 void                    gtk_canvas_box_init                     (GtkCanvasBox           *self,
-                                                                 const GtkCanvasVec2    *point,
-                                                                 const GtkCanvasVec2    *size,
+                                                                 const GtkCanvasVector  *point,
+                                                                 const GtkCanvasVector  *size,
                                                                  const graphene_vec2_t  *origin);
 void                    gtk_canvas_box_init_copy                (GtkCanvasBox           *self,
                                                                  const GtkCanvasBox     *source);
diff --git a/gtk/gtkcanvasitem.c b/gtk/gtkcanvasitem.c
index 7272ca38dd..745057657f 100644
--- a/gtk/gtkcanvasitem.c
+++ b/gtk/gtkcanvasitem.c
@@ -45,7 +45,7 @@ struct _GtkCanvasItem
   GtkCanvasBox bounds_var;
   GtkCanvasBox allocation_var;
 
-  GtkCanvasVec2 size_vecs[4];
+  GtkCanvasVector size_vecs[4];
 };
 
 enum
@@ -84,7 +84,7 @@ gtk_canvas_item_finalize (GObject *object)
   int i;
 
   for (i = 0; i < 4; i++)
-    gtk_canvas_vec2_finish (&self->size_vecs[i]);
+    gtk_canvas_vector_finish (&self->size_vecs[i]);
 
   gtk_canvas_box_finish (&self->bounds);
   gtk_canvas_box_finish (&self->bounds_var);
@@ -208,10 +208,10 @@ gtk_canvas_item_init (GtkCanvasItem *self)
   int i;
 
   for (i = 0; i < 4; i++)
-    gtk_canvas_vec2_init_variable (&self->size_vecs[i]);
+    gtk_canvas_vector_init_variable (&self->size_vecs[i]);
 
-  gtk_canvas_vec2_init_constant (&self->bounds.point, 0, 0);
-  gtk_canvas_vec2_init_copy (&self->bounds.size, &self->size_vecs[GTK_CANVAS_ITEM_MEASURE_NAT_FOR_NAT]);
+  gtk_canvas_vector_init_constant (&self->bounds.point, 0, 0);
+  gtk_canvas_vector_init_copy (&self->bounds.size, &self->size_vecs[GTK_CANVAS_ITEM_MEASURE_NAT_FOR_NAT]);
   graphene_vec2_init (&self->bounds.origin, 0, 0);
   gtk_canvas_box_init_variable (&self->bounds_var);
   gtk_canvas_box_update_variable (&self->bounds_var, &self->bounds);
@@ -267,27 +267,27 @@ gtk_canvas_item_validate_variables (GtkCanvasItem *self)
 
   for (i = 0; i < 4; i++)
     {
-      gtk_canvas_vec2_init_constant (
-          gtk_canvas_vec2_get_variable (&self->size_vecs[i]),
+      gtk_canvas_vector_init_constant (
+          gtk_canvas_vector_get_variable (&self->size_vecs[i]),
           0, 0);
     }
 
-  gtk_canvas_vec2_init_invalid (
-      gtk_canvas_vec2_get_variable (&self->allocation_var.point));
-  gtk_canvas_vec2_init_invalid (
-      gtk_canvas_vec2_get_variable (&self->allocation_var.size));
+  gtk_canvas_vector_init_invalid (
+      gtk_canvas_vector_get_variable (&self->allocation_var.point));
+  gtk_canvas_vector_init_invalid (
+      gtk_canvas_vector_get_variable (&self->allocation_var.size));
 }
 
 void
 gtk_canvas_item_allocate (GtkCanvasItem   *self,
                           graphene_rect_t *rect)
 {
-  gtk_canvas_vec2_init_constant (
-      gtk_canvas_vec2_get_variable (&self->allocation_var.point),
+  gtk_canvas_vector_init_constant (
+      gtk_canvas_vector_get_variable (&self->allocation_var.point),
       rect->origin.x + graphene_vec2_get_x (&self->bounds.origin) * rect->size.width,
       rect->origin.y + graphene_vec2_get_y (&self->bounds.origin) * rect->size.height);
-  gtk_canvas_vec2_init_constant (
-      gtk_canvas_vec2_get_variable (&self->allocation_var.size),
+  gtk_canvas_vector_init_constant (
+      gtk_canvas_vector_get_variable (&self->allocation_var.size),
       rect->size.width, rect->size.height);
   graphene_vec2_init_from_vec2 (&self->allocation_var.origin, &self->bounds.origin);
 }
@@ -321,10 +321,10 @@ gtk_canvas_item_allocate_widget (GtkCanvasItem *self,
 gboolean
 gtk_canvas_item_has_allocation (GtkCanvasItem *self)
 {
-  return !gtk_canvas_vec2_is_invalid (gtk_canvas_vec2_get_variable (&self->allocation_var.point));
+  return !gtk_canvas_vector_is_invalid (gtk_canvas_vector_get_variable (&self->allocation_var.point));
 }
 
-const GtkCanvasVec2 *
+const GtkCanvasVector *
 gtk_canvas_item_get_measure_vec2 (GtkCanvasItem            *self,
                                   GtkCanvasItemMeasurement  measure)
 {
diff --git a/gtk/gtkcanvasitemprivate.h b/gtk/gtkcanvasitemprivate.h
index b829b02c13..9e8eb6874c 100644
--- a/gtk/gtkcanvasitemprivate.h
+++ b/gtk/gtkcanvasitemprivate.h
@@ -4,7 +4,7 @@
 #include "gtkcanvasitem.h"
 
 #include "gtkcanvassize.h"
-#include "gtkcanvasvec2private.h"
+#include "gtkcanvasvectorprivate.h"
 
 G_BEGIN_DECLS
 
@@ -18,7 +18,7 @@ void                    gtk_canvas_item_allocate_widget          (GtkCanvasItem
                                                                   float                  dx,
                                                                   float                  dy);
 gboolean                gtk_canvas_item_has_allocation           (GtkCanvasItem         *self);
-const GtkCanvasVec2 *   gtk_canvas_item_get_measure_vec2         (GtkCanvasItem         *self,
+const GtkCanvasVector *   gtk_canvas_item_get_measure_vec2         (GtkCanvasItem         *self,
                                                                   GtkCanvasItemMeasurement measure);
 
 void                    gtk_canvas_item_clear_canvas             (GtkCanvasItem         *self);
diff --git a/gtk/gtkcanvaspoint.c b/gtk/gtkcanvaspoint.c
index e5536ebc7a..2e7711f378 100644
--- a/gtk/gtkcanvaspoint.c
+++ b/gtk/gtkcanvaspoint.c
@@ -29,7 +29,7 @@
 #include "gtkcanvaspoint.h"
 
 #include "gtkcanvasboxprivate.h"
-#include "gtkcanvasvec2private.h"
+#include "gtkcanvasvectorprivate.h"
 
 G_DEFINE_BOXED_TYPE (GtkCanvasPoint, gtk_canvas_point,
                      gtk_canvas_point_copy,
@@ -37,7 +37,7 @@ G_DEFINE_BOXED_TYPE (GtkCanvasPoint, gtk_canvas_point,
 
 struct _GtkCanvasPoint
 {
-  GtkCanvasVec2 vec2;
+  GtkCanvasVector vec2;
 };
 
 static GtkCanvasPoint *
@@ -62,7 +62,7 @@ gtk_canvas_point_new (float x,
   GtkCanvasPoint *self;
 
   self = gtk_canvas_point_alloc ();
-  gtk_canvas_vec2_init_constant (&self->vec2, x, y);
+  gtk_canvas_vector_init_constant (&self->vec2, x, y);
 
   return self;
 }
@@ -95,7 +95,7 @@ gtk_canvas_point_new_from_box (const GtkCanvasBox *box,
   graphene_vec2_subtract (&origin, &box->origin, &origin);
  
   self = gtk_canvas_point_alloc ();
-  gtk_canvas_vec2_init_sum (&self->vec2,
+  gtk_canvas_vector_init_sum (&self->vec2,
                             graphene_vec2_one (),
                             &box->point,
                             &origin,
@@ -112,7 +112,7 @@ gtk_canvas_point_copy (const GtkCanvasPoint *self)
   g_return_val_if_fail (self != NULL, NULL);
 
   copy = gtk_canvas_point_alloc ();
-  gtk_canvas_vec2_init_copy (&copy->vec2, &self->vec2);
+  gtk_canvas_vector_init_copy (&copy->vec2, &self->vec2);
 
   return copy;
 }
@@ -120,7 +120,7 @@ gtk_canvas_point_copy (const GtkCanvasPoint *self)
 void
 gtk_canvas_point_free (GtkCanvasPoint *self)
 {
-  gtk_canvas_vec2_finish (&self->vec2);
+  gtk_canvas_vector_finish (&self->vec2);
 
   g_slice_free (GtkCanvasPoint, self);
 }
@@ -136,7 +136,7 @@ gtk_canvas_point_eval (const GtkCanvasPoint *self,
   g_return_val_if_fail (x != NULL, FALSE);
   g_return_val_if_fail (y != NULL, FALSE);
 
-  if (!gtk_canvas_vec2_eval (&self->vec2, &vec2))
+  if (!gtk_canvas_vector_eval (&self->vec2, &vec2))
     {
       *x = 0;
       *y = 0;
diff --git a/gtk/gtkcanvassize.c b/gtk/gtkcanvassize.c
index a706def6af..72406e574d 100644
--- a/gtk/gtkcanvassize.c
+++ b/gtk/gtkcanvassize.c
@@ -31,7 +31,7 @@
 #include "gtkcanvasboxprivate.h"
 #include "gtkcanvasitemprivate.h"
 #include "gtkcanvaspoint.h"
-#include "gtkcanvasvec2private.h"
+#include "gtkcanvasvectorprivate.h"
 
 G_DEFINE_BOXED_TYPE (GtkCanvasSize, gtk_canvas_size,
                      gtk_canvas_size_copy,
@@ -39,7 +39,7 @@ G_DEFINE_BOXED_TYPE (GtkCanvasSize, gtk_canvas_size,
 
 struct _GtkCanvasSize
 {
-  GtkCanvasVec2 vec2;
+  GtkCanvasVector vec2;
 };
 
 static GtkCanvasSize *
@@ -64,7 +64,7 @@ gtk_canvas_size_new (float width,
   GtkCanvasSize *self;
 
   self = gtk_canvas_size_alloc ();
-  gtk_canvas_vec2_init_constant (&self->vec2, width, height);
+  gtk_canvas_vector_init_constant (&self->vec2, width, height);
 
   return self;
 }
@@ -83,7 +83,7 @@ gtk_canvas_size_new_from_box (const GtkCanvasBox *box)
   GtkCanvasSize *self;
 
   self = gtk_canvas_size_alloc ();
-  gtk_canvas_vec2_init_copy (&self->vec2, &box->size);
+  gtk_canvas_vector_init_copy (&self->vec2, &box->size);
 
   return self;
 }
@@ -112,7 +112,7 @@ gtk_canvas_size_new_distance (const GtkCanvasPoint *from,
   graphene_vec2_init (&minus_one, -1.f, -1.f);
 
   self = gtk_canvas_size_alloc ();
-  gtk_canvas_vec2_init_sum (&self->vec2,
+  gtk_canvas_vector_init_sum (&self->vec2,
                             graphene_vec2_one (),
                             from,
                             &minus_one,
@@ -141,7 +141,7 @@ gtk_canvas_size_new_measure_item (GtkCanvasItem            *item,
   g_return_val_if_fail (GTK_IS_CANVAS_ITEM (item), NULL);
 
   self = gtk_canvas_size_alloc ();
-  gtk_canvas_vec2_init_copy (&self->vec2,
+  gtk_canvas_vector_init_copy (&self->vec2,
                              gtk_canvas_item_get_measure_vec2 (item, measure));
 
   return self;
@@ -155,7 +155,7 @@ gtk_canvas_size_copy (const GtkCanvasSize *self)
   g_return_val_if_fail (self != NULL, NULL);
 
   copy = gtk_canvas_size_alloc ();
-  gtk_canvas_vec2_init_copy (&copy->vec2, &self->vec2);
+  gtk_canvas_vector_init_copy (&copy->vec2, &self->vec2);
 
   return copy;
 }
@@ -165,7 +165,7 @@ gtk_canvas_size_free (GtkCanvasSize *self)
 {
   g_return_if_fail (self != NULL);
 
-  gtk_canvas_vec2_finish (&self->vec2);
+  gtk_canvas_vector_finish (&self->vec2);
 
   g_slice_free (GtkCanvasSize, self);
 }
@@ -181,7 +181,7 @@ gtk_canvas_size_eval (const GtkCanvasSize *self,
   g_return_val_if_fail (width != NULL, FALSE);
   g_return_val_if_fail (height != NULL, FALSE);
 
-  if (!gtk_canvas_vec2_eval (&self->vec2, &vec2))
+  if (!gtk_canvas_vector_eval (&self->vec2, &vec2))
     {
       *width = 0;
       *height = 0;
diff --git a/gtk/gtkcanvasvectorimpl.c b/gtk/gtkcanvasvectorimpl.c
new file mode 100644
index 0000000000..154e314d8f
--- /dev/null
+++ b/gtk/gtkcanvasvectorimpl.c
@@ -0,0 +1,348 @@
+/*
+ * Copyright © 2022 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>
+ */
+
+
+/**
+ * GtkCanvasVector:
+ *
+ * `GtkCanvasVector` describes a vector in a `GtkCanvas`.
+ */
+
+#include "config.h"
+
+#include "gtkcanvasvectorprivate.h"
+
+/* {{{ Boilerplate */
+
+struct _GtkCanvasVectorClass
+{
+  const char *type_name;
+
+  void                  (* copy)                (GtkCanvasVector        *self,
+                                                 const GtkCanvasVector  *source);
+  void                  (* finish)              (GtkCanvasVector        *self);
+  gboolean              (* eval)                (const GtkCanvasVector  *self,
+                                                 graphene_vec2_t        *result);
+};
+
+/* }}} */
+/* {{{ INVALID */
+
+static void
+gtk_canvas_vector_invalid_copy (GtkCanvasVector       *vector,
+                                const GtkCanvasVector *source_vector)
+{
+  gtk_canvas_vector_init_invalid (vector);
+}
+
+static void
+gtk_canvas_vector_invalid_finish (GtkCanvasVector *vector)
+{
+}
+
+static gboolean
+gtk_canvas_vector_invalid_eval (const GtkCanvasVector *vector,
+                                graphene_vec2_t       *result)
+{
+  return FALSE;
+}
+
+static const GtkCanvasVectorClass GTK_CANVAS_VECTOR_INVALID_CLASS =
+{
+  "GtkCanvasVectorInvalid",
+  gtk_canvas_vector_invalid_copy,
+  gtk_canvas_vector_invalid_finish,
+  gtk_canvas_vector_invalid_eval,
+};
+
+void
+gtk_canvas_vector_init_invalid (GtkCanvasVector *vector)
+{
+  vector->class = &GTK_CANVAS_VECTOR_INVALID_CLASS;
+}
+
+gboolean
+gtk_canvas_vector_is_invalid (GtkCanvasVector *vector)
+{
+  return vector->class == &GTK_CANVAS_VECTOR_INVALID_CLASS;
+}
+
+/* }}} */
+/* {{{ CONSTANT */
+
+static void
+gtk_canvas_vector_constant_copy (GtkCanvasVector       *vector,
+                                 const GtkCanvasVector *source_vector)
+{
+  const GtkCanvasVectorConstant *source = &source_vector->constant;
+
+  gtk_canvas_vector_init_constant_from_vector (vector, &source->value);
+}
+
+static void
+gtk_canvas_vector_constant_finish (GtkCanvasVector *vector)
+{
+}
+
+static gboolean
+gtk_canvas_vector_constant_eval (const GtkCanvasVector *vector,
+                                 graphene_vec2_t       *result)
+{
+  const GtkCanvasVectorConstant *self = &vector->constant;
+
+  graphene_vec2_init_from_vec2 (result, &self->value);
+
+  return TRUE;
+}
+
+static const GtkCanvasVectorClass GTK_CANVAS_VECTOR_CONSTANT_CLASS =
+{
+  "GtkCanvasVectorConstant",
+  gtk_canvas_vector_constant_copy,
+  gtk_canvas_vector_constant_finish,
+  gtk_canvas_vector_constant_eval,
+};
+
+void
+gtk_canvas_vector_init_constant_from_vector (GtkCanvasVector       *vector,
+                                             const graphene_vec2_t *value)
+{
+  GtkCanvasVectorConstant *self = &vector->constant;
+
+  self->class = &GTK_CANVAS_VECTOR_CONSTANT_CLASS;
+  graphene_vec2_init_from_vec2 (&self->value, value);
+}
+
+void
+gtk_canvas_vector_init_constant (GtkCanvasVector *vector,
+                               float          x,
+                               float          y)
+{
+  graphene_vec2_t v;
+
+  graphene_vec2_init (&v, x, y);
+  gtk_canvas_vector_init_constant_from_vector (vector, &v);
+}
+
+/* }}} */
+/* {{{ SUM */
+
+static void
+gtk_canvas_vector_sum_copy (GtkCanvasVector       *vector,
+                            const GtkCanvasVector *source_vector);
+
+static void
+gtk_canvas_vector_sum_finish (GtkCanvasVector *vector)
+{
+  GtkCanvasVectorSum *self = &vector->sum;
+  gsize i;
+
+  for (i = 0; i < self->n_summands; i++)
+    {
+      gtk_canvas_vector_finish (&self->summands[i].value);
+    }
+
+  g_free (self->summands);
+}
+
+static gboolean
+gtk_canvas_vector_sum_eval (const GtkCanvasVector *vector,
+                            graphene_vec2_t       *result)
+{
+  const GtkCanvasVectorSum *self = &vector->sum;
+  gsize i;
+
+  if (!gtk_canvas_vector_eval (&self->summands[0].value, result))
+    return FALSE;
+  graphene_vec2_multiply (&self->summands[0].scale, result, result);
+
+  for (i = 1; i < self->n_summands; i++)
+    {
+      graphene_vec2_t tmp;
+
+      if (!gtk_canvas_vector_eval (&self->summands[i].value, &tmp))
+        return FALSE;
+      graphene_vec2_multiply (&self->summands[i].scale, &tmp, &tmp);
+      graphene_vec2_add (&tmp, result, result);
+    }
+
+  return TRUE;
+}
+
+static const GtkCanvasVectorClass GTK_CANVAS_VECTOR_SUM_CLASS =
+{
+  "GtkCanvasVectorSum",
+  gtk_canvas_vector_sum_copy,
+  gtk_canvas_vector_sum_finish,
+  gtk_canvas_vector_sum_eval,
+};
+
+static void
+gtk_canvas_vector_sum_copy (GtkCanvasVector       *vector,
+                            const GtkCanvasVector *source_vector)
+{
+  GtkCanvasVectorSum *self = &vector->sum;
+  const GtkCanvasVectorSum *source = &source_vector->sum;
+  gsize i;
+
+  self->class = &GTK_CANVAS_VECTOR_SUM_CLASS;
+  self->summands = g_new (GtkCanvasVectorSummand, source->n_summands);
+  self->n_summands = source->n_summands;
+  for (i = 0; i < self->n_summands; i++)
+    {
+      graphene_vec2_init_from_vec2 (&self->summands[i].scale,
+                                    &source->summands[i].scale);
+      gtk_canvas_vector_init_copy (&self->summands[i].value,
+                                   &source->summands[i].value);
+    }
+}
+
+void
+gtk_canvas_vector_init_sum (GtkCanvasVector         *vector,
+                            const graphene_vec2_t *scale,
+                            ...)
+{
+  GtkCanvasVectorSum *self = &vector->sum;
+  GArray *array;
+  GtkCanvasVectorSummand summand;
+  va_list args;
+
+  g_assert (scale != NULL);
+
+  self->class = &GTK_CANVAS_VECTOR_SUM_CLASS;
+
+  array = g_array_new (FALSE, FALSE, sizeof (GtkCanvasVectorSummand));
+  va_start (args, scale);
+
+  while (scale)
+    {
+      graphene_vec2_init_from_vec2 (&summand.scale, scale);
+      gtk_canvas_vector_init_copy (&summand.value, va_arg (args, const GtkCanvasVector *));
+      g_array_append_val (array, summand);
+      scale = va_arg (args, const graphene_vec2_t *);
+    }
+  va_end (args);
+
+  self->n_summands = array->len;
+  self->summands = (GtkCanvasVectorSummand *) g_array_free (array, FALSE);
+}
+
+/* }}} */
+/* {{{ VARIABLE */
+
+static void
+gtk_canvas_vector_init_variable_with_variable (GtkCanvasVector *vector,
+                                               GtkCanvasVector *variable);
+
+static void
+gtk_canvas_vector_variable_copy (GtkCanvasVector       *vector,
+                                 const GtkCanvasVector *source_vector)
+{
+  const GtkCanvasVectorVariable *source = &source_vector->variable;
+
+  gtk_canvas_vector_init_variable_with_variable (vector, g_rc_box_acquire (source->variable));
+}
+
+static void
+gtk_canvas_vector_variable_finish (GtkCanvasVector *vector)
+{
+  const GtkCanvasVectorVariable *self = &vector->variable;
+
+  g_rc_box_release (self->variable);
+}
+
+static gboolean
+gtk_canvas_vector_variable_eval (const GtkCanvasVector *vector,
+                                 graphene_vec2_t       *result)
+{
+  const GtkCanvasVectorVariable *self = &vector->variable;
+
+  return gtk_canvas_vector_eval (self->variable, result);
+}
+
+static const GtkCanvasVectorClass GTK_CANVAS_VECTOR_VARIABLE_CLASS =
+{
+  "GtkCanvasVectorVariable",
+  gtk_canvas_vector_variable_copy,
+  gtk_canvas_vector_variable_finish,
+  gtk_canvas_vector_variable_eval,
+};
+
+static void
+gtk_canvas_vector_init_variable_with_variable (GtkCanvasVector *vector,
+                                               GtkCanvasVector *variable)
+{
+  GtkCanvasVectorVariable *self = &vector->variable;
+
+  self->class = &GTK_CANVAS_VECTOR_VARIABLE_CLASS;
+  
+  self->variable = variable;
+}
+
+void
+gtk_canvas_vector_init_variable (GtkCanvasVector *vector)
+{
+  GtkCanvasVector *variable;
+
+  variable = g_rc_box_new (GtkCanvasVector);
+  gtk_canvas_vector_init_invalid (variable);
+
+  gtk_canvas_vector_init_variable_with_variable (vector, variable);
+}
+
+GtkCanvasVector *
+gtk_canvas_vector_get_variable (GtkCanvasVector *vector)
+{
+  GtkCanvasVectorVariable *self = &vector->variable;
+
+  g_return_val_if_fail (self->class == &GTK_CANVAS_VECTOR_VARIABLE_CLASS, NULL);
+
+  return self->variable;
+}
+
+/* }}} */
+/* {{{ PUBLIC API */
+
+void
+gtk_canvas_vector_init_copy (GtkCanvasVector       *self,
+                             const GtkCanvasVector *source)
+{
+  source->class->copy (self, source);
+}
+
+void
+gtk_canvas_vector_finish (GtkCanvasVector *self)
+{
+  self->class->finish (self);
+}
+
+gboolean
+gtk_canvas_vector_eval (const GtkCanvasVector *self,
+                        graphene_vec2_t       *result)
+{
+  g_return_val_if_fail (self != NULL, FALSE);
+  g_return_val_if_fail (result != NULL, FALSE);
+
+  if (self->class->eval (self, result))
+    return TRUE;
+
+  graphene_vec2_init_from_vec2 (result, graphene_vec2_zero ());
+  return FALSE;
+}
+
diff --git a/gtk/gtkcanvasvectorprivate.h b/gtk/gtkcanvasvectorprivate.h
new file mode 100644
index 0000000000..1a60406ab0
--- /dev/null
+++ b/gtk/gtkcanvasvectorprivate.h
@@ -0,0 +1,101 @@
+/*
+ * Copyright © 2022 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 __GTK_CANVAS_VECTOR_PRIVATE_H__
+#define __GTK_CANVAS_VECTOR_PRIVATE_H__
+
+#include <glib.h>
+#include <graphene.h>
+
+G_BEGIN_DECLS
+
+typedef struct _GtkCanvasVector GtkCanvasVector;
+typedef struct _GtkCanvasVectorClass GtkCanvasVectorClass;
+typedef struct _GtkCanvasVectorConstant GtkCanvasVectorConstant;
+typedef struct _GtkCanvasVectorSum GtkCanvasVectorSum;
+typedef struct _GtkCanvasVectorSummand GtkCanvasVectorSummand;
+typedef struct _GtkCanvasVectorVariable GtkCanvasVectorVariable;
+
+struct _GtkCanvasVectorConstant
+{
+  const GtkCanvasVectorClass *class;
+
+  graphene_vec2_t value;
+};
+
+struct _GtkCanvasVectorSum
+{
+  const GtkCanvasVectorClass *class;
+  
+  graphene_vec2_t constant;
+
+  gsize n_summands;
+  GtkCanvasVectorSummand *summands;
+};
+
+struct _GtkCanvasVectorVariable
+{
+  const GtkCanvasVectorClass *class;
+
+  /* a GtkRcBox */
+  GtkCanvasVector *variable;
+};
+
+struct _GtkCanvasVector
+{
+  union {
+    const GtkCanvasVectorClass *class;
+    GtkCanvasVectorConstant constant;
+    GtkCanvasVectorSum sum;
+    GtkCanvasVectorVariable variable;
+  };
+};
+
+struct _GtkCanvasVectorSummand
+{
+  graphene_vec2_t scale;
+  GtkCanvasVector value;
+};
+
+
+void                    gtk_canvas_vector_init_copy             (GtkCanvasVector        *self,
+                                                                 const GtkCanvasVector  *source);
+void                    gtk_canvas_vector_finish                (GtkCanvasVector        *self);
+
+gboolean                gtk_canvas_vector_eval                  (const GtkCanvasVector  *self,
+                                                                 graphene_vec2_t        *result);
+
+void                    gtk_canvas_vector_init_invalid          (GtkCanvasVector        *vector);
+void                    gtk_canvas_vector_init_constant         (GtkCanvasVector        *vector,
+                                                                 float                   x,
+                                                                 float                   y);
+void                    gtk_canvas_vector_init_constant_from_vector (GtkCanvasVector    *vector,
+                                                                 const graphene_vec2_t  *value);
+void                    gtk_canvas_vector_init_sum              (GtkCanvasVector        *vector,
+                                                                 const graphene_vec2_t  *scale,
+                                                                 ...) G_GNUC_NULL_TERMINATED;
+
+void                    gtk_canvas_vector_init_variable         (GtkCanvasVector        *vector);
+GtkCanvasVector *       gtk_canvas_vector_get_variable          (GtkCanvasVector        *vector);
+
+gboolean                gtk_canvas_vector_is_invalid            (GtkCanvasVector        *vector);
+
+G_END_DECLS
+
+#endif /* __GTK_CANVAS_VECTOR_PRIVATE_H__ */
diff --git a/gtk/meson.build b/gtk/meson.build
index 93db1fb16c..d534f0f2d5 100644
--- a/gtk/meson.build
+++ b/gtk/meson.build
@@ -32,7 +32,7 @@ gtk_private_sources = files([
   'gtkbuilderprecompile.c',
   'gtkbuiltinicon.c',
   'gtkcellareaboxcontext.c',
-  'gtkcanvasvec2.c',
+  'gtkcanvasvectorimpl.c',
   'gtkcoloreditor.c',
   'gtkcolorplane.c',
   'gtkcolorpicker.c',


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