matchObjectPtrsInit = FcTrue;
}
-static FcBool
-FcCompareValueList (FcObjectPtr o,
- FcValueListPtr v1orig, /* pattern */
- FcValueListPtr v2orig, /* target */
- FcValue *bestValue,
- double *value,
- FcResult *result)
+static FcMatcher*
+FcObjectPtrToMatcher (FcObjectPtr o)
{
- FcValueListPtr v1, v2;
- FcValueList *v1_ptrU, *v2_ptrU;
- double v, best, bestStrong, bestWeak;
- int i;
- int j;
- const char *object = FcObjectPtrU(o);
+ int i;
+ const char *object = FcObjectPtrU(o);
- /*
- * Locate the possible matching entry by examining the
- * first few characters in object
- */
i = -1;
switch (object[0]) {
case 'f':
i = MATCH_OUTLINE; break;
}
+ if (i < 0)
+ return 0;
+
if (!matchObjectPtrsInit)
FcMatchObjectPtrsInit();
- if (_FcMatchers[i].objectPtr != o) i = -1;
- if (i == -1 ||
- FcStrCmpIgnoreCase ((FcChar8 *) _FcMatchers[i].object,
- (FcChar8 *) object) != 0)
+ if (o != _FcMatchers[i].objectPtr)
+ return 0;
+
+ return _FcMatchers+i;
+}
+
+static FcBool
+FcCompareValueList (FcObjectPtr o,
+ FcValueListPtr v1orig, /* pattern */
+ FcValueListPtr v2orig, /* target */
+ FcValue *bestValue,
+ double *value,
+ FcResult *result)
+{
+ FcValueListPtr v1, v2;
+ FcValueList *v1_ptrU, *v2_ptrU;
+ double v, best, bestStrong, bestWeak;
+ int j;
+ const char *object = FcObjectPtrU(o);
+ FcMatcher *match = FcObjectPtrToMatcher(o);
+
+ if (!match)
{
if (bestValue)
*bestValue = FcValueCanonicalize(&FcValueListPtrU(v2orig)->value);
return FcTrue;
}
+
best = 1e99;
bestStrong = 1e99;
bestWeak = 1e99;
for (v2 = v2orig, v2_ptrU = FcValueListPtrU(v2); v2_ptrU;
v2 = v2_ptrU->next, v2_ptrU = FcValueListPtrU(v2))
{
- v = (*_FcMatchers[i].compare) (&v1_ptrU->value,
- &v2_ptrU->value);
+ v = (match->compare) (&v1_ptrU->value, &v2_ptrU->value);
if (v < 0)
{
*result = FcResultTypeMismatch;
}
if (value)
{
- int weak = _FcMatchers[i].weak;
- int strong = _FcMatchers[i].strong;
+ int weak = match->weak;
+ int strong = match->strong;
if (weak == strong)
value[strong] += best;
else
FcPattern *p,
FcResult *result)
{
- double score[NUM_MATCH_VALUES], bestscore[NUM_MATCH_VALUES];
+ double score;
+ double bestscore;
int f;
FcFontSet *s;
FcPattern *best;
- int i;
+ int scoring_index;
+ int *sets_offset;
int set;
+ int nfonts;
+ int fonts_left;
+ FcMatcher *matcher;
+ FcMatcher *strong_matchers[NUM_MATCH_VALUES];
+ FcMatcher *weak_matchers[NUM_MATCH_VALUES];
+ FcPatternElt *pat_elts[NUM_MATCH_VALUES];
+ int pat_elt;
+ FcBool *match_blocked;
+ int block_start;
+
+ if (!nsets || !sets || !p)
+ {
+ *result = FcResultNoMatch;
+ return 0;
+ }
- for (i = 0; i < NUM_MATCH_VALUES; i++)
- bestscore[i] = 0;
- best = 0;
if (FcDebug () & FC_DBG_MATCH)
{
printf ("Match ");
return 0;
}
}
- for (set = 0; set < nsets; set++)
+
+ sets_offset = (int *)calloc(nsets, sizeof (int));
+
+ nfonts = 0;
+ for (set = 0; set < nsets; ++set)
{
- s = sets[set];
- if (!s)
+ sets_offset[set] = nfonts;
+ if (sets[set])
+ nfonts += sets[set]->nfont;
+ }
+
+ fonts_left = nfonts;
+
+ match_blocked = (FcBool*)calloc(nfonts, sizeof(FcBool));
+
+ /* Find out all necessary matchers first, so we don't need to find them
+ * in every loop.
+ */
+
+ memset(strong_matchers, 0, sizeof (FcMatcher*) * NUM_MATCH_VALUES);
+ memset(weak_matchers, 0, sizeof (FcMatcher*) * NUM_MATCH_VALUES);
+ memset(pat_elts, 0, sizeof (FcPatternElt*) * NUM_MATCH_VALUES);
+
+ for (pat_elt = 0; pat_elt < p->num; ++pat_elt)
+ {
+ matcher = FcObjectPtrToMatcher
+ ((FcPatternEltU(p->elts)+pat_elt)->object);
+ if (matcher)
+ {
+ strong_matchers[matcher->strong] = matcher;
+ weak_matchers[matcher->weak] = matcher;
+ pat_elts [matcher->strong] = pat_elts [matcher->weak] =
+ (FcPatternEltU(p->elts)+pat_elt);
+ }
+ }
+
+ /* The old algorithm checked if each font beat 'best',
+ * scanning all of the value lists for all of the pattern elts. */
+ /* This algorithm checks each font on a element-by-element basis
+ * and blocks fonts that have already lost on some element from
+ * further consideration from being best. Basically, we've
+ * swapped the order of loops and short-circuited fonts that
+ * are out of contention right away.
+ * This saves a lot of time! */
+ best = 0;
+ block_start = 0;
+ for (scoring_index = 0; scoring_index < NUM_MATCH_VALUES; ++scoring_index)
+ {
+ FcValueListPtr v1;
+ FcValueList *v1_ptrU;
+ int v1_offset = 0;
+
+ if (!strong_matchers [scoring_index] && !weak_matchers [scoring_index])
continue;
- for (f = 0; f < s->nfont; f++)
+
+ for (v1 = pat_elts[scoring_index]->values, v1_ptrU = FcValueListPtrU(v1);
+ v1_ptrU;
+ v1 = v1_ptrU->next, v1_ptrU = FcValueListPtrU(v1), ++v1_offset)
{
+ matcher = (v1_ptrU->binding == FcValueBindingWeak) ?
+ weak_matchers[scoring_index] : strong_matchers[scoring_index];
+
+ if (!matcher) continue;
+
+ bestscore = 1e99;
+
if (FcDebug () & FC_DBG_MATCHV)
{
- printf ("Font %d ", f);
- FcPatternPrint (s->fonts[f]);
+ printf("Scoring Index %d, Value %d: %d(%d) fonts left\n",
+ scoring_index, v1_offset, fonts_left, nfonts);
}
- if (!FcCompare (p, s->fonts[f], score, result))
- return 0;
- if (FcDebug () & FC_DBG_MATCHV)
+
+ for (set = 0; set < nsets; ++set)
{
- printf ("Score");
- for (i = 0; i < NUM_MATCH_VALUES; i++)
+ s = sets[set];
+ if (!s) continue;
+
+ /* All fonts before block_start should have been knocked out. */
+ for (f = (block_start > sets_offset[set]) ? (block_start - sets_offset[set]) : 0;
+ f < s->nfont; ++f)
{
- printf (" %g", score[i]);
+ int cand_elt;
+ FcPatternElt *cand_elts;
+
+ if (match_blocked[f + sets_offset[set]])
+ continue;
+
+ score = 1e99;
+ cand_elts = FcPatternEltU(s->fonts[f]->elts);
+
+ /* Look for the appropriate element in this candidate
+ * pattern 'f' and evaluate its score wrt 'p'. */
+ for (cand_elt = 0; cand_elt < s->fonts[f]->num; ++cand_elt)
+ {
+ if (cand_elts[cand_elt].object == pat_elts[scoring_index]->object)
+ {
+ FcValueListPtr v2;
+ FcValueList *v2_ptrU;
+
+ for (v2 = cand_elts[cand_elt].values, v2_ptrU = FcValueListPtrU(v2);
+ v2_ptrU;
+ v2 = v2_ptrU->next, v2_ptrU = FcValueListPtrU(v2))
+ {
+ double v = (matcher->compare)(&v1_ptrU->value, &v2_ptrU->value);
+
+ if (v < 0)
+ {
+ *result = FcResultTypeMismatch;
+ free (match_blocked);
+ free (sets_offset);
+ return 0;
+ }
+
+ /* I'm actually kind of surprised that
+ * this isn't v + 100 * v1_offset. -PL */
+ v = v * 100 + v1_offset;
+ /* The old patch said score += v, which
+ * seems to be wrong when you have
+ * multiple matchers. This takes the
+ * best score it can find for that font. */
+ if (v < score)
+ score = v;
+ }
+ }
+ }
+
+ /* If there's a previous champion, and current score
+ * beats previous best score, on this element, then
+ * knock out the previous champion and anything
+ * else that we would have visited previous to f;
+ * clearly anything previous to f would have been
+ * less than f on this score. */
+ if (!best || score < bestscore)
+ {
+ if (best)
+ {
+ int b;
+ for (b = block_start; b < f + sets_offset[set]; ++b)
+ if (!match_blocked[b])
+ {
+ match_blocked[b] = FcTrue;
+ --fonts_left;
+ }
+ }
+
+ bestscore = score;
+ best = s->fonts[f];
+ block_start = f + sets_offset[set];
+ }
+
+ /* If f loses, then it's out too. */
+ if (best && score > bestscore)
+ {
+ match_blocked[f + sets_offset[set]] = FcTrue;
+ --fonts_left;
+ }
+
+ /* If there is only 1 font left and the best is set,
+ * then just return this font
+ */
+ if (fonts_left == 1 && best)
+ goto end;
+
+ /* Otherwise, f is equal to best on this element.
+ * Carry on to next pattern element. */
}
- printf ("\n");
}
- for (i = 0; i < NUM_MATCH_VALUES; i++)
+ if ((FcDebug () & FC_DBG_MATCHV) && best)
{
- if (best && bestscore[i] < score[i])
- break;
- if (!best || score[i] < bestscore[i])
- {
- for (i = 0; i < NUM_MATCH_VALUES; i++)
- bestscore[i] = score[i];
- best = s->fonts[f];
- break;
- }
+ printf ("Best match (scoring index %d) candidate %d ", scoring_index, block_start);
+ FcPatternPrint (best);
}
}
}
- if (FcDebug () & FC_DBG_MATCH)
+
+end:
+ free (match_blocked);
+ free (sets_offset);
+
+ if ((FcDebug () & FC_DBG_MATCH) && best)
{
- printf ("Best score");
- for (i = 0; i < NUM_MATCH_VALUES; i++)
- printf (" %g", bestscore[i]);
+ printf ("Best match (scoring index %d) %d ", scoring_index, block_start);
FcPatternPrint (best);
}
if (!best)