Change to the linux kernel coding style
[wmaker-crm.git] / util / convertfonts.c
1 /* convertfonts.c - converts fonts in a style file to fontconfig format
2 *
3 * WindowMaker window manager
4 *
5 * Copyright (c) 2004 Dan Pascu
6 *
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.
11 *
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.
16 *
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.
21 */
22
23 #define PROG_VERSION "convertfonts (Window Maker) 1.0"
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <sys/stat.h>
28 #include <string.h>
29 #include <locale.h>
30 #include <WINGs/WUtil.h>
31
32 #include "../src/wconfig.h"
33
34 char *FontOptions[] = {
35 "IconTitleFont",
36 "ClipTitleFont",
37 "DisplayFont",
38 "LargeDisplayFont",
39 "MenuTextFont",
40 "MenuTitleFont",
41 "WindowTitleFont",
42 "SystemFont",
43 "BoldSystemFont",
44 NULL
45 };
46
47 char *ProgName;
48
49 extern char *convertFont(char *font, Bool keepXLFD);
50
51 void print_help()
52 {
53 printf("\nUsage: %s <style_file>\n\n", ProgName);
54 puts("Converts fonts in a style file into fontconfig format");
55 puts("");
56 puts(" --help display this help and exit");
57 puts(" --version output version information and exit");
58 puts(" --keep-xlfd preserve the original xlfd by appending a ':xlfd=<xlfd>' hint");
59 puts(" to the font name. This property is not used by the fontconfig");
60 puts(" matching engine to find the font, but it is useful as a hint");
61 puts(" about what the original font was to allow hand tuning the");
62 puts(" result or restoring the xlfd. The default is to not add it");
63 puts(" as it results in long, unreadable and confusing names.");
64 puts("");
65 }
66
67 int main(int argc, char **argv)
68 {
69 WMPropList *style, *key, *val;
70 char *file = NULL, *oldfont, *newfont;
71 struct stat statbuf;
72 Bool keepXLFD = False;
73 int i;
74
75 ProgName = argv[0];
76
77 if (argc < 2) {
78 print_help();
79 exit(0);
80 }
81
82 for (i = 1; i < argc; i++) {
83 if (strcmp("--version", argv[i]) == 0) {
84 puts(PROG_VERSION);
85 exit(0);
86 } else if (strcmp("--help", argv[i]) == 0) {
87 print_help();
88 exit(0);
89 } else if (strcmp("--keep-xlfd", argv[i]) == 0) {
90 keepXLFD = True;;
91 } else if (argv[i][0] == '-') {
92 printf("%s: invalid argument '%s'\n", ProgName, argv[i]);
93 printf("Try '%s --help' for more information\n", ProgName);
94 exit(1);
95 } else {
96 file = argv[i];
97 }
98 }
99
100 /* we need this in order for MB_CUR_MAX to work */
101 setlocale(LC_ALL, "");
102
103 WMPLSetCaseSensitive(False);
104
105 if (stat(file, &statbuf) < 0) {
106 perror(file);
107 exit(1);
108 }
109
110 style = WMReadPropListFromFile(file);
111 if (!style) {
112 perror(file);
113 printf("%s: could not load style file.\n", ProgName);
114 exit(1);
115 }
116
117 if (!WMIsPLDictionary(style)) {
118 printf("%s: '%s' is not a well formatted style file\n", ProgName, file);
119 exit(1);
120 }
121
122 for (i = 0; FontOptions[i] != NULL; i++) {
123 key = WMCreatePLString(FontOptions[i]);
124 val = WMGetFromPLDictionary(style, key);
125 if (val) {
126 oldfont = WMGetFromPLString(val);
127 newfont = convertFont(oldfont, keepXLFD);
128 if (oldfont != newfont) {
129 val = WMCreatePLString(newfont);
130 WMPutInPLDictionary(style, key, val);
131 WMReleasePropList(val);
132 wfree(newfont);
133 }
134 }
135 WMReleasePropList(key);
136 }
137
138 WMWritePropListToFile(style, file, True);
139
140 exit(0);
141 }