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