fcdefault: no need to set FC_LANG in FcDefaultSubstitute() anymore
[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 static const struct {
29     FcObject    field;
30     FcBool      value;
31 } FcBoolDefaults[] = {
32     { FC_HINTING_OBJECT,           FcTrue       },  /* !FT_LOAD_NO_HINTING */
33     { FC_VERTICAL_LAYOUT_OBJECT,   FcFalse      },  /* FC_LOAD_VERTICAL_LAYOUT */
34     { FC_AUTOHINT_OBJECT,          FcFalse      },  /* FC_LOAD_FORCE_AUTOHINT */
35     { FC_GLOBAL_ADVANCE_OBJECT,    FcTrue       },  /* !FC_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH */
36     { FC_EMBEDDED_BITMAP_OBJECT,   FcTrue       },  /* !FC_LOAD_NO_BITMAP */
37     { FC_DECORATIVE_OBJECT,        FcFalse      },
38 };
39
40 #define NUM_FC_BOOL_DEFAULTS    (int) (sizeof FcBoolDefaults / sizeof FcBoolDefaults[0])
41
42 FcStrSet *
43 FcGetDefaultLangs (void)
44 {
45     FcStrSet *result = FcStrSetCreate ();
46     char *langs;
47
48     langs = getenv ("FC_LANG");
49     if (!langs || !langs[0])
50         langs = getenv ("LC_ALL");
51     if (!langs || !langs[0])
52         langs = getenv ("LC_CTYPE");
53     if (!langs || !langs[0])
54         langs = getenv ("LANG");
55     if (langs && langs[0])
56     {
57         if (!FcStrSetAddLangs (result, langs))
58             FcStrSetAdd (result, (const FcChar8 *) "en");
59     }
60     else
61         FcStrSetAdd (result, (const FcChar8 *) "en");
62
63     return result;
64 }
65
66 FcChar8 *
67 FcGetDefaultLang (void)
68 {
69     static FcChar8 lang_local[128] = {0};
70     FcStrSet *langs;
71
72     if (!lang_local[0])
73     {
74         langs = FcGetDefaultLangs ();
75         strncpy ((char *)lang_local, (const char *)langs->strs[0], 127);
76         lang_local[127] = 0;
77         FcStrSetDestroy (langs);
78     }
79
80     return lang_local;
81 }
82
83 void
84 FcDefaultSubstitute (FcPattern *pattern)
85 {
86     FcValue v, namelang, v2;
87     int     i;
88
89     if (FcPatternObjectGet (pattern, FC_WEIGHT_OBJECT, 0, &v) == FcResultNoMatch )
90         FcPatternObjectAddInteger (pattern, FC_WEIGHT_OBJECT, FC_WEIGHT_MEDIUM);
91
92     if (FcPatternObjectGet (pattern, FC_SLANT_OBJECT, 0, &v) == FcResultNoMatch)
93         FcPatternObjectAddInteger (pattern, FC_SLANT_OBJECT, FC_SLANT_ROMAN);
94
95     if (FcPatternObjectGet (pattern, FC_WIDTH_OBJECT, 0, &v) == FcResultNoMatch)
96         FcPatternObjectAddInteger (pattern, FC_WIDTH_OBJECT, FC_WIDTH_NORMAL);
97
98     for (i = 0; i < NUM_FC_BOOL_DEFAULTS; i++)
99         if (FcPatternObjectGet (pattern, FcBoolDefaults[i].field, 0, &v) == FcResultNoMatch)
100             FcPatternObjectAddBool (pattern, FcBoolDefaults[i].field, FcBoolDefaults[i].value);
101
102     if (FcPatternObjectGet (pattern, FC_PIXEL_SIZE_OBJECT, 0, &v) == FcResultNoMatch)
103     {
104         double  dpi, size, scale;
105
106         if (FcPatternObjectGetDouble (pattern, FC_SIZE_OBJECT, 0, &size) != FcResultMatch)
107         {
108             size = 12.0;
109             (void) FcPatternObjectDel (pattern, FC_SIZE_OBJECT);
110             FcPatternObjectAddDouble (pattern, FC_SIZE_OBJECT, size);
111         }
112         if (FcPatternObjectGetDouble (pattern, FC_SCALE_OBJECT, 0, &scale) != FcResultMatch)
113         {
114             scale = 1.0;
115             (void) FcPatternObjectDel (pattern, FC_SCALE_OBJECT);
116             FcPatternObjectAddDouble (pattern, FC_SCALE_OBJECT, scale);
117         }
118         size *= scale;
119         if (FcPatternObjectGetDouble (pattern, FC_DPI_OBJECT, 0, &dpi) != FcResultMatch)
120         {
121             dpi = 75.0;
122             (void) FcPatternObjectDel (pattern, FC_DPI_OBJECT);
123             FcPatternObjectAddDouble (pattern, FC_DPI_OBJECT, dpi);
124         }
125         size *= dpi / 72.0;
126         FcPatternObjectAddDouble (pattern, FC_PIXEL_SIZE_OBJECT, size);
127     }
128
129     if (FcPatternObjectGet (pattern, FC_FONTVERSION_OBJECT, 0, &v) == FcResultNoMatch)
130     {
131         FcPatternObjectAddInteger (pattern, FC_FONTVERSION_OBJECT, 0x7fffffff);
132     }
133
134     if (FcPatternObjectGet (pattern, FC_HINT_STYLE_OBJECT, 0, &v) == FcResultNoMatch)
135     {
136         FcPatternObjectAddInteger (pattern, FC_HINT_STYLE_OBJECT, FC_HINT_FULL);
137     }
138     if (FcPatternObjectGet (pattern, FC_NAMELANG_OBJECT, 0, &v) == FcResultNoMatch)
139     {
140         FcPatternObjectAddString (pattern, FC_NAMELANG_OBJECT, FcGetDefaultLang ());
141     }
142     /* shouldn't be failed. */
143     FcPatternObjectGet (pattern, FC_NAMELANG_OBJECT, 0, &namelang);
144     /* Add a fallback to ensure the english name when the requested language
145      * isn't available. this would helps for the fonts that have non-English
146      * name at the beginning.
147      */
148     /* Set "en-us" instead of "en" to avoid giving higher score to "en".
149      * This is a hack for the case that the orth is not like ll-cc, because,
150      * if no namelang isn't explicitly set, it will has something like ll-cc
151      * according to current locale. which may causes FcLangDifferentTerritory
152      * at FcLangCompare(). thus, the English name is selected so that
153      * exact matched "en" has higher score than ll-cc.
154      */
155     v2.type = FcTypeString;
156     v2.u.s = FcSharedStr ((FcChar8 *)"en-us");
157     if (FcPatternObjectGet (pattern, FC_FAMILYLANG_OBJECT, 0, &v) == FcResultNoMatch)
158     {
159         FcPatternObjectAdd (pattern, FC_FAMILYLANG_OBJECT, namelang, FcTrue);
160         FcPatternObjectAddWithBinding (pattern, FC_FAMILYLANG_OBJECT, v2, FcValueBindingWeak, FcTrue);
161     }
162     if (FcPatternObjectGet (pattern, FC_STYLELANG_OBJECT, 0, &v) == FcResultNoMatch)
163     {
164         FcPatternObjectAdd (pattern, FC_STYLELANG_OBJECT, namelang, FcTrue);
165         FcPatternObjectAddWithBinding (pattern, FC_STYLELANG_OBJECT, v2, FcValueBindingWeak, FcTrue);
166     }
167     if (FcPatternObjectGet (pattern, FC_FULLNAMELANG_OBJECT, 0, &v) == FcResultNoMatch)
168     {
169         FcPatternObjectAdd (pattern, FC_FULLNAMELANG_OBJECT, namelang, FcTrue);
170         FcPatternObjectAddWithBinding (pattern, FC_FULLNAMELANG_OBJECT, v2, FcValueBindingWeak, FcTrue);
171     }
172     FcSharedStrFree (v2.u.s);
173 }
174 #define __fcdefault__
175 #include "fcaliastail.h"
176 #undef __fcdefault__