summaryrefslogtreecommitdiff
path: root/pango/mini-xft/minixftdpy.c
blob: e97077b9acd350c3e6826532b7e9fece62c4c8e7 (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
/*
 * $XFree86: xc/lib/MiniXft/xftdpy.c,v 1.7 2001/04/29 03:21:17 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 <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "minixftint.h"

MiniXftDisplayInfo	*_MiniXftDisplayInfo;

static MiniXftDisplayInfo *
_MiniXftDisplayInfoGet (Display *dpy)
{
    MiniXftDisplayInfo  *info, **prev;

    for (prev = &_MiniXftDisplayInfo; (info = *prev); prev = &(*prev)->next)
    {
	if (info->display == dpy)
	{
	    /*
	     * MRU the list
	     */
	    if (prev != &_MiniXftDisplayInfo)
	    {
		*prev = info->next;
		info->next = _MiniXftDisplayInfo;
		_MiniXftDisplayInfo = info;
	    }
	    return info;
	}
    }
    info = (MiniXftDisplayInfo *) malloc (sizeof (MiniXftDisplayInfo));
    if (!info)
	goto bail0;
    
    info->display = dpy;
    info->defaults = 0;
    
    info->next = _MiniXftDisplayInfo;
    _MiniXftDisplayInfo = info;
    return info;
    
bail0:
    if (_MiniXftFontDebug () & XFT_DBG_RENDER)
    {
	printf ("MiniXftDisplayInfoGet failed to initialize, MiniXft unhappy\n");
    }
    return 0;
}

Bool
MiniXftDefaultHasRender (Display *dpy)
{
    return True;
}

Bool
MiniXftDefaultSet (Display *dpy, MiniXftPattern *defaults)
{
    MiniXftDisplayInfo  *info = _MiniXftDisplayInfoGet (dpy);

    if (!info)
	return False;
    if (info->defaults)
	MiniXftPatternDestroy (info->defaults);
    info->defaults = defaults;
    return True;
}

int
MiniXftDefaultParseBool (char *v)
{
    unsigned char    c0, c1;

    c0 = *v;
    if (isupper (c0))
	c0 = tolower (c0);
    if (c0 == 't' || c0 == 'y' || c0 == '1')
	return 1;
    if (c0 == 'f' || c0 == 'n' || c0 == '0')
	return 0;
    if (c0 == 'o')
    {
	c1 = v[1];
	if (isupper (c1))
	    c1 = tolower (c1);
	if (c1 == 'n')
	    return 1;
	if (c1 == 'f')
	    return 0;
    }
    return -1;
}

static Bool
_MiniXftDefaultInitBool (Display *dpy, MiniXftPattern *pat, char *option)
{
    return True;
}

static Bool
_MiniXftDefaultInitDouble (Display *dpy, MiniXftPattern *pat, char *option)
{
    return True;
}

static Bool
_MiniXftDefaultInitInteger (Display *dpy, MiniXftPattern *pat, char *option)
{
    return True;
}

static MiniXftPattern *
_MiniXftDefaultInit (Display *dpy)
{
    MiniXftPattern	*pat;

    pat = MiniXftPatternCreate ();
    if (!pat)
	goto bail0;

    if (!_MiniXftDefaultInitBool (dpy, pat, XFT_CORE))
	goto bail1;
    if (!_MiniXftDefaultInitDouble (dpy, pat, XFT_SCALE))
	goto bail1;
    if (!_MiniXftDefaultInitDouble (dpy, pat, XFT_DPI))
	goto bail1;
    if (!_MiniXftDefaultInitBool (dpy, pat, XFT_RENDER))
	goto bail1;
    if (!_MiniXftDefaultInitInteger (dpy, pat, XFT_RGBA))
	goto bail1;
    if (!_MiniXftDefaultInitBool (dpy, pat, XFT_ANTIALIAS))
	goto bail1;
    if (!_MiniXftDefaultInitBool (dpy, pat, XFT_MINSPACE))
	goto bail1;
    
    return pat;
    
bail1:
    MiniXftPatternDestroy (pat);
bail0:
    return 0;
}

static MiniXftResult
_MiniXftDefaultGet (Display *dpy, const char *object, int screen, MiniXftValue *v)
{
    MiniXftDisplayInfo  *info = _MiniXftDisplayInfoGet (dpy);
    MiniXftResult	    r;

    if (!info)
	return MiniXftResultNoMatch;
    
    if (!info->defaults)
    {
	info->defaults = _MiniXftDefaultInit (dpy);
	if (!info->defaults)
	    return MiniXftResultNoMatch;
    }
    r = MiniXftPatternGet (info->defaults, object, screen, v);
    if (r == MiniXftResultNoId && screen > 0)
	r = MiniXftPatternGet (info->defaults, object, 0, v);
    return r;
}

Bool
MiniXftDefaultGetBool (Display *dpy, const char *object, int screen, Bool def)
{
    MiniXftResult	    r;
    MiniXftValue	    v;

    r = _MiniXftDefaultGet (dpy, object, screen, &v);
    if (r != MiniXftResultMatch || v.type != MiniXftTypeBool)
	return def;
    return v.u.b;
}

