2 * $RCSId: xc/lib/fontconfig/src/fccfg.c,v 1.23 2002/08/31 22:17:32 keithp Exp $
4 * Copyright © 2000 Keith Packard
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.
27 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
33 #if defined (_WIN32) && !defined (R_OK)
45 config
= malloc (sizeof (FcConfig
));
48 FcMemAlloc (FC_MEM_CONFIG
, sizeof (FcConfig
));
50 config
->configDirs
= FcStrSetCreate ();
51 if (!config
->configDirs
)
54 config
->configFiles
= FcStrSetCreate ();
55 if (!config
->configFiles
)
58 config
->fontDirs
= FcStrSetCreate ();
59 if (!config
->fontDirs
)
62 config
->acceptGlobs
= FcStrSetCreate ();
63 if (!config
->acceptGlobs
)
66 config
->rejectGlobs
= FcStrSetCreate ();
67 if (!config
->rejectGlobs
)
70 config
->acceptPatterns
= FcFontSetCreate ();
71 if (!config
->acceptPatterns
)
74 config
->rejectPatterns
= FcFontSetCreate ();
75 if (!config
->rejectPatterns
)
80 if (!FcConfigSetCache (config
, (FcChar8
*) ("~/" FC_USER_CACHE_FILE
)))
84 if (config
->cache
== 0)
86 /* If no home, use the temp folder. */
88 int templen
= GetTempPath (1, dummy
);
89 FcChar8
*temp
= malloc (templen
+ 1);
95 GetTempPath (templen
+ 1, temp
);
96 cache_dir
= FcStrPlus (temp
, FC_USER_CACHE_FILE
);
98 if (!FcConfigSetCache (config
, cache_dir
))
100 FcStrFree (cache_dir
);
103 FcStrFree (cache_dir
);
110 config
->substPattern
= 0;
111 config
->substFont
= 0;
112 config
->maxObjects
= 0;
113 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
114 config
->fonts
[set
] = 0;
116 config
->rescanTime
= time(0);
117 config
->rescanInterval
= 30;
122 FcFontSetDestroy (config
->rejectPatterns
);
124 FcFontSetDestroy (config
->acceptPatterns
);
126 FcStrSetDestroy (config
->rejectGlobs
);
128 FcStrSetDestroy (config
->acceptGlobs
);
130 FcStrSetDestroy (config
->fontDirs
);
132 FcStrSetDestroy (config
->configFiles
);
134 FcStrSetDestroy (config
->configDirs
);
137 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
142 typedef struct _FcFileTime
{
148 FcConfigNewestFile (FcStrSet
*files
)
150 FcStrList
*list
= FcStrListCreate (files
);
151 FcFileTime newest
= { 0, FcFalse
};
157 while ((file
= FcStrListNext (list
)))
158 if (stat ((char *) file
, &statb
) == 0)
159 if (!newest
.set
|| statb
.st_mtime
- newest
.time
> 0)
162 newest
.time
= statb
.st_mtime
;
164 FcStrListDone (list
);
170 FcConfigUptoDate (FcConfig
*config
)
172 FcFileTime config_time
, font_time
;
173 time_t now
= time(0);
176 config
= FcConfigGetCurrent ();
180 config_time
= FcConfigNewestFile (config
->configFiles
);
181 font_time
= FcConfigNewestFile (config
->fontDirs
);
182 if ((config_time
.set
&& config_time
.time
- config
->rescanTime
> 0) ||
183 (font_time
.set
&& (font_time
.time
- config
->rescanTime
) > 0))
187 config
->rescanTime
= now
;
192 FcSubstDestroy (FcSubst
*s
)
200 FcTestDestroy (s
->test
);
202 FcEditDestroy (s
->edit
);
204 FcMemFree (FC_MEM_SUBST
, sizeof (FcSubst
));
210 FcConfigDestroy (FcConfig
*config
)
214 if (config
== _fcConfig
)
217 FcStrSetDestroy (config
->configDirs
);
218 FcStrSetDestroy (config
->fontDirs
);
219 FcStrSetDestroy (config
->configFiles
);
220 FcStrSetDestroy (config
->acceptGlobs
);
221 FcStrSetDestroy (config
->rejectGlobs
);
222 FcFontSetDestroy (config
->acceptPatterns
);
223 FcFontSetDestroy (config
->rejectPatterns
);
226 FcBlanksDestroy (config
->blanks
);
229 FcStrFree (config
->cache
);
231 FcSubstDestroy (config
->substPattern
);
232 FcSubstDestroy (config
->substFont
);
233 for (set
= FcSetSystem
; set
<= FcSetApplication
; set
++)
234 if (config
->fonts
[set
])
235 FcFontSetDestroy (config
->fonts
[set
]);
238 FcMemFree (FC_MEM_CONFIG
, sizeof (FcConfig
));
242 * Scan the current list of directories in the configuration
243 * and build the set of available fonts. Update the
244 * per-user cache file to reflect the new configuration
248 FcConfigBuildFonts (FcConfig
*config
)
251 FcGlobalCache
*cache
;
255 fonts
= FcFontSetCreate ();
259 cache
= FcGlobalCacheCreate ();
264 FcGlobalCacheLoad (cache
, config
->cache
);
266 list
= FcConfigGetFontDirs (config
);
270 while ((dir
= FcStrListNext (list
)))
272 if (FcDebug () & FC_DBG_FONTSET
)
273 printf ("scan dir %s\n", dir
);
274 FcDirScanConfig (fonts
, config
->fontDirs
, cache
,
275 config
->blanks
, dir
, FcFalse
, config
);
278 FcStrListDone (list
);
280 if (FcDebug () & FC_DBG_FONTSET
)
281 FcFontSetPrint (fonts
);
284 FcGlobalCacheSave (cache
, config
->cache
);
285 FcGlobalCacheDestroy (cache
);
287 FcConfigSetFonts (config
, fonts
, FcSetSystem
);
291 FcFontSetDestroy (fonts
);
297 FcConfigSetCurrent (FcConfig
*config
)
300 if (!FcConfigBuildFonts (config
))
304 FcConfigDestroy (_fcConfig
);
310 FcConfigGetCurrent (void)
319 FcConfigAddConfigDir (FcConfig
*config
,
322 return FcStrSetAddFilename (config
->configDirs
, d
);
326 FcConfigGetConfigDirs (FcConfig
*config
)
330 config
= FcConfigGetCurrent ();
334 return FcStrListCreate (config
->configDirs
);
338 FcConfigAddFontDir (FcConfig
*config
,
341 return FcStrSetAddFilename (config
->fontDirs
, d
);
345 FcConfigAddDir (FcConfig
*config
,
348 return (FcConfigAddConfigDir (config
, d
) &&
349 FcConfigAddFontDir (config
, d
));
353 FcConfigGetFontDirs (FcConfig
*config
)
357 config
= FcConfigGetCurrent ();
361 return FcStrListCreate (config
->fontDirs
);
365 FcConfigAddConfigFile (FcConfig
*config
,
369 FcChar8
*file
= FcConfigFilename (f
);
374 ret
= FcStrSetAdd (config
->configFiles
, file
);
380 FcConfigGetConfigFiles (FcConfig
*config
)
384 config
= FcConfigGetCurrent ();
388 return FcStrListCreate (config
->configFiles
);
392 FcConfigSetCache (FcConfig
*config
,
395 FcChar8
*new = FcStrCopyFilename (c
);
400 FcStrFree (config
->cache
);
406 FcConfigGetCache (FcConfig
*config
)
410 config
= FcConfigGetCurrent ();
414 return config
->cache
;
418 FcConfigGetFonts (FcConfig
*config
,
423 config
= FcConfigGetCurrent ();
427 return config
->fonts
[set
];
431 FcConfigSetFonts (FcConfig
*config
,
435 if (config
->fonts
[set
])
436 FcFontSetDestroy (config
->fonts
[set
]);
437 config
->fonts
[set
] = fonts
;
443 FcConfigGetBlanks (FcConfig
*config
)
447 config
= FcConfigGetCurrent ();
451 return config
->blanks
;
455 FcConfigAddBlank (FcConfig
*config
,
463 b
= FcBlanksCreate ();
467 if (!FcBlanksAdd (b
, blank
))
474 FcConfigGetRescanInverval (FcConfig
*config
)
478 config
= FcConfigGetCurrent ();
482 return config
->rescanInterval
;
486 FcConfigSetRescanInverval (FcConfig
*config
, int rescanInterval
)
490 config
= FcConfigGetCurrent ();
494 config
->rescanInterval
= rescanInterval
;
499 FcConfigAddEdit (FcConfig
*config
,
504 FcSubst
*subst
, **prev
;
508 subst
= (FcSubst
*) malloc (sizeof (FcSubst
));
511 FcMemAlloc (FC_MEM_SUBST
, sizeof (FcSubst
));
512 if (kind
== FcMatchPattern
)
513 prev
= &config
->substPattern
;
515 prev
= &config
->substFont
;
516 for (; *prev
; prev
= &(*prev
)->next
);
522 for (t
= test
; t
; t
= t
->next
)
524 if (t
->kind
== FcMatchDefault
)
528 if (config
->maxObjects
< num
)
529 config
->maxObjects
= num
;
530 if (FcDebug () & FC_DBG_EDIT
)
532 printf ("Add Subst ");
533 FcSubstPrint (subst
);
538 typedef struct _FcSubState
{
544 FcConfigPromote (FcValue v
, FcValue u
)
546 if (v
.type
== FcTypeInteger
)
548 v
.type
= FcTypeDouble
;
549 v
.u
.d
= (double) v
.u
.i
;
551 else if (v
.type
== FcTypeVoid
&& u
.type
== FcTypeMatrix
)
553 v
.u
.mi
= FcIdentityMatrix
;
554 v
.type
= FcTypeMatrix
;
556 else if (v
.type
== FcTypeString
&& u
.type
== FcTypeLangSet
)
558 v
.u
.li
= FcLangSetPtrCreateDynamic(FcLangSetPromote
559 (FcObjectPtrU(v
.u
.si
)));
560 v
.type
= FcTypeLangSet
;
566 FcConfigCompareValue (const FcValue left_o
,
568 const FcValue right_o
)
570 FcValue left
= left_o
;
571 FcValue right
= right_o
;
572 FcBool ret
= FcFalse
;
574 left
= FcConfigPromote (left
, right
);
575 right
= FcConfigPromote (right
, left
);
576 if (left
.type
== right
.type
)
580 break; /* FcConfigPromote prevents this from happening */
586 ret
= left
.u
.d
== right
.u
.d
;
589 case FcOpNotContains
:
590 ret
= left
.u
.d
!= right
.u
.d
;
593 ret
= left
.u
.d
< right
.u
.d
;
596 ret
= left
.u
.d
<= right
.u
.d
;
599 ret
= left
.u
.d
> right
.u
.d
;
602 ret
= left
.u
.d
>= right
.u
.d
;
613 ret
= left
.u
.b
== right
.u
.b
;
616 case FcOpNotContains
:
617 ret
= left
.u
.b
!= right
.u
.b
;
627 ret
= FcStrCmpIgnoreCase (FcObjectPtrU(left
.u
.si
),
628 FcObjectPtrU(right
.u
.si
)) == 0;
631 ret
= FcStrStrIgnoreCase (FcObjectPtrU(left
.u
.si
),
632 FcObjectPtrU(right
.u
.si
)) != 0;
635 ret
= FcStrCmpIgnoreCase (left
.u
.s
, right
.u
.s
) != 0;
637 case FcOpNotContains
:
638 ret
= FcStrCmpIgnoreCase (FcObjectPtrU(left
.u
.si
),
639 FcObjectPtrU(right
.u
.si
)) == 0;
650 ret
= FcMatrixEqual (FcMatrixPtrU(left
.u
.mi
), FcMatrixPtrU(right
.u
.mi
));
653 case FcOpNotContains
:
654 ret
= !FcMatrixEqual (FcMatrixPtrU(left
.u
.mi
), FcMatrixPtrU(right
.u
.mi
));
664 /* left contains right if right is a subset of left */
665 ret
= FcCharSetIsSubset (FcCharSetPtrU(right
.u
.ci
), FcCharSetPtrU(left
.u
.ci
));
667 case FcOpNotContains
:
668 /* left contains right if right is a subset of left */
669 ret
= !FcCharSetIsSubset (FcCharSetPtrU(right
.u
.ci
), FcCharSetPtrU(left
.u
.ci
));
672 ret
= FcCharSetEqual (FcCharSetPtrU(left
.u
.ci
), FcCharSetPtrU(right
.u
.ci
));
675 ret
= !FcCharSetEqual (FcCharSetPtrU(left
.u
.ci
), FcCharSetPtrU(right
.u
.ci
));
685 ret
= FcLangSetContains (FcLangSetPtrU(left
.u
.li
), FcLangSetPtrU(right
.u
.li
));
687 case FcOpNotContains
:
688 ret
= !FcLangSetContains (FcLangSetPtrU(left
.u
.li
), FcLangSetPtrU(right
.u
.li
));
691 ret
= FcLangSetEqual (FcLangSetPtrU(left
.u
.li
), FcLangSetPtrU(right
.u
.li
));
694 ret
= !FcLangSetEqual (FcLangSetPtrU(left
.u
.li
), FcLangSetPtrU(right
.u
.li
));
716 ret
= left
.u
.f
== right
.u
.f
;
719 case FcOpNotContains
:
720 ret
= left
.u
.f
!= right
.u
.f
;
730 if (op
== FcOpNotEqual
|| op
== FcOpNotContains
)
737 #define _FcDoubleFloor(d) ((int) (d))
738 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
739 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
740 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
741 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
742 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
745 FcConfigEvaluate (FcPattern
*p
, FcExpr
*e
)
753 v
.type
= FcTypeInteger
;
757 v
.type
= FcTypeDouble
;
761 v
.type
= FcTypeString
;
762 v
.u
.si
= FcObjectPtrCreateDynamic(e
->u
.sval
);
766 v
.type
= FcTypeMatrix
;
767 v
.u
.mi
= FcMatrixPtrCreateDynamic(e
->u
.mval
);
771 v
.type
= FcTypeCharSet
;
772 v
.u
.ci
= FcCharSetPtrCreateDynamic(e
->u
.cval
);
780 r
= FcPatternGet (p
, e
->u
.field
, 0, &v
);
781 if (r
!= FcResultMatch
)
785 if (FcNameConstant (e
->u
.constant
, &v
.u
.i
))
786 v
.type
= FcTypeInteger
;
791 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
792 if (vl
.type
== FcTypeBool
)
795 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.left
);
797 v
= FcConfigEvaluate (p
, e
->u
.tree
.right
->u
.tree
.right
);
810 case FcOpNotContains
:
812 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
813 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
815 v
.u
.b
= FcConfigCompareValue (vl
, e
->op
, vr
);
825 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
826 vr
= FcConfigEvaluate (p
, e
->u
.tree
.right
);
827 vl
= FcConfigPromote (vl
, vr
);
828 vr
= FcConfigPromote (vr
, vl
);
829 if (vl
.type
== vr
.type
)
835 v
.type
= FcTypeDouble
;
836 v
.u
.d
= vl
.u
.d
+ vr
.u
.d
;
839 v
.type
= FcTypeDouble
;
840 v
.u
.d
= vl
.u
.d
- vr
.u
.d
;
843 v
.type
= FcTypeDouble
;
844 v
.u
.d
= vl
.u
.d
* vr
.u
.d
;
847 v
.type
= FcTypeDouble
;
848 v
.u
.d
= vl
.u
.d
/ vr
.u
.d
;
854 if (v
.type
== FcTypeDouble
&&
855 v
.u
.d
== (double) (int) v
.u
.d
)
857 v
.type
= FcTypeInteger
;
865 v
.u
.b
= vl
.u
.b
|| vr
.u
.b
;
869 v
.u
.b
= vl
.u
.b
&& vr
.u
.b
;
879 v
.type
= FcTypeString
;
880 v
.u
.si
= FcObjectPtrCreateDynamic
881 (FcStrPlus (FcObjectPtrU(vl
.u
.si
),
882 FcObjectPtrU(vr
.u
.si
)));
884 if (!FcObjectPtrU(v
.u
.si
))
895 v
.type
= FcTypeMatrix
;
896 m
= malloc (sizeof (FcMatrix
));
899 FcMemAlloc (FC_MEM_MATRIX
, sizeof (FcMatrix
));
900 FcMatrixMultiply (m
, FcMatrixPtrU(vl
.u
.mi
),
901 FcMatrixPtrU(vr
.u
.mi
));
902 v
.u
.mi
= FcMatrixPtrCreateDynamic(m
);
925 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
938 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
944 v
.type
= FcTypeInteger
;
945 v
.u
.i
= FcDoubleFloor (vl
.u
.d
);
954 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
960 v
.type
= FcTypeInteger
;
961 v
.u
.i
= FcDoubleCeil (vl
.u
.d
);
970 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
976 v
.type
= FcTypeInteger
;
977 v
.u
.i
= FcDoubleRound (vl
.u
.d
);
986 vl
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
992 v
.type
= FcTypeInteger
;
993 v
.u
.i
= FcDoubleTrunc (vl
.u
.d
);
1002 v
.type
= FcTypeVoid
;
1008 static FcValueList
*
1009 FcConfigMatchValueList (FcPattern
*p
,
1011 FcValueList
*values
)
1013 FcValueList
*ret
= 0;
1014 FcExpr
*e
= t
->expr
;
1020 /* Compute the value of the match expression */
1021 if (e
->op
== FcOpComma
)
1023 value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1024 e
= e
->u
.tree
.right
;
1028 value
= FcConfigEvaluate (p
, e
);
1032 for (v
= values
; v
; v
= FcValueListPtrU(v
->next
))
1034 /* Compare the pattern value to the match expression value */
1035 if (FcConfigCompareValue (v
->value
, t
->op
, value
))
1042 if (t
->qual
== FcQualAll
)
1049 FcValueDestroy (value
);
1054 static FcValueList
*
1055 FcConfigValues (FcPattern
*p
, FcExpr
*e
, FcValueBinding binding
)
1062 l
= (FcValueList
*) malloc (sizeof (FcValueList
));
1065 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
1066 if (e
->op
== FcOpComma
)
1068 l
->value
= FcConfigEvaluate (p
, e
->u
.tree
.left
);
1069 l
->next
= FcValueListPtrCreateDynamic(FcConfigValues (p
, e
->u
.tree
.right
, binding
));
1073 l
->value
= FcConfigEvaluate (p
, e
);
1074 l
->next
= FcValueListPtrCreateDynamic(0);
1076 l
->binding
= binding
;
1077 lp
= FcValueListPtrCreateDynamic(l
);
1078 while (FcValueListPtrU(lp
) && FcValueListPtrU(lp
)->value
.type
== FcTypeVoid
)
1080 FcValueListPtr next
= FcValueListPtrU(lp
)->next
;
1082 if (lp
.storage
== FcStorageDynamic
)
1084 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
1093 FcConfigAdd (FcValueListPtr
*head
,
1094 FcValueList
*position
,
1098 FcValueListPtr
*prev
, last
, v
;
1099 FcValueBinding sameBinding
;
1102 sameBinding
= position
->binding
;
1104 sameBinding
= FcValueBindingWeak
;
1105 for (v
= FcValueListPtrCreateDynamic(new); FcValueListPtrU(v
);
1106 v
= FcValueListPtrU(v
)->next
)
1107 if (FcValueListPtrU(v
)->binding
== FcValueBindingSame
)
1108 FcValueListPtrU(v
)->binding
= sameBinding
;
1112 prev
= &position
->next
;
1114 for (prev
= head
; FcValueListPtrU(*prev
);
1115 prev
= &(FcValueListPtrU(*prev
)->next
))
1122 for (prev
= head
; FcValueListPtrU(*prev
);
1123 prev
= &(FcValueListPtrU(*prev
)->next
))
1125 if (FcValueListPtrU(*prev
) == position
)
1132 if (FcDebug () & FC_DBG_EDIT
)
1134 if (!FcValueListPtrU(*prev
))
1135 printf ("position not on list\n");
1139 if (FcDebug () & FC_DBG_EDIT
)
1141 printf ("%s list before ", append
? "Append" : "Prepend");
1142 FcValueListPrint (*head
);
1148 last
= FcValueListPtrCreateDynamic(new);
1149 while (FcValueListPtrU(FcValueListPtrU(last
)->next
))
1150 last
= FcValueListPtrU(last
)->next
;
1152 FcValueListPtrU(last
)->next
= *prev
;
1153 *prev
= FcValueListPtrCreateDynamic(new);
1156 if (FcDebug () & FC_DBG_EDIT
)
1158 printf ("%s list after ", append
? "Append" : "Prepend");
1159 FcValueListPrint (*head
);
1167 FcConfigDel (FcValueListPtr
*head
,
1168 FcValueList
*position
)
1170 FcValueListPtr
*prev
;
1172 for (prev
= head
; FcValueListPtrU(*prev
);
1173 prev
= &(FcValueListPtrU(*prev
)->next
))
1175 if (FcValueListPtrU(*prev
) == position
)
1177 *prev
= position
->next
;
1178 position
->next
= FcValueListPtrCreateDynamic(0);
1179 FcValueListDestroy (FcValueListPtrCreateDynamic(position
));
1186 FcConfigPatternAdd (FcPattern
*p
,
1193 FcPatternElt
*e
= FcPatternInsertElt (p
, object
);
1197 FcConfigAdd (&e
->values
, 0, append
, list
);
1202 * Delete all values associated with a field
1205 FcConfigPatternDel (FcPattern
*p
,
1208 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1211 while (FcValueListPtrU(e
->values
))
1212 FcConfigDel (&e
->values
, FcValueListPtrU(e
->values
));
1216 FcConfigPatternCanon (FcPattern
*p
,
1219 FcPatternElt
*e
= FcPatternFindElt (p
, object
);
1222 if (!FcValueListPtrU(e
->values
))
1223 FcPatternDel (p
, object
);
1227 FcConfigSubstituteWithPat (FcConfig
*config
,
1242 config
= FcConfigGetCurrent ();
1247 st
= (FcSubState
*) malloc (config
->maxObjects
* sizeof (FcSubState
));
1248 if (!st
&& config
->maxObjects
)
1250 FcMemAlloc (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1252 if (FcDebug () & FC_DBG_EDIT
)
1254 printf ("FcConfigSubstitute ");
1257 if (kind
== FcMatchPattern
)
1258 s
= config
->substPattern
;
1260 s
= config
->substFont
;
1261 for (; s
; s
= s
->next
)
1264 * Check the tests to see if
1265 * they all match the pattern
1267 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1269 if (FcDebug () & FC_DBG_EDIT
)
1271 printf ("FcConfigSubstitute test ");
1275 if (kind
== FcMatchFont
&& t
->kind
== FcMatchPattern
)
1280 st
[i
].elt
= FcPatternFindElt (m
, t
->field
);
1284 * If there's no such field in the font,
1285 * then FcQualAll matches while FcQualAny does not
1289 if (t
->qual
== FcQualAll
)
1298 * Check to see if there is a match, mark the location
1299 * to apply match-relative edits
1301 st
[i
].value
= FcConfigMatchValueList (m
, t
, FcValueListPtrU(st
[i
].elt
->values
));
1304 if (t
->qual
== FcQualFirst
&& st
[i
].value
!= FcValueListPtrU(st
[i
].elt
->values
))
1306 if (t
->qual
== FcQualNotFirst
&& st
[i
].value
== FcValueListPtrU(st
[i
].elt
->values
))
1311 if (FcDebug () & FC_DBG_EDIT
)
1312 printf ("No match\n");
1315 if (FcDebug () & FC_DBG_EDIT
)
1317 printf ("Substitute ");
1320 for (e
= s
->edit
; e
; e
= e
->next
)
1323 * Evaluate the list of expressions
1325 l
= FcConfigValues (p
, e
->expr
, e
->binding
);
1327 * Locate any test associated with this field, skipping
1328 * tests associated with the pattern when substituting in
1331 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1333 if ((t
->kind
== FcMatchFont
|| kind
== FcMatchPattern
) &&
1334 !FcStrCmpIgnoreCase ((FcChar8
*) t
->field
,
1335 (FcChar8
*) e
->field
))
1338 * KLUDGE - the pattern may have been reallocated or
1339 * things may have been inserted or deleted above
1340 * this element by other edits. Go back and find
1343 if (e
!= s
->edit
&& st
[i
].elt
)
1344 st
[i
].elt
= FcPatternFindElt (p
, t
->field
);
1353 * If there was a test, then replace the matched
1354 * value with the new list of values
1358 FcValueList
*thisValue
= st
[i
].value
;
1359 FcValueList
*nextValue
= thisValue
? FcValueListPtrU(thisValue
->next
) : 0;
1362 * Append the new list of values after the current value
1364 FcConfigAdd (&st
[i
].elt
->values
, thisValue
, FcTrue
, l
);
1366 * Delete the marked value
1368 FcConfigDel (&st
[i
].elt
->values
, thisValue
);
1370 * Adjust any pointers into the value list to ensure
1371 * future edits occur at the same place
1373 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1375 if (st
[i
].value
== thisValue
)
1376 st
[i
].value
= nextValue
;
1380 /* fall through ... */
1381 case FcOpAssignReplace
:
1383 * Delete all of the values and insert
1386 FcConfigPatternDel (p
, e
->field
);
1387 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1389 * Adjust any pointers into the value list as they no
1390 * longer point to anything valid
1394 FcPatternElt
*thisElt
= st
[i
].elt
;
1395 for (t
= s
->test
, i
= 0; t
; t
= t
->next
, i
++)
1397 if (st
[i
].elt
== thisElt
)
1405 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcFalse
, l
);
1408 /* fall through ... */
1409 case FcOpPrependFirst
:
1410 FcConfigPatternAdd (p
, e
->field
, l
, FcFalse
);
1415 FcConfigAdd (&st
[i
].elt
->values
, st
[i
].value
, FcTrue
, l
);
1418 /* fall through ... */
1419 case FcOpAppendLast
:
1420 FcConfigPatternAdd (p
, e
->field
, l
, FcTrue
);
1427 * Now go through the pattern and eliminate
1428 * any properties without data
1430 for (e
= s
->edit
; e
; e
= e
->next
)
1431 FcConfigPatternCanon (p
, e
->field
);
1433 if (FcDebug () & FC_DBG_EDIT
)
1435 printf ("FcConfigSubstitute edit");
1439 FcMemFree (FC_MEM_SUBSTATE
, config
->maxObjects
* sizeof (FcSubState
));
1441 if (FcDebug () & FC_DBG_EDIT
)
1443 printf ("FcConfigSubstitute done");
1450 FcConfigSubstitute (FcConfig
*config
,
1454 return FcConfigSubstituteWithPat (config
, p
, 0, kind
);
1457 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1459 static FcChar8 fontconfig_path
[1000] = "";
1462 DllMain (HINSTANCE hinstDLL
,
1468 switch (fdwReason
) {
1469 case DLL_PROCESS_ATTACH
:
1470 if (!GetModuleFileName ((HMODULE
) hinstDLL
, fontconfig_path
,
1471 sizeof (fontconfig_path
)))
1474 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1475 * assume it's a Unix-style installation tree, and use
1476 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1477 * folder where the DLL is as FONTCONFIG_PATH.
1479 p
= strrchr (fontconfig_path
, '\\');
1483 p
= strrchr (fontconfig_path
, '\\');
1484 if (p
&& (FcStrCmpIgnoreCase (p
+ 1, "bin") == 0 ||
1485 FcStrCmpIgnoreCase (p
+ 1, "lib") == 0))
1487 strcat (fontconfig_path
, "\\etc\\fonts");
1490 fontconfig_path
[0] = '\0';
1498 #undef FONTCONFIG_PATH
1499 #define FONTCONFIG_PATH fontconfig_path
1501 #else /* !(_WIN32 && PIC) */
1503 #endif /* !(_WIN32 && PIC) */
1505 #ifndef FONTCONFIG_FILE
1506 #define FONTCONFIG_FILE "fonts.conf"
1510 FcConfigFileExists (const FcChar8
*dir
, const FcChar8
*file
)
1515 dir
= (FcChar8
*) "";
1516 path
= malloc (strlen ((char *) dir
) + 1 + strlen ((char *) file
) + 1);
1520 strcpy ((char *) path
, (const char *) dir
);
1521 /* make sure there's a single separator */
1523 if ((!path
[0] || (path
[strlen((char *) path
)-1] != '/' &&
1524 path
[strlen((char *) path
)-1] != '\\')) &&
1527 (isalpha (file
[0]) && file
[1] == ':' && (file
[2] == '/' || file
[2] == '\\'))))
1528 strcat ((char *) path
, "\\");
1530 if ((!path
[0] || path
[strlen((char *) path
)-1] != '/') && file
[0] != '/')
1531 strcat ((char *) path
, "/");
1533 strcat ((char *) path
, (char *) file
);
1535 FcMemAlloc (FC_MEM_STRING
, strlen ((char *) path
) + 1);
1536 if (access ((char *) path
, R_OK
) == 0)
1544 FcConfigGetPath (void)
1547 FcChar8
*env
, *e
, *colon
;
1552 npath
= 2; /* default dir + null */
1553 env
= (FcChar8
*) getenv ("FONTCONFIG_PATH");
1559 if (*e
++ == FC_SEARCH_PATH_SEPARATOR
)
1562 path
= calloc (npath
, sizeof (FcChar8
*));
1572 colon
= (FcChar8
*) strchr ((char *) e
, FC_SEARCH_PATH_SEPARATOR
);
1574 colon
= e
+ strlen ((char *) e
);
1575 path
[i
] = malloc (colon
- e
+ 1);
1578 strncpy ((char *) path
[i
], (const char *) e
, colon
- e
);
1579 path
[i
][colon
- e
] = '\0';
1588 dir
= (FcChar8
*) FONTCONFIG_PATH
;
1589 path
[i
] = malloc (strlen ((char *) dir
) + 1);
1592 strcpy ((char *) path
[i
], (const char *) dir
);
1596 for (i
= 0; path
[i
]; i
++)
1604 FcConfigFreePath (FcChar8
**path
)
1608 for (p
= path
; *p
; p
++)
1613 static FcBool _FcConfigHomeEnabled
= FcTrue
;
1618 if (_FcConfigHomeEnabled
)
1620 char *home
= getenv ("HOME");
1624 home
= getenv ("USERPROFILE");
1633 FcConfigEnableHome (FcBool enable
)
1635 FcBool prev
= _FcConfigHomeEnabled
;
1636 _FcConfigHomeEnabled
= enable
;
1641 FcConfigFilename (const FcChar8
*url
)
1643 FcChar8
*file
, *dir
, **path
, **p
;
1647 url
= (FcChar8
*) getenv ("FONTCONFIG_FILE");
1649 url
= (FcChar8
*) FONTCONFIG_FILE
;
1654 if (isalpha (*url
) &&
1656 (url
[2] == '/' || url
[2] == '\\'))
1662 dir
= FcConfigHome ();
1664 file
= FcConfigFileExists (dir
, url
+ 1);
1673 file
= FcConfigFileExists (0, url
);
1676 path
= FcConfigGetPath ();
1679 for (p
= path
; *p
; p
++)
1681 file
= FcConfigFileExists (*p
, url
);
1685 FcConfigFreePath (path
);
1692 * Manage the application-specific fonts
1696 FcConfigAppFontAddFile (FcConfig
*config
,
1697 const FcChar8
*file
)
1706 config
= FcConfigGetCurrent ();
1711 subdirs
= FcStrSetCreate ();
1715 set
= FcConfigGetFonts (config
, FcSetApplication
);
1718 set
= FcFontSetCreate ();
1721 FcStrSetDestroy (subdirs
);
1724 FcConfigSetFonts (config
, set
, FcSetApplication
);
1727 if (!FcFileScanConfig (set
, subdirs
, 0, config
->blanks
, file
, FcFalse
, config
))
1729 FcStrSetDestroy (subdirs
);
1732 if ((sublist
= FcStrListCreate (subdirs
)))
1734 while ((subdir
= FcStrListNext (sublist
)))
1736 FcConfigAppFontAddDir (config
, subdir
);
1738 FcStrListDone (sublist
);
1744 FcConfigAppFontAddDir (FcConfig
*config
,
1754 config
= FcConfigGetCurrent ();
1758 subdirs
= FcStrSetCreate ();
1762 set
= FcConfigGetFonts (config
, FcSetApplication
);
1765 set
= FcFontSetCreate ();
1768 FcStrSetDestroy (subdirs
);
1771 FcConfigSetFonts (config
, set
, FcSetApplication
);
1774 if (!FcDirScanConfig (set
, subdirs
, 0, config
->blanks
, dir
, FcFalse
, config
))
1776 FcStrSetDestroy (subdirs
);
1779 if ((sublist
= FcStrListCreate (subdirs
)))
1781 while ((subdir
= FcStrListNext (sublist
)))
1783 FcConfigAppFontAddDir (config
, subdir
);
1785 FcStrListDone (sublist
);
1791 FcConfigAppFontClear (FcConfig
*config
)
1795 config
= FcConfigGetCurrent ();
1800 FcConfigSetFonts (config
, 0, FcSetApplication
);
1804 * Manage filename-based font source selectors
1808 FcConfigGlobAdd (FcConfig
*config
,
1809 const FcChar8
*glob
,
1812 FcStrSet
*set
= accept
? config
->acceptGlobs
: config
->rejectGlobs
;
1814 return FcStrSetAdd (set
, glob
);
1818 FcConfigGlobMatch (const FcChar8
*glob
,
1819 const FcChar8
*string
)
1823 while ((c
= *glob
++))
1827 /* short circuit common case */
1830 /* short circuit another common case */
1831 if (strchr ((char *) glob
, '*') == 0)
1832 string
+= strlen ((char *) string
) - strlen ((char *) glob
);
1835 if (FcConfigGlobMatch (glob
, string
))
1841 if (*string
++ == '\0')
1850 return *string
== '\0';
1854 FcConfigGlobsMatch (const FcStrSet
*globs
,
1855 const FcChar8
*string
)
1859 for (i
= 0; i
< globs
->num
; i
++)
1860 if (FcConfigGlobMatch (FcStrSetGet(globs
, i
), string
))
1866 FcConfigAcceptFilename (FcConfig
*config
,
1867 const FcChar8
*filename
)
1869 if (FcConfigGlobsMatch (config
->acceptGlobs
, filename
))
1871 if (FcConfigGlobsMatch (config
->rejectGlobs
, filename
))
1877 * Manage font-pattern based font source selectors
1881 FcConfigPatternsAdd (FcConfig
*config
,
1885 FcFontSet
*set
= accept
? config
->acceptPatterns
: config
->rejectPatterns
;
1887 return FcFontSetAdd (set
, pattern
);
1891 FcConfigPatternsMatch (const FcFontSet
*patterns
,
1892 const FcPattern
*font
)
1896 for (i
= 0; i
< patterns
->nfont
; i
++)
1897 if (FcListPatternMatchAny (patterns
->fonts
[i
], font
))
1903 FcConfigAcceptFont (FcConfig
*config
,
1904 const FcPattern
*font
)
1906 if (FcConfigPatternsMatch (config
->acceptPatterns
, font
))
1908 if (FcConfigPatternsMatch (config
->rejectPatterns
, font
))