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
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(psdrv
);
34 /***********************************************************************
37 * Performs a world-to-viewport transformation on the specified width.
39 INT
PSDRV_XWStoDS( PHYSDEV dev
, INT width
)
47 LPtoDP( dev
->hdc
, pt
, 2 );
48 return pt
[1].x
- pt
[0].x
;
51 /***********************************************************************
54 static void PSDRV_DrawLine( PHYSDEV dev
)
56 PSDRV_PDEVICE
*physDev
= get_psdrv_dev( dev
);
58 if(physDev
->pathdepth
)
61 if (physDev
->pen
.style
== PS_NULL
)
62 PSDRV_WriteNewPath(dev
);
64 PSDRV_WriteStroke(dev
);
67 /***********************************************************************
70 BOOL CDECL
PSDRV_LineTo(PHYSDEV dev
, INT x
, INT y
)
74 TRACE("%d %d\n", x
, y
);
76 GetCurrentPositionEx( dev
->hdc
, pt
);
79 LPtoDP( dev
->hdc
, pt
, 2 );
84 PSDRV_WriteMoveTo(dev
, pt
[0].x
, pt
[0].y
);
85 PSDRV_WriteLineTo(dev
, pt
[1].x
, pt
[1].y
);
93 /***********************************************************************
96 BOOL CDECL
PSDRV_Rectangle( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
)
98 PSDRV_PDEVICE
*physDev
= get_psdrv_dev( dev
);
101 TRACE("%d %d - %d %d\n", left
, top
, right
, bottom
);
103 SetRect(&rect
, left
, top
, right
, bottom
);
104 LPtoDP( dev
->hdc
, (POINT
*)&rect
, 2 );
106 /* Windows does something truly hacky here. If we're in passthrough mode
107 and our rop is R2_NOP, then we output the string below. This is used in
108 Office 2k when inserting eps files */
109 if (physDev
->job
.passthrough_state
== passthrough_active
&& GetROP2(dev
->hdc
) == R2_NOP
)
113 sprintf(buf
, "N %d %d %d %d B\n", rect
.right
- rect
.left
, rect
.bottom
- rect
.top
, rect
.left
, rect
.top
);
114 write_spool(dev
, buf
, strlen(buf
));
115 physDev
->job
.passthrough_state
= passthrough_had_rect
;
122 PSDRV_WriteRectangle(dev
, rect
.left
, rect
.top
, rect
.right
- rect
.left
,
123 rect
.bottom
- rect
.top
);
126 PSDRV_ResetClip(dev
);
131 /***********************************************************************
134 BOOL CDECL
PSDRV_RoundRect( PHYSDEV dev
, INT left
, INT top
, INT right
,
135 INT bottom
, INT ell_width
, INT ell_height
)
139 SetRect(&rect
[0], left
, top
, right
, bottom
);
140 SetRect(&rect
[1], 0, 0, ell_width
, ell_height
);
141 LPtoDP( dev
->hdc
, (POINT
*)rect
, 4 );
145 right
= rect
[0].right
;
146 bottom
= rect
[0].bottom
;
147 if (left
> right
) { INT tmp
= left
; left
= right
; right
= tmp
; }
148 if (top
> bottom
) { INT tmp
= top
; top
= bottom
; bottom
= tmp
; }
150 ell_width
= rect
[1].right
- rect
[1].left
;
151 ell_height
= rect
[1].bottom
- rect
[1].top
;
152 if (ell_width
> right
- left
) ell_width
= right
- left
;
153 if (ell_height
> bottom
- top
) ell_height
= bottom
- top
;
155 PSDRV_WriteSpool(dev
, "%RoundRect\n",11);
159 PSDRV_WriteMoveTo( dev
, left
, top
+ ell_height
/2 );
160 PSDRV_WriteArc( dev
, left
+ ell_width
/2, top
+ ell_height
/2, ell_width
,
161 ell_height
, 90.0, 180.0);
162 PSDRV_WriteLineTo( dev
, right
- ell_width
/2, top
);
163 PSDRV_WriteArc( dev
, right
- ell_width
/2, top
+ ell_height
/2, ell_width
,
164 ell_height
, 0.0, 90.0);
165 PSDRV_WriteLineTo( dev
, right
, bottom
- ell_height
/2 );
166 PSDRV_WriteArc( dev
, right
- ell_width
/2, bottom
- ell_height
/2, ell_width
,
167 ell_height
, -90.0, 0.0);
168 PSDRV_WriteLineTo( dev
, right
- ell_width
/2, bottom
);
169 PSDRV_WriteArc( dev
, left
+ ell_width
/2, bottom
- ell_height
/2, ell_width
,
170 ell_height
, 180.0, -90.0);
171 PSDRV_WriteClosePath( dev
);
175 PSDRV_ResetClip(dev
);
179 /***********************************************************************
182 * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
184 static BOOL
PSDRV_DrawArc( PHYSDEV dev
, INT left
, INT top
,
185 INT right
, INT bottom
, INT xstart
, INT ystart
,
186 INT xend
, INT yend
, int lines
)
189 double start_angle
, end_angle
, ratio
;
193 SetRect(&rect
, left
, top
, right
, bottom
);
194 LPtoDP( dev
->hdc
, (POINT
*)&rect
, 2 );
199 LPtoDP( dev
->hdc
, &start
, 1 );
200 LPtoDP( dev
->hdc
, &end
, 1 );
202 x
= (rect
.left
+ rect
.right
) / 2;
203 y
= (rect
.top
+ rect
.bottom
) / 2;
204 w
= rect
.right
- rect
.left
;
205 h
= rect
.bottom
- rect
.top
;
209 ratio
= ((double)w
)/h
;
211 /* angle is the angle after the rectangle is transformed to a square and is
212 measured anticlockwise from the +ve x-axis */
214 start_angle
= atan2((double)(y
- start
.y
) * ratio
, (double)(start
.x
- x
));
215 end_angle
= atan2((double)(y
- end
.y
) * ratio
, (double)(end
.x
- x
));
217 start_angle
*= 180.0 / M_PI
;
218 end_angle
*= 180.0 / M_PI
;
220 PSDRV_WriteSpool(dev
,"%DrawArc\n", 9);
224 if(lines
== 2) /* pie */
225 PSDRV_WriteMoveTo(dev
, x
, y
);
227 PSDRV_WriteNewPath( dev
);
229 PSDRV_WriteArc(dev
, x
, y
, w
, h
, start_angle
, end_angle
);
230 if(lines
== 1 || lines
== 2) { /* chord or pie */
231 PSDRV_WriteClosePath(dev
);
235 PSDRV_ResetClip(dev
);
241 /***********************************************************************
244 BOOL CDECL
PSDRV_Arc( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
,
245 INT xstart
, INT ystart
, INT xend
, INT yend
)
247 return PSDRV_DrawArc( dev
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
, 0 );
250 /***********************************************************************
253 BOOL CDECL
PSDRV_Chord( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
,
254 INT xstart
, INT ystart
, INT xend
, INT yend
)
256 return PSDRV_DrawArc( dev
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
, 1 );
260 /***********************************************************************
263 BOOL CDECL
PSDRV_Pie( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
,
264 INT xstart
, INT ystart
, INT xend
, INT yend
)
266 return PSDRV_DrawArc( dev
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
, 2 );
270 /***********************************************************************
273 BOOL CDECL
PSDRV_Ellipse( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
)
278 TRACE("%d %d - %d %d\n", left
, top
, right
, bottom
);
280 SetRect(&rect
, left
, top
, right
, bottom
);
281 LPtoDP( dev
->hdc
, (POINT
*)&rect
, 2 );
283 x
= (rect
.left
+ rect
.right
) / 2;
284 y
= (rect
.top
+ rect
.bottom
) / 2;
285 w
= rect
.right
- rect
.left
;
286 h
= rect
.bottom
- rect
.top
;
288 PSDRV_WriteSpool(dev
, "%Ellipse\n", 9);
292 PSDRV_WriteNewPath(dev
);
293 PSDRV_WriteArc(dev
, x
, y
, w
, h
, 0.0, 360.0);
294 PSDRV_WriteClosePath(dev
);
297 PSDRV_ResetClip(dev
);
302 /***********************************************************************
305 BOOL CDECL
PSDRV_PolyPolyline( PHYSDEV dev
, const POINT
* pts
, const DWORD
* counts
, DWORD polylines
)
307 DWORD polyline
, line
, total
;
312 for (polyline
= total
= 0; polyline
< polylines
; polyline
++) total
+= counts
[polyline
];
313 if (!(dev_pts
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(*dev_pts
) ))) return FALSE
;
314 memcpy( dev_pts
, pts
, total
* sizeof(*dev_pts
) );
315 LPtoDP( dev
->hdc
, dev_pts
, total
);
319 PSDRV_WriteSpool(dev
, "%PolyPolyline\n",14);
323 for(polyline
= 0; polyline
< polylines
; polyline
++) {
324 PSDRV_WriteMoveTo(dev
, pt
->x
, pt
->y
);
326 for(line
= 1; line
< counts
[polyline
]; line
++, pt
++)
327 PSDRV_WriteLineTo(dev
, pt
->x
, pt
->y
);
329 HeapFree( GetProcessHeap(), 0, dev_pts
);
332 PSDRV_ResetClip(dev
);
337 /***********************************************************************
340 BOOL CDECL
PSDRV_PolyPolygon( PHYSDEV dev
, const POINT
* pts
, const INT
* counts
, UINT polygons
)
342 DWORD polygon
, total
;
348 for (polygon
= total
= 0; polygon
< polygons
; polygon
++) total
+= counts
[polygon
];
349 if (!(dev_pts
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(*dev_pts
) ))) return FALSE
;
350 memcpy( dev_pts
, pts
, total
* sizeof(*dev_pts
) );
351 LPtoDP( dev
->hdc
, dev_pts
, total
);
355 PSDRV_WriteSpool(dev
, "%PolyPolygon\n",13);
359 for(polygon
= 0; polygon
< polygons
; polygon
++) {
360 PSDRV_WriteMoveTo(dev
, pt
->x
, pt
->y
);
362 for(line
= 1; line
< counts
[polygon
]; line
++, pt
++)
363 PSDRV_WriteLineTo(dev
, pt
->x
, pt
->y
);
364 PSDRV_WriteClosePath(dev
);
366 HeapFree( GetProcessHeap(), 0, dev_pts
);
368 if(GetPolyFillMode( dev
->hdc
) == ALTERNATE
)
374 PSDRV_ResetClip(dev
);
379 /***********************************************************************
382 BOOL CDECL
PSDRV_PolyBezier( PHYSDEV dev
, const POINT
*pts
, DWORD count
)
389 if (!(dev_pts
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*dev_pts
) ))) return FALSE
;
390 memcpy( dev_pts
, pts
, count
* sizeof(*dev_pts
) );
391 LPtoDP( dev
->hdc
, dev_pts
, count
);
393 PSDRV_WriteSpool(dev
, "%PolyBezier\n",12);
396 PSDRV_WriteMoveTo(dev
, dev_pts
[0].x
, dev_pts
[0].y
);
397 for (i
= 1; i
< count
; i
+= 3) PSDRV_WriteCurveTo( dev
, dev_pts
+ i
);
399 PSDRV_ResetClip(dev
);
400 HeapFree( GetProcessHeap(), 0, dev_pts
);
405 /***********************************************************************
408 BOOL CDECL
PSDRV_PolyBezierTo( PHYSDEV dev
, const POINT
*pts
, DWORD count
)
415 count
++; /* add initial position */
416 if (!(dev_pts
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*dev_pts
) ))) return FALSE
;
417 GetCurrentPositionEx( dev
->hdc
, dev_pts
);
418 memcpy( dev_pts
+ 1, pts
, (count
- 1) * sizeof(*dev_pts
) );
419 LPtoDP( dev
->hdc
, dev_pts
, count
);
421 PSDRV_WriteSpool(dev
, "%PolyBezier\n",12);
424 PSDRV_WriteMoveTo(dev
, dev_pts
[0].x
, dev_pts
[0].y
);
425 for (i
= 1; i
< count
; i
+= 3) PSDRV_WriteCurveTo( dev
, dev_pts
+ i
);
427 PSDRV_ResetClip(dev
);
428 HeapFree( GetProcessHeap(), 0, dev_pts
);
433 /***********************************************************************
436 COLORREF CDECL
PSDRV_SetPixel( PHYSDEV dev
, INT x
, INT y
, COLORREF color
)
443 LPtoDP( dev
->hdc
, &pt
, 1 );
446 /* we bracket the setcolor in gsave/grestore so that we don't trash
447 the current pen colour */
448 PSDRV_WriteGSave(dev
);
449 PSDRV_WriteRectangle( dev
, pt
.x
, pt
.y
, 0, 0 );
450 PSDRV_CreateColor( dev
, &pscolor
, color
);
451 PSDRV_WriteSetColor( dev
, &pscolor
);
452 PSDRV_WriteFill( dev
);
453 PSDRV_WriteGRestore(dev
);
454 PSDRV_ResetClip(dev
);
458 /***********************************************************************
461 BOOL CDECL
PSDRV_PaintRgn( PHYSDEV dev
, HRGN hrgn
)
463 RGNDATA
*rgndata
= NULL
;
467 TRACE("hdc=%p\n", dev
->hdc
);
469 size
= GetRegionData(hrgn
, 0, NULL
);
470 rgndata
= HeapAlloc( GetProcessHeap(), 0, size
);
472 ERR("Can't allocate buffer\n");
476 GetRegionData(hrgn
, size
, rgndata
);
477 if (rgndata
->rdh
.nCount
== 0)
480 LPtoDP(dev
->hdc
, (POINT
*)rgndata
->Buffer
, rgndata
->rdh
.nCount
* 2);
483 for(i
= 0, pRect
= (RECT
*)rgndata
->Buffer
; i
< rgndata
->rdh
.nCount
; i
++, pRect
++)
484 PSDRV_WriteRectangle(dev
, pRect
->left
, pRect
->top
, pRect
->right
- pRect
->left
, pRect
->bottom
- pRect
->top
);
487 PSDRV_WriteNewPath(dev
);
488 PSDRV_ResetClip(dev
);
491 HeapFree(GetProcessHeap(), 0, rgndata
);
495 static BOOL
paint_path( PHYSDEV dev
, BOOL stroke
, BOOL fill
)
500 int i
, size
= GetPath( dev
->hdc
, NULL
, NULL
, 0 );
502 if (size
== -1) return FALSE
;
505 AbortPath( dev
->hdc
);
508 points
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(*points
) );
509 types
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(*types
) );
510 if (!points
|| !types
) goto done
;
511 if (GetPath( dev
->hdc
, points
, types
, size
) == -1) goto done
;
512 LPtoDP( dev
->hdc
, points
, size
);
514 if (stroke
) PSDRV_SetPen(dev
);
516 for (i
= 0; i
< size
; i
++)
521 PSDRV_WriteMoveTo( dev
, points
[i
].x
, points
[i
].y
);
524 case PT_LINETO
| PT_CLOSEFIGURE
:
525 PSDRV_WriteLineTo( dev
, points
[i
].x
, points
[i
].y
);
526 if (types
[i
] & PT_CLOSEFIGURE
) PSDRV_WriteClosePath( dev
);
529 case PT_BEZIERTO
| PT_CLOSEFIGURE
:
530 PSDRV_WriteCurveTo( dev
, points
+ i
);
531 if (types
[i
] & PT_CLOSEFIGURE
) PSDRV_WriteClosePath( dev
);
536 if (fill
) PSDRV_Brush( dev
, GetPolyFillMode(dev
->hdc
) == ALTERNATE
);
537 if (stroke
) PSDRV_DrawLine(dev
);
538 else PSDRV_WriteNewPath(dev
);
539 PSDRV_ResetClip(dev
);
540 AbortPath( dev
->hdc
);
543 HeapFree( GetProcessHeap(), 0, points
);
544 HeapFree( GetProcessHeap(), 0, types
);
548 /***********************************************************************
551 BOOL CDECL
PSDRV_FillPath( PHYSDEV dev
)
553 return paint_path( dev
, FALSE
, TRUE
);
556 /***********************************************************************
557 * PSDRV_StrokeAndFillPath
559 BOOL CDECL
PSDRV_StrokeAndFillPath( PHYSDEV dev
)
561 return paint_path( dev
, TRUE
, TRUE
);
564 /***********************************************************************
567 BOOL CDECL
PSDRV_StrokePath( PHYSDEV dev
)
569 return paint_path( dev
, TRUE
, FALSE
);