r_raster.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*!
  2. \file lib/display/r_raster.c
  3. \brief Display Library - Raster graphics subroutines
  4. (C) 2001-2014 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 void init(void)
  29. {
  30. const char *fenc = getenv("GRASS_ENCODING");
  31. const char *font = getenv("GRASS_FONT");
  32. const char *line_width = getenv("GRASS_RENDER_LINE_WIDTH");
  33. const char *text_size = getenv("GRASS_RENDER_TEXT_SIZE");
  34. const char *frame = getenv("GRASS_RENDER_FRAME");
  35. D_font(font ? font : "romans");
  36. if (fenc)
  37. D_encoding(fenc);
  38. if (line_width)
  39. COM_Line_width(atof(line_width));
  40. if (text_size) {
  41. double s = atof(text_size);
  42. D_text_size(s, s);
  43. }
  44. D_text_rotation(0);
  45. if (frame) {
  46. double t, b, l, r;
  47. sscanf(frame, "%lf,%lf,%lf,%lf", &t, &b, &l, &r);
  48. COM_Set_window(t, b, l, r);
  49. }
  50. }
  51. int read_env_file(const char *path)
  52. {
  53. FILE *fd;
  54. char buf[1024];
  55. char **token;
  56. G_debug(1, "read_env_file(): %s", path);
  57. fd = fopen(path, "r");
  58. if (!fd)
  59. return -1;
  60. token = NULL;
  61. while (G_getl2(buf, sizeof(buf) - 1, fd) != 0) {
  62. token = G_tokenize(buf, "=");
  63. if (G_number_of_tokens(token) != 2)
  64. continue;
  65. G_debug(3, "\tread_env_file(): %s=%s", token[0], token[1]);
  66. G_putenv(token[0], token[1]);
  67. G_free_tokens(token);
  68. token = NULL;
  69. }
  70. return 0;
  71. }
  72. /*!
  73. \brief Open display driver
  74. Default display driver is Cairo, if not available PNG is used.
  75. \return 0 on success
  76. \return 1 no monitor defined
  77. */
  78. int D_open_driver(void)
  79. {
  80. const char *p, *m;
  81. const struct driver *drv;
  82. G_debug(1, "D_open_driver():");
  83. p = getenv("GRASS_RENDER_IMMEDIATE");
  84. m = G_getenv_nofatal("MONITOR");
  85. if (m && G_strncasecmp(m, "wx", 2) == 0) {
  86. /* wx monitors always use GRASS_RENDER_IMMEDIATE. */
  87. p = NULL; /* use default display driver */
  88. }
  89. else if (m) {
  90. char *env;
  91. const char *v;
  92. char *u_m;
  93. if (p)
  94. G_warning(_("%s variable defined, %s ignored"),
  95. "MONITOR", "GRASS_RENDER_IMMEDIATE");
  96. /* GRASS variable names should be upper case. */
  97. u_m = G_store_upper(m);
  98. env = NULL;
  99. G_asprintf(&env, "MONITOR_%s_MAPFILE", u_m);
  100. v = G_getenv_nofatal(env);
  101. p = m;
  102. if (v)
  103. G_putenv("GRASS_RENDER_FILE", v);
  104. G_asprintf(&env, "MONITOR_%s_ENVFILE", u_m);
  105. v = G_getenv_nofatal(env);
  106. if (v)
  107. read_env_file(v);
  108. }
  109. else if (!p)
  110. G_fatal_error(_("Neither %s (managed by d.mon command) nor %s (used for direct rendering) defined"),
  111. "MONITOR", "GRASS_RENDER_IMMEDIATE");
  112. if (p && G_strcasecmp(p, "default") == 0)
  113. p = NULL;
  114. drv =
  115. (p && G_strcasecmp(p, "png") == 0) ? PNG_Driver() :
  116. (p && G_strcasecmp(p, "ps") == 0) ? PS_Driver() :
  117. (p && G_strcasecmp(p, "html") == 0) ? HTML_Driver() :
  118. #ifdef USE_CAIRO
  119. (p && G_strcasecmp(p, "cairo") == 0) ? Cairo_Driver() :
  120. Cairo_Driver();
  121. #else
  122. PNG_Driver();
  123. #endif
  124. if (p && G_strcasecmp(drv->name, p) != 0)
  125. G_warning(_("Unknown display driver <%s>"), p);
  126. G_verbose_message(_("Using display driver <%s>..."), drv->name);
  127. LIB_init(drv);
  128. init();
  129. return 0;
  130. }
  131. /*!
  132. \brief Close display driver
  133. If GRASS_NOTIFY is defined, run notifier.
  134. */
  135. void D_close_driver(void)
  136. {
  137. const char *cmd = getenv("GRASS_NOTIFY");
  138. COM_Graph_close();
  139. if (cmd)
  140. system(cmd);
  141. }
  142. /*!
  143. \brief Append command to the cmd file
  144. Cmd file is created by d.mon by defining GRASS variable
  145. \c MONITOR_<name>_CMDFILE, where \c \<name\> is the upper case name of
  146. the monitor.
  147. Command string is usually generated by G_recreate_command(), NULL is
  148. used to clean up list of commands (see d.erase command).
  149. \param cmd string buffer with command or NULL
  150. \return 0 no monitor selected
  151. \return -1 on error
  152. \return 1 on success
  153. */
  154. int D_save_command(const char *cmd)
  155. {
  156. const char *mon_name, *mon_cmd;
  157. char *env, *flag, *u_mon_name;
  158. FILE *fd;
  159. G_debug(1, "D_save_command(): %s", cmd);
  160. mon_name = G_getenv_nofatal("MONITOR");
  161. if (!mon_name || /* if no monitor selected */
  162. /* or wx monitor selected and display commands called by the monitor */
  163. (G_strncasecmp(mon_name, "wx", 2) == 0 &&
  164. getenv("GRASS_RENDER_IMMEDIATE")))
  165. return 0;
  166. /* GRASS variable names should be upper case. */
  167. u_mon_name = G_store_upper(mon_name);
  168. env = NULL;
  169. G_asprintf(&env, "MONITOR_%s_CMDFILE", u_mon_name);
  170. mon_cmd = G_getenv_nofatal(env);
  171. if (!mon_cmd)
  172. return 0;
  173. if (cmd)
  174. flag = "a";
  175. else
  176. flag = "w";
  177. fd = fopen(mon_cmd, flag);
  178. if (!fd) {
  179. G_warning(_("Unable to open file '%s'"), mon_cmd);
  180. return -1;
  181. }
  182. if (cmd)
  183. fprintf(fd, "%s\n", cmd);
  184. fclose(fd);
  185. return 1;
  186. }
  187. /*!
  188. \brief Erase display (internal use only)
  189. */
  190. void D__erase(void)
  191. {
  192. COM_Erase();
  193. }
  194. /*!
  195. \brief Set text size (width and height)
  196. \param width text pixel width
  197. \param height text pixel height
  198. */
  199. void D_text_size(double width, double height)
  200. {
  201. COM_Text_size(width, height);
  202. }
  203. /*!
  204. \brief Set text rotation
  205. \param rotation value
  206. */
  207. void D_text_rotation(double rotation)
  208. {
  209. COM_Text_rotation(rotation);
  210. }
  211. /*!
  212. \brief Get clipping frame
  213. \param[out] t top
  214. \param[out] b bottom
  215. \param[out] l left
  216. \param[out] r right
  217. */
  218. void D_get_window(double *t, double *b, double *l, double *r)
  219. {
  220. return COM_Get_window(t, b, l, r);
  221. }
  222. /*!
  223. \brief Draw text
  224. Writes <em>text</em> in the current color and font, at the current text
  225. width and height, starting at the current screen location.
  226. \param text text to be drawn
  227. */
  228. void D_text(const char *text)
  229. {
  230. COM_Text(text);
  231. }
  232. /*!
  233. \brief Choose font
  234. Set current font to <em>font name</em>.
  235. \param name font name
  236. */
  237. void D_font(const char *name)
  238. {
  239. COM_Set_font(name);
  240. }
  241. /*!
  242. \brief Set encoding
  243. \param name encoding name
  244. */
  245. void D_encoding(const char *name)
  246. {
  247. COM_Set_encoding(name);
  248. }
  249. /*!
  250. \brief Get font list
  251. \param[out] list list of font names
  252. \param[out] number of items in the list
  253. */
  254. void D_font_list(char ***list, int *count)
  255. {
  256. COM_Font_list(list, count);
  257. }
  258. /*!
  259. \brief Get font info
  260. \param[out] list list of font info
  261. \param[out] number of items in the list
  262. */
  263. void D_font_info(char ***list, int *count)
  264. {
  265. COM_Font_info(list, count);
  266. }