Fixed a race condition on RPC worker thread creation, and a typo.
[wine] / dlls / wineps / graphics.c
1 /*
2  *      PostScript driver graphics functions
3  *
4  *      Copyright 1998  Huw D M Davies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <string.h>
24 #include <math.h>
25 #if defined(HAVE_FLOAT_H)
26  #include <float.h>
27 #endif
28 #if !defined(PI)
29  #define PI M_PI
30 #endif
31 #include "psdrv.h"
32 #include "wine/debug.h"
33 #include "winspool.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
36
37
38 /***********************************************************************
39  *           PSDRV_LineTo
40  */
41 BOOL PSDRV_LineTo(PSDRV_PDEVICE *physDev, INT x, INT y)
42 {
43     POINT pt[2];
44
45     TRACE("%d %d\n", x, y);
46
47     GetCurrentPositionEx( physDev->hdc, pt );
48     pt[1].x = x;
49     pt[1].y = y;
50     LPtoDP( physDev->hdc, pt, 2 );
51
52     PSDRV_SetPen(physDev);
53     PSDRV_WriteMoveTo(physDev, pt[0].x, pt[0].y );
54     PSDRV_WriteLineTo(physDev, pt[1].x, pt[1].y );
55     PSDRV_DrawLine(physDev);
56
57     return TRUE;
58 }
59
60
61 /***********************************************************************
62  *           PSDRV_Rectangle
63  */
64 BOOL PSDRV_Rectangle( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom )
65 {
66     RECT rect;
67
68     TRACE("%d %d - %d %d\n", left, top, right, bottom);
69
70     rect.left = left;
71     rect.top = top;
72     rect.right = right;
73     rect.bottom = bottom;
74     LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
75
76     PSDRV_WriteRectangle(physDev, rect.left, rect.top, rect.right - rect.left,
77                          rect.bottom - rect.top );
78     PSDRV_Brush(physDev,0);
79     PSDRV_SetPen(physDev);
80     PSDRV_DrawLine(physDev);
81     return TRUE;
82 }
83
84
85 /***********************************************************************
86  *           PSDRV_RoundRect
87  */
88 BOOL PSDRV_RoundRect( PSDRV_PDEVICE *physDev, INT left, INT top, INT right,
89                       INT bottom, INT ell_width, INT ell_height )
90 {
91     RECT rect[2];
92
93     rect[0].left   = left;
94     rect[0].top    = top;
95     rect[0].right  = right;
96     rect[0].bottom = bottom;
97     rect[1].left   = 0;
98     rect[1].top    = 0;
99     rect[1].right  = ell_width;
100     rect[1].bottom = ell_height;
101     LPtoDP( physDev->hdc, (POINT *)rect, 4 );
102
103     left   = rect[0].left;
104     top    = rect[0].top;
105     right  = rect[0].right;
106     bottom = rect[0].bottom;
107     if (left > right) { INT tmp = left; left = right; right = tmp; }
108     if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
109
110     ell_width  = rect[1].right - rect[1].left;
111     ell_height = rect[1].bottom - rect[1].top;
112     if (ell_width > right - left) ell_width = right - left;
113     if (ell_height > bottom - top) ell_height = bottom - top;
114
115     PSDRV_WriteMoveTo( physDev, left, top + ell_height/2 );
116     PSDRV_WriteArc( physDev, left + ell_width/2, top + ell_height/2, ell_width,
117                     ell_height, 90.0, 180.0);
118     PSDRV_WriteLineTo( physDev, right - ell_width/2, top );
119     PSDRV_WriteArc( physDev, right - ell_width/2, top + ell_height/2, ell_width,
120                     ell_height, 0.0, 90.0);
121     PSDRV_WriteLineTo( physDev, right, bottom - ell_height/2 );
122     PSDRV_WriteArc( physDev, right - ell_width/2, bottom - ell_height/2, ell_width,
123                     ell_height, -90.0, 0.0);
124     PSDRV_WriteLineTo( physDev, right - ell_width/2, bottom);
125     PSDRV_WriteArc( physDev, left + ell_width/2, bottom - ell_height/2, ell_width,
126                     ell_height, 180.0, -90.0);
127     PSDRV_WriteClosePath( physDev );
128
129     PSDRV_Brush(physDev,0);
130     PSDRV_SetPen(physDev);
131     PSDRV_DrawLine(physDev);
132     return TRUE;
133 }
134
135 /***********************************************************************
136  *           PSDRV_DrawArc
137  *
138  * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
139  */
140 static BOOL PSDRV_DrawArc( PSDRV_PDEVICE *physDev, INT left, INT top,
141                            INT right, INT bottom, INT xstart, INT ystart,
142                            INT xend, INT yend, int lines )
143 {
144     INT x, y, h, w;
145     double start_angle, end_angle, ratio;
146     RECT rect;
147     POINT start, end;
148
149     rect.left = left;
150     rect.top = top;
151     rect.right = right;
152     rect.bottom = bottom;
153     LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
154     start.x = xstart;
155     start.y = ystart;
156     end.x = xend;
157     end.y = yend;
158     LPtoDP( physDev->hdc, &start, 1 );
159     LPtoDP( physDev->hdc, &end, 1 );
160
161     x = (rect.left + rect.right) / 2;
162     y = (rect.top + rect.bottom) / 2;
163     w = rect.right - rect.left;
164     h = rect.bottom - rect.top;
165
166     if(w < 0) w = -w;
167     if(h < 0) h = -h;
168     ratio = ((double)w)/h;
169
170     /* angle is the angle after the rectangle is transformed to a square and is
171        measured anticlockwise from the +ve x-axis */
172
173     start_angle = atan2((double)(y - start.y) * ratio, (double)(start.x - x));
174     end_angle = atan2((double)(y - end.y) * ratio, (double)(end.x - x));
175
176     start_angle *= 180.0 / PI;
177     end_angle *= 180.0 / PI;
178
179     if(lines == 2) /* pie */
180         PSDRV_WriteMoveTo(physDev, x, y);
181     else
182         PSDRV_WriteNewPath( physDev );
183
184     PSDRV_WriteArc(physDev, x, y, w, h, start_angle, end_angle);
185     if(lines == 1 || lines == 2) { /* chord or pie */
186         PSDRV_WriteClosePath(physDev);
187         PSDRV_Brush(physDev,0);
188     }
189     PSDRV_SetPen(physDev);
190     PSDRV_DrawLine(physDev);
191     return TRUE;
192 }
193
194
195 /***********************************************************************
196  *           PSDRV_Arc
197  */
198 BOOL PSDRV_Arc( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
199                 INT xstart, INT ystart, INT xend, INT yend )
200 {
201     return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
202 }
203
204 /***********************************************************************
205  *           PSDRV_Chord
206  */
207 BOOL PSDRV_Chord( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
208                   INT xstart, INT ystart, INT xend, INT yend )
209 {
210     return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
211 }
212
213
214 /***********************************************************************
215  *           PSDRV_Pie
216  */
217 BOOL PSDRV_Pie( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
218                 INT xstart, INT ystart, INT xend, INT yend )
219 {
220     return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
221 }
222
223
224 /***********************************************************************
225  *           PSDRV_Ellipse
226  */
227 BOOL PSDRV_Ellipse( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom)
228 {
229     INT x, y, w, h;
230     RECT rect;
231
232     TRACE("%d %d - %d %d\n", left, top, right, bottom);
233
234     rect.left   = left;
235     rect.top    = top;
236     rect.right  = right;
237     rect.bottom = bottom;
238     LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
239
240     x = (rect.left + rect.right) / 2;
241     y = (rect.top + rect.bottom) / 2;
242     w = rect.right - rect.left;
243     h = rect.bottom - rect.top;
244
245     PSDRV_WriteNewPath(physDev);
246     PSDRV_WriteArc(physDev, x, y, w, h, 0.0, 360.0);
247     PSDRV_WriteClosePath(physDev);
248     PSDRV_Brush(physDev,0);
249     PSDRV_SetPen(physDev);
250     PSDRV_DrawLine(physDev);
251     return TRUE;
252 }
253
254
255 /***********************************************************************
256  *           PSDRV_PolyPolyline
257  */
258 BOOL PSDRV_PolyPolyline( PSDRV_PDEVICE *physDev, const POINT* pts, const DWORD* counts,
259                          DWORD polylines )
260 {
261     DWORD polyline, line, total;
262     POINT *dev_pts, *pt;
263
264     TRACE("\n");
265
266     for (polyline = total = 0; polyline < polylines; polyline++) total += counts[polyline];
267     if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
268     memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
269     LPtoDP( physDev->hdc, dev_pts, total );
270
271     pt = dev_pts;
272     for(polyline = 0; polyline < polylines; polyline++) {
273         PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
274         pt++;
275         for(line = 1; line < counts[polyline]; line++, pt++)
276             PSDRV_WriteLineTo(physDev, pt->x, pt->y);
277     }
278     HeapFree( GetProcessHeap(), 0, dev_pts );
279     PSDRV_SetPen(physDev);
280     PSDRV_DrawLine(physDev);
281     return TRUE;
282 }
283
284
285 /***********************************************************************
286  *           PSDRV_Polyline
287  */
288 BOOL PSDRV_Polyline( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
289 {
290     return PSDRV_PolyPolyline( physDev, pt, (LPDWORD) &count, 1 );
291 }
292
293
294 /***********************************************************************
295  *           PSDRV_PolyPolygon
296  */
297 BOOL PSDRV_PolyPolygon( PSDRV_PDEVICE *physDev, const POINT* pts, const INT* counts,
298                         UINT polygons )
299 {
300     DWORD polygon, line, total;
301     POINT *dev_pts, *pt;
302
303     TRACE("\n");
304
305     for (polygon = total = 0; polygon < polygons; polygon++) total += counts[polygon];
306     if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
307     memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
308     LPtoDP( physDev->hdc, dev_pts, total );
309
310     pt = dev_pts;
311     for(polygon = 0; polygon < polygons; polygon++) {
312         PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
313         pt++;
314         for(line = 1; line < counts[polygon]; line++, pt++)
315             PSDRV_WriteLineTo(physDev, pt->x, pt->y);
316         PSDRV_WriteClosePath(physDev);
317     }
318     HeapFree( GetProcessHeap(), 0, dev_pts );
319
320     if(GetPolyFillMode( physDev->hdc ) == ALTERNATE)
321         PSDRV_Brush(physDev, 1);
322     else /* WINDING */
323         PSDRV_Brush(physDev, 0);
324     PSDRV_SetPen(physDev);
325     PSDRV_DrawLine(physDev);
326     return TRUE;
327 }
328
329
330 /***********************************************************************
331  *           PSDRV_Polygon
332  */
333 BOOL PSDRV_Polygon( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
334 {
335      return PSDRV_PolyPolygon( physDev, pt, &count, 1 );
336 }
337
338
339 /***********************************************************************
340  *           PSDRV_SetPixel
341  */
342 COLORREF PSDRV_SetPixel( PSDRV_PDEVICE *physDev, INT x, INT y, COLORREF color )
343 {
344     PSCOLOR pscolor;
345     POINT pt;
346
347     pt.x = x;
348     pt.y = y;
349     LPtoDP( physDev->hdc, &pt, 1 );
350
351     PSDRV_WriteRectangle( physDev, pt.x, pt.y, 0, 0 );
352     PSDRV_CreateColor( physDev, &pscolor, color );
353     PSDRV_WriteSetColor( physDev, &pscolor );
354     PSDRV_WriteFill( physDev );
355     return color;
356 }
357
358
359 /***********************************************************************
360  *           PSDRV_DrawLine
361  */
362 VOID PSDRV_DrawLine( PSDRV_PDEVICE *physDev )
363 {
364     if(physDev->pathdepth)
365         return;
366
367     if (physDev->pen.style == PS_NULL)
368         PSDRV_WriteNewPath(physDev);
369     else
370         PSDRV_WriteStroke(physDev);
371 }