Removed task.h.
[wine/hacks.git] / dlls / wineps / graphics.c
blobf8222c27d8c44144b682356801d7886f049d4679
1 /*
2 * PostScript driver graphics functions
4 * Copyright 1998 Huw D M Davies
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.
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.
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
21 #include "config.h"
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"
36 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
38 /***********************************************************************
39 * PSDRV_DrawLine
41 static void PSDRV_DrawLine( PSDRV_PDEVICE *physDev )
43 if(physDev->pathdepth)
44 return;
46 if (physDev->pen.style == PS_NULL)
47 PSDRV_WriteNewPath(physDev);
48 else
49 PSDRV_WriteStroke(physDev);
52 /***********************************************************************
53 * PSDRV_LineTo
55 BOOL PSDRV_LineTo(PSDRV_PDEVICE *physDev, INT x, INT y)
57 POINT pt[2];
59 TRACE("%d %d\n", x, y);
61 GetCurrentPositionEx( physDev->hdc, pt );
62 pt[1].x = x;
63 pt[1].y = y;
64 LPtoDP( physDev->hdc, pt, 2 );
66 PSDRV_SetPen(physDev);
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);
74 return TRUE;
78 /***********************************************************************
79 * PSDRV_Rectangle
81 BOOL PSDRV_Rectangle( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom )
83 RECT rect;
85 TRACE("%d %d - %d %d\n", left, top, right, bottom);
87 rect.left = left;
88 rect.top = top;
89 rect.right = right;
90 rect.bottom = bottom;
91 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
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;
101 PSDRV_SetPen(physDev);
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;
113 /***********************************************************************
114 * PSDRV_RoundRect
116 BOOL PSDRV_RoundRect( PSDRV_PDEVICE *physDev, INT left, INT top, INT right,
117 INT bottom, INT ell_width, INT ell_height )
119 RECT rect[2];
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 );
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; }
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;
143 PSDRV_WriteSpool(physDev, "%RoundRect\n",11);
144 PSDRV_SetPen(physDev);
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 );
161 PSDRV_Brush(physDev,0);
162 PSDRV_DrawLine(physDev);
163 PSDRV_ResetClip(physDev);
164 return TRUE;
167 /***********************************************************************
168 * PSDRV_DrawArc
170 * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
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 )
176 INT x, y, h, w;
177 double start_angle, end_angle, ratio;
178 RECT rect;
179 POINT start, end;
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 );
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;
198 if(w < 0) w = -w;
199 if(h < 0) h = -h;
200 ratio = ((double)w)/h;
202 /* angle is the angle after the rectangle is transformed to a square and is
203 measured anticlockwise from the +ve x-axis */
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));
208 start_angle *= 180.0 / PI;
209 end_angle *= 180.0 / PI;
211 PSDRV_WriteSpool(physDev,"%DrawArc\n", 9);
212 PSDRV_SetPen(physDev);
214 PSDRV_SetClip(physDev);
215 if(lines == 2) /* pie */
216 PSDRV_WriteMoveTo(physDev, x, y);
217 else
218 PSDRV_WriteNewPath( physDev );
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);
225 PSDRV_DrawLine(physDev);
226 PSDRV_ResetClip(physDev);
228 return TRUE;
232 /***********************************************************************
233 * PSDRV_Arc
235 BOOL PSDRV_Arc( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
236 INT xstart, INT ystart, INT xend, INT yend )
238 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
241 /***********************************************************************
242 * PSDRV_Chord
244 BOOL PSDRV_Chord( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
245 INT xstart, INT ystart, INT xend, INT yend )
247 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
251 /***********************************************************************
252 * PSDRV_Pie
254 BOOL PSDRV_Pie( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
255 INT xstart, INT ystart, INT xend, INT yend )
257 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
261 /***********************************************************************
262 * PSDRV_Ellipse
264 BOOL PSDRV_Ellipse( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom)
266 INT x, y, w, h;
267 RECT rect;
269 TRACE("%d %d - %d %d\n", left, top, right, bottom);
271 rect.left = left;
272 rect.top = top;
273 rect.right = right;
274 rect.bottom = bottom;
275 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
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;
282 PSDRV_WriteSpool(physDev, "%Ellipse\n", 9);
283 PSDRV_SetPen(physDev);
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;
296 /***********************************************************************
297 * PSDRV_PolyPolyline
299 BOOL PSDRV_PolyPolyline( PSDRV_PDEVICE *physDev, const POINT* pts, const DWORD* counts,
300 DWORD polylines )
302 DWORD polyline, line, total;
303 POINT *dev_pts, *pt;
305 TRACE("\n");
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 );
312 pt = dev_pts;
314 PSDRV_WriteSpool(physDev, "%PolyPolyline\n",14);
315 PSDRV_SetPen(physDev);
316 PSDRV_SetClip(physDev);
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);
324 HeapFree( GetProcessHeap(), 0, dev_pts );
326 PSDRV_DrawLine(physDev);
327 PSDRV_ResetClip(physDev);
328 return TRUE;
332 /***********************************************************************
333 * PSDRV_Polyline
335 BOOL PSDRV_Polyline( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
337 return PSDRV_PolyPolyline( physDev, pt, (LPDWORD) &count, 1 );
341 /***********************************************************************
342 * PSDRV_PolyPolygon
344 BOOL PSDRV_PolyPolygon( PSDRV_PDEVICE *physDev, const POINT* pts, const INT* counts,
345 UINT polygons )
347 DWORD polygon, line, total;
348 POINT *dev_pts, *pt;
350 TRACE("\n");
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 );
357 pt = dev_pts;
359 PSDRV_WriteSpool(physDev, "%PolyPolygon\n",13);
360 PSDRV_SetPen(physDev);
361 PSDRV_SetClip(physDev);
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);
370 HeapFree( GetProcessHeap(), 0, dev_pts );
372 if(GetPolyFillMode( physDev->hdc ) == ALTERNATE)
373 PSDRV_Brush(physDev, 1);
374 else /* WINDING */
375 PSDRV_Brush(physDev, 0);
377 PSDRV_DrawLine(physDev);
378 PSDRV_ResetClip(physDev);
379 return TRUE;
383 /***********************************************************************
384 * PSDRV_Polygon
386 BOOL PSDRV_Polygon( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
388 return PSDRV_PolyPolygon( physDev, pt, &count, 1 );
392 /***********************************************************************
393 * PSDRV_SetPixel
395 COLORREF PSDRV_SetPixel( PSDRV_PDEVICE *physDev, INT x, INT y, COLORREF color )
397 PSCOLOR pscolor;
398 POINT pt;
400 pt.x = x;
401 pt.y = y;
402 LPtoDP( physDev->hdc, &pt, 1 );
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;
417 /***********************************************************************
418 * PSDRV_PaintRgn
420 BOOL PSDRV_PaintRgn( PSDRV_PDEVICE *physDev, HRGN hrgn )
423 RGNDATA *rgndata = NULL;
424 RECT *pRect;
425 DWORD size, i;
427 TRACE("hdc=%p\n", physDev->hdc);
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;
436 GetRegionData(hrgn, size, rgndata);
437 if (rgndata->rdh.nCount == 0)
438 goto end;
440 LPtoDP(physDev->hdc, (POINT*)rgndata->Buffer, rgndata->rdh.nCount * 2);
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);
447 PSDRV_Brush(physDev, 0);
448 PSDRV_ResetClip(physDev);
450 end:
451 HeapFree(GetProcessHeap(), 0, rgndata);
452 return TRUE;