Debian Codegraph @

src/fclang.c

raw
1 /*
2 * fontconfig/src/fclang.c
3 *
4 * Copyright © 2002 Keith Packard
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
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.
15 *
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.
23 */
24
25 #include "fcint.h"
26
27 #include "fcftint.h"
28
29 /* Objects MT-safe for readonly access. */
30
31 /*
32 * Keep Han languages separated by eliminating languages
33 * that the codePageRange bits says aren't supported
34 */
35
36 static const struct {
37 char bit;
38 const FcChar8 lang[6];
39 } FcCodePageRange[] = {
40 { 17, "ja" },
41 { 18, "zh-cn" },
42 { 19, "ko" },
43 { 20, "zh-tw" },
44 };
45
46 #define NUM_CODE_PAGE_RANGE (int)(sizeof FcCodePageRange / sizeof FcCodePageRange[0])
47
48 static int
49 FcLangSetIndex (const FcChar8 *lang);
50
51 static void
52 FcLangSetBitSet (FcLangSet *ls,
53 unsigned int id)
54 {
55 unsigned int bucket;
56
57 id = fcLangCharSetIndices[id];
58 bucket = id >> 5;
59 if (bucket >= ls->map_size)
60 return; /* shouldn't happen really */
61
62 ls->map[bucket] |= ((FcChar32)1U << (id & 0x1f));
63 }
64
65 static FcBool
66 FcLangSetBitGet (const FcLangSet *ls,
67 unsigned int id)
68 {
69 unsigned int bucket;
70
71 id = fcLangCharSetIndices[id];
72 bucket = id >> 5;
73 if (bucket >= ls->map_size)
74 return FcFalse;
75
76 return ((ls->map[bucket] >> (id & 0x1f)) & 1) ? FcTrue : FcFalse;
77 }
78
79 static void
80 FcLangSetBitReset (FcLangSet *ls,
81 unsigned int id)
82 {
83 unsigned int bucket;
84
85 id = fcLangCharSetIndices[id];
86 bucket = id >> 5;
87 if (bucket >= ls->map_size)
88 return; /* shouldn't happen really */
89
90 ls->map[bucket] &= ~((FcChar32)1U << (id & 0x1f));
91 }
92
93 FcLangSet *
94 FcLangSetFromCharSet (const FcCharSet *charset,
95 const FcChar8 *exclusiveLang)
96 {
97 int i, j;
98 FcChar32 missing;
99 const FcCharSet *exclusiveCharset = 0;
100 FcLangSet *ls;
101
102 if (exclusiveLang)
103 exclusiveCharset = FcLangGetCharSet (exclusiveLang);
104 ls = FcLangSetCreate();
105 if (!ls)
106 return 0;
107 if (FcDebug() & FC_DBG_LANGSET) {
108 printf ("font charset");
109 FcCharSetPrint (charset);
110 printf ("\n");
111 }
112 for (i = 0; i < NUM_LANG_CHAR_SET; i++) {
113 if (FcDebug() & FC_DBG_LANGSET) {
114 printf ("%s charset", fcLangCharSets[i].lang);
115 FcCharSetPrint (&fcLangCharSets[i].charset);
116 printf ("\n");
117 }
118
119 /*
120 * Check for Han charsets to make fonts
121 * which advertise support for a single language
122 * not support other Han languages
123 */
124 if (exclusiveCharset &&
125 FcLangIsExclusive (fcLangCharSets[i].lang)) {
126 if (fcLangCharSets[i].charset.num != exclusiveCharset->num)
127 continue;
128
129 for (j = 0; j < fcLangCharSets[i].charset.num; j++)
130 if (FcCharSetLeaf (&fcLangCharSets[i].charset, j) !=
131 FcCharSetLeaf (exclusiveCharset, j))
132 continue;
133 }
134 missing = FcCharSetSubtractCount (&fcLangCharSets[i].charset, charset);
135 if (FcDebug() & FC_DBG_SCANV) {
136 if (missing && missing < 10) {
137 FcCharSet *missed = FcCharSetSubtract (&fcLangCharSets[i].charset,
138 charset);
139 FcChar32 ucs4;
140 FcChar32 map[FC_CHARSET_MAP_SIZE];
141 FcChar32 next;
142
143 printf ("\n%s(%u) ", fcLangCharSets[i].lang, missing);
144 printf ("{");
145 for (ucs4 = FcCharSetFirstPage (missed, map, &next);
146 ucs4 != FC_CHARSET_DONE;
147 ucs4 = FcCharSetNextPage (missed, map, &next)) {
148 int i, j;
149 for (i = 0; i < FC_CHARSET_MAP_SIZE; i++)
150 if (map[i]) {
151 for (j = 0; j < 32; j++)
152 if (map[i] & (1U << j))
153 printf (" %04x", ucs4 + i * 32 + j);
154 }
155 }
156 printf (" }\n\t");
157 FcCharSetDestroy (missed);
158 } else
159 printf ("%s(%u) ", fcLangCharSets[i].lang, missing);
160 }
161 if (!missing)
162 FcLangSetBitSet (ls, i);
163 }
164
165 if (FcDebug() & FC_DBG_SCANV)
166 printf ("\n");
167
168 return ls;
169 }
170
171 FcChar8 *
172 FcLangNormalize (const FcChar8 *lang)
173 {
174 FcChar8 *result = NULL, *s, *orig;
175 char *territory, *encoding, *modifier;
176 size_t llen, tlen = 0, mlen = 0;
177
178 if (!lang || !*lang)
179 return NULL;
180
181 /* might be called without initialization */
182 FcInitDebug();
183
184 if (FcStrCmpIgnoreCase (lang, (const FcChar8 *)"C") == 0 ||
185 FcStrCmpIgnoreCase (lang, (const FcChar8 *)"C.UTF-8") == 0 ||
186 FcStrCmpIgnoreCase (lang, (const FcChar8 *)"C.utf8") == 0 ||
187 FcStrCmpIgnoreCase (lang, (const FcChar8 *)"POSIX") == 0) {
188 result = FcStrCopy ((const FcChar8 *)"en");
189 goto bail;
190 }
191
192 s = FcStrCopy (lang);
193 if (!s)
194 goto bail;
195
196 /* from the comments in glibc:
197 *
198 * LOCALE can consist of up to four recognized parts for the XPG syntax:
199 *
200 * language[_territory[.codeset]][@modifier]
201 *
202 * Beside the first all of them are allowed to be missing. If the
203 * full specified locale is not found, the less specific one are
204 * looked for. The various part will be stripped off according to
205 * the following order:
206 * (1) codeset
207 * (2) normalized codeset
208 * (3) territory
209 * (4) modifier
210 *
211 * So since we don't take care of the codeset part here, what patterns
212 * we need to deal with is:
213 *
214 * 1. language_territory@modifier
215 * 2. language@modifier
216 * 3. language
217 *
218 * then. and maybe no need to try language_territory here.
219 */
220 modifier = strchr ((const char *)s, '@');
221 if (modifier) {
222 *modifier = 0;
223 modifier++;
224 mlen = strlen (modifier);
225 }
226 encoding = strchr ((const char *)s, '.');
227 if (encoding) {
228 *encoding = 0;
229 encoding++;
230 if (modifier) {
231 memmove (encoding, modifier, mlen + 1);
232 modifier = encoding;
233 }
234 }
235 territory = strchr ((const char *)s, '_');
236 if (!territory)
237 territory = strchr ((const char *)s, '-');
238 if (territory) {
239 *territory = 0;
240 territory++;
241 tlen = strlen (territory);
242 }
243 llen = strlen ((const char *)s);
244 if (llen < 2 || llen > 3) {
245 fprintf (stderr, "Fontconfig warning: ignoring %s: not a valid language tag\n",
246 lang);
247 goto bail0;
248 }
249 if (territory && (tlen < 2 || tlen > 3) &&
250 !(territory[0] == 'z' && tlen < 5)) {
251 fprintf (stderr, "Fontconfig warning: ignoring %s: not a valid region tag\n",
252 lang);
253 goto bail0;
254 }
255 if (territory)
256 territory[-1] = '-';
257 if (modifier)
258 modifier[-1] = '@';
259 orig = FcStrDowncase (s);
260 if (!orig)
261 goto bail0;
262 if (territory) {
263 if (FcDebug() & FC_DBG_LANGSET)
264 printf ("Checking the existence of %s.orth\n", s);
265 if (FcLangSetIndex (s) < 0) {
266 memmove (territory - 1, territory + tlen, (mlen > 0 ? mlen + 1 : 0) + 1);
267 if (modifier)
268 modifier = territory;
269 } else {
270 result = s;
271 /* we'll miss the opportunity to reduce the correct size
272 * of the allocated memory for the string after that.
273 */
274 s = NULL;
275 goto bail1;
276 }
277 }
278 if (modifier) {
279 if (FcDebug() & FC_DBG_LANGSET)
280 printf ("Checking the existence of %s.orth\n", s);
281 if (FcLangSetIndex (s) < 0)
282 modifier[-1] = 0;
283 else {
284 result = s;
285 /* we'll miss the opportunity to reduce the correct size
286 * of the allocated memory for the string after that.
287 */
288 s = NULL;
289 goto bail1;
290 }
291 }
292 if (FcDebug() & FC_DBG_LANGSET)
293 printf ("Checking the existence of %s.orth\n", s);
294 if (FcLangSetIndex (s) < 0) {
295 /* there seems no languages matched in orth.
296 * add the language as is for fallback.
297 */
298 result = orig;
299 orig = NULL;
300 } else {
301 result = s;
302 /* we'll miss the opportunity to reduce the correct size
303 * of the allocated memory for the string after that.
304 */
305 s = NULL;
306 }
307 bail1:
308 if (orig)
309 FcStrFree (orig);
310 bail0:
311 if (s)
312 free (s);
313 bail:
314 if (FcDebug() & FC_DBG_LANGSET) {
315 if (result)
316 printf ("normalized: %s -> %s\n", lang, result);
317 else
318 printf ("Unable to normalize %s\n", lang);
319 }
320
321 return result;
322 }
323
324 #define FcLangEnd(c) ((c) == '-' || (c) == '\0')
325
326 FcLangResult
327 FcLangCompare (const FcChar8 *s1, const FcChar8 *s2)
328 {
329 FcChar8 c1, c2;
330 FcLangResult result = FcLangDifferentLang;
331 const FcChar8 *s1_orig = s1;
332 FcBool is_und;
333
334 is_und = FcToLower (s1[0]) == 'u' &&
335 FcToLower (s1[1]) == 'n' &&
336 FcToLower (s1[2]) == 'd' &&
337 FcLangEnd (s1[3]);
338
339 for (;;) {
340 c1 = *s1++;
341 c2 = *s2++;
342
343 c1 = FcToLower (c1);
344 c2 = FcToLower (c2);
345 if (c1 != c2) {
346 if (!is_und && FcLangEnd (c1) && FcLangEnd (c2))
347 result = FcLangDifferentTerritory;
348 return result;
349 } else if (!c1) {
350 return is_und ? result : FcLangEqual;
351 } else if (c1 == '-') {
352 if (!is_und)
353 result = FcLangDifferentTerritory;
354 }
355
356 /* If we parsed past "und-", then do not consider it undefined anymore,
357 * as there's *something* specified. */
358 if (is_und && s1 - s1_orig == 4)
359 is_und = FcFalse;
360 }
361 }
362
363 /*
364 * Return FcTrue when super contains sub.
365 *
366 * super contains sub if super and sub have the same
367 * language and either the same country or one
368 * is missing the country
369 */
370
371 static FcBool
372 FcLangContains (const FcChar8 *super, const FcChar8 *sub)
373 {
374 FcChar8 c1, c2;
375
376 for (;;) {
377 c1 = *super++;
378 c2 = *sub++;
379
380 c1 = FcToLower (c1);
381 c2 = FcToLower (c2);
382 if (c1 != c2) {
383 /* see if super has a country while sub is missing one */
384 if (c1 == '-' && c2 == '\0')
385 return FcTrue;
386 /* see if sub has a country while super is missing one */
387 if (c1 == '\0' && c2 == '-')
388 return FcTrue;
389 return FcFalse;
390 } else if (!c1)
391 return FcTrue;
392 }
393 }
394
395 const FcCharSet *
396 FcLangGetCharSet (const FcChar8 *lang)
397 {
398 int i;
399 int country = -1;
400
401 for (i = 0; i < NUM_LANG_CHAR_SET; i++) {
402 switch (FcLangCompare (lang, fcLangCharSets[i].lang)) {
403 case FcLangEqual:
404 return &fcLangCharSets[i].charset;
405 case FcLangDifferentTerritory:
406 if (country == -1)
407 country = i;
408 case FcLangDifferentLang:
409 default:
410 break;
411 }
412 }
413 if (country == -1)
414 return 0;
415 return &fcLangCharSets[country].charset;
416 }
417
418 FcStrSet *
419 FcGetLangs (void)
420 {
421 FcStrSet *langs;
422 int i;
423
424 langs = FcStrSetCreate();
425 if (!langs)
426 return 0;
427
428 for (i = 0; i < NUM_LANG_CHAR_SET; i++)
429 FcStrSetAdd (langs, fcLangCharSets[i].lang);
430
431 return langs;
432 }
433
434 FcLangSet *
435 FcLangSetCreate (void)
436 {
437 FcLangSet *ls;
438
439 ls = malloc (sizeof (FcLangSet));
440 if (!ls)
441 return 0;
442 memset (ls->map, '\0', sizeof (ls->map));
443 ls->map_size = NUM_LANG_SET_MAP;
444 ls->extra = 0;
445 return ls;
446 }
447
448 void
449 FcLangSetDestroy (FcLangSet *ls)
450 {
451 if (!ls)
452 return;
453
454 if (ls->extra)
455 FcStrSetDestroy (ls->extra);
456 free (ls);
457 }
458
459 FcLangSet *
460 FcLangSetCopy (const FcLangSet *ls)
461 {
462 FcLangSet *newp;
463
464 if (!ls)
465 return NULL;
466
467 newp = FcLangSetCreate();
468 if (!newp)
469 goto bail0;
470 memset (newp->map, '\0', sizeof (newp->map));
471 memcpy (newp->map, ls->map, FC_MIN (sizeof (newp->map), ls->map_size * sizeof (ls->map[0])));
472 if (ls->extra) {
473 FcStrList *list;
474 FcChar8 *extra;
475
476 newp->extra = FcStrSetCreate();
477 if (!newp->extra)
478 goto bail1;
479
480 list = FcStrListCreate (ls->extra);
481 if (!list)
482 goto bail1;
483
484 while ((extra = FcStrListNext (list)))
485 if (!FcStrSetAdd (newp->extra, extra)) {
486 FcStrListDone (list);
487 goto bail1;
488 }
489 FcStrListDone (list);
490 }
491 return newp;
492 bail1:
493 FcLangSetDestroy (newp);
494 bail0:
495 return 0;
496 }
497
498 /* When the language isn't found, the return value r is such that:
499 * 1) r < 0
500 * 2) -r -1 is the index of the first language in fcLangCharSets that comes
501 * after the 'lang' argument in lexicographic order.
502 *
503 * The -1 is necessary to avoid problems with language id 0 (otherwise, we
504 * wouldn't be able to distinguish between “language found, id is 0” and
505 * “language not found, sorts right before the language with id 0”).
506 */
507 static int
508 FcLangSetIndex (const FcChar8 *lang)
509 {
510 int low, high, mid = 0;
511 int cmp = 0;
512 FcChar8 firstChar = FcToLower (lang[0]);
513 FcChar8 secondChar = firstChar ? FcToLower (lang[1]) : '\0';
514
515 if (firstChar < 'a') {
516 low = 0;
517 high = fcLangCharSetRanges[0].begin;
518 } else if (firstChar > 'z') {
519 low = fcLangCharSetRanges[25].begin;
520 high = NUM_LANG_CHAR_SET - 1;
521 } else {
522 low = fcLangCharSetRanges[firstChar - 'a'].begin;
523 high = fcLangCharSetRanges[firstChar - 'a'].end;
524 /* no matches */
525 if (low > high)
526 return -(low + 1); /* one past next entry after where it would be */
527 }
528
529 while (low <= high) {
530 mid = (high + low) >> 1;
531 if (fcLangCharSets[mid].lang[0] != firstChar)
532 cmp = FcStrCmpIgnoreCase (fcLangCharSets[mid].lang, lang);
533 else { /* fast path for resolving 2-letter languages (by far the most common) after
534 * finding the first char (probably already true because of the hash table) */
535 cmp = fcLangCharSets[mid].lang[1] - secondChar;
536 if (cmp == 0 &&
537 (fcLangCharSets[mid].lang[2] != '\0' ||
538 lang[2] != '\0')) {
539 cmp = FcStrCmpIgnoreCase (fcLangCharSets[mid].lang + 2,
540 lang + 2);
541 }
542 }
543 if (cmp == 0)
544 return mid;
545 if (cmp < 0)
546 low = mid + 1;
547 else
548 high = mid - 1;
549 }
550 if (cmp < 0)
551 mid++;
552 return -(mid + 1);
553 }
554
555 FcBool
556 FcLangSetAdd (FcLangSet *ls, const FcChar8 *lang)
557 {
558 int id;
559
560 id = FcLangSetIndex (lang);
561 if (id >= 0) {
562 FcLangSetBitSet (ls, id);
563 return FcTrue;
564 }
565 if (!ls->extra) {
566 ls->extra = FcStrSetCreate();
567 if (!ls->extra)
568 return FcFalse;
569 }
570 return FcStrSetAdd (ls->extra, lang);
571 }
572
573 FcBool
574 FcLangSetDel (FcLangSet *ls, const FcChar8 *lang)
575 {
576 int id;
577
578 id = FcLangSetIndex (lang);
579 if (id >= 0) {
580 FcLangSetBitReset (ls, id);
581 } else if (ls->extra) {
582 FcStrSetDel (ls->extra, lang);
583 }
584 return FcTrue;
585 }
586
587 FcLangResult
588 FcLangSetHasLang (const FcLangSet *ls, const FcChar8 *lang)
589 {
590 int id;
591 FcLangResult best, r;
592 int i;
593
594 id = FcLangSetIndex (lang);
595 if (id < 0)
596 id = -id - 1;
597 else if (FcLangSetBitGet (ls, id))
598 return FcLangEqual;
599 best = FcLangDifferentLang;
600 for (i = id - 1; i >= 0; i--) {
601 r = FcLangCompare (lang, fcLangCharSets[i].lang);
602 if (r == FcLangDifferentLang)
603 break;
604 if (FcLangSetBitGet (ls, i) && r < best)
605 best = r;
606 }
607 for (i = id; i < NUM_LANG_CHAR_SET; i++) {
608 r = FcLangCompare (lang, fcLangCharSets[i].lang);
609 if (r == FcLangDifferentLang)
610 break;
611 if (FcLangSetBitGet (ls, i) && r < best)
612 best = r;
613 }
614 if (ls->extra) {
615 FcStrList *list = FcStrListCreate (ls->extra);
616 FcChar8 *extra;
617
618 if (list) {
619 while (best > FcLangEqual && (extra = FcStrListNext (list))) {
620 r = FcLangCompare (lang, extra);
621 if (r < best)
622 best = r;
623 }
624 FcStrListDone (list);
625 }
626 }
627 return best;
628 }
629
630 static FcLangResult
631 FcLangSetCompareStrSet (const FcLangSet *ls, FcStrSet *set)
632 {
633 FcStrList *list = FcStrListCreate (set);
634 FcLangResult r, best = FcLangDifferentLang;
635 FcChar8 *extra;
636
637 if (list) {
638 while (best > FcLangEqual && (extra = FcStrListNext (list))) {
639 r = FcLangSetHasLang (ls, extra);
640 if (r < best)
641 best = r;
642 }
643 FcStrListDone (list);
644 }
645 return best;
646 }
647
648 FcLangResult
649 FcLangSetCompare (const FcLangSet *lsa, const FcLangSet *lsb)
650 {
651 int i, j, count;
652 FcLangResult best, r;
653 FcChar32 aInCountrySet, bInCountrySet;
654
655 count = FC_MIN (lsa->map_size, lsb->map_size);
656 count = FC_MIN (NUM_LANG_SET_MAP, count);
657 for (i = 0; i < count; i++)
658 if (lsa->map[i] & lsb->map[i])
659 return FcLangEqual;
660 best = FcLangDifferentLang;
661 for (j = 0; j < NUM_COUNTRY_SET; j++) {
662 aInCountrySet = 0;
663 bInCountrySet = 0;
664
665 for (i = 0; i < count; i++) {
666 aInCountrySet |= lsa->map[i] & fcLangCountrySets[j][i];
667 bInCountrySet |= lsb->map[i] & fcLangCountrySets[j][i];
668
669 if (aInCountrySet && bInCountrySet) {
670 best = FcLangDifferentTerritory;
671 break;
672 }
673 }
674 }
675 if (lsa->extra) {
676 r = FcLangSetCompareStrSet (lsb, lsa->extra);
677 if (r < best)
678 best = r;
679 }
680 if (best > FcLangEqual && lsb->extra) {
681 r = FcLangSetCompareStrSet (lsa, lsb->extra);
682 if (r < best)
683 best = r;
684 }
685 return best;
686 }
687
688 /*
689 * Used in computing values -- mustn't allocate any storage
690 */
691 FcLangSet *
692 FcLangSetPromote (const FcChar8 *lang, FcValuePromotionBuffer *vbuf)
693 {
694 int id;
695 typedef struct {
696 FcLangSet ls;
697 FcStrSet strs;
698 FcChar8 *str;
699 } FcLangSetPromotionBuffer;
700 FcLangSetPromotionBuffer *buf = (FcLangSetPromotionBuffer *)vbuf;
701
702 FC_ASSERT_STATIC (sizeof (FcLangSetPromotionBuffer) <= sizeof (FcValuePromotionBuffer));
703
704 memset (buf->ls.map, '\0', sizeof (buf->ls.map));
705 buf->ls.map_size = NUM_LANG_SET_MAP;
706 buf->ls.extra = 0;
707 if (lang) {
708 id = FcLangSetIndex (lang);
709 if (id >= 0) {
710 FcLangSetBitSet (&buf->ls, id);
711 } else {
712 buf->ls.extra = &buf->strs;
713 buf->strs.num = 1;
714 buf->strs.size = 1;
715 buf->strs.strs = &buf->str;
716 FcRefInit (&buf->strs.ref, 1);
717 buf->str = (FcChar8 *)lang;
718 }
719 }
720 return &buf->ls;
721 }
722
723 FcChar32
724 FcLangSetHash (const FcLangSet *ls)
725 {
726 FcChar32 h = 0;
727 int i, count;
728
729 count = FC_MIN (ls->map_size, NUM_LANG_SET_MAP);
730 for (i = 0; i < count; i++)
731 h ^= ls->map[i];
732 if (ls->extra)
733 h ^= ls->extra->num;
734 return h;
735 }
736
737 FcLangSet *
738 FcNameParseLangSet (const FcChar8 *string)
739 {
740 FcChar8 lang[32], c = 0;
741 int i;
742 FcLangSet *ls;
743
744 ls = FcLangSetCreate();
745 if (!ls)
746 goto bail0;
747
748 for (;;) {
749 for (i = 0; i < 31; i++) {
750 c = *string++;
751 if (c == '\0' || c == '|')
752 break; /* end of this code */
753 lang[i] = c;
754 }
755 lang[i] = '\0';
756 if (!FcLangSetAdd (ls, lang))
757 goto bail1;
758 if (c == '\0')
759 break;
760 }
761 return ls;
762 bail1:
763 FcLangSetDestroy (ls);
764 bail0:
765 return 0;
766 }
767
768 FcBool
769 FcNameUnparseLangSet (FcStrBuf *buf, const FcLangSet *ls)
770 {
771 int i, bit, count;
772 FcChar32 bits;
773 FcBool first = FcTrue;
774
775 count = FC_MIN (ls->map_size, NUM_LANG_SET_MAP);
776 for (i = 0; i < count; i++) {
777 if ((bits = ls->map[i])) {
778 for (bit = 0; bit <= 31; bit++)
779 if (bits & (1U << bit)) {
780 int id = (i << 5) | bit;
781 if (!first)
782 if (!FcStrBufChar (buf, '|'))
783 return FcFalse;
784 if (!FcStrBufString (buf, fcLangCharSets[fcLangCharSetIndicesInv[id]].lang))
785 return FcFalse;
786 first = FcFalse;
787 }
788 }
789 }
790 if (ls->extra) {
791 FcStrList *list = FcStrListCreate (ls->extra);
792 FcChar8 *extra;
793
794 if (!list)
795 return FcFalse;
796 while ((extra = FcStrListNext (list))) {
797 if (!first)
798 if (!FcStrBufChar (buf, '|')) {
799 FcStrListDone (list);
800 return FcFalse;
801 }
802 if (!FcStrBufString (buf, extra)) {
803 FcStrListDone (list);
804 return FcFalse;
805 }
806 first = FcFalse;
807 }
808 FcStrListDone (list);
809 }
810 return FcTrue;
811 }
812
813 FcBool
814 FcLangSetEqual (const FcLangSet *lsa, const FcLangSet *lsb)
815 {
816 int i, count;
817
818 count = FC_MIN (lsa->map_size, lsb->map_size);
819 count = FC_MIN (NUM_LANG_SET_MAP, count);
820 for (i = 0; i < count; i++) {
821 if (lsa->map[i] != lsb->map[i])
822 return FcFalse;
823 }
824 if (!lsa->extra && !lsb->extra)
825 return FcTrue;
826 if (lsa->extra && lsb->extra)
827 return FcStrSetEqual (lsa->extra, lsb->extra);
828 return FcFalse;
829 }
830
831 static FcBool
832 FcLangSetContainsLang (const FcLangSet *ls, const FcChar8 *lang)
833 {
834 int id;
835 int i;
836
837 id = FcLangSetIndex (lang);
838 if (id < 0)
839 id = -id - 1;
840 else if (FcLangSetBitGet (ls, id))
841 return FcTrue;
842 /*
843 * search up and down among equal languages for a match
844 */
845 for (i = id - 1; i >= 0; i--) {
846 if (FcLangCompare (fcLangCharSets[i].lang, lang) == FcLangDifferentLang)
847 break;
848 if (FcLangSetBitGet (ls, i) &&
849 FcLangContains (fcLangCharSets[i].lang, lang))
850 return FcTrue;
851 }
852 for (i = id; i < NUM_LANG_CHAR_SET; i++) {
853 if (FcLangCompare (fcLangCharSets[i].lang, lang) == FcLangDifferentLang)
854 break;
855 if (FcLangSetBitGet (ls, i) &&
856 FcLangContains (fcLangCharSets[i].lang, lang))
857 return FcTrue;
858 }
859 if (ls->extra) {
860 FcStrList *list = FcStrListCreate (ls->extra);
861 FcChar8 *extra;
862
863 if (list) {
864 while ((extra = FcStrListNext (list))) {
865 if (FcLangContains (extra, lang))
866 break;
867 }
868 FcStrListDone (list);
869 if (extra)
870 return FcTrue;
871 }
872 }
873 return FcFalse;
874 }
875
876 /*
877 * return FcTrue if lsa contains every language in lsb
878 */
879 FcBool
880 FcLangSetContains (const FcLangSet *lsa, const FcLangSet *lsb)
881 {
882 int i, j, count;
883 FcChar32 missing;
884
885 if (FcDebug() & FC_DBG_MATCHV) {
886 printf ("FcLangSet ");
887 FcLangSetPrint (lsa);
888 printf (" contains ");
889 FcLangSetPrint (lsb);
890 printf ("\n");
891 }
892 /*
893 * check bitmaps for missing language support
894 */
895 count = FC_MIN (lsa->map_size, lsb->map_size);
896 count = FC_MIN (NUM_LANG_SET_MAP, count);
897 for (i = 0; i < count; i++) {
898 missing = lsb->map[i] & ~lsa->map[i];
899 if (missing) {
900 for (j = 0; j < 32; j++)
901 if (missing & (1U << j)) {
902 if (!FcLangSetContainsLang (lsa,
903 fcLangCharSets[fcLangCharSetIndicesInv[i * 32 + j]].lang)) {
904 if (FcDebug() & FC_DBG_MATCHV)
905 printf ("\tMissing bitmap %s\n", fcLangCharSets[fcLangCharSetIndicesInv[i * 32 + j]].lang);
906 return FcFalse;
907 }
908 }
909 }
910 }
911 if (lsb->extra) {
912 FcStrList *list = FcStrListCreate (lsb->extra);
913 FcChar8 *extra;
914
915 if (list) {
916 while ((extra = FcStrListNext (list))) {
917 if (!FcLangSetContainsLang (lsa, extra)) {
918 if (FcDebug() & FC_DBG_MATCHV)
919 printf ("\tMissing string %s\n", extra);
920 break;
921 }
922 }
923 FcStrListDone (list);
924 if (extra)
925 return FcFalse;
926 }
927 }
928 return FcTrue;
929 }
930
931 FcBool
932 FcLangSetSerializeAlloc (FcSerialize *serialize, const FcLangSet *l)
933 {
934 if (!FcSerializeAlloc (serialize, l, sizeof (FcLangSet)))
935 return FcFalse;
936 return FcTrue;
937 }
938
939 FcLangSet *
940 FcLangSetSerialize (FcSerialize *serialize, const FcLangSet *l)
941 {
942 FcLangSet *l_serialize = FcSerializePtr (serialize, l);
943
944 if (!l_serialize)
945 return NULL;
946 memset (l_serialize->map, '\0', sizeof (l_serialize->map));
947 memcpy (l_serialize->map, l->map, FC_MIN (sizeof (l_serialize->map), l->map_size * sizeof (l->map[0])));
948 l_serialize->map_size = NUM_LANG_SET_MAP;
949 l_serialize->extra = NULL; /* We don't serialize ls->extra */
950 return l_serialize;
951 }
952
953 FcStrSet *
954 FcLangSetGetLangs (const FcLangSet *ls)
955 {
956 FcStrSet *langs;
957 int i;
958
959 langs = FcStrSetCreate();
960 if (!langs)
961 return 0;
962
963 for (i = 0; i < NUM_LANG_CHAR_SET; i++)
964 if (FcLangSetBitGet (ls, i))
965 FcStrSetAdd (langs, fcLangCharSets[i].lang);
966
967 if (ls->extra) {
968 FcStrList *list = FcStrListCreate (ls->extra);
969 FcChar8 *extra;
970
971 if (list) {
972 while ((extra = FcStrListNext (list)))
973 FcStrSetAdd (langs, extra);
974
975 FcStrListDone (list);
976 }
977 }
978
979 return langs;
980 }
981
982 static FcLangSet *
983 FcLangSetOperate (const FcLangSet *a,
984 const FcLangSet *b,
985 FcBool (*func) (FcLangSet *ls,
986 const FcChar8 *s))
987 {
988 FcLangSet *langset = FcLangSetCopy (a);
989 FcStrSet *set = FcLangSetGetLangs (b);
990 FcStrList *sl = FcStrListCreate (set);
991 FcChar8 *str;
992
993 FcStrSetDestroy (set);
994 while ((str = FcStrListNext (sl))) {
995 func (langset, str);
996 }
997 FcStrListDone (sl);
998
999 return langset;
1000 }
1001
1002 FcLangSet *
1003 FcLangSetUnion (const FcLangSet *a, const FcLangSet *b)
1004 {
1005 return FcLangSetOperate (a, b, FcLangSetAdd);
1006 }
1007
1008 FcLangSet *
1009 FcLangSetSubtract (const FcLangSet *a, const FcLangSet *b)
1010 {
1011 return FcLangSetOperate (a, b, FcLangSetDel);
1012 }
1013
1014 FcBool
1015 FcLangIsExclusive (const FcChar8 *lang)
1016 {
1017 int i;
1018
1019 for (i = 0; i < NUM_CODE_PAGE_RANGE; i++) {
1020 if (FcLangCompare (lang, FcCodePageRange[i].lang) == FcLangEqual)
1021 return FcTrue;
1022 }
1023 return FcFalse;
1024 }
1025
1026 const FcChar8 *
1027 FcLangIsExclusiveFromOs2 (unsigned long os2ulUnicodeRange1, unsigned long os2ulUnicodeRange2)
1028 {
1029 unsigned int i;
1030 const FcChar8 *exclusiveLang = 0;
1031
1032 for (i = 0; i < NUM_CODE_PAGE_RANGE; i++) {
1033 unsigned long bits;
1034 int bit;
1035 if (FcCodePageRange[i].bit < 32) {
1036 bits = os2ulUnicodeRange1;
1037 bit = FcCodePageRange[i].bit;
1038 } else {
1039 bits = os2ulUnicodeRange2;
1040 bit = FcCodePageRange[i].bit - 32;
1041 }
1042 if (bits & (1U << bit)) {
1043 /*
1044 * If the font advertises support for multiple
1045 * "exclusive" languages, then include support
1046 * for any language found to have coverage
1047 */
1048 if (exclusiveLang) {
1049 exclusiveLang = 0;
1050 break;
1051 }
1052 exclusiveLang = FcCodePageRange[i].lang;
1053 }
1054 }
1055 return exclusiveLang;
1056 }
1057
1058 #define __fclang__
1059 #include "fcaliastail.h"
1060 #include "fcftaliastail.h"
1061 #undef __fclang__