2 Copyright © 1997-2011, The AROS Development Team. All rights reserved.
5 Desc: Convert Amiga font to C code.
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>
18 #include <dos/rdargs.h>
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
)
32 UBYTE outfilename
[200];
33 UBYTE prestring_buf
[200], *prestring
= NULL
;
38 STRPTR args
[4] = {NULL
, NULL
, NULL
, NULL
};
40 int error
= RETURN_OK
;
43 SDInit(); /* Init debugging */
44 rda
= ReadArgs("NAME,SIZE,OUTFILE,PRESTRING/K", (IPTR
*)args
, NULL
);
47 if (args
[0] == NULL
|| args
[1] == NULL
|| args
[2] == NULL
)
49 printf("Usage: NAME,SIZE,OUTFILE,PRESTRING/K\n");
54 strcpy(fontname
, args
[0]);
55 ysize
= atoi(args
[1]);
56 strcpy(outfilename
, args
[2]);
59 strcpy(prestring_buf
, args
[3]);
60 strcat(prestring_buf
, "_");
61 prestring
= prestring_buf
;
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");
82 strcat(fontname
, ".font");
85 GfxBase
= (struct GfxBase
*)OpenLibrary("graphics.library", 37);
88 printf("Could not open gfx.library v37\n");
93 DiskfontBase
= OpenLibrary("diskfont.library", 37);
96 printf("Couldn't open diskfont.library v37\n");
100 /* Open output file */
102 outfile
= fopen(outfilename
, "w");
105 printf("Could not open outfile \"%s\"", outfilename
);
112 fontname
, ysize
, 0, 0
115 tf
= OpenDiskFont(&ta
);
118 printf("Could not open textfont\n");
122 if (tf
->tf_Style
& FSF_COLORFONT
)
124 printf("Does not handle color fonts yet\n");
128 font2c(tf
, outfile
, prestring
);
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) */
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
[] =
170 const STRPTR font_flags
[] =
182 static UBYTE
print_tf_flags(struct TextFont
*tf
, FILE *fh
, const STRPTR
*nametab
, UBYTE flags
)
185 BOOL flag_found
= FALSE
;
188 for (i
= 0; i
< 8; i
++)
190 if (flags
& (1L << i
))
195 fprintf(fh
, "%s", nametab
[i
]);
205 static VOID UpdateIndent(UBYTE indent, UBYTE *tabs)
212 static VOID
font2c(struct TextFont
*tf
, FILE * fh
, STRPTR prestring
)
214 ULONG numchars
= NUMCHARS(tf
);
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 */
237 fprintf(fh
, "\n\n\n");
238 fprintf(fh
, "const ULONG %scharloc[] =\n", prestring
);
239 print_long_array((ULONG
*)tf
->tf_CharLoc
, numchars
, fh
);
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
);
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
);
284 if (!print_tf_flags(tf
, fh
, font_styles
, tf
->tf_Style
))
285 fprintf(fh
, "FS_NORMAL");
287 fprintf(fh
, ",\t/* Style */\n");
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
);
306 fprintf(fh
, "\t(APTR)%scharloc,\t/* CharLoc */\n", prestring
);
308 fprintf(fh
, "\tNULL,\t/* CharLoc */\n");
310 if (tf
->tf_CharSpace
)
311 fprintf(fh
, "\t(APTR)%scharspace,\t/* CharSpace */\n", prestring
);
313 fprintf(fh
, "\tNULL,\t/* CharSpace */\n");
316 fprintf(fh
, "\t(APTR)%scharkern,\t/* CharKern */\n", prestring
);
318 fprintf(fh
, "\tNULL\t/* CharKern */\n");
327 static VOID
print_word_array(UWORD
*array
, ULONG len
, FILE *fh
)
332 for (i
= 0; i
< len
- 1; i
++) /* len - 1 because last item needs ',' removed */
338 fprintf(fh
,"0x%.4x, ", *array
++);
341 /* print last entry without ',' at the end */
346 fprintf(fh
, "0x%.4x\n", *array
);
352 static VOID
print_long_array(ULONG
*array
, ULONG len
, FILE *fh
)
358 for (i
= 0; i
< len
- 1; i
++) /* numchars - 1 because last item needs ',' removed */
364 fprintf(fh
,"0x%.8lx, ", (unsigned long)(*array
++));
367 /* print last entry without ',' at the end */
372 fprintf(fh
, "0x%.8lx\n", (unsigned long)(*array
));
379 static VOID
print_chardata_ascii(struct TextFont
*tf
, FILE *fh
)
381 UWORD i
, numchars
= NUMCHARS(tf
);
384 for (i
= 0; i
< numchars
; i
++ )
392 /* Warning: Should these be used for something? */
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
++)
405 bitno
= charloc
>> 16;
406 width
= charloc
& 0xFFFF;
407 /* Extract data for this glyph */
412 if ( charptr
[bitno
>> 3] & (1 << ((~bitno
) & 0x07)) )
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) */