summaryrefslogtreecommitdiff
path: root/xc/lib/Xft/xftlist.c
blob: 478dce07fbbd743755c7ed08ae69daf6c791e457 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * $XFree86: xc/lib/Xft/xftlist.c,v 1.2 2000/12/07 23:57:28 keithp Exp $
 *
 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Keith Packard not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Keith Packard makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdlib.h>
#include "xftint.h"

XftObjectSet *
XftObjectSetCreate (void)
{
    XftObjectSet    *os;

    os = (XftObjectSet *) malloc (sizeof (XftObjectSet));
    if (!os)
	return 0;
    os->nobject = 0;
    os->sobject = 0;
    os->objects = 0;
    return os;
}

Bool
XftObjectSetAdd (XftObjectSet *os, const char *object)
{
    int		s;
    const char	**objects;
    
    if (os->nobject == os->sobject)
    {
	s = os->sobject + 4;
	if (os->objects)
	    objects = (const char **) realloc ((void *) os->objects,
					       s * sizeof (const char **));
	else
	    objects = (const char **) malloc (s * sizeof (const char **));
	if (!objects)
	    return False;
	os->objects = objects;
	os->sobject = s;
    }
    os->objects[os->nobject++] = object;
    return True;
}

void
XftObjectSetDestroy (XftObjectSet *os)
{
    if (os->objects)
	free ((void *) os->objects);
    free (os);
}

#define _XftObjectSetVapBuild(__ret__, __first__, __va__) 		\
{									\
    XftObjectSet    *__os__;						\
    const char	    *__ob__;						\
									\
    __ret__ = 0;						    	\
    __os__ = XftObjectSetCreate ();					\
    if (!__os__)							\
	goto _XftObjectSetVapBuild_bail0;				\
    __ob__ = __first__;							\
    while (__ob__)							\
    {									\
	if (!XftObjectSetAdd (__os__, __ob__))				\
	    goto _XftObjectSetVapBuild_bail1;				\
	__ob__ = va_arg (__va__, const char *);				\
    }									\
    __ret__ = __os__;							\
									\
_XftObjectSetVapBuild_bail1:						\
    if (!__ret__ && __os__)					    	\
	XftObjectSetDestroy (__os__);					\
_XftObjectSetVapBuild_bail0:						\
    ;									\
}

XftObjectSet *
XftObjectSetVaBuild (const char *first, va_list va)
{
    XftObjectSet    *ret;

    _XftObjectSetVapBuild (ret, first, va);
    return ret;
}

XftObjectSet *
XftObjectSetBuild (const char *first, ...)
{
    va_list	    va;
    XftObjectSet    *os;

    va_start (va, first);
    _XftObjectSetVapBuild (os, first, va);
    va_end (va);
    return os;
}

Bool
XftListValueCompare (XftValue	v1,
		     XftValue	v2)
{
    return _XftConfigCompareValue (v1, XftOpEqual, v2);
}

Bool
XftListValueListCompare (XftValueList	*v1orig,
			 XftValueList	*v2orig,
			 XftQual	qual)
{
    XftValueList    *v1, *v2;

    for (v1 = v1orig; v1; v1 = v1->next)
    {
	for (v2 = v2orig; v2; v2 = v2->next)
	{
	    if (_XftConfigCompareValue (v1->value, XftOpEqual, v2->value))
	    {
		if (qual == XftQualAny)
		    return True;
		else
		    break;
	    }
	}
	if (qual == XftQualAll)
	{
	    if (!v2)
		return False;
	}
    }
    if (qual == XftQualAll)
	return True;
    else
	return False;
}

/*
 * True iff all objects in "p" match "font"
 */
Bool
XftListMatch (XftPattern    *p,
	      XftPattern    *font,
	      XftQual	    qual)
{
    int		    i;
    XftPatternElt   *e;

    for (i = 0; i < p->num; i++)
    {
	e = XftPatternFind (font, p->elts[i].object, False);
	if (!e)
	{
	    if (qual == XftQualAll)
		continue;
	    else
		return False;
	}
	if (!XftListValueListCompare (p->elts[i].values, e->values, qual))
	    return False;
    }
    return True;
}

