[pango/simple-fontmap: 1/8] Add a tests for PangoHbFace and PangoHbFont




commit 8ba3cdd829dde85bf99f835c2eff9488541acc2d
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Oct 31 19:48:52 2021 -0400

    Add a tests for PangoHbFace and PangoHbFont
    
    Add a dedicated test for various aspects of
    PangoHbFont and PangoHbFace functionality.

 tests/meson.build  |   1 +
 tests/testhbfont.c | 287 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/testmisc.c   |  52 ++++++++++
 3 files changed, 340 insertions(+)
---
diff --git a/tests/meson.build b/tests/meson.build
index b885f999..54bd5c13 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -57,6 +57,7 @@ if cairo_dep.found()
     tests += [
       [ 'testmisc', [ 'testmisc.c' ], [ libpangocairo_dep, libpangoft2_dep, glib_dep, harfbuzz_dep ] ],
       [ 'testserialize', [ 'testserialize.c' ], [ libpangocairo_dep, libpangoft2_dep ] ],
+      [ 'testhbfont', [ 'testhbfont.c' ], [ libpangocairo_dep, libpangoft2_dep, glib_dep, harfbuzz_dep ] ],
     ]
 
     if host_system != 'darwin'
diff --git a/tests/testhbfont.c b/tests/testhbfont.c
new file mode 100644
index 00000000..d05c5bdb
--- /dev/null
+++ b/tests/testhbfont.c
@@ -0,0 +1,287 @@
+/* Pango
+ * testhbfont.c: Test program for PangoHbFont etc
+ *
+ * Copyright (C) 2021 Matthias Clasen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+#include <glib.h>
+#include <pango/pangocairo.h>
+
+#include <pango/pangofc-fontmap2.h>
+
+#ifdef HAVE_CAIRO_FREETYPE
+#include <pango/pango-ot.h>
+#endif
+
+
+/* Verify that the variable and monospace properties work as expected
+ * for PangoHbFamily
+ */
+static void
+test_hbfont_monospace (void)
+{
+  PangoHbFontMap *map;
+  PangoFontFamily *family;
+  char *path;
+
+  map = pango_hb_font_map_new ();
+
+  path = g_test_build_filename (G_TEST_DIST, "fonts", "Cantarell-VF.otf", NULL);
+  pango_hb_font_map_add_file (map, path);
+  g_free (path);
+
+  family = pango_font_map_get_family (PANGO_FONT_MAP (map), "Cantarell");
+
+  g_assert_nonnull (family);
+  g_assert_true (pango_font_family_is_variable (family));
+  g_assert_false (pango_font_family_is_monospace (family));
+
+  path = g_test_build_filename (G_TEST_DIST, "fonts", "DejaVuSansMono.ttf", NULL);
+  pango_hb_font_map_add_file (map, path);
+  g_free (path);
+
+  family = pango_font_map_get_family (PANGO_FONT_MAP (map), "DejaVu Sans Mono");
+
+  g_assert_nonnull (family);
+  g_assert_false (pango_font_family_is_variable (family));
+  g_assert_true (pango_font_family_is_monospace (family));
+
+  g_object_unref (map);
+}
+
+/* Verify that a description -> face -> description roundtrip works for
+ * PangoHbFaces created with pango_hb_face_new_variant
+ */
+static void
+test_hbface_roundtrip (void)
+{
+  char *path;
+  PangoHbFace *face;
+  PangoHbFace *face2;
+  PangoFontDescription *desc;
+
+  path = g_test_build_filename (G_TEST_DIST, "fonts", "Cantarell-VF.otf", NULL);
+
+  face = pango_hb_face_new_from_file (path, 0, -1, NULL, NULL);
+  g_assert_true (PANGO_IS_HB_FACE (face));
+  g_assert_cmpstr (pango_font_face_get_face_name (PANGO_FONT_FACE (face)), ==, "Regular");
+  desc = pango_font_face_describe (PANGO_FONT_FACE (face));
+  g_assert_cmpint (pango_font_description_get_set_fields (desc), ==, PANGO_FONT_MASK_FAMILY |
+                                                                     PANGO_FONT_MASK_STYLE |
+                                                                     PANGO_FONT_MASK_WEIGHT |
+                                                                     PANGO_FONT_MASK_STRETCH);
+  g_assert_cmpstr (pango_font_description_get_family (desc), ==, "Cantarell");
+  g_assert_cmpint (pango_font_description_get_style (desc), ==, PANGO_STYLE_NORMAL);
+  g_assert_cmpint (pango_font_description_get_weight (desc), ==, PANGO_WEIGHT_NORMAL);
+  g_assert_cmpint (pango_font_description_get_stretch (desc), ==, PANGO_STRETCH_NORMAL);
+  pango_font_description_free (desc);
+
+  desc = pango_font_description_new ();
+  pango_font_description_set_style (desc, PANGO_STYLE_OBLIQUE);
+  face2 = pango_hb_face_new_variant (face, &(PangoMatrix){ 1, 0.2, 0, 1, 0, 0 }, FALSE, desc);
+  pango_font_description_free (desc);
+  g_assert_true (PANGO_IS_HB_FACE (face2));
+  g_assert_cmpstr (pango_font_face_get_face_name (PANGO_FONT_FACE (face2)), ==, "Oblique");
+  desc = pango_font_face_describe (PANGO_FONT_FACE (face2));
+  g_assert_cmpint (pango_font_description_get_set_fields (desc), ==, PANGO_FONT_MASK_FAMILY |
+                                                                     PANGO_FONT_MASK_STYLE |
+                                                                     PANGO_FONT_MASK_WEIGHT |
+                                                                     PANGO_FONT_MASK_STRETCH);
+  g_assert_cmpstr (pango_font_description_get_family (desc), ==, "Cantarell");
+  g_assert_cmpint (pango_font_description_get_style (desc), ==, PANGO_STYLE_OBLIQUE);
+  g_assert_cmpint (pango_font_description_get_weight (desc), ==, PANGO_WEIGHT_NORMAL);
+  g_assert_cmpint (pango_font_description_get_stretch (desc), ==, PANGO_STRETCH_NORMAL);
+  pango_font_description_free (desc);
+  g_object_unref (face2);
+
+  desc = pango_font_description_new ();
+  pango_font_description_set_weight (desc, PANGO_WEIGHT_BOLD);
+  face2 = pango_hb_face_new_variant (face, NULL, TRUE, desc);
+  pango_font_description_free (desc);
+  g_assert_true (PANGO_IS_HB_FACE (face2));
+  g_assert_cmpstr (pango_font_face_get_face_name (PANGO_FONT_FACE (face2)), ==, "Bold");
+  desc = pango_font_face_describe (PANGO_FONT_FACE (face2));
+  g_assert_cmpint (pango_font_description_get_set_fields (desc), ==, PANGO_FONT_MASK_FAMILY |
+                                                                     PANGO_FONT_MASK_STYLE |
+                                                                     PANGO_FONT_MASK_WEIGHT |
+                                                                     PANGO_FONT_MASK_STRETCH);
+  g_assert_cmpstr (pango_font_description_get_family (desc), ==, "Cantarell");
+  g_assert_cmpint (pango_font_description_get_style (desc), ==, PANGO_STYLE_NORMAL);
+  g_assert_cmpint (pango_font_description_get_weight (desc), ==, PANGO_WEIGHT_BOLD);
+  g_assert_cmpint (pango_font_description_get_stretch (desc), ==, PANGO_STRETCH_NORMAL);
+  pango_font_description_free (desc);
+  g_object_unref (face2);
+
+  desc = pango_font_description_new ();
+  pango_font_description_set_family (desc, "Cantarellagain");
+  face2 = pango_hb_face_new_variant (face, NULL, FALSE, desc);
+  pango_font_description_free (desc);
+  g_assert_true (PANGO_IS_HB_FACE (face2));
+  g_assert_cmpstr (pango_font_face_get_face_name (PANGO_FONT_FACE (face2)), ==, "Regular");
+  desc = pango_font_face_describe (PANGO_FONT_FACE (face2));
+  g_assert_cmpint (pango_font_description_get_set_fields (desc), ==, PANGO_FONT_MASK_FAMILY |
+                                                                     PANGO_FONT_MASK_STYLE |
+                                                                     PANGO_FONT_MASK_WEIGHT |
+                                                                     PANGO_FONT_MASK_STRETCH);
+  g_assert_cmpstr (pango_font_description_get_family (desc), ==, "Cantarellagain");
+  g_assert_cmpint (pango_font_description_get_style (desc), ==, PANGO_STYLE_NORMAL);
+  g_assert_cmpint (pango_font_description_get_weight (desc), ==, PANGO_WEIGHT_NORMAL);
+  g_assert_cmpint (pango_font_description_get_stretch (desc), ==, PANGO_STRETCH_NORMAL);
+  pango_font_description_free (desc);
+  g_object_unref (face2);
+
+  g_object_unref (face);
+  g_free (path);
+}
+
+/* Verify that face -> font -> description works as expected for PangoHbFont */
+static void
+test_hbfont_roundtrip (void)
+{
+  char *path;
+  PangoHbFace *face;
+  PangoHbFont *font;
+  PangoFontDescription *desc;
+  hb_feature_t features[10];
+  unsigned int n_features;
+
+  path = g_test_build_filename (G_TEST_DIST, "fonts", "Cantarell-VF.otf", NULL);
+
+  face = pango_hb_face_new_from_file (path, 0, -1, NULL, NULL);
+  g_assert_true (PANGO_IS_HB_FACE (face));
+
+  font = pango_hb_font_new (face, 11 * PANGO_SCALE, NULL, 0, NULL, PANGO_GRAVITY_AUTO, 96., NULL);
+  g_assert_true (PANGO_IS_HB_FONT (font));
+  g_assert_true (pango_font_get_face (PANGO_FONT (font)) == PANGO_FONT_FACE (face));
+  pango_font_get_features (PANGO_FONT (font), features, 10, &n_features);
+  g_assert_cmpint (n_features, ==, 0);
+
+  desc = pango_font_describe (PANGO_FONT (font));
+  g_assert_cmpstr (pango_font_description_get_family (desc), ==, "Cantarell");
+  g_assert_cmpint (pango_font_description_get_style (desc), ==, PANGO_STYLE_NORMAL);
+  g_assert_cmpint (pango_font_description_get_weight (desc), ==, PANGO_WEIGHT_NORMAL);
+  g_assert_cmpint (pango_font_description_get_stretch (desc), ==, PANGO_STRETCH_NORMAL);
+  g_assert_cmpint (pango_font_description_get_size (desc), ==, 11 * PANGO_SCALE);
+  pango_font_description_free (desc);
+
+  g_object_unref (font);
+  g_object_unref (face);
+  g_free (path);
+}
+
+static gboolean
+get_font (PangoFontset *fonts,
+          PangoFont    *font,
+          gpointer      data)
+{
+  gboolean *found = data;
+
+  PangoFontDescription *desc = pango_font_describe (font);
+  char *s = pango_font_description_to_string (desc);
+  g_print ("%s\n", s);
+  g_free (s);
+  pango_font_description_free (desc);
+
+  *found = TRUE;
+
+  return FALSE;
+}
+
+/* Verify that pango_fontmap_load_fontset produces a non-empty result
+ * even if the language isn't covered - our itemization code relies
+ * on this.
+ */
+static void
+test_hbfont_language (void)
+{
+  PangoFontMap *map;
+  PangoContext *context;
+  PangoFontDescription *desc;
+  PangoFontset *fonts;
+  gboolean found;
+
+  map = PANGO_FONT_MAP (pango_fc_font_map2_new ());
+  context = pango_font_map_create_context (map);
+  desc = pango_font_description_from_string ("serif 11");
+
+  /* zz isn't assigned, so there should not be any fonts claiming to support
+   * this language. We are expecting to get a nonempty fontset regardless.
+   */
+  fonts = pango_font_map_load_fontset (map, context, desc, pango_language_from_string ("zz"));
+  g_assert_true (PANGO_IS_FONTSET (fonts));
+
+  found = FALSE;
+  pango_fontset_foreach (fonts, get_font, &found);
+  g_assert_true (found);
+
+  g_object_unref (fonts);
+  pango_font_description_free (desc);
+  g_object_unref (context);
+  g_object_unref (map);
+}
+
+/* Verify that pango_font_describe and pango_font_describe_with_absolute_size
+ * work as expected with PangoHbFont
+ */
+static void
+test_hbfont_describe (void)
+{
+  char *path;
+  PangoHbFace *face;
+  PangoHbFont *font;
+  PangoFontDescription *desc;
+
+  path = g_test_build_filename (G_TEST_DIST, "fonts", "Cantarell-VF.otf", NULL);
+
+  face = pango_hb_face_new_from_file (path, 0, -1, NULL, NULL);
+  g_assert_true (PANGO_IS_HB_FACE (face));
+
+  font = pango_hb_font_new (face, 11 * PANGO_SCALE, NULL, 0, NULL, PANGO_GRAVITY_AUTO, 96., NULL);
+  g_assert_true (PANGO_IS_HB_FONT (font));
+
+  desc = pango_font_describe (PANGO_FONT (font));
+  g_assert_cmpstr (pango_font_description_get_family (desc), ==, "Cantarell");
+  g_assert_cmpint (pango_font_description_get_size (desc), ==, 11 * PANGO_SCALE);
+  g_assert_true (!pango_font_description_get_size_is_absolute (desc));
+  pango_font_description_free (desc);
+
+  desc = pango_font_describe_with_absolute_size (PANGO_FONT (font));
+  g_assert_cmpstr (pango_font_description_get_family (desc), ==, "Cantarell");
+  g_assert_cmpint (pango_font_description_get_size (desc), ==, 11 * PANGO_SCALE * 96./72.);
+  g_assert_true (pango_font_description_get_size_is_absolute (desc));
+  pango_font_description_free (desc);
+
+  g_object_unref (font);
+  g_object_unref (face);
+  g_free (path);
+}
+
+int
+main (int argc, char *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/hbfont/monospace", test_hbfont_monospace);
+  g_test_add_func ("/hbface/roundtrip", test_hbface_roundtrip);
+  g_test_add_func ("/hbfont/roundtrip", test_hbfont_roundtrip);
+  g_test_add_func ("/hbfont/language", test_hbfont_language);
+  g_test_add_func ("/hbfont/describe", test_hbfont_describe);
+
+  return g_test_run ();
+}
diff --git a/tests/testmisc.c b/tests/testmisc.c
index 1aa90948..87235886 100644
--- a/tests/testmisc.c
+++ b/tests/testmisc.c
@@ -22,6 +22,7 @@
 #include "config.h"
 #include <glib.h>
 #include <pango/pangocairo.h>
