fcname.c 16.2 KB
Newer Older
Keith Packard's avatar
Keith Packard committed
1
/*
2
 * fontconfig/src/fcname.c
Keith Packard's avatar
Keith Packard committed
3
 *
4
 * Copyright © 2000 Keith Packard
Keith Packard's avatar
Keith Packard committed
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
Keith Packard's avatar
Keith Packard committed
11
 * advertising or publicity pertaining to distribution of the software without
12
 * specific, written prior permission.  The authors make no
Keith Packard's avatar
Keith Packard committed
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,
Keith Packard's avatar
Keith Packard committed
17
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18
 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
Keith Packard's avatar
Keith Packard committed
19 20 21 22 23 24
 * 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.
 */

25
#include "fcint.h"
Keith Packard's avatar
Keith Packard committed
26 27 28 29 30
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

31
static const FcObjectType FcObjects[] = {
32
#define FC_OBJECT(NAME, Type, Cmp) { FC_##NAME, Type },
33 34
#include "fcobjs.h"
#undef FC_OBJECT
Keith Packard's avatar
Keith Packard committed
35 36
};

37
#define NUM_OBJECT_TYPES ((int) (sizeof FcObjects / sizeof FcObjects[0]))
Keith Packard's avatar
Keith Packard committed
38

39
static const FcObjectType *
40 41
FcObjectFindById (FcObject object)
{
42 43 44
    if (1 <= object && object <= NUM_OBJECT_TYPES)
	return &FcObjects[object - 1];
    return FcObjectLookupOtherTypeById (object);
Keith Packard's avatar
Keith Packard committed
45 46
}

47 48 49
FcBool
FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
{
50 51
    /* Deprecated. */
    return FcFalse;
52
}
53

54 55 56
FcBool
FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
{
57 58
    /* Deprecated. */
    return FcFalse;
59
}
60

61 62 63
const FcObjectType *
FcNameGetObjectType (const char *object)
{
64 65 66 67 68 69
    int id = FcObjectLookupBuiltinIdByName (object);

    if (!id)
	return FcObjectLookupOtherTypeByName (object);

    return &FcObjects[id - 1];
70
}
71

72 73 74
FcBool
FcObjectValidType (FcObject object, FcType type)
{
75
    const FcObjectType    *t = FcObjectFindById (object);
76 77

    if (t) {
Behdad Esfahbod's avatar
Behdad Esfahbod committed
78
	switch ((int) t->type) {
79 80
	case FcTypeUnknown:
	    return FcTrue;
81 82 83 84 85
	case FcTypeDouble:
	case FcTypeInteger:
	    if (type == FcTypeDouble || type == FcTypeInteger)
		return FcTrue;
	    break;
86 87 88 89
	case FcTypeLangSet:
	    if (type == FcTypeLangSet || type == FcTypeString)
		return FcTrue;
	    break;
90
	case FcTypeRange:
91 92 93
	    if (type == FcTypeRange ||
		type == FcTypeDouble ||
		type == FcTypeInteger)
94 95
		return FcTrue;
	    break;
96
	default:
97
	    if (type == t->type)
98 99 100
		return FcTrue;
	    break;
	}
101
	return FcFalse;
102
    }
103 104 105 106 107
    return FcTrue;
}

FcObject
FcObjectFromName (const char * name)
108
{
109
    return FcObjectLookupIdByName (name);
110 111
}

112 113 114 115 116 117 118 119
FcObjectSet *
FcObjectGetSet (void)
{
    int		i;
    FcObjectSet	*os = NULL;


    os = FcObjectSetCreate ();
120
    for (i = 0; i < NUM_OBJECT_TYPES; i++)
121 122 123 124 125
	FcObjectSetAdd (os, FcObjects[i].object);

    return os;
}

126
const char *
127
FcObjectName (FcObject object)
128
{
129
    const FcObjectType   *o = FcObjectFindById (object);
130

131 132
    if (o)
	return o->object;
133 134

    return FcObjectLookupOtherNameById (object);
135 136
}

