[gtk/matthiasc/lottie2: 2/8] gsk: Implement line caps and joins




commit 3d513f0ee8e658d5bd1a93e12b085e939e99e7cc
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Nov 25 10:07:04 2020 -0500

    gsk: Implement line caps and joins
    
    Add line caps and joins to GskStroke. For the actual
    implementation, this is just passing things through
    to cairo.

 gsk/gskenums.h            | 14 ++++++++++++++
 gsk/gskrendernodeimpl.c   |  2 ++
 gsk/gskrendernodeparser.c | 23 +++++++++++++++++++++++
 gsk/gskstroke.c           | 25 +++++++++++++++++++++++++
 gsk/gskstroke.h           | 14 ++++++++++++++
 gsk/gskstrokeprivate.h    |  2 ++
 gtk/inspector/recorder.c  | 28 ++++++++++++++++++++++------
 7 files changed, 102 insertions(+), 6 deletions(-)
---
diff --git a/gsk/gskenums.h b/gsk/gskenums.h
index 44e8854874..db77f48381 100644
--- a/gsk/gskenums.h
+++ b/gsk/gskenums.h
@@ -300,4 +300,18 @@ typedef enum
 } GskGLUniformType;
 
 
+typedef enum
+{
+  GSK_LINE_CAP_BUTT,
+  GSK_LINE_CAP_ROUND,
+  GSK_LINE_CAP_SQUARE
+} GskLineCap;
+
+typedef enum
+{
+  GSK_LINE_JOIN_MITER,
+  GSK_LINE_JOIN_ROUND,
+  GSK_LINE_JOIN_BEVEL
+} GskLineJoin;
+
 #endif /* __GSK_TYPES_H__ */
diff --git a/gsk/gskrendernodeimpl.c b/gsk/gskrendernodeimpl.c
index 0fae26f198..5274296f40 100644
--- a/gsk/gskrendernodeimpl.c
+++ b/gsk/gskrendernodeimpl.c
@@ -3464,6 +3464,8 @@ gsk_stroke_node_draw (GskRenderNode *node,
   cairo_pop_group_to_source (cr);
 
   cairo_set_line_width (cr, self->stroke.line_width);
+  cairo_set_line_cap (cr, (cairo_line_cap_t)self->stroke.line_cap);
+  cairo_set_line_join (cr, (cairo_line_join_t)self->stroke.line_join);
 
   gsk_path_to_cairo (self->path, cr);
   cairo_stroke (cr);
diff --git a/gsk/gskrendernodeparser.c b/gsk/gskrendernodeparser.c
index a201919282..b36ca99a27 100644
--- a/gsk/gskrendernodeparser.c
+++ b/gsk/gskrendernodeparser.c
@@ -1791,18 +1791,36 @@ parse_fill_node (GtkCssParser *parser)
   return result;
 }
 
+static gboolean
+parse_line_cap (GtkCssParser *parser,
+                gpointer      out)
+{
+  return parse_enum (parser, GSK_TYPE_LINE_CAP, out);
+}
+
+static gboolean
+parse_line_join (GtkCssParser *parser,
+                 gpointer      out)
+{
+  return parse_enum (parser, GSK_TYPE_LINE_JOIN, out);
+}
+
 static GskRenderNode *
 parse_stroke_node (GtkCssParser *parser)
 {
   GskRenderNode *child = NULL;
   GskPath *path = NULL;
   double line_width = 1.0;
+  double line_cap = GSK_LINE_CAP_BUTT;
+  double line_join = GSK_LINE_JOIN_MITER;
   GskStroke *stroke;
 
   const Declaration declarations[] = {
     { "child", parse_node, clear_node, &child },
     { "path", parse_path, NULL, &path },
     { "line-width", parse_double, NULL, &line_width },
+    { "line-cap", parse_line_cap, NULL, &line_cap },
+    { "line-join", parse_line_join, NULL, &line_join },
   };
   GskRenderNode *result;
 
@@ -1811,6 +1829,8 @@ parse_stroke_node (GtkCssParser *parser)
     child = create_default_render_node ();
 
   stroke = gsk_stroke_new (line_width);
+  gsk_stroke_set_line_cap (stroke, line_cap);
+  gsk_stroke_set_line_join (stroke, line_join);
 
   result = gsk_stroke_node_new (child, path, stroke);
 
@@ -2566,6 +2586,9 @@ render_node_print (Printer       *p,
         stroke = gsk_stroke_node_get_stroke (node);
         append_float_param (p, "line-width", gsk_stroke_get_line_width (stroke), 0.0);
 
+        append_enum_param (p, "line-cap", GSK_TYPE_LINE_CAP, gsk_stroke_get_line_cap (stroke));
+        append_enum_param (p, "line-join", GSK_TYPE_LINE_JOIN, gsk_stroke_get_line_join (stroke));
+
         end_node (p);
       }
       break;
diff --git a/gsk/gskstroke.c b/gsk/gskstroke.c
index 755c9cdfe8..af1e793401 100644
--- a/gsk/gskstroke.c
+++ b/gsk/gskstroke.c
@@ -109,3 +109,28 @@ gsk_stroke_get_line_width (const GskStroke *self)
   return self->line_width;
 }
 
