]>
git.wh0rd.org - fontconfig.git/blob - src/fclang.c
2 * $XFree86: xc/lib/fontconfig/src/fclang.c,v 1.6 2002/08/22 18:53:22 keithp Exp $
4 * Copyright © 2002 Keith Packard, member of The XFree86 Project, Inc.
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.
32 #include "../fc-lang/fclang.h"
34 #define NUM_LANG_CHAR_SET (sizeof (fcLangCharSets) / sizeof (fcLangCharSets[0]))
35 #define NUM_LANG_SET_MAP ((NUM_LANG_CHAR_SET + 31) / 32)
38 FcChar32 map
[NUM_LANG_SET_MAP
];
42 #define FcLangSetBitSet(ls, id) ((ls)->map[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
43 #define FcLangSetBitGet(ls, id) (((ls)->map[(id)>>5] >> ((id) & 0x1f)) & 1)
46 FcFreeTypeLangSet (const FcCharSet
*charset
,
47 const FcChar8
*exclusiveLang
)
51 const FcCharSet
*exclusiveCharset
= 0;
56 exclusiveCharset
= FcCharSetForLang (exclusiveLang
);
57 ls
= FcLangSetCreate ();
60 for (i
= 0; i
< NUM_LANG_CHAR_SET
; i
++)
63 * Check for Han charsets to make fonts
64 * which advertise support for a single language
65 * not support other Han languages
67 if (exclusiveCharset
&&
68 FcFreeTypeIsExclusiveLang (fcLangCharSets
[i
].lang
) &&
69 fcLangCharSets
[i
].charset
.leaves
!= exclusiveCharset
->leaves
)
73 missing
= FcCharSetSubtractCount (&fcLangCharSets
[i
].charset
, charset
);
74 if (FcDebug() & FC_DBG_SCANV
)
76 if (missing
&& missing
< 10)
78 FcCharSet
*missed
= FcCharSetSubtract (&fcLangCharSets
[i
].charset
,
81 FcChar32 map
[FC_CHARSET_MAP_SIZE
];
84 printf ("\n%s(%d) ", fcLangCharSets
[i
].lang
, missing
);
86 for (ucs4
= FcCharSetFirstPage (missed
, map
, &next
);
87 ucs4
!= FC_CHARSET_DONE
;
88 ucs4
= FcCharSetNextPage (missed
, map
, &next
))
91 for (i
= 0; i
< FC_CHARSET_MAP_SIZE
; i
++)
94 for (j
= 0; j
< 32; j
++)
95 if (map
[i
] & (1 << j
))
96 printf (" %04x", ucs4
+ i
* 32 + j
);
100 FcCharSetDestroy (missed
);
103 printf ("%s(%d) ", fcLangCharSets
[i
].lang
, missing
);
106 FcLangSetBitSet (ls
, i
);
109 if (FcDebug() & FC_DBG_SCANV
)
116 #define FcLangEnd(c) ((c) == '-' || (c) == '\0')
119 FcLangCompare (const FcChar8
*s1
, const FcChar8
*s2
)
122 FcLangResult result
= FcLangDifferentLang
;
133 if (FcLangEnd (c1
) && FcLangEnd (c2
))
134 result
= FcLangDifferentCountry
;
140 result
= FcLangDifferentCountry
;
145 FcCharSetForLang (const FcChar8
*lang
)
149 for (i
= 0; i
< NUM_LANG_CHAR_SET
; i
++)
151 switch (FcLangCompare (lang
, fcLangCharSets
[i
].lang
)) {
153 return &fcLangCharSets
[i
].charset
;
154 case FcLangDifferentCountry
:
163 return &fcLangCharSets
[i
].charset
;
167 FcLangSetCreate (void)
171 ls
= malloc (sizeof (FcLangSet
));
174 FcMemAlloc (FC_MEM_LANGSET
, sizeof (FcLangSet
));
175 memset (ls
->map
, '\0', sizeof (ls
->map
));
181 FcLangSetDestroy (FcLangSet
*ls
)
184 FcStrSetDestroy (ls
->extra
);
185 FcMemFree (FC_MEM_LANGSET
, sizeof (FcLangSet
));
190 FcLangSetCopy (const FcLangSet
*ls
)
194 new = FcLangSetCreate ();
197 memcpy (new->map
, ls
->map
, sizeof (new->map
));
203 new->extra
= FcStrSetCreate ();
207 list
= FcStrListCreate (ls
->extra
);
211 while ((extra
= FcStrListNext (list
)))
212 if (!FcStrSetAdd (new->extra
, extra
))
214 FcStrListDone (list
);
217 FcStrListDone (list
);
221 FcLangSetDestroy (new);
227 FcLangSetIndex (const FcChar8
*lang
)
233 high
= NUM_LANG_CHAR_SET
- 1;
236 mid
= (high
+ low
) >> 1;
237 cmp
= FcStrCmpIgnoreCase (fcLangCharSets
[mid
].lang
, lang
);
251 FcLangSetAdd (FcLangSet
*ls
, const FcChar8
*lang
)
255 id
= FcLangSetIndex (lang
);
258 FcLangSetBitSet (ls
, id
);
263 ls
->extra
= FcStrSetCreate ();
267 return FcStrSetAdd (ls
->extra
, lang
);
271 FcLangSetHasLang (const FcLangSet
*ls
, const FcChar8
*lang
)
274 FcLangResult best
, r
;
277 id
= FcLangSetIndex (lang
);
280 else if (FcLangSetBitGet (ls
, id
))
282 best
= FcLangDifferentLang
;
283 for (i
= id
- 1; i
>= 0; i
--)
285 r
= FcLangCompare (lang
, fcLangCharSets
[i
].lang
);
286 if (r
== FcLangDifferentLang
)
288 if (FcLangSetBitGet (ls
, i
) && r
< best
)
291 for (i
= id
; i
< NUM_LANG_CHAR_SET
; i
++)
293 r
= FcLangCompare (lang
, fcLangCharSets
[i
].lang
);
294 if (r
== FcLangDifferentLang
)
296 if (FcLangSetBitGet (ls
, i
) && r
< best
)
301 FcStrList
*list
= FcStrListCreate (ls
->extra
);
307 while (best
> FcLangEqual
&& (extra
= FcStrListNext (list
)))
309 r
= FcLangCompare (lang
, extra
);
313 FcStrListDone (list
);
320 FcLangSetCompareStrSet (const FcLangSet
*ls
, FcStrSet
*set
)
322 FcStrList
*list
= FcStrListCreate (set
);
323 FcLangResult r
, best
= FcLangDifferentLang
;
328 while (best
> FcLangEqual
&& (extra
= FcStrListNext (list
)))
330 r
= FcLangSetHasLang (ls
, extra
);
334 FcStrListDone (list
);
340 FcLangSetCompare (const FcLangSet
*lsa
, const FcLangSet
*lsb
)
343 FcLangResult best
, r
;
345 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
346 if (lsa
->map
[i
] & lsb
->map
[i
])
348 best
= FcLangDifferentLang
;
351 r
= FcLangSetCompareStrSet (lsb
, lsa
->extra
);
355 if (best
> FcLangEqual
&& lsb
->extra
)
357 r
= FcLangSetCompareStrSet (lsa
, lsb
->extra
);
365 * Used in computing values -- mustn't allocate any storage
368 FcLangSetPromote (const FcChar8
*lang
)
371 static FcStrSet strs
;
375 memset (ls
.map
, '\0', sizeof (ls
.map
));
377 id
= FcLangSetIndex (lang
);
380 FcLangSetBitSet (&ls
, id
);
389 str
= (FcChar8
*) lang
;
395 FcLangSetHash (const FcLangSet
*ls
)
400 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
408 FcNameParseLangSet (const FcChar8
*string
)
411 const FcChar8
*end
, *next
;
414 ls
= FcLangSetCreate ();
418 while (string
&& *string
)
420 end
= (FcChar8
*) strchr ((char *) string
, '|');
423 end
= string
+ strlen ((char *) string
);
428 if (end
- string
< sizeof (lang
) - 1)
430 strncpy ((char *) lang
, (char *) string
, end
- string
);
431 lang
[end
-string
] = '\0';
432 if (!FcLangSetAdd (ls
, lang
))
439 FcLangSetDestroy (ls
);
445 FcNameUnparseLangSet (FcStrBuf
*buf
, const FcLangSet
*ls
)
449 FcBool first
= FcTrue
;
451 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
453 if ((bits
= ls
->map
[i
]))
455 for (bit
= 0; bit
<= 31; bit
++)
456 if (bits
& (1 << bit
))
458 int id
= (i
<< 5) | bit
;
460 if (!FcStrBufChar (buf
, '|'))
462 if (!FcStrBufString (buf
, fcLangCharSets
[id
].lang
))
470 FcStrList
*list
= FcStrListCreate (ls
->extra
);
475 while ((extra
= FcStrListNext (list
)))
478 if (!FcStrBufChar (buf
, '|'))
480 if (!FcStrBufString (buf
, extra
));
489 FcLangSetEqual (const FcLangSet
*lsa
, const FcLangSet
*lsb
)
493 for (i
= 0; i
< NUM_LANG_SET_MAP
; i
++)
495 if (lsa
->map
[i
] != lsb
->map
[i
])
498 if (!lsa
->extra
&& !lsb
->extra
)
500 if (lsa
->extra
&& lsb
->extra
)
501 return FcStrSetEqual (lsa
->extra
, lsb
->extra
);