Accept times without seconds.
[tangerine.git] / workbench / demos / font2c.c
blob35bc7c624fd89d0a915f9174e5bdd2ab88579bf1
1 /*
2 Copyright © 1997-98, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Convert Amiga font to C code.
6 Lang: english
7 */
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <proto/exec.h>
13 #include <proto/graphics.h>
14 #include <proto/dos.h>
15 #include <proto/diskfont.h>
16 #include <graphics/text.h>
17 #include <dos/dos.h>
18 #include <dos/rdargs.h>
20 #define SDEBUG 1
21 #define DEBUG 1
22 #include <aros/debug.h>
24 struct Library *DiskfontBase;
25 struct GfxBase *GfxBase;
27 static VOID font2c(struct TextFont *tf, FILE * fh, STRPTR prestring);
29 int main (int argc, char **argv)
31 UBYTE fontname[200];
32 UBYTE outfilename[200];
33 UBYTE prestring_buf[200], *prestring = NULL;
34 UWORD ysize = 0;
37 STRPTR s;
38 STRPTR args[4] = {NULL, NULL, NULL, NULL};
39 struct RDArgs *rda;
40 int error = RETURN_OK;
43 SDInit(); /* Init debugging */
44 rda = ReadArgs("NAME,SIZE,OUTFILE,PRESTRING/K", (IPTR *)args, NULL);
45 if (rda)
47 if (args[0] == NULL || args[1] == NULL || args[2] == NULL)
49 printf("Usage: NAME,SIZE,OUTFILE,PRESTRING/K\n");
50 error = RETURN_FAIL;
52 else
54 strcpy(fontname, args[0]);
55 ysize = atoi(args[1]);
56 strcpy(outfilename, args[2]);
57 if (args[3])
59 strcpy(prestring_buf, args[3]);
60 strcat(prestring_buf, "_");
61 prestring = prestring_buf;
63 else
64 prestring = "";
67 FreeArgs(rda);
69 else
70 error = RETURN_FAIL;
72 if (error == RETURN_OK)
74 /* Look for .font, append it if it doesn't exist */
75 if (( s = strrchr(fontname, '.')))
77 if (0 != strcmp(s, ".font"))
78 strcat(fontname, ".font");
80 else
82 strcat(fontname, ".font");
85 GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 37);
86 if (!GfxBase)
88 printf("Could not open gfx.library v37\n");
91 else
93 DiskfontBase = OpenLibrary("diskfont.library", 37);
94 if (!DiskfontBase)
96 printf("Couldn't open diskfont.library v37\n");
98 else
100 /* Open output file */
101 FILE *outfile;
102 outfile = fopen(outfilename, "w");
103 if (!outfile)
105 printf("Could not open outfile \"%s\"", outfilename);
107 else
109 struct TextFont *tf;
110 struct TextAttr ta =
112 fontname, ysize, 0, 0
115 tf = OpenDiskFont(&ta);
116 if (!tf)
118 printf("Could not open textfont\n");
120 else
122 if (tf->tf_Style & FSF_COLORFONT)
124 printf("Does not handle color fonts yet\n");
126 else
128 font2c(tf, outfile, prestring);
130 CloseFont(tf);
133 fclose(outfile);
134 } /* if (outfile opened) */
136 CloseLibrary(DiskfontBase);
138 } /* if (diskfont opened) */
140 CloseLibrary((struct Library *)GfxBase);
142 } /* if (gfx opened) */
144 } /* if (ReadArgs went ok) */
146 return error;
150 #define NUMCHARS(tf) ((tf->tf_HiChar - tf->tf_LoChar) + 2)
152 static VOID print_word_array( UWORD *array, ULONG len, FILE *fh);
153 static VOID print_long_array( ULONG *array, ULONG len, FILE *fh);
155 static VOID print_chardata_ascii( struct TextFont *tf, FILE *fh);
158 const STRPTR font_styles[] =
160 "FSF_UNDERLINED",
161 "FSF_BOLD",
162 "FSF_ITALIC",
163 "FSF_EXTENDED",
164 "FSF_COLORFONT",
165 "NOFLAG",
166 "NOFLAG",
167 "FSF_TAGGED"
170 const STRPTR font_flags[] =
172 "FPF_ROMFONT",
173 "FPF_DISKFONT",
174 "FPF_REVPATH",
175 "FPF_TALLDOT",
176 "FPF_WIDEDOT",
177 "FPF_PROPORTIONAL",
178 "FPF_DESIGNED",
179 "FPF_REMOVED"
182 static UBYTE print_tf_flags(struct TextFont *tf, FILE *fh, const STRPTR *nametab, UBYTE flags)
184 UWORD i;
185 BOOL flag_found = FALSE;
186 UBYTE num_set = 0;
188 for (i = 0; i < 8; i ++)
190 if (flags & (1L << i))
192 if (flag_found)
193 fprintf(fh, " | ");
195 fprintf(fh, "%s", nametab[i]);
196 num_set ++;
201 return num_set;
205 static VOID UpdateIndent(UBYTE indent, UBYTE *tabs)
207 while (indent --)
208 *tabs ++ = '\t';
209 *tabs = 0;
212 static VOID font2c(struct TextFont *tf, FILE * fh, STRPTR prestring)
214 ULONG numchars = NUMCHARS(tf);
216 UBYTE tabs[10];
217 UBYTE ind = 0;
219 updateindent(ind, tabs);
221 fprintf(fh, "#include <graphics/text.h>\n");
223 fprintf(fh, "\n\n\n");
224 print_chardata_ascii(tf, fh);
226 fprintf(fh, "\n\n\n");
228 if ((tf->tf_Style & FSF_COLORFONT) == 0)
230 fprintf(fh, "const UWORD %schardata[] =\n", prestring);
231 print_word_array((UWORD *)tf->tf_CharData, (tf->tf_YSize * tf->tf_Modulo) / 2, fh);
232 /* We divide by two since we want number of *words*, not bytes */
235 if (tf->tf_CharLoc)
237 fprintf(fh, "\n\n\n");
238 fprintf(fh, "const ULONG %scharloc[] =\n", prestring);
239 print_long_array((ULONG *)tf->tf_CharLoc, numchars , fh);
242 if (tf->tf_CharKern)
244 fprintf(fh, "\n\n\n");
245 fprintf(fh, "const UWORD %scharkern[] =\n", prestring);
246 print_word_array((UWORD *)tf->tf_CharKern, numchars, fh);
251 if (tf->tf_CharSpace)
253 fprintf(fh, "\n\n\n");
254 fprintf(fh, "const WORD %scharspace[] =\n", prestring);
255 print_word_array((UWORD *)tf->tf_CharKern, numchars, fh);
259 /*------------------------------------- */
261 fprintf(fh, "\n\n\n");
263 fprintf(fh, "const struct TextFont %stf =\n", prestring);
265 fprintf(fh, "{\n");
267 fprintf(fh, "\t{\t/* tf_Message */\n");
268 fprintf(fh, "\t\t{\t/* mn_Node */\n");
270 fprintf(fh, "\t\t\tNULL,\n"); /* ln_Succ */
271 fprintf(fh, "\t\t\tNULL,\n"); /* ln_Pred */
273 /* See <exec/nodes.h> */
274 #if (AROS_FLAVOUR & AROS_FLAVOUR_BINCOMPAT)
275 fprintf(fh, "\t\t\t%d,\n", tf->tf_Message.mn_Node.ln_Type); /* ln_Type */
276 fprintf(fh, "\t\t\t0,\n"); /* ln_Pri */
277 fprintf(fh, "\t\t\t\"%s\"\n", tf->tf_Message.mn_Node.ln_Name);
278 #else
279 fprintf(fh, "\t\t\t\"%s\",\n", tf->tf_Message.mn_Node.ln_Name);
280 fprintf(fh, "\t\t\t%d,\n", tf->tf_Message.mn_Node.ln_Type); /* ln_Type */
281 fprintf(fh, "\t\t\t0\n"); /* ln_Pri */
282 #endif
283 fprintf(fh, "\t\t},\n");
284 fprintf(fh, "\t\tNULL,\n"); /* mn_ReplyPort */
285 fprintf(fh, "\t\t0\n"); /* mn_Length */
286 fprintf(fh, "\t},\n");
288 fprintf(fh, "\t%d,\t/* YSize */\n", tf->tf_YSize);
290 fprintf(fh, "\t");
291 if (!print_tf_flags(tf, fh, font_styles, tf->tf_Style))
292 fprintf(fh, "FS_NORMAL");
294 fprintf(fh, ",\t/* Style */\n");
296 fprintf(fh, "\t");
297 print_tf_flags(tf, fh, font_flags, tf->tf_Flags);
299 fprintf(fh, ",\t/* Flags */\n");
301 fprintf(fh, "\t%d,\t/* XSize */\n", tf->tf_XSize);
302 fprintf(fh, "\t%d,\t/* Baseline */\n", tf->tf_Baseline);
303 fprintf(fh, "\t%d,\t/* Boldsmear */\n", tf->tf_BoldSmear);
304 fprintf(fh, "\t%d,\t/* Accessors */\n", tf->tf_Accessors);
305 fprintf(fh, "\t%d,\t/* LoChar */\n", tf->tf_LoChar);
306 fprintf(fh, "\t%d,\t/* HiChar */\n", tf->tf_HiChar);
308 fprintf(fh, "\t(APTR)%schardata,\t/* CharData */\n", prestring);
310 fprintf(fh, "\t%d,\t/* Modulo */\n", tf->tf_Modulo);
312 if (tf->tf_CharLoc)
313 fprintf(fh, "\t(APTR)%scharloc,\t/* CharLoc */\n", prestring);
314 else
315 fprintf(fh, "\tNULL,\t/* CharLoc */\n");
317 if (tf->tf_CharSpace)
318 fprintf(fh, "\t(APTR)%scharspace,\t/* CharSpace */\n", prestring);
319 else
320 fprintf(fh, "\tNULL,\t/* CharSpace */\n");
322 if (tf->tf_CharKern)
323 fprintf(fh, "\t(APTR)%scharkern,\t/* CharKern */\n", prestring);
324 else
325 fprintf(fh, "\tNULL\t/* CharKern */\n");
328 fprintf(fh, "};\n");
330 return;
334 static VOID print_word_array(UWORD *array, ULONG len, FILE *fh)
336 ULONG i;
337 fprintf(fh, "{");
339 for (i = 0; i < len - 1; i ++) /* len - 1 because last item needs ',' removed */
342 if ((i & 0x07) == 0)
343 fprintf(fh, "\n\t");
345 fprintf(fh,"0x%.4x, ", *array ++);
348 /* print last entry without ',' at the end */
350 if ((i & 0x07) == 0)
351 fprintf(fh, "\n\t");
353 fprintf(fh, "0x%.4x\n", *array);
354 fprintf(fh, "};");
356 return;
359 static VOID print_long_array(ULONG *array, ULONG len, FILE *fh)
361 ULONG i;
363 fprintf(fh, "{");
365 for (i = 0; i < len - 1; i ++) /* numchars - 1 because last item needs ',' removed */
368 if ((i & 0x03) == 0)
369 fprintf(fh, "\n\t");
371 fprintf(fh,"0x%.8lx, ", *array ++);
374 /* print last entry without ',' at the end */
376 if ((i & 0x03) == 0)
377 fprintf(fh, "\n\t");
379 fprintf(fh, "0x%.8lx\n", *array);
380 fprintf(fh, "};");
382 return;
386 static VOID print_chardata_ascii(struct TextFont *tf, FILE *fh)
388 UWORD i, numchars = NUMCHARS(tf);
389 UBYTE *charptr;
391 for (i = 0; i < numchars; i ++ )
393 UWORD row;
394 UWORD charspace;
395 UWORD charkern;
396 ULONG charloc;
397 UWORD bitno, width;
399 /* Warning: Should these be used for something? */
400 (void)charkern;
401 (void)charspace;
403 charptr = tf->tf_CharData;
405 /* tf_CharLoc is *allways* non-NULL */
406 charloc = ((ULONG *)tf->tf_CharLoc)[i];
408 for (row = 0; row < tf->tf_YSize; row ++)
410 fprintf(fh, "/* ");
412 bitno = charloc >> 16;
413 width = charloc & 0xFFFF;
414 /* Extract data for this glyph */
416 while (width --)
419 if ( charptr[bitno >> 3] & (1 << ((~bitno) & 0x07)) )
421 fprintf(fh, "@");
423 else
425 fprintf(fh, ".");
427 bitno ++;
430 /* Go to next row in glyph */
431 charptr += tf->tf_Modulo;
433 fprintf(fh, " */\n");
435 } /* for (each row in the glyph) */
437 } /* for (each glyph) */
438 return;