]> git.wh0rd.org - fontconfig.git/blob - fc-lang/fclang.tmpl.c
9bd84301fea355fb469dcb247dab1ef9a918eae2
[fontconfig.git] / fc-lang / fclang.tmpl.c
1 /*
2 * $XFree86$
3 *
4 * Copyright © 2002 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
27 typedef struct {
28 FcChar8 *lang;
29 FcCharSet charset;
30 } FcLangCharSet;
31
32 @@@
33
34 #define NUM_LANG_CHAR_SET (sizeof (fcLangCharSets) / sizeof (fcLangCharSets[0]))
35
36 FcBool
37 FcFreeTypeSetLang (FcPattern *pattern, FcCharSet *charset)
38 {
39 int i;
40 FcChar32 missing;
41
42 for (i = 0; i < NUM_LANG_CHAR_SET; i++)
43 {
44 missing = FcCharSetSubtractCount (&fcLangCharSets[i].charset, charset);
45 if (FcDebug() & FC_DBG_SCANV)
46 printf ("%s(%d) ", fcLangCharSets[i].lang, missing);
47 if (!missing && !FcFreeTypeHasLang (pattern, fcLangCharSets[i].lang))
48 if (!FcPatternAddString (pattern, FC_LANG, fcLangCharSets[i].lang))
49 return FcFalse;
50 }
51 if (FcDebug() & FC_DBG_SCANV)
52 printf ("\n");
53 return FcTrue;
54 }
55
56
57 FcLangResult
58 FcLangCompare (const FcChar8 *s1, const FcChar8 *s2)
59 {
60 const FcChar8 *orig_s1 = s1;
61 FcChar8 c1, c2;
62 FcLangResult result;
63 /*
64 * Compare ISO 639 language codes
65 */
66 for (;;)
67 {
68 c1 = *s1++;
69 c2 = *s2++;
70 if (c1 == '\0' || c1 == '-')
71 break;
72 if (c2 == '\0' || c2 == '-')
73 break;
74 c1 = FcToLower (c1);
75 c2 = FcToLower (c2);
76 if (c1 != c2)
77 return FcLangDifferentLang; /* mismatching lang code */
78 }
79 if (!c1 && !c2)
80 return FcLangEqual;
81 /*
82 * Make x-* mismatch as if the lang part didn't match
83 */
84 result = FcLangDifferentCountry;
85 if (orig_s1[0] == 'x' && (orig_s1[1] == '\0' || orig_s1[1] == '-'))
86 result = FcLangDifferentLang;
87
88 if (c1 == '\0' || c2 == '\0')
89 return result;
90 /*
91 * Compare ISO 3166 country codes
92 */
93 for (;;)
94 {
95 c1 = *s1++;
96 c2 = *s2++;
97 if (!c1 || !c2)
98 break;
99 c1 = FcToLower (c1);
100 c2 = FcToLower (c2);
101 if (c1 != c2)
102 break;
103 }
104 if (c1 == c2)
105 return FcLangEqual;
106 else
107 return result;
108 }
109
110 const FcCharSet *
111 FcCharSetForLang (const FcChar8 *lang)
112 {
113 int i;
114 int country = -1;
115 for (i = 0; i < NUM_LANG_CHAR_SET; i++)
116 {
117 switch (FcLangCompare (lang, fcLangCharSets[i].lang)) {
118 case FcLangEqual:
119 return &fcLangCharSets[i].charset;
120 case FcLangDifferentCountry:
121 if (country == -1)
122 country = i;
123 default:
124 break;
125 }
126 }
127 if (country == -1)
128 return 0;
129 return &fcLangCharSets[i].charset;
130 }