#include "tkInt.h"
typedef struct LineItem {
Tk_Item header;
Tk_Canvas canvas;
int numPoints;
double *coordPtr;
int width;
XColor *fg;
Pixmap fillStipple;
int capStyle;
int joinStyle;
GC gc;
GC arrowGC;
Tk_Uid arrow;
float arrowShapeA;
float arrowShapeB;
float arrowShapeC;
double *firstArrowPtr;
double *lastArrowPtr;
int smooth;
int splineSteps;
} LineItem;
#define PTS_IN_ARROW 6
static int ArrowheadPostscript _ANSI_ARGS_((Tcl_Interp *interp,
Tk_Canvas canvas, LineItem *linePtr,
double *arrowPtr));
static void ComputeLineBbox _ANSI_ARGS_((Tk_Canvas canvas,
LineItem *linePtr));
static int ConfigureLine _ANSI_ARGS_((Tcl_Interp *interp,
Tk_Canvas canvas, Tk_Item *itemPtr, int argc,
char **argv, int flags));
static int ConfigureArrows _ANSI_ARGS_((Tk_Canvas canvas,
LineItem *linePtr));
static int CreateLine _ANSI_ARGS_((Tcl_Interp *interp,
Tk_Canvas canvas, struct Tk_Item *itemPtr,
int argc, char **argv));
static void DeleteLine _ANSI_ARGS_((Tk_Canvas canvas,
Tk_Item *itemPtr, Display *display));
static void DisplayLine _ANSI_ARGS_((Tk_Canvas canvas,
Tk_Item *itemPtr, Display *display, Drawable dst,
int x, int y, int width, int height));
static int LineCoords _ANSI_ARGS_((Tcl_Interp *interp,
Tk_Canvas canvas, Tk_Item *itemPtr,
int argc, char **argv));
static int LineToArea _ANSI_ARGS_((Tk_Canvas canvas,
Tk_Item *itemPtr, double *rectPtr));
static double LineToPoint _ANSI_ARGS_((Tk_Canvas canvas,
Tk_Item *itemPtr, double *coordPtr));
static int LineToPostscript _ANSI_ARGS_((Tcl_Interp *interp,
Tk_Canvas canvas, Tk_Item *itemPtr, int prepass));
static int ParseArrowShape _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, Tk_Window tkwin, char *value,
char *recordPtr, int offset));
static char * PrintArrowShape _ANSI_ARGS_((ClientData clientData,
Tk_Window tkwin, char *recordPtr, int offset,
Tcl_FreeProc **freeProcPtr));
static void ScaleLine _ANSI_ARGS_((Tk_Canvas canvas,
Tk_Item *itemPtr, double originX, double originY,
double scaleX, double scaleY));
static void TranslateLine _ANSI_ARGS_((Tk_Canvas canvas,
Tk_Item *itemPtr, double deltaX, double deltaY));
static Tk_CustomOption arrowShapeOption = {ParseArrowShape,
PrintArrowShape, (ClientData) NULL};
static Tk_CustomOption tagsOption = {Tk_CanvasTagsParseProc,
Tk_CanvasTagsPrintProc, (ClientData) NULL
};
static Tk_ConfigSpec configSpecs[] = {
{TK_CONFIG_UID, "-arrow", (char *) NULL, (char *) NULL,
"none", Tk_Offset(LineItem, arrow), TK_CONFIG_DONT_SET_DEFAULT},
{TK_CONFIG_CUSTOM, "-arrowshape", (char *) NULL, (char *) NULL,
"8 10 3", Tk_Offset(LineItem, arrowShapeA),
TK_CONFIG_DONT_SET_DEFAULT, &arrowShapeOption},
{TK_CONFIG_CAP_STYLE, "-capstyle", (char *) NULL, (char *) NULL,
"butt", Tk_Offset(LineItem, capStyle), TK_CONFIG_DONT_SET_DEFAULT},
{TK_CONFIG_COLOR, "-fill", (char *) NULL, (char *) NULL,
"black", Tk_Offset(LineItem, fg), TK_CONFIG_NULL_OK},
{TK_CONFIG_JOIN_STYLE, "-joinstyle", (char *) NULL, (char *) NULL,
"round", Tk_Offset(LineItem, joinStyle), TK_CONFIG_DONT_SET_DEFAULT},
{TK_CONFIG_BOOLEAN, "-smooth", (char *) NULL, (char *) NULL,
"0", Tk_Offset(LineItem, smooth), TK_CONFIG_DONT_SET_DEFAULT},
{TK_CONFIG_INT, "-splinesteps", (char *) NULL, (char *) NULL,
"12", Tk_Offset(LineItem, splineSteps), TK_CONFIG_DONT_SET_DEFAULT},
{TK_CONFIG_BITMAP, "-stipple", (char *) NULL, (char *) NULL,
(char *) NULL, Tk_Offset(LineItem, fillStipple), TK_CONFIG_NULL_OK},
{TK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL,
(char *) NULL, 0, TK_CONFIG_NULL_OK, &tagsOption},
{TK_CONFIG_PIXELS, "-width", (char *) NULL, (char *) NULL,
"1", Tk_Offset(LineItem, width), TK_CONFIG_DONT_SET_DEFAULT},
{TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
(char *) NULL, 0, 0}
};
Tk_ItemType tkLineType = {
"line",
sizeof(LineItem),
CreateLine,
configSpecs,
ConfigureLine,
LineCoords,
DeleteLine,
DisplayLine,
0,
LineToPoint,
LineToArea,
LineToPostscript,
ScaleLine,
TranslateLine,
(Tk_ItemIndexProc *) NULL,
(Tk_ItemCursorProc *) NULL,
(Tk_ItemSelectionProc *) NULL,
(Tk_ItemInsertProc *) NULL,
(Tk_ItemDCharsProc *) NULL,
(Tk_ItemType *) NULL
};
static Tk_Uid noneUid = NULL;
static Tk_Uid firstUid = NULL;
static Tk_Uid lastUid = NULL;
static Tk_Uid bothUid = NULL;
#define MAX_STATIC_POINTS 200
static int
CreateLine(interp, canvas, itemPtr, argc, argv)
Tcl_Interp *interp;
Tk_Canvas canvas;
Tk_Item *itemPtr;
int argc;
char **argv;
{
LineItem *linePtr = (LineItem *) itemPtr;
int i;
if (argc < 4) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
Tk_PathName(Tk_CanvasTkwin(canvas)), " create ",
itemPtr->typePtr->name, " x1 y1 x2 y2 ?x3 y3 ...? ?options?\"",
(char *) NULL);
return TCL_ERROR;
}
linePtr->canvas = canvas;
linePtr->numPoints = 0;
linePtr->coordPtr = NULL;
linePtr->width = 1;
linePtr->fg = None;
linePtr->fillStipple = None;
linePtr->capStyle = CapButt;
linePtr->joinStyle = JoinRound;
linePtr->gc = None;
linePtr->arrowGC = None;
if (noneUid == NULL) {
noneUid = Tk_GetUid("none");
firstUid = Tk_GetUid("first");
lastUid = Tk_GetUid("last");
bothUid = Tk_GetUid("both");
}
linePtr->arrow = noneUid;
linePtr->arrowShapeA = 8.0;
linePtr->arrowShapeB = 10.0;
linePtr->arrowShapeC = 3.0;
linePtr->firstArrowPtr = NULL;
linePtr->lastArrowPtr = NULL;
linePtr->smooth = 0;
linePtr->splineSteps = 12;
for (i = 4; i < (argc-1); i+=2) {
if ((!isdigit(UCHAR(argv[i][0]))) &&
((argv[i][0] != '-')
|| ((argv[i][1] != '.') && !isdigit(UCHAR(argv[i][1]))))) {
break;
}
}
if (LineCoords(interp, canvas, itemPtr, i, argv) != TCL_OK) {
goto error;
}
if (ConfigureLine(interp, canvas, itemPtr, argc-i, argv+i, 0) == TCL_OK) {
return TCL_OK;
}
error:
DeleteLine(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));
return TCL_ERROR;
}
static int
LineCoords(interp, canvas, itemPtr, argc, argv)
Tcl_Interp *interp;
Tk_Canvas canvas;
Tk_Item *itemPtr;
int argc;
char **argv;
{
LineItem *linePtr = (LineItem *) itemPtr;
char buffer[TCL_DOUBLE_SPACE];
int i, numPoints;
if (argc == 0) {
double *coordPtr;
int numCoords;
numCoords = 2*linePtr->numPoints;
if (linePtr->firstArrowPtr != NULL) {
coordPtr = linePtr->firstArrowPtr;
} else {
coordPtr = linePtr->coordPtr;
}
for (i = 0; i < numCoords; i++, coordPtr++) {
if (i == 2) {
coordPtr = linePtr->coordPtr+2;
}
if ((linePtr->lastArrowPtr != NULL) && (i == (numCoords-2))) {
coordPtr = linePtr->lastArrowPtr;
}
Tcl_PrintDouble(interp, *coordPtr, buffer);
Tcl_AppendElement(interp, buffer);
}
} else if (argc < 4) {
Tcl_AppendResult(interp,
"too few coordinates for line: must have at least 4",
(char *) NULL);
return TCL_ERROR;
} else if (argc & 1) {
Tcl_AppendResult(interp,
"odd number of coordinates specified for line",
(char *) NULL);
return TCL_ERROR;
} else {
numPoints = argc/2;
if (linePtr->numPoints != numPoints) {
if (linePtr->coordPtr != NULL) {
ckfree((char *) linePtr->coordPtr);
}
linePtr->coordPtr = (double *) ckalloc((unsigned)
(sizeof(double) * argc));
linePtr->numPoints = numPoints;
}
for (i = argc-1; i >= 0; i--) {
if (Tk_CanvasGetCoord(interp, canvas, argv[i],
&linePtr->coordPtr[i]) != TCL_OK) {
return TCL_ERROR;
}
}
if (linePtr->firstArrowPtr != NULL) {
ckfree((char *) linePtr->firstArrowPtr);
linePtr->firstArrowPtr = NULL;
}
if (linePtr->lastArrowPtr != NULL) {
ckfree((char *) linePtr->lastArrowPtr);
linePtr->lastArrowPtr = NULL;
}
if (linePtr->arrow != noneUid) {
ConfigureArrows(canvas, linePtr);
}
ComputeLineBbox(canvas, linePtr);
}
return TCL_OK;
}
static int
ConfigureLine(interp, canvas, itemPtr, argc, argv, flags)
Tcl_Interp *interp;
Tk_Canvas canvas;
Tk_Item *itemPtr;
int argc;
char **argv;
int flags;
{
LineItem *linePtr = (LineItem *) itemPtr;
XGCValues gcValues;
GC newGC, arrowGC;
unsigned long mask;
Tk_Window tkwin;
tkwin = Tk_CanvasTkwin(canvas);
if (Tk_ConfigureWidget(interp, tkwin, configSpecs, argc, argv,
(char *) linePtr, flags) != TCL_OK) {
return TCL_ERROR;
}
if (linePtr->fg == NULL) {
newGC = arrowGC = None;
} else {
gcValues.foreground = linePtr->fg->pixel;
gcValues.join_style = linePtr->joinStyle;
if (linePtr->width < 0) {
linePtr->width = 1;
}
gcValues.line_width = linePtr->width;
mask = GCForeground|GCJoinStyle|GCLineWidth;
if (linePtr->fillStipple != None) {
gcValues.stipple = linePtr->fillStipple;
gcValues.fill_style = FillStippled;
mask |= GCStipple|GCFillStyle;
}
if (linePtr->arrow == noneUid) {
gcValues.cap_style = linePtr->capStyle;
mask |= GCCapStyle;
}
newGC = Tk_GetGC(tkwin, mask, &gcValues);
gcValues.line_width = 0;
arrowGC = Tk_GetGC(tkwin, mask, &gcValues);
}
if (linePtr->gc != None) {
Tk_FreeGC(Tk_Display(tkwin), linePtr->gc);
}
if (linePtr->arrowGC != None) {
Tk_FreeGC(Tk_Display(tkwin), linePtr->arrowGC);
}
linePtr->gc = newGC;
linePtr->arrowGC = arrowGC;
if (linePtr->splineSteps < 1) {
linePtr->splineSteps = 1;
} else if (linePtr->splineSteps > 100) {
linePtr->splineSteps = 100;
}
if ((linePtr->firstArrowPtr != NULL) && (linePtr->arrow != firstUid)
&& (linePtr->arrow != bothUid)) {
linePtr->coordPtr[0] = linePtr->firstArrowPtr[0];
linePtr->coordPtr[1] = linePtr->firstArrowPtr[1];
ckfree((char *) linePtr->firstArrowPtr);
linePtr->firstArrowPtr = NULL;
}
if ((linePtr->lastArrowPtr != NULL) && (linePtr->arrow != lastUid)
&& (linePtr->arrow != bothUid)) {
int i;
i = 2*(linePtr->numPoints-1);
linePtr->coordPtr[i] = linePtr->lastArrowPtr[0];
linePtr->coordPtr[i+1] = linePtr->lastArrowPtr[1];
ckfree((char *) linePtr->lastArrowPtr);
linePtr->lastArrowPtr = NULL;
}
if (linePtr->arrow != noneUid) {
if ((linePtr->arrow != firstUid) && (linePtr->arrow != lastUid)
&& (linePtr->arrow != bothUid)) {
Tcl_AppendResult(interp, "bad arrow spec \"",
linePtr->arrow, "\": must be none, first, last, or both",
(char *) NULL);
linePtr->arrow = noneUid;
return TCL_ERROR;
}
ConfigureArrows(canvas, linePtr);
}
ComputeLineBbox(canvas, linePtr);
return TCL_OK;
}
static void
DeleteLine(canvas, itemPtr, display)
Tk_Canvas canvas;
Tk_Item *itemPtr;
Display *display;
{
LineItem *linePtr = (LineItem *) itemPtr;
if (linePtr->coordPtr != NULL) {
ckfree((char *) linePtr->coordPtr);
}
if (linePtr->fg != NULL) {
Tk_FreeColor(linePtr->fg);
}
if (linePtr->fillStipple != None) {
Tk_FreeBitmap(display, linePtr->fillStipple);
}
if (linePtr->gc != None) {
Tk_FreeGC(display, linePtr->gc);
}
if (linePtr->arrowGC != None) {
Tk_FreeGC(display, linePtr->arrowGC);
}
if (linePtr->firstArrowPtr != NULL) {
ckfree((char *) linePtr->firstArrowPtr);
}
if (linePtr->lastArrowPtr != NULL) {
ckfree((char *) linePtr->lastArrowPtr);
}
}
static void
ComputeLineBbox(canvas, linePtr)
Tk_Canvas canvas;
LineItem *linePtr;
{
double *coordPtr;
int i, width;
coordPtr = linePtr->coordPtr;
linePtr->header.x1 = linePtr->header.x2 = *coordPtr;
linePtr->header.y1 = linePtr->header.y2 = coordPtr[1];
for (i = 1, coordPtr = linePtr->coordPtr+2; i < linePtr->numPoints;
i++, coordPtr += 2) {
TkIncludePoint((Tk_Item *) linePtr, coordPtr);
}
width = linePtr->width;
if (width < 1) {
width = 1;
}
linePtr->header.x1 -= width;
linePtr->header.x2 += width;
linePtr->header.y1 -= width;
linePtr->header.y2 += width;
if (linePtr->joinStyle == JoinMiter) {
for (i = linePtr->numPoints, coordPtr = linePtr->coordPtr; i >= 3;
i--, coordPtr += 2) {
double miter[4];
int j;
if (TkGetMiterPoints(coordPtr, coordPtr+2, coordPtr+4,
(double) width, miter, miter+2)) {
for (j = 0; j < 4; j += 2) {
TkIncludePoint((Tk_Item *) linePtr, miter+j);
}
}
}
}
if (linePtr->arrow != noneUid) {
if (linePtr->arrow != lastUid) {
for (i = 0, coordPtr = linePtr->firstArrowPtr; i < PTS_IN_ARROW;
i++, coordPtr += 2) {
TkIncludePoint((Tk_Item *) linePtr, coordPtr);
}
}
if (linePtr->arrow != firstUid) {
for (i = 0, coordPtr = linePtr->lastArrowPtr; i < PTS_IN_ARROW;
i++, coordPtr += 2) {
TkIncludePoint((Tk_Item *) linePtr, coordPtr);
}
}
}
linePtr->header.x1 -= 1;
linePtr->header.x2 += 1;
linePtr->header.y1 -= 1;
linePtr->header.y2 += 1;
}
static void
DisplayLine(canvas, itemPtr, display, drawable, x, y, width, height)
Tk_Canvas canvas;
Tk_Item *itemPtr;
Display *display;
Drawable drawable;
int x, y, width, height;
{
LineItem *linePtr = (LineItem *) itemPtr;
XPoint staticPoints[MAX_STATIC_POINTS];
XPoint *pointPtr;
XPoint *pPtr;
double *coordPtr;
int i, numPoints;
if (linePtr->gc == None) {
return;
}
if ((linePtr->smooth) && (linePtr->numPoints > 2)) {
numPoints = 1 + linePtr->numPoints*linePtr->splineSteps;
} else {
numPoints = linePtr->numPoints;
}
if (numPoints <= MAX_STATIC_POINTS) {
pointPtr = staticPoints;
} else {
pointPtr = (XPoint *) ckalloc((unsigned) (numPoints * sizeof(XPoint)));
}
if ((linePtr->smooth) && (linePtr->numPoints > 2)) {
numPoints = TkMakeBezierCurve(canvas, linePtr->coordPtr,
linePtr->numPoints, linePtr->splineSteps, pointPtr,
(double *) NULL);
} else {
for (i = 0, coordPtr = linePtr->coordPtr, pPtr = pointPtr;
i < linePtr->numPoints; i += 1, coordPtr += 2, pPtr++) {
Tk_CanvasDrawableCoords(canvas, coordPtr[0], coordPtr[1],
&pPtr->x, &pPtr->y);
}
}
if (linePtr->fillStipple != None) {
Tk_CanvasSetStippleOrigin(canvas, linePtr->gc);
Tk_CanvasSetStippleOrigin(canvas, linePtr->arrowGC);
}
XDrawLines(display, drawable, linePtr->gc, pointPtr, numPoints,
CoordModeOrigin);
if (pointPtr != staticPoints) {
ckfree((char *) pointPtr);
}
if (linePtr->firstArrowPtr != NULL) {
TkFillPolygon(canvas, linePtr->firstArrowPtr, PTS_IN_ARROW,
display, drawable, linePtr->gc, NULL);
}
if (linePtr->lastArrowPtr != NULL) {
TkFillPolygon(canvas, linePtr->lastArrowPtr, PTS_IN_ARROW,
display, drawable, linePtr->gc, NULL);
}
if (linePtr->fillStipple != None) {
XSetTSOrigin(display, linePtr->gc, 0, 0);
XSetTSOrigin(display, linePtr->arrowGC, 0, 0);
}
}
static double
LineToPoint(canvas, itemPtr, pointPtr)
Tk_Canvas canvas;
Tk_Item *itemPtr;
double *pointPtr;
{
LineItem *linePtr = (LineItem *) itemPtr;
double *coordPtr, *linePoints;
double staticSpace[2*MAX_STATIC_POINTS];
double poly[10];
double bestDist, dist;
int numPoints, count;
int changedMiterToBevel;
bestDist = 1.0e36;
if ((linePtr->smooth) && (linePtr->numPoints > 2)) {
numPoints = 1 + linePtr->numPoints*linePtr->splineSteps;
if (numPoints <= MAX_STATIC_POINTS) {
linePoints = staticSpace;
} else {
linePoints = (double *) ckalloc((unsigned)
(2*numPoints*sizeof(double)));
}
numPoints = TkMakeBezierCurve(canvas, linePtr->coordPtr,
linePtr->numPoints, linePtr->splineSteps, (XPoint *) NULL,
linePoints);
} else {
numPoints = linePtr->numPoints;
linePoints = linePtr->coordPtr;
}
changedMiterToBevel = 0;
for (count = numPoints, coordPtr = linePoints; count >= 2;
count--, coordPtr += 2) {
if (((linePtr->capStyle == CapRound) && (count == numPoints))
|| ((linePtr->joinStyle == JoinRound)
&& (count != numPoints))) {
dist = hypot(coordPtr[0] - pointPtr[0], coordPtr[1] - pointPtr[1])
- linePtr->width/2.0;
if (dist <= 0.0) {
bestDist = 0.0;
goto done;
} else if (dist < bestDist) {
bestDist = dist;
}
}
if (count == numPoints) {
TkGetButtPoints(coordPtr+2, coordPtr, (double) linePtr->width,
linePtr->capStyle == CapProjecting, poly, poly+2);
} else if ((linePtr->joinStyle == JoinMiter) && !changedMiterToBevel) {
poly[0] = poly[6];
poly[1] = poly[7];
poly[2] = poly[4];
poly[3] = poly[5];
} else {
TkGetButtPoints(coordPtr+2, coordPtr, (double) linePtr->width, 0,
poly, poly+2);
if ((linePtr->joinStyle == JoinBevel) || changedMiterToBevel) {
poly[8] = poly[0];
poly[9] = poly[1];
dist = TkPolygonToPoint(poly, 5, pointPtr);
if (dist <= 0.0) {
bestDist = 0.0;
goto done;
} else if (dist < bestDist) {
bestDist = dist;
}
changedMiterToBevel = 0;
}
}
if (count == 2) {
TkGetButtPoints(coordPtr, coordPtr+2, (double) linePtr->width,
linePtr->capStyle == CapProjecting, poly+4, poly+6);
} else if (linePtr->joinStyle == JoinMiter) {
if (TkGetMiterPoints(coordPtr, coordPtr+2, coordPtr+4,
(double) linePtr->width, poly+4, poly+6) == 0) {
changedMiterToBevel = 1;
TkGetButtPoints(coordPtr, coordPtr+2, (double) linePtr->width,
0, poly+4, poly+6);
}
} else {
TkGetButtPoints(coordPtr, coordPtr+2, (double) linePtr->width, 0,
poly+4, poly+6);
}
poly[8] = poly[0];
poly[9] = poly[1];
dist = TkPolygonToPoint(poly, 5, pointPtr);
if (dist <= 0.0) {
bestDist = 0.0;
goto done;
} else if (dist < bestDist) {
bestDist = dist;
}
}
if (linePtr->capStyle == CapRound) {
dist = hypot(coordPtr[0] - pointPtr[0], coordPtr[1] - pointPtr[1])
- linePtr->width/2.0;
if (dist <= 0.0) {
bestDist = 0.0;
goto done;
} else if (dist < bestDist) {
bestDist = dist;
}
}
if (linePtr->arrow != noneUid) {
if (linePtr->arrow != lastUid) {
dist = TkPolygonToPoint(linePtr->firstArrowPtr, PTS_IN_ARROW,
pointPtr);
if (dist <= 0.0) {
bestDist = 0.0;
goto done;
} else if (dist < bestDist) {
bestDist = dist;
}
}
if (linePtr->arrow != firstUid) {
dist = TkPolygonToPoint(linePtr->lastArrowPtr, PTS_IN_ARROW,
pointPtr);
if (dist <= 0.0) {
bestDist = 0.0;
goto done;
} else if (dist < bestDist) {
bestDist = dist;
}
}
}
done:
if ((linePoints != staticSpace) && (linePoints != linePtr->coordPtr)) {
ckfree((char *) linePoints);
}
return bestDist;
}
static int
LineToArea(canvas, itemPtr, rectPtr)
Tk_Canvas canvas;
Tk_Item *itemPtr;
double *rectPtr;
{
LineItem *linePtr = (LineItem *) itemPtr;
double staticSpace[2*MAX_STATIC_POINTS];
double *linePoints;
int numPoints, result;
if ((linePtr->smooth) && (linePtr->numPoints > 2)) {
numPoints = 1 + linePtr->numPoints*linePtr->splineSteps;
if (numPoints <= MAX_STATIC_POINTS) {
linePoints = staticSpace;
} else {
linePoints = (double *) ckalloc((unsigned)
(2*numPoints*sizeof(double)));
}
numPoints = TkMakeBezierCurve(canvas, linePtr->coordPtr,
linePtr->numPoints, linePtr->splineSteps, (XPoint *) NULL,
linePoints);
} else {
numPoints = linePtr->numPoints;
linePoints = linePtr->coordPtr;
}
result = TkThickPolyLineToArea(linePoints, numPoints,
(double) linePtr->width, linePtr->capStyle, linePtr->joinStyle,
rectPtr);
if (result == 0) {
goto done;
}
if (linePtr->arrow != noneUid) {
if (linePtr->arrow != lastUid) {
if (TkPolygonToArea(linePtr->firstArrowPtr, PTS_IN_ARROW,
rectPtr) != result) {
result = 0;
goto done;
}
}
if (linePtr->arrow != firstUid) {
if (TkPolygonToArea(linePtr->lastArrowPtr, PTS_IN_ARROW,
rectPtr) != result) {
result = 0;
goto done;
}
}
}
done:
if ((linePoints != staticSpace) && (linePoints != linePtr->coordPtr)) {
ckfree((char *) linePoints);
}
return result;
}
static void
ScaleLine(canvas, itemPtr, originX, originY, scaleX, scaleY)
Tk_Canvas canvas;
Tk_Item *itemPtr;
double originX, originY;
double scaleX;
double scaleY;
{
LineItem *linePtr = (LineItem *) itemPtr;
double *coordPtr;
int i;
if (linePtr->firstArrowPtr != NULL) {
linePtr->coordPtr[0] = linePtr->firstArrowPtr[0];
linePtr->coordPtr[1] = linePtr->firstArrowPtr[1];
ckfree((char *) linePtr->firstArrowPtr);
linePtr->firstArrowPtr = NULL;
}
if (linePtr->lastArrowPtr != NULL) {
int i;
i = 2*(linePtr->numPoints-1);
linePtr->coordPtr[i] = linePtr->lastArrowPtr[0];
linePtr->coordPtr[i+1] = linePtr->lastArrowPtr[1];
ckfree((char *) linePtr->lastArrowPtr);
linePtr->lastArrowPtr = NULL;
}
for (i = 0, coordPtr = linePtr->coordPtr; i < linePtr->numPoints;
i++, coordPtr += 2) {
coordPtr[0] = originX + scaleX*(*coordPtr - originX);
coordPtr[1] = originY + scaleY*(coordPtr[1] - originY);
}
if (linePtr->arrow != noneUid) {
ConfigureArrows(canvas, linePtr);
}
ComputeLineBbox(canvas, linePtr);
}
static void
TranslateLine(canvas, itemPtr, deltaX, deltaY)
Tk_Canvas canvas;
Tk_Item *itemPtr;
double deltaX, deltaY;
{
LineItem *linePtr = (LineItem *) itemPtr;
double *coordPtr;
int i;
for (i = 0, coordPtr = linePtr->coordPtr; i < linePtr->numPoints;
i++, coordPtr += 2) {
coordPtr[0] += deltaX;
coordPtr[1] += deltaY;
}
if (linePtr->firstArrowPtr != NULL) {
for (i = 0, coordPtr = linePtr->firstArrowPtr; i < PTS_IN_ARROW;
i++, coordPtr += 2) {
coordPtr[0] += deltaX;
coordPtr[1] += deltaY;
}
}
if (linePtr->lastArrowPtr != NULL) {
for (i = 0, coordPtr = linePtr->lastArrowPtr; i < PTS_IN_ARROW;
i++, coordPtr += 2) {
coordPtr[0] += deltaX;
coordPtr[1] += deltaY;
}
}
ComputeLineBbox(canvas, linePtr);
}
static int
ParseArrowShape(clientData, interp, tkwin, value, recordPtr, offset)
ClientData clientData;
Tcl_Interp *interp;
Tk_Window tkwin;
char *value;
char *recordPtr;
int offset;
{
LineItem *linePtr = (LineItem *) recordPtr;
double a, b, c;
int argc;
char **argv = NULL;
if (offset != Tk_Offset(LineItem, arrowShapeA)) {
panic("ParseArrowShape received bogus offset");
}
if (Tcl_SplitList(interp, value, &argc, &argv) != TCL_OK) {
syntaxError:
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, "bad arrow shape \"", value,
"\": must be list with three numbers", (char *) NULL);
if (argv != NULL) {
ckfree((char *) argv);
}
return TCL_ERROR;
}
if (argc != 3) {
goto syntaxError;
}
if ((Tk_CanvasGetCoord(interp, linePtr->canvas, argv[0], &a) != TCL_OK)
|| (Tk_CanvasGetCoord(interp, linePtr->canvas, argv[1], &b)
!= TCL_OK)
|| (Tk_CanvasGetCoord(interp, linePtr->canvas, argv[2], &c)
!= TCL_OK)) {
goto syntaxError;
}
linePtr->arrowShapeA = a;
linePtr->arrowShapeB = b;
linePtr->arrowShapeC = c;
ckfree((char *) argv);
return TCL_OK;
}
static char *
PrintArrowShape(clientData, tkwin, recordPtr, offset, freeProcPtr)
ClientData clientData;
Tk_Window tkwin;
char *recordPtr;
int offset;
Tcl_FreeProc **freeProcPtr;
{
LineItem *linePtr = (LineItem *) recordPtr;
char *buffer;
buffer = (char *) ckalloc(120);
sprintf(buffer, "%.5g %.5g %.5g", linePtr->arrowShapeA,
linePtr->arrowShapeB, linePtr->arrowShapeC);
*freeProcPtr = TCL_DYNAMIC;
return buffer;
}
static int
ConfigureArrows(canvas, linePtr)
Tk_Canvas canvas;
LineItem *linePtr;
{
double *poly, *coordPtr;
double dx, dy, length, sinTheta, cosTheta, temp;
double fracHeight;
double backup;
double vertX, vertY;
double shapeA, shapeB, shapeC;
shapeA = linePtr->arrowShapeA + 0.001;
shapeB = linePtr->arrowShapeB + 0.001;
shapeC = linePtr->arrowShapeC + linePtr->width/2.0 + 0.001;
fracHeight = (linePtr->width/2.0)/shapeC;
backup = fracHeight*shapeB + shapeA*(1.0 - fracHeight)/2.0;
if (linePtr->arrow != lastUid) {
poly = linePtr->firstArrowPtr;
if (poly == NULL) {
poly = (double *) ckalloc((unsigned)
(2*PTS_IN_ARROW*sizeof(double)));
poly[0] = poly[10] = linePtr->coordPtr[0];
poly[1] = poly[11] = linePtr->coordPtr[1];
linePtr->firstArrowPtr = poly;
}
dx = poly[0] - linePtr->coordPtr[2];
dy = poly[1] - linePtr->coordPtr[3];
length = hypot(dx, dy);
if (length == 0) {
sinTheta = cosTheta = 0.0;
} else {
sinTheta = dy/length;
cosTheta = dx/length;
}
vertX = poly[0] - shapeA*cosTheta;
vertY = poly[1] - shapeA*sinTheta;
temp = shapeC*sinTheta;
poly[2] = poly[0] - shapeB*cosTheta + temp;
poly[8] = poly[2] - 2*temp;
temp = shapeC*cosTheta;
poly[3] = poly[1] - shapeB*sinTheta - temp;
poly[9] = poly[3] + 2*temp;
poly[4] = poly[2]*fracHeight + vertX*(1.0-fracHeight);
poly[5] = poly[3]*fracHeight + vertY*(1.0-fracHeight);
poly[6] = poly[8]*fracHeight + vertX*(1.0-fracHeight);
poly[7] = poly[9]*fracHeight + vertY*(1.0-fracHeight);
linePtr->coordPtr[0] = poly[0] - backup*cosTheta;
linePtr->coordPtr[1] = poly[1] - backup*sinTheta;
}
if (linePtr->arrow != firstUid) {
coordPtr = linePtr->coordPtr + 2*(linePtr->numPoints-2);
poly = linePtr->lastArrowPtr;
if (poly == NULL) {
poly = (double *) ckalloc((unsigned)
(2*PTS_IN_ARROW*sizeof(double)));
poly[0] = poly[10] = coordPtr[2];
poly[1] = poly[11] = coordPtr[3];
linePtr->lastArrowPtr = poly;
}
dx = poly[0] - coordPtr[0];
dy = poly[1] - coordPtr[1];
length = hypot(dx, dy);
if (length == 0) {
sinTheta = cosTheta = 0.0;
} else {
sinTheta = dy/length;
cosTheta = dx/length;
}
vertX = poly[0] - shapeA*cosTheta;
vertY = poly[1] - shapeA*sinTheta;
temp = shapeC*sinTheta;
poly[2] = poly[0] - shapeB*cosTheta + temp;
poly[8] = poly[2] - 2*temp;
temp = shapeC*cosTheta;
poly[3] = poly[1] - shapeB*sinTheta - temp;
poly[9] = poly[3] + 2*temp;
poly[4] = poly[2]*fracHeight + vertX*(1.0-fracHeight);
poly[5] = poly[3]*fracHeight + vertY*(1.0-fracHeight);
poly[6] = poly[8]*fracHeight + vertX*(1.0-fracHeight);
poly[7] = poly[9]*fracHeight + vertY*(1.0-fracHeight);
coordPtr[2] = poly[0] - backup*cosTheta;
coordPtr[3] = poly[1] - backup*sinTheta;
}
return TCL_OK;
}
static int
LineToPostscript(interp, canvas, itemPtr, prepass)
Tcl_Interp *interp;
Tk_Canvas canvas;
Tk_Item *itemPtr;
int prepass;
{
LineItem *linePtr = (LineItem *) itemPtr;
char buffer[200];
char *style;
if (linePtr->fg == NULL) {
return TCL_OK;
}
if ((!linePtr->smooth) || (linePtr->numPoints <= 2)) {
Tk_CanvasPsPath(interp, canvas, linePtr->coordPtr, linePtr->numPoints);
} else {
if (linePtr->fillStipple == None) {
TkMakeBezierPostscript(interp, canvas, linePtr->coordPtr,
linePtr->numPoints);
} else {
double staticPoints[2*MAX_STATIC_POINTS];
double *pointPtr;
int numPoints;
numPoints = 1 + linePtr->numPoints*linePtr->splineSteps;
pointPtr = staticPoints;
if (numPoints > MAX_STATIC_POINTS) {
pointPtr = (double *) ckalloc((unsigned)
(numPoints * 2 * sizeof(double)));
}
numPoints = TkMakeBezierCurve(canvas, linePtr->coordPtr,
linePtr->numPoints, linePtr->splineSteps, (XPoint *) NULL,
pointPtr);
Tk_CanvasPsPath(interp, canvas, pointPtr, numPoints);
if (pointPtr != staticPoints) {
ckfree((char *) pointPtr);
}
}
}
sprintf(buffer, "%d setlinewidth\n", linePtr->width);
Tcl_AppendResult(interp, buffer, (char *) NULL);
style = "0 setlinecap\n";
if (linePtr->capStyle == CapRound) {
style = "1 setlinecap\n";
} else if (linePtr->capStyle == CapProjecting) {
style = "2 setlinecap\n";
}
Tcl_AppendResult(interp, style, (char *) NULL);
style = "0 setlinejoin\n";
if (linePtr->joinStyle == JoinRound) {
style = "1 setlinejoin\n";
} else if (linePtr->joinStyle == JoinBevel) {
style = "2 setlinejoin\n";
}
Tcl_AppendResult(interp, style, (char *) NULL);
if (Tk_CanvasPsColor(interp, canvas, linePtr->fg) != TCL_OK) {
return TCL_ERROR;
};
if (linePtr->fillStipple != None) {
Tcl_AppendResult(interp, "StrokeClip ", (char *) NULL);
if (Tk_CanvasPsStipple(interp, canvas, linePtr->fillStipple)
!= TCL_OK) {
return TCL_ERROR;
}
} else {
Tcl_AppendResult(interp, "stroke\n", (char *) NULL);
}
if (linePtr->firstArrowPtr != NULL) {
if (linePtr->fillStipple != None) {
Tcl_AppendResult(interp, "grestore gsave\n",
(char *) NULL);
}
if (ArrowheadPostscript(interp, canvas, linePtr,
linePtr->firstArrowPtr) != TCL_OK) {
return TCL_ERROR;
}
}
if (linePtr->lastArrowPtr != NULL) {
if (linePtr->fillStipple != None) {
Tcl_AppendResult(interp, "grestore gsave\n", (char *) NULL);
}
if (ArrowheadPostscript(interp, canvas, linePtr,
linePtr->lastArrowPtr) != TCL_OK) {
return TCL_ERROR;
}
}
return TCL_OK;
}
static int
ArrowheadPostscript(interp, canvas, linePtr, arrowPtr)
Tcl_Interp *interp;
Tk_Canvas canvas;
LineItem *linePtr;
double *arrowPtr;
{
Tk_CanvasPsPath(interp, canvas, arrowPtr, PTS_IN_ARROW);
if (linePtr->fillStipple != None) {
Tcl_AppendResult(interp, "clip ", (char *) NULL);
if (Tk_CanvasPsStipple(interp, canvas, linePtr->fillStipple)
!= TCL_OK) {
return TCL_ERROR;
}
} else {
Tcl_AppendResult(interp, "fill\n", (char *) NULL);
}
return TCL_OK;
}