graphics/psdrv forgets to pull in @DLLFLAGS@, and so is compiled non-PIC if
[wine] / graphics / psdrv / graphics.c
1 /*
2  *      PostScript driver graphics functions
3  *
4  *      Copyright 1998  Huw D M Davies
5  *
6  */
7 #include <string.h>
8 #include <math.h>
9 #include "windows.h"
10 #include "psdrv.h"
11 #include "debug.h"
12 #include "print.h"
13 #ifndef PI
14 #define PI M_PI
15 #endif
16
17 /**********************************************************************
18  *           PSDRV_MoveToEx
19  */
20 BOOL32 PSDRV_MoveToEx(DC *dc, INT32 x, INT32 y, LPPOINT32 pt)
21 {
22     TRACE(psdrv, "%d %d\n", x, y);
23     if (pt)
24     {
25         pt->x = dc->w.CursPosX;
26         pt->y = dc->w.CursPosY;
27     }
28     dc->w.CursPosX = x;
29     dc->w.CursPosY = y;
30
31     return TRUE;
32 }
33
34
35 /***********************************************************************
36  *           PSDRV_LineTo
37  */
38 BOOL32 PSDRV_LineTo(DC *dc, INT32 x, INT32 y)
39 {
40     TRACE(psdrv, "%d %d\n", x, y);
41
42     PSDRV_SetPen(dc);
43     PSDRV_WriteMoveTo(dc, XLPTODP(dc, dc->w.CursPosX),
44                           YLPTODP(dc, dc->w.CursPosY));
45     PSDRV_WriteLineTo(dc, XLPTODP(dc, x), YLPTODP(dc, y));
46     PSDRV_WriteStroke(dc);
47
48     dc->w.CursPosX = x;
49     dc->w.CursPosY = y;
50     return TRUE;
51 }
52
53
54 /***********************************************************************
55  *           PSDRV_Rectangle
56  */
57 BOOL32 PSDRV_Rectangle( DC *dc, INT32 left, INT32 top, INT32 right,
58                        INT32 bottom )
59 {
60     INT32 width = XLSTODS(dc, right - left);
61     INT32 height = YLSTODS(dc, bottom - top);
62
63
64     TRACE(psdrv, "%d %d - %d %d\n", left, top, right, bottom);
65
66     PSDRV_WriteRectangle(dc, XLPTODP(dc, left), YLPTODP(dc, top),
67                              width, height);
68
69     PSDRV_Brush(dc,0);
70     PSDRV_SetPen(dc);
71     PSDRV_WriteStroke(dc);
72     return TRUE;
73 }
74
75
76 /***********************************************************************
77  *           PSDRV_RoundRect
78  */
79 BOOL32 PSDRV_RoundRect( DC *dc, INT32 left, INT32 top, INT32 right,
80                         INT32 bottom, INT32 ell_width, INT32 ell_height )
81 {
82     left = XLPTODP( dc, left );
83     right = XLPTODP( dc, right );
84     top = YLPTODP( dc, top );
85     bottom = YLPTODP( dc, bottom );
86     ell_width = XLSTODS( dc, ell_width );
87     ell_height = YLSTODS( dc, ell_height );
88
89     if( left > right ) { INT32 tmp = left; left = right; right = tmp; }
90     if( top > bottom ) { INT32 tmp = top; top = bottom; bottom = tmp; }
91
92     if(ell_width > right - left) ell_width = right - left;
93     if(ell_height > bottom - top) ell_height = bottom - top;
94
95     PSDRV_WriteMoveTo( dc, left, top + ell_height/2 );
96     PSDRV_WriteArc( dc, left + ell_width/2, top + ell_height/2, ell_width,
97                     ell_height, 90.0, 180.0);
98     PSDRV_WriteLineTo( dc, right - ell_width/2, top );
99     PSDRV_WriteArc( dc, right - ell_width/2, top + ell_height/2, ell_width,
100                     ell_height, 0.0, 90.0);
101     PSDRV_WriteLineTo( dc, right, bottom - ell_height/2 );
102     PSDRV_WriteArc( dc, right - ell_width/2, bottom - ell_height/2, ell_width,
103                     ell_height, -90.0, 0.0);
104     PSDRV_WriteLineTo( dc, right - ell_width/2, bottom);
105     PSDRV_WriteArc( dc, left + ell_width/2, bottom - ell_height/2, ell_width,
106                     ell_height, 180.0, -90.0);
107     PSDRV_WriteClosePath( dc );
108
109     PSDRV_Brush(dc,0);
110     PSDRV_SetPen(dc);
111     PSDRV_WriteStroke(dc);
112     return TRUE;
113 }
114
115 /***********************************************************************
116  *           PSDRV_DrawArc
117  *
118  * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
119  */
120 static BOOL32 PSDRV_DrawArc( DC *dc, INT32 left, INT32 top, 
121                              INT32 right, INT32 bottom,
122                              INT32 xstart, INT32 ystart,
123                              INT32 xend, INT32 yend,
124                              int lines )
125 {
126     INT32 x, y, h, w;
127     double start_angle, end_angle, ratio;
128
129     x = XLPTODP(dc, (left + right)/2);
130     y = YLPTODP(dc, (top + bottom)/2);
131
132     w = XLSTODS(dc, (right - left));
133     h = YLSTODS(dc, (bottom - top));
134
135     if(w < 0) w = -w;
136     if(h < 0) h = -h;
137     ratio = ((double)w)/h;
138
139     /* angle is the angle after the rectangle is transformed to a square and is
140        measured anticlockwise from the +ve x-axis */
141
142     start_angle = atan2((double)(y - ystart) * ratio, (double)(xstart - x));
143     end_angle = atan2((double)(y - yend) * ratio, (double)(xend - x));
144
145     start_angle *= 180.0 / PI;
146     end_angle *= 180.0 / PI;
147
148     if(lines == 2) /* pie */
149         PSDRV_WriteMoveTo(dc, x, y);
150     PSDRV_WriteArc(dc, x, y, w, h, start_angle, end_angle);
151     if(lines == 1 || lines == 2) { /* chord or pie */
152         PSDRV_WriteClosePath(dc);
153         PSDRV_Brush(dc,0);
154     }
155     PSDRV_SetPen(dc);
156     PSDRV_WriteStroke(dc);
157     return TRUE;
158 }
159
160
161 /***********************************************************************
162  *           PSDRV_Arc
163  */
164 BOOL32 PSDRV_Arc( DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom,
165                   INT32 xstart, INT32 ystart, INT32 xend, INT32 yend )
166 {
167     return PSDRV_DrawArc( dc, left, top, right, bottom, xstart, ystart,
168                          xend, yend, 0 );
169 }
170
171 /***********************************************************************
172  *           PSDRV_Chord
173  */
174 BOOL32 PSDRV_Chord( DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom,
175                   INT32 xstart, INT32 ystart, INT32 xend, INT32 yend )
176 {
177     return PSDRV_DrawArc( dc, left, top, right, bottom, xstart, ystart,
178                          xend, yend, 1 );
179 }
180
181
182 /***********************************************************************
183  *           PSDRV_Pie
184  */
185 BOOL32 PSDRV_Pie( DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom,
186                   INT32 xstart, INT32 ystart, INT32 xend, INT32 yend )
187 {
188     return PSDRV_DrawArc( dc, left, top, right, bottom, xstart, ystart,
189                          xend, yend, 2 );
190 }
191
192
193 /***********************************************************************
194  *           PSDRV_Ellipse
195  */
196 BOOL32 PSDRV_Ellipse( DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom)
197 {
198     INT32 x, y, w, h;
199
200     TRACE(psdrv, "%d %d - %d %d\n", left, top, right, bottom);
201
202     x = XLPTODP(dc, (left + right)/2);
203     y = YLPTODP(dc, (top + bottom)/2);
204
205     w = XLSTODS(dc, (right - left));
206     h = YLSTODS(dc, (bottom - top));
207
208     PSDRV_WriteArc(dc, x, y, w, h, 0.0, 360.0);
209     PSDRV_WriteClosePath(dc);
210     PSDRV_Brush(dc,0);
211     PSDRV_SetPen(dc);
212     PSDRV_WriteStroke(dc);
213     return TRUE;
214 }
215
216
217 /***********************************************************************
218  *           PSDRV_PolyPolyline
219  */
220 BOOL32 PSDRV_PolyPolyline( DC *dc, const POINT32* pts, const DWORD* counts,
221                            DWORD polylines )
222 {
223     DWORD polyline, line;
224     const POINT32* pt;
225     TRACE(psdrv, "\n");
226
227     pt = pts;
228     for(polyline = 0; polyline < polylines; polyline++) {
229         PSDRV_WriteMoveTo(dc, XLPTODP(dc, pt->x), YLPTODP(dc, pt->y));
230         pt++;
231         for(line = 1; line < counts[polyline]; line++) {
232             PSDRV_WriteLineTo(dc, XLPTODP(dc, pt->x), YLPTODP(dc, pt->y));
233             pt++;
234         }
235     }
236     PSDRV_SetPen(dc);
237     PSDRV_WriteStroke(dc);
238     return TRUE;
239 }   
240
241
242 /***********************************************************************
243  *           PSDRV_Polyline
244  */
245 BOOL32 PSDRV_Polyline( DC *dc, const POINT32* pt, INT32 count )
246 {
247     return PSDRV_PolyPolyline( dc, pt, (LPDWORD) &count, 1 );
248 }
249
250
251 /***********************************************************************
252  *           PSDRV_PolyPolygon
253  */
254 BOOL32 PSDRV_PolyPolygon( DC *dc, const POINT32* pts, const INT32* counts,
255                           UINT32 polygons )
256 {
257     DWORD polygon, line;
258     const POINT32* pt;
259     TRACE(psdrv, "\n");
260
261     pt = pts;
262     for(polygon = 0; polygon < polygons; polygon++) {
263         PSDRV_WriteMoveTo(dc, XLPTODP(dc, pt->x), YLPTODP(dc, pt->y));
264         pt++;
265         for(line = 1; line < counts[polygon]; line++) {
266             PSDRV_WriteLineTo(dc, XLPTODP(dc, pt->x), YLPTODP(dc, pt->y));
267             pt++;
268         }
269         PSDRV_WriteClosePath(dc);
270     }
271
272     if(dc->w.polyFillMode == ALTERNATE)
273         PSDRV_Brush(dc, 1);
274     else /* WINDING */
275         PSDRV_Brush(dc, 0);
276     PSDRV_SetPen(dc);
277     PSDRV_WriteStroke(dc);
278     return TRUE;
279 }
280
281
282 /***********************************************************************
283  *           PSDRV_Polygon
284  */
285 BOOL32 PSDRV_Polygon( DC *dc, const POINT32* pt, INT32 count )
286 {
287      return PSDRV_PolyPolygon( dc, pt, &count, 1 );
288 }
289
290
291 /***********************************************************************
292  *           PSDRV_SetPixel
293  */
294 COLORREF PSDRV_SetPixel( DC *dc, INT32 x, INT32 y, COLORREF color )
295 {
296     PSDRV_PDEVICE *physDev = (PSDRV_PDEVICE *)dc->physDev;
297     PSCOLOR pscolor;
298
299     x = XLPTODP(dc, x);
300     y = YLPTODP(dc, y);
301
302     PSDRV_WriteRectangle( dc, x, y, 0, 0 );
303     PSDRV_CreateColor( physDev, &pscolor, color );
304     PSDRV_WriteSetColor( dc, &pscolor );
305     PSDRV_WriteFill( dc );
306     return color;
307 }