summaryrefslogtreecommitdiff
path: root/src/XDraw.c
blob: 03fde2ab9bde299b3bda51ad20747d17407f787c (plain)
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/* $Xorg: XDraw.c,v 1.4 2001/02/09 02:04:05 xorgcvs Exp $ */

/*

Copyright 1987, 1998  The Open Group

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.

*/
/* $XFree86: xc/lib/oldX/XDraw.c,v 1.5 2001/12/14 19:57:02 dawes Exp $ */

#include "Xlibint.h"
#include "Xlib.h"
#include "X10.h"

#define OK_RETURN 1
#define ERR_RETURN 0
#include <stddef.h>

/*

Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the name of Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

*/

/*
 *
 * Vertices_converter: This internal routine takes a list of Vertices and
 *                     returns another list of Vertices such that the
 *                     following is true:
 *
 *         (1) No Vertex has the VertexRelative flag on
 *         (2) No Vertex has the VertexCurved flag on
 *         (3) The first Vertex has the VertexDontDraw flag on
 *         (4) The path that the return Vertex list specifies should
 *             be drawn on the screen is very close to the path that the
 *             Vertex list passed to vertices_converter specified should be
 *             drawn.  The difference is due solely to the approximation
 *             of curved segments in the original Vertex list by many
 *             small straight line segments that approximate the curve.
 *
 *    Notes:
 *
 *         (1) The first Vertex must have the VertexRelative flag
 *             turned off.  (This is not checked, just assumed)
 *         (2) VertexDontDraw is automatically turned on for the first
 *             Vertex because drawing to the first Vertex makes no sense.
 *         (3) This routine is used only by XDraw & XDrawFilled.
 *         (4) A 0 is returned iff no error occurs.
 *         (5) The passed Vertex list not changed in any way.
 *
 *    Implementation:
 *
 *         A routine from the qvss device dependent code library from the
 *         X version 10 sources written by DEC was used, abet slightly
 *         modified as it provided 99% of the desired functionality.
 *
 */

/*****************************************************************************/
/*                                                                          */
/*                  WRITTEN BY DARYL F. HAZEL      8509.06                  */
/*                                                                          */
/*  Modification history:						    */
/*									    */
/*  Carver 8510.23  Fixed first time allocation of the coord buffer	    */
/*		    to allocate max(200, pathcount) amount insted of just   */
/*		    200.  Potential Xserver crash problem.		    */
/*									    */
/*  Carver 8510.21  Replaced old module by module worked on by Ram Rao and  */
/*		    Bob Scheifler to get better performace.		    */
/*									    */
/*  Carver 8510.03  Changed the curve line converter to return the coord    */
/*		    path if a successor point does not exist for curve gen. */
/*                                                                          */
/*  Carver 8510.03  Fixed memory loss problem.  A coord path is only 	    */
/*		    allocated if the current path will not fit in it and    */
/*		    before the new coord path is allocated the old one is   */
/*                  freed.        					    */
/*                                                                          */
/*  Carver 8509.24  Fixed pointer/integer mismatch {*ppathaddr_new = ...}   */
/*                                                                          */
/*  Corkum 8509.18  Modify code to intergrate into the draw curve command   */
/*                                                                          */
/*                  Modified by Mark Lillibridge 5/20/87 to make into       */
/*		    X11 Xlib XDraw() support.                               */
/****************************************************************************/
							 
/*
 * Definitions:
 *
 */

typedef short   WORD;
typedef	short	*WORD_POINTER;

/*
 * Lookup tables, buffers used in the conversion:
 *
 */


/*
 * modified path list storage information:
 */
static Vertex *pathaddr_new = NULL;  /*pointer to current modified path list*/
static int pathlist_bytes = 0; /* size of modified path list in bytes       */
static int pathlist_segs = 0;/* size of modified path list in segments    */
static Vertex *pathaddr_coords = NULL; /*ptr to path list with coordinates */
static int path_coord_list_bytes = 0;  /* size of path list (w/coords) buff */
static int path_coord_list_segs = 0;   /* size of path list (w/coords) buff */

