Bug 32853 - Export API to get the default language
[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)
50         langs = getenv ("LC_ALL");
51     if (!langs)
52         langs = getenv ("LC_CTYPE");
53     if (!langs)
54         langs = getenv ("LANG");
55     if (langs)
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_LANG_OBJECT, 0, &v) == FcResultNoMatch)
130     {
131         FcPatternObjectAddString (pattern, FC_LANG_OBJECT, FcGetDefaultLang ());
132     }
133     if (FcPatternObjectGet (pattern, FC_FONTVERSION_OBJECT, 0, &v) == FcResultNoMatch)
134     {
135         FcPatternObjectAddInteger (pattern, FC_FONTVERSION_OBJECT, 0x7fffffff);
136     }
137
138     if (FcPatternObjectGet (pattern, FC_HINT_STYLE_OBJECT, 0, &v) == FcResultNoMatch)
139     {
140         FcPatternObjectAddInteger (pattern, FC_HINT_STYLE_OBJECT, FC_HINT_FULL);
141     }
142     if (FcPatternObjectGet (pattern, FC_NAMELANG_OBJECT, 0, &v) == FcResultNoMatch)
143     {
144         FcPatternObjectAddString (pattern, FC_NAMELANG_OBJECT, FcGetDefaultLang ());
145     }
146     /* shouldn't be failed. */
147     FcPatternObjectGet (pattern, FC_NAMELANG_OBJECT, 0, &namelang);
148     /* Add a fallback to ensure the english name when the requested language
149      * isn't available. this would helps for the fonts that have non-English
150      * name at the beginning.
151      */
152     /* Set "en-us" instead of "en" to avoid giving higher score to "en".
153      * This is a hack for the case that the orth is not like ll-cc, because,
154      * if no namelang isn't explicitly set, it will has something like ll-cc
155      * according to current locale. which may causes FcLangDifferentTerritory
156      * at FcLangCompare(). thus, the English name is selected so that
157      * exact matched "en" has higher score than ll-cc.
158      */
159     v2.type = FcTypeString;
160     v2.u.s = FcSharedStr ((FcChar8 *)"en-us");
161     if (FcPatternObjectGet (pattern, FC_FAMILYLANG_OBJECT, 0, &v) == FcResultNoMatch)
162     {
163         FcPatternObjectAdd (pattern, FC_FAMILYLANG_OBJECT, namelang, FcTrue);
164         FcPatternObjectAddWithBinding (pattern, FC_FAMILYLANG_OBJECT, v2, FcValueBindingWeak, FcTrue);
165     }
166     if (FcPatternObjectGet (pattern, FC_STYLELANG_OBJECT, 0, &v) == FcResultNoMatch)
167     {
168         FcPatternObjectAdd (pattern, FC_STYLELANG_OBJECT, namelang, FcTrue);
169         FcPatternObjectAddWithBinding (pattern, FC_STYLELANG_OBJECT, v2, FcValueBindingWeak, FcTrue);
170     }
171     if (FcPatternObjectGet (pattern, FC_FULLNAMELANG_OBJECT, 0, &v) == FcResultNoMatch)
172     {
173         FcPatternObjectAdd (pattern, FC_FULLNAMELANG_OBJECT, namelang, FcTrue);
174         FcPatternObjectAddWithBinding (pattern, FC_FULLNAMELANG_OBJECT, v2, FcValueBindingWeak, FcTrue);
175     }
176     FcSharedStrFree (v2.u.s);
177 }
178 #define __fcdefault__
179 #include "fcaliastail.h"
180 #undef __fcdefault__