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
|
/* Copyright (C) 2014-2020 Free Software Foundation, Inc.
This file is part of groff.
groff is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation, either version 2 of the License, or
(at your option) any later version.
groff is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
The GNU General Public License version 2 (GPL2) is available in the
internet at <http://www.gnu.org/licenses/gpl-2.0.txt>. */
/*
* XFontName.c
*
* build/parse X Font name strings
*/
#include <config.h>
#include <X11/Xlib.h>
#include <X11/Xos.h>
#include "XFontName.h"
#include <ctype.h>
static char *
extractStringField (char *name, char *buffer, int size,
unsigned int *attrp, unsigned int bit)
{
char *buf = buffer;
if (!*name)
return 0;
while (*name && *name != '-' && size > 0) {
*buf++ = *name++;
--size;
}
if (size <= 0)
return 0;
*buf = '\0';
if (buffer[0] != '*' || buffer[1] != '\0')
*attrp |= bit;
if (*name == '-')
return name+1;
return name;
}
static char *
extractUnsignedField (char *name, unsigned int *result,
unsigned int *attrp, unsigned int bit)
{
char buf[256];
char *c;
unsigned int i;
name = extractStringField (name, buf, sizeof (buf), attrp, bit);
if (!name)
return 0;
if (!(*attrp & bit))
return name;
i = 0;
for (c = buf; *c; c++) {
if (!isdigit (*c))
return 0;
i = i * 10 + (*c - '0');
}
*result = i;
return name;
}
Bool
XParseFontName (XFontNameString fontNameString, XFontName *fontName,
unsigned int *fontNameAttributes)
{
char *name = fontNameString;
XFontName temp;
unsigned int attributes = 0;
#define GetString(field,bit)\
if (!(name = extractStringField \
(name, temp.field, sizeof (temp.field),\
&attributes, bit))) \
return False;
#define GetUnsigned(field,bit)\
if (!(name = extractUnsignedField \
(name, &temp.field, \
&attributes, bit))) \
return False;
GetString (Registry, FontNameRegistry)
GetString (Foundry, FontNameFoundry)
GetString (FamilyName, FontNameFamilyName)
GetString (WeightName, FontNameWeightName)
GetString (Slant, FontNameSlant)
GetString (SetwidthName, FontNameSetwidthName)
GetString (AddStyleName, FontNameAddStyleName)
GetUnsigned (PixelSize, FontNamePixelSize)
GetUnsigned (PointSize, FontNamePointSize)
GetUnsigned (ResolutionX, FontNameResolutionX)
GetUnsigned (ResolutionY, FontNameResolutionY)
GetString (Spacing, FontNameSpacing)
GetUnsigned (AverageWidth, FontNameAverageWidth)
GetString (CharSetRegistry, FontNameCharSetRegistry)
if (!*name) {
temp.CharSetEncoding[0] = '\0';
attributes |= FontNameCharSetEncoding;
} else {
GetString (CharSetEncoding, FontNameCharSetEncoding)
}
*fontName = temp;
*fontNameAttributes = attributes;
return True;
}
static char *
utoa (unsigned int u, char *s, int size)
{
char *t;
t = s + size;
*--t = '\0';
do
*--t = (u % 10) + '0';
while (u /= 10);
return t;
}
Bool
XFormatFontName (XFontName *fontName, unsigned int fontNameAttributes,
XFontNameString fontNameString)
{
char tmp[256];
char *name = tmp, *f;
int left = sizeof (tmp) - 1;
char number[32];
#define PutString(field, bit)\
f = (fontNameAttributes & bit) ? \
fontName->field \
: (char *)"*"; \
if ((left -= strlen (f)) < 0) \
return False; \
while (*f) \
if ((*name++ = *f++) == '-') \
return False;
#define PutHyphen()\
if (--left < 0) \
return False; \
*name++ = '-';
#define PutUnsigned(field, bit) \
f = (fontNameAttributes & bit) ? \
utoa (fontName->field, number, sizeof (number)) \
: (char *)"*"; \
if ((left -= strlen (f)) < 0) \
return False; \
while (*f) \
*name++ = *f++;
PutString (Registry, FontNameRegistry)
PutHyphen ();
PutString (Foundry, FontNameFoundry)
PutHyphen ();
PutString (FamilyName, FontNameFamilyName)
PutHyphen ();
PutString (WeightName, FontNameWeightName)
PutHyphen ();
PutString (Slant, FontNameSlant)
PutHyphen ();
PutString (SetwidthName, FontNameSetwidthName)
PutHyphen ();
PutString (AddStyleName, FontNameAddStyleName)
PutHyphen ();
PutUnsigned (PixelSize, FontNamePixelSize)
PutHyphen ();
PutUnsigned (PointSize, FontNamePointSize)
PutHyphen ();
PutUnsigned (ResolutionX, FontNameResolutionX)
PutHyphen ();
PutUnsigned (ResolutionY, FontNameResolutionY)
PutHyphen ();
PutString (Spacing, FontNameSpacing)
PutHyphen ();
PutUnsigned (AverageWidth, FontNameAverageWidth)
PutHyphen ();
PutString (CharSetRegistry, FontNameCharSetRegistry)
PutHyphen ();
PutString (CharSetEncoding, FontNameCharSetEncoding)
*name = '\0';
strcpy (fontNameString, tmp);
return True;
}
Bool
XCompareFontName (XFontName *name1, XFontName *name2,
unsigned int fontNameAttributes)
{
#define CompareString(field,bit) \
if (fontNameAttributes & bit) \
if (strcmp (name1->field, name2->field)) \
return False;
#define CompareUnsigned(field,bit) \
if (fontNameAttributes & bit) \
if (name1->field != name2->field) \
return False;
CompareString (Registry, FontNameRegistry)
CompareString (Foundry, FontNameFoundry)
CompareString (FamilyName, FontNameFamilyName)
CompareString (WeightName, FontNameWeightName)
CompareString (Slant, FontNameSlant)
CompareString (SetwidthName, FontNameSetwidthName)
CompareString (AddStyleName, FontNameAddStyleName)
CompareUnsigned (PixelSize, FontNamePixelSize)
CompareUnsigned (PointSize, FontNamePointSize)
CompareUnsigned (ResolutionX, FontNameResolutionX)
CompareUnsigned (ResolutionY, FontNameResolutionY)
CompareString (Spacing, FontNameSpacing)
CompareUnsigned (AverageWidth, FontNameAverageWidth)
CompareString (CharSetRegistry, FontNameCharSetRegistry)
CompareString (CharSetEncoding, FontNameCharSetEncoding)
return True;
}
Bool
XCopyFontName (XFontName *name1, XFontName *name2,
unsigned int fontNameAttributes)
{
#define CopyString(field,bit) \
if (fontNameAttributes & bit) \
strcpy (name2->field, name1->field);
#define CopyUnsigned(field,bit) \
if (fontNameAttributes & bit) \
name2->field = name1->field;
CopyString (Registry, FontNameRegistry)
CopyString (Foundry, FontNameFoundry)
CopyString (FamilyName, FontNameFamilyName)
CopyString (WeightName, FontNameWeightName)
CopyString (Slant, FontNameSlant)
CopyString (SetwidthName, FontNameSetwidthName)
CopyString (AddStyleName, FontNameAddStyleName)
CopyUnsigned (PixelSize, FontNamePixelSize)
CopyUnsigned (PointSize, FontNamePointSize)
CopyUnsigned (ResolutionX, FontNameResolutionX)
CopyUnsigned (ResolutionY, FontNameResolutionY)
CopyString (Spacing, FontNameSpacing)
CopyUnsigned (AverageWidth, FontNameAverageWidth)
CopyString (CharSetRegistry, FontNameCharSetRegistry)
CopyString (CharSetEncoding, FontNameCharSetEncoding)
return True;
}
|