/*1* tkCanvPoly.c --2*3* This file implements polygon items for canvas widgets.4*5* Copyright (c) 1991-1994 The Regents of the University of California.6* Copyright (c) 1994 Sun Microsystems, Inc.7*8* See the file "license.terms" for information on usage and redistribution9* of this file, and for a DISCLAIMER OF ALL WARRANTIES.10*11* SCCS: @(#) tkCanvPoly.c 1.34 96/02/15 18:52:3212*/1314#include "tkInt.h"1516/*17* The structure below defines the record for each polygon item.18*/1920typedef struct PolygonItem {21Tk_Item header; /* Generic stuff that's the same for all22* types. MUST BE FIRST IN STRUCTURE. */23int numPoints; /* Number of points in polygon (always >= 3).24* Polygon is always closed. */25int pointsAllocated; /* Number of points for which space is26* allocated at *coordPtr. */27double *coordPtr; /* Pointer to malloc-ed array containing28* x- and y-coords of all points in polygon.29* X-coords are even-valued indices, y-coords30* are corresponding odd-valued indices. */31int width; /* Width of outline. */32XColor *outlineColor; /* Color for outline. */33GC outlineGC; /* Graphics context for drawing outline. */34XColor *fillColor; /* Foreground color for polygon. */35Pixmap fillStipple; /* Stipple bitmap for filling polygon. */36GC fillGC; /* Graphics context for filling polygon. */37int smooth; /* Non-zero means draw shape smoothed (i.e.38* with Bezier splines). */39int splineSteps; /* Number of steps in each spline segment. */40} PolygonItem;4142/*43* Information used for parsing configuration specs:44*/4546static Tk_CustomOption tagsOption = {Tk_CanvasTagsParseProc,47Tk_CanvasTagsPrintProc, (ClientData) NULL48};4950static Tk_ConfigSpec configSpecs[] = {51{TK_CONFIG_COLOR, "-fill", (char *) NULL, (char *) NULL,52"black", Tk_Offset(PolygonItem, fillColor), TK_CONFIG_NULL_OK},53{TK_CONFIG_COLOR, "-outline", (char *) NULL, (char *) NULL,54(char *) NULL, Tk_Offset(PolygonItem, outlineColor), TK_CONFIG_NULL_OK},55{TK_CONFIG_BOOLEAN, "-smooth", (char *) NULL, (char *) NULL,56"0", Tk_Offset(PolygonItem, smooth), TK_CONFIG_DONT_SET_DEFAULT},57{TK_CONFIG_INT, "-splinesteps", (char *) NULL, (char *) NULL,58"12", Tk_Offset(PolygonItem, splineSteps), TK_CONFIG_DONT_SET_DEFAULT},59{TK_CONFIG_BITMAP, "-stipple", (char *) NULL, (char *) NULL,60(char *) NULL, Tk_Offset(PolygonItem, fillStipple), TK_CONFIG_NULL_OK},61{TK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL,62(char *) NULL, 0, TK_CONFIG_NULL_OK, &tagsOption},63{TK_CONFIG_PIXELS, "-width", (char *) NULL, (char *) NULL,64"1", Tk_Offset(PolygonItem, width), TK_CONFIG_DONT_SET_DEFAULT},65{TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,66(char *) NULL, 0, 0}67};6869/*70* Prototypes for procedures defined in this file:71*/7273static void ComputePolygonBbox _ANSI_ARGS_((Tk_Canvas canvas,74PolygonItem *polyPtr));75static int ConfigurePolygon _ANSI_ARGS_((Tcl_Interp *interp,76Tk_Canvas canvas, Tk_Item *itemPtr, int argc,77char **argv, int flags));78static int CreatePolygon _ANSI_ARGS_((Tcl_Interp *interp,79Tk_Canvas canvas, struct Tk_Item *itemPtr,80int argc, char **argv));81static void DeletePolygon _ANSI_ARGS_((Tk_Canvas canvas,82Tk_Item *itemPtr, Display *display));83static void DisplayPolygon _ANSI_ARGS_((Tk_Canvas canvas,84Tk_Item *itemPtr, Display *display, Drawable dst,85int x, int y, int width, int height));86static int PolygonCoords _ANSI_ARGS_((Tcl_Interp *interp,87Tk_Canvas canvas, Tk_Item *itemPtr,88int argc, char **argv));89static int PolygonToArea _ANSI_ARGS_((Tk_Canvas canvas,90Tk_Item *itemPtr, double *rectPtr));91static double PolygonToPoint _ANSI_ARGS_((Tk_Canvas canvas,92Tk_Item *itemPtr, double *pointPtr));93static int PolygonToPostscript _ANSI_ARGS_((Tcl_Interp *interp,94Tk_Canvas canvas, Tk_Item *itemPtr, int prepass));95static void ScalePolygon _ANSI_ARGS_((Tk_Canvas canvas,96Tk_Item *itemPtr, double originX, double originY,97double scaleX, double scaleY));98static void TranslatePolygon _ANSI_ARGS_((Tk_Canvas canvas,99Tk_Item *itemPtr, double deltaX, double deltaY));100101/*102* The structures below defines the polygon item type by means103* of procedures that can be invoked by generic item code.104*/105106Tk_ItemType tkPolygonType = {107"polygon", /* name */108sizeof(PolygonItem), /* itemSize */109CreatePolygon, /* createProc */110configSpecs, /* configSpecs */111ConfigurePolygon, /* configureProc */112PolygonCoords, /* coordProc */113DeletePolygon, /* deleteProc */114DisplayPolygon, /* displayProc */1150, /* alwaysRedraw */116PolygonToPoint, /* pointProc */117PolygonToArea, /* areaProc */118PolygonToPostscript, /* postscriptProc */119ScalePolygon, /* scaleProc */120TranslatePolygon, /* translateProc */121(Tk_ItemIndexProc *) NULL, /* indexProc */122(Tk_ItemCursorProc *) NULL, /* icursorProc */123(Tk_ItemSelectionProc *) NULL, /* selectionProc */124(Tk_ItemInsertProc *) NULL, /* insertProc */125(Tk_ItemDCharsProc *) NULL, /* dTextProc */126(Tk_ItemType *) NULL /* nextPtr */127};128129/*130* The definition below determines how large are static arrays131* used to hold spline points (splines larger than this have to132* have their arrays malloc-ed).133*/134135#define MAX_STATIC_POINTS 200136137/*138*--------------------------------------------------------------139*140* CreatePolygon --141*142* This procedure is invoked to create a new polygon item in143* a canvas.144*145* Results:146* A standard Tcl return value. If an error occurred in147* creating the item, then an error message is left in148* interp->result; in this case itemPtr is149* left uninitialized, so it can be safely freed by the150* caller.151*152* Side effects:153* A new polygon item is created.154*155*--------------------------------------------------------------156*/157158static int159CreatePolygon(interp, canvas, itemPtr, argc, argv)160Tcl_Interp *interp; /* Interpreter for error reporting. */161Tk_Canvas canvas; /* Canvas to hold new item. */162Tk_Item *itemPtr; /* Record to hold new item; header163* has been initialized by caller. */164int argc; /* Number of arguments in argv. */165char **argv; /* Arguments describing polygon. */166{167PolygonItem *polyPtr = (PolygonItem *) itemPtr;168int i;169170if (argc < 6) {171Tcl_AppendResult(interp, "wrong # args: should be \"",172Tk_PathName(Tk_CanvasTkwin(canvas)), " create ",173itemPtr->typePtr->name,174" x1 y1 x2 y2 x3 y3 ?x4 y4 ...? ?options?\"", (char *) NULL);175return TCL_ERROR;176}177178/*179* Carry out initialization that is needed in order to clean180* up after errors during the the remainder of this procedure.181*/182183polyPtr->numPoints = 0;184polyPtr->pointsAllocated = 0;185polyPtr->coordPtr = NULL;186polyPtr->width = 1;187polyPtr->outlineColor = NULL;188polyPtr->outlineGC = None;189polyPtr->fillColor = NULL;190polyPtr->fillStipple = None;191polyPtr->fillGC = None;192polyPtr->smooth = 0;193polyPtr->splineSteps = 12;194195/*196* Count the number of points and then parse them into a point197* array. Leading arguments are assumed to be points if they198* start with a digit or a minus sign followed by a digit.199*/200201for (i = 4; i < (argc-1); i+=2) {202if ((!isdigit(UCHAR(argv[i][0]))) &&203((argv[i][0] != '-') || (!isdigit(UCHAR(argv[i][1]))))) {204break;205}206}207if (PolygonCoords(interp, canvas, itemPtr, i, argv) != TCL_OK) {208goto error;209}210211if (ConfigurePolygon(interp, canvas, itemPtr, argc-i, argv+i, 0)212== TCL_OK) {213return TCL_OK;214}215216error:217DeletePolygon(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));218return TCL_ERROR;219}220221/*222*--------------------------------------------------------------223*224* PolygonCoords --225*226* This procedure is invoked to process the "coords" widget227* command on polygons. See the user documentation for details228* on what it does.229*230* Results:231* Returns TCL_OK or TCL_ERROR, and sets interp->result.232*233* Side effects:234* The coordinates for the given item may be changed.235*236*--------------------------------------------------------------237*/238239static int240PolygonCoords(interp, canvas, itemPtr, argc, argv)241Tcl_Interp *interp; /* Used for error reporting. */242Tk_Canvas canvas; /* Canvas containing item. */243Tk_Item *itemPtr; /* Item whose coordinates are to be244* read or modified. */245int argc; /* Number of coordinates supplied in246* argv. */247char **argv; /* Array of coordinates: x1, y1,248* x2, y2, ... */249{250PolygonItem *polyPtr = (PolygonItem *) itemPtr;251char buffer[TCL_DOUBLE_SPACE];252int i, numPoints;253254if (argc == 0) {255for (i = 0; i < 2*polyPtr->numPoints; i++) {256Tcl_PrintDouble(interp, polyPtr->coordPtr[i], buffer);257Tcl_AppendElement(interp, buffer);258}259} else if (argc < 6) {260Tcl_AppendResult(interp,261"too few coordinates for polygon: must have at least 6",262(char *) NULL);263return TCL_ERROR;264} else if (argc & 1) {265Tcl_AppendResult(interp,266"odd number of coordinates specified for polygon",267(char *) NULL);268return TCL_ERROR;269} else {270numPoints = argc/2;271if (polyPtr->pointsAllocated <= numPoints) {272if (polyPtr->coordPtr != NULL) {273ckfree((char *) polyPtr->coordPtr);274}275276/*277* One extra point gets allocated here, just in case we have278* to add another point to close the polygon.279*/280281polyPtr->coordPtr = (double *) ckalloc((unsigned)282(sizeof(double) * (argc+2)));283polyPtr->pointsAllocated = numPoints+1;284}285for (i = argc-1; i >= 0; i--) {286if (Tk_CanvasGetCoord(interp, canvas, argv[i],287&polyPtr->coordPtr[i]) != TCL_OK) {288return TCL_ERROR;289}290}291polyPtr->numPoints = numPoints;292293/*294* Close the polygon if it isn't already closed.295*/296297if ((polyPtr->coordPtr[argc-2] != polyPtr->coordPtr[0])298|| (polyPtr->coordPtr[argc-1] != polyPtr->coordPtr[1])) {299polyPtr->numPoints++;300polyPtr->coordPtr[argc] = polyPtr->coordPtr[0];301polyPtr->coordPtr[argc+1] = polyPtr->coordPtr[1];302}303ComputePolygonBbox(canvas, polyPtr);304}305return TCL_OK;306}307308/*309*--------------------------------------------------------------310*311* ConfigurePolygon --312*313* This procedure is invoked to configure various aspects314* of a polygon item such as its background color.315*316* Results:317* A standard Tcl result code. If an error occurs, then318* an error message is left in interp->result.319*320* Side effects:321* Configuration information, such as colors and stipple322* patterns, may be set for itemPtr.323*324*--------------------------------------------------------------325*/326327static int328ConfigurePolygon(interp, canvas, itemPtr, argc, argv, flags)329Tcl_Interp *interp; /* Interpreter for error reporting. */330Tk_Canvas canvas; /* Canvas containing itemPtr. */331Tk_Item *itemPtr; /* Polygon item to reconfigure. */332int argc; /* Number of elements in argv. */333char **argv; /* Arguments describing things to configure. */334int flags; /* Flags to pass to Tk_ConfigureWidget. */335{336PolygonItem *polyPtr = (PolygonItem *) itemPtr;337XGCValues gcValues;338GC newGC;339unsigned long mask;340Tk_Window tkwin;341342tkwin = Tk_CanvasTkwin(canvas);343if (Tk_ConfigureWidget(interp, tkwin, configSpecs, argc, argv,344(char *) polyPtr, flags) != TCL_OK) {345return TCL_ERROR;346}347348/*349* A few of the options require additional processing, such as350* graphics contexts.351*/352353if (polyPtr->width < 1) {354polyPtr->width = 1;355}356if (polyPtr->outlineColor == NULL) {357newGC = None;358} else {359gcValues.foreground = polyPtr->outlineColor->pixel;360gcValues.line_width = polyPtr->width;361gcValues.cap_style = CapRound;362gcValues.join_style = JoinRound;363mask = GCForeground|GCLineWidth|GCCapStyle|GCJoinStyle;364newGC = Tk_GetGC(tkwin, mask, &gcValues);365}366if (polyPtr->outlineGC != None) {367Tk_FreeGC(Tk_Display(tkwin), polyPtr->outlineGC);368}369polyPtr->outlineGC = newGC;370371if (polyPtr->fillColor == NULL) {372newGC = None;373} else {374gcValues.foreground = polyPtr->fillColor->pixel;375mask = GCForeground;376if (polyPtr->fillStipple != None) {377gcValues.stipple = polyPtr->fillStipple;378gcValues.fill_style = FillStippled;379mask |= GCStipple|GCFillStyle;380}381newGC = Tk_GetGC(tkwin, mask, &gcValues);382}383if (polyPtr->fillGC != None) {384Tk_FreeGC(Tk_Display(tkwin), polyPtr->fillGC);385}386polyPtr->fillGC = newGC;387388/*389* Keep spline parameters within reasonable limits.390*/391392if (polyPtr->splineSteps < 1) {393polyPtr->splineSteps = 1;394} else if (polyPtr->splineSteps > 100) {395polyPtr->splineSteps = 100;396}397398ComputePolygonBbox(canvas, polyPtr);399return TCL_OK;400}401402/*403*--------------------------------------------------------------404*405* DeletePolygon --406*407* This procedure is called to clean up the data structure408* associated with a polygon item.409*410* Results:411* None.412*413* Side effects:414* Resources associated with itemPtr are released.415*416*--------------------------------------------------------------417*/418419static void420DeletePolygon(canvas, itemPtr, display)421Tk_Canvas canvas; /* Info about overall canvas widget. */422Tk_Item *itemPtr; /* Item that is being deleted. */423Display *display; /* Display containing window for424* canvas. */425{426PolygonItem *polyPtr = (PolygonItem *) itemPtr;427428if (polyPtr->coordPtr != NULL) {429ckfree((char *) polyPtr->coordPtr);430}431if (polyPtr->fillColor != NULL) {432Tk_FreeColor(polyPtr->fillColor);433}434if (polyPtr->fillStipple != None) {435Tk_FreeBitmap(display, polyPtr->fillStipple);436}437if (polyPtr->outlineColor != NULL) {438Tk_FreeColor(polyPtr->outlineColor);439}440if (polyPtr->outlineGC != None) {441Tk_FreeGC(display, polyPtr->outlineGC);442}443if (polyPtr->fillGC != None) {444Tk_FreeGC(display, polyPtr->fillGC);445}446}447448/*449*--------------------------------------------------------------450*451* ComputePolygonBbox --452*453* This procedure is invoked to compute the bounding box of454* all the pixels that may be drawn as part of a polygon.455*456* Results:457* None.458*459* Side effects:460* The fields x1, y1, x2, and y2 are updated in the header461* for itemPtr.462*463*--------------------------------------------------------------464*/465466static void467ComputePolygonBbox(canvas, polyPtr)468Tk_Canvas canvas; /* Canvas that contains item. */469PolygonItem *polyPtr; /* Item whose bbox is to be470* recomputed. */471{472double *coordPtr;473int i;474475coordPtr = polyPtr->coordPtr;476polyPtr->header.x1 = polyPtr->header.x2 = *coordPtr;477polyPtr->header.y1 = polyPtr->header.y2 = coordPtr[1];478479for (i = 1, coordPtr = polyPtr->coordPtr+2; i < polyPtr->numPoints;480i++, coordPtr += 2) {481TkIncludePoint((Tk_Item *) polyPtr, coordPtr);482}483484/*485* Expand bounding box in all directions to account for the outline,486* which can stick out beyond the polygon. Add one extra pixel of487* fudge, just in case X rounds differently than we do.488*/489490i = (polyPtr->width+1)/2 + 1;491polyPtr->header.x1 -= i;492polyPtr->header.x2 += i;493polyPtr->header.y1 -= i;494polyPtr->header.y2 += i;495}496497/*498*--------------------------------------------------------------499*500* TkFillPolygon --501*502* This procedure is invoked to convert a polygon to screen503* coordinates and display it using a particular GC.504*505* Results:506* None.507*508* Side effects:509* ItemPtr is drawn in drawable using the transformation510* information in canvas.511*512*--------------------------------------------------------------513*/514515void516TkFillPolygon(canvas, coordPtr, numPoints, display, drawable, gc, outlineGC)517Tk_Canvas canvas; /* Canvas whose coordinate system518* is to be used for drawing. */519double *coordPtr; /* Array of coordinates for polygon:520* x1, y1, x2, y2, .... */521int numPoints; /* Twice this many coordinates are522* present at *coordPtr. */523Display *display; /* Display on which to draw polygon. */524Drawable drawable; /* Pixmap or window in which to draw525* polygon. */526GC gc; /* Graphics context for drawing. */527GC outlineGC; /* If not None, use this to draw an528* outline around the polygon after529* filling it. */530{531XPoint staticPoints[MAX_STATIC_POINTS];532XPoint *pointPtr;533XPoint *pPtr;534int i;535536/*537* Build up an array of points in screen coordinates. Use a538* static array unless the polygon has an enormous number of points;539* in this case, dynamically allocate an array.540*/541542if (numPoints <= MAX_STATIC_POINTS) {543pointPtr = staticPoints;544} else {545pointPtr = (XPoint *) ckalloc((unsigned) (numPoints * sizeof(XPoint)));546}547548for (i = 0, pPtr = pointPtr; i < numPoints; i += 1, coordPtr += 2, pPtr++) {549Tk_CanvasDrawableCoords(canvas, coordPtr[0], coordPtr[1], &pPtr->x,550&pPtr->y);551}552553/*554* Display polygon, then free up polygon storage if it was dynamically555* allocated.556*/557558if (gc != None) {559XFillPolygon(display, drawable, gc, pointPtr, numPoints, Complex,560CoordModeOrigin);561}562if (outlineGC != None) {563XDrawLines(display, drawable, outlineGC, pointPtr,564numPoints, CoordModeOrigin);565}566if (pointPtr != staticPoints) {567ckfree((char *) pointPtr);568}569}570571/*572*--------------------------------------------------------------573*574* DisplayPolygon --575*576* This procedure is invoked to draw a polygon item in a given577* drawable.578*579* Results:580* None.581*582* Side effects:583* ItemPtr is drawn in drawable using the transformation584* information in canvas.585*586*--------------------------------------------------------------587*/588589static void590DisplayPolygon(canvas, itemPtr, display, drawable, x, y, width, height)591Tk_Canvas canvas; /* Canvas that contains item. */592Tk_Item *itemPtr; /* Item to be displayed. */593Display *display; /* Display on which to draw item. */594Drawable drawable; /* Pixmap or window in which to draw595* item. */596int x, y, width, height; /* Describes region of canvas that597* must be redisplayed (not used). */598{599PolygonItem *polyPtr = (PolygonItem *) itemPtr;600601if ((polyPtr->fillGC == None) && (polyPtr->outlineGC == None)) {602return;603}604605/*606* If we're stippling then modify the stipple offset in the GC. Be607* sure to reset the offset when done, since the GC is supposed to be608* read-only.609*/610611if ((polyPtr->fillStipple != None) && (polyPtr->fillGC != None)) {612Tk_CanvasSetStippleOrigin(canvas, polyPtr->fillGC);613}614615if (!polyPtr->smooth) {616TkFillPolygon(canvas, polyPtr->coordPtr, polyPtr->numPoints,617display, drawable, polyPtr->fillGC, polyPtr->outlineGC);618} else {619int numPoints;620XPoint staticPoints[MAX_STATIC_POINTS];621XPoint *pointPtr;622623/*624* This is a smoothed polygon. Display using a set of generated625* spline points rather than the original points.626*/627628numPoints = 1 + polyPtr->numPoints*polyPtr->splineSteps;629if (numPoints <= MAX_STATIC_POINTS) {630pointPtr = staticPoints;631} else {632pointPtr = (XPoint *) ckalloc((unsigned)633(numPoints * sizeof(XPoint)));634}635numPoints = TkMakeBezierCurve(canvas, polyPtr->coordPtr,636polyPtr->numPoints, polyPtr->splineSteps, pointPtr,637(double *) NULL);638if (polyPtr->fillGC != None) {639XFillPolygon(display, drawable, polyPtr->fillGC, pointPtr,640numPoints, Complex, CoordModeOrigin);641}642if (polyPtr->outlineGC != None) {643XDrawLines(display, drawable, polyPtr->outlineGC, pointPtr,644numPoints, CoordModeOrigin);645}646if (pointPtr != staticPoints) {647ckfree((char *) pointPtr);648}649}650if ((polyPtr->fillStipple != None) && (polyPtr->fillGC != None)) {651XSetTSOrigin(display, polyPtr->fillGC, 0, 0);652}653}654655/*656*--------------------------------------------------------------657*658* PolygonToPoint --659*660* Computes the distance from a given point to a given661* polygon, in canvas units.662*663* Results:664* The return value is 0 if the point whose x and y coordinates665* are pointPtr[0] and pointPtr[1] is inside the polygon. If the666* point isn't inside the polygon then the return value is the667* distance from the point to the polygon.668*669* Side effects:670* None.671*672*--------------------------------------------------------------673*/674675/* ARGSUSED */676static double677PolygonToPoint(canvas, itemPtr, pointPtr)678Tk_Canvas canvas; /* Canvas containing item. */679Tk_Item *itemPtr; /* Item to check against point. */680double *pointPtr; /* Pointer to x and y coordinates. */681{682PolygonItem *polyPtr = (PolygonItem *) itemPtr;683double *coordPtr, distance;684double staticSpace[2*MAX_STATIC_POINTS];685int numPoints;686687if (!polyPtr->smooth) {688distance = TkPolygonToPoint(polyPtr->coordPtr, polyPtr->numPoints,689pointPtr);690} else {691/*692* Smoothed polygon. Generate a new set of points and use them693* for comparison.694*/695696numPoints = 1 + polyPtr->numPoints*polyPtr->splineSteps;697if (numPoints <= MAX_STATIC_POINTS) {698coordPtr = staticSpace;699} else {700coordPtr = (double *) ckalloc((unsigned)701(2*numPoints*sizeof(double)));702}703numPoints = TkMakeBezierCurve(canvas, polyPtr->coordPtr,704polyPtr->numPoints, polyPtr->splineSteps, (XPoint *) NULL,705coordPtr);706distance = TkPolygonToPoint(coordPtr, numPoints, pointPtr);707if (coordPtr != staticSpace) {708ckfree((char *) coordPtr);709}710}711if (polyPtr->outlineColor != NULL) {712distance -= polyPtr->width/2.0;713if (distance < 0) {714distance = 0;715}716}717return distance;718}719720/*721*--------------------------------------------------------------722*723* PolygonToArea --724*725* This procedure is called to determine whether an item726* lies entirely inside, entirely outside, or overlapping727* a given rectangular area.728*729* Results:730* -1 is returned if the item is entirely outside the area731* given by rectPtr, 0 if it overlaps, and 1 if it is entirely732* inside the given area.733*734* Side effects:735* None.736*737*--------------------------------------------------------------738*/739740/* ARGSUSED */741static int742PolygonToArea(canvas, itemPtr, rectPtr)743Tk_Canvas canvas; /* Canvas containing item. */744Tk_Item *itemPtr; /* Item to check against polygon. */745double *rectPtr; /* Pointer to array of four coordinates746* (x1, y1, x2, y2) describing rectangular747* area. */748{749PolygonItem *polyPtr = (PolygonItem *) itemPtr;750double *coordPtr, rect2[4], halfWidth;751double staticSpace[2*MAX_STATIC_POINTS];752int numPoints, result;753754/*755* Handle smoothed polygons by generating an expanded set of points756* against which to do the check.757*/758759if (polyPtr->smooth) {760numPoints = 1 + polyPtr->numPoints*polyPtr->splineSteps;761if (numPoints <= MAX_STATIC_POINTS) {762coordPtr = staticSpace;763} else {764coordPtr = (double *) ckalloc((unsigned)765(2*numPoints*sizeof(double)));766}767numPoints = TkMakeBezierCurve(canvas, polyPtr->coordPtr,768polyPtr->numPoints, polyPtr->splineSteps, (XPoint *) NULL,769coordPtr);770} else {771numPoints = polyPtr->numPoints;772coordPtr = polyPtr->coordPtr;773}774775if (polyPtr->width <= 1) {776/*777* The outline of the polygon doesn't stick out, so we can778* do a simple check.779*/780781result = TkPolygonToArea(coordPtr, numPoints, rectPtr);782} else {783/*784* The polygon has a wide outline, so the check is more complicated.785* First, check the line segments to see if they overlap the area.786*/787788result = TkThickPolyLineToArea(coordPtr, numPoints,789(double) polyPtr->width, CapRound, JoinRound, rectPtr);790if (result >= 0) {791goto done;792}793794/*795* There is no overlap between the polygon's outline and the796* rectangle. This means either the rectangle is entirely outside797* the polygon or entirely inside. To tell the difference,798* see whether the polygon (with 0 outline width) overlaps the799* rectangle bloated by half the outline width.800*/801802halfWidth = polyPtr->width/2.0;803rect2[0] = rectPtr[0] - halfWidth;804rect2[1] = rectPtr[1] - halfWidth;805rect2[2] = rectPtr[2] + halfWidth;806rect2[3] = rectPtr[3] + halfWidth;807if (TkPolygonToArea(coordPtr, numPoints, rect2) == -1) {808result = -1;809} else {810result = 0;811}812}813814done:815if ((coordPtr != staticSpace) && (coordPtr != polyPtr->coordPtr)) {816ckfree((char *) coordPtr);817}818return result;819}820821/*822*--------------------------------------------------------------823*824* ScalePolygon --825*826* This procedure is invoked to rescale a polygon item.827*828* Results:829* None.830*831* Side effects:832* The polygon referred to by itemPtr is rescaled so that the833* following transformation is applied to all point834* coordinates:835* x' = originX + scaleX*(x-originX)836* y' = originY + scaleY*(y-originY)837*838*--------------------------------------------------------------839*/840841static void842ScalePolygon(canvas, itemPtr, originX, originY, scaleX, scaleY)843Tk_Canvas canvas; /* Canvas containing polygon. */844Tk_Item *itemPtr; /* Polygon to be scaled. */845double originX, originY; /* Origin about which to scale rect. */846double scaleX; /* Amount to scale in X direction. */847double scaleY; /* Amount to scale in Y direction. */848{849PolygonItem *polyPtr = (PolygonItem *) itemPtr;850double *coordPtr;851int i;852853for (i = 0, coordPtr = polyPtr->coordPtr; i < polyPtr->numPoints;854i++, coordPtr += 2) {855*coordPtr = originX + scaleX*(*coordPtr - originX);856coordPtr[1] = originY + scaleY*(coordPtr[1] - originY);857}858ComputePolygonBbox(canvas, polyPtr);859}860861/*862*--------------------------------------------------------------863*864* TranslatePolygon --865*866* This procedure is called to move a polygon by a given867* amount.868*869* Results:870* None.871*872* Side effects:873* The position of the polygon is offset by (xDelta, yDelta),874* and the bounding box is updated in the generic part of the875* item structure.876*877*--------------------------------------------------------------878*/879880static void881TranslatePolygon(canvas, itemPtr, deltaX, deltaY)882Tk_Canvas canvas; /* Canvas containing item. */883Tk_Item *itemPtr; /* Item that is being moved. */884double deltaX, deltaY; /* Amount by which item is to be885* moved. */886{887PolygonItem *polyPtr = (PolygonItem *) itemPtr;888double *coordPtr;889int i;890891for (i = 0, coordPtr = polyPtr->coordPtr; i < polyPtr->numPoints;892i++, coordPtr += 2) {893*coordPtr += deltaX;894coordPtr[1] += deltaY;895}896ComputePolygonBbox(canvas, polyPtr);897}898899/*900*--------------------------------------------------------------901*902* PolygonToPostscript --903*904* This procedure is called to generate Postscript for905* polygon items.906*907* Results:908* The return value is a standard Tcl result. If an error909* occurs in generating Postscript then an error message is910* left in interp->result, replacing whatever used911* to be there. If no error occurs, then Postscript for the912* item is appended to the result.913*914* Side effects:915* None.916*917*--------------------------------------------------------------918*/919920static int921PolygonToPostscript(interp, canvas, itemPtr, prepass)922Tcl_Interp *interp; /* Leave Postscript or error message923* here. */924Tk_Canvas canvas; /* Information about overall canvas. */925Tk_Item *itemPtr; /* Item for which Postscript is926* wanted. */927int prepass; /* 1 means this is a prepass to928* collect font information; 0 means929* final Postscript is being created. */930{931char string[100];932PolygonItem *polyPtr = (PolygonItem *) itemPtr;933934/*935* Fill the area of the polygon.936*/937938if (polyPtr->fillColor != NULL) {939if (!polyPtr->smooth) {940Tk_CanvasPsPath(interp, canvas, polyPtr->coordPtr,941polyPtr->numPoints);942} else {943TkMakeBezierPostscript(interp, canvas, polyPtr->coordPtr,944polyPtr->numPoints);945}946if (Tk_CanvasPsColor(interp, canvas, polyPtr->fillColor) != TCL_OK) {947return TCL_ERROR;948}949if (polyPtr->fillStipple != None) {950Tcl_AppendResult(interp, "eoclip ", (char *) NULL);951if (Tk_CanvasPsStipple(interp, canvas, polyPtr->fillStipple)952!= TCL_OK) {953return TCL_ERROR;954}955if (polyPtr->outlineColor != NULL) {956Tcl_AppendResult(interp, "grestore gsave\n", (char *) NULL);957}958} else {959Tcl_AppendResult(interp, "eofill\n", (char *) NULL);960}961}962963/*964* Now draw the outline, if there is one.965*/966967if (polyPtr->outlineColor != NULL) {968if (!polyPtr->smooth) {969Tk_CanvasPsPath(interp, canvas, polyPtr->coordPtr,970polyPtr->numPoints);971} else {972TkMakeBezierPostscript(interp, canvas, polyPtr->coordPtr,973polyPtr->numPoints);974}975976sprintf(string, "%d setlinewidth\n", polyPtr->width);977Tcl_AppendResult(interp, string,978"1 setlinecap\n1 setlinejoin\n", (char *) NULL);979if (Tk_CanvasPsColor(interp, canvas, polyPtr->outlineColor)980!= TCL_OK) {981return TCL_ERROR;982}983Tcl_AppendResult(interp, "stroke\n", (char *) NULL);984}985return TCL_OK;986}987988989