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)
47 static FcBool langsets_populated
= FcFalse
;
50 FcFreeTypeLangSet (const FcCharSet
*charset
,
51 const FcChar8
*exclusiveLang
)
55 const FcCharSet
*exclusiveCharset
= 0;
58 if (!langsets_populated
)
60 FcLangCharSetPopulate ();
61 langsets_populated
= FcTrue
;
65 exclusiveCharset
= FcCharSetForLang (exclusiveLang
);
66 ls
= FcLangSetCreate ();
69 for (i
= 0; i
< NUM_LANG_CHAR_SET
; i
++)
72 * Check for Han charsets to make fonts
73 * which advertise support for a single language
74 * not support other Han languages
76 if (exclusiveCharset
&&
77 FcFreeTypeIsExclusiveLang (fcLangCharSets
[i
].lang
))
79 if (fcLangCharSets
[i
].charset
.num
!= exclusiveCharset
->num
)
82 for (j
= 0; j
< fcLangCharSets
[i
].charset
.num
; j
++)
83 if (FcCharSetGetLeaf(&fcLangCharSets
[i
].charset
, j
) !=
84 FcCharSetGetLeaf(exclusiveCharset
, j
))
87 missing
= FcCharSetSubtractCount (&fcLangCharSets
[i
].charset
, charset
);
88 if (FcDebug() & FC_DBG_SCANV
)
90 if (missing
&& missing
< 10)
92 FcCharSet
*missed
= FcCharSetSubtract (&fcLangCharSets
[i
].charset
,
95 FcChar32 map
[FC_CHARSET_MAP_SIZE
];
98 printf ("\n%s(%d) ", fcLangCharSets
[i
].lang
, missing
);
100 for (ucs4
= FcCharSetFirstPage (missed
, map
, &next
);
101 ucs4
!= FC_CHARSET_DONE
;
102 ucs4
= FcCharSetNextPage (missed
, map
, &next
))
105 for (i
= 0; i
< FC_CHARSET_MAP_SIZE
; i
++)
108 for (j
= 0; j
< 32; j
++)
109 if (map
[i
] & (1 << j
))
110 printf (" %04x", ucs4
+ i
* 32 + j
);
114 FcCharSetDestroy (missed
);
117 printf ("%s(%d) ", fcLangCharSets
[i
].lang
, missing
);
120 FcLangSetBitSet (ls
, i
);
123 if (FcDebug() & FC_DBG_SCANV
)
130 #define FcLangEnd(c) ((c) == '-' || (c) == '\0')
133 FcLangCompare (const FcChar8
*s1
, const FcChar8
*s2
)
136 FcLangResult result
= FcLangDifferentLang
;
147 if (FcLangEnd (c1
) && FcLangEnd (c2
))
148 result
= FcLangDifferentCountry
;
154 result
= FcLangDifferentCountry
;
159 * Return FcTrue when super contains sub.
161 * super contains sub if super and sub have the same
162 * language and either the same country or one
163 * is missing the country
167 FcLangContains (const FcChar8
*super
, const FcChar8
*sub
)
180 /* see if super has a country while sub is mising one */
181 if (c1
== '-' && c2
== '\0')
183 /* see if sub has a country while super is mising one */
184 if (c1
== '\0' && c2
== '-')
194 FcCharSetForLang (const FcChar8
*lang
)
199 if (!langsets_populated
)
201 FcLangCharSetPopulate ();
202 langsets_populated
= FcTrue
;
205 for (i
= 0; i
< NUM_LANG_CHAR_SET
; i
++)
207 switch (FcLangCompare (lang
, fcLangCharSets
[i
].lang
)) {
209 return &fcLangCharSets
[i
].charset
;
210 case FcLangDifferentCountry
:
219 return &fcLangCharSets
[i
].charset
;
223 FcLangSetCreate (void)
227 ls
= malloc (sizeof (FcLangSet
));
230 FcMemAlloc (FC_MEM_LANGSET
, sizeof (FcLangSet
));
231 memset (ls
->map
, '\0', sizeof (ls
->map
));
237 FcLangSetDestroy (FcLangSet
*ls
)
240 FcStrSetDestroy (ls
->extra
);
241 FcMemFree (FC_MEM_LANGSET
, sizeof (FcLangSet
));
246 FcLangSetCopy (const FcLangSet
*ls
)
250 new = FcLangSetCreate ();
253 memcpy (new->map
, ls
->map
, sizeof (new->map
));
259 new->extra
= FcStrSetCreate ();
263 list
= FcStrListCreate (ls
->extra
);
267 while ((extra
= FcStrListNext (list
)))
268 if (!FcStrSetAdd (new->extra
, extra
))
270 FcStrListDone (list
);
273 FcStrListDone (list
);
277 FcLangSetDestroy (new);
283 FcLangSetIndex (const FcChar8
*lang
)
285 int low
, high
, mid
= 0;
287 FcChar8 firstChar
= FcToLower(lang
[0]);
288 FcChar8 secondChar
= firstChar
? FcToLower(lang
[1]) : '\0';
293 high
= fcLangCharSetRanges
[0].begin
;
295 else if(firstChar
> 'z')
297 low
= fcLangCharSetRanges
[25].begin
;
298 high
= NUM_LANG_CHAR_SET
- 1;
302 low
= fcLangCharSetRanges
[firstChar
- 'a'].begin
;
303 high
= fcLangCharSetRanges
[firstChar
- 'a'].end
;
306 return -low
; /* next entry after where it would be */
311 mid
= (high
+ low
) >> 1;
312 if(fcLangCharSets
[mid
].lang
[0] != firstChar
)
313 cmp
= FcStrCmpIgnoreCase(fcLangCharSets
[mid
].lang
, lang
);
315 { /* fast path for resolving 2-letter languages (by far the most common) after
316 * finding the first char (probably already true because of the hash table) */
317 cmp
= fcLangCharSets
[mid
].lang
[1] - secondChar
;
319 (fcLangCharSets
[mid
].lang
[2] != '\0' ||
322 cmp
= FcStrCmpIgnoreCase(fcLangCharSets
[mid
].lang
+2,
339 FcLangSetAdd (FcLangSet
*ls
, const FcChar8
*lang
)
343 id
= FcLangSetIndex (lang
);
346 FcLangSetBitSet (ls
, id
);
351 ls
->extra
= FcStrSetCreate ();
355 return FcStrSetAdd (ls
->extra
, lang
);
359 FcLangSetHasLang (const FcLangSet
*ls
, const FcChar8
*lang
)
362 FcLangResult best
, r
;
365 id
= FcLangSetIndex (lang
);
368 else if (FcLangSetBitGet (ls
, id
))
370 best
= FcLangDifferentLang
;
371 for (i
= id
- 1; i
>= 0; i
--)
373 r
= FcLangCompare (lang
, fcLangCharSets
[i
].lang
);
374 if (r
== FcLangDifferentLang
)
376 if (FcLangSetBitGet (ls
, i
) && r
< best
)
379 for (i
= id
; i
< NUM_LANG_CHAR_SET
; i
++)
381 r
= FcLangCompare (lang
, fcLangCharSets
[i
].lang
);
382 if (r
== FcLangDifferentLang
)
384 if (FcLangSetBitGet (ls
, i
) && r
< best
)
389 FcStrList
*list
= FcStrListCreate (ls
->extra
);
395 while (best
> FcLangEqual
&& (extra
= FcStrListNext (list
)))
397 r
= FcLangCompare (lang
, extra
);
401 FcStrListDone (list
);
408 FcLangSetCompareStrSet (const FcLangSet
*ls
, FcStrSet
*set
)
410 FcStrList
*list
= FcStrListCreate (set
);
411 FcLangResult r
, best
= FcLangDifferentLang
;
416 while (best
> FcLangEqual
&& (extra
= FcStrListNext (list
)))
418 r
= FcLangSetHasLang (ls
, extra
);
422 FcStrListDone (list
);
428 FcLangSetCompare (const FcLangSet
*lsa
, const FcLangSet
*lsb
)
431 FcLangResult best
, r
;
433 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
434 if (lsa
->map
[i
] & lsb
->map
[i
])
436 best
= FcLangDifferentLang
;
437 for (j
= 0; j
< NUM_COUNTRY_SET
; j
++)
438 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
439 if ((lsa
->map
[i
] & fcLangCountrySets
[j
][i
]) &&
440 (lsb
->map
[i
] & fcLangCountrySets
[j
][i
]))
442 best
= FcLangDifferentCountry
;
447 r
= FcLangSetCompareStrSet (lsb
, lsa
->extra
);
451 if (best
> FcLangEqual
&& lsb
->extra
)
453 r
= FcLangSetCompareStrSet (lsa
, lsb
->extra
);
461 * Used in computing values -- mustn't allocate any storage
464 FcLangSetPromote (const FcChar8
*lang
)
467 static FcStrSet strs
;
471 memset (ls
.map
, '\0', sizeof (ls
.map
));
473 id
= FcLangSetIndex (lang
);
476 FcLangSetBitSet (&ls
, id
);
485 str
= (FcChar8
*) lang
;
491 FcLangSetHash (const FcLangSet
*ls
)
496 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
504 FcNameParseLangSet (const FcChar8
*string
)
510 ls
= FcLangSetCreate ();
516 for(i
= 0; i
< 31;i
++)
519 if(c
== '\0' || c
== '|')
520 break; /* end of this code */
524 if (!FcLangSetAdd (ls
, lang
))
531 FcLangSetDestroy (ls
);
537 FcNameUnparseLangSet (FcStrBuf
*buf
, const FcLangSet
*ls
)
541 FcBool first
= FcTrue
;
543 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
545 if ((bits
= ls
->map
[i
]))
547 for (bit
= 0; bit
<= 31; bit
++)
548 if (bits
& (1 << bit
))
550 int id
= (i
<< 5) | bit
;
552 if (!FcStrBufChar (buf
, '|'))
554 if (!FcStrBufString (buf
, fcLangCharSets
[id
].lang
))
562 FcStrList
*list
= FcStrListCreate (ls
->extra
);
567 while ((extra
= FcStrListNext (list
)))
570 if (!FcStrBufChar (buf
, '|'))
572 if (!FcStrBufString (buf
, extra
))
581 FcLangSetEqual (const FcLangSet
*lsa
, const FcLangSet
*lsb
)
585 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
587 if (lsa
->map
[i
] != lsb
->map
[i
])
590 if (!lsa
->extra
&& !lsb
->extra
)
592 if (lsa
->extra
&& lsb
->extra
)
593 return FcStrSetEqual (lsa
->extra
, lsb
->extra
);
598 FcLangSetContainsLang (const FcLangSet
*ls
, const FcChar8
*lang
)
603 id
= FcLangSetIndex (lang
);
606 else if (FcLangSetBitGet (ls
, id
))
609 * search up and down among equal languages for a match
611 for (i
= id
- 1; i
>= 0; i
--)
613 if (FcLangCompare (fcLangCharSets
[i
].lang
, lang
) == FcLangDifferentLang
)
615 if (FcLangSetBitGet (ls
, i
) &&
616 FcLangContains (fcLangCharSets
[i
].lang
, lang
))
619 for (i
= id
; i
< NUM_LANG_CHAR_SET
; i
++)
621 if (FcLangCompare (fcLangCharSets
[i
].lang
, lang
) == FcLangDifferentLang
)
623 if (FcLangSetBitGet (ls
, i
) &&
624 FcLangContains (fcLangCharSets
[i
].lang
, lang
))
629 FcStrList
*list
= FcStrListCreate (ls
->extra
);
634 while ((extra
= FcStrListNext (list
)))
636 if (FcLangContains (extra
, lang
))
639 FcStrListDone (list
);
648 * return FcTrue if lsa contains every language in lsb
651 FcLangSetContains (const FcLangSet
*lsa
, const FcLangSet
*lsb
)
656 if (FcDebug() & FC_DBG_MATCHV
)
658 printf ("FcLangSet "); FcLangSetPrint (lsa
);
659 printf (" contains "); FcLangSetPrint (lsb
);
663 * check bitmaps for missing language support
665 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
667 missing
= lsb
->map
[i
] & ~lsa
->map
[i
];
670 for (j
= 0; j
< 32; j
++)
671 if (missing
& (1 << j
))
673 if (!FcLangSetContainsLang (lsa
,
674 fcLangCharSets
[i
*32 + j
].lang
))
676 if (FcDebug() & FC_DBG_MATCHV
)
677 printf ("\tMissing bitmap %s\n", fcLangCharSets
[i
*32+j
].lang
);
685 FcStrList
*list
= FcStrListCreate (lsb
->extra
);
690 while ((extra
= FcStrListNext (list
)))
692 if (!FcLangSetContainsLang (lsa
, extra
))
694 if (FcDebug() & FC_DBG_MATCHV
)
695 printf ("\tMissing string %s\n", extra
);
699 FcStrListDone (list
);
707 static FcLangSet
** langsets
= 0;
708 static int langset_bank_count
= 0, langset_ptr
= 0, langset_count
= 0;
711 FcLangSetNewBank (void)
716 /* ideally, should only write one copy of any particular FcLangSet */
718 FcLangSetNeededBytes (const FcLangSet
*l
)
721 return sizeof (FcLangSet
);
725 FcLangSetNeededBytesAlign (void)
727 return __alignof__ (FcLangSet
);
731 FcLangSetEnsureBank (int bi
)
733 if (!langsets
|| bi
>= langset_bank_count
)
735 int new_count
= langset_bank_count
+ 2;
738 tt
= realloc(langsets
, new_count
* sizeof(FcLangSet
*));
743 for (i
= langset_bank_count
; i
< new_count
; i
++)
745 langset_bank_count
= new_count
;
752 FcLangSetDistributeBytes (FcCache
* metadata
, void * block_ptr
)
754 int bi
= FcCacheBankToIndex(metadata
->bank
);
755 if (!FcLangSetEnsureBank(bi
))
758 block_ptr
= ALIGN(block_ptr
, FcLangSet
);
759 langsets
[bi
] = block_ptr
;
760 block_ptr
= (void *)((char *)block_ptr
+
761 langset_count
* sizeof(FcLangSet
));
763 metadata
->langset_count
= langset_count
;
768 FcLangSetSerialize(int bank
, FcLangSet
*l
)
770 int p
= langset_ptr
, bi
= FcCacheBankToIndex(bank
);
774 langsets
[bi
][langset_ptr
] = *l
;
775 langsets
[bi
][langset_ptr
].extra
= 0;
777 return &langsets
[bi
][p
];
781 FcLangSetUnserialize (FcCache
* metadata
, void *block_ptr
)
783 int bi
= FcCacheBankToIndex(metadata
->bank
);
784 if (!FcLangSetEnsureBank(bi
))
787 FcMemAlloc (FC_MEM_LANGSET
, metadata
->langset_count
* sizeof(FcLangSet
));
788 block_ptr
= ALIGN(block_ptr
, FcLangSet
);
789 langsets
[bi
] = (FcLangSet
*)block_ptr
;
790 block_ptr
= (void *)((char *)block_ptr
+
791 metadata
->langset_count
* sizeof(FcLangSet
));