6 #include <WINGs/WUtil.h>
8 #include "../src/wconfig.h"
13 #define DEFAULT_FONT "sans serif:pixelsize=12"
15 /* X Font Name Suffix field names */
33 static int countChar(const char *str
, char c
)
40 for (; *str
!= 0; str
++) {
54 #define XLFD_TOKENS 14
56 static str
*getXLFDTokens(const char *xlfd
)
58 static str tokens
[XLFD_TOKENS
];
62 /* XXX: why does this assume there can't ever be XFNextPrefix? */
63 if (!xlfd
|| *xlfd
!= '-' || countChar(xlfd
, '-') != XLFD_TOKENS
)
66 memset(tokens
, 0, sizeof(str
) * XLFD_TOKENS
);
70 for (ptr
= xlfd
, i
= 0; i
< XLFD_TOKENS
&& len
> 0; i
++) {
76 size
= strcspn(ptr
, "-,");
86 static int strToInt(str
* token
)
88 static char buf
[32]; /* enough for an Incredibly Big Number */
90 if (token
->len
== 0 ||
91 token
->str
[0] == '*' ||
92 token
->len
>= sizeof(buf
))
95 memset(buf
, 0, sizeof(buf
));
96 strncpy(buf
, token
->str
, token
->len
);
98 /* the code using this will gracefully handle overflows */
99 return (int)strtol(buf
, NULL
, 10);
102 static char *mapWeightToName(str
* weight
)
104 static const char *normalNames
[] = { "medium", "normal", "regular" };
108 if (weight
->len
== 0)
111 for (i
= 0; i
< wlengthof(normalNames
); i
++) {
112 if (strlen(normalNames
[i
]) == weight
->len
&& strncmp(normalNames
[i
], weight
->str
, weight
->len
)) {
117 snprintf(buf
, sizeof(buf
), ":%.*s", weight
->len
, weight
->str
);
122 static char *mapSlantToName(str
* slant
)
127 switch (slant
->str
[0]) {
138 static char *xlfdToFc(const char *xlfd
, const char *useFamily
, Bool keepXLFD
)
140 str
*tokens
, *family
, *weight
, *slant
;
144 tokens
= getXLFDTokens(xlfd
);
146 return wstrdup(DEFAULT_FONT
);
148 family
= &(tokens
[FAMILY_NAME
]);
149 weight
= &(tokens
[WEIGHT_NAME
]);
150 slant
= &(tokens
[SLANT
]);
151 pixelsize
= strToInt(&tokens
[PIXEL_SIZE
]);
152 size
= strToInt(&tokens
[POINT_SIZE
]);
155 name
= wstrdup(useFamily
);
157 if (family
->len
== 0 || family
->str
[0] == '*')
158 return wstrdup(DEFAULT_FONT
);
160 snprintf(buf
, sizeof(buf
), "%.*s", family
->len
, family
->str
);
164 if (size
> 0 && pixelsize
<= 0) {
165 snprintf(buf
, sizeof(buf
), "-%d", size
/ 10);
166 name
= wstrappend(name
, buf
);
169 name
= wstrappend(name
, mapWeightToName(weight
));
170 name
= wstrappend(name
, mapSlantToName(slant
));
172 if (size
<= 0 && pixelsize
<= 0) {
173 name
= wstrappend(name
, ":pixelsize=12");
174 } else if (pixelsize
> 0) {
175 /* if pixelsize is present size will be ignored so we skip it */
176 snprintf(buf
, sizeof(buf
), ":pixelsize=%d", pixelsize
);
177 name
= wstrappend(name
, buf
);
181 name
= wstrappend(name
, ":xlfd=");
182 name
= wstrappend(name
, xlfd
);
188 /* return converted font (if conversion is needed) else the original font */
189 char *convertFont(char *font
, Bool keepXLFD
)
191 if (font
[0] == '-') {
192 if (MB_CUR_MAX
< 2) {
193 return xlfdToFc(font
, NULL
, keepXLFD
);
195 return xlfdToFc(font
, "sans serif", keepXLFD
);