revert between 56095 -> 55830 in arch
[AROS.git] / workbench / demos / font2c.c
blob2382f8955bfd54570f6165c0ed1478e4839f1dfa
1 /*
2 Copyright © 1997-2011, 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 fprintf(fh, "\t\t\t%d,\n", tf->tf_Message.mn_Node.ln_Type); /* ln_Type */
274 fprintf(fh, "\t\t\t0,\n"); /* ln_Pri */
275 fprintf(fh, "\t\t\t\"%s\"\n", tf->tf_Message.mn_Node.ln_Name);
276 fprintf(fh, "\t\t},\n");
277 fprintf(fh, "\t\tNULL,\n"); /* mn_ReplyPort */
278 fprintf(fh, "\t\t0\n"); /* mn_Length */
279 fprintf(fh, "\t},\n");
281 fprintf(fh, "\t%d,\t/* YSize */\n", tf->tf_YSize);
283 fprintf(fh, "\t");
284 if (!print_tf_flags(tf, fh, font_styles, tf->tf_Style))
285 fprintf(fh, "FS_NORMAL");
287 fprintf(fh, ",\t/* Style */\n");
289 fprintf(fh, "\t");
290 print_tf_flags(tf, fh, font_flags, tf->tf_Flags);
292 fprintf(fh, ",\t/* Flags */\n");
294 fprintf(fh, "\t%d,\t/* XSize */\n", tf->tf_XSize);
295 fprintf(fh, "\t%d,\t/* Baseline */\n", tf->tf_Baseline);
296 fprintf(fh, "\t%d,\t/* Boldsmear */\n", tf->tf_BoldSmear);
297 fprintf(fh, "\t%d,\t/* Accessors */\n", tf->tf_Accessors);
298 fprintf(fh, "\t%d,\t/* LoChar */\n", tf->tf_LoChar);
299 fprintf(fh, "\t%d,\t/* HiChar */\n", tf->tf_HiChar);
301 fprintf(fh, "\t(APTR)%schardata,\t/* CharData */\n", prestring);
303 fprintf(fh, "\t%d,\t/* Modulo */\n", tf->tf_Modulo);
305 if (tf->tf_CharLoc)
306 fprintf(fh, "\t(APTR)%scharloc,\t/* CharLoc */\n", prestring);
307 else
308 fprintf(fh, "\tNULL,\t/* CharLoc */\n");
310 if (tf->tf_CharSpace)
311 fprintf(fh, "\t(APTR)%scharspace,\t/* CharSpace */\n", prestring);
312 else
313 fprintf(fh, "\tNULL,\t/* CharSpace */\n");
315 if (tf->tf_CharKern)
316 fprintf(fh, "\t(APTR)%scharkern,\t/* CharKern */\n", prestring);
317 else
318 fprintf(fh, "\tNULL\t/* CharKern */\n");
321 fprintf(fh, "};\n");
323 return;
327 static VOID print_word_array(UWORD *array, ULONG len, FILE *fh)
329 ULONG i;
330 fprintf(fh, "{");
332 for (i = 0; i < len - 1; i ++) /* len - 1 because last item needs ',' removed */
335 if ((i & 0x07) == 0)
336 fprintf(fh, "\n\t");
338 fprintf(fh,"0x%.4x, ", *array ++);
341 /* print last entry without ',' at the end */
343 if ((i & 0x07) == 0)
344 fprintf(fh, "\n\t");
346 fprintf(fh, "0x%.4x\n", *array);
347 fprintf(fh, "};");
349 return;
352 static VOID print_long_array(ULONG *array, ULONG len, FILE *fh)
354 ULONG i;
356 fprintf(fh, "{");
358 for (i = 0; i < len - 1; i ++) /* numchars - 1 because last item needs ',' removed */
361 if ((i & 0x03) == 0)
362 fprintf(fh, "\n\t");
364 fprintf(fh,"0x%.8lx, ", (unsigned long)(*array ++));
367 /* print last entry without ',' at the end */
369 if ((i & 0x03) == 0)
370 fprintf(fh, "\n\t");
372 fprintf(fh, "0x%.8lx\n", (unsigned long)(*array));
373 fprintf(fh, "};");
375 return;
379 static VOID print_chardata_ascii(struct TextFont *tf, FILE *fh)
381 UWORD i, numchars = NUMCHARS(tf);
382 UBYTE *charptr;
384 for (i = 0; i < numchars; i ++ )
386 UWORD row;
387 UWORD charspace;
388 UWORD charkern;
389 ULONG charloc;
390 UWORD bitno, width;
392 /* Warning: Should these be used for something? */
393 (void)charkern;
394 (void)charspace;
396 charptr = tf->tf_CharData;
398 /* tf_CharLoc is *allways* non-NULL */
399 charloc = ((ULONG *)tf->tf_CharLoc)[i];
401 for (row = 0; row < tf->tf_YSize; row ++)
403 fprintf(fh, "/* ");
405 bitno = charloc >> 16;
406 width = charloc & 0xFFFF;
407 /* Extract data for this glyph */
409 while (width --)
412 if ( charptr[bitno >> 3] & (1 << ((~bitno) & 0x07)) )
414 fprintf(fh, "@");
416 else
418 fprintf(fh, ".");
420 bitno ++;
423 /* Go to next row in glyph */
424 charptr += tf->tf_Modulo;
426 fprintf(fh, " */\n");
428 } /* for (each row in the glyph) */
430 } /* for (each glyph) */
431 return;