]> git.wh0rd.org - fontconfig.git/blame - src/fcdefault.c
Bug 44826 - <alias> must contain only a single <family>
[fontconfig.git] / src / fcdefault.c
CommitLineData
24330d27 1/*
317b8492 2 * fontconfig/src/fcdefault.c
24330d27 3 *
46b51147 4 * Copyright © 2001 Keith Packard
24330d27
KP
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
5aaf466d 10 * documentation, and that the name of the author(s) not be used in
24330d27 11 * advertising or publicity pertaining to distribution of the software without
5aaf466d 12 * specific, written prior permission. The authors make no
24330d27
KP
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
3074a73b 16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
24330d27 17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
3074a73b 18 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24330d27
KP
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"
e709ddfa 26#include <locale.h>
24330d27 27
392fa276 28static const struct {
7ce19673 29 FcObject field;
24330d27
KP
30 FcBool value;
31} FcBoolDefaults[] = {
7ce19673
KP
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 */
c2c6976d 37 { FC_DECORATIVE_OBJECT, FcFalse },
24330d27
KP
38};
39
67accef4 40#define NUM_FC_BOOL_DEFAULTS (int) (sizeof FcBoolDefaults / sizeof FcBoolDefaults[0])
24330d27 41
90442681
PL
42FcChar8 *
43FcGetDefaultLang (void)
44{
45 static char lang_local [128] = {0};
46 char *ctype;
47 char *territory;
48 char *after;
49 int lang_len, territory_len;
50
51 if (lang_local [0])
52 return (FcChar8 *) lang_local;
53
54 ctype = setlocale (LC_CTYPE, NULL);
55
56 /*
57 * Check if setlocale (LC_ALL, "") has been called
58 */
59 if (!ctype || !strcmp (ctype, "C"))
60 {
61 ctype = getenv ("LC_ALL");
62 if (!ctype)
63 {
64 ctype = getenv ("LC_CTYPE");
65 if (!ctype)
66 ctype = getenv ("LANG");
67 }
68 }
69
70 /* ignore missing or empty ctype */
71 if (ctype && *ctype != '\0')
72 {
73 territory = strchr (ctype, '_');
74 if (territory)
75 {
76 lang_len = territory - ctype;
77 territory = territory + 1;
78 after = strchr (territory, '.');
79 if (!after)
80 {
81 after = strchr (territory, '@');
82 if (!after)
83 after = territory + strlen (territory);
84 }
85 territory_len = after - territory;
86 if (lang_len + 1 + territory_len + 1 <= (int) sizeof (lang_local))
87 {
88 strncpy (lang_local, ctype, lang_len);
89 lang_local[lang_len] = '-';
90 strncpy (lang_local + lang_len + 1, territory, territory_len);
91 lang_local[lang_len + 1 + territory_len] = '\0';
92 }
93 }
5d2f7a9d
KP
94 else
95 {
96 after = strchr (ctype, '.');
97 if (!after)
98 {
99 after = strchr (ctype, '@');
100 if (!after)
101 after = ctype + strlen (ctype);
102 }
103 lang_len = after - ctype;
104 if (lang_len + 1 <= (int) sizeof (lang_local))
105 {
106 strncpy (lang_local, ctype, lang_len);
107 lang_local[lang_len] = '\0';
108 }
109 }
90442681
PL
110 }
111
112 /* set default lang to en */
113 if (!lang_local [0])
114 strcpy (lang_local, "en");
115
116 return (FcChar8 *) lang_local;
117}
118
24330d27
KP
119void
120FcDefaultSubstitute (FcPattern *pattern)
121{
122 FcValue v;
123 int i;
124
7f46cdbb
KP
125 if (FcPatternObjectGet (pattern, FC_WEIGHT_OBJECT, 0, &v) == FcResultNoMatch )
126 FcPatternObjectAddInteger (pattern, FC_WEIGHT_OBJECT, FC_WEIGHT_MEDIUM);
127
128 if (FcPatternObjectGet (pattern, FC_SLANT_OBJECT, 0, &v) == FcResultNoMatch)
129 FcPatternObjectAddInteger (pattern, FC_SLANT_OBJECT, FC_SLANT_ROMAN);
24330d27 130
7ce19673
KP
131 if (FcPatternObjectGet (pattern, FC_WIDTH_OBJECT, 0, &v) == FcResultNoMatch)
132 FcPatternObjectAddInteger (pattern, FC_WIDTH_OBJECT, FC_WIDTH_NORMAL);
81fa16c3 133
24330d27 134 for (i = 0; i < NUM_FC_BOOL_DEFAULTS; i++)
7ce19673
KP
135 if (FcPatternObjectGet (pattern, FcBoolDefaults[i].field, 0, &v) == FcResultNoMatch)
136 FcPatternObjectAddBool (pattern, FcBoolDefaults[i].field, FcBoolDefaults[i].value);
594dcef0 137
7ce19673 138 if (FcPatternObjectGet (pattern, FC_PIXEL_SIZE_OBJECT, 0, &v) == FcResultNoMatch)
24330d27
KP
139 {
140 double dpi, size, scale;
141
7ce19673 142 if (FcPatternObjectGetDouble (pattern, FC_SIZE_OBJECT, 0, &size) != FcResultMatch)
24330d27
KP
143 {
144 size = 12.0;
7ce19673
KP
145 (void) FcPatternObjectDel (pattern, FC_SIZE_OBJECT);
146 FcPatternObjectAddDouble (pattern, FC_SIZE_OBJECT, size);
24330d27 147 }
7ce19673 148 if (FcPatternObjectGetDouble (pattern, FC_SCALE_OBJECT, 0, &scale) != FcResultMatch)
24330d27
KP
149 {
150 scale = 1.0;
7ce19673
KP
151 (void) FcPatternObjectDel (pattern, FC_SCALE_OBJECT);
152 FcPatternObjectAddDouble (pattern, FC_SCALE_OBJECT, scale);
24330d27
KP
153 }
154 size *= scale;
7ce19673 155 if (FcPatternObjectGetDouble (pattern, FC_DPI_OBJECT, 0, &dpi) != FcResultMatch)
24330d27
KP
156 {
157 dpi = 75.0;
7ce19673
KP
158 (void) FcPatternObjectDel (pattern, FC_DPI_OBJECT);
159 FcPatternObjectAddDouble (pattern, FC_DPI_OBJECT, dpi);
24330d27
KP
160 }
161 size *= dpi / 72.0;
7ce19673 162 FcPatternObjectAddDouble (pattern, FC_PIXEL_SIZE_OBJECT, size);
24330d27 163 }
e709ddfa 164
7ce19673 165 if (FcPatternObjectGet (pattern, FC_LANG_OBJECT, 0, &v) == FcResultNoMatch)
e709ddfa 166 {
7ce19673 167 FcPatternObjectAddString (pattern, FC_LANG_OBJECT, FcGetDefaultLang ());
e709ddfa 168 }
7ce19673 169 if (FcPatternObjectGet (pattern, FC_FONTVERSION_OBJECT, 0, &v) == FcResultNoMatch)
a342e87d 170 {
7ce19673 171 FcPatternObjectAddInteger (pattern, FC_FONTVERSION_OBJECT, 0x7fffffff);
a342e87d 172 }
f077d662 173
7ce19673 174 if (FcPatternObjectGet (pattern, FC_HINT_STYLE_OBJECT, 0, &v) == FcResultNoMatch)
f077d662 175 {
7ce19673 176 FcPatternObjectAddInteger (pattern, FC_HINT_STYLE_OBJECT, FC_HINT_FULL);
f077d662 177 }
24330d27 178}
23816bf9
KP
179#define __fcdefault__
180#include "fcaliastail.h"
181#undef __fcdefault__