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