[gtk/glyphy2: 47/47] Add a font rendering test




commit f04c7177cd3aacaf0f7e1ecd56bbe925e0552086
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Apr 2 20:57:59 2022 -0400

    Add a font rendering test

 tests/bigfont.c   | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/meson.build |   1 +
 2 files changed, 120 insertions(+)
---
diff --git a/tests/bigfont.c b/tests/bigfont.c
new file mode 100644
index 0000000000..c291cbee43
--- /dev/null
+++ b/tests/bigfont.c
@@ -0,0 +1,119 @@
+#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;
+};
+
+struct _DemoWidgetClass
+{
+  GtkWidgetClass parent_class;
+};
+
+G_DEFINE_TYPE (DemoWidget, demo_widget, GTK_TYPE_WIDGET)
+
+static void
+demo_widget_init (DemoWidget *self)
+{
+}
+
+static void
+demo_widget_snapshot (GtkWidget   *widget,
+                      GtkSnapshot *snapshot)
+{
+  DemoWidget *self = DEMO_WIDGET (widget);
+  PangoLayout *layout;
+  int width, height;
+  int pwidth, pheight;
+  PangoFontDescription *desc;
+  int size;
+  double scale;
+  int x, y;
+  GtkStyleContext *context;
+
+  width = gtk_widget_get_width (widget);
+  height = gtk_widget_get_height (widget);
+
+  layout = gtk_widget_create_pango_layout (GTK_WIDGET (self), "Best Aa");
+
+  pango_layout_get_pixel_size (layout, &pwidth, &pheight);
+  desc = pango_font_description_copy_static (pango_context_get_font_description (pango_layout_get_context 
(layout)));
+  size = pango_font_description_get_size (desc);
+
+  scale = MIN (width / (double)pwidth, height / (double)pheight);
+
+  pango_font_description_set_size (desc, size * scale * 0.5);
+  pango_layout_set_font_description (layout, desc);
+  pango_font_description_free (desc);
+
+  pango_layout_get_pixel_size (layout, &pwidth, &pheight);
+
+  x = floor ((width - pwidth) / 2);
+  y = floor ((height - pheight) / 2);
+
+  context = gtk_widget_get_style_context (widget);
+
+  gtk_snapshot_render_layout (snapshot, context, x, y, layout);
+
+  g_object_unref (layout);
+}
+
+static void
+demo_widget_dispose (GObject *object)
+{
+  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;
+}
+
+static GtkWidget *
+demo_widget_new (void)
+{
+  return g_object_new (DEMO_TYPE_WIDGET, NULL);
+}
+
+static const char css[] =
+  "* {\n"
+  "  font-family: Cantarell;\n"
+  "  font-weight: 520;\n"
+  "}";
+
+int
+main (int argc, char *argv[])
+{
+  GtkCssProvider *style;
+  GtkWidget *window;
+  GtkWidget *header;
+  GtkWidget *toggle;
+
+  gtk_init ();
+
+  style = gtk_css_provider_new ();
+  gtk_css_provider_load_from_data (style, css, strlen (css));
+  gtk_style_context_add_provider_for_display (gdk_display_get_default (),
+                                              GTK_STYLE_PROVIDER (style),
+                                              800);
+
+  window = gtk_window_new ();
+
+  gtk_window_set_child (GTK_WINDOW (window), demo_widget_new ());
+
+  gtk_widget_show (window);
+
+  while (1)
+    g_main_context_iteration (NULL, TRUE);
+
+  return 0;
+}
diff --git a/tests/meson.build b/tests/meson.build
index 1a221d1829..6efae032c1 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -4,6 +4,7 @@ gtk_tests = [
   ['testpopup'],
   ['animated-resizing', ['frame-stats.c', 'variable.c']],
   ['animated-revealing', ['frame-stats.c', 'variable.c']],
+  ['bigfont'],
   ['blur-performance', ['../gsk/gskcairoblur.c']],
   ['motion-compression'],
   ['overlayscroll'],


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