[gtk/path-stroke: 72/75] Add basic tests for strokes




commit dc6c49bc6dbc4a48917610b3103b37dedf117c39
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Dec 6 13:36:38 2020 -0500

    Add basic tests for strokes
    
    Add tests to check that any point on a path is
    always at most half the line-width away from the
    stroke path with that line-width.

 testsuite/gsk/path-stroke.c | 149 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 148 insertions(+), 1 deletion(-)
---
diff --git a/testsuite/gsk/path-stroke.c b/testsuite/gsk/path-stroke.c
index bf40707f59..92ad8c50d0 100644
--- a/testsuite/gsk/path-stroke.c
+++ b/testsuite/gsk/path-stroke.c
@@ -55,13 +55,160 @@ test_point_to_stroke (void)
   gsk_path_unref (path);
 }
 
+/* Test that the offset curves are generally where they need to be */
+
+static void
+check_stroke_at_position (GskPathMeasure *measure,
+                          GskStroke      *stroke,
+                          GskPathMeasure *stroke_measure,
+                          float           position)
+{
+  graphene_point_t p;
+  graphene_point_t s;
+  float w;
+  float d;
+
+  w = gsk_stroke_get_line_width (stroke);
+
+  gsk_path_measure_get_point (measure, position, &p, NULL);
+  gsk_path_measure_get_closest_point (stroke_measure, &p, &s);
+
+  d = graphene_point_distance (&p, &s, NULL, NULL);
+  if (d > w/2 + gsk_path_measure_get_tolerance (stroke_measure))
+    {
+      char *str = gsk_path_to_string (gsk_path_measure_get_path (measure));
+      char *str2 = gsk_path_to_string (gsk_path_measure_get_path (stroke_measure));
+      g_print ("BAD!\n"
+               "path: %s\n"
+               "stroke path: %s\n"
+               "line width: %f\n"
+               "position: %f\n"
+               "distance: %f\n"
+               "curve point: %f %f\n"
+               "offset point: %f %f\n",
+                   str, str2, w, position, d,
+                   p.x, p.y, s.x, s.y);
+      g_free (str);
+      g_free (str2);
+      g_assert_not_reached ();
+    }
+}
+
+static void
+check_stroke_distance (GskPath        *path,
+                       GskPathMeasure *measure,
+                       GskStroke      *stroke,
+                       GskPath        *stroke_path)
+{
+  GskPathMeasure *stroke_measure;
+  float length;
+  float t;
+  int i;
+
+  stroke_measure = gsk_path_measure_new_with_tolerance (stroke_path, 0.1);
+  length = gsk_path_measure_get_length (measure);
+
+  for (i = 0; i < 1000; i++)
+    {
+      t = g_test_rand_double_range (0, length);
+      check_stroke_at_position (measure, stroke, stroke_measure, t);
+    }
+
+  gsk_path_measure_unref (stroke_measure);
+}
+
+static void
+test_rect_stroke_distance (void)
+{
+  GskPathBuilder *builder;
+  GskPath *path;
+  GskPathMeasure *measure;
+  GskPath *stroke_path;
+  GskStroke *stroke;
+
+  builder = gsk_path_builder_new ();
+
+  gsk_path_builder_add_rect (builder, &GRAPHENE_RECT_INIT (0, 0, 100, 100));
+
+  path = gsk_path_builder_free_to_path (builder);
+
+  stroke = gsk_stroke_new (10);
+
+  measure = gsk_path_measure_new (path);
+  stroke_path = gsk_path_stroke (path, stroke);
+
+  check_stroke_distance (path, measure, stroke, stroke_path);
+
+  gsk_stroke_free (stroke);
+
+  gsk_path_unref (stroke_path);
+  gsk_path_measure_unref (measure);
+  gsk_path_unref (path);
+}
+
+static void
+test_circle_stroke_distance (void)
+{
+  GskPathBuilder *builder;
+  GskPath *path;
+  GskPathMeasure *measure;
+  GskPath *stroke_path;
+  GskStroke *stroke;
+
+  builder = gsk_path_builder_new ();
+
+  gsk_path_builder_add_circle (builder, &GRAPHENE_POINT_INIT (100, 100), 50);
+
+  path = gsk_path_builder_free_to_path (builder);
+
+  stroke = gsk_stroke_new (10);
+
+  measure = gsk_path_measure_new (path);
+  stroke_path = gsk_path_stroke (path, stroke);
+
+  check_stroke_distance (path, measure, stroke, stroke_path);
+
+  gsk_stroke_free (stroke);
+
+  gsk_path_unref (stroke_path);
+  gsk_path_measure_unref (measure);
+  gsk_path_unref (path);
+}
+
+static void
+test_path_stroke_distance (void)
+{
+  GskPath *path;
+  GskPathMeasure *measure;
+  GskPath *stroke_path;
+  GskStroke *stroke;
+
+  path = gsk_path_parse ("M 250 150 A 100 100 0 0 0 50 150 A 100 100 0 0 0 250 150 z M 100 100 h 100 v 100 h 
-100 z M 300 150 C 300 50, 400 50, 400 150 C 400 250, 500 250, 500 150 L 600 150 L 530 190");
+
+  stroke = gsk_stroke_new (10);
+
+  measure = gsk_path_measure_new (path);
+  stroke_path = gsk_path_stroke (path, stroke);
+
+  check_stroke_distance (path, measure, stroke, stroke_path);
+
+  gsk_stroke_free (stroke);
+
+  gsk_path_unref (stroke_path);
+  gsk_path_measure_unref (measure);
+  gsk_path_unref (path);
+}
+
 int
 main (int   argc,
       char *argv[])
 {
   gtk_test_init (&argc, &argv, NULL);
 
-  g_test_add_func ("/path/point_to_stroke", test_point_to_stroke);
+  g_test_add_func ("/stroke/point", test_point_to_stroke);
+  g_test_add_func ("/stroke/rect/distance", test_rect_stroke_distance);
+  g_test_add_func ("/stroke/circle/distance", test_circle_stroke_distance);
+  g_test_add_func ("/stroke/path/distance", test_path_stroke_distance);
 
   return g_test_run ();
 }


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