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))
72 if (fcLangCharSets[i].charset.num != exclusiveCharset->num)
75 for (j = 0; j < fcLangCharSets[i].charset.num; j++)
76 if (FcCharSetGetLeaf(&fcLangCharSets[i].charset, j) !=
77 FcCharSetGetLeaf(exclusiveCharset, j))
80 missing = FcCharSetSubtractCount (&fcLangCharSets[i].charset, charset);
81 if (FcDebug() & FC_DBG_SCANV)
83 if (missing && missing < 10)
85 FcCharSet *missed = FcCharSetSubtract (&fcLangCharSets[i].charset,
88 FcChar32 map[FC_CHARSET_MAP_SIZE];
91 printf ("\n%s(%d) ", fcLangCharSets[i].lang, missing);
93 for (ucs4 = FcCharSetFirstPage (missed, map, &next);
94 ucs4 != FC_CHARSET_DONE;
95 ucs4 = FcCharSetNextPage (missed, map, &next))
98 for (i = 0; i < FC_CHARSET_MAP_SIZE; i++)
101 for (j = 0; j < 32; j++)
102 if (map[i] & (1 << j))
103 printf (" %04x", ucs4 + i * 32 + j);
107 FcCharSetDestroy (missed);
110 printf ("%s(%d) ", fcLangCharSets[i].lang, missing);
113 FcLangSetBitSet (ls, i);
116 if (FcDebug() & FC_DBG_SCANV)
123 #define FcLangEnd(c) ((c) == '-' || (c) == '\0')
126 FcLangCompare (const FcChar8 *s1, const FcChar8 *s2)
129 FcLangResult result = FcLangDifferentLang;
140 if (FcLangEnd (c1) && FcLangEnd (c2))
141 result = FcLangDifferentCountry;
147 result = FcLangDifferentCountry;
152 * Return FcTrue when super contains sub.
154 * super contains sub if super and sub have the same
155 * language and either the same country or one
156 * is missing the country
160 FcLangContains (const FcChar8 *super, const FcChar8 *sub)
173 /* see if super has a country while sub is mising one */
174 if (c1 == '-' && c2 == '\0')
176 /* see if sub has a country while super is mising one */
177 if (c1 == '\0' && c2 == '-')
187 FcCharSetForLang (const FcChar8 *lang)
191 for (i = 0; i < NUM_LANG_CHAR_SET; i++)
193 switch (FcLangCompare (lang, fcLangCharSets[i].lang)) {
195 return &fcLangCharSets[i].charset;
196 case FcLangDifferentCountry:
205 return &fcLangCharSets[i].charset;
209 FcLangSetCreate (void)
213 ls = malloc (sizeof (FcLangSet));
216 FcMemAlloc (FC_MEM_LANGSET, sizeof (FcLangSet));
217 memset (ls->map, '\0', sizeof (ls->map));
223 FcLangSetDestroy (FcLangSet *ls)
226 FcStrSetDestroy (ls->extra);
227 FcMemFree (FC_MEM_LANGSET, sizeof (FcLangSet));
232 FcLangSetCopy (const FcLangSet *ls)
236 new = FcLangSetCreate ();
239 memcpy (new->map, ls->map, sizeof (new->map));
245 new->extra = FcStrSetCreate ();
249 list = FcStrListCreate (ls->extra);
253 while ((extra = FcStrListNext (list)))
254 if (!FcStrSetAdd (new->extra, extra))
256 FcStrListDone (list);
259 FcStrListDone (list);
263 FcLangSetDestroy (new);
269 FcLangSetIndex (const FcChar8 *lang)
271 int low, high, mid = 0;
273 FcChar8 firstChar = FcToLower(lang[0]);
274 FcChar8 secondChar = firstChar ? FcToLower(lang[1]) : '\0';
279 high = fcLangCharSetRanges[0].begin;
281 else if(firstChar > 'z')
283 low = fcLangCharSetRanges[25].begin;
284 high = NUM_LANG_CHAR_SET - 1;
288 low = fcLangCharSetRanges[firstChar - 'a'].begin;
289 high = fcLangCharSetRanges[firstChar - 'a'].end;
292 return -low; /* next entry after where it would be */
297 mid = (high + low) >> 1;
298 if(fcLangCharSets[mid].lang[0] != firstChar)
299 cmp = FcStrCmpIgnoreCase(fcLangCharSets[mid].lang, lang);
301 { /* fast path for resolving 2-letter languages (by far the most common) after
302 * finding the first char (probably already true because of the hash table) */
303 cmp = fcLangCharSets[mid].lang[1] - secondChar;
305 (fcLangCharSets[mid].lang[2] != '\0' ||
308 cmp = FcStrCmpIgnoreCase(fcLangCharSets[mid].lang+2,
325 FcLangSetAdd (FcLangSet *ls, const FcChar8 *lang)
329 id = FcLangSetIndex (lang);
332 FcLangSetBitSet (ls, id);
337 ls->extra = FcStrSetCreate ();
341 return FcStrSetAdd (ls->extra, lang);
345 FcLangSetHasLang (const FcLangSet *ls, const FcChar8 *lang)
348 FcLangResult best, r;
351 id = FcLangSetIndex (lang);
354 else if (FcLangSetBitGet (ls, id))
356 best = FcLangDifferentLang;
357 for (i = id - 1; i >= 0; i--)
359 r = FcLangCompare (lang, fcLangCharSets[i].lang);
360 if (r == FcLangDifferentLang)
362 if (FcLangSetBitGet (ls, i) && r < best)
365 for (i = id; i < NUM_LANG_CHAR_SET; i++)
367 r = FcLangCompare (lang, fcLangCharSets[i].lang);
368 if (r == FcLangDifferentLang)
370 if (FcLangSetBitGet (ls, i) && r < best)
375 FcStrList *list = FcStrListCreate (ls->extra);
381 while (best > FcLangEqual && (extra = FcStrListNext (list)))
383 r = FcLangCompare (lang, extra);
387 FcStrListDone (list);
394 FcLangSetCompareStrSet (const FcLangSet *ls, FcStrSet *set)
396 FcStrList *list = FcStrListCreate (set);
397 FcLangResult r, best = FcLangDifferentLang;
402 while (best > FcLangEqual && (extra = FcStrListNext (list)))
404 r = FcLangSetHasLang (ls, extra);
408 FcStrListDone (list);
414 FcLangSetCompare (const FcLangSet *lsa, const FcLangSet *lsb)
417 FcLangResult best, r;
419 for (i = 0; i < NUM_LANG_SET_MAP; i++)
420 if (lsa->map[i] & lsb->map[i])
422 best = FcLangDifferentLang;
423 for (j = 0; j < NUM_COUNTRY_SET; j++)
424 for (i = 0; i < NUM_LANG_SET_MAP; i++)
425 if ((lsa->map[i] & fcLangCountrySets[j][i]) &&
426 (lsb->map[i] & fcLangCountrySets[j][i]))
428 best = FcLangDifferentCountry;
433 r = FcLangSetCompareStrSet (lsb, lsa->extra);
437 if (best > FcLangEqual && lsb->extra)
439 r = FcLangSetCompareStrSet (lsa, lsb->extra);
447 * Used in computing values -- mustn't allocate any storage
450 FcLangSetPromote (const FcChar8 *lang)
453 static FcStrSet strs;
457 memset (ls.map, '\0', sizeof (ls.map));
459 id = FcLangSetIndex (lang);
462 FcLangSetBitSet (&ls, id);
471 str = (FcChar8 *) lang;
477 FcLangSetHash (const FcLangSet *ls)
482 for (i = 0; i < NUM_LANG_SET_MAP; i++)
490 FcNameParseLangSet (const FcChar8 *string)
496 ls = FcLangSetCreate ();
502 for(i = 0; i < 31;i++)
505 if(c == '\0' || c == '|')
506 break; /* end of this code */
510 if (!FcLangSetAdd (ls, lang))
517 FcLangSetDestroy (ls);
523 FcNameUnparseLangSet (FcStrBuf *buf, const FcLangSet *ls)
527 FcBool first = FcTrue;
529 for (i = 0; i < NUM_LANG_SET_MAP; i++)
531 if ((bits = ls->map[i]))
533 for (bit = 0; bit <= 31; bit++)
534 if (bits & (1 << bit))
536 int id = (i << 5) | bit;
538 if (!FcStrBufChar (buf, '|'))
540 if (!FcStrBufString (buf, fcLangCharSets[id].lang))
548 FcStrList *list = FcStrListCreate (ls->extra);
553 while ((extra = FcStrListNext (list)))
556 if (!FcStrBufChar (buf, '|'))
558 if (!FcStrBufString (buf, extra))
567 FcLangSetEqual (const FcLangSet *lsa, const FcLangSet *lsb)
571 for (i = 0; i < NUM_LANG_SET_MAP; i++)
573 if (lsa->map[i] != lsb->map[i])
576 if (!lsa->extra && !lsb->extra)
578 if (lsa->extra && lsb->extra)
579 return FcStrSetEqual (lsa->extra, lsb->extra);
584 FcLangSetContainsLang (const FcLangSet *ls, const FcChar8 *lang)
589 id = FcLangSetIndex (lang);
592 else if (FcLangSetBitGet (ls, id))
595 * search up and down among equal languages for a match
597 for (i = id - 1; i >= 0; i--)
599 if (FcLangCompare (fcLangCharSets[i].lang, lang) == FcLangDifferentLang)
601 if (FcLangSetBitGet (ls, i) &&
602 FcLangContains (fcLangCharSets[i].lang, lang))
605 for (i = id; i < NUM_LANG_CHAR_SET; i++)
607 if (FcLangCompare (fcLangCharSets[i].lang, lang) == FcLangDifferentLang)
609 if (FcLangSetBitGet (ls, i) &&
610 FcLangContains (fcLangCharSets[i].lang, lang))
615 FcStrList *list = FcStrListCreate (ls->extra);
620 while ((extra = FcStrListNext (list)))
622 if (FcLangContains (extra, lang))
625 FcStrListDone (list);
634 * return FcTrue if lsa contains every language in lsb
637 FcLangSetContains (const FcLangSet *lsa, const FcLangSet *lsb)
642 if (FcDebug() & FC_DBG_MATCHV)
644 printf ("FcLangSet "); FcLangSetPrint (lsa);
645 printf (" contains "); FcLangSetPrint (lsb);
649 * check bitmaps for missing language support
651 for (i = 0; i < NUM_LANG_SET_MAP; i++)
653 missing = lsb->map[i] & ~lsa->map[i];
656 for (j = 0; j < 32; j++)
657 if (missing & (1 << j))
659 if (!FcLangSetContainsLang (lsa,
660 fcLangCharSets[i*32 + j].lang))
662 if (FcDebug() & FC_DBG_MATCHV)
663 printf ("\tMissing bitmap %s\n", fcLangCharSets[i*32+j].lang);
671 FcStrList *list = FcStrListCreate (lsb->extra);
676 while ((extra = FcStrListNext (list)))
678 if (!FcLangSetContainsLang (lsa, extra))
680 if (FcDebug() & FC_DBG_MATCHV)
681 printf ("\tMissing string %s\n", extra);
685 FcStrListDone (list);
693 static FcLangSet ** langsets = 0;
694 static int langset_bank_count = 0, langset_ptr = 0, langset_count = 0;
697 FcLangSetNewBank (void)
702 /* ideally, should only write one copy of any particular FcLangSet */
704 FcLangSetNeededBytes (const FcLangSet *l)
707 return sizeof (FcLangSet);
711 FcLangSetEnsureBank (int bi)
713 if (!langsets || bi >= langset_bank_count)
715 int new_count = langset_bank_count + 2;
718 tt = realloc(langsets, new_count * sizeof(FcLangSet *));
723 for (i = langset_bank_count; i < new_count; i++)
725 langset_bank_count = new_count;
732 FcLangSetDistributeBytes (FcCache * metadata, void * block_ptr)
734 int bi = FcCacheBankToIndex(metadata->bank);
735 if (!FcLangSetEnsureBank(bi))
738 langsets[bi] = block_ptr;
739 block_ptr = (void *)((char *)block_ptr +
740 langset_count * sizeof(FcLangSet));
742 metadata->langset_count = langset_count;
747 FcLangSetSerialize(int bank, FcLangSet *l)
749 int p = langset_ptr, bi = FcCacheBankToIndex(bank);
753 langsets[bi][langset_ptr] = *l;
754 langsets[bi][langset_ptr].extra = 0;
756 return &langsets[bi][p];
760 FcLangSetUnserialize (FcCache metadata, void *block_ptr)
762 int bi = FcCacheBankToIndex(metadata.bank);
763 if (!FcLangSetEnsureBank(bi))
766 FcMemAlloc (FC_MEM_LANGSET, metadata.langset_count * sizeof(FcLangSet));
767 langsets[bi] = (FcLangSet *)block_ptr;
768 block_ptr = (void *)((char *)block_ptr +
769 metadata.langset_count * sizeof(FcLangSet));