1 /*
2     (C) Copyright 2007,2008, Stephen M. Cameron.
3 
4     This file is part of wordwarvi.
5 
6     wordwarvi is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     wordwarvi is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with wordwarvi; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 
20  */
21 
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #define WWVI_FONT_DEFINE_GLOBALS
26 #include "wwvi_font.h"
27 #undef  WWVI_FONT_DEFINE_GLOBALS
28 
29 typedef unsigned char stroke_t;
30 
31 /**************************
32 
33 	Here's how this works, there's a 3x7 grid. on which the
34 	letters are drawn.  You list the sequence of strokes.
35 	Use 21 to lift the pen, 99 to mark the end.
36 
37 	Inspired by Hofstadters' Creative Concepts and Creative Analogies.
38 
39                                letter 'A'    can be compactly represented by:
40 
41 		0   1   2      *   *   *    { 6, 8, 14, 12, 9, 11, 99 }
42 
43                 3   4   5      *   *   *
44 
45                 6   7   8      *---*---*
46                                        |
47                 9  10  11      *---*---*
48                                |       |
49                12  13  14      *---*---*
50 
51                15  16  17      *   *   *
52 
53                18  19  20      *   *   *
54 The grid numbers can be decoded into (x,y) coords like:
55 	x = ((n % 3) * xscale);
56 	y = (x/3) * yscale;      truncating division.
57 
58 	(not sure the above actually works)
59 
60 	instead, use decode_glyph[] to get x and y values -- see below
61 ***************************/
62 
63 static stroke_t glyph_Z[] = { 0, 2, 12, 14, 99 };
64 static stroke_t glyph_Y[] = { 0, 7, 2, 21, 7, 13, 99 };
65 static stroke_t glyph_X[] = { 0, 14, 21, 12, 2, 99 };
66 static stroke_t glyph_W[] = { 0, 12, 10, 14, 2, 21, 10, 1, 99 };
67 static stroke_t glyph_V[] = { 0, 13, 2, 99 };
68 static stroke_t glyph_U[] = { 0, 9, 13, 11, 2, 99 };
69 static stroke_t glyph_T[] = { 13, 1, 21, 0, 2, 99 };
70 static stroke_t glyph_S[] = { 9, 13, 11, 3, 1, 5, 99 };
71 static stroke_t glyph_R[] = { 12, 0, 1, 5, 7, 6, 21, 7, 14, 99 };
72 static stroke_t glyph_Q[] = { 13, 9, 3, 1, 5, 11, 13, 21, 10, 14, 99 };
73 static stroke_t glyph_P[] = { 12, 0, 1, 5, 7, 6, 99 };
74 static stroke_t glyph_O[] = { 13, 9, 3, 1, 5, 11, 13, 99 };
75 static stroke_t glyph_N[] = { 12, 0, 14, 2, 99 };
76 static stroke_t glyph_M[] = { 12, 0, 4, 2, 14, 99 };
77 static stroke_t glyph_L[] = { 0, 12, 14, 99};
78 static stroke_t glyph_K[] = { 0, 12, 21, 6, 7, 11, 14, 21, 7, 5, 2, 99};
79 static stroke_t glyph_J[] = { 9, 13, 11, 2, 99};
80 static stroke_t glyph_I[] = { 12, 14, 21, 13, 1, 21, 0, 2, 99 };
81 static stroke_t glyph_H[] = { 0, 12, 21, 2, 14, 21, 6, 8, 99 };
82 static stroke_t glyph_G[] = { 7, 8, 11, 13, 9, 3, 1, 5, 99 };
83 static stroke_t glyph_F[] = { 12, 0, 2, 21, 7, 6, 99 };
84 static stroke_t glyph_E[] = { 14, 12, 0, 2, 21, 6, 7, 99 };
85 static stroke_t glyph_D[] = { 12, 13, 11, 5, 1, 0, 12, 99 };
86 static stroke_t glyph_C[] = { 11, 13, 9, 3, 1, 5, 99 };
87 static stroke_t glyph_B[] = { 0, 12, 13, 11, 5, 1, 0, 21, 6, 8, 99 };
88 static stroke_t glyph_A[] = { 12, 3, 0, 5, 14, 21, 8, 6, 99 };
89 static stroke_t glyph_slash[] = { 12, 2, 99 };
90 static stroke_t glyph_backslash[] = { 0, 14, 99 };
91 static stroke_t glyph_pipe[] = { 1, 13, 99 };
92 static stroke_t glyph_que[] = { 13, 10, 21, 7, 5, 2, 0, 3, 99 };
93 static stroke_t glyph_bang[] = { 10, 13, 21, 1, 7, 99};
94 static stroke_t glyph_colon[] = { 6, 7, 21, 12, 13, 99 };
95 static stroke_t glyph_leftparen[] = { 2, 4, 10, 14,99 };
96 static stroke_t glyph_rightparen[] = { 0, 4, 10, 12,99 };
97 static stroke_t glyph_space[] = { 99 };
98 static stroke_t glyph_plus[] = { 6, 8, 21, 7, 13, 99 };
99 static stroke_t glyph_dash[] = { 6, 8, 99 };
100 static stroke_t glyph_0[] = { 12, 0, 2, 14, 12, 2, 99 };
101 static stroke_t glyph_9[] = { 8, 6, 0, 2, 14, 99 };
102 static stroke_t glyph_8[] = { 0, 2, 14, 12, 0, 21, 6, 8, 99 };
103 static stroke_t glyph_7[] = { 0, 2, 12, 99 };
104 static stroke_t glyph_6[] = { 2, 0, 12, 14, 8, 6, 99 };
105 static stroke_t glyph_5[] = { 2, 0, 6, 8, 14, 12, 99 };
106 static stroke_t glyph_4[] = { 0, 6, 8, 21, 2, 14, 99 };
107 static stroke_t glyph_3[] = { 0, 2, 14, 12, 21, 6, 8, 99 };
108 static stroke_t glyph_2[] = { 0, 2, 5, 9, 12, 14, 99 };
109 static stroke_t glyph_1[] = { 1, 13, 99 };
110 static stroke_t glyph_comma[] = { 13, 16, 99 };
111 static stroke_t glyph_period[] = { 12, 13, 99 };
112 static stroke_t glyph_z[] = { 6, 8, 12, 14, 99 };
113 static stroke_t glyph_y[] = { 6, 12, 14, 21, 8, 17, 19, 18,  99};
114 static stroke_t glyph_x[] = { 12, 8, 21, 6, 14, 99 };
115 static stroke_t glyph_w[] = { 6, 12, 14, 8, 21, 7, 13, 99 };
116 static stroke_t glyph_v[] = { 6, 13, 8, 99 };
117 static stroke_t glyph_u[] = { 6, 12, 14, 8, 99 };
118 static stroke_t glyph_t[] = { 14, 13, 4, 21, 6, 8, 99 };
119 static stroke_t glyph_s[] = { 8, 6, 9, 11, 14,12, 99 };
120 
121 static stroke_t glyph_a[] = { 6, 8, 14, 12, 9, 11, 99 };
122 static stroke_t glyph_b[] = { 0, 12, 14, 8, 6, 99 };
123 static stroke_t glyph_c[] = { 8, 6, 12, 14, 99 };
124 static stroke_t glyph_d[] = { 8, 6, 12, 14, 2, 99 };
125 static stroke_t glyph_e[] = { 9, 11, 8, 6, 12, 14, 99 };
126 static stroke_t glyph_f[] = { 13, 1, 2, 21, 6, 8, 99 };
127 static stroke_t glyph_g[] = { 11, 12, 6, 8, 20, 18, 99 };
128 static stroke_t glyph_h[] = { 0, 12, 21, 6, 8, 14, 99 };
129 static stroke_t glyph_i[] = { 13, 7, 21, 4, 2, 99 };
130 static stroke_t glyph_j[] = { 18, 16, 7, 21, 4, 2, 99 };
131 static stroke_t glyph_k[] = { 0, 12, 21, 6, 7, 11, 14, 21, 7, 5, 99 };
132 static stroke_t glyph_l[] = { 1, 13, 99 };
133 static stroke_t glyph_m[] = { 12, 6, 8, 14, 21, 7, 13, 99 };
134 static stroke_t glyph_n[] = { 12, 6, 7, 13, 99 };
135 static stroke_t glyph_o[] = { 12, 6, 8, 14, 12, 99 };
136 static stroke_t glyph_p[] = { 18, 6, 8, 14, 12, 99 };
137 static stroke_t glyph_q[] = { 14, 12, 6, 8, 20, 99 };
138 static stroke_t glyph_r[] = { 12, 6, 21, 9,7,8,99 };
139 static stroke_t glyph_apostrophe[] = { 7, 5, 99 };
140 static stroke_t glyph_doublequote[] = { 0, 3, 21, 1, 4, 99 };
141 static stroke_t glyph_asterisk[] = { 9, 5, 21, 3, 11, 21, 6, 8, 21, 4, 10, 99 };
142 static stroke_t glyph_underscore[] = { 18, 20, 99 };
143 
144 /* x and y offsets for decoding stroke_t's, above */
145 static struct my_point_t decode_glyph[] = {
146 	{ 0, -4 },
147 	{ 1, -4 },
148 	{ 2, -4 },
149 	{ 0, -3 },
150 	{ 1, -3 },
151 	{ 2, -3 },
152 	{ 0, -2 },
153 	{ 1, -2 },
154 	{ 2, -2 },
155 	{ 0, -1 },
156 	{ 1, -1 },
157 	{ 2, -1 },
158 	{ 0, -0 },
159 	{ 1, -0 },
160 	{ 2, -0 },
161 	{ 0, 1 },
162 	{ 1, 1 },
163 	{ 2, 1 },
164 	{ 0, 2 },
165 	{ 1, 2 },
166 	{ 2, 2 },
167 	{ 0, 3 },
168 	{ 1, 3 },
169 	{ 2, 3 },
170 };
171 
172 /* This converts a stroke_t, which is a sort of slightly compressed coding
173  * of how to draw a letter, lifted from Hofstadter's book, and converts it
174  * into a set of line segments and breaks, like all the other objects in
175  * the game, while also scaling it by some amount.  It is used in making
176  * a particular font size
177  */
prerender_glyph(stroke_t g[],int xscale,int yscale)178 static struct my_vect_obj *prerender_glyph(stroke_t g[], int xscale, int yscale)
179 {
180 	int i, x, y;
181 	int npoints = 0;
182 	struct my_point_t scratch[100];
183 	struct my_vect_obj *v;
184 
185 	/* printf("Prerendering glyph..\n"); */
186 
187 	for (i=0;g[i] != 99;i++) {
188 		if (g[i] == 21) {
189 			/* printf("LINE_BREAK\n"); */
190 			x = LINE_BREAK;
191 			y = LINE_BREAK;
192 		} else {
193 			/* x = ((g[i] % 3) * xscale);*/
194 			/* y = ((g[i]/3)-4) * yscale ;     // truncating division.*/
195 			x = decode_glyph[g[i]].x * xscale;
196 			y = decode_glyph[g[i]].y * yscale;
197 			/* printf("x=%d, y=%d\n", x,y); */
198 		}
199 		scratch[npoints].x = x;
200 		scratch[npoints].y = y;
201 		npoints++;
202 	}
203 
204 	v = (struct my_vect_obj *) malloc(sizeof(struct my_vect_obj));
205 	v->npoints = npoints;
206 	if (npoints != 0) {
207 		v->p = (struct my_point_t *) malloc(sizeof(struct my_point_t) * npoints);
208 		memcpy(v->p, scratch, sizeof(struct my_point_t) * npoints);
209 	} else
210 		v->p = NULL;
211 	return v;
212 }
213 
214 /* This makes a font, by prerendering all the known glyphs into
215  * prescaled sets of line segments that the drawing routines know
216  * how to draw.
217  */
wwvi_make_font(struct my_vect_obj *** font,int xscale,int yscale)218 int wwvi_make_font(struct my_vect_obj ***font, int xscale, int yscale)
219 {
220 	struct my_vect_obj **v;
221 
222 	v = malloc(sizeof(*v) * 256);
223 	if (!v) {
224 		if (v) free(v);
225 		return -1;
226 	}
227 	memset(v, 0, sizeof(*v) * 256);
228 	v['A'] = prerender_glyph(glyph_A, xscale, yscale);
229 	v['B'] = prerender_glyph(glyph_B, xscale, yscale);
230 	v['C'] = prerender_glyph(glyph_C, xscale, yscale);
231 	v['D'] = prerender_glyph(glyph_D, xscale, yscale);
232 	v['E'] = prerender_glyph(glyph_E, xscale, yscale);
233 	v['F'] = prerender_glyph(glyph_F, xscale, yscale);
234 	v['G'] = prerender_glyph(glyph_G, xscale, yscale);
235 	v['H'] = prerender_glyph(glyph_H, xscale, yscale);
236 	v['I'] = prerender_glyph(glyph_I, xscale, yscale);
237 	v['J'] = prerender_glyph(glyph_J, xscale, yscale);
238 	v['K'] = prerender_glyph(glyph_K, xscale, yscale);
239 	v['L'] = prerender_glyph(glyph_L, xscale, yscale);
240 	v['M'] = prerender_glyph(glyph_M, xscale, yscale);
241 	v['N'] = prerender_glyph(glyph_N, xscale, yscale);
242 	v['O'] = prerender_glyph(glyph_O, xscale, yscale);
243 	v['P'] = prerender_glyph(glyph_P, xscale, yscale);
244 	v['Q'] = prerender_glyph(glyph_Q, xscale, yscale);
245 	v['R'] = prerender_glyph(glyph_R, xscale, yscale);
246 	v['S'] = prerender_glyph(glyph_S, xscale, yscale);
247 	v['T'] = prerender_glyph(glyph_T, xscale, yscale);
248 	v['U'] = prerender_glyph(glyph_U, xscale, yscale);
249 	v['V'] = prerender_glyph(glyph_V, xscale, yscale);
250 	v['W'] = prerender_glyph(glyph_W, xscale, yscale);
251 	v['X'] = prerender_glyph(glyph_X, xscale, yscale);
252 	v['Y'] = prerender_glyph(glyph_Y, xscale, yscale);
253 	v['Z'] = prerender_glyph(glyph_Z, xscale, yscale);
254 	v['!'] = prerender_glyph(glyph_bang, xscale, yscale);
255 	v['/'] = prerender_glyph(glyph_slash, xscale, yscale);
256 	v['\\'] = prerender_glyph(glyph_backslash, xscale, yscale);
257 	v['|'] = prerender_glyph(glyph_pipe, xscale, yscale);
258 	v['?'] = prerender_glyph(glyph_que, xscale, yscale);
259 	v[':'] = prerender_glyph(glyph_colon, xscale, yscale);
260 	v['('] = prerender_glyph(glyph_leftparen, xscale, yscale);
261 	v[')'] = prerender_glyph(glyph_rightparen, xscale, yscale);
262 	v['a'] = prerender_glyph(glyph_a, xscale, yscale);
263 	v[' '] = prerender_glyph(glyph_space, xscale, yscale);
264 	v['b'] = prerender_glyph(glyph_b, xscale, yscale);
265 	v['c'] = prerender_glyph(glyph_c, xscale, yscale);
266 	v['d'] = prerender_glyph(glyph_d, xscale, yscale);
267 	v['e'] = prerender_glyph(glyph_e, xscale, yscale);
268 	v['f'] = prerender_glyph(glyph_f, xscale, yscale);
269 	v['g'] = prerender_glyph(glyph_g, xscale, yscale);
270 	v['h'] = prerender_glyph(glyph_h, xscale, yscale);
271 	v['i'] = prerender_glyph(glyph_i, xscale, yscale);
272 	v['j'] = prerender_glyph(glyph_j, xscale, yscale);
273 	v['k'] = prerender_glyph(glyph_k, xscale, yscale);
274 	v['l'] = prerender_glyph(glyph_l, xscale, yscale);
275 	v['m'] = prerender_glyph(glyph_m, xscale, yscale);
276 	v['n'] = prerender_glyph(glyph_n, xscale, yscale);
277 	v['o'] = prerender_glyph(glyph_o, xscale, yscale);
278 	v['p'] = prerender_glyph(glyph_p, xscale, yscale);
279 	v['q'] = prerender_glyph(glyph_q, xscale, yscale);
280 	v['r'] = prerender_glyph(glyph_r, xscale, yscale);
281 	v['s'] = prerender_glyph(glyph_s, xscale, yscale);
282 	v['t'] = prerender_glyph(glyph_t, xscale, yscale);
283 	v['u'] = prerender_glyph(glyph_u, xscale, yscale);
284 	v['v'] = prerender_glyph(glyph_v, xscale, yscale);
285 	v['w'] = prerender_glyph(glyph_w, xscale, yscale);
286 	v['x'] = prerender_glyph(glyph_x, xscale, yscale);
287 	v['y'] = prerender_glyph(glyph_y, xscale, yscale);
288 	v['z'] = prerender_glyph(glyph_z, xscale, yscale);
289 	v['0'] = prerender_glyph(glyph_0, xscale, yscale);
290 	v['1'] = prerender_glyph(glyph_1, xscale, yscale);
291 	v['2'] = prerender_glyph(glyph_2, xscale, yscale);
292 	v['3'] = prerender_glyph(glyph_3, xscale, yscale);
293 	v['4'] = prerender_glyph(glyph_4, xscale, yscale);
294 	v['5'] = prerender_glyph(glyph_5, xscale, yscale);
295 	v['6'] = prerender_glyph(glyph_6, xscale, yscale);
296 	v['7'] = prerender_glyph(glyph_7, xscale, yscale);
297 	v['8'] = prerender_glyph(glyph_8, xscale, yscale);
298 	v['9'] = prerender_glyph(glyph_9, xscale, yscale);
299 	v['-'] = prerender_glyph(glyph_dash, xscale, yscale);
300 	v['+'] = prerender_glyph(glyph_plus, xscale, yscale);
301 	v[','] = prerender_glyph(glyph_comma, xscale, yscale);
302 	v['.'] = prerender_glyph(glyph_period, xscale, yscale);
303 	v['\''] = prerender_glyph(glyph_apostrophe, xscale, yscale);
304 	v['\"'] = prerender_glyph(glyph_doublequote, xscale, yscale);
305 	v['*'] = prerender_glyph(glyph_asterisk, xscale, yscale);
306 	v['_'] = prerender_glyph(glyph_underscore, xscale, yscale);
307 	*font = v;
308 	return 0;
309 }
310 
311 
312