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