File: c_attribs.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 (489 lines) | stat: -rw-r--r-- 16,765 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/* This internal method is invoked by a CGMPlotter before drawing any path.
   It sets the relevant CGM attributes (line type, cap type, join type,
   line width) to what they should be. */

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

/* CGM join styles, indexed by internal number (miter/rd./bevel/triangular) */
const int _cgm_join_style[] =
{ CGM_JOIN_MITER, CGM_JOIN_ROUND, CGM_JOIN_BEVEL, CGM_JOIN_ROUND };

/* CGM cap styles, indexed by internal number (butt/rd./project/triangular) */
/* Note: we map libplot's triangular cap style to CGM's round cap style,
   not CGM's triangular cap style, since the latter plots an equilateral
   triangle, which is not libplot's convention. */
const int _cgm_cap_style[] =
{ CGM_CAP_BUTT, CGM_CAP_ROUND, CGM_CAP_PROJECTING, CGM_CAP_ROUND };

void
#ifdef _HAVE_PROTOS
_c_set_attributes (R___(Plotter *_plotter) int object_type)
#else
_c_set_attributes (R___(_plotter) object_type)
     S___(Plotter *_plotter;)
     int object_type;		/* closed path? open path? marker? etc.*/
#endif
{
  int desired_width = _plotter->drawstate->quantized_device_line_width;
  int desired_line_type = CGM_L_SOLID; /* keep compiler happy */
  double desired_dash_offset = 0.0;

  if (_plotter->drawstate->pen_type == 0)
    /* won't be edging; at most, will be filling; so nothing to do */
    return;

  /* alter CGM line width if necessary */

  switch (object_type)
    {
    case CGM_OBJECT_OPEN:
      if (_plotter->cgm_line_width != desired_width)
	/* emit "LINE WIDTH" command */
	{
	  int byte_count, data_byte_count, data_len;
	  
	  data_len = CGM_BINARY_BYTES_PER_INTEGER;
	  byte_count = data_byte_count = 0;
	  _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
				    CGM_ATTRIBUTE_ELEMENT, 3,
				    data_len, &byte_count,
				    "LINEWIDTH");
	  _cgm_emit_integer (_plotter->data->page, false, _plotter->cgm_encoding,
			     desired_width,
			     data_len, &data_byte_count, &byte_count);
	  _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
					&byte_count);
	  /* update line width */
	  _plotter->cgm_line_width = desired_width;
	}
      break;
    case CGM_OBJECT_CLOSED:
      if (_plotter->cgm_edge_width != desired_width)
	/* emit "EDGE WIDTH" command */
	{
	  int byte_count, data_byte_count, data_len;
	  
	  data_len = CGM_BINARY_BYTES_PER_INTEGER;
	  byte_count = data_byte_count = 0;
	  _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
				    CGM_ATTRIBUTE_ELEMENT, 28,
				    data_len, &byte_count,
				    "EDGEWIDTH");
	  _cgm_emit_integer (_plotter->data->page, false, _plotter->cgm_encoding,
			     desired_width,
			     data_len, &data_byte_count, &byte_count);
	  _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
					&byte_count);
	  /* update edge width */
	  _plotter->cgm_edge_width = desired_width;
	}
      break;
    default:			/* shouldn't happen */
      break;
    }
  
  /* determine line type */

  if (_plotter->data->have_dash_array 
      && _plotter->drawstate->dash_array_in_effect)
    /* user specified a dash array, and this version of CGM supports them;
       so compute a CGM-style dash array, and maybe add a new line type to
       page-specific line type table */
    {
      int num_dashes = _plotter->drawstate->dash_array_len;
      int our_num_dashes = num_dashes; /* will double if array length is odd */

      if (num_dashes > 0)
	/* non-solid line type */
	{
	  double min_sing_val, max_sing_val;
	  int i, *dashbuf;
	  plCGMCustomLineType *line_type_ptr, *old_line_type_ptr;
	  int line_type;
	  bool odd_length, matched_line_type;
	  
	  /* compute minimum singular value of user->device coordinate
	     map, which we use as a multiplicative factor to convert
	     line widths (cf. g_linewidth.c), dash lengths, etc. */
	  _matrix_sing_vals (_plotter->drawstate->transform.m, 
			     &min_sing_val, &max_sing_val);

	  /* double array length if odd (we don't trust CGM interpreters to
             handle odd-length dash arrays in the way that PS does) */
	  odd_length = (num_dashes % 2 != 0 ? true : false);
	  if (odd_length)
	    our_num_dashes *= 2;

	  dashbuf = (int *)_plot_xmalloc (our_num_dashes * sizeof(int));
	  for (i = 0; i < num_dashes; i++)
	    {
	      double dashlen;
	      int i_dashlen;
	      
	      dashlen =
		min_sing_val * _plotter->drawstate->dash_array[i];
	      i_dashlen = IROUND(dashlen);
	      if (i_dashlen == 0 && dashlen > 0.0)
		i_dashlen = 1;	/* don't use 0 if user specified non-0 */
	      dashbuf[i] = i_dashlen;
	      if (odd_length)
		dashbuf[i + num_dashes] = i_dashlen;
	    }
	  
	  /* compute offset as fraction of cycle length; must be in range
	     [0.0,1.0] (CGM convention) */
	  {
	    int cycle_length = 0;

	    for (i = 0; i < our_num_dashes; i++)
	      cycle_length += dashbuf[i]; 
	    /* cycle length now guaranteed to be > 0 */

	    desired_dash_offset = 
	      min_sing_val * _plotter->drawstate->dash_offset / cycle_length;
	    desired_dash_offset -= IFLOOR(desired_dash_offset);

	    if (desired_dash_offset < 0.0 || desired_dash_offset >= 1.0)
	      desired_dash_offset = 0.0;
	  }

	  /* search table of user-defined, page-specific CGM line types;
	     they're numbered -1, -2, -3, ... */
	  line_type_ptr = (plCGMCustomLineType *)_plotter->data->page->extra;
	  old_line_type_ptr = (plCGMCustomLineType *)NULL;
	  line_type = 0;
	  matched_line_type = false;
	  while (line_type_ptr != (plCGMCustomLineType *)NULL)
	    {
	      line_type--;

	      if (line_type_ptr->dash_array_len == our_num_dashes)
		{
		  bool foundit = true;

		  for (i = 0; i < our_num_dashes; i++)
		    {
		      if (dashbuf[i] != line_type_ptr->dashes[i])
			{
			  foundit = false;
			  break; /* break out of for loop */
			}
		    }
		  if (foundit)
		    {
		      matched_line_type = true;
		      break;	/* break out of while loop */
		    }
		}

	      /* on to next entry in line type table */
	      old_line_type_ptr = line_type_ptr;
	      line_type_ptr = line_type_ptr->next;
	    }
	  /* on exit from while(), either matched_line_type = true (with
	     line_type set correctly), or old_line_type_ptr points to tail
	     of line type list, and `line_type' is the last valid type */

	  if (matched_line_type)
	    {
	      desired_line_type = line_type;
	      free (dashbuf);
	    }
	  else
	    {
	      /* construct new record from `dashbuf', add to tail of list;
		 `dashbuf' and the record will be freed when the page is
		 written out */
	      plCGMCustomLineType *newguy;

	      newguy = (plCGMCustomLineType *)_plot_xmalloc (sizeof(plCGMCustomLineType));
	      newguy->dashes = dashbuf;
	      newguy->dash_array_len = our_num_dashes;
	      newguy->next = (plCGMCustomLineType *)NULL;
	      if (old_line_type_ptr != (plCGMCustomLineType *)NULL)
		old_line_type_ptr->next = newguy;
	      else
		_plotter->data->page->extra = newguy;

	      /* new line type index is one less than most negative
		 previously defined index */
	      desired_line_type = line_type - 1;
	    }
	}
      else
	/* zero-length dash array, i.e. solid line type */
	{
	  desired_line_type = CGM_L_SOLID;
	  desired_dash_offset = 0.0;
	}
    }
  else
    /* dash array not in effect or cannot be used, use one of CGM's
       canonical line types instead */
    {
      switch (_plotter->drawstate->line_type)
	{
	case L_SOLID:
	default:
	  desired_line_type = CGM_L_SOLID;
	  break;
	case L_DOTTED:
	  desired_line_type = CGM_L_DOTTED;
	  break;
	case L_DOTDASHED:
	  desired_line_type = CGM_L_DOTDASHED;
	  break;
	case L_SHORTDASHED:
	  desired_line_type = CGM_L_DASHED;
	  break;
	case L_LONGDASHED:
	  /* can't distinguish from shortdashed */
	  desired_line_type = CGM_L_DASHED;
	  break;
	case L_DOTDOTDASHED:
	  desired_line_type = CGM_L_DOTDOTDASHED;
	  break;
	case L_DOTDOTDOTDASHED:
	  /* map to "dotdotdashed" */
	  desired_line_type = CGM_L_DOTDOTDASHED;
	  break;
	}

      desired_dash_offset = 0.0;
    }
      
  switch (object_type)
    {
    case CGM_OBJECT_OPEN:
      if (_plotter->cgm_line_type != desired_line_type)
	/* emit "LINE TYPE" command */
	{
	  int byte_count, data_byte_count, data_len;
	  
	  data_len = 2;		/* 2 bytes per index */
	  byte_count = data_byte_count = 0;
	  _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
				    CGM_ATTRIBUTE_ELEMENT, 2,
				    data_len, &byte_count,
				    "LINETYPE");
	  _cgm_emit_index (_plotter->data->page, false, _plotter->cgm_encoding,
			   desired_line_type,
			   data_len, &data_byte_count, &byte_count);
	  _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
					&byte_count);
	  /* update line type */
	  _plotter->cgm_line_type = desired_line_type;
	}

      if (_plotter->cgm_max_version >= 3
	  && _plotter->cgm_dash_offset != desired_dash_offset)
	/* emit "LINE TYPE INITIAL OFFSET" command */
	{
	  int byte_count, data_byte_count, data_len;
	  
	  data_len = 4;		/* 4 bytes per fixed-pt. real */
	  byte_count = data_byte_count = 0;
	  _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
				    CGM_ATTRIBUTE_ELEMENT, 40,
				    data_len, &byte_count,
				    "LINETYPEINITOFFSET");
	  _cgm_emit_real_fixed_point (_plotter->data->page, false, _plotter->cgm_encoding,
				      desired_dash_offset,
				      data_len, &data_byte_count, &byte_count);
	  _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
					&byte_count);
	  /* update dash offset, and CGM version needed for this page */
	  _plotter->cgm_dash_offset = desired_dash_offset;
	  _plotter->cgm_page_version = IMAX(3, _plotter->cgm_page_version);
	}
      break;

    case CGM_OBJECT_CLOSED:
      if (_plotter->cgm_edge_type != desired_line_type)
	/* emit "EDGE TYPE" command */
	{
	  int byte_count, data_byte_count, data_len;
	  
	  data_len = 2;		/* 2 bytes per index */
	  byte_count = data_byte_count = 0;
	  _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
				    CGM_ATTRIBUTE_ELEMENT, 27,
				    data_len, &byte_count,
				    "EDGETYPE");
	  _cgm_emit_index (_plotter->data->page, false, _plotter->cgm_encoding,
			   desired_line_type,
			   data_len, &data_byte_count, &byte_count);
	  _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
					&byte_count);
	  /* update edge type */
	  _plotter->cgm_edge_type = desired_line_type;
	}

      if (_plotter->cgm_max_version >= 3
	  && _plotter->cgm_edge_dash_offset != desired_dash_offset)
	/* emit "EDGE TYPE INITIAL OFFSET" command */
	{
	  int byte_count, data_byte_count, data_len;
	  
	  data_len = 4;		/* 4 bytes per fixed-pt. real */
	  byte_count = data_byte_count = 0;
	  _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
				    CGM_ATTRIBUTE_ELEMENT, 47,
				    data_len, &byte_count,
				    "EDGETYPEINITOFFSET");
	  _cgm_emit_real_fixed_point (_plotter->data->page, false, _plotter->cgm_encoding,
				      desired_dash_offset,
				      data_len, &data_byte_count, &byte_count);
	  _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
					&byte_count);
	  /* update dash offset, and CGM version needed for this page */
	  _plotter->cgm_edge_dash_offset = desired_dash_offset;
	  _plotter->cgm_page_version = IMAX(3, _plotter->cgm_page_version);
	}
      break;

    default:
      break;
    }

  if (_plotter->cgm_max_version >= 3)
    /* have line/edge cap/join style, and miter limit commands */
    {
      int desired_join_style = _cgm_join_style[_plotter->drawstate->join_type];
      int desired_cap_style = _cgm_cap_style[_plotter->drawstate->cap_type];
      double desired_miter_limit = _plotter->drawstate->miter_limit;
      
      switch (object_type)
	{
	case CGM_OBJECT_OPEN:
	  if (_plotter->cgm_cap_style != desired_cap_style)
	    /* emit "LINE CAP" command */
	    {
	      int byte_count, data_byte_count, data_len;
	      
	      /* set line cap style */
	      data_len = 2 * 2;	/* 2 bytes per index */
	      byte_count = data_byte_count = 0;
	      _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
					CGM_ATTRIBUTE_ELEMENT, 37,
					data_len, &byte_count,
					"LINECAP");
	      _cgm_emit_index (_plotter->data->page, false, _plotter->cgm_encoding,
			       desired_cap_style,
			       data_len, &data_byte_count, &byte_count);
	      _cgm_emit_index (_plotter->data->page, false, _plotter->cgm_encoding,
			       CGM_DASH_CAP_MATCH,
			       data_len, &data_byte_count, &byte_count);
	      _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
					    &byte_count);
	      /* update cap style, and CGM version needed for this page */
	      _plotter->cgm_cap_style = desired_cap_style;
	      _plotter->cgm_page_version = IMAX(3, _plotter->cgm_page_version);
	    }
	  break;
	case CGM_OBJECT_CLOSED:
	  if (_plotter->cgm_edge_cap_style != desired_cap_style)
	    /* emit "EDGE CAP" command */
	    {
	      int byte_count, data_byte_count, data_len;
	      
	      data_len = 2 * 2;	/* 2 bytes per index */
	      byte_count = data_byte_count = 0;
	      _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
					CGM_ATTRIBUTE_ELEMENT, 44,
					data_len, &byte_count,
					"EDGECAP");
	      _cgm_emit_index (_plotter->data->page, false, _plotter->cgm_encoding,
			       desired_cap_style,
			       data_len, &data_byte_count, &byte_count);
	      _cgm_emit_index (_plotter->data->page, false, _plotter->cgm_encoding,
			       CGM_DASH_CAP_MATCH,
			       data_len, &data_byte_count, &byte_count);
	      _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
					    &byte_count);
	      /* update edge cap style, and CGM version needed for this page */
	      _plotter->cgm_edge_cap_style = desired_cap_style;
	      _plotter->cgm_page_version = IMAX(3, _plotter->cgm_page_version);
	    }
	  break;
	default:
	  break;
	}
  
      switch (object_type)
	{
	case CGM_OBJECT_OPEN:
	  if (_plotter->cgm_join_style != desired_join_style)
	    /* emit "LINE JOIN" command */
	    {
	      int byte_count, data_byte_count, data_len;
	      
	      /* set line join style */
	      data_len = 2;	/* 2 bytes per index */
	      byte_count = data_byte_count = 0;
	      _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
					CGM_ATTRIBUTE_ELEMENT, 38,
					data_len, &byte_count,
					"LINEJOIN");
	      _cgm_emit_index (_plotter->data->page, false, _plotter->cgm_encoding,
			       desired_join_style,
			       data_len, &data_byte_count, &byte_count);
	      _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
					    &byte_count);
	      /* update join style, and CGM version needed for this page */
	      _plotter->cgm_join_style = desired_join_style;
	      _plotter->cgm_page_version = IMAX(3, _plotter->cgm_page_version);
	    }
	  break;
	case CGM_OBJECT_CLOSED:
	  if (_plotter->cgm_edge_join_style != desired_join_style)
	    /* emit "EDGE JOIN" command */
	    {
	      int byte_count, data_byte_count, data_len;
	      
	      /* do it over again, this time for edge join style */
	      data_len = 2;	/* 2 bytes per index */
	      byte_count = data_byte_count = 0;
	      _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
					CGM_ATTRIBUTE_ELEMENT, 45,
					data_len, &byte_count,
					"EDGEJOIN");
	      _cgm_emit_index (_plotter->data->page, false, _plotter->cgm_encoding,
			       desired_join_style,
			       data_len, &data_byte_count, &byte_count);
	      _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
					    &byte_count);
	      /* update edge join style, and CGM version needed for this page*/
	      _plotter->cgm_edge_join_style = desired_join_style;
	      _plotter->cgm_page_version = IMAX(3, _plotter->cgm_page_version);
	    }
	  break;
	default:
	  break;
	}

      if (_plotter->cgm_miter_limit != desired_miter_limit)
	/* emit "MITRE LIMIT" command */
	{
	  int byte_count, data_byte_count, data_len;
	  
	  data_len = 4;	/* 4 bytes per fixed-point real */
	  byte_count = data_byte_count = 0;
	  _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
				    CGM_CONTROL_ELEMENT, 19,
				    data_len, &byte_count,
				    "MITRELIMIT");
	  _cgm_emit_real_fixed_point (_plotter->data->page, false, _plotter->cgm_encoding,
				      desired_miter_limit,
				      data_len, &data_byte_count, &byte_count);
	  _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
					&byte_count);
	  /* update miter limit, and CGM version needed for this page */
	  _plotter->cgm_miter_limit = desired_miter_limit;
	  _plotter->cgm_page_version = IMAX(3, _plotter->cgm_page_version);
	}
    }
}