aboutsummaryrefslogtreecommitdiff
path: root/generic/Draw.c
blob: ce48b3d7fb399b2ca6f2c34af52153ae7b4a52a8 (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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
/*
 * Draw.c -- Implementation of common drawing routines.
 *
 * Authors		: Patrick Lecoanet.
 * Creation date	: Sat Dec 10 12:51:30 1994
 *
 * $Id$
 */

/*
 *  Copyright (c) 1993 - 1999 CENA, Patrick Lecoanet --
 *
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this code; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


/*
 **********************************************************************************
 *
 * The algorihms used to draw the arrows, to do the 3d effects and to
 * smooth the polygons are adapted from Tk.
 *
 **********************************************************************************
 */

#include "config.h"
#include "Types.h"
#include "Draw.h"
#include "Geo.h"
#include "List.h"
#include "WidgetInfo.h"

#include <math.h>
#include <stdarg.h>


#define	POLYGON_RELIEF_DRAW	0
#define	POLYGON_RELIEF_DIST	1
#define	POLYGON_RELIEF_BBOX	2
#define	POLYGON_RELIEF_IN_BBOX	3

#define	TOP_CONTRAST		13
#define BOTTOM_CONTRAST		6
#define MAX_INTENSITY		65535

#define ARROW_SHAPE_B		10.0
#define ARROW_SHAPE_C		5.0
#define OPEN_ARROW_SHAPE_A	4.0
#define CLOSED_ARROW_SHAPE_A	ARROW_SHAPE_B

#define LIGHTNING_SHAPE_A_RATIO	10.0
#define LIGHTNING_SHAPE_B_RATIO	8.0


/*
 **********************************************************************************
 *
 * SetLineStyle -- 
 *
 **********************************************************************************
 */
void
SetLineStyle(Display	*display,
	     GC		gc,
	     LineStyle	line_style)
{
  XGCValues		values;
  static const char	dashed[] = { 8 };
  static const char	dotted[] = { 2, 5 };
  static const char	mixed[]  = { 8, 5, 2, 5 };

  values.line_style = LineOnOffDash;
  switch (line_style) {
  case LINE_DASHED :
    XSetDashes(display, gc, 0, dashed, 1);
    break;
  case LINE_MIXED :
    XSetDashes(display, gc, 0, mixed, 4);
    break;
  case LINE_DOTTED :
    XSetDashes(display, gc, 0, dotted, 2);
    break;
  default:
    values.line_style = LineSolid;
    break;
  }
  XChangeGC(display, gc, GCLineStyle, &values);
}


/*
 **********************************************************************************
 *
 * GetLineShape --
 *	Compute the points describing the given line shape between point p1 and p2.
 *	If bbox is non null, it is filled with the bounding box of the shape.
 *
 * For the time being this procedure handles straight lines, right and left
 * lightnings, right and left corners, right and left double corners..
 *
 *
 * Here are the parameters for lightnings:
 *
 *                                        *******
 *                                 *******     *
 *                           ******           *
 *                     ******      ******+   *
 *               ******      ******     *   *|
 *         ******      ******          *   * | LIGHTNING_SHAPE_A
 *   ******      ******               *   *  |
 *         ******                    *   *   |
 * ..******.........................+.+.*........................******..
 *   |                             *   *                    ******
 *   |                            *   *               ******      ******
 *   |                           *   *          ******      ******
 *   |                          *   *     ******      ******
 *   |                         *   *******      ******
 *   |                        *          ******
 *   |                       *     ******
 *   |                      ********   
 *   |                         |    | |
 *   |                         |----| | LIGHTNING_SHAPE_B
 *   |                                |
 *   |--------------------------------| LENGTH / 2
 *
 **********************************************************************************
 */
void
GetLineShape(ZnPoint		*p1,
	     ZnPoint		*p2,
	     unsigned int	line_width,
	     LineShape		shape,
	     ZnBBox		*bbox,
	     ZnList		to_points)
{
  ZnPoint	*points;
  int		num_points, i;

  /*
   * Compute all line points according to shape.
   */
  if ((shape == LINE_LEFT_LIGHTNING) ||
      (shape == LINE_RIGHT_LIGHTNING)) {
    double	alpha, theta;
    double	length, length2;
    double	shape_a, shape_b;
    double	dx, dy;
    double	temp;

    num_points = LIGHTNING_POINTS;
    ZnListAssertSize(to_points, num_points);
    points = (ZnPoint *) ZnListArray(to_points);

    points[0] = *p1;
    points[3] = *p2;

    dx = p2->x - p1->x;
    dy = p2->y - p1->y;
    length = hypot(dx, dy);
    shape_a = length/LIGHTNING_SHAPE_A_RATIO + ((double) line_width)/2;
    shape_b = length/LIGHTNING_SHAPE_B_RATIO + ((double) line_width)/2;

    if (shape == LINE_LEFT_LIGHTNING)
      alpha = atan2(shape_a, shape_b);
    else
      alpha = -atan2(shape_a, shape_b);
    length2 = hypot(shape_a, shape_b);
    theta = atan2(-dy, dx);

    dx = p1->x + dx/2;
    dy = p1->y + dy/2;
    temp = cos(theta + alpha) * length2;
    points[1].x = dx + temp;
    points[2].x = dx - temp;
    temp = sin(theta + alpha) * length2;
    points[1].y = dy - temp;
    points[2].y = dy + temp;
  }
  else if (shape == LINE_LEFT_CORNER ||
	   shape == LINE_RIGHT_CORNER) {
    num_points = CORNER_POINTS;
    ZnListAssertSize(to_points, num_points);
    points = (ZnPoint *) ZnListArray(to_points);

    points[0] = *p1;
    points[2] = *p2;

    if (shape == LINE_LEFT_CORNER) {
      points[1].x = p1->x;
      points[1].y = p2->y;
    }
    else {
      points[1].x = p2->x;
      points[1].y = p1->y;
    }
  }
  else if (shape == LINE_DOUBLE_LEFT_CORNER ||
	   shape == LINE_DOUBLE_RIGHT_CORNER) {
    int	dx, dy;

    num_points = DOUBLE_CORNER_POINTS;
    ZnListAssertSize(to_points, num_points);
    points = (ZnPoint *) ZnListArray(to_points);

    points[0] = *p1;
    points[3] = *p2;

    if (shape == LINE_DOUBLE_LEFT_CORNER) {
      dy = p2->y - p1->y;
      points[1].x = p1->x;
      points[2].x = p2->x;
      points[1].y = points[2].y = p1->y + dy/2;
    }
    else {
      dx = p2->x - p1->x;
      points[1].x = points[2].x = p1->x + dx/2;
      points[1].y = p1->y;
      points[2].y = p2->y;
    }
  }
  else /* if (shape) == LINE_STRAIGHT) */ {
    num_points = STRAIGHT_POINTS;
    ZnListAssertSize(to_points, num_points);
    points = (ZnPoint *) ZnListArray(to_points);

    points[0] = *p1;
    points[1] = *p2;
  }

  /*
   * Fill in the bbox, if requested.
   */
  if (bbox) {
    ResetBBox(bbox);
    for (i = 0; i < num_points; i++) {
      AddPointToBBox(bbox, points[i].x, points[i].y);
    }
    
    /* Enlarge to take line_width into account. */
    if (line_width > 1) {
      int lw_2 = (line_width+1)/2;
      
      bbox->orig.x -= lw_2;
      bbox->orig.y -= lw_2;
      bbox->corner.x += lw_2;
      bbox->corner.y += lw_2;
    }
  }
}


/*
 **********************************************************************************
 *
 * DrawLineShape --
 *	Draw a line given the points describing its path. It is designed to work
 *	with GetLineShape albeit it does fairly trivial things. In the future some
 *	shapes might need cooperation between the two and the clients will be ready
 *	for that.
 *
 *
 **********************************************************************************
 */
void
DrawLineShape(WidgetInfo	*wi,
	      ZnPoint		*p,
	      int		num_p,
	      LineStyle		line_style,
	      ZnColor		foreground,
	      unsigned int	line_width,
	      LineShape		shape)
{
  XPoint	*xpoints;
  int		i;
  XGCValues	values;

  /*
   * Setup GC.
   */
  SetLineStyle(wi->dpy, wi->gc, line_style);
  values.foreground = ZnPixel(foreground);
  values.line_width = (line_width == 1) ? 0 : line_width;
  values.fill_style = FillSolid;
  values.join_style = JoinRound;
  values.cap_style = CapRound;
  XChangeGC(wi->dpy, wi->gc,
	    GCFillStyle|GCLineWidth|GCJoinStyle|GCCapStyle|GCForeground, &values);
  ZnListAssertSize(wi->work_xpts, num_p);
  xpoints = (XPoint *) ZnListArray(wi->work_xpts);
  for (i = 0; i < num_p; i++) {
    xpoints[i].x = p[i].x;
    xpoints[i].y = p[i].y;
  }
  XDrawLines(wi->dpy, wi->draw_buffer, wi->gc, xpoints, num_p, CoordModeOrigin);
}


/*
 * ReliefIndexOfSegment --
 */
static long
ReliefColorOfSegment(ZnReal		x1,
		     ZnReal		y1,
		     ZnReal		x2,
		     ZnReal		y2,
		     ReliefStyle	relief,
		     ZnGradient		*gradient,
		     WidgetInfo		*wi)
{
  ZnReal	angle, angle_step, origin, position;
  int		num_colors;
  
  num_colors = RELIEF_STEPS*2+1;
  angle_step = 2*M_PI / num_colors;
  origin = -(DegreesToRadian(wi->light_angle))-(angle_step/2.0);
  if (relief == RELIEF_BEVEL_IN) {
    origin += M_PI;
  }

  angle = (ProjectionToAngle(y1 - y2, x2 - x1) + M_PI - origin);
  while (angle < 0.0) {
    angle += 2*M_PI;
  }
  while (angle > 2*M_PI) {
    angle -= 2*M_PI;
  }	
  position = (angle/angle_step)*(100/num_colors);
  /*printf("position %g, angle %g, origin %g\n",
    position,
    RadianToDegrees(angle),
    RadianToDegrees(origin));*/

  return ZnPixel(ZnGetGradientColor(wi->win, gradient, position));
}


/*
 **********************************************************************************
 *
 * DrawRectangleRelief --
 *	Draw the bevels inside bbox.
 *
 **********************************************************************************
 */
void
DrawRectangleRelief(WidgetInfo		*wi,
		    ReliefStyle		relief,
		    ZnGradient		*gradient,
		    XRectangle		*bbox,
		    unsigned int	line_width)
{
  XPoint	bevel[4];
  
  /*
   * If we haven't enough space to draw, exit.
   */
  if ((bbox->width < 2*line_width) || (bbox->height < 2*line_width)) {
    return;
  }
  
  /*
   * Grooves and ridges are drawn with two recursives calls with
   * half the width of the original one.
   */
  if ((relief == RELIEF_RIDGE) || (relief == RELIEF_GROOVE)) {
    unsigned int	new_line_width;
    int			offset;
    XRectangle		internal_bbox;
    
    new_line_width = line_width/2;
    offset = line_width - new_line_width;
    DrawRectangleRelief(wi,
 			(relief==RELIEF_GROOVE)?RELIEF_BEVEL_IN:RELIEF_BEVEL_OUT,
 			gradient, bbox, new_line_width);
    internal_bbox = *bbox;
    internal_bbox.x +=offset;
    internal_bbox.y += offset;
    internal_bbox.width -= offset*2;
    internal_bbox.height -= offset*2;
    DrawRectangleRelief(wi,
 			(relief==RELIEF_GROOVE)?RELIEF_BEVEL_OUT:RELIEF_BEVEL_IN,
 			gradient, &internal_bbox, new_line_width);
    return;
  }

  XSetFillStyle(wi->dpy, wi->gc, FillSolid);
  
  bevel[0].x = bbox->x;
  bevel[0].y = bevel[1].y = bbox->y;
  bevel[1].x = bbox->x + bbox->width;
  bevel[2].y = bevel[3].y = bbox->y + line_width;
  bevel[2].x = bevel[1].x - line_width;
  bevel[3].x = bevel[0].x + line_width;  
  XSetForeground(wi->dpy, wi->gc,
		 ReliefColorOfSegment(bevel[1].x, bevel[1].y, bevel[0].x, bevel[0].y,
				      relief, gradient, wi));
  XFillPolygon(wi->dpy, wi->draw_buffer, wi->gc, bevel, 4, Convex, CoordModeOrigin);

  bevel[0] = bevel[1];
  bevel[3] = bevel[2];
  bevel[1].y += bbox->height;
  bevel[2].y = bevel[1].y - line_width;
  XSetForeground(wi->dpy, wi->gc,
		 ReliefColorOfSegment(bevel[1].x, bevel[1].y, bevel[0].x, bevel[0].y,
				      relief, gradient, wi));
  XFillPolygon(wi->dpy, wi->draw_buffer, wi->gc, bevel, 4, Convex, CoordModeOrigin);

  bevel[0] = bevel[1];
  bevel[3] = bevel[2];
  bevel[1].x -= bbox->width;
  bevel[2].x = bevel[1].x + line_width;
  XSetForeground(wi->dpy, wi->gc,
		 ReliefColorOfSegment(bevel[1].x, bevel[1].y, bevel[0].x, bevel[0].y,
				      relief, gradient, wi));
  XFillPolygon(wi->dpy, wi->draw_buffer, wi->gc, bevel, 4, Convex, CoordModeOrigin);

  bevel[0] = bevel[1];
  bevel[3] = bevel[2];
  bevel[1].x = bbox->x;
  bevel[1].y = bbox->y;
  bevel[2].x = bevel[3].x;
  bevel[2].y = bbox->y + line_width;
  XSetForeground(wi->dpy, wi->gc,
		 ReliefColorOfSegment(bevel[1].x, bevel[1].y, bevel[0].x, bevel[0].y,
				      relief, gradient, wi));
  XFillPolygon(wi->dpy, wi->draw_buffer, wi->gc, bevel, 4, Convex, CoordModeOrigin);
}


static void
DoPolygonRelief(ZnPoint		*p,
		int		num_points,
		int		line_width,
		int		what_to_do,
		...)
{
  int			i, j, processed_points, *result=NULL;
  ZnPoint		*p1, *p11=NULL, *p2;
  ZnPoint		pp1, pp2, new_pp1, new_pp2;
  ZnPoint		perp, c, shift1, shift2;
  ZnPoint		bevel_points[4];
  XPoint		bevel_xpoints[5];
  ZnBool		folded, closed, colinear;
  WidgetInfo		*wi = NULL;
  ReliefStyle		relief = 0;
  ZnGradient		*gradient = NULL;
  ZnPoint		*pp = NULL;
  double		*dist = NULL;
  ZnBBox		*bbox = NULL;
  va_list		var;
#if 0
  ZnBool		toggle=True;
#endif
  ZnReal		dx, dy;
  
  va_start(var, what_to_do);
  if (what_to_do == POLYGON_RELIEF_DIST) {
    pp = va_arg(var, ZnPoint *);
    dist = va_arg(var, double *);
    *dist = 1.0e40;
  }
  if (what_to_do == POLYGON_RELIEF_IN_BBOX) {
    bbox = va_arg(var, ZnBBox *);
    result = va_arg(var, int *);
  }
  else if (what_to_do == POLYGON_RELIEF_BBOX) {
    bbox = va_arg(var, ZnBBox *);
    ResetBBox(bbox);
  }
  else if (what_to_do == POLYGON_RELIEF_DRAW) {
    wi = va_arg(var, WidgetInfo *);
    relief = va_arg(var, int);
    gradient = va_arg(var, ZnGradient *);
  }
  va_end(var);

  /*
   * If the polygon is closed (last point is the same as first) open it by
   * dropping the last point. The algorithm closes the path automatically.
   * We remember this to decide if we draw the last bevel or not and if we
   * need to generate ends perpendicular to the path..
   */
  closed = False;
  if ((p->x == p[num_points-1].x) && (p->y == p[num_points-1].y)) {
    closed = True;
    num_points--;
  }
  /*printf("num_points=%d(%s)\n", num_points, closed?"closed":"");*/
  
  /*
   * We loop on all vertices of the polygon.
   * At each step we try to compute the corresponding border
   * corner `corner'. Then we build a polygon for the bevel.
   * Things look like this:
   *
   *          bevel[1]     /
   *             *        /
   *		 |       /
   *             |      /
   *         pp1 *    * p[i-1]
   *             |    | bevel[0]
   *             |    |
   *             |    |
   *             |    | bevel[3]
   *             |    | p[i]
   *             |    | p1                 p2
   *         pp2 *    *--------------------*
   *             |
   *             |
   *      corner *----*--------------------*
   *     bevel[2]   new_pp1             new_pp2
   *
   * pp1 and pp2 are the ends of a segment // to p1 p2 at line_width
   * from it. These points are *NOT* necessarily on the perpendicular
   * going through p1 or p2.
   * This loop needs a bootstrap phase of two iterations (i.e we need to
   * process two points). This is why we start at the point before the last
   * and then wrap to the first point.
   * The algorithm discards any duplicate contiguous points.
   * It makes a special case if two consecutives edges are folded:
   *
   *  bevel[1]      pp1            pp2        a bevel[2]
   *    *-----------*--------------*----------*
   *                                           \
   *                                            \
   *     p[i-1]                                  \  bevel[3]
   *       *--------*-------------------------*---* corner
   *    bevel[0]    p2                       p1  /
   *                                            /
   *                                           /
   *      ----------*-----------*-------------*
   *             new_pp1     new_pp2          c
   *
   * In such a case we need to compute a, c, corner from pp1, pp2, new_pp1
   * and new_pp2. We compute the perpendicular to p1,p2 through p1, intersect
   * it with pp1,pp2 to obtain a, intersect it with new_pp1, new_pp2 to
   * obtain c, shift a,c and intersect it with p1,p2 to obtain corner.
   *
   */

  processed_points = 0;
  if (!closed) {
    i = 0;
    p1 = p;
  }
  else {
    i = -2;
    p1 = &p[num_points-2];
  }
  for (p2 = p1+1; i < num_points; i++, p2++) {
    /*
     * When it is time to wrap, do it
     */
    if ((i == -1) || (i == num_points-1)) {
      p2 = p;
    }
    /*
     * Skip over close vertices.
     */
    dx = p2->x - p1->x;
    dy = p2->y - p1->y;
    if ((ABS(dx) < 1.0) && (ABS(dy) < 1.0)) {
      continue;
    }

    ShiftLine(p1, p2, line_width, &new_pp1, &new_pp2);
    bevel_points[3] = *p1;
    folded = False;
    colinear = False;
    /*
     * The first two cases are for `open' polygons. We compute
     * a bevel closure that is perpendicular to the path.
     */
    if ((processed_points == 0) && !closed) {
      perp.x = p1->x + (p2->y - p1->y);
      perp.y = p1->y - (p2->x - p1->x);
      IntersectLines(p1, &perp, &new_pp1, &new_pp2, &bevel_points[2]);
    }
    else if ((processed_points == num_points-1) && !closed) {
      perp.x = p1->x + (p11->y - p1->y);
      perp.y = p1->y - (p11->x - p1->x);
      IntersectLines(p1, &perp, &pp1, &pp2, &bevel_points[2]);      
    }
    else if (processed_points >= 1) {
      ZnReal	dotp, dist, odx, ody;
      
      /*
       * The dot product of the two faces tell if the are
       * folded or colinear. The
       */
      odx = p11->x - p1->x;
      ody = p11->y - p1->y;
      dotp = odx*dx + ody*dy;
      dist = LineToPointDist(p11, p2, p1);
      if ((dist < 4.0) && (dotp <= 0)) {
	perp.x = p1->x + (p2->y - p1->y);
	perp.y = p1->y - (p2->x - p1->x);
	IntersectLines(p1, &perp, &new_pp1, &new_pp2, &bevel_points[2]);
	colinear = True;
      }
      else {
	folded = !IntersectLines(&new_pp1, &new_pp2, &pp1, &pp2, &bevel_points[2]);
	folded = folded && (dotp < 0);
	if (folded) {
	  /*printf("DoPolygonRelief: folded edges detected, %g@%g, %g@%g, %g@%g, %g@%g\n",
		 pp1.x, pp1.y, pp2.x, pp2.y, new_pp1.x, new_pp1.y,
		 new_pp2.x, new_pp2.y);*/
	  perp.x = p1->x + (p2->y - p1->y);
	  perp.y = p1->y - (p2->x - p1->x);
	  IntersectLines(p1, &perp, &pp1, &pp2, &bevel_points[2]);
	  IntersectLines(p1, &perp, &new_pp1, &new_pp2, &c);
	  ShiftLine(p1, &perp, line_width, &shift1, &shift2);
	  IntersectLines(p1, p2, &shift1, &shift2, &bevel_points[3]);
	}
      }
    }

    if ((processed_points >= 2) || (!closed && (processed_points == 1))) {
      if (what_to_do == POLYGON_RELIEF_DIST) {
	double	new_dist;

	new_dist = PolygonToPointDist(bevel_points, 4, pp);
	if (new_dist < 0) {
	  new_dist = 0;
	}
	*dist = MIN(*dist, new_dist);
      }
      else if (what_to_do == POLYGON_RELIEF_IN_BBOX) {
	if (processed_points <= 2) {
	  *result = PolygonInBBox(bevel_points, 4, bbox, NULL);
	  if (*result == 0) {
	    return;
	  }
	}
	else {
	  if (PolygonInBBox(bevel_points, 4, bbox, NULL) != *result) {
	    *result = 0;
	    return;
	  }
	}
      }
      else if (what_to_do == POLYGON_RELIEF_BBOX) {
	int	i;
	
	for (i = 0; i < 4; i++) {
	  AddPointToBBox(bbox, bevel_points[i].x, bevel_points[i].y);
	}
      }
      else if (what_to_do == POLYGON_RELIEF_DRAW) {
	XGCValues	gc_values;
#if 1
	gc_values.foreground = ReliefColorOfSegment(bevel_points[0].x, bevel_points[0].y,
						    bevel_points[3].x, bevel_points[3].y,
						    relief, gradient, wi);
#endif
#if 0
	gc_values.foreground = toggle ? WhitePixelOfScreen(wi->screen):BlackPixelOfScreen(wi->screen);
#endif
#if 0
	gc_values.foreground = colinear ? WhitePixelOfScreen(wi->screen):BlackPixelOfScreen(wi->screen);
#endif
#if 0	
	toggle = !toggle;
#endif
	gc_values.fill_style = FillSolid;
	XChangeGC(wi->dpy, wi->gc, GCFillStyle|GCForeground, &gc_values);
	
	for (j = 0; j < 4; j++) {
	  bevel_xpoints[j].x = REAL_TO_INT(bevel_points[j].x);
	  bevel_xpoints[j].y = REAL_TO_INT(bevel_points[j].y);
	}
#if 1
	XFillPolygon(wi->dpy, wi->draw_buffer, wi->gc, bevel_xpoints, 4,
		     Convex, CoordModeOrigin);
#endif
#if 0
	bevel_xpoints[4] = bevel_xpoints[0];
	gc_values.line_width = 0;
	XChangeGC(wi->dpy, wi->gc, GCLineWidth, &gc_values);
	XDrawLines(wi->dpy, wi->draw_buffer, wi->gc, bevel_xpoints, 5,
		   CoordModeOrigin);
#endif
      }
    }

    p11 = p1;
    p1 = p2;
    pp1 = new_pp1;
    pp2 = new_pp2;
    bevel_points[0] = bevel_points[3];
    if (folded) {
      bevel_points[1] = c;
    }
    else if ((processed_points >= 1) || !closed) {
      bevel_points[1] = bevel_points[2];
    }

    processed_points++;
  }
}


/*
 **********************************************************************************
 *
 * GetPolygonReliefBBox --
 *	Returns the bevelled polygon bounding box.
 *
 **********************************************************************************
 */
void
GetPolygonReliefBBox(ZnPoint	*points,
		     int	num_points,
		     int	line_width,
		     ZnBBox	*bbox)
{
  DoPolygonRelief(points, num_points, line_width, POLYGON_RELIEF_BBOX, bbox);
}


/*
 **********************************************************************************
 *
 * PolygonReliefInBBox --
 *	Returns (-1) if the relief is entirely outside the bbox, (1) if it is
 *	entirely inside or (0) if in between
 *
 **********************************************************************************
 */
int
PolygonReliefInBBox(ZnPoint	*points,
		    int		num_points,
		    int		line_width,
		    ZnBBox	*area)
{
  int	result;

  DoPolygonRelief(points, num_points, line_width, POLYGON_RELIEF_IN_BBOX, area, &result);

  return result;
}


/*
 **********************************************************************************
 *
 * PolygonReliefToPointDist --
 *	Returns the distance between the given point and
 *	the bevelled polygon.
 *
 **********************************************************************************
 */
double
PolygonReliefToPointDist(ZnPoint	*points,
			 int		num_points,
			 int		line_width,
			 ZnPoint	*pp)
{
  double	dist;

  DoPolygonRelief(points, num_points, line_width, POLYGON_RELIEF_DIST, pp, &dist);

  return dist;
}


/*
 **********************************************************************************
 *
 * DrawPolygonRelief --
 *	Draw the bevels around path.
 *
 **********************************************************************************
 */
void
DrawPolygonRelief(WidgetInfo	*wi,
		  ReliefStyle	relief,
		  ZnGradient	*gradient,
		  ZnPoint	*points,
		  int		num_points,
		  int		line_width)
{
  /*
   * Grooves and ridges are drawn with two calls. The first
   * with the original width, the second with half the width.
   */
  if ((relief == RELIEF_RIDGE) || (relief == RELIEF_GROOVE)) {
    DoPolygonRelief(points, num_points, line_width, POLYGON_RELIEF_DRAW, wi,
  		    (int) (relief==RELIEF_GROOVE)?RELIEF_BEVEL_OUT:RELIEF_BEVEL_IN,
 		    gradient);
    DoPolygonRelief(points, num_points, line_width/2, POLYGON_RELIEF_DRAW, wi,
 		    (int) (relief==RELIEF_GROOVE)?RELIEF_BEVEL_IN:RELIEF_BEVEL_OUT,
 		    gradient);
  }
  else {
    DoPolygonRelief(points, num_points, line_width, POLYGON_RELIEF_DRAW, wi,
		    (int) relief, gradient);
  }
}

static void
DrawRadialGradient(struct _WidgetInfo	*wi,
		  ZnGradient		*grad,
		  XRectangle		*bbox)
{
}

static void
DrawAxialGradient(struct _WidgetInfo	*wi,
		  ZnGradient		*grad,
		  XRectangle		*bbox)
{
#define NUM_STENCILS 8
  static int	s[NUM_STENCILS] = { 0, 1, 3, 5, 7, 9, 11, 13 };
  int		angle = grad->g.angle;
  int		num_shades = grad->num_shades;
  int		num_shades_2 = num_shades /2;
  int		num_colors = grad->num_colors;
  int		x, y, span, stop, i, j, k, p;
  int		*startp, *widthp, width, height;
  int		position;
  ZnReal	pos, pos_span_bc, pos_span_ac, pos_span;
  ZnReal	stencil_step_bc, stencil_step_ac, stencil_step;
  short		origin, limit;
  ZnBool	dir;
  ZnGradientColor *color, *next_color;
  XColor	*shade;

  /*
   * We can only handle 0, 90, 180, 270.
   */
  angle = (angle / 90) * 90;
  
  x = bbox->x;
  y = bbox->y;
  if ((angle == 0) || (angle == 180)) {
    startp = &x;
    origin = bbox->x;
    limit = bbox->x+bbox->width;
    height = bbox->height;
    widthp = &width;
  }
  else {
    startp = &y;
    origin = bbox->y;
    limit = bbox->y+bbox->height;
    width = bbox->width;
    widthp = &height;
  }
  
  dir = True;
  if ((angle == 180) || (angle == 270)) {
    dir = False;
  }

  span = limit - origin;
  
  /*
   * External loop iterates over the gradient colors.
   */
  XSetTSOrigin(wi->dpy, wi->gc, bbox->x, bbox->y);
  ZnRealizeGradient(grad, wi->win);
  if (dir) {
    *startp = origin;
  }
  else {
    stop = limit;
  }
  for (k = 0; k < num_colors-1; k++) {
    /*
     * Mid loop iterates over the current color shades.
     */
    color = grad->colors[k];
    next_color = grad->colors[k+1];
    position = color->position;
    pos_span_bc = (next_color->position - position)*color->control/100.0;
    pos_span_ac = (next_color->position - position)-pos_span_bc;
    stencil_step_bc = pos_span_bc/(num_shades_2*NUM_STENCILS);
    stencil_step_ac = pos_span_ac/(num_shades_2*NUM_STENCILS);
    /*printf("span bc %g, span ac %g, stencil bc %g, stencil ac %g\n",
      pos_span_bc, pos_span_ac, stencil_step_bc, stencil_step_ac);*/
    pos_span = pos_span_bc;
    stencil_step = stencil_step_bc;
    pos = position;
    for (j = 0; j < num_shades; j++) {
      p = j+1;
      if (j >= num_shades_2) {
	pos = position + pos_span_bc;
	pos_span = pos_span_ac;
	stencil_step = stencil_step_ac;
	p -= num_shades_2;
      }
      if (dir) {
	stop = origin + span*(pos + (pos_span*p/(num_shades_2)))/100;
      }
      else {
	*startp = limit - span*(pos + (pos_span*p/(num_shades_2)))/100;
      }
      *widthp = stop - *startp;
      XSetFillStyle(wi->dpy, wi->gc, FillSolid);
      shade = color->shades[j];
      XSetForeground(wi->dpy, wi->gc, ZnPixel(shade));
      XFillRectangle(wi->dpy, wi->draw_buffer, wi->gc, x, y, width, height);
#if 1
      /*
       * Inner loop iterates over the stencils used to
       * smooth the current shade.
       */
      p--;
      if (dir) {
	*startp = origin + span*(pos + (stencil_step*(p*NUM_STENCILS+1)))/100;
      }
      else {
	stop = limit - span*(pos + (stencil_step*(p*NUM_STENCILS+1)))/100;
      }
      XSetFillStyle(wi->dpy, wi->gc, FillStippled);
      if (j < num_shades-1) {
	shade = color->shades[j+1];
      }
      else {
	shade = next_color->shades[0];
      }
      XSetForeground(wi->dpy, wi->gc, ZnPixel(shade));
      for (i = 1; i < NUM_STENCILS; i++) {
	if (dir) {
	  stop = origin + span*(pos + (stencil_step*(p*NUM_STENCILS+i+1)))/100;
	}
	else {
	  *startp = limit - span*(pos + (stencil_step*(p*NUM_STENCILS+i+1)))/100;
	}
	*widthp = stop - *startp;
	XSetStipple(wi->dpy, wi->gc, wi->alpha_stipples[s[i]]);
	XFillRectangle(wi->dpy, wi->draw_buffer, wi->gc, x, y, width, height);
	if (dir) {
	  *startp = stop;
	}
	else {
	  stop = *startp;
	}
      }
#else
      if (dir) {
	*startp = stop;
      }
      else {
	stop = *startp;
      }
#endif
    }
  }
}

void
DrawPolygonGradient(struct _WidgetInfo	*wi,
		    ZnGradient		*gradient,
		    ZnPoly		*poly,
		    ZnBBox		*bbox)
{
  ZnBBox	lbbox;
  XRectangle	r;
  int		i;
  
  /*
   * The polygon has to be reduced for this to give meaningful
   * results.
   */
  if (!bbox) {
    ResetBBox(&lbbox);
    for (i = 0; i < poly->num_contours; i++) {
      if (!poly->holes[i]) {
	AddPointsToBBox(&lbbox, poly->contours[i].points,
			poly->contours[i].num_points);
      }
    }
    bbox = &lbbox;
  }
  BBox2XRect(bbox, &r);
  ITEM_P.PushClip(wi, poly, False, True);
  if (gradient->type == ZN_AXIAL_GRADIENT) {
    DrawAxialGradient(wi, gradient, &r);
  }
  else if (gradient->type == ZN_RADIAL_GRADIENT) {
    DrawRadialGradient(wi, gradient, &r);
  }
  ITEM_P.PopClip(wi, True);
}

void
DrawRectangleGradient(struct _WidgetInfo	*wi,
		      ZnGradient		*grad,
		      XRectangle		*bbox)
{
  if (grad->type == ZN_AXIAL_GRADIENT) {
    DrawAxialGradient(wi, grad, bbox);
  }
  else if (grad->type == ZN_RADIAL_GRADIENT) {
    ZnPoly	poly;
    ZnPoint	points[2];

    points[0].x = bbox->x;
    points[0].y = bbox->y;
    points[1].x = bbox->x + bbox->width;
    points[1].y = bbox->x + bbox->height;
    POLY_CONTOUR1(&poly, points, 2);
    ITEM_P.PushClip(wi, &poly, False, True);
    DrawRadialGradient(wi, grad, bbox);
    ITEM_P.PopClip(wi, True);
  }
}