main.c 16 KB

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