drawing.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <assert.h>
  29. #ifdef NO_TGMATH_H
  30. # include <math.h>
  31. #else
  32. # include <tgmath.h>
  33. #endif
  34. #include "puzzles.h"
  35. struct print_colour {
  36. int hatch;
  37. int hatch_when; /* 0=never 1=only-in-b&w 2=always */
  38. float r, g, b;
  39. float grey;
  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 *drawing_new(const drawing_api *api, midend *me, void *handle)
  53. {
  54. drawing *dr = snew(drawing);
  55. dr->api = api;
  56. dr->handle = handle;
  57. dr->colours = NULL;
  58. dr->ncolours = dr->coloursize = 0;
  59. dr->scale = 1.0F;
  60. dr->me = me;
  61. dr->laststatus = NULL;
  62. return dr;
  63. }
  64. void drawing_free(drawing *dr)
  65. {
  66. sfree(dr->laststatus);
  67. sfree(dr->colours);
  68. sfree(dr);
  69. }
  70. void draw_text(drawing *dr, int x, int y, int fonttype, int fontsize,
  71. int align, int colour, const char *text)
  72. {
  73. dr->api->draw_text(dr->handle, x, y, fonttype, fontsize, align,
  74. colour, text);
  75. }
  76. void draw_rect(drawing *dr, int x, int y, int w, int h, int colour)
  77. {
  78. dr->api->draw_rect(dr->handle, x, y, w, h, colour);
  79. }
  80. void draw_line(drawing *dr, int x1, int y1, int x2, int y2, int colour)
  81. {
  82. dr->api->draw_line(dr->handle, x1, y1, x2, y2, colour);
  83. }
  84. void draw_thick_line(drawing *dr, float thickness,
  85. float x1, float y1, float x2, float y2, int colour)
  86. {
  87. if (thickness < 1.0F)
  88. thickness = 1.0F;
  89. if (dr->api->draw_thick_line) {
  90. dr->api->draw_thick_line(dr->handle, thickness,
  91. x1, y1, x2, y2, colour);
  92. } else {
  93. /* We'll fake it up with a filled polygon. The tweak to the
  94. * thickness empirically compensates for rounding errors, because
  95. * polygon rendering uses integer coordinates.
  96. */
  97. float len = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
  98. float tvhatx = (x2 - x1)/len * (thickness/2 - 0.2F);
  99. float tvhaty = (y2 - y1)/len * (thickness/2 - 0.2F);
  100. int p[8];
  101. p[0] = x1 - tvhaty;
  102. p[1] = y1 + tvhatx;
  103. p[2] = x2 - tvhaty;
  104. p[3] = y2 + tvhatx;
  105. p[4] = x2 + tvhaty;
  106. p[5] = y2 - tvhatx;
  107. p[6] = x1 + tvhaty;
  108. p[7] = y1 - tvhatx;
  109. dr->api->draw_polygon(dr->handle, p, 4, colour, colour);
  110. }
  111. }
  112. void draw_polygon(drawing *dr, const int *coords, int npoints,
  113. int fillcolour, int outlinecolour)
  114. {
  115. dr->api->draw_polygon(dr->handle, coords, npoints, fillcolour,
  116. outlinecolour);
  117. }
  118. void draw_circle(drawing *dr, int cx, int cy, int radius,
  119. int fillcolour, int outlinecolour)
  120. {
  121. dr->api->draw_circle(dr->handle, cx, cy, radius, fillcolour,
  122. outlinecolour);
  123. }
  124. void draw_update(drawing *dr, int x, int y, int w, int h)
  125. {
  126. if (dr->api->draw_update)
  127. dr->api->draw_update(dr->handle, x, y, w, h);
  128. }
  129. void clip(drawing *dr, int x, int y, int w, int h)
  130. {
  131. dr->api->clip(dr->handle, x, y, w, h);
  132. }
  133. void unclip(drawing *dr)
  134. {
  135. dr->api->unclip(dr->handle);
  136. }
  137. void start_draw(drawing *dr)
  138. {
  139. dr->api->start_draw(dr->handle);
  140. }
  141. void end_draw(drawing *dr)
  142. {
  143. dr->api->end_draw(dr->handle);
  144. }
  145. char *text_fallback(drawing *dr, const char *const *strings, int nstrings)
  146. {
  147. int i;
  148. /*
  149. * If the drawing implementation provides one of these, use it.
  150. */
  151. if (dr && dr->api->text_fallback)
  152. return dr->api->text_fallback(dr->handle, strings, nstrings);
  153. /*
  154. * Otherwise, do the simple thing and just pick the first string
  155. * that fits in plain ASCII. It will then need no translation
  156. * out of UTF-8.
  157. */
  158. for (i = 0; i < nstrings; i++) {
  159. const char *p;
  160. for (p = strings[i]; *p; p++)
  161. if (*p & 0x80)
  162. break;
  163. if (!*p)
  164. return dupstr(strings[i]);
  165. }
  166. /*
  167. * The caller was responsible for making sure _some_ string in
  168. * the list was in plain ASCII.
  169. */
  170. assert(!"Should never get here");
  171. return NULL; /* placate optimiser */
  172. }
  173. void status_bar(drawing *dr, const char *text)
  174. {
  175. char *rewritten;
  176. if (!dr->api->status_bar)
  177. return;
  178. assert(dr->me);
  179. rewritten = midend_rewrite_statusbar(dr->me, text);
  180. if (!dr->laststatus || strcmp(rewritten, dr->laststatus)) {
  181. dr->api->status_bar(dr->handle, rewritten);
  182. sfree(dr->laststatus);
  183. dr->laststatus = rewritten;
  184. } else {
  185. sfree(rewritten);
  186. }
  187. }
  188. blitter *blitter_new(drawing *dr, int w, int h)
  189. {
  190. return dr->api->blitter_new(dr->handle, w, h);
  191. }
  192. void blitter_free(drawing *dr, blitter *bl)
  193. {
  194. dr->api->blitter_free(dr->handle, bl);
  195. }
  196. void blitter_save(drawing *dr, blitter *bl, int x, int y)
  197. {
  198. dr->api->blitter_save(dr->handle, bl, x, y);
  199. }
  200. void blitter_load(drawing *dr, blitter *bl, int x, int y)
  201. {
  202. dr->api->blitter_load(dr->handle, bl, x, y);
  203. }
  204. void print_begin_doc(drawing *dr, int pages)
  205. {
  206. dr->api->begin_doc(dr->handle, pages);
  207. }
  208. void print_begin_page(drawing *dr, int number)
  209. {
  210. dr->api->begin_page(dr->handle, number);
  211. }
  212. void print_begin_puzzle(drawing *dr, float xm, float xc,
  213. float ym, float yc, int pw, int ph, float wmm,
  214. float scale)
  215. {
  216. dr->scale = scale;
  217. dr->ncolours = 0;
  218. dr->api->begin_puzzle(dr->handle, xm, xc, ym, yc, pw, ph, wmm);
  219. }
  220. void print_end_puzzle(drawing *dr)
  221. {
  222. dr->api->end_puzzle(dr->handle);
  223. dr->scale = 1.0F;
  224. }
  225. void print_end_page(drawing *dr, int number)
  226. {
  227. dr->api->end_page(dr->handle, number);
  228. }
  229. void print_end_doc(drawing *dr)
  230. {
  231. dr->api->end_doc(dr->handle);
  232. }
  233. void print_get_colour(drawing *dr, int colour, bool printing_in_colour,
  234. int *hatch, float *r, float *g, float *b)
  235. {
  236. assert(colour >= 0 && colour < dr->ncolours);
  237. if (dr->colours[colour].hatch_when == 2 ||
  238. (dr->colours[colour].hatch_when == 1 && !printing_in_colour)) {
  239. *hatch = dr->colours[colour].hatch;
  240. } else {
  241. *hatch = -1;
  242. if (printing_in_colour) {
  243. *r = dr->colours[colour].r;
  244. *g = dr->colours[colour].g;
  245. *b = dr->colours[colour].b;
  246. } else {
  247. *r = *g = *b = dr->colours[colour].grey;
  248. }
  249. }
  250. }
  251. static int print_generic_colour(drawing *dr, float r, float g, float b,
  252. float grey, int hatch, int hatch_when)
  253. {
  254. if (dr->ncolours >= dr->coloursize) {
  255. dr->coloursize = dr->ncolours + 16;
  256. dr->colours = sresize(dr->colours, dr->coloursize,
  257. struct print_colour);
  258. }
  259. dr->colours[dr->ncolours].hatch = hatch;
  260. dr->colours[dr->ncolours].hatch_when = hatch_when;
  261. dr->colours[dr->ncolours].r = r;
  262. dr->colours[dr->ncolours].g = g;
  263. dr->colours[dr->ncolours].b = b;
  264. dr->colours[dr->ncolours].grey = grey;
  265. return dr->ncolours++;
  266. }
  267. int print_mono_colour(drawing *dr, int grey)
  268. {
  269. return print_generic_colour(dr, grey, grey, grey, grey, -1, 0);
  270. }
  271. int print_grey_colour(drawing *dr, float grey)
  272. {
  273. return print_generic_colour(dr, grey, grey, grey, grey, -1, 0);
  274. }
  275. int print_hatched_colour(drawing *dr, int hatch)
  276. {
  277. return print_generic_colour(dr, 0, 0, 0, 0, hatch, 2);
  278. }
  279. int print_rgb_mono_colour(drawing *dr, float r, float g, float b, int grey)
  280. {
  281. return print_generic_colour(dr, r, g, b, grey, -1, 0);
  282. }
  283. int print_rgb_grey_colour(drawing *dr, float r, float g, float b, float grey)
  284. {
  285. return print_generic_colour(dr, r, g, b, grey, -1, 0);
  286. }
  287. int print_rgb_hatched_colour(drawing *dr, float r, float g, float b, int hatch)
  288. {
  289. return print_generic_colour(dr, r, g, b, 0, hatch, 1);
  290. }
  291. void print_line_width(drawing *dr, int width)
  292. {
  293. /*
  294. * I don't think it's entirely sensible to have line widths be
  295. * entirely relative to the puzzle size; there is a point
  296. * beyond which lines are just _stupidly_ thick. On the other
  297. * hand, absolute line widths aren't particularly nice either
  298. * because they start to feel a bit feeble at really large
  299. * scales.
  300. *
  301. * My experimental answer is to scale line widths as the
  302. * _square root_ of the main puzzle scale. Double the puzzle
  303. * size, and the line width multiplies by 1.4.
  304. */
  305. dr->api->line_width(dr->handle, (float)sqrt(dr->scale) * width);
  306. }
  307. void print_line_dotted(drawing *dr, bool dotted)
  308. {
  309. dr->api->line_dotted(dr->handle, dotted);
  310. }