Commit 216fac98 authored by Keith Packard's avatar Keith Packard

Add match routine that returns list of fonts

parent ee9061ef
......@@ -458,6 +458,20 @@ FcFontMatch (FcConfig *config,
FcPattern *p,
FcResult *result);
FcPattern *
FcFontRenderPrepare (FcConfig *config,
FcPattern *pat,
FcPattern *font);
FcFontSet *
FcFontSetSort (FcConfig *config,
FcFontSet **sets,
int nsets,
FcPattern *p,
FcBool trim,
FcCharSet **csp,
FcResult *result);
/* fcmatrix.c */
FcMatrix *
FcMatrixCopy (const FcMatrix *mat);
......
This diff is collapsed.
/*
* $XFree86: $
*
* Copyright © 2002 Keith Packard, member of The XFree86 Project, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Keith Packard not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. Keith Packard makes no
* representations about the suitability of this software for any purpose. It
* is provided "as is" without express or implied warranty.
*
* KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef _FCAVL_H_
#define _FCAVL_H_
/*
* fcavl.h
*
* balanced binary tree
*/
typedef struct _FcAvlNode FcAvlNode;
struct _FcAvlNode {
FcAvlNode *left, *right;
short balance;
};
typedef FcBool (*FcAvlMore) (FcAvlNode *a, FcAvlNode *b);
FcBool FcAvlInsert (FcAvlMore more, FcAvlNode **tree, FcAvlNode *leaf);
int FcAvlDelete (FcAvlMore more, FcAvlNode **tree, FcAvlNode *leaf);
#endif /* _FCAVL_H_ */
......@@ -238,6 +238,49 @@ FcCompare (FcPattern *pat,
return FcTrue;
}
FcPattern *
FcFontRenderPrepare (FcConfig *config,
FcPattern *pat,
FcPattern *font)
{
FcPattern *new;
int i;
FcPatternElt *fe, *pe;
FcValue v;
double score[NUM_MATCHER];
FcResult result;
new = FcPatternCreate ();
if (!new)
return 0;
for (i = 0; i < font->num; i++)
{
fe = &font->elts[i];
pe = FcPatternFind (pat, fe->object, FcFalse);
if (pe)
{
if (!FcCompareValueList (pe->object, pe->values,
fe->values, &v, score, &result))
{
FcPatternDestroy (new);
return 0;
}
}
else
v = fe->values->value;
FcPatternAdd (new, fe->object, v, FcTrue);
}
for (i = 0; i < pat->num; i++)
{
pe = &pat->elts[i];
fe = FcPatternFind (font, pe->object, FcFalse);
if (!fe)
FcPatternAdd (new, pe->object, pe->values->value, FcTrue);
}
FcConfigSubstitute (config, new, FcMatchFont);
return new;
}
FcPattern *
FcFontSetMatch (FcConfig *config,
FcFontSet **sets,
......@@ -249,11 +292,8 @@ FcFontSetMatch (FcConfig *config,
int f;
FcFontSet *s;
FcPattern *best;
FcPattern *new;
FcPatternElt *fe, *pe;
FcValue v;
int i;
FcSetName set;
int set;
for (i = 0; i < NUM_MATCHER; i++)
bestscore[i] = 0;
......@@ -318,35 +358,7 @@ FcFontSetMatch (FcConfig *config,
*result = FcResultNoMatch;
return 0;
}
new = FcPatternCreate ();
if (!new)
return 0;
for (i = 0; i < best->num; i++)
{
fe = &best->elts[i];
pe = FcPatternFind (p, fe->object, FcFalse);
if (pe)
{
if (!FcCompareValueList (pe->object, pe->values,
fe->values, &v, score, result))
{
FcPatternDestroy (new);
return 0;
}
}
else
v = fe->values->value;
FcPatternAdd (new, fe->object, v, FcTrue);
}
for (i = 0; i < p->num; i++)
{
pe = &p->elts[i];
fe = FcPatternFind (best, pe->object, FcFalse);
if (!fe)
FcPatternAdd (new, pe->object, pe->values->value, FcTrue);
}
FcConfigSubstitute (config, new, FcMatchFont);
return new;
return FcFontRenderPrepare (config, p, best);
}
FcPattern *
......@@ -370,3 +382,150 @@ FcFontMatch (FcConfig *config,
sets[nsets++] = config->fonts[FcSetApplication];
return FcFontSetMatch (config, sets, nsets, p, result);
}
#include "fcavl.h"
typedef struct _FcSortNode {
FcAvlNode avl;
FcPattern *pattern;
double score[NUM_MATCHER];
} FcSortNode;
static FcBool
FcSortMore (FcAvlNode *aa, FcAvlNode *ab)
{
FcSortNode *a = (FcSortNode *) aa;
FcSortNode *b = (FcSortNode *) ab;
int i;
for (i = 0; i < NUM_MATCHER; i++)
{
if (a->score[i] > b->score[i])
return FcTrue;
if (a->score[i] < b->score[i])
return FcFalse;
}
if (aa > ab)
return FcTrue;
return FcFalse;
}
static FcBool
FcSortWalk (FcSortNode *n, FcFontSet *fs, FcCharSet **cs, FcBool trim)
{
FcCharSet *ncs;
if (!n)
return FcTrue;
if (!FcSortWalk ((FcSortNode *) n->avl.left, fs, cs, trim))
return FcFalse;
if (FcPatternGetCharSet (n->pattern, FC_CHARSET, 0, &ncs) == FcResultMatch)
{
if (!trim || !*cs || FcCharSetSubtractCount (ncs, *cs) != 0)
{
if (*cs)
{
ncs = FcCharSetUnion (ncs, *cs);
if (!ncs)
return FcFalse;
FcCharSetDestroy (*cs);
}
else
ncs = FcCharSetCopy (ncs);
*cs = ncs;
if (!FcFontSetAdd (fs, n->pattern))
return FcFalse;
}
}
if (!FcSortWalk ((FcSortNode *) n->avl.right, fs, cs, trim))
return FcFalse;
return FcTrue;
}
FcFontSet *
FcFontSetSort (FcConfig *config,
FcFontSet **sets,
int nsets,
FcPattern *p,
FcBool trim,
FcCharSet **csp,
FcResult *result)
{
FcFontSet *ret;
FcFontSet *s;
FcSortNode *nodes;
int nnodes;
FcSortNode *root;
FcSortNode *new;
FcCharSet *cs;
int set;
int f;
int i;
nnodes = 0;
for (set = 0; set < nsets; set++)
{
s = sets[set];
if (!s)
continue;
nnodes += s->nfont;
}
if (!nnodes)
goto bail0;
nodes = malloc (nnodes * sizeof (FcSortNode));
if (!nodes)
goto bail0;
root = 0;
new = nodes;
for (set = 0; set < nsets; set++)
{
s = sets[set];
if (!s)
continue;
for (f = 0; f < s->nfont; f++)
{
if (FcDebug () & FC_DBG_MATCHV)
{
printf ("Font %d ", f);
FcPatternPrint (s->fonts[f]);
}
new->pattern = s->fonts[f];
if (!FcCompare (p, new->pattern, new->score, result))
goto bail1;
if (FcDebug () & FC_DBG_MATCHV)
{
printf ("Score");
for (i = 0; i < NUM_MATCHER; i++)
{
printf (" %g", new->score[i]);
}
printf ("\n");
}
FcAvlInsert (FcSortMore, (FcAvlNode **) &root, &new->avl);
new++;
}
}
ret = FcFontSetCreate ();
if (!ret)
goto bail1;
cs = 0;
if (!FcSortWalk (root, ret, &cs, trim))
goto bail2;
*csp = cs;
return ret;
bail2:
if (cs)
FcCharSetDestroy (cs);
FcFontSetDestroy (ret);
bail1:
free (nodes);
bail0:
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment