Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libtk/generic/tkCanvPoly.c
1810 views
1
/*
2
* tkCanvPoly.c --
3
*
4
* This file implements polygon items for canvas widgets.
5
*
6
* Copyright (c) 1991-1994 The Regents of the University of California.
7
* Copyright (c) 1994 Sun Microsystems, Inc.
8
*
9
* See the file "license.terms" for information on usage and redistribution
10
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11
*
12
* SCCS: @(#) tkCanvPoly.c 1.34 96/02/15 18:52:32
13
*/
14
15
#include "tkInt.h"
16
17
/*
18
* The structure below defines the record for each polygon item.
19
*/
20
21
typedef struct PolygonItem {
22
Tk_Item header; /* Generic stuff that's the same for all
23
* types. MUST BE FIRST IN STRUCTURE. */
24
int numPoints; /* Number of points in polygon (always >= 3).
25
* Polygon is always closed. */
26
int pointsAllocated; /* Number of points for which space is
27
* allocated at *coordPtr. */
28
double *coordPtr; /* Pointer to malloc-ed array containing
29
* x- and y-coords of all points in polygon.
30
* X-coords are even-valued indices, y-coords
31
* are corresponding odd-valued indices. */
32
int width; /* Width of outline. */
33
XColor *outlineColor; /* Color for outline. */
34
GC outlineGC; /* Graphics context for drawing outline. */
35
XColor *fillColor; /* Foreground color for polygon. */
36
Pixmap fillStipple; /* Stipple bitmap for filling polygon. */
37
GC fillGC; /* Graphics context for filling polygon. */
38
int smooth; /* Non-zero means draw shape smoothed (i.e.
39
* with Bezier splines). */
40
int splineSteps; /* Number of steps in each spline segment. */
41
} PolygonItem;
42
43
/*
44
* Information used for parsing configuration specs:
45
*/
46
47
static Tk_CustomOption tagsOption = {Tk_CanvasTagsParseProc,
48
Tk_CanvasTagsPrintProc, (ClientData) NULL
49
};
50
51
static Tk_ConfigSpec configSpecs[] = {
52
{TK_CONFIG_COLOR, "-fill", (char *) NULL, (char *) NULL,
53
"black", Tk_Offset(PolygonItem, fillColor), TK_CONFIG_NULL_OK},
54
{TK_CONFIG_COLOR, "-outline", (char *) NULL, (char *) NULL,
55
(char *) NULL, Tk_Offset(PolygonItem, outlineColor), TK_CONFIG_NULL_OK},
56
{TK_CONFIG_BOOLEAN, "-smooth", (char *) NULL, (char *) NULL,
57
"0", Tk_Offset(PolygonItem, smooth), TK_CONFIG_DONT_SET_DEFAULT},
58
{TK_CONFIG_INT, "-splinesteps", (char *) NULL, (char *) NULL,
59
"12", Tk_Offset(PolygonItem, splineSteps), TK_CONFIG_DONT_SET_DEFAULT},
60
{TK_CONFIG_BITMAP, "-stipple", (char *) NULL, (char *) NULL,
61
(char *) NULL, Tk_Offset(PolygonItem, fillStipple), TK_CONFIG_NULL_OK},
62
{TK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL,
63
(char *) NULL, 0, TK_CONFIG_NULL_OK, &tagsOption},
64
{TK_CONFIG_PIXELS, "-width", (char *) NULL, (char *) NULL,
65
"1", Tk_Offset(PolygonItem, width), TK_CONFIG_DONT_SET_DEFAULT},
66
{TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
67
(char *) NULL, 0, 0}
68
};
69
70
/*
71
* Prototypes for procedures defined in this file:
72
*/
73
74
static void ComputePolygonBbox _ANSI_ARGS_((Tk_Canvas canvas,
75
PolygonItem *polyPtr));
76
static int ConfigurePolygon _ANSI_ARGS_((Tcl_Interp *interp,
77
Tk_Canvas canvas, Tk_Item *itemPtr, int argc,
78
char **argv, int flags));
79
static int CreatePolygon _ANSI_ARGS_((Tcl_Interp *interp,
80
Tk_Canvas canvas, struct Tk_Item *itemPtr,
81
int argc, char **argv));
82
static void DeletePolygon _ANSI_ARGS_((Tk_Canvas canvas,
83
Tk_Item *itemPtr, Display *display));
84
static void DisplayPolygon _ANSI_ARGS_((Tk_Canvas canvas,
85
Tk_Item *itemPtr, Display *display, Drawable dst,
86
int x, int y, int width, int height));
87
static int PolygonCoords _ANSI_ARGS_((Tcl_Interp *interp,
88
Tk_Canvas canvas, Tk_Item *itemPtr,
89
int argc, char **argv));
90
static int PolygonToArea _ANSI_ARGS_((Tk_Canvas canvas,
91
Tk_Item *itemPtr, double *rectPtr));
92
static double PolygonToPoint _ANSI_ARGS_((Tk_Canvas canvas,
93
Tk_Item *itemPtr, double *pointPtr));
94
static int PolygonToPostscript _ANSI_ARGS_((Tcl_Interp *interp,
95
Tk_Canvas canvas, Tk_Item *itemPtr, int prepass));
96
static void ScalePolygon _ANSI_ARGS_((Tk_Canvas canvas,
97
Tk_Item *itemPtr, double originX, double originY,
98
double scaleX, double scaleY));
99
static void TranslatePolygon _ANSI_ARGS_((Tk_Canvas canvas,
100
Tk_Item *itemPtr, double deltaX, double deltaY));
101
102
/*
103
* The structures below defines the polygon item type by means
104
* of procedures that can be invoked by generic item code.
105
*/
106
107
Tk_ItemType tkPolygonType = {
108
"polygon", /* name */
109
sizeof(PolygonItem), /* itemSize */
110
CreatePolygon, /* createProc */
111
configSpecs, /* configSpecs */
112
ConfigurePolygon, /* configureProc */
113
PolygonCoords, /* coordProc */
114
DeletePolygon, /* deleteProc */
115
DisplayPolygon, /* displayProc */
116
0, /* alwaysRedraw */
117
PolygonToPoint, /* pointProc */
118
PolygonToArea, /* areaProc */
119
PolygonToPostscript, /* postscriptProc */
120
ScalePolygon, /* scaleProc */
121
TranslatePolygon, /* translateProc */
122
(Tk_ItemIndexProc *) NULL, /* indexProc */
123
(Tk_ItemCursorProc *) NULL, /* icursorProc */
124
(Tk_ItemSelectionProc *) NULL, /* selectionProc */
125
(Tk_ItemInsertProc *) NULL, /* insertProc */
126
(Tk_ItemDCharsProc *) NULL, /* dTextProc */
127
(Tk_ItemType *) NULL /* nextPtr */
128
};
129
130
/*
131
* The definition below determines how large are static arrays
132
* used to hold spline points (splines larger than this have to
133
* have their arrays malloc-ed).
134
*/
135
136
#define MAX_STATIC_POINTS 200
137
138
/*
139
*--------------------------------------------------------------
140
*
141
* CreatePolygon --
142
*
143
* This procedure is invoked to create a new polygon item in
144
* a canvas.
145
*
146
* Results:
147
* A standard Tcl return value. If an error occurred in
148
* creating the item, then an error message is left in
149
* interp->result; in this case itemPtr is
150
* left uninitialized, so it can be safely freed by the
151
* caller.
152
*
153
* Side effects:
154
* A new polygon item is created.
155
*
156
*--------------------------------------------------------------
157
*/
158
159
static int
160
CreatePolygon(interp, canvas, itemPtr, argc, argv)
161
Tcl_Interp *interp; /* Interpreter for error reporting. */
162
Tk_Canvas canvas; /* Canvas to hold new item. */
163
Tk_Item *itemPtr; /* Record to hold new item; header
164
* has been initialized by caller. */
165
int argc; /* Number of arguments in argv. */
166
char **argv; /* Arguments describing polygon. */
167
{
168
PolygonItem *polyPtr = (PolygonItem *) itemPtr;
169
int i;
170
171
if (argc < 6) {
172
Tcl_AppendResult(interp, "wrong # args: should be \"",
173
Tk_PathName(Tk_CanvasTkwin(canvas)), " create ",
174
itemPtr->typePtr->name,
175
" x1 y1 x2 y2 x3 y3 ?x4 y4 ...? ?options?\"", (char *) NULL);
176
return TCL_ERROR;
177
}
178
179
/*
180
* Carry out initialization that is needed in order to clean
181
* up after errors during the the remainder of this procedure.
182
*/
183
184
polyPtr->numPoints = 0;
185
polyPtr->pointsAllocated = 0;
186
polyPtr->coordPtr = NULL;
187
polyPtr->width = 1;
188
polyPtr->outlineColor = NULL;
189
polyPtr->outlineGC = None;
190
polyPtr->fillColor = NULL;
191
polyPtr->fillStipple = None;
192
polyPtr->fillGC = None;
193
polyPtr->smooth = 0;
194
polyPtr->splineSteps = 12;
195
196
/*
197
* Count the number of points and then parse them into a point
198
* array. Leading arguments are assumed to be points if they
199
* start with a digit or a minus sign followed by a digit.
200
*/
201
202
for (i = 4; i < (argc-1); i+=2) {
203
if ((!isdigit(UCHAR(argv[i][0]))) &&
204
((argv[i][0] != '-') || (!isdigit(UCHAR(argv[i][1]))))) {
205
break;
206
}
207
}
208
if (PolygonCoords(interp, canvas, itemPtr, i, argv) != TCL_OK) {
209
goto error;
210
}
211
212
if (ConfigurePolygon(interp, canvas, itemPtr, argc-i, argv+i, 0)
213
== TCL_OK) {
214
return TCL_OK;
215
}
216
217
error:
218
DeletePolygon(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));
219
return TCL_ERROR;
220
}
221
222
/*
223
*--------------------------------------------------------------
224
*
225
* PolygonCoords --
226
*
227
* This procedure is invoked to process the "coords" widget
228
* command on polygons. See the user documentation for details
229
* on what it does.
230
*
231
* Results:
232
* Returns TCL_OK or TCL_ERROR, and sets interp->result.
233
*
234
* Side effects:
235
* The coordinates for the given item may be changed.
236
*
237
*--------------------------------------------------------------
238
*/
239
240
static int
241
PolygonCoords(interp, canvas, itemPtr, argc, argv)
242
Tcl_Interp *interp; /* Used for error reporting. */
243
Tk_Canvas canvas; /* Canvas containing item. */
244
Tk_Item *itemPtr; /* Item whose coordinates are to be
245
* read or modified. */
246
int argc; /* Number of coordinates supplied in
247
* argv. */
248
char **argv; /* Array of coordinates: x1, y1,
249
* x2, y2, ... */
250
{
251
PolygonItem *polyPtr = (PolygonItem *) itemPtr;
252
char buffer[TCL_DOUBLE_SPACE];
253
int i, numPoints;
254
255
if (argc == 0) {
256
for (i = 0; i < 2*polyPtr->numPoints; i++) {
257
Tcl_PrintDouble(interp, polyPtr->coordPtr[i], buffer);
258
Tcl_AppendElement(interp, buffer);
259
}
260
} else if (argc < 6) {
261
Tcl_AppendResult(interp,
262
"too few coordinates for polygon: must have at least 6",
263
(char *) NULL);
264
return TCL_ERROR;
265
} else if (argc & 1) {
266
Tcl_AppendResult(interp,
267
"odd number of coordinates specified for polygon",
268
(char *) NULL);
269
return TCL_ERROR;
270
} else {
271
numPoints = argc/2;
272
if (polyPtr->pointsAllocated <= numPoints) {
273
if (polyPtr->coordPtr != NULL) {
274
ckfree((char *) polyPtr->coordPtr);
275
}
276
277
/*
278
* One extra point gets allocated here, just in case we have
279
* to add another point to close the polygon.
280
*/
281
282
polyPtr->coordPtr = (double *) ckalloc((unsigned)
283
(sizeof(double) * (argc+2)));
284
polyPtr->pointsAllocated = numPoints+1;
285
}
286
for (i = argc-1; i >= 0; i--) {
287
if (Tk_CanvasGetCoord(interp, canvas, argv[i],
288
&polyPtr->coordPtr[i]) != TCL_OK) {
289
return TCL_ERROR;
290
}
291
}
292
polyPtr->numPoints = numPoints;
293
294
/*
295
* Close the polygon if it isn't already closed.
296
*/
297
298
if ((polyPtr->coordPtr[argc-2] != polyPtr->coordPtr[0])
299
|| (polyPtr->coordPtr[argc-1] != polyPtr->coordPtr[1])) {
300
polyPtr->numPoints++;
301
polyPtr->coordPtr[argc] = polyPtr->coordPtr[0];
302
polyPtr->coordPtr[argc+1] = polyPtr->coordPtr[1];
303
}
304
ComputePolygonBbox(canvas, polyPtr);
305
}
306
return TCL_OK;
307
}
308
309
/*
310
*--------------------------------------------------------------
311
*
312
* ConfigurePolygon --
313
*
314
* This procedure is invoked to configure various aspects
315
* of a polygon item such as its background color.
316
*
317
* Results:
318
* A standard Tcl result code. If an error occurs, then
319
* an error message is left in interp->result.
320
*
321
* Side effects:
322
* Configuration information, such as colors and stipple
323
* patterns, may be set for itemPtr.
324
*
325
*--------------------------------------------------------------
326
*/
327
328
static int
329
ConfigurePolygon(interp, canvas, itemPtr, argc, argv, flags)
330
Tcl_Interp *interp; /* Interpreter for error reporting. */
331
Tk_Canvas canvas; /* Canvas containing itemPtr. */
332
Tk_Item *itemPtr; /* Polygon item to reconfigure. */
333
int argc; /* Number of elements in argv. */
334
char **argv; /* Arguments describing things to configure. */
335
int flags; /* Flags to pass to Tk_ConfigureWidget. */
336
{
337
PolygonItem *polyPtr = (PolygonItem *) itemPtr;
338
XGCValues gcValues;
339
GC newGC;
340
unsigned long mask;
341
Tk_Window tkwin;
342
343
tkwin = Tk_CanvasTkwin(canvas);
344
if (Tk_ConfigureWidget(interp, tkwin, configSpecs, argc, argv,
345
(char *) polyPtr, flags) != TCL_OK) {
346
return TCL_ERROR;
347
}
348
349
/*
350
* A few of the options require additional processing, such as
351
* graphics contexts.
352
*/
353
354
if (polyPtr->width < 1) {
355
polyPtr->width = 1;
356
}
357
if (polyPtr->outlineColor == NULL) {
358
newGC = None;
359
} else {
360
gcValues.foreground = polyPtr->outlineColor->pixel;
361
gcValues.line_width = polyPtr->width;
362
gcValues.cap_style = CapRound;
363
gcValues.join_style = JoinRound;
364
mask = GCForeground|GCLineWidth|GCCapStyle|GCJoinStyle;
365
newGC = Tk_GetGC(tkwin, mask, &gcValues);
366
}
367
if (polyPtr->outlineGC != None) {
368
Tk_FreeGC(Tk_Display(tkwin), polyPtr->outlineGC);
369
}
370
polyPtr->outlineGC = newGC;
371
372
if (polyPtr->fillColor == NULL) {
373
newGC = None;
374
} else {
375
gcValues.foreground = polyPtr->fillColor->pixel;
376
mask = GCForeground;
377
if (polyPtr->fillStipple != None) {
378
gcValues.stipple = polyPtr->fillStipple;
379
gcValues.fill_style = FillStippled;
380
mask |= GCStipple|GCFillStyle;
381
}
382
newGC = Tk_GetGC(tkwin, mask, &gcValues);
383
}
384
if (polyPtr->fillGC != None) {
385
Tk_FreeGC(Tk_Display(tkwin), polyPtr->fillGC);
386
}
387
polyPtr->fillGC = newGC;
388
389
/*
390
* Keep spline parameters within reasonable limits.
391
*/
392
393
if (polyPtr->splineSteps < 1) {
394
polyPtr->splineSteps = 1;
395
} else if (polyPtr->splineSteps > 100) {
396
polyPtr->splineSteps = 100;
397
}
398
399
ComputePolygonBbox(canvas, polyPtr);
400
return TCL_OK;
401
}
402
403
/*
404
*--------------------------------------------------------------
405
*
406
* DeletePolygon --
407
*
408
* This procedure is called to clean up the data structure
409
* associated with a polygon item.
410
*
411
* Results:
412
* None.
413
*
414
* Side effects:
415
* Resources associated with itemPtr are released.
416
*
417
*--------------------------------------------------------------
418
*/
419
420
static void
421
DeletePolygon(canvas, itemPtr, display)
422
Tk_Canvas canvas; /* Info about overall canvas widget. */
423
Tk_Item *itemPtr; /* Item that is being deleted. */
424
Display *display; /* Display containing window for
425
* canvas. */
426
{
427
PolygonItem *polyPtr = (PolygonItem *) itemPtr;
428
429
if (polyPtr->coordPtr != NULL) {
430
ckfree((char *) polyPtr->coordPtr);
431
}
432
if (polyPtr->fillColor != NULL) {
433
Tk_FreeColor(polyPtr->fillColor);
434
}
435
if (polyPtr->fillStipple != None) {
436
Tk_FreeBitmap(display, polyPtr->fillStipple);
437
}
438
if (polyPtr->outlineColor != NULL) {
439
Tk_FreeColor(polyPtr->outlineColor);
440
}
441
if (polyPtr->outlineGC != None) {
442
Tk_FreeGC(display, polyPtr->outlineGC);
443
}
444
if (polyPtr->fillGC != None) {
445
Tk_FreeGC(display, polyPtr->fillGC);
446
}
447
}
448
449
/*
450
*--------------------------------------------------------------
451
*
452
* ComputePolygonBbox --
453
*
454
* This procedure is invoked to compute the bounding box of
455
* all the pixels that may be drawn as part of a polygon.
456
*
457
* Results:
458
* None.
459
*
460
* Side effects:
461
* The fields x1, y1, x2, and y2 are updated in the header
462
* for itemPtr.
463
*
464
*--------------------------------------------------------------
465
*/
466
467
static void
468
ComputePolygonBbox(canvas, polyPtr)
469
Tk_Canvas canvas; /* Canvas that contains item. */
470
PolygonItem *polyPtr; /* Item whose bbox is to be
471
* recomputed. */
472
{
473
double *coordPtr;
474
int i;
475
476
coordPtr = polyPtr->coordPtr;
477
polyPtr->header.x1 = polyPtr->header.x2 = *coordPtr;
478
polyPtr->header.y1 = polyPtr->header.y2 = coordPtr[1];
479
480
for (i = 1, coordPtr = polyPtr->coordPtr+2; i < polyPtr->numPoints;
481
i++, coordPtr += 2) {
482
TkIncludePoint((Tk_Item *) polyPtr, coordPtr);
483
}
484
485
/*
486
* Expand bounding box in all directions to account for the outline,
487
* which can stick out beyond the polygon. Add one extra pixel of
488
* fudge, just in case X rounds differently than we do.
489
*/
490
491
i = (polyPtr->width+1)/2 + 1;
492
polyPtr->header.x1 -= i;
493
polyPtr->header.x2 += i;
494
polyPtr->header.y1 -= i;
495
polyPtr->header.y2 += i;
496
}
497
498
/*
499
*--------------------------------------------------------------
500
*
501
* TkFillPolygon --
502
*
503
* This procedure is invoked to convert a polygon to screen
504
* coordinates and display it using a particular GC.
505
*
506
* Results:
507
* None.
508
*
509
* Side effects:
510
* ItemPtr is drawn in drawable using the transformation
511
* information in canvas.
512
*
513
*--------------------------------------------------------------
514
*/
515
516
void
517
TkFillPolygon(canvas, coordPtr, numPoints, display, drawable, gc, outlineGC)
518
Tk_Canvas canvas; /* Canvas whose coordinate system
519
* is to be used for drawing. */
520
double *coordPtr; /* Array of coordinates for polygon:
521
* x1, y1, x2, y2, .... */
522
int numPoints; /* Twice this many coordinates are
523
* present at *coordPtr. */
524
Display *display; /* Display on which to draw polygon. */
525
Drawable drawable; /* Pixmap or window in which to draw
526
* polygon. */
527
GC gc; /* Graphics context for drawing. */
528
GC outlineGC; /* If not None, use this to draw an
529
* outline around the polygon after
530
* filling it. */
531
{
532
XPoint staticPoints[MAX_STATIC_POINTS];
533
XPoint *pointPtr;
534
XPoint *pPtr;
535
int i;
536
537
/*
538
* Build up an array of points in screen coordinates. Use a
539
* static array unless the polygon has an enormous number of points;
540
* in this case, dynamically allocate an array.
541
*/
542
543
if (numPoints <= MAX_STATIC_POINTS) {
544
pointPtr = staticPoints;
545
} else {
546
pointPtr = (XPoint *) ckalloc((unsigned) (numPoints * sizeof(XPoint)));
547
}
548
549
for (i = 0, pPtr = pointPtr; i < numPoints; i += 1, coordPtr += 2, pPtr++) {
550
Tk_CanvasDrawableCoords(canvas, coordPtr[0], coordPtr[1], &pPtr->x,
551
&pPtr->y);
552
}
553
554
/*
555
* Display polygon, then free up polygon storage if it was dynamically
556
* allocated.
557
*/
558
559
if (gc != None) {
560
XFillPolygon(display, drawable, gc, pointPtr, numPoints, Complex,
561
CoordModeOrigin);
562
}
563
if (outlineGC != None) {
564
XDrawLines(display, drawable, outlineGC, pointPtr,
565
numPoints, CoordModeOrigin);
566
}
567
if (pointPtr != staticPoints) {
568
ckfree((char *) pointPtr);
569
}
570
}
571
572
/*
573
*--------------------------------------------------------------
574
*
575
* DisplayPolygon --
576
*
577
* This procedure is invoked to draw a polygon item in a given
578
* drawable.
579
*
580
* Results:
581
* None.
582
*
583
* Side effects:
584
* ItemPtr is drawn in drawable using the transformation
585
* information in canvas.
586
*
587
*--------------------------------------------------------------
588
*/
589
590
static void
591
DisplayPolygon(canvas, itemPtr, display, drawable, x, y, width, height)
592
Tk_Canvas canvas; /* Canvas that contains item. */
593
Tk_Item *itemPtr; /* Item to be displayed. */
594
Display *display; /* Display on which to draw item. */
595
Drawable drawable; /* Pixmap or window in which to draw
596
* item. */
597
int x, y, width, height; /* Describes region of canvas that
598
* must be redisplayed (not used). */
599
{
600
PolygonItem *polyPtr = (PolygonItem *) itemPtr;
601
602
if ((polyPtr->fillGC == None) && (polyPtr->outlineGC == None)) {
603
return;
604
}
605
606
/*
607
* If we're stippling then modify the stipple offset in the GC. Be
608
* sure to reset the offset when done, since the GC is supposed to be
609
* read-only.
610
*/
611
612
if ((polyPtr->fillStipple != None) && (polyPtr->fillGC != None)) {
613
Tk_CanvasSetStippleOrigin(canvas, polyPtr->fillGC);
614
}
615
616
if (!polyPtr->smooth) {
617
TkFillPolygon(canvas, polyPtr->coordPtr, polyPtr->numPoints,
618
display, drawable, polyPtr->fillGC, polyPtr->outlineGC);
619
} else {
620
int numPoints;
621
XPoint staticPoints[MAX_STATIC_POINTS];
622
XPoint *pointPtr;
623
624
/*
625
* This is a smoothed polygon. Display using a set of generated
626
* spline points rather than the original points.
627
*/
628
629
numPoints = 1 + polyPtr->numPoints*polyPtr->splineSteps;
630
if (numPoints <= MAX_STATIC_POINTS) {
631
pointPtr = staticPoints;
632
} else {
633
pointPtr = (XPoint *) ckalloc((unsigned)
634
(numPoints * sizeof(XPoint)));
635
}
636
numPoints = TkMakeBezierCurve(canvas, polyPtr->coordPtr,
637
polyPtr->numPoints, polyPtr->splineSteps, pointPtr,
638
(double *) NULL);
639
if (polyPtr->fillGC != None) {
640
XFillPolygon(display, drawable, polyPtr->fillGC, pointPtr,
641
numPoints, Complex, CoordModeOrigin);
642
}
643
if (polyPtr->outlineGC != None) {
644
XDrawLines(display, drawable, polyPtr->outlineGC, pointPtr,
645
numPoints, CoordModeOrigin);
646
}
647
if (pointPtr != staticPoints) {
648
ckfree((char *) pointPtr);
649
}
650
}
651
if ((polyPtr->fillStipple != None) && (polyPtr->fillGC != None)) {
652
XSetTSOrigin(display, polyPtr->fillGC, 0, 0);
653
}
654
}
655
656
/*
657
*--------------------------------------------------------------
658
*
659
* PolygonToPoint --
660
*
661
* Computes the distance from a given point to a given
662
* polygon, in canvas units.
663
*
664
* Results:
665
* The return value is 0 if the point whose x and y coordinates
666
* are pointPtr[0] and pointPtr[1] is inside the polygon. If the
667
* point isn't inside the polygon then the return value is the
668
* distance from the point to the polygon.
669
*
670
* Side effects:
671
* None.
672
*
673
*--------------------------------------------------------------
674
*/
675
676
/* ARGSUSED */
677
static double
678
PolygonToPoint(canvas, itemPtr, pointPtr)
679
Tk_Canvas canvas; /* Canvas containing item. */
680
Tk_Item *itemPtr; /* Item to check against point. */
681
double *pointPtr; /* Pointer to x and y coordinates. */
682
{
683
PolygonItem *polyPtr = (PolygonItem *) itemPtr;
684
double *coordPtr, distance;
685
double staticSpace[2*MAX_STATIC_POINTS];
686
int numPoints;
687
688
if (!polyPtr->smooth) {
689
distance = TkPolygonToPoint(polyPtr->coordPtr, polyPtr->numPoints,
690
pointPtr);
691
} else {
692
/*
693
* Smoothed polygon. Generate a new set of points and use them
694
* for comparison.
695
*/
696
697
numPoints = 1 + polyPtr->numPoints*polyPtr->splineSteps;
698
if (numPoints <= MAX_STATIC_POINTS) {
699
coordPtr = staticSpace;
700
} else {
701
coordPtr = (double *) ckalloc((unsigned)
702
(2*numPoints*sizeof(double)));
703
}
704
numPoints = TkMakeBezierCurve(canvas, polyPtr->coordPtr,
705
polyPtr->numPoints, polyPtr->splineSteps, (XPoint *) NULL,
706
coordPtr);
707
distance = TkPolygonToPoint(coordPtr, numPoints, pointPtr);
708
if (coordPtr != staticSpace) {
709
ckfree((char *) coordPtr);
710
}
711
}
712
if (polyPtr->outlineColor != NULL) {
713
distance -= polyPtr->width/2.0;
714
if (distance < 0) {
715
distance = 0;
716
}
717
}
718
return distance;
719
}
720
721
/*
722
*--------------------------------------------------------------
723
*
724
* PolygonToArea --
725
*
726
* This procedure is called to determine whether an item
727
* lies entirely inside, entirely outside, or overlapping
728
* a given rectangular area.
729
*
730
* Results:
731
* -1 is returned if the item is entirely outside the area
732
* given by rectPtr, 0 if it overlaps, and 1 if it is entirely
733
* inside the given area.
734
*
735
* Side effects:
736
* None.
737
*
738
*--------------------------------------------------------------
739
*/
740
741
/* ARGSUSED */
742
static int
743
PolygonToArea(canvas, itemPtr, rectPtr)
744
Tk_Canvas canvas; /* Canvas containing item. */
745
Tk_Item *itemPtr; /* Item to check against polygon. */
746
double *rectPtr; /* Pointer to array of four coordinates
747
* (x1, y1, x2, y2) describing rectangular
748
* area. */
749
{
750
PolygonItem *polyPtr = (PolygonItem *) itemPtr;
751
double *coordPtr, rect2[4], halfWidth;
752
double staticSpace[2*MAX_STATIC_POINTS];
753
int numPoints, result;
754
755
/*
756
* Handle smoothed polygons by generating an expanded set of points
757
* against which to do the check.
758
*/
759
760
if (polyPtr->smooth) {
761
numPoints = 1 + polyPtr->numPoints*polyPtr->splineSteps;
762
if (numPoints <= MAX_STATIC_POINTS) {
763
coordPtr = staticSpace;
764
} else {
765
coordPtr = (double *) ckalloc((unsigned)
766
(2*numPoints*sizeof(double)));
767
}
768
numPoints = TkMakeBezierCurve(canvas, polyPtr->coordPtr,
769
polyPtr->numPoints, polyPtr->splineSteps, (XPoint *) NULL,
770
coordPtr);
771
} else {
772
numPoints = polyPtr->numPoints;
773
coordPtr = polyPtr->coordPtr;
774
}
775
776
if (polyPtr->width <= 1) {
777
/*
778
* The outline of the polygon doesn't stick out, so we can
779
* do a simple check.
780
*/
781
782
result = TkPolygonToArea(coordPtr, numPoints, rectPtr);
783
} else {
784
/*
785
* The polygon has a wide outline, so the check is more complicated.
786
* First, check the line segments to see if they overlap the area.
787
*/
788
789
result = TkThickPolyLineToArea(coordPtr, numPoints,
790
(double) polyPtr->width, CapRound, JoinRound, rectPtr);
791
if (result >= 0) {
792
goto done;
793
}
794
795
/*
796
* There is no overlap between the polygon's outline and the
797
* rectangle. This means either the rectangle is entirely outside
798
* the polygon or entirely inside. To tell the difference,
799
* see whether the polygon (with 0 outline width) overlaps the
800
* rectangle bloated by half the outline width.
801
*/
802
803
halfWidth = polyPtr->width/2.0;
804
rect2[0] = rectPtr[0] - halfWidth;
805
rect2[1] = rectPtr[1] - halfWidth;
806
rect2[2] = rectPtr[2] + halfWidth;
807
rect2[3] = rectPtr[3] + halfWidth;
808
if (TkPolygonToArea(coordPtr, numPoints, rect2) == -1) {
809
result = -1;
810
} else {
811
result = 0;
812
}
813
}
814
815
done:
816
if ((coordPtr != staticSpace) && (coordPtr != polyPtr->coordPtr)) {
817
ckfree((char *) coordPtr);
818
}
819
return result;
820
}
821
822
/*
823
*--------------------------------------------------------------
824
*
825
* ScalePolygon --
826
*
827
* This procedure is invoked to rescale a polygon item.
828
*
829
* Results:
830
* None.
831
*
832
* Side effects:
833
* The polygon referred to by itemPtr is rescaled so that the
834
* following transformation is applied to all point
835
* coordinates:
836
* x' = originX + scaleX*(x-originX)
837
* y' = originY + scaleY*(y-originY)
838
*
839
*--------------------------------------------------------------
840
*/
841
842
static void
843
ScalePolygon(canvas, itemPtr, originX, originY, scaleX, scaleY)
844
Tk_Canvas canvas; /* Canvas containing polygon. */
845
Tk_Item *itemPtr; /* Polygon to be scaled. */
846
double originX, originY; /* Origin about which to scale rect. */
847
double scaleX; /* Amount to scale in X direction. */
848
double scaleY; /* Amount to scale in Y direction. */
849
{
850
PolygonItem *polyPtr = (PolygonItem *) itemPtr;
851
double *coordPtr;
852
int i;
853
854
for (i = 0, coordPtr = polyPtr->coordPtr; i < polyPtr->numPoints;
855
i++, coordPtr += 2) {
856
*coordPtr = originX + scaleX*(*coordPtr - originX);
857
coordPtr[1] = originY + scaleY*(coordPtr[1] - originY);
858
}
859
ComputePolygonBbox(canvas, polyPtr);
860
}
861
862
/*
863
*--------------------------------------------------------------
864
*
865
* TranslatePolygon --
866
*
867
* This procedure is called to move a polygon by a given
868
* amount.
869
*
870
* Results:
871
* None.
872
*
873
* Side effects:
874
* The position of the polygon is offset by (xDelta, yDelta),
875
* and the bounding box is updated in the generic part of the
876
* item structure.
877
*
878
*--------------------------------------------------------------
879
*/
880
881
static void
882
TranslatePolygon(canvas, itemPtr, deltaX, deltaY)
883
Tk_Canvas canvas; /* Canvas containing item. */
884
Tk_Item *itemPtr; /* Item that is being moved. */
885
double deltaX, deltaY; /* Amount by which item is to be
886
* moved. */
887
{
888
PolygonItem *polyPtr = (PolygonItem *) itemPtr;
889
double *coordPtr;
890
int i;
891
892
for (i = 0, coordPtr = polyPtr->coordPtr; i < polyPtr->numPoints;
893
i++, coordPtr += 2) {
894
*coordPtr += deltaX;
895
coordPtr[1] += deltaY;
896
}
897
ComputePolygonBbox(canvas, polyPtr);
898
}
899
900
/*
901
*--------------------------------------------------------------
902
*
903
* PolygonToPostscript --
904
*
905
* This procedure is called to generate Postscript for
906
* polygon items.
907
*
908
* Results:
909
* The return value is a standard Tcl result. If an error
910
* occurs in generating Postscript then an error message is
911
* left in interp->result, replacing whatever used
912
* to be there. If no error occurs, then Postscript for the
913
* item is appended to the result.
914
*
915
* Side effects:
916
* None.
917
*
918
*--------------------------------------------------------------
919
*/
920
921
static int
922
PolygonToPostscript(interp, canvas, itemPtr, prepass)
923
Tcl_Interp *interp; /* Leave Postscript or error message
924
* here. */
925
Tk_Canvas canvas; /* Information about overall canvas. */
926
Tk_Item *itemPtr; /* Item for which Postscript is
927
* wanted. */
928
int prepass; /* 1 means this is a prepass to
929
* collect font information; 0 means
930
* final Postscript is being created. */
931
{
932
char string[100];
933
PolygonItem *polyPtr = (PolygonItem *) itemPtr;
934
935
/*
936
* Fill the area of the polygon.
937
*/
938
939
if (polyPtr->fillColor != NULL) {
940
if (!polyPtr->smooth) {
941
Tk_CanvasPsPath(interp, canvas, polyPtr->coordPtr,
942
polyPtr->numPoints);
943
} else {
944
TkMakeBezierPostscript(interp, canvas, polyPtr->coordPtr,
945
polyPtr->numPoints);
946
}
947
if (Tk_CanvasPsColor(interp, canvas, polyPtr->fillColor) != TCL_OK) {
948
return TCL_ERROR;
949
}
950
if (polyPtr->fillStipple != None) {
951
Tcl_AppendResult(interp, "eoclip ", (char *) NULL);
952
if (Tk_CanvasPsStipple(interp, canvas, polyPtr->fillStipple)
953
!= TCL_OK) {
954
return TCL_ERROR;
955
}
956
if (polyPtr->outlineColor != NULL) {
957
Tcl_AppendResult(interp, "grestore gsave\n", (char *) NULL);
958
}
959
} else {
960
Tcl_AppendResult(interp, "eofill\n", (char *) NULL);
961
}
962
}
963
964
/*
965
* Now draw the outline, if there is one.
966
*/
967
968
if (polyPtr->outlineColor != NULL) {
969
if (!polyPtr->smooth) {
970
Tk_CanvasPsPath(interp, canvas, polyPtr->coordPtr,
971
polyPtr->numPoints);
972
} else {
973
TkMakeBezierPostscript(interp, canvas, polyPtr->coordPtr,
974
polyPtr->numPoints);
975
}
976
977
sprintf(string, "%d setlinewidth\n", polyPtr->width);
978
Tcl_AppendResult(interp, string,
979
"1 setlinecap\n1 setlinejoin\n", (char *) NULL);
980
if (Tk_CanvasPsColor(interp, canvas, polyPtr->outlineColor)
981
!= TCL_OK) {
982
return TCL_ERROR;
983
}
984
Tcl_AppendResult(interp, "stroke\n", (char *) NULL);
985
}
986
return TCL_OK;
987
}
988
989