+#include <pango/pangofc-fontmap2.h>
 
 #ifdef HAVE_CAIRO_FREETYPE
 #include <pango/pango-ot.h>
@@ -726,6 +727,56 @@ test_gravity_metrics (void)
   g_object_unref (context);
 }
 
+static void
+test_gravity_metrics2 (void)
+{
+  PangoHbFontMap *map;
+  PangoContext *context;
+  PangoFontDescription *desc;
+  PangoFont *font;
+  PangoGlyph glyph;
+  PangoGravity gravity;
+  PangoRectangle ink[4];
+  PangoRectangle log[4];
+  char *path;
+
+  map = pango_hb_font_map_new ();
+  path = g_test_build_filename (G_TEST_DIST, "fonts", "Cantarell-VF.otf", NULL);
+  pango_hb_font_map_add_file (map, path);
+  g_free (path);
+
+  context = pango_font_map_create_context (PANGO_FONT_MAP (map));
+
+  desc = pango_font_description_from_string ("Cantarell 64");
+
+  glyph = 1; /* A */
+
+  for (gravity = PANGO_GRAVITY_SOUTH; gravity <= PANGO_GRAVITY_WEST; gravity++)
+    {
+      pango_font_description_set_gravity (desc, gravity);
+      font = pango_font_map_load_font (PANGO_FONT_MAP (map), context, desc);
+      pango_font_get_glyph_extents (font, glyph, &ink[gravity], &log[gravity]);
+      g_object_unref (font);
+    }
+
+  g_assert_cmpint (ink[PANGO_GRAVITY_EAST].width, ==, ink[PANGO_GRAVITY_SOUTH].height);
+  g_assert_cmpint (ink[PANGO_GRAVITY_EAST].height, ==, ink[PANGO_GRAVITY_SOUTH].width);
+  g_assert_cmpint (ink[PANGO_GRAVITY_NORTH].width, ==, ink[PANGO_GRAVITY_SOUTH].width);
+  g_assert_cmpint (ink[PANGO_GRAVITY_NORTH].height, ==, ink[PANGO_GRAVITY_SOUTH].height);
+  g_assert_cmpint (ink[PANGO_GRAVITY_WEST].width, ==, ink[PANGO_GRAVITY_SOUTH].height);
+  g_assert_cmpint (ink[PANGO_GRAVITY_WEST].height, ==, ink[PANGO_GRAVITY_SOUTH].width);
+
+  /* Seems that harfbuzz has some off-by-one differences in advance width
+   * when fonts differ by a scale of -1.
+   */
+  g_assert_cmpint (log[PANGO_GRAVITY_SOUTH].width + log[PANGO_GRAVITY_NORTH].width, <=, 1);
+  g_assert_cmpint (log[PANGO_GRAVITY_EAST].width, ==, log[PANGO_GRAVITY_WEST].width);
+
+  pango_font_description_free (desc);
+  g_object_unref (context);
+  g_object_unref (map);
+}
+
 static void
 test_transform_rectangle (void)
 {
@@ -851,6 +902,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/layout/empty-line-height", test_empty_line_height);
   g_test_add_func ("/layout/gravity-metrics", test_gravity_metrics);
   g_test_add_func ("/layout/wrap-char", test_wrap_char);
+  g_test_add_func ("/layout/gravity-metrics2", test_gravity_metrics2);
   g_test_add_func ("/matrix/transform-rectangle", test_transform_rectangle);
   g_test_add_func ("/itemize/small-caps-crash", test_small_caps_crash);
 


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