Keith Packard's avatar
Keith Packard committed
137
static const FcConstant _FcBaseConstants[] = {
138 139 140
    { (FcChar8 *) "thin",	    "weight",   FC_WEIGHT_THIN, },
    { (FcChar8 *) "extralight",	    "weight",   FC_WEIGHT_EXTRALIGHT, },
    { (FcChar8 *) "ultralight",	    "weight",   FC_WEIGHT_EXTRALIGHT, },
141 142
    { (FcChar8 *) "demilight",	    "weight",   FC_WEIGHT_DEMILIGHT, },
    { (FcChar8 *) "semilight",	    "weight",   FC_WEIGHT_DEMILIGHT, },
143
    { (FcChar8 *) "light",	    "weight",   FC_WEIGHT_LIGHT, },
144
    { (FcChar8 *) "book",	    "weight",	FC_WEIGHT_BOOK, },
145
    { (FcChar8 *) "regular",	    "weight",   FC_WEIGHT_REGULAR, },
146 147
    { (FcChar8 *) "medium",	    "weight",   FC_WEIGHT_MEDIUM, },
    { (FcChar8 *) "demibold",	    "weight",   FC_WEIGHT_DEMIBOLD, },
148
    { (FcChar8 *) "semibold",	    "weight",   FC_WEIGHT_DEMIBOLD, },
149
    { (FcChar8 *) "bold",	    "weight",   FC_WEIGHT_BOLD, },
150 151
    { (FcChar8 *) "extrabold",	    "weight",   FC_WEIGHT_EXTRABOLD, },
    { (FcChar8 *) "ultrabold",	    "weight",   FC_WEIGHT_EXTRABOLD, },
152
    { (FcChar8 *) "black",	    "weight",   FC_WEIGHT_BLACK, },
153
    { (FcChar8 *) "heavy",	    "weight",	FC_WEIGHT_HEAVY, },
154 155 156 157 158

    { (FcChar8 *) "roman",	    "slant",    FC_SLANT_ROMAN, },
    { (FcChar8 *) "italic",	    "slant",    FC_SLANT_ITALIC, },
    { (FcChar8 *) "oblique",	    "slant",    FC_SLANT_OBLIQUE, },

159 160 161
    { (FcChar8 *) "ultracondensed", "width",	FC_WIDTH_ULTRACONDENSED },
    { (FcChar8 *) "extracondensed", "width",	FC_WIDTH_EXTRACONDENSED },
    { (FcChar8 *) "condensed",	    "width",	FC_WIDTH_CONDENSED },
162
    { (FcChar8 *) "semicondensed",  "width",	FC_WIDTH_SEMICONDENSED },
163 164 165 166 167
    { (FcChar8 *) "normal",	    "width",	FC_WIDTH_NORMAL },
    { (FcChar8 *) "semiexpanded",   "width",	FC_WIDTH_SEMIEXPANDED },
    { (FcChar8 *) "expanded",	    "width",	FC_WIDTH_EXPANDED },
    { (FcChar8 *) "extraexpanded",  "width",	FC_WIDTH_EXTRAEXPANDED },
    { (FcChar8 *) "ultraexpanded",  "width",	FC_WIDTH_ULTRAEXPANDED },
168

169
    { (FcChar8 *) "proportional",   "spacing",  FC_PROPORTIONAL, },
170
    { (FcChar8 *) "dual",	    "spacing",  FC_DUAL, },
171 172 173
    { (FcChar8 *) "mono",	    "spacing",  FC_MONO, },
    { (FcChar8 *) "charcell",	    "spacing",  FC_CHARCELL, },

Keith Packard's avatar
Keith Packard committed
174
    { (FcChar8 *) "unknown",	    "rgba",	    FC_RGBA_UNKNOWN },
175 176 177 178
    { (FcChar8 *) "rgb",	    "rgba",	    FC_RGBA_RGB, },
    { (FcChar8 *) "bgr",	    "rgba",	    FC_RGBA_BGR, },
    { (FcChar8 *) "vrgb",	    "rgba",	    FC_RGBA_VRGB },
    { (FcChar8 *) "vbgr",	    "rgba",	    FC_RGBA_VBGR },
Keith Packard's avatar
Keith Packard committed
179
    { (FcChar8 *) "none",	    "rgba",	    FC_RGBA_NONE },
180 181 182 183 184

    { (FcChar8 *) "hintnone",	    "hintstyle",   FC_HINT_NONE },
    { (FcChar8 *) "hintslight",	    "hintstyle",   FC_HINT_SLIGHT },
    { (FcChar8 *) "hintmedium",	    "hintstyle",   FC_HINT_MEDIUM },
    { (FcChar8 *) "hintfull",	    "hintstyle",   FC_HINT_FULL },
185 186 187 188 189

    { (FcChar8 *) "antialias",	    "antialias",    FcTrue },
    { (FcChar8 *) "hinting",	    "hinting",	    FcTrue },
    { (FcChar8 *) "verticallayout", "verticallayout",	FcTrue },
    { (FcChar8 *) "autohint",	    "autohint",	    FcTrue },
190
    { (FcChar8 *) "globaladvance",  "globaladvance",	FcTrue }, /* deprecated */
191 192 193 194 195 196
    { (FcChar8 *) "outline",	    "outline",	    FcTrue },
    { (FcChar8 *) "scalable",	    "scalable",	    FcTrue },
    { (FcChar8 *) "minspace",	    "minspace",	    FcTrue },
    { (FcChar8 *) "embolden",	    "embolden",	    FcTrue },
    { (FcChar8 *) "embeddedbitmap", "embeddedbitmap",	FcTrue },
    { (FcChar8 *) "decorative",	    "decorative",   FcTrue },
197 198 199 200
    { (FcChar8 *) "lcdnone",	    "lcdfilter",    FC_LCD_NONE },
    { (FcChar8 *) "lcddefault",	    "lcdfilter",    FC_LCD_DEFAULT },
    { (FcChar8 *) "lcdlight",	    "lcdfilter",    FC_LCD_LIGHT },
    { (FcChar8 *) "lcdlegacy",	    "lcdfilter",    FC_LCD_LEGACY },
Keith Packard's avatar
Keith Packard committed
201 202 203 204 205 206 207
};

