| 1 |
/* |
| 2 |
* fontconfig/src/fcmatch.c |
| 3 |
* |
| 4 |
* Copyright © 2000 Keith Packard |
| 5 |
* |
| 6 |
* Permission to use, copy, modify, distribute, and sell this software and its |
| 7 |
* documentation for any purpose is hereby granted without fee, provided that |
| 8 |
* the above copyright notice appear in all copies and that both that |
| 9 |
* copyright notice and this permission notice appear in supporting |
| 10 |
* documentation, and that the name of the author(s) not be used in |
| 11 |
* advertising or publicity pertaining to distribution of the software without |
| 12 |
* specific, written prior permission. The authors make no |
| 13 |
* representations about the suitability of this software for any purpose. It |
| 14 |
* is provided "as is" without express or implied warranty. |
| 15 |
* |
| 16 |
* THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 17 |
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 18 |
* EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 19 |
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 20 |
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 21 |
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 22 |
* PERFORMANCE OF THIS SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "fcint.h" |
| 26 |
|
| 27 |
static double |
| 28 |
FcCompareNumber (const FcValue *value1, const FcValue *value2, FcValue *bestValue) |
| 29 |
{ |
| 30 |
double v1, v2, v; |
| 31 |
|
| 32 |
switch ((int)value1->type) { |
| 33 |
case FcTypeInteger: |
| 34 |
v1 = (double)value1->u.i; |
| 35 |
break; |
| 36 |
case FcTypeDouble: |
| 37 |
v1 = value1->u.d; |
| 38 |
break; |
| 39 |
default: |
| 40 |
return -1.0; |
| 41 |
} |
| 42 |
switch ((int)value2->type) { |
| 43 |
case FcTypeInteger: |
| 44 |
v2 = (double)value2->u.i; |
| 45 |
break; |
| 46 |
case FcTypeDouble: |
| 47 |
v2 = value2->u.d; |
| 48 |
break; |
| 49 |
default: |
| 50 |
return -1.0; |
| 51 |
} |
| 52 |
v = v2 - v1; |
| 53 |
if (v < 0) |
| 54 |
v = -v; |
| 55 |
*bestValue = FcValueCanonicalize (value2); |
| 56 |
return v; |
| 57 |
} |
| 58 |
|
| 59 |
static double |
| 60 |
FcCompareString (const FcValue *v1, const FcValue *v2, FcValue *bestValue) |
| 61 |
{ |
| 62 |
*bestValue = FcValueCanonicalize (v2); |
| 63 |
return (double)FcStrCmpIgnoreCase (FcValueString (v1), FcValueString (v2)) != 0; |
| 64 |
} |
| 65 |
|
| 66 |
static double |
| 67 |
FcCompareFamily (const FcValue *v1, const FcValue *v2, FcValue *bestValue) |
| 68 |
{ |
| 69 |
/* rely on the guarantee in FcPatternObjectAddWithBinding that |
| 70 |
* families are always FcTypeString. */ |
| 71 |
const FcChar8 *v1_string = FcValueString (v1); |
| 72 |
const FcChar8 *v2_string = FcValueString (v2); |
| 73 |
|
| 74 |
*bestValue = FcValueCanonicalize (v2); |
| 75 |
|
| 76 |
if (FcToLower (*v1_string) != FcToLower (*v2_string) && |
| 77 |
*v1_string != ' ' && *v2_string != ' ') |
| 78 |
return 1.0; |
| 79 |
|
| 80 |
return (double)FcStrCmpIgnoreBlanksAndCase (v1_string, v2_string) != 0; |
| 81 |
} |
| 82 |
|
| 83 |
static double |
| 84 |
FcComparePostScript (const FcValue *v1, const FcValue *v2, FcValue *bestValue) |
| 85 |
{ |
| 86 |
const FcChar8 *v1_string = FcValueString (v1); |
| 87 |
const FcChar8 *v2_string = FcValueString (v2); |
| 88 |
int n; |
| 89 |
size_t len1, len2, mlen; |
| 90 |
|
| 91 |
*bestValue = FcValueCanonicalize (v2); |
| 92 |
|
| 93 |
if (FcToLower (*v1_string) != FcToLower (*v2_string) && |
| 94 |
*v1_string != ' ' && *v2_string != ' ') |
| 95 |
return 1.0; |
| 96 |
|
| 97 |
n = FcStrMatchIgnoreCaseAndDelims (v1_string, v2_string, (const FcChar8 *)" -,"); |
| 98 |
len1 = strlen ((const char *)v1_string); |
| 99 |
len2 = strlen ((const char *)v2_string); |
| 100 |
mlen = FC_MAX (len1, len2); |
| 101 |
|
| 102 |
return (double)(mlen - n) / (double)mlen; |
| 103 |
} |
| 104 |
|
| 105 |
static double |
| 106 |
FcCompareLang (const FcValue *v1, const FcValue *v2, FcValue *bestValue) |
| 107 |
{ |
| 108 |
FcLangResult result; |
| 109 |
|
| 110 |
switch ((int)v1->type) { |
| 111 |
case FcTypeLangSet: |
| 112 |
switch ((int)v2->type) { |
| 113 |
case FcTypeLangSet: |
| 114 |
result = FcLangSetCompare (FcValueLangSet (v1), FcValueLangSet (v2)); |
| 115 |
break; |
| 116 |
case FcTypeString: |
| 117 |
result = FcLangSetHasLang (FcValueLangSet (v1), FcValueString (v2)); |
| 118 |
break; |
| 119 |
default: |
| 120 |
return -1.0; |
| 121 |
} |
| 122 |
break; |
| 123 |
case FcTypeString: |
| 124 |
switch ((int)v2->type) { |
| 125 |
case FcTypeLangSet: |
| 126 |
result = FcLangSetHasLang (FcValueLangSet (v2), FcValueString (v1)); |
| 127 |
break; |
| 128 |
case FcTypeString: |
| 129 |
result = FcLangCompare (FcValueString (v1), FcValueString (v2)); |
| 130 |
break; |
| 131 |
default: |
| 132 |
return -1.0; |
| 133 |
} |
| 134 |
break; |
| 135 |
default: |
| 136 |
return -1.0; |
| 137 |
} |
| 138 |
*bestValue = FcValueCanonicalize (v2); |
| 139 |
switch (result) { |
| 140 |
case FcLangEqual: |
| 141 |
return 0; |
| 142 |
case FcLangDifferentCountry: |
| 143 |
return 1; |
| 144 |
case FcLangDifferentLang: |
| 145 |
default: |
| 146 |
return 2; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
static double |
| 151 |
FcCompareBool (const FcValue *v1, const FcValue *v2, FcValue *bestValue) |
| 152 |
{ |
| 153 |
if (v2->type != FcTypeBool || v1->type != FcTypeBool) |
| 154 |
return -1.0; |
| 155 |
|
| 156 |
bestValue->type = FcTypeBool; |
| 157 |
if (v2->u.b != FcDontCare) |
| 158 |
bestValue->u.b = v2->u.b; |
| 159 |
else |
| 160 |
bestValue->u.b = v1->u.b; |
| 161 |
|
| 162 |
return (double)((v2->u.b ^ v1->u.b) == 1); |
| 163 |
} |
| 164 |
|
| 165 |
static double |
| 166 |
FcCompareCharSet (const FcValue *v1, const FcValue *v2, FcValue *bestValue) |
| 167 |
{ |
| 168 |
*bestValue = FcValueCanonicalize (v2); /* TODO Improve. */ |
| 169 |
return (double)FcCharSetSubtractCount (FcValueCharSet (v1), FcValueCharSet (v2)); |
| 170 |
} |
| 171 |
|
| 172 |
static double |
| 173 |
FcCompareRange (const FcValue *v1, const FcValue *v2, FcValue *bestValue) |
| 174 |
{ |
| 175 |
FcValue value1 = FcValueCanonicalize (v1); |
| 176 |
FcValue value2 = FcValueCanonicalize (v2); |
| 177 |
double b1, e1, b2, e2, d; |
| 178 |
|
| 179 |
switch ((int)value1.type) { |
| 180 |
case FcTypeInteger: |
| 181 |
b1 = e1 = value1.u.i; |
| 182 |
break; |
| 183 |
case FcTypeDouble: |
| 184 |
b1 = e1 = value1.u.d; |
| 185 |
break; |
| 186 |
case FcTypeRange: |
| 187 |
b1 = value1.u.r->begin; |
| 188 |
e1 = value1.u.r->end; |
| 189 |
break; |
| 190 |
default: |
| 191 |
return -1; |
| 192 |
} |
| 193 |
switch ((int)value2.type) { |
| 194 |
case FcTypeInteger: |
| 195 |
b2 = e2 = value2.u.i; |
| 196 |
break; |
| 197 |
case FcTypeDouble: |
| 198 |
b2 = e2 = value2.u.d; |
| 199 |
break; |
| 200 |
case FcTypeRange: |
| 201 |
b2 = value2.u.r->begin; |
| 202 |
e2 = value2.u.r->end; |
| 203 |
break; |
| 204 |
default: |
| 205 |
return -1; |
| 206 |
} |
| 207 |
|
| 208 |
if (e1 < b2) |
| 209 |
d = b2; |
| 210 |
else if (e2 < b1) |
| 211 |
d = e2; |
| 212 |
else |
| 213 |
d = (FC_MAX (b1, b2) + FC_MIN (e1, e2)) * .5; |
| 214 |
|
| 215 |
bestValue->type = FcTypeDouble; |
| 216 |
bestValue->u.d = d; |
| 217 |
|
| 218 |
/* If the ranges overlap, it's a match, otherwise return closest distance. */ |
| 219 |
if (e1 < b2 || e2 < b1) |
| 220 |
return FC_MIN (fabs (b2 - e1), fabs (b1 - e2)); |
| 221 |
else |
| 222 |
return 0.0; |
| 223 |
} |
| 224 |
|
| 225 |
static double |
| 226 |
FcCompareSize (const FcValue *v1, const FcValue *v2, FcValue *bestValue) |
| 227 |
{ |
| 228 |
FcValue value1 = FcValueCanonicalize (v1); |
| 229 |
FcValue value2 = FcValueCanonicalize (v2); |
| 230 |
double b1, e1, b2, e2; |
| 231 |
|
| 232 |
switch ((int)value1.type) { |
| 233 |
case FcTypeInteger: |
| 234 |
b1 = e1 = value1.u.i; |
| 235 |
break; |
| 236 |
case FcTypeDouble: |
| 237 |
b1 = e1 = value1.u.d; |
| 238 |
break; |
| 239 |
case FcTypeRange: |
| 240 |
b1 = value1.u.r->begin; |
| 241 |
e1 = value1.u.r->end; |
| 242 |
break; |
| 243 |
default: |
| 244 |
return -1; |
| 245 |
} |
| 246 |
switch ((int)value2.type) { |
| 247 |
case FcTypeInteger: |
| 248 |
b2 = e2 = value2.u.i; |
| 249 |
break; |
| 250 |
case FcTypeDouble: |
| 251 |
b2 = e2 = value2.u.d; |
| 252 |
break; |
| 253 |
case FcTypeRange: |
| 254 |
b2 = value2.u.r->begin; |
| 255 |
e2 = value2.u.r->end; |
| 256 |
break; |
| 257 |
default: |
| 258 |
return -1; |
| 259 |
} |
| 260 |
|
| 261 |
bestValue->type = FcTypeDouble; |
| 262 |
bestValue->u.d = (b1 + e1) * .5; |
| 263 |
|
| 264 |
/* If the ranges overlap, it's a match, otherwise return closest distance. */ |
| 265 |
if (e1 < b2 || e2 < b1) |
| 266 |
return FC_MIN (fabs (b2 - e1), fabs (b1 - e2)); |
| 267 |
if (b2 != e2 && b1 == e2) /* Semi-closed interval. */ |
| 268 |
return 1e-15; |
| 269 |
else |
| 270 |
return 0.0; |
| 271 |
} |
| 272 |
|
| 273 |
static double |
| 274 |
FcCompareFilename (const FcValue *v1, const FcValue *v2, FcValue *bestValue) |
| 275 |
{ |
| 276 |
const FcChar8 *s1 = FcValueString (v1), *s2 = FcValueString (v2); |
| 277 |
*bestValue = FcValueCanonicalize (v2); |
| 278 |
if (FcStrCmp (s1, s2) == 0) |
| 279 |
return 0.0; |
| 280 |
else if (FcStrCmpIgnoreCase (s1, s2) == 0) |
| 281 |
return 1.0; |
| 282 |
else if (FcStrGlobMatch (s1, s2)) |
| 283 |
return 2.0; |
| 284 |
else |
| 285 |
return 3.0; |
| 286 |
} |
| 287 |
|
| 288 |
/* Define priorities to -1 for objects that don't have a compare function. */ |
| 289 |
|
| 290 |
#define PRI_NULL(n) \ |
| 291 |
PRI_##n##_STRONG = -1, \ |
| 292 |
PRI_##n##_WEAK = -1, |
| 293 |
#define PRI1(n) |
| 294 |
#define PRI_FcCompareFamily(n) PRI1 (n) |
| 295 |
#define PRI_FcCompareString(n) PRI1 (n) |
| 296 |
#define PRI_FcCompareNumber(n) PRI1 (n) |
| 297 |
#define PRI_FcCompareBool(n) PRI1 (n) |
| 298 |
#define PRI_FcCompareFilename(n) PRI1 (n) |
| 299 |
#define PRI_FcCompareCharSet(n) PRI1 (n) |
| 300 |
#define PRI_FcCompareLang(n) PRI1 (n) |
| 301 |
#define PRI_FcComparePostScript(n) PRI1 (n) |
| 302 |
#define PRI_FcCompareRange(n) PRI1 (n) |
| 303 |
#define PRI_FcCompareSize(n) PRI1 (n) |
| 304 |
|
| 305 |
#define FC_OBJECT(NAME, Type, Cmp) PRI_##Cmp (NAME) |
| 306 |
|
| 307 |
typedef enum _FcMatcherPriorityDummy { |
| 308 |
#include "fcobjs.h" |
| 309 |
} FcMatcherPriorityDummy; |
| 310 |
|
| 311 |
#undef FC_OBJECT |
| 312 |
|
| 313 |
/* Canonical match priority order. */ |
| 314 |
|
| 315 |
#undef PRI1 |
| 316 |
#define PRI1(n) \ |
| 317 |
PRI_##n, \ |
| 318 |
PRI_##n##_STRONG = PRI_##n, \ |
| 319 |
PRI_##n##_WEAK = PRI_##n |
| 320 |
|
| 321 |
typedef enum _FcMatcherPriority { |
| 322 |
PRI1 (FILE), |
| 323 |
PRI1 (FONT_WRAPPER), |
| 324 |
PRI1 (FONTFORMAT), |
| 325 |
PRI1 (VARIABLE), |
| 326 |
PRI1 (NAMED_INSTANCE), |
| 327 |
PRI1 (SCALABLE), |
| 328 |
PRI1 (COLOR), |
| 329 |
PRI1 (FOUNDRY), |
| 330 |
PRI1 (CHARSET), |
| 331 |
PRI_FAMILY_STRONG, |
| 332 |
PRI_POSTSCRIPT_NAME_STRONG, |
| 333 |
PRI1 (LANG), |
| 334 |
PRI_FAMILY_WEAK, |
| 335 |
PRI_POSTSCRIPT_NAME_WEAK, |
| 336 |
PRI1 (SYMBOL), |
| 337 |
PRI1 (SPACING), |
| 338 |
PRI1 (SIZE), |
| 339 |
PRI1 (PIXEL_SIZE), |
| 340 |
PRI1 (STYLE), |
| 341 |
PRI1 (SLANT), |
| 342 |
PRI1 (WEIGHT), |
| 343 |
PRI1 (WIDTH), |
| 344 |
PRI1 (FONT_HAS_HINT), |
| 345 |
PRI1 (DECORATIVE), |
| 346 |
PRI1 (ANTIALIAS), |
| 347 |
PRI1 (RASTERIZER), |
| 348 |
PRI1 (OUTLINE), |
| 349 |
PRI1 (ORDER), |
| 350 |
PRI1 (FONTVERSION), |
| 351 |
PRI_END |
| 352 |
} FcMatcherPriority; |
| 353 |
|
| 354 |
#undef PRI1 |
| 355 |
|
| 356 |
typedef struct _FcMatcher { |
| 357 |
FcObject object; |
| 358 |
double (*compare) (const FcValue *v1, const FcValue *v2, FcValue *bestValue); |
| 359 |
int strong, weak; |
| 360 |
} FcMatcher; |
| 361 |
|
| 362 |
/* |
| 363 |
* Order is significant, it defines the precedence of |
| 364 |
* each value, earlier values are more significant than |
| 365 |
* later values |
| 366 |
*/ |
| 367 |
#define FC_OBJECT(NAME, Type, Cmp) { FC_##NAME##_OBJECT, Cmp, PRI_##NAME##_STRONG, PRI_##NAME##_WEAK }, |
| 368 |
static const FcMatcher _FcMatchers[] = { |
| 369 |
{ FC_INVALID_OBJECT, NULL, -1, -1 }, |
| 370 |
#include "fcobjs.h" |
| 371 |
}; |
| 372 |
#undef FC_OBJECT |
| 373 |
|
| 374 |
static const FcMatcher * |
| 375 |
FcObjectToMatcher (FcObject object, |
| 376 |
FcBool include_lang) |
| 377 |
{ |
| 378 |
if (include_lang) { |
| 379 |
switch (object) { |
| 380 |
case FC_FAMILYLANG_OBJECT: |
| 381 |
case FC_STYLELANG_OBJECT: |
| 382 |
case FC_FULLNAMELANG_OBJECT: |
| 383 |
object = FC_LANG_OBJECT; |
| 384 |
break; |
| 385 |
} |
| 386 |
} |
| 387 |
if (object > FC_MAX_BASE_OBJECT || |
| 388 |
!_FcMatchers[object].compare || |
| 389 |
_FcMatchers[object].strong == -1 || |
| 390 |
_FcMatchers[object].weak == -1) |
| 391 |
return NULL; |
| 392 |
|
| 393 |
return _FcMatchers + object; |
| 394 |
} |
| 395 |
|
| 396 |
static FcBool |
| 397 |
FcCompareValueList (FcObject object, |
| 398 |
const FcMatcher *match, |
| 399 |
FcValueListPtr v1orig, /* pattern */ |
| 400 |
FcValueListPtr v2orig, /* target */ |
| 401 |
FcValue *bestValue, |
| 402 |
double *value, |
| 403 |
int *n, |
| 404 |
FcResult *result) |
| 405 |
{ |
| 406 |
FcValueListPtr v1, v2; |
| 407 |
double v, best, bestStrong, bestWeak; |
| 408 |
int j, k, pos = 0; |
| 409 |
int weak, strong; |
| 410 |
|
| 411 |
if (!match) { |
| 412 |
if (bestValue) |
| 413 |
*bestValue = FcValueCanonicalize (&v2orig->value); |
| 414 |
if (n) |
| 415 |
*n = 0; |
| 416 |
return FcTrue; |
| 417 |
} |
| 418 |
|
| 419 |
weak = match->weak; |
| 420 |
strong = match->strong; |
| 421 |
|
| 422 |
best = 1e99; |
| 423 |
bestStrong = 1e99; |
| 424 |
bestWeak = 1e99; |
| 425 |
for (v1 = v1orig, j = 0; v1; v1 = FcValueListNext (v1), j++) { |
| 426 |
for (v2 = v2orig, k = 0; v2; v2 = FcValueListNext (v2), k++) { |
| 427 |
FcValue matchValue; |
| 428 |
v = (match->compare) (&v1->value, &v2->value, &matchValue); |
| 429 |
if (v < 0) { |
| 430 |
*result = FcResultTypeMismatch; |
| 431 |
return FcFalse; |
| 432 |
} |
| 433 |
v = v * 1000 + j * 100 + k * (v2->value.type == FcTypeString ? 1 : 0); |
| 434 |
if (v < best) { |
| 435 |
if (bestValue) |
| 436 |
*bestValue = matchValue; |
| 437 |
best = v; |
| 438 |
pos = k; |
| 439 |
} |
| 440 |
if (weak == strong) { |
| 441 |
/* found the best possible match */ |
| 442 |
if (best < 1000) |
| 443 |
goto done; |
| 444 |
} else if (v1->binding == FcValueBindingStrong) { |
| 445 |
if (v < bestStrong) |
| 446 |
bestStrong = v; |
| 447 |
} else { |
| 448 |
if (v < bestWeak) |
| 449 |
bestWeak = v; |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
done: |
| 454 |
if (FcDebug() & FC_DBG_MATCHV) { |
| 455 |
printf (" %s: %g ", FcObjectName (object), best); |
| 456 |
FcValueListPrint (v1orig); |
| 457 |
printf (", "); |
| 458 |
FcValueListPrint (v2orig); |
| 459 |
printf ("\n"); |
| 460 |
} |
| 461 |
if (value) { |
| 462 |
if (weak == strong) |
| 463 |
value[strong] += best; |
| 464 |
else { |
| 465 |
value[weak] += bestWeak; |
| 466 |
value[strong] += bestStrong; |
| 467 |
} |
| 468 |
} |
| 469 |
if (n) |
| 470 |
*n = pos; |
| 471 |
|
| 472 |
return FcTrue; |
| 473 |
} |
| 474 |
|
| 475 |
/* The bulk of the time in FcFontMatch and FcFontSort goes to |
| 476 |
* walking long lists of family names. We speed this up with a |
| 477 |
* hash table. |
| 478 |
*/ |
| 479 |
typedef struct |
| 480 |
{ |
| 481 |
double strong_value; |
| 482 |
double weak_value; |
| 483 |
} FamilyEntry; |
| 484 |
|
| 485 |
typedef struct |
| 486 |
{ |
| 487 |
FcHashTable *family_hash; |
| 488 |
} FcCompareData; |
| 489 |
|
| 490 |
static void |
| 491 |
FcCompareDataClear (FcCompareData *data) |
| 492 |
{ |
| 493 |
FcHashTableDestroy (data->family_hash); |
| 494 |
} |
| 495 |
|
| 496 |
static void |
| 497 |
FcCompareDataInit (FcPattern *pat, |
| 498 |
FcCompareData *data) |
| 499 |
{ |
| 500 |
FcHashTable *table; |
| 501 |
FcPatternElt *elt; |
| 502 |
FcValueListPtr l; |
| 503 |
int i; |
| 504 |
const void *key; |
| 505 |
FamilyEntry *e; |
| 506 |
|
| 507 |
table = FcHashTableCreate ((FcHashFunc)FcStrHashIgnoreBlanksAndCase, |
| 508 |
(FcCompareFunc)FcStrCmpIgnoreBlanksAndCase, |
| 509 |
NULL, |
| 510 |
NULL, |
| 511 |
NULL, |
| 512 |
free); |
| 513 |
|
| 514 |
elt = FcPatternObjectFindElt (pat, FC_FAMILY_OBJECT); |
| 515 |
if (elt) { |
| 516 |
for (l = FcPatternEltValues (elt), i = 0; l; l = FcValueListNext (l), i++) { |
| 517 |
key = FcValueString (&l->value); |
| 518 |
if (!FcHashTableFind (table, key, (void **)&e)) { |
| 519 |
e = malloc (sizeof (FamilyEntry)); |
| 520 |
e->strong_value = 1e99; |
| 521 |
e->weak_value = 1e99; |
| 522 |
FcHashTableAdd (table, (void *)key, e); |
| 523 |
} |
| 524 |
if (l->binding == FcValueBindingWeak) { |
| 525 |
if (i < e->weak_value) |
| 526 |
e->weak_value = i; |
| 527 |
} else { |
| 528 |
if (i < e->strong_value) |
| 529 |
e->strong_value = i; |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
data->family_hash = table; |
| 535 |
} |
| 536 |
|
| 537 |
static FcBool |
| 538 |
FcCompareFamilies (FcPattern *pat, |
| 539 |
FcValueListPtr v1orig, |
| 540 |
FcPattern *fnt, |
| 541 |
FcValueListPtr v2orig, |
| 542 |
double *value, |
| 543 |
FcResult *result, |
| 544 |
FcHashTable *table) |
| 545 |
{ |
| 546 |
FcValueListPtr v2; |
| 547 |
double strong_value; |
| 548 |
double weak_value; |
| 549 |
const void *key; |
| 550 |
FamilyEntry *e; |
| 551 |
|
| 552 |
assert (table != NULL); |
| 553 |
|
| 554 |
strong_value = 1e99; |
| 555 |
weak_value = 1e99; |
| 556 |
|
| 557 |
for (v2 = v2orig; v2; v2 = FcValueListNext (v2)) { |
| 558 |
key = FcValueString (&v2->value); |
| 559 |
if (FcHashTableFind (table, key, (void **)&e)) { |
| 560 |
if (e->strong_value < strong_value) |
| 561 |
strong_value = e->strong_value; |
| 562 |
if (e->weak_value < weak_value) |
| 563 |
weak_value = e->weak_value; |
| 564 |
} |
| 565 |
} |
| 566 |
if (FcDebug() & FC_DBG_MATCHV) { |
| 567 |
printf ("%s: %g (%g) ", FcObjectName (FC_FAMILY_OBJECT), strong_value, weak_value); |
| 568 |
FcValueListPrint (v1orig); |
| 569 |
printf (", "); |
| 570 |
FcValueListPrint (v2orig); |
| 571 |
printf ("\n"); |
| 572 |
} |
| 573 |
|
| 574 |
value[PRI_FAMILY_STRONG] = strong_value; |
| 575 |
value[PRI_FAMILY_WEAK] = weak_value; |
| 576 |
|
| 577 |
return FcTrue; |
| 578 |
} |
| 579 |
|
| 580 |
/* |
| 581 |
* Return a value indicating the distance between the two lists of |
| 582 |
* values |
| 583 |
*/ |
| 584 |
|
| 585 |
static FcBool |
| 586 |
FcCompare (FcPattern *pat, |
| 587 |
FcPattern *fnt, |
| 588 |
double *value, |
| 589 |
FcResult *result, |
| 590 |
FcCompareData *data) |
| 591 |
{ |
| 592 |
int i, i1, i2; |
| 593 |
|
| 594 |
for (i = 0; i < PRI_END; i++) |
| 595 |
value[i] = 0.0; |
| 596 |
|
| 597 |
i1 = 0; |
| 598 |
i2 = 0; |
| 599 |
while (i1 < pat->num && i2 < fnt->num) { |
| 600 |
FcPatternElt *elt_i1 = &FcPatternElts (pat)[i1]; |
| 601 |
FcPatternElt *elt_i2 = &FcPatternElts (fnt)[i2]; |
| 602 |
|
| 603 |
i = FcObjectCompare (elt_i1->object, elt_i2->object); |
| 604 |
if (i > 0) |
| 605 |
i2++; |
| 606 |
else if (i < 0) |
| 607 |
i1++; |
| 608 |
else if (elt_i1->object == FC_FAMILY_OBJECT && data->family_hash) { |
| 609 |
if (!FcCompareFamilies (pat, FcPatternEltValues (elt_i1), |
| 610 |
fnt, FcPatternEltValues (elt_i2), |
| 611 |
value, result, |
| 612 |
data->family_hash)) |
| 613 |
return FcFalse; |
| 614 |
i1++; |
| 615 |
i2++; |
| 616 |
} else { |
| 617 |
const FcMatcher *match = FcObjectToMatcher (elt_i1->object, FcFalse); |
| 618 |
if (!FcCompareValueList (elt_i1->object, match, |
| 619 |
FcPatternEltValues (elt_i1), |
| 620 |
FcPatternEltValues (elt_i2), |
| 621 |
NULL, value, NULL, result)) |
| 622 |
return FcFalse; |
| 623 |
i1++; |
| 624 |
i2++; |
| 625 |
} |
| 626 |
} |
| 627 |
return FcTrue; |
| 628 |
} |
| 629 |
|
| 630 |
FcPattern * |
| 631 |
FcFontRenderPrepare (FcConfig *config, |
| 632 |
FcPattern *pat, |
| 633 |
FcPattern *font) |
| 634 |
{ |
| 635 |
FcPattern *newp; |
| 636 |
int i; |
| 637 |
FcPatternElt *fe, *pe; |
| 638 |
FcValue v; |
| 639 |
FcResult result; |
| 640 |
FcBool variable = FcFalse; |
| 641 |
FcStrBuf variations; |
| 642 |
|
| 643 |
assert (pat != NULL); |
| 644 |
assert (font != NULL); |
| 645 |
|
| 646 |
FcPatternObjectGetBool (font, FC_VARIABLE_OBJECT, 0, &variable); |
| 647 |
assert (variable != FcDontCare); |
| 648 |
if (variable) |
| 649 |
FcStrBufInit (&variations, NULL, 0); |
| 650 |
|
| 651 |
newp = FcPatternCreate(); |
| 652 |
if (!newp) |
| 653 |
return NULL; |
| 654 |
for (i = 0; i < font->num; i++) { |
| 655 |
fe = &FcPatternElts (font)[i]; |
| 656 |
if (fe->object == FC_FAMILYLANG_OBJECT || |
| 657 |
fe->object == FC_STYLELANG_OBJECT || |
| 658 |
fe->object == FC_FULLNAMELANG_OBJECT) { |
| 659 |
/* ignore those objects. we need to deal with them |
| 660 |
* another way */ |
| 661 |
continue; |
| 662 |
} |
| 663 |
if (fe->object == FC_FAMILY_OBJECT || |
| 664 |
fe->object == FC_STYLE_OBJECT || |
| 665 |
fe->object == FC_FULLNAME_OBJECT) { |
| 666 |
FcPatternElt *fel, *pel; |
| 667 |
|
| 668 |
FC_ASSERT_STATIC ((FC_FAMILY_OBJECT + 1) == FC_FAMILYLANG_OBJECT); |
| 669 |
FC_ASSERT_STATIC ((FC_STYLE_OBJECT + 1) == FC_STYLELANG_OBJECT); |
| 670 |
FC_ASSERT_STATIC ((FC_FULLNAME_OBJECT + 1) == FC_FULLNAMELANG_OBJECT); |
| 671 |
|
| 672 |
fel = FcPatternObjectFindElt (font, fe->object + 1); |
| 673 |
pel = FcPatternObjectFindElt (pat, fe->object + 1); |
| 674 |
|
| 675 |
if (fel && pel) { |
| 676 |
/* The font has name languages, and pattern asks for specific language(s). |
| 677 |
* Match on language and and prefer that result. |
| 678 |
* Note: Currently the code only give priority to first matching language. |
| 679 |
*/ |
| 680 |
int n = 1, j; |
| 681 |
FcValueListPtr l1, l2, ln = NULL, ll = NULL; |
| 682 |
const FcMatcher *match = FcObjectToMatcher (pel->object, FcTrue); |
| 683 |
|
| 684 |
if (!FcCompareValueList (pel->object, match, |
| 685 |
FcPatternEltValues (pel), |
| 686 |
FcPatternEltValues (fel), NULL, NULL, &n, &result)) { |
| 687 |
FcPatternDestroy (newp); |
| 688 |
return NULL; |
| 689 |
} |
| 690 |
|
| 691 |
for (j = 0, l1 = FcPatternEltValues (fe), l2 = FcPatternEltValues (fel); |
| 692 |
l1 != NULL || l2 != NULL; |
| 693 |
j++, l1 = l1 ? FcValueListNext (l1) : NULL, l2 = l2 ? FcValueListNext (l2) : NULL) { |
| 694 |
FcValueListPtr (*func) (FcValueListPtr, FcValue, FcValueBinding); |
| 695 |
FcValueBinding binding = FcValueBindingEnd; |
| 696 |
|
| 697 |
if (j == n) { |
| 698 |
binding = FcValueBindingStrong; |
| 699 |
func = FcValueListPrepend; |
| 700 |
} else |
| 701 |
func = FcValueListAppend; |
| 702 |
if (l1) { |
| 703 |
ln = func (ln, |
| 704 |
FcValueCanonicalize (&l1->value), |
| 705 |
l1->binding); |
| 706 |
} |
| 707 |
if (l2) { |
| 708 |
if (binding == FcValueBindingEnd) |
| 709 |
binding = l2->binding; |
| 710 |
ll = func (ll, |
| 711 |
FcValueCanonicalize (&l2->value), |
| 712 |
binding); |
| 713 |
} |
| 714 |
} |
| 715 |
FcPatternObjectListAdd (newp, fe->object, ln, FcFalse); |
| 716 |
FcPatternObjectListAdd (newp, fel->object, ll, FcFalse); |
| 717 |
|
| 718 |
continue; |
| 719 |
} else if (fel) { |
| 720 |
/* Pattern doesn't ask for specific language. Copy all for name and |
| 721 |
* lang. */ |
| 722 |
FcValueListPtr l1, l2; |
| 723 |
|
| 724 |
l1 = FcValueListDuplicate (FcPatternEltValues (fe)); |
| 725 |
l2 = FcValueListDuplicate (FcPatternEltValues (fel)); |
| 726 |
FcPatternObjectListAdd (newp, fe->object, l1, FcFalse); |
| 727 |
FcPatternObjectListAdd (newp, fel->object, l2, FcFalse); |
| 728 |
|
| 729 |
continue; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
pe = FcPatternObjectFindElt (pat, fe->object); |
| 734 |
if (pe) { |
| 735 |
const FcMatcher *match = FcObjectToMatcher (pe->object, FcFalse); |
| 736 |
if (!FcCompareValueList (pe->object, match, |
| 737 |
FcPatternEltValues (pe), |
| 738 |
FcPatternEltValues (fe), &v, NULL, NULL, &result)) { |
| 739 |
FcPatternDestroy (newp); |
| 740 |
return NULL; |
| 741 |
} |
| 742 |
FcPatternObjectAdd (newp, fe->object, v, FcFalse); |
| 743 |
|
| 744 |
/* Set font-variations settings for standard axes in variable fonts. */ |
| 745 |
if (variable && |
| 746 |
FcPatternEltValues (fe)->value.type == FcTypeRange && |
| 747 |
(fe->object == FC_WEIGHT_OBJECT || |
| 748 |
fe->object == FC_WIDTH_OBJECT || |
| 749 |
fe->object == FC_SIZE_OBJECT)) { |
| 750 |
double num; |
| 751 |
FcChar8 temp[128]; |
| 752 |
const char *tag = " "; |
| 753 |
assert (v.type == FcTypeDouble); |
| 754 |
num = v.u.d; |
| 755 |
if (variations.len) |
| 756 |
FcStrBufChar (&variations, ','); |
| 757 |
switch (fe->object) { |
| 758 |
case FC_WEIGHT_OBJECT: |
| 759 |
tag = "wght"; |
| 760 |
num = FcWeightToOpenType (num); |
| 761 |
break; |
| 762 |
|
| 763 |
case FC_WIDTH_OBJECT: |
| 764 |
tag = "wdth"; |
| 765 |
break; |
| 766 |
|
| 767 |
case FC_SIZE_OBJECT: |
| 768 |
tag = "opsz"; |
| 769 |
break; |
| 770 |
} |
| 771 |
sprintf ((char *)temp, "%4s=%g", tag, num); |
| 772 |
FcStrBufString (&variations, temp); |
| 773 |
} |
| 774 |
} else { |
| 775 |
FcPatternObjectListAdd (newp, fe->object, |
| 776 |
FcValueListDuplicate (FcPatternEltValues (fe)), |
| 777 |
FcTrue); |
| 778 |
} |
| 779 |
} |
| 780 |
for (i = 0; i < pat->num; i++) { |
| 781 |
pe = &FcPatternElts (pat)[i]; |
| 782 |
fe = FcPatternObjectFindElt (font, pe->object); |
| 783 |
if (!fe && |
| 784 |
pe->object != FC_FAMILYLANG_OBJECT && |
| 785 |
pe->object != FC_STYLELANG_OBJECT && |
| 786 |
pe->object != FC_FULLNAMELANG_OBJECT) { |
| 787 |
FcPatternObjectListAdd (newp, pe->object, |
| 788 |
FcValueListDuplicate (FcPatternEltValues (pe)), |
| 789 |
FcFalse); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
if (variable && variations.len) { |
| 794 |
FcChar8 *vars = NULL; |
| 795 |
if (FcPatternObjectGetString (newp, FC_FONT_VARIATIONS_OBJECT, 0, &vars) == FcResultMatch) { |
| 796 |
FcStrBufChar (&variations, ','); |
| 797 |
FcStrBufString (&variations, vars); |
| 798 |
FcPatternObjectDel (newp, FC_FONT_VARIATIONS_OBJECT); |
| 799 |
} |
| 800 |
|
| 801 |
FcPatternObjectAddString (newp, FC_FONT_VARIATIONS_OBJECT, FcStrBufDoneStatic (&variations)); |
| 802 |
FcStrBufDestroy (&variations); |
| 803 |
} |
| 804 |
|
| 805 |
FcConfigSubstituteWithPat (config, newp, pat, FcMatchFont); |
| 806 |
return newp; |
| 807 |
} |
| 808 |
|
| 809 |
static FcPattern * |
| 810 |
FcFontSetMatchInternal (FcFontSet **sets, |
| 811 |
int nsets, |
| 812 |
FcPattern *p, |
| 813 |
FcResult *result) |
| 814 |
{ |
| 815 |
double score[PRI_END], bestscore[PRI_END]; |
| 816 |
int f; |
| 817 |
FcFontSet *s; |
| 818 |
FcPattern *best, *pat = NULL; |
| 819 |
int i; |
| 820 |
int set; |
| 821 |
FcCompareData data; |
| 822 |
const FcPatternElt *elt; |
| 823 |
|
| 824 |
for (i = 0; i < PRI_END; i++) |
| 825 |
bestscore[i] = 0; |
| 826 |
best = 0; |
| 827 |
if (FcDebug() & FC_DBG_MATCH) { |
| 828 |
printf ("Match "); |
| 829 |
FcPatternPrint (p); |
| 830 |
} |
| 831 |
|
| 832 |
FcCompareDataInit (p, &data); |
| 833 |
|
| 834 |
for (set = 0; set < nsets; set++) { |
| 835 |
s = sets[set]; |
| 836 |
if (!s) |
| 837 |
continue; |
| 838 |
for (f = 0; f < s->nfont; f++) { |
| 839 |
if (FcDebug() & FC_DBG_MATCHV) { |
| 840 |
printf ("Font %d ", f); |
| 841 |
FcPatternPrint (s->fonts[f]); |
| 842 |
} |
| 843 |
if (!FcCompare (p, s->fonts[f], score, result, &data)) { |
| 844 |
FcCompareDataClear (&data); |
| 845 |
return 0; |
| 846 |
} |
| 847 |
if (FcDebug() & FC_DBG_MATCHV) { |
| 848 |
printf ("Score"); |
| 849 |
for (i = 0; i < PRI_END; i++) { |
| 850 |
printf (" %g", score[i]); |
| 851 |
} |
| 852 |
printf ("\n"); |
| 853 |
} |
| 854 |
for (i = 0; i < PRI_END; i++) { |
| 855 |
if (best && bestscore[i] < score[i]) |
| 856 |
break; |
| 857 |
if (!best || score[i] < bestscore[i]) { |
| 858 |
for (i = 0; i < PRI_END; i++) |
| 859 |
bestscore[i] = score[i]; |
| 860 |
best = s->fonts[f]; |
| 861 |
break; |
| 862 |
} |
| 863 |
} |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
FcCompareDataClear (&data); |
| 868 |
|
| 869 |
/* Update the binding according to the score to indicate how exactly values matches on. */ |
| 870 |
if (best) { |
| 871 |
pat = FcPatternCreate(); |
| 872 |
elt = FcPatternElts (best); |
| 873 |
for (i = 0; i < FcPatternObjectCount (best); i++) { |
| 874 |
const FcMatcher *match = FcObjectToMatcher (elt[i].object, FcFalse); |
| 875 |
FcValueListPtr l = FcPatternEltValues (&elt[i]); |
| 876 |
|
| 877 |
if (!match) |
| 878 |
FcPatternObjectListAdd (pat, elt[i].object, |
| 879 |
FcValueListDuplicate (l), FcTrue); |
| 880 |
else { |
| 881 |
FcValueBinding binding = FcValueBindingWeak; |
| 882 |
FcValueListPtr newp = NULL, ll, t = NULL; |
| 883 |
FcValue v; |
| 884 |
|
| 885 |
/* If the value was matched exactly, update the binding to Strong. */ |
| 886 |
if (bestscore[match->strong] < 1000) |
| 887 |
binding = FcValueBindingStrong; |
| 888 |
|
| 889 |
for (ll = l; ll != NULL; ll = FcValueListNext (ll)) { |
| 890 |
if (!newp) { |
| 891 |
t = newp = FcValueListCreate(); |
| 892 |
} else { |
| 893 |
t->next = FcValueListCreate(); |
| 894 |
t = FcValueListNext (t); |
| 895 |
} |
| 896 |
v = FcValueCanonicalize (&ll->value); |
| 897 |
t->value = FcValueSave (v); |
| 898 |
t->binding = binding; |
| 899 |
t->next = NULL; |
| 900 |
} |
| 901 |
FcPatternObjectListAdd (pat, elt[i].object, newp, FcTrue); |
| 902 |
} |
| 903 |
} |
| 904 |
} |
| 905 |
if (FcDebug() & FC_DBG_MATCH) { |
| 906 |
printf ("Best score"); |
| 907 |
for (i = 0; i < PRI_END; i++) |
| 908 |
printf (" %g", bestscore[i]); |
| 909 |
printf ("\n"); |
| 910 |
FcPatternPrint (pat); |
| 911 |
} |
| 912 |
if (FcDebug() & FC_DBG_MATCH2) { |
| 913 |
char *env = getenv ("FC_DBG_MATCH_FILTER"); |
| 914 |
FcObjectSet *os = NULL; |
| 915 |
|
| 916 |
if (env) { |
| 917 |
char *ss, *s; |
| 918 |
char *p; |
| 919 |
FcBool f = FcTrue; |
| 920 |
|
| 921 |
ss = s = strdup (env); |
| 922 |
os = FcObjectSetCreate(); |
| 923 |
while (f) { |
| 924 |
size_t len; |
| 925 |
char *x; |
| 926 |
|
| 927 |
if (!(p = strchr (s, ','))) { |
| 928 |
f = FcFalse; |
| 929 |
len = strlen (s); |
| 930 |
} else { |
| 931 |
len = (p - s); |
| 932 |
} |
| 933 |
x = malloc (sizeof (char) * (len + 1)); |
| 934 |
if (x) { |
| 935 |
strcpy (x, s); |
| 936 |
if (FcObjectFromName (x) > 0) |
| 937 |
FcObjectSetAdd (os, x); |
| 938 |
s = p + 1; |
| 939 |
free (x); |
| 940 |
} |
| 941 |
} |
| 942 |
free (ss); |
| 943 |
} |
| 944 |
FcPatternPrint2 (p, pat, os); |
| 945 |
if (os) |
| 946 |
FcObjectSetDestroy (os); |
| 947 |
} |
| 948 |
/* assuming that 'result' is initialized with FcResultNoMatch |
| 949 |
* outside this function */ |
| 950 |
if (pat) |
| 951 |
*result = FcResultMatch; |
| 952 |
|
| 953 |
return pat; |
| 954 |
} |
| 955 |
|
| 956 |
FcPattern * |
| 957 |
FcFontSetMatch (FcConfig *config, |
| 958 |
FcFontSet **sets, |
| 959 |
int nsets, |
| 960 |
FcPattern *p, |
| 961 |
FcResult *result) |
| 962 |
{ |
| 963 |
FcPattern *best, *ret = NULL; |
| 964 |
|
| 965 |
assert (sets != NULL); |
| 966 |
assert (p != NULL); |
| 967 |
assert (result != NULL); |
| 968 |
|
| 969 |
*result = FcResultNoMatch; |
| 970 |
|
| 971 |
config = FcConfigReference (config); |
| 972 |
if (!config) |
| 973 |
return NULL; |
| 974 |
best = FcFontSetMatchInternal (sets, nsets, p, result); |
| 975 |
if (best) { |
| 976 |
ret = FcFontRenderPrepare (config, p, best); |
| 977 |
FcPatternDestroy (best); |
| 978 |
} |
| 979 |
|
| 980 |
FcConfigDestroy (config); |
| 981 |
|
| 982 |
return ret; |
| 983 |
} |
| 984 |
|
| 985 |
FcPattern * |
| 986 |
FcFontMatch (FcConfig *config, |
| 987 |
FcPattern *p, |
| 988 |
FcResult *result) |
| 989 |
{ |
| 990 |
FcFontSet *sets[2]; |
| 991 |
int nsets; |
| 992 |
FcPattern *best, *ret = NULL; |
| 993 |
|
| 994 |
assert (p != NULL); |
| 995 |
assert (result != NULL); |
| 996 |
|
| 997 |
*result = FcResultNoMatch; |
| 998 |
|
| 999 |
config = FcConfigReference (config); |
| 1000 |
if (!config) |
| 1001 |
return NULL; |
| 1002 |
nsets = 0; |
| 1003 |
if (config->fonts[FcSetSystem]) |
| 1004 |
sets[nsets++] = config->fonts[FcSetSystem]; |
| 1005 |
if (config->fonts[FcSetApplication]) |
| 1006 |
sets[nsets++] = config->fonts[FcSetApplication]; |
| 1007 |
|
| 1008 |
best = FcFontSetMatchInternal (sets, nsets, p, result); |
| 1009 |
if (best) { |
| 1010 |
ret = FcFontRenderPrepare (config, p, best); |
| 1011 |
FcPatternDestroy (best); |
| 1012 |
} |
| 1013 |
|
| 1014 |
FcConfigDestroy (config); |
| 1015 |
|
| 1016 |
return ret; |
| 1017 |
} |
| 1018 |
|
| 1019 |
typedef struct _FcSortNode { |
| 1020 |
FcPattern *pattern; |
| 1021 |
double score[PRI_END]; |
| 1022 |
} FcSortNode; |
| 1023 |
|
| 1024 |
static int |
| 1025 |
FcSortCompare (const void *aa, const void *ab) |
| 1026 |
{ |
| 1027 |
FcSortNode *a = *(FcSortNode **)aa; |
| 1028 |
FcSortNode *b = *(FcSortNode **)ab; |
| 1029 |
double *as = &a->score[0]; |
| 1030 |
double *bs = &b->score[0]; |
| 1031 |
double ad = 0, bd = 0; |
| 1032 |
int i; |
| 1033 |
|
| 1034 |
i = PRI_END; |
| 1035 |
while (i-- && (ad = *as++) == (bd = *bs++)) |
| 1036 |
; |
| 1037 |
return ad < bd ? -1 : ad > bd ? 1 |
| 1038 |
: 0; |
| 1039 |
} |
| 1040 |
|
| 1041 |
static FcBool |
| 1042 |
FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **csp, FcBool trim) |
| 1043 |
{ |
| 1044 |
FcBool ret = FcFalse; |
| 1045 |
FcCharSet *cs; |
| 1046 |
int i; |
| 1047 |
|
| 1048 |
cs = 0; |
| 1049 |
if (trim || csp) { |
| 1050 |
cs = FcCharSetCreate(); |
| 1051 |
if (cs == NULL) |
| 1052 |
goto bail; |
| 1053 |
} |
| 1054 |
|
| 1055 |
for (i = 0; i < nnode; i++) { |
| 1056 |
FcSortNode *node = *n++; |
| 1057 |
FcBool adds_chars = FcFalse; |
| 1058 |
|
| 1059 |
/* |
| 1060 |
* Only fetch node charset if we'd need it |
| 1061 |
*/ |
| 1062 |
if (cs) { |
| 1063 |
FcCharSet *ncs; |
| 1064 |
|
| 1065 |
if (FcPatternGetCharSet (node->pattern, FC_CHARSET, 0, &ncs) != |
| 1066 |
FcResultMatch) |
| 1067 |
continue; |
| 1068 |
|
| 1069 |
if (!FcCharSetMerge (cs, ncs, &adds_chars)) |
| 1070 |
goto bail; |
| 1071 |
} |
| 1072 |
|
| 1073 |
/* |
| 1074 |
* If this font isn't a subset of the previous fonts, |
| 1075 |
* add it to the list |
| 1076 |
*/ |
| 1077 |
if (!i || !trim || adds_chars) { |
| 1078 |
FcPatternReference (node->pattern); |
| 1079 |
if (FcDebug() & FC_DBG_MATCHV) { |
| 1080 |
printf ("Add "); |
| 1081 |
FcPatternPrint (node->pattern); |
| 1082 |
} |
| 1083 |
if (!FcFontSetAdd (fs, node->pattern)) { |
| 1084 |
FcPatternDestroy (node->pattern); |
| 1085 |
goto bail; |
| 1086 |
} |
| 1087 |
} |
| 1088 |
} |
| 1089 |
if (csp) { |
| 1090 |
*csp = cs; |
| 1091 |
cs = 0; |
| 1092 |
} |
| 1093 |
|
| 1094 |
ret = FcTrue; |
| 1095 |
|
| 1096 |
bail: |
| 1097 |
if (cs) |
| 1098 |
FcCharSetDestroy (cs); |
| 1099 |
|
| 1100 |
return ret; |
| 1101 |
} |
| 1102 |
|
| 1103 |
void |
| 1104 |
FcFontSetSortDestroy (FcFontSet *fs) |
| 1105 |
{ |
| 1106 |
FcFontSetDestroy (fs); |
| 1107 |
} |
| 1108 |
|
| 1109 |
FcFontSet * |
| 1110 |
FcFontSetSort (FcConfig *config, |
| 1111 |
FcFontSet **sets, |
| 1112 |
int nsets, |
| 1113 |
FcPattern *p, |
| 1114 |
FcBool trim, |
| 1115 |
FcCharSet **csp, |
| 1116 |
FcResult *result) |
| 1117 |
{ |
| 1118 |
FcFontSet *ret; |
| 1119 |
FcFontSet *s; |
| 1120 |
FcSortNode *nodes; |
| 1121 |
FcSortNode **nodeps, **nodep; |
| 1122 |
int nnodes; |
| 1123 |
FcSortNode *newp; |
| 1124 |
int set; |
| 1125 |
int f; |
| 1126 |
int i; |
| 1127 |
int nPatternLang; |
| 1128 |
FcBool *patternLangSat; |
| 1129 |
FcValue patternLang; |
| 1130 |
FcCompareData data; |
| 1131 |
|
| 1132 |
assert (sets != NULL); |
| 1133 |
assert (p != NULL); |
| 1134 |
assert (result != NULL); |
| 1135 |
|
| 1136 |
/* There are some implementation that relying on the result of |
| 1137 |
* "result" to check if the return value of FcFontSetSort |
| 1138 |
* is valid or not. |
| 1139 |
* So we should initialize it to the conservative way since |
| 1140 |
* this function doesn't return NULL anymore. |
| 1141 |
*/ |
| 1142 |
if (result) |
| 1143 |
*result = FcResultNoMatch; |
| 1144 |
|
| 1145 |
if (FcDebug() & FC_DBG_MATCH) { |
| 1146 |
printf ("Sort "); |
| 1147 |
FcPatternPrint (p); |
| 1148 |
} |
| 1149 |
nnodes = 0; |
| 1150 |
for (set = 0; set < nsets; set++) { |
| 1151 |
s = sets[set]; |
| 1152 |
if (!s) |
| 1153 |
continue; |
| 1154 |
nnodes += s->nfont; |
| 1155 |
} |
| 1156 |
if (!nnodes) |
| 1157 |
return FcFontSetCreate(); |
| 1158 |
|
| 1159 |
for (nPatternLang = 0; |
| 1160 |
FcPatternGet (p, FC_LANG, nPatternLang, &patternLang) == FcResultMatch; |
| 1161 |
nPatternLang++) |
| 1162 |
; |
| 1163 |
|
| 1164 |
/* freed below */ |
| 1165 |
nodes = malloc (nnodes * sizeof (FcSortNode) + |
| 1166 |
nnodes * sizeof (FcSortNode *) + |
| 1167 |
nPatternLang * sizeof (FcBool)); |
| 1168 |
if (!nodes) |
| 1169 |
goto bail0; |
| 1170 |
nodeps = (FcSortNode **)(nodes + nnodes); |
| 1171 |
patternLangSat = (FcBool *)(nodeps + nnodes); |
| 1172 |
|
| 1173 |
FcCompareDataInit (p, &data); |
| 1174 |
|
| 1175 |
newp = nodes; |
| 1176 |
nodep = nodeps; |
| 1177 |
for (set = 0; set < nsets; set++) { |
| 1178 |
s = sets[set]; |
| 1179 |
if (!s) |
| 1180 |
continue; |
| 1181 |
for (f = 0; f < s->nfont; f++) { |
| 1182 |
if (FcDebug() & FC_DBG_MATCHV) { |
| 1183 |
printf ("Font %d ", f); |
| 1184 |
FcPatternPrint (s->fonts[f]); |
| 1185 |
} |
| 1186 |
newp->pattern = s->fonts[f]; |
| 1187 |
if (!FcCompare (p, newp->pattern, newp->score, result, &data)) |
| 1188 |
goto bail1; |
| 1189 |
/* TODO: Should we check a FcPattern in FcFontSet? |
| 1190 |
* This way may not work if someone has own list of application fonts |
| 1191 |
* That said, just to reduce the cost for lookup so far. |
| 1192 |
*/ |
| 1193 |
if (config->prefer_app_fonts && s != config->fonts[FcSetApplication]) { |
| 1194 |
newp->score[PRI_ORDER] += 1000; |
| 1195 |
} |
| 1196 |
if (FcDebug() & FC_DBG_MATCHV) { |
| 1197 |
printf ("Score"); |
| 1198 |
for (i = 0; i < PRI_END; i++) { |
| 1199 |
printf (" %g", newp->score[i]); |
| 1200 |
} |
| 1201 |
printf ("\n"); |
| 1202 |
} |
| 1203 |
*nodep = newp; |
| 1204 |
newp++; |
| 1205 |
nodep++; |
| 1206 |
} |
| 1207 |
} |
| 1208 |
|
| 1209 |
FcCompareDataClear (&data); |
| 1210 |
|
| 1211 |
nnodes = newp - nodes; |
| 1212 |
|
| 1213 |
qsort (nodeps, nnodes, sizeof (FcSortNode *), |
| 1214 |
FcSortCompare); |
| 1215 |
|
| 1216 |
for (i = 0; i < nPatternLang; i++) |
| 1217 |
patternLangSat[i] = FcFalse; |
| 1218 |
|
| 1219 |
for (f = 0; f < nnodes; f++) { |
| 1220 |
FcBool satisfies = FcFalse; |
| 1221 |
/* |
| 1222 |
* If this node matches any language, go check |
| 1223 |
* which ones and satisfy those entries |
| 1224 |
*/ |
| 1225 |
if (nodeps[f]->score[PRI_LANG] < 2000) { |
| 1226 |
for (i = 0; i < nPatternLang; i++) { |
| 1227 |
FcValue nodeLang; |
| 1228 |
|
| 1229 |
if (!patternLangSat[i] && |
| 1230 |
FcPatternGet (p, FC_LANG, i, &patternLang) == FcResultMatch && |
| 1231 |
FcPatternGet (nodeps[f]->pattern, FC_LANG, 0, &nodeLang) == FcResultMatch) { |
| 1232 |
FcValue matchValue; |
| 1233 |
double compare = FcCompareLang (&patternLang, &nodeLang, &matchValue); |
| 1234 |
if (compare >= 0 && compare < 2) { |
| 1235 |
if (FcDebug() & FC_DBG_MATCHV) { |
| 1236 |
FcChar8 *family; |
| 1237 |
FcChar8 *style; |
| 1238 |
|
| 1239 |
if (FcPatternGetString (nodeps[f]->pattern, FC_FAMILY, 0, &family) == FcResultMatch && |
| 1240 |
FcPatternGetString (nodeps[f]->pattern, FC_STYLE, 0, &style) == FcResultMatch) |
| 1241 |
printf ("Font %s:%s matches language %d\n", family, style, i); |
| 1242 |
} |
| 1243 |
patternLangSat[i] = FcTrue; |
| 1244 |
satisfies = FcTrue; |
| 1245 |
break; |
| 1246 |
} |
| 1247 |
} |
| 1248 |
} |
| 1249 |
} |
| 1250 |
if (!satisfies) { |
| 1251 |
nodeps[f]->score[PRI_LANG] = 10000.0; |
| 1252 |
} |
| 1253 |
} |
| 1254 |
|
| 1255 |
/* |
| 1256 |
* Re-sort once the language issues have been settled |
| 1257 |
*/ |
| 1258 |
qsort (nodeps, nnodes, sizeof (FcSortNode *), |
| 1259 |
FcSortCompare); |
| 1260 |
|
| 1261 |
ret = FcFontSetCreate(); |
| 1262 |
if (!ret) |
| 1263 |
goto bail1; |
| 1264 |
|
| 1265 |
if (!FcSortWalk (nodeps, nnodes, ret, csp, trim)) |
| 1266 |
goto bail2; |
| 1267 |
|
| 1268 |
free (nodes); |
| 1269 |
|
| 1270 |
if (ret->nfont > 0) { |
| 1271 |
*result = FcResultMatch; |
| 1272 |
if (FcDebug() & FC_DBG_MATCH) { |
| 1273 |
printf ("First font "); |
| 1274 |
FcPatternPrint (ret->fonts[0]); |
| 1275 |
} |
| 1276 |
} |
| 1277 |
|
| 1278 |
return ret; |
| 1279 |
|
| 1280 |
bail2: |
| 1281 |
FcFontSetDestroy (ret); |
| 1282 |
bail1: |
| 1283 |
free (nodes); |
| 1284 |
bail0: |
| 1285 |
return 0; |
| 1286 |
} |
| 1287 |
|
| 1288 |
FcFontSet * |
| 1289 |
FcFontSort (FcConfig *config, |
| 1290 |
FcPattern *p, |
| 1291 |
FcBool trim, |
| 1292 |
FcCharSet **csp, |
| 1293 |
FcResult *result) |
| 1294 |
{ |
| 1295 |
FcFontSet *sets[2], *ret; |
| 1296 |
int nsets; |
| 1297 |
|
| 1298 |
assert (p != NULL); |
| 1299 |
assert (result != NULL); |
| 1300 |
|
| 1301 |
*result = FcResultNoMatch; |
| 1302 |
|
| 1303 |
config = FcConfigReference (config); |
| 1304 |
if (!config) |
| 1305 |
return NULL; |
| 1306 |
nsets = 0; |
| 1307 |
if (config->fonts[FcSetSystem]) |
| 1308 |
sets[nsets++] = config->fonts[FcSetSystem]; |
| 1309 |
if (config->fonts[FcSetApplication]) |
| 1310 |
sets[nsets++] = config->fonts[FcSetApplication]; |
| 1311 |
ret = FcFontSetSort (config, sets, nsets, p, trim, csp, result); |
| 1312 |
FcConfigDestroy (config); |
| 1313 |
|
| 1314 |
return ret; |
| 1315 |
} |
| 1316 |
#define __fcmatch__ |
| 1317 |
#include "fcaliastail.h" |
| 1318 |
#undef __fcmatch__ |