~ubuntu-branches/ubuntu/lucid/graphviz/lucid-security

« back to all changes in this revision

Viewing changes to tkspline/tkspline.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen M Moraco
  • Date: 2002-02-05 18:52:12 UTC
  • Revision ID: james.westby@ubuntu.com-20020205185212-8i04c70te00rc40y
Tags: upstream-1.7.16
ImportĀ upstreamĀ versionĀ 1.7.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <tk.h>
 
2
#include <tkInt.h>
 
3
 
 
4
#ifdef HAVE_CONFIG_H
 
5
#include "gvconfig.h"
 
6
#endif
 
7
 
 
8
/*
 
9
 * Prototypes for procedures defined in this file:
 
10
 */
 
11
 
 
12
static int SplineCurve _ANSI_ARGS_((Tk_Canvas canvas,
 
13
            double *pointPtr, int numPoints, int numSteps,
 
14
            XPoint xPoints[], double dblPoints[]));
 
15
 
 
16
static void SplineCurvePostscript _ANSI_ARGS_((Tcl_Interp *interp,
 
17
            Tk_Canvas canvas, double *pointPtr, 
 
18
            int numPoints, int numSteps));
 
19
                           
 
20
#if TARGET_OS_MAC
 
21
/*
 
22
 * "export" is a MetroWerks specific pragma.  It flags the linker that  
 
23
 * any symbols that are defined when this pragma is on will be exported 
 
24
 * to shared libraries that link with this library.
 
25
 */
 
26
#   pragma export on
 
27
    int Tkspline_Init( Tcl_Interp *interp );
 
28
        int Tkspline_SafeInit( Tcl_Interp *interp );
 
29
#   pragma export reset
 
30
 
 
31
#   define VERSION "0.4"
 
32
#endif
 
33
 
 
34
int Tkspline_Init _ANSI_ARGS_((Tcl_Interp *interp));
 
35
 
 
36
int Tkspline_SafeInit _ANSI_ARGS_((Tcl_Interp *interp));
 
37
 
 
38
/* 
 
39
 * structure that is hooked in to describe the new smoothing method 
 
40
 */
 
41
 
 
42
static Tk_SmoothMethod splineSmoothMethod = {
 
43
    "spline",                         /* used as in:  -smooth spline  */
 
44
    SplineCurve,                      /* canvas curve generator */
 
45
    SplineCurvePostscript,            /* postscript curve generator */
 
46
};
 
47
 
 
48
 
 
49
/*
 
50
 *--------------------------------------------------------------
 
51
 *
 
52
 * SplineCurve --
 
53
 *
 
54
 *        Given a set of spline control points draw the Bezier splines. 
 
55
 *        There must be 3N+1 points for bezier splines, otherwise smoothing
 
56
 *        defaults to using the builtin algorithm.
 
57
 *        Produces output points in either of two    forms.
 
58
 *
 
59
 * Results:
 
60
 *        Either or both of the xPoints or dblPoints arrays are filled
 
61
 *        in.  The return value is the number of points placed in the
 
62
 *        arrays.
 
63
 *
 
64
 * Side effects:
 
65
 *        None.
 
66
 *
 
67
 *--------------------------------------------------------------
 
68
 */
 
69
 
 
70
 
 
71
static int
 
72
SplineCurve(canvas, pointPtr, numPoints, numSteps, xPoints, dblPoints)
 
73
    Tk_Canvas canvas;                    /* Canvas in which curve is to be
 
74
                                         * drawn. */
 
75
    double *pointPtr;                    /* Array of input coordinates:  x0,
 
76
                                         * y0, x1, y1, etc.. */
 
77
    int numPoints;                        /* Number of points at pointPtr. */
 
78
    int numSteps;                        /* Number of steps to use for each
 
79
                                         * spline segments (determines
 
80
                                         * smoothness of curve). */
 
81
    XPoint xPoints[];                    /* Array of XPoints to fill in (e.g.
 
82
                                         * for display.  NULL means don't
 
83
                                         * fill in any XPoints. */
 
84
    double dblPoints[];                    /* Array of points to fill in as
 
85
                                         * doubles, in the form x0, y0,
 
86
                                         * x1, y1, ....  NULL means don't
 
87
                                         * fill in anything in this form. 
 
88
                                         * Caller must make sure that this
 
89
                                         * array has enough space. */
 
90
{
 
91
    int outputPoints, i;
 
92
 
 
93
    /* if the number of points is invalid, use the old function */
 
94
    if ((numPoints < 4) || (numPoints % 3 != 1)) {
 
95
        return TkMakeBezierCurve(canvas, pointPtr, numPoints, numSteps,
 
96
                xPoints, dblPoints);
 
97
    }
 
98
    /* if pointPtr == NULL, just return a maximum for the number of
 
99
     * points to be calculated */
 
100
    if (!pointPtr) {
 
101
        return (1 + (numPoints/3)*numSteps);
 
102
    }
 
103
    outputPoints = 0;
 
104
    if (xPoints != NULL) {
 
105
        Tk_CanvasDrawableCoords(canvas, pointPtr[0], pointPtr[1],
 
106
                &xPoints->x, &xPoints->y);
 
107
        xPoints += 1;
 
108
    }
 
109
    if (dblPoints != NULL) {
 
110
        dblPoints[0] = pointPtr[0];
 
111
        dblPoints[1] = pointPtr[1];
 
112
        dblPoints += 2;
 
113
    }
 
114
    outputPoints += 1;
 
115
 
 
116
    for (i = 2; i < numPoints; i += 3, pointPtr += 6) {
 
117
        if (xPoints != NULL) {
 
118
            TkBezierScreenPoints(canvas, pointPtr, numSteps, xPoints);
 
119
            xPoints += numSteps;
 
120
        }
 
121
        if (dblPoints != NULL) {
 
122
            TkBezierPoints(pointPtr, numSteps, dblPoints);
 
123
            dblPoints += 2*numSteps;
 
124
        }
 
125
        outputPoints += numSteps;
 
126
    }
 
127
    return outputPoints;
 
128
}
 
