main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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(_("printing"));
  80. module->description = _("Produces hardcopy PostScript map output.");
  81. rflag = G_define_flag();
  82. rflag->key = 'r';
  83. rflag->description = _("Rotate plot 90 degrees");
  84. rflag->guisection = _("Output settings");
  85. pflag = G_define_flag();
  86. pflag->key = 'p';
  87. pflag->description =
  88. _("List paper formats (name width height left right top bottom(margin))");
  89. pflag->suppress_required = YES;
  90. pflag->guisection = _("Utility");
  91. eflag = G_define_flag();
  92. eflag->key = 'e';
  93. eflag->description =
  94. _("Create EPS (Encapsulated PostScript) instead of PostScript file");
  95. eflag->guisection = _("Output settings");
  96. bflag = G_define_flag();
  97. bflag->key = 'b';
  98. bflag->description =
  99. _("Describe map-box's position on the page and exit (inches from top-left of paper)");
  100. bflag->suppress_required = YES;
  101. bflag->guisection = _("Utility");
  102. input_file = G_define_standard_option(G_OPT_F_INPUT);
  103. input_file->label = _("File containing mapping instructions");
  104. input_file->description = _("Use '-' to enter instructions from keyboard)");
  105. output_file = G_define_standard_option(G_OPT_F_OUTPUT);
  106. output_file->description = _("Name for PostScript output file");
  107. copies = G_define_option();
  108. copies->key = "copies";
  109. copies->type = TYPE_INTEGER;
  110. copies->options = "1-20";
  111. copies->description = _("Number of copies to print");
  112. copies->required = NO;
  113. copies->guisection = _("Output settings");
  114. if (G_parser(argc, argv))
  115. exit(EXIT_FAILURE);
  116. /* PS.map_* variables are set to 0 (not defined) and then may be
  117. * reset by 'maploc'. When script is read, main() should call
  118. * reset_map_location() to reset map size to fit to paper */
  119. G_zero(&PS, sizeof(struct PS_data));
  120. /* Print paper sizes to stdout */
  121. if (pflag->answer) {
  122. print_papers();
  123. exit(EXIT_SUCCESS);
  124. }
  125. rotate_plot = rflag->answer;
  126. eps_output = eflag->answer;
  127. /* set default paper */
  128. set_paper("a4");
  129. strcpy(buf, "black");
  130. BLACK = get_color_number(buf);
  131. strcpy(buf, "white");
  132. WHITE = get_color_number(buf);
  133. strcpy(buf, "grey");
  134. GREY = get_color_number(buf);
  135. /* initialize */
  136. vector_init();
  137. copies_set = 0;
  138. m_info.x = m_info.y = -1.0;
  139. vector.x = vector.y = -1.0;
  140. ct.x = ct.y = -1.0;
  141. ct.width = -1.0;
  142. cmt.color = BLACK;
  143. m_info.font = G_store(def_font);
  144. vector.font = G_store(def_font);
  145. hdr.font = G_store(def_font);
  146. cmt.font = G_store(def_font);
  147. ct.font = G_store(def_font);
  148. m_info.fontsize = 10;
  149. vector.fontsize = 10;
  150. hdr.fontsize = 10;
  151. cmt.fontsize = 10;
  152. ct.fontsize = 10;
  153. ct.cols = 1;
  154. tracefd = NULL;
  155. inputfd = NULL;
  156. labels.count = 0;
  157. labels.other = NULL;
  158. can_reset_scale = 1;
  159. hdr.fp = NULL;
  160. grp.do_group = 0;
  161. brd.r = brd.g = brd.b = 0.;
  162. brd.width = 1.;
  163. PS.min_y = 72.0 * (PS.page_height - PS.top_marg);
  164. PS.set_y = 100.0 * PS.min_y;
  165. PS.cell_fd = -1;
  166. PS.do_border = TRUE;
  167. /* arguments */
  168. if (input_file->answer) {
  169. if (strcmp(input_file->answer, "-")) {
  170. inputfd = fopen(input_file->answer, "r");
  171. if (!inputfd)
  172. G_fatal_error(_("Unable to open file '%s': %s"),
  173. input_file->answer, strerror(errno));
  174. }
  175. else {
  176. inputfd = stdin;
  177. }
  178. }
  179. else {
  180. G_fatal_error(_("Required parameter <%s> not set:\n\t(%s)"),
  181. input_file->key, input_file->label);
  182. }
  183. if (copies->answer) {
  184. if (sscanf(copies->answer, "%d", &ps_copies) != 1) {
  185. ps_copies = 1;
  186. error(copies->answer, "", _("illegal copies request"));
  187. }
  188. copies_set = 1;
  189. }
  190. if (!bflag->answer) {
  191. if (output_file->answer) {
  192. if ((PS.fp = fopen(output_file->answer, "w")) == NULL)
  193. G_fatal_error("Unable to create file '%s': %s",
  194. output_file->answer, strerror(errno));
  195. }
  196. else {
  197. G_fatal_error(_("Required parameter <%s> not set:\n\t(%s)"),
  198. output_file->key, output_file->description);
  199. }
  200. }
  201. else
  202. PS.fp = NULL;
  203. /* get current mapset */
  204. PS.cell_mapset = G_mapset();
  205. /* get current window */
  206. Rast_get_window(&PS.w);
  207. read_instructions(copies_set, can_reset_scale);
  208. /* reset map location base on 'paper' on 'location' */
  209. reset_map_location();
  210. if (bflag->answer) {
  211. map_setup();
  212. fprintf(stdout, "bbox=%.3f,%.3f,%.3f,%.3f\n", PS.map_left / 72.0,
  213. PS.page_height - (PS.map_bot / 72.0), PS.map_right / 72.0,
  214. PS.page_height - (PS.map_top / 72.0));
  215. /* +/- 0.5 ? see ps.map.c brd.* */
  216. exit(EXIT_SUCCESS);
  217. }
  218. /* write the PostScript output file */
  219. ps_mask_file = G_tempfile();
  220. ps_map();
  221. /* cleanup the tempfiles */
  222. unlink(ps_mask_file);
  223. if (PS.plfile)
  224. unlink(PS.plfile);
  225. if (PS.commentfile)
  226. unlink(PS.commentfile);
  227. /* if(sessionfile) unlink(sessionfile); created in session.c (how to remove?) */
  228. if (labels.other)
  229. unlink(labels.other);
  230. G_done_msg(_("PostScript file '%s' successfully written."),
  231. output_file->answer);
  232. exit(EXIT_SUCCESS);
  233. }