fclang.c 23.9 KB
Newer Older
1
/*
2
 * fontconfig/src/fclang.c
3
 *
4
 * Copyright © 2002 Keith Packard
5 6 7 8 9
 *
 * 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
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 14 15
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
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 20 21 22 23 24 25
 * 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"
26
#include "fcftint.h"
27

28 29
/* Objects MT-safe for readonly access. */

30
typedef struct {
31
    const FcChar8    	lang[16];
32
    const FcCharSet	charset;
33 34
} FcLangCharSet;

35 36 37 38 39
typedef struct {
    int begin;
    int end;
} FcLangCharSetRange;

40
#include "../fc-lang/fclang.h"
41

42
struct _FcLangSet {
43
    FcStrSet	*extra;
44
    FcChar32    map_size;
45
    FcChar32	map[NUM_LANG_SET_MAP];
46 47
};

48 49 50
static int FcLangSetIndex (const FcChar8 *lang);


51 52 53 54
static void
FcLangSetBitSet (FcLangSet    *ls,
		 unsigned int  id)
{
55
  unsigned int bucket;
56 57 58 59 60 61 62 63 64 65 66 67 68

  id = fcLangCharSetIndices[id];
  bucket = id >> 5;
  if (bucket >= ls->map_size)
    return; /* shouldn't happen really */

  ls->map[bucket] |= ((FcChar32) 1 << (id & 0x1f));
}

static FcBool
FcLangSetBitGet (const FcLangSet *ls,
		 unsigned int     id)
{
69
  unsigned int bucket;
70 71 72 73 74 75 76 77

  id = fcLangCharSetIndices[id];
  bucket = id >> 5;
  if (bucket >= ls->map_size)
    return FcFalse;

  return ((ls->map[bucket] >> (id & 0x1f)) & 1) ? FcTrue : FcFalse;
}
78

79 80 81 82
static void
FcLangSetBitReset (FcLangSet    *ls,
		   unsigned int  id)
{
83
  unsigned int bucket;
84 85 86 87 88 89 90 91 92

  id = fcLangCharSetIndices[id];
  bucket = id >> 5;
  if (bucket >= ls->map_size)
    return; /* shouldn't happen really */

  ls->map[bucket] &= ~((FcChar32) 1 << (id & 0x1f));
}

93
FcLangSet *
94
FcFreeTypeLangSet (const FcCharSet  *charset,
95
		   const FcChar8    *exclusiveLang)
