Release 20030408.
[wine/gsoc-2012-control.git] / dlls / wineps / graphics.c
blobf9f39a592244113eb6253761c77d337ed1902a89
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 <string.h>
24 #include <math.h>
25 #if defined(HAVE_FLOAT_H)
26 #include <float.h>
27 #endif
28 #if !defined(PI)
29 #define PI M_PI
30 #endif
31 #include "psdrv.h"
32 #include "wine/debug.h"
33 #include "winspool.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
38 /***********************************************************************
39 * PSDRV_LineTo
41 BOOL PSDRV_LineTo(PSDRV_PDEVICE *physDev, INT x, INT y)
43 POINT pt[2];
45 TRACE("%d %d\n", x, y);
47 GetCurrentPositionEx( physDev->hdc, pt );
48 pt[1].x = x;
49 pt[1].y = y;
50 LPtoDP( physDev->hdc, pt, 2 );
52 PSDRV_SetPen(physDev);
53 PSDRV_WriteMoveTo(physDev, pt[0].x, pt[0].y );
54 PSDRV_WriteLineTo(physDev, pt[1].x, pt[1].y );
55 PSDRV_DrawLine(physDev);
57 return TRUE;
61 /***********************************************************************
62 * PSDRV_Rectangle
64 BOOL PSDRV_Rectangle( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom )
66 RECT rect;
68 TRACE("%d %d - %d %d\n", left, top, right, bottom);
70 rect.left = left;
71 rect.top = top;
72 rect.right = right;
73 rect.bottom = bottom;
74 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
76 PSDRV_WriteRectangle(physDev, rect.left, rect.top, rect.right - rect.left,
77 rect.bottom - rect.top );
78 PSDRV_Brush(physDev,0);
79 PSDRV_SetPen(physDev);
80 PSDRV_DrawLine(physDev);
81 return TRUE;
85 /***********************************************************************
86 * PSDRV_RoundRect
88 BOOL PSDRV_RoundRect( PSDRV_PDEVICE *physDev, INT left, INT top, INT right,
89 INT bottom, INT ell_width, INT ell_height )
91 RECT rect[2];
93 rect[0].left = left;
94 rect[0].top = top;
95 rect[0].right = right;
96 rect[0].bottom = bottom;
97 rect[1].left = 0;
98 rect[1].top = 0;
99 rect[1].right = ell_width;
100 rect[1].bottom = ell_height;
101 LPtoDP( physDev->hdc, (POINT *)rect, 4 );
103 left = rect[0].left;
104 top = rect[0].top;
105 right = rect[0].right;
106 bottom = rect[0].bottom;
107 if (left > right) { INT tmp = left; left = right; right = tmp; }
108 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
110 ell_width = rect[1].right - rect[1].left;
111 ell_height = rect[1].bottom - rect[1].top;
112 if (ell_width > right - left) ell_width = right - left;
113 if (ell_height > bottom - top) ell_height = bottom - top;
115 PSDRV_WriteMoveTo( physDev, left, top + ell_height/2 );
116 PSDRV_WriteArc( physDev, left + ell_width/2, top + ell_height/2, ell_width,
117 ell_height, 90.0, 180.0);
118 PSDRV_WriteLineTo( physDev, right - ell_width/2, top );
119 PSDRV_WriteArc( physDev, right - ell_width/2, top + ell_height/2, ell_width,
120 ell_height, 0.0, 90.0);
121 PSDRV_WriteLineTo( physDev, right, bottom - ell_height/2 );
122 PSDRV_WriteArc( physDev, right - ell_width/2, bottom - ell_height/2, ell_width,
123 ell_height, -90.0, 0.0);
124 PSDRV_WriteLineTo( physDev, right - ell_width/2, bottom);
125 PSDRV_WriteArc( physDev, left + ell_width/2, bottom - ell_height/2, ell_width,
126 ell_height, 180.0, -90.0);
127 PSDRV_WriteClosePath( physDev );
129 PSDRV_Brush(physDev,0);
130 PSDRV_SetPen(physDev);
131 PSDRV_DrawLine(physDev);
132 return TRUE;
135 /***********************************************************************
136 * PSDRV_DrawArc
138 * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
140 static BOOL PSDRV_DrawArc( PSDRV_PDEVICE *physDev, INT left, INT top,
141 INT right, INT bottom, INT xstart, INT ystart,
142 INT xend, INT yend, int lines )
144 INT x, y, h, w;
145 double start_angle, end_angle, ratio;
146 RECT rect;
147 POINT start, end;
149 rect.left = left;
150 rect.top = top;
151 rect.right = right;
152 rect.bottom = bottom;
153 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
154 start.x = xstart;
155 start.y = ystart;
156 end.x = xend;
157 end.y = yend;
158 LPtoDP( physDev->hdc, &start, 1 );
159 LPtoDP( physDev->hdc, &end, 1 );
161 x = (rect.left + rect.right) / 2;
162 y = (rect.top + rect.bottom) / 2;
163 w = rect.right - rect.left;
164 h = rect.bottom - rect.top;
166 if(w < 0) w = -w;
167 if(h < 0) h = -h;
168 ratio = ((double)w)/h;
170 /* angle is the angle after the rectangle is transformed to a square and is
171 measured anticlockwise from the +ve x-axis */
173 start_angle = atan2((double)(y - start.y) * ratio, (double)(start.x - x));
174 end_angle = atan2((double)(y - end.y) * ratio, (double)(end.x - x));
176 start_angle *= 180.0 / PI;
177 end_angle *= 180.0 / PI;
179 if(lines == 2) /* pie */
180 PSDRV_WriteMoveTo(physDev, x, y);
181 else
182 PSDRV_WriteNewPath( physDev );
184 PSDRV_WriteArc(physDev, x, y, w, h, start_angle, end_angle);
185 if(lines == 1 || lines == 2) { /* chord or pie */
186 PSDRV_WriteClosePath(physDev);
187 PSDRV_Brush(physDev,0);
189 PSDRV_SetPen(physDev);
190 PSDRV_DrawLine(physDev);
191 return TRUE;
195 /***********************************************************************
196 * PSDRV_Arc
198 BOOL PSDRV_Arc( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
199 INT xstart, INT ystart, INT xend, INT yend )
201 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
204 /***********************************************************************
205 * PSDRV_Chord
207 BOOL PSDRV_Chord( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
208 INT xstart, INT ystart, INT xend, INT yend )
210 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
214 /***********************************************************************
215 * PSDRV_Pie
217 BOOL PSDRV_Pie( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
218 INT xstart, INT ystart, INT xend, INT yend )
220 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
224 /***********************************************************************
225 * PSDRV_Ellipse
227 BOOL PSDRV_Ellipse( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom)
229 INT x, y, w, h;
230 RECT rect;
232 TRACE("%d %d - %d %d\n", left, top, right, bottom);
234 rect.left = left;
235 rect.top = top;
236 rect.right = right;
237 rect.bottom = bottom;
238 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
240 x = (rect.left + rect.right) / 2;
241 y = (rect.top + rect.bottom) / 2;
242 w = rect.right - rect.left;
243 h = rect.bottom - rect.top;
245 PSDRV_WriteNewPath(physDev);
246 PSDRV_WriteArc(physDev, x, y, w, h, 0.0, 360.0);
247 PSDRV_WriteClosePath(physDev);
248 PSDRV_Brush(physDev,0);
249 PSDRV_SetPen(physDev);
250 PSDRV_DrawLine(physDev);
251 return TRUE;
255 /***********************************************************************
256 * PSDRV_PolyPolyline
258 BOOL PSDRV_PolyPolyline( PSDRV_PDEVICE *physDev, const POINT* pts, const DWORD* counts,
259 DWORD polylines )
261 DWORD polyline, line, total;
262 POINT *dev_pts, *pt;
264 TRACE("\n");
266 for (polyline = total = 0; polyline < polylines; polyline++) total += counts[polyline];
267 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
268 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
269 LPtoDP( physDev->hdc, dev_pts, total );
271 pt = dev_pts;
272 for(polyline = 0; polyline < polylines; polyline++) {
273 PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
274 pt++;
275 for(line = 1; line < counts[polyline]; line++, pt++)
276 PSDRV_WriteLineTo(physDev, pt->x, pt->y);
278 HeapFree( GetProcessHeap(), 0, dev_pts );
279 PSDRV_SetPen(physDev);
280 PSDRV_DrawLine(physDev);
281 return TRUE;
285 /***********************************************************************
286 * PSDRV_Polyline
288 BOOL PSDRV_Polyline( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
290 return PSDRV_PolyPolyline( physDev, pt, (LPDWORD) &count, 1 );
294 /***********************************************************************
295 * PSDRV_PolyPolygon
297 BOOL PSDRV_PolyPolygon( PSDRV_PDEVICE *physDev, const POINT* pts, const INT* counts,
298 UINT polygons )
300 DWORD polygon, line, total;
301 POINT *dev_pts, *pt;
303 TRACE("\n");
305 for (polygon = total = 0; polygon < polygons; polygon++) total += counts[polygon];
306 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
307 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
308 LPtoDP( physDev->hdc, dev_pts, total );
310 pt = dev_pts;
311 for(polygon = 0; polygon < polygons; polygon++) {
312 PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
313 pt++;
314 for(line = 1; line < counts[polygon]; line++, pt++)
315 PSDRV_WriteLineTo(physDev, pt->x, pt->y);
316 PSDRV_WriteClosePath(physDev);
318 HeapFree( GetProcessHeap(), 0, dev_pts );
320 if(GetPolyFillMode( physDev->hdc ) == ALTERNATE)
321 PSDRV_Brush(physDev, 1);
322 else /* WINDING */
323 PSDRV_Brush(physDev, 0);
324 PSDRV_SetPen(physDev);
325 PSDRV_DrawLine(physDev);
326 return TRUE;
330 /***********************************************************************
331 * PSDRV_Polygon
333 BOOL PSDRV_Polygon( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
335 return PSDRV_PolyPolygon( physDev, pt, &count, 1 );
339 /***********************************************************************
340 * PSDRV_SetPixel
342 COLORREF PSDRV_SetPixel( PSDRV_PDEVICE *physDev, INT x, INT y, COLORREF color )
344 PSCOLOR pscolor;
345 POINT pt;
347 pt.x = x;
348 pt.y = y;
349 LPtoDP( physDev->hdc, &pt, 1 );
351 PSDRV_WriteRectangle( physDev, pt.x, pt.y, 0, 0 );
352 PSDRV_CreateColor( physDev, &pscolor, color );
353 PSDRV_WriteSetColor( physDev, &pscolor );
354 PSDRV_WriteFill( physDev );
355 return color;
359 /***********************************************************************
360 * PSDRV_DrawLine
362 VOID PSDRV_DrawLine( PSDRV_PDEVICE *physDev )
364 if(physDev->pathdepth)
365 return;
367 if (physDev->pen.style == PS_NULL)
368 PSDRV_WriteNewPath(physDev);
369 else
370 PSDRV_WriteStroke(physDev);