summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2021-11-27 15:17:15 +0000
committerMatthias Clasen <mclasen@redhat.com>2021-11-27 15:17:15 +0000
commit80b197df0c7d66929e40989ff23021126d6c9881 (patch)
tree5fc0b846de72005be91db902f1438fdab1655665
parentac2bf585e04c49be419f44a97c4ef0de0fd21014 (diff)
parentb2aabec7ca3d46d7833fef694915e4ddb5a179fe (diff)
downloadpango-80b197df0c7d66929e40989ff23021126d6c9881.tar.gz
Merge branch 'font-serialize-test' into 'main'
Avoid cosmetic ugliness See merge request GNOME/pango!522
-rw-r--r--pango/fonts.c5
-rw-r--r--tests/meson.build2
-rw-r--r--tests/test-font.c31
-rw-r--r--tests/testserialize.c78
4 files changed, 111 insertions, 5 deletions
diff --git a/pango/fonts.c b/pango/fonts.c
index d94feaf8..129e84d3 100644
--- a/pango/fonts.c
+++ b/pango/fonts.c
@@ -546,7 +546,7 @@ pango_font_description_set_variations_static (PangoFontDescription *desc,
/**
* pango_font_description_set_variations:
* @desc: a `PangoFontDescription`.
- * @variations: a string representing the variations
+ * @variations: (nullable): a string representing the variations
*
* Sets the variations field of a font description.
*
@@ -1492,7 +1492,8 @@ pango_font_description_to_string (const PangoFontDescription *desc)
g_string_append (result, "px");
}
- if (desc->variations && desc->mask & PANGO_FONT_MASK_VARIATIONS)
+ if ((desc->variations && desc->mask & PANGO_FONT_MASK_VARIATIONS) &&
+ desc->variations[0] != '\0')
{
g_string_append (result, " @");
g_string_append (result, desc->variations);
diff --git a/tests/meson.build b/tests/meson.build
index 1984ed03..af56433e 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -51,12 +51,12 @@ if cairo_dep.found()
[ 'cxx-test', [ 'cxx-test.cpp' ], [ libpangocairo_dep, gobject_dep, harfbuzz_dep ] ],
[ 'test-harfbuzz', [ 'test-harfbuzz.c' ], [ libpangocairo_dep, gobject_dep, harfbuzz_dep ] ],
[ 'test-break', [ 'test-break.c', 'test-common.c', 'validate-log-attrs.c' ], [libpangocairo_dep, glib_dep, harfbuzz_dep ] ],
- [ 'testserialize', [ 'testserialize.c' ], [ libpangocairo_dep ] ],
]
if build_pangoft2
tests += [
[ 'testmisc', [ 'testmisc.c' ], [ libpangocairo_dep, libpangoft2_dep, glib_dep, harfbuzz_dep ] ],
+ [ 'testserialize', [ 'testserialize.c' ], [ libpangocairo_dep, libpangoft2_dep ] ],
]
if host_system != 'darwin'
diff --git a/tests/test-font.c b/tests/test-font.c
index bfe321a0..2eef4396 100644
--- a/tests/test-font.c
+++ b/tests/test-font.c
@@ -82,7 +82,7 @@ test_roundtrip (void)
}
static void
-test_variation (void)
+test_variations (void)
{
PangoFontDescription *desc1;
PangoFontDescription *desc2;
@@ -119,6 +119,32 @@ test_variation (void)
}
static void
+test_empty_variations (void)
+{
+ PangoFontDescription *desc;
+ gchar *str;
+
+ desc = pango_font_description_from_string ("Cantarell 14");
+ g_assert_nonnull (desc);
+ g_assert_cmpint ((pango_font_description_get_set_fields (desc) & PANGO_FONT_MASK_VARIATIONS), ==, 0);
+ g_assert_null (pango_font_description_get_variations (desc));
+
+ str = pango_font_description_to_string (desc);
+ g_assert_cmpstr (str, ==, "Cantarell 14");
+ g_free (str);
+
+ pango_font_description_set_variations (desc, "");
+ g_assert_cmpint ((pango_font_description_get_set_fields (desc) & PANGO_FONT_MASK_VARIATIONS), ==, PANGO_FONT_MASK_VARIATIONS);
+ g_assert_cmpstr (pango_font_description_get_variations (desc), ==, "");
+
+ str = pango_font_description_to_string (desc);
+ g_assert_cmpstr (str, ==, "Cantarell 14");
+ g_free (str);
+
+ pango_font_description_free (desc);
+}
+
+static void
test_metrics (void)
{
PangoFontDescription *desc;
@@ -543,7 +569,8 @@ main (int argc, char *argv[])
g_test_add_func ("/pango/font/metrics", test_metrics);
g_test_add_func ("/pango/fontdescription/parse", test_parse);
g_test_add_func ("/pango/fontdescription/roundtrip", test_roundtrip);
- g_test_add_func ("/pango/fontdescription/variation", test_variation);
+ g_test_add_func ("/pango/fontdescription/variations", test_variations);
+ g_test_add_func ("/pango/fontdescription/empty-variations", test_empty_variations);
g_test_add_func ("/pango/fontdescription/to-filename", test_to_filename);
g_test_add_func ("/pango/fontdescription/set-gravity", test_set_gravity);
g_test_add_func ("/pango/fontdescription/match", test_match);
diff --git a/tests/testserialize.c b/tests/testserialize.c
index bd962a56..4ea7425e 100644
--- a/tests/testserialize.c
+++ b/tests/testserialize.c
@@ -22,6 +22,7 @@
#include <glib.h>
#include <pango/pangocairo.h>
+#include <pango/pangocairo-fc.h>
#include <gio/gio.h>
static void
@@ -125,6 +126,43 @@ test_serialize_tab_array (void)
}
static void
+test_serialize_font (void)
+{
+ PangoContext *context;
+ PangoFontDescription *desc;
+ PangoFont *font;
+ GBytes *bytes;
+ const char *expected =
+ "{\n"
+ " \"description\" : \"Cantarell 20 @wght=600\",\n"
+ " \"checksum\" : \"5bcb6ee14ee9d210b2e91d643de1fe456e9d1aea770983fdb05951545efebbe2\",\n"
+ " \"variations\" : {\n"
+ " \"wght\" : 5583\n"
+ " },\n"
+ " \"matrix\" : [\n"
+ " 1.0,\n"
+ " -0.0,\n"
+ " -0.0,\n"
+ " 1.0,\n"
+ " 0.0,\n"
+ " 0.0\n"
+ " ]\n"
+ "}";
+
+ context = pango_font_map_create_context (pango_cairo_font_map_get_default ());
+ desc = pango_font_description_from_string ("Cantarell Italic 20 @wght=600");
+ font = pango_context_load_font (context, desc);
+
+ bytes = pango_font_serialize (font);
+ g_assert_cmpstr (g_bytes_get_data (bytes, NULL), ==, expected);
+ g_bytes_unref (bytes);
+
+ g_object_unref (font);
+ pango_font_description_free (desc);
+ g_object_unref (context);
+}
+
+static void
test_serialize_layout_minimal (void)
{
const char *test =
@@ -351,13 +389,53 @@ test_serialize_layout_invalid (void)
g_object_unref (context);
}
+
+static void
+install_fonts (void)
+{
+ char *dir;
+ FcConfig *config;
+ PangoFontMap *map;
+ char *path;
+ gsize len;
+ char *conf;
+
+ dir = g_test_build_filename (G_TEST_DIST, "fonts", NULL);
+
+ map = g_object_new (PANGO_TYPE_CAIRO_FC_FONT_MAP, NULL);
+
+ config = FcConfigCreate ();
+
+ path = g_test_build_filename (G_TEST_DIST, "fonts/fonts.conf", NULL);
+ g_file_get_contents (path, &conf, &len, NULL);
+
+ if (!FcConfigParseAndLoadFromMemory (config, (const FcChar8 *) conf, TRUE))
+ g_error ("Failed to parse fontconfig configuration");
+
+ g_free (conf);
+ g_free (path);
+
+ FcConfigAppFontAddDir (config, (const FcChar8 *) dir);
+ pango_fc_font_map_set_config (PANGO_FC_FONT_MAP (map), config);
+ FcConfigDestroy (config);
+
+ pango_cairo_font_map_set_default (PANGO_CAIRO_FONT_MAP (map));
+
+ g_object_unref (map);
+
+ g_free (dir);
+}
+
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
+ install_fonts ();
+
g_test_add_func ("/serialize/attr-list", test_serialize_attr_list);
g_test_add_func ("/serialize/tab-array", test_serialize_tab_array);
+ g_test_add_func ("/serialize/font", test_serialize_font);
g_test_add_func ("/serialize/layout/minimal", test_serialize_layout_minimal);
g_test_add_func ("/serialize/layout/valid", test_serialize_layout_valid);
g_test_add_func ("/serialize/layout/context", test_serialize_layout_context);