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 * Performs a world-to-viewport transformation on the specified height.
59 INT PSDRV_YWStoDS( PSDRV_PDEVICE *physDev, INT height )
67 LPtoDP( physDev->hdc, pt, 2 );
68 return pt[1].y - pt[0].y;
71 /***********************************************************************
74 static void PSDRV_DrawLine( PSDRV_PDEVICE *physDev )
76 if(physDev->pathdepth)
79 if (physDev->pen.style == PS_NULL)
80 PSDRV_WriteNewPath(physDev);
82 PSDRV_WriteStroke(physDev);
85 /***********************************************************************
88 BOOL CDECL PSDRV_LineTo(PSDRV_PDEVICE *physDev, INT x, INT y)
92 TRACE("%d %d\n", x, y);
94 GetCurrentPositionEx( physDev->hdc, pt );
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);
111 /***********************************************************************
114 BOOL CDECL PSDRV_Rectangle( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom )
118 TRACE("%d %d - %d %d\n", left, top, right, bottom);
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) {
131 sprintf(buf, "N %d %d %d %d B\n", rect.right - rect.left, rect.bottom - rect.top, rect.left, rect.top);
132 write_spool(physDev, buf, strlen(buf));
133 physDev->job.had_passthrough_rect = 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);
149 /***********************************************************************
152 BOOL CDECL PSDRV_RoundRect( PSDRV_PDEVICE *physDev, INT left, INT top, INT right,
153 INT bottom, INT ell_width, INT ell_height )
159 rect[0].right = right;
160 rect[0].bottom = bottom;
163 rect[1].right = ell_width;
164 rect[1].bottom = ell_height;
165 LPtoDP( physDev->hdc, (POINT *)rect, 4 );
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);
203 /***********************************************************************
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 )
213 double start_angle, end_angle, ratio;
220 rect.bottom = bottom;
221 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
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;
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);
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);
268 /***********************************************************************
271 BOOL CDECL 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 /***********************************************************************
280 BOOL CDECL 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 /***********************************************************************
290 BOOL CDECL 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 /***********************************************************************
300 BOOL CDECL PSDRV_Ellipse( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom)
305 TRACE("%d %d - %d %d\n", left, top, right, bottom);
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);
332 /***********************************************************************
335 BOOL CDECL PSDRV_PolyPolyline( PSDRV_PDEVICE *physDev, const POINT* pts, const DWORD* counts,
338 DWORD polyline, line, total;
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 );
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);
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);
368 /***********************************************************************
371 BOOL CDECL PSDRV_Polyline( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
373 return PSDRV_PolyPolyline( physDev, pt, (LPDWORD) &count, 1 );
377 /***********************************************************************
380 BOOL CDECL PSDRV_PolyPolygon( PSDRV_PDEVICE *physDev, const POINT* pts, const INT* counts,
383 DWORD polygon, total;
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 );
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);
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);
412 PSDRV_Brush(physDev, 0);
414 PSDRV_DrawLine(physDev);
415 PSDRV_ResetClip(physDev);
420 /***********************************************************************
423 BOOL CDECL PSDRV_Polygon( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
425 return PSDRV_PolyPolygon( physDev, pt, &count, 1 );
429 /***********************************************************************
432 COLORREF CDECL PSDRV_SetPixel( PSDRV_PDEVICE *physDev, INT x, INT y, COLORREF color )
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);
454 /***********************************************************************
457 BOOL CDECL PSDRV_PaintRgn( PSDRV_PDEVICE *physDev, HRGN hrgn )
460 RGNDATA *rgndata = NULL;
464 TRACE("hdc=%p\n", physDev->hdc);
466 size = GetRegionData(hrgn, 0, NULL);
467 rgndata = HeapAlloc( GetProcessHeap(), 0, size );
469 ERR("Can't allocate buffer\n");
473 GetRegionData(hrgn, size, rgndata);
474 if (rgndata->rdh.nCount == 0)
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);
488 HeapFree(GetProcessHeap(), 0, rgndata);