Debian Codegraph @

src/fcrange.c

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