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
|
/*
* $XFree86: xc/lib/Xft/xftxlfd.c,v 1.1 2000/11/29 08:39:24 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 <string.h>
#include <stdio.h>
#include "xftint.h"
static XftSymbolic XftXlfdWeights[] = {
{ "light", XFT_WEIGHT_LIGHT },
{ "medium", XFT_WEIGHT_MEDIUM },
{ "regular", XFT_WEIGHT_MEDIUM },
{ "demibold", XFT_WEIGHT_DEMIBOLD },
{ "bold", XFT_WEIGHT_BOLD },
{ "black", XFT_WEIGHT_BLACK },
};
#define NUM_XLFD_WEIGHTS (sizeof XftXlfdWeights/sizeof XftXlfdWeights[0])
static XftSymbolic XftXlfdSlants[] = {
{ "r", XFT_SLANT_ROMAN },
{ "i", XFT_SLANT_ITALIC },
{ "o", XFT_SLANT_OBLIQUE },
};
#define NUM_XLFD_SLANTS (sizeof XftXlfdSlants/sizeof XftXlfdSlants[0])
XftPattern *
XftXlfdParse (const char *xlfd_orig, Bool ignore_scalable)
{
XftPattern *pat;
const char *xlfd = xlfd_orig;
const char *foundry;
const char *family;
const char *weight_name;
const char *slant;
const char *registry;
const char *encoding;
char *save;
char style[128];
int pixel;
int point;
int resx;
int resy;
int slant_value, weight_value;
if (*xlfd != '-')
return 0;
if (!(xlfd = strchr (foundry = ++xlfd, '-'))) return 0;
if (!(xlfd = strchr (family = ++xlfd, '-'))) return 0;
if (!(xlfd = strchr (weight_name = ++xlfd, '-'))) return 0;
if (!(xlfd = strchr (slant = ++xlfd, '-'))) return 0;
if (!(xlfd = strchr (/* setwidth_name = */ ++xlfd, '-'))) return 0;
if (!(xlfd = strchr (/* add_style_name = */ ++xlfd, '-'))) return 0;
if (!(xlfd = _XftGetInt (++xlfd, &pixel))) return 0;
if (!(xlfd = _XftGetInt (++xlfd, &point))) return 0;
if (!(xlfd = _XftGetInt (++xlfd, &resx))) return 0;
if (!(xlfd = _XftGetInt (++xlfd, &resy))) return 0;
if (!(xlfd = strchr (/* spacing = */ ++xlfd, '-'))) return 0;
if (!(xlfd = strchr (/* average_width = */ ++xlfd, '-'))) return 0;
if (!(xlfd = strchr (registry = ++xlfd, '-'))) return 0;
/* make sure no fields follow this one */
if ((xlfd = strchr (encoding = ++xlfd, '-'))) return 0;
if (ignore_scalable && !pixel)
return 0;
pat = XftPatternCreate ();
if (!pat)
return 0;
save = (char *) malloc (strlen (foundry) + 1);
if (!save)
return 0;
if (!XftPatternAddString (pat, XFT_XLFD, xlfd_orig)) goto bail;
if (!XftPatternAddBool (pat, XFT_CORE, True)) goto bail;
if (!XftPatternAddString (pat, XFT_FOUNDRY, _XftSplitStr (foundry, save))) goto bail;
if (!XftPatternAddString (pat, XFT_FAMILY, _XftSplitStr (family, save))) goto bail;
weight_value = _XftMatchSymbolic (XftXlfdWeights, NUM_XLFD_WEIGHTS,
_XftSplitStr (weight_name, save),
XFT_WEIGHT_MEDIUM);
if (!XftPatternAddInteger (pat, XFT_WEIGHT, weight_value))
goto bail;
slant_value = _XftMatchSymbolic (XftXlfdSlants, NUM_XLFD_SLANTS,
_XftSplitStr (slant, save),
XFT_SLANT_ROMAN);
if (!XftPatternAddInteger (pat, XFT_SLANT, slant_value))
goto bail;
/*
* Build a style name
*/
style[0] = '\0';
switch (weight_value) {
case XFT_WEIGHT_LIGHT: strcat (style, "light"); break;
case XFT_WEIGHT_DEMIBOLD: strcat (style, "demibold"); break;
case XFT_WEIGHT_BOLD: strcat (style, "bold"); break;
case XFT_WEIGHT_BLACK: strcat (style, "black"); break;
}
if (slant_value != XFT_SLANT_ROMAN) {
if (style[0])
strcat (style, " ");
switch (slant_value) {
case XFT_SLANT_ITALIC: strcat (style, "italic"); break;
case XFT_SLANT_OBLIQUE: strcat (style, "oblique"); break;
}
}
if (!style[0])
strcat (style, "regular");
if (!XftPatternAddString (pat, XFT_STYLE, style))
goto bail;
if (pixel)
if (!XftPatternAddDouble (pat, XFT_PIXEL_SIZE, (double) pixel)) goto bail;
if (!XftPatternAddBool (pat, XFT_SCALABLE, pixel == 0)) goto bail;
if (!XftPatternAddString (pat, XFT_ENCODING, _XftDownStr (registry, save))) goto bail;
free (save);
return pat;
bail:
free (save);
XftPatternDestroy (pat);
return 0;
}
Bool
XftCoreAddFonts (XftFontSet *set, Display *dpy, Bool ignore_scalable)
{
char **xlfds;
int num;
int i;
XftPattern *font;
Bool ret;
xlfds = XListFonts (dpy,
"-*-*-*-*-*-*-*-*-*-*-*-*-*-*",
10000, &num);
if (!xlfds)
return False;
ret = True;
for (i = 0; ret && i < num; i++)
{
font = XftXlfdParse (xlfds[i], ignore_scalable);
if (font)
{
#if 0
printf ("xlfd %s ", xlfds[i]);
XftPatternPrint (font);
#endif
if (!XftFontSetAdd (set, font))
ret = False;
}
}
XFreeFontNames (xlfds);
return ret;
}
XFontStruct *
XftCoreOpen (Display *dpy, XftPattern *pattern)
{
char *xlfd;
char *xlfd_pixel = 0;
char *i, *o;
int d;
Bool scalable;
double pixel_size;
int pixel_int;
XFontStruct *ret;
#if 0
printf ("Core ");
XftPatternPrint (pattern);
#endif
if (XftPatternGetString (pattern, XFT_XLFD, 0, &xlfd) != XftResultMatch)
return 0;
if (XftPatternGetBool (pattern, XFT_SCALABLE, 0, &scalable) != XftResultMatch)
return 0;
if (scalable)
{
if (XftPatternGetDouble (pattern, XFT_PIXEL_SIZE, 0, &pixel_size) != XftResultMatch)
return 0;
pixel_int = (int) (pixel_size + 0.5);
if (pixel_int)
{
xlfd_pixel = (char *) malloc (strlen (xlfd) + 32);
i = xlfd;
o = xlfd_pixel;
d = 0;
while (d != 7 && *i)
{
if ((*o++ = *i++) == '-')
d++;
}
if (*i)
{
sprintf (o, "%d", pixel_int);
o += strlen (o);
while (*i != '-')
++i;
}
while ((*o++ = *i++));
#if 0
printf ("original %s sized %s\n", xlfd, xlfd_pixel);
#endif
xlfd = xlfd_pixel;
}
}
ret = XLoadQueryFont (dpy, xlfd);
if (xlfd_pixel)
free (xlfd_pixel);
return ret;
}
|