main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /****************************************************************************
  2. *
  3. * MODULE: ps.map
  4. * AUTHOR(S): Paul W. Carlson 1992 (original contributor)
  5. * Radim Blazek <radim.blazek gmail.com>
  6. * Bob Covill <bcovill tekmap.ns.ca>
  7. * Huidae Cho <grass4u gmail.com>
  8. * Glynn Clements <glynn gclements.plus.com>
  9. * Hamish Bowman <hamish_b yahoo.com>
  10. * Markus Neteler <neteler itc.it>
  11. * Alessandro Frigeri <afrigeri unipg.it>
  12. * Martin Landa <landa.martin gmail.com>
  13. * PURPOSE: Hardcopy PostScript map output utility (based on p.map program)
  14. * COPYRIGHT: (C) 2003-2011 by the GRASS Development Team
  15. *
  16. * This program is free software under the GNU General
  17. * Public License (>=v2). Read the file COPYING that
  18. * comes with GRASS for details.
  19. *
  20. *****************************************************************************/
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <stdlib.h>
  24. #include <signal.h>
  25. #include <errno.h>
  26. #include <grass/gis.h>
  27. #include <grass/colors.h>
  28. #include <grass/raster.h>
  29. #include <grass/glocale.h>
  30. #include "map_info.h"
  31. #include "vector.h"
  32. #include "labels.h"
  33. #include "header.h"
  34. #include "border.h"
  35. #include "comment.h"
  36. #include "colortable.h"
  37. #include "local_proto.h"
  38. struct border brd;
  39. struct map_info m_info;
  40. struct labels labels;
  41. struct colortable ct;
  42. struct PS_data PS;
  43. int WHITE = 0;
  44. int BLACK = 1;
  45. int GREY = 9;
  46. int sec_draw;
  47. struct vector vector;
  48. struct header hdr;
  49. struct scalebar sb;
  50. struct comment cmt;
  51. struct PS_group grp;
  52. FILE *tracefd;
  53. FILE *inputfd;
  54. int do_mapinfo;
  55. int do_vlegend;
  56. char *ps_mask_file;
  57. int rotate_plot;
  58. int eps_output;
  59. int ps_copies = 1;
  60. int main(int argc, char *argv[])
  61. {
  62. char buf[1024];
  63. int can_reset_scale;
  64. int copies_set;
  65. struct Option *input_file;
  66. struct Option *output_file;
  67. struct Option *copies;
  68. struct Flag *rflag, *pflag, *eflag, *bflag;
  69. struct GModule *module;
  70. static char *def_font = "Helvetica";
  71. /**************** begin ******************************/
  72. signal(SIGINT, exit);
  73. signal(SIGTERM, exit);
  74. setbuf(stderr, NULL);
  75. G_gisinit(argv[0]);
  76. /* Set description */
  77. module = G_define_module();
  78. G_add_keyword(_("postscript"));
  79. G_add_keyword(_("map"));
  80. G_add_keyword(_("printing"));
  81. module->description = _("Produces hardcopy PostScript map output.");
  82. rflag = G_define_flag();
  83. rflag->key = 'r';
  84. rflag->description = _("Rotate plot 90 degrees");
  85. rflag->guisection = _("Output settings");
  86. pflag = G_define_flag();
  87. pflag->key = 'p';
  88. pflag->description =
  89. _("List paper formats (name width height left right top bottom(margin))");
  90. pflag->suppress_required = YES;
  91. pflag->guisection = _("Utility");
  92. eflag = G_define_flag();
  93. eflag->key = 'e';
  94. eflag->description =
  95. _("Create EPS (Encapsulated PostScript) instead of PostScript file");
  96. eflag->guisection = _("Output settings");
  97. bflag = G_define_flag();
  98. bflag->key = 'b';
  99. bflag->description =
  100. _("Describe map-box's position on the page and exit (inches from top-left of paper)");
  101. bflag->suppress_required = YES;
  102. bflag->guisection = _("Utility");
  103. input_file = G_define_standard_option(G_OPT_F_INPUT);
  104. input_file->label = _("File containing mapping instructions");
  105. input_file->description = _("Use '-' to enter instructions from keyboard)");
  106. output_file = G_define_standard_option(G_OPT_F_OUTPUT);
  107. output_file->description = _("Name for PostScript output file");
  108. copies = G_define_option();
  109. copies->key = "copies";
  110. copies->type = TYPE_INTEGER;
  111. copies->options = "1-20";
  112. copies->description = _("Number of copies to print");
  113. copies->required = NO;
  114. copies->guisection = _("Output settings");
  115. if (G_parser(argc, argv))
  116. exit(EXIT_FAILURE);
  117. G_zero(&PS, sizeof(struct PS_data));
  118. /* Print paper sizes to stdout */
  119. if (pflag->answer) {
  120. print_papers();
  121. exit(EXIT_SUCCESS);
  122. }
  123. rotate_plot = rflag->answer;
  124. eps_output = eflag->answer;
  125. /* set default paper */
  126. set_paper("a4");
  127. strcpy(buf, "black");
  128. BLACK = get_color_number(buf);
  129. strcpy(buf, "white");
  130. WHITE = get_color_number(buf);
  131. strcpy(buf, "grey");
  132. GREY = get_color_number(buf);
  133. /* initialize */
  134. vector_init();
  135. copies_set = 0;
  136. m_info.x = m_info.y = -1.0;
  137. vector.x = vector.y = -1.0;
  138. ct.x = ct.y = -1.0;
  139. ct.width = -1.0;
  140. cmt.color = BLACK;
  141. m_info.font = G_store(def_font);
  142. vector.font = G_store(def_font);
  143. hdr.font = G_store(def_font);
  144. cmt.font = G_store(def_font);
  145. ct.font = G_store(def_font);
  146. m_info.fontsize = 10;
  147. vector.fontsize = 10;
  148. hdr.fontsize = 10;
  149. cmt.fontsize = 10;
  150. ct.fontsize = 10;
  151. ct.cols = 1;
  152. tracefd = NULL;
  153. inputfd = NULL;
  154. labels.count = 0;
  155. labels.other = NULL;
  156. can_reset_scale = 1;
  157. hdr.fp = NULL;
  158. grp.do_group = 0;
  159. brd.R = brd.G = brd.B = 0.;
  160. brd.width = 1.;
  161. PS.min_y = 72.0 * (PS.page_height - PS.top_marg);
  162. PS.set_y = 100.0 * PS.min_y;
  163. PS.cell_fd = -1;
  164. PS.do_border = TRUE;
  165. /* PS.map_* variables are set to 0 (not defined) and then may be
  166. * reset by 'maploc'. When script is read, main() should call
  167. * reset_map_location() to reset map size to fit to paper */
  168. /* arguments */
  169. if (input_file->answer) {
  170. if (strcmp(input_file->answer, "-")) {
  171. inputfd = fopen(input_file->answer, "r");
  172. if (!inputfd)
  173. G_fatal_error(_("Unable to open file '%s': %s"),
  174. input_file->answer, strerror(errno));
  175. }
  176. else {
  177. inputfd = stdin;
  178. }
  179. }
  180. else {
  181. G_fatal_error(_("Required parameter <%s> not set:\n\t(%s)"),
  182. input_file->key, input_file->label);
  183. }
  184. if (copies->answer) {
  185. if (sscanf(copies->answer, "%d", &ps_copies) != 1) {
  186. ps_copies = 1;
  187. error(copies->answer, "", _("illegal copies request"));
  188. }
  189. copies_set = 1;
  190. }
  191. if (!bflag->answer) {
  192. if (output_file->answer) {
  193. if ((PS.fp = fopen(output_file->answer, "w")) == NULL)
  194. G_fatal_error("Unable to create file '%s': %s",
  195. output_file->answer, strerror(errno));
  196. }
  197. else {
  198. G_fatal_error(_("Required parameter <%s> not set:\n\t(%s)"),
  199. output_file->key, output_file->description);
  200. }
  201. }
  202. else
  203. PS.fp = NULL;
  204. /* get current mapset */
  205. PS.cell_mapset = G_mapset();
  206. /* set current window */
  207. G_get_set_window(&PS.w);
  208. Rast_set_window(&PS.w);
  209. read_instructions(inputfd, &PS, copies_set, ps_copies, can_reset_scale,
  210. &sb, &do_mapinfo, &do_vlegend, &grp);
  211. /* reset map location base on 'paper' on 'location' */
  212. reset_map_location();
  213. if (bflag->answer) {
  214. map_setup();
  215. fprintf(stdout, "bbox=%.3f,%.3f,%.3f,%.3f\n", PS.map_left / 72.0,
  216. PS.page_height - (PS.map_bot / 72.0), PS.map_right / 72.0,
  217. PS.page_height - (PS.map_top / 72.0));
  218. /* +/- 0.5 ? see ps.map.c brd.* */
  219. exit(EXIT_SUCCESS);
  220. }
  221. /* write the PostScript output file */
  222. ps_mask_file = G_tempfile();
  223. ps_map();
  224. G_done_msg(_("PostScript file '%s' successfully written."),
  225. output_file->answer);
  226. /* cleanup the tempfiles */
  227. unlink(ps_mask_file);
  228. if (PS.plfile)
  229. unlink(PS.plfile);
  230. if (PS.commentfile)
  231. unlink(PS.commentfile);
  232. /* if(sessionfile) unlink(sessionfile); created in session.c (how to remove?) */
  233. if (labels.other)
  234. unlink(labels.other);
  235. exit(EXIT_SUCCESS);
  236. }