xref: /netbsd-src/sys/arch/amiga/stand/dumpfont/fontdumper.c (revision ae1bfcddc410612bc8c58b807e1830becb69a24c)
1 /*
2  * Routine to allow user to select from available fonts that fit restricitons of
3  * NetBSD display code and then dump that font in the format for inclusion in the
4  * kernel. Only character values 32-255 are dumped.
5  *
6  * Current kernel only allows fonts up to 8 pixels wide & non-proportional.
7  * If this changes, the font requestor flags and restriction tests will need updating.
8  * Also the NetBSDwidth value, cursor bits and dumping of font hex values needs updating.
9  *
10  * Author: Alan Bair
11  * Dated:  11/12/1993
12  *
13  * Added printing of some other useful data for future (and current) expansion.
14  * -ch
15  * Dated:  11/17/1993
16  *
17  *	$Id: fontdumper.c,v 1.2 1994/02/11 07:02:57 chopps Exp $
18  */
19 
20 /* Original code by Markus Wild */
21 /* This is a *real* hack to dump the topaz80 kernel font. This one is
22    ways nicer than the ugly Mach font, but we'll have to dump it from a
23    running system to not run against Commodore copyrights. *NEVER* distribute
24    the generated font with BSD, always regenerate! */
25 
26 #include <exec/types.h>
27 #include <exec/memory.h>
28 #include <dos/dos.h>
29 #include <graphics/gfx.h>
30 #include <graphics/rastport.h>
31 #include <graphics/text.h>
32 #include <libraries/asl.h>
33 
34 #include <inline/exec.h>
35 #include <inline/graphics.h>
36 
37 #include <stdio.h>
38 
39 #define NetBSDwidth	8
40 
41 main(int argc, char *argv[])
42 {
43     unsigned char str[256];
44     int i;
45     int j;
46     struct RastPort rp;
47     unsigned char *pp;
48     struct BitMap bm = {
49 	256, 	/* bytes per row */
50 	8,	/* rows */
51 	0,	/* flags */
52 	1,	/* depth */
53 	0,	/* pad */
54 	0 	/* planes */
55 	};
56     struct TextAttr ta;
57     struct TextFont *tf;
58     struct FontRequester *fr;
59     struct TagItem frtags[] = {
60 	ASL_Hail, (ULONG)"NetBSD font choices",
61 	ASL_Width, 640,
62 	ASL_Height, 400,
63 	ASL_LeftEdge, 10,
64 	ASL_TopEdge, 10,
65 	ASL_OKText, (ULONG)"Dump",
66 	ASL_CancelText, (ULONG)"Cancel",
67 	ASL_FontName, (ULONG)"topaz.font",
68 	ASL_FontHeight, 8L,
69 	ASL_FontStyles, FS_NORMAL,
70 	ASL_FuncFlags, FONF_STYLES | FONF_FIXEDWIDTH,
71 	TAG_DONE
72 	    };
73 
74     /* Let the user pick a font to dump */
75     if (fr = (struct FontRequester *)
76 	AllocAslRequest(ASL_FontRequest, frtags)) {
77 	if (!AslRequest(fr, NULL)) {
78 	    FreeAslRequest(fr);
79 	    fprintf(stderr, "User requested exit\n");
80 	    exit (0);
81 	}
82 	ta.ta_Name = (STRPTR)malloc(strlen(fr->fo_Attr.ta_Name));
83 	strcpy(ta.ta_Name, fr->fo_Attr.ta_Name);
84 	ta.ta_YSize = fr->fo_Attr.ta_YSize;
85 	ta.ta_Style = fr->fo_Attr.ta_Style;
86 	ta.ta_Flags = fr->fo_Attr.ta_Flags;
87 	FreeAslRequest(fr);
88     } else {
89 	fprintf(stderr, "Can't allocate Font Requestor\n");
90 	exit (1);
91     }
92 
93     /* Open the selected font */
94     tf = (struct TextFont *)OpenDiskFont (&ta);
95     if (! tf) {
96 	fprintf (stderr, "Can't open font: %s\n", ta.ta_Name);
97 	exit (1);
98     }
99 #ifdef DEBUG
100     fprintf(stderr, "Information on selected font:\n");
101     fprintf(stderr, "Name=%s\n", ta.ta_Name);
102     fprintf(stderr, "Height=%d tf_Style=%x tf_Flags=%x Width=%d Baseline=%d\n",
103 	    tf->tf_YSize, tf->tf_Style, tf->tf_Flags, tf->tf_XSize, tf->tf_Baseline);
104 #endif
105 
106     /* Check for NetBSD restrictions */
107     if (tf->tf_Flags & FPF_PROPORTIONAL) {
108 	fprintf(stderr, "NetBSD does not support proportional fonts\n");
109 	exit (1);
110     }
111     if (tf->tf_XSize > NetBSDwidth) {
112 	fprintf(stderr, "NetBSD does not support fonts wider than %d pixels\n", NetBSDwidth);
113 	exit (1);
114     }
115 
116     /* Allocate area to render font in */
117     InitBitMap(&bm, 1, 256 * NetBSDwidth, tf->tf_YSize);
118     InitRastPort (&rp);
119     rp.BitMap = &bm;
120     bm.Planes[0] = pp = AllocRaster (256 * NetBSDwidth, tf->tf_YSize);
121     if (!pp) {
122 	fprintf (stderr, "Can't allocate raster!\n");
123 	exit (1);
124     }
125 
126     /* Initialize string to be rendered */
127     for (i = 32; i < 256; i++) {
128 	str[i - 32] = i;
129     }
130 
131     /* Render string with selected font */
132     SetFont (&rp, tf);
133     SetSoftStyle(&rp, ta.ta_Style ^ tf->tf_Style, (FSF_BOLD | FSF_UNDERLINED | FSF_ITALIC));
134     Move (&rp, 0, tf->tf_Baseline);
135     ClearEOL(&rp);
136     if (tf->tf_XSize != NetBSDwidth) {
137 	/* right-justify char in cell */
138 	Move (&rp, NetBSDwidth - tf->tf_XSize, tf->tf_Baseline);
139 	/* Narrow font, put each character in space of normal font */
140 	for (i = 0; i < (256 - 32); i++) {
141 	    Text (&rp, &str[i], 1);
142 	    Move (&rp, rp.cp_x + (NetBSDwidth - tf->tf_XSize), rp.cp_y);
143 	}
144     } else {
145 	Text (&rp, str, 256 - 32);
146     }
147 
148     /* Dump them.. */
149     printf ("/* Generated automatically by fontdumper.c. *DONT* distribute\n");
150     printf ("   this file, it may contain information Copyright by Commodore!\n");
151     printf ("\n");
152     printf ("   Font: %s/%d\n", ta.ta_Name, tf->tf_YSize);
153     printf (" */\n\n");
154 
155     printf ("unsigned char kernel_font_width  = %d;\n", tf->tf_XSize);
156     printf ("unsigned char kernel_font_height = %d;\n", tf->tf_YSize);
157     printf ("unsigned char kernel_font_baseline = %d;\n", tf->tf_Baseline);
158     printf ("short         kernel_font_boldsmear = %d;\n", tf->tf_BoldSmear);
159     printf ("unsigned char kernel_font_lo = 32;\n");
160     printf ("unsigned char kernel_font_hi = 255;\n\n");
161 
162     printf ("unsigned char kernel_cursor[] = {\n");
163     for (j = 0; j < (tf->tf_YSize -1); j++) {
164 	printf ("0xff, ");
165     }
166     printf ("0xff };\n\n");
167 
168     printf ("unsigned char kernel_font[] = {\n");
169     for (i = 0; i < 256 - 32; i++) {
170 	printf ("/* %c */", i + 32);
171 	for (j = 0; j < tf->tf_YSize; j++) {
172 	    printf (" 0x%02x,", pp[i+j*256]);
173 	    }
174 	printf ("\n");
175     }
176     printf ("};\n");
177 
178     CloseFont (tf);
179     FreeRaster (pp, 256 * NetBSDwidth, tf->tf_YSize);
180     return (0);
181 }
182