]>
git.wh0rd.org - fontconfig.git/blob - src/fclang.c
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.
28 const FcChar8 lang
[8];
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
= FcLangGetCharSet (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
= FcLangDifferentTerritory
;
159 result
= FcLangDifferentTerritory
;
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 FcLangGetCharSet (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 FcLangDifferentTerritory
:
218 return &fcLangCharSets
[country
].charset
;
227 langs
= FcStrSetCreate();
231 for (i
= 0; i
< NUM_LANG_CHAR_SET
; i
++)
232 FcStrSetAdd (langs
, fcLangCharSets
[i
].lang
);
238 FcLangSetCreate (void)
242 ls
= malloc (sizeof (FcLangSet
));
245 FcMemAlloc (FC_MEM_LANGSET
, sizeof (FcLangSet
));
246 memset (ls
->map
, '\0', sizeof (ls
->map
));
252 FcLangSetDestroy (FcLangSet
*ls
)
255 FcStrSetDestroy (ls
->extra
);
256 FcMemFree (FC_MEM_LANGSET
, sizeof (FcLangSet
));
261 FcLangSetCopy (const FcLangSet
*ls
)
265 new = FcLangSetCreate ();
268 memcpy (new->map
, ls
->map
, sizeof (new->map
));
274 new->extra
= FcStrSetCreate ();
278 list
= FcStrListCreate (ls
->extra
);
282 while ((extra
= FcStrListNext (list
)))
283 if (!FcStrSetAdd (new->extra
, extra
))
285 FcStrListDone (list
);
288 FcStrListDone (list
);
292 FcLangSetDestroy (new);
298 FcLangSetIndex (const FcChar8
*lang
)
300 int low
, high
, mid
= 0;
302 FcChar8 firstChar
= FcToLower(lang
[0]);
303 FcChar8 secondChar
= firstChar
? FcToLower(lang
[1]) : '\0';
308 high
= fcLangCharSetRanges
[0].begin
;
310 else if(firstChar
> 'z')
312 low
= fcLangCharSetRanges
[25].begin
;
313 high
= NUM_LANG_CHAR_SET
- 1;
317 low
= fcLangCharSetRanges
[firstChar
- 'a'].begin
;
318 high
= fcLangCharSetRanges
[firstChar
- 'a'].end
;
321 return -low
; /* next entry after where it would be */
326 mid
= (high
+ low
) >> 1;
327 if(fcLangCharSets
[mid
].lang
[0] != firstChar
)
328 cmp
= FcStrCmpIgnoreCase(fcLangCharSets
[mid
].lang
, lang
);
330 { /* fast path for resolving 2-letter languages (by far the most common) after
331 * finding the first char (probably already true because of the hash table) */
332 cmp
= fcLangCharSets
[mid
].lang
[1] - secondChar
;
334 (fcLangCharSets
[mid
].lang
[2] != '\0' ||
337 cmp
= FcStrCmpIgnoreCase(fcLangCharSets
[mid
].lang
+2,
354 FcLangSetAdd (FcLangSet
*ls
, const FcChar8
*lang
)
358 id
= FcLangSetIndex (lang
);
361 FcLangSetBitSet (ls
, id
);
366 ls
->extra
= FcStrSetCreate ();
370 return FcStrSetAdd (ls
->extra
, lang
);
374 FcLangSetHasLang (const FcLangSet
*ls
, const FcChar8
*lang
)
377 FcLangResult best
, r
;
380 id
= FcLangSetIndex (lang
);
383 else if (FcLangSetBitGet (ls
, id
))
385 best
= FcLangDifferentLang
;
386 for (i
= id
- 1; i
>= 0; i
--)
388 r
= FcLangCompare (lang
, fcLangCharSets
[i
].lang
);
389 if (r
== FcLangDifferentLang
)
391 if (FcLangSetBitGet (ls
, i
) && r
< best
)
394 for (i
= id
; i
< NUM_LANG_CHAR_SET
; i
++)
396 r
= FcLangCompare (lang
, fcLangCharSets
[i
].lang
);
397 if (r
== FcLangDifferentLang
)
399 if (FcLangSetBitGet (ls
, i
) && r
< best
)
404 FcStrList
*list
= FcStrListCreate (ls
->extra
);
409 while (best
> FcLangEqual
&& (extra
= FcStrListNext (list
)))
411 r
= FcLangCompare (lang
, extra
);
415 FcStrListDone (list
);
422 FcLangSetCompareStrSet (const FcLangSet
*ls
, FcStrSet
*set
)
424 FcStrList
*list
= FcStrListCreate (set
);
425 FcLangResult r
, best
= FcLangDifferentLang
;
430 while (best
> FcLangEqual
&& (extra
= FcStrListNext (list
)))
432 r
= FcLangSetHasLang (ls
, extra
);
436 FcStrListDone (list
);
442 FcLangSetCompare (const FcLangSet
*lsa
, const FcLangSet
*lsb
)
445 FcLangResult best
, r
;
447 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
448 if (lsa
->map
[i
] & lsb
->map
[i
])
450 best
= FcLangDifferentLang
;
451 for (j
= 0; j
< NUM_COUNTRY_SET
; j
++)
452 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
453 if ((lsa
->map
[i
] & fcLangCountrySets
[j
][i
]) &&
454 (lsb
->map
[i
] & fcLangCountrySets
[j
][i
]))
456 best
= FcLangDifferentTerritory
;
461 r
= FcLangSetCompareStrSet (lsb
, lsa
->extra
);
465 if (best
> FcLangEqual
&& lsb
->extra
)
467 r
= FcLangSetCompareStrSet (lsa
, lsb
->extra
);
475 * Used in computing values -- mustn't allocate any storage
478 FcLangSetPromote (const FcChar8
*lang
)
481 static FcStrSet strs
;
485 memset (ls
.map
, '\0', sizeof (ls
.map
));
487 id
= FcLangSetIndex (lang
);
490 FcLangSetBitSet (&ls
, id
);
499 str
= (FcChar8
*) lang
;
505 FcLangSetHash (const FcLangSet
*ls
)
510 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
518 FcNameParseLangSet (const FcChar8
*string
)
524 ls
= FcLangSetCreate ();
530 for(i
= 0; i
< 31;i
++)
533 if(c
== '\0' || c
== '|')
534 break; /* end of this code */
538 if (!FcLangSetAdd (ls
, lang
))
545 FcLangSetDestroy (ls
);
551 FcNameUnparseLangSet (FcStrBuf
*buf
, const FcLangSet
*ls
)
555 FcBool first
= FcTrue
;
557 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
559 if ((bits
= ls
->map
[i
]))
561 for (bit
= 0; bit
<= 31; bit
++)
562 if (bits
& (1 << bit
))
564 int id
= (i
<< 5) | bit
;
566 if (!FcStrBufChar (buf
, '|'))
568 if (!FcStrBufString (buf
, fcLangCharSets
[id
].lang
))
576 FcStrList
*list
= FcStrListCreate (ls
->extra
);
581 while ((extra
= FcStrListNext (list
)))
584 if (!FcStrBufChar (buf
, '|'))
586 FcStrListDone (list
);
589 if (!FcStrBufString (buf
, extra
))
591 FcStrListDone (list
);
596 FcStrListDone (list
);
602 FcLangSetEqual (const FcLangSet
*lsa
, const FcLangSet
*lsb
)
606 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
608 if (lsa
->map
[i
] != lsb
->map
[i
])
611 if (!lsa
->extra
&& !lsb
->extra
)
613 if (lsa
->extra
&& lsb
->extra
)
614 return FcStrSetEqual (lsa
->extra
, lsb
->extra
);
619 FcLangSetContainsLang (const FcLangSet
*ls
, const FcChar8
*lang
)
624 id
= FcLangSetIndex (lang
);
627 else if (FcLangSetBitGet (ls
, id
))
630 * search up and down among equal languages for a match
632 for (i
= id
- 1; i
>= 0; i
--)
634 if (FcLangCompare (fcLangCharSets
[i
].lang
, lang
) == FcLangDifferentLang
)
636 if (FcLangSetBitGet (ls
, i
) &&
637 FcLangContains (fcLangCharSets
[i
].lang
, lang
))
640 for (i
= id
; i
< NUM_LANG_CHAR_SET
; i
++)
642 if (FcLangCompare (fcLangCharSets
[i
].lang
, lang
) == FcLangDifferentLang
)
644 if (FcLangSetBitGet (ls
, i
) &&
645 FcLangContains (fcLangCharSets
[i
].lang
, lang
))
650 FcStrList
*list
= FcStrListCreate (ls
->extra
);
655 while ((extra
= FcStrListNext (list
)))
657 if (FcLangContains (extra
, lang
))
660 FcStrListDone (list
);
669 * return FcTrue if lsa contains every language in lsb
672 FcLangSetContains (const FcLangSet
*lsa
, const FcLangSet
*lsb
)
677 if (FcDebug() & FC_DBG_MATCHV
)
679 printf ("FcLangSet "); FcLangSetPrint (lsa
);
680 printf (" contains "); FcLangSetPrint (lsb
);
684 * check bitmaps for missing language support
686 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
688 missing
= lsb
->map
[i
] & ~lsa
->map
[i
];
691 for (j
= 0; j
< 32; j
++)
692 if (missing
& (1 << j
))
694 if (!FcLangSetContainsLang (lsa
,
695 fcLangCharSets
[i
*32 + j
].lang
))
697 if (FcDebug() & FC_DBG_MATCHV
)
698 printf ("\tMissing bitmap %s\n", fcLangCharSets
[i
*32+j
].lang
);
706 FcStrList
*list
= FcStrListCreate (lsb
->extra
);
711 while ((extra
= FcStrListNext (list
)))
713 if (!FcLangSetContainsLang (lsa
, extra
))
715 if (FcDebug() & FC_DBG_MATCHV
)
716 printf ("\tMissing string %s\n", extra
);
720 FcStrListDone (list
);
729 FcLangSetSerializeAlloc (FcSerialize
*serialize
, const FcLangSet
*l
)
731 if (!FcSerializeAlloc (serialize
, l
, sizeof (FcLangSet
)))
737 FcLangSetSerialize(FcSerialize
*serialize
, const FcLangSet
*l
)
739 FcLangSet
*l_serialize
= FcSerializePtr (serialize
, l
);
747 #include "fcaliastail.h"