X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=src%2Ffcxml.c;h=0952b0b4424f30c967cdc975621945131d214fea;hb=2b0f3f1128e479dd3d32022336c967655e6c4821;hp=243819300e3136695923cc6b221dac718ce50c16;hpb=529291bef436384a06db246fda30e08d5812de14;p=fontconfig.git diff --git a/src/fcxml.c b/src/fcxml.c index 2438193..0952b0b 100644 --- a/src/fcxml.c +++ b/src/fcxml.c @@ -1,5 +1,5 @@ /* - * $RCSId: xc/lib/fontconfig/src/fcxml.c,v 1.21 2002/08/22 18:53:22 keithp Exp $ + * fontconfig/src/fcxml.c * * Copyright © 2002 Keith Packard * @@ -13,9 +13,9 @@ * 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, + * THE AUTHOR(S) 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 + * EVENT SHALL THE AUTHOR(S) 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 @@ -53,11 +53,15 @@ #endif /* ENABLE_LIBXML2 */ #ifdef _WIN32 +#define _WIN32_WINNT 0x0500 #define STRICT #include #undef STRICT +#include #endif +static void +FcExprDestroy (FcExpr *e); void FcTestDestroy (FcTest *test) @@ -65,130 +69,100 @@ FcTestDestroy (FcTest *test) if (test->next) FcTestDestroy (test->next); FcExprDestroy (test->expr); - FcStrFree ((FcChar8 *) test->field); FcMemFree (FC_MEM_TEST, sizeof (FcTest)); free (test); } -FcExpr * -FcExprCreateInteger (int i) +static FcExpr * +FcExprCreateInteger (FcConfig *config, int i) { - FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr)); - + FcExpr *e = FcConfigAllocExpr (config); if (e) { - FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr)); e->op = FcOpInteger; e->u.ival = i; } return e; } -FcExpr * -FcExprCreateDouble (double d) +static FcExpr * +FcExprCreateDouble (FcConfig *config, double d) { - FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr)); - + FcExpr *e = FcConfigAllocExpr (config); if (e) { - FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr)); e->op = FcOpDouble; e->u.dval = d; } return e; } -FcExpr * -FcExprCreateString (const FcChar8 *s) +static FcExpr * +FcExprCreateString (FcConfig *config, const FcChar8 *s) { - FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr)); - + FcExpr *e = FcConfigAllocExpr (config); if (e) { - FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr)); e->op = FcOpString; - e->u.sval = FcStrCopy (s); + e->u.sval = FcStrStaticName (s); } return e; } -FcExpr * -FcExprCreateMatrix (const FcMatrix *m) +static FcExpr * +FcExprCreateMatrix (FcConfig *config, const FcMatrix *m) { - FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr)); - + FcExpr *e = FcConfigAllocExpr (config); if (e) { - FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr)); e->op = FcOpMatrix; e->u.mval = FcMatrixCopy (m); } return e; } -FcExpr * -FcExprCreateBool (FcBool b) +static FcExpr * +FcExprCreateBool (FcConfig *config, FcBool b) { - FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr)); - + FcExpr *e = FcConfigAllocExpr (config); if (e) { - FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr)); e->op = FcOpBool; e->u.bval = b; } return e; } -FcExpr * -FcExprCreateNil (void) -{ - FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr)); - - if (e) - { - FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr)); - e->op = FcOpNil; - } - return e; -} - -FcExpr * -FcExprCreateField (const char *field) +static FcExpr * +FcExprCreateField (FcConfig *config, const char *field) { - FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr)); - + FcExpr *e = FcConfigAllocExpr (config); if (e) { - FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr)); e->op = FcOpField; - e->u.field = (char *) FcStrCopy ((FcChar8 *) field); + e->u.object = FcObjectFromName (field); } return e; } -FcExpr * -FcExprCreateConst (const FcChar8 *constant) +static FcExpr * +FcExprCreateConst (FcConfig *config, const FcChar8 *constant) { - FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr)); - + FcExpr *e = FcConfigAllocExpr (config); if (e) { - FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr)); e->op = FcOpConst; - e->u.constant = FcStrCopy (constant); + e->u.constant = FcStrStaticName (constant); } return e; } -FcExpr * -FcExprCreateOp (FcExpr *left, FcOp op, FcExpr *right) +static FcExpr * +FcExprCreateOp (FcConfig *config, FcExpr *left, FcOp op, FcExpr *right) { - FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr)); - + FcExpr *e = FcConfigAllocExpr (config); if (e) { - FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr)); e->op = op; e->u.tree.left = left; e->u.tree.right = right; @@ -196,7 +170,7 @@ FcExprCreateOp (FcExpr *left, FcOp op, FcExpr *right) return e; } -void +static void FcExprDestroy (FcExpr *e) { if (!e) @@ -207,7 +181,6 @@ FcExprDestroy (FcExpr *e) case FcOpDouble: break; case FcOpString: - FcStrFree (e->u.sval); break; case FcOpMatrix: FcMatrixFree (e->u.mval); @@ -218,10 +191,8 @@ FcExprDestroy (FcExpr *e) case FcOpBool: break; case FcOpField: - FcStrFree ((FcChar8 *) e->u.field); break; case FcOpConst: - FcStrFree (e->u.constant); break; case FcOpAssign: case FcOpAssignReplace: @@ -260,8 +231,8 @@ FcExprDestroy (FcExpr *e) case FcOpInvalid: break; } - FcMemFree (FC_MEM_EXPR, sizeof (FcExpr)); - free (e); + + e->op = FcOpNil; } void @@ -269,22 +240,16 @@ FcEditDestroy (FcEdit *e) { if (e->next) FcEditDestroy (e->next); - FcStrFree ((FcChar8 *) e->field); if (e->expr) FcExprDestroy (e->expr); free (e); } -char * -FcConfigSaveField (const char *field) -{ - return (char *) FcStrCopy ((FcChar8 *) field); -} - typedef enum _FcElement { FcElementNone, FcElementFontconfig, FcElementDir, + FcElementCacheDir, FcElementCache, FcElementInclude, FcElementConfig, @@ -345,12 +310,13 @@ static const struct { } fcElementMap[] = { { "fontconfig", FcElementFontconfig }, { "dir", FcElementDir }, + { "cachedir", FcElementCacheDir }, { "cache", FcElementCache }, { "include", FcElementInclude }, { "config", FcElementConfig }, { "match", FcElementMatch }, { "alias", FcElementAlias }, - + { "blank", FcElementBlank }, { "rescan", FcElementRescan }, @@ -415,6 +381,7 @@ typedef struct _FcPStack { FcElement element; FcChar8 **attr; FcStrBuf str; + FcChar8 *attr_buf_static[16]; } FcPStack; typedef enum _FcVStackTag { @@ -451,7 +418,7 @@ typedef struct _FcVStack { int integer; double _double; FcMatrix *matrix; - FcBool bool; + FcBool bool_; FcTest *test; FcQual qual; @@ -470,6 +437,10 @@ typedef struct _FcConfigParse { const FcChar8 *name; FcConfig *config; XML_Parser parser; + int pstack_static_used; + FcPStack pstack_static[8]; + int vstack_static_used; + FcVStack vstack_static[64]; } FcConfigParse; typedef enum _FcConfigSeverity { @@ -546,6 +517,8 @@ FcTypecheckValue (FcConfigParse *parse, FcType value, FcType type) if ((value == FcTypeLangSet && type == FcTypeString) || (value == FcTypeString && type == FcTypeLangSet)) return; + if (type == (FcType) -1) + return; FcConfigMessage (parse, FcSevereWarning, "saw %s, expected %s", FcTypeName (value), FcTypeName (type)); } @@ -557,6 +530,10 @@ FcTypecheckExpr (FcConfigParse *parse, FcExpr *expr, FcType type) const FcObjectType *o; const FcConstant *c; + /* If parsing the expression failed, some nodes may be NULL */ + if (!expr) + return; + switch (expr->op) { case FcOpInteger: case FcOpDouble: @@ -577,7 +554,7 @@ FcTypecheckExpr (FcConfigParse *parse, FcExpr *expr, FcType type) case FcOpNil: break; case FcOpField: - o = FcNameGetObjectType (expr->u.field); + o = FcNameGetObjectType (FcObjectName (expr->u.object)); if (o) FcTypecheckValue (parse, o->type, type); break; @@ -657,10 +634,10 @@ FcTestCreate (FcConfigParse *parse, test->next = 0; test->kind = kind; test->qual = qual; - test->field = (char *) FcStrCopy (field); + test->object = FcObjectFromName ((const char *) field); test->op = compare; test->expr = expr; - o = FcNameGetObjectType (test->field); + o = FcNameGetObjectType (FcObjectName (test->object)); if (o) FcTypecheckExpr (parse, expr, o->type); } @@ -669,7 +646,7 @@ FcTestCreate (FcConfigParse *parse, static FcEdit * FcEditCreate (FcConfigParse *parse, - const char *field, + FcObject object, FcOp op, FcExpr *expr, FcValueBinding binding) @@ -681,197 +658,141 @@ FcEditCreate (FcConfigParse *parse, const FcObjectType *o; e->next = 0; - e->field = field; /* already saved in grammar */ + e->object = object; e->op = op; e->expr = expr; e->binding = binding; - o = FcNameGetObjectType (e->field); + o = FcNameGetObjectType (FcObjectName (e->object)); if (o) FcTypecheckExpr (parse, expr, o->type); } return e; } -static void -FcVStackPush (FcConfigParse *parse, FcVStack *vstack) -{ - vstack->prev = parse->vstack; - vstack->pstack = parse->pstack ? parse->pstack->prev : 0; - parse->vstack = vstack; -} - static FcVStack * -FcVStackCreate (void) +FcVStackCreateAndPush (FcConfigParse *parse) { FcVStack *new; - new = malloc (sizeof (FcVStack)); - if (!new) - return 0; - FcMemAlloc (FC_MEM_VSTACK, sizeof (FcVStack)); + if (parse->vstack_static_used < sizeof (parse->vstack_static) / sizeof (parse->vstack_static[0])) + new = &parse->vstack_static[parse->vstack_static_used++]; + else + { + new = malloc (sizeof (FcVStack)); + if (!new) + return 0; + FcMemAlloc (FC_MEM_VSTACK, sizeof (FcVStack)); + } new->tag = FcVStackNone; new->prev = 0; - return new; -} -static void -FcVStackDestroy (FcVStack *vstack) -{ - FcVStack *prev; + new->prev = parse->vstack; + new->pstack = parse->pstack ? parse->pstack->prev : 0; + parse->vstack = new; - for (; vstack; vstack = prev) - { - prev = vstack->prev; - switch (vstack->tag) { - case FcVStackNone: - break; - case FcVStackString: - case FcVStackFamily: - case FcVStackField: - case FcVStackConstant: - case FcVStackGlob: - FcStrFree (vstack->u.string); - break; - case FcVStackPattern: - FcPatternDestroy (vstack->u.pattern); - break; - case FcVStackInteger: - case FcVStackDouble: - break; - case FcVStackMatrix: - FcMatrixFree (vstack->u.matrix); - break; - case FcVStackBool: - break; - case FcVStackTest: - FcTestDestroy (vstack->u.test); - break; - case FcVStackExpr: - case FcVStackPrefer: - case FcVStackAccept: - case FcVStackDefault: - FcExprDestroy (vstack->u.expr); - break; - case FcVStackEdit: - FcEditDestroy (vstack->u.edit); - break; - } - FcMemFree (FC_MEM_VSTACK, sizeof (FcVStack)); - free (vstack); - } + return new; } static FcBool FcVStackPushString (FcConfigParse *parse, FcVStackTag tag, FcChar8 *string) { - FcVStack *vstack = FcVStackCreate (); + FcVStack *vstack = FcVStackCreateAndPush (parse); if (!vstack) return FcFalse; vstack->u.string = string; vstack->tag = tag; - FcVStackPush (parse, vstack); return FcTrue; } static FcBool FcVStackPushInteger (FcConfigParse *parse, int integer) { - FcVStack *vstack = FcVStackCreate (); + FcVStack *vstack = FcVStackCreateAndPush (parse); if (!vstack) return FcFalse; vstack->u.integer = integer; vstack->tag = FcVStackInteger; - FcVStackPush (parse, vstack); return FcTrue; } static FcBool FcVStackPushDouble (FcConfigParse *parse, double _double) { - FcVStack *vstack = FcVStackCreate (); + FcVStack *vstack = FcVStackCreateAndPush (parse); if (!vstack) return FcFalse; vstack->u._double = _double; vstack->tag = FcVStackDouble; - FcVStackPush (parse, vstack); return FcTrue; } static FcBool FcVStackPushMatrix (FcConfigParse *parse, FcMatrix *matrix) { - FcVStack *vstack = FcVStackCreate (); - if (!vstack) - return FcFalse; + FcVStack *vstack; matrix = FcMatrixCopy (matrix); if (!matrix) - { - FcVStackDestroy (vstack); return FcFalse; - } + vstack = FcVStackCreateAndPush (parse); + if (!vstack) + return FcFalse; vstack->u.matrix = matrix; vstack->tag = FcVStackMatrix; - FcVStackPush (parse, vstack); return FcTrue; } static FcBool -FcVStackPushBool (FcConfigParse *parse, FcBool bool) +FcVStackPushBool (FcConfigParse *parse, FcBool bool_) { - FcVStack *vstack = FcVStackCreate (); + FcVStack *vstack = FcVStackCreateAndPush (parse); if (!vstack) return FcFalse; - vstack->u.bool = bool; + vstack->u.bool_ = bool_; vstack->tag = FcVStackBool; - FcVStackPush (parse, vstack); return FcTrue; } static FcBool FcVStackPushTest (FcConfigParse *parse, FcTest *test) { - FcVStack *vstack = FcVStackCreate (); + FcVStack *vstack = FcVStackCreateAndPush (parse); if (!vstack) return FcFalse; vstack->u.test = test; vstack->tag = FcVStackTest; - FcVStackPush (parse, vstack); return FcTrue; } static FcBool FcVStackPushExpr (FcConfigParse *parse, FcVStackTag tag, FcExpr *expr) { - FcVStack *vstack = FcVStackCreate (); + FcVStack *vstack = FcVStackCreateAndPush (parse); if (!vstack) return FcFalse; vstack->u.expr = expr; vstack->tag = tag; - FcVStackPush (parse, vstack); return FcTrue; } static FcBool FcVStackPushEdit (FcConfigParse *parse, FcEdit *edit) { - FcVStack *vstack = FcVStackCreate (); + FcVStack *vstack = FcVStackCreateAndPush (parse); if (!vstack) return FcFalse; vstack->u.edit = edit; vstack->tag = FcVStackEdit; - FcVStackPush (parse, vstack); return FcTrue; } static FcBool FcVStackPushPattern (FcConfigParse *parse, FcPattern *pattern) { - FcVStack *vstack = FcVStackCreate (); + FcVStack *vstack = FcVStackCreateAndPush (parse); if (!vstack) return FcFalse; vstack->u.pattern = pattern; vstack->tag = FcVStackPattern; - FcVStackPush (parse, vstack); return FcTrue; } @@ -884,28 +805,74 @@ FcVStackFetch (FcConfigParse *parse, int off) return vstack; } -static void -FcVStackClear (FcConfigParse *parse) +static FcVStack * +FcVStackPeek (FcConfigParse *parse) { - while (parse->vstack && parse->vstack->pstack == parse->pstack) - { - FcVStack *vstack = parse->vstack; - parse->vstack = vstack->prev; - vstack->prev = 0; - FcVStackDestroy (vstack); - } + FcVStack *vstack = parse->vstack; + + return vstack && vstack->pstack == parse->pstack ? vstack : 0; } -static FcVStack * -FcVStackPop (FcConfigParse *parse) +static void +FcVStackPopAndDestroy (FcConfigParse *parse) { FcVStack *vstack = parse->vstack; if (!vstack || vstack->pstack != parse->pstack) - return 0; + return; + parse->vstack = vstack->prev; - vstack->prev = 0; - return vstack; + + switch (vstack->tag) { + case FcVStackNone: + break; + case FcVStackFamily: + break; + case FcVStackString: + case FcVStackField: + case FcVStackConstant: + case FcVStackGlob: + FcStrFree (vstack->u.string); + break; + case FcVStackPattern: + FcPatternDestroy (vstack->u.pattern); + break; + case FcVStackInteger: + case FcVStackDouble: + break; + case FcVStackMatrix: + FcMatrixFree (vstack->u.matrix); + break; + case FcVStackBool: + break; + case FcVStackTest: + FcTestDestroy (vstack->u.test); + break; + case FcVStackExpr: + case FcVStackPrefer: + case FcVStackAccept: + case FcVStackDefault: + FcExprDestroy (vstack->u.expr); + break; + case FcVStackEdit: + FcEditDestroy (vstack->u.edit); + break; + } + + if (vstack == &parse->vstack_static[parse->vstack_static_used - 1]) + parse->vstack_static_used--; + else + { + FcMemFree (FC_MEM_VSTACK, sizeof (FcVStack)); + free (vstack); + } +} + +static void +FcVStackClear (FcConfigParse *parse) +{ + while (FcVStackPeek (parse)) + FcVStackPopAndDestroy (parse); } static int @@ -922,7 +889,7 @@ FcVStackElements (FcConfigParse *parse) } static FcChar8 ** -FcConfigSaveAttr (const XML_Char **attr) +FcConfigSaveAttr (const XML_Char **attr, FcChar8 **buf, int size_bytes) { int slen; int i; @@ -934,10 +901,21 @@ FcConfigSaveAttr (const XML_Char **attr) slen = 0; for (i = 0; attr[i]; i++) slen += strlen ((char *) attr[i]) + 1; - new = malloc ((i + 1) * sizeof (FcChar8 *) + slen); - if (!new) + if (i == 0) return 0; - FcMemAlloc (FC_MEM_ATTR, 1); /* size is too expensive */ + slen += (i + 1) * sizeof (FcChar8 *); + if (slen <= size_bytes) + new = buf; + else + { + new = malloc (slen); + if (!new) + { + FcConfigMessage (0, FcSevereError, "out of memory"); + return 0; + } + FcMemAlloc (FC_MEM_ATTR, 1); /* size is too expensive */ + } s = (FcChar8 *) (new + (i + 1)); for (i = 0; attr[i]; i++) { @@ -952,21 +930,21 @@ FcConfigSaveAttr (const XML_Char **attr) static FcBool FcPStackPush (FcConfigParse *parse, FcElement element, const XML_Char **attr) { - FcPStack *new = malloc (sizeof (FcPStack)); + FcPStack *new; - if (!new) - return FcFalse; - FcMemAlloc (FC_MEM_PSTACK, sizeof (FcPStack)); - new->prev = parse->pstack; - new->element = element; - if (attr) + if (parse->pstack_static_used < sizeof (parse->pstack_static) / sizeof (parse->pstack_static[0])) + new = &parse->pstack_static[parse->pstack_static_used++]; + else { - new->attr = FcConfigSaveAttr (attr); - if (!new->attr) - FcConfigMessage (parse, FcSevereError, "out of memory"); + new = malloc (sizeof (FcPStack)); + if (!new) + return FcFalse; + FcMemAlloc (FC_MEM_PSTACK, sizeof (FcPStack)); } - else - new->attr = 0; + + new->prev = parse->pstack; + new->element = element; + new->attr = FcConfigSaveAttr (attr, new->attr_buf_static, sizeof (new->attr_buf_static)); FcStrBufInit (&new->str, 0, 0); parse->pstack = new; return FcTrue; @@ -986,13 +964,19 @@ FcPStackPop (FcConfigParse *parse) old = parse->pstack; parse->pstack = old->prev; FcStrBufDestroy (&old->str); - if (old->attr) + if (old->attr && old->attr != old->attr_buf_static) { FcMemFree (FC_MEM_ATTR, 1); /* size is to expensive */ free (old->attr); } - FcMemFree (FC_MEM_PSTACK, sizeof (FcPStack)); - free (old); + + if (old == &parse->pstack_static[parse->pstack_static_used - 1]) + parse->pstack_static_used--; + else + { + FcMemFree (FC_MEM_PSTACK, sizeof (FcPStack)); + free (old); + } return FcTrue; } @@ -1000,7 +984,9 @@ static FcBool FcConfigInit (FcConfigParse *parse, const FcChar8 *name, FcConfig *config, XML_Parser parser) { parse->pstack = 0; + parse->pstack_static_used = 0; parse->vstack = 0; + parse->vstack_static_used = 0; parse->error = FcFalse; parse->name = name; parse->config = config; @@ -1104,7 +1090,7 @@ FcParseInt (FcConfigParse *parse) if (!parse->pstack) return; - s = FcStrBufDone (&parse->pstack->str); + s = FcStrBufDoneStatic (&parse->pstack->str); if (!s) { FcConfigMessage (parse, FcSevereError, "out of memory"); @@ -1116,7 +1102,7 @@ FcParseInt (FcConfigParse *parse) FcConfigMessage (parse, FcSevereError, "\"%s\": not a valid integer", s); else FcVStackPushInteger (parse, l); - FcStrFree (s); + FcStrBufDestroy (&parse->pstack->str); } /* @@ -1185,7 +1171,7 @@ FcParseDouble (FcConfigParse *parse) if (!parse->pstack) return; - s = FcStrBufDone (&parse->pstack->str); + s = FcStrBufDoneStatic (&parse->pstack->str); if (!s) { FcConfigMessage (parse, FcSevereError, "out of memory"); @@ -1197,7 +1183,7 @@ FcParseDouble (FcConfigParse *parse) FcConfigMessage (parse, FcSevereError, "\"%s\": not a valid double", s); else FcVStackPushDouble (parse, d); - FcStrFree (s); + FcStrBufDestroy (&parse->pstack->str); } static void @@ -1224,7 +1210,7 @@ FcParseMatrix (FcConfigParse *parse) enum { m_done, m_xx, m_xy, m_yx, m_yy } matrix_state = m_yy; FcMatrix m; - while ((vstack = FcVStackPop (parse))) + while ((vstack = FcVStackPeek (parse))) { double v; switch (vstack->tag) { @@ -1246,7 +1232,7 @@ FcParseMatrix (FcConfigParse *parse) case m_yy: m.yy = v; break; default: break; } - FcVStackDestroy (vstack); + FcVStackPopAndDestroy (parse); matrix_state--; } if (matrix_state != m_done) @@ -1256,13 +1242,13 @@ FcParseMatrix (FcConfigParse *parse) } static FcBool -FcConfigLexBool (FcConfigParse *parse, const FcChar8 *bool) +FcConfigLexBool (FcConfigParse *parse, const FcChar8 *bool_) { FcBool result = FcFalse; - if (!FcNameBool (bool, &result)) + if (!FcNameBool (bool_, &result)) FcConfigMessage (parse, FcSevereWarning, "\"%s\" is not known boolean", - bool); + bool_); return result; } @@ -1273,14 +1259,41 @@ FcParseBool (FcConfigParse *parse) if (!parse->pstack) return; - s = FcStrBufDone (&parse->pstack->str); + s = FcStrBufDoneStatic (&parse->pstack->str); if (!s) { FcConfigMessage (parse, FcSevereError, "out of memory"); return; } FcVStackPushBool (parse, FcConfigLexBool (parse, s)); - FcStrFree (s); + FcStrBufDestroy (&parse->pstack->str); +} + +static FcBool +FcConfigLexBinding (FcConfigParse *parse, + const FcChar8 *binding_string, + FcValueBinding *binding_ret) +{ + FcValueBinding binding; + + if (!binding_string) + binding = FcValueBindingWeak; + else + { + if (!strcmp ((char *) binding_string, "weak")) + binding = FcValueBindingWeak; + else if (!strcmp ((char *) binding_string, "strong")) + binding = FcValueBindingStrong; + else if (!strcmp ((char *) binding_string, "same")) + binding = FcValueBindingSame; + else + { + FcConfigMessage (parse, FcSevereWarning, "invalid binding \"%s\"", binding_string); + return FcFalse; + } + } + *binding_ret = binding; + return FcTrue; } static void @@ -1289,20 +1302,20 @@ FcParseFamilies (FcConfigParse *parse, FcVStackTag tag) FcVStack *vstack; FcExpr *left, *expr = 0, *new; - while ((vstack = FcVStackPop (parse))) + while ((vstack = FcVStackPeek (parse))) { if (vstack->tag != FcVStackFamily) { FcConfigMessage (parse, FcSevereWarning, "non-family"); - FcVStackDestroy (vstack); + FcVStackPopAndDestroy (parse); continue; } left = vstack->u.expr; vstack->tag = FcVStackNone; - FcVStackDestroy (vstack); + FcVStackPopAndDestroy (parse); if (expr) { - new = FcExprCreateOp (left, FcOpComma, expr); + new = FcExprCreateOp (parse->config, left, FcOpComma, expr); if (!new) { FcConfigMessage (parse, FcSevereError, "out of memory"); @@ -1333,14 +1346,14 @@ FcParseFamily (FcConfigParse *parse) if (!parse->pstack) return; - s = FcStrBufDone (&parse->pstack->str); + s = FcStrBufDoneStatic (&parse->pstack->str); if (!s) { FcConfigMessage (parse, FcSevereError, "out of memory"); return; } - expr = FcExprCreateString (s); - FcStrFree (s); + expr = FcExprCreateString (parse->config, s); + FcStrBufDestroy (&parse->pstack->str); if (expr) FcVStackPushExpr (parse, FcVStackFamily, expr); } @@ -1352,14 +1365,17 @@ FcParseAlias (FcConfigParse *parse) FcEdit *edit = 0, *next; FcVStack *vstack; FcTest *test; + FcValueBinding binding; - while ((vstack = FcVStackPop (parse))) + if (!FcConfigLexBinding (parse, FcConfigGetAttribute (parse, "binding"), &binding)) + return; + while ((vstack = FcVStackPeek (parse))) { switch (vstack->tag) { case FcVStackFamily: if (family) { - new = FcExprCreateOp (vstack->u.expr, FcOpComma, family); + new = FcExprCreateOp (parse->config, vstack->u.expr, FcOpComma, family); if (!new) FcConfigMessage (parse, FcSevereError, "out of memory"); else @@ -1395,7 +1411,7 @@ FcParseAlias (FcConfigParse *parse) FcConfigMessage (parse, FcSevereWarning, "bad alias"); break; } - FcVStackDestroy (vstack); + FcVStackPopAndDestroy (parse); } if (!family) { @@ -1411,10 +1427,10 @@ FcParseAlias (FcConfigParse *parse) if (prefer) { edit = FcEditCreate (parse, - FcConfigSaveField ("family"), + FC_FAMILY_OBJECT, FcOpPrepend, prefer, - FcValueBindingWeak); + binding); if (edit) edit->next = 0; else @@ -1424,10 +1440,10 @@ FcParseAlias (FcConfigParse *parse) { next = edit; edit = FcEditCreate (parse, - FcConfigSaveField ("family"), + FC_FAMILY_OBJECT, FcOpAppend, accept, - FcValueBindingWeak); + binding); if (edit) edit->next = next; else @@ -1437,10 +1453,10 @@ FcParseAlias (FcConfigParse *parse) { next = edit; edit = FcEditCreate (parse, - FcConfigSaveField ("family"), + FC_FAMILY_OBJECT, FcOpAppendLast, def, - FcValueBindingWeak); + binding); if (edit) edit->next = next; else @@ -1464,7 +1480,7 @@ FcParseAlias (FcConfigParse *parse) static FcExpr * FcPopExpr (FcConfigParse *parse) { - FcVStack *vstack = FcVStackPop (parse); + FcVStack *vstack = FcVStackPeek (parse); FcExpr *expr = 0; if (!vstack) return 0; @@ -1473,13 +1489,13 @@ FcPopExpr (FcConfigParse *parse) break; case FcVStackString: case FcVStackFamily: - expr = FcExprCreateString (vstack->u.string); + expr = FcExprCreateString (parse->config, vstack->u.string); break; case FcVStackField: - expr = FcExprCreateField ((char *) vstack->u.string); + expr = FcExprCreateField (parse->config, (char *) vstack->u.string); break; case FcVStackConstant: - expr = FcExprCreateConst (vstack->u.string); + expr = FcExprCreateConst (parse->config, vstack->u.string); break; case FcVStackGlob: /* XXX: What's the correct action here? (CDW) */ @@ -1491,16 +1507,16 @@ FcPopExpr (FcConfigParse *parse) vstack->tag = FcVStackNone; break; case FcVStackInteger: - expr = FcExprCreateInteger (vstack->u.integer); + expr = FcExprCreateInteger (parse->config, vstack->u.integer); break; case FcVStackDouble: - expr = FcExprCreateDouble (vstack->u._double); + expr = FcExprCreateDouble (parse->config, vstack->u._double); break; case FcVStackMatrix: - expr = FcExprCreateMatrix (vstack->u.matrix); + expr = FcExprCreateMatrix (parse->config, vstack->u.matrix); break; case FcVStackBool: - expr = FcExprCreateBool (vstack->u.bool); + expr = FcExprCreateBool (parse->config, vstack->u.bool_); break; case FcVStackTest: break; @@ -1513,7 +1529,7 @@ FcPopExpr (FcConfigParse *parse) default: break; } - FcVStackDestroy (vstack); + FcVStackPopAndDestroy (parse); return expr; } @@ -1535,7 +1551,7 @@ FcPopBinary (FcConfigParse *parse, FcOp op) { if (expr) { - new = FcExprCreateOp (left, op, expr); + new = FcExprCreateOp (parse->config, left, op, expr); if (!new) { FcConfigMessage (parse, FcSevereError, "out of memory"); @@ -1571,7 +1587,7 @@ FcPopUnary (FcConfigParse *parse, FcOp op) if ((operand = FcPopExpr (parse))) { - new = FcExprCreateOp (operand, op, 0); + new = FcExprCreateOp (parse->config, operand, op, 0); if (!new) { FcExprDestroy (operand); @@ -1596,7 +1612,7 @@ FcParseInclude (FcConfigParse *parse) const FcChar8 *i; FcBool ignore_missing = FcFalse; - s = FcStrBufDone (&parse->pstack->str); + s = FcStrBufDoneStatic (&parse->pstack->str); if (!s) { FcConfigMessage (parse, FcSevereError, "out of memory"); @@ -1607,7 +1623,7 @@ FcParseInclude (FcConfigParse *parse) ignore_missing = FcTrue; if (!FcConfigParseAndLoad (parse->config, s, !ignore_missing)) parse->error = FcTrue; - FcStrFree (s); + FcStrBufDestroy (&parse->pstack->str); } typedef struct _FcOpMap { @@ -1645,7 +1661,6 @@ FcConfigLexCompare (const FcChar8 *compare) return FcConfigLexOp (compare, fcCompareOps, NUM_COMPARE_OPS); } - static void FcParseTest (FcConfigParse *parse) { @@ -1668,6 +1683,8 @@ FcParseTest (FcConfigParse *parse) kind = FcMatchPattern; else if (!strcmp ((char *) kind_string, "font")) kind = FcMatchFont; + else if (!strcmp ((char *) kind_string, "scan")) + kind = FcMatchScan; else if (!strcmp ((char *) kind_string, "default")) kind = FcMatchDefault; else @@ -1750,7 +1767,6 @@ FcParseEdit (FcConfigParse *parse) { const FcChar8 *name; const FcChar8 *mode_string; - const FcChar8 *binding_string; FcOp mode; FcValueBinding binding; FcExpr *expr; @@ -1774,25 +1790,12 @@ FcParseEdit (FcConfigParse *parse) return; } } - binding_string = FcConfigGetAttribute (parse, "binding"); - if (!binding_string) - binding = FcValueBindingWeak; - else - { - if (!strcmp ((char *) binding_string, "weak")) - binding = FcValueBindingWeak; - else if (!strcmp ((char *) binding_string, "strong")) - binding = FcValueBindingStrong; - else if (!strcmp ((char *) binding_string, "same")) - binding = FcValueBindingSame; - else - { - FcConfigMessage (parse, FcSevereWarning, "invalid edit binding \"%s\"", binding_string); - return; - } - } + if (!FcConfigLexBinding (parse, FcConfigGetAttribute (parse, "binding"), &binding)) + return; + expr = FcPopBinary (parse, FcOpComma); - edit = FcEditCreate (parse, (char *) FcStrCopy (name), mode, expr, binding); + edit = FcEditCreate (parse, FcObjectFromName ((char *) name), + mode, expr, binding); if (!edit) { FcConfigMessage (parse, FcSevereError, "out of memory"); @@ -1821,13 +1824,15 @@ FcParseMatch (FcConfigParse *parse) kind = FcMatchPattern; else if (!strcmp ((char *) kind_name, "font")) kind = FcMatchFont; + else if (!strcmp ((char *) kind_name, "scan")) + kind = FcMatchScan; else { FcConfigMessage (parse, FcSevereWarning, "invalid match target \"%s\"", kind_name); return; } } - while ((vstack = FcVStackPop (parse))) + while ((vstack = FcVStackPeek (parse))) { switch (vstack->tag) { case FcVStackTest: @@ -1839,12 +1844,18 @@ FcParseMatch (FcConfigParse *parse) vstack->u.edit->next = edit; edit = vstack->u.edit; vstack->tag = FcVStackNone; + if (kind == FcMatchScan && edit->object > FC_MAX_BASE_OBJECT) + { + FcConfigMessage (parse, FcSevereError, + " cannot edit user-defined object \"%s\"", + FcObjectName(edit->object)); + } break; default: FcConfigMessage (parse, FcSevereWarning, "invalid match element"); break; } - FcVStackDestroy (vstack); + FcVStackPopAndDestroy (parse); } if (!FcConfigAddEdit (parse->config, test, edit, kind)) FcConfigMessage (parse, FcSevereError, "out of memory"); @@ -1855,7 +1866,7 @@ FcParseAcceptRejectFont (FcConfigParse *parse, FcElement element) { FcVStack *vstack; - while ((vstack = FcVStackPop (parse))) + while ((vstack = FcVStackPeek (parse))) { switch (vstack->tag) { case FcVStackGlob: @@ -1880,7 +1891,7 @@ FcParseAcceptRejectFont (FcConfigParse *parse, FcElement element) FcConfigMessage (parse, FcSevereWarning, "bad font selector"); break; } - FcVStackDestroy (vstack); + FcVStackPopAndDestroy (parse); } } @@ -1888,7 +1899,7 @@ FcParseAcceptRejectFont (FcConfigParse *parse, FcElement element) static FcValue FcPopValue (FcConfigParse *parse) { - FcVStack *vstack = FcVStackPop (parse); + FcVStack *vstack = FcVStackPeek (parse); FcValue value; value.type = FcTypeVoid; @@ -1898,7 +1909,7 @@ FcPopValue (FcConfigParse *parse) switch (vstack->tag) { case FcVStackString: - value.u.s = FcStrCopy (vstack->u.string); + value.u.s = FcStrStaticName (vstack->u.string); if (value.u.s) value.type = FcTypeString; break; @@ -1920,7 +1931,7 @@ FcPopValue (FcConfigParse *parse) value.type = FcTypeMatrix; break; case FcVStackBool: - value.u.b = vstack->u.bool; + value.u.b = vstack->u.bool_; value.type = FcTypeBool; break; default: @@ -1928,7 +1939,7 @@ FcPopValue (FcConfigParse *parse) vstack->tag); break; } - FcVStackDestroy (vstack); + FcVStackPopAndDestroy (parse); return value; } @@ -1962,8 +1973,10 @@ FcParsePatelt (FcConfigParse *parse) if (!FcPatternAdd (pattern, name, value, FcTrue)) { FcConfigMessage (parse, FcSevereError, "out of memory"); + FcValueDestroy(value); break; } + FcValueDestroy(value); } FcVStackPushPattern (parse, pattern); @@ -1981,7 +1994,7 @@ FcParsePattern (FcConfigParse *parse) return; } - while ((vstack = FcVStackPop (parse))) + while ((vstack = FcVStackPeek (parse))) { switch (vstack->tag) { case FcVStackPattern: @@ -1996,7 +2009,7 @@ FcParsePattern (FcConfigParse *parse) FcConfigMessage (parse, FcSevereWarning, "unknown pattern element"); break; } - FcVStackDestroy (vstack); + FcVStackPopAndDestroy (parse); } FcVStackPushPattern (parse, pattern); @@ -2007,7 +2020,10 @@ FcEndElement(void *userData, const XML_Char *name) { FcConfigParse *parse = userData; FcChar8 *data; - +#ifdef _WIN32 + FcChar8 buffer[1000]; +#endif + if (!parse->pstack) return; switch (parse->pstack->element) { @@ -2016,6 +2032,74 @@ FcEndElement(void *userData, const XML_Char *name) case FcElementFontconfig: break; case FcElementDir: + data = FcStrBufDoneStatic (&parse->pstack->str); + if (!data) + { + FcConfigMessage (parse, FcSevereError, "out of memory"); + break; + } +#ifdef _WIN32 + if (strcmp (data, "CUSTOMFONTDIR") == 0) + { + char *p; + data = buffer; + if (!GetModuleFileName (NULL, buffer, sizeof (buffer) - 20)) + { + FcConfigMessage (parse, FcSevereError, "GetModuleFileName failed"); + break; + } + /* + * Must use the multi-byte aware function to search + * for backslash because East Asian double-byte code + * pages have characters with backslash as the second + * byte. + */ + p = _mbsrchr (data, '\\'); + if (p) *p = '\0'; + strcat (data, "\\fonts"); + } + else if (strcmp (data, "APPSHAREFONTDIR") == 0) + { + char *p; + data = buffer; + if (!GetModuleFileName (NULL, buffer, sizeof (buffer) - 20)) + { + FcConfigMessage (parse, FcSevereError, "GetModuleFileName failed"); + break; + } + p = _mbsrchr (data, '\\'); + if (p) *p = '\0'; + strcat (data, "\\..\\share\\fonts"); + } + else if (strcmp (data, "WINDOWSFONTDIR") == 0) + { + int rc; + data = buffer; +#if _WIN32_WINNT >= 0x0500 + rc = GetSystemWindowsDirectory (buffer, sizeof (buffer) - 20); +#else + rc = GetWindowsDirectory (buffer, sizeof (buffer) - 20); +#endif + if (rc == 0 || rc > sizeof (buffer) - 20) + { + FcConfigMessage (parse, FcSevereError, "GetSystemWindowsDirectory failed"); + break; + } + if (data [strlen (data) - 1] != '\\') + strcat (data, "\\"); + strcat (data, "fonts"); + } +#endif + if (strlen ((char *) data) == 0) + FcConfigMessage (parse, FcSevereWarning, "empty font directory name ignored"); + else if (!FcStrUsesHome (data) || FcConfigHome ()) + { + if (!FcConfigAddDir (parse->config, data)) + FcConfigMessage (parse, FcSevereError, "out of memory; cannot add directory %s", data); + } + FcStrBufDestroy (&parse->pstack->str); + break; + case FcElementCacheDir: data = FcStrBufDone (&parse->pstack->str); if (!data) { @@ -2023,7 +2107,7 @@ FcEndElement(void *userData, const XML_Char *name) break; } #ifdef _WIN32 - if (strcmp (data, "WINDOWSFONTDIR") == 0) + if (strcmp (data, "WINDOWSTEMPDIR_FONTCONFIG_CACHE") == 0) { int rc; FcStrFree (data); @@ -2034,38 +2118,35 @@ FcEndElement(void *userData, const XML_Char *name) break; } FcMemAlloc (FC_MEM_STRING, 1000); - rc = GetWindowsDirectory (data, 800); + rc = GetTempPath (800, data); if (rc == 0 || rc > 800) { - FcConfigMessage (parse, FcSevereError, "GetWindowsDirectory failed"); + FcConfigMessage (parse, FcSevereError, "GetTempPath failed"); FcStrFree (data); break; } if (data [strlen (data) - 1] != '\\') strcat (data, "\\"); - strcat (data, "fonts"); + strcat (data, "fontconfig\\cache"); } #endif if (!FcStrUsesHome (data) || FcConfigHome ()) { - if (!FcConfigAddDir (parse->config, data)) - FcConfigMessage (parse, FcSevereError, "out of memory; cannot add directory %s", data); + if (!FcConfigAddCacheDir (parse->config, data)) + FcConfigMessage (parse, FcSevereError, "out of memory; cannot add cache directory %s", data); } FcStrFree (data); break; + case FcElementCache: - data = FcStrBufDone (&parse->pstack->str); + data = FcStrBufDoneStatic (&parse->pstack->str); if (!data) { FcConfigMessage (parse, FcSevereError, "out of memory"); break; } - if (!FcStrUsesHome (data) || FcConfigHome ()) - { - if (!FcConfigSetCache (parse->config, data)) - FcConfigMessage (parse, FcSevereError, "out of memory"); - } - FcStrFree (data); + /* discard this data; no longer used */ + FcStrBufDestroy (&parse->pstack->str); break; case FcElementInclude: FcParseInclude (parse); @@ -2264,6 +2345,14 @@ FcEndDoctypeDecl (void *userData) #endif /* ENABLE_LIBXML2 */ +static int +FcSortCmpStr (const void *a, const void *b) +{ + const FcChar8 *as = *((FcChar8 **) a); + const FcChar8 *bs = *((FcChar8 **) b); + return FcStrCmp (as, bs); +} + static FcBool FcConfigParseAndLoadDir (FcConfig *config, const FcChar8 *name, @@ -2333,7 +2422,7 @@ FcConfigParseAndLoadDir (FcConfig *config, { int i; qsort (files->strs, files->num, sizeof (FcChar8 *), - (int (*)(const void *, const void *)) FcStrCmp); + (int (*)(const void *, const void *)) FcSortCmpStr); for (i = 0; ret && i < files->num; i++) ret = FcConfigParseAndLoad (config, files->strs[i], complain); } @@ -2476,3 +2565,6 @@ bail0: } return FcTrue; } +#define __fcxml__ +#include "fcaliastail.h" +#undef __fcxml__