#include <exec/types.h>
#include <graphics/text.h>
#include <graphics/gfxbase.h>
#include <functions.h>
#include <stdio.h>

struct	GfxBase		*GfxBase = 0;
struct	Library		*DiskfontBase = 0;
struct	TextFont	*FontPtr = 0;

struct TextAttr NewFont = {
	NULL,			/* Font Name		*/
	0,			/* Font Height		*/
	FS_NORMAL,		/* Style		*/
	FPF_DESIGNED };		/* Preferences		*/


char name[250];

main (argc, argv)
int argc;
char **argv;
{
	FILE *fp;
	int i, j, count;
	UWORD *w;
	ULONG *l;

	if (argc != 4) {
		printf ("Usage: %s fontname fontsize outfile\n",argv[0]);
		printf ("(ie. %s diamond.font 20 DIAMOND.c)\n", argv[0]);
		exit (0);
	}

	NewFont.ta_Name = (STRPTR)argv[1];
	NewFont.ta_YSize = atoi (argv[2]);

	if (( GfxBase = (struct GfxBase *)OpenLibrary ("graphics.library", LIBRARY_VERSION)) == NULL)
		Quit ("Couldn't open graphics.library");

	if (( DiskfontBase = OpenLibrary ("diskfont.library", LIBRARY_VERSION)) == NULL)
		Quit ("Couldn't open diskfont.library");

	if (( FontPtr = (struct TextFont *)OpenDiskFont (&NewFont)) == NULL)
		Quit ("Couldn't open font");

	if (!( fp = fopen (argv[3], "w")))
		Quit ("Couldn't open outfile");

	strcpy (name, "");
	strncat (name, argv[1], strlen (argv[1]) - 5);
	strcat (name, "Font");
	name[0] = toupper (name[0]);

	fprintf (fp, "#include <graphics/text.h>\n\n");

	fprintf (fp, "static char fontname[] = \"%s\";\n\n", argv[1]);

	fprintf (fp, "static UWORD CharData[] = {\n");
	w = (UWORD *)FontPtr->tf_CharData;
	for (i=0; i<FontPtr->tf_YSize; i++) {
		count = 0;
		for (j = 0; j <FontPtr->tf_Modulo / 2; j++) {
			if (count == 0) fprintf (fp, "\t");
			fprintf (fp, "0x%04x,", *w++);
			count++;
			if (count == 8) {
				count = 0;
				fprintf (fp, "\n");
			}
		}
		if (count) {
			fprintf (fp, "\n");
		}
	}
	fprintf (fp, "};\n\n");

	fprintf (fp, "static ULONG CharLoc[] = {\n");
	l = (ULONG *)FontPtr->tf_CharLoc;
	count = 0;
	for (i=FontPtr->tf_LoChar; i<=FontPtr->tf_HiChar; i++) {
		if (count == 0) fprintf (fp, "\t");
		fprintf (fp, "0x%08x,", *l++);
		count++;
		if (count == 4) {
			count = 0;
			fprintf (fp, "\n");
		}
	}
	if (count) {
		fprintf (fp, "\n");
	}
	fprintf (fp, "};\n\n");

	w = (UWORD *)FontPtr->tf_CharSpace;
	if (w) {
		fprintf (fp, "static UWORD CharSpace[] = {\n");
		count = 0;
		for (i=FontPtr->tf_LoChar; i<=FontPtr->tf_HiChar; i++) {
			if (count == 0) fprintf (fp, "\t");
			fprintf (fp, "0x%04x,", *w++);
			count++;
			if (count == 8) {
				count = 0;
				fprintf (fp, "\n");
			}
		}
		if (count) {
			fprintf (fp, "\n");
		}
		fprintf (fp, "};\n\n");
	}

	w = (UWORD *)FontPtr->tf_CharKern;
	if (w) {
		fprintf (fp, "static UWORD CharKern[] = {\n");
		count = 0;
		for (i=FontPtr->tf_LoChar; i<=FontPtr->tf_HiChar; i++) {
			if (count == 0) fprintf (fp, "\t");
			fprintf (fp, "0x%04x,", *w++);
			count++;
			if (count == 8) {
				count = 0;
				fprintf (fp, "\n");
			}
		}
		if (count) {
			fprintf (fp, "\n");
		}
		fprintf (fp, "};\n\n");
	}

	fprintf (fp, "struct TextFont %s = {\n", name);
	fprintf (fp, "\t{\t\t/* Message\t*/\n");
	fprintf (fp, "\t\t{\t\t/* Node\t*/\n");
	fprintf (fp, "\t\t\tNULL,\t\t/* ln_Succ\t*/\n");
	fprintf (fp, "\t\t\tNULL,\t\t/* ln_Pred\t*/\n");
	fprintf (fp, "\t\t\tNT_FONT,\t/* ln_Type\t*/\n");
	fprintf (fp, "\t\t\t0,\t\t/* ln_Pri\t*/\n");
	fprintf (fp, "\t\t\tfontname,\t/* ln_Name\t*/\n");
	fprintf (fp, "\t\t},\n");
	fprintf (fp, "\t\tNULL,\t\t\t/* mn_ReplyPort\t*/\n");
	fprintf	(fp, "\t\tsizeof (%s) +\n", name);
	fprintf	(fp, "\t\tsizeof (fontname) +\n");
	fprintf	(fp, "\t\tsizeof (CharData) +\n");
	fprintf	(fp, "\t\tsizeof (CharLoc) + \n");
	if (FontPtr->tf_CharSpace) {
		fprintf	(fp, "\t\tsizeof (CharSpace) +\n");
	}
	if (FontPtr->tf_CharKern) {
		fprintf	(fp, "\t\tsizeof (CharKern) + \n");
	}
	fprintf (fp, "\t\t0,\t\t\t/* mn_Length\t*/\n");
	fprintf (fp, "\t},\n");
	fprintf (fp, "\t%u,\t\t/* tf_YSize\t*/\n", FontPtr->tf_YSize);
	fprintf (fp, "\t%u,\t\t/* tf_Style\t*/\n", FontPtr->tf_Style);
	fprintf (fp, "\t%u,\t\t/* tf_Flags\t*/\n", FontPtr->tf_Flags);
	fprintf (fp, "\t%u,\t\t/* tf_XSize\t*/\n", FontPtr->tf_XSize);
	fprintf (fp, "\t%u,\t\t/* tf_Baseline\t*/\n", FontPtr->tf_Baseline);
	fprintf (fp, "\t%u,\t\t/* tf_BoldSmear\t*/\n", FontPtr->tf_BoldSmear);
	fprintf (fp, "\t0,\t\t/* tf_Accessors\t*/\n");
	fprintf (fp, "\t%u,\t\t/* tf_LoChar\t*/\n", FontPtr->tf_LoChar);
	fprintf (fp, "\t%u,\t\t/* tf_HiChar\t*/\n", FontPtr->tf_HiChar);
	fprintf (fp, "\t(APTR)&CharData,/* tf_CharData\t*/\n");
	fprintf (fp, "\t%u,\t\t/* tf_Modulo\t*/\n", FontPtr->tf_Modulo);
	fprintf (fp, "\t(APTR)&CharLoc,\t/* tf_CharLoc\t*/\n");
	if (FontPtr->tf_CharSpace) {
		fprintf (fp, "\t(APTR)&CharSpace,/*tf_CharSpace\t*/\n");
	} else {
		fprintf (fp, "\tNULL,\t\t/* tf_CharSpace\t*/\n");
	}
	if (FontPtr->tf_CharKern) {
		fprintf (fp, "\t(APTR)&CharKern,/* tf_CharKern\t*/\n");
	} else {
		fprintf (fp, "\tNULL,\t\t/* tf_CharKern\t*/\n");
	}
	fprintf (fp, "};\n\n");

	fclose (fp);

	Quit (NULL);
}

Quit (message)
char *message;
{
	if (FontPtr)		CloseFont (FontPtr);
	if (DiskfontBase)	CloseLibrary (DiskfontBase);
	if (GfxBase)		CloseLibrary (GfxBase);

	if (message)
		printf ("error: %s\n", message);

	exit (0);
}	
