- Check whether libXft is at least version 2.1.2 else refuse to compile.
[wmaker-crm.git] / util / convertfonts.c
blob2ed83a8a8290fa84b5696dcc0729e7acc324072d
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.
24 #define PROG_VERSION "convertfonts (Window Maker) 1.0"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <sys/stat.h>
29 #include <string.h>
30 #include <WINGs/WUtil.h>
32 #include "../src/wconfig.h"
34 #define DEFAULT_FONT "sans-serif:pixelsize=12"
36 char *FontOptions[] = {
37 "IconTitleFont",
38 "ClipTitleFont",
39 "DisplayFont",
40 "LargeDisplayFont",
41 "MenuTextFont",
42 "MenuTitleFont",
43 "WindowTitleFont",
44 "SystemFont",
45 "BoldSystemFont",
46 NULL
50 char *ProgName;
53 static int
54 countChar(char *str, char c)
56 int count = 0;
58 if (!str)
59 return 0;
61 for (; *str!=0; str++) {
62 if (*str == c) {
63 count++;
67 return count;
71 typedef struct str {
72 char *str;
73 int len;
74 } str;
76 #define XLFD_TOKENS 14
79 static str*
80 getXLFDTokens(char *xlfd)
82 static str tokens[XLFD_TOKENS];
83 int i, len, size;
84 char *ptr;
86 if (!xlfd || *xlfd!='-' || countChar(xlfd, '-')<XLFD_TOKENS)
87 return NULL;
89 memset(tokens, 0, sizeof(str)*XLFD_TOKENS);
91 len = strlen(xlfd);
93 for (ptr=xlfd, i=0; i<XLFD_TOKENS && len>0; i++) {
94 /* skip one '-' */
95 ptr++;
96 len--;
97 if (len <= 0)
98 break;
99 size = strcspn(ptr, "-");
100 tokens[i].str = ptr;
101 tokens[i].len = size;
102 ptr += size;
103 len -= size;
106 return tokens;
110 static int
111 strToInt(str *token)
113 int res=0, pos, c;
115 if (token->len==0 || token->str[0]=='*') {
116 return -1;
117 } else {
118 for (res=0, pos=0; pos<token->len; pos++) {
119 c = token->str[pos] - '0';
120 if (c<0 || c>9)
121 break;
122 res = res*10 + c;
125 return res;
129 static char*
130 mapSlantToName(str *slant)
132 if (slant->len==0 || slant->str[0]=='*')
133 return "roman";
135 switch(slant->str[0]) {
136 case 'i':
137 return "italic";
138 case 'o':
139 return "oblique";
140 case 'r':
141 default:
142 return "roman";
147 char*
148 xlfdToFc(char *xlfd, char *useFamily, Bool keepXLFD)
150 str *tokens, *family, *weight, *slant;
151 char *name, buf[512];
152 int size, pixelsize;
154 tokens = getXLFDTokens(xlfd);
155 if (!tokens)
156 return wstrdup(DEFAULT_FONT);
158 family = &(tokens[1]);
159 weight = &(tokens[2]);
160 slant = &(tokens[3]);
162 if (useFamily) {
163 name = wstrdup(useFamily);
164 } else {
165 if (family->len==0 || family->str[0]=='*')
166 return wstrdup(DEFAULT_FONT);
168 sprintf(buf, "%.*s", family->len, family->str);
169 name = wstrdup(buf);
172 pixelsize = strToInt(&tokens[6]);
173 size = strToInt(&tokens[7]);
175 if (size<=0 && pixelsize<=0) {
176 name = wstrappend(name, ":pixelsize=12");
177 } else if (pixelsize>0) {
178 /* if pixelsize is present size will be ignored so we skip it */
179 sprintf(buf, ":pixelsize=%d", pixelsize);
180 name = wstrappend(name, buf);
181 } else {
182 sprintf(buf, "-%d", size/10);
183 name = wstrappend(name, buf);
186 if (weight->len>0 && weight->str[0]!='*') {
187 sprintf(buf, ":weight=%.*s", weight->len, weight->str);
188 name = wstrappend(name, buf);
191 if (slant->len>0 && slant->str[0]!='*') {
192 sprintf(buf, ":slant=%s", mapSlantToName(slant));
193 name = wstrappend(name, buf);
196 if (keepXLFD) {
197 name = wstrappend(name, ":xlfd=");
198 name = wstrappend(name, xlfd);
201 return name;
205 /* return converted font (if conversion is needed) else the original font */
206 static char*
207 convertFont(char *font, Bool keepXLFD)
209 if (font[0]=='-') {
210 if (!strchr(font, ',')) {
211 return xlfdToFc(font, NULL, keepXLFD);
212 } else {
213 return xlfdToFc(font, "sans-serif", keepXLFD);
215 } else {
216 return font;
221 void
222 print_help()
224 printf("\nUsage: %s <style_file>\n\n", ProgName);
225 puts("Converts fonts in a style file into fontconfig format");
226 puts("");
227 puts(" --help display this help and exit");
228 puts(" --version output version information and exit");
229 puts(" --keep-xlfd preserve the original xlfd by appending a ':xlfd=<xlfd>' hint");
230 puts(" to the font name. This property is not used by the fontconfig");
231 puts(" matching engine to find the font, but it is useful as a hint");
232 puts(" about what the original font was to allow hand tuning the");
233 puts(" result or restoring the xlfd. The default is to not add it");
234 puts(" as it results in long, unreadable and confusing names.");
235 puts(" --sets-too try to approximate fontsets by using their first complete xlfd.");
236 puts(" This only works for singlebyte languages. The default is to");
237 puts(" replace the fontset with the default: 'sans-serif:pixelsize=12'");
238 puts(" which should display properly for any language.");
239 puts("");
242 // replace --sets-too with something better
244 main(int argc, char **argv)
246 WMPropList *style, *key, *val;
247 char *file = NULL, *oldfont, *newfont;
248 struct stat statbuf;
249 Bool keepXLFD = False;
250 int i;
252 ProgName = argv[0];
254 if (argc<2) {
255 print_help();
256 exit(0);
259 for (i=1; i < argc; i++) {
260 if (strcmp("--version", argv[i])==0) {
261 puts(PROG_VERSION);
262 exit(0);
263 } else if (strcmp("--help", argv[i])==0) {
264 print_help();
265 exit(0);
266 } else if (strcmp("--keep-xlfd", argv[i])==0) {
267 keepXLFD = True;;
268 } else if (argv[i][0]=='-') {
269 printf("%s: invalid argument '%s'\n", ProgName, argv[i]);
270 printf("Try '%s --help' for more information\n", ProgName);
271 exit(1);
272 } else {
273 file = argv[i];
277 WMPLSetCaseSensitive(False);
279 if (stat(file, &statbuf) < 0) {
280 perror(file);
281 exit(1);
284 style = WMReadPropListFromFile(file);
285 if (!style) {
286 perror(file);
287 printf("%s: could not load style file.\n", ProgName);
288 exit(1);
291 if (!WMIsPLDictionary(style)) {
292 printf("%s: '%s' is not a well formatted style file\n", ProgName, file);
293 exit(1);
296 for (i=0; FontOptions[i]!=NULL; i++) {
297 key = WMCreatePLString(FontOptions[i]);
298 val = WMGetFromPLDictionary(style, key);
299 if (val) {
300 oldfont = WMGetFromPLString(val);
301 newfont = convertFont(oldfont, keepXLFD);
302 if (oldfont != newfont) {
303 val = WMCreatePLString(newfont);
304 WMPutInPLDictionary(style, key, val);
305 WMReleasePropList(val);
306 wfree(newfont);
309 WMReleasePropList(key);
312 WMWritePropListToFile(style, file, True);
314 exit(0);