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