]>
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.
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
) &&
71 fcLangCharSets
[i
].charset
.leaves
!= exclusiveCharset
->leaves
)
75 missing
= FcCharSetSubtractCount (&fcLangCharSets
[i
].charset
, charset
);
76 if (FcDebug() & FC_DBG_SCANV
)
78 if (missing
&& missing
< 10)
80 FcCharSet
*missed
= FcCharSetSubtract (&fcLangCharSets
[i
].charset
,
83 FcChar32 map
[FC_CHARSET_MAP_SIZE
];
86 printf ("\n%s(%d) ", fcLangCharSets
[i
].lang
, missing
);
88 for (ucs4
= FcCharSetFirstPage (missed
, map
, &next
);
89 ucs4
!= FC_CHARSET_DONE
;
90 ucs4
= FcCharSetNextPage (missed
, map
, &next
))
93 for (i
= 0; i
< FC_CHARSET_MAP_SIZE
; i
++)
96 for (j
= 0; j
< 32; j
++)
97 if (map
[i
] & (1 << j
))
98 printf (" %04x", ucs4
+ i
* 32 + j
);
102 FcCharSetDestroy (missed
);
105 printf ("%s(%d) ", fcLangCharSets
[i
].lang
, missing
);
108 FcLangSetBitSet (ls
, i
);
111 if (FcDebug() & FC_DBG_SCANV
)
118 #define FcLangEnd(c) ((c) == '-' || (c) == '\0')
121 FcLangCompare (const FcChar8
*s1
, const FcChar8
*s2
)
124 FcLangResult result
= FcLangDifferentLang
;
135 if (FcLangEnd (c1
) && FcLangEnd (c2
))
136 result
= FcLangDifferentCountry
;
142 result
= FcLangDifferentCountry
;
147 * Return FcTrue when s1 contains s2.
149 * s1 contains s2 if s1 equals s2 or if s1 is a
150 * language with a country and s2 is just a language
154 FcLangContains (const FcChar8
*s1
, const FcChar8
*s2
)
167 /* see if s1 has a country while s2 is mising one */
168 if (c1
== '-' && c2
== '\0')
178 FcCharSetForLang (const FcChar8
*lang
)
182 for (i
= 0; i
< NUM_LANG_CHAR_SET
; i
++)
184 switch (FcLangCompare (lang
, fcLangCharSets
[i
].lang
)) {
186 return &fcLangCharSets
[i
].charset
;
187 case FcLangDifferentCountry
:
196 return &fcLangCharSets
[i
].charset
;
200 FcLangSetCreate (void)
204 ls
= malloc (sizeof (FcLangSet
));
207 FcMemAlloc (FC_MEM_LANGSET
, sizeof (FcLangSet
));
208 memset (ls
->map
, '\0', sizeof (ls
->map
));
214 FcLangSetDestroy (FcLangSet
*ls
)
217 FcStrSetDestroy (ls
->extra
);
218 FcMemFree (FC_MEM_LANGSET
, sizeof (FcLangSet
));
223 FcLangSetCopy (const FcLangSet
*ls
)
227 new = FcLangSetCreate ();
230 memcpy (new->map
, ls
->map
, sizeof (new->map
));
236 new->extra
= FcStrSetCreate ();
240 list
= FcStrListCreate (ls
->extra
);
244 while ((extra
= FcStrListNext (list
)))
245 if (!FcStrSetAdd (new->extra
, extra
))
247 FcStrListDone (list
);
250 FcStrListDone (list
);
254 FcLangSetDestroy (new);
260 FcLangSetIndex (const FcChar8
*lang
)
262 int low
, high
, mid
= 0;
264 FcChar8 firstChar
= FcToLower(lang
[0]);
265 FcChar8 secondChar
= firstChar
? FcToLower(lang
[1]) : '\0';
270 high
= fcLangCharSetRanges
[0].begin
;
272 else if(firstChar
> 'z')
274 low
= fcLangCharSetRanges
[25].begin
;
275 high
= NUM_LANG_CHAR_SET
- 1;
279 low
= fcLangCharSetRanges
[firstChar
- 'a'].begin
;
280 high
= fcLangCharSetRanges
[firstChar
- 'a'].end
;
283 return -low
; /* next entry after where it would be */
288 mid
= (high
+ low
) >> 1;
289 if(fcLangCharSets
[mid
].lang
[0] != firstChar
)
290 cmp
= FcStrCmpIgnoreCase(fcLangCharSets
[mid
].lang
, lang
);
292 { /* fast path for resolving 2-letter languages (by far the most common) after
293 * finding the first char (probably already true because of the hash table) */
294 cmp
= fcLangCharSets
[mid
].lang
[1] - secondChar
;
296 (fcLangCharSets
[mid
].lang
[2] != '\0' ||
299 cmp
= FcStrCmpIgnoreCase(fcLangCharSets
[mid
].lang
+2,
316 FcLangSetAdd (FcLangSet
*ls
, const FcChar8
*lang
)
320 id
= FcLangSetIndex (lang
);
323 FcLangSetBitSet (ls
, id
);
328 ls
->extra
= FcStrSetCreate ();
332 return FcStrSetAdd (ls
->extra
, lang
);
336 FcLangSetHasLang (const FcLangSet
*ls
, const FcChar8
*lang
)
339 FcLangResult best
, r
;
342 id
= FcLangSetIndex (lang
);
345 else if (FcLangSetBitGet (ls
, id
))
347 best
= FcLangDifferentLang
;
348 for (i
= id
- 1; i
>= 0; i
--)
350 r
= FcLangCompare (lang
, fcLangCharSets
[i
].lang
);
351 if (r
== FcLangDifferentLang
)
353 if (FcLangSetBitGet (ls
, i
) && r
< best
)
356 for (i
= id
; i
< NUM_LANG_CHAR_SET
; i
++)
358 r
= FcLangCompare (lang
, fcLangCharSets
[i
].lang
);
359 if (r
== FcLangDifferentLang
)
361 if (FcLangSetBitGet (ls
, i
) && r
< best
)
366 FcStrList
*list
= FcStrListCreate (ls
->extra
);
372 while (best
> FcLangEqual
&& (extra
= FcStrListNext (list
)))
374 r
= FcLangCompare (lang
, extra
);
378 FcStrListDone (list
);
385 FcLangSetCompareStrSet (const FcLangSet
*ls
, FcStrSet
*set
)
387 FcStrList
*list
= FcStrListCreate (set
);
388 FcLangResult r
, best
= FcLangDifferentLang
;
393 while (best
> FcLangEqual
&& (extra
= FcStrListNext (list
)))
395 r
= FcLangSetHasLang (ls
, extra
);
399 FcStrListDone (list
);
405 FcLangSetCompare (const FcLangSet
*lsa
, const FcLangSet
*lsb
)
408 FcLangResult best
, r
;
410 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
411 if (lsa
->map
[i
] & lsb
->map
[i
])
413 best
= FcLangDifferentLang
;
414 for (j
= 0; j
< NUM_COUNTRY_SET
; j
++)
415 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
416 if ((lsa
->map
[i
] & fcLangCountrySets
[j
][i
]) &&
417 (lsb
->map
[i
] & fcLangCountrySets
[j
][i
]))
419 best
= FcLangDifferentCountry
;
424 r
= FcLangSetCompareStrSet (lsb
, lsa
->extra
);
428 if (best
> FcLangEqual
&& lsb
->extra
)
430 r
= FcLangSetCompareStrSet (lsa
, lsb
->extra
);
438 * Used in computing values -- mustn't allocate any storage
441 FcLangSetPromote (const FcChar8
*lang
)
444 static FcStrSet strs
;
448 memset (ls
.map
, '\0', sizeof (ls
.map
));
450 id
= FcLangSetIndex (lang
);
453 FcLangSetBitSet (&ls
, id
);
462 str
= (FcChar8
*) lang
;
468 FcLangSetHash (const FcLangSet
*ls
)
473 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
481 FcNameParseLangSet (const FcChar8
*string
)
487 ls
= FcLangSetCreate ();
493 for(i
= 0; i
< 31;i
++)
496 if(c
== '\0' || c
== '|')
497 break; /* end of this code */
501 if (!FcLangSetAdd (ls
, lang
))
508 FcLangSetDestroy (ls
);
514 FcNameUnparseLangSet (FcStrBuf
*buf
, const FcLangSet
*ls
)
518 FcBool first
= FcTrue
;
520 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
522 if ((bits
= ls
->map
[i
]))
524 for (bit
= 0; bit
<= 31; bit
++)
525 if (bits
& (1 << bit
))
527 int id
= (i
<< 5) | bit
;
529 if (!FcStrBufChar (buf
, '|'))
531 if (!FcStrBufString (buf
, fcLangCharSets
[id
].lang
))
539 FcStrList
*list
= FcStrListCreate (ls
->extra
);
544 while ((extra
= FcStrListNext (list
)))
547 if (!FcStrBufChar (buf
, '|'))
549 if (!FcStrBufString (buf
, extra
))
558 FcLangSetEqual (const FcLangSet
*lsa
, const FcLangSet
*lsb
)
562 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
564 if (lsa
->map
[i
] != lsb
->map
[i
])
567 if (!lsa
->extra
&& !lsb
->extra
)
569 if (lsa
->extra
&& lsb
->extra
)
570 return FcStrSetEqual (lsa
->extra
, lsb
->extra
);
575 FcLangSetContainsLang (const FcLangSet
*ls
, const FcChar8
*lang
)
580 id
= FcLangSetIndex (lang
);
583 else if (FcLangSetBitGet (ls
, id
))
586 * search up and down among equal languages for a match
588 for (i
= id
- 1; i
>= 0; i
--)
590 if (FcLangCompare (fcLangCharSets
[i
].lang
, lang
) == FcLangDifferentLang
)
592 if (FcLangSetBitGet (ls
, i
) &&
593 FcLangContains (fcLangCharSets
[i
].lang
, lang
))
596 for (i
= id
; i
< NUM_LANG_CHAR_SET
; i
++)
598 if (FcLangCompare (fcLangCharSets
[i
].lang
, lang
) == FcLangDifferentLang
)
600 if (FcLangSetBitGet (ls
, i
) &&
601 FcLangContains (fcLangCharSets
[i
].lang
, lang
))
606 FcStrList
*list
= FcStrListCreate (ls
->extra
);
611 while ((extra
= FcStrListNext (list
)))
613 if (FcLangContains (extra
, lang
))
616 FcStrListDone (list
);
625 * return FcTrue if lsa contains every language in lsb
628 FcLangSetContains (const FcLangSet
*lsa
, const FcLangSet
*lsb
)
633 if (FcDebug() & FC_DBG_MATCHV
)
635 printf ("FcLangSet "); FcLangSetPrint (lsa
);
636 printf (" contains "); FcLangSetPrint (lsb
);
640 * check bitmaps for missing language support
642 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
644 missing
= lsb
->map
[i
] & ~lsa
->map
[i
];
647 for (j
= 0; j
< 32; j
++)
648 if (missing
& (1 << j
))
650 if (!FcLangSetContainsLang (lsa
,
651 fcLangCharSets
[i
*32 + j
].lang
))
653 if (FcDebug() & FC_DBG_MATCHV
)
654 printf ("\tMissing bitmap %s\n", fcLangCharSets
[i
*32+j
].lang
);
662 FcStrList
*list
= FcStrListCreate (lsb
->extra
);
667 while ((extra
= FcStrListNext (list
)))
669 if (!FcLangSetContainsLang (lsa
, extra
))
671 if (FcDebug() & FC_DBG_MATCHV
)
672 printf ("\tMissing string %s\n", extra
);
676 FcStrListDone (list
);