INT21_GetFreeDiskSpace(): The drive parameter is found in the DL
[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 <stdio.h>
24 #include <string.h>
25 #include <math.h>
26 #if defined(HAVE_FLOAT_H)
27  #include <float.h>
28 #endif
29 #if !defined(PI)
30  #define PI M_PI
31 #endif
32 #include "psdrv.h"
33 #include "wine/debug.h"
34 #include "winspool.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
37
38 /***********************************************************************
39  *           PSDRV_DrawLine
40  */
41 static void PSDRV_DrawLine( PSDRV_PDEVICE *physDev )
42 {
43     if(physDev->pathdepth)
44         return;
45
46     if (physDev->pen.style == PS_NULL)
47         PSDRV_WriteNewPath(physDev);
48     else
49         PSDRV_WriteStroke(physDev);
50 }
51
52 /***********************************************************************
53  *           PSDRV_LineTo
54  */
55 BOOL PSDRV_LineTo(PSDRV_PDEVICE *physDev, INT x, INT y)
56 {
57     POINT pt[2];
58
59     TRACE("%d %d\n", x, y);
60
61     GetCurrentPositionEx( physDev->hdc, pt );
62     pt[1].x = x;
63     pt[1].y = y;
64     LPtoDP( physDev->hdc, pt, 2 );
65
66     PSDRV_SetPen(physDev);
67
68     PSDRV_SetClip(physDev);
69     PSDRV_WriteMoveTo(physDev, pt[0].x, pt[0].y );
70     PSDRV_WriteLineTo(physDev, pt[1].x, pt[1].y );
71     PSDRV_DrawLine(physDev);
72     PSDRV_ResetClip(physDev);
73
74     return TRUE;
75 }
76
77
78 /***********************************************************************
79  *           PSDRV_Rectangle
80  */
81 BOOL PSDRV_Rectangle( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom )
82 {
83     RECT rect;
84
85     TRACE("%d %d - %d %d\n", left, top, right, bottom);
86
87     rect.left = left;
88     rect.top = top;
89     rect.right = right;
90     rect.bottom = bottom;
91     LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
92
93     /* HACK to get inserted eps files printing from Office 2k */
94     if(GetROP2(physDev->hdc) == R2_NOP) {
95       char buf[256];
96       sprintf(buf, "%ld %ld %ld %ld B\n", rect.right - rect.left, rect.bottom - rect.top, rect.left, rect.top);
97       PSDRV_WriteSpool(physDev, buf, strlen(buf));
98       return TRUE;
99     }
100
101     PSDRV_SetPen(physDev);
102
103     PSDRV_SetClip(physDev);
104     PSDRV_WriteRectangle(physDev, rect.left, rect.top, rect.right - rect.left,
105                          rect.bottom - rect.top );
106     PSDRV_Brush(physDev,0);
107     PSDRV_DrawLine(physDev);
108     PSDRV_ResetClip(physDev);
109     return TRUE;
110 }
111
112
113 /***********************************************************************
114  *           PSDRV_RoundRect
115  */
116 BOOL PSDRV_RoundRect( PSDRV_PDEVICE *physDev, INT left, INT top, INT right,
117                       INT bottom, INT ell_width, INT ell_height )
118 {
119     RECT rect[2];
120
121     rect[0].left   = left;
122     rect[0].top    = top;
123     rect[0].right  = right;
124     rect[0].bottom = bottom;
125     rect[1].left   = 0;
126     rect[1].top    = 0;
127     rect[1].right  = ell_width;
128     rect[1].bottom = ell_height;
129     LPtoDP( physDev->hdc, (POINT *)rect, 4 );
130
131     left   = rect[0].left;
132     top    = rect[0].top;
133     right  = rect[0].right;
134     bottom = rect[0].bottom;
135     if (left > right) { INT tmp = left; left = right; right = tmp; }
136     if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
137
138     ell_width  = rect[1].right - rect[1].left;
139     ell_height = rect[1].bottom - rect[1].top;
140     if (ell_width > right - left) ell_width = right - left;
141     if (ell_height > bottom - top) ell_height = bottom - top;
142
143     PSDRV_WriteSpool(physDev, "%RoundRect\n",11);
144     PSDRV_SetPen(physDev);
145
146     PSDRV_SetClip(physDev);
147     PSDRV_WriteMoveTo( physDev, left, top + ell_height/2 );
148     PSDRV_WriteArc( physDev, left + ell_width/2, top + ell_height/2, ell_width,
149                     ell_height, 90.0, 180.0);
150     PSDRV_WriteLineTo( physDev, right - ell_width/2, top );
151     PSDRV_WriteArc( physDev, right - ell_width/2, top + ell_height/2, ell_width,
152                     ell_height, 0.0, 90.0);
153     PSDRV_WriteLineTo( physDev, right, bottom - ell_height/2 );
154     PSDRV_WriteArc( physDev, right - ell_width/2, bottom - ell_height/2, ell_width,
155                     ell_height, -90.0, 0.0);
156     PSDRV_WriteLineTo( physDev, right - ell_width/2, bottom);
157     PSDRV_WriteArc( physDev, left + ell_width/2, bottom - ell_height/2, ell_width,
158                     ell_height, 180.0, -90.0);
159     PSDRV_WriteClosePath( physDev );
160
161     PSDRV_Brush(physDev,0);
162     PSDRV_DrawLine(physDev);
163     PSDRV_ResetClip(physDev);
164     return TRUE;
165 }
166
167 /***********************************************************************
168  *           PSDRV_DrawArc
169  *
170  * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
171  */
172 static BOOL PSDRV_DrawArc( PSDRV_PDEVICE *physDev, INT left, INT top,
173                            INT right, INT bottom, INT xstart, INT ystart,
174                            INT xend, INT yend, int lines )
175 {
176     INT x, y, h, w;
177     double start_angle, end_angle, ratio;
178     RECT rect;
179     POINT start, end;
180
181     rect.left = left;
182     rect.top = top;
183     rect.right = right;
184     rect.bottom = bottom;
185     LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
186     start.x = xstart;
187     start.y = ystart;
188     end.x = xend;
189     end.y = yend;
190     LPtoDP( physDev->hdc, &start, 1 );
191     LPtoDP( physDev->hdc, &end, 1 );
192
193     x = (rect.left + rect.right) / 2;
194     y = (rect.top + rect.bottom) / 2;
195     w = rect.right - rect.left;
196     h = rect.bottom - rect.top;
197
198     if(w < 0) w = -w;
199     if(h < 0) h = -h;
200     ratio = ((double)w)/h;
201
202     /* angle is the angle after the rectangle is transformed to a square and is
203        measured anticlockwise from the +ve x-axis */
204
205     start_angle = atan2((double)(y - start.y) * ratio, (double)(start.x - x));
206     end_angle = atan2((double)(y - end.y) * ratio, (double)(end.x - x));
207
208     start_angle *= 180.0 / PI;
209     end_angle *= 180.0 / PI;
210
211     PSDRV_WriteSpool(physDev,"%DrawArc\n", 9);
212     PSDRV_SetPen(physDev);
213
214     PSDRV_SetClip(physDev);
215     if(lines == 2) /* pie */
216         PSDRV_WriteMoveTo(physDev, x, y);
217     else
218         PSDRV_WriteNewPath( physDev );
219
220     PSDRV_WriteArc(physDev, x, y, w, h, start_angle, end_angle);
221     if(lines == 1 || lines == 2) { /* chord or pie */
222         PSDRV_WriteClosePath(physDev);
223         PSDRV_Brush(physDev,0);
224     }
225     PSDRV_DrawLine(physDev);
226     PSDRV_ResetClip(physDev);
227
228     return TRUE;
229 }
230
231
232 /***********************************************************************
233  *           PSDRV_Arc
234  */
235 BOOL PSDRV_Arc( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
236                 INT xstart, INT ystart, INT xend, INT yend )
237 {
238     return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
239 }
240
241 /***********************************************************************
242  *           PSDRV_Chord
243  */
244 BOOL PSDRV_Chord( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
245                   INT xstart, INT ystart, INT xend, INT yend )
246 {
247     return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
248 }
249
250
251 /***********************************************************************
252  *           PSDRV_Pie
253  */
254 BOOL PSDRV_Pie( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
255                 INT xstart, INT ystart, INT xend, INT yend )
256 {
257     return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
258 }
259
260
261 /***********************************************************************
262  *           PSDRV_Ellipse
263  */
264 BOOL PSDRV_Ellipse( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom)
265 {
266     INT x, y, w, h;
267     RECT rect;
268
269     TRACE("%d %d - %d %d\n", left, top, right, bottom);
270
271     rect.left   = left;
272     rect.top    = top;
273     rect.right  = right;
274     rect.bottom = bottom;
275     LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
276
277     x = (rect.left + rect.right) / 2;
278     y = (rect.top + rect.bottom) / 2;
279     w = rect.right - rect.left;
280     h = rect.bottom - rect.top;
281
282     PSDRV_WriteSpool(physDev, "%Ellipse\n", 9);
283     PSDRV_SetPen(physDev);
284
285     PSDRV_SetClip(physDev);
286     PSDRV_WriteNewPath(physDev);
287     PSDRV_WriteArc(physDev, x, y, w, h, 0.0, 360.0);
288     PSDRV_WriteClosePath(physDev);
289     PSDRV_Brush(physDev,0);
290     PSDRV_DrawLine(physDev);
291     PSDRV_ResetClip(physDev);
292     return TRUE;
293 }
294
295
296 /***********************************************************************
297  *           PSDRV_PolyPolyline
298  */
299 BOOL PSDRV_PolyPolyline( PSDRV_PDEVICE *physDev, const POINT* pts, const DWORD* counts,
300                          DWORD polylines )
301 {
302     DWORD polyline, line, total;
303     POINT *dev_pts, *pt;
304
305     TRACE("\n");
306
307     for (polyline = total = 0; polyline < polylines; polyline++) total += counts[polyline];
308     if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
309     memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
310     LPtoDP( physDev->hdc, dev_pts, total );
311
312     pt = dev_pts;
313
314     PSDRV_WriteSpool(physDev, "%PolyPolyline\n",14);
315     PSDRV_SetPen(physDev);
316     PSDRV_SetClip(physDev);
317
318     for(polyline = 0; polyline < polylines; polyline++) {
319         PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
320         pt++;
321         for(line = 1; line < counts[polyline]; line++, pt++)
322             PSDRV_WriteLineTo(physDev, pt->x, pt->y);
323     }
324     HeapFree( GetProcessHeap(), 0, dev_pts );
325
326     PSDRV_DrawLine(physDev);
327     PSDRV_ResetClip(physDev);
328     return TRUE;
329 }
330
331
332 /***********************************************************************
333  *           PSDRV_Polyline
334  */
335 BOOL PSDRV_Polyline( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
336 {
337     return PSDRV_PolyPolyline( physDev, pt, (LPDWORD) &count, 1 );
338 }
339
340
341 /***********************************************************************
342  *           PSDRV_PolyPolygon
343  */
344 BOOL PSDRV_PolyPolygon( PSDRV_PDEVICE *physDev, const POINT* pts, const INT* counts,
345                         UINT polygons )
346 {
347     DWORD polygon, line, total;
348     POINT *dev_pts, *pt;
349
350     TRACE("\n");
351
352     for (polygon = total = 0; polygon < polygons; polygon++) total += counts[polygon];
353     if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
354     memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
355     LPtoDP( physDev->hdc, dev_pts, total );
356
357     pt = dev_pts;
358
359     PSDRV_WriteSpool(physDev, "%PolyPolygon\n",13);
360     PSDRV_SetPen(physDev);
361     PSDRV_SetClip(physDev);
362
363     for(polygon = 0; polygon < polygons; polygon++) {
364         PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
365         pt++;
366         for(line = 1; line < counts[polygon]; line++, pt++)
367             PSDRV_WriteLineTo(physDev, pt->x, pt->y);
368         PSDRV_WriteClosePath(physDev);
369     }
370     HeapFree( GetProcessHeap(), 0, dev_pts );
371
372     if(GetPolyFillMode( physDev->hdc ) == ALTERNATE)
373         PSDRV_Brush(physDev, 1);
374     else /* WINDING */
375         PSDRV_Brush(physDev, 0);
376
377     PSDRV_DrawLine(physDev);
378     PSDRV_ResetClip(physDev);
379     return TRUE;
380 }
381
382
383 /***********************************************************************
384  *           PSDRV_Polygon
385  */
386 BOOL PSDRV_Polygon( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
387 {
388      return PSDRV_PolyPolygon( physDev, pt, &count, 1 );
389 }
390
391
392 /***********************************************************************
393  *           PSDRV_SetPixel
394  */
395 COLORREF PSDRV_SetPixel( PSDRV_PDEVICE *physDev, INT x, INT y, COLORREF color )
396 {
397     PSCOLOR pscolor;
398     POINT pt;
399
400     pt.x = x;
401     pt.y = y;
402     LPtoDP( physDev->hdc, &pt, 1 );
403
404     PSDRV_SetClip(physDev);
405     /* we bracket the setcolor in gsave/grestore so that we don't trash
406        the current pen colour */
407     PSDRV_WriteGSave(physDev);
408     PSDRV_WriteRectangle( physDev, pt.x, pt.y, 0, 0 );
409     PSDRV_CreateColor( physDev, &pscolor, color );
410     PSDRV_WriteSetColor( physDev, &pscolor );
411     PSDRV_WriteFill( physDev );
412     PSDRV_WriteGRestore(physDev);
413     PSDRV_ResetClip(physDev);
414     return color;
415 }
416
417 /***********************************************************************
418  *           PSDRV_PaintRgn
419  */
420 BOOL PSDRV_PaintRgn( PSDRV_PDEVICE *physDev, HRGN hrgn )
421 {
422     
423     RGNDATA *rgndata = NULL;
424     RECT *pRect;
425     DWORD size, i;
426
427     TRACE("hdc=%p\n", physDev->hdc);
428
429     size = GetRegionData(hrgn, 0, NULL);
430     rgndata = HeapAlloc( GetProcessHeap(), 0, size );
431     if(!rgndata) {
432         ERR("Can't allocate buffer\n");
433         return FALSE;
434     }
435     
436     GetRegionData(hrgn, size, rgndata);
437     if (rgndata->rdh.nCount == 0)
438         goto end;
439
440     LPtoDP(physDev->hdc, (POINT*)rgndata->Buffer, rgndata->rdh.nCount * 2);
441
442     PSDRV_SetClip(physDev);
443     PSDRV_WriteNewPath(physDev);
444     for(i = 0, pRect = (RECT*)rgndata->Buffer; i < rgndata->rdh.nCount; i++, pRect++)
445         PSDRV_WriteRectangle(physDev, pRect->left, pRect->top, pRect->right - pRect->left, pRect->bottom - pRect->top);
446
447     PSDRV_Brush(physDev, 0);
448     PSDRV_ResetClip(physDev);
449
450  end:
451     HeapFree(GetProcessHeap(), 0, rgndata);
452     return TRUE;
453 }