WINGs: Dead code removal
[wmaker-crm.git] / util / convertfonts.c
blob2a023c6af9e3169d215f26e618dfb3578f2edf13
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,
20 * USA.
23 #ifdef __GLIBC__
24 #define _GNU_SOURCE /* getopt_long */
25 #endif
27 #include <sys/stat.h>
29 #include <getopt.h>
30 #include <locale.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #include <WINGs/WUtil.h>
37 #include "../src/wconfig.h"
39 char *FontOptions[] = {
40 "IconTitleFont",
41 "ClipTitleFont",
42 "DisplayFont",
43 "LargeDisplayFont",
44 "MenuTextFont",
45 "MenuTitleFont",
46 "WindowTitleFont",
47 "SystemFont",
48 "BoldSystemFont",
49 NULL
52 extern char *__progname;
54 extern char *convertFont(char *font, Bool keepXLFD);
56 void print_help(int print_usage, int exitval)
58 printf("Usage: %s [-h] [-v] [--keep-xlfd] <style_file>\n", __progname);
59 if (print_usage) {
60 puts("Converts fonts in a style file into fontconfig format");
61 puts("");
62 puts(" -h, --help display this help and exit");
63 puts(" -v, --version output version information and exit");
64 puts(" --keep-xlfd preserve the original xlfd by appending a ':xlfd=<xlfd>' hint");
65 puts(" to the font name. This property is not used by the fontconfig");
66 puts(" matching engine to find the font, but it is useful as a hint");
67 puts(" about what the original font was to allow hand tuning the");
68 puts(" result or restoring the xlfd. The default is to not add it");
69 puts(" as it results in long, unreadable and confusing names.");
71 exit(exitval);
74 int main(int argc, char **argv)
76 WMPropList *style, *key, *val;
77 char *file = NULL, *oldfont, *newfont;
78 struct stat st;
79 Bool keepXLFD = False;
80 int i, ch;
82 struct option longopts[] = {
83 { "version", no_argument, NULL, 'v' },
84 { "help", no_argument, NULL, 'h' },
85 { "keep-xlfd", no_argument, &keepXLFD, True },
86 { NULL, 0, NULL, 0 }
89 while ((ch = getopt_long(argc, argv, "hv", longopts, NULL)) != -1)
90 switch(ch) {
91 case 'v':
92 printf("%s (Window Maker %s)\n", __progname, VERSION);
93 return 0;
94 /* NOTREACHED */
95 case 'h':
96 print_help(1, 0);
97 /* NOTREACHED */
98 case 0:
99 break;
100 default:
101 print_help(0, 1);
102 /* NOTREACHED */
105 argc -= optind;
106 argv += optind;
108 if (argc != 1)
109 print_help(0, 1);
111 file = argv[0];
113 if (stat(file, &st) != 0) {
114 perror(file);
115 return 1;
118 if (!S_ISREG(st.st_mode)) { /* maybe symlink too? */
119 fprintf(stderr, "%s: `%s' is not a regular file\n", __progname, file);
120 return 1;
123 /* we need this in order for MB_CUR_MAX to work */
124 /* this contradicts big time with getstyle */
125 setlocale(LC_ALL, "");
127 WMPLSetCaseSensitive(False);
129 style = WMReadPropListFromFile(file);
130 if (!style) {
131 perror(file);
132 printf("%s: could not load style file\n", __progname);
133 return 1;
136 if (!WMIsPLDictionary(style)) {
137 printf("%s: '%s' is not a well formatted style file\n", __progname, file);
138 return 1;
141 for (i = 0; FontOptions[i] != NULL; i++) {
142 key = WMCreatePLString(FontOptions[i]);
143 val = WMGetFromPLDictionary(style, key);
144 if (val) {
145 oldfont = WMGetFromPLString(val);
146 newfont = convertFont(oldfont, keepXLFD);
147 if (oldfont != newfont) {
148 val = WMCreatePLString(newfont);
149 WMPutInPLDictionary(style, key, val);
150 WMReleasePropList(val);
151 wfree(newfont);
154 WMReleasePropList(key);
157 WMWritePropListToFile(style, file);
159 return 0;