]> git.wh0rd.org - fontconfig.git/blob - src/fcdefault.c
8445cf031ce6d459ec1b51cbd0fc8c03dcb84968
[fontconfig.git] / src / fcdefault.c
1 /*
2 * $XFree86: xc/lib/fontconfig/src/fcdefault.c,v 1.2 2002/07/09 22:08:14 keithp Exp $
3 *
4 * Copyright © 2001 Keith Packard, member of The XFree86 Project, Inc.
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 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 (sizeof FcBoolDefaults / sizeof FcBoolDefaults[0])
39
40 void
41 FcDefaultSubstitute (FcPattern *pattern)
42 {
43 FcValue v;
44 int i;
45
46 if (FcPatternGet (pattern, FC_STYLE, 0, &v) == FcResultNoMatch)
47 {
48 if (FcPatternGet (pattern, FC_WEIGHT, 0, &v) == FcResultNoMatch )
49 {
50 FcPatternAddInteger (pattern, FC_WEIGHT, FC_WEIGHT_MEDIUM);
51 }
52 if (FcPatternGet (pattern, FC_SLANT, 0, &v) == FcResultNoMatch)
53 {
54 FcPatternAddInteger (pattern, FC_SLANT, FC_SLANT_ROMAN);
55 }
56 }
57
58 for (i = 0; i < NUM_FC_BOOL_DEFAULTS; i++)
59 if (FcPatternGet (pattern, FcBoolDefaults[i].field, 0, &v) == FcResultNoMatch)
60 FcPatternAddBool (pattern, FcBoolDefaults[i].field, FcBoolDefaults[i].value);
61
62 if (FcPatternGet (pattern, FC_PIXEL_SIZE, 0, &v) == FcResultNoMatch)
63 {
64 double dpi, size, scale;
65
66 if (FcPatternGetDouble (pattern, FC_SIZE, 0, &size) != FcResultMatch)
67 {
68 size = 12.0;
69 (void) FcPatternDel (pattern, FC_SIZE);
70 FcPatternAddDouble (pattern, FC_SIZE, size);
71 }
72 if (FcPatternGetDouble (pattern, FC_SCALE, 0, &scale) != FcResultMatch)
73 {
74 scale = 1.0;
75 (void) FcPatternDel (pattern, FC_SCALE);
76 FcPatternAddDouble (pattern, FC_SCALE, scale);
77 }
78 size *= scale;
79 if (FcPatternGetDouble (pattern, FC_DPI, 0, &dpi) != FcResultMatch)
80 {
81 dpi = 75.0;
82 (void) FcPatternDel (pattern, FC_DPI);
83 FcPatternAddDouble (pattern, FC_DPI, dpi);
84 }
85 size *= dpi / 72.0;
86 FcPatternAddDouble (pattern, FC_PIXEL_SIZE, size);
87 }
88
89 if (FcPatternGet (pattern, FC_LANG, 0, &v) == FcResultNoMatch)
90 {
91 char *lang;
92 char *territory;
93 char *after;
94 int lang_len, territory_len;
95 char lang_local[128];
96 char *ctype = setlocale (LC_CTYPE, NULL);
97
98 /*
99 * Check if setlocale (LC_ALL, "") has been called
100 */
101 if (!ctype || !strcmp (ctype, "C"))
102 {
103 ctype = getenv ("LC_ALL");
104 if (!ctype)
105 {
106 ctype = getenv ("LC_CTYPE");
107 if (!ctype)
108 ctype = getenv ("LANG");
109 }
110 }
111 if (ctype)
112 {
113 lang = ctype;
114 territory = strchr (ctype, '_');
115 if (territory)
116 {
117 lang_len = territory - lang;
118 territory = territory + 1;
119 after = strchr (territory, '.');
120 if (!after)
121 {
122 after = strchr (territory, '@');
123 if (!after)
124 after = territory + strlen (territory);
125 }
126 territory_len = after - territory;
127 if (lang_len + 1 + territory_len + 1 <= sizeof (lang_local))
128 {
129 strncpy (lang_local, lang, lang_len);
130 lang_local[lang_len] = '-';
131 strncpy (lang_local + lang_len + 1, territory, territory_len);
132 lang_local[lang_len + 1 + territory_len] = '\0';
133 FcPatternAddString (pattern, FC_LANG, (FcChar8 *) lang_local);
134 }
135 }
136 else
137 FcPatternAddString (pattern, FC_LANG, (FcChar8 *) lang);
138 }
139 }
140 if (FcPatternGet (pattern, FC_FONTVERSION, 0, &v) == FcResultNoMatch)
141 {
142 FcPatternAddInteger (pattern, FC_FONTVERSION, 0x7fffffff);
143 }
144 }