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.
36 int hatch_when
; /* 0=never 1=only-in-b&w 2=always */
42 const drawing_api
*api
;
44 struct print_colour
*colours
;
45 int ncolours
, coloursize
;
47 /* `me' is only used in status_bar(), so print-oriented instances of
48 * this may set it to NULL. */
53 drawing
*drawing_new(const drawing_api
*api
, midend
*me
, void *handle
)
55 drawing
*dr
= snew(drawing
);
59 dr
->ncolours
= dr
->coloursize
= 0;
62 dr
->laststatus
= NULL
;
66 void drawing_free(drawing
*dr
)
68 sfree(dr
->laststatus
);
73 void draw_text(drawing
*dr
, int x
, int y
, int fonttype
, int fontsize
,
74 int align
, int colour
, char *text
)
76 dr
->api
->draw_text(dr
->handle
, x
, y
, fonttype
, fontsize
, align
,
80 void draw_rect(drawing
*dr
, int x
, int y
, int w
, int h
, int colour
)
82 dr
->api
->draw_rect(dr
->handle
, x
, y
, w
, h
, colour
);
85 void draw_line(drawing
*dr
, int x1
, int y1
, int x2
, int y2
, int colour
)
87 dr
->api
->draw_line(dr
->handle
, x1
, y1
, x2
, y2
, colour
);
90 void draw_thick_line(drawing
*dr
, float thickness
,
91 float x1
, float y1
, float x2
, float y2
, int colour
)
93 if (dr
->api
->draw_thick_line
) {
94 dr
->api
->draw_thick_line(dr
->handle
, thickness
,
95 x1
, y1
, x2
, y2
, colour
);
97 /* We'll fake it up with a filled polygon. The tweak to the
98 * thickness empirically compensates for rounding errors, because
99 * polygon rendering uses integer coordinates.
101 float len
= sqrt((x2
- x1
)*(x2
- x1
) + (y2
- y1
)*(y2
- y1
));
102 float tvhatx
= (x2
- x1
)/len
* (thickness
/2 - 0.2);
103 float tvhaty
= (y2
- y1
)/len
* (thickness
/2 - 0.2);
114 dr
->api
->draw_polygon(dr
->handle
, p
, 4, colour
, colour
);
118 void draw_polygon(drawing
*dr
, int *coords
, int npoints
,
119 int fillcolour
, int outlinecolour
)
121 dr
->api
->draw_polygon(dr
->handle
, coords
, npoints
, fillcolour
,
125 void draw_circle(drawing
*dr
, int cx
, int cy
, int radius
,
126 int fillcolour
, int outlinecolour
)
128 dr
->api
->draw_circle(dr
->handle
, cx
, cy
, radius
, fillcolour
,
132 void draw_update(drawing
*dr
, int x
, int y
, int w
, int h
)
134 if (dr
->api
->draw_update
)
135 dr
->api
->draw_update(dr
->handle
, x
, y
, w
, h
);
138 void clip(drawing
*dr
, int x
, int y
, int w
, int h
)
140 dr
->api
->clip(dr
->handle
, x
, y
, w
, h
);
143 void unclip(drawing
*dr
)
145 dr
->api
->unclip(dr
->handle
);
148 void start_draw(drawing
*dr
)
150 dr
->api
->start_draw(dr
->handle
);
153 void end_draw(drawing
*dr
)
155 dr
->api
->end_draw(dr
->handle
);
158 char *text_fallback(drawing
*dr
, const char *const *strings
, int nstrings
)
163 * If the drawing implementation provides one of these, use it.
165 if (dr
&& dr
->api
->text_fallback
)
166 return dr
->api
->text_fallback(dr
->handle
, strings
, nstrings
);
169 * Otherwise, do the simple thing and just pick the first string
170 * that fits in plain ASCII. It will then need no translation
173 for (i
= 0; i
< nstrings
; i
++) {
176 for (p
= strings
[i
]; *p
; p
++)
180 return dupstr(strings
[i
]);
184 * The caller was responsible for making sure _some_ string in
185 * the list was in plain ASCII.
187 assert(!"Should never get here");
188 return NULL
; /* placate optimiser */
191 void status_bar(drawing
*dr
, char *text
)
195 if (!dr
->api
->status_bar
)
200 rewritten
= midend_rewrite_statusbar(dr
->me
, text
);
201 if (!dr
->laststatus
|| strcmp(rewritten
, dr
->laststatus
)) {
202 dr
->api
->status_bar(dr
->handle
, rewritten
);
203 sfree(dr
->laststatus
);
204 dr
->laststatus
= rewritten
;
210 blitter
*blitter_new(drawing
*dr
, int w
, int h
)
212 return dr
->api
->blitter_new(dr
->handle
, w
, h
);
215 void blitter_free(drawing
*dr
, blitter
*bl
)
217 dr
->api
->blitter_free(dr
->handle
, bl
);
220 void blitter_save(drawing
*dr
, blitter
*bl
, int x
, int y
)
222 dr
->api
->blitter_save(dr
->handle
, bl
, x
, y
);
225 void blitter_load(drawing
*dr
, blitter
*bl
, int x
, int y
)
227 dr
->api
->blitter_load(dr
->handle
, bl
, x
, y
);
230 void print_begin_doc(drawing
*dr
, int pages
)
232 dr
->api
->begin_doc(dr
->handle
, pages
);
235 void print_begin_page(drawing
*dr
, int number
)
237 dr
->api
->begin_page(dr
->handle
, number
);
240 void print_begin_puzzle(drawing
*dr
, float xm
, float xc
,
241 float ym
, float yc
, int pw
, int ph
, float wmm
,
246 dr
->api
->begin_puzzle(dr
->handle
, xm
, xc
, ym
, yc
, pw
, ph
, wmm
);
249 void print_end_puzzle(drawing
*dr
)
251 dr
->api
->end_puzzle(dr
->handle
);
255 void print_end_page(drawing
*dr
, int number
)
257 dr
->api
->end_page(dr
->handle
, number
);
260 void print_end_doc(drawing
*dr
)
262 dr
->api
->end_doc(dr
->handle
);
265 void print_get_colour(drawing
*dr
, int colour
, int printing_in_colour
,
266 int *hatch
, float *r
, float *g
, float *b
)
268 assert(colour
>= 0 && colour
< dr
->ncolours
);
269 if (dr
->colours
[colour
].hatch_when
== 2 ||
270 (dr
->colours
[colour
].hatch_when
== 1 && !printing_in_colour
)) {
271 *hatch
= dr
->colours
[colour
].hatch
;
274 if (printing_in_colour
) {
275 *r
= dr
->colours
[colour
].r
;
276 *g
= dr
->colours
[colour
].g
;
277 *b
= dr
->colours
[colour
].b
;
279 *r
= *g
= *b
= dr
->colours
[colour
].grey
;
284 static int print_generic_colour(drawing
*dr
, float r
, float g
, float b
,
285 float grey
, int hatch
, int hatch_when
)
287 if (dr
->ncolours
>= dr
->coloursize
) {
288 dr
->coloursize
= dr
->ncolours
+ 16;
289 dr
->colours
= sresize(dr
->colours
, dr
->coloursize
,
290 struct print_colour
);
292 dr
->colours
[dr
->ncolours
].hatch
= hatch
;
293 dr
->colours
[dr
->ncolours
].hatch_when
= hatch_when
;
294 dr
->colours
[dr
->ncolours
].r
= r
;
295 dr
->colours
[dr
->ncolours
].g
= g
;
296 dr
->colours
[dr
->ncolours
].b
= b
;
297 dr
->colours
[dr
->ncolours
].grey
= grey
;
298 return dr
->ncolours
++;
301 int print_mono_colour(drawing
*dr
, int grey
)
303 return print_generic_colour(dr
, grey
, grey
, grey
, grey
, -1, 0);
306 int print_grey_colour(drawing
*dr
, float grey
)
308 return print_generic_colour(dr
, grey
, grey
, grey
, grey
, -1, 0);
311 int print_hatched_colour(drawing
*dr
, int hatch
)
313 return print_generic_colour(dr
, 0, 0, 0, 0, hatch
, 2);
316 int print_rgb_mono_colour(drawing
*dr
, float r
, float g
, float b
, int grey
)
318 return print_generic_colour(dr
, r
, g
, b
, grey
, -1, 0);
321 int print_rgb_grey_colour(drawing
*dr
, float r
, float g
, float b
, float grey
)
323 return print_generic_colour(dr
, r
, g
, b
, grey
, -1, 0);
326 int print_rgb_hatched_colour(drawing
*dr
, float r
, float g
, float b
, int hatch
)
328 return print_generic_colour(dr
, r
, g
, b
, 0, hatch
, 1);
331 void print_line_width(drawing
*dr
, int width
)
334 * I don't think it's entirely sensible to have line widths be
335 * entirely relative to the puzzle size; there is a point
336 * beyond which lines are just _stupidly_ thick. On the other
337 * hand, absolute line widths aren't particularly nice either
338 * because they start to feel a bit feeble at really large
341 * My experimental answer is to scale line widths as the
342 * _square root_ of the main puzzle scale. Double the puzzle
343 * size, and the line width multiplies by 1.4.
345 dr
->api
->line_width(dr
->handle
, (float)sqrt(dr
->scale
) * width
);
348 void print_line_dotted(drawing
*dr
, int dotted
)
350 dr
->api
->line_dotted(dr
->handle
, dotted
);