| 1 |
/* |
| 2 |
* Copyright © 2000 Keith Packard |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, distribute, and sell this software and its |
| 5 |
* documentation for any purpose is hereby granted without fee, provided that |
| 6 |
* the above copyright notice appear in all copies and that both that |
| 7 |
* copyright notice and this permission notice appear in supporting |
| 8 |
* documentation, and that the name of the author(s) not be used in |
| 9 |
* advertising or publicity pertaining to distribution of the software without |
| 10 |
* specific, written prior permission. The authors make no |
| 11 |
* representations about the suitability of this software for any purpose. It |
| 12 |
* is provided "as is" without express or implied warranty. |
| 13 |
* |
| 14 |
* THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 15 |
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 16 |
* EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 17 |
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 18 |
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 19 |
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 20 |
* PERFORMANCE OF THIS SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "fcint.h" |
| 24 |
|
| 25 |
#include "fcftint.h" |
| 26 |
|
| 27 |
/* Objects MT-safe for readonly access. */ |
| 28 |
|
| 29 |
FcPattern * |
| 30 |
FcPatternCreate (void) |
| 31 |
{ |
| 32 |
FcPattern *p; |
| 33 |
|
| 34 |
p = (FcPattern *)malloc (sizeof (FcPattern)); |
| 35 |
if (!p) |
| 36 |
return 0; |
| 37 |
memset (p, 0, sizeof (FcPattern)); |
| 38 |
p->num = 0; |
| 39 |
p->size = 0; |
| 40 |
p->elts_offset = FcPtrToOffset (p, NULL); |
| 41 |
FcRefInit (&p->ref, 1); |
| 42 |
return p; |
| 43 |
} |
| 44 |
|
| 45 |
void |
| 46 |
FcValueDestroy (FcValue v) |
| 47 |
{ |
| 48 |
switch ((int)v.type) { |
| 49 |
case FcTypeString: |
| 50 |
FcFree (v.u.s); |
| 51 |
break; |
| 52 |
case FcTypeMatrix: |
| 53 |
FcMatrixFree ((FcMatrix *)v.u.m); |
| 54 |
break; |
| 55 |
case FcTypeCharSet: |
| 56 |
FcCharSetDestroy ((FcCharSet *)v.u.c); |
| 57 |
break; |
| 58 |
case FcTypeLangSet: |
| 59 |
FcLangSetDestroy ((FcLangSet *)v.u.l); |
| 60 |
break; |
| 61 |
case FcTypeRange: |
| 62 |
FcRangeDestroy ((FcRange *)v.u.r); |
| 63 |
break; |
| 64 |
default: |
| 65 |
break; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
FcValue |
| 70 |
FcValueCanonicalize (const FcValue *v) |
| 71 |
{ |
| 72 |
FcValue newp; |
| 73 |
|
| 74 |
switch ((int)v->type) { |
| 75 |
case FcTypeString: |
| 76 |
newp.u.s = FcValueString (v); |
| 77 |
newp.type = FcTypeString; |
| 78 |
break; |
| 79 |
case FcTypeCharSet: |
| 80 |
newp.u.c = FcValueCharSet (v); |
| 81 |
newp.type = FcTypeCharSet; |
| 82 |
break; |
| 83 |
case FcTypeLangSet: |
| 84 |
newp.u.l = FcValueLangSet (v); |
| 85 |
newp.type = FcTypeLangSet; |
| 86 |
break; |
| 87 |
case FcTypeRange: |
| 88 |
newp.u.r = FcValueRange (v); |
| 89 |
newp.type = FcTypeRange; |
| 90 |
break; |
| 91 |
default: |
| 92 |
newp = *v; |
| 93 |
break; |
| 94 |
} |
| 95 |
return newp; |
| 96 |
} |
| 97 |
|
| 98 |
FcValue |
| 99 |
FcValueSave (FcValue v) |
| 100 |
{ |
| 101 |
switch ((int)v.type) { |
| 102 |
case FcTypeString: |
| 103 |
v.u.s = FcStrdup (v.u.s); |
| 104 |
if (!v.u.s) |
| 105 |
v.type = FcTypeVoid; |
| 106 |
break; |
| 107 |
case FcTypeMatrix: |
| 108 |
v.u.m = FcMatrixCopy (v.u.m); |
| 109 |
if (!v.u.m) |
| 110 |
v.type = FcTypeVoid; |
| 111 |
break; |
| 112 |
case FcTypeCharSet: |
| 113 |
v.u.c = FcCharSetCopy ((FcCharSet *)v.u.c); |
| 114 |
if (!v.u.c) |
| 115 |
v.type = FcTypeVoid; |
| 116 |
break; |
| 117 |
case FcTypeLangSet: |
| 118 |
v.u.l = FcLangSetCopy (v.u.l); |
| 119 |
if (!v.u.l) |
| 120 |
v.type = FcTypeVoid; |
| 121 |
break; |
| 122 |
case FcTypeRange: |
| 123 |
v.u.r = FcRangeCopy (v.u.r); |
| 124 |
if (!v.u.r) |
| 125 |
v.type = FcTypeVoid; |
| 126 |
break; |
| 127 |
default: |
| 128 |
break; |
| 129 |
} |
| 130 |
return v; |
| 131 |
} |
| 132 |
|
| 133 |
FcValueListPtr |
| 134 |
FcValueListCreate (void) |
| 135 |
{ |
| 136 |
return calloc (1, sizeof (FcValueList)); |
| 137 |
} |
| 138 |
|
| 139 |
void |
| 140 |
FcValueListDestroy (FcValueListPtr l) |
| 141 |
{ |
| 142 |
FcValueListPtr next; |
| 143 |
for (; l; l = next) { |
| 144 |
FcValueDestroy (l->value); |
| 145 |
next = FcValueListNext (l); |
| 146 |
free (l); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
FcValueListPtr |
| 151 |
FcValueListPrepend (FcValueListPtr vallist, |
| 152 |
FcValue value, |
| 153 |
FcValueBinding binding) |
| 154 |
{ |
| 155 |
FcValueListPtr newp; |
| 156 |
|
| 157 |
if (value.type == FcTypeVoid) |
| 158 |
return vallist; |
| 159 |
newp = FcValueListCreate(); |
| 160 |
if (!newp) |
| 161 |
return vallist; |
| 162 |
|
| 163 |
newp->value = FcValueSave (value); |
| 164 |
newp->binding = binding; |
| 165 |
newp->next = vallist; |
| 166 |
|
| 167 |
return newp; |
| 168 |
} |
| 169 |
|
| 170 |
FcValueListPtr |
| 171 |
FcValueListAppend (FcValueListPtr vallist, |
| 172 |
FcValue value, |
| 173 |
FcValueBinding binding) |
| 174 |
{ |
| 175 |
FcValueListPtr newp, last; |
| 176 |
|
| 177 |
if (value.type == FcTypeVoid) |
| 178 |
return vallist; |
| 179 |
newp = FcValueListCreate(); |
| 180 |
if (!newp) |
| 181 |
return vallist; |
| 182 |
|
| 183 |
newp->value = FcValueSave (value); |
| 184 |
newp->binding = binding; |
| 185 |
newp->next = NULL; |
| 186 |
|
| 187 |
if (vallist) { |
| 188 |
for (last = vallist; FcValueListNext (last); last = FcValueListNext (last)) |
| 189 |
; |
| 190 |
|
| 191 |
last->next = newp; |
| 192 |
} else |
| 193 |
vallist = newp; |
| 194 |
|
| 195 |
return vallist; |
| 196 |
} |
| 197 |
|
| 198 |
FcValueListPtr |
| 199 |
FcValueListDuplicate (FcValueListPtr orig) |
| 200 |
{ |
| 201 |
FcValueListPtr newp = NULL, l, t = NULL; |
| 202 |
FcValue v; |
| 203 |
|
| 204 |
for (l = orig; l != NULL; l = FcValueListNext (l)) { |
| 205 |
if (!newp) { |
| 206 |
t = newp = FcValueListCreate(); |
| 207 |
} else { |
| 208 |
t->next = FcValueListCreate(); |
| 209 |
t = FcValueListNext (t); |
| 210 |
} |
| 211 |
v = FcValueCanonicalize (&l->value); |
| 212 |
t->value = FcValueSave (v); |
| 213 |
t->binding = l->binding; |
| 214 |
t->next = NULL; |
| 215 |
} |
| 216 |
|
| 217 |
return newp; |
| 218 |
} |
| 219 |
|
| 220 |
FcBool |
| 221 |
FcValueEqual (FcValue va, FcValue vb) |
| 222 |
{ |
| 223 |
if (va.type != vb.type) { |
| 224 |
if (va.type == FcTypeInteger) { |
| 225 |
va.type = FcTypeDouble; |
| 226 |
va.u.d = va.u.i; |
| 227 |
} |
| 228 |
if (vb.type == FcTypeInteger) { |
| 229 |
vb.type = FcTypeDouble; |
| 230 |
vb.u.d = vb.u.i; |
| 231 |
} |
| 232 |
if (va.type != vb.type) |
| 233 |
return FcFalse; |
| 234 |
} |
| 235 |
switch (va.type) { |
| 236 |
case FcTypeUnknown: |
| 237 |
return FcFalse; /* don't know how to compare this object */ |
| 238 |
case FcTypeVoid: |
| 239 |
return FcTrue; |
| 240 |
case FcTypeInteger: |
| 241 |
return va.u.i == vb.u.i; |
| 242 |
case FcTypeDouble: |
| 243 |
return va.u.d == vb.u.d; |
| 244 |
case FcTypeString: |
| 245 |
return FcStrCmpIgnoreCase (va.u.s, vb.u.s) == 0; |
| 246 |
case FcTypeBool: |
| 247 |
return va.u.b == vb.u.b; |
| 248 |
case FcTypeMatrix: |
| 249 |
return FcMatrixEqual (va.u.m, vb.u.m); |
| 250 |
case FcTypeCharSet: |
| 251 |
return FcCharSetEqual (va.u.c, vb.u.c); |
| 252 |
case FcTypeFTFace: |
| 253 |
return va.u.f == vb.u.f; |
| 254 |
case FcTypeLangSet: |
| 255 |
return FcLangSetEqual (va.u.l, vb.u.l); |
| 256 |
case FcTypeRange: |
| 257 |
return FcRangeIsInRange (va.u.r, vb.u.r); |
| 258 |
} |
| 259 |
return FcFalse; |
| 260 |
} |
| 261 |
|
| 262 |
static FcChar32 |
| 263 |
FcDoubleHash (double d) |
| 264 |
{ |
| 265 |
if (d < 0) |
| 266 |
d = -d; |
| 267 |
if (d > 0xffffffff) |
| 268 |
d = 0xffffffff; |
| 269 |
return (FcChar32)d; |
| 270 |
} |
| 271 |
|
| 272 |
FcChar32 |
| 273 |
FcStringHash (const FcChar8 *s) |
| 274 |
{ |
| 275 |
FcChar8 c; |
| 276 |
FcChar32 h = 0; |
| 277 |
|
| 278 |
if (s) |
| 279 |
while ((c = *s++)) |
| 280 |
h = ((h << 1) | (h >> 31)) ^ c; |
| 281 |
return h; |
| 282 |
} |
| 283 |
|
| 284 |
static FcChar32 |
| 285 |
FcValueHash (const FcValue *v) |
| 286 |
{ |
| 287 |
switch (v->type) { |
| 288 |
case FcTypeUnknown: |
| 289 |
case FcTypeVoid: |
| 290 |
return 0; |
| 291 |
case FcTypeInteger: |
| 292 |
return (FcChar32)v->u.i; |
| 293 |
case FcTypeDouble: |
| 294 |
return FcDoubleHash (v->u.d); |
| 295 |
case FcTypeString: |
| 296 |
return FcStringHash (FcValueString (v)); |
| 297 |
case FcTypeBool: |
| 298 |
return (FcChar32)v->u.b; |
| 299 |
case FcTypeMatrix: |
| 300 |
return (FcDoubleHash (v->u.m->xx) ^ |
| 301 |
FcDoubleHash (v->u.m->xy) ^ |
| 302 |
FcDoubleHash (v->u.m->yx) ^ |
| 303 |
FcDoubleHash (v->u.m->yy)); |
| 304 |
case FcTypeCharSet: |
| 305 |
return (FcChar32)FcValueCharSet (v)->num; |
| 306 |
case FcTypeFTFace: |
| 307 |
return FcStringHash ((const FcChar8 *)((FT_Face)v->u.f)->family_name) ^ |
| 308 |
FcStringHash ((const FcChar8 *)((FT_Face)v->u.f)->style_name); |
| 309 |
case FcTypeLangSet: |
| 310 |
return FcLangSetHash (FcValueLangSet (v)); |
| 311 |
case FcTypeRange: |
| 312 |
return FcRangeHash (FcValueRange (v)); |
| 313 |
} |
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
static FcBool |
| 318 |
FcValueListEqual (FcValueListPtr la, FcValueListPtr lb) |
| 319 |
{ |
| 320 |
if (la == lb) |
| 321 |
return FcTrue; |
| 322 |
|
| 323 |
while (la && lb) { |
| 324 |
if (!FcValueEqual (la->value, lb->value)) |
| 325 |
return FcFalse; |
| 326 |
la = FcValueListNext (la); |
| 327 |
lb = FcValueListNext (lb); |
| 328 |
} |
| 329 |
if (la || lb) |
| 330 |
return FcFalse; |
| 331 |
return FcTrue; |
| 332 |
} |
| 333 |
|
| 334 |
static FcChar32 |
| 335 |
FcValueListHash (FcValueListPtr l) |
| 336 |
{ |
| 337 |
FcChar32 hash = 0; |
| 338 |
|
| 339 |
for (; l; l = FcValueListNext (l)) { |
| 340 |
hash = ((hash << 1) | (hash >> 31)) ^ FcValueHash (&l->value); |
| 341 |
} |
| 342 |
return hash; |
| 343 |
} |
| 344 |
|
| 345 |
static void * |
| 346 |
FcPatternGetCacheObject (FcPattern *p) |
| 347 |
{ |
| 348 |
/* We use a value to find the cache, instead of the FcPattern object |
| 349 |
* because the pattern itself may be a cache allocation if we rewrote the path, |
| 350 |
* so the p may not be in the cached region. */ |
| 351 |
return FcPatternEltValues (&FcPatternElts (p)[0]); |
| 352 |
} |
| 353 |
|
| 354 |
FcPattern * |
| 355 |
FcPatternCacheRewriteFile (const FcPattern *p, |
| 356 |
FcCache *cache, |
| 357 |
const FcChar8 *relocated_font_file) |
| 358 |
{ |
| 359 |
FcPatternElt *elts = FcPatternElts (p); |
| 360 |
size_t i, j; |
| 361 |
FcChar8 *data; |
| 362 |
FcPattern *new_p; |
| 363 |
FcPatternElt *new_elts; |
| 364 |
FcValueList *new_value_list; |
| 365 |
size_t new_path_len = strlen ((char *)relocated_font_file); |
| 366 |
FcChar8 *new_path; |
| 367 |
|
| 368 |
/* Allocate space for the patter, the PatternElt headers and |
| 369 |
* the FC_FILE FcValueList and path that will be freed with the |
| 370 |
* cache */ |
| 371 |
data = FcCacheAllocate (cache, |
| 372 |
sizeof (FcPattern) + |
| 373 |
p->num * sizeof (FcPatternElt) + |
| 374 |
sizeof (FcValueList) + |
| 375 |
new_path_len + 1); |
| 376 |
|
| 377 |
new_p = (FcPattern *)data; |
| 378 |
data += sizeof (FcPattern); |
| 379 |
new_elts = (FcPatternElt *)(data); |
| 380 |
data += p->num * sizeof (FcPatternElt); |
| 381 |
new_value_list = (FcValueList *)data; |
| 382 |
data += sizeof (FcValueList); |
| 383 |
new_path = data; |
| 384 |
|
| 385 |
*new_p = *p; |
| 386 |
new_p->elts_offset = FcPtrToOffset (new_p, new_elts); |
| 387 |
|
| 388 |
/* Copy all but the FILE values from the cache */ |
| 389 |
for (i = 0, j = 0; i < p->num; i++) { |
| 390 |
FcPatternElt *elt = &elts[i]; |
| 391 |
new_elts[j].object = elt->object; |
| 392 |
if (elt->object != FC_FILE_OBJECT) |
| 393 |
new_elts[j++].values = FcPatternEltValues (elt); |
| 394 |
else |
| 395 |
new_elts[j++].values = new_value_list; |
| 396 |
} |
| 397 |
|
| 398 |
new_value_list->next = NULL; |
| 399 |
new_value_list->value.type = FcTypeString; |
| 400 |
new_value_list->value.u.s = new_path; |
| 401 |
new_value_list->binding = FcValueBindingWeak; |
| 402 |
|
| 403 |
/* Add rewritten path at the end */ |
| 404 |
strcpy ((char *)new_path, (char *)relocated_font_file); |
| 405 |
|
| 406 |
return new_p; |
| 407 |
} |
| 408 |
|
| 409 |
void |
| 410 |
FcPatternDestroy (FcPattern *p) |
| 411 |
{ |
| 412 |
int i; |
| 413 |
FcPatternElt *elts; |
| 414 |
|
| 415 |
if (!p) |
| 416 |
return; |
| 417 |
|
| 418 |
if (FcRefIsConst (&p->ref)) { |
| 419 |
FcCacheObjectDereference (FcPatternGetCacheObject (p)); |
| 420 |
return; |
| 421 |
} |
| 422 |
|
| 423 |
if (FcRefDec (&p->ref) != 1) |
| 424 |
return; |
| 425 |
|
| 426 |
elts = FcPatternElts (p); |
| 427 |
for (i = 0; i < FcPatternObjectCount (p); i++) |
| 428 |
FcValueListDestroy (FcPatternEltValues (&elts[i])); |
| 429 |
|
| 430 |
free (elts); |
| 431 |
free (p); |
| 432 |
} |
| 433 |
|
| 434 |
int |
| 435 |
FcPatternObjectCount (const FcPattern *pat) |
| 436 |
{ |
| 437 |
if (pat) |
| 438 |
return pat->num; |
| 439 |
|
| 440 |
return 0; |
| 441 |
} |
| 442 |
|
| 443 |
static int |
| 444 |
FcPatternObjectPosition (const FcPattern *p, FcObject object) |
| 445 |
{ |
| 446 |
int low, high, mid, c; |
| 447 |
FcPatternElt *elts = FcPatternElts (p); |
| 448 |
|
| 449 |
low = 0; |
| 450 |
high = FcPatternObjectCount (p) - 1; |
| 451 |
c = 1; |
| 452 |
mid = 0; |
| 453 |
while (low <= high) { |
| 454 |
mid = (low + high) >> 1; |
| 455 |
c = elts[mid].object - object; |
| 456 |
if (c == 0) |
| 457 |
return mid; |
| 458 |
if (c < 0) |
| 459 |
low = mid + 1; |
| 460 |
else |
| 461 |
high = mid - 1; |
| 462 |
} |
| 463 |
if (c < 0) |
| 464 |
mid++; |
| 465 |
return -(mid + 1); |
| 466 |
} |
| 467 |
|
| 468 |
int |
| 469 |
FcPatternPosition (const FcPattern *p, const char *object) |
| 470 |
{ |
| 471 |
return FcPatternObjectPosition (p, FcObjectFromName (object)); |
| 472 |
} |
| 473 |
|
| 474 |
FcPatternElt * |
| 475 |
FcPatternObjectFindElt (const FcPattern *p, FcObject object) |
| 476 |
{ |
| 477 |
int i = FcPatternObjectPosition (p, object); |
| 478 |
if (i < 0) |
| 479 |
return 0; |
| 480 |
return &FcPatternElts (p)[i]; |
| 481 |
} |
| 482 |
|
| 483 |
FcPatternElt * |
| 484 |
FcPatternObjectInsertElt (FcPattern *p, FcObject object) |
| 485 |
{ |
| 486 |
int i; |
| 487 |
FcPatternElt *e; |
| 488 |
|
| 489 |
i = FcPatternObjectPosition (p, object); |
| 490 |
if (i < 0) { |
| 491 |
i = -i - 1; |
| 492 |
|
| 493 |
/* reallocate array */ |
| 494 |
if (FcPatternObjectCount (p) + 1 >= p->size) { |
| 495 |
int s = p->size + 16; |
| 496 |
if (p->size) { |
| 497 |
FcPatternElt *e0 = FcPatternElts (p); |
| 498 |
e = (FcPatternElt *)realloc (e0, s * sizeof (FcPatternElt)); |
| 499 |
if (!e) /* maybe it was mmapped */ |
| 500 |
{ |
| 501 |
e = malloc (s * sizeof (FcPatternElt)); |
| 502 |
if (e) |
| 503 |
memcpy (e, e0, FcPatternObjectCount (p) * sizeof (FcPatternElt)); |
| 504 |
} |
| 505 |
} else |
| 506 |
e = (FcPatternElt *)malloc (s * sizeof (FcPatternElt)); |
| 507 |
if (!e) |
| 508 |
return FcFalse; |
| 509 |
p->elts_offset = FcPtrToOffset (p, e); |
| 510 |
while (p->size < s) { |
| 511 |
e[p->size].object = 0; |
| 512 |
e[p->size].values = NULL; |
| 513 |
p->size++; |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
e = FcPatternElts (p); |
| 518 |
/* move elts up */ |
| 519 |
memmove (e + i + 1, |
| 520 |
e + i, |
| 521 |
sizeof (FcPatternElt) * |
| 522 |
(FcPatternObjectCount (p) - i)); |
| 523 |
|
| 524 |
/* bump count */ |
| 525 |
p->num++; |
| 526 |
|
| 527 |
e[i].object = object; |
| 528 |
e[i].values = NULL; |
| 529 |
} |
| 530 |
|
| 531 |
return FcPatternElts (p) + i; |
| 532 |
} |
| 533 |
|
| 534 |
FcBool |
| 535 |
FcPatternEqual (const FcPattern *pa, const FcPattern *pb) |
| 536 |
{ |
| 537 |
FcPatternIter ia, ib; |
| 538 |
|
| 539 |
if (pa == pb) |
| 540 |
return FcTrue; |
| 541 |
|
| 542 |
if (FcPatternObjectCount (pa) != FcPatternObjectCount (pb)) |
| 543 |
return FcFalse; |
| 544 |
FcPatternIterStart (pa, &ia); |
| 545 |
FcPatternIterStart (pb, &ib); |
| 546 |
do { |
| 547 |
FcBool ra, rb; |
| 548 |
|
| 549 |
if (!FcPatternIterEqual (pa, &ia, pb, &ib)) |
| 550 |
return FcFalse; |
| 551 |
ra = FcPatternIterNext (pa, &ia); |
| 552 |
rb = FcPatternIterNext (pb, &ib); |
| 553 |
if (!ra && !rb) |
| 554 |
break; |
| 555 |
} while (1); |
| 556 |
|
| 557 |
return FcTrue; |
| 558 |
} |
| 559 |
|
| 560 |
FcChar32 |
| 561 |
FcPatternHash (const FcPattern *p) |
| 562 |
{ |
| 563 |
int i; |
| 564 |
FcChar32 h = 0; |
| 565 |
FcPatternElt *pe = FcPatternElts (p); |
| 566 |
|
| 567 |
for (i = 0; i < FcPatternObjectCount (p); i++) { |
| 568 |
h = (((h << 1) | (h >> 31)) ^ |
| 569 |
pe[i].object ^ |
| 570 |
FcValueListHash (FcPatternEltValues (&pe[i]))); |
| 571 |
} |
| 572 |
return h; |
| 573 |
} |
| 574 |
|
| 575 |
FcBool |
| 576 |
FcPatternEqualSubset (const FcPattern *pai, const FcPattern *pbi, const FcObjectSet *os) |
| 577 |
{ |
| 578 |
FcPatternElt *ea, *eb; |
| 579 |
int i; |
| 580 |
|
| 581 |
for (i = 0; i < os->nobject; i++) { |
| 582 |
FcObject object = FcObjectFromName (os->objects[i]); |
| 583 |
ea = FcPatternObjectFindElt (pai, object); |
| 584 |
eb = FcPatternObjectFindElt (pbi, object); |
| 585 |
if (ea) { |
| 586 |
if (!eb) |
| 587 |
return FcFalse; |
| 588 |
if (!FcValueListEqual (FcPatternEltValues (ea), FcPatternEltValues (eb))) |
| 589 |
return FcFalse; |
| 590 |
} else { |
| 591 |
if (eb) |
| 592 |
return FcFalse; |
| 593 |
} |
| 594 |
} |
| 595 |
return FcTrue; |
| 596 |
} |
| 597 |
|
| 598 |
FcBool |
| 599 |
FcPatternObjectListAdd (FcPattern *p, |
| 600 |
FcObject object, |
| 601 |
FcValueListPtr list, |
| 602 |
FcBool append) |
| 603 |
{ |
| 604 |
FcPatternElt *e; |
| 605 |
FcValueListPtr l, *prev; |
| 606 |
|
| 607 |
if (FcRefIsConst (&p->ref)) |
| 608 |
goto bail0; |
| 609 |
|
| 610 |
/* |
| 611 |
* Make sure the stored type is valid for built-in objects |
| 612 |
*/ |
| 613 |
for (l = list; l != NULL; l = FcValueListNext (l)) { |
| 614 |
if (!FcObjectValidType (object, l->value.type)) { |
| 615 |
fprintf (stderr, |
| 616 |
"Fontconfig warning: FcPattern object %s does not accept value", FcObjectName (object)); |
| 617 |
FcValuePrintFile (stderr, l->value); |
| 618 |
fprintf (stderr, "\n"); |
| 619 |
goto bail0; |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
e = FcPatternObjectInsertElt (p, object); |
| 624 |
if (!e) |
| 625 |
goto bail0; |
| 626 |
|
| 627 |
if (append) { |
| 628 |
for (prev = &e->values; *prev; prev = &(*prev)->next) |
| 629 |
; |
| 630 |
*prev = list; |
| 631 |
} else { |
| 632 |
for (prev = &list; *prev; prev = &(*prev)->next) |
| 633 |
; |
| 634 |
*prev = e->values; |
| 635 |
e->values = list; |
| 636 |
} |
| 637 |
|
| 638 |
return FcTrue; |
| 639 |
|
| 640 |
bail0: |
| 641 |
return FcFalse; |
| 642 |
} |
| 643 |
|
| 644 |
FcBool |
| 645 |
FcPatternObjectAddWithBinding (FcPattern *p, |
| 646 |
FcObject object, |
| 647 |
FcValue value, |
| 648 |
FcValueBinding binding, |
| 649 |
FcBool append) |
| 650 |
{ |
| 651 |
FcPatternElt *e; |
| 652 |
FcValueListPtr newp, *prev; |
| 653 |
|
| 654 |
if (FcRefIsConst (&p->ref)) |
| 655 |
goto bail0; |
| 656 |
|
| 657 |
newp = FcValueListCreate(); |
| 658 |
if (!newp) |
| 659 |
goto bail0; |
| 660 |
|
| 661 |
newp->value = FcValueSave (value); |
| 662 |
newp->binding = binding; |
| 663 |
newp->next = NULL; |
| 664 |
|
| 665 |
if (newp->value.type == FcTypeVoid) |
| 666 |
goto bail1; |
| 667 |
|
| 668 |
/* |
| 669 |
* Make sure the stored type is valid for built-in objects |
| 670 |
*/ |
| 671 |
if (!FcObjectValidType (object, newp->value.type)) { |
| 672 |
fprintf (stderr, |
| 673 |
"Fontconfig warning: FcPattern object %s does not accept value", |
| 674 |
FcObjectName (object)); |
| 675 |
FcValuePrintFile (stderr, newp->value); |
| 676 |
fprintf (stderr, "\n"); |
| 677 |
goto bail1; |
| 678 |
} |
| 679 |
|
| 680 |
e = FcPatternObjectInsertElt (p, object); |
| 681 |
if (!e) |
| 682 |
goto bail1; |
| 683 |
|
| 684 |
if (append) { |
| 685 |
for (prev = &e->values; *prev; prev = &(*prev)->next) |
| 686 |
; |
| 687 |
*prev = newp; |
| 688 |
} else { |
| 689 |
newp->next = e->values; |
| 690 |
e->values = newp; |
| 691 |
} |
| 692 |
|
| 693 |
return FcTrue; |
| 694 |
|
| 695 |
bail1: |
| 696 |
FcValueListDestroy (newp); |
| 697 |
bail0: |
| 698 |
return FcFalse; |
| 699 |
} |
| 700 |
|
| 701 |
FcBool |
| 702 |
FcPatternObjectAdd (FcPattern *p, FcObject object, FcValue value, FcBool append) |
| 703 |
{ |
| 704 |
return FcPatternObjectAddWithBinding (p, object, |
| 705 |
value, FcValueBindingStrong, append); |
| 706 |
} |
| 707 |
|
| 708 |
FcBool |
| 709 |
FcPatternAdd (FcPattern *p, const char *object, FcValue value, FcBool append) |
| 710 |
{ |
| 711 |
return FcPatternObjectAddWithBinding (p, FcObjectFromName (object), |
| 712 |
value, FcValueBindingStrong, append); |
| 713 |
} |
| 714 |
|
| 715 |
FcBool |
| 716 |
FcPatternAddWeak (FcPattern *p, const char *object, FcValue value, FcBool append) |
| 717 |
{ |
| 718 |
return FcPatternObjectAddWithBinding (p, FcObjectFromName (object), |
| 719 |
value, FcValueBindingWeak, append); |
| 720 |
} |
| 721 |
|
| 722 |
FcBool |
| 723 |
FcPatternObjectDel (FcPattern *p, FcObject object) |
| 724 |
{ |
| 725 |
FcPatternElt *e; |
| 726 |
|
| 727 |
e = FcPatternObjectFindElt (p, object); |
| 728 |
if (!e) |
| 729 |
return FcFalse; |
| 730 |
|
| 731 |
/* destroy value */ |
| 732 |
FcValueListDestroy (e->values); |
| 733 |
|
| 734 |
/* shuffle existing ones down */ |
| 735 |
memmove (e, e + 1, |
| 736 |
(FcPatternElts (p) + FcPatternObjectCount (p) - (e + 1)) * |
| 737 |
sizeof (FcPatternElt)); |
| 738 |
p->num--; |
| 739 |
e = FcPatternElts (p) + FcPatternObjectCount (p); |
| 740 |
e->object = 0; |
| 741 |
e->values = NULL; |
| 742 |
return FcTrue; |
| 743 |
} |
| 744 |
|
| 745 |
FcBool |
| 746 |
FcPatternDel (FcPattern *p, const char *object) |
| 747 |
{ |
| 748 |
return FcPatternObjectDel (p, FcObjectFromName (object)); |
| 749 |
} |
| 750 |
|
| 751 |
FcBool |
| 752 |
FcPatternRemove (FcPattern *p, const char *object, int id) |
| 753 |
{ |
| 754 |
FcPatternElt *e; |
| 755 |
FcValueListPtr *prev, l; |
| 756 |
|
| 757 |
e = FcPatternObjectFindElt (p, FcObjectFromName (object)); |
| 758 |
if (!e) |
| 759 |
return FcFalse; |
| 760 |
for (prev = &e->values; (l = *prev); prev = &l->next) { |
| 761 |
if (!id) { |
| 762 |
*prev = l->next; |
| 763 |
l->next = NULL; |
| 764 |
FcValueListDestroy (l); |
| 765 |
if (!e->values) |
| 766 |
FcPatternDel (p, object); |
| 767 |
return FcTrue; |
| 768 |
} |
| 769 |
id--; |
| 770 |
} |
| 771 |
return FcFalse; |
| 772 |
} |
| 773 |
|
| 774 |
FcBool |
| 775 |
FcPatternObjectAddInteger (FcPattern *p, FcObject object, int i) |
| 776 |
{ |
| 777 |
FcValue v; |
| 778 |
|
| 779 |
v.type = FcTypeInteger; |
| 780 |
v.u.i = i; |
| 781 |
return FcPatternObjectAdd (p, object, v, FcTrue); |
| 782 |
} |
| 783 |
|
| 784 |
FcBool |
| 785 |
FcPatternAddInteger (FcPattern *p, const char *object, int i) |
| 786 |
{ |
| 787 |
return FcPatternObjectAddInteger (p, FcObjectFromName (object), i); |
| 788 |
} |
| 789 |
|
| 790 |
FcBool |
| 791 |
FcPatternObjectAddDouble (FcPattern *p, FcObject object, double d) |
| 792 |
{ |
| 793 |
FcValue v; |
| 794 |
|
| 795 |
v.type = FcTypeDouble; |
| 796 |
v.u.d = d; |
| 797 |
return FcPatternObjectAdd (p, object, v, FcTrue); |
| 798 |
} |
| 799 |
|
| 800 |
FcBool |
| 801 |
FcPatternAddDouble (FcPattern *p, const char *object, double d) |
| 802 |
{ |
| 803 |
return FcPatternObjectAddDouble (p, FcObjectFromName (object), d); |
| 804 |
} |
| 805 |
|
| 806 |
FcBool |
| 807 |
FcPatternObjectAddString (FcPattern *p, FcObject object, const FcChar8 *s) |
| 808 |
{ |
| 809 |
FcValue v; |
| 810 |
|
| 811 |
if (!s) { |
| 812 |
v.type = FcTypeVoid; |
| 813 |
v.u.s = 0; |
| 814 |
return FcPatternObjectAdd (p, object, v, FcTrue); |
| 815 |
} |
| 816 |
|
| 817 |
v.type = FcTypeString; |
| 818 |
v.u.s = s; |
| 819 |
return FcPatternObjectAdd (p, object, v, FcTrue); |
| 820 |
} |
| 821 |
|
| 822 |
FcBool |
| 823 |
FcPatternAddString (FcPattern *p, const char *object, const FcChar8 *s) |
| 824 |
{ |
| 825 |
return FcPatternObjectAddString (p, FcObjectFromName (object), s); |
| 826 |
} |
| 827 |
|
| 828 |
FcBool |
| 829 |
FcPatternAddMatrix (FcPattern *p, const char *object, const FcMatrix *s) |
| 830 |
{ |
| 831 |
FcValue v; |
| 832 |
|
| 833 |
v.type = FcTypeMatrix; |
| 834 |
v.u.m = s; |
| 835 |
return FcPatternAdd (p, object, v, FcTrue); |
| 836 |
} |
| 837 |
|
| 838 |
FcBool |
| 839 |
FcPatternObjectAddBool (FcPattern *p, FcObject object, FcBool b) |
| 840 |
{ |
| 841 |
FcValue v; |
| 842 |
|
| 843 |
v.type = FcTypeBool; |
| 844 |
v.u.b = b; |
| 845 |
return FcPatternObjectAdd (p, object, v, FcTrue); |
| 846 |
} |
| 847 |
|
| 848 |
FcBool |
| 849 |
FcPatternAddBool (FcPattern *p, const char *object, FcBool b) |
| 850 |
{ |
| 851 |
return FcPatternObjectAddBool (p, FcObjectFromName (object), b); |
| 852 |
} |
| 853 |
|
| 854 |
FcBool |
| 855 |
FcPatternObjectAddCharSet (FcPattern *p, FcObject object, const FcCharSet *c) |
| 856 |
{ |
| 857 |
FcValue v; |
| 858 |
|
| 859 |
v.type = FcTypeCharSet; |
| 860 |
v.u.c = (FcCharSet *)c; |
| 861 |
return FcPatternObjectAdd (p, object, v, FcTrue); |
| 862 |
} |
| 863 |
|
| 864 |
FcBool |
| 865 |
FcPatternAddCharSet (FcPattern *p, const char *object, const FcCharSet *c) |
| 866 |
{ |
| 867 |
return FcPatternObjectAddCharSet (p, FcObjectFromName (object), c); |
| 868 |
} |
| 869 |
|
| 870 |
FcBool |
| 871 |
FcPatternAddFTFace (FcPattern *p, const char *object, const FT_Face f) |
| 872 |
{ |
| 873 |
FcValue v; |
| 874 |
|
| 875 |
v.type = FcTypeFTFace; |
| 876 |
v.u.f = (void *)f; |
| 877 |
return FcPatternAdd (p, object, v, FcTrue); |
| 878 |
} |
| 879 |
|
| 880 |
FcBool |
| 881 |
FcPatternObjectAddLangSet (FcPattern *p, FcObject object, const FcLangSet *ls) |
| 882 |
{ |
| 883 |
FcValue v; |
| 884 |
|
| 885 |
v.type = FcTypeLangSet; |
| 886 |
v.u.l = (FcLangSet *)ls; |
| 887 |
return FcPatternObjectAdd (p, object, v, FcTrue); |
| 888 |
} |
| 889 |
|
| 890 |
FcBool |
| 891 |
FcPatternAddLangSet (FcPattern *p, const char *object, const FcLangSet *ls) |
| 892 |
{ |
| 893 |
return FcPatternObjectAddLangSet (p, FcObjectFromName (object), ls); |
| 894 |
} |
| 895 |
|
| 896 |
FcBool |
| 897 |
FcPatternObjectAddRange (FcPattern *p, FcObject object, const FcRange *r) |
| 898 |
{ |
| 899 |
FcValue v; |
| 900 |
|
| 901 |
v.type = FcTypeRange; |
| 902 |
v.u.r = (FcRange *)r; |
| 903 |
return FcPatternObjectAdd (p, object, v, FcTrue); |
| 904 |
} |
| 905 |
|
| 906 |
FcBool |
| 907 |
FcPatternAddRange (FcPattern *p, const char *object, const FcRange *r) |
| 908 |
{ |
| 909 |
return FcPatternObjectAddRange (p, FcObjectFromName (object), r); |
| 910 |
} |
| 911 |
|
| 912 |
FcResult |
| 913 |
FcPatternObjectGetWithBinding (const FcPattern *p, FcObject object, int id, FcValue *v, FcValueBinding *b) |
| 914 |
{ |
| 915 |
FcPatternElt *e; |
| 916 |
FcValueListPtr l; |
| 917 |
|
| 918 |
if (!p) |
| 919 |
return FcResultNoMatch; |
| 920 |
e = FcPatternObjectFindElt (p, object); |
| 921 |
if (!e) |
| 922 |
return FcResultNoMatch; |
| 923 |
for (l = FcPatternEltValues (e); l; l = FcValueListNext (l)) { |
| 924 |
if (!id) { |
| 925 |
*v = FcValueCanonicalize (&l->value); |
| 926 |
if (b) |
| 927 |
*b = l->binding; |
| 928 |
return FcResultMatch; |
| 929 |
} |
| 930 |
id--; |
| 931 |
} |
| 932 |
return FcResultNoId; |
| 933 |
} |
| 934 |
|
| 935 |
FcResult |
| 936 |
FcPatternObjectGet (const FcPattern *p, FcObject object, int id, FcValue *v) |
| 937 |
{ |
| 938 |
return FcPatternObjectGetWithBinding (p, object, id, v, NULL); |
| 939 |
} |
| 940 |
|
| 941 |
FcResult |
| 942 |
FcPatternGetWithBinding (const FcPattern *p, const char *object, int id, FcValue *v, FcValueBinding *b) |
| 943 |
{ |
| 944 |
return FcPatternObjectGetWithBinding (p, FcObjectFromName (object), id, v, b); |
| 945 |
} |
| 946 |
|
| 947 |
FcResult |
| 948 |
FcPatternGet (const FcPattern *p, const char *object, int id, FcValue *v) |
| 949 |
{ |
| 950 |
return FcPatternObjectGetWithBinding (p, FcObjectFromName (object), id, v, NULL); |
| 951 |
} |
| 952 |
|
| 953 |
FcResult |
| 954 |
FcPatternObjectGetInteger (const FcPattern *p, FcObject object, int id, int *i) |
| 955 |
{ |
| 956 |
FcValue v; |
| 957 |
FcResult r; |
| 958 |
|
| 959 |
r = FcPatternObjectGet (p, object, id, &v); |
| 960 |
if (r != FcResultMatch) |
| 961 |
return r; |
| 962 |
switch ((int)v.type) { |
| 963 |
case FcTypeDouble: |
| 964 |
*i = (int)v.u.d; |
| 965 |
break; |
| 966 |
case FcTypeInteger: |
| 967 |
*i = v.u.i; |
| 968 |
break; |
| 969 |
default: |
| 970 |
return FcResultTypeMismatch; |
| 971 |
} |
| 972 |
return FcResultMatch; |
| 973 |
} |
| 974 |
|
| 975 |
FcResult |
| 976 |
FcPatternGetInteger (const FcPattern *p, const char *object, int id, int *i) |
| 977 |
{ |
| 978 |
return FcPatternObjectGetInteger (p, FcObjectFromName (object), id, i); |
| 979 |
} |
| 980 |
|
| 981 |
FcResult |
| 982 |
FcPatternObjectGetDouble (const FcPattern *p, FcObject object, int id, double *d) |
| 983 |
{ |
| 984 |
FcValue v; |
| 985 |
FcResult r; |
| 986 |
|
| 987 |
r = FcPatternObjectGet (p, object, id, &v); |
| 988 |
if (r != FcResultMatch) |
| 989 |
return r; |
| 990 |
switch ((int)v.type) { |
| 991 |
case FcTypeDouble: |
| 992 |
*d = v.u.d; |
| 993 |
break; |
| 994 |
case FcTypeInteger: |
| 995 |
*d = (double)v.u.i; |
| 996 |
break; |
| 997 |
default: |
| 998 |
return FcResultTypeMismatch; |
| 999 |
} |
| 1000 |
return FcResultMatch; |
| 1001 |
} |
| 1002 |
|
| 1003 |
FcResult |
| 1004 |
FcPatternGetDouble (const FcPattern *p, const char *object, int id, double *d) |
| 1005 |
{ |
| 1006 |
return FcPatternObjectGetDouble (p, FcObjectFromName (object), id, d); |
| 1007 |
} |
| 1008 |
|
| 1009 |
FcResult |
| 1010 |
FcPatternObjectGetString (const FcPattern *p, FcObject object, int id, FcChar8 **s) |
| 1011 |
{ |
| 1012 |
FcValue v; |
| 1013 |
FcResult r; |
| 1014 |
|
| 1015 |
r = FcPatternObjectGet (p, object, id, &v); |
| 1016 |
if (r != FcResultMatch) |
| 1017 |
return r; |
| 1018 |
if (v.type != FcTypeString) |
| 1019 |
return FcResultTypeMismatch; |
| 1020 |
|
| 1021 |
*s = (FcChar8 *)v.u.s; |
| 1022 |
return FcResultMatch; |
| 1023 |
} |
| 1024 |
|
| 1025 |
FcResult |
| 1026 |
FcPatternGetString (const FcPattern *p, const char *object, int id, FcChar8 **s) |
| 1027 |
{ |
| 1028 |
return FcPatternObjectGetString (p, FcObjectFromName (object), id, s); |
| 1029 |
} |
| 1030 |
|
| 1031 |
FcResult |
| 1032 |
FcPatternGetMatrix (const FcPattern *p, const char *object, int id, FcMatrix **m) |
| 1033 |
{ |
| 1034 |
FcValue v; |
| 1035 |
FcResult r; |
| 1036 |
|
| 1037 |
r = FcPatternGet (p, object, id, &v); |
| 1038 |
if (r != FcResultMatch) |
| 1039 |
return r; |
| 1040 |
if (v.type != FcTypeMatrix) |
| 1041 |
return FcResultTypeMismatch; |
| 1042 |
*m = (FcMatrix *)v.u.m; |
| 1043 |
return FcResultMatch; |
| 1044 |
} |
| 1045 |
|
| 1046 |
FcResult |
| 1047 |
FcPatternObjectGetBool (const FcPattern *p, FcObject object, int id, FcBool *b) |
| 1048 |
{ |
| 1049 |
FcValue v; |
| 1050 |
FcResult r; |
| 1051 |
|
| 1052 |
r = FcPatternObjectGet (p, object, id, &v); |
| 1053 |
if (r != FcResultMatch) |
| 1054 |
return r; |
| 1055 |
if (v.type != FcTypeBool) |
| 1056 |
return FcResultTypeMismatch; |
| 1057 |
*b = v.u.b; |
| 1058 |
return FcResultMatch; |
| 1059 |
} |
| 1060 |
|
| 1061 |
FcResult |
| 1062 |
FcPatternGetBool (const FcPattern *p, const char *object, int id, FcBool *b) |
| 1063 |
{ |
| 1064 |
return FcPatternObjectGetBool (p, FcObjectFromName (object), id, b); |
| 1065 |
} |
| 1066 |
|
| 1067 |
FcResult |
| 1068 |
FcPatternGetCharSet (const FcPattern *p, const char *object, int id, FcCharSet **c) |
| 1069 |
{ |
| 1070 |
return FcPatternObjectGetCharSet (p, FcObjectFromName (object), id, c); |
| 1071 |
} |
| 1072 |
|
| 1073 |
FcResult |
| 1074 |
FcPatternObjectGetCharSet (const FcPattern *p, FcObject object, int id, FcCharSet **c) |
| 1075 |
{ |
| 1076 |
FcValue v; |
| 1077 |
FcResult r; |
| 1078 |
|
| 1079 |
r = FcPatternObjectGet (p, object, id, &v); |
| 1080 |
if (r != FcResultMatch) |
| 1081 |
return r; |
| 1082 |
if (v.type != FcTypeCharSet) |
| 1083 |
return FcResultTypeMismatch; |
| 1084 |
*c = (FcCharSet *)v.u.c; |
| 1085 |
return FcResultMatch; |
| 1086 |
} |
| 1087 |
|
| 1088 |
FcResult |
| 1089 |
FcPatternGetFTFace (const FcPattern *p, const char *object, int id, FT_Face *f) |
| 1090 |
{ |
| 1091 |
FcValue v; |
| 1092 |
FcResult r; |
| 1093 |
|
| 1094 |
r = FcPatternGet (p, object, id, &v); |
| 1095 |
if (r != FcResultMatch) |
| 1096 |
return r; |
| 1097 |
if (v.type != FcTypeFTFace) |
| 1098 |
return FcResultTypeMismatch; |
| 1099 |
*f = (FT_Face)v.u.f; |
| 1100 |
return FcResultMatch; |
| 1101 |
} |
| 1102 |
|
| 1103 |
FcResult |
| 1104 |
FcPatternGetLangSet (const FcPattern *p, const char *object, int id, FcLangSet **ls) |
| 1105 |
{ |
| 1106 |
return FcPatternObjectGetLangSet (p, FcObjectFromName (object), id, ls); |
| 1107 |
} |
| 1108 |
|
| 1109 |
FcResult |
| 1110 |
FcPatternObjectGetLangSet (const FcPattern *p, FcObject object, int id, FcLangSet **ls) |
| 1111 |
{ |
| 1112 |
FcValue v; |
| 1113 |
FcResult r; |
| 1114 |
|
| 1115 |
r = FcPatternObjectGet (p, object, id, &v); |
| 1116 |
if (r != FcResultMatch) |
| 1117 |
return r; |
| 1118 |
if (v.type != FcTypeLangSet) |
| 1119 |
return FcResultTypeMismatch; |
| 1120 |
*ls = (FcLangSet *)v.u.l; |
| 1121 |
return FcResultMatch; |
| 1122 |
} |
| 1123 |
|
| 1124 |
FcResult |
| 1125 |
FcPatternObjectGetRange (const FcPattern *p, FcObject object, int id, FcRange **r) |
| 1126 |
{ |
| 1127 |
FcValue v; |
| 1128 |
FcResult res; |
| 1129 |
|
| 1130 |
res = FcPatternObjectGet (p, object, id, &v); |
| 1131 |
if (res != FcResultMatch) |
| 1132 |
return res; |
| 1133 |
switch ((int)v.type) { |
| 1134 |
case FcTypeRange: |
| 1135 |
*r = (FcRange *)v.u.r; |
| 1136 |
break; |
| 1137 |
default: |
| 1138 |
return FcResultTypeMismatch; |
| 1139 |
} |
| 1140 |
return FcResultMatch; |
| 1141 |
} |
| 1142 |
|
| 1143 |
FcResult |
| 1144 |
FcPatternGetRange (const FcPattern *p, const char *object, int id, FcRange **r) |
| 1145 |
{ |
| 1146 |
return FcPatternObjectGetRange (p, FcObjectFromName (object), id, r); |
| 1147 |
} |
| 1148 |
|
| 1149 |
FcPattern * |
| 1150 |
FcPatternDuplicate (const FcPattern *orig) |
| 1151 |
{ |
| 1152 |
FcPattern *newp; |
| 1153 |
FcPatternIter iter; |
| 1154 |
FcValueListPtr l; |
| 1155 |
|
| 1156 |
if (!orig) |
| 1157 |
return NULL; |
| 1158 |
|
| 1159 |
newp = FcPatternCreate(); |
| 1160 |
if (!newp) |
| 1161 |
goto bail0; |
| 1162 |
|
| 1163 |
FcPatternIterStart (orig, &iter); |
| 1164 |
do { |
| 1165 |
for (l = FcPatternIterGetValues (orig, &iter); l; l = FcValueListNext (l)) { |
| 1166 |
if (!FcPatternObjectAddWithBinding (newp, FcPatternIterGetObjectId (orig, &iter), |
| 1167 |
FcValueCanonicalize (&l->value), |
| 1168 |
l->binding, |
| 1169 |
FcTrue)) |
| 1170 |
goto bail1; |
| 1171 |
} |
| 1172 |
} while (FcPatternIterNext (orig, &iter)); |
| 1173 |
|
| 1174 |
return newp; |
| 1175 |
|
| 1176 |
bail1: |
| 1177 |
FcPatternDestroy (newp); |
| 1178 |
bail0: |
| 1179 |
return 0; |
| 1180 |
} |
| 1181 |
|
| 1182 |
void |
| 1183 |
FcPatternReference (FcPattern *p) |
| 1184 |
{ |
| 1185 |
if (!FcRefIsConst (&p->ref)) |
| 1186 |
FcRefInc (&p->ref); |
| 1187 |
else |
| 1188 |
FcCacheObjectReference (FcPatternGetCacheObject (p)); |
| 1189 |
} |
| 1190 |
|
| 1191 |
FcPattern * |
| 1192 |
FcPatternVaBuild (FcPattern *p, va_list va) |
| 1193 |
{ |
| 1194 |
FcPattern *ret; |
| 1195 |
|
| 1196 |
FcPatternVapBuild (ret, p, va); |
| 1197 |
return ret; |
| 1198 |
} |
| 1199 |
|
| 1200 |
FcPattern * |
| 1201 |
FcPatternBuild (FcPattern *p, ...) |
| 1202 |
{ |
| 1203 |
va_list va; |
| 1204 |
|
| 1205 |
va_start (va, p); |
| 1206 |
FcPatternVapBuild (p, p, va); |
| 1207 |
va_end (va); |
| 1208 |
return p; |
| 1209 |
} |
| 1210 |
|
| 1211 |
/* |
| 1212 |
* Add all of the elements in 's' to 'p' |
| 1213 |
*/ |
| 1214 |
FcBool |
| 1215 |
FcPatternAppend (FcPattern *p, FcPattern *s) |
| 1216 |
{ |
| 1217 |
FcPatternIter iter; |
| 1218 |
FcValueListPtr v; |
| 1219 |
|
| 1220 |
FcPatternIterStart (s, &iter); |
| 1221 |
do { |
| 1222 |
for (v = FcPatternIterGetValues (s, &iter); v; v = FcValueListNext (v)) { |
| 1223 |
if (!FcPatternObjectAddWithBinding (p, FcPatternIterGetObjectId (s, &iter), |
| 1224 |
FcValueCanonicalize (&v->value), |
| 1225 |
v->binding, FcTrue)) |
| 1226 |
return FcFalse; |
| 1227 |
} |
| 1228 |
} while (FcPatternIterNext (s, &iter)); |
| 1229 |
|
| 1230 |
return FcTrue; |
| 1231 |
} |
| 1232 |
|
| 1233 |
FcPattern * |
| 1234 |
FcPatternFilter (FcPattern *p, const FcObjectSet *os) |
| 1235 |
{ |
| 1236 |
int i; |
| 1237 |
FcPattern *ret; |
| 1238 |
FcPatternElt *e; |
| 1239 |
FcValueListPtr v; |
| 1240 |
|
| 1241 |
if (!os) |
| 1242 |
return FcPatternDuplicate (p); |
| 1243 |
|
| 1244 |
ret = FcPatternCreate(); |
| 1245 |
if (!ret) |
| 1246 |
return NULL; |
| 1247 |
|
| 1248 |
for (i = 0; i < os->nobject; i++) { |
| 1249 |
FcObject object = FcObjectFromName (os->objects[i]); |
| 1250 |
e = FcPatternObjectFindElt (p, object); |
| 1251 |
if (e) { |
| 1252 |
for (v = FcPatternEltValues (e); v; v = FcValueListNext (v)) { |
| 1253 |
if (!FcPatternObjectAddWithBinding (ret, e->object, |
| 1254 |
FcValueCanonicalize (&v->value), |
| 1255 |
v->binding, FcTrue)) |
| 1256 |
goto bail0; |
| 1257 |
} |
| 1258 |
} |
| 1259 |
} |
| 1260 |
return ret; |
| 1261 |
|
| 1262 |
bail0: |
| 1263 |
FcPatternDestroy (ret); |
| 1264 |
return NULL; |
| 1265 |
} |
| 1266 |
|
| 1267 |
typedef struct _FcPatternPrivateIter { |
| 1268 |
FcPatternElt *elt; |
| 1269 |
int pos; |
| 1270 |
} FcPatternPrivateIter; |
| 1271 |
|
| 1272 |
static void |
| 1273 |
FcPatternIterSet (const FcPattern *pat, FcPatternPrivateIter *iter) |
| 1274 |
{ |
| 1275 |
iter->elt = FcPatternObjectCount (pat) > 0 && iter->pos < FcPatternObjectCount (pat) ? &FcPatternElts (pat)[iter->pos] : NULL; |
| 1276 |
} |
| 1277 |
|
| 1278 |
void |
| 1279 |
FcPatternIterStart (const FcPattern *pat, FcPatternIter *iter) |
| 1280 |
{ |
| 1281 |
FcPatternPrivateIter *priv = (FcPatternPrivateIter *)iter; |
| 1282 |
|
| 1283 |
priv->pos = 0; |
| 1284 |
FcPatternIterSet (pat, priv); |
| 1285 |
} |
| 1286 |
|
| 1287 |
FcBool |
| 1288 |
FcPatternIterNext (const FcPattern *pat, FcPatternIter *iter) |
| 1289 |
{ |
| 1290 |
FcPatternPrivateIter *priv = (FcPatternPrivateIter *)iter; |
| 1291 |
|
| 1292 |
priv->pos++; |
| 1293 |
if (priv->pos >= FcPatternObjectCount (pat)) |
| 1294 |
return FcFalse; |
| 1295 |
FcPatternIterSet (pat, priv); |
| 1296 |
|
| 1297 |
return FcTrue; |
| 1298 |
} |
| 1299 |
|
| 1300 |
FcBool |
| 1301 |
FcPatternIterEqual (const FcPattern *p1, FcPatternIter *i1, |
| 1302 |
const FcPattern *p2, FcPatternIter *i2) |
| 1303 |
{ |
| 1304 |
FcBool b1 = FcPatternIterIsValid (p1, i1); |
| 1305 |
FcBool b2 = FcPatternIterIsValid (p2, i2); |
| 1306 |
|
| 1307 |
if (!i1 && !i2) |
| 1308 |
return FcTrue; |
| 1309 |
if (!b1 || !b2) |
| 1310 |
return FcFalse; |
| 1311 |
if (FcPatternIterGetObjectId (p1, i1) != FcPatternIterGetObjectId (p2, i2)) |
| 1312 |
return FcFalse; |
| 1313 |
|
| 1314 |
return FcValueListEqual (FcPatternIterGetValues (p1, i1), |
| 1315 |
FcPatternIterGetValues (p2, i2)); |
| 1316 |
} |
| 1317 |
|
| 1318 |
FcBool |
| 1319 |
FcPatternFindObjectIter (const FcPattern *pat, FcPatternIter *iter, FcObject object) |
| 1320 |
{ |
| 1321 |
FcPatternPrivateIter *priv = (FcPatternPrivateIter *)iter; |
| 1322 |
int i = FcPatternObjectPosition (pat, object); |
| 1323 |
|
| 1324 |
priv->elt = NULL; |
| 1325 |
if (i < 0) |
| 1326 |
return FcFalse; |
| 1327 |
|
| 1328 |
priv->pos = i; |
| 1329 |
FcPatternIterSet (pat, priv); |
| 1330 |
|
| 1331 |
return FcTrue; |
| 1332 |
} |
| 1333 |
|
| 1334 |
FcBool |
| 1335 |
FcPatternFindIter (const FcPattern *pat, FcPatternIter *iter, const char *object) |
| 1336 |
{ |
| 1337 |
return FcPatternFindObjectIter (pat, iter, FcObjectFromName (object)); |
| 1338 |
} |
| 1339 |
|
| 1340 |
FcBool |
| 1341 |
FcPatternIterIsValid (const FcPattern *pat, FcPatternIter *iter) |
| 1342 |
{ |
| 1343 |
FcPatternPrivateIter *priv = (FcPatternPrivateIter *)iter; |
| 1344 |
|
| 1345 |
if (priv && priv->elt) |
| 1346 |
return FcTrue; |
| 1347 |
|
| 1348 |
return FcFalse; |
| 1349 |
} |
| 1350 |
|
| 1351 |
FcObject |
| 1352 |
FcPatternIterGetObjectId (const FcPattern *pat, FcPatternIter *iter) |
| 1353 |
{ |
| 1354 |
FcPatternPrivateIter *priv = (FcPatternPrivateIter *)iter; |
| 1355 |
|
| 1356 |
if (priv && priv->elt) |
| 1357 |
return priv->elt->object; |
| 1358 |
|
| 1359 |
return 0; |
| 1360 |
} |
| 1361 |
|
| 1362 |
const char * |
| 1363 |
FcPatternIterGetObject (const FcPattern *pat, FcPatternIter *iter) |
| 1364 |
{ |
| 1365 |
return FcObjectName (FcPatternIterGetObjectId (pat, iter)); |
| 1366 |
} |
| 1367 |
|
| 1368 |
FcValueListPtr |
| 1369 |
FcPatternIterGetValues (const FcPattern *pat, FcPatternIter *iter) |
| 1370 |
{ |
| 1371 |
FcPatternPrivateIter *priv = (FcPatternPrivateIter *)iter; |
| 1372 |
|
| 1373 |
if (priv && priv->elt) |
| 1374 |
return FcPatternEltValues (priv->elt); |
| 1375 |
|
| 1376 |
return NULL; |
| 1377 |
} |
| 1378 |
|
| 1379 |
int |
| 1380 |
FcPatternIterValueCount (const FcPattern *pat, FcPatternIter *iter) |
| 1381 |
{ |
| 1382 |
int count = 0; |
| 1383 |
FcValueListPtr l; |
| 1384 |
|
| 1385 |
for (l = FcPatternIterGetValues (pat, iter); l; l = FcValueListNext (l)) |
| 1386 |
count++; |
| 1387 |
|
| 1388 |
return count; |
| 1389 |
} |
| 1390 |
|
| 1391 |
FcResult |
| 1392 |
FcPatternIterGetValue (const FcPattern *pat, FcPatternIter *iter, int id, FcValue *v, FcValueBinding *b) |
| 1393 |
{ |
| 1394 |
FcValueListPtr l; |
| 1395 |
|
| 1396 |
for (l = FcPatternIterGetValues (pat, iter); l; l = FcValueListNext (l)) { |
| 1397 |
if (id == 0) { |
| 1398 |
*v = FcValueCanonicalize (&l->value); |
| 1399 |
if (b) |
| 1400 |
*b = l->binding; |
| 1401 |
return FcResultMatch; |
| 1402 |
} |
| 1403 |
id--; |
| 1404 |
} |
| 1405 |
return FcResultNoId; |
| 1406 |
} |
| 1407 |
|
| 1408 |
FcBool |
| 1409 |
FcPatternSerializeAlloc (FcSerialize *serialize, const FcPattern *pat) |
| 1410 |
{ |
| 1411 |
int i; |
| 1412 |
FcPatternElt *elts = FcPatternElts (pat); |
| 1413 |
|
| 1414 |
if (!FcSerializeAlloc (serialize, pat, sizeof (FcPattern))) |
| 1415 |
return FcFalse; |
| 1416 |
if (!FcSerializeAlloc (serialize, elts, FcPatternObjectCount (pat) * sizeof (FcPatternElt))) |
| 1417 |
return FcFalse; |
| 1418 |
for (i = 0; i < FcPatternObjectCount (pat); i++) |
| 1419 |
if (!FcValueListSerializeAlloc (serialize, FcPatternEltValues (elts + i))) |
| 1420 |
return FcFalse; |
| 1421 |
return FcTrue; |
| 1422 |
} |
| 1423 |
|
| 1424 |
FcPattern * |
| 1425 |
FcPatternSerialize (FcSerialize *serialize, const FcPattern *pat) |
| 1426 |
{ |
| 1427 |
FcPattern *pat_serialized; |
| 1428 |
FcPatternElt *elts = FcPatternElts (pat); |
| 1429 |
FcPatternElt *elts_serialized; |
| 1430 |
FcValueList *values_serialized; |
| 1431 |
int i; |
| 1432 |
|
| 1433 |
pat_serialized = FcSerializePtr (serialize, pat); |
| 1434 |
if (!pat_serialized) |
| 1435 |
return NULL; |
| 1436 |
*pat_serialized = *pat; |
| 1437 |
pat_serialized->size = FcPatternObjectCount (pat); |
| 1438 |
FcRefSetConst (&pat_serialized->ref); |
| 1439 |
|
| 1440 |
elts_serialized = FcSerializePtr (serialize, elts); |
| 1441 |
if (!elts_serialized) |
| 1442 |
return NULL; |
| 1443 |
|
| 1444 |
pat_serialized->elts_offset = FcPtrToOffset (pat_serialized, |
| 1445 |
elts_serialized); |
| 1446 |
|
| 1447 |
for (i = 0; i < FcPatternObjectCount (pat); i++) { |
| 1448 |
values_serialized = FcValueListSerialize (serialize, FcPatternEltValues (elts + i)); |
| 1449 |
if (!values_serialized) |
| 1450 |
return NULL; |
| 1451 |
elts_serialized[i].object = elts[i].object; |
| 1452 |
elts_serialized[i].values = FcPtrToEncodedOffset (&elts_serialized[i], |
| 1453 |
values_serialized, |
| 1454 |
FcValueList); |
| 1455 |
} |
| 1456 |
if (FcDebug() & FC_DBG_CACHEV) { |
| 1457 |
printf ("Raw pattern:\n"); |
| 1458 |
FcPatternPrint (pat); |
| 1459 |
printf ("Serialized pattern:\n"); |
| 1460 |
FcPatternPrint (pat_serialized); |
| 1461 |
printf ("\n"); |
| 1462 |
} |
| 1463 |
return pat_serialized; |
| 1464 |
} |
| 1465 |
|
| 1466 |
FcBool |
| 1467 |
FcValueListSerializeAlloc (FcSerialize *serialize, const FcValueList *vl) |
| 1468 |
{ |
| 1469 |
while (vl) { |
| 1470 |
if (!FcSerializeAlloc (serialize, vl, sizeof (FcValueList))) |
| 1471 |
return FcFalse; |
| 1472 |
switch ((int)vl->value.type) { |
| 1473 |
case FcTypeString: |
| 1474 |
if (!FcStrSerializeAlloc (serialize, vl->value.u.s)) |
| 1475 |
return FcFalse; |
| 1476 |
break; |
| 1477 |
case FcTypeCharSet: |
| 1478 |
if (!FcCharSetSerializeAlloc (serialize, vl->value.u.c)) |
| 1479 |
return FcFalse; |
| 1480 |
break; |
| 1481 |
case FcTypeLangSet: |
| 1482 |
if (!FcLangSetSerializeAlloc (serialize, vl->value.u.l)) |
| 1483 |
return FcFalse; |
| 1484 |
break; |
| 1485 |
case FcTypeRange: |
| 1486 |
if (!FcRangeSerializeAlloc (serialize, vl->value.u.r)) |
| 1487 |
return FcFalse; |
| 1488 |
break; |
| 1489 |
default: |
| 1490 |
break; |
| 1491 |
} |
| 1492 |
vl = vl->next; |
| 1493 |
} |
| 1494 |
return FcTrue; |
| 1495 |
} |
| 1496 |
|
| 1497 |
FcValueList * |
| 1498 |
FcValueListSerialize (FcSerialize *serialize, const FcValueList *vl) |
| 1499 |
{ |
| 1500 |
FcValueList *vl_serialized; |
| 1501 |
FcChar8 *s_serialized; |
| 1502 |
FcCharSet *c_serialized; |
| 1503 |
FcLangSet *l_serialized; |
| 1504 |
FcRange *r_serialized; |
| 1505 |
FcValueList *head_serialized = NULL; |
| 1506 |
FcValueList *prev_serialized = NULL; |
| 1507 |
|
| 1508 |
while (vl) { |
| 1509 |
vl_serialized = FcSerializePtr (serialize, vl); |
| 1510 |
if (!vl_serialized) |
| 1511 |
return NULL; |
| 1512 |
|
| 1513 |
if (prev_serialized) |
| 1514 |
prev_serialized->next = FcPtrToEncodedOffset (prev_serialized, |
| 1515 |
vl_serialized, |
| 1516 |
FcValueList); |
| 1517 |
else |
| 1518 |
head_serialized = vl_serialized; |
| 1519 |
|
| 1520 |
vl_serialized->next = NULL; |
| 1521 |
vl_serialized->value.type = vl->value.type; |
| 1522 |
switch ((int)vl->value.type) { |
| 1523 |
case FcTypeInteger: |
| 1524 |
vl_serialized->value.u.i = vl->value.u.i; |
| 1525 |
break; |
| 1526 |
case FcTypeDouble: |
| 1527 |
vl_serialized->value.u.d = vl->value.u.d; |
| 1528 |
break; |
| 1529 |
case FcTypeString: |
| 1530 |
s_serialized = FcStrSerialize (serialize, vl->value.u.s); |
| 1531 |
if (!s_serialized) |
| 1532 |
return NULL; |
| 1533 |
vl_serialized->value.u.s = FcPtrToEncodedOffset (&vl_serialized->value, |
| 1534 |
s_serialized, |
| 1535 |
FcChar8); |
| 1536 |
break; |
| 1537 |
case FcTypeBool: |
| 1538 |
vl_serialized->value.u.b = vl->value.u.b; |
| 1539 |
break; |
| 1540 |
case FcTypeMatrix: |
| 1541 |
/* can't happen */ |
| 1542 |
break; |
| 1543 |
case FcTypeCharSet: |
| 1544 |
c_serialized = FcCharSetSerialize (serialize, vl->value.u.c); |
| 1545 |
if (!c_serialized) |
| 1546 |
return NULL; |
| 1547 |
vl_serialized->value.u.c = FcPtrToEncodedOffset (&vl_serialized->value, |
| 1548 |
c_serialized, |
| 1549 |
FcCharSet); |
| 1550 |
break; |
| 1551 |
case FcTypeFTFace: |
| 1552 |
/* can't happen */ |
| 1553 |
break; |
| 1554 |
case FcTypeLangSet: |
| 1555 |
l_serialized = FcLangSetSerialize (serialize, vl->value.u.l); |
| 1556 |
if (!l_serialized) |
| 1557 |
return NULL; |
| 1558 |
vl_serialized->value.u.l = FcPtrToEncodedOffset (&vl_serialized->value, |
| 1559 |
l_serialized, |
| 1560 |
FcLangSet); |
| 1561 |
break; |
| 1562 |
case FcTypeRange: |
| 1563 |
r_serialized = FcRangeSerialize (serialize, vl->value.u.r); |
| 1564 |
if (!r_serialized) |
| 1565 |
return NULL; |
| 1566 |
vl_serialized->value.u.r = FcPtrToEncodedOffset (&vl_serialized->value, |
| 1567 |
r_serialized, |
| 1568 |
FcRange); |
| 1569 |
break; |
| 1570 |
default: |
| 1571 |
break; |
| 1572 |
} |
| 1573 |
prev_serialized = vl_serialized; |
| 1574 |
vl = vl->next; |
| 1575 |
} |
| 1576 |
return head_serialized; |
| 1577 |
} |
| 1578 |
|
| 1579 |
#define __fcpat__ |
| 1580 |
#include "fcaliastail.h" |
| 1581 |
#include "fcftaliastail.h" |
| 1582 |
#undef __fcpat__ |