6 #include <WINGs/WUtil.h>
8 #include "../src/wconfig.h"
10 #define DEFAULT_FONT "sans serif:pixelsize=12"
12 /* X Font Name Suffix field names */
30 static int countChar(char *str
, char c
)
37 for (; *str
!= 0; str
++) {
51 #define XLFD_TOKENS 14
53 static str
*getXLFDTokens(char *xlfd
)
55 static str tokens
[XLFD_TOKENS
];
59 /* XXX: why does this assume there can't ever be XFNextPrefix? */
60 if (!xlfd
|| *xlfd
!= '-' || countChar(xlfd
, '-') != XLFD_TOKENS
)
63 memset(tokens
, 0, sizeof(str
) * XLFD_TOKENS
);
67 for (ptr
= xlfd
, i
= 0; i
< XLFD_TOKENS
&& len
> 0; i
++) {
73 size
= strcspn(ptr
, "-,");
83 static int strToInt(str
* token
)
85 static char buf
[32]; /* enough for an Incredibly Big Number */
87 if (token
->len
== 0 ||
88 token
->str
[0] == '*' ||
89 token
->len
>= sizeof(buf
))
92 memset(buf
, 0, sizeof(buf
));
93 strncpy(buf
, token
->str
, token
->len
);
95 /* the code using this will gracefully handle overflows */
96 return (int)strtol(buf
, NULL
, 10);
99 static char *mapWeightToName(str
* weight
)
101 char *normalNames
[] = { "medium", "normal", "regular" };
105 if (weight
->len
== 0)
108 for (i
= 0; i
< sizeof(normalNames
) / sizeof(char *); i
++) {
109 if (strlen(normalNames
[i
]) == weight
->len
&& strncmp(normalNames
[i
], weight
->str
, weight
->len
)) {
114 snprintf(buf
, sizeof(buf
), ":%.*s", weight
->len
, weight
->str
);
119 static char *mapSlantToName(str
* slant
)
124 switch (slant
->str
[0]) {
135 char *xlfdToFc(char *xlfd
, char *useFamily
, Bool keepXLFD
)
137 str
*tokens
, *family
, *weight
, *slant
;
141 tokens
= getXLFDTokens(xlfd
);
143 return wstrdup(DEFAULT_FONT
);
145 family
= &(tokens
[FAMILY_NAME
]);
146 weight
= &(tokens
[WEIGHT_NAME
]);
147 slant
= &(tokens
[SLANT
]);
148 pixelsize
= strToInt(&tokens
[PIXEL_SIZE
]);
149 size
= strToInt(&tokens
[POINT_SIZE
]);
152 name
= wstrdup(useFamily
);
154 if (family
->len
== 0 || family
->str
[0] == '*')
155 return wstrdup(DEFAULT_FONT
);
157 snprintf(buf
, sizeof(buf
), "%.*s", family
->len
, family
->str
);
161 if (size
> 0 && pixelsize
<= 0) {
162 snprintf(buf
, sizeof(buf
), "-%d", size
/ 10);
163 name
= wstrappend(name
, buf
);
166 name
= wstrappend(name
, mapWeightToName(weight
));
167 name
= wstrappend(name
, mapSlantToName(slant
));
169 if (size
<= 0 && pixelsize
<= 0) {
170 name
= wstrappend(name
, ":pixelsize=12");
171 } else if (pixelsize
> 0) {
172 /* if pixelsize is present size will be ignored so we skip it */
173 snprintf(buf
, sizeof(buf
), ":pixelsize=%d", pixelsize
);
174 name
= wstrappend(name
, buf
);
178 name
= wstrappend(name
, ":xlfd=");
179 name
= wstrappend(name
, xlfd
);
185 /* return converted font (if conversion is needed) else the original font */
186 char *convertFont(char *font
, Bool keepXLFD
)
188 if (font
[0] == '-') {
189 if (MB_CUR_MAX
< 2) {
190 return xlfdToFc(font
, NULL
, keepXLFD
);
192 return xlfdToFc(font
, "sans serif", keepXLFD
);