crypt32/tests: Don't crash on NT4.
[wine/multimedia.git] / dlls / wineps.drv / graphics.c
blob2d98abd3d3932bc3216ae35af5d19cf3ab972173
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 <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"
35 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
37 /***********************************************************************
38 * PSDRV_XWStoDS
40 * Performs a world-to-viewport transformation on the specified width.
42 INT PSDRV_XWStoDS( PSDRV_PDEVICE *physDev, INT width )
44 POINT pt[2];
46 pt[0].x = 0;
47 pt[0].y = 0;
48 pt[1].x = width;
49 pt[1].y = 0;
50 LPtoDP( physDev->hdc, pt, 2 );
51 return pt[1].x - pt[0].x;
54 /***********************************************************************
55 * PSDRV_YWStoDS
57 * Performs a world-to-viewport transformation on the specified height.
59 INT PSDRV_YWStoDS( PSDRV_PDEVICE *physDev, INT height )
61 POINT pt[2];
63 pt[0].x = 0;
64 pt[0].y = 0;
65 pt[1].x = 0;
66 pt[1].y = height;
67 LPtoDP( physDev->hdc, pt, 2 );
68 return pt[1].y - pt[0].y;
71 /***********************************************************************
72 * PSDRV_DrawLine
74 static void PSDRV_DrawLine( PSDRV_PDEVICE *physDev )
76 if(physDev->pathdepth)
77 return;
79 if (physDev->pen.style == PS_NULL)
80 PSDRV_WriteNewPath(physDev);
81 else
82 PSDRV_WriteStroke(physDev);
85 /***********************************************************************
86 * PSDRV_LineTo
88 BOOL PSDRV_LineTo(PSDRV_PDEVICE *physDev, INT x, INT y)
90 POINT pt[2];
92 TRACE("%d %d\n", x, y);
94 GetCurrentPositionEx( physDev->hdc, pt );
95 pt[1].x = x;
96 pt[1].y = y;
97 LPtoDP( physDev->hdc, pt, 2 );
99 PSDRV_SetPen(physDev);
101 PSDRV_SetClip(physDev);
102 PSDRV_WriteMoveTo(physDev, pt[0].x, pt[0].y );
103 PSDRV_WriteLineTo(physDev, pt[1].x, pt[1].y );
104 PSDRV_DrawLine(physDev);
105 PSDRV_ResetClip(physDev);
107 return TRUE;
111 /***********************************************************************
112 * PSDRV_Rectangle
114 BOOL PSDRV_Rectangle( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom )
116 RECT rect;
118 TRACE("%d %d - %d %d\n", left, top, right, bottom);
120 rect.left = left;
121 rect.top = top;
122 rect.right = right;
123 rect.bottom = bottom;
124 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
126 /* Windows does something truly hacky here. If we're in passthrough mode
127 and our rop is R2_NOP, then we output the string below. This is used in
128 Office 2k when inserting eps files */
129 if(physDev->job.in_passthrough && !physDev->job.had_passthrough_rect && GetROP2(physDev->hdc) == R2_NOP) {
130 char buf[256];
131 sprintf(buf, "N %d %d %d %d B\n", rect.right - rect.left, rect.bottom - rect.top, rect.left, rect.top);
132 WriteSpool16(physDev->job.hJob, buf, strlen(buf));
133 physDev->job.had_passthrough_rect = TRUE;
134 return TRUE;
137 PSDRV_SetPen(physDev);
139 PSDRV_SetClip(physDev);
140 PSDRV_WriteRectangle(physDev, rect.left, rect.top, rect.right - rect.left,
141 rect.bottom - rect.top );
142 PSDRV_Brush(physDev,0);
143 PSDRV_DrawLine(physDev);
144 PSDRV_ResetClip(physDev);
145 return TRUE;
149 /***********************************************************************
150 * PSDRV_RoundRect
152 BOOL PSDRV_RoundRect( PSDRV_PDEVICE *physDev, INT left, INT top, INT right,
153 INT bottom, INT ell_width, INT ell_height )
155 RECT rect[2];
157 rect[0].left = left;
158 rect[0].top = top;
159 rect[0].right = right;
160 rect[0].bottom = bottom;
161 rect[1].left = 0;
162 rect[1].top = 0;
163 rect[1].right = ell_width;
164 rect[1].bottom = ell_height;
165 LPtoDP( physDev->hdc, (POINT *)rect, 4 );
167 left = rect[0].left;
168 top = rect[0].top;
169 right = rect[0].right;
170 bottom = rect[0].bottom;
171 if (left > right) { INT tmp = left; left = right; right = tmp; }
172 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
174 ell_width = rect[1].right - rect[1].left;
175 ell_height = rect[1].bottom - rect[1].top;
176 if (ell_width > right - left) ell_width = right - left;
177 if (ell_height > bottom - top) ell_height = bottom - top;
179 PSDRV_WriteSpool(physDev, "%RoundRect\n",11);
180 PSDRV_SetPen(physDev);
182 PSDRV_SetClip(physDev);
183 PSDRV_WriteMoveTo( physDev, left, top + ell_height/2 );
184 PSDRV_WriteArc( physDev, left + ell_width/2, top + ell_height/2, ell_width,
185 ell_height, 90.0, 180.0);
186 PSDRV_WriteLineTo( physDev, right - ell_width/2, top );
187 PSDRV_WriteArc( physDev, right - ell_width/2, top + ell_height/2, ell_width,
188 ell_height, 0.0, 90.0);
189 PSDRV_WriteLineTo( physDev, right, bottom - ell_height/2 );
190 PSDRV_WriteArc( physDev, right - ell_width/2, bottom - ell_height/2, ell_width,
191 ell_height, -90.0, 0.0);
192 PSDRV_WriteLineTo( physDev, right - ell_width/2, bottom);
193 PSDRV_WriteArc( physDev, left + ell_width/2, bottom - ell_height/2, ell_width,
194 ell_height, 180.0, -90.0);
195 PSDRV_WriteClosePath( physDev );
197 PSDRV_Brush(physDev,0);
198 PSDRV_DrawLine(physDev);
199 PSDRV_ResetClip(physDev);
200 return TRUE;
203 /***********************************************************************
204 * PSDRV_DrawArc
206 * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
208 static BOOL PSDRV_DrawArc( PSDRV_PDEVICE *physDev, INT left, INT top,
209 INT right, INT bottom, INT xstart, INT ystart,
210 INT xend, INT yend, int lines )
212 INT x, y, h, w;
213 double start_angle, end_angle, ratio;
214 RECT rect;
215 POINT start, end;
217 rect.left = left;
218 rect.top = top;
219 rect.right = right;
220 rect.bottom = bottom;
221 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
222 start.x = xstart;
223 start.y = ystart;
224 end.x = xend;
225 end.y = yend;
226 LPtoDP( physDev->hdc, &start, 1 );
227 LPtoDP( physDev->hdc, &end, 1 );
229 x = (rect.left + rect.right) / 2;
230 y = (rect.top + rect.bottom) / 2;
231 w = rect.right - rect.left;
232 h = rect.bottom - rect.top;
234 if(w < 0) w = -w;
235 if(h < 0) h = -h;
236 ratio = ((double)w)/h;
238 /* angle is the angle after the rectangle is transformed to a square and is
239 measured anticlockwise from the +ve x-axis */
241 start_angle = atan2((double)(y - start.y) * ratio, (double)(start.x - x));
242 end_angle = atan2((double)(y - end.y) * ratio, (double)(end.x - x));
244 start_angle *= 180.0 / PI;
245 end_angle *= 180.0 / PI;
247 PSDRV_WriteSpool(physDev,"%DrawArc\n", 9);
248 PSDRV_SetPen(physDev);
250 PSDRV_SetClip(physDev);
251 if(lines == 2) /* pie */
252 PSDRV_WriteMoveTo(physDev, x, y);
253 else
254 PSDRV_WriteNewPath( physDev );
256 PSDRV_WriteArc(physDev, x, y, w, h, start_angle, end_angle);
257 if(lines == 1 || lines == 2) { /* chord or pie */
258 PSDRV_WriteClosePath(physDev);
259 PSDRV_Brush(physDev,0);
261 PSDRV_DrawLine(physDev);
262 PSDRV_ResetClip(physDev);
264 return TRUE;
268 /***********************************************************************
269 * PSDRV_Arc
271 BOOL PSDRV_Arc( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
272 INT xstart, INT ystart, INT xend, INT yend )
274 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
277 /***********************************************************************
278 * PSDRV_Chord
280 BOOL PSDRV_Chord( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
281 INT xstart, INT ystart, INT xend, INT yend )
283 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
287 /***********************************************************************
288 * PSDRV_Pie
290 BOOL PSDRV_Pie( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
291 INT xstart, INT ystart, INT xend, INT yend )
293 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
297 /***********************************************************************
298 * PSDRV_Ellipse
300 BOOL PSDRV_Ellipse( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom)
302 INT x, y, w, h;
303 RECT rect;
305 TRACE("%d %d - %d %d\n", left, top, right, bottom);
307 rect.left = left;
308 rect.top = top;
309 rect.right = right;
310 rect.bottom = bottom;
311 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
313 x = (rect.left + rect.right) / 2;
314 y = (rect.top + rect.bottom) / 2;
315 w = rect.right - rect.left;
316 h = rect.bottom - rect.top;
318 PSDRV_WriteSpool(physDev, "%Ellipse\n", 9);
319 PSDRV_SetPen(physDev);
321 PSDRV_SetClip(physDev);
322 PSDRV_WriteNewPath(physDev);
323 PSDRV_WriteArc(physDev, x, y, w, h, 0.0, 360.0);
324 PSDRV_WriteClosePath(physDev);
325 PSDRV_Brush(physDev,0);
326 PSDRV_DrawLine(physDev);
327 PSDRV_ResetClip(physDev);
328 return TRUE;
332 /***********************************************************************
333 * PSDRV_PolyPolyline
335 BOOL PSDRV_PolyPolyline( PSDRV_PDEVICE *physDev, const POINT* pts, const DWORD* counts,
336 DWORD polylines )
338 DWORD polyline, line, total;
339 POINT *dev_pts, *pt;
341 TRACE("\n");
343 for (polyline = total = 0; polyline < polylines; polyline++) total += counts[polyline];
344 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
345 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
346 LPtoDP( physDev->hdc, dev_pts, total );
348 pt = dev_pts;
350 PSDRV_WriteSpool(physDev, "%PolyPolyline\n",14);
351 PSDRV_SetPen(physDev);
352 PSDRV_SetClip(physDev);
354 for(polyline = 0; polyline < polylines; polyline++) {
355 PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
356 pt++;
357 for(line = 1; line < counts[polyline]; line++, pt++)
358 PSDRV_WriteLineTo(physDev, pt->x, pt->y);
360 HeapFree( GetProcessHeap(), 0, dev_pts );
362 PSDRV_DrawLine(physDev);
363 PSDRV_ResetClip(physDev);
364 return TRUE;
368 /***********************************************************************
369 * PSDRV_Polyline
371 BOOL PSDRV_Polyline( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
373 return PSDRV_PolyPolyline( physDev, pt, (LPDWORD) &count, 1 );
377 /***********************************************************************
378 * PSDRV_PolyPolygon
380 BOOL PSDRV_PolyPolygon( PSDRV_PDEVICE *physDev, const POINT* pts, const INT* counts,
381 UINT polygons )
383 DWORD polygon, total;
384 INT line;
385 POINT *dev_pts, *pt;
387 TRACE("\n");
389 for (polygon = total = 0; polygon < polygons; polygon++) total += counts[polygon];
390 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
391 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
392 LPtoDP( physDev->hdc, dev_pts, total );
394 pt = dev_pts;
396 PSDRV_WriteSpool(physDev, "%PolyPolygon\n",13);
397 PSDRV_SetPen(physDev);
398 PSDRV_SetClip(physDev);
400 for(polygon = 0; polygon < polygons; polygon++) {
401 PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
402 pt++;
403 for(line = 1; line < counts[polygon]; line++, pt++)
404 PSDRV_WriteLineTo(physDev, pt->x, pt->y);
405 PSDRV_WriteClosePath(physDev);
407 HeapFree( GetProcessHeap(), 0, dev_pts );
409 if(GetPolyFillMode( physDev->hdc ) == ALTERNATE)
410 PSDRV_Brush(physDev, 1);
411 else /* WINDING */
412 PSDRV_Brush(physDev, 0);
414 PSDRV_DrawLine(physDev);
415 PSDRV_ResetClip(physDev);
416 return TRUE;
420 /***********************************************************************
421 * PSDRV_Polygon
423 BOOL PSDRV_Polygon( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
425 return PSDRV_PolyPolygon( physDev, pt, &count, 1 );
429 /***********************************************************************
430 * PSDRV_SetPixel
432 COLORREF PSDRV_SetPixel( PSDRV_PDEVICE *physDev, INT x, INT y, COLORREF color )
434 PSCOLOR pscolor;
435 POINT pt;
437 pt.x = x;
438 pt.y = y;
439 LPtoDP( physDev->hdc, &pt, 1 );
441 PSDRV_SetClip(physDev);
442 /* we bracket the setcolor in gsave/grestore so that we don't trash
443 the current pen colour */
444 PSDRV_WriteGSave(physDev);
445 PSDRV_WriteRectangle( physDev, pt.x, pt.y, 0, 0 );
446 PSDRV_CreateColor( physDev, &pscolor, color );
447 PSDRV_WriteSetColor( physDev, &pscolor );
448 PSDRV_WriteFill( physDev );
449 PSDRV_WriteGRestore(physDev);
450 PSDRV_ResetClip(physDev);
451 return color;
454 /***********************************************************************
455 * PSDRV_PaintRgn
457 BOOL PSDRV_PaintRgn( PSDRV_PDEVICE *physDev, HRGN hrgn )
460 RGNDATA *rgndata = NULL;
461 RECT *pRect;
462 DWORD size, i;
464 TRACE("hdc=%p\n", physDev->hdc);
466 size = GetRegionData(hrgn, 0, NULL);
467 rgndata = HeapAlloc( GetProcessHeap(), 0, size );
468 if(!rgndata) {
469 ERR("Can't allocate buffer\n");
470 return FALSE;
473 GetRegionData(hrgn, size, rgndata);
474 if (rgndata->rdh.nCount == 0)
475 goto end;
477 LPtoDP(physDev->hdc, (POINT*)rgndata->Buffer, rgndata->rdh.nCount * 2);
479 PSDRV_SetClip(physDev);
480 PSDRV_WriteNewPath(physDev);
481 for(i = 0, pRect = (RECT*)rgndata->Buffer; i < rgndata->rdh.nCount; i++, pRect++)
482 PSDRV_WriteRectangle(physDev, pRect->left, pRect->top, pRect->right - pRect->left, pRect->bottom - pRect->top);
484 PSDRV_Brush(physDev, 0);
485 PSDRV_ResetClip(physDev);
487 end:
488 HeapFree(GetProcessHeap(), 0, rgndata);
489 return TRUE;