96
{
97
    int		    i, j;
98 99
    FcChar32	    missing;
    const FcCharSet *exclusiveCharset = 0;
100
    FcLangSet	    *ls;
101

102
    if (exclusiveLang)
103
	exclusiveCharset = FcLangGetCharSet (exclusiveLang);
104 105 106
    ls = FcLangSetCreate ();
    if (!ls)
	return 0;
107
    if (FcDebug() & FC_DBG_LANGSET)
108
    {
109
	printf ("font charset");
110 111 112
	FcCharSetPrint (charset);
	printf ("\n");
    }
113 114
    for (i = 0; i < NUM_LANG_CHAR_SET; i++)
    {
115
	if (FcDebug() & FC_DBG_LANGSET)
116
	{
117
	    printf ("%s charset", fcLangCharSets[i].lang);
118 119 120 121
	    FcCharSetPrint (&fcLangCharSets[i].charset);
	    printf ("\n");
	}
	
122 123 124 125 126 127
	/*
	 * Check for Han charsets to make fonts
	 * which advertise support for a single language
	 * not support other Han languages
	 */
	if (exclusiveCharset &&
128
	    FcFreeTypeIsExclusiveLang (fcLangCharSets[i].lang))
129
	{
130 131 132 133
	    if (fcLangCharSets[i].charset.num != exclusiveCharset->num)
		continue;

	    for (j = 0; j < fcLangCharSets[i].charset.num; j++)
134
		if (FcCharSetLeaf(&fcLangCharSets[i].charset, j) !=
135
		    FcCharSetLeaf(exclusiveCharset, j))
136
		    continue;
137
	}
138 139
	missing = FcCharSetSubtractCount (&fcLangCharSets[i].charset, charset);
        if (FcDebug() & FC_DBG_SCANV)
140 141 142
	{
	    if (missing && missing < 10)
	    {
143
		FcCharSet   *missed = FcCharSetSubtract (&fcLangCharSets[i].charset,
144 145 146 147 148
							 charset);
		FcChar32    ucs4;
		FcChar32    map[FC_CHARSET_MAP_SIZE];
		FcChar32    next;

149
		printf ("\n%s(%u) ", fcLangCharSets[i].lang, missing);
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
		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
168
		printf ("%s(%u) ", fcLangCharSets[i].lang, missing);
169
	}
170
	if (!missing)
171
	    FcLangSetBitSet (ls, i);
172
    }
173

174 175
    if (FcDebug() & FC_DBG_SCANV)
	printf ("\n");
176 177


178
    return ls;
179 180
}

181 182 183 184 185
FcChar8 *
FcLangNormalize (const FcChar8 *lang)
{
    FcChar8 *result = NULL, *s, *orig;
    char *territory, *encoding, *modifier;
186
    size_t llen, tlen = 0, mlen = 0;
187 188 189 190

    if (!lang || !*lang)
	return NULL;

191 192 193
    /* might be called without initialization */
    FcInitDebug ();

194
    if (FcStrCmpIgnoreCase (lang, (const FcChar8 *)"C") == 0 ||
195 196
	FcStrCmpIgnoreCase (lang, (const FcChar8 *)"C.UTF-8") == 0 ||
	FcStrCmpIgnoreCase (lang, (const FcChar8 *)"C.utf8") == 0 ||
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
	FcStrCmpIgnoreCase (lang, (const FcChar8 *)"POSIX") == 0)
    {
	result = FcStrCopy ((const FcChar8 *)"en");
	goto bail;
    }

    s = FcStrCopy (lang);
    if (!s)
	goto bail;

    /* from the comments in glibc:
     *
     * LOCALE can consist of up to four recognized parts for the XPG syntax:
     *
     *            language[_territory[.codeset]][@modifier]
     *
     * Beside the first all of them are allowed to be missing.  If the
     * full specified locale is not found, the less specific one are
     * looked for.  The various part will be stripped off according to
     * the following order:
     *            (1) codeset
     *            (2) normalized codeset
     *            (3) territory
     *            (4) modifier
     *
     * So since we don't take care of the codeset part here, what patterns
     * we need to deal with is:
     *
     *   1. language_territory@modifier
     *   2. language@modifier
     *   3. language
     *
     * then. and maybe no need to try language_territory here.
     */
    modifier = strchr ((const char *) s, '@');
    if (modifier)
    {
	*modifier = 0;
	modifier++;
	mlen = strlen (modifier);
    }
    encoding = strchr ((const char *) s, '.');
    if (encoding)
    {
	*encoding = 0;
	encoding++;
	if (modifier)
	{
	    memmove (encoding, modifier, mlen + 1);
	    modifier = encoding;
	}
    }
    territory = strchr ((const char *) s, '_');
    if (!territory)
	territory = strchr ((const char *) s, '-');
    if (territory)
    {
	*territory = 0;
	territory++;
	tlen = strlen (territory);
    }
    llen = strlen ((const char *) s);
    if (llen < 2 || llen > 3)
    {
	fprintf (stderr, "Fontconfig warning: ignoring %s: not a valid language tag\n",
		 lang);
	goto bail0;
    }
265 266
    if (territory && (tlen < 2 || tlen > 3) &&
	!(territory[0] == 'z' && tlen < 5))
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
    {
	fprintf (stderr, "Fontconfig warning: ignoring %s: not a valid region tag\n",
		 lang);
	goto bail0;
    }
    if (territory)
	territory[-1] = '-';
    if (modifier)
	modifier[-1] = '@';
    orig = FcStrDowncase (s);
    if (!orig)
	goto bail0;
    if (territory)
    {
	if (FcDebug () & FC_DBG_LANGSET)
	    printf("Checking the existence of %s.orth\n", s);
	if (FcLangSetIndex (s) < 0)
	{
	    memmove (territory - 1, territory + tlen, (mlen > 0 ? mlen + 1 : 0) + 1);
	    if (modifier)
		modifier = territory;
	}
	else
	{
	    result = s;
292 293 294
	    /* we'll miss the opportunity to reduce the correct size
	     * of the allocated memory for the string after that.
	     */
295 296 297 298 299 300 301 302 303 304 305 306 307
	    s = NULL;
	    goto bail1;
	}
    }
    if (modifier)
    {
	if (FcDebug () & FC_DBG_LANGSET)
	    printf("Checking the existence of %s.orth\n", s);
	if (FcLangSetIndex (s) < 0)
	    modifier[-1] = 0;
	else
	{
	    result = s;
308 309 310
	    /* we'll miss the opportunity to reduce the correct size
	     * of the allocated memory for the string after that.
	     */
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
	    s = NULL;
	    goto bail1;
	}
    }
    if (FcDebug () & FC_DBG_LANGSET)
	printf("Checking the existence of %s.orth\n", s);
    if (FcLangSetIndex (s) < 0)
    {
	/* there seems no languages matched in orth.
	 * add the language as is for fallback.
	 */
	result = orig;
	orig = NULL;
    }
    else
    {
	result = s;
328 329 330
	/* we'll miss the opportunity to reduce the correct size
	 * of the allocated memory for the string after that.
	 */
331 332 333 334
	s = NULL;
    }
  bail1:
    if (orig)
335
	FcStrFree (orig);
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
  bail0:
    if (s)
	free (s);
  bail:
    if (FcDebug () & FC_DBG_LANGSET)
    {
	if (result)
	    printf ("normalized: %s -> %s\n", lang, result);
	else
	    printf ("Unable to normalize %s\n", lang);
    }

    return result;
}

351
#define FcLangEnd(c)	((c) == '-' || (c) == '\0')
352 353 354 355 356

FcLangResult
FcLangCompare (const FcChar8 *s1, const FcChar8 *s2)
{
    FcChar8	    c1, c2;
357
    FcLangResult    result = FcLangDifferentLang;
Behdad Esfahbod's avatar
Behdad Esfahbod committed
358
    const FcChar8  *s1_orig = s1;
359 360 361 362 363 364
    FcBool	    is_und;

    is_und = FcToLower (s1[0]) == 'u' &&
	     FcToLower (s1[1]) == 'n' &&
	     FcToLower (s1[2]) == 'd' &&
	     FcLangEnd (s1[3]);
365

366 367 368 369
    for (;;)
    {
	c1 = *s1++;
	c2 = *s2++;
370
	
371 372 373
	c1 = FcToLower (c1);
	c2 = FcToLower (c2);
	if (c1 != c2)
374
	{
375
	    if (!is_und && FcLangEnd (c1) && FcLangEnd (c2))
376
		result = FcLangDifferentTerritory;
377 378 379
	    return result;
	}
	else if (!c1)
380 381 382
	{
	    return is_und ? result : FcLangEqual;
	}
383
	else if (c1 == '-')
384 385 386 387 388 389 390 391 392
	{
	    if (!is_und)
		result = FcLangDifferentTerritory;
	}

	/* If we parsed past "und-", then do not consider it undefined anymore,
	 * as there's *something* specified. */
	if (is_und && s1 - s1_orig == 4)
	    is_und = FcFalse;
393 394 395
    }
}

396
/*
397
 * Return FcTrue when super contains sub.
398
 *
399 400 401
 * super contains sub if super and sub have the same
 * language and either the same country or one
 * is missing the country
402 403 404
 */

static FcBool
405
FcLangContains (const FcChar8 *super, const FcChar8 *sub)
406 407 408 409 410
{
    FcChar8	    c1, c2;

    for (;;)
    {
411 412
	c1 = *super++;
	c2 = *sub++;
413 414 415 416 417
	
	c1 = FcToLower (c1);
	c2 = FcToLower (c2);
	if (c1 != c2)
	{
418
	    /* see if super has a country while sub is mising one */
419 420
	    if (c1 == '-' && c2 == '\0')
		return FcTrue;
421 422 423
	    /* see if sub has a country while super is mising one */
	    if (c1 == '\0' && c2 == '-')
		return FcTrue;
424 425 426 427 428 429 430
	    return FcFalse;
	}
	else if (!c1)
	    return FcTrue;
    }
}

431
const FcCharSet *
432
FcLangGetCharSet (const FcChar8 *lang)
433 434 435
{
    int		i;
    int		country = -1;
436

437 438 439 440 441
    for (i = 0; i < NUM_LANG_CHAR_SET; i++)
    {
	switch (FcLangCompare (lang, fcLangCharSets[i].lang)) {
	case FcLangEqual:
	    return &fcLangCharSets[i].charset;
442
	case FcLangDifferentTerritory:
443 444
	    if (country == -1)
		country = i;
445
	case FcLangDifferentLang:
446 447 448 449 450 451
	default:
	    break;
	}
    }
    if (country == -1)
	return 0;
452
    return &fcLangCharSets[country].charset;
453
}
454

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
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;
}

