/*
* tkCanvLine.c --
*
* This file implements line items for canvas widgets.
*
* Copyright (c) 1991-1994 The Regents of the University of California.
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tkCanvLine.c 1.44 96/12/02 10:54:33
*/
#include "tkInt.h"
/*
* The structure below defines the record for each line item.
*/
typedef struct LineItem {
* types. MUST BE FIRST IN STRUCTURE. */
* parsing arrow shapes. */
* x- and y-coords of all points in line.
* X-coords are even-valued indices, y-coords
* are corresponding odd-valued indices. If
* the line has arrowheads then the first
* and last points have been adjusted to refer
* to the necks of the arrowheads rather than
* their tips. The actual endpoints are
* stored in the *firstArrowPtr and
* *lastArrowPtr, if they exist. */
* "none", "first", "last", or "both". */
* point, measured along shaft. */
* edge of shaft. */
* describing polygon for arrowhead at first
* point in line. First point of arrowhead
* is tip. Malloc'ed. NULL means no arrowhead
* at first point. */
* point in line (PTS_IN_ARROW points, first
* of which is tip). Malloc'ed. NULL means
* no arrowhead at last point. */
* with Bezier splines). */
} LineItem;
/*
* Number of points in an arrowHead:
*/
/*
* Prototypes for procedures defined in this file:
*/
double *arrowPtr));
Tcl_FreeProc **freeProcPtr));
/*
* Information used for parsing configuration specs. If you change any
* of the default strings, be sure to change the corresponding default
* values in CreateLine.
*/
};
(char *) NULL, 0, 0}
};
/*
* The structures below defines the line item type by means
* of procedures that can be invoked by generic item code.
*/
"line", /* name */
sizeof(LineItem), /* itemSize */
CreateLine, /* createProc */
configSpecs, /* configSpecs */
ConfigureLine, /* configureProc */
LineCoords, /* coordProc */
DeleteLine, /* deleteProc */
DisplayLine, /* displayProc */
0, /* alwaysRedraw */
LineToPoint, /* pointProc */
LineToArea, /* areaProc */
LineToPostscript, /* postscriptProc */
ScaleLine, /* scaleProc */
TranslateLine, /* translateProc */
};
/*
* The Tk_Uid's below refer to uids for the various arrow types:
*/
/*
* The definition below determines how large are static arrays
* used to hold spline points (splines larger than this have to
* have their arrays malloc-ed).
*/
/*
*--------------------------------------------------------------
*
* CreateLine --
*
* This procedure is invoked to create a new line item in
* a canvas.
*
* Results:
* A standard Tcl return value. If an error occurred in
* creating the item, then an error message is left in
* interp->result; in this case itemPtr is left uninitialized,
* so it can be safely freed by the caller.
*
* Side effects:
* A new line item is created.
*
*--------------------------------------------------------------
*/
static int
* has been initialized by caller. */
int argc; /* Number of arguments in argv. */
char **argv; /* Arguments describing line. */
{
int i;
if (argc < 4) {
(char *) NULL);
return TCL_ERROR;
}
/*
* Carry out initialization that is needed to set defaults and to
* allow proper cleanup after errors during the the remainder of
* this procedure.
*/
}
/*
* Count the number of points and then parse them into a point
* array. Leading arguments are assumed to be points if they
* start with a digit or a minus sign followed by a digit.
*/
((argv[i][0] != '-')
break;
}
}
goto error;
}
return TCL_OK;
}
return TCL_ERROR;
}
/*
*--------------------------------------------------------------
*
* LineCoords --
*
* This procedure is invoked to process the "coords" widget
* command on lines. See the user documentation for details
* on what it does.
*
* Results:
* Returns TCL_OK or TCL_ERROR, and sets interp->result.
*
* Side effects:
* The coordinates for the given item may be changed.
*
*--------------------------------------------------------------
*/
static int
* read or modified. */
int argc; /* Number of coordinates supplied in
* argv. */
char **argv; /* Array of coordinates: x1, y1,
* x2, y2, ... */
{
int i, numPoints;
if (argc == 0) {
double *coordPtr;
int numCoords;
} else {
}
if (i == 2) {
}
}
}
} else if (argc < 4) {
"too few coordinates for line: must have at least 4",
(char *) NULL);
return TCL_ERROR;
} else if (argc & 1) {
"odd number of coordinates specified for line",
(char *) NULL);
return TCL_ERROR;
} else {
}
(sizeof(double) * argc));
}
for (i = argc-1; i >= 0; i--) {
return TCL_ERROR;
}
}
/*
* Update arrowheads by throwing away any existing arrow-head
* information and calling ConfigureArrows to recompute it.
*/
}
}
}
}
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* ConfigureLine --
*
* This procedure is invoked to configure various aspects
* of a line item such as its background color.
*
* Results:
* A standard Tcl result code. If an error occurs, then
* an error message is left in interp->result.
*
* Side effects:
* Configuration information, such as colors and stipple
* patterns, may be set for itemPtr.
*
*--------------------------------------------------------------
*/
static int
int argc; /* Number of elements in argv. */
char **argv; /* Arguments describing things to configure. */
int flags; /* Flags to pass to Tk_ConfigureWidget. */
{
unsigned long mask;
return TCL_ERROR;
}
/*
* A few of the options require additional processing, such as
* graphics contexts.
*/
} else {
}
}
mask |= GCCapStyle;
}
gcValues.line_width = 0;
}
}
}
/*
* Keep spline parameters within reasonable limits.
*/
}
/*
* Setup arrowheads, if needed. If arrowheads are turned off,
* restore the line's endpoints (they were shortened when the
* arrowheads were added).
*/
}
int i;
}
(char *) NULL);
return TCL_ERROR;
}
}
/*
* Recompute bounding box for line.
*/
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* DeleteLine --
*
* This procedure is called to clean up the data structure
* associated with a line item.
*
* Results:
* None.
*
* Side effects:
* Resources associated with itemPtr are released.
*
*--------------------------------------------------------------
*/
static void
* canvas. */
{
}
}
}
}
}
}
}
}
/*
*--------------------------------------------------------------
*
* ComputeLineBbox --
*
* This procedure is invoked to compute the bounding box of
* all the pixels that may be drawn as part of a line.
*
* Results:
* None.
*
* Side effects:
* The fields x1, y1, x2, and y2 are updated in the header
* for itemPtr.
*
*--------------------------------------------------------------
*/
static void
* recomputed. */
{
double *coordPtr;
int i, width;
/*
* Compute the bounding box of all the points in the line,
* then expand in all directions by the line's width to take
* care of butting or rounded corners and projecting or
* rounded caps. This expansion is an overestimate (worst-case
* is square root of two over two) but it's simple. Don't do
* anything special for curves. This causes an additional
* overestimate in the bounding box, but is faster.
*/
i++, coordPtr += 2) {
}
if (width < 1) {
width = 1;
}
/*
* For mitered lines, make a second pass through all the points.
* Compute the locations of the two miter vertex points and add
* those into the bounding box.
*/
i--, coordPtr += 2) {
int j;
for (j = 0; j < 4; j += 2) {
}
}
}
}
/*
* Add in the sizes of arrowheads, if any.
*/
i++, coordPtr += 2) {
}
}
i++, coordPtr += 2) {
}
}
}
/*
* Add one more pixel of fudge factor just to be safe (e.g.
* X may round differently than we do).
*/
}
/*
*--------------------------------------------------------------
*
* DisplayLine --
*
* This procedure is invoked to draw a line item in a given
* drawable.
*
* Results:
* None.
*
* Side effects:
* ItemPtr is drawn in drawable using the transformation
* information in canvas.
*
*--------------------------------------------------------------
*/
static void
* item. */
* must be redisplayed (not used). */
{
double *coordPtr;
int i, numPoints;
return;
}
/*
* Build up an array of points in screen coordinates. Use a
* static array unless the line has an enormous number of points;
* in this case, dynamically allocate an array. For smoothed lines,
* generate the curve points on each redisplay.
*/
} else {
}
if (numPoints <= MAX_STATIC_POINTS) {
} else {
}
(double *) NULL);
} else {
}
}
/*
* Display line, the free up line storage if it was dynamically
* allocated. If we're stippling, then modify the stipple offset
* in the GC. Be sure to reset the offset when done, since the
* GC is supposed to be read-only.
*/
}
if (pointPtr != staticPoints) {
}
/*
* Display arrowheads, if they are wanted.
*/
}
}
}
}
/*
*--------------------------------------------------------------
*
* LineToPoint --
*
* Computes the distance from a given point to a given
* line, in canvas units.
*
* Results:
* The return value is 0 if the point whose x and y coordinates
* are pointPtr[0] and pointPtr[1] is inside the line. If the
* point isn't inside the line then the return value is the
* distance from the point to the line.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
/* ARGSUSED */
static double
double *pointPtr; /* Pointer to x and y coordinates. */
{
* had to be treated as beveled after all
* because the angle was < 11 degrees. */
bestDist = 1.0e36;
/*
* Handle smoothed lines by generating an expanded set of points
* against which to do the check.
*/
if (numPoints <= MAX_STATIC_POINTS) {
} else {
linePoints = (double *) ckalloc((unsigned)
(2*numPoints*sizeof(double)));
}
} else {
}
/*
* The overall idea is to iterate through all of the edges of
* the line, computing a polygon for each edge and testing the
* point against that polygon. In addition, there are additional
* tests to deal with rounded joints and caps.
*/
changedMiterToBevel = 0;
/*
* If rounding is done around the first point then compute
* the distance between the point and the point.
*/
if (dist <= 0.0) {
bestDist = 0.0;
goto done;
}
}
/*
* Compute the polygonal shape corresponding to this edge,
* consisting of two points for the first point of the edge
* and two points for the last point of the edge.
*/
} else {
/*
* If this line uses beveled joints, then check the distance
* to a polygon comprising the last two points of the previous
* polygon and the first two from this polygon; this checks
* the wedges that fill the mitered joint.
*/
if (dist <= 0.0) {
bestDist = 0.0;
goto done;
}
changedMiterToBevel = 0;
}
}
if (count == 2) {
changedMiterToBevel = 1;
}
} else {
}
if (dist <= 0.0) {
bestDist = 0.0;
goto done;
}
}
/*
* If caps are rounded, check the distance to the cap around the
* final end point of the line.
*/
if (dist <= 0.0) {
bestDist = 0.0;
goto done;
}
}
/*
* If there are arrowheads, check the distance to the arrowheads.
*/
pointPtr);
if (dist <= 0.0) {
bestDist = 0.0;
goto done;
}
}
pointPtr);
if (dist <= 0.0) {
bestDist = 0.0;
goto done;
}
}
}
done:
ckfree((char *) linePoints);
}
return bestDist;
}
/*
*--------------------------------------------------------------
*
* LineToArea --
*
* This procedure is called to determine whether an item
* lies entirely inside, entirely outside, or overlapping
* a given rectangular area.
*
* Results:
* -1 is returned if the item is entirely outside the
* area, 0 if it overlaps, and 1 if it is entirely
* inside the given area.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
/* ARGSUSED */
static int
double *rectPtr;
{
double *linePoints;
/*
* Handle smoothed lines by generating an expanded set of points
* against which to do the check.
*/
if (numPoints <= MAX_STATIC_POINTS) {
} else {
linePoints = (double *) ckalloc((unsigned)
(2*numPoints*sizeof(double)));
}
} else {
}
/*
* Check the segments of the line.
*/
rectPtr);
if (result == 0) {
goto done;
}
/*
* Check arrowheads, if any.
*/
result = 0;
goto done;
}
}
result = 0;
goto done;
}
}
}
done:
ckfree((char *) linePoints);
}
return result;
}
/*
*--------------------------------------------------------------
*
* ScaleLine --
*
* This procedure is invoked to rescale a line item.
*
* Results:
* None.
*
* Side effects:
* The line referred to by itemPtr is rescaled so that the
* following transformation is applied to all point
* coordinates:
* x' = originX + scaleX*(x-originX)
* y' = originY + scaleY*(y-originY)
*
*--------------------------------------------------------------
*/
static void
double scaleX; /* Amount to scale in X direction. */
double scaleY; /* Amount to scale in Y direction. */
{
double *coordPtr;
int i;
/*
* Delete any arrowheads before scaling all the points (so that
* the end-points of the line get restored).
*/
}
int i;
}
i++, coordPtr += 2) {
}
}
}
/*
*--------------------------------------------------------------
*
* TranslateLine --
*
* This procedure is called to move a line by a given amount.
*
* Results:
* None.
*
* Side effects:
* The position of the line is offset by (xDelta, yDelta), and
* the bounding box is updated in the generic part of the item
* structure.
*
*--------------------------------------------------------------
*/
static void
* moved. */
{
double *coordPtr;
int i;
i++, coordPtr += 2) {
}
i++, coordPtr += 2) {
}
}
i++, coordPtr += 2) {
}
}
}
/*
*--------------------------------------------------------------
*
* ParseArrowShape --
*
* This procedure is called back during option parsing to
* parse arrow shape information.
*
* Results:
* The return value is a standard Tcl result: TCL_OK means
* that the arrow shape information was parsed ok, and
* TCL_ERROR means it couldn't be parsed.
*
* Side effects:
* Arrow information in recordPtr is updated.
*
*--------------------------------------------------------------
*/
/* ARGSUSED */
static int
char *value; /* Textual specification of arrow shape. */
char *recordPtr; /* Pointer to item record in which to
* store arrow information. */
int offset; /* Offset of shape information in widget
* record. */
{
double a, b, c;
int argc;
panic("ParseArrowShape received bogus offset");
}
"\": must be list with three numbers", (char *) NULL);
}
return TCL_ERROR;
}
if (argc != 3) {
goto syntaxError;
}
!= TCL_OK)
!= TCL_OK)) {
goto syntaxError;
}
linePtr->arrowShapeA = a;
linePtr->arrowShapeB = b;
linePtr->arrowShapeC = c;
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* PrintArrowShape --
*
* This procedure is a callback invoked by the configuration
* code to return a printable value describing an arrow shape.
*
* Results:
* None.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
/* ARGSUSED */
static char *
char *recordPtr; /* Pointer to item record containing current
* shape information. */
int offset; /* Offset of arrow information in record. */
* free string here. */
{
char *buffer;
return buffer;
}
/*
*--------------------------------------------------------------
*
* ConfigureArrows --
*
* If arrowheads have been requested for a line, this
* procedure makes arrangements for the arrowheads.
*
* Results:
* Always returns TCL_OK.
*
* Side effects:
* Information in linePtr is set up for one or two arrowheads.
* the firstArrowPtr and lastArrowPtr polygons are allocated
* and initialized, if need be, and the end points of the line
* are adjusted so that a thick line doesn't stick out past
* the arrowheads.
*
*--------------------------------------------------------------
*/
/* ARGSUSED */
static int
* displayed (interp and tkwin
* fields are needed). */
{
* arrowhead width. */
* so the line ends in the middle
* of the arrowhead. */
* explanation below). */
/*
* The code below makes a tiny increase in the shape parameters
* for the line. This is a bit of a hack, but it seems to result
* in displays that more closely approximate the specified parameters.
* Without the adjustment, the arrows come out smaller than expected.
*/
/*
* If there's an arrowhead on the first point of the line, compute
* its polygon and adjust the first point of the line so that the
* line doesn't stick out past the leading edge of the arrowhead.
*/
(2*PTS_IN_ARROW*sizeof(double)));
}
if (length == 0) {
} else {
}
/*
* Polygon done. Now move the first point towards the second so
* that the corners at the end of the line are inside the
* arrowhead.
*/
}
/*
* Similar arrowhead calculation for the last point of the line.
*/
(2*PTS_IN_ARROW*sizeof(double)));
}
if (length == 0) {
} else {
}
}
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* LineToPostscript --
*
* This procedure is called to generate Postscript for
* line items.
*
* Results:
* The return value is a standard Tcl result. If an error
* occurs in generating Postscript then an error message is
* left in interp->result, replacing whatever used
* to be there. If no error occurs, then Postscript for the
* item is appended to the result.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
static int
* here. */
* wanted. */
int prepass; /* 1 means this is a prepass to
* collect font information; 0 means
* final Postscript is being created. */
{
char *style;
return TCL_OK;
}
/*
* Generate a path for the line's center-line (do this differently
* for straight lines and smoothed lines).
*/
} else {
} else {
/*
* Special hack: Postscript printers don't appear to be able
* to turn a path drawn with "curveto"s into a clipping path
* without exceeding resource limits, so TkMakeBezierPostscript
* won't work for stippled curves. Instead, generate all of
* the intermediate points here and output them into the
* Postscript file with "lineto"s instead.
*/
double *pointPtr;
int numPoints;
if (numPoints > MAX_STATIC_POINTS) {
(numPoints * 2 * sizeof(double)));
}
pointPtr);
if (pointPtr != staticPoints) {
}
}
}
/*
* Set other line-drawing parameters and stroke out the line.
*/
style = "0 setlinecap\n";
style = "1 setlinecap\n";
style = "2 setlinecap\n";
}
style = "0 setlinejoin\n";
style = "1 setlinejoin\n";
style = "2 setlinejoin\n";
}
return TCL_ERROR;
};
!= TCL_OK) {
return TCL_ERROR;
}
} else {
}
/*
* Output polygons for the arrowheads, if there are any.
*/
(char *) NULL);
}
return TCL_ERROR;
}
}
}
return TCL_ERROR;
}
}
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* ArrowheadPostscript --
*
* This procedure is called to generate Postscript for
* an arrowhead for a line item.
*
* Results:
* The return value is a standard Tcl result. If an error
* occurs in generating Postscript then an error message is
* left in interp->result, replacing whatever used
* to be there. If no error occurs, then Postscript for the
* arrowhead is appended to the result.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
static int
* here. */
* being generated. */
double *arrowPtr; /* Pointer to first of five points
* describing arrowhead polygon. */
{
!= TCL_OK) {
return TCL_ERROR;
}
} else {
}
return TCL_OK;
}