gdi32: Update pixel colours when the colour table changes.
[wine/multimedia.git] / dlls / wineps.drv / graphics.c
blob95e9653bd95d729e8275fa2b03dfdc97c8cb7256
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 "config.h"
23 #include <stdio.h>
24 #include <string.h>
25 #include <math.h>
26 #if defined(HAVE_FLOAT_H)
27 # include <float.h>
28 #endif
29 #if !defined(PI)
30 # define PI M_PI
31 #endif
32 #include "psdrv.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
37 /***********************************************************************
38 * PSDRV_XWStoDS
40 * Performs a world-to-viewport transformation on the specified width.
42 INT PSDRV_XWStoDS( PSDRV_PDEVICE *physDev, INT width )
44 POINT pt[2];
46 pt[0].x = 0;
47 pt[0].y = 0;
48 pt[1].x = width;
49 pt[1].y = 0;
50 LPtoDP( physDev->hdc, pt, 2 );
51 return pt[1].x - pt[0].x;
54 /***********************************************************************
55 * PSDRV_DrawLine
57 static void PSDRV_DrawLine( PSDRV_PDEVICE *physDev )
59 if(physDev->pathdepth)
60 return;
62 if (physDev->pen.style == PS_NULL)
63 PSDRV_WriteNewPath(physDev);
64 else
65 PSDRV_WriteStroke(physDev);
68 /***********************************************************************
69 * PSDRV_LineTo
71 BOOL CDECL PSDRV_LineTo(PSDRV_PDEVICE *physDev, INT x, INT y)
73 POINT pt[2];
75 TRACE("%d %d\n", x, y);
77 GetCurrentPositionEx( physDev->hdc, pt );
78 pt[1].x = x;
79 pt[1].y = y;
80 LPtoDP( physDev->hdc, pt, 2 );
82 PSDRV_SetPen(physDev);
84 PSDRV_SetClip(physDev);
85 PSDRV_WriteMoveTo(physDev, pt[0].x, pt[0].y );
86 PSDRV_WriteLineTo(physDev, pt[1].x, pt[1].y );
87 PSDRV_DrawLine(physDev);
88 PSDRV_ResetClip(physDev);
90 return TRUE;
94 /***********************************************************************
95 * PSDRV_Rectangle
97 BOOL CDECL PSDRV_Rectangle( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom )
99 RECT rect;
101 TRACE("%d %d - %d %d\n", left, top, right, bottom);
103 rect.left = left;
104 rect.top = top;
105 rect.right = right;
106 rect.bottom = bottom;
107 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
109 /* Windows does something truly hacky here. If we're in passthrough mode
110 and our rop is R2_NOP, then we output the string below. This is used in
111 Office 2k when inserting eps files */
112 if(physDev->job.in_passthrough && !physDev->job.had_passthrough_rect && GetROP2(physDev->hdc) == R2_NOP) {
113 char buf[256];
114 sprintf(buf, "N %d %d %d %d B\n", rect.right - rect.left, rect.bottom - rect.top, rect.left, rect.top);
115 write_spool(physDev, buf, strlen(buf));
116 physDev->job.had_passthrough_rect = TRUE;
117 return TRUE;
120 PSDRV_SetPen(physDev);
122 PSDRV_SetClip(physDev);
123 PSDRV_WriteRectangle(physDev, rect.left, rect.top, rect.right - rect.left,
124 rect.bottom - rect.top );
125 PSDRV_Brush(physDev,0);
126 PSDRV_DrawLine(physDev);
127 PSDRV_ResetClip(physDev);
128 return TRUE;
132 /***********************************************************************
133 * PSDRV_RoundRect
135 BOOL CDECL PSDRV_RoundRect( PSDRV_PDEVICE *physDev, INT left, INT top, INT right,
136 INT bottom, INT ell_width, INT ell_height )
138 RECT rect[2];
140 rect[0].left = left;
141 rect[0].top = top;
142 rect[0].right = right;
143 rect[0].bottom = bottom;
144 rect[1].left = 0;
145 rect[1].top = 0;
146 rect[1].right = ell_width;
147 rect[1].bottom = ell_height;
148 LPtoDP( physDev->hdc, (POINT *)rect, 4 );
150 left = rect[0].left;
151 top = rect[0].top;
152 right = rect[0].right;
153 bottom = rect[0].bottom;
154 if (left > right) { INT tmp = left; left = right; right = tmp; }
155 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
157 ell_width = rect[1].right - rect[1].left;
158 ell_height = rect[1].bottom - rect[1].top;
159 if (ell_width > right - left) ell_width = right - left;
160 if (ell_height > bottom - top) ell_height = bottom - top;
162 PSDRV_WriteSpool(physDev, "%RoundRect\n",11);
163 PSDRV_SetPen(physDev);
165 PSDRV_SetClip(physDev);
166 PSDRV_WriteMoveTo( physDev, left, top + ell_height/2 );
167 PSDRV_WriteArc( physDev, left + ell_width/2, top + ell_height/2, ell_width,
168 ell_height, 90.0, 180.0);
169 PSDRV_WriteLineTo( physDev, right - ell_width/2, top );
170 PSDRV_WriteArc( physDev, right - ell_width/2, top + ell_height/2, ell_width,
171 ell_height, 0.0, 90.0);
172 PSDRV_WriteLineTo( physDev, right, bottom - ell_height/2 );
173 PSDRV_WriteArc( physDev, right - ell_width/2, bottom - ell_height/2, ell_width,
174 ell_height, -90.0, 0.0);
175 PSDRV_WriteLineTo( physDev, right - ell_width/2, bottom);
176 PSDRV_WriteArc( physDev, left + ell_width/2, bottom - ell_height/2, ell_width,
177 ell_height, 180.0, -90.0);
178 PSDRV_WriteClosePath( physDev );
180 PSDRV_Brush(physDev,0);
181 PSDRV_DrawLine(physDev);
182 PSDRV_ResetClip(physDev);
183 return TRUE;
186 /***********************************************************************
187 * PSDRV_DrawArc
189 * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
191 static BOOL PSDRV_DrawArc( PSDRV_PDEVICE *physDev, INT left, INT top,
192 INT right, INT bottom, INT xstart, INT ystart,
193 INT xend, INT yend, int lines )
195 INT x, y, h, w;
196 double start_angle, end_angle, ratio;
197 RECT rect;
198 POINT start, end;
200 rect.left = left;
201 rect.top = top;
202 rect.right = right;
203 rect.bottom = bottom;
204 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
205 start.x = xstart;
206 start.y = ystart;
207 end.x = xend;
208 end.y = yend;
209 LPtoDP( physDev->hdc, &start, 1 );
210 LPtoDP( physDev->hdc, &end, 1 );
212 x = (rect.left + rect.right) / 2;
213 y = (rect.top + rect.bottom) / 2;
214 w = rect.right - rect.left;
215 h = rect.bottom - rect.top;
217 if(w < 0) w = -w;
218 if(h < 0) h = -h;
219 ratio = ((double)w)/h;
221 /* angle is the angle after the rectangle is transformed to a square and is
222 measured anticlockwise from the +ve x-axis */
224 start_angle = atan2((double)(y - start.y) * ratio, (double)(start.x - x));
225 end_angle = atan2((double)(y - end.y) * ratio, (double)(end.x - x));
227 start_angle *= 180.0 / PI;
228 end_angle *= 180.0 / PI;
230 PSDRV_WriteSpool(physDev,"%DrawArc\n", 9);
231 PSDRV_SetPen(physDev);
233 PSDRV_SetClip(physDev);
234 if(lines == 2) /* pie */
235 PSDRV_WriteMoveTo(physDev, x, y);
236 else
237 PSDRV_WriteNewPath( physDev );
239 PSDRV_WriteArc(physDev, x, y, w, h, start_angle, end_angle);
240 if(lines == 1 || lines == 2) { /* chord or pie */
241 PSDRV_WriteClosePath(physDev);
242 PSDRV_Brush(physDev,0);
244 PSDRV_DrawLine(physDev);
245 PSDRV_ResetClip(physDev);
247 return TRUE;
251 /***********************************************************************
252 * PSDRV_Arc
254 BOOL CDECL PSDRV_Arc( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
255 INT xstart, INT ystart, INT xend, INT yend )
257 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
260 /***********************************************************************
261 * PSDRV_Chord
263 BOOL CDECL PSDRV_Chord( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
264 INT xstart, INT ystart, INT xend, INT yend )
266 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
270 /***********************************************************************
271 * PSDRV_Pie
273 BOOL CDECL PSDRV_Pie( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
274 INT xstart, INT ystart, INT xend, INT yend )
276 return PSDRV_DrawArc( physDev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
280 /***********************************************************************
281 * PSDRV_Ellipse
283 BOOL CDECL PSDRV_Ellipse( PSDRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom)
285 INT x, y, w, h;
286 RECT rect;
288 TRACE("%d %d - %d %d\n", left, top, right, bottom);
290 rect.left = left;
291 rect.top = top;
292 rect.right = right;
293 rect.bottom = bottom;
294 LPtoDP( physDev->hdc, (POINT *)&rect, 2 );
296 x = (rect.left + rect.right) / 2;
297 y = (rect.top + rect.bottom) / 2;
298 w = rect.right - rect.left;
299 h = rect.bottom - rect.top;
301 PSDRV_WriteSpool(physDev, "%Ellipse\n", 9);
302 PSDRV_SetPen(physDev);
304 PSDRV_SetClip(physDev);
305 PSDRV_WriteNewPath(physDev);
306 PSDRV_WriteArc(physDev, x, y, w, h, 0.0, 360.0);
307 PSDRV_WriteClosePath(physDev);
308 PSDRV_Brush(physDev,0);
309 PSDRV_DrawLine(physDev);
310 PSDRV_ResetClip(physDev);
311 return TRUE;
315 /***********************************************************************
316 * PSDRV_PolyPolyline
318 BOOL CDECL PSDRV_PolyPolyline( PSDRV_PDEVICE *physDev, const POINT* pts, const DWORD* counts,
319 DWORD polylines )
321 DWORD polyline, line, total;
322 POINT *dev_pts, *pt;
324 TRACE("\n");
326 for (polyline = total = 0; polyline < polylines; polyline++) total += counts[polyline];
327 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
328 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
329 LPtoDP( physDev->hdc, dev_pts, total );
331 pt = dev_pts;
333 PSDRV_WriteSpool(physDev, "%PolyPolyline\n",14);
334 PSDRV_SetPen(physDev);
335 PSDRV_SetClip(physDev);
337 for(polyline = 0; polyline < polylines; polyline++) {
338 PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
339 pt++;
340 for(line = 1; line < counts[polyline]; line++, pt++)
341 PSDRV_WriteLineTo(physDev, pt->x, pt->y);
343 HeapFree( GetProcessHeap(), 0, dev_pts );
345 PSDRV_DrawLine(physDev);
346 PSDRV_ResetClip(physDev);
347 return TRUE;
351 /***********************************************************************
352 * PSDRV_Polyline
354 BOOL CDECL PSDRV_Polyline( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
356 return PSDRV_PolyPolyline( physDev, pt, (LPDWORD) &count, 1 );
360 /***********************************************************************
361 * PSDRV_PolyPolygon
363 BOOL CDECL PSDRV_PolyPolygon( PSDRV_PDEVICE *physDev, const POINT* pts, const INT* counts,
364 UINT polygons )
366 DWORD polygon, total;
367 INT line;
368 POINT *dev_pts, *pt;
370 TRACE("\n");
372 for (polygon = total = 0; polygon < polygons; polygon++) total += counts[polygon];
373 if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
374 memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
375 LPtoDP( physDev->hdc, dev_pts, total );
377 pt = dev_pts;
379 PSDRV_WriteSpool(physDev, "%PolyPolygon\n",13);
380 PSDRV_SetPen(physDev);
381 PSDRV_SetClip(physDev);
382 PSDRV_WriteNewPath(physDev);
384 for(polygon = 0; polygon < polygons; polygon++) {
385 PSDRV_WriteMoveTo(physDev, pt->x, pt->y);
386 pt++;
387 for(line = 1; line < counts[polygon]; line++, pt++)
388 PSDRV_WriteLineTo(physDev, pt->x, pt->y);
389 PSDRV_WriteClosePath(physDev);
391 HeapFree( GetProcessHeap(), 0, dev_pts );
393 if(GetPolyFillMode( physDev->hdc ) == ALTERNATE)
394 PSDRV_Brush(physDev, 1);
395 else /* WINDING */
396 PSDRV_Brush(physDev, 0);
398 PSDRV_DrawLine(physDev);
399 PSDRV_ResetClip(physDev);
400 return TRUE;
404 /***********************************************************************
405 * PSDRV_Polygon
407 BOOL CDECL PSDRV_Polygon( PSDRV_PDEVICE *physDev, const POINT* pt, INT count )
409 return PSDRV_PolyPolygon( physDev, pt, &count, 1 );
413 /***********************************************************************
414 * PSDRV_SetPixel
416 COLORREF CDECL PSDRV_SetPixel( PSDRV_PDEVICE *physDev, INT x, INT y, COLORREF color )
418 PSCOLOR pscolor;
419 POINT pt;
421 pt.x = x;
422 pt.y = y;
423 LPtoDP( physDev->hdc, &pt, 1 );
425 PSDRV_SetClip(physDev);
426 /* we bracket the setcolor in gsave/grestore so that we don't trash
427 the current pen colour */
428 PSDRV_WriteGSave(physDev);
429 PSDRV_WriteRectangle( physDev, pt.x, pt.y, 0, 0 );
430 PSDRV_CreateColor( physDev, &pscolor, color );
431 PSDRV_WriteSetColor( physDev, &pscolor );
432 PSDRV_WriteFill( physDev );
433 PSDRV_WriteGRestore(physDev);
434 PSDRV_ResetClip(physDev);
435 return color;
438 /***********************************************************************
439 * PSDRV_PaintRgn
441 BOOL CDECL PSDRV_PaintRgn( PSDRV_PDEVICE *physDev, HRGN hrgn )
444 RGNDATA *rgndata = NULL;
445 RECT *pRect;
446 DWORD size, i;
448 TRACE("hdc=%p\n", physDev->hdc);
450 size = GetRegionData(hrgn, 0, NULL);
451 rgndata = HeapAlloc( GetProcessHeap(), 0, size );
452 if(!rgndata) {
453 ERR("Can't allocate buffer\n");
454 return FALSE;
457 GetRegionData(hrgn, size, rgndata);
458 if (rgndata->rdh.nCount == 0)
459 goto end;
461 LPtoDP(physDev->hdc, (POINT*)rgndata->Buffer, rgndata->rdh.nCount * 2);
463 PSDRV_SetClip(physDev);
464 PSDRV_WriteNewPath(physDev);
465 for(i = 0, pRect = (RECT*)rgndata->Buffer; i < rgndata->rdh.nCount; i++, pRect++)
466 PSDRV_WriteRectangle(physDev, pRect->left, pRect->top, pRect->right - pRect->left, pRect->bottom - pRect->top);
468 PSDRV_Brush(physDev, 0);
469 PSDRV_ResetClip(physDev);
471 end:
472 HeapFree(GetProcessHeap(), 0, rgndata);
473 return TRUE;