5 #include <WINGs/WUtil.h>
7 #include "../src/wconfig.h"
9 #define DEFAULT_FONT "sans serif:pixelsize=12"
11 static int countChar(char *str, char c)
18 for (; *str != 0; str++) {
32 #define XLFD_TOKENS 14
34 static str *getXLFDTokens(char *xlfd)
36 static str tokens[XLFD_TOKENS];
40 if (!xlfd || *xlfd != '-' || countChar(xlfd, '-') < XLFD_TOKENS)
43 memset(tokens, 0, sizeof(str) * XLFD_TOKENS);
47 for (ptr = xlfd, i = 0; i < XLFD_TOKENS && len > 0; i++) {
53 size = strcspn(ptr, "-,");
63 static int strToInt(str * token)
67 if (token->len == 0 || token->str[0] == '*') {
70 for (res = 0, pos = 0; pos < token->len; pos++) {
71 c = token->str[pos] - '0';
80 static char *mapWeightToName(str * weight)
82 char *normalNames[] = { "medium", "normal", "regular" };
89 for (i = 0; i < sizeof(normalNames) / sizeof(char *); i++) {
90 if (strlen(normalNames[i]) == weight->len && strncmp(normalNames[i], weight->str, weight->len)) {
95 snprintf(buf, sizeof(buf), ":%.*s", weight->len, weight->str);
100 static char *mapSlantToName(str * slant)
105 switch (slant->str[0]) {
116 char *xlfdToFc(char *xlfd, char *useFamily, Bool keepXLFD)
118 str *tokens, *family, *weight, *slant;
122 tokens = getXLFDTokens(xlfd);
124 return wstrdup(DEFAULT_FONT);
126 family = &(tokens[1]);
127 weight = &(tokens[2]);
128 slant = &(tokens[3]);
129 pixelsize = strToInt(&tokens[6]);
130 size = strToInt(&tokens[7]);
133 name = wstrdup(useFamily);
135 if (family->len == 0 || family->str[0] == '*')
136 return wstrdup(DEFAULT_FONT);
138 snprintf(buf, sizeof(buf), "%.*s", family->len, family->str);
142 if (size > 0 && pixelsize <= 0) {
143 snprintf(buf, sizeof(buf), "-%d", size / 10);
144 name = wstrappend(name, buf);
147 name = wstrappend(name, mapWeightToName(weight));
148 name = wstrappend(name, mapSlantToName(slant));
150 if (size <= 0 && pixelsize <= 0) {
151 name = wstrappend(name, ":pixelsize=12");
152 } else if (pixelsize > 0) {
153 /* if pixelsize is present size will be ignored so we skip it */
154 snprintf(buf, sizeof(buf), ":pixelsize=%d", pixelsize);
155 name = wstrappend(name, buf);
159 name = wstrappend(name, ":xlfd=");
160 name = wstrappend(name, xlfd);
166 /* return converted font (if conversion is needed) else the original font */
167 char *convertFont(char *font, Bool keepXLFD)
169 if (font[0] == '-') {
170 if (MB_CUR_MAX < 2) {
171 return xlfdToFc(font, NULL, keepXLFD);
173 return xlfdToFc(font, "sans serif", keepXLFD);