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 /* Windows does something truly hacky here. If we're in passthrough mode
128 and our rop is R2_NOP, then we output the string below. This is used in
129 Office 2k when inserting eps files */
130 if(physDev
->job
.in_passthrough
&& !physDev
->job
.had_passthrough_rect
&& GetROP2(physDev
->hdc
) == R2_NOP
) {
132 sprintf(buf
, "N %ld %ld %ld %ld B\n", rect
.right
- rect
.left
, rect
.bottom
- rect
.top
, rect
.left
, rect
.top
);
133 WriteSpool16(physDev
->job
.hJob
, buf
, strlen(buf
));
134 physDev
->job
.had_passthrough_rect
= TRUE
;
138 PSDRV_SetPen(physDev
);
140 PSDRV_SetClip(physDev
);
141 PSDRV_WriteRectangle(physDev
, rect
.left
, rect
.top
, rect
.right
- rect
.left
,
142 rect
.bottom
- rect
.top
);
143 PSDRV_Brush(physDev
,0);
144 PSDRV_DrawLine(physDev
);
145 PSDRV_ResetClip(physDev
);
150 /***********************************************************************
153 BOOL
PSDRV_RoundRect( PSDRV_PDEVICE
*physDev
, INT left
, INT top
, INT right
,
154 INT bottom
, INT ell_width
, INT ell_height
)
160 rect
[0].right
= right
;
161 rect
[0].bottom
= bottom
;
164 rect
[1].right
= ell_width
;
165 rect
[1].bottom
= ell_height
;
166 LPtoDP( physDev
->hdc
, (POINT
*)rect
, 4 );
170 right
= rect
[0].right
;
171 bottom
= rect
[0].bottom
;
172 if (left
> right
) { INT tmp
= left
; left
= right
; right
= tmp
; }
173 if (top
> bottom
) { INT tmp
= top
; top
= bottom
; bottom
= tmp
; }
175 ell_width
= rect
[1].right
- rect
[1].left
;
176 ell_height
= rect
[1].bottom
- rect
[1].top
;
177 if (ell_width
> right
- left
) ell_width
= right
- left
;
178 if (ell_height
> bottom
- top
) ell_height
= bottom
- top
;
180 PSDRV_WriteSpool(physDev
, "%RoundRect\n",11);
181 PSDRV_SetPen(physDev
);
183 PSDRV_SetClip(physDev
);
184 PSDRV_WriteMoveTo( physDev
, left
, top
+ ell_height
/2 );
185 PSDRV_WriteArc( physDev
, left
+ ell_width
/2, top
+ ell_height
/2, ell_width
,
186 ell_height
, 90.0, 180.0);
187 PSDRV_WriteLineTo( physDev
, right
- ell_width
/2, top
);
188 PSDRV_WriteArc( physDev
, right
- ell_width
/2, top
+ ell_height
/2, ell_width
,
189 ell_height
, 0.0, 90.0);
190 PSDRV_WriteLineTo( physDev
, right
, bottom
- ell_height
/2 );
191 PSDRV_WriteArc( physDev
, right
- ell_width
/2, bottom
- ell_height
/2, ell_width
,
192 ell_height
, -90.0, 0.0);
193 PSDRV_WriteLineTo( physDev
, right
- ell_width
/2, bottom
);
194 PSDRV_WriteArc( physDev
, left
+ ell_width
/2, bottom
- ell_height
/2, ell_width
,
195 ell_height
, 180.0, -90.0);
196 PSDRV_WriteClosePath( physDev
);
198 PSDRV_Brush(physDev
,0);
199 PSDRV_DrawLine(physDev
);
200 PSDRV_ResetClip(physDev
);
204 /***********************************************************************
207 * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
209 static BOOL
PSDRV_DrawArc( PSDRV_PDEVICE
*physDev
, INT left
, INT top
,
210 INT right
, INT bottom
, INT xstart
, INT ystart
,
211 INT xend
, INT yend
, int lines
)
214 double start_angle
, end_angle
, ratio
;
221 rect
.bottom
= bottom
;
222 LPtoDP( physDev
->hdc
, (POINT
*)&rect
, 2 );
227 LPtoDP( physDev
->hdc
, &start
, 1 );
228 LPtoDP( physDev
->hdc
, &end
, 1 );
230 x
= (rect
.left
+ rect
.right
) / 2;
231 y
= (rect
.top
+ rect
.bottom
) / 2;
232 w
= rect
.right
- rect
.left
;
233 h
= rect
.bottom
- rect
.top
;
237 ratio
= ((double)w
)/h
;
239 /* angle is the angle after the rectangle is transformed to a square and is
240 measured anticlockwise from the +ve x-axis */
242 start_angle
= atan2((double)(y
- start
.y
) * ratio
, (double)(start
.x
- x
));
243 end_angle
= atan2((double)(y
- end
.y
) * ratio
, (double)(end
.x
- x
));
245 start_angle
*= 180.0 / PI
;
246 end_angle
*= 180.0 / PI
;
248 PSDRV_WriteSpool(physDev
,"%DrawArc\n", 9);
249 PSDRV_SetPen(physDev
);
251 PSDRV_SetClip(physDev
);
252 if(lines
== 2) /* pie */
253 PSDRV_WriteMoveTo(physDev
, x
, y
);
255 PSDRV_WriteNewPath( physDev
);
257 PSDRV_WriteArc(physDev
, x
, y
, w
, h
, start_angle
, end_angle
);
258 if(lines
== 1 || lines
== 2) { /* chord or pie */
259 PSDRV_WriteClosePath(physDev
);
260 PSDRV_Brush(physDev
,0);
262 PSDRV_DrawLine(physDev
);
263 PSDRV_ResetClip(physDev
);
269 /***********************************************************************
272 BOOL
PSDRV_Arc( PSDRV_PDEVICE
*physDev
, INT left
, INT top
, INT right
, INT bottom
,
273 INT xstart
, INT ystart
, INT xend
, INT yend
)
275 return PSDRV_DrawArc( physDev
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
, 0 );
278 /***********************************************************************
281 BOOL
PSDRV_Chord( PSDRV_PDEVICE
*physDev
, INT left
, INT top
, INT right
, INT bottom
,
282 INT xstart
, INT ystart
, INT xend
, INT yend
)
284 return PSDRV_DrawArc( physDev
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
, 1 );
288 /***********************************************************************
291 BOOL
PSDRV_Pie( PSDRV_PDEVICE
*physDev
, INT left
, INT top
, INT right
, INT bottom
,
292 INT xstart
, INT ystart
, INT xend
, INT yend
)
294 return PSDRV_DrawArc( physDev
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
, 2 );
298 /***********************************************************************
301 BOOL
PSDRV_Ellipse( PSDRV_PDEVICE
*physDev
, INT left
, INT top
, INT right
, INT bottom
)
306 TRACE("%d %d - %d %d\n", left
, top
, right
, bottom
);
311 rect
.bottom
= bottom
;
312 LPtoDP( physDev
->hdc
, (POINT
*)&rect
, 2 );
314 x
= (rect
.left
+ rect
.right
) / 2;
315 y
= (rect
.top
+ rect
.bottom
) / 2;
316 w
= rect
.right
- rect
.left
;
317 h
= rect
.bottom
- rect
.top
;
319 PSDRV_WriteSpool(physDev
, "%Ellipse\n", 9);
320 PSDRV_SetPen(physDev
);
322 PSDRV_SetClip(physDev
);
323 PSDRV_WriteNewPath(physDev
);
324 PSDRV_WriteArc(physDev
, x
, y
, w
, h
, 0.0, 360.0);
325 PSDRV_WriteClosePath(physDev
);
326 PSDRV_Brush(physDev
,0);
327 PSDRV_DrawLine(physDev
);
328 PSDRV_ResetClip(physDev
);
333 /***********************************************************************
336 BOOL
PSDRV_PolyPolyline( PSDRV_PDEVICE
*physDev
, const POINT
* pts
, const DWORD
* counts
,
339 DWORD polyline
, line
, total
;
344 for (polyline
= total
= 0; polyline
< polylines
; polyline
++) total
+= counts
[polyline
];
345 if (!(dev_pts
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(*dev_pts
) ))) return FALSE
;
346 memcpy( dev_pts
, pts
, total
* sizeof(*dev_pts
) );
347 LPtoDP( physDev
->hdc
, dev_pts
, total
);
351 PSDRV_WriteSpool(physDev
, "%PolyPolyline\n",14);
352 PSDRV_SetPen(physDev
);
353 PSDRV_SetClip(physDev
);
355 for(polyline
= 0; polyline
< polylines
; polyline
++) {
356 PSDRV_WriteMoveTo(physDev
, pt
->x
, pt
->y
);
358 for(line
= 1; line
< counts
[polyline
]; line
++, pt
++)
359 PSDRV_WriteLineTo(physDev
, pt
->x
, pt
->y
);
361 HeapFree( GetProcessHeap(), 0, dev_pts
);
363 PSDRV_DrawLine(physDev
);
364 PSDRV_ResetClip(physDev
);
369 /***********************************************************************
372 BOOL
PSDRV_Polyline( PSDRV_PDEVICE
*physDev
, const POINT
* pt
, INT count
)
374 return PSDRV_PolyPolyline( physDev
, pt
, (LPDWORD
) &count
, 1 );
378 /***********************************************************************
381 BOOL
PSDRV_PolyPolygon( PSDRV_PDEVICE
*physDev
, const POINT
* pts
, const INT
* counts
,
384 DWORD polygon
, total
;
390 for (polygon
= total
= 0; polygon
< polygons
; polygon
++) total
+= counts
[polygon
];
391 if (!(dev_pts
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(*dev_pts
) ))) return FALSE
;
392 memcpy( dev_pts
, pts
, total
* sizeof(*dev_pts
) );
393 LPtoDP( physDev
->hdc
, dev_pts
, total
);
397 PSDRV_WriteSpool(physDev
, "%PolyPolygon\n",13);
398 PSDRV_SetPen(physDev
);
399 PSDRV_SetClip(physDev
);
401 for(polygon
= 0; polygon
< polygons
; polygon
++) {
402 PSDRV_WriteMoveTo(physDev
, pt
->x
, pt
->y
);
404 for(line
= 1; line
< counts
[polygon
]; line
++, pt
++)
405 PSDRV_WriteLineTo(physDev
, pt
->x
, pt
->y
);
406 PSDRV_WriteClosePath(physDev
);
408 HeapFree( GetProcessHeap(), 0, dev_pts
);
410 if(GetPolyFillMode( physDev
->hdc
) == ALTERNATE
)
411 PSDRV_Brush(physDev
, 1);
413 PSDRV_Brush(physDev
, 0);
415 PSDRV_DrawLine(physDev
);
416 PSDRV_ResetClip(physDev
);
421 /***********************************************************************
424 BOOL
PSDRV_Polygon( PSDRV_PDEVICE
*physDev
, const POINT
* pt
, INT count
)
426 return PSDRV_PolyPolygon( physDev
, pt
, &count
, 1 );
430 /***********************************************************************
433 COLORREF
PSDRV_SetPixel( PSDRV_PDEVICE
*physDev
, INT x
, INT y
, COLORREF color
)
440 LPtoDP( physDev
->hdc
, &pt
, 1 );
442 PSDRV_SetClip(physDev
);
443 /* we bracket the setcolor in gsave/grestore so that we don't trash
444 the current pen colour */
445 PSDRV_WriteGSave(physDev
);
446 PSDRV_WriteRectangle( physDev
, pt
.x
, pt
.y
, 0, 0 );
447 PSDRV_CreateColor( physDev
, &pscolor
, color
);
448 PSDRV_WriteSetColor( physDev
, &pscolor
);
449 PSDRV_WriteFill( physDev
);
450 PSDRV_WriteGRestore(physDev
);
451 PSDRV_ResetClip(physDev
);
455 /***********************************************************************
458 BOOL
PSDRV_PaintRgn( PSDRV_PDEVICE
*physDev
, HRGN hrgn
)
461 RGNDATA
*rgndata
= NULL
;
465 TRACE("hdc=%p\n", physDev
->hdc
);
467 size
= GetRegionData(hrgn
, 0, NULL
);
468 rgndata
= HeapAlloc( GetProcessHeap(), 0, size
);
470 ERR("Can't allocate buffer\n");
474 GetRegionData(hrgn
, size
, rgndata
);
475 if (rgndata
->rdh
.nCount
== 0)
478 LPtoDP(physDev
->hdc
, (POINT
*)rgndata
->Buffer
, rgndata
->rdh
.nCount
* 2);
480 PSDRV_SetClip(physDev
);
481 PSDRV_WriteNewPath(physDev
);
482 for(i
= 0, pRect
= (RECT
*)rgndata
->Buffer
; i
< rgndata
->rdh
.nCount
; i
++, pRect
++)
483 PSDRV_WriteRectangle(physDev
, pRect
->left
, pRect
->top
, pRect
->right
- pRect
->left
, pRect
->bottom
- pRect
->top
);
485 PSDRV_Brush(physDev
, 0);
486 PSDRV_ResetClip(physDev
);
489 HeapFree(GetProcessHeap(), 0, rgndata
);