summaryrefslogtreecommitdiff
path: root/gs/base/vdtrace.c
blob: 978747e6bd96804a6fce563241b742062ec931c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* Copyright (C) 2001-2006 Artifex Software, Inc.
   All Rights Reserved.
  
   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied, modified
   or distributed except as expressly authorized under the terms of that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/

/* $Id$ */
/* Visual tracer service */

#include "math_.h"
#include "gxfixed.h"
#include "vdtrace.h"

/* Global data for all instances : */
vd_trace_interface * vd_trace0 = NULL, * vd_trace1 = NULL;
char vd_flags[128] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0""\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
                     "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0""\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
                     "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0""\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
                     "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0""\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";

static double px, py;

#define NullRET if(vd_trace1 == NULL) return

static inline double scale_x(vd_trace_interface *I, double x)
{ return (x - I->orig_x) * I->scale_x + I->shift_x;
}

static inline double scale_y(vd_trace_interface *I, double y)
{ return (y - I->orig_y) * I->scale_y + I->shift_y;
}

#define SX(x) scale_x(vd_trace1, x)
#define SY(y) scale_y(vd_trace1, y)

static inline double bezier_point(double p0, double p1, double p2, double p3, double t)
{   double s = 1-t;
    return p0*s*s*s + 3*p1*s*s*t + 3*p2*s*t*t + p3*t*t*t;
}

static void vd_flatten(double p0x, double p0y, double p1x, double p1y, double p2x, double p2y, double p3x, double p3y)
{   
#ifdef DEBUG
    double flat = 0.5;
    double d2x0 = (p0x - 2 * p1x + p2x), d2y0 = (p0y - 2 * p1y + p2y);
    double d2x1 = (p1x - 2 * p2x + p3x), d2y1 = (p1y - 2 * p2y + p3y);
    double d2norm0 = hypot(d2x0, d2y0);
    double d2norm1 = hypot(d2x1, d2y1);
    double D = max(d2norm0, d2norm1); /* This is half of maximum norm of 2nd derivative of the curve by parameter t. */
    int NN = (int)ceil(sqrt(D * 3 / 4 / flat)); /* Number of output segments. */
    int i;
    int N = max(NN, 1); /* safety (if the curve degenerates to line) */
    double e = 0.5 / N;

    for (i = 0; i < N; i++) {
	double t = (double)i / N + e;
	double px = bezier_point(p0x, p1x, p2x, p3x, t);
	double py = bezier_point(p0y, p1y, p2y, p3y, t);

	vd_lineto(px, py);
    }
    vd_lineto(p3x, p3y);
#endif
}

void vd_impl_moveto(double x, double y)
{   NullRET;
    px = SX(x), py = SY(y);
    vd_trace1->moveto(vd_trace1, px, py);
}

void vd_impl_lineto(double x, double y)
{   NullRET;
    px = SX(x), py = SY(y);
    vd_trace1->lineto(vd_trace1, px, py);
}

void vd_impl_lineto_multi(const struct gs_fixed_point_s *p, int n)
{   int i;
    NullRET;
    for (i = 0; i < n; i++) {
        px = SX(p[i].x), py = SY(p[i].y);
        vd_trace1->lineto(vd_trace1, px, py);
    }
}

void vd_impl_curveto(double x1, double y1, double x2, double y2, double x3, double y3)
{   double p1x, p1y, p2x, p2y, p3x, p3y;

    NullRET;
    p1x = SX(x1), p1y = SY(y1);
    p2x = SX(x2), p2y = SY(y2);
    p3x = SX(x3), p3y = SY(y3);
    if (vd_trace1->curveto != NULL)
        vd_trace1->curveto(vd_trace1, p1x, p1y, p2x, p2y, p3x, p3y);
    else
        vd_flatten(px, py, p1x, p1y, p2x, p2y, p3x, p3y);
    px = p3x, py = p3y;
}

void vd_impl_bar(double x0, double y0, double x1, double y1, int w, unsigned long c)
{   NullRET;
    vd_trace1->setcolor(vd_trace1, c);
    vd_trace1->setlinewidth(vd_trace1, w);
    vd_trace1->beg_path(vd_trace1);
    vd_trace1->moveto(vd_trace1, SX(x0), SY(y0));
    vd_trace1->lineto(vd_trace1, SX(x1), SY(y1));
    vd_trace1->end_path(vd_trace1);
    vd_trace1->stroke(vd_trace1);
}