#define NUM_FC_CONSTANTS   (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])

FcBool
FcNameRegisterConstants (const FcConstant *consts, int nconsts)
{
208 209
    /* Deprecated. */
    return FcFalse;
Keith Packard's avatar
Keith Packard committed
210 211 212 213 214
}

FcBool
FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
{
215
    /* Deprecated. */
Keith Packard's avatar
Keith Packard committed
216 217 218 219
    return FcFalse;
}

const FcConstant *
220
FcNameGetConstant (const FcChar8 *string)
Keith Packard's avatar
Keith Packard committed
221
{
222
    unsigned int	    i;
223 224 225 226

    for (i = 0; i < NUM_FC_CONSTANTS; i++)
	if (!FcStrCmpIgnoreCase (string, _FcBaseConstants[i].name))
	    return &_FcBaseConstants[i];
227

Keith Packard's avatar
Keith Packard committed
228 229 230 231
    return 0;
}

FcBool
232
FcNameConstant (const FcChar8 *string, int *result)
Keith Packard's avatar
Keith Packard committed
233 234 235 236 237 238 239 240 241 242 243 244
{
    const FcConstant	*c;

    if ((c = FcNameGetConstant(string)))
    {
	*result = c->value;
	return FcTrue;
    }
    return FcFalse;
}