471 472 473 474 475 476 477 478 479
FcLangSet *
FcLangSetCreate (void)
{
    FcLangSet	*ls;

    ls = malloc (sizeof (FcLangSet));
    if (!ls)
	return 0;
    memset (ls->map, '\0', sizeof (ls->map));
480
    ls->map_size = NUM_LANG_SET_MAP;
481
    ls->extra = 0;
482 483 484 485 486 487
    return ls;
}

void
FcLangSetDestroy (FcLangSet *ls)
{
488 489 490
    if (!ls)
	return;

491 492
    if (ls->extra)
	FcStrSetDestroy (ls->extra);
493 494 495 496 497 498 499 500
    free (ls);
}

FcLangSet *
FcLangSetCopy (const FcLangSet *ls)
{
    FcLangSet	*new;

501 502 503
    if (!ls)
	return NULL;

504 505 506
    new = FcLangSetCreate ();
    if (!new)
	goto bail0;
507 508
    memset (new->map, '\0', sizeof (new->map));
    memcpy (new->map, ls->map, FC_MIN (sizeof (new->map), ls->map_size * sizeof (ls->map[0])));
509
    if (ls->extra)
510 511 512 513
    {
	FcStrList	*list;
	FcChar8		*extra;
	
514 515
	new->extra = FcStrSetCreate ();
	if (!new->extra)
516 517
	    goto bail1;

518
	list = FcStrListCreate (ls->extra);	
519 520 521 522
	if (!list)
	    goto bail1;
	
	while ((extra = FcStrListNext (list)))
523
	    if (!FcStrSetAdd (new->extra, extra))
524 525 526 527 528 529 530 531 532 533 534 535 536
	    {
		FcStrListDone (list);
		goto bail1;
	    }
	FcStrListDone (list);
    }
    return new;
bail1:
    FcLangSetDestroy (new);
bail0:
    return 0;
}

