main.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: d.text
  5. *
  6. * AUTHOR(S): James Westervelt, US Army CERL
  7. * Updated by Huidae Cho, Markus Neteler
  8. *
  9. * PURPOSE: display text in active frame
  10. *
  11. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. /*
  19. * d.text commands:
  20. *
  21. * .F {font|path}[:charset] font
  22. * .C {color_name|RR:GG:BB|0xRRGGBB} color
  23. * .G {color_name|RR:GG:BB|0xRRGGBB} background color
  24. * .S [+|-]size[p] text size
  25. * +/-: relative size
  26. * p: size in pixels
  27. * .B {0|1} bold (double print) off/on
  28. * .A {ll|lc|lr|cl|cc|cr|ul|uc|ur} align (TODO)
  29. * .R [+|-]rotation[r] rotation
  30. * +/-: relative rotation
  31. * r: angle in radian
  32. * .I linespacing linespacing
  33. * .X [+|-]x[%|p] x: relative to x origin
  34. * +/-: relative dx
  35. * %: percentage
  36. * p: pixels
  37. * .Y [+|-]y[%|p] y: relative to y origin
  38. * +/-: relative dy
  39. * %: percentage
  40. * p: pixels
  41. * .L {0|1} linefeed off/on
  42. * .E [+|-]east[%|p] x origin: geographic coordinates
  43. * +/-: relative de
  44. * %: percentage
  45. * p: pixels
  46. * .N [+|-]north[%|p] y origin: geographic coordinates
  47. * +/-: relative dn
  48. * %: percentage
  49. * p: pixels
  50. * .. draw one dot (.)
  51. * .<SPACE> comments
  52. */
  53. #include <math.h>
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <unistd.h>
  58. #include <grass/gis.h>
  59. #include <grass/display.h>
  60. #include <grass/colors.h>
  61. #include <grass/glocale.h>
  62. #define BACKWARD_COMPATIBILITY
  63. #define DEFAULT_COLOR "gray"
  64. struct rectinfo
  65. {
  66. double t, b, l, r;
  67. };
  68. static void set_color(char *);
  69. static int get_coordinates(double *, double *, double *, double *,
  70. struct rectinfo, char **, char, char);
  71. static void draw_text(char *, double *, double *, double, char *, double, char, int, int, int);
  72. int main(int argc, char **argv)
  73. {
  74. struct GModule *module;
  75. struct
  76. {
  77. struct Option *text;
  78. struct Option *size;
  79. struct Option *fgcolor;
  80. struct Option *bgcolor;
  81. struct Option *line;
  82. struct Option *at;
  83. struct Option *rotation;
  84. struct Option *align;
  85. struct Option *linespacing;
  86. struct Option *font;
  87. struct Option *path;
  88. struct Option *charset;
  89. struct Option *input;
  90. } opt;
  91. struct
  92. {
  93. struct Flag *p;
  94. struct Flag *g;
  95. struct Flag *b;
  96. struct Flag *r;
  97. struct Flag *s;
  98. } flag;
  99. /* options and flags */
  100. char *text;
  101. double size;
  102. double x, y;
  103. int line;
  104. double rotation;
  105. char align[3];
  106. double linespacing;
  107. char bold;
  108. /* window info */
  109. struct rectinfo win;
  110. /* command file */
  111. FILE *cmd_fp;
  112. char buf[512];
  113. int first_text;
  114. int linefeed;
  115. int set_l;
  116. double orig_x, orig_y;
  117. double prev_x, prev_y;
  118. double set_x, set_y;
  119. double east, north;
  120. int do_background, fg_color, bg_color;
  121. /* initialize the GIS calls */
  122. G_gisinit(argv[0]);
  123. module = G_define_module();
  124. G_add_keyword(_("display"));
  125. G_add_keyword(_("cartography"));
  126. module->description =
  127. _("Draws text in the active display frame on the graphics monitor using the current font.");
  128. opt.text = G_define_option();
  129. opt.text->key = "text";
  130. opt.text->type = TYPE_STRING;
  131. opt.text->required = NO;
  132. opt.text->description = _("Text to display");
  133. opt.size = G_define_option();
  134. opt.size->key = "size";
  135. opt.size->type = TYPE_DOUBLE;
  136. opt.size->required = NO;
  137. opt.size->answer = "5";
  138. opt.size->options = "0-100";
  139. opt.size->description =
  140. _("Height of letters in percentage of available frame height");
  141. opt.fgcolor = G_define_option();
  142. opt.fgcolor->key = "color";
  143. opt.fgcolor->type = TYPE_STRING;
  144. opt.fgcolor->answer = DEFAULT_COLOR;
  145. opt.fgcolor->required = NO;
  146. opt.fgcolor->description =
  147. _("Text color, either a standard GRASS color or R:G:B triplet");
  148. opt.fgcolor->gisprompt = "old_color,color,color";
  149. opt.bgcolor = G_define_option();
  150. opt.bgcolor->key = "bgcolor";
  151. opt.bgcolor->type = TYPE_STRING;
  152. opt.bgcolor->required = NO;
  153. opt.bgcolor->description =
  154. _("Text background color, either a standard GRASS color or R:G:B triplet");
  155. opt.bgcolor->gisprompt = "old_color,color,color";
  156. opt.line = G_define_option();
  157. opt.line->key = "line";
  158. opt.line->required = NO;
  159. opt.line->type = TYPE_INTEGER;
  160. opt.line->options = "1-1000";
  161. opt.line->description =
  162. _("The screen line number on which text will begin to be drawn");
  163. opt.at = G_define_option();
  164. opt.at->key = "at";
  165. opt.at->key_desc = "x,y";
  166. opt.at->type = TYPE_DOUBLE;
  167. opt.at->required = NO;
  168. opt.at->description =
  169. _("Screen position at which text will begin to be drawn (percentage, [0,0] is lower left)");
  170. opt.align = G_define_option();
  171. opt.align->key = "align";
  172. opt.align->type = TYPE_STRING;
  173. opt.align->required = NO;
  174. opt.align->answer = "ll";
  175. opt.align->options = "ll,lc,lr,cl,cc,cr,ul,uc,ur";
  176. opt.align->description = _("Text alignment");
  177. opt.rotation = G_define_option();
  178. opt.rotation->key = "rotation";
  179. opt.rotation->type = TYPE_DOUBLE;
  180. opt.rotation->required = NO;
  181. opt.rotation->answer = "0";
  182. opt.rotation->description =
  183. _("Rotation angle in degrees (counter-clockwise)");
  184. opt.linespacing = G_define_option();
  185. opt.linespacing->key = "linespacing";
  186. opt.linespacing->type = TYPE_DOUBLE;
  187. opt.linespacing->required = NO;
  188. opt.linespacing->answer = "1.25";
  189. opt.linespacing->description = _("Line spacing");
  190. opt.font = G_define_option();
  191. opt.font->key = "font";
  192. opt.font->type = TYPE_STRING;
  193. opt.font->required = NO;
  194. opt.font->description = _("Font name");
  195. opt.path = G_define_standard_option(G_OPT_F_INPUT);
  196. opt.path->key = "path";
  197. opt.path->required = NO;
  198. opt.path->description = _("Path to font file");
  199. opt.path->gisprompt = "old,font,file";
  200. opt.charset = G_define_option();
  201. opt.charset->key = "charset";
  202. opt.charset->type = TYPE_STRING;
  203. opt.charset->required = NO;
  204. opt.charset->description =
  205. _("Text encoding (only applicable to TrueType fonts)");
  206. opt.input = G_define_standard_option(G_OPT_F_INPUT);
  207. opt.input->required = NO;
  208. opt.input->description = _("Input file");
  209. flag.p = G_define_flag();
  210. flag.p->key = 'p';
  211. flag.p->description = _("Screen position in pixels ([0,0] is top left)");
  212. flag.g = G_define_flag();
  213. flag.g->key = 'g';
  214. flag.g->description = _("Screen position in geographic coordinates");
  215. flag.b = G_define_flag();
  216. flag.b->key = 'b';
  217. flag.b->description = _("Use bold text");
  218. flag.r = G_define_flag();
  219. flag.r->key = 'r';
  220. flag.r->description = _("Use radians instead of degrees for rotation");
  221. flag.s = G_define_flag();
  222. flag.s->key = 's';
  223. flag.s->description = _("Font size is height in pixels");
  224. /* check command line */
  225. if (G_parser(argc, argv))
  226. exit(1);
  227. /* parse and check options and flags */
  228. if ((opt.line->answer && opt.at->answer) ||
  229. (flag.p->answer && flag.g->answer))
  230. G_fatal_error(_("Please choose only one placement method"));
  231. text = opt.text->answer;
  232. line = (opt.line->answer ? atoi(opt.line->answer) : 1);
  233. /* calculate rotation angle in radian */
  234. rotation = atof(opt.rotation->answer);
  235. if (!flag.r->answer)
  236. rotation *= M_PI / 180.0;
  237. rotation = fmod(rotation, 2.0 * M_PI);
  238. if (rotation < 0.0)
  239. rotation += 2.0 * M_PI;
  240. strncpy(align, opt.align->answer, 2);
  241. linespacing = atof(opt.linespacing->answer);
  242. bold = flag.b->answer;
  243. if (D_open_driver() != 0)
  244. G_fatal_error(_("No graphics device selected. "
  245. "Use d.mon to select graphics device."));
  246. if (opt.font->answer)
  247. D_font(opt.font->answer);
  248. else if (opt.path->answer)
  249. D_font(opt.path->answer);
  250. if (opt.charset->answer)
  251. D_encoding(opt.charset->answer);
  252. D_setup_unity(0);
  253. /* figure out where to put text */
  254. D_get_src(&win.t, &win.b, &win.l, &win.r);
  255. if (flag.s->answer)
  256. size = atof(opt.size->answer);
  257. else
  258. #ifdef BACKWARD_COMPATIBILITY
  259. size = atof(opt.size->answer) / 100.0 * (win.b - win.t) / linespacing;
  260. #else
  261. size = atof(opt.size->answer) / 100.0 * (win.b - win.t);
  262. #endif
  263. fg_color = D_parse_color(opt.fgcolor->answer, TRUE);
  264. if (opt.bgcolor->answer) {
  265. do_background = 1;
  266. bg_color = D_parse_color(opt.bgcolor->answer, TRUE);
  267. if (bg_color == 0) /* ie color="none" */
  268. do_background = 0;
  269. } else
  270. do_background = 0;
  271. set_color(opt.fgcolor->answer);
  272. orig_x = orig_y = 0;
  273. if (opt.at->answer) {
  274. if (get_coordinates(&x, &y, &east, &north,
  275. win, opt.at->answers,
  276. flag.p->answer, flag.g->answer))
  277. G_fatal_error(_("Invalid coordinates"));
  278. orig_x = x;
  279. orig_y = y;
  280. }
  281. else {
  282. x = win.l + (size * linespacing + 0.5) - size; /* d.text: +5 */
  283. y = win.t + line * (size * linespacing + 0.5);
  284. }
  285. prev_x = x;
  286. prev_y = y;
  287. D_text_size(size, size);
  288. D_text_rotation(rotation * 180.0 / M_PI);
  289. if (text) {
  290. double x2, y2;
  291. x2 = x;
  292. y2 = y;
  293. if (text[0])
  294. draw_text(text, &x2, &y2, size, align, rotation, bold, do_background, fg_color, bg_color);
  295. /* reset */
  296. D_text_size(5, 5);
  297. D_text_rotation(0.0);
  298. D_save_command(G_recreate_command());
  299. D_close_driver();
  300. exit(EXIT_SUCCESS);
  301. }
  302. if (!opt.input->answer || strcmp(opt.input->answer, "-") == 0)
  303. cmd_fp = stdin;
  304. else {
  305. cmd_fp = fopen(opt.input->answer, "r");
  306. if (!cmd_fp)
  307. G_fatal_error(_("Unable to open input file <%s>"), opt.input->answer);
  308. }
  309. if (isatty(fileno(cmd_fp)))
  310. fprintf(stderr,
  311. _("\nPlease enter text instructions. Enter EOF (ctrl-d) on last line to quit\n"));
  312. set_x = set_y = set_l = 0;
  313. first_text = 1;
  314. linefeed = 1;
  315. /* do the plotting */
  316. while (fgets(buf, sizeof(buf), cmd_fp)) {
  317. int buf_len;
  318. char *buf_ptr, *ptr;
  319. buf_len = strlen(buf) - 1;
  320. for (; buf[buf_len] == '\r' || buf[buf_len] == '\n'; buf_len--) ;
  321. buf[buf_len + 1] = 0;
  322. if (buf[0] == '.' && buf[1] != '.') {
  323. int i;
  324. double d;
  325. G_squeeze(buf); /* added 6/91 DBS @ CWU */
  326. for (buf_ptr = buf + 2; *buf_ptr == ' '; buf_ptr++) ;
  327. buf_len = strlen(buf_ptr);
  328. switch (buf[1] & 0x7f) {
  329. case 'F':
  330. /* font */
  331. if ((ptr = strchr(buf_ptr, ':')))
  332. *ptr = 0;
  333. D_font(buf_ptr);
  334. if (ptr)
  335. D_encoding(ptr + 1);
  336. break;
  337. case 'C':
  338. /* color */
  339. set_color(buf_ptr);
  340. fg_color = D_parse_color(buf_ptr, 1);
  341. break;
  342. case 'G':
  343. /* background color */
  344. bg_color = D_parse_color(buf_ptr, 1);
  345. do_background = 1;
  346. break;
  347. case 'S':
  348. /* size */
  349. i = 0;
  350. if (strchr("+-", buf_ptr[0]))
  351. i = 1;
  352. d = atof(buf_ptr);
  353. if (buf_ptr[buf_len - 1] != 'p')
  354. #ifdef BACKWARD_COMPATIBILITY
  355. d *= (win.b - win.t) / 100.0 / linespacing;
  356. #else
  357. d *= (win.b - win.t) / 100.0;
  358. #endif
  359. size = d + (i ? size : 0);
  360. D_text_size(size, size);
  361. break;
  362. case 'B':
  363. /* bold */
  364. bold = (atoi(buf_ptr) ? 1 : 0);
  365. break;
  366. case 'A':
  367. /* align */
  368. strncpy(align, buf_ptr, 2);
  369. break;
  370. case 'R':
  371. /* rotation */
  372. i = 0;
  373. if (strchr("+-", buf_ptr[0]))
  374. i = 1;
  375. d = atof(buf_ptr);
  376. if (buf_ptr[buf_len - 1] != 'r')
  377. d *= M_PI / 180.0;
  378. d += (i ? rotation : 0.0);
  379. rotation = fmod(d, 2.0 * M_PI);
  380. if (rotation < 0.0)
  381. rotation += 2.0 * M_PI;
  382. D_text_rotation(rotation * 180.0 / M_PI);
  383. break;
  384. case 'I':
  385. /* linespacing */
  386. linespacing = atof(buf_ptr);
  387. break;
  388. case 'X':
  389. /* x */
  390. set_l = 0;
  391. set_x = 1;
  392. i = 0;
  393. if (strchr("+-", buf_ptr[0]))
  394. i = 1;
  395. d = atof(buf_ptr);
  396. if (buf_ptr[buf_len - 1] == '%')
  397. /* percentage */
  398. d *= (win.r - win.l) / 100.0;
  399. else if (buf_ptr[buf_len - 1] != 'p')
  400. /* column */
  401. d = (d - 1) * size * linespacing + 0.5;
  402. x = prev_x = d + (i ? x : orig_x);
  403. break;
  404. case 'Y':
  405. /* y */
  406. set_l = 0;
  407. set_y = 1;
  408. i = 0;
  409. if (strchr("+-", buf_ptr[0]))
  410. i = 1;
  411. d = atof(buf_ptr);
  412. if (buf_ptr[buf_len - 1] == '%')
  413. /* percentage */
  414. d = win.b - d * (win.b - win.t) / 100.0;
  415. else if (buf_ptr[buf_len - 1] != 'p')
  416. /* row */
  417. d *= size * linespacing + 0.5;
  418. y = prev_y = d + (i ? y : orig_y);
  419. break;
  420. case 'L':
  421. /* linefeed */
  422. set_l = 1;
  423. linefeed = (atoi(buf_ptr) ? 1 : 0);
  424. break;
  425. case 'E':
  426. i = 0;
  427. if (strchr("+-", buf_ptr[0]))
  428. i = 1;
  429. d = atof(buf_ptr);
  430. if (buf_ptr[buf_len - 1] == '%')
  431. d *= (win.r - win.l) / 100.0;
  432. else if (buf_ptr[buf_len - 1] != 'p')
  433. d = D_u_to_d_col(d);
  434. x = prev_x = orig_x = d + (i ? orig_x : win.l);
  435. break;
  436. case 'N':
  437. i = 0;
  438. if (strchr("+-", buf_ptr[0]))
  439. i = 1;
  440. d = atof(buf_ptr);
  441. if (buf_ptr[buf_len - 1] == '%')
  442. d *= (win.b - win.t) / 100.0;
  443. else if (buf_ptr[buf_len - 1] != 'p')
  444. d = D_u_to_d_row(d);
  445. y = prev_y = orig_y = d + (i ? orig_y : win.t);
  446. break;
  447. }
  448. }
  449. else {
  450. buf_ptr = buf;
  451. if (buf[0] == '.' && buf[1] == '.')
  452. buf_ptr++;
  453. if (!first_text && (linefeed || set_l)) {
  454. /* if x is not given, increment x */
  455. if (!set_x)
  456. x = prev_x +
  457. (size * linespacing + 0.5) * sin(rotation);
  458. /* if y is not given, increment y */
  459. if (!set_y)
  460. y = prev_y +
  461. (size * linespacing + 0.5) * cos(rotation);
  462. prev_x = x;
  463. prev_y = y;
  464. }
  465. set_x = set_y = set_l = first_text = 0;
  466. draw_text(buf_ptr, &x, &y, size, align, rotation, bold, do_background, fg_color, bg_color);
  467. }
  468. }
  469. if (cmd_fp != stdin)
  470. fclose(cmd_fp);
  471. /* reset */
  472. D_text_size(5, 5);
  473. D_text_rotation(0.0);
  474. D_close_driver();
  475. exit(EXIT_SUCCESS);
  476. }
  477. static void set_color(char *tcolor)
  478. {
  479. int r, g, b, color;
  480. if (sscanf(tcolor, "%d:%d:%d", &r, &g, &b) == 3 ||
  481. sscanf(tcolor, "0x%02x%02x%02x", &r, &g, &b) == 3) {
  482. if (r >= 0 && r < 256 && g >= 0 && g < 256 && b >= 0 && b < 256) {
  483. D_RGB_color(r, g, b);
  484. }
  485. }
  486. else {
  487. color = D_translate_color(tcolor);
  488. if (!color) {
  489. G_warning(_("[%s]: No such color. Use '%s'"), tcolor,
  490. DEFAULT_COLOR);
  491. color = D_translate_color(DEFAULT_COLOR);
  492. }
  493. D_use_color(color);
  494. }
  495. return;
  496. }
  497. static int
  498. get_coordinates(double *x, double *y, double *east, double *north,
  499. struct rectinfo win, char **at, char pixel,
  500. char geocoor)
  501. {
  502. double e, n;
  503. if (at) {
  504. e = atof(at[0]);
  505. n = atof(at[1]);
  506. if (pixel) {
  507. *x = e + win.l;
  508. *y = n + win.t;
  509. e = D_d_to_u_col(*x);
  510. n = D_d_to_u_row(*y);
  511. }
  512. else if (geocoor) {
  513. *x = D_u_to_d_col(e);
  514. *y = D_u_to_d_row(n);
  515. }
  516. else {
  517. *x = win.l + (win.r - win.l) * e / 100.0;
  518. *y = win.t + (win.b - win.t) * (100.0 - n) / 100.0;
  519. e = D_d_to_u_col(*x);
  520. n = D_d_to_u_row(*y);
  521. }
  522. }
  523. else
  524. return 1;
  525. if (east)
  526. *east = e;
  527. if (north)
  528. *north = n;
  529. return 0;
  530. }
  531. static void draw_text(char *text, double *x, double *y, double size, char *align,
  532. double rotation, char bold, int do_background, int fg_color, int bg_color)
  533. {
  534. double w, h;
  535. double t, b, l, r;
  536. double c, s;
  537. int pt, pb, pl, pr;
  538. /* TODO: get text dimension */
  539. /* R_get_text_box() does not work with rotation and returns a little bit
  540. * bigger dimension than actual text size */
  541. if (rotation != 0.0)
  542. D_text_rotation(0.0);
  543. D_get_text_box(text, &t, &b, &l, &r);
  544. if (rotation != 0.0)
  545. D_text_rotation(rotation * 180.0 / M_PI);
  546. w = r - l;
  547. h = b - t;
  548. if (w > 0)
  549. w += 0.2 * size;
  550. else
  551. /* D_text() does not draw " ". */
  552. w = 0.8 * size;
  553. if (h > 0)
  554. h += 0.2 * size;
  555. else
  556. /* D_text() does not draw " ". */
  557. h = 0.8 * size;
  558. c = cos(rotation);
  559. s = sin(rotation);
  560. if (strcmp(align, "ll") != 0) {
  561. switch (align[0]) {
  562. case 'l':
  563. break;
  564. case 'c':
  565. *x += h / 2.0 * s;
  566. *y += h / 2.0 * c;
  567. break;
  568. case 'u':
  569. *x += h * s;
  570. *y += h * c;
  571. break;
  572. }
  573. switch (align[1]) {
  574. case 'l':
  575. break;
  576. case 'c':
  577. *x -= w / 2.0 * c;
  578. *y += w / 2.0 * s;
  579. break;
  580. case 'r':
  581. *x -= w * c;
  582. *y += w * s;
  583. break;
  584. }
  585. }
  586. if (do_background) {
  587. pl = *x - size/2; /* some pixels margin for both sides */
  588. pt = *y + size/2;
  589. pr = *x + w + size/2;
  590. pb = *y - h - size/2;
  591. D_use_color(bg_color);
  592. D_box_abs(pl, pt, pr, pb); /* draw the box */
  593. D_use_color(fg_color); /* restore */
  594. }
  595. D_pos_abs(*x, *y);
  596. D_text(text);
  597. if (bold) {
  598. D_pos_abs(*x, *y + 1);
  599. D_text(text);
  600. D_pos_abs(*x + 1, *y);
  601. D_text(text);
  602. }
  603. *x += w * c;
  604. *y -= w * s;
  605. return;
  606. }