wineps: Fix return value in path drawing routines.
[wine.git] / dlls / wineps.drv / graphics.c
blob469a1587464f0449b08945c706d619e25acee8c1
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <math.h>
25 #include <float.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "psdrv.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
34 /***********************************************************************
35 * PSDRV_XWStoDS
37 * Performs a world-to-viewport transformation on the specified width.
39 INT PSDRV_XWStoDS( PHYSDEV dev, INT width )
41 POINT pt[2];
43 pt[0].x = 0;
44 pt[0].y = 0;
45 pt[1].x = width;
46 pt[1].y = 0;
47 LPtoDP( dev->hdc, pt, 2 );
48 return pt[1].x - pt[0].x;
51 /***********************************************************************
52 * PSDRV_DrawLine
54 static void PSDRV_DrawLine( PHYSDEV dev )
56 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
58 if(physDev->pathdepth)
59 return;
61 if (physDev->pen.style == PS_NULL)
62 PSDRV_WriteNewPath(dev);
63 else
64 PSDRV_WriteStroke(dev);
67 /***********************************************************************
68 * PSDRV_LineTo
70 BOOL CDECL PSDRV_LineTo(PHYSDEV dev, INT x, INT y)
72 POINT pt[2];
74 TRACE("%d %d\n", x, y);
76 GetCurrentPositionEx( dev->hdc, pt );
77 pt[1].x = x;
78 pt[1].y = y;
79 LPtoDP( dev->hdc, pt, 2 );
81 PSDRV_SetPen(dev);
83 PSDRV_SetClip(dev);
84 PSDRV_WriteMoveTo(dev, pt[0].x, pt[0].y );
85 PSDRV_WriteLineTo(dev, pt[1].x, pt[1].y );
86 PSDRV_DrawLine(dev);
87 PSDRV_ResetClip(dev);
89 return TRUE;
93 /***********************************************************************
94 * PSDRV_Rectangle
96 BOOL CDECL PSDRV_Rectangle( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
98 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
99 RECT rect;
101 TRACE("%d %d - %d %d\n", left, top, right, bottom);
103 SetRect(&rect, left, top, right, bottom);
104 LPtoDP( dev->hdc, (POINT *)&rect, 2 );
106 /* Windows does something truly hacky here. If we're in passthrough mode
107 and our rop is R2_NOP, then we output the string below. This is used in
108 Office 2k when inserting eps files */
109 if (physDev->job.passthrough_state == passthrough_active && GetROP2(dev->hdc) == R2_NOP)
111 char buf[256];
113 sprintf(buf, "N %ld %ld %ld %ld B\n", rect.right - rect.left, rect.bottom - rect.top, rect.left, rect.top);
114 write_spool(dev, buf, strlen(buf));
115 physDev->job.passthrough_state = passthrough_had_rect;
116 return TRUE;
119 PSDRV_SetPen(dev);
121 PSDRV_SetClip(dev);
122 PSDRV_WriteRectangle(dev, rect.left, rect.top, rect.right - rect.left,
123 rect.bottom - rect.top );
124 PSDRV_Brush(dev,0);
125 PSDRV_DrawLine(dev);
126 PSDRV_ResetClip(dev);
127 return TRUE;
131 /***********************************************************************
132 * PSDRV_RoundRect
134 BOOL CDECL PSDRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right,
135 INT bottom, INT ell_width, INT ell_height )
137 RECT rect[2];
139 SetRect(&rect[0], left, top, right, bottom);
140 SetRect(&rect[1], 0, 0, ell_width, ell_height);
141 LPtoDP( dev->hdc, (POINT *)rect, 4 );
143 left = rect[0].left;
144 top = rect[0].top;
145 right = rect[0].right;
146 bottom = rect[0].bottom;
147 if (left > right) { INT tmp = left; left = right; right = tmp; }
148 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
150 ell_width = rect[1].right - rect[1].left;
151 ell_height = rect[1].bottom - rect[1].top;
152 if (ell_width > right - left) ell_width = right - left;
153 if (ell_height > bottom - top) ell_height = bottom - top;
155 PSDRV_WriteSpool(dev, "%RoundRect\n",11);
156 PSDRV_SetPen(dev);
158 PSDRV_SetClip(dev);
159 PSDRV_WriteMoveTo( dev, left, top + ell_height/2 );
160 PSDRV_WriteArc( dev, left + ell_width/2, top + ell_height/2, ell_width,
161 ell_height, 90.0, 180.0);
162 PSDRV_WriteLineTo( dev, right - ell_width/2, top );
163 PSDRV_WriteArc( dev, right - ell_width/2, top + ell_height/2, ell_width,
164 ell_height, 0.0, 90.0);
165 PSDRV_WriteLineTo( dev, right, bottom - ell_height/2 );
166 PSDRV_WriteArc( dev, right - ell_width/2, bottom - ell_height/2, ell_width,
167 ell_height, -90.0, 0.0);
168 PSDRV_WriteLineTo( dev, right - ell_width/2, bottom);
169 PSDRV_WriteArc( dev, left + ell_width/2, bottom - ell_height/2, ell_width,
170 ell_height, 180.0, -90.0);
171 PSDRV_WriteClosePath( dev );
173 PSDRV_Brush(dev,0);
174 PSDRV_DrawLine(dev);
175 PSDRV_ResetClip(dev);
176 return TRUE;
179 /***********************************************************************
180 * PSDRV_DrawArc
182 * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
184 static BOOL PSDRV_DrawArc( PHYSDEV dev, INT left, INT top,
185 INT right, INT bottom, INT xstart, INT ystart,
186 INT xend, INT yend, int lines )
188 INT x, y, h, w;
189 double start_angle, end_angle, ratio;
190 RECT rect;
191 POINT start, end;
193 SetRect(&rect, left, top, right, bottom);
194 LPtoDP( dev->hdc, (POINT *)&rect, 2 );
195 start.x = xstart;
196 start.y = ystart;
197 end.x = xend;
198 end.y = yend;
199 LPtoDP( dev->hdc, &start, 1 );
200 LPtoDP( dev->hdc, &end, 1 );
202 x = (rect.left + rect.right) / 2;
203 y = (rect.top + rect.bottom) / 2;
204 w = rect.right - rect.left;
205 h = rect.bottom - rect.top;
207 if(w < 0) w = -w;
208 if(h < 0) h = -h;
209 ratio = ((double)w)/h;
211 /* angle is the angle after the rectangle is transformed to a square and is
212 measured anticlockwise from the +ve x-axis */
214 start_angle = atan2((double)(y - start.y) * ratio, (double)(start.x - x));
215 end_angle = atan2((double)(y - end.y) * ratio, (double)(end.x - x));
217 start_angle *= 180.0 / M_PI;
218 end_angle *= 180.0 / M_PI;
220 PSDRV_WriteSpool(dev,"%DrawArc\n", 9);
221 PSDRV_SetPen(dev);
223 PSDRV_SetClip(dev);
224 if(lines == 2) /* pie */
225 PSDRV_WriteMoveTo(dev, x, y);
226 else
227 PSDRV_WriteNewPath( dev );
229 if(GetArcDirection(dev->hdc) == AD_COUNTERCLOCKWISE)
230 PSDRV_WriteArc(dev, x, y, w, h, start_angle, end_angle);
231 else
232 PSDRV_WriteArc(dev, x, y, w, h, end_angle, start_angle);
233 if(lines == 1 || lines == 2) { /* chord or pie */
234 PSDRV_WriteClosePath(dev);
235 PSDRV_Brush(dev,0);
237 PSDRV_DrawLine(dev);
238 PSDRV_ResetClip(dev);
240 return TRUE;
244 /***********************************************************************
245 * PSDRV_Arc
247 BOOL CDECL PSDRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
248 INT xstart, INT ystart, INT xend, INT yend )
250 return PSDRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
253 /***********************************************************************
254 * PSDRV_Chord
256 BOOL CDECL PSDRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
257 INT xstart, INT ystart, INT xend, INT yend )
259 return PSDRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
263 /***********************************************************************
264 * PSDRV_Pie
266 BOOL CDECL PSDRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
267 INT xstart, INT ystart, INT xend, INT yend )
269 return PSDRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
273 /***********************************************************************
274 * PSDRV_Ellipse
276 BOOL CDECL PSDRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom)
278 INT x, y, w, h;
279 RECT rect;
281 TRACE("%d %d - %d %d\n", left, top, right, bottom);
283 SetRect(&rect, left, top, right, bottom);
284 LPtoDP( dev->hdc, (POINT *)&rect, 2 );
286 x = (rect.left + rect.right) / 2;
287 y = (rect.top + rect.bottom) / 2;
288 w = rect.right - rect.left;
289 h = rect.bottom - rect.top;
291 PSDRV_WriteSpool(dev, "%Ellipse\n", 9);
292 PSDRV_SetPen(dev);
294 PSDRV_SetClip(dev);
295 PSDRV_WriteNewPath(dev);
296 PSDRV_WriteArc(dev, x, y, w, h, 0.0, 360.0);
297 PSDRV_WriteClosePath(dev);
298 PSDRV_Brush(dev,0);
299 PSDRV_DrawLine(dev);
300 PSDRV_ResetClip(dev);
301 return TRUE;
305 /***********************************************************************
306 * PSDRV_PolyPolyline
308 BOOL CDECL PSDRV_PolyPolyline( PHYSDEV dev, const POINT* pts, const DWORD* counts, DWORD polylines )
310 DWORD polyline, line, total;
311 POINT *dev_pts, *pt;
313 TRACE("\n");
315 for (polyline = total = 0; polyline < polylines; polyline++) total += counts[polyline];
316 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
317 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
318 LPtoDP( dev->hdc, dev_pts, total );
320 pt = dev_pts;
322 PSDRV_WriteSpool(dev, "%PolyPolyline\n",14);
323 PSDRV_SetPen(dev);
324 PSDRV_SetClip(dev);
326 for(polyline = 0; polyline < polylines; polyline++) {
327 PSDRV_WriteMoveTo(dev, pt->x, pt->y);
328 pt++;
329 for(line = 1; line < counts[polyline]; line++, pt++)
330 PSDRV_WriteLineTo(dev, pt->x, pt->y);
332 HeapFree( GetProcessHeap(), 0, dev_pts );
334 PSDRV_DrawLine(dev);
335 PSDRV_ResetClip(dev);
336 return TRUE;
340 /***********************************************************************
341 * PSDRV_PolyPolygon
343 BOOL CDECL PSDRV_PolyPolygon( PHYSDEV dev, const POINT* pts, const INT* counts, UINT polygons )
345 DWORD polygon, total;
346 INT line;
347 POINT *dev_pts, *pt;
349 TRACE("\n");
351 for (polygon = total = 0; polygon < polygons; polygon++) total += counts[polygon];
352 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
353 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
354 LPtoDP( dev->hdc, dev_pts, total );
356 pt = dev_pts;
358 PSDRV_WriteSpool(dev, "%PolyPolygon\n",13);
359 PSDRV_SetPen(dev);
360 PSDRV_SetClip(dev);
362 for(polygon = 0; polygon < polygons; polygon++) {
363 PSDRV_WriteMoveTo(dev, pt->x, pt->y);
364 pt++;
365 for(line = 1; line < counts[polygon]; line++, pt++)
366 PSDRV_WriteLineTo(dev, pt->x, pt->y);
367 PSDRV_WriteClosePath(dev);
369 HeapFree( GetProcessHeap(), 0, dev_pts );
371 if(GetPolyFillMode( dev->hdc ) == ALTERNATE)
372 PSDRV_Brush(dev, 1);
373 else /* WINDING */
374 PSDRV_Brush(dev, 0);
376 PSDRV_DrawLine(dev);
377 PSDRV_ResetClip(dev);
378 return TRUE;
381 static BOOL poly_bezier( PHYSDEV dev, const POINT *pt0, const POINT *pts, DWORD count)
383 POINT dev_pts[3];
384 DWORD i;
386 TRACE( "\n" );
388 dev_pts[0] = *pt0;
389 LPtoDP( dev->hdc, dev_pts, 1 );
391 PSDRV_WriteSpool( dev, "%PolyBezier\n", 12 );
392 PSDRV_SetPen( dev );
393 PSDRV_SetClip( dev );
394 PSDRV_WriteMoveTo( dev, dev_pts[0].x, dev_pts[0].y );
395 for (i = 0; i < count; i += 3)
397 memcpy( dev_pts, pts, sizeof(dev_pts) );
398 LPtoDP( dev->hdc, dev_pts, 3 );
399 PSDRV_WriteCurveTo( dev, dev_pts );
401 PSDRV_DrawLine(dev);
402 PSDRV_ResetClip(dev);
403 return TRUE;
406 /***********************************************************************
407 * PSDRV_PolyBezier
409 BOOL CDECL PSDRV_PolyBezier( PHYSDEV dev, const POINT *pts, DWORD count )
411 return poly_bezier( dev, pts, pts + 1, count - 1 );
415 /***********************************************************************
416 * PSDRV_PolyBezierTo
418 BOOL CDECL PSDRV_PolyBezierTo( PHYSDEV dev, const POINT *pts, DWORD count )
420 POINT pt0;
422 GetCurrentPositionEx( dev->hdc, &pt0 );
423 return poly_bezier( dev, &pt0, pts, count );
427 /***********************************************************************
428 * PSDRV_SetPixel
430 COLORREF CDECL PSDRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
432 PSCOLOR pscolor;
433 POINT pt;
435 pt.x = x;
436 pt.y = y;
437 LPtoDP( dev->hdc, &pt, 1 );
439 PSDRV_SetClip(dev);
440 /* we bracket the setcolor in gsave/grestore so that we don't trash
441 the current pen colour */
442 PSDRV_WriteGSave(dev);
443 PSDRV_WriteRectangle( dev, pt.x, pt.y, 1, 1 );
444 PSDRV_CreateColor( dev, &pscolor, color );
445 PSDRV_WriteSetColor( dev, &pscolor );
446 PSDRV_WriteFill( dev );
447 PSDRV_WriteGRestore(dev);
448 PSDRV_ResetClip(dev);
449 return color;
452 /***********************************************************************
453 * PSDRV_PaintRgn
455 BOOL CDECL PSDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
457 RGNDATA *rgndata = NULL;
458 RECT *pRect;
459 DWORD size, i;
461 TRACE("hdc=%p\n", dev->hdc);
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;
470 GetRegionData(hrgn, size, rgndata);
471 if (rgndata->rdh.nCount == 0)
472 goto end;
474 LPtoDP(dev->hdc, (POINT*)rgndata->Buffer, rgndata->rdh.nCount * 2);
476 PSDRV_SetClip(dev);
477 for(i = 0, pRect = (RECT*)rgndata->Buffer; i < rgndata->rdh.nCount; i++, pRect++)
478 PSDRV_WriteRectangle(dev, pRect->left, pRect->top, pRect->right - pRect->left, pRect->bottom - pRect->top);
480 PSDRV_Brush(dev, 0);
481 PSDRV_WriteNewPath(dev);
482 PSDRV_ResetClip(dev);
484 end:
485 HeapFree(GetProcessHeap(), 0, rgndata);
486 return TRUE;
489 static BOOL paint_path( PHYSDEV dev, BOOL stroke, BOOL fill )
491 POINT *points;
492 BYTE *types;
493 int i, size = GetPath( dev->hdc, NULL, NULL, 0 );
495 if (size == -1) return FALSE;
496 if (!size)
498 AbortPath( dev->hdc );
499 return TRUE;
501 points = HeapAlloc( GetProcessHeap(), 0, size * sizeof(*points) );
502 types = HeapAlloc( GetProcessHeap(), 0, size * sizeof(*types) );
503 if (!points || !types) goto done;
504 if (GetPath( dev->hdc, points, types, size ) == -1) goto done;
505 LPtoDP( dev->hdc, points, size );
507 if (stroke) PSDRV_SetPen(dev);
508 PSDRV_SetClip(dev);
509 for (i = 0; i < size; i++)
511 switch (types[i])
513 case PT_MOVETO:
514 PSDRV_WriteMoveTo( dev, points[i].x, points[i].y );
515 break;
516 case PT_LINETO:
517 case PT_LINETO | PT_CLOSEFIGURE:
518 PSDRV_WriteLineTo( dev, points[i].x, points[i].y );
519 if (types[i] & PT_CLOSEFIGURE) PSDRV_WriteClosePath( dev );
520 break;
521 case PT_BEZIERTO:
522 case PT_BEZIERTO | PT_CLOSEFIGURE:
523 PSDRV_WriteCurveTo( dev, points + i );
524 if (types[i] & PT_CLOSEFIGURE) PSDRV_WriteClosePath( dev );
525 i += 2;
526 break;
529 if (fill) PSDRV_Brush( dev, GetPolyFillMode(dev->hdc) == ALTERNATE );
530 if (stroke) PSDRV_DrawLine(dev);
531 else PSDRV_WriteNewPath(dev);
532 PSDRV_ResetClip(dev);
533 AbortPath( dev->hdc );
535 done:
536 HeapFree( GetProcessHeap(), 0, points );
537 HeapFree( GetProcessHeap(), 0, types );
538 return TRUE;
541 /***********************************************************************
542 * PSDRV_FillPath
544 BOOL CDECL PSDRV_FillPath( PHYSDEV dev )
546 return paint_path( dev, FALSE, TRUE );
549 /***********************************************************************
550 * PSDRV_StrokeAndFillPath
552 BOOL CDECL PSDRV_StrokeAndFillPath( PHYSDEV dev )
554 return paint_path( dev, TRUE, TRUE );
557 /***********************************************************************
558 * PSDRV_StrokePath
560 BOOL CDECL PSDRV_StrokePath( PHYSDEV dev )
562 return paint_path( dev, TRUE, FALSE );