[gtk/path-ops: 3/8] Add a glyphs test app




commit d38d871f8e39199b46e5d1c60bc052c082716fe6
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Mar 19 23:57:22 2022 -0400

    Add a glyphs test app
    
    This is a testbed for gsk_path_simplify. It loads
    a single glyph from a font file, and shows the outlines.

 tests/glyphs.c    | 662 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/meson.build |   1 +
 2 files changed, 663 insertions(+)
---
diff --git a/tests/glyphs.c b/tests/glyphs.c
new file mode 100644
index 0000000000..03d87cbaa1
--- /dev/null
+++ b/tests/glyphs.c
@@ -0,0 +1,662 @@
+#include <gtk/gtk.h>
+
+#define DEMO_TYPE_WIDGET (demo_widget_get_type ())
+G_DECLARE_FINAL_TYPE (DemoWidget, demo_widget, DEMO, WIDGET, GtkWidget)
+
+struct _DemoWidget
+{
+  GtkWidget parent_instance;
+  GskPath *orig_path;
+  GskPath *path;
+  GskPath *control_path;
+  graphene_point_t point;
+  graphene_point_t point2;
+  graphene_vec2_t tangent;
+  float curvature;
+  graphene_point_t center;
+
+  GtkWidget *label;
+
+  GskFillRule fill_rule;
+  gboolean do_fill;
+  gboolean do_outline;
+  gboolean show_points;
+  gboolean show_controls;
+  gboolean show_bounding_box;
+
+  graphene_rect_t bounds;
+
+  double zoom;
+
+  char *font_file;
+  gunichar ch;
+};
+
+struct _DemoWidgetClass
+{
+  GtkWidgetClass parent_class;
+};
+
+G_DEFINE_TYPE (DemoWidget, demo_widget, GTK_TYPE_WIDGET)
+
+static void
+demo_widget_init (DemoWidget *self)
+{
+  self->label = gtk_label_new ("");
+  gtk_widget_set_parent (self->label, GTK_WIDGET (self));
+  gtk_widget_set_halign (self->label, GTK_ALIGN_END);
+  gtk_widget_set_valign (self->label, GTK_ALIGN_START);
+  self->do_outline = TRUE;
+  self->font_file = g_strdup ("/usr/share/fonts/cantarell/Cantarell-VF.otf");
+  self->ch = 'e';
+}
+
+static void
+draw_point (GtkSnapshot            *snapshot,
+            const graphene_point_t *pt)
+{
+  graphene_rect_t bounds;
+  GdkRGBA color = { 1, 0, 0, 1 };
+
+  bounds.origin.x = pt->x - 2;
+  bounds.origin.y = pt->y - 2;
+  bounds.size.width = 4;
+  bounds.size.height = 4;
+
+  gtk_snapshot_append_color (snapshot, &color, &bounds);
+}
+
+static gboolean
+curve_cb (GskPathOperation        op,
+          const graphene_point_t *pts,
+          gsize                   n_ts,
+          float                   weight,
+          gpointer                user_data)
+{
+  GtkSnapshot *snapshot = user_data;
+
+  switch (op)
+    {
+    case GSK_PATH_MOVE:
+      break;
+
+    case GSK_PATH_CLOSE:
+      draw_point (snapshot, &pts[0]);
+      break;
+
+    case GSK_PATH_LINE:
+      draw_point (snapshot, &pts[1]);
+      break;
+
+    case GSK_PATH_CURVE:
+      draw_point (snapshot, &pts[1]);
+      draw_point (snapshot, &pts[2]);
+      draw_point (snapshot, &pts[3]);
+      break;
+
+    case GSK_PATH_CONIC:
+    default:
+      g_assert_not_reached ();
+    }
+
+  return TRUE;
+}
+
+static gboolean
+point_cb (GskPathOperation        op,
+          const graphene_point_t *pts,
+          gsize                   n_ts,
+          float                   weight,
+          gpointer                user_data)
+{
+  GtkSnapshot *snapshot = user_data;
+
+  switch (op)
+    {
+    case GSK_PATH_MOVE:
+      break;
+
+    case GSK_PATH_CLOSE:
+      draw_point (snapshot, &pts[0]);
+      break;
+
+    case GSK_PATH_LINE:
+      draw_point (snapshot, &pts[1]);
+      break;
+
+    case GSK_PATH_CURVE:
+      draw_point (snapshot, &pts[3]);
+      break;
+
+    case GSK_PATH_CONIC:
+    default:
+      g_assert_not_reached ();
+    }
+
+  return TRUE;
+}
+
+static void
+demo_widget_snapshot (GtkWidget   *widget,
+                      GtkSnapshot *snapshot)
+{
+  DemoWidget *self = DEMO_WIDGET (widget);
+  int width, height;
+
+  if (!self->path)
+    return;
+
+  gtk_snapshot_save (snapshot);
+
+  gtk_snapshot_scale (snapshot, self->zoom, self->zoom);
+
+  width = gtk_widget_get_width (widget);
+  height = gtk_widget_get_width (widget);
+
+  if (self->do_fill)
+    {
+      gtk_snapshot_push_fill (snapshot, self->path, self->fill_rule);
+
+      gtk_snapshot_append_color (snapshot,
+                                 &(GdkRGBA){ 1, 0, 1, 0.2},
+                                 &GRAPHENE_RECT_INIT (0, 0, width, height ));
+
+      gtk_snapshot_pop (snapshot);
+    }
+
+  if (self->do_outline)
+    {
+      GskStroke *stroke;
+
+      stroke = gsk_stroke_new (1.0);
+      gtk_snapshot_push_stroke (snapshot, self->path, stroke);
+      gsk_stroke_free (stroke);
+
+      gtk_snapshot_append_color (snapshot,
+                                 &(GdkRGBA){ 0, 0, 0, 1},
+                                 &GRAPHENE_RECT_INIT (0, 0, width, height ));
+
+      gtk_snapshot_pop (snapshot);
+    }
+
+  if (self->show_controls)
+    {
+      GskStroke *stroke;
+
+      stroke = gsk_stroke_new (1.0);
+      gtk_snapshot_push_stroke (snapshot, self->control_path, stroke);
+      gsk_stroke_free (stroke);
+
+      gtk_snapshot_append_color (snapshot,
+                                 &(GdkRGBA){ 1, 0, 0, 1},
+                                 &GRAPHENE_RECT_INIT (0, 0, width, height ));
+
+      gtk_snapshot_pop (snapshot);
+
+      gsk_path_foreach (self->path, GSK_PATH_FOREACH_ALLOW_CURVE, curve_cb, snapshot);
+    }
+  else if (self->show_points)
+    {
+      gsk_path_foreach (self->path, GSK_PATH_FOREACH_ALLOW_CURVE, point_cb, snapshot);
+    }
+
+  if (self->show_bounding_box)
+    {
+      graphene_rect_t bounds;
+
+      if (gsk_path_get_bounds (self->path, &bounds))
+        {
+          GskPathBuilder *builder;
+          GskPath *path;
+          GskStroke *stroke;
+
+          builder = gsk_path_builder_new ();
+
+          gsk_path_builder_add_rect (builder, &bounds);
+
+          path = gsk_path_builder_free_to_path (builder);
+
+          stroke = gsk_stroke_new (1.0);
+          gtk_snapshot_push_stroke (snapshot, path, stroke);
+          gsk_stroke_free (stroke);
+
+          gtk_snapshot_append_color (snapshot,
+                                     &(GdkRGBA){ 0, 0, 0, 0.5},
+                                     &GRAPHENE_RECT_INIT (0, 0, width, height ));
+
+          gtk_snapshot_pop (snapshot);
+
+          gsk_path_unref (path);
+        }
+    }
+
+  gtk_snapshot_restore (snapshot);
+}
+
+static void
+demo_widget_measure (GtkWidget      *widget,
+                     GtkOrientation  orientation,
+                     int             for_size,
+                     int            *minimum,
+                     int            *natural,
+                     int            *minimum_baseline,
+                     int            *natural_baseline)
+{
+  DemoWidget *self = DEMO_WIDGET (widget);
+  float size;
+
+  if (orientation == GTK_ORIENTATION_HORIZONTAL)
+    size = self->zoom * (self->bounds.origin.x + self->bounds.size.width + 10);
+  else
+    size = self->zoom * (self->bounds.origin.y + self->bounds.size.height + 10);
+
+  *minimum = *natural = (int)size;
+}
+
+static void
+demo_widget_size_allocate (GtkWidget           *widget,
+                           int                  width,
+                           int                  height,
+                           int                  baseline)
+{
+  DemoWidget *self = DEMO_WIDGET (widget);
+  GtkRequisition min, nat;
+
+  gtk_widget_get_preferred_size (self->label, &min, &nat);
+
+  gtk_widget_size_allocate (self->label,
+                            &(GtkAllocation) { width - nat.width, 0, nat.width, nat.height},
+                            -1);
+}
+
+static void
+demo_widget_dispose (GObject *object)
+{
+  DemoWidget *self = DEMO_WIDGET (object);
+
+  g_clear_pointer (&self->path, gsk_path_unref);
+  g_clear_pointer (&self->control_path, gsk_path_unref);
+  g_clear_pointer (&self->label, gtk_widget_unparent);
+  g_free (self->font_file);
+
+  G_OBJECT_CLASS (demo_widget_parent_class)->dispose (object);
+}
+
+static void
+demo_widget_class_init (DemoWidgetClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
+
+  object_class->dispose = demo_widget_dispose;
+
+  widget_class->snapshot = demo_widget_snapshot;
+  widget_class->measure = demo_widget_measure;
+  widget_class->size_allocate = demo_widget_size_allocate;
+}
+
+static GtkWidget *
+demo_widget_new (void)
+{
+  return g_object_new (DEMO_TYPE_WIDGET, NULL);
+}
+
+static gboolean
+control_cb (GskPathOperation        op,
+            const graphene_point_t *pts,
+            gsize                   n_pts,
+            float                   weight,
+            gpointer                user_data)
+{
+  GskPathBuilder *builder = user_data;
+
+  switch (op)
+    {
+    case GSK_PATH_MOVE:
+      gsk_path_builder_move_to (builder, pts[0].x, pts[0].y);
+      break;
+    case GSK_PATH_CLOSE:
+      gsk_path_builder_close (builder);
+      break;
+    case GSK_PATH_LINE:
+      gsk_path_builder_line_to (builder, pts[1].x, pts[1].y);
+      break;
+    case GSK_PATH_CURVE:
+      gsk_path_builder_line_to (builder, pts[1].x, pts[1].y);
+      gsk_path_builder_line_to (builder, pts[2].x, pts[2].y);
+      gsk_path_builder_line_to (builder, pts[3].x, pts[3].y);
+      break;
+    case GSK_PATH_CONIC:
+    default:
+      g_assert_not_reached ();
+    }
+
+  return TRUE;
+}
+
+static void
+demo_widget_set_path (DemoWidget *self,
+                      GskPath    *path)
+{
+  GskPathBuilder *builder;
+
+  g_clear_pointer (&self->path, gsk_path_unref);
+  g_clear_pointer (&self->control_path, gsk_path_unref);
+
+  self->path = gsk_path_simplify (path);
+
+  builder = gsk_path_builder_new ();
+  gsk_path_foreach (self->path, GSK_PATH_FOREACH_ALLOW_CURVE, control_cb, builder);
+  self->control_path = gsk_path_builder_free_to_path (builder);
+
+  gsk_path_get_bounds (self->path, &self->bounds);
+
+  gtk_widget_queue_draw (GTK_WIDGET (self));
+}
+
+static float glyph_x0, glyph_y0;
+
+static void
+move_to (hb_draw_funcs_t *dfuncs,
+         GskPathBuilder  *builder,
+         hb_draw_state_t *st,
+         float            x,
+         float            y,
+         void            *data)
+{
+  gsk_path_builder_move_to (builder, glyph_x0 + x, glyph_y0 - y);
+}
+
+static void
+line_to (hb_draw_funcs_t *dfuncs,
+         GskPathBuilder  *builder,
+         hb_draw_state_t *st,
+         float            x,
+         float            y,
+         void            *data)
+{
+  gsk_path_builder_line_to (builder, glyph_x0 + x, glyph_y0 - y);
+}
+
+static void
+cubic_to (hb_draw_funcs_t *dfuncs,
+          GskPathBuilder  *builder,
+          hb_draw_state_t *st,
+          float            x1,
+          float            y1,
+          float            x2,
+          float            y2,
+          float            x3,
+          float            y3,
+          void            *data)
+{
+  gsk_path_builder_curve_to (builder,
+                             glyph_x0 + x1, glyph_y0 - y1,
+                             glyph_x0 + x2, glyph_y0 - y2,
+                             glyph_x0 + x3, glyph_y0 - y3);
+}
+
+
+static void
+close_path (hb_draw_funcs_t *dfuncs,
+            GskPathBuilder  *builder,
+            hb_draw_state_t *st,
+            void            *data)
+{
+  gsk_path_builder_close (builder);
+}
+
+static void
+init_demo (DemoWidget *demo)
+{
+  hb_blob_t *blob;
+  hb_face_t *face;
+  hb_font_t *font;
+  hb_draw_funcs_t *funcs;
+  hb_codepoint_t glyph;
+  GskPathBuilder *builder;
+  GskPath *path;
+  hb_glyph_extents_t extents;
+
+  builder = gsk_path_builder_new ();
+
+  blob = hb_blob_create_from_file (demo->font_file);
+  face = hb_face_create (blob, 0);
+  font = hb_font_create (face);
+
+  funcs = hb_draw_funcs_create ();
+
+  hb_draw_funcs_set_move_to_func (funcs, (hb_draw_move_to_func_t) move_to, NULL, NULL);
+  hb_draw_funcs_set_line_to_func (funcs, (hb_draw_line_to_func_t) line_to, NULL, NULL);
+  hb_draw_funcs_set_cubic_to_func (funcs, (hb_draw_cubic_to_func_t) cubic_to, NULL, NULL);
+  hb_draw_funcs_set_close_path_func (funcs, (hb_draw_close_path_func_t) close_path, NULL, NULL);
+
+  hb_font_get_nominal_glyph (font, demo->ch, &glyph);
+
+  hb_font_get_glyph_extents (font, glyph, &extents);
+
+  glyph_x0 = 10 + extents.x_bearing;
+  glyph_y0 = 10 + extents.y_bearing;
+
+  gsk_path_builder_move_to (builder, extents.x_bearing, - extents.height);
+
+  hb_font_get_glyph_shape (font, glyph, funcs, builder);
+
+  hb_draw_funcs_destroy (funcs);
+
+  hb_font_destroy (font);
+  hb_face_destroy (face);
+  hb_blob_destroy (blob);
+
+  path = gsk_path_builder_free_to_path (builder);
+
+  demo_widget_set_path (demo, path);
+
+  gsk_path_unref (path);
+
+  demo->zoom = 1;
+}
+
+static void
+demo_widget_set_font_file (DemoWidget *demo,
+                           const char *file)
+{
+  g_free (demo->font_file);
+  demo->font_file = g_strdup (file);
+  init_demo (demo);
+}
+
+static void
+demo_widget_set_char (DemoWidget *demo,
+                      const char *text)
+{
+  demo->ch = g_utf8_get_char (text);
+  init_demo (demo);
+}
+
+static void
+zoom_changed (GtkRange   *range,
+              DemoWidget *self)
+{
+  self->zoom = gtk_range_get_value (range);
+  gtk_widget_queue_resize (GTK_WIDGET (self));
+}
+
+static void
+bb_toggled (GtkCheckButton *button,
+            DemoWidget      *self)
+{
+  self->show_bounding_box = gtk_check_button_get_active (button);
+  gtk_widget_queue_draw (GTK_WIDGET (self));
+}
+
+static void
+fill_toggled (GtkCheckButton *button,
+              DemoWidget      *self)
+{
+  self->do_fill = gtk_check_button_get_active (button);
+  gtk_widget_queue_draw (GTK_WIDGET (self));
+}
+
+static void
+points_toggled (GtkCheckButton *button,
+                DemoWidget      *self)
+{
+  self->show_points = gtk_check_button_get_active (button);
+  gtk_widget_queue_draw (GTK_WIDGET (self));
+}
+
+static void
+controls_toggled (GtkCheckButton *button,
+                  DemoWidget      *self)
+{
+  self->show_controls = gtk_check_button_get_active (button);
+  gtk_widget_queue_draw (GTK_WIDGET (self));
+}
+
+static void
+outline_toggled (GtkCheckButton *button,
+                 DemoWidget      *self)
+{
+  self->do_outline = gtk_check_button_get_active (button);
+  gtk_widget_queue_draw (GTK_WIDGET (self));
+}
+
+static void
+fill_rule_changed (GtkDropDown *combo,
+                   GParamSpec  *pspec,
+                   DemoWidget  *self)
+{
+  self->fill_rule = (GskFillRule)gtk_drop_down_get_selected (combo);
+  gtk_widget_queue_draw (GTK_WIDGET (self));
+}
+
+static void
+response_cb (GtkNativeDialog *dialog,
+             int response,
+             gpointer user_data)
+{
+  GtkButton *button = user_data;
+  DemoWidget *demo;
+
+  demo = (DemoWidget *)g_object_get_data (G_OBJECT (dialog), "demo");
+
+  if (response == GTK_RESPONSE_ACCEPT)
+    {
+      GFile *file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
+      const char *path = g_file_peek_path (file);
+      char *basename = g_path_get_basename (path);
+
+      gtk_button_set_label (button, basename);
+      g_free (basename);
+
+      demo_widget_set_font_file (demo, path);
+    }
+
+  gtk_native_dialog_destroy (dialog);
+  g_object_unref (dialog);
+}
+
+static void
+filechooser_cb (GtkButton  *button,
+                DemoWidget *demo)
+{
+  GtkFileChooserNative *dialog;
+
+  dialog = gtk_file_chooser_native_new ("Font",
+                                        GTK_WINDOW (gtk_widget_get_ancestor (GTK_WIDGET (button), 
GTK_TYPE_WINDOW)),
+                                        GTK_FILE_CHOOSER_ACTION_OPEN,
+                                        "Open", "Cancel");
+
+  g_object_set_data (G_OBJECT (dialog), "demo", demo);
+  g_signal_connect (dialog, "response", G_CALLBACK (response_cb), button);
+  gtk_native_dialog_show (GTK_NATIVE_DIALOG (dialog));
+}
+
+static void
+activate_entry (GtkEntry   *entry,
+                DemoWidget *demo)
+{
+  demo_widget_set_char (demo, gtk_editable_get_text (GTK_EDITABLE (entry)));
+}
+
+int
+main (int argc, char *argv[])
+{
+  GtkWidget *window, *box, *demo;
+  GtkWidget *button;
+  GtkWidget *header, *toggle;
+  GtkWidget *combo, *sw;
+  GtkWidget *zoom_scale;
+  GtkWidget *hbox, *entry;
+
+  gtk_init ();
+
+  window = gtk_window_new ();
+  gtk_window_set_default_size (GTK_WINDOW (window), 700, 500);
+  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
+  gtk_window_set_child (GTK_WINDOW (window), box);
+
+  demo = demo_widget_new ();
+  gtk_widget_set_hexpand (demo, TRUE);
+  gtk_widget_set_vexpand (demo, TRUE);
+  sw = gtk_scrolled_window_new ();
+  gtk_box_append (GTK_BOX (box), sw);
+  gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), demo);
+
+  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
+  gtk_box_append (GTK_BOX (box), hbox);
+  button = gtk_button_new_with_label ("Cantarell-VF.otf");
+  gtk_box_append (GTK_BOX (hbox), button);
+
+  g_signal_connect (button, "clicked", G_CALLBACK (filechooser_cb), demo);
+  entry = gtk_entry_new ();
+  gtk_editable_set_text (GTK_EDITABLE (entry), "e");
+  gtk_box_append (GTK_BOX (hbox), entry);
+  g_signal_connect (entry, "activate", G_CALLBACK (activate_entry), demo);
+
+  toggle = gtk_check_button_new_with_label ("Outline");
+  gtk_check_button_set_active (GTK_CHECK_BUTTON (toggle), TRUE);
+  g_signal_connect (toggle, "toggled", G_CALLBACK (outline_toggled), demo);
+  gtk_box_append (GTK_BOX (hbox), toggle);
+
+  toggle = gtk_check_button_new_with_label ("Fill");
+  g_signal_connect (toggle, "toggled", G_CALLBACK (fill_toggled), demo);
+  gtk_box_append (GTK_BOX (hbox), toggle);
+
+  toggle = gtk_check_button_new_with_label ("Points");
+  g_signal_connect (toggle, "toggled", G_CALLBACK (points_toggled), demo);
+  gtk_box_append (GTK_BOX (hbox), toggle);
+
+  toggle = gtk_check_button_new_with_label ("Controls");
+  g_signal_connect (toggle, "toggled", G_CALLBACK (controls_toggled), demo);
+  gtk_box_append (GTK_BOX (hbox), toggle);
+
+  toggle = gtk_check_button_new_with_label ("Bounds");
+  g_signal_connect (toggle, "toggled", G_CALLBACK (bb_toggled), demo);
+  gtk_box_append (GTK_BOX (hbox), toggle);
+
+  combo = gtk_drop_down_new_from_strings ((const char *[]){"Winding", "Even-Odd", NULL });
+  g_signal_connect (combo, "notify::selected", G_CALLBACK (fill_rule_changed), demo);
+  gtk_box_append (GTK_BOX (hbox), combo);
+
+  init_demo (DEMO_WIDGET (demo));
+
+  header = gtk_header_bar_new ();
+
+  zoom_scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, 0.3, 3, 0.1);
+  gtk_scale_add_mark (GTK_SCALE (zoom_scale), 1.0, GTK_POS_BOTTOM, NULL);
+  gtk_range_set_value (GTK_RANGE (zoom_scale), 1);
+  g_signal_connect (zoom_scale, "value-changed", G_CALLBACK (zoom_changed), demo);
+  gtk_widget_set_size_request (zoom_scale, 150, -1);
+  gtk_header_bar_pack_end (GTK_HEADER_BAR (header), zoom_scale);
+
+  gtk_window_set_titlebar (GTK_WINDOW (window), header);
+
+  gtk_window_present (GTK_WINDOW (window));
+
+  while (g_list_model_get_n_items (gtk_window_get_toplevels ()) > 0)
+    g_main_context_iteration (NULL, TRUE);
+
+  return 0;
+}
diff --git a/tests/meson.build b/tests/meson.build
index 7597f03181..1a221d1829 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -8,6 +8,7 @@ gtk_tests = [
   ['motion-compression'],
   ['overlayscroll'],
   ['curve2'],
+  ['glyphs'],
   ['testupload'],
   ['testtransform'],
   ['testdropdown'],


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