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( print_ctx
*ctx
, INT width
)
47 LPtoDP( ctx
->hdc
, pt
, 2 );
48 return pt
[1].x
- pt
[0].x
;
51 /***********************************************************************
54 static void PSDRV_DrawLine( print_ctx
*ctx
)
59 if (ctx
->pen
.style
== PS_NULL
)
60 PSDRV_WriteNewPath(ctx
);
62 PSDRV_WriteStroke(ctx
);
65 /***********************************************************************
68 BOOL
PSDRV_LineTo(print_ctx
*ctx
, INT x
, INT y
)
72 TRACE("%d %d\n", x
, y
);
74 GetCurrentPositionEx( ctx
->hdc
, pt
);
77 LPtoDP( ctx
->hdc
, pt
, 2 );
82 PSDRV_WriteMoveTo(ctx
, pt
[0].x
, pt
[0].y
);
83 PSDRV_WriteLineTo(ctx
, pt
[1].x
, pt
[1].y
);
91 /***********************************************************************
94 BOOL
PSDRV_Rectangle( print_ctx
*ctx
, INT left
, INT top
, INT right
, INT bottom
)
98 TRACE("%d %d - %d %d\n", left
, top
, right
, bottom
);
100 SetRect(&rect
, left
, top
, right
, bottom
);
101 LPtoDP( ctx
->hdc
, (POINT
*)&rect
, 2 );
103 /* Windows does something truly hacky here. If we're in passthrough mode
104 and our rop is R2_NOP, then we output the string below. This is used in
105 Office 2k when inserting eps files */
106 if (ctx
->job
.passthrough_state
== passthrough_active
&& GetROP2(ctx
->hdc
) == R2_NOP
)
110 sprintf(buf
, "N %ld %ld %ld %ld B\n", rect
.right
- rect
.left
, rect
.bottom
- rect
.top
, rect
.left
, rect
.top
);
111 write_spool(ctx
, buf
, strlen(buf
));
112 ctx
->job
.passthrough_state
= passthrough_had_rect
;
119 PSDRV_WriteRectangle(ctx
, rect
.left
, rect
.top
, rect
.right
- rect
.left
,
120 rect
.bottom
- rect
.top
);
123 PSDRV_ResetClip(ctx
);
128 /***********************************************************************
131 BOOL
PSDRV_RoundRect( print_ctx
*ctx
, INT left
, INT top
, INT right
,
132 INT bottom
, INT ell_width
, INT ell_height
)
136 SetRect(&rect
[0], left
, top
, right
, bottom
);
137 SetRect(&rect
[1], 0, 0, ell_width
, ell_height
);
138 LPtoDP( ctx
->hdc
, (POINT
*)rect
, 4 );
142 right
= rect
[0].right
;
143 bottom
= rect
[0].bottom
;
144 if (left
> right
) { INT tmp
= left
; left
= right
; right
= tmp
; }
145 if (top
> bottom
) { INT tmp
= top
; top
= bottom
; bottom
= tmp
; }
147 ell_width
= rect
[1].right
- rect
[1].left
;
148 ell_height
= rect
[1].bottom
- rect
[1].top
;
149 if (ell_width
> right
- left
) ell_width
= right
- left
;
150 if (ell_height
> bottom
- top
) ell_height
= bottom
- top
;
152 PSDRV_WriteSpool(ctx
, "%RoundRect\n",11);
156 PSDRV_WriteMoveTo( ctx
, left
, top
+ ell_height
/2 );
157 PSDRV_WriteArc( ctx
, left
+ ell_width
/2, top
+ ell_height
/2, ell_width
,
158 ell_height
, 90.0, 180.0);
159 PSDRV_WriteLineTo( ctx
, right
- ell_width
/2, top
);
160 PSDRV_WriteArc( ctx
, right
- ell_width
/2, top
+ ell_height
/2, ell_width
,
161 ell_height
, 0.0, 90.0);
162 PSDRV_WriteLineTo( ctx
, right
, bottom
- ell_height
/2 );
163 PSDRV_WriteArc( ctx
, right
- ell_width
/2, bottom
- ell_height
/2, ell_width
,
164 ell_height
, -90.0, 0.0);
165 PSDRV_WriteLineTo( ctx
, right
- ell_width
/2, bottom
);
166 PSDRV_WriteArc( ctx
, left
+ ell_width
/2, bottom
- ell_height
/2, ell_width
,
167 ell_height
, 180.0, -90.0);
168 PSDRV_WriteClosePath( ctx
);
172 PSDRV_ResetClip(ctx
);
176 /***********************************************************************
179 * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
181 static BOOL
PSDRV_DrawArc( print_ctx
*ctx
, INT left
, INT top
,
182 INT right
, INT bottom
, INT xstart
, INT ystart
,
183 INT xend
, INT yend
, int lines
)
186 double start_angle
, end_angle
, ratio
;
190 SetRect(&rect
, left
, top
, right
, bottom
);
191 LPtoDP( ctx
->hdc
, (POINT
*)&rect
, 2 );
196 LPtoDP( ctx
->hdc
, &start
, 1 );
197 LPtoDP( ctx
->hdc
, &end
, 1 );
199 x
= (rect
.left
+ rect
.right
) / 2;
200 y
= (rect
.top
+ rect
.bottom
) / 2;
201 w
= rect
.right
- rect
.left
;
202 h
= rect
.bottom
- rect
.top
;
206 ratio
= ((double)w
)/h
;
208 /* angle is the angle after the rectangle is transformed to a square and is
209 measured anticlockwise from the +ve x-axis */
211 start_angle
= atan2((double)(y
- start
.y
) * ratio
, (double)(start
.x
- x
));
212 end_angle
= atan2((double)(y
- end
.y
) * ratio
, (double)(end
.x
- x
));
214 start_angle
*= 180.0 / M_PI
;
215 end_angle
*= 180.0 / M_PI
;
217 PSDRV_WriteSpool(ctx
,"%DrawArc\n", 9);
221 if(lines
== 2) /* pie */
222 PSDRV_WriteMoveTo(ctx
, x
, y
);
224 PSDRV_WriteNewPath( ctx
);
226 if(GetArcDirection(ctx
->hdc
) == AD_COUNTERCLOCKWISE
)
227 PSDRV_WriteArc(ctx
, x
, y
, w
, h
, start_angle
, end_angle
);
229 PSDRV_WriteArc(ctx
, x
, y
, w
, h
, end_angle
, start_angle
);
230 if(lines
== 1 || lines
== 2) { /* chord or pie */
231 PSDRV_WriteClosePath(ctx
);
235 PSDRV_ResetClip(ctx
);
241 /***********************************************************************
244 BOOL
PSDRV_Arc( print_ctx
*ctx
, INT left
, INT top
, INT right
, INT bottom
,
245 INT xstart
, INT ystart
, INT xend
, INT yend
)
247 return PSDRV_DrawArc( ctx
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
, 0 );
250 /***********************************************************************
253 BOOL
PSDRV_Chord( print_ctx
*ctx
, INT left
, INT top
, INT right
, INT bottom
,
254 INT xstart
, INT ystart
, INT xend
, INT yend
)
256 return PSDRV_DrawArc( ctx
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
, 1 );
260 /***********************************************************************
263 BOOL
PSDRV_Pie( print_ctx
*ctx
, INT left
, INT top
, INT right
, INT bottom
,
264 INT xstart
, INT ystart
, INT xend
, INT yend
)
266 return PSDRV_DrawArc( ctx
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
, 2 );
270 /***********************************************************************
273 BOOL
PSDRV_Ellipse( print_ctx
*ctx
, 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( ctx
->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(ctx
, "%Ellipse\n", 9);
292 PSDRV_WriteNewPath(ctx
);
293 PSDRV_WriteArc(ctx
, x
, y
, w
, h
, 0.0, 360.0);
294 PSDRV_WriteClosePath(ctx
);
297 PSDRV_ResetClip(ctx
);
302 /***********************************************************************
305 BOOL
PSDRV_PolyPolyline( print_ctx
*ctx
, 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( ctx
->hdc
, dev_pts
, total
);
319 PSDRV_WriteSpool(ctx
, "%PolyPolyline\n",14);
323 for(polyline
= 0; polyline
< polylines
; polyline
++) {
324 PSDRV_WriteMoveTo(ctx
, pt
->x
, pt
->y
);
326 for(line
= 1; line
< counts
[polyline
]; line
++, pt
++)
327 PSDRV_WriteLineTo(ctx
, pt
->x
, pt
->y
);
329 HeapFree( GetProcessHeap(), 0, dev_pts
);
332 PSDRV_ResetClip(ctx
);
337 /***********************************************************************
340 BOOL
PSDRV_PolyPolygon( print_ctx
*ctx
, 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( ctx
->hdc
, dev_pts
, total
);
355 PSDRV_WriteSpool(ctx
, "%PolyPolygon\n",13);
359 for(polygon
= 0; polygon
< polygons
; polygon
++) {
360 PSDRV_WriteMoveTo(ctx
, pt
->x
, pt
->y
);
362 for(line
= 1; line
< counts
[polygon
]; line
++, pt
++)
363 PSDRV_WriteLineTo(ctx
, pt
->x
, pt
->y
);
364 PSDRV_WriteClosePath(ctx
);
366 HeapFree( GetProcessHeap(), 0, dev_pts
);
368 if(GetPolyFillMode( ctx
->hdc
) == ALTERNATE
)
374 PSDRV_ResetClip(ctx
);
378 static BOOL
poly_bezier( print_ctx
*ctx
, const POINT
*pt0
, const POINT
*pts
, DWORD count
)
386 LPtoDP( ctx
->hdc
, dev_pts
, 1 );
388 PSDRV_WriteSpool( ctx
, "%PolyBezier\n", 12 );
390 PSDRV_SetClip( ctx
);
391 PSDRV_WriteMoveTo( ctx
, dev_pts
[0].x
, dev_pts
[0].y
);
392 for (i
= 0; i
< count
; i
+= 3)
394 memcpy( dev_pts
, pts
, sizeof(dev_pts
) );
395 LPtoDP( ctx
->hdc
, dev_pts
, 3 );
396 PSDRV_WriteCurveTo( ctx
, dev_pts
);
399 PSDRV_ResetClip(ctx
);
403 /***********************************************************************
406 BOOL
PSDRV_PolyBezier( print_ctx
*ctx
, const POINT
*pts
, DWORD count
)
408 return poly_bezier( ctx
, pts
, pts
+ 1, count
- 1 );
412 /***********************************************************************
415 BOOL
PSDRV_PolyBezierTo( print_ctx
*ctx
, const POINT
*pts
, DWORD count
)
419 GetCurrentPositionEx( ctx
->hdc
, &pt0
);
420 return poly_bezier( ctx
, &pt0
, pts
, count
);
424 /***********************************************************************
427 COLORREF
PSDRV_SetPixel( print_ctx
*ctx
, INT x
, INT y
, COLORREF color
)
434 LPtoDP( ctx
->hdc
, &pt
, 1 );
437 /* we bracket the setcolor in gsave/grestore so that we don't trash
438 the current pen colour */
439 PSDRV_WriteGSave(ctx
);
440 PSDRV_WriteRectangle( ctx
, pt
.x
, pt
.y
, 1, 1 );
441 PSDRV_CreateColor( ctx
, &pscolor
, color
);
442 PSDRV_WriteSetColor( ctx
, &pscolor
);
443 PSDRV_WriteFill( ctx
);
444 PSDRV_WriteGRestore(ctx
);
445 PSDRV_ResetClip(ctx
);
449 /***********************************************************************
452 BOOL
PSDRV_PaintRgn( print_ctx
*ctx
, HRGN hrgn
)
454 RGNDATA
*rgndata
= NULL
;
458 TRACE("hdc=%p\n", ctx
->hdc
);
460 size
= GetRegionData(hrgn
, 0, NULL
);
461 rgndata
= HeapAlloc( GetProcessHeap(), 0, size
);
463 ERR("Can't allocate buffer\n");
467 GetRegionData(hrgn
, size
, rgndata
);
468 if (rgndata
->rdh
.nCount
== 0)
471 LPtoDP(ctx
->hdc
, (POINT
*)rgndata
->Buffer
, rgndata
->rdh
.nCount
* 2);
474 for(i
= 0, pRect
= (RECT
*)rgndata
->Buffer
; i
< rgndata
->rdh
.nCount
; i
++, pRect
++)
475 PSDRV_WriteRectangle(ctx
, pRect
->left
, pRect
->top
, pRect
->right
- pRect
->left
, pRect
->bottom
- pRect
->top
);
478 PSDRV_WriteNewPath(ctx
);
479 PSDRV_ResetClip(ctx
);
482 HeapFree(GetProcessHeap(), 0, rgndata
);
486 static BOOL
paint_path( print_ctx
*ctx
, BOOL stroke
, BOOL fill
)
490 int i
, size
= GetPath( ctx
->hdc
, NULL
, NULL
, 0 );
492 if (size
== -1) return FALSE
;
495 AbortPath( ctx
->hdc
);
498 points
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(*points
) );
499 types
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(*types
) );
500 if (!points
|| !types
) goto done
;
501 if (GetPath( ctx
->hdc
, points
, types
, size
) == -1) goto done
;
502 LPtoDP( ctx
->hdc
, points
, size
);
504 if (stroke
) PSDRV_SetPen(ctx
);
506 for (i
= 0; i
< size
; i
++)
511 PSDRV_WriteMoveTo( ctx
, points
[i
].x
, points
[i
].y
);
514 case PT_LINETO
| PT_CLOSEFIGURE
:
515 PSDRV_WriteLineTo( ctx
, points
[i
].x
, points
[i
].y
);
516 if (types
[i
] & PT_CLOSEFIGURE
) PSDRV_WriteClosePath( ctx
);
519 case PT_BEZIERTO
| PT_CLOSEFIGURE
:
520 PSDRV_WriteCurveTo( ctx
, points
+ i
);
521 if (types
[i
] & PT_CLOSEFIGURE
) PSDRV_WriteClosePath( ctx
);
526 if (fill
) PSDRV_Brush( ctx
, GetPolyFillMode(ctx
->hdc
) == ALTERNATE
);
527 if (stroke
) PSDRV_DrawLine(ctx
);
528 else PSDRV_WriteNewPath(ctx
);
529 PSDRV_ResetClip(ctx
);
530 AbortPath( ctx
->hdc
);
533 HeapFree( GetProcessHeap(), 0, points
);
534 HeapFree( GetProcessHeap(), 0, types
);
538 /***********************************************************************
541 BOOL
PSDRV_FillPath( print_ctx
*ctx
)
543 return paint_path( ctx
, FALSE
, TRUE
);
546 /***********************************************************************
547 * PSDRV_StrokeAndFillPath
549 BOOL
PSDRV_StrokeAndFillPath( print_ctx
*ctx
)
551 return paint_path( ctx
, TRUE
, TRUE
);
554 /***********************************************************************
557 BOOL
PSDRV_StrokePath( print_ctx
*ctx
)
559 return paint_path( ctx
, TRUE
, FALSE
);