get_scalebar.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Function: read_scalebar
  2. **
  3. ** Author: Paul W. Carlson April 1992
  4. */
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "local_proto.h"
  8. #define KEY(x) (strcmp(key,x)==0)
  9. static char *help[] = {
  10. "where x y",
  11. "length length",
  12. "units auto|meters|kilometers|feet|miles|nautmiles",
  13. "height height",
  14. "segment no_segemnts",
  15. "numbers no_labels",
  16. "font fontname",
  17. "fontsize fontsize",
  18. "color fontcolor",
  19. "bgcolor backgroundcolor",
  20. "background [Y|n]",
  21. "width #",
  22. ""
  23. };
  24. int read_scalebar(void)
  25. {
  26. char buf[1024];
  27. char *key, *data;
  28. char ch;
  29. /* struct defined in decorate.h */
  30. sb.segment = 4; /* four segments */
  31. sb.numbers = 1; /* label each segment */
  32. sb.font = G_store("Helvetica");
  33. sb.fontsize = 8;
  34. sb.color = BLACK; /* TODO: multi-color */
  35. sb.width = 1.;
  36. sb.length = -1.;
  37. sb.height = 0.1; /* default height in inches */
  38. sb.x = PS.page_width / 2.;
  39. sb.y = 2.;
  40. sb.bgcolor = 1; /* TODO: multi-color */
  41. sb.units = SB_UNITS_AUTO; /* default to automatic based on value in PROJ_UNITS */
  42. while (input(2, buf, help)) {
  43. if (!key_data(buf, &key, &data))
  44. continue;
  45. if (KEY("where")) {
  46. if (sscanf(data, "%lf %lf", &sb.x, &sb.y) != 2) {
  47. error(key, data, "illegal where request");
  48. }
  49. else
  50. continue;
  51. }
  52. if (KEY("height")) {
  53. if (sscanf(data, "%lf", &sb.height) != 1 || sb.height <= 0.) {
  54. error(key, data, "illegal height request");
  55. }
  56. else
  57. continue;
  58. }
  59. if (KEY("length")) {
  60. if (sscanf(data, "%lf", &sb.length) != 1 || sb.length <= 0.) {
  61. error(key, data, "illegal length request");
  62. }
  63. else
  64. continue;
  65. }
  66. if (KEY("units")) {
  67. G_strip(data);
  68. if (strcmp(data, "auto") == 0) {
  69. sb.units = SB_UNITS_AUTO;
  70. continue;
  71. }
  72. else if (G_projection() == PROJECTION_XY) {
  73. error(key, data,
  74. "Earth units not available in simple XY location");
  75. }
  76. else if (strcmp(data, "meters") == 0) {
  77. sb.units = SB_UNITS_METERS;
  78. continue;
  79. }
  80. else if (strcmp(data, "kilometers") == 0 || strcmp(data, "km") == 0) {
  81. sb.units = SB_UNITS_KM;
  82. continue;
  83. }
  84. else if (strcmp(data, "feet") == 0) {
  85. sb.units = SB_UNITS_FEET;
  86. continue;
  87. }
  88. else if (strcmp(data, "miles") == 0) {
  89. sb.units = SB_UNITS_MILES;
  90. continue;
  91. }
  92. else if (strcmp(data, "nautmiles") == 0 || strcmp(data, "nm") == 0) {
  93. sb.units = SB_UNITS_NMILES;
  94. continue;
  95. }
  96. else
  97. error(key, data, "illegal units request");
  98. }
  99. if (KEY("segment")) {
  100. if (sscanf(data, "%d", &sb.segment) != 1 || sb.segment <= 0) {
  101. error(key, data, "illegal segment request");
  102. }
  103. else
  104. continue;
  105. }
  106. if (KEY("numbers")) {
  107. if (sscanf(data, "%d", &sb.numbers) != 1 || sb.numbers <= 0) {
  108. error(key, data, "illegal numbers request");
  109. }
  110. else
  111. continue;
  112. }
  113. if (KEY("font")) {
  114. get_font(data);
  115. G_free(sb.font);
  116. sb.font = G_store(data);
  117. continue;
  118. }
  119. if (KEY("fontsize")) {
  120. if (sscanf(data, "%d", &sb.fontsize) != 1 || sb.fontsize <= 0) {
  121. error(key, data, "illegal fontsize request");
  122. }
  123. else
  124. continue;
  125. }
  126. if (KEY("color"))
  127. {
  128. sb.color = get_color_number(data);
  129. if (sb.color < 0)
  130. {
  131. sb.color = BLACK;
  132. error(key, data, "illegal color request");
  133. }
  134. continue;
  135. }
  136. if (KEY("background")) {
  137. sb.bgcolor = yesno(key, data);
  138. continue;
  139. }
  140. if (KEY("width")) {
  141. sb.width = -1.;
  142. ch = ' ';
  143. if ((sscanf(data, "%lf%c", &sb.width, &ch) < 1) ||
  144. (sb.width < 0.)) {
  145. sb.width = 1.;
  146. error(key, data, "illegal grid width request");
  147. }
  148. if (ch == 'i')
  149. sb.width = sb.width * 72.0;
  150. continue;
  151. }
  152. error(key, data, "illegal request (scalebar)");
  153. }
  154. return 0;
  155. }