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 <limits.h>
27#include <string.h>
28
29/* MT-safe */
30
31static const struct {
32    FcObject	field;
33    FcBool	value;
34} FcBoolDefaults[] = {
35    { FC_HINTING_OBJECT,	   FcTrue	},  /* !FT_LOAD_NO_HINTING */
36    { FC_VERTICAL_LAYOUT_OBJECT,   FcFalse	},  /* FC_LOAD_VERTICAL_LAYOUT */
37    { FC_AUTOHINT_OBJECT,	   FcFalse	},  /* FC_LOAD_FORCE_AUTOHINT */
38    { FC_GLOBAL_ADVANCE_OBJECT,    FcTrue	},  /* !FC_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH */
39    { FC_EMBEDDED_BITMAP_OBJECT,   FcTrue 	},  /* !FC_LOAD_NO_BITMAP */
40    { FC_DECORATIVE_OBJECT,	   FcFalse	},
41    { FC_SYMBOL_OBJECT,		   FcFalse	},
42    { FC_VARIABLE_OBJECT,	   FcFalse	},
43};
44
45#define NUM_FC_BOOL_DEFAULTS	(int) (sizeof FcBoolDefaults / sizeof FcBoolDefaults[0])
46
47FcStrSet *default_langs;
48
49FcStrSet *
50FcGetDefaultLangs (void)
51{
52    FcStrSet *result;
53retry:
54    result = (FcStrSet *) fc_atomic_ptr_get (&default_langs);
55    if (!result)
56    {
57	char *langs;
58
59	result = FcStrSetCreate ();
60
61	langs = getenv ("FC_LANG");
62	if (!langs || !langs[0])
63	    langs = getenv ("LC_ALL");
64	if (!langs || !langs[0])
65        {
66            langs = getenv ("LC_CTYPE");
67            // On some macOS systems, LC_CTYPE is set to "UTF-8", which doesn't
68            // give any languge information. In this case, ignore LC_CTYPE and
69            // continue the search with LANG.
70            if (langs && (FcStrCmpIgnoreCase((const FcChar8 *) langs,
71                                             (const FcChar8 *)"UTF-8") == 0))
72            {
73                langs = NULL;
74            }
75        }
76	if (!langs || !langs[0])
77	    langs = getenv ("LANG");
78	if (langs && langs[0])
79	{
80	    if (!FcStrSetAddLangs (result, langs))
81		FcStrSetAdd (result, (const FcChar8 *) "en");
82	}
83	else
84	    FcStrSetAdd (result, (const FcChar8 *) "en");
85
86	FcRefSetConst (&result->ref);
87	if (!fc_atomic_ptr_cmpexch (&default_langs, NULL, result)) {
88	    FcRefInit (&result->ref, 1);
89	    FcStrSetDestroy (result);
90	    goto retry;
91	}
92    }
93
94    return result;
95}
96
97static FcChar8 *default_lang; /* MT-safe */
98
99FcChar8 *
100FcGetDefaultLang (void)
101{
102    FcChar8 *lang;
103retry:
104    lang = fc_atomic_ptr_get (&default_lang);
105    if (!lang)
106    {
107	FcStrSet *langs = FcGetDefaultLangs ();
108	lang = FcStrdup (langs->strs[0]);
109
110	if (!fc_atomic_ptr_cmpexch (&default_lang, NULL, lang)) {
111	    free (lang);
112	    goto retry;
113	}
114    }
115
116    return lang;
117}
118
119static FcChar8 *default_prgname;
120
121FcChar8 *
122FcGetPrgname (void)
123{
124    FcChar8 *prgname;
125retry:
126    prgname = fc_atomic_ptr_get (&default_prgname);
127    if (!prgname)
128    {
129#ifdef _WIN32
130	char buf[MAX_PATH+1];
131
132	/* TODO This is ASCII-only; fix it. */
133	if (GetModuleFileNameA (GetModuleHandle (NULL), buf, sizeof (buf) / sizeof (buf[0])) > 0)
134	{
135	    char *p;
136	    unsigned int len;
137
138	    p = strrchr (buf, '\\');
139	    if (p)
140		p++;
141	    else
142		p = buf;
143
144	    len = strlen (p);
145
146	    if (len > 4 && 0 == strcmp (p + len - 4, ".exe"))
147	    {
148		len -= 4;
149		buf[len] = '\0';
150	    }
151
152	    prgname = FcStrdup (p);
153	}
154#elif defined (HAVE_GETPROGNAME)
155	const char *q = getprogname ();
156	if (q)
157	    prgname = FcStrdup (q);
158	else
159	    prgname = FcStrdup ("");
160#else
161# if defined (HAVE_GETEXECNAME)
162	char *p = FcStrdup(getexecname ());
163# elif defined (HAVE_READLINK)
164	size_t size = FC_PATH_MAX;
165	char *p = NULL;
166
167	while (1)
168	{
169	    char *buf = malloc (size);
170	    ssize_t len;
171
172	    if (!buf)
173		break;
174
175	    len = readlink ("/proc/self/exe", buf, size - 1);
176	    if (len < 0)
177	    {
178		free (buf);
179		break;
180	    }
181	    if (len < size - 1)
182	    {
183		buf[len] = 0;
184		p = buf;
185		break;
186	    }
187
188	    free (buf);
189	    size *= 2;
190	}
191# else
192	char *p = NULL;
193# endif
194	if (p)
195	{
196	    char *r = strrchr (p, '/');
197	    if (r)
198		r++;
199	    else
200		r = p;
201
202	    prgname = FcStrdup (r);
203	}
204
205	if (!prgname)
206	    prgname = FcStrdup ("");
207
208	if (p)
209	    free (p);
210#endif
211
212	if (!fc_atomic_ptr_cmpexch (&default_prgname, NULL, prgname)) {
213	    free (prgname);
214	    goto retry;
215	}
216    }
217
218    if (prgname && !prgname[0])
219	return NULL;
220
221    return prgname;
222}
223
224static FcChar8 *default_desktop_name;
225
226FcChar8 *
227FcGetDesktopName (void)
228{
229    FcChar8 *desktop_name;
230retry:
231    desktop_name = fc_atomic_ptr_get (&default_desktop_name);
232    if (!desktop_name)
233    {
234	char *s = getenv ("XDG_CURRENT_DESKTOP");
235
236	if (!s)
237	    desktop_name = FcStrdup ("");
238	else
239	    desktop_name = FcStrdup (s);
240	if (!desktop_name)
241	{
242	    fprintf (stderr, "Fontconfig error: out of memory in %s\n",
243		     __FUNCTION__);
244	    return NULL;
245	}
246
247	if (!fc_atomic_ptr_cmpexch(&default_desktop_name, NULL, desktop_name))
248	{
249	    free (desktop_name);
250	    goto retry;
251	}
252    }
253    if (desktop_name && !desktop_name[0])
254	return NULL;
255
256    return desktop_name;
257}
258
259void
260FcDefaultFini (void)
261{
262    FcChar8  *lang;
263    FcStrSet *langs;
264    FcChar8  *prgname;
265    FcChar8  *desktop;
266
267    lang = fc_atomic_ptr_get (&default_lang);
268    if (lang && fc_atomic_ptr_cmpexch (&default_lang, lang, NULL))
269    {
270	free (lang);
271    }
272
273    langs = fc_atomic_ptr_get (&default_langs);
274    if (langs && fc_atomic_ptr_cmpexch (&default_langs, langs, NULL))
275    {
276	FcRefInit (&langs->ref, 1);
277	FcStrSetDestroy (langs);
278    }
279
280    prgname = fc_atomic_ptr_get (&default_prgname);
281    if (prgname && fc_atomic_ptr_cmpexch (&default_prgname, prgname, NULL))
282    {
283	free (prgname);
284    }
285
286    desktop = fc_atomic_ptr_get (&default_desktop_name);
287    if (desktop && fc_atomic_ptr_cmpexch(&default_desktop_name, desktop, NULL))
288    {
289	free (desktop);
290    }
291}
292
293void
294FcDefaultSubstitute (FcPattern *pattern)
295{
296    FcPatternIter iter;
297    FcValue v, namelang, v2;
298    int	    i;
299    double	dpi, size, scale, pixelsize;
300
301    if (!FcPatternFindObjectIter (pattern, &iter, FC_WEIGHT_OBJECT))
302	FcPatternObjectAddInteger (pattern, FC_WEIGHT_OBJECT, FC_WEIGHT_NORMAL);
303
304    if (!FcPatternFindObjectIter (pattern, &iter, FC_SLANT_OBJECT))
305	FcPatternObjectAddInteger (pattern, FC_SLANT_OBJECT, FC_SLANT_ROMAN);
306
307    if (!FcPatternFindObjectIter (pattern, &iter, FC_WIDTH_OBJECT))
308	FcPatternObjectAddInteger (pattern, FC_WIDTH_OBJECT, FC_WIDTH_NORMAL);
309
310    for (i = 0; i < NUM_FC_BOOL_DEFAULTS; i++)
311	if (!FcPatternFindObjectIter (pattern, &iter, FcBoolDefaults[i].field))
312	    FcPatternObjectAddBool (pattern, FcBoolDefaults[i].field, FcBoolDefaults[i].value);
313
314    if (FcPatternObjectGetDouble (pattern, FC_SIZE_OBJECT, 0, &size) != FcResultMatch)
315    {
316	FcRange *r;
317	double b, e;
318	if (FcPatternObjectGetRange (pattern, FC_SIZE_OBJECT, 0, &r) == FcResultMatch && FcRangeGetDouble (r, &b, &e))
319	    size = (b + e) * .5;
320	else
321	    size = 12.0L;
322    }
323    if (FcPatternObjectGetDouble (pattern, FC_SCALE_OBJECT, 0, &scale) != FcResultMatch)
324	scale = 1.0;
325    if (FcPatternObjectGetDouble (pattern, FC_DPI_OBJECT, 0, &dpi) != FcResultMatch)
326	dpi = 75.0;
327
328    if (!FcPatternFindObjectIter (pattern, &iter, FC_PIXEL_SIZE_OBJECT))
329    {
330	(void) FcPatternObjectDel (pattern, FC_SCALE_OBJECT);
331	FcPatternObjectAddDouble (pattern, FC_SCALE_OBJECT, scale);
332	pixelsize = size * scale;
333	(void) FcPatternObjectDel (pattern, FC_DPI_OBJECT);
334	FcPatternObjectAddDouble (pattern, FC_DPI_OBJECT, dpi);
335	pixelsize *= dpi / 72.0;
336	FcPatternObjectAddDouble (pattern, FC_PIXEL_SIZE_OBJECT, pixelsize);
337    }
338    else
339    {
340	FcPatternIterGetValue(pattern, &iter, 0, &v, NULL);
341	size = v.u.d;
342	size = size / dpi * 72.0 / scale;
343    }
344    (void) FcPatternObjectDel (pattern, FC_SIZE_OBJECT);
345    FcPatternObjectAddDouble (pattern, FC_SIZE_OBJECT, size);
346
347    if (!FcPatternFindObjectIter (pattern, &iter, FC_FONTVERSION_OBJECT))
348	FcPatternObjectAddInteger (pattern, FC_FONTVERSION_OBJECT, 0x7fffffff);
349
350    if (!FcPatternFindObjectIter (pattern, &iter, FC_HINT_STYLE_OBJECT))
351	FcPatternObjectAddInteger (pattern, FC_HINT_STYLE_OBJECT, FC_HINT_FULL);
352
353    if (!FcPatternFindObjectIter (pattern, &iter, FC_NAMELANG_OBJECT))
354	FcPatternObjectAddString (pattern, FC_NAMELANG_OBJECT, FcGetDefaultLang ());
355
356    /* shouldn't be failed. */
357    FcPatternObjectGet (pattern, FC_NAMELANG_OBJECT, 0, &namelang);
358    /* Add a fallback to ensure the english name when the requested language
359     * isn't available. this would helps for the fonts that have non-English
360     * name at the beginning.
361     */
362    /* Set "en-us" instead of "en" to avoid giving higher score to "en".
363     * This is a hack for the case that the orth is not like ll-cc, because,
364     * if no namelang isn't explicitly set, it will has something like ll-cc
365     * according to current locale. which may causes FcLangDifferentTerritory
366     * at FcLangCompare(). thus, the English name is selected so that
367     * exact matched "en" has higher score than ll-cc.
368     */
369    v2.type = FcTypeString;
370    v2.u.s = (FcChar8 *) "en-us";
371    if (!FcPatternFindObjectIter (pattern, &iter, FC_FAMILYLANG_OBJECT))
372    {
373	FcPatternObjectAdd (pattern, FC_FAMILYLANG_OBJECT, namelang, FcTrue);
374	FcPatternObjectAddWithBinding (pattern, FC_FAMILYLANG_OBJECT, v2, FcValueBindingWeak, FcTrue);
375    }
376    if (!FcPatternFindObjectIter (pattern, &iter, FC_STYLELANG_OBJECT))
377    {
378	FcPatternObjectAdd (pattern, FC_STYLELANG_OBJECT, namelang, FcTrue);
379	FcPatternObjectAddWithBinding (pattern, FC_STYLELANG_OBJECT, v2, FcValueBindingWeak, FcTrue);
380    }
381    if (!FcPatternFindObjectIter (pattern, &iter, FC_FULLNAMELANG_OBJECT))
382    {
383	FcPatternObjectAdd (pattern, FC_FULLNAMELANG_OBJECT, namelang, FcTrue);
384	FcPatternObjectAddWithBinding (pattern, FC_FULLNAMELANG_OBJECT, v2, FcValueBindingWeak, FcTrue);
385    }
386
387    if (FcPatternObjectGet (pattern, FC_PRGNAME_OBJECT, 0, &v) == FcResultNoMatch)
388    {
389	FcChar8 *prgname = FcGetPrgname ();
390	if (prgname)
391	    FcPatternObjectAddString (pattern, FC_PRGNAME_OBJECT, prgname);
392    }
393
394    if (FcPatternObjectGet (pattern, FC_DESKTOP_NAME_OBJECT, 0, &v) == FcResultNoMatch)
395    {
396	FcChar8 *desktop = FcGetDesktopName ();
397	if (desktop)
398	    FcPatternObjectAddString (pattern, FC_DESKTOP_NAME_OBJECT, desktop);
399    }
400
401    if (!FcPatternFindObjectIter (pattern, &iter, FC_ORDER_OBJECT))
402	FcPatternObjectAddInteger (pattern, FC_ORDER_OBJECT, 0);
403}
404#define __fcdefault__
405#include "fcaliastail.h"
406#undef __fcdefault__
407