537 538 539 540 541 542 543 544 545
/* When the language isn't found, the return value r is such that:
 *  1) r < 0
 *  2) -r -1 is the index of the first language in fcLangCharSets that comes
 *     after the 'lang' argument in lexicographic order.
 *
 *  The -1 is necessary to avoid problems with language id 0 (otherwise, we
 *  wouldn't be able to distinguish between “language found, id is 0” and
 *  “language not found, sorts right before the language with id 0”).
 */
546 547 548
static int
FcLangSetIndex (const FcChar8 *lang)
{
549 550
    int	    low, high, mid = 0;
    int	    cmp = 0;
551
    FcChar8 firstChar = FcToLower(lang[0]);
552
    FcChar8 secondChar = firstChar ? FcToLower(lang[1]) : '\0';
553

554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    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)
570
	    return -(low+1); /* one past next entry after where it would be */
571
    }
572 573 574 575

    while (low <= high)
    {
	mid = (high + low) >> 1;
576 577 578 579 580
	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) */
581
	    cmp = fcLangCharSets[mid].lang[1] - secondChar;
582 583
	    if (cmp == 0 &&
		(fcLangCharSets[mid].lang[2] != '\0' ||
584
		 lang[2] != '\0'))
585
	    {
586
		cmp = FcStrCmpIgnoreCase(fcLangCharSets[mid].lang+2,
587
					 lang+2);
588 589 590
	    }
	}
	if (cmp == 0)
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
	    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;
    }
