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( PHYSDEV dev
, INT width
)
50 LPtoDP( dev
->hdc
, pt
, 2 );
51 return pt
[1].x
- pt
[0].x
;
54 /***********************************************************************
57 static void PSDRV_DrawLine( PHYSDEV dev
)
59 PSDRV_PDEVICE
*physDev
= get_psdrv_dev( dev
);
61 if(physDev
->pathdepth
)
64 if (physDev
->pen
.style
== PS_NULL
)
65 PSDRV_WriteNewPath(dev
);
67 PSDRV_WriteStroke(dev
);
70 /***********************************************************************
73 BOOL
PSDRV_LineTo(PHYSDEV dev
, INT x
, INT y
)
77 TRACE("%d %d\n", x
, y
);
79 GetCurrentPositionEx( dev
->hdc
, pt
);
82 LPtoDP( dev
->hdc
, pt
, 2 );
87 PSDRV_WriteMoveTo(dev
, pt
[0].x
, pt
[0].y
);
88 PSDRV_WriteLineTo(dev
, pt
[1].x
, pt
[1].y
);
96 /***********************************************************************
99 BOOL
PSDRV_Rectangle( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
)
101 PSDRV_PDEVICE
*physDev
= get_psdrv_dev( dev
);
104 TRACE("%d %d - %d %d\n", left
, top
, right
, bottom
);
109 rect
.bottom
= bottom
;
110 LPtoDP( dev
->hdc
, (POINT
*)&rect
, 2 );
112 /* Windows does something truly hacky here. If we're in passthrough mode
113 and our rop is R2_NOP, then we output the string below. This is used in
114 Office 2k when inserting eps files */
115 if(physDev
->job
.in_passthrough
&& !physDev
->job
.had_passthrough_rect
&& GetROP2(dev
->hdc
) == R2_NOP
) {
117 sprintf(buf
, "N %d %d %d %d B\n", rect
.right
- rect
.left
, rect
.bottom
- rect
.top
, rect
.left
, rect
.top
);
118 write_spool(dev
, buf
, strlen(buf
));
119 physDev
->job
.had_passthrough_rect
= TRUE
;
126 PSDRV_WriteRectangle(dev
, rect
.left
, rect
.top
, rect
.right
- rect
.left
,
127 rect
.bottom
- rect
.top
);
130 PSDRV_ResetClip(dev
);
135 /***********************************************************************
138 BOOL
PSDRV_RoundRect( PHYSDEV dev
, INT left
, INT top
, INT right
,
139 INT bottom
, INT ell_width
, INT ell_height
)
145 rect
[0].right
= right
;
146 rect
[0].bottom
= bottom
;
149 rect
[1].right
= ell_width
;
150 rect
[1].bottom
= ell_height
;
151 LPtoDP( dev
->hdc
, (POINT
*)rect
, 4 );
155 right
= rect
[0].right
;
156 bottom
= rect
[0].bottom
;
157 if (left
> right
) { INT tmp
= left
; left
= right
; right
= tmp
; }
158 if (top
> bottom
) { INT tmp
= top
; top
= bottom
; bottom
= tmp
; }
160 ell_width
= rect
[1].right
- rect
[1].left
;
161 ell_height
= rect
[1].bottom
- rect
[1].top
;
162 if (ell_width
> right
- left
) ell_width
= right
- left
;
163 if (ell_height
> bottom
- top
) ell_height
= bottom
- top
;
165 PSDRV_WriteSpool(dev
, "%RoundRect\n",11);
169 PSDRV_WriteMoveTo( dev
, left
, top
+ ell_height
/2 );
170 PSDRV_WriteArc( dev
, left
+ ell_width
/2, top
+ ell_height
/2, ell_width
,
171 ell_height
, 90.0, 180.0);
172 PSDRV_WriteLineTo( dev
, right
- ell_width
/2, top
);
173 PSDRV_WriteArc( dev
, right
- ell_width
/2, top
+ ell_height
/2, ell_width
,
174 ell_height
, 0.0, 90.0);
175 PSDRV_WriteLineTo( dev
, right
, bottom
- ell_height
/2 );
176 PSDRV_WriteArc( dev
, right
- ell_width
/2, bottom
- ell_height
/2, ell_width
,
177 ell_height
, -90.0, 0.0);
178 PSDRV_WriteLineTo( dev
, right
- ell_width
/2, bottom
);
179 PSDRV_WriteArc( dev
, left
+ ell_width
/2, bottom
- ell_height
/2, ell_width
,
180 ell_height
, 180.0, -90.0);
181 PSDRV_WriteClosePath( dev
);
185 PSDRV_ResetClip(dev
);
189 /***********************************************************************
192 * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
194 static BOOL
PSDRV_DrawArc( PHYSDEV dev
, INT left
, INT top
,
195 INT right
, INT bottom
, INT xstart
, INT ystart
,
196 INT xend
, INT yend
, int lines
)
199 double start_angle
, end_angle
, ratio
;
206 rect
.bottom
= bottom
;
207 LPtoDP( dev
->hdc
, (POINT
*)&rect
, 2 );
212 LPtoDP( dev
->hdc
, &start
, 1 );
213 LPtoDP( dev
->hdc
, &end
, 1 );
215 x
= (rect
.left
+ rect
.right
) / 2;
216 y
= (rect
.top
+ rect
.bottom
) / 2;
217 w
= rect
.right
- rect
.left
;
218 h
= rect
.bottom
- rect
.top
;
222 ratio
= ((double)w
)/h
;
224 /* angle is the angle after the rectangle is transformed to a square and is
225 measured anticlockwise from the +ve x-axis */
227 start_angle
= atan2((double)(y
- start
.y
) * ratio
, (double)(start
.x
- x
));
228 end_angle
= atan2((double)(y
- end
.y
) * ratio
, (double)(end
.x
- x
));
230 start_angle
*= 180.0 / PI
;
231 end_angle
*= 180.0 / PI
;
233 PSDRV_WriteSpool(dev
,"%DrawArc\n", 9);
237 if(lines
== 2) /* pie */
238 PSDRV_WriteMoveTo(dev
, x
, y
);
240 PSDRV_WriteNewPath( dev
);
242 PSDRV_WriteArc(dev
, x
, y
, w
, h
, start_angle
, end_angle
);
243 if(lines
== 1 || lines
== 2) { /* chord or pie */
244 PSDRV_WriteClosePath(dev
);
248 PSDRV_ResetClip(dev
);
254 /***********************************************************************
257 BOOL
PSDRV_Arc( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
,
258 INT xstart
, INT ystart
, INT xend
, INT yend
)
260 return PSDRV_DrawArc( dev
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
, 0 );
263 /***********************************************************************
266 BOOL
PSDRV_Chord( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
,
267 INT xstart
, INT ystart
, INT xend
, INT yend
)
269 return PSDRV_DrawArc( dev
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
, 1 );
273 /***********************************************************************
276 BOOL
PSDRV_Pie( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
,
277 INT xstart
, INT ystart
, INT xend
, INT yend
)
279 return PSDRV_DrawArc( dev
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
, 2 );
283 /***********************************************************************
286 BOOL
PSDRV_Ellipse( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
)
291 TRACE("%d %d - %d %d\n", left
, top
, right
, bottom
);
296 rect
.bottom
= bottom
;
297 LPtoDP( dev
->hdc
, (POINT
*)&rect
, 2 );
299 x
= (rect
.left
+ rect
.right
) / 2;
300 y
= (rect
.top
+ rect
.bottom
) / 2;
301 w
= rect
.right
- rect
.left
;
302 h
= rect
.bottom
- rect
.top
;
304 PSDRV_WriteSpool(dev
, "%Ellipse\n", 9);
308 PSDRV_WriteNewPath(dev
);
309 PSDRV_WriteArc(dev
, x
, y
, w
, h
, 0.0, 360.0);
310 PSDRV_WriteClosePath(dev
);
313 PSDRV_ResetClip(dev
);
318 /***********************************************************************
321 BOOL
PSDRV_PolyPolyline( PHYSDEV dev
, const POINT
* pts
, const DWORD
* counts
, DWORD polylines
)
323 DWORD polyline
, line
, total
;
328 for (polyline
= total
= 0; polyline
< polylines
; polyline
++) total
+= counts
[polyline
];
329 if (!(dev_pts
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(*dev_pts
) ))) return FALSE
;
330 memcpy( dev_pts
, pts
, total
* sizeof(*dev_pts
) );
331 LPtoDP( dev
->hdc
, dev_pts
, total
);
335 PSDRV_WriteSpool(dev
, "%PolyPolyline\n",14);
339 for(polyline
= 0; polyline
< polylines
; polyline
++) {
340 PSDRV_WriteMoveTo(dev
, pt
->x
, pt
->y
);
342 for(line
= 1; line
< counts
[polyline
]; line
++, pt
++)
343 PSDRV_WriteLineTo(dev
, pt
->x
, pt
->y
);
345 HeapFree( GetProcessHeap(), 0, dev_pts
);
348 PSDRV_ResetClip(dev
);
353 /***********************************************************************
356 BOOL
PSDRV_PolyPolygon( PHYSDEV dev
, const POINT
* pts
, const INT
* counts
, UINT polygons
)
358 DWORD polygon
, total
;
364 for (polygon
= total
= 0; polygon
< polygons
; polygon
++) total
+= counts
[polygon
];
365 if (!(dev_pts
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(*dev_pts
) ))) return FALSE
;
366 memcpy( dev_pts
, pts
, total
* sizeof(*dev_pts
) );
367 LPtoDP( dev
->hdc
, dev_pts
, total
);
371 PSDRV_WriteSpool(dev
, "%PolyPolygon\n",13);
375 for(polygon
= 0; polygon
< polygons
; polygon
++) {
376 PSDRV_WriteMoveTo(dev
, pt
->x
, pt
->y
);
378 for(line
= 1; line
< counts
[polygon
]; line
++, pt
++)
379 PSDRV_WriteLineTo(dev
, pt
->x
, pt
->y
);
380 PSDRV_WriteClosePath(dev
);
382 HeapFree( GetProcessHeap(), 0, dev_pts
);
384 if(GetPolyFillMode( dev
->hdc
) == ALTERNATE
)
390 PSDRV_ResetClip(dev
);
395 /***********************************************************************
398 BOOL
PSDRV_PolyBezier( PHYSDEV dev
, const POINT
*pts
, DWORD count
)
405 if (!(dev_pts
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*dev_pts
) ))) return FALSE
;
406 memcpy( dev_pts
, pts
, count
* sizeof(*dev_pts
) );
407 LPtoDP( dev
->hdc
, dev_pts
, count
);
409 PSDRV_WriteSpool(dev
, "%PolyBezier\n",12);
412 PSDRV_WriteMoveTo(dev
, dev_pts
[0].x
, dev_pts
[0].y
);
413 for (i
= 1; i
< count
; i
+= 3) PSDRV_WriteCurveTo( dev
, dev_pts
+ i
);
415 PSDRV_ResetClip(dev
);
416 HeapFree( GetProcessHeap(), 0, dev_pts
);
421 /***********************************************************************
424 BOOL
PSDRV_PolyBezierTo( PHYSDEV dev
, const POINT
*pts
, DWORD count
)
431 count
++; /* add initial position */
432 if (!(dev_pts
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(*dev_pts
) ))) return FALSE
;
433 GetCurrentPositionEx( dev
->hdc
, dev_pts
);
434 memcpy( dev_pts
+ 1, pts
, (count
- 1) * sizeof(*dev_pts
) );
435 LPtoDP( dev
->hdc
, dev_pts
, count
);
437 PSDRV_WriteSpool(dev
, "%PolyBezier\n",12);
440 PSDRV_WriteMoveTo(dev
, dev_pts
[0].x
, dev_pts
[0].y
);
441 for (i
= 1; i
< count
; i
+= 3) PSDRV_WriteCurveTo( dev
, dev_pts
+ i
);
443 PSDRV_ResetClip(dev
);
444 HeapFree( GetProcessHeap(), 0, dev_pts
);
449 /***********************************************************************
452 COLORREF
PSDRV_SetPixel( PHYSDEV dev
, INT x
, INT y
, COLORREF color
)
459 LPtoDP( dev
->hdc
, &pt
, 1 );
462 /* we bracket the setcolor in gsave/grestore so that we don't trash
463 the current pen colour */
464 PSDRV_WriteGSave(dev
);
465 PSDRV_WriteRectangle( dev
, pt
.x
, pt
.y
, 0, 0 );
466 PSDRV_CreateColor( dev
, &pscolor
, color
);
467 PSDRV_WriteSetColor( dev
, &pscolor
);
468 PSDRV_WriteFill( dev
);
469 PSDRV_WriteGRestore(dev
);
470 PSDRV_ResetClip(dev
);
474 /***********************************************************************
477 BOOL
PSDRV_PaintRgn( PHYSDEV dev
, HRGN hrgn
)
479 RGNDATA
*rgndata
= NULL
;
483 TRACE("hdc=%p\n", dev
->hdc
);
485 size
= GetRegionData(hrgn
, 0, NULL
);
486 rgndata
= HeapAlloc( GetProcessHeap(), 0, size
);
488 ERR("Can't allocate buffer\n");
492 GetRegionData(hrgn
, size
, rgndata
);
493 if (rgndata
->rdh
.nCount
== 0)
496 LPtoDP(dev
->hdc
, (POINT
*)rgndata
->Buffer
, rgndata
->rdh
.nCount
* 2);
499 for(i
= 0, pRect
= (RECT
*)rgndata
->Buffer
; i
< rgndata
->rdh
.nCount
; i
++, pRect
++)
500 PSDRV_WriteRectangle(dev
, pRect
->left
, pRect
->top
, pRect
->right
- pRect
->left
, pRect
->bottom
- pRect
->top
);
503 PSDRV_WriteNewPath(dev
);
504 PSDRV_ResetClip(dev
);
507 HeapFree(GetProcessHeap(), 0, rgndata
);
511 static BOOL
paint_path( PHYSDEV dev
, BOOL stroke
, BOOL fill
)
516 int i
, size
= GetPath( dev
->hdc
, NULL
, NULL
, 0 );
518 if (size
== -1) return FALSE
;
519 if (!size
) return TRUE
;
520 points
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(*points
) );
521 types
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(*types
) );
522 if (!points
|| !types
) goto done
;
523 if (GetPath( dev
->hdc
, points
, types
, size
) == -1) goto done
;
524 LPtoDP( dev
->hdc
, points
, size
);
526 if (stroke
) PSDRV_SetPen(dev
);
528 for (i
= 0; i
< size
; i
++)
533 PSDRV_WriteMoveTo( dev
, points
[i
].x
, points
[i
].y
);
536 case PT_LINETO
| PT_CLOSEFIGURE
:
537 PSDRV_WriteLineTo( dev
, points
[i
].x
, points
[i
].y
);
538 if (types
[i
] & PT_CLOSEFIGURE
) PSDRV_WriteClosePath( dev
);
541 case PT_BEZIERTO
| PT_CLOSEFIGURE
:
542 PSDRV_WriteCurveTo( dev
, points
+ i
);
543 if (types
[i
] & PT_CLOSEFIGURE
) PSDRV_WriteClosePath( dev
);
548 if (fill
) PSDRV_Brush( dev
, GetPolyFillMode(dev
->hdc
) == ALTERNATE
);
549 if (stroke
) PSDRV_DrawLine(dev
);
550 else PSDRV_WriteNewPath(dev
);
551 PSDRV_ResetClip(dev
);
554 HeapFree( GetProcessHeap(), 0, points
);
555 HeapFree( GetProcessHeap(), 0, types
);
559 /***********************************************************************
562 BOOL
PSDRV_FillPath( PHYSDEV dev
)
564 return paint_path( dev
, FALSE
, TRUE
);
567 /***********************************************************************
568 * PSDRV_StrokeAndFillPath
570 BOOL
PSDRV_StrokeAndFillPath( PHYSDEV dev
)
572 return paint_path( dev
, TRUE
, TRUE
);
575 /***********************************************************************
578 BOOL
PSDRV_StrokePath( PHYSDEV dev
)
580 return paint_path( dev
, TRUE
, FALSE
);