+void
+gsk_stroke_set_line_cap (GskStroke  *self,
+                         GskLineCap  line_cap)
+{
+  self->line_cap = line_cap;
+}
+
+GskLineCap
+gsk_stroke_get_line_cap (const GskStroke *self)
+{
+  return self->line_cap;
+}
+
+void
+gsk_stroke_set_line_join (GskStroke   *self,
+                          GskLineJoin  line_join)
+{
+  self->line_join = line_join;
+}
+
+GskLineJoin
+gsk_stroke_get_line_join (const GskStroke *self)
+{
+  return self->line_join;
+}
diff --git a/gsk/gskstroke.h b/gsk/gskstroke.h
index 3650325606..d9d1404ec6 100644
--- a/gsk/gskstroke.h
+++ b/gsk/gskstroke.h
@@ -26,6 +26,7 @@
 
 
 #include <gsk/gsktypes.h>
+#include <gsk/gskenums.h>
 
 G_BEGIN_DECLS
 
@@ -50,6 +51,19 @@ void                    gsk_stroke_set_line_width               (GskStroke
 GDK_AVAILABLE_IN_ALL
 float                   gsk_stroke_get_line_width               (const GskStroke        *self);
 
+GDK_AVAILABLE_IN_ALL
+void                    gsk_stroke_set_line_cap                 (GskStroke              *self,
+                                                                 GskLineCap              line_cap);
+
+GDK_AVAILABLE_IN_ALL
+GskLineCap              gsk_stroke_get_line_cap                 (const GskStroke        *self);
+
+GDK_AVAILABLE_IN_ALL
+void                    gsk_stroke_set_line_join                (GskStroke              *self,
+                                                                 GskLineJoin             line_join);
+
+GDK_AVAILABLE_IN_ALL
+GskLineJoin             gsk_stroke_get_line_join                (const GskStroke        *self);
 
 G_END_DECLS
 
diff --git a/gsk/gskstrokeprivate.h b/gsk/gskstrokeprivate.h
index aa77b88b12..f3754997d8 100644
--- a/gsk/gskstrokeprivate.h
+++ b/gsk/gskstrokeprivate.h
@@ -28,6 +28,8 @@ G_BEGIN_DECLS
 struct _GskStroke
 {
   float line_width;
+  GskLineCap line_cap;
+  GskLineJoin line_join;
 };
 
 static inline void
diff --git a/gtk/inspector/recorder.c b/gtk/inspector/recorder.c
index 4bf094a38c..267ad0de57 100644
--- a/gtk/inspector/recorder.c
+++ b/gtk/inspector/recorder.c
@@ -583,6 +583,20 @@ add_float_row (GtkListStore  *store,
   g_free (text);
 }
 
+static const char *
+enum_to_nick (GType type,
+              int   value)
+{
+  GEnumClass *class;
+  GEnumValue *v;
+
+  class = g_type_class_ref (type);
+  v = g_enum_get_value (class, value);
+  g_type_class_unref (class);
+
+  return v->value_nick;
+}
+
 static void
 populate_render_node_properties (GtkListStore  *store,
                                  GskRenderNode *node)
@@ -811,9 +825,7 @@ populate_render_node_properties (GtkListStore  *store,
     case GSK_BLEND_NODE:
       {
         GskBlendMode mode = gsk_blend_node_get_blend_mode (node);
-        tmp = g_enum_to_string (GSK_TYPE_BLEND_MODE, mode);
-        add_text_row (store, "Blendmode", tmp);
-        g_free (tmp);
+        add_text_row (store, "Blendmode", enum_to_nick (GSK_TYPE_BLEND_MODE, mode));
       }
       break;
 
@@ -1049,14 +1061,13 @@ populate_render_node_properties (GtkListStore  *store,
     case GSK_FILL_NODE:
       {
         GskPath *path = gsk_fill_node_get_path (node);
+        GskFillRule fill_rule = gsk_fill_node_get_fill_rule (node);
 
         tmp = gsk_path_to_string (path);
         add_text_row (store, "Path", tmp);
         g_free (tmp);
 
-        tmp = g_enum_to_string (GSK_TYPE_FILL_RULE, gsk_fill_node_get_fill_rule (node));
-        add_text_row (store, "Fill rule", tmp);
-        g_free (tmp);
+        add_text_row (store, "Fill rule", enum_to_nick (GSK_TYPE_FILL_RULE, fill_rule));
       }
       break;
 
@@ -1064,6 +1075,8 @@ populate_render_node_properties (GtkListStore  *store,
       {
         GskPath *path = gsk_stroke_node_get_path (node);
         const GskStroke *stroke = gsk_stroke_node_get_stroke (node);
+        GskLineCap line_cap = gsk_stroke_get_line_cap (stroke);
+        GskLineJoin line_join = gsk_stroke_get_line_join (stroke);
 
         tmp = gsk_path_to_string (path);
         add_text_row (store, "Path", tmp);
@@ -1072,6 +1085,9 @@ populate_render_node_properties (GtkListStore  *store,
         tmp = g_strdup_printf ("%.2f", gsk_stroke_get_line_width (stroke));
         add_text_row (store, "Line width", tmp);
         g_free (tmp);
+
+        add_text_row (store, "Line cap", enum_to_nick (GSK_TYPE_LINE_CAP, line_cap));
+        add_text_row (store, "Line join", enum_to_nick (GSK_TYPE_LINE_JOIN, line_join));
       }
       break;
 


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