utils.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <grass/gis.h>
  2. #include <grass/raster.h>
  3. #include <grass/Vect.h>
  4. #include <grass/display.h>
  5. #include <grass/glocale.h>
  6. #include "local_proto.h"
  7. #include "plot.h"
  8. void show_label(double *px, double *py, LATTR *lattr, const char *text)
  9. {
  10. double X = *px, Y = *py;
  11. int Xoffset, Yoffset;
  12. double xarr[5], yarr[5];
  13. double T, B, L, R;
  14. X = X + D_get_d_to_u_xconv() * 0.5 * lattr->size;
  15. Y = Y + D_get_d_to_u_yconv() * 1.5 * lattr->size;
  16. D_pos_abs(X, Y);
  17. D_get_text_box(text, &T, &B, &L, &R);
  18. /* Expand border 1/2 of text size */
  19. T = T - D_get_d_to_u_yconv() * lattr->size / 2;
  20. B = B + D_get_d_to_u_yconv() * lattr->size / 2;
  21. L = L - D_get_d_to_u_xconv() * lattr->size / 2;
  22. R = R + D_get_d_to_u_xconv() * lattr->size / 2;
  23. Xoffset = 0;
  24. Yoffset = 0;
  25. if (lattr->xref == LCENTER)
  26. Xoffset = -(R - L) / 2;
  27. if (lattr->xref == LRIGHT)
  28. Xoffset = -(R - L);
  29. if (lattr->yref == LCENTER)
  30. Yoffset = -(B - T) / 2;
  31. if (lattr->yref == LBOTTOM)
  32. Yoffset = -(B - T);
  33. if (lattr->has_bgcolor || lattr->has_bcolor) {
  34. xarr[0] = xarr[1] = xarr[4] = L + Xoffset;
  35. xarr[2] = xarr[3] = R + Xoffset;
  36. yarr[0] = yarr[3] = yarr[4] = B + Yoffset;
  37. yarr[1] = yarr[2] = T + Yoffset;
  38. if (lattr->has_bgcolor) {
  39. D_RGB_color(lattr->bgcolor.R, lattr->bgcolor.G,
  40. lattr->bgcolor.B);
  41. D_polygon_abs(xarr, yarr, 5);
  42. }
  43. if (lattr->has_bcolor) {
  44. D_RGB_color(lattr->bcolor.R, lattr->bcolor.G,
  45. lattr->bcolor.B);
  46. D_polyline_abs(xarr, yarr, 5);
  47. }
  48. D_RGB_color(lattr->color.R, lattr->color.G, lattr->color.B);
  49. }
  50. D_pos_abs(X + Xoffset, Y + Yoffset);
  51. D_text(text);
  52. }
  53. void show_label_line(const struct line_pnts *Points, int ltype, LATTR *lattr, const char *text)
  54. {
  55. double X, Y;
  56. if ((ltype & GV_POINTS) || Points->n_points == 1)
  57. /* point/centroid or line/boundary with one coor */
  58. {
  59. X = Points->x[0];
  60. Y = Points->y[0];
  61. }
  62. else if (Points->n_points == 2) { /* line with two coors */
  63. X = (Points->x[0] + Points->x[1]) / 2;
  64. Y = (Points->y[0] + Points->y[1]) / 2;
  65. }
  66. else {
  67. int i = Points->n_points / 2;
  68. X = Points->x[i];
  69. Y = Points->y[i];
  70. }
  71. show_label(&X, &Y, lattr, text);
  72. }