FcBool
245
FcNameBool (const FcChar8 *v, FcBool *result)
Keith Packard's avatar
Keith Packard committed
246 247 248 249
{
    char    c0, c1;

    c0 = *v;
250
    c0 = FcToLower (c0);
Keith Packard's avatar
Keith Packard committed
251 252 253 254 255 256 257 258 259 260
    if (c0 == 't' || c0 == 'y' || c0 == '1')
    {
	*result = FcTrue;
	return FcTrue;
    }
    if (c0 == 'f' || c0 == 'n' || c0 == '0')
    {
	*result = FcFalse;
	return FcTrue;
    }
261 262 263 264 265
    if (c0 == 'd' || c0 == 'x' || c0 == '2')
    {
	*result = FcDontCare;
	return FcTrue;
    }
Keith Packard's avatar
Keith Packard committed
266 267 268
    if (c0 == 'o')
    {
	c1 = v[1];
269
	c1 = FcToLower (c1);
Keith Packard's avatar
Keith Packard committed
270 271 272 273 274 275 276 277 278 279
	if (c1 == 'n')
	{
	    *result = FcTrue;
	    return FcTrue;
	}
	if (c1 == 'f')
	{
	    *result = FcFalse;
	    return FcTrue;
	}
280 281 282 283 284
	if (c1 == 'r')
	{
	    *result = FcDontCare;
	    return FcTrue;
	}
Keith Packard's avatar
Keith Packard committed
285 286 287 288 289
    }
    return FcFalse;
}

static FcValue
290
FcNameConvert (FcType type, FcChar8 *string)
Keith Packard's avatar
Keith Packard committed
291 292
{
    FcValue	v;
293
    FcMatrix	m;
294 295
    double	b, e;
    char	*p;
Keith Packard's avatar
Keith Packard committed
296 297

    v.type = type;
Behdad Esfahbod's avatar
Behdad Esfahbod committed
298
    switch ((int) v.type) {
Keith Packard's avatar
Keith Packard committed
299 300
    case FcTypeInteger:
	if (!FcNameConstant (string, &v.u.i))
301
	    v.u.i = atoi ((char *) string);
Keith Packard's avatar
Keith Packard committed
302 303
	break;
    case FcTypeString:
Behdad Esfahbod's avatar
Behdad Esfahbod committed
304
	v.u.s = FcStrdup (string);
305 306
	if (!v.u.s)
	    v.type = FcTypeVoid;
Keith Packard's avatar
Keith Packard committed
307 308 309 310 311 312
	break;
    case FcTypeBool:
	if (!FcNameBool (string, &v.u.b))
	    v.u.b = FcFalse;
	break;
    case FcTypeDouble:
313
	v.u.d = strtod ((char *) string, 0);
Keith Packard's avatar
Keith Packard committed
314 315
	break;
    case FcTypeMatrix:
Behdad Esfahbod's avatar
Behdad Esfahbod committed
316
	FcMatrixInit (&m);
317 318
	sscanf ((char *) string, "%lg %lg %lg %lg", &m.xx, &m.xy, &m.yx, &m.yy);
	v.u.m = FcMatrixCopy (&m);
Keith Packard's avatar
Keith Packard committed
319 320
	break;
    case FcTypeCharSet:
321
	v.u.c = FcNameParseCharSet (string);
322 323
	if (!v.u.c)
	    v.type = FcTypeVoid;
Keith Packard's avatar
Keith Packard committed
324
	break;
325
    case FcTypeLangSet:
326
	v.u.l = FcNameParseLangSet (string);
327 328
	if (!v.u.l)
	    v.type = FcTypeVoid;
329
	break;
330
    case FcTypeRange:
331
	if (sscanf ((char *) string, "[%lg %lg]", &b, &e) != 2)
332
	{
333 334 335 336
	    char *sc, *ec;
	    size_t len = strlen ((const char *) string);
	    int si, ei;

337 338
	    sc = malloc (len + 1);
	    ec = malloc (len + 1);
339 340 341 342 343 344 345 346 347
	    if (sc && ec && sscanf ((char *) string, "[%s %[^]]]", sc, ec) == 2)
	    {
		if (FcNameConstant ((const FcChar8 *) sc, &si) &&
		    FcNameConstant ((const FcChar8 *) ec, &ei))
		    v.u.r =  FcRangeCreateDouble (si, ei);
		else
		    goto bail1;
	    }
	    else
348
	    {
349 350 351 352 353 354 355 356 357 358
	    bail1:
		v.type = FcTypeDouble;
		if (FcNameConstant (string, &si))
		{
		    v.u.d = (double) si;
		} else {
		    v.u.d = strtod ((char *) string, &p);
		    if (p != NULL && p[0] != 0)
			v.type = FcTypeVoid;
		}
359
	    }
360 361 362 363
	    if (sc)
		free (sc);
	    if (ec)
		free (ec);
364 365 366 367
	}
	else
	    v.u.r = FcRangeCreateDouble (b, e);
	break;
Keith Packard's avatar
Keith Packard committed
368 369 370 371 372 373
    default:
	break;
    }
    return v;
}

