Graph.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #include "cairodriver.h"
  2. #include <cairo-ps.h>
  3. #include <cairo-pdf.h>
  4. #include <cairo-svg.h>
  5. #if defined(USE_X11) && CAIRO_HAS_XLIB_SURFACE
  6. #include <cairo-xlib.h>
  7. #endif
  8. #include <unistd.h>
  9. #ifndef __MINGW32__
  10. #include <fcntl.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <sys/mman.h>
  14. #endif
  15. #if defined(USE_X11) && CAIRO_HAS_XLIB_SURFACE
  16. #include <X11/X.h>
  17. #include <X11/Xlib.h>
  18. #include <X11/Xutil.h>
  19. #endif
  20. /* globals */
  21. char *file_name;
  22. int file_type;
  23. int is_vector;
  24. int width, height, stride;
  25. unsigned char *grid;
  26. int modified;
  27. int auto_write;
  28. int mapped;
  29. /* background color */
  30. double bgcolor_r, bgcolor_g, bgcolor_b, bgcolor_a;
  31. /* cairo objects */
  32. cairo_surface_t *surface;
  33. cairo_t *cairo;
  34. static void init_cairo(void);
  35. static int ends_with(const char *string, const char *suffix);
  36. static void map_file(void);
  37. #if defined(USE_X11) && CAIRO_HAS_XLIB_SURFACE
  38. static int init_xlib(void)
  39. {
  40. Display *dpy;
  41. Drawable win;
  42. unsigned long xid;
  43. XVisualInfo templ;
  44. XVisualInfo *vinfo;
  45. int count;
  46. Window root;
  47. unsigned int depth;
  48. int si;
  49. unsigned int ui;
  50. Visual *visual;
  51. char *p;
  52. p = getenv("GRASS_CAIRO_DRAWABLE");
  53. if (!p || sscanf(p, "%li", &xid) != 1)
  54. G_fatal_error("invalid Drawable XID: %s", p);
  55. win = xid;
  56. dpy = XOpenDisplay(NULL);
  57. if (!dpy)
  58. G_fatal_error("Unable to open display");
  59. p = getenv("GRASS_CAIRO_VISUAL");
  60. if (!p || sscanf(p, "%li", &xid) != 1)
  61. G_fatal_error("invalid Visual XID: %s", p);
  62. templ.visualid = xid;
  63. vinfo = XGetVisualInfo(dpy, VisualIDMask, &templ, &count);
  64. if (!vinfo || !count)
  65. G_fatal_error("Unable to obtain visual");
  66. visual = vinfo[0].visual;
  67. if (!XGetGeometry
  68. (dpy, win, &root, &si, &si, &width, &height, &ui, &depth))
  69. G_fatal_error("Unable to query drawable");
  70. surface = cairo_xlib_surface_create(dpy, win, visual, width, height);
  71. if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS)
  72. G_fatal_error("Failed to initialize Xlib surface");
  73. cairo = cairo_create(surface);
  74. file_name = "<X11>";
  75. file_type = FTYPE_X11;
  76. screen_right = screen_left + width;
  77. screen_bottom = screen_top + height;
  78. return 0;
  79. }
  80. #endif
  81. static int init_file(void)
  82. {
  83. int do_read = 0;
  84. int do_map = 0;
  85. char *p;
  86. /* set image properties */
  87. width = screen_width;
  88. height = screen_height;
  89. stride = width * 4;
  90. /* get file name */
  91. p = getenv("GRASS_PNGFILE");
  92. if (!p || strlen(p) == 0)
  93. p = DEFAULT_FILE_NAME;
  94. file_name = p;
  95. /* get file type (from extension) */
  96. if (file_type == FTYPE_X11) ; /* skip */
  97. else if (ends_with(file_name, ".ppm"))
  98. file_type = FTYPE_PPM;
  99. else if (ends_with(file_name, ".bmp"))
  100. file_type = FTYPE_BMP;
  101. #if CAIRO_HAS_PNG_FUNCTIONS
  102. else if (ends_with(file_name, ".png"))
  103. file_type = FTYPE_PNG;
  104. #endif
  105. #if CAIRO_HAS_PDF_SURFACE
  106. else if (ends_with(file_name, ".pdf"))
  107. file_type = FTYPE_PDF;
  108. #endif
  109. #if CAIRO_HAS_PS_SURFACE
  110. else if (ends_with(file_name, ".ps"))
  111. file_type = FTYPE_PS;
  112. #endif
  113. #if CAIRO_HAS_SVG_SURFACE
  114. else if (ends_with(file_name, ".svg"))
  115. file_type = FTYPE_SVG;
  116. #endif
  117. else
  118. G_fatal_error("Unknown file extension: %s", p);
  119. G_debug(1, "File type: %s (%d)", file_name, file_type);
  120. switch (file_type) {
  121. case FTYPE_PDF:
  122. case FTYPE_PS:
  123. case FTYPE_SVG:
  124. is_vector = 1;
  125. break;
  126. }
  127. p = getenv("GRASS_PNG_MAPPED");
  128. do_map = p && strcmp(p, "TRUE") == 0 && ends_with(file_name, ".bmp");
  129. p = getenv("GRASS_PNG_READ");
  130. do_read = p && strcmp(p, "TRUE") == 0;
  131. if (is_vector) {
  132. do_read = do_map = 0;
  133. bgcolor_a = 1.0;
  134. }
  135. if (do_read && access(file_name, 0) != 0)
  136. do_read = 0;
  137. G_message
  138. ("cairo: collecting to file: %s,\n GRASS_WIDTH=%d, GRASS_HEIGHT=%d",
  139. file_name, width, height);
  140. if (do_read && do_map)
  141. map_file();
  142. if (!mapped && !is_vector)
  143. grid = G_malloc(height * stride);
  144. init_cairo();
  145. if (!do_read && !is_vector) {
  146. Cairo_Erase();
  147. modified = 1;
  148. }
  149. if (do_read && !mapped)
  150. cairo_read_image();
  151. if (do_map && !mapped) {
  152. cairo_write_image();
  153. map_file();
  154. init_cairo();
  155. }
  156. return 0;
  157. }
  158. int Cairo_Graph_set(void)
  159. {
  160. char *p;
  161. G_gisinit("Cairo driver");
  162. G_debug(1, "Cairo_Graph_set");
  163. /* get background color */
  164. p = getenv("GRASS_BACKGROUNDCOLOR");
  165. if (p && *p) {
  166. unsigned int red, green, blue;
  167. if (sscanf(p, "%02x%02x%02x", &red, &green, &blue) == 3) {
  168. bgcolor_r = CAIROCOLOR(red);
  169. bgcolor_g = CAIROCOLOR(green);
  170. bgcolor_b = CAIROCOLOR(blue);
  171. }
  172. else
  173. G_fatal_error("Unknown background color: %s", p);
  174. }
  175. else
  176. bgcolor_r = bgcolor_g = bgcolor_b = 1.0;
  177. /* get background transparency setting */
  178. p = getenv("GRASS_TRANSPARENT");
  179. if (p && strcmp(p, "TRUE") == 0)
  180. bgcolor_a = 0.0;
  181. else
  182. bgcolor_a = 1.0;
  183. p = getenv("GRASS_PNG_AUTO_WRITE");
  184. auto_write = p && strcmp(p, "TRUE") == 0;
  185. #if defined(USE_X11) && CAIRO_HAS_XLIB_SURFACE
  186. p = getenv("GRASS_CAIRO_DRAWABLE");
  187. if (p)
  188. return init_xlib();
  189. #endif
  190. return init_file();
  191. }
  192. void Cairo_Graph_close(void)
  193. {
  194. G_debug(1, "Cairo_Graph_close");
  195. cairo_write_image();
  196. if (cairo) {
  197. cairo_destroy(cairo);
  198. cairo = NULL;
  199. }
  200. if (surface) {
  201. cairo_surface_destroy(surface);
  202. surface = NULL;
  203. }
  204. }
  205. static void init_cairo(void)
  206. {
  207. G_debug(1, "init_cairo");
  208. /* create cairo surface */
  209. switch (file_type) {
  210. case FTYPE_PPM:
  211. case FTYPE_BMP:
  212. case FTYPE_PNG:
  213. surface =
  214. (cairo_surface_t *) cairo_image_surface_create_for_data(
  215. grid, CAIRO_FORMAT_ARGB32, width, height, stride);
  216. break;
  217. #if CAIRO_HAS_PDF_SURFACE
  218. case FTYPE_PDF:
  219. surface =
  220. (cairo_surface_t *) cairo_pdf_surface_create(
  221. file_name, (double) width, (double) height);
  222. break;
  223. #endif
  224. #if CAIRO_HAS_PS_SURFACE
  225. case FTYPE_PS:
  226. surface =
  227. (cairo_surface_t *) cairo_ps_surface_create(
  228. file_name, (double) width, (double) height);
  229. break;
  230. #endif
  231. #if CAIRO_HAS_SVG_SURFACE
  232. case FTYPE_SVG:
  233. surface =
  234. (cairo_surface_t *) cairo_svg_surface_create(
  235. file_name, (double) width, (double) height);
  236. break;
  237. #endif
  238. default:
  239. G_fatal_error("Unknown Cairo surface type");
  240. break;
  241. }
  242. if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS)
  243. G_fatal_error("Failed to initialize Cairo surface");
  244. cairo = cairo_create(surface);
  245. }
  246. /* Returns TRUE if string ends with suffix (case insensitive) */
  247. static int ends_with(const char *string, const char *suffix)
  248. {
  249. if (strlen(string) < strlen(suffix))
  250. return FALSE;
  251. return G_strcasecmp(suffix,
  252. string + strlen(string) - strlen(suffix)) == 0;
  253. }
  254. static void map_file(void)
  255. {
  256. #ifndef __MINGW32__
  257. size_t size = HEADER_SIZE + width * height * sizeof(unsigned int);
  258. void *ptr;
  259. int fd;
  260. fd = open(file_name, O_RDWR);
  261. if (fd < 0)
  262. return;
  263. ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (off_t) 0);
  264. if (ptr == MAP_FAILED)
  265. return;
  266. if (grid) {
  267. cairo_destroy(cairo);
  268. cairo_surface_destroy(surface);
  269. G_free(grid);
  270. }
  271. grid = (char *)ptr + HEADER_SIZE;
  272. close(fd);
  273. mapped = 1;
  274. #endif
  275. }