2 * drawing.c: Intermediary between the drawing interface as
3 * presented to the back end, and that implemented by the front
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.
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.
40 const drawing_api *api;
42 struct print_colour *colours;
43 int ncolours, coloursize;
45 /* `me' is only used in status_bar(), so print-oriented instances of
46 * this may set it to NULL. */
51 drawing *drawing_new(const drawing_api *api, midend *me, void *handle)
53 drawing *dr = snew(drawing);
57 dr->ncolours = dr->coloursize = 0;
60 dr->laststatus = NULL;
64 void drawing_free(drawing *dr)
66 sfree(dr->laststatus);
71 void draw_text(drawing *dr, int x, int y, int fonttype, int fontsize,
72 int align, int colour, char *text)
74 dr->api->draw_text(dr->handle, x, y, fonttype, fontsize, align,
78 void draw_rect(drawing *dr, int x, int y, int w, int h, int colour)
80 dr->api->draw_rect(dr->handle, x, y, w, h, colour);
83 void draw_line(drawing *dr, int x1, int y1, int x2, int y2, int colour)
85 dr->api->draw_line(dr->handle, x1, y1, x2, y2, colour);
88 void draw_polygon(drawing *dr, int *coords, int npoints,
89 int fillcolour, int outlinecolour)
91 dr->api->draw_polygon(dr->handle, coords, npoints, fillcolour,
95 void draw_circle(drawing *dr, int cx, int cy, int radius,
96 int fillcolour, int outlinecolour)
98 dr->api->draw_circle(dr->handle, cx, cy, radius, fillcolour,
102 void draw_update(drawing *dr, int x, int y, int w, int h)
104 if (dr->api->draw_update)
105 dr->api->draw_update(dr->handle, x, y, w, h);
108 void clip(drawing *dr, int x, int y, int w, int h)
110 dr->api->clip(dr->handle, x, y, w, h);
113 void unclip(drawing *dr)
115 dr->api->unclip(dr->handle);
118 void start_draw(drawing *dr)
120 dr->api->start_draw(dr->handle);
123 void end_draw(drawing *dr)
125 dr->api->end_draw(dr->handle);
128 void status_bar(drawing *dr, char *text)
132 if (!dr->api->status_bar)
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;
147 blitter *blitter_new(drawing *dr, int w, int h)
149 return dr->api->blitter_new(dr->handle, w, h);
152 void blitter_free(drawing *dr, blitter *bl)
154 dr->api->blitter_free(dr->handle, bl);
157 void blitter_save(drawing *dr, blitter *bl, int x, int y)
159 dr->api->blitter_save(dr->handle, bl, x, y);
162 void blitter_load(drawing *dr, blitter *bl, int x, int y)
164 dr->api->blitter_load(dr->handle, bl, x, y);
167 void print_begin_doc(drawing *dr, int pages)
169 dr->api->begin_doc(dr->handle, pages);
172 void print_begin_page(drawing *dr, int number)
174 dr->api->begin_page(dr->handle, number);
177 void print_begin_puzzle(drawing *dr, float xm, float xc,
178 float ym, float yc, int pw, int ph, float wmm,
183 dr->api->begin_puzzle(dr->handle, xm, xc, ym, yc, pw, ph, wmm);
186 void print_end_puzzle(drawing *dr)
188 dr->api->end_puzzle(dr->handle);
192 void print_end_page(drawing *dr, int number)
194 dr->api->end_page(dr->handle, number);
197 void print_end_doc(drawing *dr)
199 dr->api->end_doc(dr->handle);
202 void print_get_colour(drawing *dr, int colour, int *hatch,
203 float *r, float *g, float *b)
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;
212 int print_rgb_colour(drawing *dr, int hatch, float r, float g, float b)
214 if (dr->ncolours >= dr->coloursize) {
215 dr->coloursize = dr->ncolours + 16;
216 dr->colours = sresize(dr->colours, dr->coloursize,
217 struct print_colour);
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++;
226 int print_grey_colour(drawing *dr, int hatch, float grey)
228 return print_rgb_colour(dr, hatch, grey, grey, grey);
231 int print_mono_colour(drawing *dr, int grey)
233 return print_rgb_colour(dr, grey ? HATCH_CLEAR : HATCH_SOLID,
237 void print_line_width(drawing *dr, int width)
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
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.
251 dr->api->line_width(dr->handle, sqrt(dr->scale) * width);