File: g_attrib.c

package info (click to toggle)
plotutils 2.4.1-11
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 11,676 kB
  • ctags: 6,967
  • sloc: ansic: 76,305; sh: 15,172; cpp: 12,403; yacc: 2,604; makefile: 888; lex: 144
file content (341 lines) | stat: -rw-r--r-- 11,065 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* This file contains the linemod method, which is a standard part of
   libplot.  It sets a drawing attribute: the line style used in subsequent
   drawing operations.

   This version searches for the specified line style in a table of known
   line styles (see g_dash2.c). */

/* This file also contains the capmod method, which is a GNU extension to
   libplot.  It sets a drawing attribute: the cap mode used when
   subsequently drawing open paths. */

/* This file also contains the joinmod method, which is a GNU extension to
   libplot.  It sets a drawing attribute: the join mode used when
   subsequently drawing paths consisting of more than a single segment. */

/* This file also contains the miterlimit method, which is a GNU extension
   to libplot.  It sets a drawing attribute: the miter limit of polylines
   subsequently drawn on the display device.  This attribute controls the
   treatment of corners when the join mode is set to "miter". */

/* This file contains the orientation method, which is a GNU extension to
   libplot.  It sets a drawing attribute: whether or not closed paths of
   the three built-in types (rectangles, circles, ellipses) should be drawn
   counterclockwise or clockwise.  The former is the default. */

/* This file contains the fillmod method, which is a GNU extension to
   libplot.  It sets a drawing attribute: the fill rule used when
   subsequently drawing filled objects, i.e., the rule used to determine
   which points are `inside'.

   In principle, both the `odd winding number' rule and the `nonzero
   winding number' rule are supported.  The former is the default. */

#include "sys-defines.h"
#include "extern.h"

int
#ifdef _HAVE_PROTOS
_API_linemod (R___(Plotter *_plotter) const char *s)
#else
_API_linemod (R___(_plotter) s)
     S___(Plotter *_plotter;) 
     const char *s;
#endif
{
  bool matched = false;
  char *line_mode;
  int i;

  if (!_plotter->data->open)
    {
      _plotter->error (R___(_plotter) 
		       "linemod: invalid operation");
      return -1;
    }

  _API_endpath (S___(_plotter)); /* flush path if any */

  /* null pointer resets to default */
  if ((!s) || !strcmp(s, "(null)"))
    s = _default_drawstate.line_mode;

  free ((char *)_plotter->drawstate->line_mode);
  line_mode = (char *)_plot_xmalloc (strlen (s) + 1);
  strcpy (line_mode, s);
  _plotter->drawstate->line_mode = line_mode;
  
  if (strcmp (s, "disconnected") == 0)
     /* we'll implement disconnected lines by drawing a filled circle at
	each path join point; see g_endpath.c */
    {
      _plotter->drawstate->line_type = L_SOLID;
      _plotter->drawstate->points_are_connected = false;
      matched = true;
    }
  
  else	/* search table of libplot's builtin line styles */
    for (i = 0; i < NUM_LINE_STYLES; i++)
      {
	if (strcmp (s, _line_styles[i].name) == 0)
	  {
	    _plotter->drawstate->line_type =
	      _line_styles[i].type;
	    _plotter->drawstate->points_are_connected = true;
	    matched = true;
	    break;
	  }
      }
  
  if (matched == false)
    /* don't recognize, silently switch to default mode */
    _API_linemod (R___(_plotter) _default_drawstate.line_mode);

  /* for future paths, use builtin line style rather than user-specified
     dash array */
  _plotter->drawstate->dash_array_in_effect = false;

  return 0;
}

int
#ifdef _HAVE_PROTOS
_API_capmod (R___(Plotter *_plotter) const char *s)
#else
_API_capmod (R___(_plotter) s)
     S___(Plotter *_plotter;) 
     const char *s;
