r_raster.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*!
  2. \file lib/display/r_raster.c
  3. \brief Display Library - Raster graphics subroutines
  4. (C) 2001-2009, 2011 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. \author Monitors support by Martin Landa <landa.martin gmail.com>
  9. */
  10. #include <grass/config.h>
  11. #include <errno.h>
  12. #include <signal.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <grass/gis.h>
  18. #include <grass/glocale.h>
  19. #include <grass/display.h>
  20. #include "driver.h"
  21. extern const struct driver *PNG_Driver(void);
  22. extern const struct driver *PS_Driver(void);
  23. extern const struct driver *HTML_Driver(void);
  24. #ifdef USE_CAIRO
  25. extern const struct driver *Cairo_Driver(void);
  26. #endif
  27. static int read_env_file(const char *);
  28. static struct {
  29. double t, b, l, r;
  30. } screen;
  31. static void init(void)
  32. {
  33. const char *fenc = getenv("GRASS_ENCODING");
  34. const char *font = getenv("GRASS_FONT");
  35. const char *line_width = getenv("GRASS_RENDER_LINE_WIDTH");
  36. const char *text_size = getenv("GRASS_RENDER_TEXT_SIZE");
  37. const char *frame = getenv("GRASS_RENDER_FRAME");
  38. D_font(font ? font : "romans");
  39. if (fenc)
  40. D_encoding(fenc);
  41. if (line_width)
  42. COM_Line_width(atof(line_width));
  43. if (text_size) {
  44. double s = atof(text_size);
  45. D_text_size(s, s);
  46. }
  47. D_text_rotation(0);
  48. if (frame) {
  49. sscanf(frame, "%lf,%lf,%lf,%lf", &screen.t, &screen.b, &screen.l, &screen.r);
  50. COM_Set_window(screen.t, screen.b, screen.l, screen.r);
  51. }
  52. else
  53. COM_Get_window(&screen.t, &screen.b, &screen.l, &screen.r);
  54. }
  55. int read_env_file(const char *path)
  56. {
  57. FILE *fd;
  58. char buf[1024];
  59. char **token;
  60. fd = fopen(path, "r");
  61. if (!fd)
  62. return -1;
  63. token = NULL;
  64. while (G_getl2(buf, sizeof(buf) - 1, fd) != 0) {
  65. token = G_tokenize(buf, "=");
  66. if (G_number_of_tokens(token) != 2)
  67. continue;
  68. G_debug(3, "\tread_env_file(): %s=%s", token[0], token[1]);
  69. G_putenv(token[0], token[1]);
  70. G_free_tokens(token);
  71. token = NULL;
  72. }
  73. return 0;
  74. }
  75. /*!
  76. \brief Open display driver
  77. Default display driver is Cairo, if not available PNG is used.
  78. \return 0 on success
  79. \return 1 no monitor defined
  80. */
  81. int D_open_driver(void)
  82. {
  83. const char *p, *m;
  84. G_debug(1, "D_open_driver():");
  85. p = getenv("GRASS_RENDER_IMMEDIATE");
  86. m = G__getenv("MONITOR");
  87. if (m && G_strncasecmp(m, "wx", 2) == 0) {
  88. /* wx monitors always use GRASS_RENDER_IMMEDIATE. */
  89. p = NULL; /* use default display driver */
  90. } else if (m) {
  91. char *env;
  92. const char *v;
  93. char *u_m;
  94. if (p)
  95. G_warning(_("%s variable defined, %s ignored"),
  96. "MONITOR", "GRASS_RENDER_IMMEDIATE");
  97. /* GRASS variable names should be upper case. */
  98. u_m = G_store_upper(m);
  99. env = NULL;
  100. G_asprintf(&env, "MONITOR_%s_MAPFILE", u_m);
  101. v = G__getenv(env);
  102. p = m;
  103. if (v)
  104. G_putenv("GRASS_RENDER_FILE", v);
  105. G_asprintf(&env, "MONITOR_%s_ENVFILE", u_m);
  106. v = G__getenv(env);
  107. if (v)
  108. read_env_file(v);
  109. }
  110. const struct driver *drv =
  111. (p && G_strcasecmp(p, "PNG") == 0) ? PNG_Driver() :
  112. (p && G_strcasecmp(p, "PS") == 0) ? PS_Driver() :
  113. (p && G_strcasecmp(p, "HTML") == 0) ? HTML_Driver() :
  114. #ifdef USE_CAIRO
  115. (p && G_strcasecmp(p, "cairo") == 0) ? Cairo_Driver() :
  116. Cairo_Driver();
  117. #else
  118. PNG_Driver();
  119. #endif
  120. if (p && G_strcasecmp(drv->name, p) != 0)
  121. G_warning(_("Unknown display driver <%s>"), p);
  122. G_verbose_message(_("Using display driver <%s>..."), drv->name);
  123. LIB_init(drv);
  124. init();
  125. return 0;
  126. }
  127. /*!
  128. \brief Close display driver
  129. If GRASS_NOTIFY is defined, run notifier.
  130. */
  131. void D_close_driver(void)
  132. {
  133. const char *cmd = getenv("GRASS_NOTIFY");
  134. COM_Graph_close();
  135. if (cmd)
  136. system(cmd);
  137. }
  138. /*!
  139. \brief Append command to the cmd file
  140. Cmd file is created by d.mon by defining GRASS variable
  141. \c MONITOR_<name>_CMDFILE, where \c \<name\> is the upper case name of
  142. the monitor.
  143. Command string is usually generated by G_recreate_command(), NULL is
  144. used to clean up list of commands (see d.erase command).
  145. \param cmd string buffer with command or NULL
  146. \return 0 no monitor selected
  147. \return -1 on error
  148. \return 1 on success
  149. */
  150. int D_save_command(const char *cmd)
  151. {
  152. const char *mon_name, *mon_cmd;
  153. char *env, *flag, *u_mon_name;
  154. FILE *fd;
  155. G_debug(1, "D_save_command(): %s", cmd);
  156. mon_name = G__getenv("MONITOR");
  157. if (!mon_name || /* if no monitor selected */
  158. /* or wx monitor selected and display commands called by the monitor */
  159. (G_strncasecmp(mon_name, "wx", 2) == 0 &&
  160. getenv("GRASS_RENDER_IMMEDIATE")))
  161. return 0;
  162. /* GRASS variable names should be upper case. */
  163. u_mon_name = G_store_upper(mon_name);
  164. env = NULL;
  165. G_asprintf(&env, "MONITOR_%s_CMDFILE", u_mon_name);
  166. mon_cmd = G__getenv(env);
  167. if (!mon_cmd)
  168. return 0;
  169. if (cmd)
  170. flag = "a";
  171. else
  172. flag = "w";
  173. fd = fopen(mon_cmd, flag);
  174. if (!fd) {
  175. G_warning(_("Unable to open file '%s'"), mon_cmd);
  176. return -1;
  177. }
  178. if (cmd)
  179. fprintf(fd, "%s\n", cmd);
  180. fclose(fd);
  181. return 1;
  182. }
  183. /*!
  184. \brief Erase display (internal use only)
  185. */
  186. void D__erase(void)
  187. {
  188. COM_Erase();
  189. }
  190. /*!
  191. \brief Set text size (width and height)
  192. \param width text pixel width
  193. \param height text pixel height
  194. */
  195. void D_text_size(double width, double height)
  196. {
  197. COM_Text_size(width, height);
  198. }
  199. /*!
  200. \brief Set text rotation
  201. \param rotation value
  202. */
  203. void D_text_rotation(double rotation)
  204. {
  205. COM_Text_rotation(rotation);
  206. }
  207. /*!
  208. \brief Draw text
  209. Writes <em>text</em> in the current color and font, at the current text
  210. width and height, starting at the current screen location.
  211. \param text text to be drawn
  212. */
  213. void D_text(const char *text)
  214. {
  215. COM_Text(text);
  216. }
  217. /*!
  218. \brief Choose font
  219. Set current font to <em>font name</em>.
  220. \param name font name
  221. */
  222. void D_font(const char *name)
  223. {
  224. COM_Set_font(name);
  225. }
  226. /*!
  227. \brief Set encoding
  228. \param name encoding name
  229. */
  230. void D_encoding(const char *name)
  231. {
  232. COM_Set_encoding(name);
  233. }
  234. /*!
  235. \brief Get font list
  236. \param[out] list list of font names
  237. \param[out] number of items in the list
  238. */
  239. void D_font_list(char ***list, int *count)
  240. {
  241. COM_Font_list(list, count);
  242. }
  243. /*!
  244. \brief Get font info
  245. \param[out] list list of font info
  246. \param[out] number of items in the list
  247. */
  248. void D_font_info(char ***list, int *count)
  249. {
  250. COM_Font_info(list, count);
  251. }
  252. /*!
  253. * \brief get graphical clipping window
  254. *
  255. * Queries the graphical clipping window (origin is top right)
  256. *
  257. * \param[out] t top edge of clip window
  258. * \param[out] b bottom edge of clip window
  259. * \param[out] l left edge of clip window
  260. * \param[out] r right edge of clip window
  261. * \return ~
  262. */
  263. void D_get_clip_window(double *t, double *b, double *l, double *r)
  264. {
  265. COM_Get_window(t, b, l, r);
  266. }
  267. /*!
  268. * \brief set graphical clipping window
  269. *
  270. * Sets the graphical clipping window to the specified rectangle
  271. * (origin is top right)
  272. *
  273. * \param t top edge of clip window
  274. * \param b bottom edge of clip window
  275. * \param l left edge of clip window
  276. * \param r right edge of clip window
  277. * \return ~
  278. */
  279. void D_set_clip_window(double t, double b, double l, double r)
  280. {
  281. if (t < screen.t) t = screen.t;
  282. if (b > screen.b) b = screen.b;
  283. if (l < screen.l) l = screen.l;
  284. if (r > screen.r) r = screen.r;
  285. COM_Set_window(t, b, l, r);
  286. }
  287. /*!
  288. * \brief get graphical window (frame)
  289. *
  290. * Queries the graphical frame (origin is top right)
  291. *
  292. * \param[out] t top edge of frame
  293. * \param[out] b bottom edge of frame
  294. * \param[out] l left edge of frame
  295. * \param[out] r right edge of frame
  296. * \return ~
  297. */
  298. void D_get_frame(double *t, double *b, double *l, double *r)
  299. {
  300. *t = screen.t;
  301. *b = screen.b;
  302. *l = screen.l;
  303. *r = screen.r;
  304. }
  305. /*!
  306. * \brief set graphical clipping window to map window
  307. *
  308. * Sets the graphical clipping window to the pixel window that corresponds
  309. * to the current database region.
  310. *
  311. * \param ~
  312. * \return ~
  313. */
  314. void D_set_clip_window_to_map_window(void)
  315. {
  316. D_set_clip_window(
  317. D_get_d_north(), D_get_d_south(),
  318. D_get_d_west(), D_get_d_east());
  319. }
  320. /*!
  321. * \brief set clipping window to screen window
  322. *
  323. * Sets the clipping window to the pixel window that corresponds to the
  324. * full screen window. Off screen rendering is still clipped.
  325. *
  326. * \param ~
  327. * \return ~
  328. */
  329. void D_set_clip_window_to_screen_window(void)
  330. {
  331. COM_Set_window(screen.t, screen.b, screen.l, screen.r);
  332. }