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