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
26 #if defined(HAVE_FLOAT_H)
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
37 /***********************************************************************
40 * Performs a world-to-viewport transformation on the specified width.
42 INT PSDRV_XWStoDS( PSDRV_PDEVICE *physDev, INT width )
50 LPtoDP( physDev->hdc, pt, 2 );
51 return pt[1].x - pt[0].x;
54 /***********************************************************************
57 static void PSDRV_DrawLine( PSDRV_PDEVICE *physDev )
59 if(physDev->pathdepth)
62 if (physDev->pen.style == PS_NULL)
63 PSDRV_WriteNewPath(physDev);
65 PSDRV_WriteStroke(physDev);
68 /***********************************************************************
71 BOOL CDECL PSDRV_LineTo(PSDRV_PDEVICE *physDev, INT x, INT y)
75 TRACE("%d %d\n", x, y);
77 GetCurrentPositionEx( physDev->hdc, pt );
80 LPtoDP( physDev->hdc, pt, 2 );
82 PSDRV_SetPen(physDev);
84 PSDRV_SetClip(physDev);
85 PSDRV_WriteMoveTo(physDev, pt[0].x, pt[0].y );
86 PSDRV_WriteLineTo(physDev, pt[1].x, pt[1].y );
87 PSDRV_DrawLine(physDev);
88 PSDRV_ResetClip(physDev);
94 /***********************************************************************
97 BOOL CDECL PSDRV_Rectangle( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom )
101 TRACE("%d %d - %d %d\n", left, top, right, bottom);
106 rect.bottom = bottom;
107 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
109 /* Windows does something truly hacky here. If we're in passthrough mode
110 and our rop is R2_NOP, then we output the string below. This is used in
111 Office 2k when inserting eps files */
112 if(physDev->job.in_passthrough && !physDev->job.had_passthrough_rect && GetROP2(physDev->hdc) == R2_NOP) {
114 sprintf(buf, "N %d %d %d %d B\n", rect.right - rect.left, rect.bottom - rect.top, rect.left, rect.top);
115 write_spool(physDev, buf, strlen(buf));
116 physDev->job.had_passthrough_rect = TRUE;
120 PSDRV_SetPen(physDev);
122 PSDRV_SetClip(physDev);
123 PSDRV_WriteRectangle(physDev, rect.left, rect.top, rect.right - rect.left,
124 rect.bottom - rect.top );
125 PSDRV_Brush(physDev,0);
126 PSDRV_DrawLine(physDev);
127 PSDRV_ResetClip(physDev);
132 /***********************************************************************
135 BOOL CDECL PSDRV_RoundRect( PSDRV_PDEVICE *physDev, INT left, INT top, INT right,
136 INT bottom, INT ell_width, INT ell_height )
142 rect[0].right = right;
143 rect[0].bottom = bottom;
146 rect[1].right = ell_width;
147 rect[1].bottom = ell_height;
148 LPtoDP( physDev->hdc, (POINT *)rect, 4 );
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(physDev, "%RoundRect\n",11);
163 PSDRV_SetPen(physDev);
165 PSDRV_SetClip(physDev);
166 PSDRV_WriteMoveTo( physDev, left, top + ell_height/2 );
167 PSDRV_WriteArc( physDev, left + ell_width/2, top + ell_height/2, ell_width,
168 ell_height, 90.0, 180.0);
169 PSDRV_WriteLineTo( physDev, right - ell_width/2, top );
170 PSDRV_WriteArc( physDev, right - ell_width/2, top + ell_height/2, ell_width,
171 ell_height, 0.0, 90.0);
172 PSDRV_WriteLineTo( physDev, right, bottom - ell_height/2 );
173 PSDRV_WriteArc( physDev, right - ell_width/2, bottom - ell_height/2, ell_width,
174 ell_height, -90.0, 0.0);
175 PSDRV_WriteLineTo( physDev, right - ell_width/2, bottom);
176 PSDRV_WriteArc( physDev, left + ell_width/2, bottom - ell_height/2, ell_width,
177 ell_height, 180.0, -90.0);
178 PSDRV_WriteClosePath( physDev );
180 PSDRV_Brush(physDev,0);
181 PSDRV_DrawLine(physDev);
182 PSDRV_ResetClip(physDev);
186 /***********************************************************************
189 * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
191 static BOOL PSDRV_DrawArc( PSDRV_PDEVICE *physDev, INT left, INT top,
192 INT right, INT bottom, INT xstart, INT ystart,
193 INT xend, INT yend, int lines )
196 double start_angle, end_angle, ratio;
203 rect.bottom = bottom;
204 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
209 LPtoDP( physDev->hdc, &start, 1 );
210 LPtoDP( physDev->hdc, &end, 1 );
212 x = (rect.left + rect.right) / 2;
213 y = (rect.top + rect.bottom) / 2;
214 w = rect.right - rect.left;
215 h = rect.bottom - rect.top;
219 ratio = ((double)w)/h;
221 /* angle is the angle after the rectangle is transformed to a square and is
222 measured anticlockwise from the +ve x-axis */
224 start_angle = atan2((double)(y - start.y) * ratio, (double)(start.x - x));
225 end_angle = atan2((double)(y - end.y) * ratio, (double)(end.x - x));
227 start_angle *= 180.0 / PI;
228 end_angle *= 180.0 / PI;
230 PSDRV_WriteSpool(physDev,"%DrawArc\n", 9);
231 PSDRV_SetPen(physDev);
233 PSDRV_SetClip(physDev);
234 if(lines == 2) /* pie */
235 PSDRV_WriteMoveTo(physDev, x, y);
237 PSDRV_WriteNewPath( physDev );
239 PSDRV_WriteArc(physDev, x, y, w, h, start_angle, end_angle);
240 if(lines == 1 || lines == 2) { /* chord or pie */
241 PSDRV_WriteClosePath(physDev);
242 PSDRV_Brush(physDev,0);
244 PSDRV_DrawLine(physDev);
245 PSDRV_ResetClip(physDev);
251 /***********************************************************************
254 BOOL CDECL PSDRV_Arc( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
255 INT xstart, INT ystart, INT xend, INT yend )
257 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
260 /***********************************************************************
263 BOOL CDECL PSDRV_Chord( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
264 INT xstart, INT ystart, INT xend, INT yend )
266 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
270 /***********************************************************************
273 BOOL CDECL PSDRV_Pie( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
274 INT xstart, INT ystart, INT xend, INT yend )
276 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
280 /***********************************************************************
283 BOOL CDECL PSDRV_Ellipse( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom)
288 TRACE("%d %d - %d %d\n", left, top, right, bottom);
293 rect.bottom = bottom;
294 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
296 x = (rect.left + rect.right) / 2;
297 y = (rect.top + rect.bottom) / 2;
298 w = rect.right - rect.left;
299 h = rect.bottom - rect.top;
301 PSDRV_WriteSpool(physDev, "%Ellipse\n", 9);
302 PSDRV_SetPen(physDev);
304 PSDRV_SetClip(physDev);
305 PSDRV_WriteNewPath(physDev);
306 PSDRV_WriteArc(physDev, x, y, w, h, 0.0, 360.0);
307 PSDRV_WriteClosePath(physDev);
308 PSDRV_Brush(physDev,0);
309 PSDRV_DrawLine(physDev);
310 PSDRV_ResetClip(physDev);
315 /***********************************************************************
318 BOOL CDECL PSDRV_PolyPolyline( PSDRV_PDEVICE *physDev, const POINT* pts, const DWORD* counts,
321 DWORD polyline, line, total;
326 for (polyline = total = 0; polyline < polylines; polyline++) total += counts[polyline];
327 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
328 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
329 LPtoDP( physDev->hdc, dev_pts, total );
333 PSDRV_WriteSpool(physDev, "%PolyPolyline\n",14);
334 PSDRV_SetPen(physDev);
335 PSDRV_SetClip(physDev);
337 for(polyline = 0; polyline < polylines; polyline++) {
338 PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
340 for(line = 1; line < counts[polyline]; line++, pt++)
341 PSDRV_WriteLineTo(physDev, pt->x, pt->y);
343 HeapFree( GetProcessHeap(), 0, dev_pts );
345 PSDRV_DrawLine(physDev);
346 PSDRV_ResetClip(physDev);
351 /***********************************************************************
354 BOOL CDECL PSDRV_Polyline( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
356 return PSDRV_PolyPolyline( physDev, pt, (LPDWORD) &count, 1 );
360 /***********************************************************************
363 BOOL CDECL PSDRV_PolyPolygon( PSDRV_PDEVICE *physDev, const POINT* pts, const INT* counts,
366 DWORD polygon, total;
372 for (polygon = total = 0; polygon < polygons; polygon++) total += counts[polygon];
373 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
374 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
375 LPtoDP( physDev->hdc, dev_pts, total );
379 PSDRV_WriteSpool(physDev, "%PolyPolygon\n",13);
380 PSDRV_SetPen(physDev);
381 PSDRV_SetClip(physDev);
382 PSDRV_WriteNewPath(physDev);
384 for(polygon = 0; polygon < polygons; polygon++) {
385 PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
387 for(line = 1; line < counts[polygon]; line++, pt++)
388 PSDRV_WriteLineTo(physDev, pt->x, pt->y);
389 PSDRV_WriteClosePath(physDev);
391 HeapFree( GetProcessHeap(), 0, dev_pts );
393 if(GetPolyFillMode( physDev->hdc ) == ALTERNATE)
394 PSDRV_Brush(physDev, 1);
396 PSDRV_Brush(physDev, 0);
398 PSDRV_DrawLine(physDev);
399 PSDRV_ResetClip(physDev);
404 /***********************************************************************
407 BOOL CDECL PSDRV_Polygon( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
409 return PSDRV_PolyPolygon( physDev, pt, &count, 1 );
413 /***********************************************************************
416 COLORREF CDECL PSDRV_SetPixel( PSDRV_PDEVICE *physDev, INT x, INT y, COLORREF color )
423 LPtoDP( physDev->hdc, &pt, 1 );
425 PSDRV_SetClip(physDev);
426 /* we bracket the setcolor in gsave/grestore so that we don't trash
427 the current pen colour */
428 PSDRV_WriteGSave(physDev);
429 PSDRV_WriteRectangle( physDev, pt.x, pt.y, 0, 0 );
430 PSDRV_CreateColor( physDev, &pscolor, color );
431 PSDRV_WriteSetColor( physDev, &pscolor );
432 PSDRV_WriteFill( physDev );
433 PSDRV_WriteGRestore(physDev);
434 PSDRV_ResetClip(physDev);
438 /***********************************************************************
441 BOOL CDECL PSDRV_PaintRgn( PSDRV_PDEVICE *physDev, HRGN hrgn )
444 RGNDATA *rgndata = NULL;
448 TRACE("hdc=%p\n", physDev->hdc);
450 size = GetRegionData(hrgn, 0, NULL);
451 rgndata = HeapAlloc( GetProcessHeap(), 0, size );
453 ERR("Can't allocate buffer\n");
457 GetRegionData(hrgn, size, rgndata);
458 if (rgndata->rdh.nCount == 0)
461 LPtoDP(physDev->hdc, (POINT*)rgndata->Buffer, rgndata->rdh.nCount * 2);
463 PSDRV_SetClip(physDev);
464 PSDRV_WriteNewPath(physDev);
465 for(i = 0, pRect = (RECT*)rgndata->Buffer; i < rgndata->rdh.nCount; i++, pRect++)
466 PSDRV_WriteRectangle(physDev, pRect->left, pRect->top, pRect->right - pRect->left, pRect->bottom - pRect->top);
468 PSDRV_Brush(physDev, 0);
469 PSDRV_ResetClip(physDev);
472 HeapFree(GetProcessHeap(), 0, rgndata);