From 5bad26ccb6686f1b9c8df6c1e9b49a72d42ad661 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 20 Aug 2014 16:07:26 -0400 Subject: [PATCH] Simplify FcRange --- src/fcdbg.c | 10 +---- src/fcint.h | 25 +----------- src/fcmatch.c | 2 +- src/fcname.c | 14 +------ src/fcrange.c | 109 +++++++++----------------------------------------- src/fcxml.c | 12 ++---- 6 files changed, 28 insertions(+), 144 deletions(-) diff --git a/src/fcdbg.c b/src/fcdbg.c index 7a1928a..985fee9 100644 --- a/src/fcdbg.c +++ b/src/fcdbg.c @@ -29,8 +29,6 @@ static void _FcValuePrintFile (FILE *f, const FcValue v) { - FcRange r; - switch (v.type) { case FcTypeUnknown: fprintf (f, ""); @@ -64,8 +62,7 @@ _FcValuePrintFile (FILE *f, const FcValue v) fprintf (f, "face"); break; case FcTypeRange: - r = FcRangeCanonicalize (v.u.r); - fprintf (f, "[%g %g)", r.u.d.begin, r.u.d.end); + fprintf (f, "[%g %g)", v.u.r->begin, v.u.r->end); break; } } @@ -267,8 +264,6 @@ FcOpPrint (FcOp op_) void FcExprPrint (const FcExpr *expr) { - FcRange r; - if (!expr) printf ("none"); else switch (FC_OP_GET_OP (expr->op)) { case FcOpInteger: printf ("%d", expr->u.ival); break; @@ -286,8 +281,7 @@ FcExprPrint (const FcExpr *expr) printf ("]"); break; case FcOpRange: - r = FcRangeCanonicalize (expr->u.rval); - printf ("(%g, %g)", r.u.d.begin, r.u.d.end); + printf ("(%g, %g)", expr->u.rval->begin, expr->u.rval->end); break; case FcOpBool: printf ("%s", expr->u.bval ? "true" : "false"); break; case FcOpCharSet: printf ("charset\n"); break; diff --git a/src/fcint.h b/src/fcint.h index 83815f7..bc4f924 100644 --- a/src/fcint.h +++ b/src/fcint.h @@ -96,11 +96,6 @@ extern pfnSHGetFolderPathA pSHGetFolderPathA; #define FC_MAX(a,b) ((a) > (b) ? (a) : (b)) #define FC_ABS(a) ((a) < 0 ? -(a) : (a)) -#define FcDoubleIsZero(a) (fabs ((a)) <= DBL_EPSILON) -#define FcDoubleCmpEQ(a,b) (fabs ((a) - (b)) <= DBL_EPSILON) -#define FcDoubleCmpGE(a,b) (FcDoubleCmpEQ (a, b) || (a) > (b)) -#define FcDoubleCmpLE(a,b) (FcDoubleCmpEQ (a, b) || (a) < (b)) - /* slim_internal.h */ #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && defined(__ELF__) && !defined(__sun) #define FcPrivate __attribute__((__visibility__("hidden"))) @@ -252,21 +247,9 @@ typedef struct _FcExprName { FcMatchKind kind; } FcExprName; -typedef struct _FcRangeInt { - FcChar32 begin; - FcChar32 end; -} FcRangeInt; -typedef struct _FcRangeDouble { +struct _FcRange { double begin; double end; -} FcRangeDouble; -struct _FcRange { - FcBool is_double; - FcBool is_inclusive; - union { - FcRangeInt i; - FcRangeDouble d; - } u; }; @@ -1083,15 +1066,9 @@ FcMatrixFree (FcMatrix *mat); /* fcrange.c */ -FcPrivate FcRange -FcRangeCanonicalize (const FcRange *range); - FcPrivate FcRange * FcRangePromote (double v, FcValuePromotionBuffer *vbuf); -FcPrivate FcBool -FcRangeIsZero (const FcRange *r); - FcPrivate FcBool FcRangeIsInRange (const FcRange *a, const FcRange *b); diff --git a/src/fcmatch.c b/src/fcmatch.c index 623d4aa..7fbaf0e 100644 --- a/src/fcmatch.c +++ b/src/fcmatch.c @@ -220,7 +220,7 @@ FcCompareSizeRange (FcValue *v1, FcValue *v2) if (FcRangeIsInRange (r1, r2)) ret = 0.0; else - ret = FC_MIN (fabs (r1->u.d.end - r2->u.d.begin), fabs (r1->u.d.begin - r2->u.d.end)); + ret = FC_MIN (fabs (r1->end - r2->begin), fabs (r1->begin - r2->end)); bail: if (r1) diff --git a/src/fcname.c b/src/fcname.c index aec6114..8be36c7 100644 --- a/src/fcname.c +++ b/src/fcname.c @@ -500,7 +500,6 @@ FcNameUnparseValue (FcStrBuf *buf, { FcChar8 temp[1024]; FcValue v = FcValueCanonicalize(v0); - FcRange r; switch (v.type) { case FcTypeUnknown: @@ -527,17 +526,8 @@ FcNameUnparseValue (FcStrBuf *buf, case FcTypeFTFace: return FcTrue; case FcTypeRange: - r = FcRangeCanonicalize (v.u.r); - if (!FcDoubleIsZero (r.u.d.begin) || !FcDoubleIsZero (r.u.d.end)) - { - if (FcDoubleCmpEQ (r.u.d.begin, r.u.d.end)) - sprintf ((char *) temp, "%g", r.u.d.begin); - else - sprintf ((char *) temp, "[%g %g)", r.u.d.begin, r.u.d.end); - return FcNameUnparseString (buf, temp, 0); - } - else - return FcTrue; + sprintf ((char *) temp, "[%g %g)", v.u.r->begin, v.u.r->end); + return FcNameUnparseString (buf, temp, 0); } return FcFalse; } diff --git a/src/fcrange.c b/src/fcrange.c index 9b1b67b..f70226c 100644 --- a/src/fcrange.c +++ b/src/fcrange.c @@ -32,10 +32,8 @@ FcRangeCreateDouble (double begin, double end) if (ret) { - ret->is_double = FcTrue; - ret->is_inclusive = FcDoubleCmpEQ (begin, end); - ret->u.d.begin = begin; - ret->u.d.end = end; + ret->begin = begin; + ret->end = end; } return ret; @@ -48,10 +46,8 @@ FcRangeCreateInteger (FcChar32 begin, FcChar32 end) if (ret) { - ret->is_double = FcFalse; - ret->is_inclusive = (begin == end); - ret->u.i.begin = begin; - ret->u.i.end = end; + ret->begin = begin; + ret->end = end; } return ret; @@ -66,14 +62,7 @@ FcRangeDestroy (FcRange *range) FcRange * FcRangeCopy (const FcRange *range) { - FcRange *ret; - - if (range->is_double) - ret = FcRangeCreateDouble (range->u.d.begin, range->u.d.end); - else - ret = FcRangeCreateInteger (range->u.i.begin, range->u.i.end); - - return ret; + return FcRangeCreateDouble (range->begin, range->end); } FcBool @@ -81,41 +70,14 @@ FcRangeGetDouble(const FcRange *range, double *begin, double *end) { if (!range) return FcFalse; - if (range->is_double) - { - if (begin) - *begin = range->u.d.begin; - if (end) - *end = range->u.d.end; - } - else - { - if (begin) - *begin = (double)range->u.i.begin; - if (end) - *end = (double)range->u.i.end; - } + if (begin) + *begin = range->begin; + if (end) + *end = range->end; return FcTrue; } -FcRange -FcRangeCanonicalize (const FcRange *range) -{ - FcRange new; - - if (range->is_double) - new = *range; - else - { - new.is_double = FcTrue; - new.is_inclusive = range->is_inclusive; - new.u.d.begin = (double)range->u.i.begin; - new.u.d.end = (double)range->u.i.end; - } - return new; -} - FcRange * FcRangePromote (double v, FcValuePromotionBuffer *vbuf) { @@ -125,50 +87,24 @@ FcRangePromote (double v, FcValuePromotionBuffer *vbuf) FcRangePromotionBuffer *buf = (FcRangePromotionBuffer *) vbuf; FC_ASSERT_STATIC (sizeof (FcRangePromotionBuffer) <= sizeof (FcValuePromotionBuffer)); - buf->r.is_double = FcTrue; - buf->r.is_inclusive = FcTrue; - buf->r.u.d.begin = v; - buf->r.u.d.end = v; + buf->r.begin = v; + buf->r.end = v; return &buf->r; } -FcBool -FcRangeIsZero (const FcRange *r) -{ - FcRange c; - - if (!r) - return FcFalse; - c = FcRangeCanonicalize (r); - - return FcDoubleIsZero (c.u.d.begin) && FcDoubleIsZero (c.u.d.end); -} - FcBool FcRangeIsInRange (const FcRange *a, const FcRange *b) { - FcRange ca, cb; - FcBool f; - if (!a || !b) return FcFalse; - ca = FcRangeCanonicalize (a); - cb = FcRangeCanonicalize (b); - if (ca.is_inclusive & cb.is_inclusive) - f = ca.u.d.end <= cb.u.d.end; - else - f = ca.u.d.end < cb.u.d.end; - - return FcDoubleCmpGE (ca.u.d.begin, cb.u.d.begin) && f; + return a->begin >= b->begin && a->end <= b->end; } FcBool FcRangeCompare (FcOp op, const FcRange *a, const FcRange *b) { - FcRange ca, cb; - switch ((int) op) { case FcOpEqual: case FcOpContains: @@ -178,21 +114,13 @@ FcRangeCompare (FcOp op, const FcRange *a, const FcRange *b) case FcOpNotContains: return !FcRangeIsInRange (a, b); case FcOpLess: - ca = FcRangeCanonicalize (a); - cb = FcRangeCanonicalize (b); - return ca.u.d.begin < cb.u.d.begin; + return a->begin < b->begin; case FcOpLessEqual: - ca = FcRangeCanonicalize (a); - cb = FcRangeCanonicalize (b); - return FcDoubleCmpLE (ca.u.d.begin, cb.u.d.begin); + return a->begin <= b->begin; case FcOpMore: - ca = FcRangeCanonicalize (a); - cb = FcRangeCanonicalize (b); - return ca.u.d.end > cb.u.d.end; + return a->end > b->end; case FcOpMoreEqual: - ca = FcRangeCanonicalize (a); - cb = FcRangeCanonicalize (b); - return FcDoubleCmpGE (ca.u.d.end, cb.u.d.end); + return a->end >= b->end; default: break; } @@ -202,9 +130,8 @@ FcRangeCompare (FcOp op, const FcRange *a, const FcRange *b) FcChar32 FcRangeHash (const FcRange *r) { - FcRange c = FcRangeCanonicalize (r); - int b = (int) (c.u.d.begin * 100); - int e = FcDoubleCmpEQ (c.u.d.end, DBL_MAX) ? INT_MAX : (int) (c.u.d.end * 100); + int b = (int) (r->begin * 100); + int e = (int) (r->end * 100); return b ^ (b << 1) ^ (e << 9); } diff --git a/src/fcxml.c b/src/fcxml.c index c39028e..bad6b81 100644 --- a/src/fcxml.c +++ b/src/fcxml.c @@ -1261,7 +1261,6 @@ FcParseBlank (FcConfigParse *parse) { int n = FcVStackElements (parse); FcChar32 i, begin, end; - FcRange r; while (n-- > 0) { @@ -1278,9 +1277,8 @@ FcParseBlank (FcConfigParse *parse) goto bail; break; case FcVStackRange: - r = FcRangeCanonicalize (v->u.range); - begin = (FcChar32)r.u.d.begin; - end = (FcChar32)r.u.d.end; + begin = (FcChar32) v->u.range->begin; + end = (FcChar32) v->u.range->end; if (begin <= end) { for (i = begin; i <= end; i++) @@ -1593,7 +1591,6 @@ FcParseCharSet (FcConfigParse *parse) FcVStack *vstack; FcCharSet *charset = FcCharSetCreate (); FcChar32 i, begin, end; - FcRange r; int n = 0; while ((vstack = FcVStackPeek (parse))) @@ -1608,9 +1605,8 @@ FcParseCharSet (FcConfigParse *parse) n++; break; case FcVStackRange: - r = FcRangeCanonicalize (vstack->u.range); - begin = (FcChar32)r.u.d.begin; - end = (FcChar32)r.u.d.end; + begin = (FcChar32) vstack->u.range->begin; + end = (FcChar32) vstack->u.range->end; if (begin <= end) {