2 * fontconfig/test/test-bz89617.c
4 * Copyright © 2000 Keith Packard
5 * Copyright © 2018 Akira TAGOH
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of the author(s) not be used in
12 * advertising or publicity pertaining to distribution of the software without
13 * specific, written prior permission. The authors make no
14 * representations about the suitability of this software for any purpose. It
15 * is provided "as is" without express or implied warranty.
17 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23 * PERFORMANCE OF THIS SOFTWARE.
30 #ifndef HAVE_STRUCT_DIRENT_D_TYPE
31 #include <sys/types.h>
36 #undef FcConfigBuildFonts
38 #undef FcConfigGetCurrent
39 #undef FcConfigParseAndLoadFromMemory
40 #undef FcConfigUptoDate
42 #undef FcInitReinitialize
43 #undef FcPatternCreate
44 #undef FcPatternDestroy
45 #include <fontconfig/fontconfig.h>
48 #define fc_mkdtemp mkdtemp
51 fc_mkdtemp (char *template)
53 if (!mktemp (template) || mkdir (template, 0700))
61 mkdir_p (const char *dir)
66 if (strlen (dir) == 0)
68 parent = (char *) FcStrDirname ((const FcChar8 *) dir);
71 if (access (parent, F_OK) == 0)
72 ret = mkdir (dir, 0755) == 0 && chmod (dir, 0755) == 0;
73 else if (access (parent, F_OK) == -1)
74 ret = mkdir_p (parent) && (mkdir (dir, 0755) == 0) && chmod (dir, 0755) == 0;
83 unlink_dirs (const char *dir)
85 DIR *d = opendir (dir);
87 size_t len = strlen (dir);
90 #ifndef HAVE_STRUCT_DIRENT_D_TYPE
96 while ((e = readdir (d)) != NULL)
100 if (strcmp (e->d_name, ".") == 0 ||
101 strcmp (e->d_name, "..") == 0)
103 l = strlen (e->d_name) + 1;
106 n = malloc (l + len + 1);
113 n[len] = FC_DIR_SEPARATOR;
114 strcpy (&n[len + 1], e->d_name);
115 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
116 if (e->d_type == DT_DIR)
118 if (stat (n, &statb) == -1)
120 fprintf (stderr, "E: %s\n", n);
124 if (S_ISDIR (statb.st_mode))
127 if (!unlink_dirs (n))
129 fprintf (stderr, "E: %s\n", n);
136 if (unlink (n) == -1)
138 fprintf (stderr, "E: %s\n", n);
148 if (rmdir (dir) == -1)
150 fprintf (stderr, "E: %s\n", dir);
158 FcLangNormalize (const FcChar8 *lang)
172 FcChar8 *fontdir = NULL, *cachedir = NULL, *fontname;
173 char *basedir, template[512] = "/tmp/bz106632-XXXXXX";
176 const FcChar8 *tconf = "<fontconfig>\n"
178 " <cachedir>%s</cachedir>\n"
185 fprintf (stderr, "D: Creating tmp dir\n");
186 basedir = fc_mkdtemp (template);
189 fprintf (stderr, "%s: %s\n", template, strerror (errno));
192 fontdir = FcStrBuildFilename (basedir, "fonts", NULL);
193 cachedir = FcStrBuildFilename (basedir, "cache", NULL);
194 fprintf (stderr, "D: Creating %s\n", fontdir);
196 fprintf (stderr, "D: Creating %s\n", cachedir);
199 fprintf (stderr, "D: Copying %s to %s\n", FONTFILE, fontdir);
200 snprintf (cmd, 512, "cp -a %s %s", FONTFILE, fontdir);
203 fprintf (stderr, "D: Loading a config\n");
204 snprintf (conf, 1024, tconf, fontdir, cachedir);
205 config = FcConfigCreate ();
206 if (!FcConfigParseAndLoadFromMemory (config, conf, FcTrue))
208 printf ("E: Unable to load config\n");
212 if (!FcConfigBuildFonts (config))
214 printf ("E: unable to build fonts\n");
218 fprintf (stderr, "D: Obtaining fonts information\n");
219 pat = FcPatternCreate ();
220 fs = FcFontList (config, pat, NULL);
221 FcPatternDestroy (pat);
222 if (!fs || fs->nfont != 1)
224 printf ("E: Unexpected the number of fonts: %d\n", !fs ? -1 : fs->nfont);
228 fprintf (stderr, "D: Removing %s\n", fontdir);
229 snprintf (cmd, 512, "rm -f %s%s*", fontdir, FC_DIR_SEPARATOR_S);
231 fprintf (stderr, "D: Reinitializing\n");
232 if (!FcConfigUptoDate (config) || !FcInitReinitialize ())
234 fprintf (stderr, "E: Unable to reinitialize\n");
238 if (FcConfigGetCurrent () == config)
240 fprintf (stderr, "E: config wasn't reloaded\n");
244 config = FcConfigCreate ();
245 if (!FcConfigParseAndLoadFromMemory (config, conf, FcTrue))
247 printf ("E: Unable to load config again\n");
251 if (!FcConfigBuildFonts (config))
253 printf ("E: unable to build fonts again\n");
257 fprintf (stderr, "D: Obtaining fonts information again\n");
258 pat = FcPatternCreate ();
259 fs = FcFontList (config, pat, NULL);
260 FcPatternDestroy (pat);
261 if (!fs || fs->nfont != 0)
263 printf ("E: Unexpected the number of fonts: %d\n", !fs ? -1 : fs->nfont);
267 fprintf (stderr, "D: Copying %s to %s\n", FONTFILE, fontdir);
268 snprintf (cmd, 512, "cp -a %s %s", FONTFILE, fontdir);
270 fprintf (stderr, "D: Reinitializing\n");
271 if (!FcConfigUptoDate (config) || !FcInitReinitialize ())
273 fprintf (stderr, "E: Unable to reinitialize\n");
277 if (FcConfigGetCurrent () == config)
279 fprintf (stderr, "E: config wasn't reloaded\n");
283 config = FcConfigCreate ();
284 if (!FcConfigParseAndLoadFromMemory (config, conf, FcTrue))
286 printf ("E: Unable to load config again\n");
290 if (!FcConfigBuildFonts (config))
292 printf ("E: unable to build fonts again\n");
296 fprintf (stderr, "D: Obtaining fonts information\n");
297 pat = FcPatternCreate ();
298 fs = FcFontList (config, pat, NULL);
299 FcPatternDestroy (pat);
300 if (!fs || fs->nfont != 1)
302 printf ("E: Unexpected the number of fonts: %d\n", !fs ? -1 : fs->nfont);
308 fprintf (stderr, "Cleaning up\n");
309 unlink_dirs (basedir);
313 FcStrFree (cachedir);