Moved ts_xlib.c into x11drv and removed libwine_tsx11.
[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
148     rect.left = left;
149     rect.top = top;
150     rect.right = right;
151     rect.bottom = bottom;
152     LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
153
154     x = (rect.left + rect.right) / 2;
155     y = (rect.top + rect.bottom) / 2;
156     w = rect.right - rect.left;
157     h = rect.bottom - rect.top;
158
159     if(w < 0) w = -w;
160     if(h < 0) h = -h;
161     ratio = ((double)w)/h;
162
163     /* angle is the angle after the rectangle is transformed to a square and is
164        measured anticlockwise from the +ve x-axis */
165
166     start_angle = atan2((double)(y - ystart) * ratio, (double)(xstart - x));
167     end_angle = atan2((double)(y - yend) * ratio, (double)(xend - x));
168
169     start_angle *= 180.0 / PI;
170     end_angle *= 180.0 / PI;
171
172     if(lines == 2) /* pie */
173         PSDRV_WriteMoveTo(physDev, x, y);
174     else
175         PSDRV_WriteNewPath( physDev );
176
177     PSDRV_WriteArc(physDev, x, y, w, h, start_angle, end_angle);
178     if(lines == 1 || lines == 2) { /* chord or pie */
179         PSDRV_WriteClosePath(physDev);
180         PSDRV_Brush(physDev,0);
181     }
182     PSDRV_SetPen(physDev);
183     PSDRV_DrawLine(physDev);
184     return TRUE;
185 }
186
187
188 /***********************************************************************
189  *           PSDRV_Arc
190  */
191 BOOL PSDRV_Arc( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
192                 INT xstart, INT ystart, INT xend, INT yend )
193 {
194     return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
195 }
196
197 /***********************************************************************
198  *           PSDRV_Chord
199  */
200 BOOL PSDRV_Chord( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
201                   INT xstart, INT ystart, INT xend, INT yend )
202 {
203     return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
204 }
205
206
207 /***********************************************************************
208  *           PSDRV_Pie
209  */
210 BOOL PSDRV_Pie( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
211                 INT xstart, INT ystart, INT xend, INT yend )
212 {
213     return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
214 }
215
216
217 /***********************************************************************
218  *           PSDRV_Ellipse
219  */
220 BOOL PSDRV_Ellipse( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom)
221 {
222     INT x, y, w, h;
223     RECT rect;
224
225     TRACE("%d %d - %d %d\n", left, top, right, bottom);
226
227     rect.left   = left;
228     rect.top    = top;
229     rect.right  = right;
230     rect.bottom = bottom;
231     LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
232
233     x = (rect.left + rect.right) / 2;
234     y = (rect.top + rect.bottom) / 2;
235     w = rect.right - rect.left;
236     h = rect.bottom - rect.top;
237
238     PSDRV_WriteNewPath(physDev);
239     PSDRV_WriteArc(physDev, x, y, w, h, 0.0, 360.0);
240     PSDRV_WriteClosePath(physDev);
241     PSDRV_Brush(physDev,0);
242     PSDRV_SetPen(physDev);
243     PSDRV_DrawLine(physDev);
244     return TRUE;
245 }
246
247
248 /***********************************************************************
249  *           PSDRV_PolyPolyline
250  */
251 BOOL PSDRV_PolyPolyline( PSDRV_PDEVICE *physDev, const POINT* pts, const DWORD* counts,
252                          DWORD polylines )
253 {
254     DWORD polyline, line, total;
255     POINT *dev_pts, *pt;
256
257     TRACE("\n");
258
259     for (polyline = total = 0; polyline < polylines; polyline++) total += counts[polyline];
260     if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
261     memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
262     LPtoDP( physDev->hdc, dev_pts, total );
263
264     pt = dev_pts;
265     for(polyline = 0; polyline < polylines; polyline++) {
266         PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
267         pt++;
268         for(line = 1; line < counts[polyline]; line++, pt++)
269             PSDRV_WriteLineTo(physDev, pt->x, pt->y);
270     }
271     HeapFree( GetProcessHeap(), 0, dev_pts );
272     PSDRV_SetPen(physDev);
273     PSDRV_DrawLine(physDev);
274     return TRUE;
275 }
276
277
278 /***********************************************************************
279  *           PSDRV_Polyline
280  */
281 BOOL PSDRV_Polyline( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
282 {
283     return PSDRV_PolyPolyline( physDev, pt, (LPDWORD) &count, 1 );
284 }
285
286
287 /***********************************************************************
288  *           PSDRV_PolyPolygon
289  */
290 BOOL PSDRV_PolyPolygon( PSDRV_PDEVICE *physDev, const POINT* pts, const INT* counts,
291                         UINT polygons )
292 {
293     DWORD polygon, line, total;
294     POINT *dev_pts, *pt;
295
296     TRACE("\n");
297
298     for (polygon = total = 0; polygon < polygons; polygon++) total += counts[polygon];
299     if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
300     memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
301     LPtoDP( physDev->hdc, dev_pts, total );
302
303     pt = dev_pts;
304     for(polygon = 0; polygon < polygons; polygon++) {
305         PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
306         pt++;
307         for(line = 1; line < counts[polygon]; line++, pt++)
308             PSDRV_WriteLineTo(physDev, pt->x, pt->y);
309         PSDRV_WriteClosePath(physDev);
310     }
311     HeapFree( GetProcessHeap(), 0, dev_pts );
312
313     if(GetPolyFillMode( physDev->hdc ) == ALTERNATE)
314         PSDRV_Brush(physDev, 1);
315     else /* WINDING */
316         PSDRV_Brush(physDev, 0);
317     PSDRV_SetPen(physDev);
318     PSDRV_DrawLine(physDev);
319     return TRUE;
320 }
321
322
323 /***********************************************************************
324  *           PSDRV_Polygon
325  */
326 BOOL PSDRV_Polygon( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
327 {
328      return PSDRV_PolyPolygon( physDev, pt, &count, 1 );
329 }
330
331
332 /***********************************************************************
333  *           PSDRV_SetPixel
334  */
335 COLORREF PSDRV_SetPixel( PSDRV_PDEVICE *physDev, INT x, INT y, COLORREF color )
336 {
337     PSCOLOR pscolor;
338     POINT pt;
339
340     pt.x = x;
341     pt.y = y;
342     LPtoDP( physDev->hdc, &pt, 1 );
343
344     PSDRV_WriteRectangle( physDev, pt.x, pt.y, 0, 0 );
345     PSDRV_CreateColor( physDev, &pscolor, color );
346     PSDRV_WriteSetColor( physDev, &pscolor );
347     PSDRV_WriteFill( physDev );
348     return color;
349 }
350
351
352 /***********************************************************************
353  *           PSDRV_DrawLine
354  */
355 VOID PSDRV_DrawLine( PSDRV_PDEVICE *physDev )
356 {
357     if (physDev->pen.style == PS_NULL)
358         PSDRV_WriteNewPath(physDev);
359     else
360         PSDRV_WriteStroke(physDev);
361 }