XFontName.c revision 8c7c3c7e
1/* 2 * XFontName.c 3 * 4 * build/parse X Font name strings 5 */ 6 7# include <X11/Xlib.h> 8# include <X11/Intrinsic.h> 9# include "XFontName.h" 10#include <ctype.h> 11 12static char * 13extractStringField ( 14 char *name, 15 char *buffer, 16 int size, 17 unsigned int *attrp, 18 unsigned int bit) 19{ 20 char *buf = buffer; 21 22 if (!*name) 23 return NULL; 24 while (*name && *name != '-' && size > 0) { 25 *buf++ = *name++; 26 --size; 27 } 28 if (size <= 0) 29 return NULL; 30 *buf = '\0'; 31 if (buffer[0] != '*' || buffer[1] != '\0') 32 *attrp |= bit; 33 if (*name == '-') 34 return name+1; 35 return name; 36} 37 38static char * 39extractUnsignedField ( 40 char *name, 41 unsigned int *result, 42 unsigned int *attrp, 43 unsigned int bit) 44{ 45 char buf[256]; 46 unsigned int i; 47 48 name = extractStringField (name, buf, sizeof (buf), attrp, bit); 49 if (!name) 50 return NULL; 51 if (!(*attrp & bit)) 52 return name; 53 i = 0; 54 for (char *c = buf; *c; c++) { 55 if (!isdigit (*c)) 56 return NULL; 57 i = i * 10 + (*c - '0'); 58 } 59 *result = i; 60 return name; 61} 62 63Bool 64XParseFontName (XFontNameString fontNameString, XFontName *fontName, 65 unsigned int *fontNameAttributes) 66{ 67 char *name = fontNameString; 68 XFontName temp; 69 unsigned int attributes = 0; 70 71#define GetString(field,bit)\ 72 if (!(name = extractStringField \ 73 (name, temp.field, sizeof (temp.field),\ 74 &attributes, bit))) \ 75 return False; 76 77#define GetUnsigned(field,bit)\ 78 if (!(name = extractUnsignedField \ 79 (name, &temp.field, \ 80 &attributes, bit))) \ 81 return False; 82 83 GetString (Registry, FontNameRegistry) 84 GetString (Foundry, FontNameFoundry) 85 GetString (FamilyName, FontNameFamilyName) 86 GetString (WeightName, FontNameWeightName) 87 GetString (Slant, FontNameSlant) 88 GetString (SetwidthName, FontNameSetwidthName) 89 GetString (AddStyleName, FontNameAddStyleName) 90 GetUnsigned (PixelSize, FontNamePixelSize) 91 GetUnsigned (PointSize, FontNamePointSize) 92 GetUnsigned (ResolutionX, FontNameResolutionX) 93 GetUnsigned (ResolutionY, FontNameResolutionY) 94 GetString (Spacing, FontNameSpacing) 95 GetUnsigned (AverageWidth, FontNameAverageWidth) 96 GetString (CharSetRegistry, FontNameCharSetRegistry) 97 if (!*name) { 98 temp.CharSetEncoding[0] = '\0'; 99 attributes |= FontNameCharSetEncoding; 100 } else { 101 GetString (CharSetEncoding, FontNameCharSetEncoding) 102 } 103 *fontName = temp; 104 *fontNameAttributes = attributes; 105 return True; 106} 107 108static char * 109utoa ( 110 unsigned int u, 111 char *s, 112 int size) 113{ 114 char *t; 115 116 t = s + size; 117 *--t = '\0'; 118 do 119 *--t = (u % 10) + '0'; 120 while (u /= 10); 121 return t; 122} 123 124Bool 125XFormatFontName (XFontName *fontName, unsigned int fontNameAttributes, 126 XFontNameString fontNameString) 127{ 128 XFontNameString tmp; 129 char *name = tmp; 130 const char *f; 131 int left = sizeof (tmp) - 1; 132 char number[32]; 133 134#define PutString(field, bit)\ 135 f = (fontNameAttributes & bit) ? \ 136 fontName->field \ 137 : "*"; \ 138 if ((left -= strlen (f)) < 0) \ 139 return False; \ 140 while (*f) \ 141 if ((*name++ = *f++) == '-') \ 142 return False; 143#define PutHyphen()\ 144 if (--left < 0) \ 145 return False; \ 146 *name++ = '-'; 147 148#define PutUnsigned(field, bit) \ 149 f = (fontNameAttributes & bit) ? \ 150 utoa (fontName->field, number, sizeof (number)) \ 151 : "*"; \ 152 if ((left -= strlen (f)) < 0) \ 153 return False; \ 154 while (*f) \ 155 *name++ = *f++; 156 157 PutString (Registry, FontNameRegistry) 158 PutHyphen (); 159 PutString (Foundry, FontNameFoundry) 160 PutHyphen (); 161 PutString (FamilyName, FontNameFamilyName) 162 PutHyphen (); 163 PutString (WeightName, FontNameWeightName) 164 PutHyphen (); 165 PutString (Slant, FontNameSlant) 166 PutHyphen (); 167 PutString (SetwidthName, FontNameSetwidthName) 168 PutHyphen (); 169 PutString (AddStyleName, FontNameAddStyleName) 170 PutHyphen (); 171 PutUnsigned (PixelSize, FontNamePixelSize) 172 PutHyphen (); 173 PutUnsigned (PointSize, FontNamePointSize) 174 PutHyphen (); 175 PutUnsigned (ResolutionX, FontNameResolutionX) 176 PutHyphen (); 177 PutUnsigned (ResolutionY, FontNameResolutionY) 178 PutHyphen (); 179 PutString (Spacing, FontNameSpacing) 180 PutHyphen (); 181 PutUnsigned (AverageWidth, FontNameAverageWidth) 182 PutHyphen (); 183 PutString (CharSetRegistry, FontNameCharSetRegistry) 184 PutHyphen (); 185 PutString (CharSetEncoding, FontNameCharSetEncoding) 186 *name = '\0'; 187 strcpy (fontNameString, tmp); 188 return True; 189} 190