int
MiniXftDefaultGetInteger (Display *dpy, const char *object, int screen, int def)
{
    MiniXftResult	    r;
    MiniXftValue	    v;

    r = _MiniXftDefaultGet (dpy, object, screen, &v);
    if (r != MiniXftResultMatch || v.type != MiniXftTypeInteger)
	return def;
    return v.u.i;
}

double
MiniXftDefaultGetDouble (Display *dpy, const char *object, int screen, double def)
{
    MiniXftResult	    r;
    MiniXftValue	    v;

    r = _MiniXftDefaultGet (dpy, object, screen, &v);
    if (r != MiniXftResultMatch || v.type != MiniXftTypeDouble)
	return def;
    return v.u.d;
}

MiniXftFontSet *
MiniXftDisplayGetFontSet (Display *dpy)
{
    return 0;
}

static double default_dpi = 0.0;

void
MiniXftSetDPI (double dpi)
{
  default_dpi = dpi;
}

void
MiniXftDefaultSubstitute (Display *dpy, int screen, MiniXftPattern *pattern)
{
    MiniXftValue	v;
    double	size;
    double	scale;

    if (MiniXftPatternGet (pattern, XFT_STYLE, 0, &v) == MiniXftResultNoMatch)
    {
	if (MiniXftPatternGet (pattern, XFT_WEIGHT, 0, &v) == MiniXftResultNoMatch )
	{
	    MiniXftPatternAddInteger (pattern, XFT_WEIGHT, XFT_WEIGHT_MEDIUM);
	}
	if (MiniXftPatternGet (pattern, XFT_SLANT, 0, &v) == MiniXftResultNoMatch)
	{
	    MiniXftPatternAddInteger (pattern, XFT_SLANT, XFT_SLANT_ROMAN);
	}
    }
    if (MiniXftPatternGet (pattern, XFT_ENCODING, 0, &v) == MiniXftResultNoMatch)
	MiniXftPatternAddString (pattern, XFT_ENCODING, "iso8859-1");
    if (MiniXftPatternGet (pattern, XFT_RENDER, 0, &v) == MiniXftResultNoMatch)
    {
	MiniXftPatternAddBool (pattern, XFT_RENDER,
			   MiniXftDefaultGetBool (dpy, XFT_RENDER, screen, 
					      MiniXftDefaultHasRender (dpy)));
    }
    if (MiniXftPatternGet (pattern, XFT_CORE, 0, &v) == MiniXftResultNoMatch)
    {
	MiniXftPatternAddBool (pattern, XFT_CORE,
			   MiniXftDefaultGetBool (dpy, XFT_CORE, screen, 
					      !MiniXftDefaultHasRender (dpy)));
    }
    if (MiniXftPatternGet (pattern, XFT_ANTIALIAS, 0, &v) == MiniXftResultNoMatch)
    {
	MiniXftPatternAddBool (pattern, XFT_ANTIALIAS,
			   MiniXftDefaultGetBool (dpy, XFT_ANTIALIAS, screen,
					      True));
    }
    if (MiniXftPatternGet (pattern, XFT_RGBA, 0, &v) == MiniXftResultNoMatch)
    {
	MiniXftPatternAddInteger (pattern, XFT_RGBA,
			      MiniXftDefaultGetInteger (dpy, XFT_RGBA, screen, 
						    XFT_RGBA_NONE));
    }
    if (MiniXftPatternGet (pattern, XFT_MINSPACE, 0, &v) == MiniXftResultNoMatch)
    {
	MiniXftPatternAddBool (pattern, XFT_MINSPACE,
			   MiniXftDefaultGetBool (dpy, XFT_MINSPACE, screen,
					      False));
    }
    if (MiniXftPatternGet (pattern, XFT_PIXEL_SIZE, 0, &v) == MiniXftResultNoMatch)
    {
	double	dpi;

	if (MiniXftPatternGet (pattern, XFT_SIZE, 0, &v) != MiniXftResultMatch)
	{
	    size = 12.0;
	    MiniXftPatternAddDouble (pattern, XFT_SIZE, size);
	}
	else
	{
	    switch (v.type) {
	    case MiniXftTypeInteger:
		size = (double) v.u.i;
		break;
	    case MiniXftTypeDouble:
		size = v.u.d;
		break;
	    default:
		size = 12.0;
		break;
	    }
	}
	scale = MiniXftDefaultGetDouble (dpy, XFT_SCALE, screen, 1.0);
	size *= scale;
	if (default_dpi > 0.0)
	  dpi = default_dpi;
	else
	  dpi = 72.0;
	dpi = MiniXftDefaultGetDouble (dpy, XFT_DPI, screen, dpi);
	size = size * dpi / 72.0;
	MiniXftPatternAddDouble (pattern, XFT_PIXEL_SIZE, size);
    }
}