void vd_impl_square(double x, double y, int w, unsigned int c)
{   NullRET;
    vd_trace1->setcolor(vd_trace1, c);
    vd_trace1->setlinewidth(vd_trace1, 1);
    vd_trace1->beg_path(vd_trace1);
    vd_trace1->moveto(vd_trace1, SX(x) - w, SY(y) - w);
    vd_trace1->lineto(vd_trace1, SX(x) + w, SY(y) - w);
    vd_trace1->lineto(vd_trace1, SX(x) + w, SY(y) + w);
    vd_trace1->lineto(vd_trace1, SX(x) - w, SY(y) + w);
    vd_trace1->lineto(vd_trace1, SX(x) - w, SY(y) - w);
    vd_trace1->end_path(vd_trace1);
    vd_trace1->stroke(vd_trace1);
}

void vd_impl_rect(double x0, double y0, double x1, double y1, int w, unsigned int c)
{   NullRET;
    vd_trace1->setcolor(vd_trace1, c);
    vd_trace1->setlinewidth(vd_trace1, w);
    vd_trace1->beg_path(vd_trace1);
    vd_trace1->moveto(vd_trace1, SX(x0), SY(y0));
    vd_trace1->lineto(vd_trace1, SX(x0), SY(y1));
    vd_trace1->lineto(vd_trace1, SX(x1), SY(y1));
    vd_trace1->lineto(vd_trace1, SX(x1), SY(y0));
    vd_trace1->lineto(vd_trace1, SX(x0), SY(y0));
    vd_trace1->end_path(vd_trace1);
    vd_trace1->stroke(vd_trace1);
}

void vd_impl_quad(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, int w, unsigned int c)
{   NullRET;
    vd_trace1->setcolor(vd_trace1, c);
    vd_trace1->setlinewidth(vd_trace1, w);
    vd_trace1->beg_path(vd_trace1);
    vd_trace1->moveto(vd_trace1, SX(x0), SY(y0));
    vd_trace1->lineto(vd_trace1, SX(x1), SY(y1));
    vd_trace1->lineto(vd_trace1, SX(x2), SY(y2));
    vd_trace1->lineto(vd_trace1, SX(x3), SY(y3));
    vd_trace1->lineto(vd_trace1, SX(x0), SY(y0));
    vd_trace1->end_path(vd_trace1);
    vd_trace1->stroke(vd_trace1);
}

void vd_impl_curve(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, int w, unsigned long c)
{   NullRET;
    vd_trace1->setcolor(vd_trace1, c);
    vd_trace1->setlinewidth(vd_trace1, w);
    vd_trace1->beg_path(vd_trace1);
    vd_trace1->moveto(vd_trace1, SX(x0), SY(y0));
    vd_impl_curveto(x1, y1, x2, y2, x3, y3);
    vd_trace1->end_path(vd_trace1);
    vd_trace1->stroke(vd_trace1);
}

void vd_impl_circle(double x, double y, int r, unsigned long c)
{   NullRET;
    vd_trace1->setcolor(vd_trace1, c);
    vd_trace1->setlinewidth(vd_trace1, 1);
    vd_trace1->circle(vd_trace1, SX(x), SY(y), r);
}

void vd_impl_round(double x, double y, int r, unsigned long c)
{   NullRET;
    vd_trace1->setcolor(vd_trace1, c);
    vd_trace1->setlinewidth(vd_trace1, 1);
    vd_trace1->round(vd_trace1, SX(x), SY(y), r);
}

void vd_impl_pixel(double x, double y, unsigned long c)
{   NullRET;
    vd_trace1->pixel(vd_trace1, SX(x), SY(y), c);
}

void vd_impl_text(double x, double y, char *s, unsigned long c)
{   NullRET;
    vd_trace1->setcolor(vd_trace1, c);
    vd_trace1->text(vd_trace1, SX(x), SY(y), s);
}

void vd_setflag(char f, char v)
{   vd_flags[f & 127] = v;
}