r_text.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* Function: record_label
  2. **
  3. ** This is a modified version of the p.map function.
  4. **
  5. ** Modified by: Paul W. Carlson May 1992
  6. */
  7. #include <string.h>
  8. #include <grass/colors.h>
  9. #include <grass/raster.h>
  10. #include "clr.h"
  11. #include "labels.h"
  12. #include "local_proto.h"
  13. #define EQ(x,y) (strcmp(x,y)==0)
  14. #define KEY(x) EQ(key,x)
  15. #define LEFT 0
  16. #define RIGHT 1
  17. #define LOWER 0
  18. #define UPPER 1
  19. #define CENTER 2
  20. extern char *get_color_name();
  21. extern int get_color_number();
  22. static char *help[] = {
  23. "font fontname",
  24. "color color",
  25. "width #",
  26. "background color|none",
  27. "border color|none",
  28. "size #",
  29. "fontsize fontsize",
  30. "hcolor color|none",
  31. "hwidth #",
  32. "ref upper|lower|center left|right|center",
  33. "rotate deg CCW",
  34. "xoffset #",
  35. "yoffset #",
  36. "opaque [y|n]",
  37. ""
  38. };
  39. int read_text(char *east, char *north, char *text)
  40. {
  41. PSCOLOR color, hcolor, background, border;
  42. int r, g, b;
  43. int ret;
  44. int xoffset;
  45. int yoffset;
  46. float size;
  47. int fontsize;
  48. double width;
  49. double hwidth;
  50. double rotate;
  51. int xref, yref;
  52. int opaque;
  53. char t1[128];
  54. char buf[1024];
  55. char *key, *data;
  56. FILE *fd;
  57. char fontname[128];
  58. set_color(&color, 0, 0, 0); /* black */
  59. unset_color(&hcolor);
  60. unset_color(&background);
  61. unset_color(&border);
  62. opaque = TRUE;
  63. size = 0.0;
  64. fontsize = 0;
  65. xoffset = 0;
  66. yoffset = 0;
  67. width = 1.;
  68. hwidth = 0.;
  69. rotate = 0.0;
  70. xref = CENTER;
  71. yref = CENTER;
  72. strcpy(fontname, "Helvetica");
  73. while (*text == ' ' || *text == '\t')
  74. text++;
  75. if (*text == '\\')
  76. text++;
  77. if (*text == 0) {
  78. error("text", "", "no text given");
  79. gobble_input();
  80. return 0;
  81. }
  82. while (input(2, buf, help)) {
  83. if (!key_data(buf, &key, &data))
  84. continue;
  85. if (KEY("font")) {
  86. get_font(data);
  87. strcpy(fontname, data);
  88. continue;
  89. }
  90. if (KEY("color")) {
  91. ret = G_str_to_color(data, &r, &g, &b);
  92. if (ret == 1)
  93. set_color(&color, r, g, b);
  94. else if (ret == 2)
  95. error(key, data, "primary color cannot be \"none\"");
  96. else
  97. error(key, data, "illegal color request");
  98. continue;
  99. }
  100. if (KEY("hcolor")) {
  101. ret = G_str_to_color(data, &r, &g, &b);
  102. if (ret == 1)
  103. set_color(&hcolor, r, g, b);
  104. else if (ret == 2)
  105. unset_color(&hcolor);
  106. else
  107. error(key, data, "illegal hcolor request");
  108. if (color_none(&hcolor) || hwidth <= 0.)
  109. hwidth = 0.;
  110. continue;
  111. }
  112. if (KEY("background")) {
  113. ret = G_str_to_color(data, &r, &g, &b);
  114. if (ret == 1)
  115. set_color(&background, r, g, b);
  116. else if (ret == 2) {
  117. unset_color(&background);
  118. opaque = FALSE;
  119. }
  120. else
  121. error(key, data, "illegal background color request");
  122. continue;
  123. }
  124. if (KEY("border")) {
  125. ret = G_str_to_color(data, &r, &g, &b);
  126. if (ret == 1)
  127. set_color(&border, r, g, b);
  128. else if (ret == 2)
  129. unset_color(&border);
  130. else
  131. error(key, data, "illegal border color request");
  132. continue;
  133. }
  134. if (KEY("opaque")) {
  135. opaque = yesno(key, data);
  136. continue;
  137. }
  138. if (KEY("width")) {
  139. width = -1.;
  140. *t1 = 0;
  141. if (sscanf(data, "%lf%1s", &width, t1) < 1 || width < 0.) {
  142. width = 1.;
  143. error(key, data, "illegal width request");
  144. }
  145. if (t1[0] == 'i')
  146. width = width / 72.0;
  147. continue;
  148. }
  149. if (KEY("hwidth")) {
  150. hwidth = -1.;
  151. *t1 = 0;
  152. if (sscanf(data, "%lf%1s", &hwidth, t1) < 1 || hwidth < 0.) {
  153. hwidth = 0.;
  154. error(key, data, "illegal width request");
  155. }
  156. if (t1[0] == 'i')
  157. hwidth = hwidth / 72.0;
  158. continue;
  159. }
  160. if (KEY("size")) {
  161. double x;
  162. if (!scan_resolution(data, &x)) {
  163. size = 0.0;
  164. error(key, data, "illegal size request");
  165. }
  166. else
  167. size = x;
  168. continue;
  169. }
  170. if (KEY("fontsize")) {
  171. if (sscanf(data, "%d", &fontsize) != 1 || fontsize <= 0) {
  172. error(key, data, "illegal fontsize request");
  173. }
  174. else
  175. continue;
  176. }
  177. if (KEY("xoffset")) {
  178. *t1 = 0;
  179. if (sscanf(data, "%d%1s", &xoffset, t1) != 1 || *t1) {
  180. xoffset = 0;
  181. error(key, data, "illegal request (text)");
  182. }
  183. continue;
  184. }
  185. if (KEY("yoffset")) {
  186. *t1 = 0;
  187. if (sscanf(data, "%d%1s", &yoffset, t1) != 1 || *t1) {
  188. yoffset = 0;
  189. error(key, data, "illegal request (text)");
  190. }
  191. continue;
  192. }
  193. if (KEY("rotate")) {
  194. if (sscanf(data, "%lf", &rotate) != 1) {
  195. rotate = 0.0;
  196. error(key, data, "illegal rotate request");
  197. }
  198. continue;
  199. }
  200. if (KEY("ref")) {
  201. if (!scan_ref(data, &xref, &yref)) {
  202. xref = CENTER;
  203. yref = CENTER;
  204. error(key, data, "illegal ref request");
  205. }
  206. continue;
  207. }
  208. error(key, data, "illegal request (text)");
  209. }
  210. /* if file doesn't exist create it and close it */
  211. if (labels.other == NULL) {
  212. labels.other = G_tempfile();
  213. if ((fd = fopen(labels.other, "w")) != NULL)
  214. fclose(fd);
  215. }
  216. /* open file in append mode */
  217. fd = fopen(labels.other, "a");
  218. if (fd == NULL) {
  219. error("misc labels file", "", "can't open");
  220. return 1;
  221. }
  222. /* write the file */
  223. fprintf(fd, "font: %s\n", fontname);
  224. fprintf(fd, "east: %s\n", east);
  225. fprintf(fd, "north: %s\n", north);
  226. fprintf(fd, "xoffset: %d\n", xoffset);
  227. fprintf(fd, "yoffset: %d\n", yoffset);
  228. fprintf(fd, "width: %f\n", width);
  229. fprintf(fd, "hwidth: %f\n", hwidth);
  230. fprintf(fd, "size: %f\n", size);
  231. fprintf(fd, "fontsize: %d\n", fontsize);
  232. fprintf(fd, "opaque: %s\n", opaque ? "yes" : "no");
  233. if (rotate != 0)
  234. fprintf(fd, "rotate: %f\n", rotate);
  235. fprintf(fd, "color: ");
  236. if (!color_none(&color))
  237. fprintf(fd, "%d:%d:%d\n", color.r, color.g, color.b);
  238. else
  239. fprintf(fd, "black\n");
  240. fprintf(fd, "hcolor: ");
  241. if (!color_none(&hcolor))
  242. fprintf(fd, "%d:%d:%d\n", hcolor.r, hcolor.g, hcolor.b);
  243. else
  244. fprintf(fd, "none\n");
  245. fprintf(fd, "background: ");
  246. if (!color_none(&background))
  247. fprintf(fd, "%d:%d:%d\n", background.r, background.g, background.b);
  248. else
  249. fprintf(fd, "none\n");
  250. fprintf(fd, "border: ");
  251. if (!color_none(&border))
  252. fprintf(fd, "%d:%d:%d\n", border.r, border.g, border.b);
  253. else
  254. fprintf(fd, "none\n");
  255. fprintf(fd, "ref: ");
  256. switch (yref) {
  257. case UPPER:
  258. fprintf(fd, "upper");
  259. break;
  260. case LOWER:
  261. fprintf(fd, "lower");
  262. break;
  263. case CENTER:
  264. fprintf(fd, "center");
  265. break;
  266. }
  267. switch (xref) {
  268. case LEFT:
  269. fprintf(fd, " left");
  270. break;
  271. case RIGHT:
  272. fprintf(fd, " right");
  273. break;
  274. case CENTER:
  275. fprintf(fd, "%s", (xref == CENTER) ? "" : " center");
  276. break;
  277. }
  278. fprintf(fd, "\n");
  279. fprintf(fd, "text:%s\n\n", text);
  280. fclose(fd);
  281. return 0;
  282. }