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.
29 const FcCharSet charset;
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;
57 exclusiveCharset = FcCharSetForLang (exclusiveLang);
58 ls = FcLangSetCreate ();
61 if (FcDebug() & FC_DBG_LANGSET)
63 printf ("font charset\n");
64 FcCharSetPrint (charset);
67 for (i = 0; i < NUM_LANG_CHAR_SET; i++)
69 if (FcDebug() & FC_DBG_LANGSET)
71 printf ("%s charset\n", fcLangCharSets[i].lang);
72 FcCharSetPrint (&fcLangCharSets[i].charset);
77 * Check for Han charsets to make fonts
78 * which advertise support for a single language
79 * not support other Han languages
81 if (exclusiveCharset &&
82 FcFreeTypeIsExclusiveLang (fcLangCharSets[i].lang))
84 if (fcLangCharSets[i].charset.num != exclusiveCharset->num)
87 for (j = 0; j < fcLangCharSets[i].charset.num; j++)
88 if (FcCharSetLeaf(&fcLangCharSets[i].charset, j) !=
89 FcCharSetLeaf(exclusiveCharset, j))
92 missing = FcCharSetSubtractCount (&fcLangCharSets[i].charset, charset);
93 if (FcDebug() & FC_DBG_SCANV)
95 if (missing && missing < 10)
97 FcCharSet *missed = FcCharSetSubtract (&fcLangCharSets[i].charset,
100 FcChar32 map[FC_CHARSET_MAP_SIZE];
103 printf ("\n%s(%u) ", fcLangCharSets[i].lang, missing);
105 for (ucs4 = FcCharSetFirstPage (missed, map, &next);
106 ucs4 != FC_CHARSET_DONE;
107 ucs4 = FcCharSetNextPage (missed, map, &next))
110 for (i = 0; i < FC_CHARSET_MAP_SIZE; i++)
113 for (j = 0; j < 32; j++)
114 if (map[i] & (1 << j))
115 printf (" %04x", ucs4 + i * 32 + j);
119 FcCharSetDestroy (missed);
122 printf ("%s(%u) ", fcLangCharSets[i].lang, missing);
125 FcLangSetBitSet (ls, i);
128 if (FcDebug() & FC_DBG_SCANV)
135 #define FcLangEnd(c) ((c) == '-' || (c) == '\0')
138 FcLangCompare (const FcChar8 *s1, const FcChar8 *s2)
141 FcLangResult result = FcLangDifferentLang;
152 if (FcLangEnd (c1) && FcLangEnd (c2))
153 result = FcLangDifferentCountry;
159 result = FcLangDifferentCountry;
164 * Return FcTrue when super contains sub.
166 * super contains sub if super and sub have the same
167 * language and either the same country or one
168 * is missing the country
172 FcLangContains (const FcChar8 *super, const FcChar8 *sub)
185 /* see if super has a country while sub is mising one */
186 if (c1 == '-' && c2 == '\0')
188 /* see if sub has a country while super is mising one */
189 if (c1 == '\0' && c2 == '-')
199 FcCharSetForLang (const FcChar8 *lang)
204 for (i = 0; i < NUM_LANG_CHAR_SET; i++)
206 switch (FcLangCompare (lang, fcLangCharSets[i].lang)) {
208 return &fcLangCharSets[i].charset;
209 case FcLangDifferentCountry:
218 return &fcLangCharSets[country].charset;
222 FcLangSetCreate (void)
226 ls = malloc (sizeof (FcLangSet));
229 FcMemAlloc (FC_MEM_LANGSET, sizeof (FcLangSet));
230 memset (ls->map, '\0', sizeof (ls->map));
236 FcLangSetDestroy (FcLangSet *ls)
239 FcStrSetDestroy (ls->extra);
240 FcMemFree (FC_MEM_LANGSET, sizeof (FcLangSet));
245 FcLangSetCopy (const FcLangSet *ls)
249 new = FcLangSetCreate ();
252 memcpy (new->map, ls->map, sizeof (new->map));
258 new->extra = FcStrSetCreate ();
262 list = FcStrListCreate (ls->extra);
266 while ((extra = FcStrListNext (list)))
267 if (!FcStrSetAdd (new->extra, extra))
269 FcStrListDone (list);
272 FcStrListDone (list);
276 FcLangSetDestroy (new);
282 FcLangSetIndex (const FcChar8 *lang)
284 int low, high, mid = 0;
286 FcChar8 firstChar = FcToLower(lang[0]);
287 FcChar8 secondChar = firstChar ? FcToLower(lang[1]) : '\0';
292 high = fcLangCharSetRanges[0].begin;
294 else if(firstChar > 'z')
296 low = fcLangCharSetRanges[25].begin;
297 high = NUM_LANG_CHAR_SET - 1;
301 low = fcLangCharSetRanges[firstChar - 'a'].begin;
302 high = fcLangCharSetRanges[firstChar - 'a'].end;
305 return -low; /* next entry after where it would be */
310 mid = (high + low) >> 1;
311 if(fcLangCharSets[mid].lang[0] != firstChar)
312 cmp = FcStrCmpIgnoreCase(fcLangCharSets[mid].lang, lang);
314 { /* fast path for resolving 2-letter languages (by far the most common) after
315 * finding the first char (probably already true because of the hash table) */
316 cmp = fcLangCharSets[mid].lang[1] - secondChar;
318 (fcLangCharSets[mid].lang[2] != '\0' ||
321 cmp = FcStrCmpIgnoreCase(fcLangCharSets[mid].lang+2,
338 FcLangSetAdd (FcLangSet *ls, const FcChar8 *lang)
342 id = FcLangSetIndex (lang);
345 FcLangSetBitSet (ls, id);
350 ls->extra = FcStrSetCreate ();
354 return FcStrSetAdd (ls->extra, lang);
358 FcLangSetHasLang (const FcLangSet *ls, const FcChar8 *lang)
361 FcLangResult best, r;
364 id = FcLangSetIndex (lang);
367 else if (FcLangSetBitGet (ls, id))
369 best = FcLangDifferentLang;
370 for (i = id - 1; i >= 0; i--)
372 r = FcLangCompare (lang, fcLangCharSets[i].lang);
373 if (r == FcLangDifferentLang)
375 if (FcLangSetBitGet (ls, i) && r < best)
378 for (i = id; i < NUM_LANG_CHAR_SET; i++)
380 r = FcLangCompare (lang, fcLangCharSets[i].lang);
381 if (r == FcLangDifferentLang)
383 if (FcLangSetBitGet (ls, i) && r < best)
388 FcStrList *list = FcStrListCreate (ls->extra);
393 while (best > FcLangEqual && (extra = FcStrListNext (list)))
395 r = FcLangCompare (lang, extra);
399 FcStrListDone (list);
406 FcLangSetCompareStrSet (const FcLangSet *ls, FcStrSet *set)
408 FcStrList *list = FcStrListCreate (set);
409 FcLangResult r, best = FcLangDifferentLang;
414 while (best > FcLangEqual && (extra = FcStrListNext (list)))
416 r = FcLangSetHasLang (ls, extra);
420 FcStrListDone (list);
426 FcLangSetCompare (const FcLangSet *lsa, const FcLangSet *lsb)
429 FcLangResult best, r;
431 for (i = 0; i < NUM_LANG_SET_MAP; i++)
432 if (lsa->map[i] & lsb->map[i])
434 best = FcLangDifferentLang;
435 for (j = 0; j < NUM_COUNTRY_SET; j++)
436 for (i = 0; i < NUM_LANG_SET_MAP; i++)
437 if ((lsa->map[i] & fcLangCountrySets[j][i]) &&
438 (lsb->map[i] & fcLangCountrySets[j][i]))
440 best = FcLangDifferentCountry;
445 r = FcLangSetCompareStrSet (lsb, lsa->extra);
449 if (best > FcLangEqual && lsb->extra)
451 r = FcLangSetCompareStrSet (lsa, lsb->extra);
459 * Used in computing values -- mustn't allocate any storage
462 FcLangSetPromote (const FcChar8 *lang)
465 static FcStrSet strs;
469 memset (ls.map, '\0', sizeof (ls.map));
471 id = FcLangSetIndex (lang);
474 FcLangSetBitSet (&ls, id);
483 str = (FcChar8 *) lang;
489 FcLangSetHash (const FcLangSet *ls)
494 for (i = 0; i < NUM_LANG_SET_MAP; i++)
502 FcNameParseLangSet (const FcChar8 *string)
508 ls = FcLangSetCreate ();
514 for(i = 0; i < 31;i++)
517 if(c == '\0' || c == '|')
518 break; /* end of this code */
522 if (!FcLangSetAdd (ls, lang))
529 FcLangSetDestroy (ls);
535 FcNameUnparseLangSet (FcStrBuf *buf, const FcLangSet *ls)
539 FcBool first = FcTrue;
541 for (i = 0; i < NUM_LANG_SET_MAP; i++)
543 if ((bits = ls->map[i]))
545 for (bit = 0; bit <= 31; bit++)
546 if (bits & (1 << bit))
548 int id = (i << 5) | bit;
550 if (!FcStrBufChar (buf, '|'))
552 if (!FcStrBufString (buf, fcLangCharSets[id].lang))
560 FcStrList *list = FcStrListCreate (ls->extra);
565 while ((extra = FcStrListNext (list)))
568 if (!FcStrBufChar (buf, '|'))
570 FcStrListDone (list);
573 if (!FcStrBufString (buf, extra))
575 FcStrListDone (list);
580 FcStrListDone (list);
586 FcLangSetEqual (const FcLangSet *lsa, const FcLangSet *lsb)
590 for (i = 0; i < NUM_LANG_SET_MAP; i++)
592 if (lsa->map[i] != lsb->map[i])
595 if (!lsa->extra && !lsb->extra)
597 if (lsa->extra && lsb->extra)
598 return FcStrSetEqual (lsa->extra, lsb->extra);
603 FcLangSetContainsLang (const FcLangSet *ls, const FcChar8 *lang)
608 id = FcLangSetIndex (lang);
611 else if (FcLangSetBitGet (ls, id))
614 * search up and down among equal languages for a match
616 for (i = id - 1; i >= 0; i--)
618 if (FcLangCompare (fcLangCharSets[i].lang, lang) == FcLangDifferentLang)
620 if (FcLangSetBitGet (ls, i) &&
621 FcLangContains (fcLangCharSets[i].lang, lang))
624 for (i = id; i < NUM_LANG_CHAR_SET; i++)
626 if (FcLangCompare (fcLangCharSets[i].lang, lang) == FcLangDifferentLang)
628 if (FcLangSetBitGet (ls, i) &&
629 FcLangContains (fcLangCharSets[i].lang, lang))
634 FcStrList *list = FcStrListCreate (ls->extra);
639 while ((extra = FcStrListNext (list)))
641 if (FcLangContains (extra, lang))
644 FcStrListDone (list);
653 * return FcTrue if lsa contains every language in lsb
656 FcLangSetContains (const FcLangSet *lsa, const FcLangSet *lsb)
661 if (FcDebug() & FC_DBG_MATCHV)
663 printf ("FcLangSet "); FcLangSetPrint (lsa);
664 printf (" contains "); FcLangSetPrint (lsb);
668 * check bitmaps for missing language support
670 for (i = 0; i < NUM_LANG_SET_MAP; i++)
672 missing = lsb->map[i] & ~lsa->map[i];
675 for (j = 0; j < 32; j++)
676 if (missing & (1 << j))
678 if (!FcLangSetContainsLang (lsa,
679 fcLangCharSets[i*32 + j].lang))
681 if (FcDebug() & FC_DBG_MATCHV)
682 printf ("\tMissing bitmap %s\n", fcLangCharSets[i*32+j].lang);
690 FcStrList *list = FcStrListCreate (lsb->extra);
695 while ((extra = FcStrListNext (list)))
697 if (!FcLangSetContainsLang (lsa, extra))
699 if (FcDebug() & FC_DBG_MATCHV)
700 printf ("\tMissing string %s\n", extra);
704 FcStrListDone (list);
713 FcLangSetSerializeAlloc (FcSerialize *serialize, const FcLangSet *l)
715 if (!FcSerializeAlloc (serialize, l, sizeof (FcLangSet)))
721 FcLangSetSerialize(FcSerialize *serialize, const FcLangSet *l)
723 FcLangSet *l_serialize = FcSerializePtr (serialize, l);