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