]> git.wh0rd.org - fontconfig.git/blob - src/fcdefault.c
Support localized font family and style names. This has been reported to
[fontconfig.git] / src / fcdefault.c
1 /*
2 * $RCSId: xc/lib/fontconfig/src/fcdefault.c,v 1.2 2002/07/09 22:08:14 keithp Exp $
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 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.
15 *
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.
23 */
24
25 #include "fcint.h"
26 #include <locale.h>
27
28 static struct {
29 const char *field;
30 FcBool value;
31 } FcBoolDefaults[] = {
32 { FC_HINTING, FcTrue }, /* !FT_LOAD_NO_HINTING */
33 { FC_VERTICAL_LAYOUT, FcFalse }, /* FC_LOAD_VERTICAL_LAYOUT */
34 { FC_AUTOHINT, FcFalse }, /* FC_LOAD_FORCE_AUTOHINT */
35 { FC_GLOBAL_ADVANCE, FcTrue }, /* !FC_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH */
36 };
37
38 #define NUM_FC_BOOL_DEFAULTS (int) (sizeof FcBoolDefaults / sizeof FcBoolDefaults[0])
39
40 FcChar8 *
41 FcGetDefaultLang (void)
42 {
43 static char lang_local [128] = {0};
44 char *ctype;
45 char *territory;
46 char *after;
47 int lang_len, territory_len;
48
49 if (lang_local [0])
50 return (FcChar8 *) lang_local;
51
52 ctype = setlocale (LC_CTYPE, NULL);
53
54 /*
55 * Check if setlocale (LC_ALL, "") has been called
56 */
57 if (!ctype || !strcmp (ctype, "C"))
58 {
59 ctype = getenv ("LC_ALL");
60 if (!ctype)
61 {
62 ctype = getenv ("LC_CTYPE");
63 if (!ctype)
64 ctype = getenv ("LANG");
65 }
66 }
67
68 /* ignore missing or empty ctype */
69 if (ctype && *ctype != '\0')
70 {
71 territory = strchr (ctype, '_');
72 if (territory)
73 {
74 lang_len = territory - ctype;
75 territory = territory + 1;
76 after = strchr (territory, '.');
77 if (!after)
78 {
79 after = strchr (territory, '@');
80 if (!after)
81 after = territory + strlen (territory);
82 }
83 territory_len = after - territory;
84 if (lang_len + 1 + territory_len + 1 <= (int) sizeof (lang_local))
85 {
86 strncpy (lang_local, ctype, lang_len);
87 lang_local[lang_len] = '-';
88 strncpy (lang_local + lang_len + 1, territory, territory_len);
89 lang_local[lang_len + 1 + territory_len] = '\0';
90 }
91 }
92 }
93
94 /* set default lang to en */
95 if (!lang_local [0])
96 strcpy (lang_local, "en");
97
98 return (FcChar8 *) lang_local;
99 }
100
101 void
102 FcDefaultSubstitute (FcPattern *pattern)
103 {
104 FcValue v;
105 int i;
106
107 if (FcPatternGet (pattern, FC_STYLE, 0, &v) == FcResultNoMatch)
108 {
109 if (FcPatternGet (pattern, FC_WEIGHT, 0, &v) == FcResultNoMatch )
110 {
111 FcPatternAddInteger (pattern, FC_WEIGHT, FC_WEIGHT_MEDIUM);
112 }
113 if (FcPatternGet (pattern, FC_SLANT, 0, &v) == FcResultNoMatch)
114 {
115 FcPatternAddInteger (pattern, FC_SLANT, FC_SLANT_ROMAN);
116 }
117 }
118
119 if (FcPatternGet (pattern, FC_WIDTH, 0, &v) == FcResultNoMatch)
120 FcPatternAddInteger (pattern, FC_WIDTH, FC_WIDTH_NORMAL);
121
122 for (i = 0; i < NUM_FC_BOOL_DEFAULTS; i++)
123 if (FcPatternGet (pattern, FcBoolDefaults[i].field, 0, &v) == FcResultNoMatch)
124 FcPatternAddBool (pattern, FcBoolDefaults[i].field, FcBoolDefaults[i].value);
125
126 if (FcPatternGet (pattern, FC_PIXEL_SIZE, 0, &v) == FcResultNoMatch)
127 {
128 double dpi, size, scale;
129
130 if (FcPatternGetDouble (pattern, FC_SIZE, 0, &size) != FcResultMatch)
131 {
132 size = 12.0;
133 (void) FcPatternDel (pattern, FC_SIZE);
134 FcPatternAddDouble (pattern, FC_SIZE, size);
135 }
136 if (FcPatternGetDouble (pattern, FC_SCALE, 0, &scale) != FcResultMatch)
137 {
138 scale = 1.0;
139 (void) FcPatternDel (pattern, FC_SCALE);
140 FcPatternAddDouble (pattern, FC_SCALE, scale);
141 }
142 size *= scale;
143 if (FcPatternGetDouble (pattern, FC_DPI, 0, &dpi) != FcResultMatch)
144 {
145 dpi = 75.0;
146 (void) FcPatternDel (pattern, FC_DPI);
147 FcPatternAddDouble (pattern, FC_DPI, dpi);
148 }
149 size *= dpi / 72.0;
150 FcPatternAddDouble (pattern, FC_PIXEL_SIZE, size);
151 }
152
153 if (FcPatternGet (pattern, FC_LANG, 0, &v) == FcResultNoMatch)
154 {
155 FcPatternAddString (pattern, FC_LANG, FcGetDefaultLang ());
156 }
157 if (FcPatternGet (pattern, FC_FONTVERSION, 0, &v) == FcResultNoMatch)
158 {
159 FcPatternAddInteger (pattern, FC_FONTVERSION, 0x7fffffff);
160 }
161
162 if (FcPatternGet (pattern, FC_HINT_STYLE, 0, &v) == FcResultNoMatch)
163 {
164 FcPatternAddInteger (pattern, FC_HINT_STYLE, FC_HINT_FULL);
165 }
166 }