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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
47 static GSList *fonts = NULL;
48 static GSList *cursors = NULL;
52 static gboolean debug = FALSE;
54 #define HEX(c) (((c) >= '0' && (c) <= '9') ? \
55 ((c) - '0') : (toupper(c) - 'A' + 10))
57 static void print_font(fi)
62 for (y = 0; y < dh; y++)
64 for (x = 0; x < dw; x++)
66 printf(fi->bitmap[y*dw+x]? "X" : " ");
72 static void print_cursor(ci)
77 for (y = 0; y < ci->height; y++)
80 for (x = 0; x < ci->width; x++)
82 if (ci->hotx == x && ci->hoty == y)
85 switch (ci->data[y*ci->width+x])
102 static gint read_bdf_font(fname)
108 gboolean startchar = FALSE, startbitmap = FALSE;
109 gchar *charname,*p,*bitmap;
117 if (!(f = fopen(fname, "r")))
123 if (fgets(line, sizeof(line), f) && strncasecmp("STARTFONT ", line, 10))
125 printf("!BDF font file\n");
131 while (fgets(line, sizeof(line), f))
135 if (!strncasecmp("STARTCHAR ", line, 10))
138 charname = g_strndup(p + 10,
139 strcspn(p+10, "\r\n"));
141 else if (!strncasecmp("FONTBOUNDINGBOX ", line, 16))
142 sscanf(p+16, "%d %d %d %d", &dw, &dh, &dx, &dy);
146 if (!strncasecmp("ENDCHAR", line, 7))
151 printf(" %*s*/\n", dw, "");
155 nfi = g_malloc(sizeof(font_info_t));
156 memset(nfi, '\0', sizeof(font_info_t));
158 nfi->name = charname;
160 nfi->bitmap = bitmap;
164 fonts = g_slist_append(fonts, nfi);
166 else if (startbitmap)
171 px = x - dx + py * dw;
172 for (cx = 0; cx < w; cx++)
174 mask = 1 << (3 - (cx % 4));
177 (mask & HEX(line[cx/4])) != 0;
180 printf(bitmap[px+cx] ? "X" : " ");
184 printf(" %*s*/\n/* %*s", dw-w, "", dw+dx, "");
186 else if (!strncasecmp("BBX ", line, 4))
188 sscanf(p+4, "%d %d %d %d", &w, &h, &x, &y);
190 printf("/* %s: */\n/* %*s", charname, dw+dx, "");
192 else if (!strncasecmp("ENCODING ", line, 9))
194 if (sscanf(p+9, "%d %d", &tmp, &id) != 2)
197 else if (!strncasecmp("BITMAP", line, 6))
201 bitmap = g_malloc(dw*dh);
202 memset(bitmap, '\0', dw*dh);
207 if (strncasecmp("ENDFONT", line, 7))
215 static gint font_info_compare(fi, name)
219 return strcmp(name, fi->name);
222 static cursor_info_t *gen_cursor(bmap, mask)
227 int bx = dw,by = dh,ex = 0,ey = 0;
230 for (j = 0; j < dh; j++)
232 gboolean havep = FALSE;
234 for (i = 0; i < dw; i++)
236 if (bmap->bitmap[j*dw+i] || mask->bitmap[j*dw+i])
251 ci = g_malloc(sizeof(cursor_info_t));
252 ci->name = g_strdup(bmap->name);
256 ci->height = ey - by;
258 ci->hotx = bmap->hotx - bx;
259 ci->hoty = ci->height - (bmap->hoty - by);
261 ci->data = g_malloc(ci->width * ci->height);
262 memset(ci->data, '\0', ci->width * ci->height);
264 for (j = 0; j < ci->height; j++)
266 for (i = 0; i < ci->width; i++)
268 int ofs = (by + j) * dw + bx + i;
270 ci->data[j*ci->width + i] = mask->bitmap[ofs] *
271 (1 + bmap->bitmap[ofs]);
278 static void compose_cursors_from_fonts()
282 for (l = g_slist_copy (fonts); l; l = g_slist_delete_link (l,l))
284 font_info_t *fi = l->data;
288 name = g_strconcat(fi->name, "_mask", NULL);
290 if ((ml = g_slist_find_custom(fonts, name,
291 (GCompareFunc) font_info_compare)))
293 cursors = g_slist_append(cursors, gen_cursor(l->data, ml->data));
294 fonts = g_slist_remove(fonts, l->data);
295 fonts = g_slist_remove(fonts, ml->data);
302 static char *dump_cursor(ci, id)
306 static gchar cdata[8192];
312 sprintf(cdata, " { \"%s\", %d, %d, %d, %d, %d, \n \"",
313 ci->name, ci->id, ci->width, ci->height, ci->hotx, ci->hoty);
314 p = cdata + strlen(cdata);
316 for (i = 0; i < ci->width * ci->height; i++)
330 sprintf(p, "\\%03o", c);
334 if (i > 0 && !(i % 64))
336 strcpy(p ,"\"\n \"");
342 sprintf(p, "\\%03o", c);
351 static int dump_cursors()
356 fprintf(f, "static const struct { const gchar *name; gint type; guchar width; guchar height; guchar hotx; guchar hoty; guchar *data; } cursors[] = {\n");
358 for (ptr = cursors; ptr; ptr = ptr->next)
361 print_cursor(ptr->data);
362 fprintf(f, "%s, \n", dump_cursor(ptr->data));
365 fprintf(f, " { NULL, 0, 0, 0, 0, 0, NULL },\n};\n");
370 gint main(argc, argv)
376 printf("missing parameters !\n");
377 printf("Usage: %s [BDF cursor file]\n", argv[0]);
381 if (g_getenv ("BDFCURSOR_DEBUG") != NULL)
384 if (read_bdf_font(argv[1]) || !fonts)
386 printf("Error reading font\n");
390 compose_cursors_from_fonts();
394 printf("failed to generate cursors from font!\n");
402 printf("some fonts remained unconverted!\n");