main.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: d.title
  5. *
  6. * AUTHOR(S): James Westervelt, US Army CERL
  7. *
  8. * PURPOSE: print out title for raster on stdout
  9. *
  10. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. *****************************************************************************/
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <grass/display.h>
  21. #include <grass/gis.h>
  22. #include <grass/raster.h>
  23. #include <grass/spawn.h>
  24. #include <grass/glocale.h>
  25. #include "options.h"
  26. #include "local_proto.h"
  27. const char *map_name;
  28. const char *color;
  29. float size;
  30. int type;
  31. int main(int argc, char **argv)
  32. {
  33. struct Cell_head window;
  34. struct Categories cats;
  35. struct GModule *module;
  36. struct Option *opt1, *opt2, *opt3;
  37. struct Flag *fancy_mode, *simple_mode, *draw;
  38. char *tmpfile;
  39. FILE *fp;
  40. /* Initialize the GIS calls */
  41. G_gisinit(argv[0]);
  42. module = G_define_module();
  43. G_add_keyword(_("display"));
  44. module->description =
  45. _("Create a TITLE for a raster map in a form suitable "
  46. "for display with d.text.");
  47. opt1 = G_define_standard_option(G_OPT_R_MAP);
  48. opt2 = G_define_option();
  49. opt2->key = "color";
  50. opt2->type = TYPE_STRING;
  51. opt2->answer = DEFAULT_FG_COLOR;
  52. opt2->required = NO;
  53. opt2->gisprompt = "old_color,color,color";
  54. opt2->description = _("Sets the text color");
  55. opt3 = G_define_option();
  56. opt3->key = "size";
  57. opt3->type = TYPE_DOUBLE;
  58. opt3->answer = "4.0";
  59. opt3->options = "0-100";
  60. opt3->description =
  61. _("Sets the text size as percentage of the frame's height");
  62. draw = G_define_flag();
  63. draw->key = 'd';
  64. draw->description = _("Draw title on current display");
  65. fancy_mode = G_define_flag();
  66. fancy_mode->key = 'f';
  67. fancy_mode->description = _("Do a fancier title");
  68. /* currently just title, but it doesn't have to be /that/ simple */
  69. simple_mode = G_define_flag();
  70. simple_mode->key = 's';
  71. simple_mode->description = _("Do a simple title");
  72. /* Check command line */
  73. if (G_parser(argc, argv))
  74. exit(EXIT_FAILURE);
  75. map_name = opt1->answer;
  76. color = opt2->answer;
  77. if (opt3->answer != NULL)
  78. sscanf(opt3->answer, "%f", &size);
  79. type = fancy_mode->answer ? FANCY : NORMAL;
  80. if (fancy_mode->answer && simple_mode->answer)
  81. G_fatal_error(_("Title can be fancy or simple, not both"));
  82. if (!strlen(map_name))
  83. G_fatal_error(_("No map name given"));
  84. Rast_get_cellhd(map_name, "", &window);
  85. if (Rast_read_cats(map_name, "", &cats) == -1)
  86. G_fatal_error(_("Unable to read category file of raster map <%s>"),
  87. map_name);
  88. if (draw->answer) {
  89. tmpfile = G_convert_dirseps_to_host(G_tempfile());
  90. if (!(fp = fopen(tmpfile, "w")))
  91. G_fatal_error(_("Unable to open temporary file <%s>"), tmpfile);
  92. }
  93. else
  94. fp = stdout;
  95. if (type == NORMAL)
  96. normal(&window, &cats, simple_mode->answer, fp);
  97. else
  98. fancy(&window, &cats, fp);
  99. if (draw->answer) {
  100. char inarg[GPATH_MAX];
  101. fclose(fp);
  102. sprintf(inarg, "input=%s", tmpfile);
  103. G_spawn("d.text", "d.text", inarg, NULL);
  104. unlink(tmpfile);
  105. /* note a tmp file will remain, created by d.text so it can survive d.redraw */
  106. }
  107. exit(EXIT_SUCCESS);
  108. }