2 * fontconfig/src/fcrange.c
4 * Copyright © 2002 Keith Packard
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.
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.
29 FcRangeCreateDouble (double begin, double end)
31 FcRange *ret = malloc (sizeof (FcRange));
35 ret->is_double = FcTrue;
36 ret->is_inclusive = FcDoubleCmpEQ (begin, end);
37 ret->u.d.begin = begin;
45 FcRangeCreateInteger (FcChar32 begin, FcChar32 end)
47 FcRange *ret = malloc (sizeof (FcRange));
51 ret->is_double = FcFalse;
52 ret->is_inclusive = (begin == end);
53 ret->u.i.begin = begin;
61 FcRangeDestroy (FcRange *range)
67 FcRangeCopy (const FcRange *range)
72 ret = FcRangeCreateDouble (range->u.d.begin, range->u.d.end);
74 ret = FcRangeCreateInteger (range->u.i.begin, range->u.i.end);
80 FcRangeGetDouble(const FcRange *range, double *begin, double *end)
87 *begin = range->u.d.begin;
89 *end = range->u.d.end;
94 *begin = (double)range->u.i.begin;
96 *end = (double)range->u.i.end;
103 FcRangeCanonicalize (const FcRange *range)
107 if (range->is_double)
111 new.is_double = FcTrue;
112 new.is_inclusive = range->is_inclusive;
113 new.u.d.begin = (double)range->u.i.begin;
114 new.u.d.end = (double)range->u.i.end;
120 FcRangePromote (double v, FcValuePromotionBuffer *vbuf)
124 } FcRangePromotionBuffer;
125 FcRangePromotionBuffer *buf = (FcRangePromotionBuffer *) vbuf;
127 FC_ASSERT_STATIC (sizeof (FcRangePromotionBuffer) <= sizeof (FcValuePromotionBuffer));
128 buf->r.is_double = FcTrue;
129 buf->r.is_inclusive = FcTrue;
130 buf->r.u.d.begin = v;
137 FcRangeIsZero (const FcRange *r)
143 c = FcRangeCanonicalize (r);
145 return FcDoubleIsZero (c.u.d.begin) && FcDoubleIsZero (c.u.d.end);
149 FcRangeIsInRange (const FcRange *a, const FcRange *b)
157 ca = FcRangeCanonicalize (a);
158 cb = FcRangeCanonicalize (b);
159 if (ca.is_inclusive & cb.is_inclusive)
160 f = ca.u.d.end <= cb.u.d.end;
162 f = ca.u.d.end < cb.u.d.end;
164 return FcDoubleCmpGE (ca.u.d.begin, cb.u.d.begin) && f;
168 FcRangeCompare (FcOp op, const FcRange *a, const FcRange *b)
176 return FcRangeIsInRange (a, b);
178 case FcOpNotContains:
179 return !FcRangeIsInRange (a, b);
181 ca = FcRangeCanonicalize (a);
182 cb = FcRangeCanonicalize (b);
183 return ca.u.d.begin < cb.u.d.begin;
185 ca = FcRangeCanonicalize (a);
186 cb = FcRangeCanonicalize (b);
187 return FcDoubleCmpLE (ca.u.d.begin, cb.u.d.begin);
189 ca = FcRangeCanonicalize (a);
190 cb = FcRangeCanonicalize (b);
191 return ca.u.d.end > cb.u.d.end;
193 ca = FcRangeCanonicalize (a);
194 cb = FcRangeCanonicalize (b);
195 return FcDoubleCmpGE (ca.u.d.end, cb.u.d.end);
203 FcRangeHash (const FcRange *r)
205 FcRange c = FcRangeCanonicalize (r);
206 int b = (int) (c.u.d.begin * 100);
207 int e = (int) (c.u.d.end * 100);
209 return b ^ (b << 1) ^ (e << 9);
213 FcRangeSerializeAlloc (FcSerialize *serialize, const FcRange *r)
215 if (!FcSerializeAlloc (serialize, r, sizeof (FcRange)))
221 FcRangeSerialize (FcSerialize *serialize, const FcRange *r)
223 FcRange *r_serialize = FcSerializePtr (serialize, r);
227 memcpy (r_serialize, r, sizeof (FcRange));
233 #include "fcaliastail.h"