server: Allow specifying the security descriptor for a new thread.
[wine.git] / dlls / wineps.drv / graphics.c
blob4887bd0b427b9d722ed028f4777edeaa83f76dfb
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.in_passthrough && !physDev->job.had_passthrough_rect && GetROP2(dev->hdc) == R2_NOP) {
117 char buf[256];
118 sprintf(buf, "N %d %d %d %d B\n", rect.right - rect.left, rect.bottom - rect.top, rect.left, rect.top);
119 write_spool(dev, buf, strlen(buf));
120 physDev->job.had_passthrough_rect = TRUE;
121 return TRUE;
124 PSDRV_SetPen(dev);
126 PSDRV_SetClip(dev);
127 PSDRV_WriteRectangle(dev, rect.left, rect.top, rect.right - rect.left,
128 rect.bottom - rect.top );
129 PSDRV_Brush(dev,0);
130 PSDRV_DrawLine(dev);
131 PSDRV_ResetClip(dev);
132 return TRUE;
136 /***********************************************************************
137 * PSDRV_RoundRect
139 BOOL PSDRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right,
140 INT bottom, INT ell_width, INT ell_height )
142 RECT rect[2];
144 SetRect(&rect[0], left, top, right, bottom);
145 SetRect(&rect[1], 0, 0, ell_width, ell_height);
146 LPtoDP( dev->hdc, (POINT *)rect, 4 );
148 left = rect[0].left;
149 top = rect[0].top;
150 right = rect[0].right;
151 bottom = rect[0].bottom;
152 if (left > right) { INT tmp = left; left = right; right = tmp; }
153 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
155 ell_width = rect[1].right - rect[1].left;
156 ell_height = rect[1].bottom - rect[1].top;
157 if (ell_width > right - left) ell_width = right - left;
158 if (ell_height > bottom - top) ell_height = bottom - top;
160 PSDRV_WriteSpool(dev, "%RoundRect\n",11);
161 PSDRV_SetPen(dev);
163 PSDRV_SetClip(dev);
164 PSDRV_WriteMoveTo( dev, left, top + ell_height/2 );
165 PSDRV_WriteArc( dev, left + ell_width/2, top + ell_height/2, ell_width,
166 ell_height, 90.0, 180.0);
167 PSDRV_WriteLineTo( dev, right - ell_width/2, top );
168 PSDRV_WriteArc( dev, right - ell_width/2, top + ell_height/2, ell_width,
169 ell_height, 0.0, 90.0);
170 PSDRV_WriteLineTo( dev, right, bottom - ell_height/2 );
171 PSDRV_WriteArc( dev, right - ell_width/2, bottom - ell_height/2, ell_width,
172 ell_height, -90.0, 0.0);
173 PSDRV_WriteLineTo( dev, right - ell_width/2, bottom);
174 PSDRV_WriteArc( dev, left + ell_width/2, bottom - ell_height/2, ell_width,
175 ell_height, 180.0, -90.0);
176 PSDRV_WriteClosePath( dev );
178 PSDRV_Brush(dev,0);
179 PSDRV_DrawLine(dev);
180 PSDRV_ResetClip(dev);
181 return TRUE;
184 /***********************************************************************
185 * PSDRV_DrawArc
187 * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
189 static BOOL PSDRV_DrawArc( PHYSDEV dev, INT left, INT top,
190 INT right, INT bottom, INT xstart, INT ystart,
191 INT xend, INT yend, int lines )
193 INT x, y, h, w;
194 double start_angle, end_angle, ratio;
195 RECT rect;
196 POINT start, end;
198 SetRect(&rect, left, top, right, bottom);
199 LPtoDP( dev->hdc, (POINT *)&rect, 2 );
200 start.x = xstart;
201 start.y = ystart;
202 end.x = xend;
203 end.y = yend;
204 LPtoDP( dev->hdc, &start, 1 );
205 LPtoDP( dev->hdc, &end, 1 );
207 x = (rect.left + rect.right) / 2;
208 y = (rect.top + rect.bottom) / 2;
209 w = rect.right - rect.left;
210 h = rect.bottom - rect.top;
212 if(w < 0) w = -w;
213 if(h < 0) h = -h;
214 ratio = ((double)w)/h;
216 /* angle is the angle after the rectangle is transformed to a square and is
217 measured anticlockwise from the +ve x-axis */
219 start_angle = atan2((double)(y - start.y) * ratio, (double)(start.x - x));
220 end_angle = atan2((double)(y - end.y) * ratio, (double)(end.x - x));
222 start_angle *= 180.0 / PI;
223 end_angle *= 180.0 / PI;
225 PSDRV_WriteSpool(dev,"%DrawArc\n", 9);
226 PSDRV_SetPen(dev);
228 PSDRV_SetClip(dev);
229 if(lines == 2) /* pie */
230 PSDRV_WriteMoveTo(dev, x, y);
231 else
232 PSDRV_WriteNewPath( dev );
234 PSDRV_WriteArc(dev, x, y, w, h, start_angle, end_angle);
235 if(lines == 1 || lines == 2) { /* chord or pie */
236 PSDRV_WriteClosePath(dev);
237 PSDRV_Brush(dev,0);
239 PSDRV_DrawLine(dev);
240 PSDRV_ResetClip(dev);
242 return TRUE;
246 /***********************************************************************
247 * PSDRV_Arc
249 BOOL PSDRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
250 INT xstart, INT ystart, INT xend, INT yend )
252 return PSDRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
255 /***********************************************************************
256 * PSDRV_Chord
258 BOOL PSDRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
259 INT xstart, INT ystart, INT xend, INT yend )
261 return PSDRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
265 /***********************************************************************
266 * PSDRV_Pie
268 BOOL PSDRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
269 INT xstart, INT ystart, INT xend, INT yend )
271 return PSDRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
275 /***********************************************************************
276 * PSDRV_Ellipse
278 BOOL PSDRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom)
280 INT x, y, w, h;
281 RECT rect;
283 TRACE("%d %d - %d %d\n", left, top, right, bottom);
285 SetRect(&rect, left, top, right, bottom);
286 LPtoDP( dev->hdc, (POINT *)&rect, 2 );
288 x = (rect.left + rect.right) / 2;
289 y = (rect.top + rect.bottom) / 2;
290 w = rect.right - rect.left;
291 h = rect.bottom - rect.top;
293 PSDRV_WriteSpool(dev, "%Ellipse\n", 9);
294 PSDRV_SetPen(dev);
296 PSDRV_SetClip(dev);
297 PSDRV_WriteNewPath(dev);
298 PSDRV_WriteArc(dev, x, y, w, h, 0.0, 360.0);
299 PSDRV_WriteClosePath(dev);
300 PSDRV_Brush(dev,0);
301 PSDRV_DrawLine(dev);
302 PSDRV_ResetClip(dev);
303 return TRUE;
307 /***********************************************************************
308 * PSDRV_PolyPolyline
310 BOOL PSDRV_PolyPolyline( PHYSDEV dev, const POINT* pts, const DWORD* counts, DWORD polylines )
312 DWORD polyline, line, total;
313 POINT *dev_pts, *pt;
315 TRACE("\n");
317 for (polyline = total = 0; polyline < polylines; polyline++) total += counts[polyline];
318 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
319 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
320 LPtoDP( dev->hdc, dev_pts, total );
322 pt = dev_pts;
324 PSDRV_WriteSpool(dev, "%PolyPolyline\n",14);
325 PSDRV_SetPen(dev);
326 PSDRV_SetClip(dev);
328 for(polyline = 0; polyline < polylines; polyline++) {
329 PSDRV_WriteMoveTo(dev, pt->x, pt->y);
330 pt++;
331 for(line = 1; line < counts[polyline]; line++, pt++)
332 PSDRV_WriteLineTo(dev, pt->x, pt->y);
334 HeapFree( GetProcessHeap(), 0, dev_pts );
336 PSDRV_DrawLine(dev);
337 PSDRV_ResetClip(dev);
338 return TRUE;
342 /***********************************************************************
343 * PSDRV_PolyPolygon
345 BOOL PSDRV_PolyPolygon( PHYSDEV dev, const POINT* pts, const INT* counts, UINT polygons )
347 DWORD polygon, total;
348 INT line;
349 POINT *dev_pts, *pt;
351 TRACE("\n");
353 for (polygon = total = 0; polygon < polygons; polygon++) total += counts[polygon];
354 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
355 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
356 LPtoDP( dev->hdc, dev_pts, total );
358 pt = dev_pts;
360 PSDRV_WriteSpool(dev, "%PolyPolygon\n",13);
361 PSDRV_SetPen(dev);
362 PSDRV_SetClip(dev);
364 for(polygon = 0; polygon < polygons; polygon++) {
365 PSDRV_WriteMoveTo(dev, pt->x, pt->y);
366 pt++;
367 for(line = 1; line < counts[polygon]; line++, pt++)
368 PSDRV_WriteLineTo(dev, pt->x, pt->y);
369 PSDRV_WriteClosePath(dev);
371 HeapFree( GetProcessHeap(), 0, dev_pts );
373 if(GetPolyFillMode( dev->hdc ) == ALTERNATE)
374 PSDRV_Brush(dev, 1);
375 else /* WINDING */
376 PSDRV_Brush(dev, 0);
378 PSDRV_DrawLine(dev);
379 PSDRV_ResetClip(dev);
380 return TRUE;
384 /***********************************************************************
385 * PSDRV_PolyBezier
387 BOOL PSDRV_PolyBezier( PHYSDEV dev, const POINT *pts, DWORD count )
389 DWORD i;
390 POINT *dev_pts;
392 TRACE("\n");
394 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*dev_pts) ))) return FALSE;
395 memcpy( dev_pts, pts, count * sizeof(*dev_pts) );
396 LPtoDP( dev->hdc, dev_pts, count );
398 PSDRV_WriteSpool(dev, "%PolyBezier\n",12);
399 PSDRV_SetPen(dev);
400 PSDRV_SetClip(dev);
401 PSDRV_WriteMoveTo(dev, dev_pts[0].x, dev_pts[0].y );
402 for (i = 1; i < count; i += 3) PSDRV_WriteCurveTo( dev, dev_pts + i );
403 PSDRV_DrawLine(dev);
404 PSDRV_ResetClip(dev);
405 HeapFree( GetProcessHeap(), 0, dev_pts );
406 return TRUE;
410 /***********************************************************************
411 * PSDRV_PolyBezierTo
413 BOOL PSDRV_PolyBezierTo( PHYSDEV dev, const POINT *pts, DWORD count )
415 DWORD i;
416 POINT *dev_pts;
418 TRACE("\n");
420 count++; /* add initial position */
421 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, count * sizeof(*dev_pts) ))) return FALSE;
422 GetCurrentPositionEx( dev->hdc, dev_pts );
423 memcpy( dev_pts + 1, pts, (count - 1) * sizeof(*dev_pts) );
424 LPtoDP( dev->hdc, dev_pts, count );
426 PSDRV_WriteSpool(dev, "%PolyBezier\n",12);
427 PSDRV_SetPen(dev);
428 PSDRV_SetClip(dev);
429 PSDRV_WriteMoveTo(dev, dev_pts[0].x, dev_pts[0].y );
430 for (i = 1; i < count; i += 3) PSDRV_WriteCurveTo( dev, dev_pts + i );
431 PSDRV_DrawLine(dev);
432 PSDRV_ResetClip(dev);
433 HeapFree( GetProcessHeap(), 0, dev_pts );
434 return TRUE;
438 /***********************************************************************
439 * PSDRV_SetPixel
441 COLORREF PSDRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
443 PSCOLOR pscolor;
444 POINT pt;
446 pt.x = x;
447 pt.y = y;
448 LPtoDP( dev->hdc, &pt, 1 );
450 PSDRV_SetClip(dev);
451 /* we bracket the setcolor in gsave/grestore so that we don't trash
452 the current pen colour */
453 PSDRV_WriteGSave(dev);
454 PSDRV_WriteRectangle( dev, pt.x, pt.y, 0, 0 );
455 PSDRV_CreateColor( dev, &pscolor, color );
456 PSDRV_WriteSetColor( dev, &pscolor );
457 PSDRV_WriteFill( dev );
458 PSDRV_WriteGRestore(dev);
459 PSDRV_ResetClip(dev);
460 return color;
463 /***********************************************************************
464 * PSDRV_PaintRgn
466 BOOL PSDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
468 RGNDATA *rgndata = NULL;
469 RECT *pRect;
470 DWORD size, i;
472 TRACE("hdc=%p\n", dev->hdc);
474 size = GetRegionData(hrgn, 0, NULL);
475 rgndata = HeapAlloc( GetProcessHeap(), 0, size );
476 if(!rgndata) {
477 ERR("Can't allocate buffer\n");
478 return FALSE;
481 GetRegionData(hrgn, size, rgndata);
482 if (rgndata->rdh.nCount == 0)
483 goto end;
485 LPtoDP(dev->hdc, (POINT*)rgndata->Buffer, rgndata->rdh.nCount * 2);
487 PSDRV_SetClip(dev);
488 for(i = 0, pRect = (RECT*)rgndata->Buffer; i < rgndata->rdh.nCount; i++, pRect++)
489 PSDRV_WriteRectangle(dev, pRect->left, pRect->top, pRect->right - pRect->left, pRect->bottom - pRect->top);
491 PSDRV_Brush(dev, 0);
492 PSDRV_WriteNewPath(dev);
493 PSDRV_ResetClip(dev);
495 end:
496 HeapFree(GetProcessHeap(), 0, rgndata);
497 return TRUE;
500 static BOOL paint_path( PHYSDEV dev, BOOL stroke, BOOL fill )
502 POINT *points;
503 BYTE *types;
504 BOOL ret = FALSE;
505 int i, size = GetPath( dev->hdc, NULL, NULL, 0 );
507 if (size == -1) return FALSE;
508 if (!size)
510 AbortPath( dev->hdc );
511 return TRUE;
513 points = HeapAlloc( GetProcessHeap(), 0, size * sizeof(*points) );
514 types = HeapAlloc( GetProcessHeap(), 0, size * sizeof(*types) );
515 if (!points || !types) goto done;
516 if (GetPath( dev->hdc, points, types, size ) == -1) goto done;
517 LPtoDP( dev->hdc, points, size );
519 if (stroke) PSDRV_SetPen(dev);
520 PSDRV_SetClip(dev);
521 for (i = 0; i < size; i++)
523 switch (types[i])
525 case PT_MOVETO:
526 PSDRV_WriteMoveTo( dev, points[i].x, points[i].y );
527 break;
528 case PT_LINETO:
529 case PT_LINETO | PT_CLOSEFIGURE:
530 PSDRV_WriteLineTo( dev, points[i].x, points[i].y );
531 if (types[i] & PT_CLOSEFIGURE) PSDRV_WriteClosePath( dev );
532 break;
533 case PT_BEZIERTO:
534 case PT_BEZIERTO | PT_CLOSEFIGURE:
535 PSDRV_WriteCurveTo( dev, points + i );
536 if (types[i] & PT_CLOSEFIGURE) PSDRV_WriteClosePath( dev );
537 i += 2;
538 break;
541 if (fill) PSDRV_Brush( dev, GetPolyFillMode(dev->hdc) == ALTERNATE );
542 if (stroke) PSDRV_DrawLine(dev);
543 else PSDRV_WriteNewPath(dev);
544 PSDRV_ResetClip(dev);
545 AbortPath( dev->hdc );
547 done:
548 HeapFree( GetProcessHeap(), 0, points );
549 HeapFree( GetProcessHeap(), 0, types );
550 return ret;
553 /***********************************************************************
554 * PSDRV_FillPath
556 BOOL PSDRV_FillPath( PHYSDEV dev )
558 return paint_path( dev, FALSE, TRUE );
561 /***********************************************************************
562 * PSDRV_StrokeAndFillPath
564 BOOL PSDRV_StrokeAndFillPath( PHYSDEV dev )
566 return paint_path( dev, TRUE, TRUE );
569 /***********************************************************************
570 * PSDRV_StrokePath
572 BOOL PSDRV_StrokePath( PHYSDEV dev )
574 return paint_path( dev, TRUE, FALSE );