1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2001 Stefan Ondrejicka
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
45 static GSList *fonts = NULL;
46 static GSList *cursors = NULL;
50 static gboolean debug = FALSE;
52 #define HEX(c) (((c) >= '0' && (c) <= '9') ? \
53 ((c) - '0') : (toupper(c) - 'A' + 10))
55 static void print_font(fi)
60 for (y = 0; y < dh; y++)
62 for (x = 0; x < dw; x++)
64 printf(fi->bitmap[y*dw+x]? "X" : " ");
70 static void print_cursor(ci)
75 for (y = 0; y < ci->height; y++)
78 for (x = 0; x < ci->width; x++)
80 if (ci->hotx == x && ci->hoty == y)
83 switch (ci->data[y*ci->width+x])
100 static gint read_bdf_font(fname)
106 gboolean startchar = FALSE, startbitmap = FALSE;
107 gchar *charname,*p,*bitmap;
115 if (!(f = fopen(fname, "r")))
121 if (fgets(line, sizeof(line), f) && strncasecmp("STARTFONT ", line, 10))
123 printf("!BDF font file\n");
129 while (fgets(line, sizeof(line), f))
133 if (!strncasecmp("STARTCHAR ", line, 10))
136 charname = g_strndup(p + 10,
137 strcspn(p+10, "\r\n"));
139 else if (!strncasecmp("FONTBOUNDINGBOX ", line, 16))
140 sscanf(p+16, "%d %d %d %d", &dw, &dh, &dx, &dy);
144 if (!strncasecmp("ENDCHAR", line, 7))
149 printf(" %*s*/\n", dw, "");
153 nfi = g_malloc(sizeof(font_info_t));
154 memset(nfi, '\0', sizeof(font_info_t));
156 nfi->name = charname;
158 nfi->bitmap = bitmap;
162 fonts = g_slist_append(fonts, nfi);
164 else if (startbitmap)
169 px = x - dx + py * dw;
170 for (cx = 0; cx < w; cx++)
172 mask = 1 << (3 - (cx % 4));
175 (mask & HEX(line[cx/4])) != 0;
178 printf(bitmap[px+cx] ? "X" : " ");
182 printf(" %*s*/\n/* %*s", dw-w, "", dw+dx, "");
184 else if (!strncasecmp("BBX ", line, 4))
186 sscanf(p+4, "%d %d %d %d", &w, &h, &x, &y);
188 printf("/* %s: */\n/* %*s", charname, dw+dx, "");
190 else if (!strncasecmp("ENCODING ", line, 9))
192 if (sscanf(p+9, "%d %d", &tmp, &id) != 2)
195 else if (!strncasecmp("BITMAP", line, 6))
199 bitmap = g_malloc(dw*dh);
200 memset(bitmap, '\0', dw*dh);
205 if (strncasecmp("ENDFONT", line, 7))
213 static gint font_info_compare(fi, name)
217 return strcmp(name, fi->name);
220 static cursor_info_t *gen_cursor(bmap, mask)
225 int bx = dw,by = dh,ex = 0,ey = 0;
228 for (j = 0; j < dh; j++)
230 gboolean havep = FALSE;
232 for (i = 0; i < dw; i++)
234 if (bmap->bitmap[j*dw+i] || mask->bitmap[j*dw+i])
249 ci = g_malloc(sizeof(cursor_info_t));
250 ci->name = g_strdup(bmap->name);
254 ci->height = ey - by;
256 ci->hotx = bmap->hotx - bx;
257 ci->hoty = ci->height - (bmap->hoty - by);
259 ci->data = g_malloc(ci->width * ci->height);
260 memset(ci->data, '\0', ci->width * ci->height);
262 for (j = 0; j < ci->height; j++)
264 for (i = 0; i < ci->width; i++)
266 int ofs = (by + j) * dw + bx + i;
268 ci->data[j*ci->width + i] = mask->bitmap[ofs] *
269 (1 + bmap->bitmap[ofs]);
276 static void compose_cursors_from_fonts()
280 for (l = g_slist_copy (fonts); l; l = g_slist_delete_link (l,l))
282 font_info_t *fi = l->data;
286 name = g_strconcat(fi->name, "_mask", NULL);
288 if ((ml = g_slist_find_custom(fonts, name,
289 (GCompareFunc) font_info_compare)))
291 cursors = g_slist_append(cursors, gen_cursor(l->data, ml->data));
292 fonts = g_slist_remove(fonts, l->data);
293 fonts = g_slist_remove(fonts, ml->data);
300 static char *dump_cursor(ci, id)
304 static gchar cdata[8192];
310 sprintf(cdata, " { \"%s\", %d, %d, %d, %d, %d, \n \"",
311 ci->name, ci->id, ci->width, ci->height, ci->hotx, ci->hoty);
312 p = cdata + strlen(cdata);
314 for (i = 0; i < ci->width * ci->height; i++)
328 sprintf(p, "\\%03o", c);
332 if (i > 0 && !(i % 64))
334 strcpy(p ,"\"\n \"");
340 sprintf(p, "\\%03o", c);
349 static int dump_cursors()
354 fprintf(f, "static const struct { const gchar *name; gint type; guchar width; guchar height; guchar hotx; guchar hoty; guchar *data; } cursors[] = {\n");
356 for (ptr = cursors; ptr; ptr = ptr->next)
359 print_cursor(ptr->data);
360 fprintf(f, "%s, \n", dump_cursor(ptr->data));
363 fprintf(f, " { NULL, 0, 0, 0, 0, 0, NULL },\n};\n");
368 gint main(argc, argv)
374 printf("missing parameters !\n");
375 printf("Usage: %s [BDF cursor file]\n", argv[0]);
379 if (g_getenv ("BDFCURSOR_DEBUG") != NULL)
382 if (read_bdf_font(argv[1]) || !fonts)
384 printf("Error reading font\n");
388 compose_cursors_from_fonts();
392 printf("failed to generate cursors from font!\n");
400 printf("some fonts remained unconverted!\n");