"Fossies" - the Fresh Open Source Software Archive

Member "pango-1.44.7/tests/test-font.c" (25 Oct 2019, 8044 Bytes) of package /linux/misc/pango-1.44.7.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "test-font.c": 1.44.3_vs_1.44.4.

    1 /* Pango
    2  * test-font.c: Test PangoFontDescription
    3  *
    4  * Copyright (C) 2014 Red Hat, Inc
    5  *
    6  * This library is free software; you can redistribute it and/or
    7  * modify it under the terms of the GNU Library General Public
    8  * License as published by the Free Software Foundation; either
    9  * version 2 of the License, or (at your option) any later version.
   10  *
   11  * This library is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   14  * Library General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU Library General Public
   17  * License along with this library; if not, write to the
   18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   19  * Boston, MA 02111-1307, USA.
   20  */
   21 
   22 #include <glib.h>
   23 #include <string.h>
   24 #include <locale.h>
   25 
   26 #include <pango/pangocairo.h>
   27 
   28 static PangoContext *context;
   29 
   30 static void
   31 test_parse (void)
   32 {
   33   PangoFontDescription *desc;
   34 
   35   desc = pango_font_description_from_string ("Cantarell 14");
   36 
   37   g_assert_cmpstr (pango_font_description_get_family (desc), ==, "Cantarell");
   38   g_assert (!pango_font_description_get_size_is_absolute (desc));
   39   g_assert_cmpint (pango_font_description_get_size (desc), ==, 14 * PANGO_SCALE);
   40   g_assert_cmpint (pango_font_description_get_style (desc), ==, PANGO_STYLE_NORMAL);
   41   g_assert_cmpint (pango_font_description_get_variant (desc), ==, PANGO_VARIANT_NORMAL);
   42   g_assert_cmpint (pango_font_description_get_weight (desc), ==, PANGO_WEIGHT_NORMAL);
   43   g_assert_cmpint (pango_font_description_get_stretch (desc), ==, PANGO_STRETCH_NORMAL);
   44   g_assert_cmpint (pango_font_description_get_gravity (desc), ==, PANGO_GRAVITY_SOUTH);
   45   g_assert_cmpint (pango_font_description_get_set_fields (desc), ==, PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_STYLE | PANGO_FONT_MASK_VARIANT | PANGO_FONT_MASK_WEIGHT | PANGO_FONT_MASK_STRETCH | PANGO_FONT_MASK_SIZE);
   46 
   47   pango_font_description_free (desc); 
   48 
   49   desc = pango_font_description_from_string ("Sans Bold Italic Condensed 22.5px");
   50 
   51   g_assert_cmpstr (pango_font_description_get_family (desc), ==, "Sans");
   52   g_assert (pango_font_description_get_size_is_absolute (desc)); 
   53   g_assert_cmpint (pango_font_description_get_size (desc), ==, 225 * PANGO_SCALE / 10);
   54   g_assert_cmpint (pango_font_description_get_style (desc), ==, PANGO_STYLE_ITALIC);
   55   g_assert_cmpint (pango_font_description_get_variant (desc), ==, PANGO_VARIANT_NORMAL); 
   56   g_assert_cmpint (pango_font_description_get_weight (desc), ==, PANGO_WEIGHT_BOLD);
   57   g_assert_cmpint (pango_font_description_get_stretch (desc), ==, PANGO_STRETCH_CONDENSED); 
   58   g_assert_cmpint (pango_font_description_get_gravity (desc), ==, PANGO_GRAVITY_SOUTH);  g_assert_cmpint (pango_font_description_get_set_fields (desc), ==, PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_STYLE | PANGO_FONT_MASK_VARIANT | PANGO_FONT_MASK_WEIGHT | PANGO_FONT_MASK_STRETCH | PANGO_FONT_MASK_SIZE);
   59 
   60   pango_font_description_free (desc); 
   61 }
   62 
   63 static void
   64 test_roundtrip (void)
   65 {
   66   PangoFontDescription *desc;
   67  gchar *str;
   68 
   69   desc = pango_font_description_from_string ("Cantarell 14");
   70   str = pango_font_description_to_string (desc);
   71   g_assert_cmpstr (str, ==, "Cantarell 14");
   72   pango_font_description_free (desc); 
   73   g_free (str);
   74 
   75   desc = pango_font_description_from_string ("Sans Bold Italic Condensed 22.5px");
   76   str = pango_font_description_to_string (desc);
   77   g_assert_cmpstr (str, ==, "Sans Bold Italic Condensed 22.5px");
   78   pango_font_description_free (desc); 
   79   g_free (str);
   80 }
   81 
   82 static void
   83 test_variation (void)
   84 {
   85   PangoFontDescription *desc1;
   86   PangoFontDescription *desc2;
   87   gchar *str;
   88 
   89   desc1 = pango_font_description_from_string ("Cantarell 14");
   90   g_assert (desc1 != NULL);
   91   g_assert ((pango_font_description_get_set_fields (desc1) & PANGO_FONT_MASK_VARIATIONS) == 0);
   92   g_assert (pango_font_description_get_variations (desc1) == NULL);
   93 
   94   str = pango_font_description_to_string (desc1);
   95   g_assert_cmpstr (str, ==, "Cantarell 14");
   96   g_free (str);
   97 
   98   desc2 = pango_font_description_from_string ("Cantarell 14 @wght=100,wdth=235");
   99   g_assert (desc2 != NULL);
  100   g_assert ((pango_font_description_get_set_fields (desc2) & PANGO_FONT_MASK_VARIATIONS) != 0);
  101   g_assert_cmpstr (pango_font_description_get_variations (desc2), ==, "wght=100,wdth=235");
  102 
  103   str = pango_font_description_to_string (desc2);
  104   g_assert_cmpstr (str, ==, "Cantarell 14 @wght=100,wdth=235");
  105   g_free (str);
  106 
  107   g_assert (!pango_font_description_equal (desc1, desc2));
  108 
  109   pango_font_description_set_variations (desc1, "wght=100,wdth=235");
  110   g_assert ((pango_font_description_get_set_fields (desc1) & PANGO_FONT_MASK_VARIATIONS) != 0);
  111   g_assert_cmpstr (pango_font_description_get_variations (desc1), ==, "wght=100,wdth=235");
  112 
  113   g_assert (pango_font_description_equal (desc1, desc2));
  114 
  115   pango_font_description_free (desc1);
  116   pango_font_description_free (desc2);
  117 }
  118 
  119 static void
  120 test_metrics (void)
  121 {
  122   PangoFontDescription *desc;
  123   PangoFontMetrics *metrics;
  124   char *str;
  125 
  126 
  127   if (strcmp (G_OBJECT_TYPE_NAME (pango_context_get_font_map (context)), "PangoCairoWin32FontMap") == 0)
  128     desc = pango_font_description_from_string ("Verdana 11");
  129   else
  130     desc = pango_font_description_from_string ("Cantarell 11");
  131 
  132   str = pango_font_description_to_string (desc);
  133 
  134   metrics = pango_context_get_metrics (context, desc, pango_language_get_default ());
  135 
  136   g_test_message ("%s metrics\n"
  137                   "\tascent %d\n"
  138                   "\tdescent %d\n"
  139                   "\theight %d\n"
  140                   "\tchar width %d\n"
  141                   "\tdigit width %d\n"
  142                   "\tunderline position %d\n"
  143                   "\tunderline thickness %d\n"
  144                   "\tstrikethrough position %d\n"
  145                   "\tstrikethrough thickness %d\n",
  146                   str,
  147                   pango_font_metrics_get_ascent (metrics),
  148                   pango_font_metrics_get_descent (metrics),
  149                   pango_font_metrics_get_height (metrics),
  150                   pango_font_metrics_get_approximate_char_width (metrics),
  151                   pango_font_metrics_get_approximate_digit_width (metrics),
  152                   pango_font_metrics_get_underline_position (metrics),
  153                   pango_font_metrics_get_underline_thickness (metrics),
  154                   pango_font_metrics_get_strikethrough_position (metrics),
  155                   pango_font_metrics_get_strikethrough_thickness (metrics));
  156 
  157   pango_font_metrics_unref (metrics);
  158   g_free (str);
  159   pango_font_description_free (desc);
  160 }
  161 
  162 static void
  163 test_extents (void)
  164 {
  165   char *str = "Composer";
  166   GList *items;
  167   PangoItem *item;
  168   PangoGlyphString *glyphs;
  169   PangoRectangle ink, log;
  170   PangoContext *context;
  171 
  172   context = pango_font_map_create_context (pango_cairo_font_map_get_default ());
  173   pango_context_set_font_description (context, pango_font_description_from_string ("Cantarell 11"));
  174 
  175   items = pango_itemize (context, str, 0, strlen (str), NULL, NULL);
  176   glyphs = pango_glyph_string_new ();
  177   item = items->data;
  178   pango_shape (str, strlen (str), &item->analysis, glyphs);
  179   pango_glyph_string_extents (glyphs, item->analysis.font, &ink, &log);
  180 
  181   g_assert_cmpint (ink.width, >=, 0);
  182   g_assert_cmpint (ink.height, >=, 0);
  183   g_assert_cmpint (log.width, >=, 0);
  184   g_assert_cmpint (log.height, >=, 0);
  185 
  186   pango_glyph_string_free (glyphs);
  187   g_list_free_full (items, (GDestroyNotify)pango_item_free);
  188   g_object_unref (context);
  189 }
  190 
  191 int
  192 main (int argc, char *argv[])
  193 {
  194   g_setenv ("LC_ALL", "C", TRUE);
  195   setlocale (LC_ALL, "");
  196 
  197   g_test_init (&argc, &argv, NULL);
  198 
  199   context = pango_font_map_create_context (pango_cairo_font_map_get_default ());
  200 
  201   g_test_add_func ("/pango/font/metrics", test_metrics);
  202   g_test_add_func ("/pango/fontdescription/parse", test_parse);
  203   g_test_add_func ("/pango/fontdescription/roundtrip", test_roundtrip);
  204   g_test_add_func ("/pango/fontdescription/variation", test_variation);
  205   g_test_add_func ("/pango/font/extents", test_extents);
  206 
  207   return g_test_run ();
  208 }