1 /* convertfonts.c - converts fonts in a style file to fontconfig format
3 * WindowMaker window manager
5 * Copyright (c) 2004 Dan Pascu
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
24 #define PROG_VERSION "convertfonts (Window Maker) 1.0"
30 #include <WINGs/WUtil.h>
32 #include "../src/wconfig.h"
34 #define DEFAULT_FONT "sans-serif:pixelsize=12"
36 char *FontOptions
[] = {
54 countChar(char *str
, char c
)
61 for (; *str
!=0; str
++) {
76 #define XLFD_TOKENS 14
80 getXLFDTokens(char *xlfd
)
82 static str tokens
[XLFD_TOKENS
];
86 if (!xlfd
|| *xlfd
!='-' || countChar(xlfd
, '-')<XLFD_TOKENS
)
89 memset(tokens
, 0, sizeof(str
)*XLFD_TOKENS
);
93 for (ptr
=xlfd
, i
=0; i
<XLFD_TOKENS
&& len
>0; i
++) {
99 size
= strcspn(ptr
, "-");
101 tokens
[i
].len
= size
;
115 if (token
->len
==0 || token
->str
[0]=='*') {
118 for (res
=0, pos
=0; pos
<token
->len
; pos
++) {
119 c
= token
->str
[pos
] - '0';
130 mapSlantToName(str
*slant
)
132 if (slant
->len
==0 || slant
->str
[0]=='*')
135 switch(slant
->str
[0]) {
148 xlfdToFc(char *xlfd
, char *useFamily
, Bool keepXLFD
)
150 str
*tokens
, *family
, *weight
, *slant
;
151 char *name
, buf
[512];
154 tokens
= getXLFDTokens(xlfd
);
156 return wstrdup(DEFAULT_FONT
);
158 family
= &(tokens
[1]);
159 weight
= &(tokens
[2]);
160 slant
= &(tokens
[3]);
163 name
= wstrdup(useFamily
);
165 if (family
->len
==0 || family
->str
[0]=='*')
166 return wstrdup(DEFAULT_FONT
);
168 sprintf(buf
, "%.*s", family
->len
, family
->str
);
172 pixelsize
= strToInt(&tokens
[6]);
173 size
= strToInt(&tokens
[7]);
175 if (size
<=0 && pixelsize
<=0) {
176 name
= wstrappend(name
, ":pixelsize=12");
177 } else if (pixelsize
>0) {
178 /* if pixelsize is present size will be ignored so we skip it */
179 sprintf(buf
, ":pixelsize=%d", pixelsize
);
180 name
= wstrappend(name
, buf
);
182 sprintf(buf
, "-%d", size
/10);
183 name
= wstrappend(name
, buf
);
186 if (weight
->len
>0 && weight
->str
[0]!='*') {
187 sprintf(buf
, ":weight=%.*s", weight
->len
, weight
->str
);
188 name
= wstrappend(name
, buf
);
191 if (slant
->len
>0 && slant
->str
[0]!='*') {
192 sprintf(buf
, ":slant=%s", mapSlantToName(slant
));
193 name
= wstrappend(name
, buf
);
197 name
= wstrappend(name
, ":xlfd=");
198 name
= wstrappend(name
, xlfd
);
205 /* return converted font (if conversion is needed) else the original font */
207 convertFont(char *font
, Bool keepXLFD
)
210 if (!strchr(font
, ',')) {
211 return xlfdToFc(font
, NULL
, keepXLFD
);
213 return xlfdToFc(font
, "sans-serif", keepXLFD
);
224 printf("\nUsage: %s <style_file>\n\n", ProgName
);
225 puts("Converts fonts in a style file into fontconfig format");
227 puts(" --help display this help and exit");
228 puts(" --version output version information and exit");
229 puts(" --keep-xlfd preserve the original xlfd by appending a ':xlfd=<xlfd>' hint");
230 puts(" to the font name. This property is not used by the fontconfig");
231 puts(" matching engine to find the font, but it is useful as a hint");
232 puts(" about what the original font was to allow hand tuning the");
233 puts(" result or restoring the xlfd. The default is to not add it");
234 puts(" as it results in long, unreadable and confusing names.");
235 puts(" --sets-too try to approximate fontsets by using their first complete xlfd.");
236 puts(" This only works for singlebyte languages. The default is to");
237 puts(" replace the fontset with the default: 'sans-serif:pixelsize=12'");
238 puts(" which should display properly for any language.");
242 // replace --sets-too with something better
244 main(int argc
, char **argv
)
246 WMPropList
*style
, *key
, *val
;
247 char *file
= NULL
, *oldfont
, *newfont
;
249 Bool keepXLFD
= False
;
259 for (i
=1; i
< argc
; i
++) {
260 if (strcmp("--version", argv
[i
])==0) {
263 } else if (strcmp("--help", argv
[i
])==0) {
266 } else if (strcmp("--keep-xlfd", argv
[i
])==0) {
268 } else if (argv
[i
][0]=='-') {
269 printf("%s: invalid argument '%s'\n", ProgName
, argv
[i
]);
270 printf("Try '%s --help' for more information\n", ProgName
);
277 WMPLSetCaseSensitive(False
);
279 if (stat(file
, &statbuf
) < 0) {
284 style
= WMReadPropListFromFile(file
);
287 printf("%s: could not load style file.\n", ProgName
);
291 if (!WMIsPLDictionary(style
)) {
292 printf("%s: '%s' is not a well formatted style file\n", ProgName
, file
);
296 for (i
=0; FontOptions
[i
]!=NULL
; i
++) {
297 key
= WMCreatePLString(FontOptions
[i
]);
298 val
= WMGetFromPLDictionary(style
, key
);
300 oldfont
= WMGetFromPLString(val
);
301 newfont
= convertFont(oldfont
, keepXLFD
);
302 if (oldfont
!= newfont
) {
303 val
= WMCreatePLString(newfont
);
304 WMPutInPLDictionary(style
, key
, val
);
305 WMReleasePropList(val
);
309 WMReleasePropList(key
);
312 WMWritePropListToFile(style
, file
, True
);