129
 
 
130
/*
 
131
 *--------------------------------------------------------------
 
132
 *
 
133
 * SplineCurvePostscript --
 
134
 *
 
135
 *    This procedure generates Postscript commands that create
 
136
 *    a path corresponding to a given Bezier curve.
 
137
 *
 
138
 * Results:
 
139
 *    None.  Postscript commands to generate the path are appended
 
140
 *    to interp->result.
 
141
 *
 
142
 * Side effects:
 
143
 *    None.
 
144
 *
 
145
 *--------------------------------------------------------------
 
146
 */
 
147
 
 
148
static void
 
149
SplineCurvePostscript(interp, canvas, pointPtr, numPoints, numSteps)
 
150
    Tcl_Interp *interp;               /* Interpreter in whose result the
 
151
                                       * Postscript is to be stored.  */
 
152
    Tk_Canvas canvas;                 /* Canvas widget for which the
 
153
                                       * Postscript is being generated. */
 
154
    double *pointPtr;                 /* Array of input coordinates: x0,
 
155
                                       * y0, x1, y1, etc.. */
 
156
    int numPoints;                    /* Number of points at pointPtr. */
 
157
#if (TK_MAJOR_VERSION < 8) || ((TK_MAJOR_VERSION == 8) && (TK_MAJOR_VERSION < 3))
 
158
    int numSteps;                      /* Not Used */
 
159
#endif
 
160
{
 
161
    int i;
 
162
    char buffer[200];
 
163
 
 
164
    /* if the number of points is invalid, use the old function */
 
165
    if ((numPoints < 4) || (numPoints % 3 != 1)) {
 
166
#if (TK_MAJOR_VERSION < 8) || ((TK_MAJOR_VERSION == 8) && (TK_MAJOR_VERSION < 3))
 
167
        TkMakeBezierPostscript(interp, canvas, pointPtr, numPoints, numSteps);
 
168
#else
 
169
        TkMakeBezierPostscript(interp, canvas, pointPtr, numPoints);
 
170
#endif
 
171
        return;
 
172
    }
 
173
 
 
174
    sprintf(buffer, "%.15g %.15g moveto\n",
 
175
      pointPtr[0], Tk_CanvasPsY(canvas, pointPtr[1]));
 
176
    Tcl_AppendResult(interp, buffer, (char *) NULL);
 
177
 
 
178
    /*
 
179
     * Cycle through all the remaining points in the curve, generating
 
180
     * a curve section for each vertex in the linear path.
 
181
     */
 
182
 
 
183
    for (i = numPoints-2, pointPtr += 2; i > 0; i -= 3, pointPtr += 6) {
 
184
      sprintf(buffer, "%.15g %.15g %.15g %.15g %.15g %.15g curveto\n",
 
185
              pointPtr[0], Tk_CanvasPsY(canvas, pointPtr[1]),
 
186
              pointPtr[2], Tk_CanvasPsY(canvas, pointPtr[3]),
 
187
              pointPtr[4], Tk_CanvasPsY(canvas, pointPtr[5]));
 
188
      Tcl_AppendResult(interp, buffer, (char *) NULL);
 
189
    }
 
190
}
 
191
 
 
192
/*
 
193
 *--------------------------------------------------------------
 
194
 *
 
195
 * Tkspline_Init -- Function to call on loading the Tkspline DLL
 
196
 *
 
197
 * Results: Returns TCL_OK, or TCL_ERROR if unable to load.
 
198
 *
 
199
 * Side effects: Package Tkspline added to package list
 
200
 *         lines and polygons have a new "spline" smooth method.
 
201
 *
 
202
 *--------------------------------------------------------------
 
203
 */
 
204
 
 
205
int Tkspline_Init(interp)
 
206
    Tcl_Interp *interp;
 
207
{
 
208
#ifdef USE_TCL_STUBS
 
209
    if (Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL) {
 
210
        return TCL_ERROR;
 
211
    }
 
212
    if (Tk_InitStubs(interp, TK_VERSION, 0) == NULL) {
 
213
        return TCL_ERROR;
 
214
    }
 
215
#else
 
216
    if (Tcl_PkgRequire(interp, "Tcl", TCL_VERSION, 0) != TCL_OK) {
 
217
        return TCL_ERROR;
 
218
    }
 
219
    if (Tcl_PkgRequire(interp, "Tk", TK_VERSION, 0) != TCL_OK) {
 
220
        return TCL_ERROR;
 
221
    }
 
222
#endif
 
223
 
 
224
    Tk_CreateSmoothMethod(interp, &splineSmoothMethod);
 
225
 
 
226
    return Tcl_PkgProvide(interp, "Tkspline", VERSION);
 
227
}
 
228
 
 
229
int Tkspline_SafeInit(Tcl_Interp * interp)
 
230
{   
 
231
    return Tkspline_Init(interp);
 
232
}