chiark / gitweb /
Cleanup: relieve frontends of the duty to call
[sgt-puzzles.git] / drawing.c
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     float r, g, b;
37 };
38
39 struct drawing {
40     const drawing_api *api;
41     void *handle;
42     struct print_colour *colours;
43     int ncolours, coloursize;
44     float scale;
45     /* `me' is only used in status_bar(), so print-oriented instances of
46      * this may set it to NULL. */
47     midend *me;
48     char *laststatus;
49 };
50
51 drawing *drawing_new(const drawing_api *api, midend *me, void *handle)
52 {
53     drawing *dr = snew(drawing);
54     dr->api = api;
55     dr->handle = handle;
56     dr->colours = NULL;
57     dr->ncolours = dr->coloursize = 0;
58     dr->scale = 1.0F;
59     dr->me = me;
60     dr->laststatus = NULL;
61     return dr;
62 }
63
64 void drawing_free(drawing *dr)
65 {
66     sfree(dr->laststatus);
67     sfree(dr->colours);
68     sfree(dr);
69 }
70
71 void draw_text(drawing *dr, int x, int y, int fonttype, int fontsize,
72                int align, int colour, char *text)
73 {
74     dr->api->draw_text(dr->handle, x, y, fonttype, fontsize, align,
75                        colour, text);
76 }
77
78 void draw_rect(drawing *dr, int x, int y, int w, int h, int colour)
79 {
80     dr->api->draw_rect(dr->handle, x, y, w, h, colour);
81 }
82
83 void draw_line(drawing *dr, int x1, int y1, int x2, int y2, int colour)
84 {
85     dr->api->draw_line(dr->handle, x1, y1, x2, y2, colour);
86 }
87
88 void draw_polygon(drawing *dr, int *coords, int npoints,
89                   int fillcolour, int outlinecolour)
90 {
91     dr->api->draw_polygon(dr->handle, coords, npoints, fillcolour,
92                           outlinecolour);
93 }
94
95 void draw_circle(drawing *dr, int cx, int cy, int radius,
96                  int fillcolour, int outlinecolour)
97 {
98     dr->api->draw_circle(dr->handle, cx, cy, radius, fillcolour,
99                          outlinecolour);
100 }
101
102 void draw_update(drawing *dr, int x, int y, int w, int h)
103 {
104     if (dr->api->draw_update)
105         dr->api->draw_update(dr->handle, x, y, w, h);
106 }
107
108 void clip(drawing *dr, int x, int y, int w, int h)
109 {
110     dr->api->clip(dr->handle, x, y, w, h);
111 }
112
113 void unclip(drawing *dr)
114 {
115     dr->api->unclip(dr->handle);
116 }
117
118 void start_draw(drawing *dr)
119 {
120     dr->api->start_draw(dr->handle);
121 }
122
123 void end_draw(drawing *dr)
124 {
125     dr->api->end_draw(dr->handle);
126 }
127
128 void status_bar(drawing *dr, char *text)
129 {
130     char *rewritten;
131
132     if (!dr->api->status_bar)
133         return;
134
135     assert(dr->me);
136
137     rewritten = midend_rewrite_statusbar(dr->me, text);
138     if (!dr->laststatus || strcmp(rewritten, dr->laststatus)) {
139         dr->api->status_bar(dr->handle, rewritten);
140         sfree(dr->laststatus);
141         dr->laststatus = rewritten;
142     } else {
143         sfree(rewritten);
144     }
145 }
146
147 blitter *blitter_new(drawing *dr, int w, int h)
148 {
149     return dr->api->blitter_new(dr->handle, w, h);
150 }
151
152 void blitter_free(drawing *dr, blitter *bl)
153 {
154     dr->api->blitter_free(dr->handle, bl);
155 }
156
157 void blitter_save(drawing *dr, blitter *bl, int x, int y)
158 {
159     dr->api->blitter_save(dr->handle, bl, x, y);
160 }
161
162 void blitter_load(drawing *dr, blitter *bl, int x, int y)
163 {
164     dr->api->blitter_load(dr->handle, bl, x, y);
165 }
166
167 void print_begin_doc(drawing *dr, int pages)
168 {
169     dr->api->begin_doc(dr->handle, pages);
170 }
171
172 void print_begin_page(drawing *dr, int number)
173 {
174     dr->api->begin_page(dr->handle, number);
175 }
176
177 void print_begin_puzzle(drawing *dr, float xm, float xc,
178                         float ym, float yc, int pw, int ph, float wmm,
179                         float scale)
180 {
181     dr->scale = scale;
182     dr->ncolours = 0;
183     dr->api->begin_puzzle(dr->handle, xm, xc, ym, yc, pw, ph, wmm);
184 }
185
186 void print_end_puzzle(drawing *dr)
187 {
188     dr->api->end_puzzle(dr->handle);
189     dr->scale = 1.0F;
190 }
191
192 void print_end_page(drawing *dr, int number)
193 {
194     dr->api->end_page(dr->handle, number);
195 }
196
197 void print_end_doc(drawing *dr)
198 {
199     dr->api->end_doc(dr->handle);
200 }
201
202 void print_get_colour(drawing *dr, int colour, int *hatch,
203                       float *r, float *g, float *b)
204 {
205     assert(colour >= 0 && colour < dr->ncolours);
206     *hatch = dr->colours[colour].hatch;
207     *r = dr->colours[colour].r;
208     *g = dr->colours[colour].g;
209     *b = dr->colours[colour].b;
210 }
211
212 int print_rgb_colour(drawing *dr, int hatch, float r, float g, float b)
213 {
214     if (dr->ncolours >= dr->coloursize) {
215         dr->coloursize = dr->ncolours + 16;
216         dr->colours = sresize(dr->colours, dr->coloursize,
217                               struct print_colour);
218     }
219     dr->colours[dr->ncolours].hatch = hatch;
220     dr->colours[dr->ncolours].r = r;
221     dr->colours[dr->ncolours].g = g;
222     dr->colours[dr->ncolours].b = b;
223     return dr->ncolours++;
224 }
225
226 int print_grey_colour(drawing *dr, int hatch, float grey)
227 {
228     return print_rgb_colour(dr, hatch, grey, grey, grey);
229 }
230
231 int print_mono_colour(drawing *dr, int grey)
232 {
233     return print_rgb_colour(dr, grey ? HATCH_CLEAR : HATCH_SOLID,
234                             grey, grey, grey);
235 }
236
237 void print_line_width(drawing *dr, int width)
238 {
239     /*
240      * I don't think it's entirely sensible to have line widths be
241      * entirely relative to the puzzle size; there is a point
242      * beyond which lines are just _stupidly_ thick. On the other
243      * hand, absolute line widths aren't particularly nice either
244      * because they start to feel a bit feeble at really large
245      * scales.
246      * 
247      * My experimental answer is to scale line widths as the
248      * _square root_ of the main puzzle scale. Double the puzzle
249      * size, and the line width multiplies by 1.4.
250      */
251     dr->api->line_width(dr->handle, sqrt(dr->scale) * width);
252 }