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