summaryrefslogtreecommitdiff
path: root/tests/test-no-fonts.c
blob: c057874d67300bd26f2d1e6f688890194f195361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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 <glib.h>
#include <string.h>
#include <locale.h>

#ifndef G_OS_WIN32
#include <unistd.h>
#endif

#include "config.h"
#include <pango/pangocairo.h>
#include <pango/pangocairo-fc.h>
#include <pango/pangofc-fontmap.h>
#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 ();
}