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