4 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
30 static FcConfig
*fcConfig
;
38 config
= malloc (sizeof (FcConfig
));
42 config
->dirs
= malloc (sizeof (char *));
47 config
->configFiles
= malloc (sizeof (char *));
48 if (!config
->configFiles
)
50 config
->configFiles
[0] = 0;
53 if (!FcConfigSetCache (config
, "~/" FC_USER_CACHE_FILE
))
58 config
->substPattern
= 0;
59 config
->substFont
= 0;
60 config
->maxObjects
= 0;
61 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
62 config
->fonts
[set
] = 0;
67 free (config
->configFiles
);
77 FcSubstDestroy (FcSubst
*s
)
84 FcTestDestroy (s
->test
);
85 FcEditDestroy (s
->edit
);
91 FcConfigDestroyStrings (char **strings
)
95 for (s
= strings
; s
&& *s
; s
++)
102 FcConfigAddString (char ***strings
, char *string
)
108 for (s
= *strings
; s
&& *s
; s
++)
110 s
= malloc ((n
+ 2) * sizeof (char *));
115 memcpy (s
, *strings
, n
* sizeof (char *));
122 FcConfigDestroy (FcConfig
*config
)
125 FcConfigDestroyStrings (config
->dirs
);
126 FcConfigDestroyStrings (config
->configFiles
);
128 free (config
->cache
);
130 FcSubstDestroy (config
->substPattern
);
131 FcSubstDestroy (config
->substFont
);
132 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
133 if (config
->fonts
[set
])
134 FcFontSetDestroy (config
->fonts
[set
]);
138 * Scan the current list of directories in the configuration
139 * and build the set of available fonts. Update the
140 * per-user cache file to reflect the new configuration
144 FcConfigBuildFonts (FcConfig
*config
)
150 fonts
= FcFontSetCreate ();
154 cache
= FcFileCacheCreate ();
158 FcFileCacheLoad (cache
, config
->cache
);
160 for (d
= config
->dirs
; d
&& *d
; d
++)
162 if (FcDebug () & FC_DBG_FONTSET
)
163 printf ("scan dir %s\n", *d
);
164 FcDirScan (fonts
, cache
, config
->blanks
, *d
, FcFalse
);
167 if (FcDebug () & FC_DBG_FONTSET
)
168 FcFontSetPrint (fonts
);
170 FcFileCacheSave (cache
, config
->cache
);
171 FcFileCacheDestroy (cache
);
173 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
177 FcFontSetDestroy (fonts
);
183 FcConfigSetCurrent (FcConfig
*config
)
186 if (!FcConfigBuildFonts (config
))
190 FcConfigDestroy (fcConfig
);
196 FcConfigGetCurrent (void)
202 FcConfigAddDir (FcConfig
*config
,
213 dir
= (char *) malloc (strlen (h
) + strlen (d
));
221 dir
= (char *) malloc (strlen (d
) + 1);
226 if (!FcConfigAddString (&config
->dirs
, dir
))
235 FcConfigGetDirs (FcConfig
*config
)
239 config
= FcConfigGetCurrent ();
247 FcConfigAddConfigFile (FcConfig
*config
,
251 file
= FcConfigFilename (f
);
254 if (!FcConfigAddString (&config
->configFiles
, file
))
263 FcConfigGetConfigFiles (FcConfig
*config
)
267 config
= FcConfigGetCurrent ();
271 return config
->configFiles
;
275 FcConfigSetCache (FcConfig
*config
,
286 new = (char *) malloc (strlen (h
) + strlen (c
));
297 free (config
->cache
);
303 FcConfigGetCache (FcConfig
*config
)
307 config
= FcConfigGetCurrent ();
311 return config
->cache
;
315 FcConfigGetFonts (FcConfig
*config
,
320 config
= FcConfigGetCurrent ();
324 return config
->fonts
[set
];
328 FcConfigSetFonts (FcConfig
*config
,
332 if (config
->fonts
[set
])
333 FcFontSetDestroy (config
->fonts
[set
]);
334 config
->fonts
[set
] = fonts
;
338 FcConfigGetBlanks (FcConfig
*config
)
342 config
= FcConfigGetCurrent ();
346 return config
->blanks
;
350 FcConfigAddBlank (FcConfig
*config
,
358 b
= FcBlanksCreate ();
362 if (!FcBlanksAdd (b
, blank
))
369 FcConfigAddEdit (FcConfig
*config
,
374 FcSubst
*subst
, **prev
;
378 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
381 if (kind
== FcMatchPattern
)
382 prev
= &config
->substPattern
;
384 prev
= &config
->substFont
;
385 for (; *prev
; prev
= &(*prev
)->next
);
390 if (FcDebug () & FC_DBG_EDIT
)
392 printf ("Add Subst ");
393 FcSubstPrint (subst
);
396 for (t
= test
; t
; t
= t
->next
)
398 if (config
->maxObjects
< num
)
399 config
->maxObjects
= num
;
403 typedef struct _FcSubState
{
408 static const FcMatrix FcIdentityMatrix
= { 1, 0, 0, 1 };
411 FcConfigPromote (FcValue v
, FcValue u
)
413 if (v
.type
== FcTypeInteger
)
415 v
.type
= FcTypeDouble
;
416 v
.u
.d
= (double) v
.u
.i
;
418 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
420 v
.u
.m
= (FcMatrix
*) &FcIdentityMatrix
;
421 v
.type
= FcTypeMatrix
;
427 FcConfigCompareValue (FcValue m
,
431 FcBool ret
= FcFalse
;
433 m
= FcConfigPromote (m
, v
);
434 v
= FcConfigPromote (v
, m
);
435 if (m
.type
== v
.type
)
440 break; /* FcConfigPromote prevents this from happening */
445 ret
= m
.u
.d
== v
.u
.d
;
448 ret
= m
.u
.d
!= v
.u
.d
;
454 ret
= m
.u
.d
<= v
.u
.d
;
460 ret
= m
.u
.d
>= v
.u
.d
;
470 ret
= m
.u
.b
== v
.u
.b
;
473 ret
= m
.u
.b
!= v
.u
.b
;
483 ret
= FcStrCmpIgnoreCase (m
.u
.s
, v
.u
.s
) == 0;
486 ret
= FcStrCmpIgnoreCase (m
.u
.s
, v
.u
.s
) != 0;
496 ret
= FcMatrixEqual (m
.u
.m
, v
.u
.m
);
499 ret
= !FcMatrixEqual (m
.u
.m
, v
.u
.m
);
508 /* m contains v if v - m is empty */
509 ret
= FcCharSetSubtractCount (v
.u
.c
, m
.u
.c
) == 0;
512 ret
= FcCharSetEqual (m
.u
.c
, v
.u
.c
);
515 ret
= !FcCharSetEqual (m
.u
.c
, v
.u
.c
);
535 if (op
== FcOpNotEqual
)
543 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
552 v
.type
= FcTypeInteger
;
556 v
.type
= FcTypeDouble
;
560 v
.type
= FcTypeString
;
565 v
.type
= FcTypeMatrix
;
570 v
.type
= FcTypeCharSet
;
579 r
= FcPatternGet (p
, e
->u
.field
, 0, &v
);
580 if (r
!= FcResultMatch
)
584 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
585 v
.type
= FcTypeInteger
;
590 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
591 if (vl
.type
== FcTypeBool
)
594 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
596 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
615 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
616 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
617 vl
= FcConfigPromote (vl
, vr
);
618 vr
= FcConfigPromote (vr
, vl
);
619 if (vl
.type
== vr
.type
)
625 v
.type
= FcTypeDouble
;
626 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
629 v
.type
= FcTypeDouble
;
630 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
633 v
.type
= FcTypeDouble
;
634 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
637 v
.type
= FcTypeDouble
;
638 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
643 v
.u
.b
= vl
.u
.d
== vr
.u
.d
;
647 v
.u
.b
= vl
.u
.d
!= vr
.u
.d
;
651 v
.u
.b
= vl
.u
.d
< vr
.u
.d
;
655 v
.u
.b
= vl
.u
.d
<= vr
.u
.d
;
659 v
.u
.b
= vl
.u
.d
> vr
.u
.d
;
663 v
.u
.b
= vl
.u
.d
>= vr
.u
.d
;
669 if (v
.type
== FcTypeDouble
&&
670 v
.u
.d
== (double) (int) v
.u
.d
)
672 v
.type
= FcTypeInteger
;
680 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
684 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
689 v
.u
.b
= vl
.u
.b
== vr
.u
.b
;
693 v
.u
.b
= vl
.u
.b
!= vr
.u
.b
;
705 v
.u
.b
= FcStrCmpIgnoreCase (vl
.u
.s
, vr
.u
.s
) == 0;
709 v
.u
.b
= FcStrCmpIgnoreCase (vl
.u
.s
, vr
.u
.s
) != 0;
712 v
.type
= FcTypeString
;
713 v
.u
.s
= FcStrPlus (vl
.u
.s
, vr
.u
.s
);
726 v
.u
.b
= FcMatrixEqual (vl
.u
.m
, vr
.u
.m
);
730 v
.u
.b
= FcMatrixEqual (vl
.u
.m
, vr
.u
.m
);
733 v
.type
= FcTypeMatrix
;
734 m
= malloc (sizeof (FcMatrix
));
737 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
738 FcMatrixMultiply (m
, vl
.u
.m
, vr
.u
.m
);
754 /* vl contains vr if vr - vl is empty */
756 v
.u
.b
= FcCharSetSubtractCount (vr
.u
.c
, vl
.u
.c
) == 0;
760 v
.u
.b
= FcCharSetEqual (vl
.u
.c
, vr
.u
.c
);
764 v
.u
.b
= !FcCharSetEqual (vl
.u
.c
, vr
.u
.c
);
782 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
802 FcConfigMatchValueList (FcPattern
*p
,
806 FcValueList
*ret
= 0;
807 FcValue value
= FcConfigEvaluate (p
, t
->expr
);
809 for (; v
; v
= v
->next
)
811 if (FcConfigCompareValue (v
->value
, t
->op
, value
))
818 if (t
->qual
== FcQualAll
)
825 FcValueDestroy (value
);
830 FcConfigValues (FcPattern
*p
, FcExpr
*e
)
836 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
839 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
840 if (e
->op
== FcOpComma
)
842 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
843 l
->next
= FcConfigValues (p
, e
->u
.tree
.right
);
847 l
->value
= FcConfigEvaluate (p
, e
);
850 while (l
->value
.type
== FcTypeVoid
)
852 FcValueList
*next
= l
->next
;
854 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
862 FcConfigAdd (FcValueList
**head
,
863 FcValueList
*position
,
867 FcValueList
**prev
, *last
;
872 prev
= &position
->next
;
874 for (prev
= head
; *prev
; prev
= &(*prev
)->next
)
881 for (prev
= head
; *prev
; prev
= &(*prev
)->next
)
883 if (*prev
== position
)
890 if (FcDebug () & FC_DBG_EDIT
)
893 printf ("position not on list\n");
897 if (FcDebug () & FC_DBG_EDIT
)
899 printf ("%s list before ", append
? "Append" : "Prepend");
900 FcValueListPrint (*head
);
914 if (FcDebug () & FC_DBG_EDIT
)
916 printf ("%s list after ", append
? "Append" : "Prepend");
917 FcValueListPrint (*head
);
925 FcConfigDel (FcValueList
**head
,
926 FcValueList
*position
)
930 for (prev
= head
; *prev
; prev
= &(*prev
)->next
)
932 if (*prev
== position
)
934 *prev
= position
->next
;
936 FcValueListDestroy (position
);
943 FcConfigPatternAdd (FcPattern
*p
,
950 FcPatternElt
*e
= FcPatternFind (p
, object
, FcTrue
);
954 FcConfigAdd (&e
->values
, 0, append
, list
);
959 * Delete all values associated with a field
962 FcConfigPatternDel (FcPattern
*p
,
965 FcPatternElt
*e
= FcPatternFind (p
, object
, FcFalse
);
969 FcConfigDel (&e
->values
, e
->values
);
973 FcConfigPatternCanon (FcPattern
*p
,
976 FcPatternElt
*e
= FcPatternFind (p
, object
, FcFalse
);
980 FcPatternDel (p
, object
);
984 FcConfigSubstitute (FcConfig
*config
,
997 config
= FcConfigGetCurrent ();
1002 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1003 if (!st
&& config
->maxObjects
)
1005 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1007 if (FcDebug () & FC_DBG_EDIT
)
1009 printf ("FcConfigSubstitute ");
1012 if (kind
== FcMatchPattern
)
1013 s
= config
->substPattern
;
1015 s
= config
->substFont
;
1016 for (; s
; s
= s
->next
)
1019 * Check the tests to see if
1020 * they all match the pattern
1022 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1024 if (FcDebug () & FC_DBG_EDIT
)
1026 printf ("FcConfigSubstitute test ");
1029 st
[i
].elt
= FcPatternFind (p
, t
->field
, FcFalse
);
1031 * If there's no such field in the font,
1032 * then FcQualAll matches while FcQualAny does not
1036 if (t
->qual
== FcQualAll
)
1045 * Check to see if there is a match, mark the location
1046 * to apply match-relative edits
1048 st
[i
].value
= FcConfigMatchValueList (p
, t
, st
[i
].elt
->values
);
1054 if (FcDebug () & FC_DBG_EDIT
)
1055 printf ("No match\n");
1058 if (FcDebug () & FC_DBG_EDIT
)
1060 printf ("Substitute ");
1063 for (e
= s
->edit
; e
; e
= e
->next
)
1066 * Evaluate the list of expressions
1068 l
= FcConfigValues (p
, e
->expr
);
1070 * Locate any test associated with this field
1072 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1073 if (!FcStrCmpIgnoreCase (t
->field
, e
->field
))
1078 * If there was a test, then replace the matched
1079 * value with the new list of values
1083 FcValueList
*thisValue
= st
[i
].value
;
1084 FcValueList
*nextValue
= thisValue
? thisValue
->next
: 0;
1087 * Append the new list of values after the current value
1089 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1091 * Adjust any pointers into the value list to ensure
1092 * future edits occur at the same place
1094 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1096 if (st
[i
].value
== thisValue
)
1097 st
[i
].value
= nextValue
;
1100 * Delete the marked value
1102 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1105 /* fall through ... */
1106 case FcOpAssignReplace
:
1108 * Delete all of the values and insert
1111 FcConfigPatternDel (p
, e
->field
);
1112 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1114 * Adjust any pointers into the value list as they no
1115 * longer point to anything valid
1119 FcPatternElt
*thisElt
= st
[i
].elt
;
1120 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1122 if (st
[i
].elt
== thisElt
)
1130 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1133 /* fall through ... */
1134 case FcOpPrependFirst
:
1135 FcConfigPatternAdd (p
, e
->field
, l
, FcFalse
);
1140 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1143 /* fall through ... */
1144 case FcOpAppendLast
:
1145 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1152 * Now go through the pattern and eliminate
1153 * any properties without data
1155 for (e
= s
->edit
; e
; e
= e
->next
)
1156 FcConfigPatternCanon (p
, e
->field
);
1158 if (FcDebug () & FC_DBG_EDIT
)
1160 printf ("FcConfigSubstitute edit");
1164 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1166 if (FcDebug () & FC_DBG_EDIT
)
1168 printf ("FcConfigSubstitute done");
1174 #ifndef FONTCONFIG_PATH
1175 #define FONTCONFIG_PATH "/etc/fonts"
1178 #ifndef FONTCONFIG_FILE
1179 #define FONTCONFIG_FILE "fonts.conf"
1183 FcConfigFileExists (const char *dir
, const char *file
)
1189 path
= malloc (strlen (dir
) + 1 + strlen (file
) + 1);
1194 /* make sure there's a single separating / */
1195 if ((!path
[0] || path
[strlen(path
)-1] != '/') && file
[0] != '/')
1197 strcat (path
, file
);
1199 if (access (path
, R_OK
) == 0)
1207 FcConfigGetPath (void)
1210 char *env
, *e
, *colon
;
1215 npath
= 2; /* default dir + null */
1216 env
= getenv ("FONTCONFIG_PATH");
1225 path
= calloc (npath
, sizeof (char *));
1235 colon
= strchr (e
, ':');
1237 colon
= e
+ strlen (e
);
1238 path
[i
] = malloc (colon
- e
+ 1);
1241 strncpy (path
[i
], e
, colon
- e
);
1242 path
[i
][colon
- e
] = '\0';
1251 dir
= FONTCONFIG_PATH
;
1252 path
[i
] = malloc (strlen (dir
) + 1);
1255 strcpy (path
[i
], dir
);
1259 for (i
= 0; path
[i
]; i
++)
1267 FcConfigFreePath (char **path
)
1271 for (p
= path
; *p
; p
++)
1277 FcConfigFilename (const char *url
)
1279 char *file
, *dir
, **path
, **p
;
1283 url
= getenv ("FONTCONFIG_FILE");
1285 url
= FONTCONFIG_FILE
;
1289 dir
= getenv ("HOME");
1291 file
= FcConfigFileExists (dir
, url
+ 1);
1296 file
= FcConfigFileExists (0, url
);
1299 path
= FcConfigGetPath ();
1302 for (p
= path
; *p
; p
++)
1304 file
= FcConfigFileExists (*p
, url
);
1308 FcConfigFreePath (path
);
1315 * Manage the application-specific fonts
1319 FcConfigAppFontAddFile (FcConfig
*config
,
1326 config
= FcConfigGetCurrent ();
1331 set
= FcConfigGetFonts (config
, FcSetApplication
);
1334 set
= FcFontSetCreate ();
1337 FcConfigSetFonts (config
, set
, FcSetApplication
);
1339 return FcFileScan (set
, 0, config
->blanks
, file
, FcFalse
);
1343 FcConfigAppFontAddDir (FcConfig
*config
,
1350 config
= FcConfigGetCurrent ();
1354 set
= FcConfigGetFonts (config
, FcSetApplication
);
1357 set
= FcFontSetCreate ();
1360 FcConfigSetFonts (config
, set
, FcSetApplication
);
1362 return FcDirScan (set
, 0, config
->blanks
, dir
, FcFalse
);
1366 FcConfigAppFontClear (FcConfig
*config
)
1368 FcConfigSetFonts (config
, 0, FcSetApplication
);