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