1 /*
2  * drawing.c: Intermediary between the drawing interface as
3  * presented to the back end, and that implemented by the front
4  * end.
5  *
6  * Mostly just looks up calls in a vtable and passes them through
7  * unchanged. However, on the printing side it tracks print colours
8  * so the front end API doesn't have to.
9  *
10  * FIXME:
11  *
12  *  - I'd _like_ to do automatic draw_updates, but it's a pain for
13  *    draw_text in particular. I'd have to invent a front end API
14  *    which retrieved the text bounds.
15  *     + that might allow me to do the alignment centrally as well?
16  * 	  * perhaps not, because PS can't return this information,
17  * 	    so there would have to be a special case for it.
18  *     + however, that at least doesn't stand in the way of using
19  * 	 the text bounds for draw_update, because PS doesn't need
20  * 	 draw_update since it's printing-only. Any _interactive_
21  * 	 drawing API couldn't get away with refusing to tell you
22  * 	 what parts of the screen a text draw had covered, because
23  * 	 you would inevitably need to erase it later on.
24  */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <math.h>
31 
32 #include "puzzles.h"
33 
34 struct print_colour {
35     int hatch;
36     int hatch_when;		       /* 0=never 1=only-in-b&w 2=always */
37     float r, g, b;
38     float grey;
39 };
40 
41 struct drawing {
42     const drawing_api *api;
43     void *handle;
44     struct print_colour *colours;
45     int ncolours, coloursize;
46     float scale;
47     /* `me' is only used in status_bar(), so print-oriented instances of
48      * this may set it to NULL. */
49     midend *me;
50     char *laststatus;
51 };
52 
drawing_new(const drawing_api * api,midend * me,void * handle)53 drawing *drawing_new(const drawing_api *api, midend *me, void *handle)
54 {
55     drawing *dr = snew(drawing);
56     dr->api = api;
57     dr->handle = handle;
58     dr->colours = NULL;
59     dr->ncolours = dr->coloursize = 0;
60     dr->scale = 1.0F;
61     dr->me = me;
62     dr->laststatus = NULL;
63     return dr;
64 }
65 
drawing_free(drawing * dr)66 void drawing_free(drawing *dr)
67 {
68     sfree(dr->laststatus);
69     sfree(dr->colours);
70     sfree(dr);
71 }
72 
draw_text(drawing * dr,int x,int y,int fonttype,int fontsize,int align,int colour,const char * text)73 void draw_text(drawing *dr, int x, int y, int fonttype, int fontsize,
74                int align, int colour, const char *text)
75 {
76     dr->api->draw_text(dr->handle, x, y, fonttype, fontsize, align,
77 		       colour, text);
78 }
79 
draw_rect(drawing * dr,int x,int y,int w,int h,int colour)80 void draw_rect(drawing *dr, int x, int y, int w, int h, int colour)
81 {
82     dr->api->draw_rect(dr->handle, x, y, w, h, colour);
83 }
84 
draw_line(drawing * dr,int x1,int y1,int x2,int y2,int colour)85 void draw_line(drawing *dr, int x1, int y1, int x2, int y2, int colour)
86 {
87     dr->api->draw_line(dr->handle, x1, y1, x2, y2, colour);
88 }
89 
draw_thick_line(drawing * dr,float thickness,float x1,float y1,float x2,float y2,int colour)90 void draw_thick_line(drawing *dr, float thickness,
91 		     float x1, float y1, float x2, float y2, int colour)
92 {
93     if (thickness < 1.0)
94         thickness = 1.0;
95     if (dr->api->draw_thick_line) {
96 	dr->api->draw_thick_line(dr->handle, thickness,
97 				 x1, y1, x2, y2, colour);
98     } else {
99 	/* We'll fake it up with a filled polygon.  The tweak to the
100 	 * thickness empirically compensates for rounding errors, because
101 	 * polygon rendering uses integer coordinates.
102 	 */
103 	float len = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
104 	float tvhatx = (x2 - x1)/len * (thickness/2 - 0.2);
105 	float tvhaty = (y2 - y1)/len * (thickness/2 - 0.2);
106 	int p[8];
107 
108 	p[0] = x1 - tvhaty;
109 	p[1] = y1 + tvhatx;
110 	p[2] = x2 - tvhaty;
111 	p[3] = y2 + tvhatx;
112 	p[4] = x2 + tvhaty;
113 	p[5] = y2 - tvhatx;
114 	p[6] = x1 + tvhaty;
115 	p[7] = y1 - tvhatx;
116 	dr->api->draw_polygon(dr->handle, p, 4, colour, colour);
117     }
118 }
119 
draw_polygon(drawing * dr,const int * coords,int npoints,int fillcolour,int outlinecolour)120 void draw_polygon(drawing *dr, const int *coords, int npoints,
121                   int fillcolour, int outlinecolour)
122 {
123     dr->api->draw_polygon(dr->handle, coords, npoints, fillcolour,
124 			  outlinecolour);
125 }
126 
draw_circle(drawing * dr,int cx,int cy,int radius,int fillcolour,int outlinecolour)127 void draw_circle(drawing *dr, int cx, int cy, int radius,
128                  int fillcolour, int outlinecolour)
129 {
130     dr->api->draw_circle(dr->handle, cx, cy, radius, fillcolour,
131 			 outlinecolour);
132 }
133 
draw_update(drawing * dr,int x,int y,int w,int h)134 void draw_update(drawing *dr, int x, int y, int w, int h)
135 {
136     if (dr->api->draw_update)
137 	dr->api->draw_update(dr->handle, x, y, w, h);
138 }
139 
clip(drawing * dr,int x,int y,int w,int h)140 void clip(drawing *dr, int x, int y, int w, int h)
141 {
142     dr->api->clip(dr->handle, x, y, w, h);
143 }
144 
unclip(drawing * dr)145 void unclip(drawing *dr)
146 {
147     dr->api->unclip(dr->handle);
148 }
149 
start_draw(drawing * dr)150 void start_draw(drawing *dr)
151 {
152     dr->api->start_draw(dr->handle);
153 }
154 
end_draw(drawing * dr)155 void end_draw(drawing *dr)
156 {
157     dr->api->end_draw(dr->handle);
158 }
159 
text_fallback(drawing * dr,const char * const * strings,int nstrings)160 char *text_fallback(drawing *dr, const char *const *strings, int nstrings)
161 {
162     int i;
163 
164     /*
165      * If the drawing implementation provides one of these, use it.
166      */
167     if (dr && dr->api->text_fallback)
168 	return dr->api->text_fallback(dr->handle, strings, nstrings);
169 
170     /*
171      * Otherwise, do the simple thing and just pick the first string
172      * that fits in plain ASCII. It will then need no translation
173      * out of UTF-8.
174      */
175     for (i = 0; i < nstrings; i++) {
176 	const char *p;
177 
178 	for (p = strings[i]; *p; p++)
179 	    if (*p & 0x80)
180 		break;
181 	if (!*p)
182 	    return dupstr(strings[i]);
183     }
184 
185     /*
186      * The caller was responsible for making sure _some_ string in
187      * the list was in plain ASCII.
188      */
189     assert(!"Should never get here");
190     return NULL;		       /* placate optimiser */
191 }
192 
status_bar(drawing * dr,const char * text)193 void status_bar(drawing *dr, const char *text)
194 {
195     char *rewritten;
196 
197     if (!dr->api->status_bar)
198 	return;
199 
200     assert(dr->me);
201 
202     rewritten = midend_rewrite_statusbar(dr->me, text);
203     if (!dr->laststatus || strcmp(rewritten, dr->laststatus)) {
204 	dr->api->status_bar(dr->handle, rewritten);
205 	sfree(dr->laststatus);
206 	dr->laststatus = rewritten;
207     } else {
208 	sfree(rewritten);
209     }
210 }
211 
blitter_new(drawing * dr,int w,int h)212 blitter *blitter_new(drawing *dr, int w, int h)
213 {
214     return dr->api->blitter_new(dr->handle, w, h);
215 }
216 
blitter_free(drawing * dr,blitter * bl)217 void blitter_free(drawing *dr, blitter *bl)
218 {
219     dr->api->blitter_free(dr->handle, bl);
220 }
221 
blitter_save(drawing * dr,blitter * bl,int x,int y)222 void blitter_save(drawing *dr, blitter *bl, int x, int y)
223 {
224     dr->api->blitter_save(dr->handle, bl, x, y);
225 }
226 
blitter_load(drawing * dr,blitter * bl,int x,int y)227 void blitter_load(drawing *dr, blitter *bl, int x, int y)
228 {
229     dr->api->blitter_load(dr->handle, bl, x, y);
230 }
231 
print_begin_doc(drawing * dr,int pages)232 void print_begin_doc(drawing *dr, int pages)
233 {
234     dr->api->begin_doc(dr->handle, pages);
235 }
236 
print_begin_page(drawing * dr,int number)237 void print_begin_page(drawing *dr, int number)
238 {
239     dr->api->begin_page(dr->handle, number);
240 }
241 
print_begin_puzzle(drawing * dr,float xm,float xc,float ym,float yc,int pw,int ph,float wmm,float scale)242 void print_begin_puzzle(drawing *dr, float xm, float xc,
243 			float ym, float yc, int pw, int ph, float wmm,
244 			float scale)
245 {
246     dr->scale = scale;
247     dr->ncolours = 0;
248     dr->api->begin_puzzle(dr->handle, xm, xc, ym, yc, pw, ph, wmm);
249 }
250 
print_end_puzzle(drawing * dr)251 void print_end_puzzle(drawing *dr)
252 {
253     dr->api->end_puzzle(dr->handle);
254     dr->scale = 1.0F;
255 }
256 
print_end_page(drawing * dr,int number)257 void print_end_page(drawing *dr, int number)
258 {
259     dr->api->end_page(dr->handle, number);
260 }
261 
print_end_doc(drawing * dr)262 void print_end_doc(drawing *dr)
263 {
264     dr->api->end_doc(dr->handle);
265 }
266 
print_get_colour(drawing * dr,int colour,bool printing_in_colour,int * hatch,float * r,float * g,float * b)267 void print_get_colour(drawing *dr, int colour, bool printing_in_colour,
268 		      int *hatch, float *r, float *g, float *b)
269 {
270     assert(colour >= 0 && colour < dr->ncolours);
271     if (dr->colours[colour].hatch_when == 2 ||
272 	(dr->colours[colour].hatch_when == 1 && !printing_in_colour)) {
273 	*hatch = dr->colours[colour].hatch;
274     } else {
275 	*hatch = -1;
276 	if (printing_in_colour) {
277 	    *r = dr->colours[colour].r;
278 	    *g = dr->colours[colour].g;
279 	    *b = dr->colours[colour].b;
280 	} else {
281 	    *r = *g = *b = dr->colours[colour].grey;
282 	}
283     }
284 }
285 
print_generic_colour(drawing * dr,float r,float g,float b,float grey,int hatch,int hatch_when)286 static int print_generic_colour(drawing *dr, float r, float g, float b,
287 				float grey, int hatch, int hatch_when)
288 {
289     if (dr->ncolours >= dr->coloursize) {
290 	dr->coloursize = dr->ncolours + 16;
291 	dr->colours = sresize(dr->colours, dr->coloursize,
292 			      struct print_colour);
293     }
294     dr->colours[dr->ncolours].hatch = hatch;
295     dr->colours[dr->ncolours].hatch_when = hatch_when;
296     dr->colours[dr->ncolours].r = r;
297     dr->colours[dr->ncolours].g = g;
298     dr->colours[dr->ncolours].b = b;
299     dr->colours[dr->ncolours].grey = grey;
300     return dr->ncolours++;
301 }
302 
print_mono_colour(drawing * dr,int grey)303 int print_mono_colour(drawing *dr, int grey)
304 {
305     return print_generic_colour(dr, grey, grey, grey, grey, -1, 0);
306 }
307 
print_grey_colour(drawing * dr,float grey)308 int print_grey_colour(drawing *dr, float grey)
309 {
310     return print_generic_colour(dr, grey, grey, grey, grey, -1, 0);
311 }
312 
print_hatched_colour(drawing * dr,int hatch)313 int print_hatched_colour(drawing *dr, int hatch)
314 {
315     return print_generic_colour(dr, 0, 0, 0, 0, hatch, 2);
316 }
317 
print_rgb_mono_colour(drawing * dr,float r,float g,float b,int grey)318 int print_rgb_mono_colour(drawing *dr, float r, float g, float b, int grey)
319 {
320     return print_generic_colour(dr, r, g, b, grey, -1, 0);
321 }
322 
print_rgb_grey_colour(drawing * dr,float r,float g,float b,float grey)323 int print_rgb_grey_colour(drawing *dr, float r, float g, float b, float grey)
324 {
325     return print_generic_colour(dr, r, g, b, grey, -1, 0);
326 }
327 
print_rgb_hatched_colour(drawing * dr,float r,float g,float b,int hatch)328 int print_rgb_hatched_colour(drawing *dr, float r, float g, float b, int hatch)
329 {
330     return print_generic_colour(dr, r, g, b, 0, hatch, 1);
331 }
332 
print_line_width(drawing * dr,int width)333 void print_line_width(drawing *dr, int width)
334 {
335     /*
336      * I don't think it's entirely sensible to have line widths be
337      * entirely relative to the puzzle size; there is a point
338      * beyond which lines are just _stupidly_ thick. On the other
339      * hand, absolute line widths aren't particularly nice either
340      * because they start to feel a bit feeble at really large
341      * scales.
342      *
343      * My experimental answer is to scale line widths as the
344      * _square root_ of the main puzzle scale. Double the puzzle
345      * size, and the line width multiplies by 1.4.
346      */
347     dr->api->line_width(dr->handle, (float)sqrt(dr->scale) * width);
348 }
349 
print_line_dotted(drawing * dr,bool dotted)350 void print_line_dotted(drawing *dr, bool dotted)
351 {
352     dr->api->line_dotted(dr->handle, dotted);
353 }
354