/*
 * THE TABLE OF PSEUDO FLOATING-POINT NUMBERS WHICH MAKE UP THE
 * MULTIPLICATIVE COEFFICIENTS FOR THE CUBIC SPLINE ALGORITHM.
 */
static unsigned short bntable[] = {
			    0xfc18, 0x7f97, 0x0476, 0xffde,
			    0xf8be, 0x7e5d, 0x0972, 0xff76,
			    0xf5f4, 0x7c56, 0x0eec, 0xfecc,
			    0xf3ba, 0x7988, 0x14da, 0xfde6,
			    0xf20b, 0x75fe, 0x1b2f, 0xfcca,
			    0xf0e2, 0x71c1, 0x21dd, 0xfb81,
			    0xf038, 0x6ce1, 0x28d3, 0xfa16,
			    0xf001, 0x676d, 0x3000, 0xf894,
			    0xf034, 0x6176, 0x3750, 0xf709,
			    0xf0c3, 0x5b0f, 0x3eae, 0xf583,
			    0xf1a1, 0x544b, 0x4605, 0xf410,
			    0xf2c0, 0x4d41, 0x4d41, 0xf2c0,
			    0xf410, 0x4605, 0x544b, 0xf1a1,
			    0xf583, 0x3eae, 0x5b0f, 0xf0c3,
			    0xf709, 0x3750, 0x6176, 0xf034,
			    0xf894, 0x3000, 0x676d, 0xf001,
			    0xfa16, 0x28d3, 0x6ce1, 0xf038,
			    0xfb81, 0x21dd, 0x71c1, 0xf0e2,
			    0xfcca, 0x1b2f, 0x75fe, 0xf20b,
			    0xfde6, 0x14da, 0x7988, 0xf3ba,
			    0xfecc, 0x0eec, 0x7c56, 0xf5f4,
			    0xff76, 0x0972, 0x7e5d, 0xf8be,
			    0xffde, 0x0476, 0x7f97, 0xfc18
			    };
			    
/*
 * this is the table which determines how many straight-line segments to draw
 * for a given curved segment.
 */
static short segtable[] = {  10,  3, 56,	/* pl <= 10, 3 segments     */
			     32,  4, 40,	/* pl <= 32, 4 segments     */
			     80,  6, 24,	/* pl <= 80, 6 segments     */
			     96,  8, 16,	/* pl <= 96, 8 segments     */
			     192, 12, 8,	/* pl <= 192, 12 segments   */
			     32767, 24, 00	/* pl <= 32767, 24 segments */
			   };
			     
/*
 *
 * The Real routine:
 *
 */
							 
