r_raster.c 6.1 KB

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