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