static int vertices_converter(pathaddr, pathcount, ppathaddr_new, newpathcnt)
  Vertex  *pathaddr;	    /* address of original path list             */
  int	  pathcount;        /* number of points in original path list    */
  Vertex  **ppathaddr_new;  /* ptr to addr of new path list      */
  int     *newpathcnt;	    /* ptr to new number of pts in path list */
{
  int p0x, p0y;   	    /* coordinates of curve's predecessor point  */
  int p1x, p1y;   	    /* coordinates of curve's starting point     */
  int p2x, p2y;   	    /* coordinates of curve's ending point       */
  int p3x, p3y;   	    /* coordinates of curve's successor point    */
  int	flag;	    	    /* flag word of current end-point in p.list  */
  int successor_x = 0;	    /* X coordinate of curve's successor point   */
  int successor_y = 0;	    /* Y coordinate of curve's successor point   */
  int little_endian;	    /* stupid bntable is little-endian */
  WORD increment;	    /* bntable-ptr-increment value       */
  WORD diffx, diffy;	    /* absolute values of x, y end-point diffs   */
  WORD lmajoraxis;	    /* "length" of seg projected onto major axis */
  union		/* accumulator variables for computing end-points... */
    {	    	/* ...using the bntable.                             */
      struct
	{
	  WORD low;
	  WORD high;
	} sword;
      int lword;
    } xxe, yye;
  register Vertex *pnewpath;/* pointer for traversing modified path list */
  int	    newpathcount;   /* number of end-points in modified path list*/
  register Vertex *pflag;   /* pointer used for looking for fig's last pt*/
  int	    i;	    	    /* general-purpose loop variable             */
  int	    ecf_flag_set;   /* flag indicating existence of set ECF flag */
  int       count;
  int	    curve_flag;	    /* 0 = no curves in path; 1 = curves in path */
  
  /*
   * The following is necessary because of the pseudo-floating point
   * calculations done down below.
   */
  i = 1;
  if (*(char *) &i)
    little_endian = 1;
  else
    little_endian = 0;

  {
    register Vertex *poldpath;/* pointer for traversing original path list */
    register Vertex *p_coord_path; /* ptr to path list with coordinates    */

    /* Init the return path count to 0 in case of error */
    *newpathcnt = 0;
    newpathcount = 0;
    
    /* Use path buffers from last time */
    pnewpath = pathaddr_new;
    p_coord_path = pathaddr_coords;
    
    /*    
     * if size of coordinate path list buffer is not at least large enough to
     * accommodate the specified path list, allocate enough memory to do so
     */
    if (path_coord_list_segs == 0) 		/* first time through*/
      {
	path_coord_list_segs = max (200, pathcount);
	path_coord_list_bytes = path_coord_list_segs * sizeof(Vertex);
	if ((pathaddr_coords = (Vertex *)Xmalloc(path_coord_list_bytes)) != 0)
	  p_coord_path = pathaddr_coords;
	else
	  return(ERR_RETURN);
      }
    else    	    	    	    	/* all but first time through       */
      {
	/* If coord buffer is large enough for this path then reuse it, other
	   wise free the old one and get a new one (Carver) 8510.03 */
	if (path_coord_list_segs < pathcount)
	  {
	    Xfree( pathaddr_coords);
	    path_coord_list_segs = pathcount;
	    path_coord_list_bytes = path_coord_list_segs * sizeof(Vertex);
	    if ((pathaddr_coords =(Vertex *)Xmalloc(path_coord_list_bytes)) !=0)
	      p_coord_path = pathaddr_coords;
	    else
	      return(ERR_RETURN);
	  };
      }

    /*
     * Copy given Vertex list (pathaddr) to coordinate buffer converting
     * relative coordinates to absolute coordinates.
     *
     * Sets curve_flag iff at least one Vertex has the VertexCurved flag set.
     * 
     */
    
    poldpath = pathaddr;
    curve_flag = 0;
    
    *p_coord_path = *poldpath++;       /* First Vertex is a special case */
    p_coord_path->flags = (p_coord_path->flags | VertexDontDraw)
                          & ~VertexRelative;
    if ((p_coord_path++)->flags & VertexCurved)
      curve_flag = 1;
    for (i = pathcount - 1; i > 0; i--)
      {
	if (poldpath->flags & VertexRelative) 
	  {                           /* compute coordinates using last pt */
	    p_coord_path->x = poldpath->x + p_coord_path[-1].x;
	    p_coord_path->y = poldpath->y + p_coord_path[-1].y;
	    p_coord_path->flags = poldpath->flags & ~VertexRelative;
	  }
    	else *p_coord_path = *poldpath;
	p_coord_path++;
    	if ((poldpath++)->flags & VertexCurved)
	  curve_flag = 1;
      }	    
  }        /* End of scope for poldaddr, p_coord_path */

  /*
   * if it has been determined that there are no curved segment end points in
   * the specified path list, return the path list containing the coordinates
   * to the calling routine.
   */
  if (!curve_flag)
    {
      *newpathcnt    = pathcount;
      *ppathaddr_new = pathaddr_coords;
      return(OK_RETURN);
    }
  
  /*
   * if size of modified path list buffer is not at least large enough to
   * accommodate the specified path list allocate enough memory to do so
   */
  pathaddr = pathaddr_coords;
  if (pathlist_segs < pathcount)
    {
      if (pathlist_segs == 0)           /* Fix storage leak -- MDL 5/20/87 */
	Xfree( pathaddr_new );
      pathlist_segs = pathcount;
      pathlist_bytes = pathcount * sizeof(Vertex);
      if ((pathaddr_new = (Vertex *)Xmalloc(pathlist_bytes)) != 0)
	pnewpath = pathaddr_new;
      else
	return(ERR_RETURN);
    }

  /*
   * initialize the beginning and ending coordinates of the first segment
   */
  p1x = 0;
  p1y = 0;
  p2x = 0;
  p2y = 0;
  
  {
    register WORD *pbntable;	/* table of multiplicative coeffics.*/
    register WORD *psegtable;	/* table used to detrmn num.sub-segs*/
    register WORD m;	    	/* num segments into which curve is divided */

    /*
     * MAIN LOOP OF THE PATH_LIST_CONVERTER ROUTINE
     */
    for ( count=pathcount ; count > 0; count--)
      {
	p0x = p1x;    /* save previous values of path-list coordinate pairs*/
	p0y = p1y;
	p1x = p2x;
	p1y = p2y;
	
	/*
    	 * read next end-point's coordinates from the path list
	 */
	p2x = pathaddr->x;
	p2y = pathaddr->y;
	flag = (pathaddr++)->flags;
	
	/*
	 * CURVED-segment considerations
	 */
	if (flag & VertexCurved)
	  {
	    /*
	     * determine which point to use as the successor point:  the next
	     * point in the list (if there is one), or a previously-saved point
	     * (when drawing closed figures)
	     */
	    if (flag & VertexEndClosed)	/* last segment of closed fig*/
	      {
		p3x = successor_x;
		p3y = successor_y;
	      }
    	    else    	   	    	/* stand-alone curved segment*/
	      {
		if (count <= 1) 	/* no points to use as successor pt */
		  {
		    
		    /* in this case draw the coord path */
		    *newpathcnt    = pathcount;
		    *ppathaddr_new = pathaddr_coords;
		    return(OK_RETURN);
		  }
		else
		  {
		    /*
    	    	     * read next end-point's coordinates from the path list...
	    	     */
	    	    p3x = pathaddr->x;
	    	    p3y = pathaddr->y;
		    
	    	    if (flag & VertexStartClosed)
		      {      /* first segment of closed figure */
			/*
			 * save P3 as successor pt for closed-fig-ending curve
			 */
			successor_x = p3x;
			successor_y = p3y;
			
			/*
			 * Traverse the original path list looking for ECF flag
    	    	    	 * After finding it, back pointer up to previous point
			 * and save the information for use as curve's pred. pt
			 */
    	    	    	ecf_flag_set = 0;
			pflag = pathaddr;
			for (i = count; i > 0; i--)
			  {
			    if (pflag->flags & VertexEndClosed)
			      {
				pflag--;
	    	    	    	p1x = pflag->x;
	    	    	    	p1y = pflag->y;
    	    	    	    	ecf_flag_set = 1;
				break;
			      }
			    pflag++;
			  }
    	    	    	if (!ecf_flag_set)
			  return(ERR_RETURN);
		      }     /* end code pertaining to starting figure*/
		  }	    /* end code pertaining to successor points   */
	      }   	    /* end code pertaining to stand-alone curved segs*/
	    
	    if ((flag & VertexDontDraw) == 0)
	      {
		/*
	    	 * determine the "length" of the segment along the major axis
	    	 */
	    	if ((diffx = p2x - p1x) < 0)
		  diffx = ~diffx + 1;
	    	if ((diffy = p2y - p1y) < 0)
		  diffy = ~diffy + 1;
	    	lmajoraxis = (diffx >= diffy) ? diffx : diffy;
	    	if (lmajoraxis == 0)	/* for vector to have length of...  */
		  lmajoraxis = 1;	/* ...at least one                  */
		
		/*
	    	 * compute M, the number of sub-segments into which a curved 
		 * segment is divided
		 */
	    	psegtable = segtable;
	    	while (lmajoraxis > *psegtable++)/* search for appropriate..*/
		  psegtable += 2;		 /* ...table entry          */
	    	m = *psegtable++;		 /* read number of segments */
	    	increment = *psegtable >> 1;
		
		/*
    	    	 * determine if there is enough room remaining in the modified
		 * path-list buffer to hold ALL of the curve's sub-segment in-
		 * formation; if not, double the buffer size (if possible).
		 */
		while ((newpathcount + m) > pathlist_segs)
		  {
		    pathlist_segs *= 2;
		    pathlist_bytes *= 2;
		    if ((pathaddr_new = (Vertex *)Xrealloc(pathaddr_new, 
							 pathlist_bytes)) != 0)
		      pnewpath = pathaddr_new + newpathcount;
	    	    else
		      return(ERR_RETURN);
		  }
		
		/*
	    	 * generate end-points of sub-segs into which curve is divided
		 */
	    	pbntable = (WORD *) bntable; /* initialize the pointer to the Bn table*/
	    	for ( ; m > 1; m--)
		  {
		    pbntable += increment;
		    xxe.lword = *pbntable * p0x;
		    yye.lword = *pbntable++ * p0y;
		    xxe.lword += *pbntable * p1x;
		    yye.lword += *pbntable++ * p1y;
		    xxe.lword += *pbntable * p2x;
		    yye.lword += *pbntable++ * p2y;
		    xxe.lword += *pbntable * p3x;
		    yye.lword += *pbntable++ * p3y;
		    xxe.lword = xxe.lword << 1;	/* double values...  */
		    yye.lword = yye.lword << 1;	/* ...bn accts for it*/
		    xxe.lword += 0x8000; /* round off the accum value */
		    yye.lword += 0x8000;
		    if (little_endian) {
			pnewpath->x = xxe.sword.high;/* the X coordinate  */
			pnewpath->y = yye.sword.high;/* the Y coordinate  */
		    } else {
			pnewpath->x = xxe.sword.low;/* the X coordinate  */
			pnewpath->y = yye.sword.low;/* the Y coordinate  */
		    }
		    (pnewpath++)->flags = 0;	/* the flag word     */
    	    	    newpathcount++;/* increment segment end-point countr*/
		  }	    	   /* end sub-segment end-point compute loop*/
	      }   	   /* end DRAW-segment considerations code      */
	  }	    	   /* end CURVED-segment considerations code        */
	
	/*
    	 * put end-point from original path list into modified list
	 */
    	pnewpath->x = p2x;
	pnewpath->y = p2y;
	(pnewpath++)->flags = flag & ~VertexCurved;
	newpathcount++;
	
	/*
    	 * determine whether or not the modified path list is full, and if so,
	 * double its size
	 */
	if (newpathcount == pathlist_segs)
	  {
	    pathlist_bytes *= 2;
	    pathlist_segs *= 2;
	    if ((pathaddr_new = (Vertex *)Xrealloc(pathaddr_new, 
						 pathlist_bytes)) != 0)
	      pnewpath = pathaddr_new + newpathcount;
    	    else
	      return(ERR_RETURN);
	  }
      }	    	    /* END of PATH_LIST_CONVERTER's main loop        */
    
    /*
     * return the address of the modified path list and the number of segment
     * end-points it contains
     */
    *newpathcnt    = newpathcount;
    *ppathaddr_new = pathaddr_new;
  }    

  return(OK_RETURN);
}