374 375
static const FcChar8 *
FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
Keith Packard's avatar
Keith Packard committed
376
{
377
    FcChar8    c;
378

379 380 381 382 383 384
    while ((c = *cur))
    {
	if (!isspace (c))
	    break;
	++cur;
    }
Keith Packard's avatar
Keith Packard committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    while ((c = *cur))
    {
	if (c == '\\')
	{
	    ++cur;
	    if (!(c = *cur))
		break;
	}
	else if (strchr (delim, c))
	    break;
	++cur;
	*save++ = c;
    }
    *save = 0;
    *last = *cur;
    if (*cur)
	cur++;
    return cur;
}

FcPattern *
406
FcNameParse (const FcChar8 *name)
Keith Packard's avatar
Keith Packard committed
407
{
408
    FcChar8		*save;
Keith Packard's avatar
Keith Packard committed
409 410
    FcPattern		*pat;
    double		d;
411 412
    FcChar8		*e;
    FcChar8		delim;
Keith Packard's avatar
Keith Packard committed
413 414 415 416
    FcValue		v;
    const FcObjectType	*t;
    const FcConstant	*c;

417
    /* freed below */
418
    save = malloc (strlen ((char *) name) + 1);
Keith Packard's avatar
Keith Packard committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
    if (!save)
	goto bail0;
    pat = FcPatternCreate ();
    if (!pat)
	goto bail1;

    for (;;)
    {
	name = FcNameFindNext (name, "-,:", save, &delim);
	if (save[0])
	{
	    if (!FcPatternAddString (pat, FC_FAMILY, save))
		goto bail2;
	}
	if (delim != ',')
	    break;
    }
    if (delim == '-')
    {
	for (;;)
	{
	    name = FcNameFindNext (name, "-,:", save, &delim);
441
	    d = strtod ((char *) save, (char **) &e);
Keith Packard's avatar
Keith Packard committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
	    if (e != save)
	    {
		if (!FcPatternAddDouble (pat, FC_SIZE, d))
		    goto bail2;
	    }
	    if (delim != ',')
		break;
	}
    }
    while (delim == ':')
    {
	name = FcNameFindNext (name, "=_:", save, &delim);
	if (save[0])
	{
	    if (delim == '=' || delim == '_')
	    {
458
		t = FcNameGetObjectType ((char *) save);
Keith Packard's avatar
Keith Packard committed
459 460 461
		for (;;)
		{
		    name = FcNameFindNext (name, ":,", save, &delim);
462
		    if (t)
Keith Packard's avatar
Keith Packard committed
463
		    {
464
			v = FcNameConvert (t->type, save);
Keith Packard's avatar
Keith Packard committed
465 466
			if (!FcPatternAdd (pat, t->object, v, FcTrue))
			{
467
			    FcValueDestroy (v);
Keith Packard's avatar
Keith Packard committed
468 469
			    goto bail2;
			}
470
			FcValueDestroy (v);
Keith Packard's avatar
Keith Packard committed
471 472 473 474 475 476 477 478 479
		    }
		    if (delim != ',')
			break;
		}
	    }
	    else
	    {
		if ((c = FcNameGetConstant (save)))
		{
480
		    t = FcNameGetObjectType ((char *) c->object);
481 482
		    if (t == NULL)
			goto bail2;
Behdad Esfahbod's avatar
Behdad Esfahbod committed
483
		    switch ((int) t->type) {
484 485 486 487 488 489 490 491 492
		    case FcTypeInteger:
		    case FcTypeDouble:
			if (!FcPatternAddInteger (pat, c->object, c->value))
			    goto bail2;
			break;
		    case FcTypeBool:
			if (!FcPatternAddBool (pat, c->object, c->value))
			    goto bail2;
			break;
493 494 495 496
		    case FcTypeRange:
			if (!FcPatternAddInteger (pat, c->object, c->value))
			    goto bail2;
			break;
497 498 499
		    default:
			break;
		    }
Keith Packard's avatar
Keith Packard committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
		}
	    }
	}
    }

    free (save);
    return pat;

bail2:
    FcPatternDestroy (pat);
bail1:
    free (save);
bail0:
    return 0;
}
static FcBool
516
FcNameUnparseString (FcStrBuf	    *buf,
Keith Packard's avatar
Keith Packard committed
517 518 519 520 521 522 523 524
		     const FcChar8  *string,
		     const FcChar8  *escape)
{
    FcChar8 c;
    while ((c = *string++))
    {
	if (escape && strchr ((char *) escape, (char) c))
	{
525
	    if (!FcStrBufChar (buf, escape[0]))
Keith Packard's avatar
Keith Packard committed
526 527
		return FcFalse;
	}
528
	if (!FcStrBufChar (buf, c))
Keith Packard's avatar
Keith Packard committed
529 530 531 532 533
	    return FcFalse;
    }
    return FcTrue;
}

