Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / groff / src / libs / libxutil / XFontName.c
blob9ba7b8d1686f9ac7e3b9f6566337a6e1ba2f3f5e
1 /* $NetBSD$ */
3 /*
4 * XFontName.c
6 * build/parse X Font name strings
7 */
9 #include <X11/Xlib.h>
10 #include <X11/Xos.h>
11 #include "XFontName.h"
12 #include <ctype.h>
14 static char *
15 extractStringField (char *name, char *buffer, int size,
16 unsigned int *attrp, unsigned int bit)
18 char *buf = buffer;
20 if (!*name)
21 return 0;
22 while (*name && *name != '-' && size > 0) {
23 *buf++ = *name++;
24 --size;
26 if (size <= 0)
27 return 0;
28 *buf = '\0';
29 if (buffer[0] != '*' || buffer[1] != '\0')
30 *attrp |= bit;
31 if (*name == '-')
32 return name+1;
33 return name;
36 static char *
37 extractUnsignedField (char *name, unsigned int *result,
38 unsigned int *attrp, unsigned int bit)
40 char buf[256];
41 char *c;
42 unsigned int i;
44 name = extractStringField (name, buf, sizeof (buf), attrp, bit);
45 if (!name)
46 return 0;
47 if (!(*attrp & bit))
48 return name;
49 i = 0;
50 for (c = buf; *c; c++) {
51 if (!isdigit (*c))
52 return 0;
53 i = i * 10 + (*c - '0');
55 *result = i;
56 return name;
59 Bool
60 XParseFontName (XFontNameString fontNameString, XFontName *fontName,
61 unsigned int *fontNameAttributes)
63 char *name = fontNameString;
64 XFontName temp;
65 unsigned int attributes = 0;
67 #define GetString(field,bit)\
68 if (!(name = extractStringField \
69 (name, temp.field, sizeof (temp.field),\
70 &attributes, bit))) \
71 return False;
73 #define GetUnsigned(field,bit)\
74 if (!(name = extractUnsignedField \
75 (name, &temp.field, \
76 &attributes, bit))) \
77 return False;
79 GetString (Registry, FontNameRegistry)
80 GetString (Foundry, FontNameFoundry)
81 GetString (FamilyName, FontNameFamilyName)
82 GetString (WeightName, FontNameWeightName)
83 GetString (Slant, FontNameSlant)
84 GetString (SetwidthName, FontNameSetwidthName)
85 GetString (AddStyleName, FontNameAddStyleName)
86 GetUnsigned (PixelSize, FontNamePixelSize)
87 GetUnsigned (PointSize, FontNamePointSize)
88 GetUnsigned (ResolutionX, FontNameResolutionX)
89 GetUnsigned (ResolutionY, FontNameResolutionY)
90 GetString (Spacing, FontNameSpacing)
91 GetUnsigned (AverageWidth, FontNameAverageWidth)
92 GetString (CharSetRegistry, FontNameCharSetRegistry)
93 if (!*name) {
94 temp.CharSetEncoding[0] = '\0';
95 attributes |= FontNameCharSetEncoding;
96 } else {
97 GetString (CharSetEncoding, FontNameCharSetEncoding)
99 *fontName = temp;
100 *fontNameAttributes = attributes;
101 return True;
104 static char *
105 utoa (unsigned int u, char *s, int size)
107 char *t;
109 t = s + size;
110 *--t = '\0';
112 *--t = (u % 10) + '0';
113 while (u /= 10);
114 return t;
117 Bool
118 XFormatFontName (XFontName *fontName, unsigned int fontNameAttributes,
119 XFontNameString fontNameString)
121 char tmp[256];
122 char *name = tmp, *f;
123 int left = sizeof (tmp) - 1;
124 char number[32];
126 #define PutString(field, bit)\
127 f = (fontNameAttributes & bit) ? \
128 fontName->field \
129 : (char *)"*"; \
130 if ((left -= strlen (f)) < 0) \
131 return False; \
132 while (*f) \
133 if ((*name++ = *f++) == '-') \
134 return False;
135 #define PutHyphen()\
136 if (--left < 0) \
137 return False; \
138 *name++ = '-';
140 #define PutUnsigned(field, bit) \
141 f = (fontNameAttributes & bit) ? \
142 utoa (fontName->field, number, sizeof (number)) \
143 : (char *)"*"; \
144 if ((left -= strlen (f)) < 0) \
145 return False; \
146 while (*f) \
147 *name++ = *f++;
149 PutString (Registry, FontNameRegistry)
150 PutHyphen ();
151 PutString (Foundry, FontNameFoundry)
152 PutHyphen ();
153 PutString (FamilyName, FontNameFamilyName)
154 PutHyphen ();
155 PutString (WeightName, FontNameWeightName)
156 PutHyphen ();
157 PutString (Slant, FontNameSlant)
158 PutHyphen ();
159 PutString (SetwidthName, FontNameSetwidthName)
160 PutHyphen ();
161 PutString (AddStyleName, FontNameAddStyleName)
162 PutHyphen ();
163 PutUnsigned (PixelSize, FontNamePixelSize)
164 PutHyphen ();
165 PutUnsigned (PointSize, FontNamePointSize)
166 PutHyphen ();
167 PutUnsigned (ResolutionX, FontNameResolutionX)
168 PutHyphen ();
169 PutUnsigned (ResolutionY, FontNameResolutionY)
170 PutHyphen ();
171 PutString (Spacing, FontNameSpacing)
172 PutHyphen ();
173 PutUnsigned (AverageWidth, FontNameAverageWidth)
174 PutHyphen ();
175 PutString (CharSetRegistry, FontNameCharSetRegistry)
176 PutHyphen ();
177 PutString (CharSetEncoding, FontNameCharSetEncoding)
178 *name = '\0';
179 strcpy (fontNameString, tmp);
180 return True;
183 Bool
184 XCompareFontName (XFontName *name1, XFontName *name2,
185 unsigned int fontNameAttributes)
187 #define CompareString(field,bit) \
188 if (fontNameAttributes & bit) \
189 if (strcmp (name1->field, name2->field)) \
190 return False;
192 #define CompareUnsigned(field,bit) \
193 if (fontNameAttributes & bit) \
194 if (name1->field != name2->field) \
195 return False;
197 CompareString (Registry, FontNameRegistry)
198 CompareString (Foundry, FontNameFoundry)
199 CompareString (FamilyName, FontNameFamilyName)
200 CompareString (WeightName, FontNameWeightName)
201 CompareString (Slant, FontNameSlant)
202 CompareString (SetwidthName, FontNameSetwidthName)
203 CompareString (AddStyleName, FontNameAddStyleName)
204 CompareUnsigned (PixelSize, FontNamePixelSize)
205 CompareUnsigned (PointSize, FontNamePointSize)
206 CompareUnsigned (ResolutionX, FontNameResolutionX)
207 CompareUnsigned (ResolutionY, FontNameResolutionY)
208 CompareString (Spacing, FontNameSpacing)
209 CompareUnsigned (AverageWidth, FontNameAverageWidth)
210 CompareString (CharSetRegistry, FontNameCharSetRegistry)
211 CompareString (CharSetEncoding, FontNameCharSetEncoding)
212 return True;
215 Bool
216 XCopyFontName (XFontName *name1, XFontName *name2,
217 unsigned int fontNameAttributes)
219 #define CopyString(field,bit) \
220 if (fontNameAttributes & bit) \
221 strcpy (name2->field, name1->field);
223 #define CopyUnsigned(field,bit) \
224 if (fontNameAttributes & bit) \
225 name2->field = name1->field;
227 CopyString (Registry, FontNameRegistry)
228 CopyString (Foundry, FontNameFoundry)
229 CopyString (FamilyName, FontNameFamilyName)
230 CopyString (WeightName, FontNameWeightName)
231 CopyString (Slant, FontNameSlant)
232 CopyString (SetwidthName, FontNameSetwidthName)
233 CopyString (AddStyleName, FontNameAddStyleName)
234 CopyUnsigned (PixelSize, FontNamePixelSize)
235 CopyUnsigned (PointSize, FontNamePointSize)
236 CopyUnsigned (ResolutionX, FontNameResolutionX)
237 CopyUnsigned (ResolutionY, FontNameResolutionY)
238 CopyString (Spacing, FontNameSpacing)
239 CopyUnsigned (AverageWidth, FontNameAverageWidth)
240 CopyString (CharSetRegistry, FontNameCharSetRegistry)
241 CopyString (CharSetEncoding, FontNameCharSetEncoding)
242 return True;