]> git.wh0rd.org - fontconfig.git/blame - src/fclang.c
Bug 44826 - <alias> must contain only a single <family>
[fontconfig.git] / src / fclang.c
CommitLineData
3de8881e 1/*
317b8492 2 * fontconfig/src/fclang.c
3de8881e 3 *
46b51147 4 * Copyright © 2002 Keith Packard
3de8881e
KP
5 *
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
5aaf466d 10 * documentation, and that the name of the author(s) not be used in
3de8881e 11 * advertising or publicity pertaining to distribution of the software without
5aaf466d 12 * specific, written prior permission. The authors make no
3de8881e
KP
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
3074a73b 16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
3de8881e 17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
3074a73b 18 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
3de8881e
KP
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.
23 */
24
25#include "fcint.h"
dbd065ad 26#include "fcftint.h"
3de8881e
KP
27
28typedef struct {
e85789a9 29 const FcChar8 lang[8];
21696e5b 30 const FcCharSet charset;
3de8881e
KP
31} FcLangCharSet;
32
793e946c
KP
33typedef struct {
34 int begin;
35 int end;
36} FcLangCharSetRange;
37
e50b9ae7 38#include "../fc-lang/fclang.h"
3de8881e 39
d8d73958 40struct _FcLangSet {
4262e0b3 41 FcStrSet *extra;
8480c6f8 42 FcChar32 map_size;
1f4e6fec 43 FcChar32 map[NUM_LANG_SET_MAP];
d8d73958
KP
44};
45
1f4e6fec
BE
46static void
47FcLangSetBitSet (FcLangSet *ls,
48 unsigned int id)
49{
50 int bucket;
51
52 id = fcLangCharSetIndices[id];
53 bucket = id >> 5;
54 if (bucket >= ls->map_size)
55 return; /* shouldn't happen really */
56
57 ls->map[bucket] |= ((FcChar32) 1 << (id & 0x1f));
58}
59
60static FcBool
61FcLangSetBitGet (const FcLangSet *ls,
62 unsigned int id)
63{
64 int bucket;
65
66 id = fcLangCharSetIndices[id];
67 bucket = id >> 5;
68 if (bucket >= ls->map_size)
69 return FcFalse;
70
71 return ((ls->map[bucket] >> (id & 0x1f)) & 1) ? FcTrue : FcFalse;
72}
d8d73958 73
3c862aad
AT
74static void
75FcLangSetBitReset (FcLangSet *ls,
76 unsigned int id)
77{
78 int bucket;
79
80 id = fcLangCharSetIndices[id];
81 bucket = id >> 5;
82 if (bucket >= ls->map_size)
83 return; /* shouldn't happen really */
84
85 ls->map[bucket] &= ~((FcChar32) 1 << (id & 0x1f));
86}
87
d8d73958 88FcLangSet *
594dcef0 89FcFreeTypeLangSet (const FcCharSet *charset,
e50b9ae7 90 const FcChar8 *exclusiveLang)
3de8881e 91{
cd2ec1a9 92 int i, j;
e50b9ae7 93 FcChar32 missing;
e50b9ae7 94 const FcCharSet *exclusiveCharset = 0;
d8d73958 95 FcLangSet *ls;
82f35f8b 96
e50b9ae7 97 if (exclusiveLang)
cf223cc7 98 exclusiveCharset = FcLangGetCharSet (exclusiveLang);
d8d73958
KP
99 ls = FcLangSetCreate ();
100 if (!ls)
101 return 0;
594dcef0 102 if (FcDebug() & FC_DBG_LANGSET)
7ce19673 103 {
7c12181f 104 printf ("font charset");
7ce19673
KP
105 FcCharSetPrint (charset);
106 printf ("\n");
107 }
3de8881e
KP
108 for (i = 0; i < NUM_LANG_CHAR_SET; i++)
109 {
594dcef0 110 if (FcDebug() & FC_DBG_LANGSET)
7ce19673 111 {
7c12181f 112 printf ("%s charset", fcLangCharSets[i].lang);
7ce19673
KP
113 FcCharSetPrint (&fcLangCharSets[i].charset);
114 printf ("\n");
115 }
116
e50b9ae7
KP
117 /*
118 * Check for Han charsets to make fonts
119 * which advertise support for a single language
120 * not support other Han languages
121 */
122 if (exclusiveCharset &&
cd2ec1a9 123 FcFreeTypeIsExclusiveLang (fcLangCharSets[i].lang))
e50b9ae7 124 {
cd2ec1a9
PL
125 if (fcLangCharSets[i].charset.num != exclusiveCharset->num)
126 continue;
127
128 for (j = 0; j < fcLangCharSets[i].charset.num; j++)
594dcef0 129 if (FcCharSetLeaf(&fcLangCharSets[i].charset, j) !=
7ce19673 130 FcCharSetLeaf(exclusiveCharset, j))
cd2ec1a9 131 continue;
e50b9ae7 132 }
3de8881e
KP
133 missing = FcCharSetSubtractCount (&fcLangCharSets[i].charset, charset);
134 if (FcDebug() & FC_DBG_SCANV)
c80d2ac4
KP
135 {
136 if (missing && missing < 10)
137 {
594dcef0 138 FcCharSet *missed = FcCharSetSubtract (&fcLangCharSets[i].charset,
c80d2ac4
KP
139 charset);
140 FcChar32 ucs4;
141 FcChar32 map[FC_CHARSET_MAP_SIZE];
142 FcChar32 next;
143
0d745819 144 printf ("\n%s(%u) ", fcLangCharSets[i].lang, missing);
c80d2ac4
KP
145 printf ("{");
146 for (ucs4 = FcCharSetFirstPage (missed, map, &next);
147 ucs4 != FC_CHARSET_DONE;
148 ucs4 = FcCharSetNextPage (missed, map, &next))
149 {
150 int i, j;
151 for (i = 0; i < FC_CHARSET_MAP_SIZE; i++)
152 if (map[i])
153 {
154 for (j = 0; j < 32; j++)
155 if (map[i] & (1 << j))
156 printf (" %04x", ucs4 + i * 32 + j);
157 }
158 }
159 printf (" }\n\t");
160 FcCharSetDestroy (missed);
161 }
162 else
0d745819 163 printf ("%s(%u) ", fcLangCharSets[i].lang, missing);
c80d2ac4 164 }
e50b9ae7 165 if (!missing)
d8d73958 166 FcLangSetBitSet (ls, i);
3de8881e 167 }
e50b9ae7 168
3de8881e
KP
169 if (FcDebug() & FC_DBG_SCANV)
170 printf ("\n");
594dcef0
BE
171
172
d8d73958 173 return ls;
3de8881e
KP
174}
175
d8d73958 176#define FcLangEnd(c) ((c) == '-' || (c) == '\0')
3de8881e
KP
177
178FcLangResult
179FcLangCompare (const FcChar8 *s1, const FcChar8 *s2)
180{
3de8881e 181 FcChar8 c1, c2;
d8d73958
KP
182 FcLangResult result = FcLangDifferentLang;
183
3de8881e
KP
184 for (;;)
185 {
186 c1 = *s1++;
187 c2 = *s2++;
d8d73958 188
3de8881e
KP
189 c1 = FcToLower (c1);
190 c2 = FcToLower (c2);
191 if (c1 != c2)
d8d73958
KP
192 {
193 if (FcLangEnd (c1) && FcLangEnd (c2))
c833409f 194 result = FcLangDifferentTerritory;
d8d73958
KP
195 return result;
196 }
197 else if (!c1)
198 return FcLangEqual;
199 else if (c1 == '-')
c833409f 200 result = FcLangDifferentTerritory;
3de8881e 201 }
3de8881e
KP
202}
203
793e946c 204/*
594dcef0 205 * Return FcTrue when super contains sub.
793e946c 206 *
74a623e0
KP
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
793e946c
KP
210 */
211
212static FcBool
74a623e0 213FcLangContains (const FcChar8 *super, const FcChar8 *sub)
793e946c
KP
214{
215 FcChar8 c1, c2;
216
217 for (;;)
218 {
74a623e0
KP
219 c1 = *super++;
220 c2 = *sub++;
793e946c
KP
221
222 c1 = FcToLower (c1);
223 c2 = FcToLower (c2);
224 if (c1 != c2)
225 {
74a623e0 226 /* see if super has a country while sub is mising one */
793e946c
KP
227 if (c1 == '-' && c2 == '\0')
228 return FcTrue;
74a623e0
KP
229 /* see if sub has a country while super is mising one */
230 if (c1 == '\0' && c2 == '-')
231 return FcTrue;
793e946c
KP
232 return FcFalse;
233 }
234 else if (!c1)
235 return FcTrue;
236 }
237}
238
3de8881e 239const FcCharSet *
cf223cc7 240FcLangGetCharSet (const FcChar8 *lang)
3de8881e
KP
241{
242 int i;
243 int country = -1;
82f35f8b 244
3de8881e
KP
245 for (i = 0; i < NUM_LANG_CHAR_SET; i++)
246 {
247 switch (FcLangCompare (lang, fcLangCharSets[i].lang)) {
248 case FcLangEqual:
249 return &fcLangCharSets[i].charset;
c833409f 250 case FcLangDifferentTerritory:
3de8881e
KP
251 if (country == -1)
252 country = i;
1f4e6fec 253 case FcLangDifferentLang:
3de8881e
KP
254 default:
255 break;
256 }
257 }
258 if (country == -1)
259 return 0;
b36f2a39 260 return &fcLangCharSets[country].charset;
3de8881e 261}
d8d73958 262
cf223cc7
BE
263FcStrSet *
264FcGetLangs (void)
265{
266 FcStrSet *langs;
267 int i;
268
269 langs = FcStrSetCreate();
270 if (!langs)
271 return 0;
272
273 for (i = 0; i < NUM_LANG_CHAR_SET; i++)
274 FcStrSetAdd (langs, fcLangCharSets[i].lang);
275
276 return langs;
277}
278
d8d73958
KP
279FcLangSet *
280FcLangSetCreate (void)
281{
282 FcLangSet *ls;
283
284 ls = malloc (sizeof (FcLangSet));
285 if (!ls)
286 return 0;
287 FcMemAlloc (FC_MEM_LANGSET, sizeof (FcLangSet));
288 memset (ls->map, '\0', sizeof (ls->map));
1f4e6fec 289 ls->map_size = NUM_LANG_SET_MAP;
4262e0b3 290 ls->extra = 0;
d8d73958
KP
291 return ls;
292}
293
294void
295FcLangSetDestroy (FcLangSet *ls)
296{
4262e0b3
PL
297 if (ls->extra)
298 FcStrSetDestroy (ls->extra);
d8d73958
KP
299 FcMemFree (FC_MEM_LANGSET, sizeof (FcLangSet));
300 free (ls);
301}
302
303FcLangSet *
304FcLangSetCopy (const FcLangSet *ls)
305{
306 FcLangSet *new;
307
308 new = FcLangSetCreate ();
309 if (!new)
310 goto bail0;
1f4e6fec
BE
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])));
4262e0b3 313 if (ls->extra)
d8d73958
KP
314 {
315 FcStrList *list;
316 FcChar8 *extra;
317
4262e0b3
PL
318 new->extra = FcStrSetCreate ();
319 if (!new->extra)
d8d73958
KP
320 goto bail1;
321
4262e0b3 322 list = FcStrListCreate (ls->extra);
d8d73958
KP
323 if (!list)
324 goto bail1;
325
326 while ((extra = FcStrListNext (list)))
4262e0b3 327 if (!FcStrSetAdd (new->extra, extra))
d8d73958
KP
328 {
329 FcStrListDone (list);
330 goto bail1;
331 }
332 FcStrListDone (list);
333 }
334 return new;
335bail1:
336 FcLangSetDestroy (new);
337bail0:
338 return 0;
339}
340
341static int
342FcLangSetIndex (const FcChar8 *lang)
343{
12d49d3c
CW
344 int low, high, mid = 0;
345 int cmp = 0;
594dcef0 346 FcChar8 firstChar = FcToLower(lang[0]);
947afeb5 347 FcChar8 secondChar = firstChar ? FcToLower(lang[1]) : '\0';
594dcef0 348
793e946c
KP
349 if (firstChar < 'a')
350 {
351 low = 0;
352 high = fcLangCharSetRanges[0].begin;
353 }
354 else if(firstChar > 'z')
355 {
356 low = fcLangCharSetRanges[25].begin;
357 high = NUM_LANG_CHAR_SET - 1;
358 }
359 else
360 {
361 low = fcLangCharSetRanges[firstChar - 'a'].begin;
362 high = fcLangCharSetRanges[firstChar - 'a'].end;
363 /* no matches */
364 if (low > high)
365 return -low; /* next entry after where it would be */
366 }
d8d73958 367
d8d73958
KP
368 while (low <= high)
369 {
370 mid = (high + low) >> 1;
793e946c
KP
371 if(fcLangCharSets[mid].lang[0] != firstChar)
372 cmp = FcStrCmpIgnoreCase(fcLangCharSets[mid].lang, lang);
373 else
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) */
947afeb5 376 cmp = fcLangCharSets[mid].lang[1] - secondChar;
594dcef0
BE
377 if (cmp == 0 &&
378 (fcLangCharSets[mid].lang[2] != '\0' ||
947afeb5 379 lang[2] != '\0'))
793e946c 380 {
594dcef0 381 cmp = FcStrCmpIgnoreCase(fcLangCharSets[mid].lang+2,
947afeb5 382 lang+2);
793e946c 383 }
793e946c
KP
384 }
385 if (cmp == 0)
d8d73958
KP
386 return mid;
387 if (cmp < 0)
388 low = mid + 1;
389 else
390 high = mid - 1;
391 }
392 if (cmp < 0)
393 mid++;
394 return -(mid + 1);
395}
396
397FcBool
398FcLangSetAdd (FcLangSet *ls, const FcChar8 *lang)
399{
400 int id;
401
402 id = FcLangSetIndex (lang);
403 if (id >= 0)
404 {
405 FcLangSetBitSet (ls, id);
406 return FcTrue;
407 }
4262e0b3 408 if (!ls->extra)
d8d73958 409 {
4262e0b3
PL
410 ls->extra = FcStrSetCreate ();
411 if (!ls->extra)
d8d73958
KP
412 return FcFalse;
413 }
4262e0b3 414 return FcStrSetAdd (ls->extra, lang);
d8d73958
KP
415}
416
3c862aad
AT
417FcBool
418FcLangSetDel (FcLangSet *ls, const FcChar8 *lang)
419{
420 int id;
421
422 id = FcLangSetIndex (lang);
423 if (id >= 0)
424 {
425 FcLangSetBitReset (ls, id);
426 }
427 else if (ls->extra)
428 {
429 FcStrSetDel (ls->extra, lang);
430 }
431 return FcTrue;
432}
433
d8d73958
KP
434FcLangResult
435FcLangSetHasLang (const FcLangSet *ls, const FcChar8 *lang)
436{
437 int id;
438 FcLangResult best, r;
439 int i;
440
441 id = FcLangSetIndex (lang);
2458a6d8
KP
442 if (id < 0)
443 id = -id - 1;
444 else if (FcLangSetBitGet (ls, id))
d8d73958 445 return FcLangEqual;
d8d73958
KP
446 best = FcLangDifferentLang;
447 for (i = id - 1; i >= 0; i--)
448 {
449 r = FcLangCompare (lang, fcLangCharSets[i].lang);
450 if (r == FcLangDifferentLang)
451 break;
452 if (FcLangSetBitGet (ls, i) && r < best)
453 best = r;
454 }
455 for (i = id; i < NUM_LANG_CHAR_SET; i++)
456 {
457 r = FcLangCompare (lang, fcLangCharSets[i].lang);
458 if (r == FcLangDifferentLang)
459 break;
460 if (FcLangSetBitGet (ls, i) && r < best)
461 best = r;
462 }
4262e0b3 463 if (ls->extra)
d8d73958 464 {
4262e0b3 465 FcStrList *list = FcStrListCreate (ls->extra);
d8d73958 466 FcChar8 *extra;
d8d73958
KP
467
468 if (list)
469 {
470 while (best > FcLangEqual && (extra = FcStrListNext (list)))
471 {
472 r = FcLangCompare (lang, extra);
473 if (r < best)
474 best = r;
475 }
476 FcStrListDone (list);
477 }
478 }
479 return best;
480}
481
482static FcLangResult
483FcLangSetCompareStrSet (const FcLangSet *ls, FcStrSet *set)
484{
485 FcStrList *list = FcStrListCreate (set);
486 FcLangResult r, best = FcLangDifferentLang;
487 FcChar8 *extra;
488
489 if (list)
490 {
491 while (best > FcLangEqual && (extra = FcStrListNext (list)))
492 {
493 r = FcLangSetHasLang (ls, extra);
494 if (r < best)
495 best = r;
496 }
497 FcStrListDone (list);
498 }
499 return best;
500}
501
502FcLangResult
503FcLangSetCompare (const FcLangSet *lsa, const FcLangSet *lsb)
504{
1f4e6fec 505 int i, j, count;
d8d73958
KP
506 FcLangResult best, r;
507
1f4e6fec
BE
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++)
d8d73958
KP
511 if (lsa->map[i] & lsb->map[i])
512 return FcLangEqual;
513 best = FcLangDifferentLang;
234397b4 514 for (j = 0; j < NUM_COUNTRY_SET; j++)
1f4e6fec 515 for (i = 0; i < count; i++)
234397b4
DD
516 if ((lsa->map[i] & fcLangCountrySets[j][i]) &&
517 (lsb->map[i] & fcLangCountrySets[j][i]))
518 {
c833409f 519 best = FcLangDifferentTerritory;
234397b4
DD
520 break;
521 }
4262e0b3 522 if (lsa->extra)
d8d73958 523 {
4262e0b3 524 r = FcLangSetCompareStrSet (lsb, lsa->extra);
d8d73958
KP
525 if (r < best)
526 best = r;
527 }
4262e0b3 528 if (best > FcLangEqual && lsb->extra)
d8d73958 529 {
4262e0b3 530 r = FcLangSetCompareStrSet (lsa, lsb->extra);
d8d73958
KP
531 if (r < best)
532 best = r;
533 }
534 return best;
535}
536
537/*
538 * Used in computing values -- mustn't allocate any storage
0d47cfab 539 * XXX Not thread-safe
d8d73958
KP
540 */
541FcLangSet *
542FcLangSetPromote (const FcChar8 *lang)
543{
544 static FcLangSet ls;
545 static FcStrSet strs;
546 static FcChar8 *str;
547 int id;
548
549 memset (ls.map, '\0', sizeof (ls.map));
0d47cfab 550 ls.map_size = NUM_LANG_SET_MAP;
4262e0b3 551 ls.extra = 0;
d8d73958
KP
552 id = FcLangSetIndex (lang);
553 if (id > 0)
554 {
555 FcLangSetBitSet (&ls, id);
556 }
557 else
558 {
4262e0b3 559 ls.extra = &strs;
d8d73958
KP
560 strs.num = 1;
561 strs.size = 1;
4262e0b3 562 strs.strs = &str;
47d4f950 563 strs.ref = 1;
d8d73958
KP
564 str = (FcChar8 *) lang;
565 }
566 return &ls;
567}
568
569FcChar32
570FcLangSetHash (const FcLangSet *ls)
571{
572 FcChar32 h = 0;
a3b24268 573 int i, count;
d8d73958 574
a3b24268
BE
575 count = FC_MIN (ls->map_size, NUM_LANG_SET_MAP);
576 for (i = 0; i < count; i++)
d8d73958 577 h ^= ls->map[i];
4262e0b3
PL
578 if (ls->extra)
579 h ^= ls->extra->num;
d8d73958
KP
580 return h;
581}
582
583FcLangSet *
584FcNameParseLangSet (const FcChar8 *string)
585{
1f4e6fec 586 FcChar8 lang[32], c = 0;
793e946c 587 int i;
d8d73958
KP
588 FcLangSet *ls;
589
590 ls = FcLangSetCreate ();
591 if (!ls)
592 goto bail0;
593
793e946c 594 for(;;)
d8d73958 595 {
793e946c 596 for(i = 0; i < 31;i++)
d8d73958 597 {
793e946c
KP
598 c = *string++;
599 if(c == '\0' || c == '|')
600 break; /* end of this code */
601 lang[i] = c;
d8d73958 602 }
793e946c
KP
603 lang[i] = '\0';
604 if (!FcLangSetAdd (ls, lang))
605 goto bail1;
606 if(c == '\0')
607 break;
d8d73958
KP
608 }
609 return ls;
610bail1:
611 FcLangSetDestroy (ls);
612bail0:
613 return 0;
614}
615
616FcBool
617FcNameUnparseLangSet (FcStrBuf *buf, const FcLangSet *ls)
618{
1f4e6fec 619 int i, bit, count;
c5f0a65b 620 FcChar32 bits;
d8d73958
KP
621 FcBool first = FcTrue;
622
1f4e6fec
BE
623 count = FC_MIN (ls->map_size, NUM_LANG_SET_MAP);
624 for (i = 0; i < count; i++)
c5f0a65b
BE
625 {
626 if ((bits = ls->map[i]))
627 {
628 for (bit = 0; bit <= 31; bit++)
629 if (bits & (1 << bit))
630 {
631 int id = (i << 5) | bit;
632 if (!first)
633 if (!FcStrBufChar (buf, '|'))
634 return FcFalse;
635 if (!FcStrBufString (buf, fcLangCharSets[fcLangCharSetIndicesInv[id]].lang))
d8d73958 636 return FcFalse;
c5f0a65b
BE
637 first = FcFalse;
638 }
639 }
640 }
4262e0b3 641 if (ls->extra)
d8d73958 642 {
4262e0b3 643 FcStrList *list = FcStrListCreate (ls->extra);
d8d73958
KP
644 FcChar8 *extra;
645
646 if (!list)
647 return FcFalse;
648 while ((extra = FcStrListNext (list)))
649 {
650 if (!first)
651 if (!FcStrBufChar (buf, '|'))
04f7d3e7
PL
652 {
653 FcStrListDone (list);
d8d73958 654 return FcFalse;
04f7d3e7 655 }
793e946c 656 if (!FcStrBufString (buf, extra))
04f7d3e7
PL
657 {
658 FcStrListDone (list);
659 return FcFalse;
660 }
d8d73958
KP
661 first = FcFalse;
662 }
2de24638 663 FcStrListDone (list);
d8d73958
KP
664 }
665 return FcTrue;
666}
667
668FcBool
669FcLangSetEqual (const FcLangSet *lsa, const FcLangSet *lsb)
670{
1f4e6fec 671 int i, count;
d8d73958 672
1f4e6fec
BE
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++)
d8d73958
KP
676 {
677 if (lsa->map[i] != lsb->map[i])
678 return FcFalse;
679 }
4262e0b3 680 if (!lsa->extra && !lsb->extra)
d8d73958 681 return FcTrue;
4262e0b3
PL
682 if (lsa->extra && lsb->extra)
683 return FcStrSetEqual (lsa->extra, lsb->extra);
d8d73958
KP
684 return FcFalse;
685}
793e946c
KP
686
687static FcBool
688FcLangSetContainsLang (const FcLangSet *ls, const FcChar8 *lang)
689{
690 int id;
793e946c
KP
691 int i;
692
693 id = FcLangSetIndex (lang);
694 if (id < 0)
695 id = -id - 1;
696 else if (FcLangSetBitGet (ls, id))
697 return FcTrue;
698 /*
699 * search up and down among equal languages for a match
700 */
701 for (i = id - 1; i >= 0; i--)
702 {
703 if (FcLangCompare (fcLangCharSets[i].lang, lang) == FcLangDifferentLang)
704 break;
705 if (FcLangSetBitGet (ls, i) &&
706 FcLangContains (fcLangCharSets[i].lang, lang))
707 return FcTrue;
708 }
709 for (i = id; i < NUM_LANG_CHAR_SET; i++)
710 {
711 if (FcLangCompare (fcLangCharSets[i].lang, lang) == FcLangDifferentLang)
712 break;
713 if (FcLangSetBitGet (ls, i) &&
714 FcLangContains (fcLangCharSets[i].lang, lang))
715 return FcTrue;
716 }
4262e0b3 717 if (ls->extra)
793e946c 718 {
4262e0b3 719 FcStrList *list = FcStrListCreate (ls->extra);
793e946c 720 FcChar8 *extra;
793e946c
KP
721
722 if (list)
723 {
724 while ((extra = FcStrListNext (list)))
725 {
726 if (FcLangContains (extra, lang))
727 break;
728 }
729 FcStrListDone (list);
730 if (extra)
731 return FcTrue;
732 }
733 }
734 return FcFalse;
735}
736
737/*
738 * return FcTrue if lsa contains every language in lsb
739 */
740FcBool
741FcLangSetContains (const FcLangSet *lsa, const FcLangSet *lsb)
742{
1f4e6fec 743 int i, j, count;
793e946c
KP
744 FcChar32 missing;
745
746 if (FcDebug() & FC_DBG_MATCHV)
747 {
748 printf ("FcLangSet "); FcLangSetPrint (lsa);
749 printf (" contains "); FcLangSetPrint (lsb);
750 printf ("\n");
751 }
752 /*
753 * check bitmaps for missing language support
754 */
1f4e6fec
BE
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++)
793e946c
KP
758 {
759 missing = lsb->map[i] & ~lsa->map[i];
760 if (missing)
761 {
762 for (j = 0; j < 32; j++)
594dcef0 763 if (missing & (1 << j))
793e946c
KP
764 {
765 if (!FcLangSetContainsLang (lsa,
d354a321 766 fcLangCharSets[fcLangCharSetIndicesInv[i*32 + j]].lang))
793e946c
KP
767 {
768 if (FcDebug() & FC_DBG_MATCHV)
d354a321 769 printf ("\tMissing bitmap %s\n", fcLangCharSets[fcLangCharSetIndicesInv[i*32+j]].lang);
793e946c
KP
770 return FcFalse;
771 }
772 }
773 }
774 }
4262e0b3 775 if (lsb->extra)
793e946c 776 {
4262e0b3 777 FcStrList *list = FcStrListCreate (lsb->extra);
793e946c
KP
778 FcChar8 *extra;
779
780 if (list)
781 {
782 while ((extra = FcStrListNext (list)))
783 {
784 if (!FcLangSetContainsLang (lsa, extra))
785 {
786 if (FcDebug() & FC_DBG_MATCHV)
787 printf ("\tMissing string %s\n", extra);
788 break;
789 }
790 }
791 FcStrListDone (list);
792 if (extra)
793 return FcFalse;
794 }
795 }
796 return FcTrue;
797}
cd2ec1a9 798
7ce19673
KP
799FcBool
800FcLangSetSerializeAlloc (FcSerialize *serialize, const FcLangSet *l)
cd2ec1a9 801{
7ce19673
KP
802 if (!FcSerializeAlloc (serialize, l, sizeof (FcLangSet)))
803 return FcFalse;
4262e0b3 804 return FcTrue;
cd2ec1a9 805}
212c9f43 806
4262e0b3 807FcLangSet *
7ce19673 808FcLangSetSerialize(FcSerialize *serialize, const FcLangSet *l)
4262e0b3 809{
7ce19673 810 FcLangSet *l_serialize = FcSerializePtr (serialize, l);
4262e0b3 811
7ce19673
KP
812 if (!l_serialize)
813 return NULL;
a3b24268
BE
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;
1f4e6fec 817 l_serialize->extra = NULL; /* We don't serialize ls->extra */
7ce19673 818 return l_serialize;
212c9f43 819}
d62b85af
BE
820
821FcStrSet *
822FcLangSetGetLangs (const FcLangSet *ls)
823{
824 FcStrSet *langs;
825 int i;
826
827 langs = FcStrSetCreate();
828 if (!langs)
829 return 0;
830
831 for (i = 0; i < NUM_LANG_CHAR_SET; i++)
832 if (FcLangSetBitGet (ls, i))
833 FcStrSetAdd (langs, fcLangCharSets[i].lang);
834
835 if (ls->extra)
836 {
837 FcStrList *list = FcStrListCreate (ls->extra);
838 FcChar8 *extra;
839
840 if (list)
841 {
842 while ((extra = FcStrListNext (list)))
843 FcStrSetAdd (langs, extra);
844
845 FcStrListDone (list);
846 }
847 }
848
849 return langs;
850}
851
3c862aad
AT
852static FcLangSet *
853FcLangSetOperate(const FcLangSet *a,
854 const FcLangSet *b,
855 FcBool (*func) (FcLangSet *ls,
856 const FcChar8 *s))
857{
858 FcLangSet *langset = FcLangSetCopy (a);
859 FcStrList *sl = FcStrListCreate (FcLangSetGetLangs (b));
860 FcChar8 *str;
861
862 while ((str = FcStrListNext (sl)))
863 {
864 func (langset, str);
865 }
866 FcStrListDone (sl);
867
868 return langset;
869}
870
871FcLangSet *
872FcLangSetUnion (const FcLangSet *a, const FcLangSet *b)
873{
874 return FcLangSetOperate(a, b, FcLangSetAdd);
875}
876
877FcLangSet *
878FcLangSetSubtract (const FcLangSet *a, const FcLangSet *b)
879{
880 return FcLangSetOperate(a, b, FcLangSetDel);
881}
882
23816bf9
KP
883#define __fclang__
884#include "fcaliastail.h"
a0a1da22 885#include "fcftaliastail.h"
23816bf9 886#undef __fclang__