main.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. D_open_driver();
  244. if (opt.font->answer)
  245. D_font(opt.font->answer);
  246. else if (opt.path->answer)
  247. D_font(opt.path->answer);
  248. if (opt.charset->answer)
  249. D_encoding(opt.charset->answer);
  250. D_setup_unity(0);
  251. /* figure out where to put text */
  252. D_get_src(&win.t, &win.b, &win.l, &win.r);
  253. if (flag.s->answer)
  254. size = atof(opt.size->answer);
  255. else
  256. #ifdef BACKWARD_COMPATIBILITY
  257. size = atof(opt.size->answer) / 100.0 * (win.b - win.t) / linespacing;
  258. #else
  259. size = atof(opt.size->answer) / 100.0 * (win.b - win.t);
  260. #endif
  261. fg_color = D_parse_color(opt.fgcolor->answer, TRUE);
  262. if (opt.bgcolor->answer) {
  263. do_background = 1;
  264. bg_color = D_parse_color(opt.bgcolor->answer, TRUE);
  265. if (bg_color == 0) /* ie color="none" */
  266. do_background = 0;
  267. } else
  268. do_background = 0;
  269. set_color(opt.fgcolor->answer);
  270. orig_x = orig_y = 0;
  271. if (opt.at->answer) {
  272. if (get_coordinates(&x, &y, &east, &north,
  273. win, opt.at->answers,
  274. flag.p->answer, flag.g->answer))
  275. G_fatal_error(_("Invalid coordinates"));
  276. orig_x = x;
  277. orig_y = y;
  278. }
  279. else {
  280. x = win.l + (size * linespacing + 0.5) - size; /* d.text: +5 */
  281. y = win.t + line * (size * linespacing + 0.5);
  282. }
  283. prev_x = x;
  284. prev_y = y;
  285. D_text_size(size, size);
  286. D_text_rotation(rotation * 180.0 / M_PI);
  287. if (text) {
  288. double x2, y2;
  289. x2 = x;
  290. y2 = y;
  291. if (text[0])
  292. draw_text(text, &x2, &y2, size, align, rotation, bold, do_background, fg_color, bg_color);
  293. /* reset */
  294. D_text_size(5, 5);
  295. D_text_rotation(0.0);
  296. D_save_command(G_recreate_command());
  297. D_close_driver();
  298. exit(EXIT_SUCCESS);
  299. }
  300. if (!opt.input->answer || strcmp(opt.input->answer, "-") == 0)
  301. cmd_fp = stdin;
  302. else {
  303. cmd_fp = fopen(opt.input->answer, "r");
  304. if (!cmd_fp)
  305. G_fatal_error(_("Unable to open input file <%s>"), opt.input->answer);
  306. }
  307. if (isatty(fileno(cmd_fp)))
  308. fprintf(stderr,
  309. _("\nPlease enter text instructions. Enter EOF (ctrl-d) on last line to quit\n"));
  310. set_x = set_y = set_l = 0;
  311. first_text = 1;
  312. linefeed = 1;
  313. /* do the plotting */
  314. while (fgets(buf, sizeof(buf), cmd_fp)) {
  315. int buf_len;
  316. char *buf_ptr, *ptr;
  317. buf_len = strlen(buf) - 1;
  318. for (; buf[buf_len] == '\r' || buf[buf_len] == '\n'; buf_len--) ;
  319. buf[buf_len + 1] = 0;
  320. if (buf[0] == '.' && buf[1] != '.') {
  321. int i;
  322. double d;
  323. G_squeeze(buf); /* added 6/91 DBS @ CWU */
  324. for (buf_ptr = buf + 2; *buf_ptr == ' '; buf_ptr++) ;
  325. buf_len = strlen(buf_ptr);
  326. switch (buf[1] & 0x7f) {
  327. case 'F':
  328. /* font */
  329. if ((ptr = strchr(buf_ptr, ':')))
  330. *ptr = 0;
  331. D_font(buf_ptr);
  332. if (ptr)
  333. D_encoding(ptr + 1);
  334. break;
  335. case 'C':
  336. /* color */
  337. set_color(buf_ptr);
  338. fg_color = D_parse_color(buf_ptr, 1);
  339. break;
  340. case 'G':
  341. /* background color */
  342. bg_color = D_parse_color(buf_ptr, 1);
  343. do_background = 1;
  344. break;
  345. case 'S':
  346. /* size */
  347. i = 0;
  348. if (strchr("+-", buf_ptr[0]))
  349. i = 1;
  350. d = atof(buf_ptr);
  351. if (buf_ptr[buf_len - 1] != 'p')
  352. #ifdef BACKWARD_COMPATIBILITY
  353. d *= (win.b - win.t) / 100.0 / linespacing;
  354. #else
  355. d *= (win.b - win.t) / 100.0;
  356. #endif
  357. size = d + (i ? size : 0);
  358. D_text_size(size, size);
  359. break;
  360. case 'B':
  361. /* bold */
  362. bold = (atoi(buf_ptr) ? 1 : 0);
  363. break;
  364. case 'A':
  365. /* align */
  366. strncpy(align, buf_ptr, 2);
  367. break;
  368. case 'R':
  369. /* rotation */
  370. i = 0;
  371. if (strchr("+-", buf_ptr[0]))
  372. i = 1;
  373. d = atof(buf_ptr);
  374. if (buf_ptr[buf_len - 1] != 'r')
  375. d *= M_PI / 180.0;
  376. d += (i ? rotation : 0.0);
  377. rotation = fmod(d, 2.0 * M_PI);
  378. if (rotation < 0.0)
  379. rotation += 2.0 * M_PI;
  380. D_text_rotation(rotation * 180.0 / M_PI);
  381. break;
  382. case 'I':
  383. /* linespacing */
  384. linespacing = atof(buf_ptr);
  385. break;
  386. case 'X':
  387. /* x */
  388. set_l = 0;
  389. set_x = 1;
  390. i = 0;
  391. if (strchr("+-", buf_ptr[0]))
  392. i = 1;
  393. d = atof(buf_ptr);
  394. if (buf_ptr[buf_len - 1] == '%')
  395. /* percentage */
  396. d *= (win.r - win.l) / 100.0;
  397. else if (buf_ptr[buf_len - 1] != 'p')
  398. /* column */
  399. d = (d - 1) * size * linespacing + 0.5;
  400. x = prev_x = d + (i ? x : orig_x);
  401. break;
  402. case 'Y':
  403. /* y */
  404. set_l = 0;
  405. set_y = 1;
  406. i = 0;
  407. if (strchr("+-", buf_ptr[0]))
  408. i = 1;
  409. d = atof(buf_ptr);
  410. if (buf_ptr[buf_len - 1] == '%')
  411. /* percentage */
  412. d = win.b - d * (win.b - win.t) / 100.0;
  413. else if (buf_ptr[buf_len - 1] != 'p')
  414. /* row */
  415. d *= size * linespacing + 0.5;
  416. y = prev_y = d + (i ? y : orig_y);
  417. break;
  418. case 'L':
  419. /* linefeed */
  420. set_l = 1;
  421. linefeed = (atoi(buf_ptr) ? 1 : 0);
  422. break;
  423. case 'E':
  424. i = 0;
  425. if (strchr("+-", buf_ptr[0]))
  426. i = 1;
  427. d = atof(buf_ptr);
  428. if (buf_ptr[buf_len - 1] == '%')
  429. d *= (win.r - win.l) / 100.0;
  430. else if (buf_ptr[buf_len - 1] != 'p')
  431. d = D_u_to_d_col(d);
  432. x = prev_x = orig_x = d + (i ? orig_x : win.l);
  433. break;
  434. case 'N':
  435. i = 0;
  436. if (strchr("+-", buf_ptr[0]))
  437. i = 1;
  438. d = atof(buf_ptr);
  439. if (buf_ptr[buf_len - 1] == '%')
  440. d *= (win.b - win.t) / 100.0;
  441. else if (buf_ptr[buf_len - 1] != 'p')
  442. d = D_u_to_d_row(d);
  443. y = prev_y = orig_y = d + (i ? orig_y : win.t);
  444. break;
  445. }
  446. }
  447. else {
  448. buf_ptr = buf;
  449. if (buf[0] == '.' && buf[1] == '.')
  450. buf_ptr++;
  451. if (!first_text && (linefeed || set_l)) {
  452. /* if x is not given, increment x */
  453. if (!set_x)
  454. x = prev_x +
  455. (size * linespacing + 0.5) * sin(rotation);
  456. /* if y is not given, increment y */
  457. if (!set_y)
  458. y = prev_y +
  459. (size * linespacing + 0.5) * cos(rotation);
  460. prev_x = x;
  461. prev_y = y;
  462. }
  463. set_x = set_y = set_l = first_text = 0;
  464. draw_text(buf_ptr, &x, &y, size, align, rotation, bold, do_background, fg_color, bg_color);
  465. }
  466. }
  467. if (cmd_fp != stdin)
  468. fclose(cmd_fp);
  469. /* reset */
  470. D_text_size(5, 5);
  471. D_text_rotation(0.0);
  472. D_close_driver();
  473. exit(EXIT_SUCCESS);
  474. }
  475. static void set_color(char *tcolor)
  476. {
  477. int r, g, b, color;
  478. if (sscanf(tcolor, "%d:%d:%d", &r, &g, &b) == 3 ||
  479. sscanf(tcolor, "0x%02x%02x%02x", &r, &g, &b) == 3) {
  480. if (r >= 0 && r < 256 && g >= 0 && g < 256 && b >= 0 && b < 256) {
  481. D_RGB_color(r, g, b);
  482. }
  483. }
  484. else {
  485. color = D_translate_color(tcolor);
  486. if (!color) {
  487. G_warning(_("[%s]: No such color. Use '%s'"), tcolor,
  488. DEFAULT_COLOR);
  489. color = D_translate_color(DEFAULT_COLOR);
  490. }
  491. D_use_color(color);
  492. }
  493. return;
  494. }
  495. static int
  496. get_coordinates(double *x, double *y, double *east, double *north,
  497. struct rectinfo win, char **at, char pixel,
  498. char geocoor)
  499. {
  500. double e, n;
  501. if (at) {
  502. e = atof(at[0]);
  503. n = atof(at[1]);
  504. if (pixel) {
  505. *x = e + win.l;
  506. *y = n + win.t;
  507. e = D_d_to_u_col(*x);
  508. n = D_d_to_u_row(*y);
  509. }
  510. else if (geocoor) {
  511. *x = D_u_to_d_col(e);
  512. *y = D_u_to_d_row(n);
  513. }
  514. else {
  515. *x = win.l + (win.r - win.l) * e / 100.0;
  516. *y = win.t + (win.b - win.t) * (100.0 - n) / 100.0;
  517. e = D_d_to_u_col(*x);
  518. n = D_d_to_u_row(*y);
  519. }
  520. }
  521. else
  522. return 1;
  523. if (east)
  524. *east = e;
  525. if (north)
  526. *north = n;
  527. return 0;
  528. }
  529. static void draw_text(char *text, double *x, double *y, double size, char *align,
  530. double rotation, char bold, int do_background, int fg_color, int bg_color)
  531. {
  532. double w, h;
  533. double t, b, l, r;
  534. double c, s;
  535. int pt, pb, pl, pr;
  536. /* TODO: get text dimension */
  537. /* R_get_text_box() does not work with rotation and returns a little bit
  538. * bigger dimension than actual text size */
  539. if (rotation != 0.0)
  540. D_text_rotation(0.0);
  541. D_get_text_box(text, &t, &b, &l, &r);
  542. if (rotation != 0.0)
  543. D_text_rotation(rotation * 180.0 / M_PI);
  544. w = r - l;
  545. h = b - t;
  546. if (w > 0)
  547. w += 0.2 * size;
  548. else
  549. /* D_text() does not draw " ". */
  550. w = 0.8 * size;
  551. if (h > 0)
  552. h += 0.2 * size;
  553. else
  554. /* D_text() does not draw " ". */
  555. h = 0.8 * size;
  556. c = cos(rotation);
  557. s = sin(rotation);
  558. if (strcmp(align, "ll") != 0) {
  559. switch (align[0]) {
  560. case 'l':
  561. break;
  562. case 'c':
  563. *x += h / 2.0 * s;
  564. *y += h / 2.0 * c;
  565. break;
  566. case 'u':
  567. *x += h * s;
  568. *y += h * c;
  569. break;
  570. }
  571. switch (align[1]) {
  572. case 'l':
  573. break;
  574. case 'c':
  575. *x -= w / 2.0 * c;
  576. *y += w / 2.0 * s;
  577. break;
  578. case 'r':
  579. *x -= w * c;
  580. *y += w * s;
  581. break;
  582. }
  583. }
  584. if (do_background) {
  585. pl = *x - size/2; /* some pixels margin for both sides */
  586. pt = *y + size/2;
  587. pr = *x + w + size/2;
  588. pb = *y - h - size/2;
  589. D_use_color(bg_color);
  590. D_box_abs(pl, pt, pr, pb); /* draw the box */
  591. D_use_color(fg_color); /* restore */
  592. }
  593. D_pos_abs(*x, *y);
  594. D_text(text);
  595. if (bold) {
  596. D_pos_abs(*x, *y + 1);
  597. D_text(text);
  598. D_pos_abs(*x + 1, *y);
  599. D_text(text);
  600. }
  601. *x += w * c;
  602. *y -= w * s;
  603. return;
  604. }