fcrange.c revision a32e9e42
1/*
2 * fontconfig/src/fcrange.c
3 *
4 * Copyright © 2002 Keith Packard
5 *
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.
15 *
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.
23 */
24
25#include "fcint.h"
26
27
28FcRange *
29FcRangeCreateDouble (double begin, double end)
30{
31    FcRange *ret = malloc (sizeof (FcRange));
32
33    if (ret)
34    {
35	ret->begin = begin;
36	ret->end = end;
37    }
38
39    return ret;
40}
41
42FcRange *
43FcRangeCreateInteger (FcChar32 begin, FcChar32 end)
44{
45    FcRange *ret = malloc (sizeof (FcRange));
46
47    if (ret)
48    {
49	ret->begin = begin;
50	ret->end = end;
51    }
52
53    return ret;
54}
55
56void
57FcRangeDestroy (FcRange *range)
58{
59    free (range);
60}
61
62FcRange *
63FcRangeCopy (const FcRange *range)
64{
65    return FcRangeCreateDouble (range->begin, range->end);
66}
67
68FcBool
69FcRangeGetDouble(const FcRange *range, double *begin, double *end)
70{
71    if (!range)
72	return FcFalse;
73    if (begin)
74	*begin = range->begin;
75    if (end)
76	*end = range->end;
77
78    return FcTrue;
79}
80
81FcRange *
82FcRangePromote (double v, FcValuePromotionBuffer *vbuf)
83{
84    typedef struct {
85	FcRange	r;
86    } FcRangePromotionBuffer;
87    FcRangePromotionBuffer *buf = (FcRangePromotionBuffer *) vbuf;
88
89    FC_ASSERT_STATIC (sizeof (FcRangePromotionBuffer) <= sizeof (FcValuePromotionBuffer));
90    buf->r.begin = v;
91    buf->r.end = v;
92
93    return &buf->r;
94}
95
96FcBool
97FcRangeIsInRange (const FcRange *a, const FcRange *b)
98{
99    return a->begin >= b->begin && a->end <= b->end;
100}
101
102FcBool
103FcRangeCompare (FcOp op, const FcRange *a, const FcRange *b)
104{
105    switch ((int) op) {
106    case FcOpEqual:
107	return a->begin == b->begin && a->end == b->end;
108    case FcOpContains:
109    case FcOpListing:
110	return FcRangeIsInRange (a, b);
111    case FcOpNotEqual:
112	return a->begin != b->begin || a->end != b->end;
113    case FcOpNotContains:
114	return !FcRangeIsInRange (a, b);
115    case FcOpLess:
116	return a->end < b->begin;
117    case FcOpLessEqual:
118	return a->end <= b->begin;
119    case FcOpMore:
120	return a->begin > b->end;
121    case FcOpMoreEqual:
122	return a->begin >= b->end;
123    default:
124	break;
125    }
126    return FcFalse;
127}
128
129FcChar32
130FcRangeHash (const FcRange *r)
131{
132    int b = (int) (r->begin * 100);
133    int e = (int) (r->end * 100);
134
135    return b ^ (b << 1) ^ (e << 9);
136}
137
138FcBool
139FcRangeSerializeAlloc (FcSerialize *serialize, const FcRange *r)
140{
141    if (!FcSerializeAlloc (serialize, r, sizeof (FcRange)))
142	return FcFalse;
143    return FcTrue;
144}
145
146FcRange *
147FcRangeSerialize (FcSerialize *serialize, const FcRange *r)
148{
149    FcRange *r_serialize = FcSerializePtr (serialize, r);
150
151    if (!r_serialize)
152	return NULL;
153    memcpy (r_serialize, r, sizeof (FcRange));
154
155    return r_serialize;
156}
157
158#define __fcrange__
159#include "fcaliastail.h"
160#undef __fcrange__
161