wmaker: Moved function prototype to the appropriate header
[wmaker-crm.git] / util / convertfonts.c
blob4a22fcccc792ae31e7411760c03049d47b49fe6b
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 #include "common.h"
41 char *FontOptions[] = {
42 "IconTitleFont",
43 "ClipTitleFont",
44 "DisplayFont",
45 "LargeDisplayFont",
46 "MenuTextFont",
47 "MenuTitleFont",
48 "WindowTitleFont",
49 "SystemFont",
50 "BoldSystemFont",
51 NULL
54 extern char *__progname;
57 static void print_help(int print_usage, int exitval)
59 printf("Usage: %s [-h] [-v] [--keep-xlfd] <style_file>\n", __progname);
60 if (print_usage) {
61 puts("Converts fonts in a style file into fontconfig format");
62 puts("");
63 puts(" -h, --help display this help and exit");
64 puts(" -v, --version output version information and exit");
65 puts(" --keep-xlfd preserve the original xlfd by appending a ':xlfd=<xlfd>' hint");
66 puts(" to the font name. This property is not used by the fontconfig");
67 puts(" matching engine to find the font, but it is useful as a hint");
68 puts(" about what the original font was to allow hand tuning the");
69 puts(" result or restoring the xlfd. The default is to not add it");
70 puts(" as it results in long, unreadable and confusing names.");
72 exit(exitval);
75 int main(int argc, char **argv)
77 WMPropList *style, *key, *val;
78 char *file = NULL, *oldfont, *newfont;
79 struct stat st;
80 Bool keepXLFD = False;
81 int i, ch;
83 struct option longopts[] = {
84 { "version", no_argument, NULL, 'v' },
85 { "help", no_argument, NULL, 'h' },
86 { "keep-xlfd", no_argument, &keepXLFD, True },
87 { NULL, 0, NULL, 0 }
90 while ((ch = getopt_long(argc, argv, "hv", longopts, NULL)) != -1)
91 switch(ch) {
92 case 'v':
93 printf("%s (Window Maker %s)\n", __progname, VERSION);
94 return 0;
95 /* NOTREACHED */
96 case 'h':
97 print_help(1, 0);
98 /* NOTREACHED */
99 case 0:
100 break;
101 default:
102 print_help(0, 1);
103 /* NOTREACHED */
106 argc -= optind;
107 argv += optind;
109 if (argc != 1)
110 print_help(0, 1);
112 file = argv[0];
114 if (stat(file, &st) != 0) {
115 perror(file);
116 return 1;
119 if (!S_ISREG(st.st_mode)) { /* maybe symlink too? */
120 fprintf(stderr, "%s: `%s' is not a regular file\n", __progname, file);
121 return 1;
124 /* we need this in order for MB_CUR_MAX to work */
125 /* this contradicts big time with getstyle */
126 setlocale(LC_ALL, "");
128 WMPLSetCaseSensitive(False);
130 style = WMReadPropListFromFile(file);
131 if (!style) {
132 perror(file);
133 printf("%s: could not load style file\n", __progname);
134 return 1;
137 if (!WMIsPLDictionary(style)) {
138 printf("%s: '%s' is not a well formatted style file\n", __progname, file);
139 return 1;
142 for (i = 0; FontOptions[i] != NULL; i++) {
143 key = WMCreatePLString(FontOptions[i]);
144 val = WMGetFromPLDictionary(style, key);
145 if (val) {
146 oldfont = WMGetFromPLString(val);
147 newfont = convertFont(oldfont, keepXLFD);
148 if (oldfont != newfont) {
149 val = WMCreatePLString(newfont);
150 WMPutInPLDictionary(style, key, val);
151 WMReleasePropList(val);
152 wfree(newfont);
155 WMReleasePropList(key);
158 WMWritePropListToFile(style, file);
160 return 0;