main.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: d.text
  5. *
  6. * AUTHOR(S): James Westervelt, US Army CERL
  7. *
  8. * PURPOSE: display text in active frame
  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. /*
  18. * d.text
  19. *
  20. * Draw text in a text window. Text lines come from stdin.
  21. * R_text control:
  22. * .C color_name change color
  23. * .S size change text size
  24. * .B 0 bold (double print) off
  25. * .B 1 bold (double print) on
  26. * .F name change font to name
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <grass/gis.h>
  33. #include <grass/display.h>
  34. #include <grass/raster.h>
  35. #include <grass/colors.h>
  36. #include <grass/glocale.h>
  37. int
  38. main (int argc, char **argv)
  39. {
  40. char *cmd_ptr, *max_buff ;
  41. char buff[512] ;
  42. char window_name[64] ;
  43. float size ;
  44. int bold ;
  45. int R, G, B, color = 0;
  46. int cur_dot_row, cur_dot_column ;
  47. int dots_per_line ;
  48. int start_line ;
  49. int t, b, l, r ;
  50. int tsize ;
  51. double x,y ;
  52. double rotation;
  53. struct GModule *module;
  54. struct Option *opt1, *opt2, *opt3, *opt4, *opt5;
  55. struct Flag *flag_b;
  56. char *wind_file_name;
  57. FILE *wind_file;
  58. /* Initialize the GIS calls */
  59. G_gisinit(argv[0]) ;
  60. module = G_define_module();
  61. module->keywords = _("display");
  62. module->description =
  63. _("Draws text in the active display frame on the graphics monitor using the current font.");
  64. opt1 = G_define_option() ;
  65. opt1->key = "size" ;
  66. opt1->type = TYPE_DOUBLE ;
  67. opt1->required = NO ;
  68. opt1->answer = "5" ;
  69. opt1->options = "0-100";
  70. opt1->description=_("Height of letters in percentage of available frame height");
  71. opt2 = G_define_option() ;
  72. opt2->key = "color" ;
  73. opt2->type = TYPE_STRING ;
  74. opt2->answer = "gray" ;
  75. opt2->required = NO ;
  76. opt2->description=
  77. _("Text color, either a standard GRASS color or R:G:B triplet");
  78. opt2->gisprompt = GISPROMPT_COLOR;
  79. opt3 = G_define_option() ;
  80. opt3->key = "line" ;
  81. opt3->required = NO ;
  82. opt3->type = TYPE_INTEGER ;
  83. opt3->options = "1-1000" ;
  84. opt3->description=
  85. _("The screen line number on which text will begin to be drawn");
  86. opt4 = G_define_option() ;
  87. opt4->key = "at" ;
  88. opt4->key_desc = "x,y";
  89. opt4->type = TYPE_DOUBLE ;
  90. opt4->options = "0-100";
  91. opt4->required = NO ;
  92. opt4->description=
  93. _("Screen position at which text will begin to be drawn (percentage, [0,0] is lower left)");
  94. opt5 = G_define_option();
  95. opt5->key = "rotation";
  96. opt5->type = TYPE_DOUBLE;
  97. opt5->required = NO;
  98. opt5->answer = "0";
  99. opt5->description = _("Rotation angle in degrees (counter-clockwise)");
  100. flag_b = G_define_flag();
  101. flag_b->key = 'b';
  102. flag_b->description = _("Use bold text");
  103. /* Check command line */
  104. if (G_parser(argc, argv))
  105. exit(1);
  106. if(opt3->answer && opt4->answer)
  107. G_fatal_error(_("Please choose only one placement method"));
  108. if (isatty(0))
  109. fprintf (stdout,"\nPlease enter text instructions. Enter EOF (ctrl-d) on last line to quit\n");
  110. sscanf(opt1->answer,"%f",&size);
  111. if (R_open_driver() != 0)
  112. G_fatal_error (_("No graphics device selected"));
  113. /* Parse and select text color */
  114. if(sscanf(opt2->answer, "%d:%d:%d", &R, &G, &B) == 3) {
  115. if (R>=0 && R<256 && G>=0 && G<256 && B>=0 && B<256) {
  116. R_RGB_color(R, G, B);
  117. color = 1; /* to make success test below happy */
  118. }
  119. }
  120. else {
  121. color = D_translate_color(opt2->answer);
  122. R_standard_color(color);
  123. }
  124. if(!color)
  125. G_fatal_error(_("[%s]: No such color"), opt2->answer);
  126. rotation = atof(opt5->answer);
  127. if (D_get_cur_wind(window_name))
  128. G_fatal_error(_("No current window")) ;
  129. if (D_set_cur_wind(window_name))
  130. G_fatal_error(_("Current window not available")) ;
  131. /* Figure out where to put text */
  132. D_get_screen_window(&t, &b, &l, &r) ;
  133. R_set_window(t, b, l, r) ;
  134. dots_per_line = (int)(size/100.0 * (float)(b - t)) ;
  135. tsize = (int)(.8 * (float)dots_per_line) ;
  136. if(!opt4->answer) {
  137. if(opt3->answer)
  138. sscanf(opt3->answer,"%d",&start_line);
  139. else start_line = 1;
  140. cur_dot_row = t + (start_line - 1) * dots_per_line;
  141. cur_dot_column = l+5;
  142. }
  143. else {
  144. x = atof(opt4->answers[0]);
  145. y = atof(opt4->answers[1]);
  146. if(x<0 || x>100 || y<0 || y>100)
  147. G_fatal_error(_("value [%.0f,%.0f] out of range [0-100]"), x, y);
  148. cur_dot_row = t+(int)((b-t)*(100.-y)/100.);
  149. cur_dot_column = l+(int)((r-l)*x/100.);
  150. }
  151. R_text_size(tsize, tsize) ;
  152. R_text_rotation((float)rotation);
  153. if(flag_b->answer)
  154. bold = 1;
  155. else
  156. bold = 0 ;
  157. wind_file_name = G_tempfile();
  158. if ((wind_file=fopen(wind_file_name,"w")) == NULL)
  159. G_fatal_error(_("Unable to open temporary file <%s>"),wind_file_name);
  160. /* Do the plotting */
  161. while (fgets(buff,512,stdin))
  162. {
  163. fprintf(wind_file, "%s", buff);
  164. if (*buff == '.')
  165. {
  166. for(cmd_ptr=buff+2; *cmd_ptr==' '; cmd_ptr++) ;
  167. G_squeeze(cmd_ptr); /* added 6/91 DBS @ CWU */
  168. switch (buff[1] & 0x7F)
  169. {
  170. case 'F': /* font */
  171. R_font(cmd_ptr) ;
  172. break ;
  173. case 'C': /* color */
  174. if(sscanf(cmd_ptr, "%d:%d:%d", &R, &G, &B) == 3) {
  175. if (R>=0 && R<256 && G>=0 && G<256 && B>=0 && B<256) {
  176. R_RGB_color(R, G, B);
  177. }
  178. }
  179. else
  180. if (color = D_translate_color(cmd_ptr))
  181. R_standard_color(color) ;
  182. break ;
  183. case 'S': /* size */
  184. if (sscanf(cmd_ptr, "%f", &size))
  185. {
  186. dots_per_line = (int)(size/100.0 * (float)(b - t)) ;
  187. tsize = (int)(.8 * (float)dots_per_line) ;
  188. R_text_size(tsize, tsize) ;
  189. }
  190. break ;
  191. case 'B': /* bold */
  192. if (! sscanf(cmd_ptr, "%d", &bold))
  193. bold = 0 ;
  194. break ;
  195. case 'R': /* rotation */
  196. if (sscanf(cmd_ptr, "%lf", &rotation))
  197. {
  198. R_text_rotation((float)rotation);
  199. }
  200. break ;
  201. default:
  202. break ;
  203. }
  204. }
  205. else
  206. {
  207. if(!opt4->answer)
  208. cur_dot_row += dots_per_line ;
  209. R_move_abs(cur_dot_column, cur_dot_row) ;
  210. R_text(buff) ;
  211. if (bold)
  212. {
  213. R_move_abs(cur_dot_column, 1 + cur_dot_row) ;
  214. R_text(buff) ;
  215. R_move_abs(cur_dot_column + 1, cur_dot_row) ;
  216. R_text(buff) ;
  217. }
  218. }
  219. }
  220. R_text_size(5, 5) ;
  221. fclose(wind_file);
  222. max_buff = G_malloc(strlen(wind_file_name)+strlen(G_recreate_command())+4);
  223. sprintf(max_buff, "%s < %s", G_recreate_command(), wind_file_name);
  224. D_add_to_list(max_buff);
  225. G_free(max_buff);
  226. R_text_rotation(0.0); /* reset */
  227. R_close_driver();
  228. exit(0);
  229. }