Minor
[platform/upstream/fontconfig.git] / src / fcdefault.c
1 /*
2  * fontconfig/src/fcdefault.c
3  *
4  * Copyright © 2001 Keith Packard
5  *
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 the author(s) not be used in
11  * advertising or publicity pertaining to distribution of the software without
12  * specific, written prior permission.  The authors make no
13  * representations about the suitability of this software for any purpose.  It
14  * is provided "as is" without express or implied warranty.
15  *
16  * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18  * EVENT SHALL THE AUTHOR(S) 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.
23  */
24
25 #include "fcint.h"
26 #include <string.h>
27
28 /* MT-safe */
29
30 static const struct {
31     FcObject    field;
32     FcBool      value;
33 } FcBoolDefaults[] = {
34     { FC_HINTING_OBJECT,           FcTrue       },  /* !FT_LOAD_NO_HINTING */
35     { FC_VERTICAL_LAYOUT_OBJECT,   FcFalse      },  /* FC_LOAD_VERTICAL_LAYOUT */
36     { FC_AUTOHINT_OBJECT,          FcFalse      },  /* FC_LOAD_FORCE_AUTOHINT */
37     { FC_GLOBAL_ADVANCE_OBJECT,    FcTrue       },  /* !FC_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH */
38     { FC_EMBEDDED_BITMAP_OBJECT,   FcTrue       },  /* !FC_LOAD_NO_BITMAP */
39     { FC_DECORATIVE_OBJECT,        FcFalse      },
40 };
41
42 #define NUM_FC_BOOL_DEFAULTS    (int) (sizeof FcBoolDefaults / sizeof FcBoolDefaults[0])
43
44 FcStrSet *default_langs;
45
46 FcStrSet *
47 FcGetDefaultLangs (void)
48 {
49     FcStrSet *result;
50 retry:
51     result = (FcStrSet *) fc_atomic_ptr_get (&default_langs);
52     if (!result)
53     {
54         char *langs;
55
56         result = FcStrSetCreate ();
57
58         langs = getenv ("FC_LANG");
59         if (!langs || !langs[0])
60             langs = getenv ("LC_ALL");
61         if (!langs || !langs[0])
62             langs = getenv ("LC_CTYPE");
63         if (!langs || !langs[0])
64             langs = getenv ("LANG");
65         if (langs && langs[0])
66         {
67             if (!FcStrSetAddLangs (result, langs))
68                 FcStrSetAdd (result, (const FcChar8 *) "en");
69         }
70         else
71             FcStrSetAdd (result, (const FcChar8 *) "en");
72
73         FcRefSetConst (&result->ref);
74         if (!fc_atomic_ptr_cmpexch (&default_langs, NULL, result)) {
75             FcRefInit (&result->ref, 1);
76             FcStrSetDestroy (result);
77             goto retry;
78         }
79     }
80
81     return result;
82 }
83
84 static FcChar8 *default_lang; /* MT-safe */
85
86 FcChar8 *
87 FcGetDefaultLang (void)
88 {
89     FcChar8 *lang;
90 retry:
91     lang = fc_atomic_ptr_get (&default_lang);
92     if (!lang)
93     {
94         FcStrSet *langs = FcGetDefaultLangs ();
95         lang = FcStrdup (langs->strs[0]);
96         FcStrSetDestroy (langs);
97
98         if (!fc_atomic_ptr_cmpexch (&default_lang, NULL, lang)) {
99             free (lang);
100             goto retry;
101         }
102     }
103
104     return lang;
105 }
106
107 void
108 FcDefaultFini (void)
109 {
110     FcChar8  *lang;
111     FcStrSet *langs;
112
113     lang = fc_atomic_ptr_get (&default_lang);
114     if (lang && fc_atomic_ptr_cmpexch (&default_lang, lang, NULL)) {
115         free (lang);
116     }
117
118     langs = fc_atomic_ptr_get (&default_langs);
119     if (langs && fc_atomic_ptr_cmpexch (&default_langs, langs, NULL)) {
120         FcRefInit (&langs->ref, 1);
121         FcStrSetDestroy (langs);
122     }
123 }
124
125 void
126 FcDefaultSubstitute (FcPattern *pattern)
127 {
128     FcValue v, namelang, v2;
129     int     i;
130
131     if (FcPatternObjectGet (pattern, FC_WEIGHT_OBJECT, 0, &v) == FcResultNoMatch )
132         FcPatternObjectAddInteger (pattern, FC_WEIGHT_OBJECT, FC_WEIGHT_MEDIUM);
133
134     if (FcPatternObjectGet (pattern, FC_SLANT_OBJECT, 0, &v) == FcResultNoMatch)
135         FcPatternObjectAddInteger (pattern, FC_SLANT_OBJECT, FC_SLANT_ROMAN);
136
137     if (FcPatternObjectGet (pattern, FC_WIDTH_OBJECT, 0, &v) == FcResultNoMatch)
138         FcPatternObjectAddInteger (pattern, FC_WIDTH_OBJECT, FC_WIDTH_NORMAL);
139
140     for (i = 0; i < NUM_FC_BOOL_DEFAULTS; i++)
141         if (FcPatternObjectGet (pattern, FcBoolDefaults[i].field, 0, &v) == FcResultNoMatch)
142             FcPatternObjectAddBool (pattern, FcBoolDefaults[i].field, FcBoolDefaults[i].value);
143
144     if (FcPatternObjectGet (pattern, FC_PIXEL_SIZE_OBJECT, 0, &v) == FcResultNoMatch)
145     {
146         double  dpi, size, scale;
147
148         if (FcPatternObjectGetDouble (pattern, FC_SIZE_OBJECT, 0, &size) != FcResultMatch)
149         {
150             size = 12.0;
151             (void) FcPatternObjectDel (pattern, FC_SIZE_OBJECT);
152             FcPatternObjectAddDouble (pattern, FC_SIZE_OBJECT, size);
153         }
154         if (FcPatternObjectGetDouble (pattern, FC_SCALE_OBJECT, 0, &scale) != FcResultMatch)
155         {
156             scale = 1.0;
157             (void) FcPatternObjectDel (pattern, FC_SCALE_OBJECT);
158             FcPatternObjectAddDouble (pattern, FC_SCALE_OBJECT, scale);
159         }
160         size *= scale;
161         if (FcPatternObjectGetDouble (pattern, FC_DPI_OBJECT, 0, &dpi) != FcResultMatch)
162         {
163             dpi = 75.0;
164             (void) FcPatternObjectDel (pattern, FC_DPI_OBJECT);
165             FcPatternObjectAddDouble (pattern, FC_DPI_OBJECT, dpi);
166         }
167         size *= dpi / 72.0;
168         FcPatternObjectAddDouble (pattern, FC_PIXEL_SIZE_OBJECT, size);
169     }
170
171     if (FcPatternObjectGet (pattern, FC_FONTVERSION_OBJECT, 0, &v) == FcResultNoMatch)
172     {
173         FcPatternObjectAddInteger (pattern, FC_FONTVERSION_OBJECT, 0x7fffffff);
174     }
175
176     if (FcPatternObjectGet (pattern, FC_HINT_STYLE_OBJECT, 0, &v) == FcResultNoMatch)
177     {
178         FcPatternObjectAddInteger (pattern, FC_HINT_STYLE_OBJECT, FC_HINT_FULL);
179     }
180     if (FcPatternObjectGet (pattern, FC_NAMELANG_OBJECT, 0, &v) == FcResultNoMatch)
181     {
182         FcPatternObjectAddString (pattern, FC_NAMELANG_OBJECT, FcGetDefaultLang ());
183     }
184     /* shouldn't be failed. */
185     FcPatternObjectGet (pattern, FC_NAMELANG_OBJECT, 0, &namelang);
186     /* Add a fallback to ensure the english name when the requested language
187      * isn't available. this would helps for the fonts that have non-English
188      * name at the beginning.
189      */
190     /* Set "en-us" instead of "en" to avoid giving higher score to "en".
191      * This is a hack for the case that the orth is not like ll-cc, because,
192      * if no namelang isn't explicitly set, it will has something like ll-cc
193      * according to current locale. which may causes FcLangDifferentTerritory
194      * at FcLangCompare(). thus, the English name is selected so that
195      * exact matched "en" has higher score than ll-cc.
196      */
197     v2.type = FcTypeString;
198     v2.u.s = FcStrdup ("en-us");
199     if (FcPatternObjectGet (pattern, FC_FAMILYLANG_OBJECT, 0, &v) == FcResultNoMatch)
200     {
201         FcPatternObjectAdd (pattern, FC_FAMILYLANG_OBJECT, namelang, FcTrue);
202         FcPatternObjectAddWithBinding (pattern, FC_FAMILYLANG_OBJECT, v2, FcValueBindingWeak, FcTrue);
203     }
204     if (FcPatternObjectGet (pattern, FC_STYLELANG_OBJECT, 0, &v) == FcResultNoMatch)
205     {
206         FcPatternObjectAdd (pattern, FC_STYLELANG_OBJECT, namelang, FcTrue);
207         FcPatternObjectAddWithBinding (pattern, FC_STYLELANG_OBJECT, v2, FcValueBindingWeak, FcTrue);
208     }
209     if (FcPatternObjectGet (pattern, FC_FULLNAMELANG_OBJECT, 0, &v) == FcResultNoMatch)
210     {
211         FcPatternObjectAdd (pattern, FC_FULLNAMELANG_OBJECT, namelang, FcTrue);
212         FcPatternObjectAddWithBinding (pattern, FC_FULLNAMELANG_OBJECT, v2, FcValueBindingWeak, FcTrue);
213     }
214     FcFree (v2.u.s);
215 }
216 #define __fcdefault__
217 #include "fcaliastail.h"
218 #undef __fcdefault__