/* Written by Mark Lillibridge */

static XPoint *XDraw_points = NULL;    /* Buffer to hold list of points for */
static int    point_count = 0;         /* use in calling XDrawLines         */

Status XDraw (dpy, d, gc, vlist, vcount)
    register Display *dpy;
    Drawable d;
    GC gc;
    Vertex *vlist;
    int vcount;
{
  Vertex *newvlist;
  int newvcount;
  XPoint *p;
  int pcount;

  /* If less than 2 vertices, we don't have to do anything (no lines) */
  if (vcount<2)
    return(OK_RETURN);

  /* Convert curved lines to straight lines & change relative references to */
  /* absolute references. */
  if (!vertices_converter( vlist, vcount, &newvlist, &newvcount))
    return(ERR_RETURN);

  /* Insure we have enough buffer space */
  if (point_count < newvcount) {
    if (point_count != 0)
      Xfree( XDraw_points );
    if (!(XDraw_points = (XPoint *) Xmalloc( newvcount * sizeof(XPoint) )))
      return(ERR_RETURN);
    point_count = newvcount;
  }

  /*
   * Draw the lines defined by newvlist using seperate XDrawLines calls
   * to insure that all the lines that should be joined are and that closed
   * figures are joined correctly.
   */
  while (newvcount>0) {
    p = XDraw_points;               /* Put points in buffer */
    pcount = 0;
    p->x = newvlist->x;             /* Copy first point */
    (p++)->y = (newvlist++)->y;
    newvcount--; pcount++;

    /*
     * Copy more points until out of points or need to stop XDrawLine
     * because either we don't want to join to the next point
     * (VertexDontDraw) or we want to stop after the next point so that
     * a closed figure will be joined correctly.  (We have to stop before
     * a VertexStartClosed because the vertex marked VertexStartClosed
     * must be the first vertex in its XDrawLines call to get joining
     * to work correctly.
     */
    while (newvcount > 0 && !(newvlist->flags & (VertexDontDraw |
						 VertexStartClosed |
						 VertexEndClosed))) {
      p->x = newvlist->x;
      (p++)->y = (newvlist++)->y;
      newvcount--; pcount++;
    }

    /*
     * If stopped only because of need to start a new XDrawLines, copy
     * next point but don't advance pointer so two XdrawLines act like one.
     */
    if ( newvcount > 0 && !(newvlist->flags & VertexDontDraw) ) {
      p->x = newvlist->x;
      (p++)->y = newvlist->y;
      pcount++;
    }

    /* Do the XDrawLines if there are any lines to draw */
    if (pcount>1)
      XDrawLines(dpy, d, gc, XDraw_points, pcount, CoordModeOrigin);
  }

  return(OK_RETURN);
}

