draw_n_arrow.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * draw_n_arrow() places a north arrow somewhere in the display frame
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <grass/gis.h>
  7. #include <grass/display.h>
  8. #include <grass/symbol.h>
  9. #include <grass/colors.h>
  10. #include <grass/glocale.h>
  11. #include "options.h"
  12. int draw_n_arrow(double east, double north, double fontsize,
  13. char *n_arrow_num, double line_width)
  14. {
  15. double x_pos, y_pos;
  16. double t, b, l, r;
  17. double tt, tb, tl, tr; /* text box*/
  18. SYMBOL *Symb;
  19. RGBA_Color *line_color, *fill_color;
  20. int R, G, B;
  21. double x0, y0;
  22. char icon[64];
  23. double symbol_size;
  24. /* Establish text size */
  25. if (fontsize > 0)
  26. D_text_size(fontsize, fontsize);
  27. D_setup_unity(0);
  28. D_get_src(&t, &b, &l, &r);
  29. x_pos = east * (r - l) / 100.;
  30. y_pos = (100. - north) * (b - t) / 100.;
  31. if (line_width > 0)
  32. D_line_width(line_width);
  33. if (fontsize > 0) {
  34. /* draw the "N" */
  35. D_get_text_box("N", &tt, &tb, &tl, &tr);
  36. D_use_color(fg_color);
  37. /* positions manually tuned */
  38. switch (n_arrow_num[0]) {
  39. case '1':
  40. D_pos_abs(x_pos - (tr + tl) / 2, y_pos - 45);
  41. D_text("N");
  42. break;
  43. case '3':
  44. D_pos_abs(x_pos - (tr + tl) / 2, y_pos - 60);
  45. D_text("N");
  46. break;
  47. case '4':
  48. D_pos_abs(x_pos - (tr + tl) / 2, y_pos - 45);
  49. D_text("N");
  50. break;
  51. case '7':
  52. D_pos_abs(x_pos - (tr + tl) / 2, y_pos - 70);
  53. D_text("N");
  54. break;
  55. case '9':
  56. case 'f':
  57. D_pos_abs(x_pos - (tr + tl) / 2, y_pos - 55);
  58. D_text("N");
  59. break;
  60. case 'b':
  61. D_pos_abs(x_pos - (tr + tl) / 2, y_pos - 48.5);
  62. D_text("N");
  63. break;
  64. case '2':
  65. case '5':
  66. case '6':
  67. case '8':
  68. break;
  69. default:
  70. G_fatal_error(_("Could not parse symbol"));
  71. }
  72. }
  73. /* display the north arrow symbol */
  74. line_color = G_malloc(sizeof(RGBA_Color));
  75. fill_color = G_malloc(sizeof(RGBA_Color));
  76. if (D_color_number_to_RGB(fg_color, &R, &G, &B) == 0)
  77. /* fall back to black on failure */
  78. G_str_to_color(DEFAULT_FG_COLOR, &R, &G, &B);
  79. line_color->r = (unsigned char)R;
  80. line_color->g = (unsigned char)G;
  81. line_color->b = (unsigned char)B;
  82. line_color->a = RGBA_COLOR_OPAQUE;
  83. if (D_color_number_to_RGB(bg_color, &R, &G, &B) == 0)
  84. /* fall back to black on failure */
  85. G_str_to_color(DEFAULT_FG_COLOR, &R, &G, &B);
  86. fill_color->r = (unsigned char)R;
  87. fill_color->g = (unsigned char)G;
  88. fill_color->b = (unsigned char)B;
  89. fill_color->a = RGBA_COLOR_OPAQUE;
  90. if (n_arrow_num[0] == '2' || n_arrow_num[0] == '9')
  91. fill_color->a = RGBA_COLOR_TRANSPARENT;
  92. /* sizes manually tuned */
  93. switch (n_arrow_num[0]) {
  94. case '1':
  95. symbol_size = 35.;
  96. break;
  97. case '2':
  98. symbol_size = 19.;
  99. break;
  100. case '3':
  101. symbol_size = 20.;
  102. break;
  103. case '4':
  104. symbol_size = 15.;
  105. break;
  106. case '5':
  107. case '6':
  108. symbol_size = 14.;
  109. break;
  110. case '7':
  111. symbol_size = 23.;
  112. break;
  113. case '8':
  114. case '9':
  115. symbol_size = 17.;
  116. break;
  117. case 'b':
  118. symbol_size = 80.;
  119. break;
  120. case 'f':
  121. symbol_size = 100.;
  122. break;
  123. default:
  124. G_fatal_error(_("Could not parse symbol"));
  125. }
  126. x0 = D_d_to_u_col(x_pos);
  127. y0 = D_d_to_u_row(y_pos);
  128. if (n_arrow_num[0] == 'b')
  129. strcpy(icon, "n_arrows/basic_compass");
  130. else if (n_arrow_num[0] == 'f')
  131. strcpy(icon, "n_arrows/fancy_compass");
  132. else {
  133. strcpy(icon, "n_arrows/n_arrow");
  134. strncat(icon, n_arrow_num, 32);
  135. }
  136. Symb = S_read(icon);
  137. if(!Symb)
  138. G_fatal_error(_("Could not read symbol \"%s\""), icon);
  139. S_stroke(Symb, symbol_size, 0.0, 0);
  140. D_symbol(Symb, x0, y0, line_color, fill_color);
  141. if (line_width > 0)
  142. D_line_width(0);
  143. G_free(Symb);
  144. G_free(line_color);
  145. G_free(fill_color);
  146. return 0;
  147. }