summaryrefslogtreecommitdiff
path: root/tests/test-layout.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test-layout.c')
-rw-r--r--tests/test-layout.c392
1 files changed, 92 insertions, 300 deletions
diff --git a/tests/test-layout.c b/tests/test-layout.c
index 7415efe7..62aba460 100644
--- a/tests/test-layout.c
+++ b/tests/test-layout.c
@@ -29,278 +29,29 @@
#include "config.h"
#include <pango/pangocairo.h>
+#include <pango/pangocairo-fc.h>
+#include <pango/pangofc-fontmap.h>
#include "test-common.h"
-static PangoContext *context;
-
-static gboolean opt_show_font;
-
-static const gchar *
-enum_value_nick (GType type, gint value)
-{
- GEnumClass *eclass;
- GEnumValue *ev;
-
- eclass = g_type_class_ref (type);
- ev = g_enum_get_value (eclass, value);
- g_type_class_unref (eclass);
-
- if (ev)
- return ev->value_nick;
- else
- return "?";
-}
-
-static const gchar *
-direction_name (PangoDirection dir)
-{
- return enum_value_nick (PANGO_TYPE_DIRECTION, dir);
-}
-
-static const gchar *
-gravity_name (PangoGravity gravity)
-{
- return enum_value_nick (PANGO_TYPE_GRAVITY, gravity);
-}
-
-static const gchar *
-script_name (PangoScript script)
-{
- return enum_value_nick (PANGO_TYPE_SCRIPT, script);
-}
-
-static gchar *
-font_name (PangoFont *font)
-{
- PangoFontDescription *desc;
- gchar *name;
-
- desc = pango_font_describe (font);
- name = pango_font_description_to_string (desc);
- pango_font_description_free (desc);
-
- return name;
-}
-
-static void
-dump_lines (PangoLayout *layout, GString *string)
-{
- PangoLayoutIter *iter;
- const gchar *text;
- gint index, index2;
- gboolean has_more;
- gchar *char_str;
- gint i;
- PangoLayoutLine *line;
-
- text = pango_layout_get_text (layout);
- iter = pango_layout_get_iter (layout);
-
- has_more = TRUE;
- index = pango_layout_iter_get_index (iter);
- index2 = 0;
- i = 0;
- while (has_more)
- {
- line = pango_layout_iter_get_line (iter);
- has_more = pango_layout_iter_next_line (iter);
- i++;
-
- if (has_more)
- {
- index2 = pango_layout_iter_get_index (iter);
- char_str = g_strndup (text + index, index2 - index);
- }
- else
- {
- char_str = g_strdup (text + index);
- }
-
- g_string_append_printf (string, "i=%d, index=%d, paragraph-start=%d, dir=%s '%s'\n",
- i, index, line->is_paragraph_start, direction_name (line->resolved_dir),
- char_str);
- g_free (char_str);
-
- index = index2;
- }
- pango_layout_iter_free (iter);
-}
-
-#define ANALYSIS_FLAGS (PANGO_ANALYSIS_FLAG_CENTERED_BASELINE | \
- PANGO_ANALYSIS_FLAG_IS_ELLIPSIS | \
- PANGO_ANALYSIS_FLAG_NEED_HYPHEN)
-
-static void
-dump_runs (PangoLayout *layout, GString *string)
-{
- PangoLayoutIter *iter;
- PangoLayoutRun *run;
- PangoItem *item;
- const gchar *text;
- gint index;
- gboolean has_more;
- gchar *char_str;
- gint i;
- gchar *font = 0;
-
- text = pango_layout_get_text (layout);
- iter = pango_layout_get_iter (layout);
-
- has_more = TRUE;
- i = 0;
- while (has_more)
- {
- run = pango_layout_iter_get_run (iter);
- index = pango_layout_iter_get_index (iter);
- has_more = pango_layout_iter_next_run (iter);
- i++;
-
- if (run)
- {
- item = ((PangoGlyphItem*)run)->item;
- char_str = g_strndup (text + item->offset, item->length);
- font = font_name (item->analysis.font);
- g_string_append_printf (string, "i=%d, index=%d, chars=%d, level=%d, gravity=%s, flags=%d, font=%s, script=%s, language=%s, '%s'\n",
- i, index, item->num_chars, item->analysis.level,
- gravity_name (item->analysis.gravity),
- item->analysis.flags & ANALYSIS_FLAGS,
- opt_show_font ? font : "OMITTED", /* for some reason, this fails on build.gnome.org, so leave it out */
- script_name (item->analysis.script),
- pango_language_to_string (item->analysis.language),
- char_str);
- print_attributes (item->analysis.extra_attrs, string);
- g_free (font);
- g_free (char_str);
- }
- else
- {
- g_string_append_printf (string, "i=%d, index=%d, no run, line end\n",
- i, index);
- }
- }
- pango_layout_iter_free (iter);
-}
-
-static void
-dump_directions (PangoLayout *layout, GString *string)
-{
- const char *text, *p;
-
- text = pango_layout_get_text (layout);
- for (p = text; *p; p = g_utf8_next_char (p))
- {
- g_string_append_printf (string, "%d ", pango_layout_get_direction (layout, p - text));
- }
- g_string_append (string, "\n");
-}
-
-static void
-dump_cursor_positions (PangoLayout *layout, GString *string)
-{
- const char *text;
- int index, trailing;
-
- text = pango_layout_get_text (layout);
-
- index = 0;
- trailing = 0;
-
- while (index < G_MAXINT)
- {
- g_string_append_printf (string, "%d(%d) ", index, trailing);
-
- while (trailing--)
- index = g_utf8_next_char (text + index) - text;
-
- pango_layout_move_cursor_visually (layout, TRUE, index, 0, 1, &index, &trailing);
- }
-
- g_string_append (string, "\n");
-}
-
-static void
-test_file (const char *filename, GString *string)
-{
- char *contents;
- gsize length;
- GBytes *bytes;
- GError *error = NULL;
- PangoLayout *layout;
-
- if (context == NULL)
- context = pango_font_map_create_context (pango_cairo_font_map_get_default ());
-
- g_file_get_contents (filename, &contents, &length, &error);
- g_assert_no_error (error);
-
- bytes = g_bytes_new_take (contents, length);
-
- layout = pango_layout_deserialize (context, bytes, &error);
- g_assert_no_error (error);
-
- g_bytes_unref (bytes);
-
- /* generate the dumps */
- g_string_append (string, pango_layout_get_text (layout));
-
- g_string_append (string, "\n--- parameters\n\n");
-
- g_string_append_printf (string, "wrapped: %d\n", pango_layout_is_wrapped (layout));
- g_string_append_printf (string, "ellipsized: %d\n", pango_layout_is_ellipsized (layout));
- g_string_append_printf (string, "lines: %d\n", pango_layout_get_line_count (layout));
- if (pango_layout_get_width (layout) > 0)
- g_string_append_printf (string, "width: %d\n", pango_layout_get_width (layout));
- if (pango_layout_get_height (layout) > 0)
- g_string_append_printf (string, "height: %d\n", pango_layout_get_height (layout));
- if (pango_layout_get_indent (layout) != 0)
- g_string_append_printf (string, "indent: %d\n", pango_layout_get_indent (layout));
-
- g_string_append (string, "\n--- attributes\n\n");
- print_attr_list (pango_layout_get_attributes (layout), string);
-
- g_string_append (string, "\n--- directions\n\n");
- dump_directions (layout, string);
-
- g_string_append (string, "\n--- cursor positions\n\n");
- dump_cursor_positions (layout, string);
-
- g_string_append (string, "\n--- lines\n\n");
- dump_lines (layout, string);
-
- g_string_append (string, "\n--- runs\n\n");
- dump_runs (layout, string);
-
- g_object_unref (layout);
-}
-
-static gchar *
-get_expected_filename (const char *filename)
-{
- char *f, *p, *expected;
-
- f = g_strdup (filename);
- p = strstr (f, ".layout");
- if (p)
- *p = 0;
- expected = g_strconcat (f, ".expected", NULL);
-
- g_free (f);
-
- return expected;
-}
-
static void
test_layout (gconstpointer d)
{
const char *filename = d;
- char *expected_file;
GError *error = NULL;
- GString *dump;
char *diff;
- PangoFontFamily **families;
- int n_families;
- gboolean found_cantarell;
+ GBytes *bytes;
+ char *contents;
+ gsize length;
+ GBytes *orig;
+ PangoContext *context;
+ PangoLayout *layout;
+
+ if (!PANGO_IS_FC_FONT_MAP (pango_cairo_font_map_get_default ()))
+ {
+ g_test_skip ("Not an fc fontmap. Skipping...");
+ return;
+ }
char *old_locale = g_strdup (setlocale (LC_ALL, NULL));
setlocale (LC_ALL, "en_US.UTF-8");
@@ -313,39 +64,25 @@ test_layout (gconstpointer d)
return;
}
- if (context == NULL)
- context = pango_font_map_create_context (pango_cairo_font_map_get_default ());
-
- found_cantarell = FALSE;
- pango_context_list_families (context, &families, &n_families);
- for (int i = 0; i < n_families; i++)
- {
- if (strcmp (pango_font_family_get_name (families[i]), "Cantarell") == 0)
- {
- found_cantarell = TRUE;
- break;
- }
- }
- g_free (families);
-
- if (!found_cantarell)
- {
- char *msg = g_strdup_printf ("Cantarell font not available, skipping itemization %s", filename);
- g_test_skip (msg);
- g_free (msg);
- g_free (old_locale);
- return;
- }
+ g_file_get_contents (filename, &contents, &length, &error);
+ g_assert_no_error (error);
+ orig = g_bytes_new_take (contents, length);
- expected_file = get_expected_filename (filename);
+ context = pango_font_map_create_context (pango_cairo_font_map_get_default ());
+ layout = pango_layout_deserialize (context, orig, PANGO_LAYOUT_DESERIALIZE_CONTEXT, &error);
+ g_assert_no_error (error);
- dump = g_string_sized_new (0);
+ bytes = pango_layout_serialize (layout, PANGO_LAYOUT_SERIALIZE_CONTEXT | PANGO_LAYOUT_SERIALIZE_OUTPUT);
- test_file (filename, dump);
+ g_object_unref (layout);
+ g_object_unref (context);
- diff = diff_with_file (expected_file, dump->str, dump->len, &error);
+ diff = diff_bytes (orig, bytes, &error);
g_assert_no_error (error);
+ g_bytes_unref (bytes);
+ g_bytes_unref (orig);
+
setlocale (LC_ALL, old_locale);
g_free (old_locale);
@@ -365,8 +102,37 @@ test_layout (gconstpointer d)
}
g_free (diff);
- g_string_free (dump, TRUE);
- g_free (expected_file);
+}
+
+static void
+install_fonts (const char *dir)
+{
+ FcConfig *config;
+ PangoFontMap *map;
+ char *conf;
+
+ map = g_object_new (PANGO_TYPE_CAIRO_FC_FONT_MAP, NULL);
+
+ config = FcConfigCreate ();
+
+ conf = g_strdup_printf ("<?xml version=\"1.0\"?>\n"
+ "<!DOCTYPE fontconfig SYSTEM \"urn:fontconfig:fonts.dtd\">\n"
+ "<fontconfig>\n"
+ " <cachedir>%s/cache</cachedir>\n"
+ "</fontconfig>", dir);
+
+ if (!FcConfigParseAndLoadFromMemory (config, (const FcChar8 *) conf, TRUE))
+ g_error ("Failed to parse fontconfig configuration");
+
+ g_free (conf);
+
+ 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);
}
int
@@ -374,11 +140,12 @@ main (int argc, char *argv[])
{
GDir *dir;
GError *error = NULL;
+ char *opt_fonts = NULL;
const gchar *name;
char *path;
GOptionContext *option_context;
GOptionEntry entries[] = {
- { "show-fonts", '0', 0, G_OPTION_ARG_NONE, &opt_show_font, "Print font names in dumps", NULL },
+ { "fonts", 0, 0, G_OPTION_ARG_FILENAME, &opt_fonts, "Fonts to use", "DIR" },
{ NULL, 0 },
};
@@ -394,24 +161,49 @@ main (int argc, char *argv[])
}
g_option_context_free (option_context);
- if (g_getenv ("PANGO_TEST_SHOW_FONT"))
- opt_show_font = TRUE;
+ if (opt_fonts)
+ install_fonts (opt_fonts);
/* allow to easily generate expected output for new test cases */
if (argc > 1 && argv[1][0] != '-')
{
- GString *string;
+ char *contents;
+ gsize length;
+ GError *error = NULL;
+ GBytes *orig;
+ GBytes *bytes;
+ PangoContext *context;
+ PangoLayout *layout;
- string = g_string_sized_new (0);
- test_file (argv[1], string);
- g_print ("%s", string->str);
- g_string_free (string, TRUE);
+ g_file_get_contents (argv[1], &contents, &length, &error);
+ g_assert_no_error (error);
+ orig = g_bytes_new_take (contents, length);
+ context = pango_font_map_create_context (pango_cairo_font_map_get_default ());
+ layout = pango_layout_deserialize (context, orig, PANGO_LAYOUT_DESERIALIZE_CONTEXT, &error);
+ g_assert_no_error (error);
+
+ bytes = pango_layout_serialize (layout, PANGO_LAYOUT_SERIALIZE_CONTEXT | PANGO_LAYOUT_SERIALIZE_OUTPUT);
+
+ g_object_unref (layout);
+ g_object_unref (context);
+
+ g_print ("%s", (const char *)g_bytes_get_data (bytes, NULL));
+
+ g_bytes_unref (bytes);
+ g_bytes_unref (orig);
return 0;
}
g_test_init (&argc, &argv, NULL);
+ if (!opt_fonts)
+ {
+ path = g_test_build_filename (G_TEST_DIST, "fonts", NULL);
+ install_fonts (path);
+ g_free (path);
+ }
+
path = g_test_build_filename (G_TEST_DIST, "layouts", NULL);
dir = g_dir_open (path, 0, &error);
g_free (path);