Status XDrawFilled (dpy, d, gc, vlist, vcount)
    register Display *dpy;
    Drawable d;
    GC gc;
    Vertex *vlist;
    int vcount;
{
  Vertex *newvlist;
  int newvcount;
  XPoint *p;
  int pcount;

  /* If less than 2 vertices, we don't have to do anything (no lines) */
  if (vcount<2)
    return(OK_RETURN);

  /* Convert curved lines to straight lines & change relative references to */
  /* absolute references. */
  if (!vertices_converter( vlist, vcount, &newvlist, &newvcount))
    return(ERR_RETURN);

  /* Insure we have enough buffer space */
  if (point_count < newvcount) {
    if (point_count != 0)
      Xfree( XDraw_points );
    if (!(XDraw_points = (XPoint *) Xmalloc( newvcount * sizeof(XPoint) )))
      return(ERR_RETURN);
    point_count = newvcount;
  }

  /*
   * Draw the lines defined by newvlist using seperate XDrawLines calls
   * to insure that all the lines that should be joined are and that closed
   * figures are joined correctly.
   */
  while (newvcount>0) {
    p = XDraw_points;               /* Put points in buffer */
    pcount = 0;
    p->x = newvlist->x;             /* Copy first point */
    (p++)->y = (newvlist++)->y;
    newvcount--; pcount++;

    /*
     * Copy more points until out of points or need to stop XDrawLine
     * because either we don't want to join to the next point
     * (VertexDontDraw) or we want to stop after the next point so that
     * a closed figure will be joined correctly.  (We have to stop before
     * a VertexStartClosed because the vertex marked VertexStartClosed
     * must be the first vertex in its XDrawLines call to get joining
     * to work correctly.
     */
    while (newvcount > 0 && !(newvlist->flags & (VertexStartClosed |
						 VertexEndClosed))) {
      p->x = newvlist->x;
      (p++)->y = (newvlist++)->y;
      newvcount--; pcount++;
    }

    /*
     * If stopped only because of need to start a new XDrawLines, copy
     * next point but don't advance pointer so two XdrawLines act like one.
     */
    if ( newvcount > 0 ) {
      p->x = newvlist->x;
      (p++)->y = newvlist->y;
      pcount++;
    }

    /* Do the XDrawLines if there are any lines to draw */
    if (pcount>1) {
	  XFillPolygon(dpy, d, gc, XDraw_points, pcount, Complex,
		       CoordModeOrigin);
    }
  }

  return(OK_RETURN);
}