main.c 16 KB

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