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