2 * fontconfig/test/test-conf.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.
27 #include <fontconfig/fontconfig.h>
31 FcStrSet *configDirs; /* directories to scan for fonts */
32 FcStrSet *configMapDirs;
35 FcStrSet *configFiles; /* config files loaded */
36 void *subst[FcMatchKindEnd];
37 int maxObjects; /* maximum number of tests in all substs */
38 FcStrSet *acceptGlobs;
39 FcStrSet *rejectGlobs;
40 FcFontSet *acceptPatterns;
41 FcFontSet *rejectPatterns;
42 FcFontSet *fonts[FcSetApplication + 1];
46 build_pattern (json_object *obj)
48 json_object_iter iter;
49 FcPattern *pat = FcPatternCreate ();
51 json_object_object_foreachC (obj, iter)
54 FcBool destroy_v = FcFalse;
57 if (json_object_get_type (iter.val) == json_type_boolean)
60 v.u.b = json_object_get_boolean (iter.val);
62 else if (json_object_get_type (iter.val) == json_type_double)
64 v.type = FcTypeDouble;
65 v.u.d = json_object_get_double (iter.val);
67 else if (json_object_get_type (iter.val) == json_type_int)
69 v.type = FcTypeInteger;
70 v.u.i = json_object_get_int (iter.val);
72 else if (json_object_get_type (iter.val) == json_type_string)
74 const FcObjectType *o = FcNameGetObjectType (iter.key);
75 if (o && (o->type == FcTypeRange || o->type == FcTypeDouble || o->type == FcTypeInteger))
77 const FcConstant *c = FcNameGetConstant ((const FcChar8 *) json_object_get_string (iter.val));
79 fprintf (stderr, "E: value is not a known constant\n");
80 fprintf (stderr, " key: %s\n", iter.key);
81 fprintf (stderr, " val: %s\n", json_object_get_string (iter.val));
84 if (strcmp (c->object, iter.key) != 0)
86 fprintf (stderr, "E: value is a constant of different object\n");
87 fprintf (stderr, " key: %s\n", iter.key);
88 fprintf (stderr, " val: %s\n", json_object_get_string (iter.val));
89 fprintf (stderr, " key implied by value: %s\n", c->object);
92 v.type = FcTypeInteger;
95 else if (strcmp (json_object_get_string (iter.val), "DontCare") == 0)
102 v.type = FcTypeString;
103 v.u.s = (const FcChar8 *) json_object_get_string (iter.val);
106 else if (json_object_get_type (iter.val) == json_type_null)
110 else if (json_object_get_type (iter.val) == json_type_array)
116 n = json_object_array_length (iter.val);
118 fprintf (stderr, "E: value is an empty array\n");
122 o = json_object_array_get_idx (iter.val, 0);
123 type = json_object_get_type (o);
124 if (type == json_type_string) {
125 const FcObjectType *fc_o = FcNameGetObjectType (iter.key);
126 if (fc_o && fc_o->type == FcTypeCharSet) {
127 FcCharSet* cs = FcCharSetCreate ();
129 fprintf (stderr, "E: failed to create charset\n");
132 v.type = FcTypeCharSet;
135 for (i = 0; i < n; i++)
138 int len, nchar, wchar;
142 o = json_object_array_get_idx (iter.val, i);
143 type = json_object_get_type (o);
144 if (type != json_type_string) {
145 fprintf (stderr, "E: charset value not string\n");
149 src = (const FcChar8 *) json_object_get_string (o);
150 len = json_object_get_string_len (o);
151 valid = FcUtf8Len (src, len, &nchar, &wchar);
152 if (valid == FcFalse) {
153 fprintf (stderr, "E: charset entry not well formed\n");
158 fprintf (stderr, "E: charset entry not not one codepoint\n");
162 FcUtf8ToUcs4 (src, &dst, len);
163 if (FcCharSetAddChar (cs, dst) == FcFalse) {
164 fprintf (stderr, "E: failed to add to charset\n");
169 } else if (fc_o && fc_o->type == FcTypeString) {
170 for (i = 0; i < n; i++)
172 o = json_object_array_get_idx (iter.val, i);
173 type = json_object_get_type (o);
174 if (type != json_type_string) {
175 fprintf (stderr, "E: unable to convert to string\n");
178 v.type = FcTypeString;
179 v.u.s = (const FcChar8 *) json_object_get_string (o);
180 FcPatternAdd (pat, iter.key, v, FcTrue);
185 FcLangSet* ls = FcLangSetCreate ();
187 fprintf (stderr, "E: failed to create langset\n");
190 v.type = FcTypeLangSet;
193 for (i = 0; i < n; i++)
195 o = json_object_array_get_idx (iter.val, i);
196 type = json_object_get_type (o);
197 if (type != json_type_string) {
198 fprintf (stderr, "E: langset value not string\n");
202 if (FcLangSetAdd (ls, (const FcChar8 *)json_object_get_string (o)) == FcFalse) {
203 fprintf (stderr, "E: failed to add to langset\n");
209 } else if (type == json_type_double || type == json_type_int) {
210 const FcObjectType *fc_o = FcNameGetObjectType (iter.key);
213 if (fc_o && fc_o->type == FcTypeDouble) {
214 for (i = 0; i < n; i++)
216 o = json_object_array_get_idx (iter.val, i);
217 type = json_object_get_type (o);
218 if (type == json_type_double) {
219 v.type = FcTypeDouble;
220 v.u.d = json_object_get_double (o);
221 } else if (type == json_type_int) {
222 v.type = FcTypeInteger;
223 v.u.i = json_object_get_int (o);
225 fprintf (stderr, "E: unable to convert to double\n");
228 FcPatternAdd (pat, iter.key, v, FcTrue);
233 if (n != 2 && n != 4) {
234 fprintf (stderr, "E: array starting with number not range or matrix\n");
237 for (i = 0; i < n; i++) {
238 o = json_object_array_get_idx (iter.val, i);
239 type = json_object_get_type (o);
240 if (type != json_type_double && type != json_type_int) {
241 fprintf (stderr, "E: numeric array entry not a number\n");
244 values[i] = json_object_get_double (o);
247 v.type = FcTypeRange;
248 v.u.r = FcRangeCreateDouble (values[0], values[1]);
250 fprintf (stderr, "E: failed to create range\n");
255 v.type = FcTypeMatrix;
257 matrix.xx = values[0];
258 matrix.xy = values[1];
259 matrix.yx = values[2];
260 matrix.yy = values[3];
264 fprintf (stderr, "E: array format not recognized\n");
270 fprintf (stderr, "W: unexpected object to build a pattern: (%s %s)", iter.key, json_type_to_name (json_object_get_type (iter.val)));
273 if (v.type != FcTypeVoid)
274 FcPatternAdd (pat, iter.key, v, FcTrue);
282 build_fs (json_object *obj)
284 FcFontSet *fs = FcFontSetCreate ();
287 n = json_object_array_length (obj);
288 for (i = 0; i < n; i++)
290 json_object *o = json_object_array_get_idx (obj, i);
293 if (json_object_get_type (o) != json_type_object)
295 pat = build_pattern (o);
296 FcFontSetAdd (fs, pat);
303 build_fonts (FcConfig *config, json_object *root)
308 if (!json_object_object_get_ex (root, "fonts", &fonts) ||
309 json_object_get_type (fonts) != json_type_array)
311 fprintf (stderr, "W: No fonts defined\n");
314 fs = build_fs (fonts);
315 /* FcConfigSetFonts (config, fs, FcSetSystem); */
316 if (config->fonts[FcSetSystem])
317 FcFontSetDestroy (config->fonts[FcSetSystem]);
318 config->fonts[FcSetSystem] = fs;
324 run_test (FcConfig *config, json_object *root)
329 if (!json_object_object_get_ex (root, "tests", &tests) ||
330 json_object_get_type (tests) != json_type_array)
332 fprintf (stderr, "W: No test cases defined\n");
335 n = json_object_array_length (tests);
336 for (i = 0; i < n; i++)
338 json_object *obj = json_object_array_get_idx (tests, i);
339 json_object_iter iter;
340 FcPattern *query = NULL;
341 FcPattern *result = NULL;
342 FcFontSet *result_fs = NULL;
343 const char *method = NULL;
345 if (json_object_get_type (obj) != json_type_object)
347 json_object_object_foreachC (obj, iter)
349 if (strcmp (iter.key, "method") == 0)
351 if (json_object_get_type (iter.val) != json_type_string)
353 fprintf (stderr, "W: invalid type of method: (%s)\n", json_type_to_name (json_object_get_type (iter.val)));
356 method = json_object_get_string (iter.val);
358 else if (strcmp (iter.key, "query") == 0)
360 if (json_object_get_type (iter.val) != json_type_object)
362 fprintf (stderr, "W: invalid type of query: (%s)\n", json_type_to_name (json_object_get_type (iter.val)));
366 FcPatternDestroy (query);
367 query = build_pattern (iter.val);
369 else if (strcmp (iter.key, "result") == 0)
371 if (json_object_get_type (iter.val) != json_type_object)
373 fprintf (stderr, "W: invalid type of result: (%s)\n", json_type_to_name (json_object_get_type (iter.val)));
377 FcPatternDestroy (result);
378 result = build_pattern (iter.val);
380 else if (strcmp (iter.key, "result_fs") == 0)
382 if (json_object_get_type (iter.val) != json_type_array)
384 fprintf (stderr, "W: invalid type of result_fs: (%s)\n", json_type_to_name (json_object_get_type (iter.val)));
388 FcFontSetDestroy (result_fs);
389 result_fs = build_fs (iter.val);
393 fprintf (stderr, "W: unknown object: %s\n", iter.key);
396 if (method != NULL && strcmp (method, "match") == 0)
403 fprintf (stderr, "E: no query defined.\n");
409 fprintf (stderr, "E: no result defined.\n");
413 FcConfigSubstitute (config, query, FcMatchPattern);
414 FcDefaultSubstitute (query);
415 match = FcFontMatch (config, query, &res);
421 FcPatternIterStart (result, &iter);
424 vc = FcPatternIterValueCount (result, &iter);
425 for (x = 0; x < vc; x++)
429 if (FcPatternIterGetValue (result, &iter, x, &vr, NULL) != FcResultMatch)
431 fprintf (stderr, "E: unable to obtain a value from the expected result\n");
435 if (FcPatternGet (match, FcPatternIterGetObject (result, &iter), x, &vm) != FcResultMatch)
437 vm.type = FcTypeVoid;
439 if (!FcValueEqual (vm, vr))
441 printf ("E: failed to compare %s:\n", FcPatternIterGetObject (result, &iter));
442 printf (" actual result:");
444 printf ("\n expected result:");
451 } while (FcPatternIterNext (result, &iter));
453 FcPatternDestroy (match);
457 fprintf (stderr, "E: no match\n");
461 else if (method != NULL && strcmp (method, "list") == 0)
467 fprintf (stderr, "E: no query defined.\n");
473 fprintf (stderr, "E: no result_fs defined.\n");
477 fs = FcFontList (config, query, NULL);
480 fprintf (stderr, "E: failed on FcFontList\n");
487 if (fs->nfont != result_fs->nfont)
489 printf ("E: The number of results is different:\n");
490 printf (" actual result: %d\n", fs->nfont);
491 printf (" expected result: %d\n", result_fs->nfont);
495 for (i = 0; i < fs->nfont; i++)
500 FcPatternIterStart (result_fs->fonts[i], &iter);
503 vc = FcPatternIterValueCount (result_fs->fonts[i], &iter);
504 for (x = 0; x < vc; x++)
508 if (FcPatternIterGetValue (result_fs->fonts[i], &iter, x, &vr, NULL) != FcResultMatch)
510 fprintf (stderr, "E: unable to obtain a value from the expected result\n");
514 if (FcPatternGet (fs->fonts[i], FcPatternIterGetObject (result_fs->fonts[i], &iter), x, &vm) != FcResultMatch)
516 vm.type = FcTypeVoid;
518 if (!FcValueEqual (vm, vr))
520 printf ("E: failed to compare %s:\n", FcPatternIterGetObject (result_fs->fonts[i], &iter));
521 printf (" actual result:");
523 printf ("\n expected result:");
530 } while (FcPatternIterNext (result_fs->fonts[i], &iter));
533 FcFontSetDestroy (fs);
538 fprintf (stderr, "W: unknown testing method: %s\n", method);
544 FcPatternDestroy (result);
549 FcFontSetDestroy (result_fs);
554 FcPatternDestroy (query);
563 run_scenario (FcConfig *config, char *file)
566 json_object *root, *scenario;
568 root = json_object_from_file (file);
571 fprintf (stderr, "E: Unable to read the file: %s\n", file);
574 if (!build_fonts (config, root))
579 if (!run_test (config, root))
586 json_object_put (root);
592 load_config (FcConfig *config, char *file)
599 if ((fp = fopen(file, "rb")) == NULL)
601 fseek (fp, 0L, SEEK_END);
603 fseek (fp, 0L, SEEK_SET);
604 buf = malloc (sizeof (char) * (len + 1));
610 fread (buf, (size_t)len, sizeof (char), fp);
613 ret = FcConfigParseAndLoadFromMemory (config, (const FcChar8 *) buf, FcTrue);
623 main (int argc, char **argv)
630 fprintf(stderr, "Usage: %s <conf file> <test scenario>\n", argv[0]);
634 config = FcConfigCreate ();
635 if (!load_config (config, argv[1]))
637 fprintf(stderr, "E: Failed to load config\n");
641 if (!run_scenario (config, argv[2]))
647 FcConfigDestroy (config);