aboutsummaryrefslogtreecommitdiff
path: root/sys/gio/fonts/mkfont.c
blob: 841d99f571e4f25ee6acf2ed708644951cdc0617 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <stdio.h>

#define	DEBUG		0
#define	MASK		0x3F
#define	SCALE		1.0

#define max(a,b)		(a > b ? a : b)
#define min(a,b)		(a < b ? a : b)
/*
#define	XCOORD()		((*dp - 'R') + 9)
#define	YCOORD()		(('R' - *dp) + 14)
#define	XCOORD()	(max(0,min(20,((int)(((*dp - 'R') + 9)*SCALE+0.5)))))
#define	XCOORD()	(max(0,min(20,((int)(((*dp - 'R') - minx)*SCALE+0.5)))))
#define	YCOORD()	((int)((('R' - *dp) + 13) * SCALE + 0.5))
#define	XCOORD()	((int)(((*dp - 'R') - minx) * SCALE + 0.5))
#define	YCOORD()	(max(0,min(32,(((int)(('R' - *dp) + 13)*SCALE+0.5)))))
*/
#define	XCOORD()	(max(0,((int)(((*dp - 'R') - minx - 2) * SCALE + 0.5))))
#define	YCOORD()	(max(0,min(35,(((int)(('R' - *dp) + 16)*SCALE+0.5)))))
#define ENCODE(pen,x,y) ((int)(((pen<<12)|((x&MASK)<<6))|(y&MASK)))

int 	chridx[200];		/* Character index table	*/
int 	chrwid[200];		/* Character width table	*/
int 	chrtab[5000];		/* Character stroke table	*/


struct hershey_tab {
    int   num;                  /* hershey number       */
    int   length;               /* length               */
    char  *code;                /* stroke data string   */
} htab[] = {
#include "hershey.dat"
};

int	encode();


main (argc, argv)
int	argc;
char 	*argv[];
{
	register int i=0;
	int	minx, maxx, charnum=0, idx=0, hnum, hindex, hlength;
	short	x, y, pen, xspace, yspace;
	char 	ch, *dp, *data;

	/* Read all the hershey numbers from standard input and build up a
	 * table of stroke data.
	 */
	ch = 32;
	while (scanf ("%d", &hnum) != EOF) {

	    chridx[charnum] = idx + 1;

	    /* Get the index for the given number. */
	    for (hindex=0; hnum != htab[hindex].num; hindex++)
		;

	    hlength = htab[hindex].length;
	    dp = data = htab[hindex].code;

	    if (DEBUG)
		printf ("'%c' %4d: index=%4d len=%3d dlen=%3d %s\n",
		    ch, hnum, hindex, hlength, strlen(data),
		    (strlen(data) % 2) ? "ERROR" : "");

	    /* Now decode the stroke data into X-Y pairs, first pair is for
	     * proportional spacing.
	     */
	    minx = (*dp - 'R'); dp++;
	    maxx = (*dp - 'R'); dp++;
	    chrwid[charnum++] = min (32, maxx - minx + 5);

	    if (DEBUG) printf("\twidth (%02d) (%d,%d)\n", maxx-minx,minx,maxx);

	    /* Next pair is the initial move.  The Y coords are flipped
	     * for what we need so fix that every place we get a Yval.
	     */
	    pen = 0;
	    x = XCOORD(); dp++;
x = (ch == '1' ? x-3: x);
	    y = YCOORD(); dp++;
	    chrtab[idx++] = ENCODE(pen, x, y);

	    if (DEBUG) printf ("\tmove (%3d,%3d) '%s'\n", x, y, dp);

	    /* The remainder of the codes are move/draw strokes.
	     */
	    for (i=0; i < (hlength-2); i++) {
		if (*dp == ' ') {
		    pen = 0;
		    x = XCOORD(); dp++;	/* skip pen-up coords */
x = (ch == '1' ? x-3: x);
		    y = YCOORD(); dp++;
		    i++;
		} else
		    pen = 1;
		x = XCOORD(); dp++;
x = (ch == '1' ? x-3: x);
		y = YCOORD(); dp++;

		chrtab[idx++] = ENCODE(pen, x, y);

	        if (DEBUG) 
		    printf("\t%s (%3d,%3d) => %6d\n",
			pen?"draw":"move", x, y, ENCODE(pen,x,y));
	    }
	    chrtab[idx++] = ENCODE(0, 0, 0);
	    ch++;
	}

	print_prologue (charnum, idx);
	print_index (chridx, charnum);
	printf ("\n\n# Width data.\n\n");
	print_widths (chrwid, charnum);
	printf ("\n\n# Stroke data.\n\n");
	print_strokes (chrtab, idx);
}


print_index (idxtab, N)
int	*idxtab, N;
{
	register int i, j, start=1, end=5;

	for (i=0; i < N; ) {
	    printf ("data    (chridx(i), i=%03d,%03d) /", start, min(N,end));
	    for (j=0; j < 5 && i < N; j++)
		printf ("%5d%c", idxtab[i++], (j<4 && i<N ? ',' : '/'));
	    printf ("\n");
	    start = end + 1;
	    end += 5;
	}
}


print_widths (wtab, N)
int	*wtab, N;
{
	register int i, j, start=1, end=5;

	for (i=0; i < N; ) {
	    printf ("data    (chrwid(i), i=%03d,%03d) /", start, min(N,end));
	    for (j=0; j < 5 && i < N; j++)
		printf ("%5d%c", wtab[i++], (j<4 && i<N ? ',' : '/'));
	    printf ("\n");
	    start = end + 1;
	    end += 5;
	}
}


print_strokes (strtab, N)
int	*strtab, N;
{
	register int i, j, start=1, end=5;

	for (i=0; i < N; ) {
	    printf ("data    (chrtab(i), i=%04d,%04d) /", start, min(N,end));
	    for (j=0; j < 5 && i < N; j++)
		printf ("%6d%c", strtab[i++], (j<4 && i<N ? ',' : '/'));
	    printf ("\n");
	    start = end + 1;
	    end += 5;
	}
}


print_prologue(nidx, nchar)
int	nidx;
int	nchar;
{

printf ("# CHRTAB -- Table of strokes for the printable ASCII characters.  Each\n");
printf ("# character is encoded as a series of strokes.  Each stroke is ex-\n");
printf ("# pressed by a single integer containing the following bitfields:\n");
printf ("#\n");
printf ("#       2                   1\n");
printf ("#       0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1\n");
printf ("#                 | | | |         | |         |\n");
printf ("#                 | | | +---------+ +---------+\n");
printf ("#                 | | |      |           |\n");
printf ("#                 | | |      X           Y\n");
printf ("#                 | | |\n");
printf ("#                 | | +-- pen up/down\n");
printf ("#                 | +---- begin paint (not used at present)\n");
printf ("#                 +------ end paint (not used at present)\n");
printf ("#\n");
printf ("#----------------------------------------------------------------------------\n");
printf ("\n");
printf ("# Define the database.\n");
printf ("\n");
printf ("short   chridx[%d]\t# character index in chrtab\n", nidx+1);
printf ("short   chrwid[%d]\t# character width table\n", nidx+1);
printf ("short   chrtab[%d]\t# stroke data to draw the characters\n", nchar+1);
printf ("\n");
printf ("# Index into CHRTAB of each printable character (starting with SP)\n");
printf ("\n");
}