#endif
{
  char *cap_mode;

  if (!_plotter->data->open)
    {
      _plotter->error (R___(_plotter) 
		       "capmod: invalid operation");
      return -1;
    }

  _API_endpath (S___(_plotter)); /* flush path if any */

  /* null pointer resets to default */
  if ((!s) || !strcmp(s, "(null)"))
    s = _default_drawstate.cap_mode;

  free ((char *)_plotter->drawstate->cap_mode);
  cap_mode = (char *)_plot_xmalloc (strlen (s) + 1);
  strcpy (cap_mode, s);
  _plotter->drawstate->cap_mode = cap_mode;

  /* The following four cap types are now standard. */

  if (strcmp( s, "butt") == 0)
    _plotter->drawstate->cap_type = CAP_BUTT;
  else if (strcmp( s, "round") == 0)
    _plotter->drawstate->cap_type = CAP_ROUND;
  else if (strcmp( s, "projecting") == 0)
    _plotter->drawstate->cap_type = CAP_PROJECT;
  else if (strcmp( s, "triangular") == 0)
    _plotter->drawstate->cap_type = CAP_TRIANGULAR;
  else
    /* don't recognize, silently switch to default mode */
    return _API_capmod (R___(_plotter) _default_drawstate.cap_mode);
  
  return 0;
}

int
#ifdef _HAVE_PROTOS
_API_joinmod (R___(Plotter *_plotter) const char *s)
#else
_API_joinmod (R___(_plotter) s)
     S___(Plotter *_plotter;) 
     const char *s;
#endif
{
  char *join_mode;

  if (!_plotter->data->open)
    {
      _plotter->error (R___(_plotter) 
		       "joinmod: invalid operation");
      return -1;
    }

  _API_endpath (S___(_plotter)); /* flush path if any */

  /* null pointer resets to default */
  if ((!s) || !strcmp(s, "(null)"))
    s = _default_drawstate.join_mode;

  free ((char *)_plotter->drawstate->join_mode);
  join_mode = (char *)_plot_xmalloc (strlen (s) + 1);
  strcpy (join_mode, s);
  _plotter->drawstate->join_mode = join_mode;

  /* The following four join types are now standard. */

  if (strcmp( s, "miter") == 0)
    _plotter->drawstate->join_type = JOIN_MITER;
  else if (strcmp( s, "mitre") == 0)
    _plotter->drawstate->join_type = JOIN_MITER;
  else if (strcmp( s, "round") == 0)
    _plotter->drawstate->join_type = JOIN_ROUND;
  else if (strcmp( s, "bevel") == 0)
    _plotter->drawstate->join_type = JOIN_BEVEL;
  else if (strcmp( s, "triangular") == 0)
    _plotter->drawstate->join_type = JOIN_TRIANGULAR;
  else
    /* unknown, so silently switch to default mode (via recursive call) */
    return _API_joinmod (R___(_plotter) _default_drawstate.join_mode);
  
  return 0;
}

/* Below is the miterlimit method, which is a GNU extension to libplot.  It
   sets a drawing attribute: the miter limit of polylines subsequently
   drawn on the display device.  This attribute controls the treatment of
   corners when the join mode is set to "miter".

   At a join point of a wide polyline, the `miter length' is defined to be
   the distance between the inner corner and the outer corner.  The miter
   limit is the maximum value that can be tolerated for the miter length
   divided by the line width.  If this value is exceeded, the miter will be
   `cut off': the "bevel" join mode will be used instead.

   Examples of typical values for the miter limit are 10.43 (the
   unchangeable value used by the X Window System, which cuts off miters at
   join angles less than 11 degrees), 5.0 (the default value used by
   HP-GL/2 and PCL 5 devices, which cuts off miters at join angles less
   than 22.1 degrees), 2.0 (cuts off miters at join angles less than 60
   degrees), 1.414 (cuts off miters at join angles less than 90 degrees),
   and 1.0 (cuts off all miters).  

   In general, the miter limit is the cosecant of one-half of the minimum
   join angle for mitering, and values less than 1.0 are meaningless.  For
   example, 10.43 is csc((11 degrees)/2) = 1 / sin((11 degrees)/2).
   Mitering is allowed to take place if 1 / sin(theta/2) <= MITERLIMIT,
   i.e. sin(theta/2) >= 1/MITERLIMIT, where theta > 0 is the join angle. */