Bool
XftListAppend (XftFontSet   *s,
	       XftPattern   *font,
	       XftObjectSet *os)
{
    int		    f;
    int		    o;
    XftPattern	    *l;
    XftPatternElt   *e;
    XftValueList    *v;

    for (f = 0; f < s->nfont; f++)
    {
	l = s->fonts[f];
	if (XftListMatch (l, font, XftQualAll))
	    return True;
    }
    l = XftPatternCreate ();
    if (!l)
	goto bail0;
    for (o = 0; o < os->nobject; o++)
    {
	e = XftPatternFind (font, os->objects[o], False);
	if (e)
	{
	    for (v = e->values; v; v = v->next)
	    {
		if (!XftPatternAdd (l, os->objects[o], v->value, True))
		    goto bail1;
	    }
	}
    }
    if (!XftFontSetAdd (s, l))
	goto bail1;
    return True;
bail1:
    XftPatternDestroy (l);
bail0:
    return False;
}

XftFontSet *
XftListFontSets (XftFontSet	**sets,
		 int		nsets,
		 XftPattern	*p,
		 XftObjectSet	*os)
{
    XftFontSet	*ret;
    XftFontSet	*s;
    int		f;
    int		set;

    ret = XftFontSetCreate ();
    if (!ret)
	goto bail0;
    for (set = 0; set < nsets; set++)
    {
	s = sets[set];
	for (f = 0; f < s->nfont; f++)
	{
	    if (XftListMatch (p, s->fonts[f], XftQualAny))
	    {
		if (!XftListAppend (ret, s->fonts[f], os))
		    goto bail1;
	    }
	}
    }
    return ret;
bail1:
    XftFontSetDestroy (ret);
bail0:
    return 0;
}

XftFontSet *
XftListFontsPatternObjects (Display	    *dpy,
			    int		    screen,
			    XftPattern	    *pattern,
			    XftObjectSet    *os)
{
    XftFontSet	*sets[2];
    int		nsets = 0;
#ifdef FREETYPE2
    Bool	core, render;
    XftResult	result;
#endif

    if (!XftInit (0))
	return 0;

#ifdef FREETYPE2
    render = core = False;
    result = XftPatternGetBool (pattern, XFT_CORE, 0, &core);
    if (result != XftResultMatch)
	core = XftDefaultGetBool (dpy, XFT_CORE, screen,
				  !XftDefaultHasRender (dpy));

    result = XftPatternGetBool (pattern, XFT_RENDER, 0, &render);
    if (result != XftResultMatch)
	render = XftDefaultGetBool (dpy, XFT_RENDER, screen,
				    XftDefaultHasRender (dpy));
    if (render)
    {
	if (XftInitFtLibrary ())
	{
	    sets[nsets] = _XftFontSet;
	    if (sets[nsets])
		nsets++;
	}
    }
    if (core)
#endif
    {
	sets[nsets] = XftDisplayGetFontSet (dpy);
	if (sets[nsets])
	    nsets++;
    }
    return XftListFontSets (sets, nsets, pattern, os);
}

XftFontSet *
XftListFonts (Display	*dpy,
	      int	screen,
	      ...)
{
    va_list	    va;
    XftFontSet	    *fs;
    XftObjectSet    *os;
    XftPattern	    *pattern;
    const char	    *first;

    va_start (va, screen);
    
    _XftPatternVapBuild (pattern, 0, va);
    
    first = va_arg (va, const char *);
    _XftObjectSetVapBuild (os, first, va);
    
    va_end (va);
    
    fs = XftListFontsPatternObjects (dpy, screen, pattern, os);
    XftPatternDestroy (pattern);
    XftObjectSetDestroy (os);
    return fs;
}