2 * $RCSId: xc/lib/fontconfig/src/fclang.c,v 1.7 2002/08/26 23:34:31 keithp Exp $
4 * Copyright © 2002 Keith Packard
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.
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.
37 #include "../fc-lang/fclang.h"
40 FcChar32 map[NUM_LANG_SET_MAP];
44 #define FcLangSetBitSet(ls, id) ((ls)->map[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
45 #define FcLangSetBitGet(ls, id) (((ls)->map[(id)>>5] >> ((id) & 0x1f)) & 1)
48 FcFreeTypeLangSet (const FcCharSet *charset,
49 const FcChar8 *exclusiveLang)
53 const FcCharSet *exclusiveCharset = 0;
58 exclusiveCharset = FcCharSetForLang (exclusiveLang);
59 ls = FcLangSetCreate ();
62 for (i = 0; i < NUM_LANG_CHAR_SET; i++)
65 * Check for Han charsets to make fonts
66 * which advertise support for a single language
67 * not support other Han languages
69 if (exclusiveCharset &&
70 FcFreeTypeIsExclusiveLang (fcLangCharSets[i].lang) &&
71 fcLangCharSets[i].charset.leaves != exclusiveCharset->leaves)
75 missing = FcCharSetSubtractCount (&fcLangCharSets[i].charset, charset);
76 if (FcDebug() & FC_DBG_SCANV)
78 if (missing && missing < 10)
80 FcCharSet *missed = FcCharSetSubtract (&fcLangCharSets[i].charset,
83 FcChar32 map[FC_CHARSET_MAP_SIZE];
86 printf ("\n%s(%d) ", fcLangCharSets[i].lang, missing);
88 for (ucs4 = FcCharSetFirstPage (missed, map, &next);
89 ucs4 != FC_CHARSET_DONE;
90 ucs4 = FcCharSetNextPage (missed, map, &next))
93 for (i = 0; i < FC_CHARSET_MAP_SIZE; i++)
96 for (j = 0; j < 32; j++)
97 if (map[i] & (1 << j))
98 printf (" %04x", ucs4 + i * 32 + j);
102 FcCharSetDestroy (missed);
105 printf ("%s(%d) ", fcLangCharSets[i].lang, missing);
108 FcLangSetBitSet (ls, i);
111 if (FcDebug() & FC_DBG_SCANV)
118 #define FcLangEnd(c) ((c) == '-' || (c) == '\0')
121 FcLangCompare (const FcChar8 *s1, const FcChar8 *s2)
124 FcLangResult result = FcLangDifferentLang;
135 if (FcLangEnd (c1) && FcLangEnd (c2))
136 result = FcLangDifferentCountry;
142 result = FcLangDifferentCountry;
147 * Return FcTrue when super contains sub.
149 * super contains sub if super and sub have the same
150 * language and either the same country or one
151 * is missing the country
155 FcLangContains (const FcChar8 *super, const FcChar8 *sub)
168 /* see if super has a country while sub is mising one */
169 if (c1 == '-' && c2 == '\0')
171 /* see if sub has a country while super is mising one */
172 if (c1 == '\0' && c2 == '-')
182 FcCharSetForLang (const FcChar8 *lang)
186 for (i = 0; i < NUM_LANG_CHAR_SET; i++)
188 switch (FcLangCompare (lang, fcLangCharSets[i].lang)) {
190 return &fcLangCharSets[i].charset;
191 case FcLangDifferentCountry:
200 return &fcLangCharSets[i].charset;
204 FcLangSetCreate (void)
208 ls = malloc (sizeof (FcLangSet));
211 FcMemAlloc (FC_MEM_LANGSET, sizeof (FcLangSet));
212 memset (ls->map, '\0', sizeof (ls->map));
218 FcLangSetDestroy (FcLangSet *ls)
221 FcStrSetDestroy (ls->extra);
222 FcMemFree (FC_MEM_LANGSET, sizeof (FcLangSet));
227 FcLangSetCopy (const FcLangSet *ls)
231 new = FcLangSetCreate ();
234 memcpy (new->map, ls->map, sizeof (new->map));
240 new->extra = FcStrSetCreate ();
244 list = FcStrListCreate (ls->extra);
248 while ((extra = FcStrListNext (list)))
249 if (!FcStrSetAdd (new->extra, extra))
251 FcStrListDone (list);
254 FcStrListDone (list);
258 FcLangSetDestroy (new);
264 FcLangSetIndex (const FcChar8 *lang)
266 int low, high, mid = 0;
268 FcChar8 firstChar = FcToLower(lang[0]);
269 FcChar8 secondChar = firstChar ? FcToLower(lang[1]) : '\0';
274 high = fcLangCharSetRanges[0].begin;
276 else if(firstChar > 'z')
278 low = fcLangCharSetRanges[25].begin;
279 high = NUM_LANG_CHAR_SET - 1;
283 low = fcLangCharSetRanges[firstChar - 'a'].begin;
284 high = fcLangCharSetRanges[firstChar - 'a'].end;
287 return -low; /* next entry after where it would be */
292 mid = (high + low) >> 1;
293 if(fcLangCharSets[mid].lang[0] != firstChar)
294 cmp = FcStrCmpIgnoreCase(fcLangCharSets[mid].lang, lang);
296 { /* fast path for resolving 2-letter languages (by far the most common) after
297 * finding the first char (probably already true because of the hash table) */
298 cmp = fcLangCharSets[mid].lang[1] - secondChar;
300 (fcLangCharSets[mid].lang[2] != '\0' ||
303 cmp = FcStrCmpIgnoreCase(fcLangCharSets[mid].lang+2,
320 FcLangSetAdd (FcLangSet *ls, const FcChar8 *lang)
324 id = FcLangSetIndex (lang);
327 FcLangSetBitSet (ls, id);
332 ls->extra = FcStrSetCreate ();
336 return FcStrSetAdd (ls->extra, lang);
340 FcLangSetHasLang (const FcLangSet *ls, const FcChar8 *lang)
343 FcLangResult best, r;
346 id = FcLangSetIndex (lang);
349 else if (FcLangSetBitGet (ls, id))
351 best = FcLangDifferentLang;
352 for (i = id - 1; i >= 0; i--)
354 r = FcLangCompare (lang, fcLangCharSets[i].lang);
355 if (r == FcLangDifferentLang)
357 if (FcLangSetBitGet (ls, i) && r < best)
360 for (i = id; i < NUM_LANG_CHAR_SET; i++)
362 r = FcLangCompare (lang, fcLangCharSets[i].lang);
363 if (r == FcLangDifferentLang)
365 if (FcLangSetBitGet (ls, i) && r < best)
370 FcStrList *list = FcStrListCreate (ls->extra);
376 while (best > FcLangEqual && (extra = FcStrListNext (list)))
378 r = FcLangCompare (lang, extra);
382 FcStrListDone (list);
389 FcLangSetCompareStrSet (const FcLangSet *ls, FcStrSet *set)
391 FcStrList *list = FcStrListCreate (set);
392 FcLangResult r, best = FcLangDifferentLang;
397 while (best > FcLangEqual && (extra = FcStrListNext (list)))
399 r = FcLangSetHasLang (ls, extra);
403 FcStrListDone (list);
409 FcLangSetCompare (const FcLangSet *lsa, const FcLangSet *lsb)
412 FcLangResult best, r;
414 for (i = 0; i < NUM_LANG_SET_MAP; i++)
415 if (lsa->map[i] & lsb->map[i])
417 best = FcLangDifferentLang;
418 for (j = 0; j < NUM_COUNTRY_SET; j++)
419 for (i = 0; i < NUM_LANG_SET_MAP; i++)
420 if ((lsa->map[i] & fcLangCountrySets[j][i]) &&
421 (lsb->map[i] & fcLangCountrySets[j][i]))
423 best = FcLangDifferentCountry;
428 r = FcLangSetCompareStrSet (lsb, lsa->extra);
432 if (best > FcLangEqual && lsb->extra)
434 r = FcLangSetCompareStrSet (lsa, lsb->extra);
442 * Used in computing values -- mustn't allocate any storage
445 FcLangSetPromote (const FcChar8 *lang)
448 static FcStrSet strs;
452 memset (ls.map, '\0', sizeof (ls.map));
454 id = FcLangSetIndex (lang);
457 FcLangSetBitSet (&ls, id);
466 str = (FcChar8 *) lang;
472 FcLangSetHash (const FcLangSet *ls)
477 for (i = 0; i < NUM_LANG_SET_MAP; i++)
485 FcNameParseLangSet (const FcChar8 *string)
491 ls = FcLangSetCreate ();
497 for(i = 0; i < 31;i++)
500 if(c == '\0' || c == '|')
501 break; /* end of this code */
505 if (!FcLangSetAdd (ls, lang))
512 FcLangSetDestroy (ls);
518 FcNameUnparseLangSet (FcStrBuf *buf, const FcLangSet *ls)
522 FcBool first = FcTrue;
524 for (i = 0; i < NUM_LANG_SET_MAP; i++)
526 if ((bits = ls->map[i]))
528 for (bit = 0; bit <= 31; bit++)
529 if (bits & (1 << bit))
531 int id = (i << 5) | bit;
533 if (!FcStrBufChar (buf, '|'))
535 if (!FcStrBufString (buf, fcLangCharSets[id].lang))
543 FcStrList *list = FcStrListCreate (ls->extra);
548 while ((extra = FcStrListNext (list)))
551 if (!FcStrBufChar (buf, '|'))
553 if (!FcStrBufString (buf, extra))
562 FcLangSetEqual (const FcLangSet *lsa, const FcLangSet *lsb)
566 for (i = 0; i < NUM_LANG_SET_MAP; i++)
568 if (lsa->map[i] != lsb->map[i])
571 if (!lsa->extra && !lsb->extra)
573 if (lsa->extra && lsb->extra)
574 return FcStrSetEqual (lsa->extra, lsb->extra);
579 FcLangSetContainsLang (const FcLangSet *ls, const FcChar8 *lang)
584 id = FcLangSetIndex (lang);
587 else if (FcLangSetBitGet (ls, id))
590 * search up and down among equal languages for a match
592 for (i = id - 1; i >= 0; i--)
594 if (FcLangCompare (fcLangCharSets[i].lang, lang) == FcLangDifferentLang)
596 if (FcLangSetBitGet (ls, i) &&
597 FcLangContains (fcLangCharSets[i].lang, lang))
600 for (i = id; i < NUM_LANG_CHAR_SET; i++)
602 if (FcLangCompare (fcLangCharSets[i].lang, lang) == FcLangDifferentLang)
604 if (FcLangSetBitGet (ls, i) &&
605 FcLangContains (fcLangCharSets[i].lang, lang))
610 FcStrList *list = FcStrListCreate (ls->extra);
615 while ((extra = FcStrListNext (list)))
617 if (FcLangContains (extra, lang))
620 FcStrListDone (list);
629 * return FcTrue if lsa contains every language in lsb
632 FcLangSetContains (const FcLangSet *lsa, const FcLangSet *lsb)
637 if (FcDebug() & FC_DBG_MATCHV)
639 printf ("FcLangSet "); FcLangSetPrint (lsa);
640 printf (" contains "); FcLangSetPrint (lsb);
644 * check bitmaps for missing language support
646 for (i = 0; i < NUM_LANG_SET_MAP; i++)
648 missing = lsb->map[i] & ~lsa->map[i];
651 for (j = 0; j < 32; j++)
652 if (missing & (1 << j))
654 if (!FcLangSetContainsLang (lsa,
655 fcLangCharSets[i*32 + j].lang))
657 if (FcDebug() & FC_DBG_MATCHV)
658 printf ("\tMissing bitmap %s\n", fcLangCharSets[i*32+j].lang);
666 FcStrList *list = FcStrListCreate (lsb->extra);
671 while ((extra = FcStrListNext (list)))
673 if (!FcLangSetContainsLang (lsa, extra))
675 if (FcDebug() & FC_DBG_MATCHV)
676 printf ("\tMissing string %s\n", extra);
680 FcStrListDone (list);