kernel32/tests: Add a test to check some fields in fake dlls.
[wine.git] / dlls / wineps.drv / graphics.c
blob66108d13388a3c6bf4dae2094364eeb1fdde2d8a
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 "config.h"
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <math.h>
27 #if defined(HAVE_FLOAT_H)
28 # include <float.h>
29 #endif
30 #if !defined(PI)
31 # define PI M_PI
32 #endif
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winuser.h"
36 #include "psdrv.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
41 /***********************************************************************
42 * PSDRV_XWStoDS
44 * Performs a world-to-viewport transformation on the specified width.
46 INT PSDRV_XWStoDS( PHYSDEV dev, INT width )
48 POINT pt[2];
50 pt[0].x = 0;
51 pt[0].y = 0;
52 pt[1].x = width;
53 pt[1].y = 0;
54 LPtoDP( dev->hdc, pt, 2 );
55 return pt[1].x - pt[0].x;
58 /***********************************************************************
59 * PSDRV_DrawLine
61 static void PSDRV_DrawLine( PHYSDEV dev )
63 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
65 if(physDev->pathdepth)
66 return;
68 if (physDev->pen.style == PS_NULL)
69 PSDRV_WriteNewPath(dev);
70 else
71 PSDRV_WriteStroke(dev);
74 /***********************************************************************
75 * PSDRV_LineTo
77 BOOL PSDRV_LineTo(PHYSDEV dev, INT x, INT y)
79 POINT pt[2];
81 TRACE("%d %d\n", x, y);
83 GetCurrentPositionEx( dev->hdc, pt );
84 pt[1].x = x;
85 pt[1].y = y;
86 LPtoDP( dev->hdc, pt, 2 );
88 PSDRV_SetPen(dev);
90 PSDRV_SetClip(dev);
91 PSDRV_WriteMoveTo(dev, pt[0].x, pt[0].y );
92 PSDRV_WriteLineTo(dev, pt[1].x, pt[1].y );
93 PSDRV_DrawLine(dev);
94 PSDRV_ResetClip(dev);
96 return TRUE;
100 /***********************************************************************
101 * PSDRV_Rectangle
103 BOOL PSDRV_Rectangle( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
105 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
106 RECT rect;
108 TRACE("%d %d - %d %d\n", left, top, right, bottom);
110 SetRect(&rect, left, top, right, bottom);
111 LPtoDP( dev->hdc, (POINT *)&rect, 2 );
113 /* Windows does something truly hacky here. If we're in passthrough mode
114 and our rop is R2_NOP, then we output the string below. This is used in
115 Office 2k when inserting eps files */
116 if (physDev->job.passthrough_state == passthrough_active && GetROP2(dev->hdc) == R2_NOP)
118 char buf[256];
120 sprintf(buf, "N %d %d %d %d B\n", rect.right - rect.left, rect.bottom - rect.top, rect.left, rect.top);
121 write_spool(dev, buf, strlen(buf));
122 physDev->job.passthrough_state = passthrough_had_rect;
123 return TRUE;
126 PSDRV_SetPen(dev);
128 PSDRV_SetClip(dev);
129 PSDRV_WriteRectangle(dev, rect.left, rect.top, rect.right - rect.left,
130 rect.bottom - rect.top );
131 PSDRV_Brush(dev,0);
132 PSDRV_DrawLine(dev);
133 PSDRV_ResetClip(dev);
134 return TRUE;
138 /***********************************************************************
139 * PSDRV_RoundRect
141 BOOL PSDRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right,
142 INT bottom, INT ell_width, INT ell_height )
144 RECT rect[2];
146 SetRect(&rect[0], left, top, right, bottom);
147 SetRect(&rect[1], 0, 0, ell_width, ell_height);
148 LPtoDP( dev->hdc, (POINT *)rect, 4 );
150 left = rect[0].left;
151 top = rect[0].top;
152 right = rect[0].right;
153 bottom = rect[0].bottom;
154 if (left > right) { INT tmp = left; left = right; right = tmp; }
155 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
157 ell_width = rect[1].right - rect[1].left;
158 ell_height = rect[1].bottom - rect[1].top;
159 if (ell_width > right - left) ell_width = right - left;
160 if (ell_height > bottom - top) ell_height = bottom - top;
162 PSDRV_WriteSpool(dev, "%RoundRect\n",11);
163 PSDRV_SetPen(dev);
165 PSDRV_SetClip(dev);
166 PSDRV_WriteMoveTo( dev, left, top + ell_height/2 );
167 PSDRV_WriteArc( dev, left + ell_width/2, top + ell_height/2, ell_width,
168 ell_height, 90.0, 180.0);
169 PSDRV_WriteLineTo( dev, right - ell_width/2, top );
170 PSDRV_WriteArc( dev, right - ell_width/2, top + ell_height/2, ell_width,
171 ell_height, 0.0, 90.0);
172 PSDRV_WriteLineTo( dev, right, bottom - ell_height/2 );
173 PSDRV_WriteArc( dev, right - ell_width/2, bottom - ell_height/2, ell_width,
174 ell_height, -90.0, 0.0);
175 PSDRV_WriteLineTo( dev, right - ell_width/2, bottom);
176 PSDRV_WriteArc( dev, left + ell_width/2, bottom - ell_height/2, ell_width,
177 ell_height, 180.0, -90.0);
178 PSDRV_WriteClosePath( dev );
180 PSDRV_Brush(dev,0);
181 PSDRV_DrawLine(dev);
182 PSDRV_ResetClip(dev);
183 return TRUE;
186 /***********************************************************************
187 * PSDRV_DrawArc
189 * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
191 static BOOL PSDRV_DrawArc( PHYSDEV dev, INT left, INT top,
192 INT right, INT bottom, INT xstart, INT ystart,
193 INT xend, INT yend, int lines )
195 INT x, y, h, w;
196 double start_angle, end_angle, ratio;
197 RECT rect;
198 POINT start, end;
200 SetRect(&rect, left, top, right, bottom);
201 LPtoDP( dev->hdc, (POINT *)&rect, 2 );
202 start.x = xstart;
203 start.y = ystart;
204 end.x = xend;
205 end.y = yend;
206 LPtoDP( dev->hdc, &start, 1 );
207 LPtoDP( dev->hdc, &end, 1 );
209 x = (rect.left + rect.right) / 2;
210 y = (rect.top + rect.bottom) / 2;
211 w = rect.right - rect.left;
212 h = rect.bottom - rect.top;
214 if(w < 0) w = -w;
215 if(h < 0) h = -h;
216 ratio = ((double)w)/h;
218 /* angle is the angle after the rectangle is transformed to a square and is
219 measured anticlockwise from the +ve x-axis */
221 start_angle = atan2((double)(y - start.y) * ratio, (double)(start.x - x));
222 end_angle = atan2((double)(y - end.y) * ratio, (double)(end.x - x));
224 start_angle *= 180.0 / PI;
225 end_angle *= 180.0 / PI;
227 PSDRV_WriteSpool(dev,"%DrawArc\n", 9);
228 PSDRV_SetPen(dev);
230 PSDRV_SetClip(dev);
231 if(lines == 2) /* pie */
232 PSDRV_WriteMoveTo(dev, x, y);
233 else
234 PSDRV_WriteNewPath( dev );
236 PSDRV_WriteArc(dev, x, y, w, h, start_angle, end_angle);
237 if(lines == 1 || lines == 2) { /* chord or pie */
238 PSDRV_WriteClosePath(dev);
239 PSDRV_Brush(dev,0);
241 PSDRV_DrawLine(dev);
242 PSDRV_ResetClip(dev);
244 return TRUE;
248 /***********************************************************************
249 * PSDRV_Arc
251 BOOL PSDRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
252 INT xstart, INT ystart, INT xend, INT yend )
254 return PSDRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
257 /***********************************************************************
258 * PSDRV_Chord
260 BOOL PSDRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
261 INT xstart, INT ystart, INT xend, INT yend )
263 return PSDRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
267 /***********************************************************************
268 * PSDRV_Pie
270 BOOL PSDRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
271 INT xstart, INT ystart, INT xend, INT yend )
273 return PSDRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
277 /***********************************************************************
278 * PSDRV_Ellipse
280 BOOL PSDRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom)
282 INT x, y, w, h;
283 RECT rect;
285 TRACE("%d %d - %d %d\n", left, top, right, bottom);
287 SetRect(&rect, left, top, right, bottom);
288 LPtoDP( dev->hdc, (POINT *)&rect, 2 );
290 x = (rect.left + rect.right) / 2;
291 y = (rect.top + rect.bottom) / 2;
292 w = rect.right - rect.left;
293 h = rect.bottom - rect.top;
295 PSDRV_WriteSpool(dev, "%Ellipse\n", 9);
296 PSDRV_SetPen(dev);
298 PSDRV_SetClip(dev);
299 PSDRV_WriteNewPath(dev);
300 PSDRV_WriteArc(dev, x, y, w, h, 0.0, 360.0);
301 PSDRV_WriteClosePath(dev);
302 PSDRV_Brush(dev,0);
303 PSDRV_DrawLine(dev);
304 PSDRV_ResetClip(dev);
305 return TRUE;
309 /***********************************************************************
310 * PSDRV_PolyPolyline
312 BOOL PSDRV_PolyPolyline( PHYSDEV dev, const POINT* pts, const DWORD* counts, DWORD polylines )
314 DWORD polyline, line, total;
315 POINT *dev_pts, *pt;
317 TRACE("\n");
319 for (polyline = total = 0; polyline < polylines; polyline++) total += counts[polyline];
320 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
321 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
322 LPtoDP( dev->hdc, dev_pts, total );
324 pt = dev_pts;
326 PSDRV_WriteSpool(dev, "%PolyPolyline\n",14);
327 PSDRV_SetPen(dev);
328 PSDRV_SetClip(dev);
330 for(polyline = 0; polyline < polylines; polyline++) {
331 PSDRV_WriteMoveTo(dev, pt->x, pt->y);
332 pt++;
333 for(line = 1; line < counts[polyline]; line++, pt++)
334 PSDRV_WriteLineTo(dev, pt->x, pt->y);
336 HeapFree( GetProcessHeap(), 0, dev_pts );
338 PSDRV_DrawLine(dev);
339 PSDRV_ResetClip(dev);
340 return TRUE;
344 /***********************************************************************
345 * PSDRV_PolyPolygon
347 BOOL PSDRV_PolyPolygon( PHYSDEV dev, const POINT* pts, const INT* counts, UINT polygons )
349 DWORD polygon, total;
350 INT line;
351 POINT *dev_pts, *pt;
353 TRACE("\n");
355 for (polygon = total = 0; polygon < polygons; polygon++) total += counts[polygon];
356 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
357 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
358 LPtoDP( dev->hdc, dev_pts, total );
360 pt = dev_pts;
362 PSDRV_WriteSpool(dev, "%PolyPolygon\n",13);
363 PSDRV_SetPen(dev);
364 PSDRV_SetClip(dev);
366 for(polygon = 0; polygon < polygons; polygon++) {
367 PSDRV_WriteMoveTo(dev, pt->x, pt->y);
368 pt++;
369 for(line = 1; line < counts[polygon]; line++, pt++)
370 PSDRV_WriteLineTo(dev, pt->x, pt->y);
371 PSDRV_WriteClosePath(dev);
373 HeapFree( GetProcessHeap(), 0, dev_pts );
375 if(GetPolyFillMode( dev->hdc ) == ALTERNATE)
376 PSDRV_Brush(dev, 1);
377 else /* WINDING */
378 PSDRV_Brush(dev, 0);
380 PSDRV_DrawLine(dev);
381 PSDRV_ResetClip(dev);
382 return TRUE;
386 /***********************************************************************
387 * PSDRV_PolyBezier
389 BOOL PSDRV_PolyBezier( PHYSDEV dev, const POINT *pts, DWORD count )
391 DWORD i;
392 POINT *dev_pts;
394 TRACE("\n");
396 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*dev_pts) ))) return FALSE;
397 memcpy( dev_pts, pts, count * sizeof(*dev_pts) );
398 LPtoDP( dev->hdc, dev_pts, count );
400 PSDRV_WriteSpool(dev, "%PolyBezier\n",12);
401 PSDRV_SetPen(dev);
402 PSDRV_SetClip(dev);
403 PSDRV_WriteMoveTo(dev, dev_pts[0].x, dev_pts[0].y );
404 for (i = 1; i < count; i += 3) PSDRV_WriteCurveTo( dev, dev_pts + i );
405 PSDRV_DrawLine(dev);
406 PSDRV_ResetClip(dev);
407 HeapFree( GetProcessHeap(), 0, dev_pts );
408 return TRUE;
412 /***********************************************************************
413 * PSDRV_PolyBezierTo
415 BOOL PSDRV_PolyBezierTo( PHYSDEV dev, const POINT *pts, DWORD count )
417 DWORD i;
418 POINT *dev_pts;
420 TRACE("\n");
422 count++; /* add initial position */
423 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*dev_pts) ))) return FALSE;
424 GetCurrentPositionEx( dev->hdc, dev_pts );
425 memcpy( dev_pts + 1, pts, (count - 1) * sizeof(*dev_pts) );
426 LPtoDP( dev->hdc, dev_pts, count );
428 PSDRV_WriteSpool(dev, "%PolyBezier\n",12);
429 PSDRV_SetPen(dev);
430 PSDRV_SetClip(dev);
431 PSDRV_WriteMoveTo(dev, dev_pts[0].x, dev_pts[0].y );
432 for (i = 1; i < count; i += 3) PSDRV_WriteCurveTo( dev, dev_pts + i );
433 PSDRV_DrawLine(dev);
434 PSDRV_ResetClip(dev);
435 HeapFree( GetProcessHeap(), 0, dev_pts );
436 return TRUE;
440 /***********************************************************************
441 * PSDRV_SetPixel
443 COLORREF PSDRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
445 PSCOLOR pscolor;
446 POINT pt;
448 pt.x = x;
449 pt.y = y;
450 LPtoDP( dev->hdc, &pt, 1 );
452 PSDRV_SetClip(dev);
453 /* we bracket the setcolor in gsave/grestore so that we don't trash
454 the current pen colour */
455 PSDRV_WriteGSave(dev);
456 PSDRV_WriteRectangle( dev, pt.x, pt.y, 0, 0 );
457 PSDRV_CreateColor( dev, &pscolor, color );
458 PSDRV_WriteSetColor( dev, &pscolor );
459 PSDRV_WriteFill( dev );
460 PSDRV_WriteGRestore(dev);
461 PSDRV_ResetClip(dev);
462 return color;
465 /***********************************************************************
466 * PSDRV_PaintRgn
468 BOOL PSDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
470 RGNDATA *rgndata = NULL;
471 RECT *pRect;
472 DWORD size, i;
474 TRACE("hdc=%p\n", dev->hdc);
476 size = GetRegionData(hrgn, 0, NULL);
477 rgndata = HeapAlloc( GetProcessHeap(), 0, size );
478 if(!rgndata) {
479 ERR("Can't allocate buffer\n");
480 return FALSE;
483 GetRegionData(hrgn, size, rgndata);
484 if (rgndata->rdh.nCount == 0)
485 goto end;
487 LPtoDP(dev->hdc, (POINT*)rgndata->Buffer, rgndata->rdh.nCount * 2);
489 PSDRV_SetClip(dev);
490 for(i = 0, pRect = (RECT*)rgndata->Buffer; i < rgndata->rdh.nCount; i++, pRect++)
491 PSDRV_WriteRectangle(dev, pRect->left, pRect->top, pRect->right - pRect->left, pRect->bottom - pRect->top);
493 PSDRV_Brush(dev, 0);
494 PSDRV_WriteNewPath(dev);
495 PSDRV_ResetClip(dev);
497 end:
498 HeapFree(GetProcessHeap(), 0, rgndata);
499 return TRUE;
502 static BOOL paint_path( PHYSDEV dev, BOOL stroke, BOOL fill )
504 POINT *points;
505 BYTE *types;
506 BOOL ret = FALSE;
507 int i, size = GetPath( dev->hdc, NULL, NULL, 0 );
509 if (size == -1) return FALSE;
510 if (!size)
512 AbortPath( dev->hdc );
513 return TRUE;
515 points = HeapAlloc( GetProcessHeap(), 0, size * sizeof(*points) );
516 types = HeapAlloc( GetProcessHeap(), 0, size * sizeof(*types) );
517 if (!points || !types) goto done;
518 if (GetPath( dev->hdc, points, types, size ) == -1) goto done;
519 LPtoDP( dev->hdc, points, size );
521 if (stroke) PSDRV_SetPen(dev);
522 PSDRV_SetClip(dev);
523 for (i = 0; i < size; i++)
525 switch (types[i])
527 case PT_MOVETO:
528 PSDRV_WriteMoveTo( dev, points[i].x, points[i].y );
529 break;
530 case PT_LINETO:
531 case PT_LINETO | PT_CLOSEFIGURE:
532 PSDRV_WriteLineTo( dev, points[i].x, points[i].y );
533 if (types[i] & PT_CLOSEFIGURE) PSDRV_WriteClosePath( dev );
534 break;
535 case PT_BEZIERTO:
536 case PT_BEZIERTO | PT_CLOSEFIGURE:
537 PSDRV_WriteCurveTo( dev, points + i );
538 if (types[i] & PT_CLOSEFIGURE) PSDRV_WriteClosePath( dev );
539 i += 2;
540 break;
543 if (fill) PSDRV_Brush( dev, GetPolyFillMode(dev->hdc) == ALTERNATE );
544 if (stroke) PSDRV_DrawLine(dev);
545 else PSDRV_WriteNewPath(dev);
546 PSDRV_ResetClip(dev);
547 AbortPath( dev->hdc );
549 done:
550 HeapFree( GetProcessHeap(), 0, points );
551 HeapFree( GetProcessHeap(), 0, types );
552 return ret;
555 /***********************************************************************
556 * PSDRV_FillPath
558 BOOL PSDRV_FillPath( PHYSDEV dev )
560 return paint_path( dev, FALSE, TRUE );
563 /***********************************************************************
564 * PSDRV_StrokeAndFillPath
566 BOOL PSDRV_StrokeAndFillPath( PHYSDEV dev )
568 return paint_path( dev, TRUE, TRUE );
571 /***********************************************************************
572 * PSDRV_StrokePath
574 BOOL PSDRV_StrokePath( PHYSDEV dev )
576 return paint_path( dev, TRUE, FALSE );