1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
|
/*
* fontconfig/src/fclang.c
*
* Copyright © 2002 Keith Packard
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Keith Packard not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. Keith Packard makes no
* representations about the suitability of this software for any purpose. It
* is provided "as is" without express or implied warranty.
*
* THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include "fcint.h"
#include "fcftint.h"
typedef struct {
const FcChar8 lang[8];
const FcCharSet charset;
} FcLangCharSet;
typedef struct {
int begin;
int end;
} FcLangCharSetRange;
#include "../fc-lang/fclang.h"
struct _FcLangSet {
FcChar32 map[NUM_LANG_SET_MAP];
FcStrSet *extra;
};
#define FcLangSetBitSet(ls, id) ((ls)->map[(fcLangCharSetIndices[id])>>5] |= ((FcChar32) 1 << ((fcLangCharSetIndices[id]) & 0x1f)))
#define FcLangSetBitGet(ls, id) (((ls)->map[(fcLangCharSetIndices[id])>>5] >> ((fcLangCharSetIndices[id]) & 0x1f)) & 1)
FcLangSet *
FcFreeTypeLangSet (const FcCharSet *charset,
const FcChar8 *exclusiveLang)
{
int i, j;
FcChar32 missing;
const FcCharSet *exclusiveCharset = 0;
FcLangSet *ls;
if (exclusiveLang)
exclusiveCharset = FcLangGetCharSet (exclusiveLang);
ls = FcLangSetCreate ();
if (!ls)
return 0;
if (FcDebug() & FC_DBG_LANGSET)
{
printf ("font charset\n");
FcCharSetPrint (charset);
printf ("\n");
}
for (i = 0; i < NUM_LANG_CHAR_SET; i++)
{
if (FcDebug() & FC_DBG_LANGSET)
{
printf ("%s charset\n", fcLangCharSets[i].lang);
FcCharSetPrint (&fcLangCharSets[i].charset);
printf ("\n");
}
/*
* Check for Han charsets to make fonts
* which advertise support for a single language
* not support other Han languages
*/
if (exclusiveCharset &&
FcFreeTypeIsExclusiveLang (fcLangCharSets[i].lang))
{
if (fcLangCharSets[i].charset.num != exclusiveCharset->num)
continue;
for (j = 0; j < fcLangCharSets[i].charset.num; j++)
if (FcCharSetLeaf(&fcLangCharSets[i].charset, j) !=
FcCharSetLeaf(exclusiveCharset, j))
continue;
}
missing = FcCharSetSubtractCount (&fcLangCharSets[i].charset, charset);
if (FcDebug() & FC_DBG_SCANV)
{
if (missing && missing < 10)
{
FcCharSet *missed = FcCharSetSubtract (&fcLangCharSets[i].charset,
charset);
FcChar32 ucs4;
FcChar32 map[FC_CHARSET_MAP_SIZE];
FcChar32 next;
printf ("\n%s(%u) ", fcLangCharSets[i].lang, missing);
printf ("{");
for (ucs4 = FcCharSetFirstPage (missed, map, &next);
ucs4 != FC_CHARSET_DONE;
ucs4 = FcCharSetNextPage (missed, map, &next))
{
int i, j;
for (i = 0; i < FC_CHARSET_MAP_SIZE; i++)
if (map[i])
{
for (j = 0; j < 32; j++)
if (map[i] & (1 << j))
printf (" %04x", ucs4 + i * 32 + j);
}
}
printf (" }\n\t");
FcCharSetDestroy (missed);
}
else
printf ("%s(%u) ", fcLangCharSets[i].lang, missing);
}
if (!missing)
FcLangSetBitSet (ls, i);
}
if (FcDebug() & FC_DBG_SCANV)
printf ("\n");
return ls;
}
#define FcLangEnd(c) ((c) == '-' || (c) == '\0')
FcLangResult
FcLangCompare (const FcChar8 *s1, const FcChar8 *s2)
{
FcChar8 c1, c2;
FcLangResult result = FcLangDifferentLang;
for (;;)
{
c1 = *s1++;
c2 = *s2++;
c1 = FcToLower (c1);
c2 = FcToLower (c2);
if (c1 != c2)
{
if (FcLangEnd (c1) && FcLangEnd (c2))
result = FcLangDifferentTerritory;
return result;
}
else if (!c1)
return FcLangEqual;
else if (c1 == '-')
result = FcLangDifferentTerritory;
}
}
/*
* Return FcTrue when super contains sub.
*
* super contains sub if super and sub have the same
* language and either the same country or one
* is missing the country
*/
static FcBool
FcLangContains (const FcChar8 *super, const FcChar8 *sub)
{
FcChar8 c1, c2;
for (;;)
{
c1 = *super++;
c2 = *sub++;
c1 = FcToLower (c1);
c2 = FcToLower (c2);
if (c1 != c2)
{
/* see if super has a country while sub is mising one */
if (c1 == '-' && c2 == '\0')
return FcTrue;
/* see if sub has a country while super is mising one */
if (c1 == '\0' && c2 == '-')
return FcTrue;
return FcFalse;
}
else if (!c1)
return FcTrue;
}
}
const FcCharSet *
FcLangGetCharSet (const FcChar8 *lang)
{
int i;
int country = -1;
for (i = 0; i < NUM_LANG_CHAR_SET; i++)
{
switch (FcLangCompare (lang, fcLangCharSets[i].lang)) {
case FcLangEqual:
return &fcLangCharSets[i].charset;
case FcLangDifferentTerritory:
if (country == -1)
country = i;
default:
break;
}
}
if (country == -1)
return 0;
return &fcLangCharSets[country].charset;
}
FcStrSet *
FcGetLangs (void)
{
FcStrSet *langs;
int i;
langs = FcStrSetCreate();
if (!langs)
return 0;
for (i = 0; i < NUM_LANG_CHAR_SET; i++)
FcStrSetAdd (langs, fcLangCharSets[i].lang);
return langs;
}
FcLangSet *
FcLangSetCreate (void)
{
FcLangSet *ls;
ls = malloc (sizeof (FcLangSet));
if (!ls)
return 0;
FcMemAlloc (FC_MEM_LANGSET, sizeof (FcLangSet));
memset (ls->map, '\0', sizeof (ls->map));
ls->extra = 0;
return ls;
}
void
FcLangSetDestroy (FcLangSet *ls)
{
if (ls->extra)
FcStrSetDestroy (ls->extra);
FcMemFree (FC_MEM_LANGSET, sizeof (FcLangSet));
free (ls);
}
FcLangSet *
FcLangSetCopy (const FcLangSet *ls)
{
FcLangSet *new;
new = FcLangSetCreate ();
if (!new)
goto bail0;
memcpy (new->map, ls->map, sizeof (new->map));
if (ls->extra)
{
FcStrList *list;
FcChar8 *extra;
new->extra = FcStrSetCreate ();
if (!new->extra)
goto bail1;
list = FcStrListCreate (ls->extra);
if (!list)
goto bail1;
while ((extra = FcStrListNext (list)))
if (!FcStrSetAdd (new->extra, extra))
{
FcStrListDone (list);
goto bail1;
}
FcStrListDone (list);
}
return new;
bail1:
FcLangSetDestroy (new);
bail0:
return 0;
}
static int
FcLangSetIndex (const FcChar8 *lang)
{
int low, high, mid = 0;
int cmp = 0;
FcChar8 firstChar = FcToLower(lang[0]);
FcChar8 secondChar = firstChar ? FcToLower(lang[1]) : '\0';
if (firstChar < 'a')
{
low = 0;
high = fcLangCharSetRanges[0].begin;
}
else if(firstChar > 'z')
{
low = fcLangCharSetRanges[25].begin;
high = NUM_LANG_CHAR_SET - 1;
}
else
{
low = fcLangCharSetRanges[firstChar - 'a'].begin;
high = fcLangCharSetRanges[firstChar - 'a'].end;
/* no matches */
if (low > high)
return -low; /* next entry after where it would be */
}
while (low <= high)
{
mid = (high + low) >> 1;
if(fcLangCharSets[mid].lang[0] != firstChar)
cmp = FcStrCmpIgnoreCase(fcLangCharSets[mid].lang, lang);
else
{ /* fast path for resolving 2-letter languages (by far the most common) after
* finding the first char (probably already true because of the hash table) */
cmp = fcLangCharSets[mid].lang[1] - secondChar;
if (cmp == 0 &&
(fcLangCharSets[mid].lang[2] != '\0' ||
lang[2] != '\0'))
{
cmp = FcStrCmpIgnoreCase(fcLangCharSets[mid].lang+2,
lang+2);
}
}
if (cmp == 0)
return mid;
if (cmp < 0)
low = mid + 1;
else
high = mid - 1;
}
if (cmp < 0)
mid++;
return -(mid + 1);
}
FcBool
FcLangSetAdd (FcLangSet *ls, const FcChar8 *lang)
{
int id;
id = FcLangSetIndex (lang);
if (id >= 0)
{
FcLangSetBitSet (ls, id);
return FcTrue;
}
if (!ls->extra)
{
ls->extra = FcStrSetCreate ();
if (!ls->extra)
return FcFalse;
}
return FcStrSetAdd (ls->extra, lang);
}
FcLangResult
FcLangSetHasLang (const FcLangSet *ls, const FcChar8 *lang)
{
int id;
FcLangResult best, r;
int i;
id = FcLangSetIndex (lang);
if (id < 0)
id = -id - 1;
else if (FcLangSetBitGet (ls, id))
return FcLangEqual;
best = FcLangDifferentLang;
for (i = id - 1; i >= 0; i--)
{
r = FcLangCompare (lang, fcLangCharSets[i].lang);
if (r == FcLangDifferentLang)
break;
if (FcLangSetBitGet (ls, i) && r < best)
best = r;
}
for (i = id; i < NUM_LANG_CHAR_SET; i++)
{
r = FcLangCompare (lang, fcLangCharSets[i].lang);
if (r == FcLangDifferentLang)
break;
if (FcLangSetBitGet (ls, i) && r < best)
best = r;
}
if (ls->extra)
{
FcStrList *list = FcStrListCreate (ls->extra);
FcChar8 *extra;
if (list)
{
while (best > FcLangEqual && (extra = FcStrListNext (list)))
{
r = FcLangCompare (lang, extra);
if (r < best)
best = r;
}
FcStrListDone (list);
}
}
return best;
}
static FcLangResult
FcLangSetCompareStrSet (const FcLangSet *ls, FcStrSet *set)
{
FcStrList *list = FcStrListCreate (set);
FcLangResult r, best = FcLangDifferentLang;
FcChar8 *extra;
if (list)
{
while (best > FcLangEqual && (extra = FcStrListNext (list)))
{
r = FcLangSetHasLang (ls, extra);
if (r < best)
best = r;
}
FcStrListDone (list);
}
return best;
}
FcLangResult
FcLangSetCompare (const FcLangSet *lsa, const FcLangSet *lsb)
{
int i, j;
FcLangResult best, r;
for (i = 0; i < NUM_LANG_SET_MAP; i++)
if (lsa->map[i] & lsb->map[i])
return FcLangEqual;
best = FcLangDifferentLang;
for (j = 0; j < NUM_COUNTRY_SET; j++)
for (i = 0; i < NUM_LANG_SET_MAP; i++)
if ((lsa->map[i] & fcLangCountrySets[j][i]) &&
(lsb->map[i] & fcLangCountrySets[j][i]))
{
best = FcLangDifferentTerritory;
break;
}
if (lsa->extra)
{
r = FcLangSetCompareStrSet (lsb, lsa->extra);
if (r < best)
best = r;
}
if (best > FcLangEqual && lsb->extra)
{
r = FcLangSetCompareStrSet (lsa, lsb->extra);
if (r < best)
best = r;
}
return best;
}
/*
* Used in computing values -- mustn't allocate any storage
*/
FcLangSet *
FcLangSetPromote (const FcChar8 *lang)
{
static FcLangSet ls;
static FcStrSet strs;
static FcChar8 *str;
int id;
memset (ls.map, '\0', sizeof (ls.map));
ls.extra = 0;
id = FcLangSetIndex (lang);
if (id > 0)
{
FcLangSetBitSet (&ls, id);
}
else
{
ls.extra = &strs;
strs.num = 1;
strs.size = 1;
strs.strs = &str;
strs.ref = 1;
str = (FcChar8 *) lang;
}
return &ls;
}
FcChar32
FcLangSetHash (const FcLangSet *ls)
{
FcChar32 h = 0;
int i;
for (i = 0; i < NUM_LANG_SET_MAP; i++)
h ^= ls->map[i];
if (ls->extra)
h ^= ls->extra->num;
return h;
}
FcLangSet *
FcNameParseLangSet (const FcChar8 *string)
{
FcChar8 lang[32],c;
int i;
FcLangSet *ls;
ls = FcLangSetCreate ();
if (!ls)
goto bail0;
for(;;)
{
for(i = 0; i < 31;i++)
{
c = *string++;
if(c == '\0' || c == '|')
break; /* end of this code */
lang[i] = c;
}
lang[i] = '\0';
if (!FcLangSetAdd (ls, lang))
goto bail1;
if(c == '\0')
break;
}
return ls;
bail1:
FcLangSetDestroy (ls);
bail0:
return 0;
}
FcBool
FcNameUnparseLangSet (FcStrBuf *buf, const FcLangSet *ls)
{
int i;
FcBool first = FcTrue;
for (i = 0; i < NUM_LANG_CHAR_SET; i++)
if (FcLangSetBitGet (ls, i))
{
if (!first)
if (!FcStrBufChar (buf, '|'))
return FcFalse;
if (!FcStrBufString (buf, fcLangCharSets[i].lang))
return FcFalse;
first = FcFalse;
}
if (ls->extra)
{
FcStrList *list = FcStrListCreate (ls->extra);
FcChar8 *extra;
if (!list)
return FcFalse;
while ((extra = FcStrListNext (list)))
{
if (!first)
if (!FcStrBufChar (buf, '|'))
{
FcStrListDone (list);
return FcFalse;
}
if (!FcStrBufString (buf, extra))
{
FcStrListDone (list);
return FcFalse;
}
first = FcFalse;
}
FcStrListDone (list);
}
return FcTrue;
}
FcBool
FcLangSetEqual (const FcLangSet *lsa, const FcLangSet *lsb)
{
int i;
for (i = 0; i < NUM_LANG_SET_MAP; i++)
{
if (lsa->map[i] != lsb->map[i])
return FcFalse;
}
if (!lsa->extra && !lsb->extra)
return FcTrue;
if (lsa->extra && lsb->extra)
return FcStrSetEqual (lsa->extra, lsb->extra);
return FcFalse;
}
static FcBool
FcLangSetContainsLang (const FcLangSet *ls, const FcChar8 *lang)
{
int id;
int i;
id = FcLangSetIndex (lang);
if (id < 0)
id = -id - 1;
else if (FcLangSetBitGet (ls, id))
return FcTrue;
/*
* search up and down among equal languages for a match
*/
for (i = id - 1; i >= 0; i--)
{
if (FcLangCompare (fcLangCharSets[i].lang, lang) == FcLangDifferentLang)
break;
if (FcLangSetBitGet (ls, i) &&
FcLangContains (fcLangCharSets[i].lang, lang))
return FcTrue;
}
for (i = id; i < NUM_LANG_CHAR_SET; i++)
{
if (FcLangCompare (fcLangCharSets[i].lang, lang) == FcLangDifferentLang)
break;
if (FcLangSetBitGet (ls, i) &&
FcLangContains (fcLangCharSets[i].lang, lang))
return FcTrue;
}
if (ls->extra)
{
FcStrList *list = FcStrListCreate (ls->extra);
FcChar8 *extra;
if (list)
{
while ((extra = FcStrListNext (list)))
{
if (FcLangContains (extra, lang))
break;
}
FcStrListDone (list);
if (extra)
return FcTrue;
}
}
return FcFalse;
}
/*
* return FcTrue if lsa contains every language in lsb
*/
FcBool
FcLangSetContains (const FcLangSet *lsa, const FcLangSet *lsb)
{
int i, j;
FcChar32 missing;
if (FcDebug() & FC_DBG_MATCHV)
{
printf ("FcLangSet "); FcLangSetPrint (lsa);
printf (" contains "); FcLangSetPrint (lsb);
printf ("\n");
}
/*
* check bitmaps for missing language support
*/
for (i = 0; i < NUM_LANG_SET_MAP; i++)
{
missing = lsb->map[i] & ~lsa->map[i];
if (missing)
{
for (j = 0; j < 32; j++)
if (missing & (1 << j))
{
if (!FcLangSetContainsLang (lsa,
fcLangCharSets[i*32 + j].lang))
{
if (FcDebug() & FC_DBG_MATCHV)
printf ("\tMissing bitmap %s\n", fcLangCharSets[i*32+j].lang);
return FcFalse;
}
}
}
}
if (lsb->extra)
{
FcStrList *list = FcStrListCreate (lsb->extra);
FcChar8 *extra;
if (list)
{
while ((extra = FcStrListNext (list)))
{
if (!FcLangSetContainsLang (lsa, extra))
{
if (FcDebug() & FC_DBG_MATCHV)
printf ("\tMissing string %s\n", extra);
break;
}
}
FcStrListDone (list);
if (extra)
return FcFalse;
}
}
return FcTrue;
}
FcBool
FcLangSetSerializeAlloc (FcSerialize *serialize, const FcLangSet *l)
{
if (!FcSerializeAlloc (serialize, l, sizeof (FcLangSet)))
return FcFalse;
return FcTrue;
}
FcLangSet *
FcLangSetSerialize(FcSerialize *serialize, const FcLangSet *l)
{
FcLangSet *l_serialize = FcSerializePtr (serialize, l);
if (!l_serialize)
return NULL;
*l_serialize = *l;
return l_serialize;
}
FcStrSet *
FcLangSetGetLangs (const FcLangSet *ls)
{
FcStrSet *langs;
int i;
langs = FcStrSetCreate();
if (!langs)
return 0;
for (i = 0; i < NUM_LANG_CHAR_SET; i++)
if (FcLangSetBitGet (ls, i))
FcStrSetAdd (langs, fcLangCharSets[i].lang);
if (ls->extra)
{
FcStrList *list = FcStrListCreate (ls->extra);
FcChar8 *extra;
if (list)
{
while ((extra = FcStrListNext (list)))
FcStrSetAdd (langs, extra);
FcStrListDone (list);
}
}
return langs;
}
#define __fclang__
#include "fcaliastail.h"
#include "fcftaliastail.h"
#undef __fclang__
|