mfplat/tests: Add a separate test function for MFGetPlaneSize().
[wine.git] / dlls / wineps.drv / graphics.c
blob27c0ba45edf6e133819cd59e212a49327d641acd
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 %d %d %d %d 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 PSDRV_WriteArc(dev, x, y, w, h, start_angle, end_angle);
230 if(lines == 1 || lines == 2) { /* chord or pie */
231 PSDRV_WriteClosePath(dev);
232 PSDRV_Brush(dev,0);
234 PSDRV_DrawLine(dev);
235 PSDRV_ResetClip(dev);
237 return TRUE;
241 /***********************************************************************
242 * PSDRV_Arc
244 BOOL CDECL PSDRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
245 INT xstart, INT ystart, INT xend, INT yend )
247 return PSDRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
250 /***********************************************************************
251 * PSDRV_Chord
253 BOOL CDECL PSDRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
254 INT xstart, INT ystart, INT xend, INT yend )
256 return PSDRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
260 /***********************************************************************
261 * PSDRV_Pie
263 BOOL CDECL PSDRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
264 INT xstart, INT ystart, INT xend, INT yend )
266 return PSDRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
270 /***********************************************************************
271 * PSDRV_Ellipse
273 BOOL CDECL PSDRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom)
275 INT x, y, w, h;
276 RECT rect;
278 TRACE("%d %d - %d %d\n", left, top, right, bottom);
280 SetRect(&rect, left, top, right, bottom);
281 LPtoDP( dev->hdc, (POINT *)&rect, 2 );
283 x = (rect.left + rect.right) / 2;
284 y = (rect.top + rect.bottom) / 2;
285 w = rect.right - rect.left;
286 h = rect.bottom - rect.top;
288 PSDRV_WriteSpool(dev, "%Ellipse\n", 9);
289 PSDRV_SetPen(dev);
291 PSDRV_SetClip(dev);
292 PSDRV_WriteNewPath(dev);
293 PSDRV_WriteArc(dev, x, y, w, h, 0.0, 360.0);
294 PSDRV_WriteClosePath(dev);
295 PSDRV_Brush(dev,0);
296 PSDRV_DrawLine(dev);
297 PSDRV_ResetClip(dev);
298 return TRUE;
302 /***********************************************************************
303 * PSDRV_PolyPolyline
305 BOOL CDECL PSDRV_PolyPolyline( PHYSDEV dev, const POINT* pts, const DWORD* counts, DWORD polylines )
307 DWORD polyline, line, total;
308 POINT *dev_pts, *pt;
310 TRACE("\n");
312 for (polyline = total = 0; polyline < polylines; polyline++) total += counts[polyline];
313 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
314 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
315 LPtoDP( dev->hdc, dev_pts, total );
317 pt = dev_pts;
319 PSDRV_WriteSpool(dev, "%PolyPolyline\n",14);
320 PSDRV_SetPen(dev);
321 PSDRV_SetClip(dev);
323 for(polyline = 0; polyline < polylines; polyline++) {
324 PSDRV_WriteMoveTo(dev, pt->x, pt->y);
325 pt++;
326 for(line = 1; line < counts[polyline]; line++, pt++)
327 PSDRV_WriteLineTo(dev, pt->x, pt->y);
329 HeapFree( GetProcessHeap(), 0, dev_pts );
331 PSDRV_DrawLine(dev);
332 PSDRV_ResetClip(dev);
333 return TRUE;
337 /***********************************************************************
338 * PSDRV_PolyPolygon
340 BOOL CDECL PSDRV_PolyPolygon( PHYSDEV dev, const POINT* pts, const INT* counts, UINT polygons )
342 DWORD polygon, total;
343 INT line;
344 POINT *dev_pts, *pt;
346 TRACE("\n");
348 for (polygon = total = 0; polygon < polygons; polygon++) total += counts[polygon];
349 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
350 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
351 LPtoDP( dev->hdc, dev_pts, total );
353 pt = dev_pts;
355 PSDRV_WriteSpool(dev, "%PolyPolygon\n",13);
356 PSDRV_SetPen(dev);
357 PSDRV_SetClip(dev);
359 for(polygon = 0; polygon < polygons; polygon++) {
360 PSDRV_WriteMoveTo(dev, pt->x, pt->y);
361 pt++;
362 for(line = 1; line < counts[polygon]; line++, pt++)
363 PSDRV_WriteLineTo(dev, pt->x, pt->y);
364 PSDRV_WriteClosePath(dev);
366 HeapFree( GetProcessHeap(), 0, dev_pts );
368 if(GetPolyFillMode( dev->hdc ) == ALTERNATE)
369 PSDRV_Brush(dev, 1);
370 else /* WINDING */
371 PSDRV_Brush(dev, 0);
373 PSDRV_DrawLine(dev);
374 PSDRV_ResetClip(dev);
375 return TRUE;
379 /***********************************************************************
380 * PSDRV_PolyBezier
382 BOOL CDECL PSDRV_PolyBezier( PHYSDEV dev, const POINT *pts, DWORD count )
384 DWORD i;
385 POINT *dev_pts;
387 TRACE("\n");
389 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*dev_pts) ))) return FALSE;
390 memcpy( dev_pts, pts, count * sizeof(*dev_pts) );
391 LPtoDP( dev->hdc, dev_pts, count );
393 PSDRV_WriteSpool(dev, "%PolyBezier\n",12);
394 PSDRV_SetPen(dev);
395 PSDRV_SetClip(dev);
396 PSDRV_WriteMoveTo(dev, dev_pts[0].x, dev_pts[0].y );
397 for (i = 1; i < count; i += 3) PSDRV_WriteCurveTo( dev, dev_pts + i );
398 PSDRV_DrawLine(dev);
399 PSDRV_ResetClip(dev);
400 HeapFree( GetProcessHeap(), 0, dev_pts );
401 return TRUE;
405 /***********************************************************************
406 * PSDRV_PolyBezierTo
408 BOOL CDECL PSDRV_PolyBezierTo( PHYSDEV dev, const POINT *pts, DWORD count )
410 DWORD i;
411 POINT *dev_pts;
413 TRACE("\n");
415 count++; /* add initial position */
416 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*dev_pts) ))) return FALSE;
417 GetCurrentPositionEx( dev->hdc, dev_pts );
418 memcpy( dev_pts + 1, pts, (count - 1) * sizeof(*dev_pts) );
419 LPtoDP( dev->hdc, dev_pts, count );
421 PSDRV_WriteSpool(dev, "%PolyBezier\n",12);
422 PSDRV_SetPen(dev);
423 PSDRV_SetClip(dev);
424 PSDRV_WriteMoveTo(dev, dev_pts[0].x, dev_pts[0].y );
425 for (i = 1; i < count; i += 3) PSDRV_WriteCurveTo( dev, dev_pts + i );
426 PSDRV_DrawLine(dev);
427 PSDRV_ResetClip(dev);
428 HeapFree( GetProcessHeap(), 0, dev_pts );
429 return TRUE;
433 /***********************************************************************
434 * PSDRV_SetPixel
436 COLORREF CDECL PSDRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
438 PSCOLOR pscolor;
439 POINT pt;
441 pt.x = x;
442 pt.y = y;
443 LPtoDP( dev->hdc, &pt, 1 );
445 PSDRV_SetClip(dev);
446 /* we bracket the setcolor in gsave/grestore so that we don't trash
447 the current pen colour */
448 PSDRV_WriteGSave(dev);
449 PSDRV_WriteRectangle( dev, pt.x, pt.y, 0, 0 );
450 PSDRV_CreateColor( dev, &pscolor, color );
451 PSDRV_WriteSetColor( dev, &pscolor );
452 PSDRV_WriteFill( dev );
453 PSDRV_WriteGRestore(dev);
454 PSDRV_ResetClip(dev);
455 return color;
458 /***********************************************************************
459 * PSDRV_PaintRgn
461 BOOL CDECL PSDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
463 RGNDATA *rgndata = NULL;
464 RECT *pRect;
465 DWORD size, i;
467 TRACE("hdc=%p\n", dev->hdc);
469 size = GetRegionData(hrgn, 0, NULL);
470 rgndata = HeapAlloc( GetProcessHeap(), 0, size );
471 if(!rgndata) {
472 ERR("Can't allocate buffer\n");
473 return FALSE;
476 GetRegionData(hrgn, size, rgndata);
477 if (rgndata->rdh.nCount == 0)
478 goto end;
480 LPtoDP(dev->hdc, (POINT*)rgndata->Buffer, rgndata->rdh.nCount * 2);
482 PSDRV_SetClip(dev);
483 for(i = 0, pRect = (RECT*)rgndata->Buffer; i < rgndata->rdh.nCount; i++, pRect++)
484 PSDRV_WriteRectangle(dev, pRect->left, pRect->top, pRect->right - pRect->left, pRect->bottom - pRect->top);
486 PSDRV_Brush(dev, 0);
487 PSDRV_WriteNewPath(dev);
488 PSDRV_ResetClip(dev);
490 end:
491 HeapFree(GetProcessHeap(), 0, rgndata);
492 return TRUE;
495 static BOOL paint_path( PHYSDEV dev, BOOL stroke, BOOL fill )
497 POINT *points;
498 BYTE *types;
499 BOOL ret = FALSE;
500 int i, size = GetPath( dev->hdc, NULL, NULL, 0 );
502 if (size == -1) return FALSE;
503 if (!size)
505 AbortPath( dev->hdc );
506 return TRUE;
508 points = HeapAlloc( GetProcessHeap(), 0, size * sizeof(*points) );
509 types = HeapAlloc( GetProcessHeap(), 0, size * sizeof(*types) );
510 if (!points || !types) goto done;
511 if (GetPath( dev->hdc, points, types, size ) == -1) goto done;
512 LPtoDP( dev->hdc, points, size );
514 if (stroke) PSDRV_SetPen(dev);
515 PSDRV_SetClip(dev);
516 for (i = 0; i < size; i++)
518 switch (types[i])
520 case PT_MOVETO:
521 PSDRV_WriteMoveTo( dev, points[i].x, points[i].y );
522 break;
523 case PT_LINETO:
524 case PT_LINETO | PT_CLOSEFIGURE:
525 PSDRV_WriteLineTo( dev, points[i].x, points[i].y );
526 if (types[i] & PT_CLOSEFIGURE) PSDRV_WriteClosePath( dev );
527 break;
528 case PT_BEZIERTO:
529 case PT_BEZIERTO | PT_CLOSEFIGURE:
530 PSDRV_WriteCurveTo( dev, points + i );
531 if (types[i] & PT_CLOSEFIGURE) PSDRV_WriteClosePath( dev );
532 i += 2;
533 break;
536 if (fill) PSDRV_Brush( dev, GetPolyFillMode(dev->hdc) == ALTERNATE );
537 if (stroke) PSDRV_DrawLine(dev);
538 else PSDRV_WriteNewPath(dev);
539 PSDRV_ResetClip(dev);
540 AbortPath( dev->hdc );
542 done:
543 HeapFree( GetProcessHeap(), 0, points );
544 HeapFree( GetProcessHeap(), 0, types );
545 return ret;
548 /***********************************************************************
549 * PSDRV_FillPath
551 BOOL CDECL PSDRV_FillPath( PHYSDEV dev )
553 return paint_path( dev, FALSE, TRUE );
556 /***********************************************************************
557 * PSDRV_StrokeAndFillPath
559 BOOL CDECL PSDRV_StrokeAndFillPath( PHYSDEV dev )
561 return paint_path( dev, TRUE, TRUE );
564 /***********************************************************************
565 * PSDRV_StrokePath
567 BOOL CDECL PSDRV_StrokePath( PHYSDEV dev )
569 return paint_path( dev, TRUE, FALSE );