symbol.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /****************************************************************************
  2. *
  3. * MODULE: display
  4. * AUTHOR(S): Hamish Bowman <hamish_b yahoo.com> (original contributor)
  5. * (adapted from Radim Blazek's d.vect code)
  6. * Glynn Clements <glynn gclements.plus.com>
  7. * PURPOSE: draw a symbol at pixel coordinates
  8. * COPYRIGHT: (C) 2005-2007 by M. Hamish Bowman, and
  9. * the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <grass/gis.h>
  17. #include <grass/display.h>
  18. #include <grass/symbol.h>
  19. #include <grass/glocale.h>
  20. static void symbol(const SYMBOL *Symb, double x0, double y0,
  21. const RGBA_Color *fill_color,
  22. const RGBA_Color *line_color,
  23. const RGBA_Color *string_color)
  24. {
  25. int i, j, k;
  26. const SYMBPART *part;
  27. const SYMBCHAIN *chain;
  28. double xp, yp;
  29. double *x, *y;
  30. double sx = D_get_d_to_u_xconv();
  31. double sy = D_get_d_to_u_yconv();
  32. G_debug(2, "D_symbol(): %d parts", Symb->count);
  33. for (i = 0; i < Symb->count; i++) {
  34. part = Symb->part[i];
  35. switch (part->type) {
  36. case S_POLYGON:
  37. /* draw background fills */
  38. if ((part->fcolor.color == S_COL_DEFAULT &&
  39. fill_color->a != RGBA_COLOR_NONE) ||
  40. part->fcolor.color == S_COL_DEFINED) {
  41. if (part->fcolor.color == S_COL_DEFAULT)
  42. D_RGB_color(fill_color->r, fill_color->g, fill_color->b);
  43. else
  44. D_RGB_color(part->fcolor.r, part->fcolor.g,
  45. part->fcolor.b);
  46. for (j = 0; j < part->count; j++) { /* for each component polygon */
  47. chain = part->chain[j];
  48. x = G_malloc(sizeof(double) * chain->scount);
  49. y = G_malloc(sizeof(double) * chain->scount);
  50. for (k = 0; k < chain->scount; k++) {
  51. x[k] = x0 + sx * chain->sx[k];
  52. y[k] = y0 - sy * chain->sy[k];
  53. }
  54. D_polygon_abs(x, y, chain->scount);
  55. G_free(x);
  56. G_free(y);
  57. }
  58. }
  59. /* again, to draw the lines */
  60. if ((part->color.color == S_COL_DEFAULT &&
  61. line_color->a != RGBA_COLOR_NONE) ||
  62. part->color.color == S_COL_DEFINED) {
  63. if (part->color.color == S_COL_DEFAULT)
  64. D_RGB_color(line_color->r, line_color->g, line_color->b);
  65. else
  66. D_RGB_color(part->color.r, part->color.g, part->color.b);
  67. for (j = 0; j < part->count; j++) {
  68. chain = part->chain[j];
  69. D_begin();
  70. for (k = 0; k < chain->scount; k++) {
  71. xp = x0 + sx * chain->sx[k];
  72. yp = y0 - sy * chain->sy[k];
  73. if (k == 0)
  74. D_move_abs(xp, yp);
  75. else
  76. D_cont_abs(xp, yp);
  77. }
  78. D_end();
  79. D_stroke();
  80. }
  81. }
  82. break;
  83. case S_STRING:
  84. if (part->color.color == S_COL_NONE)
  85. break;
  86. else if (part->color.color == S_COL_DEFAULT &&
  87. string_color->a != RGBA_COLOR_NONE)
  88. D_RGB_color(string_color->r, string_color->g, string_color->b);
  89. else
  90. D_RGB_color(part->color.r, part->color.g, part->color.b);
  91. chain = part->chain[0];
  92. D_begin();
  93. for (j = 0; j < chain->scount; j++) {
  94. xp = x0 + sx * chain->sx[j];
  95. yp = y0 - sy * chain->sy[j];
  96. if (j == 0)
  97. D_move_abs(xp, yp);
  98. else
  99. D_cont_abs(xp, yp);
  100. }
  101. D_end();
  102. D_stroke();
  103. break;
  104. } /* switch */
  105. } /* for loop */
  106. }
  107. /*!
  108. * \brief draw a symbol at pixel coordinates
  109. *
  110. * Draws a symbol (one of $GISBASE/etc/symbols/) to the active display.
  111. * The starting x0,y0 coordinate corresponds to the center of the icon.
  112. * The symbol must be pre-processed with S_stroke() before being sent
  113. * to this function.
  114. *
  115. * \par Example
  116. * \code
  117. * #include <grass/display.h>
  118. * #include <grass/symbol.h>
  119. * ...
  120. * SYMBOL *Symb;
  121. * Symb = S_read( symbol_name );
  122. * S_stroke( Symb, size, rotation, tolerance );
  123. * D_symbol( Symb, x0, y0, line_color, fill_color );
  124. * \endcode
  125. *
  126. * \param Symb The symbol name (e.g. basic/circle)
  127. * \param x0 The starting x display coordinate (pixel)
  128. * \param y0 The starting y display coordinate (pixel)
  129. * \param line_color Outline color
  130. * \param fill_color Fill color
  131. * \return void
  132. */
  133. void D_symbol(const SYMBOL *Symb, double x0, double y0,
  134. const RGBA_Color *line_color,
  135. const RGBA_Color *fill_color)
  136. {
  137. symbol(Symb, x0, y0, fill_color, line_color, line_color);
  138. }
  139. /*!
  140. * \brief draw a symbol at pixel coordinates (alternate)
  141. *
  142. * Draws a symbol (one of $GISBASE/etc/symbols/) to the active display.
  143. * The same as D_symbol(), but it uses a primary and secondary color
  144. * instead of line and fill color. The primary color is used to draw
  145. * stroke lines (STRINGs) and as the fill color for polygons. The secondary
  146. * color is used for polygon outlines.
  147. *
  148. * \param Symb The symbol name (e.g. basic/circle)
  149. * \param x0 The starting x display coordinate (pixel)
  150. * \param y0 The starting y display coordinate (pixel)
  151. * \param primary_color Primary draw color
  152. * \param secondary_color Secondary draw color
  153. * \return void
  154. */
  155. void D_symbol2(const SYMBOL *Symb, double x0, double y0,
  156. const RGBA_Color *primary_color,
  157. const RGBA_Color *secondary_color)
  158. {
  159. symbol(Symb, x0, y0, primary_color, secondary_color, primary_color);
  160. }