window.c clean code 1
[wmaker-crm.git] / util / convertfonts.c
blobc24a812f6796957499783e261c0f8593aa05c10d
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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #ifdef __GLIBC__
23 #define _GNU_SOURCE /* getopt_long */
24 #endif
26 #include <sys/stat.h>
28 #include <getopt.h>
29 #include <locale.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include <WINGs/WUtil.h>
36 #include "../src/wconfig.h"
38 char *FontOptions[] = {
39 "IconTitleFont",
40 "ClipTitleFont",
41 "DisplayFont",
42 "LargeDisplayFont",
43 "MenuTextFont",
44 "MenuTitleFont",
45 "WindowTitleFont",
46 "SystemFont",
47 "BoldSystemFont",
48 NULL
51 extern char *__progname;
53 extern char *convertFont(char *font, Bool keepXLFD);
55 void print_help(int print_usage, int exitval)
57 printf("Usage: %s [-h] [-v] [--keep-xlfd] <style_file>\n", __progname);
58 if (print_usage) {
59 puts("Converts fonts in a style file into fontconfig format");
60 puts("");
61 puts(" -h, --help display this help and exit");
62 puts(" -v, --version output version information and exit");
63 puts(" --keep-xlfd preserve the original xlfd by appending a ':xlfd=<xlfd>' hint");
64 puts(" to the font name. This property is not used by the fontconfig");
65 puts(" matching engine to find the font, but it is useful as a hint");
66 puts(" about what the original font was to allow hand tuning the");
67 puts(" result or restoring the xlfd. The default is to not add it");
68 puts(" as it results in long, unreadable and confusing names.");
70 exit(exitval);
73 int main(int argc, char **argv)
75 WMPropList *style, *key, *val;
76 char *file = NULL, *oldfont, *newfont;
77 struct stat st;
78 Bool keepXLFD = False;
79 int i, ch;
81 struct option longopts[] = {
82 { "version", no_argument, NULL, 'v' },
83 { "help", no_argument, NULL, 'h' },
84 { "keep-xlfd", no_argument, &keepXLFD, True },
85 { NULL, 0, NULL, 0 }
88 while ((ch = getopt_long(argc, argv, "hv", longopts, NULL)) != -1)
89 switch(ch) {
90 case 'v':
91 printf("%s (Window Maker %s)\n", __progname, VERSION);
92 return 0;
93 /* NOTREACHED */
94 case 'h':
95 print_help(1, 0);
96 /* NOTREACHED */
97 case 0:
98 break;
99 default:
100 print_help(0, 1);
101 /* NOTREACHED */
104 argc -= optind;
105 argv += optind;
107 if (argc != 1)
108 print_help(0, 1);
110 file = argv[0];
112 if (stat(file, &st) != 0) {
113 perror(file);
114 return 1;
117 if (!S_ISREG(st.st_mode)) { /* maybe symlink too? */
118 fprintf(stderr, "%s: `%s' is not a regular file\n", __progname, file);
119 return 1;
122 /* we need this in order for MB_CUR_MAX to work */
123 /* this contradicts big time with getstyle */
124 setlocale(LC_ALL, "");
126 WMPLSetCaseSensitive(False);
128 style = WMReadPropListFromFile(file);
129 if (!style) {
130 perror(file);
131 printf("%s: could not load style file\n", __progname);
132 return 1;
135 if (!WMIsPLDictionary(style)) {
136 printf("%s: '%s' is not a well formatted style file\n", __progname, file);
137 return 1;
140 for (i = 0; FontOptions[i] != NULL; i++) {
141 key = WMCreatePLString(FontOptions[i]);
142 val = WMGetFromPLDictionary(style, key);
143 if (val) {
144 oldfont = WMGetFromPLString(val);
145 newfont = convertFont(oldfont, keepXLFD);
146 if (oldfont != newfont) {
147 val = WMCreatePLString(newfont);
148 WMPutInPLDictionary(style, key, val);
149 WMReleasePropList(val);
150 wfree(newfont);
153 WMReleasePropList(key);
156 WMWritePropListToFile(style, file);
158 return 0;