]>
git.wh0rd.org - fontconfig.git/blob - src/fclang.c
2 * fontconfig/src/fclang.c
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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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 FcChar8 lang
[8];
30 const FcCharSet charset
;
38 #include "../fc-lang/fclang.h"
43 FcChar32 map
[NUM_LANG_SET_MAP
];
47 FcLangSetBitSet (FcLangSet
*ls
,
52 id
= fcLangCharSetIndices
[id
];
54 if (bucket
>= ls
->map_size
)
55 return; /* shouldn't happen really */
57 ls
->map
[bucket
] |= ((FcChar32
) 1 << (id
& 0x1f));
61 FcLangSetBitGet (const FcLangSet
*ls
,
66 id
= fcLangCharSetIndices
[id
];
68 if (bucket
>= ls
->map_size
)
71 return ((ls
->map
[bucket
] >> (id
& 0x1f)) & 1) ? FcTrue
: FcFalse
;
75 FcLangSetBitReset (FcLangSet
*ls
,
80 id
= fcLangCharSetIndices
[id
];
82 if (bucket
>= ls
->map_size
)
83 return; /* shouldn't happen really */
85 ls
->map
[bucket
] &= ~((FcChar32
) 1 << (id
& 0x1f));
89 FcFreeTypeLangSet (const FcCharSet
*charset
,
90 const FcChar8
*exclusiveLang
)
94 const FcCharSet
*exclusiveCharset
= 0;
98 exclusiveCharset
= FcLangGetCharSet (exclusiveLang
);
99 ls
= FcLangSetCreate ();
102 if (FcDebug() & FC_DBG_LANGSET
)
104 printf ("font charset");
105 FcCharSetPrint (charset
);
108 for (i
= 0; i
< NUM_LANG_CHAR_SET
; i
++)
110 if (FcDebug() & FC_DBG_LANGSET
)
112 printf ("%s charset", fcLangCharSets
[i
].lang
);
113 FcCharSetPrint (&fcLangCharSets
[i
].charset
);
118 * Check for Han charsets to make fonts
119 * which advertise support for a single language
120 * not support other Han languages
122 if (exclusiveCharset
&&
123 FcFreeTypeIsExclusiveLang (fcLangCharSets
[i
].lang
))
125 if (fcLangCharSets
[i
].charset
.num
!= exclusiveCharset
->num
)
128 for (j
= 0; j
< fcLangCharSets
[i
].charset
.num
; j
++)
129 if (FcCharSetLeaf(&fcLangCharSets
[i
].charset
, j
) !=
130 FcCharSetLeaf(exclusiveCharset
, j
))
133 missing
= FcCharSetSubtractCount (&fcLangCharSets
[i
].charset
, charset
);
134 if (FcDebug() & FC_DBG_SCANV
)
136 if (missing
&& missing
< 10)
138 FcCharSet
*missed
= FcCharSetSubtract (&fcLangCharSets
[i
].charset
,
141 FcChar32 map
[FC_CHARSET_MAP_SIZE
];
144 printf ("\n%s(%u) ", fcLangCharSets
[i
].lang
, missing
);
146 for (ucs4
= FcCharSetFirstPage (missed
, map
, &next
);
147 ucs4
!= FC_CHARSET_DONE
;
148 ucs4
= FcCharSetNextPage (missed
, map
, &next
))
151 for (i
= 0; i
< FC_CHARSET_MAP_SIZE
; i
++)
154 for (j
= 0; j
< 32; j
++)
155 if (map
[i
] & (1 << j
))
156 printf (" %04x", ucs4
+ i
* 32 + j
);
160 FcCharSetDestroy (missed
);
163 printf ("%s(%u) ", fcLangCharSets
[i
].lang
, missing
);
166 FcLangSetBitSet (ls
, i
);
169 if (FcDebug() & FC_DBG_SCANV
)
176 #define FcLangEnd(c) ((c) == '-' || (c) == '\0')
179 FcLangCompare (const FcChar8
*s1
, const FcChar8
*s2
)
182 FcLangResult result
= FcLangDifferentLang
;
193 if (FcLangEnd (c1
) && FcLangEnd (c2
))
194 result
= FcLangDifferentTerritory
;
200 result
= FcLangDifferentTerritory
;
205 * Return FcTrue when super contains sub.
207 * super contains sub if super and sub have the same
208 * language and either the same country or one
209 * is missing the country
213 FcLangContains (const FcChar8
*super
, const FcChar8
*sub
)
226 /* see if super has a country while sub is mising one */
227 if (c1
== '-' && c2
== '\0')
229 /* see if sub has a country while super is mising one */
230 if (c1
== '\0' && c2
== '-')
240 FcLangGetCharSet (const FcChar8
*lang
)
245 for (i
= 0; i
< NUM_LANG_CHAR_SET
; i
++)
247 switch (FcLangCompare (lang
, fcLangCharSets
[i
].lang
)) {
249 return &fcLangCharSets
[i
].charset
;
250 case FcLangDifferentTerritory
:
253 case FcLangDifferentLang
:
260 return &fcLangCharSets
[country
].charset
;
269 langs
= FcStrSetCreate();
273 for (i
= 0; i
< NUM_LANG_CHAR_SET
; i
++)
274 FcStrSetAdd (langs
, fcLangCharSets
[i
].lang
);
280 FcLangSetCreate (void)
284 ls
= malloc (sizeof (FcLangSet
));
287 FcMemAlloc (FC_MEM_LANGSET
, sizeof (FcLangSet
));
288 memset (ls
->map
, '\0', sizeof (ls
->map
));
289 ls
->map_size
= NUM_LANG_SET_MAP
;
295 FcLangSetDestroy (FcLangSet
*ls
)
298 FcStrSetDestroy (ls
->extra
);
299 FcMemFree (FC_MEM_LANGSET
, sizeof (FcLangSet
));
304 FcLangSetCopy (const FcLangSet
*ls
)
308 new = FcLangSetCreate ();
311 memset (new->map
, '\0', sizeof (new->map
));
312 memcpy (new->map
, ls
->map
, FC_MIN (sizeof (new->map
), ls
->map_size
* sizeof (ls
->map
[0])));
318 new->extra
= FcStrSetCreate ();
322 list
= FcStrListCreate (ls
->extra
);
326 while ((extra
= FcStrListNext (list
)))
327 if (!FcStrSetAdd (new->extra
, extra
))
329 FcStrListDone (list
);
332 FcStrListDone (list
);
336 FcLangSetDestroy (new);
342 FcLangSetIndex (const FcChar8
*lang
)
344 int low
, high
, mid
= 0;
346 FcChar8 firstChar
= FcToLower(lang
[0]);
347 FcChar8 secondChar
= firstChar
? FcToLower(lang
[1]) : '\0';
352 high
= fcLangCharSetRanges
[0].begin
;
354 else if(firstChar
> 'z')
356 low
= fcLangCharSetRanges
[25].begin
;
357 high
= NUM_LANG_CHAR_SET
- 1;
361 low
= fcLangCharSetRanges
[firstChar
- 'a'].begin
;
362 high
= fcLangCharSetRanges
[firstChar
- 'a'].end
;
365 return -low
; /* next entry after where it would be */
370 mid
= (high
+ low
) >> 1;
371 if(fcLangCharSets
[mid
].lang
[0] != firstChar
)
372 cmp
= FcStrCmpIgnoreCase(fcLangCharSets
[mid
].lang
, lang
);
374 { /* fast path for resolving 2-letter languages (by far the most common) after
375 * finding the first char (probably already true because of the hash table) */
376 cmp
= fcLangCharSets
[mid
].lang
[1] - secondChar
;
378 (fcLangCharSets
[mid
].lang
[2] != '\0' ||
381 cmp
= FcStrCmpIgnoreCase(fcLangCharSets
[mid
].lang
+2,
398 FcLangSetAdd (FcLangSet
*ls
, const FcChar8
*lang
)
402 id
= FcLangSetIndex (lang
);
405 FcLangSetBitSet (ls
, id
);
410 ls
->extra
= FcStrSetCreate ();
414 return FcStrSetAdd (ls
->extra
, lang
);
418 FcLangSetDel (FcLangSet
*ls
, const FcChar8
*lang
)
422 id
= FcLangSetIndex (lang
);
425 FcLangSetBitReset (ls
, id
);
429 FcStrSetDel (ls
->extra
, lang
);
435 FcLangSetHasLang (const FcLangSet
*ls
, const FcChar8
*lang
)
438 FcLangResult best
, r
;
441 id
= FcLangSetIndex (lang
);
444 else if (FcLangSetBitGet (ls
, id
))
446 best
= FcLangDifferentLang
;
447 for (i
= id
- 1; i
>= 0; i
--)
449 r
= FcLangCompare (lang
, fcLangCharSets
[i
].lang
);
450 if (r
== FcLangDifferentLang
)
452 if (FcLangSetBitGet (ls
, i
) && r
< best
)
455 for (i
= id
; i
< NUM_LANG_CHAR_SET
; i
++)
457 r
= FcLangCompare (lang
, fcLangCharSets
[i
].lang
);
458 if (r
== FcLangDifferentLang
)
460 if (FcLangSetBitGet (ls
, i
) && r
< best
)
465 FcStrList
*list
= FcStrListCreate (ls
->extra
);
470 while (best
> FcLangEqual
&& (extra
= FcStrListNext (list
)))
472 r
= FcLangCompare (lang
, extra
);
476 FcStrListDone (list
);
483 FcLangSetCompareStrSet (const FcLangSet
*ls
, FcStrSet
*set
)
485 FcStrList
*list
= FcStrListCreate (set
);
486 FcLangResult r
, best
= FcLangDifferentLang
;
491 while (best
> FcLangEqual
&& (extra
= FcStrListNext (list
)))
493 r
= FcLangSetHasLang (ls
, extra
);
497 FcStrListDone (list
);
503 FcLangSetCompare (const FcLangSet
*lsa
, const FcLangSet
*lsb
)
506 FcLangResult best
, r
;
508 count
= FC_MIN (lsa
->map_size
, lsb
->map_size
);
509 count
= FC_MIN (NUM_LANG_SET_MAP
, count
);
510 for (i
= 0; i
< count
; i
++)
511 if (lsa
->map
[i
] & lsb
->map
[i
])
513 best
= FcLangDifferentLang
;
514 for (j
= 0; j
< NUM_COUNTRY_SET
; j
++)
515 for (i
= 0; i
< count
; i
++)
516 if ((lsa
->map
[i
] & fcLangCountrySets
[j
][i
]) &&
517 (lsb
->map
[i
] & fcLangCountrySets
[j
][i
]))
519 best
= FcLangDifferentTerritory
;
524 r
= FcLangSetCompareStrSet (lsb
, lsa
->extra
);
528 if (best
> FcLangEqual
&& lsb
->extra
)
530 r
= FcLangSetCompareStrSet (lsa
, lsb
->extra
);
538 * Used in computing values -- mustn't allocate any storage
539 * XXX Not thread-safe
542 FcLangSetPromote (const FcChar8
*lang
)
545 static FcStrSet strs
;
549 memset (ls
.map
, '\0', sizeof (ls
.map
));
550 ls
.map_size
= NUM_LANG_SET_MAP
;
552 id
= FcLangSetIndex (lang
);
555 FcLangSetBitSet (&ls
, id
);
564 str
= (FcChar8
*) lang
;
570 FcLangSetHash (const FcLangSet
*ls
)
575 count
= FC_MIN (ls
->map_size
, NUM_LANG_SET_MAP
);
576 for (i
= 0; i
< count
; i
++)
584 FcNameParseLangSet (const FcChar8
*string
)
586 FcChar8 lang
[32], c
= 0;
590 ls
= FcLangSetCreate ();
596 for(i
= 0; i
< 31;i
++)
599 if(c
== '\0' || c
== '|')
600 break; /* end of this code */
604 if (!FcLangSetAdd (ls
, lang
))
611 FcLangSetDestroy (ls
);
617 FcNameUnparseLangSet (FcStrBuf
*buf
, const FcLangSet
*ls
)
621 FcBool first
= FcTrue
;
623 count
= FC_MIN (ls
->map_size
, NUM_LANG_SET_MAP
);
624 for (i
= 0; i
< count
; i
++)
626 if ((bits
= ls
->map
[i
]))
628 for (bit
= 0; bit
<= 31; bit
++)
629 if (bits
& (1 << bit
))
631 int id
= (i
<< 5) | bit
;
633 if (!FcStrBufChar (buf
, '|'))
635 if (!FcStrBufString (buf
, fcLangCharSets
[fcLangCharSetIndicesInv
[id
]].lang
))
643 FcStrList
*list
= FcStrListCreate (ls
->extra
);
648 while ((extra
= FcStrListNext (list
)))
651 if (!FcStrBufChar (buf
, '|'))
653 FcStrListDone (list
);
656 if (!FcStrBufString (buf
, extra
))
658 FcStrListDone (list
);
663 FcStrListDone (list
);
669 FcLangSetEqual (const FcLangSet
*lsa
, const FcLangSet
*lsb
)
673 count
= FC_MIN (lsa
->map_size
, lsb
->map_size
);
674 count
= FC_MIN (NUM_LANG_SET_MAP
, count
);
675 for (i
= 0; i
< count
; i
++)
677 if (lsa
->map
[i
] != lsb
->map
[i
])
680 if (!lsa
->extra
&& !lsb
->extra
)
682 if (lsa
->extra
&& lsb
->extra
)
683 return FcStrSetEqual (lsa
->extra
, lsb
->extra
);
688 FcLangSetContainsLang (const FcLangSet
*ls
, const FcChar8
*lang
)
693 id
= FcLangSetIndex (lang
);
696 else if (FcLangSetBitGet (ls
, id
))
699 * search up and down among equal languages for a match
701 for (i
= id
- 1; i
>= 0; i
--)
703 if (FcLangCompare (fcLangCharSets
[i
].lang
, lang
) == FcLangDifferentLang
)
705 if (FcLangSetBitGet (ls
, i
) &&
706 FcLangContains (fcLangCharSets
[i
].lang
, lang
))
709 for (i
= id
; i
< NUM_LANG_CHAR_SET
; i
++)
711 if (FcLangCompare (fcLangCharSets
[i
].lang
, lang
) == FcLangDifferentLang
)
713 if (FcLangSetBitGet (ls
, i
) &&
714 FcLangContains (fcLangCharSets
[i
].lang
, lang
))
719 FcStrList
*list
= FcStrListCreate (ls
->extra
);
724 while ((extra
= FcStrListNext (list
)))
726 if (FcLangContains (extra
, lang
))
729 FcStrListDone (list
);
738 * return FcTrue if lsa contains every language in lsb
741 FcLangSetContains (const FcLangSet
*lsa
, const FcLangSet
*lsb
)
746 if (FcDebug() & FC_DBG_MATCHV
)
748 printf ("FcLangSet "); FcLangSetPrint (lsa
);
749 printf (" contains "); FcLangSetPrint (lsb
);
753 * check bitmaps for missing language support
755 count
= FC_MIN (lsa
->map_size
, lsb
->map_size
);
756 count
= FC_MIN (NUM_LANG_SET_MAP
, count
);
757 for (i
= 0; i
< count
; i
++)
759 missing
= lsb
->map
[i
] & ~lsa
->map
[i
];
762 for (j
= 0; j
< 32; j
++)
763 if (missing
& (1 << j
))
765 if (!FcLangSetContainsLang (lsa
,
766 fcLangCharSets
[fcLangCharSetIndicesInv
[i
*32 + j
]].lang
))
768 if (FcDebug() & FC_DBG_MATCHV
)
769 printf ("\tMissing bitmap %s\n", fcLangCharSets
[fcLangCharSetIndicesInv
[i
*32+j
]].lang
);
777 FcStrList
*list
= FcStrListCreate (lsb
->extra
);
782 while ((extra
= FcStrListNext (list
)))
784 if (!FcLangSetContainsLang (lsa
, extra
))
786 if (FcDebug() & FC_DBG_MATCHV
)
787 printf ("\tMissing string %s\n", extra
);
791 FcStrListDone (list
);
800 FcLangSetSerializeAlloc (FcSerialize
*serialize
, const FcLangSet
*l
)
802 if (!FcSerializeAlloc (serialize
, l
, sizeof (FcLangSet
)))
808 FcLangSetSerialize(FcSerialize
*serialize
, const FcLangSet
*l
)
810 FcLangSet
*l_serialize
= FcSerializePtr (serialize
, l
);
814 memset (l_serialize
->map
, '\0', sizeof (l_serialize
->map
));
815 memcpy (l_serialize
->map
, l
->map
, FC_MIN (sizeof (l_serialize
->map
), l
->map_size
* sizeof (l
->map
[0])));
816 l_serialize
->map_size
= NUM_LANG_SET_MAP
;
817 l_serialize
->extra
= NULL
; /* We don't serialize ls->extra */
822 FcLangSetGetLangs (const FcLangSet
*ls
)
827 langs
= FcStrSetCreate();
831 for (i
= 0; i
< NUM_LANG_CHAR_SET
; i
++)
832 if (FcLangSetBitGet (ls
, i
))
833 FcStrSetAdd (langs
, fcLangCharSets
[i
].lang
);
837 FcStrList
*list
= FcStrListCreate (ls
->extra
);
842 while ((extra
= FcStrListNext (list
)))
843 FcStrSetAdd (langs
, extra
);
845 FcStrListDone (list
);
853 FcLangSetOperate(const FcLangSet
*a
,
855 FcBool (*func
) (FcLangSet
*ls
,
858 FcLangSet
*langset
= FcLangSetCopy (a
);
859 FcStrList
*sl
= FcStrListCreate (FcLangSetGetLangs (b
));
862 while ((str
= FcStrListNext (sl
)))
872 FcLangSetUnion (const FcLangSet
*a
, const FcLangSet
*b
)
874 return FcLangSetOperate(a
, b
, FcLangSetAdd
);
878 FcLangSetSubtract (const FcLangSet
*a
, const FcLangSet
*b
)
880 return FcLangSetOperate(a
, b
, FcLangSetDel
);
884 #include "fcaliastail.h"
885 #include "fcftaliastail.h"