Clean up some coverage files; a few accidentally included PUA values and
[platform/upstream/fontconfig.git] / src / fclang.c
1 /*
2  * $XFree86: xc/lib/fontconfig/src/fclang.c,v 1.3 2002/07/08 07:31:53 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         {
64             if (missing && missing < 10)
65             {
66                 FcCharSet   *missed = FcCharSetSubtract (&fcLangCharSets[i].charset, 
67                                                          charset);
68                 FcChar32    ucs4;
69                 FcChar32    map[FC_CHARSET_MAP_SIZE];
70                 FcChar32    next;
71
72                 printf ("\n%s(%d) ", fcLangCharSets[i].lang, missing);
73                 printf ("{");
74                 for (ucs4 = FcCharSetFirstPage (missed, map, &next);
75                      ucs4 != FC_CHARSET_DONE;
76                      ucs4 = FcCharSetNextPage (missed, map, &next))
77                 {
78                     int     i, j;
79                     for (i = 0; i < FC_CHARSET_MAP_SIZE; i++)
80                         if (map[i])
81                         {
82                             for (j = 0; j < 32; j++)
83                                 if (map[i] & (1 << j))
84                                     printf (" %04x", ucs4 + i * 32 + j);
85                         }
86                 }
87                 printf (" }\n\t");
88                 FcCharSetDestroy (missed);
89             }
90             else
91                 printf ("%s(%d) ", fcLangCharSets[i].lang, missing);
92         }
93         if (!missing)
94         {
95             if (!FcPatternAddString (pattern, FC_LANG, fcLangCharSets[i].lang))
96                 return FcFalse;
97             hasLang = FcTrue;
98         }
99     }
100     /*
101      * Make sure it has a lang entry
102      */
103     if (!hasLang)
104         if (!FcPatternAddString (pattern, FC_LANG, (FcChar8 *) "x-unknown"))
105             return FcFalse;
106
107     if (FcDebug() & FC_DBG_SCANV)
108         printf ("\n");
109     return FcTrue;
110 }
111
112
113 FcLangResult
114 FcLangCompare (const FcChar8 *s1, const FcChar8 *s2)
115 {
116     const FcChar8   *orig_s1 = s1;
117     FcChar8         c1, c2;
118     FcLangResult    result;
119     /*
120      * Compare ISO 639 language codes
121      */
122     for (;;)
123     {
124         c1 = *s1++;
125         c2 = *s2++;
126         if (c1 == '\0' || c1 == '-')
127             break;
128         if (c2 == '\0' || c2 == '-')
129             break;
130         c1 = FcToLower (c1);
131         c2 = FcToLower (c2);
132         if (c1 != c2)
133             return FcLangDifferentLang;     /* mismatching lang code */
134     }
135     if (!c1 && !c2)
136         return FcLangEqual;
137     /*
138      * Make x-* mismatch as if the lang part didn't match
139      */
140     result = FcLangDifferentCountry;
141     if (orig_s1[0] == 'x' && (orig_s1[1] == '\0' || orig_s1[1] == '-'))
142         result = FcLangDifferentLang;
143     
144     if (c1 == '\0' || c2 == '\0')
145         return result;
146     /*
147      * Compare ISO 3166 country codes
148      */
149     for (;;)
150     {
151         c1 = *s1++;
152         c2 = *s2++;
153         if (!c1 || !c2)
154             break;
155         c1 = FcToLower (c1);
156         c2 = FcToLower (c2);
157         if (c1 != c2)
158             break;
159     }
160     if (c1 == c2)
161         return FcLangEqual;
162     else
163         return result;
164 }
165
166 const FcCharSet *
167 FcCharSetForLang (const FcChar8 *lang)
168 {
169     int         i;
170     int         country = -1;
171     for (i = 0; i < NUM_LANG_CHAR_SET; i++)
172     {
173         switch (FcLangCompare (lang, fcLangCharSets[i].lang)) {
174         case FcLangEqual:
175             return &fcLangCharSets[i].charset;
176         case FcLangDifferentCountry:
177             if (country == -1)
178                 country = i;
179         default:
180             break;
181         }
182     }
183     if (country == -1)
184         return 0;
185     return &fcLangCharSets[i].charset;
186 }