wined3d: Remove the redundant "pow2_width" and "pow2_height" fields.
[wine.git] / dlls / wineps.drv / graphics.c
blob0b6cb0c67f18ee114a7d20580ab28a62e4e4ab45
1 /*
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
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <math.h>
25 #include <float.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "psdrv.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
34 /***********************************************************************
35 * PSDRV_XWStoDS
37 * Performs a world-to-viewport transformation on the specified width.
39 INT PSDRV_XWStoDS( print_ctx *ctx, INT width )
41 POINT pt[2];
43 pt[0].x = 0;
44 pt[0].y = 0;
45 pt[1].x = width;
46 pt[1].y = 0;
47 LPtoDP( ctx->hdc, pt, 2 );
48 return pt[1].x - pt[0].x;
51 /***********************************************************************
52 * PSDRV_DrawLine
54 static void PSDRV_DrawLine( print_ctx *ctx )
56 if(ctx->pathdepth)
57 return;
59 if (ctx->pen.style == PS_NULL)
60 PSDRV_WriteNewPath(ctx);
61 else
62 PSDRV_WriteStroke(ctx);
65 /***********************************************************************
66 * PSDRV_LineTo
68 BOOL PSDRV_LineTo(print_ctx *ctx, INT x, INT y)
70 POINT pt[2];
72 TRACE("%d %d\n", x, y);
74 GetCurrentPositionEx( ctx->hdc, pt );
75 pt[1].x = x;
76 pt[1].y = y;
77 LPtoDP( ctx->hdc, pt, 2 );
79 PSDRV_SetPen(ctx);
81 PSDRV_SetClip(ctx);
82 PSDRV_WriteMoveTo(ctx, pt[0].x, pt[0].y );
83 PSDRV_WriteLineTo(ctx, pt[1].x, pt[1].y );
84 PSDRV_DrawLine(ctx);
85 PSDRV_ResetClip(ctx);
87 return TRUE;
91 /***********************************************************************
92 * PSDRV_Rectangle
94 BOOL PSDRV_Rectangle( print_ctx *ctx, INT left, INT top, INT right, INT bottom )
96 RECT rect;
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)
108 char buf[256];
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;
113 return TRUE;
116 PSDRV_SetPen(ctx);
118 PSDRV_SetClip(ctx);
119 PSDRV_WriteRectangle(ctx, rect.left, rect.top, rect.right - rect.left,
120 rect.bottom - rect.top );
121 PSDRV_Brush(ctx,0);
122 PSDRV_DrawLine(ctx);
123 PSDRV_ResetClip(ctx);
124 return TRUE;
128 /***********************************************************************
129 * PSDRV_RoundRect
131 BOOL PSDRV_RoundRect( print_ctx *ctx, INT left, INT top, INT right,
132 INT bottom, INT ell_width, INT ell_height )
134 RECT rect[2];
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 );
140 left = rect[0].left;
141 top = rect[0].top;
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);
153 PSDRV_SetPen(ctx);
155 PSDRV_SetClip(ctx);
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 );
170 PSDRV_Brush(ctx,0);
171 PSDRV_DrawLine(ctx);
172 PSDRV_ResetClip(ctx);
173 return TRUE;
176 /***********************************************************************
177 * PSDRV_DrawArc
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 )
185 INT x, y, h, w;
186 double start_angle, end_angle, ratio;
187 RECT rect;
188 POINT start, end;
190 SetRect(&rect, left, top, right, bottom);
191 LPtoDP( ctx->hdc, (POINT *)&rect, 2 );
192 start.x = xstart;
193 start.y = ystart;
194 end.x = xend;
195 end.y = yend;
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;
204 if(w < 0) w = -w;
205 if(h < 0) h = -h;
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);
218 PSDRV_SetPen(ctx);
220 PSDRV_SetClip(ctx);
221 if(lines == 2) /* pie */
222 PSDRV_WriteMoveTo(ctx, x, y);
223 else
224 PSDRV_WriteNewPath( ctx );
226 if(GetArcDirection(ctx->hdc) == AD_COUNTERCLOCKWISE)
227 PSDRV_WriteArc(ctx, x, y, w, h, start_angle, end_angle);
228 else
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);
232 PSDRV_Brush(ctx,0);
234 PSDRV_DrawLine(ctx);
235 PSDRV_ResetClip(ctx);
237 return TRUE;
241 /***********************************************************************
242 * PSDRV_Arc
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 /***********************************************************************
251 * PSDRV_Chord
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 /***********************************************************************
261 * PSDRV_Pie
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 /***********************************************************************
271 * PSDRV_Ellipse
273 BOOL PSDRV_Ellipse( print_ctx *ctx, INT left, INT top, INT right, INT bottom)
275 INT x, y, w, h;
276 RECT rect;
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);
289 PSDRV_SetPen(ctx);
291 PSDRV_SetClip(ctx);
292 PSDRV_WriteNewPath(ctx);
293 PSDRV_WriteArc(ctx, x, y, w, h, 0.0, 360.0);
294 PSDRV_WriteClosePath(ctx);
295 PSDRV_Brush(ctx,0);
296 PSDRV_DrawLine(ctx);
297 PSDRV_ResetClip(ctx);
298 return TRUE;
302 /***********************************************************************
303 * PSDRV_PolyPolyline
305 BOOL PSDRV_PolyPolyline( print_ctx *ctx, const POINT* pts, const DWORD* counts, DWORD polylines )
307 DWORD polyline, line, total;
308 POINT *dev_pts, *pt;
310 TRACE("\n");
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 );
317 pt = dev_pts;
319 PSDRV_WriteSpool(ctx, "%PolyPolyline\n",14);
320 PSDRV_SetPen(ctx);
321 PSDRV_SetClip(ctx);
323 for(polyline = 0; polyline < polylines; polyline++) {
324 PSDRV_WriteMoveTo(ctx, pt->x, pt->y);
325 pt++;
326 for(line = 1; line < counts[polyline]; line++, pt++)
327 PSDRV_WriteLineTo(ctx, pt->x, pt->y);
329 HeapFree( GetProcessHeap(), 0, dev_pts );
331 PSDRV_DrawLine(ctx);
332 PSDRV_ResetClip(ctx);
333 return TRUE;
337 /***********************************************************************
338 * PSDRV_PolyPolygon
340 BOOL PSDRV_PolyPolygon( print_ctx *ctx, const POINT* pts, const INT* counts, UINT polygons )
342 DWORD polygon, total;
343 INT line;
344 POINT *dev_pts, *pt;
346 TRACE("\n");
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 );
353 pt = dev_pts;
355 PSDRV_WriteSpool(ctx, "%PolyPolygon\n",13);
356 PSDRV_SetPen(ctx);
357 PSDRV_SetClip(ctx);
359 for(polygon = 0; polygon < polygons; polygon++) {
360 PSDRV_WriteMoveTo(ctx, pt->x, pt->y);
361 pt++;
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)
369 PSDRV_Brush(ctx, 1);
370 else /* WINDING */
371 PSDRV_Brush(ctx, 0);
373 PSDRV_DrawLine(ctx);
374 PSDRV_ResetClip(ctx);
375 return TRUE;
378 static BOOL poly_bezier( print_ctx *ctx, const POINT *pt0, const POINT *pts, DWORD count)
380 POINT dev_pts[3];
381 DWORD i;
383 TRACE( "\n" );
385 dev_pts[0] = *pt0;
386 LPtoDP( ctx->hdc, dev_pts, 1 );
388 PSDRV_WriteSpool( ctx, "%PolyBezier\n", 12 );
389 PSDRV_SetPen( ctx );
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 );
398 PSDRV_DrawLine(ctx);
399 PSDRV_ResetClip(ctx);
400 return TRUE;
403 /***********************************************************************
404 * PSDRV_PolyBezier
406 BOOL PSDRV_PolyBezier( print_ctx *ctx, const POINT *pts, DWORD count )
408 return poly_bezier( ctx, pts, pts + 1, count - 1 );
412 /***********************************************************************
413 * PSDRV_PolyBezierTo
415 BOOL PSDRV_PolyBezierTo( print_ctx *ctx, const POINT *pts, DWORD count )
417 POINT pt0;
419 GetCurrentPositionEx( ctx->hdc, &pt0 );
420 return poly_bezier( ctx, &pt0, pts, count );
424 /***********************************************************************
425 * PSDRV_SetPixel
427 COLORREF PSDRV_SetPixel( print_ctx *ctx, INT x, INT y, COLORREF color )
429 PSCOLOR pscolor;
430 POINT pt;
432 pt.x = x;
433 pt.y = y;
434 LPtoDP( ctx->hdc, &pt, 1 );
436 PSDRV_SetClip(ctx);
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);
446 return color;
449 /***********************************************************************
450 * PSDRV_PaintRgn
452 BOOL PSDRV_PaintRgn( print_ctx *ctx, HRGN hrgn )
454 RGNDATA *rgndata = NULL;
455 RECT *pRect;
456 DWORD size, i;
458 TRACE("hdc=%p\n", ctx->hdc);
460 size = GetRegionData(hrgn, 0, NULL);
461 rgndata = HeapAlloc( GetProcessHeap(), 0, size );
462 if(!rgndata) {
463 ERR("Can't allocate buffer\n");
464 return FALSE;
467 GetRegionData(hrgn, size, rgndata);
468 if (rgndata->rdh.nCount == 0)
469 goto end;
471 LPtoDP(ctx->hdc, (POINT*)rgndata->Buffer, rgndata->rdh.nCount * 2);
473 PSDRV_SetClip(ctx);
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);
477 PSDRV_Brush(ctx, 0);
478 PSDRV_WriteNewPath(ctx);
479 PSDRV_ResetClip(ctx);
481 end:
482 HeapFree(GetProcessHeap(), 0, rgndata);
483 return TRUE;
486 static BOOL paint_path( print_ctx *ctx, BOOL stroke, BOOL fill )
488 POINT *points;
489 BYTE *types;
490 int i, size = GetPath( ctx->hdc, NULL, NULL, 0 );
492 if (size == -1) return FALSE;
493 if (!size)
495 AbortPath( ctx->hdc );
496 return TRUE;
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);
505 PSDRV_SetClip(ctx);
506 for (i = 0; i < size; i++)
508 switch (types[i])
510 case PT_MOVETO:
511 PSDRV_WriteMoveTo( ctx, points[i].x, points[i].y );
512 break;
513 case PT_LINETO:
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 );
517 break;
518 case PT_BEZIERTO:
519 case PT_BEZIERTO | PT_CLOSEFIGURE:
520 PSDRV_WriteCurveTo( ctx, points + i );
521 if (types[i] & PT_CLOSEFIGURE) PSDRV_WriteClosePath( ctx );
522 i += 2;
523 break;
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 );
532 done:
533 HeapFree( GetProcessHeap(), 0, points );
534 HeapFree( GetProcessHeap(), 0, types );
535 return TRUE;
538 /***********************************************************************
539 * PSDRV_FillPath
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 /***********************************************************************
555 * PSDRV_StrokePath
557 BOOL PSDRV_StrokePath( print_ctx *ctx )
559 return paint_path( ctx, TRUE, FALSE );