613
    if (!ls->extra)
614
    {
615 616
	ls->extra = FcStrSetCreate ();
	if (!ls->extra)
617 618
	    return FcFalse;
    }
619
    return FcStrSetAdd (ls->extra, lang);
620 621
}

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
FcBool
FcLangSetDel (FcLangSet *ls, const FcChar8 *lang)
{
    int	id;

    id = FcLangSetIndex (lang);
    if (id >= 0)
    {
	FcLangSetBitReset (ls, id);
    }
    else if (ls->extra)
    {
	FcStrSetDel (ls->extra, lang);
    }
    return FcTrue;
}

639 640 641 642 643 644 645 646
FcLangResult
FcLangSetHasLang (const FcLangSet *ls, const FcChar8 *lang)
{
    int		    id;
    FcLangResult    best, r;
    int		    i;

    id = FcLangSetIndex (lang);
647 648 649
    if (id < 0)
	id = -id - 1;
    else if (FcLangSetBitGet (ls, id))
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
	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;
    }
668
    if (ls->extra)
669
    {
670
	FcStrList	*list = FcStrListCreate (ls->extra);
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
	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)
{
710
    int		    i, j, count;
711
    FcLangResult    best, r;
712
    FcChar32 aInCountrySet, bInCountrySet;
713

714 715 716
    count = FC_MIN (lsa->map_size, lsb->map_size);
    count = FC_MIN (NUM_LANG_SET_MAP, count);
    for (i = 0; i < count; i++)
717 718 719
	if (lsa->map[i] & lsb->map[i])
	    return FcLangEqual;
    best = FcLangDifferentLang;
720
    for (j = 0; j < NUM_COUNTRY_SET; j++)
721 722 723 724
    {
	aInCountrySet = 0;
	bInCountrySet = 0;

725
	for (i = 0; i < count; i++)
726 727 728 729 730
	{
	    aInCountrySet |= lsa->map[i] & fcLangCountrySets[j][i];
	    bInCountrySet |= lsb->map[i] & fcLangCountrySets[j][i];

	    if (aInCountrySet && bInCountrySet)
731
	    {
732
		best = FcLangDifferentTerritory;
733 734
		break;
	    }
735 736
	}
    }
737
    if (lsa->extra)
738
    {
739
	r = FcLangSetCompareStrSet (lsb, lsa->extra);
740 741 742
	if (r < best)
	    best = r;
    }
743
    if (best > FcLangEqual && lsb->extra)
744
    {
745
	r = FcLangSetCompareStrSet (lsa, lsb->extra);
746 747 748 749 750 751 752 753 754 755
	if (r < best)
	    best = r;
    }
    return best;
}

/*
 * Used in computing values -- mustn't allocate any storage
 */
FcLangSet *
756
FcLangSetPromote (const FcChar8 *lang, FcValuePromotionBuffer *vbuf)
757
{
758 759 760 761 762 763 764 765 766 767 768 769 770
    int		id;
    typedef struct {
	FcLangSet  ls;
	FcStrSet   strs;
	FcChar8   *str;
    } FcLangSetPromotionBuffer;
    FcLangSetPromotionBuffer *buf = (FcLangSetPromotionBuffer *) vbuf;

    FC_ASSERT_STATIC (sizeof (FcLangSetPromotionBuffer) <= sizeof (FcValuePromotionBuffer));

    memset (buf->ls.map, '\0', sizeof (buf->ls.map));
    buf->ls.map_size = NUM_LANG_SET_MAP;
    buf->ls.extra = 0;
771
    if (lang)
772
    {
773
	id = FcLangSetIndex (lang);
774
	if (id >= 0)
775 776 777 778 779 780 781 782 783 784 785 786
	{
	    FcLangSetBitSet (&buf->ls, id);
	}
	else
	{
	    buf->ls.extra = &buf->strs;
	    buf->strs.num = 1;
	    buf->strs.size = 1;
	    buf->strs.strs = &buf->str;
	    FcRefInit (&buf->strs.ref, 1);
	    buf->str = (FcChar8 *) lang;
	}
787
    }
788
    return &buf->ls;
789 790 791 792 793 794
}