534
FcBool
535
FcNameUnparseValue (FcStrBuf	*buf,
536
		    FcValue	*v0,
Keith Packard's avatar
Keith Packard committed
537 538 539
		    FcChar8	*escape)
{
    FcChar8	temp[1024];
540
    FcValue v = FcValueCanonicalize(v0);
541

Keith Packard's avatar
Keith Packard committed
542
    switch (v.type) {
543
    case FcTypeUnknown:
Keith Packard's avatar
Keith Packard committed
544 545 546 547 548 549 550 551 552
    case FcTypeVoid:
	return FcTrue;
    case FcTypeInteger:
	sprintf ((char *) temp, "%d", v.u.i);
	return FcNameUnparseString (buf, temp, 0);
    case FcTypeDouble:
	sprintf ((char *) temp, "%g", v.u.d);
	return FcNameUnparseString (buf, temp, 0);
    case FcTypeString:
553
	return FcNameUnparseString (buf, v.u.s, escape);
Keith Packard's avatar
Keith Packard committed
554
    case FcTypeBool:
555 556 557 558
	return FcNameUnparseString (buf,
				    v.u.b == FcTrue  ? (FcChar8 *) "True" :
				    v.u.b == FcFalse ? (FcChar8 *) "False" :
				                       (FcChar8 *) "DontCare", 0);
Keith Packard's avatar
Keith Packard committed
559
    case FcTypeMatrix:
560
	sprintf ((char *) temp, "%g %g %g %g",
561
		 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
Keith Packard's avatar
Keith Packard committed
562 563
	return FcNameUnparseString (buf, temp, 0);
    case FcTypeCharSet:
564
	return FcNameUnparseCharSet (buf, v.u.c);
565
    case FcTypeLangSet:
566
	return FcNameUnparseLangSet (buf, v.u.l);
567 568
    case FcTypeFTFace:
	return FcTrue;
569
    case FcTypeRange:
570
	sprintf ((char *) temp, "[%g %g]", v.u.r->begin, v.u.r->end);
Behdad Esfahbod's avatar
Behdad Esfahbod committed
571
	return FcNameUnparseString (buf, temp, 0);
Keith Packard's avatar
Keith Packard committed
572 573 574 575
    }
    return FcFalse;
}

576
FcBool
577
FcNameUnparseValueList (FcStrBuf	*buf,
578
			FcValueListPtr	v,
579
			FcChar8		*escape)
Keith Packard's avatar
Keith Packard committed
580
{
581
    while (v)
Keith Packard's avatar
Keith Packard committed
582
    {
583
	if (!FcNameUnparseValue (buf, &v->value, escape))
Keith Packard's avatar
Keith Packard committed
584
	    return FcFalse;
585
	if ((v = FcValueListNext(v)) != NULL)
586
	    if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
Keith Packard's avatar
Keith Packard committed
587 588 589 590 591 592 593 594 595 596
		return FcFalse;
    }
    return FcTrue;
}

#define FC_ESCAPE_FIXED    "\\-:,"
#define FC_ESCAPE_VARIABLE "\\=_:,"

FcChar8 *
FcNameUnparse (FcPattern *pat)
597 598 599 600 601 602
{
    return FcNameUnparseEscaped (pat, FcTrue);
}

FcChar8 *
FcNameUnparseEscaped (FcPattern *pat, FcBool escape)
Keith Packard's avatar
Keith Packard committed
603
{
604 605
    FcStrBuf		    buf, buf2;
    FcChar8		    buf_static[8192], buf2_static[256];
Keith Packard's avatar
Keith Packard committed
606 607 608
    int			    i;
    FcPatternElt	    *e;

609
    FcStrBufInit (&buf, buf_static, sizeof (buf_static));
610
    FcStrBufInit (&buf2, buf2_static, sizeof (buf2_static));
611
    e = FcPatternObjectFindElt (pat, FC_FAMILY_OBJECT);
Keith Packard's avatar
Keith Packard committed
612 613
    if (e)
    {
614
        if (!FcNameUnparseValueList (&buf, FcPatternEltValues(e), escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
Keith Packard's avatar
Keith Packard committed
615 616
	    goto bail0;
    }
617
    e = FcPatternObjectFindElt (pat, FC_SIZE_OBJECT);
Keith Packard's avatar
Keith Packard committed
618 619
    if (e)
    {
620 621 622
	FcChar8 *p;

	if (!FcNameUnparseString (&buf2, (FcChar8 *) "-", 0))
Keith Packard's avatar
Keith Packard committed
623
	    goto bail0;
624
	if (!FcNameUnparseValueList (&buf2, FcPatternEltValues(e), escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
Keith Packard's avatar
Keith Packard committed
625
	    goto bail0;
626 627 628 629 630
	p = FcStrBufDoneStatic (&buf2);
	FcStrBufDestroy (&buf2);
	if (strlen ((const char *)p) > 1)
	    if (!FcStrBufString (&buf, p))
		goto bail0;
Keith Packard's avatar
Keith Packard committed
631
    }
632
    for (i = 0; i < NUM_OBJECT_TYPES; i++)
Keith Packard's avatar
Keith Packard committed
633
    {
634 635 636 637 638 639 640 641 642
	FcObject id = i + 1;
	const FcObjectType	    *o;
	o = &FcObjects[i];
	if (!strcmp (o->object, FC_FAMILY) ||
	    !strcmp (o->object, FC_SIZE))
	    continue;
    
	e = FcPatternObjectFindElt (pat, id);
	if (e)
Keith Packard's avatar
Keith Packard committed
643
	{
644 645 646 647 648 649 650 651 652
	    if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
		goto bail0;
	    if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, escape ? (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
		goto bail0;
	    if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
		goto bail0;
	    if (!FcNameUnparseValueList (&buf, FcPatternEltValues(e), escape ?
					 (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
		goto bail0;
Keith Packard's avatar
Keith Packard committed
653 654
	}
    }
655
    return FcStrBufDone (&buf);
Keith Packard's avatar
Keith Packard committed
656
bail0:
657
    FcStrBufDestroy (&buf);
Keith Packard's avatar
Keith Packard committed
658 659
    return 0;
}
660 661 662
#define __fcname__
#include "fcaliastail.h"
#undef __fcname__