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.
10 * FIXME: could we also sort out rewrite_statusbar in here? Also
11 * I'd _like_ to do automatic draw_updates, but it's a pain for
12 * draw_text in particular - I could invent a front end API which
13 * retrieved the text bounds and then do the alignment myself as
14 * well, except that that doesn't work for PS. As usual.
30 const drawing_api *api;
32 struct print_colour *colours;
33 int ncolours, coloursize;
37 drawing *drawing_init(const drawing_api *api, void *handle)
39 drawing *dr = snew(drawing);
43 dr->ncolours = dr->coloursize = 0;
48 void drawing_free(drawing *dr)
54 void draw_text(drawing *dr, int x, int y, int fonttype, int fontsize,
55 int align, int colour, char *text)
57 dr->api->draw_text(dr->handle, x, y, fonttype, fontsize, align,
61 void draw_rect(drawing *dr, int x, int y, int w, int h, int colour)
63 dr->api->draw_rect(dr->handle, x, y, w, h, colour);
66 void draw_line(drawing *dr, int x1, int y1, int x2, int y2, int colour)
68 dr->api->draw_line(dr->handle, x1, y1, x2, y2, colour);
71 void draw_polygon(drawing *dr, int *coords, int npoints,
72 int fillcolour, int outlinecolour)
74 dr->api->draw_polygon(dr->handle, coords, npoints, fillcolour,
78 void draw_circle(drawing *dr, int cx, int cy, int radius,
79 int fillcolour, int outlinecolour)
81 dr->api->draw_circle(dr->handle, cx, cy, radius, fillcolour,
85 void draw_update(drawing *dr, int x, int y, int w, int h)
87 if (dr->api->draw_update)
88 dr->api->draw_update(dr->handle, x, y, w, h);
91 void clip(drawing *dr, int x, int y, int w, int h)
93 dr->api->clip(dr->handle, x, y, w, h);
96 void unclip(drawing *dr)
98 dr->api->unclip(dr->handle);
101 void start_draw(drawing *dr)
103 dr->api->start_draw(dr->handle);
106 void end_draw(drawing *dr)
108 dr->api->end_draw(dr->handle);
111 void status_bar(drawing *dr, char *text)
113 if (dr->api->status_bar)
114 dr->api->status_bar(dr->handle, text);
117 blitter *blitter_new(drawing *dr, int w, int h)
119 return dr->api->blitter_new(dr->handle, w, h);
122 void blitter_free(drawing *dr, blitter *bl)
124 dr->api->blitter_free(dr->handle, bl);
127 void blitter_save(drawing *dr, blitter *bl, int x, int y)
129 dr->api->blitter_save(dr->handle, bl, x, y);
132 void blitter_load(drawing *dr, blitter *bl, int x, int y)
134 dr->api->blitter_load(dr->handle, bl, x, y);
137 void print_begin_doc(drawing *dr, int pages)
139 dr->api->begin_doc(dr->handle, pages);
142 void print_begin_page(drawing *dr, int number)
144 dr->api->begin_page(dr->handle, number);
147 void print_begin_puzzle(drawing *dr, float xm, float xc,
148 float ym, float yc, int pw, int ph, float wmm,
153 dr->api->begin_puzzle(dr->handle, xm, xc, ym, yc, pw, ph, wmm);
156 void print_end_puzzle(drawing *dr)
158 dr->api->end_puzzle(dr->handle);
162 void print_end_page(drawing *dr, int number)
164 dr->api->end_page(dr->handle, number);
167 void print_end_doc(drawing *dr)
169 dr->api->end_doc(dr->handle);
172 void print_get_colour(drawing *dr, int colour, int *hatch,
173 float *r, float *g, float *b)
175 assert(colour >= 0 && colour < dr->ncolours);
176 *hatch = dr->colours[colour].hatch;
177 *r = dr->colours[colour].r;
178 *g = dr->colours[colour].g;
179 *b = dr->colours[colour].b;
182 int print_rgb_colour(drawing *dr, int hatch, float r, float g, float b)
184 if (dr->ncolours >= dr->coloursize) {
185 dr->coloursize = dr->ncolours + 16;
186 dr->colours = sresize(dr->colours, dr->coloursize,
187 struct print_colour);
189 dr->colours[dr->ncolours].hatch = hatch;
190 dr->colours[dr->ncolours].r = r;
191 dr->colours[dr->ncolours].g = g;
192 dr->colours[dr->ncolours].b = b;
193 return dr->ncolours++;
196 int print_grey_colour(drawing *dr, int hatch, float grey)
198 return print_rgb_colour(dr, hatch, grey, grey, grey);
201 int print_mono_colour(drawing *dr, int grey)
203 return print_rgb_colour(dr, grey ? HATCH_CLEAR : HATCH_SOLID,
207 void print_line_width(drawing *dr, int width)
210 * I don't think it's entirely sensible to have line widths be
211 * entirely relative to the puzzle size; there is a point
212 * beyond which lines are just _stupidly_ thick. On the other
213 * hand, absolute line widths aren't particularly nice either
214 * because they start to feel a bit feeble at really large
217 * My experimental answer is to scale line widths as the
218 * _square root_ of the main puzzle scale. Double the puzzle
219 * size, and the line width multiplies by 1.4.
221 dr->api->line_width(dr->handle, sqrt(dr->scale) * width);