int
#ifdef _HAVE_PROTOS
_API_fmiterlimit (R___(Plotter *_plotter) double new_miter_limit)
#else
_API_fmiterlimit (R___(_plotter) new_miter_limit)
     S___(Plotter *_plotter;)
     double new_miter_limit;
#endif
{
  if (!_plotter->data->open)
    {
      _plotter->error (R___(_plotter) 
		       "flinewidth: invalid operation");
      return -1;
    }

  _API_endpath (S___(_plotter)); /* flush path if any */

  if (new_miter_limit < 1.0)	/* reset to default */
    new_miter_limit = DEFAULT_MITER_LIMIT;

  /* set the new miter limit in the drawing state */
  _plotter->drawstate->miter_limit = new_miter_limit;
  
  return 0;
}

/* Below is the orientation method, which is a GNU extension to libplot.
   It sets a drawing attribute: whether or not closed paths of the three
   built-in types (rectangles, circles, ellipses) should be drawn
   counterclockwise or clockwise.  The former is the default. */

/* This attribute-setting method, though path-related, doesn't invoke
   _API_endpath() to end the compound path under construction (if any).
   That's because it makes sense to invoke this method between the simple
   paths of a compound path. */

int
#ifdef _HAVE_PROTOS
_API_orientation (R___(Plotter *_plotter) int direction)
#else
_API_orientation (R___(_plotter) direction)
     S___(Plotter *_plotter;) 
     int direction;
#endif
{
  if (!_plotter->data->open)
    {
      _plotter->error (R___(_plotter) 
		       "orientation: invalid operation");
      return -1;
    }

  if (direction != 1 && direction != -1)
    /* OOB switches to default */
    direction = _default_drawstate.orientation;

  _plotter->drawstate->orientation = direction;
  
  return 0;
}

/* Below is the fillmod method, which is a GNU extension to libplot.  It
   sets a drawing attribute: the fill rule used when subsequently drawing
   filled objects, i.e., the rule used to determine which points are
   `inside'.

   In principle, both the `odd winding number' rule and the `nonzero
   winding number' rule are supported.  The former is the default. */

int
#ifdef _HAVE_PROTOS
_API_fillmod (R___(Plotter *_plotter) const char *s)
#else
_API_fillmod (R___(_plotter) s)
     S___(Plotter *_plotter;) 
     const char *s;
#endif
{
  const char *default_s;
  char *fill_rule;

  if (!_plotter->data->open)
    {
      _plotter->error (R___(_plotter) 
		       "fillmod: invalid operation");
      return -1;
    }

  _API_endpath (S___(_plotter)); /* flush path if any */

  /* determine default fill rule (can't just read from default drawing
     state, because not all Plotters support both standard rules) */
  default_s = _default_drawstate.fill_rule;
  if (strcmp (default_s, "even-odd") == 0
      && _plotter->data->have_odd_winding_fill == 0)
    default_s = "nonzero-winding";
  else if (strcmp (default_s, "nonzero-winding") == 0
	   && _plotter->data->have_nonzero_winding_fill == 0)
    default_s = "even-odd";
  
  /* null pointer resets to default */
  if ((!s) || !strcmp(s, "(null)"))
    s = default_s;

  free ((char *)_plotter->drawstate->fill_rule);
  fill_rule = (char *)_plot_xmalloc (strlen (s) + 1);
  strcpy (fill_rule, s);
  _plotter->drawstate->fill_rule = fill_rule;

  if ((strcmp (s, "even-odd") == 0 || strcmp (s, "alternate") == 0)
      && _plotter->data->have_odd_winding_fill)
    _plotter->drawstate->fill_rule_type = FILL_ODD_WINDING;
  else if ((strcmp (s, "nonzero-winding") == 0 || strcmp (s, "winding") == 0)
	   && _plotter->data->have_nonzero_winding_fill)
    _plotter->drawstate->fill_rule_type = FILL_NONZERO_WINDING;
  else
    /* unknown, so silently switch to default fill rule (via recursive call) */
    _API_fillmod (R___(_plotter) default_s);

  return 0;
}