[pango/font-serialize-test: 8/9] font: Avoid cosmetic ugliness




commit 88ab72ed2f92cd1bf70ca7b720723329fdca7945
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Nov 27 09:14:12 2021 -0500

    font: Avoid cosmetic ugliness
    
    When setting variations to "", font descriptions
    would add an ugly useless " @" at the end of their
    serialization. Avoid that.
    
    Test included.

 pango/fonts.c     |  5 +++--
 tests/test-font.c | 31 +++++++++++++++++++++++++++++--
 2 files changed, 32 insertions(+), 4 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/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;
@@ -118,6 +118,32 @@ test_variation (void)
   pango_font_description_free (desc2);
 }
 
+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)
 {
@@ -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);


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