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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #if defined(HAVE_FLOAT_H)
33 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
38 /***********************************************************************
41 * Performs a world-to-viewport transformation on the specified width.
43 INT PSDRV_XWStoDS( PSDRV_PDEVICE *physDev, INT width )
51 LPtoDP( physDev->hdc, pt, 2 );
52 return pt[1].x - pt[0].x;
55 /***********************************************************************
58 * Performs a world-to-viewport transformation on the specified height.
60 INT PSDRV_YWStoDS( PSDRV_PDEVICE *physDev, INT height )
68 LPtoDP( physDev->hdc, pt, 2 );
69 return pt[1].y - pt[0].y;
72 /***********************************************************************
75 static void PSDRV_DrawLine( PSDRV_PDEVICE *physDev )
77 if(physDev->pathdepth)
80 if (physDev->pen.style == PS_NULL)
81 PSDRV_WriteNewPath(physDev);
83 PSDRV_WriteStroke(physDev);
86 /***********************************************************************
89 BOOL PSDRV_LineTo(PSDRV_PDEVICE *physDev, INT x, INT y)
93 TRACE("%d %d\n", x, y);
95 GetCurrentPositionEx( physDev->hdc, pt );
98 LPtoDP( physDev->hdc, pt, 2 );
100 PSDRV_SetPen(physDev);
102 PSDRV_SetClip(physDev);
103 PSDRV_WriteMoveTo(physDev, pt[0].x, pt[0].y );
104 PSDRV_WriteLineTo(physDev, pt[1].x, pt[1].y );
105 PSDRV_DrawLine(physDev);
106 PSDRV_ResetClip(physDev);
112 /***********************************************************************
115 BOOL PSDRV_Rectangle( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom )
119 TRACE("%d %d - %d %d\n", left, top, right, bottom);
124 rect.bottom = bottom;
125 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
127 /* HACK to get inserted eps files printing from Office 2k */
128 if(GetROP2(physDev->hdc) == R2_NOP) {
130 sprintf(buf, "%ld %ld %ld %ld B\n", rect.right - rect.left, rect.bottom - rect.top, rect.left, rect.top);
131 PSDRV_WriteSpool(physDev, buf, strlen(buf));
135 PSDRV_SetPen(physDev);
137 PSDRV_SetClip(physDev);
138 PSDRV_WriteRectangle(physDev, rect.left, rect.top, rect.right - rect.left,
139 rect.bottom - rect.top );
140 PSDRV_Brush(physDev,0);
141 PSDRV_DrawLine(physDev);
142 PSDRV_ResetClip(physDev);
147 /***********************************************************************
150 BOOL PSDRV_RoundRect( PSDRV_PDEVICE *physDev, INT left, INT top, INT right,
151 INT bottom, INT ell_width, INT ell_height )
157 rect[0].right = right;
158 rect[0].bottom = bottom;
161 rect[1].right = ell_width;
162 rect[1].bottom = ell_height;
163 LPtoDP( physDev->hdc, (POINT *)rect, 4 );
167 right = rect[0].right;
168 bottom = rect[0].bottom;
169 if (left > right) { INT tmp = left; left = right; right = tmp; }
170 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
172 ell_width = rect[1].right - rect[1].left;
173 ell_height = rect[1].bottom - rect[1].top;
174 if (ell_width > right - left) ell_width = right - left;
175 if (ell_height > bottom - top) ell_height = bottom - top;
177 PSDRV_WriteSpool(physDev, "%RoundRect\n",11);
178 PSDRV_SetPen(physDev);
180 PSDRV_SetClip(physDev);
181 PSDRV_WriteMoveTo( physDev, left, top + ell_height/2 );
182 PSDRV_WriteArc( physDev, left + ell_width/2, top + ell_height/2, ell_width,
183 ell_height, 90.0, 180.0);
184 PSDRV_WriteLineTo( physDev, right - ell_width/2, top );
185 PSDRV_WriteArc( physDev, right - ell_width/2, top + ell_height/2, ell_width,
186 ell_height, 0.0, 90.0);
187 PSDRV_WriteLineTo( physDev, right, bottom - ell_height/2 );
188 PSDRV_WriteArc( physDev, right - ell_width/2, bottom - ell_height/2, ell_width,
189 ell_height, -90.0, 0.0);
190 PSDRV_WriteLineTo( physDev, right - ell_width/2, bottom);
191 PSDRV_WriteArc( physDev, left + ell_width/2, bottom - ell_height/2, ell_width,
192 ell_height, 180.0, -90.0);
193 PSDRV_WriteClosePath( physDev );
195 PSDRV_Brush(physDev,0);
196 PSDRV_DrawLine(physDev);
197 PSDRV_ResetClip(physDev);
201 /***********************************************************************
204 * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
206 static BOOL PSDRV_DrawArc( PSDRV_PDEVICE *physDev, INT left, INT top,
207 INT right, INT bottom, INT xstart, INT ystart,
208 INT xend, INT yend, int lines )
211 double start_angle, end_angle, ratio;
218 rect.bottom = bottom;
219 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
224 LPtoDP( physDev->hdc, &start, 1 );
225 LPtoDP( physDev->hdc, &end, 1 );
227 x = (rect.left + rect.right) / 2;
228 y = (rect.top + rect.bottom) / 2;
229 w = rect.right - rect.left;
230 h = rect.bottom - rect.top;
234 ratio = ((double)w)/h;
236 /* angle is the angle after the rectangle is transformed to a square and is
237 measured anticlockwise from the +ve x-axis */
239 start_angle = atan2((double)(y - start.y) * ratio, (double)(start.x - x));
240 end_angle = atan2((double)(y - end.y) * ratio, (double)(end.x - x));
242 start_angle *= 180.0 / PI;
243 end_angle *= 180.0 / PI;
245 PSDRV_WriteSpool(physDev,"%DrawArc\n", 9);
246 PSDRV_SetPen(physDev);
248 PSDRV_SetClip(physDev);
249 if(lines == 2) /* pie */
250 PSDRV_WriteMoveTo(physDev, x, y);
252 PSDRV_WriteNewPath( physDev );
254 PSDRV_WriteArc(physDev, x, y, w, h, start_angle, end_angle);
255 if(lines == 1 || lines == 2) { /* chord or pie */
256 PSDRV_WriteClosePath(physDev);
257 PSDRV_Brush(physDev,0);
259 PSDRV_DrawLine(physDev);
260 PSDRV_ResetClip(physDev);
266 /***********************************************************************
269 BOOL PSDRV_Arc( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
270 INT xstart, INT ystart, INT xend, INT yend )
272 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
275 /***********************************************************************
278 BOOL PSDRV_Chord( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
279 INT xstart, INT ystart, INT xend, INT yend )
281 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
285 /***********************************************************************
288 BOOL PSDRV_Pie( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
289 INT xstart, INT ystart, INT xend, INT yend )
291 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
295 /***********************************************************************
298 BOOL PSDRV_Ellipse( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom)
303 TRACE("%d %d - %d %d\n", left, top, right, bottom);
308 rect.bottom = bottom;
309 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
311 x = (rect.left + rect.right) / 2;
312 y = (rect.top + rect.bottom) / 2;
313 w = rect.right - rect.left;
314 h = rect.bottom - rect.top;
316 PSDRV_WriteSpool(physDev, "%Ellipse\n", 9);
317 PSDRV_SetPen(physDev);
319 PSDRV_SetClip(physDev);
320 PSDRV_WriteNewPath(physDev);
321 PSDRV_WriteArc(physDev, x, y, w, h, 0.0, 360.0);
322 PSDRV_WriteClosePath(physDev);
323 PSDRV_Brush(physDev,0);
324 PSDRV_DrawLine(physDev);
325 PSDRV_ResetClip(physDev);
330 /***********************************************************************
333 BOOL PSDRV_PolyPolyline( PSDRV_PDEVICE *physDev, const POINT* pts, const DWORD* counts,
336 DWORD polyline, line, total;
341 for (polyline = total = 0; polyline < polylines; polyline++) total += counts[polyline];
342 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
343 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
344 LPtoDP( physDev->hdc, dev_pts, total );
348 PSDRV_WriteSpool(physDev, "%PolyPolyline\n",14);
349 PSDRV_SetPen(physDev);
350 PSDRV_SetClip(physDev);
352 for(polyline = 0; polyline < polylines; polyline++) {
353 PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
355 for(line = 1; line < counts[polyline]; line++, pt++)
356 PSDRV_WriteLineTo(physDev, pt->x, pt->y);
358 HeapFree( GetProcessHeap(), 0, dev_pts );
360 PSDRV_DrawLine(physDev);
361 PSDRV_ResetClip(physDev);
366 /***********************************************************************
369 BOOL PSDRV_Polyline( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
371 return PSDRV_PolyPolyline( physDev, pt, (LPDWORD) &count, 1 );
375 /***********************************************************************
378 BOOL PSDRV_PolyPolygon( PSDRV_PDEVICE *physDev, const POINT* pts, const INT* counts,
381 DWORD polygon, line, total;
386 for (polygon = total = 0; polygon < polygons; polygon++) total += counts[polygon];
387 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
388 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
389 LPtoDP( physDev->hdc, dev_pts, total );
393 PSDRV_WriteSpool(physDev, "%PolyPolygon\n",13);
394 PSDRV_SetPen(physDev);
395 PSDRV_SetClip(physDev);
397 for(polygon = 0; polygon < polygons; polygon++) {
398 PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
400 for(line = 1; line < counts[polygon]; line++, pt++)
401 PSDRV_WriteLineTo(physDev, pt->x, pt->y);
402 PSDRV_WriteClosePath(physDev);
404 HeapFree( GetProcessHeap(), 0, dev_pts );
406 if(GetPolyFillMode( physDev->hdc ) == ALTERNATE)
407 PSDRV_Brush(physDev, 1);
409 PSDRV_Brush(physDev, 0);
411 PSDRV_DrawLine(physDev);
412 PSDRV_ResetClip(physDev);
417 /***********************************************************************
420 BOOL PSDRV_Polygon( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
422 return PSDRV_PolyPolygon( physDev, pt, &count, 1 );
426 /***********************************************************************
429 COLORREF PSDRV_SetPixel( PSDRV_PDEVICE *physDev, INT x, INT y, COLORREF color )
436 LPtoDP( physDev->hdc, &pt, 1 );
438 PSDRV_SetClip(physDev);
439 /* we bracket the setcolor in gsave/grestore so that we don't trash
440 the current pen colour */
441 PSDRV_WriteGSave(physDev);
442 PSDRV_WriteRectangle( physDev, pt.x, pt.y, 0, 0 );
443 PSDRV_CreateColor( physDev, &pscolor, color );
444 PSDRV_WriteSetColor( physDev, &pscolor );
445 PSDRV_WriteFill( physDev );
446 PSDRV_WriteGRestore(physDev);
447 PSDRV_ResetClip(physDev);
451 /***********************************************************************
454 BOOL PSDRV_PaintRgn( PSDRV_PDEVICE *physDev, HRGN hrgn )
457 RGNDATA *rgndata = NULL;
461 TRACE("hdc=%p\n", physDev->hdc);
463 size = GetRegionData(hrgn, 0, NULL);
464 rgndata = HeapAlloc( GetProcessHeap(), 0, size );
466 ERR("Can't allocate buffer\n");
470 GetRegionData(hrgn, size, rgndata);
471 if (rgndata->rdh.nCount == 0)
474 LPtoDP(physDev->hdc, (POINT*)rgndata->Buffer, rgndata->rdh.nCount * 2);
476 PSDRV_SetClip(physDev);
477 PSDRV_WriteNewPath(physDev);
478 for(i = 0, pRect = (RECT*)rgndata->Buffer; i < rgndata->rdh.nCount; i++, pRect++)
479 PSDRV_WriteRectangle(physDev, pRect->left, pRect->top, pRect->right - pRect->left, pRect->bottom - pRect->top);
481 PSDRV_Brush(physDev, 0);
482 PSDRV_ResetClip(physDev);
485 HeapFree(GetProcessHeap(), 0, rgndata);