From e27ac590d44e87adac235c1b63429dc5f832f782 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 26 Sep 2022 20:41:19 -0400 Subject: Add a test for no-fonts Test that the layout is made up entirely of unknown glyphs if we have no font. The main point of this test is to ensure that we don't crash if analysis->font is NULL. --- tests/meson.build | 1 + tests/nofonts/fonts.conf | 71 ++++++++++++++++++++++++++++++ tests/test-no-fonts.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 181 insertions(+) create mode 100644 tests/nofonts/fonts.conf create mode 100644 tests/test-no-fonts.c diff --git a/tests/meson.build b/tests/meson.build index e75b8e2f..8b4bfa38 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -63,6 +63,7 @@ if cairo_dep.found() tests += [ [ 'test-layout', [ 'test-layout.c', 'test-common.c' ], [ libpangocairo_dep, libpangoft2_dep ] ], [ 'test-fonts', [ 'test-fonts.c', 'test-common.c' ], [ libpangocairo_dep, libpangoft2_dep ] ], + [ 'test-no-fonts', [ 'test-no-fonts.c' ], [ libpangocairo_dep, libpangoft2_dep ] ], ] endif endif diff --git a/tests/nofonts/fonts.conf b/tests/nofonts/fonts.conf new file mode 100644 index 00000000..15685421 --- /dev/null +++ b/tests/nofonts/fonts.conf @@ -0,0 +1,71 @@ + + + + /tmp/cache + + + + false + + + + pixelsize + pixelsize + + + + + + + false + + + 1.0 + + + + matrix + + pixelsizefixupfactor 0 + 0 pixelsizefixupfactor + + + + + + size + pixelsizefixupfactor + + + + + + Noto Color Emoji + emoji + + + + + und-zsye + + + emoji + + + emoji + + + + + emoji + + Noto Color Emoji + + + + + monospace + DejaVu Sans Mono + + + diff --git a/tests/test-no-fonts.c b/tests/test-no-fonts.c new file mode 100644 index 00000000..c057874d --- /dev/null +++ b/tests/test-no-fonts.c @@ -0,0 +1,109 @@ +/* Pango + * test-no-fonts.c: Check that lack of fonts is survivable + * + * Copyright (C) 2022 Red Hat, Inc + * + * 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 +#include +#include + +#ifndef G_OS_WIN32 +#include +#endif + +#include "config.h" +#include +#include +#include +#include "test-common.h" + + +static void +install_fonts (const char *dir) +{ + FcConfig *config; + PangoFontMap *map; + char *path; + gsize len; + char *conf; + + map = g_object_new (PANGO_TYPE_CAIRO_FC_FONT_MAP, NULL); + + config = FcConfigCreate (); + + path = g_build_filename (dir, "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); +} + +static void +test_nofonts (void) +{ + PangoContext *context; + const char *text; + PangoLayout *layout; + int missing; + + context = pango_font_map_create_context (pango_cairo_font_map_get_default ()); + layout = pango_layout_new (context); + + text = "Schlaf, Kindlein schlaf! " + "Der Vater hüt' die Schaf, " + "die Mutter schúttelt's Bäumelein, " + "da fällt herab ein Träumelein. " + "Schlaf, Kindlein schlaf!"; + + pango_layout_set_text (layout, text, -1); + missing = pango_layout_get_unknown_glyphs_count (layout); + g_assert_cmpint (missing, ==, g_utf8_strlen (text, -1)); + + g_object_unref (layout); + g_object_unref (context); +} + +int +main (int argc, char *argv[]) +{ + char *path; + + setlocale (LC_ALL, ""); + + g_test_init (&argc, &argv, NULL); + + path = g_test_build_filename (G_TEST_DIST, "nofonts", NULL); + install_fonts (path); + g_free (path); + + g_test_add_func ("/layout/nofonts", test_nofonts); + + return g_test_run (); +} -- cgit v1.2.1