FcChar32
FcLangSetHash (const FcLangSet *ls)
{
    FcChar32	h = 0;
795
    int		i, count;
796

797 798
    count = FC_MIN (ls->map_size, NUM_LANG_SET_MAP);
    for (i = 0; i < count; i++)
799
	h ^= ls->map[i];
800 801
    if (ls->extra)
	h ^= ls->extra->num;
802 803 804 805 806 807
    return h;
}

FcLangSet *
FcNameParseLangSet (const FcChar8 *string)
{
808
    FcChar8	    lang[32], c = 0;
809
    int i;
810 811 812 813 814 815
    FcLangSet	    *ls;

    ls = FcLangSetCreate ();
    if (!ls)
	goto bail0;

816
    for(;;)
817
    {
818
	for(i = 0; i < 31;i++)
819
	{
820 821 822 823
	    c = *string++;
	    if(c == '\0' || c == '|')
		break; /* end of this code */
	    lang[i] = c;
824
	}
825 826 827 828 829
	lang[i] = '\0';
	if (!FcLangSetAdd (ls, lang))
	    goto bail1;
	if(c == '\0')
	    break;
830 831 832 833 834 835 836 837 838 839 840
    }
    return ls;
bail1:
    FcLangSetDestroy (ls);
bail0:
    return 0;
}

FcBool
FcNameUnparseLangSet (FcStrBuf *buf, const FcLangSet *ls)
{
841
    int		i, bit, count;
842
    FcChar32	bits;
843 844
    FcBool	first = FcTrue;

845 846
    count = FC_MIN (ls->map_size, NUM_LANG_SET_MAP);
    for (i = 0; i < count; i++)
847 848 849 850 851 852 853 854 855 856 857
    {
	if ((bits = ls->map[i]))
	{
	    for (bit = 0; bit <= 31; bit++)
		if (bits & (1 << bit))
		{
		    int id = (i << 5) | bit;
		    if (!first)
			if (!FcStrBufChar (buf, '|'))
			    return FcFalse;
		    if (!FcStrBufString (buf, fcLangCharSets[fcLangCharSetIndicesInv[id]].lang))
858
			return FcFalse;
859 860 861 862
		    first = FcFalse;
		}
	}
    }
863
    if (ls->extra)
864
    {
865
	FcStrList   *list = FcStrListCreate (ls->extra);
866 867 868 869 870 871 872 873
	FcChar8	    *extra;

	if (!list)
	    return FcFalse;
	while ((extra = FcStrListNext (list)))
	{
	    if (!first)
		if (!FcStrBufChar (buf, '|'))
874 875
                {
                    FcStrListDone (list);
876
		    return FcFalse;
877
                }
878
	    if (!FcStrBufString (buf, extra))
879 880 881 882
                {
                    FcStrListDone (list);
                    return FcFalse;
                }
883 884
	    first = FcFalse;
	}
885
        FcStrListDone (list);
886 887 888 889 890 891 892
    }
    return FcTrue;
}

FcBool
FcLangSetEqual (const FcLangSet *lsa, const FcLangSet *lsb)
{
893
    int	    i, count;
894

895 896 897
    count = FC_MIN (lsa->map_size, lsb->map_size);
    count = FC_MIN (NUM_LANG_SET_MAP, count);
    for (i = 0; i < count; i++)
898 899 900 901
    {
	if (lsa->map[i] != lsb->map[i])
	    return FcFalse;
    }
902
    if (!lsa->extra && !lsb->extra)
903
	return FcTrue;
904 905
    if (lsa->extra && lsb->extra)
	return FcStrSetEqual (lsa->extra, lsb->extra);
906 907
    return FcFalse;
}
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938

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;
    }
