Add editing langset feature.
[platform/upstream/fontconfig.git] / src / fclang.c
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 #include "fcftint.h"
27
28 typedef struct {
29     const FcChar8       lang[8];
30     const FcCharSet     charset;
31 } FcLangCharSet;
32
33 typedef struct {
34     int begin;
35     int end;
36 } FcLangCharSetRange;
37
38 #include "../fc-lang/fclang.h"
39
40 struct _FcLangSet {
41     FcStrSet    *extra;
42     FcChar32    map_size;
43     FcChar32    map[NUM_LANG_SET_MAP];
44 };
45
46 static void
47 FcLangSetBitSet (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
60 static FcBool
61 FcLangSetBitGet (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 }
73
74 static void
75 FcLangSetBitReset (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
88 FcLangSet *
89 FcFreeTypeLangSet (const FcCharSet  *charset,
90                    const FcChar8    *exclusiveLang)
91 {
92     int             i, j;
93     FcChar32        missing;
94     const FcCharSet *exclusiveCharset = 0;
95     FcLangSet       *ls;
96
97     if (exclusiveLang)
98         exclusiveCharset = FcLangGetCharSet (exclusiveLang);
99     ls = FcLangSetCreate ();
100     if (!ls)
101         return 0;
102     if (FcDebug() & FC_DBG_LANGSET)
103     {
104         printf ("font charset");
105         FcCharSetPrint (charset);
106         printf ("\n");
107     }
108     for (i = 0; i < NUM_LANG_CHAR_SET; i++)
109     {
110         if (FcDebug() & FC_DBG_LANGSET)
111         {
112             printf ("%s charset", fcLangCharSets[i].lang);
113             FcCharSetPrint (&fcLangCharSets[i].charset);
114             printf ("\n");
115         }
116         
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 &&
123             FcFreeTypeIsExclusiveLang (fcLangCharSets[i].lang))
124         {
125             if (fcLangCharSets[i].charset.num != exclusiveCharset->num)
126                 continue;
127
128             for (j = 0; j < fcLangCharSets[i].charset.num; j++)
129                 if (FcCharSetLeaf(&fcLangCharSets[i].charset, j) !=
130                     FcCharSetLeaf(exclusiveCharset, j))
131                     continue;
132         }
133         missing = FcCharSetSubtractCount (&fcLangCharSets[i].charset, charset);
134         if (FcDebug() & FC_DBG_SCANV)
135         {
136             if (missing && missing < 10)
137             {
138                 FcCharSet   *missed = FcCharSetSubtract (&fcLangCharSets[i].charset,
139                                                          charset);
140                 FcChar32    ucs4;
141                 FcChar32    map[FC_CHARSET_MAP_SIZE];
142                 FcChar32    next;
143
144                 printf ("\n%s(%u) ", fcLangCharSets[i].lang, missing);
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
163                 printf ("%s(%u) ", fcLangCharSets[i].lang, missing);
164         }
165         if (!missing)
166             FcLangSetBitSet (ls, i);
167     }
168
169     if (FcDebug() & FC_DBG_SCANV)
170         printf ("\n");
171
172
173     return ls;
174 }
175
176 #define FcLangEnd(c)    ((c) == '-' || (c) == '\0')
177
178 FcLangResult
179 FcLangCompare (const FcChar8 *s1, const FcChar8 *s2)
180 {
181     FcChar8         c1, c2;
182     FcLangResult    result = FcLangDifferentLang;
183
184     for (;;)
185     {
186         c1 = *s1++;
187         c2 = *s2++;
188         
189         c1 = FcToLower (c1);
190         c2 = FcToLower (c2);
191         if (c1 != c2)
192         {
193             if (FcLangEnd (c1) && FcLangEnd (c2))
194                 result = FcLangDifferentTerritory;
195             return result;
196         }
197         else if (!c1)
198             return FcLangEqual;
199         else if (c1 == '-')
200             result = FcLangDifferentTerritory;
201     }
202 }
203
204 /*
205  * Return FcTrue when super contains sub.
206  *
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
210  */
211
212 static FcBool
213 FcLangContains (const FcChar8 *super, const FcChar8 *sub)
214 {
215     FcChar8         c1, c2;
216
217     for (;;)
218     {
219         c1 = *super++;
220         c2 = *sub++;
221         
222         c1 = FcToLower (c1);
223         c2 = FcToLower (c2);
224         if (c1 != c2)
225         {
226             /* see if super has a country while sub is mising one */
227             if (c1 == '-' && c2 == '\0')
228                 return FcTrue;
229             /* see if sub has a country while super is mising one */
230             if (c1 == '\0' && c2 == '-')
231                 return FcTrue;
232             return FcFalse;
233         }
234         else if (!c1)
235             return FcTrue;
236     }
237 }
238
239 const FcCharSet *
240 FcLangGetCharSet (const FcChar8 *lang)
241 {
242     int         i;
243     int         country = -1;
244
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;
250         case FcLangDifferentTerritory:
251             if (country == -1)
252                 country = i;
253         case FcLangDifferentLang:
254         default:
255             break;
256         }
257     }
258     if (country == -1)
259         return 0;
260     return &fcLangCharSets[country].charset;
261 }
262
263 FcStrSet *
264 FcGetLangs (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
279 FcLangSet *
280 FcLangSetCreate (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));
289     ls->map_size = NUM_LANG_SET_MAP;
290     ls->extra = 0;
291     return ls;
292 }
293
294 void
295 FcLangSetDestroy (FcLangSet *ls)
296 {
297     if (ls->extra)
298         FcStrSetDestroy (ls->extra);
299     FcMemFree (FC_MEM_LANGSET, sizeof (FcLangSet));
300     free (ls);
301 }
302
303 FcLangSet *
304 FcLangSetCopy (const FcLangSet *ls)
305 {
306     FcLangSet   *new;
307
308     new = FcLangSetCreate ();
309     if (!new)
310         goto bail0;
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])));
313     if (ls->extra)
314     {
315         FcStrList       *list;
316         FcChar8         *extra;
317         
318         new->extra = FcStrSetCreate ();
319         if (!new->extra)
320             goto bail1;
321
322         list = FcStrListCreate (ls->extra);     
323         if (!list)
324             goto bail1;
325         
326         while ((extra = FcStrListNext (list)))
327             if (!FcStrSetAdd (new->extra, extra))
328             {
329                 FcStrListDone (list);
330                 goto bail1;
331             }
332         FcStrListDone (list);
333     }
334     return new;
335 bail1:
336     FcLangSetDestroy (new);
337 bail0:
338     return 0;
339 }
340
341 static int
342 FcLangSetIndex (const FcChar8 *lang)
343 {
344     int     low, high, mid = 0;
345     int     cmp = 0;
346     FcChar8 firstChar = FcToLower(lang[0]);
347     FcChar8 secondChar = firstChar ? FcToLower(lang[1]) : '\0';
348
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     }
367
368     while (low <= high)
369     {
370         mid = (high + low) >> 1;
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) */
376             cmp = fcLangCharSets[mid].lang[1] - secondChar;
377             if (cmp == 0 &&
378                 (fcLangCharSets[mid].lang[2] != '\0' ||
379                  lang[2] != '\0'))
380             {
381                 cmp = FcStrCmpIgnoreCase(fcLangCharSets[mid].lang+2,
382                                          lang+2);
383             }
384         }
385         if (cmp == 0)
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
397 FcBool
398 FcLangSetAdd (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     }
408     if (!ls->extra)
409     {
410         ls->extra = FcStrSetCreate ();
411         if (!ls->extra)
412             return FcFalse;
413     }
414     return FcStrSetAdd (ls->extra, lang);
415 }
416
417 FcBool
418 FcLangSetDel (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
434 FcLangResult
435 FcLangSetHasLang (const FcLangSet *ls, const FcChar8 *lang)
436 {
437     int             id;
438     FcLangResult    best, r;
439     int             i;
440
441     id = FcLangSetIndex (lang);
442     if (id < 0)
443         id = -id - 1;
444     else if (FcLangSetBitGet (ls, id))
445         return FcLangEqual;
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     }
463     if (ls->extra)
464     {
465         FcStrList       *list = FcStrListCreate (ls->extra);
466         FcChar8         *extra;
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
482 static FcLangResult
483 FcLangSetCompareStrSet (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
502 FcLangResult
503 FcLangSetCompare (const FcLangSet *lsa, const FcLangSet *lsb)
504 {
505     int             i, j, count;
506     FcLangResult    best, r;
507
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++)
511         if (lsa->map[i] & lsb->map[i])
512             return FcLangEqual;
513     best = FcLangDifferentLang;
514     for (j = 0; j < NUM_COUNTRY_SET; j++)
515         for (i = 0; i < count; i++)
516             if ((lsa->map[i] & fcLangCountrySets[j][i]) &&
517                 (lsb->map[i] & fcLangCountrySets[j][i]))
518             {
519                 best = FcLangDifferentTerritory;
520                 break;
521             }
522     if (lsa->extra)
523     {
524         r = FcLangSetCompareStrSet (lsb, lsa->extra);
525         if (r < best)
526             best = r;
527     }
528     if (best > FcLangEqual && lsb->extra)
529     {
530         r = FcLangSetCompareStrSet (lsa, lsb->extra);
531         if (r < best)
532             best = r;
533     }
534     return best;
535 }
536
537 /*
538  * Used in computing values -- mustn't allocate any storage
539  * XXX Not thread-safe
540  */
541 FcLangSet *
542 FcLangSetPromote (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));
550     ls.map_size = NUM_LANG_SET_MAP;
551     ls.extra = 0;
552     id = FcLangSetIndex (lang);
553     if (id > 0)
554     {
555         FcLangSetBitSet (&ls, id);
556     }
557     else
558     {
559         ls.extra = &strs;
560         strs.num = 1;
561         strs.size = 1;
562         strs.strs = &str;
563         strs.ref = 1;
564         str = (FcChar8 *) lang;
565     }
566     return &ls;
567 }
568
569 FcChar32
570 FcLangSetHash (const FcLangSet *ls)
571 {
572     FcChar32    h = 0;
573     int         i, count;
574
575     count = FC_MIN (ls->map_size, NUM_LANG_SET_MAP);
576     for (i = 0; i < count; i++)
577         h ^= ls->map[i];
578     if (ls->extra)
579         h ^= ls->extra->num;
580     return h;
581 }
582
583 FcLangSet *
584 FcNameParseLangSet (const FcChar8 *string)
585 {
586     FcChar8         lang[32], c = 0;
587     int i;
588     FcLangSet       *ls;
589
590     ls = FcLangSetCreate ();
591     if (!ls)
592         goto bail0;
593
594     for(;;)
595     {
596         for(i = 0; i < 31;i++)
597         {
598             c = *string++;
599             if(c == '\0' || c == '|')
600                 break; /* end of this code */
601             lang[i] = c;
602         }
603         lang[i] = '\0';
604         if (!FcLangSetAdd (ls, lang))
605             goto bail1;
606         if(c == '\0')
607             break;
608     }
609     return ls;
610 bail1:
611     FcLangSetDestroy (ls);
612 bail0:
613     return 0;
614 }
615
616 FcBool
617 FcNameUnparseLangSet (FcStrBuf *buf, const FcLangSet *ls)
618 {
619     int         i, bit, count;
620     FcChar32    bits;
621     FcBool      first = FcTrue;
622
623     count = FC_MIN (ls->map_size, NUM_LANG_SET_MAP);
624     for (i = 0; i < count; i++)
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))
636                         return FcFalse;
637                     first = FcFalse;
638                 }
639         }
640     }
641     if (ls->extra)
642     {
643         FcStrList   *list = FcStrListCreate (ls->extra);
644         FcChar8     *extra;
645
646         if (!list)
647             return FcFalse;
648         while ((extra = FcStrListNext (list)))
649         {
650             if (!first)
651                 if (!FcStrBufChar (buf, '|'))
652                 {
653                     FcStrListDone (list);
654                     return FcFalse;
655                 }
656             if (!FcStrBufString (buf, extra))
657                 {
658                     FcStrListDone (list);
659                     return FcFalse;
660                 }
661             first = FcFalse;
662         }
663         FcStrListDone (list);
664     }
665     return FcTrue;
666 }
667
668 FcBool
669 FcLangSetEqual (const FcLangSet *lsa, const FcLangSet *lsb)
670 {
671     int     i, count;
672
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++)
676     {
677         if (lsa->map[i] != lsb->map[i])
678             return FcFalse;
679     }
680     if (!lsa->extra && !lsb->extra)
681         return FcTrue;
682     if (lsa->extra && lsb->extra)
683         return FcStrSetEqual (lsa->extra, lsb->extra);
684     return FcFalse;
685 }
686
687 static FcBool
688 FcLangSetContainsLang (const FcLangSet *ls, const FcChar8 *lang)
689 {
690     int             id;
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     }
717     if (ls->extra)
718     {
719         FcStrList       *list = FcStrListCreate (ls->extra);
720         FcChar8         *extra;
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  */
740 FcBool
741 FcLangSetContains (const FcLangSet *lsa, const FcLangSet *lsb)
742 {
743     int             i, j, count;
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      */
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++)
758     {
759         missing = lsb->map[i] & ~lsa->map[i];
760         if (missing)
761         {
762             for (j = 0; j < 32; j++)
763                 if (missing & (1 << j))
764                 {
765                     if (!FcLangSetContainsLang (lsa,
766                                                 fcLangCharSets[fcLangCharSetIndicesInv[i*32 + j]].lang))
767                     {
768                         if (FcDebug() & FC_DBG_MATCHV)
769                             printf ("\tMissing bitmap %s\n", fcLangCharSets[fcLangCharSetIndicesInv[i*32+j]].lang);
770                         return FcFalse;
771                     }
772                 }
773         }
774     }
775     if (lsb->extra)
776     {
777         FcStrList   *list = FcStrListCreate (lsb->extra);
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 }
798
799 FcBool
800 FcLangSetSerializeAlloc (FcSerialize *serialize, const FcLangSet *l)
801 {
802     if (!FcSerializeAlloc (serialize, l, sizeof (FcLangSet)))
803         return FcFalse;
804     return FcTrue;
805 }
806
807 FcLangSet *
808 FcLangSetSerialize(FcSerialize *serialize, const FcLangSet *l)
809 {
810     FcLangSet   *l_serialize = FcSerializePtr (serialize, l);
811
812     if (!l_serialize)
813         return NULL;
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;
817     l_serialize->extra = NULL; /* We don't serialize ls->extra */
818     return l_serialize;
819 }
820
821 FcStrSet *
822 FcLangSetGetLangs (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
852 static FcLangSet *
853 FcLangSetOperate(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
871 FcLangSet *
872 FcLangSetUnion (const FcLangSet *a, const FcLangSet *b)
873 {
874     return FcLangSetOperate(a, b, FcLangSetAdd);
875 }
876
877 FcLangSet *
878 FcLangSetSubtract (const FcLangSet *a, const FcLangSet *b)
879 {
880     return FcLangSetOperate(a, b, FcLangSetDel);
881 }
882
883 #define __fclang__
884 #include "fcaliastail.h"
885 #include "fcftaliastail.h"
886 #undef __fclang__