5 #include <WINGs/WUtil.h>
7 #include "../src/wconfig.h"
9 #define DEFAULT_FONT "sans serif:pixelsize=12"
12 countChar(char *str
, char c
)
19 for (; *str
!=0; str
++) {
34 #define XLFD_TOKENS 14
38 getXLFDTokens(char *xlfd
)
40 static str tokens
[XLFD_TOKENS
];
44 if (!xlfd
|| *xlfd
!='-' || countChar(xlfd
, '-')<XLFD_TOKENS
)
47 memset(tokens
, 0, sizeof(str
)*XLFD_TOKENS
);
51 for (ptr
=xlfd
, i
=0; i
<XLFD_TOKENS
&& len
>0; i
++) {
57 size
= strcspn(ptr
, "-,");
73 if (token
->len
==0 || token
->str
[0]=='*') {
76 for (res
=0, pos
=0; pos
<token
->len
; pos
++) {
77 c
= token
->str
[pos
] - '0';
88 mapWeightToName(str
*weight
)
90 char *normalNames
[] = {"medium", "normal", "regular"};
97 for (i
=0; i
<sizeof(normalNames
)/sizeof(char*); i
++) {
98 if (strlen(normalNames
[i
])==weight
->len
&&
99 strncmp(normalNames
[i
], weight
->str
, weight
->len
))
105 snprintf(buf
, sizeof(buf
), ":%.*s", weight
->len
, weight
->str
);
112 mapSlantToName(str
*slant
)
117 switch(slant
->str
[0]) {
130 xlfdToFc(char *xlfd
, char *useFamily
, Bool keepXLFD
)
132 str
*tokens
, *family
, *weight
, *slant
;
133 char *name
, buf
[64], *slt
;
136 tokens
= getXLFDTokens(xlfd
);
138 return wstrdup(DEFAULT_FONT
);
140 family
= &(tokens
[1]);
141 weight
= &(tokens
[2]);
142 slant
= &(tokens
[3]);
143 pixelsize
= strToInt(&tokens
[6]);
144 size
= strToInt(&tokens
[7]);
147 name
= wstrdup(useFamily
);
149 if (family
->len
==0 || family
->str
[0]=='*')
150 return wstrdup(DEFAULT_FONT
);
152 snprintf(buf
, sizeof(buf
), "%.*s", family
->len
, family
->str
);
156 if (size
>0 && pixelsize
<=0) {
157 snprintf(buf
, sizeof(buf
), "-%d", size
/10);
158 name
= wstrappend(name
, buf
);
161 name
= wstrappend(name
, mapWeightToName(weight
));
162 name
= wstrappend(name
, mapSlantToName(slant
));
164 if (size
<=0 && pixelsize
<=0) {
165 name
= wstrappend(name
, ":pixelsize=12");
166 } else if (pixelsize
>0) {
167 /* if pixelsize is present size will be ignored so we skip it */
168 snprintf(buf
, sizeof(buf
), ":pixelsize=%d", pixelsize
);
169 name
= wstrappend(name
, buf
);
173 name
= wstrappend(name
, ":xlfd=");
174 name
= wstrappend(name
, xlfd
);
181 /* return converted font (if conversion is needed) else the original font */
183 convertFont(char *font
, Bool keepXLFD
)
186 if (MB_CUR_MAX
< 2) {
187 return xlfdToFc(font
, NULL
, keepXLFD
);
189 return xlfdToFc(font
, "sans serif", keepXLFD
);