939
    if (ls->extra)
940
    {
941
	FcStrList	*list = FcStrListCreate (ls->extra);
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
	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)
{
965
    int		    i, j, count;
966 967 968 969 970 971 972 973 974 975 976
    FcChar32	    missing;

    if (FcDebug() & FC_DBG_MATCHV)
    {
	printf ("FcLangSet "); FcLangSetPrint (lsa);
	printf (" contains "); FcLangSetPrint (lsb);
	printf ("\n");
    }
    /*
     * check bitmaps for missing language support
     */
977 978 979
    count = FC_MIN (lsa->map_size, lsb->map_size);
    count = FC_MIN (NUM_LANG_SET_MAP, count);
    for (i = 0; i < count; i++)
980 981 982 983 984
    {
	missing = lsb->map[i] & ~lsa->map[i];
	if (missing)
	{
	    for (j = 0; j < 32; j++)
985
		if (missing & (1 << j))
986 987
		{
		    if (!FcLangSetContainsLang (lsa,
988
						fcLangCharSets[fcLangCharSetIndicesInv[i*32 + j]].lang))
989 990
		    {
			if (FcDebug() & FC_DBG_MATCHV)
991
			    printf ("\tMissing bitmap %s\n", fcLangCharSets[fcLangCharSetIndicesInv[i*32+j]].lang);
992 993 994 995 996
			return FcFalse;
		    }
		}
	}
    }
997
    if (lsb->extra)
998
    {
999
	FcStrList   *list = FcStrListCreate (lsb->extra);
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
	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;
}
1020

1021 1022
FcBool
FcLangSetSerializeAlloc (FcSerialize *serialize, const FcLangSet *l)
1023
{
1024 1025
    if (!FcSerializeAlloc (serialize, l, sizeof (FcLangSet)))
	return FcFalse;
1026
    return FcTrue;
1027
}
1028

1029
FcLangSet *
1030
FcLangSetSerialize(FcSerialize *serialize, const FcLangSet *l)
1031
{
1032
    FcLangSet	*l_serialize = FcSerializePtr (serialize, l);
1033

1034 1035
    if (!l_serialize)
	return NULL;
1036 1037 1038
    memset (l_serialize->map, '\0', sizeof (l_serialize->map));
    memcpy (l_serialize->map, l->map, FC_MIN (sizeof (l_serialize->map), l->map_size * sizeof (l->map[0])));
    l_serialize->map_size = NUM_LANG_SET_MAP;
1039
    l_serialize->extra = NULL; /* We don't serialize ls->extra */
1040
    return l_serialize;
1041
}
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073

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;
}

1074 1075 1076 1077 1078 1079 1080
static FcLangSet *
FcLangSetOperate(const FcLangSet	*a,
		 const FcLangSet	*b,
		 FcBool			(*func) (FcLangSet 	*ls,
						 const FcChar8	*s))
{
    FcLangSet	*langset = FcLangSetCopy (a);
Akira TAGOH's avatar
Akira TAGOH committed
1081 1082
    FcStrSet	*set = FcLangSetGetLangs (b);
    FcStrList	*sl = FcStrListCreate (set);
1083 1084
    FcChar8	*str;

Akira TAGOH's avatar
Akira TAGOH committed
1085
    FcStrSetDestroy (set);
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
    while ((str = FcStrListNext (sl)))
    {
	func (langset, str);
    }
    FcStrListDone (sl);

    return langset;
}

FcLangSet *
FcLangSetUnion (const FcLangSet *a, const FcLangSet *b)
{
    return FcLangSetOperate(a, b, FcLangSetAdd);
}

FcLangSet *
FcLangSetSubtract (const FcLangSet *a, const FcLangSet *b)
{
    return FcLangSetOperate(a, b, FcLangSetDel);
}

1107 1108
#define __fclang__
#include "fcaliastail.h"
1109
#include "fcftaliastail.h"
1110
#undef __fclang__