Update iso639-2 language coverage info, fix Georgian orthography to
[platform/upstream/fontconfig.git] / src / fclang.c
1 /*
2  * $XFree86: xc/lib/fontconfig/fc-lang/fclang.tmpl.c,v 1.1 2002/07/06 23:21:36 keithp Exp $
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 #include "../fc-lang/fclang.h"
33
34 #define NUM_LANG_CHAR_SET       (sizeof (fcLangCharSets) / sizeof (fcLangCharSets[0]))
35                                                  
36 FcBool
37 FcFreeTypeSetLang (FcPattern        *pattern, 
38                    FcCharSet        *charset, 
39                    const FcChar8    *exclusiveLang)
40 {
41     int             i;
42     FcChar32        missing;
43     FcBool          hasLang = FcFalse;
44     const FcCharSet *exclusiveCharset = 0;
45
46     if (exclusiveLang)
47         exclusiveCharset = FcCharSetForLang (exclusiveLang);
48     for (i = 0; i < NUM_LANG_CHAR_SET; i++)
49     {
50         /*
51          * Check for Han charsets to make fonts
52          * which advertise support for a single language
53          * not support other Han languages
54          */
55         if (exclusiveCharset &&
56             FcFreeTypeIsExclusiveLang (fcLangCharSets[i].lang) &&
57             fcLangCharSets[i].charset.leaves != exclusiveCharset->leaves)
58         {
59             continue;
60         }
61         missing = FcCharSetSubtractCount (&fcLangCharSets[i].charset, charset);
62         if (FcDebug() & FC_DBG_SCANV)
63             printf ("%s(%d) ", fcLangCharSets[i].lang, missing);
64         if (!missing)
65         {
66             if (!FcPatternAddString (pattern, FC_LANG, fcLangCharSets[i].lang))
67                 return FcFalse;
68             hasLang = FcTrue;
69         }
70     }
71     /*
72      * Make sure it has a lang entry
73      */
74     if (!hasLang)
75         if (!FcPatternAddString (pattern, FC_LANG, (FcChar8 *) "x-unknown"))
76             return FcFalse;
77
78     if (FcDebug() & FC_DBG_SCANV)
79         printf ("\n");
80     return FcTrue;
81 }
82
83
84 FcLangResult
85 FcLangCompare (const FcChar8 *s1, const FcChar8 *s2)
86 {
87     const FcChar8   *orig_s1 = s1;
88     FcChar8         c1, c2;
89     FcLangResult    result;
90     /*
91      * Compare ISO 639 language codes
92      */
93     for (;;)
94     {
95         c1 = *s1++;
96         c2 = *s2++;
97         if (c1 == '\0' || c1 == '-')
98             break;
99         if (c2 == '\0' || c2 == '-')
100             break;
101         c1 = FcToLower (c1);
102         c2 = FcToLower (c2);
103         if (c1 != c2)
104             return FcLangDifferentLang;     /* mismatching lang code */
105     }
106     if (!c1 && !c2)
107         return FcLangEqual;
108     /*
109      * Make x-* mismatch as if the lang part didn't match
110      */
111     result = FcLangDifferentCountry;
112     if (orig_s1[0] == 'x' && (orig_s1[1] == '\0' || orig_s1[1] == '-'))
113         result = FcLangDifferentLang;
114     
115     if (c1 == '\0' || c2 == '\0')
116         return result;
117     /*
118      * Compare ISO 3166 country codes
119      */
120     for (;;)
121     {
122         c1 = *s1++;
123         c2 = *s2++;
124         if (!c1 || !c2)
125             break;
126         c1 = FcToLower (c1);
127         c2 = FcToLower (c2);
128         if (c1 != c2)
129             break;
130     }
131     if (c1 == c2)
132         return FcLangEqual;
133     else
134         return result;
135 }
136
137 const FcCharSet *
138 FcCharSetForLang (const FcChar8 *lang)
139 {
140     int         i;
141     int         country = -1;
142     for (i = 0; i < NUM_LANG_CHAR_SET; i++)
143     {
144         switch (FcLangCompare (lang, fcLangCharSets[i].lang)) {
145         case FcLangEqual:
146             return &fcLangCharSets[i].charset;
147         case FcLangDifferentCountry:
148             if (country == -1)
149                 country = i;
150         default:
151             break;
152         }
153     }
154     if (country == -1)
155         return 0;
156     return &fcLangCharSets[i].charset;
157 }