GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
symbol.c
Go to the documentation of this file.
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 
17 #include <grass/gis.h>
18 #include <grass/display.h>
19 #include <grass/symbol.h>
20 #include <grass/glocale.h>
21 
22 static void symbol(const SYMBOL *Symb, double x0, double y0,
23  const RGBA_Color *fill_color, const RGBA_Color *line_color,
24  const RGBA_Color *string_color)
25 {
26  int i, j, k;
27  const SYMBPART *part;
28  const SYMBCHAIN *chain;
29  double xp, yp;
30  double *x, *y;
31  double sx = D_get_d_to_u_xconv();
32  double sy = D_get_d_to_u_yconv();
33 
34  G_debug(2, "D_symbol(): %d parts", Symb->count);
35 
36  for (i = 0; i < Symb->count; i++) {
37  part = Symb->part[i];
38 
39  switch (part->type) {
40 
41  case S_POLYGON:
42  /* draw background fills */
43  if ((part->fcolor.color == S_COL_DEFAULT &&
44  fill_color->a != RGBA_COLOR_NONE) ||
45  part->fcolor.color == S_COL_DEFINED) {
46  if (part->fcolor.color == S_COL_DEFAULT)
47  D_RGB_color(fill_color->r, fill_color->g, fill_color->b);
48  else
49  D_RGB_color(part->fcolor.r, part->fcolor.g, part->fcolor.b);
50 
51  for (j = 0; j < part->count;
52  j++) { /* for each component polygon */
53  chain = part->chain[j];
54 
55  x = G_malloc(sizeof(double) * chain->scount);
56  y = G_malloc(sizeof(double) * chain->scount);
57 
58  for (k = 0; k < chain->scount; k++) {
59  x[k] = x0 + sx * chain->sx[k];
60  y[k] = y0 - sy * chain->sy[k];
61  }
62  D_polygon_abs(x, y, chain->scount);
63 
64  G_free(x);
65  G_free(y);
66  }
67  }
68  /* again, to draw the lines */
69  if ((part->color.color == S_COL_DEFAULT &&
70  line_color->a != RGBA_COLOR_NONE) ||
71  part->color.color == S_COL_DEFINED) {
72  if (part->color.color == S_COL_DEFAULT)
73  D_RGB_color(line_color->r, line_color->g, line_color->b);
74  else
75  D_RGB_color(part->color.r, part->color.g, part->color.b);
76 
77  for (j = 0; j < part->count; j++) {
78  chain = part->chain[j];
79 
80  D_begin();
81  for (k = 0; k < chain->scount; k++) {
82  xp = x0 + sx * chain->sx[k];
83  yp = y0 - sy * chain->sy[k];
84  if (k == 0)
85  D_move_abs(xp, yp);
86  else
87  D_cont_abs(xp, yp);
88  }
89  D_end();
90  D_stroke();
91  }
92  }
93  break;
94 
95  case S_STRING:
96  if (part->color.color == S_COL_NONE)
97  break;
98  else if (part->color.color == S_COL_DEFAULT &&
99  string_color->a != RGBA_COLOR_NONE)
100  D_RGB_color(string_color->r, string_color->g, string_color->b);
101  else
102  D_RGB_color(part->color.r, part->color.g, part->color.b);
103 
104  chain = part->chain[0];
105 
106  D_begin();
107  for (j = 0; j < chain->scount; j++) {
108  xp = x0 + sx * chain->sx[j];
109  yp = y0 - sy * chain->sy[j];
110  if (j == 0)
111  D_move_abs(xp, yp);
112  else
113  D_cont_abs(xp, yp);
114  }
115  D_end();
116  D_stroke();
117  break;
118 
119  } /* switch */
120  } /* for loop */
121 }
122 
123 /*!
124  * \brief draw a symbol at pixel coordinates
125  *
126  * Draws a symbol (one of $GISBASE/etc/symbols/) to the active display.
127  * The starting x0,y0 coordinate corresponds to the center of the icon.
128  * The symbol must be pre-processed with S_stroke() before being sent
129  * to this function.
130  *
131  * \par Example
132  * \code
133  * #include <grass/display.h>
134  * #include <grass/symbol.h>
135  * ...
136  * SYMBOL *Symb;
137  * Symb = S_read( symbol_name );
138  * S_stroke( Symb, size, rotation, tolerance );
139  * D_symbol( Symb, x0, y0, line_color, fill_color );
140  * \endcode
141  *
142  * \param Symb The symbol name (e.g. basic/circle)
143  * \param x0 The starting x display coordinate (pixel)
144  * \param y0 The starting y display coordinate (pixel)
145  * \param line_color Outline color
146  * \param fill_color Fill color
147  * \return void
148  */
149 
150 void D_symbol(const SYMBOL *Symb, double x0, double y0,
151  const RGBA_Color *line_color, const RGBA_Color *fill_color)
152 {
153  symbol(Symb, x0, y0, fill_color, line_color, line_color);
154 }
155 
156 /*!
157  * \brief draw a symbol at pixel coordinates (alternate)
158  *
159  * Draws a symbol (one of $GISBASE/etc/symbols/) to the active display.
160  * The same as D_symbol(), but it uses a primary and secondary color
161  * instead of line and fill color. The primary color is used to draw
162  * stroke lines (STRINGs) and as the fill color for polygons. The secondary
163  * color is used for polygon outlines.
164  *
165  * \param Symb The symbol name (e.g. basic/circle)
166  * \param x0 The starting x display coordinate (pixel)
167  * \param y0 The starting y display coordinate (pixel)
168  * \param primary_color Primary draw color
169  * \param secondary_color Secondary draw color
170  * \return void
171  */
172 void D_symbol2(const SYMBOL *Symb, double x0, double y0,
173  const RGBA_Color *primary_color,
174  const RGBA_Color *secondary_color)
175 {
176  symbol(Symb, x0, y0, primary_color, secondary_color, primary_color);
177 }
double D_get_d_to_u_yconv(void)
Definition: cnversions.c:172
void D_stroke(void)
Definition: draw2.c:326
void D_cont_abs(double, double)
Definition: draw2.c:313
void D_end(void)
Definition: draw2.c:299
void D_polygon_abs(const double *, const double *, int)
Definition: draw2.c:389
void D_RGB_color(int, int, int)
Definition: tran_colr.c:215
void D_move_abs(double, double)
Definition: draw2.c:305
void D_begin(void)
Definition: draw2.c:294
double D_get_d_to_u_xconv(void)
Definition: cnversions.c:168
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#define G_malloc(n)
Definition: defs/gis.h:94
int G_debug(int, const char *,...) __attribute__((format(printf
#define RGBA_COLOR_NONE
Definition: display.h:17
double * sy
Definition: symbol.h:48
int scount
Definition: symbol.h:47
double * sx
Definition: symbol.h:48
int b
Definition: symbol.h:24
int r
Definition: symbol.h:24
int color
Definition: symbol.h:23
int g
Definition: symbol.h:24
Definition: symbol.h:60
int count
Definition: symbol.h:65
SYMBPART ** part
Definition: symbol.h:66
SYMBCHAIN ** chain
Definition: symbol.h:57
SYMBCOLOR fcolor
Definition: symbol.h:55
SYMBCOLOR color
Definition: symbol.h:54
int type
Definition: symbol.h:53
int count
Definition: symbol.h:56
unsigned char a
Definition: display.h:9
unsigned char g
Definition: display.h:9
unsigned char b
Definition: display.h:9
unsigned char r
Definition: display.h:9
void D_symbol2(const SYMBOL *Symb, double x0, double y0, const RGBA_Color *primary_color, const RGBA_Color *secondary_color)
draw a symbol at pixel coordinates (alternate)
Definition: symbol.c:172
void D_symbol(const SYMBOL *Symb, double x0, double y0, const RGBA_Color *line_color, const RGBA_Color *fill_color)
draw a symbol at pixel coordinates
Definition: symbol.c:150
#define S_STRING
Definition: symbol.h:15
#define S_COL_DEFAULT
Definition: symbol.h:18
#define S_COL_NONE
Definition: symbol.h:19
#define S_POLYGON
Definition: symbol.h:16
#define S_COL_DEFINED
Definition: symbol.h:20
#define x