msvcp140: Implement _Stat and _Lstat.
[wine.git] / dlls / gdi32 / painting.c
blobd61ab4ac0f2b647a92841f897921e4f8851428e1
1 /*
2 * GDI drawing functions.
4 * Copyright 1993, 1994 Alexandre Julliard
5 * Copyright 1997 Bertho A. Stultiens
6 * 1999 Huw D M Davies
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winerror.h"
34 #include "gdi_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
40 /***********************************************************************
41 * null driver fallback implementations
44 BOOL nulldrv_AngleArc( PHYSDEV dev, INT x, INT y, DWORD radius, FLOAT start, FLOAT sweep )
46 INT x1 = GDI_ROUND( x + cos( start * M_PI / 180 ) * radius );
47 INT y1 = GDI_ROUND( y - sin( start * M_PI / 180 ) * radius );
48 INT x2 = GDI_ROUND( x + cos( (start + sweep) * M_PI / 180) * radius );
49 INT y2 = GDI_ROUND( y - sin( (start + sweep) * M_PI / 180) * radius );
50 INT arcdir = SetArcDirection( dev->hdc, sweep >= 0 ? AD_COUNTERCLOCKWISE : AD_CLOCKWISE );
51 BOOL ret = ArcTo( dev->hdc, x - radius, y - radius, x + radius, y + radius, x1, y1, x2, y2 );
52 SetArcDirection( dev->hdc, arcdir );
53 return ret;
56 BOOL nulldrv_ArcTo( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
57 INT xstart, INT ystart, INT xend, INT yend )
59 INT width = abs( right - left );
60 INT height = abs( bottom - top );
61 double xradius = width / 2.0;
62 double yradius = height / 2.0;
63 double xcenter = right > left ? left + xradius : right + xradius;
64 double ycenter = bottom > top ? top + yradius : bottom + yradius;
65 double angle;
67 if (!height || !width) return FALSE;
68 /* draw a line from the current position to the starting point of the arc, then draw the arc */
69 angle = atan2( (ystart - ycenter) / height, (xstart - xcenter) / width );
70 LineTo( dev->hdc, GDI_ROUND( xcenter + cos(angle) * xradius ),
71 GDI_ROUND( ycenter + sin(angle) * yradius ));
72 return Arc( dev->hdc, left, top, right, bottom, xstart, ystart, xend, yend );
75 BOOL nulldrv_FillRgn( PHYSDEV dev, HRGN rgn, HBRUSH brush )
77 BOOL ret = FALSE;
78 HBRUSH prev;
80 if ((prev = SelectObject( dev->hdc, brush )))
82 ret = PaintRgn( dev->hdc, rgn );
83 SelectObject( dev->hdc, prev );
85 return ret;
88 BOOL nulldrv_FrameRgn( PHYSDEV dev, HRGN rgn, HBRUSH brush, INT width, INT height )
90 BOOL ret = FALSE;
91 HRGN tmp = CreateRectRgn( 0, 0, 0, 0 );
93 if (tmp)
95 if (REGION_FrameRgn( tmp, rgn, width, height )) ret = FillRgn( dev->hdc, tmp, brush );
96 DeleteObject( tmp );
98 return ret;
101 BOOL nulldrv_InvertRgn( PHYSDEV dev, HRGN rgn )
103 HBRUSH prev_brush = SelectObject( dev->hdc, GetStockObject(BLACK_BRUSH) );
104 INT prev_rop = SetROP2( dev->hdc, R2_NOT );
105 BOOL ret = PaintRgn( dev->hdc, rgn );
106 SelectObject( dev->hdc, prev_brush );
107 SetROP2( dev->hdc, prev_rop );
108 return ret;
111 BOOL nulldrv_PolyBezier( PHYSDEV dev, const POINT *points, DWORD count )
113 BOOL ret = FALSE;
114 POINT *pts;
115 INT n;
117 if ((pts = GDI_Bezier( points, count, &n )))
119 ret = Polyline( dev->hdc, pts, n );
120 HeapFree( GetProcessHeap(), 0, pts );
122 return ret;
125 BOOL nulldrv_PolyBezierTo( PHYSDEV dev, const POINT *points, DWORD count )
127 DC *dc = get_nulldrv_dc( dev );
128 BOOL ret = FALSE;
129 POINT *pts = HeapAlloc( GetProcessHeap(), 0, sizeof(POINT) * (count + 1) );
131 if (pts)
133 pts[0] = dc->cur_pos;
134 memcpy( pts + 1, points, sizeof(POINT) * count );
135 ret = PolyBezier( dev->hdc, pts, count + 1 );
136 HeapFree( GetProcessHeap(), 0, pts );
138 return ret;
141 BOOL nulldrv_PolyDraw( PHYSDEV dev, const POINT *points, const BYTE *types, DWORD count )
143 DC *dc = get_nulldrv_dc( dev );
144 POINT *line_pts = NULL, *bzr_pts = NULL, bzr[4];
145 DWORD i;
146 INT num_pts, num_bzr_pts, space, size;
148 /* check for valid point types */
149 for (i = 0; i < count; i++)
151 switch (types[i])
153 case PT_MOVETO:
154 case PT_LINETO | PT_CLOSEFIGURE:
155 case PT_LINETO:
156 break;
157 case PT_BEZIERTO:
158 if (i + 2 >= count) return FALSE;
159 if (types[i + 1] != PT_BEZIERTO) return FALSE;
160 if ((types[i + 2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) return FALSE;
161 i += 2;
162 break;
163 default:
164 return FALSE;
168 space = count + 300;
169 line_pts = HeapAlloc( GetProcessHeap(), 0, space * sizeof(POINT) );
170 num_pts = 1;
172 line_pts[0] = dc->cur_pos;
173 for (i = 0; i < count; i++)
175 switch (types[i])
177 case PT_MOVETO:
178 if (num_pts >= 2) Polyline( dev->hdc, line_pts, num_pts );
179 num_pts = 0;
180 line_pts[num_pts++] = points[i];
181 break;
182 case PT_LINETO:
183 case (PT_LINETO | PT_CLOSEFIGURE):
184 line_pts[num_pts++] = points[i];
185 break;
186 case PT_BEZIERTO:
187 bzr[0].x = line_pts[num_pts - 1].x;
188 bzr[0].y = line_pts[num_pts - 1].y;
189 memcpy( &bzr[1], &points[i], 3 * sizeof(POINT) );
191 if ((bzr_pts = GDI_Bezier( bzr, 4, &num_bzr_pts )))
193 size = num_pts + (count - i) + num_bzr_pts;
194 if (space < size)
196 space = size * 2;
197 line_pts = HeapReAlloc( GetProcessHeap(), 0, line_pts, space * sizeof(POINT) );
199 memcpy( &line_pts[num_pts], &bzr_pts[1], (num_bzr_pts - 1) * sizeof(POINT) );
200 num_pts += num_bzr_pts - 1;
201 HeapFree( GetProcessHeap(), 0, bzr_pts );
203 i += 2;
204 break;
206 if (types[i] & PT_CLOSEFIGURE) line_pts[num_pts++] = line_pts[0];
209 if (num_pts >= 2) Polyline( dev->hdc, line_pts, num_pts );
210 HeapFree( GetProcessHeap(), 0, line_pts );
211 return TRUE;
214 BOOL nulldrv_PolylineTo( PHYSDEV dev, const POINT *points, INT count )
216 DC *dc = get_nulldrv_dc( dev );
217 BOOL ret = FALSE;
218 POINT *pts;
220 if (!count) return FALSE;
221 if ((pts = HeapAlloc( GetProcessHeap(), 0, sizeof(POINT) * (count + 1) )))
223 pts[0] = dc->cur_pos;
224 memcpy( pts + 1, points, sizeof(POINT) * count );
225 ret = Polyline( dev->hdc, pts, count + 1 );
226 HeapFree( GetProcessHeap(), 0, pts );
228 return ret;
231 /***********************************************************************
232 * LineTo (GDI32.@)
234 BOOL WINAPI LineTo( HDC hdc, INT x, INT y )
236 DC * dc = get_dc_ptr( hdc );
237 PHYSDEV physdev;
238 BOOL ret;
240 TRACE( "%p, (%d, %d)\n", hdc, x, y );
242 if(!dc) return FALSE;
244 update_dc( dc );
245 physdev = GET_DC_PHYSDEV( dc, pLineTo );
246 ret = physdev->funcs->pLineTo( physdev, x, y );
248 if(ret)
250 dc->cur_pos.x = x;
251 dc->cur_pos.y = y;
253 release_dc_ptr( dc );
254 return ret;
258 /***********************************************************************
259 * MoveToEx (GDI32.@)
261 BOOL WINAPI MoveToEx( HDC hdc, INT x, INT y, LPPOINT pt )
263 BOOL ret;
264 PHYSDEV physdev;
265 DC * dc = get_dc_ptr( hdc );
267 TRACE( "%p, (%d, %d), %p\n", hdc, x, y, pt );
269 if(!dc) return FALSE;
271 if(pt)
272 *pt = dc->cur_pos;
274 dc->cur_pos.x = x;
275 dc->cur_pos.y = y;
277 physdev = GET_DC_PHYSDEV( dc, pMoveTo );
278 ret = physdev->funcs->pMoveTo( physdev, x, y );
279 release_dc_ptr( dc );
280 return ret;
284 /***********************************************************************
285 * Arc (GDI32.@)
287 BOOL WINAPI Arc( HDC hdc, INT left, INT top, INT right,
288 INT bottom, INT xstart, INT ystart,
289 INT xend, INT yend )
291 PHYSDEV physdev;
292 BOOL ret;
293 DC * dc = get_dc_ptr( hdc );
295 TRACE( "%p, (%d, %d)-(%d, %d), (%d, %d), (%d, %d)\n", hdc, left, top, right, bottom, xstart, ystart, xend, yend );
297 if (!dc) return FALSE;
298 update_dc( dc );
299 physdev = GET_DC_PHYSDEV( dc, pArc );
300 ret = physdev->funcs->pArc( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
301 release_dc_ptr( dc );
302 return ret;
305 /***********************************************************************
306 * ArcTo (GDI32.@)
308 BOOL WINAPI ArcTo( HDC hdc,
309 INT left, INT top,
310 INT right, INT bottom,
311 INT xstart, INT ystart,
312 INT xend, INT yend )
314 double width = abs( right - left ),
315 height = abs( bottom - top ),
316 xradius = width/2,
317 yradius = height/2,
318 xcenter = right > left ? left+xradius : right+xradius,
319 ycenter = bottom > top ? top+yradius : bottom+yradius,
320 angle;
321 PHYSDEV physdev;
322 BOOL result;
323 DC * dc = get_dc_ptr( hdc );
325 TRACE( "%p, (%d, %d)-(%d, %d), (%d, %d), (%d, %d)\n", hdc, left, top, right, bottom, xstart, ystart, xend, yend );
327 if(!dc) return FALSE;
328 update_dc( dc );
329 physdev = GET_DC_PHYSDEV( dc, pArcTo );
330 result = physdev->funcs->pArcTo( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
332 if (result)
334 angle = atan2(((yend-ycenter)/height),
335 ((xend-xcenter)/width));
336 dc->cur_pos.x = GDI_ROUND( xcenter + (cos( angle ) * xradius) );
337 dc->cur_pos.y = GDI_ROUND( ycenter + (sin( angle ) * yradius) );
339 release_dc_ptr( dc );
340 return result;
344 /***********************************************************************
345 * Pie (GDI32.@)
347 BOOL WINAPI Pie( HDC hdc, INT left, INT top,
348 INT right, INT bottom, INT xstart, INT ystart,
349 INT xend, INT yend )
351 BOOL ret;
352 PHYSDEV physdev;
353 DC * dc = get_dc_ptr( hdc );
355 TRACE( "%p, (%d, %d)-(%d, %d), (%d, %d), (%d, %d)\n", hdc, left, top, right, bottom, xstart, ystart, xend, yend );
357 if (!dc) return FALSE;
358 update_dc( dc );
359 physdev = GET_DC_PHYSDEV( dc, pPie );
360 ret = physdev->funcs->pPie( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
361 release_dc_ptr( dc );
362 return ret;
366 /***********************************************************************
367 * Chord (GDI32.@)
369 BOOL WINAPI Chord( HDC hdc, INT left, INT top,
370 INT right, INT bottom, INT xstart, INT ystart,
371 INT xend, INT yend )
373 BOOL ret;
374 PHYSDEV physdev;
375 DC * dc = get_dc_ptr( hdc );
377 TRACE( "%p, (%d, %d)-(%d, %d), (%d, %d), (%d, %d)\n", hdc, left, top, right, bottom, xstart, ystart, xend, yend );
379 if (!dc) return FALSE;
380 update_dc( dc );
381 physdev = GET_DC_PHYSDEV( dc, pChord );
382 ret = physdev->funcs->pChord( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
383 release_dc_ptr( dc );
384 return ret;
388 /***********************************************************************
389 * Ellipse (GDI32.@)
391 BOOL WINAPI Ellipse( HDC hdc, INT left, INT top,
392 INT right, INT bottom )
394 BOOL ret;
395 PHYSDEV physdev;
396 DC * dc = get_dc_ptr( hdc );
398 TRACE( "%p, (%d, %d)-(%d, %d)\n", hdc, left, top, right, bottom );
400 if (!dc) return FALSE;
401 update_dc( dc );
402 physdev = GET_DC_PHYSDEV( dc, pEllipse );
403 ret = physdev->funcs->pEllipse( physdev, left, top, right, bottom );
404 release_dc_ptr( dc );
405 return ret;
409 /***********************************************************************
410 * Rectangle (GDI32.@)
412 BOOL WINAPI Rectangle( HDC hdc, INT left, INT top,
413 INT right, INT bottom )
415 PHYSDEV physdev;
416 BOOL ret;
417 DC * dc = get_dc_ptr( hdc );
419 TRACE( "%p, (%d, %d)-(%d, %d)\n", hdc, left, top, right, bottom );
421 if (!dc) return FALSE;
422 update_dc( dc );
423 physdev = GET_DC_PHYSDEV( dc, pRectangle );
424 ret = physdev->funcs->pRectangle( physdev, left, top, right, bottom );
425 release_dc_ptr( dc );
426 return ret;
430 /***********************************************************************
431 * RoundRect (GDI32.@)
433 BOOL WINAPI RoundRect( HDC hdc, INT left, INT top, INT right,
434 INT bottom, INT ell_width, INT ell_height )
436 PHYSDEV physdev;
437 BOOL ret;
438 DC *dc = get_dc_ptr( hdc );
440 TRACE( "%p, (%d, %d)-(%d, %d), %dx%d\n", hdc, left, top, right, bottom, ell_width, ell_height );
442 if (!dc) return FALSE;
443 update_dc( dc );
444 physdev = GET_DC_PHYSDEV( dc, pRoundRect );
445 ret = physdev->funcs->pRoundRect( physdev, left, top, right, bottom, ell_width, ell_height );
446 release_dc_ptr( dc );
447 return ret;
450 /***********************************************************************
451 * SetPixel (GDI32.@)
453 COLORREF WINAPI SetPixel( HDC hdc, INT x, INT y, COLORREF color )
455 PHYSDEV physdev;
456 COLORREF ret;
457 DC * dc = get_dc_ptr( hdc );
459 if (!dc) return 0;
460 update_dc( dc );
461 physdev = GET_DC_PHYSDEV( dc, pSetPixel );
462 ret = physdev->funcs->pSetPixel( physdev, x, y, color );
463 release_dc_ptr( dc );
464 return ret;
467 /***********************************************************************
468 * SetPixelV (GDI32.@)
470 BOOL WINAPI SetPixelV( HDC hdc, INT x, INT y, COLORREF color )
472 PHYSDEV physdev;
473 DC * dc = get_dc_ptr( hdc );
475 if (!dc) return FALSE;
476 update_dc( dc );
477 physdev = GET_DC_PHYSDEV( dc, pSetPixel );
478 physdev->funcs->pSetPixel( physdev, x, y, color );
479 release_dc_ptr( dc );
480 return TRUE;
483 /***********************************************************************
484 * GetPixel (GDI32.@)
486 COLORREF WINAPI GetPixel( HDC hdc, INT x, INT y )
488 PHYSDEV physdev;
489 COLORREF ret;
490 DC * dc = get_dc_ptr( hdc );
492 if (!dc) return CLR_INVALID;
493 update_dc( dc );
494 physdev = GET_DC_PHYSDEV( dc, pGetPixel );
495 ret = physdev->funcs->pGetPixel( physdev, x, y );
496 release_dc_ptr( dc );
497 return ret;
501 /******************************************************************************
502 * GdiSetPixelFormat [GDI32.@]
504 * Probably not the correct semantics, it's supposed to be an internal backend for SetPixelFormat.
506 BOOL WINAPI GdiSetPixelFormat( HDC hdc, INT format, const PIXELFORMATDESCRIPTOR *descr )
508 DC *dc;
509 BOOL ret = TRUE;
511 TRACE("(%p,%d,%p)\n", hdc, format, descr);
513 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
515 if (!dc->pixel_format) dc->pixel_format = format;
516 else ret = (dc->pixel_format == format);
517 release_dc_ptr( dc );
518 return ret;
522 /******************************************************************************
523 * GdiDescribePixelFormat [GDI32.@]
525 * Probably not the correct semantics, it's supposed to be an internal backend for DescribePixelFormat.
527 INT WINAPI GdiDescribePixelFormat( HDC hdc, INT format, UINT size, PIXELFORMATDESCRIPTOR *descr )
529 FIXME( "(%p,%d,%d,%p): stub\n", hdc, format, size, descr );
530 return 0;
534 /******************************************************************************
535 * GdiSwapBuffers [GDI32.@]
537 * Probably not the correct semantics, it's supposed to be an internal backend for SwapBuffers.
539 BOOL WINAPI GdiSwapBuffers( HDC hdc )
541 FIXME( "(%p): stub\n", hdc );
542 return FALSE;
546 /***********************************************************************
547 * PaintRgn (GDI32.@)
549 BOOL WINAPI PaintRgn( HDC hdc, HRGN hrgn )
551 PHYSDEV physdev;
552 BOOL ret;
553 DC * dc = get_dc_ptr( hdc );
555 TRACE( "%p, %p\n", hdc, hrgn );
557 if (!dc) return FALSE;
558 update_dc( dc );
559 physdev = GET_DC_PHYSDEV( dc, pPaintRgn );
560 ret = physdev->funcs->pPaintRgn( physdev, hrgn );
561 release_dc_ptr( dc );
562 return ret;
566 /***********************************************************************
567 * FillRgn (GDI32.@)
569 BOOL WINAPI FillRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush )
571 PHYSDEV physdev;
572 BOOL retval;
573 DC * dc = get_dc_ptr( hdc );
575 TRACE( "%p, %p, %p\n", hdc, hrgn, hbrush );
577 if (!dc) return FALSE;
578 update_dc( dc );
579 physdev = GET_DC_PHYSDEV( dc, pFillRgn );
580 retval = physdev->funcs->pFillRgn( physdev, hrgn, hbrush );
581 release_dc_ptr( dc );
582 return retval;
586 /***********************************************************************
587 * FrameRgn (GDI32.@)
589 BOOL WINAPI FrameRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush,
590 INT nWidth, INT nHeight )
592 PHYSDEV physdev;
593 BOOL ret;
594 DC *dc = get_dc_ptr( hdc );
596 TRACE( "%p, %p, %p, %dx%d\n", hdc, hrgn, hbrush, nWidth, nHeight );
598 if (!dc) return FALSE;
599 update_dc( dc );
600 physdev = GET_DC_PHYSDEV( dc, pFrameRgn );
601 ret = physdev->funcs->pFrameRgn( physdev, hrgn, hbrush, nWidth, nHeight );
602 release_dc_ptr( dc );
603 return ret;
607 /***********************************************************************
608 * InvertRgn (GDI32.@)
610 BOOL WINAPI InvertRgn( HDC hdc, HRGN hrgn )
612 PHYSDEV physdev;
613 BOOL ret;
614 DC *dc = get_dc_ptr( hdc );
616 TRACE( "%p, %p\n", hdc, hrgn );
618 if (!dc) return FALSE;
619 update_dc( dc );
620 physdev = GET_DC_PHYSDEV( dc, pInvertRgn );
621 ret = physdev->funcs->pInvertRgn( physdev, hrgn );
622 release_dc_ptr( dc );
623 return ret;
627 /**********************************************************************
628 * Polyline (GDI32.@)
630 BOOL WINAPI Polyline( HDC hdc, const POINT* pt, INT count )
632 PHYSDEV physdev;
633 BOOL ret;
634 DC * dc = get_dc_ptr( hdc );
636 TRACE( "%p, %p, %d\n", hdc, pt, count );
638 if (!dc) return FALSE;
639 update_dc( dc );
640 physdev = GET_DC_PHYSDEV( dc, pPolyline );
641 ret = physdev->funcs->pPolyline( physdev, pt, count );
642 release_dc_ptr( dc );
643 return ret;
646 /**********************************************************************
647 * PolylineTo (GDI32.@)
649 BOOL WINAPI PolylineTo( HDC hdc, const POINT* pt, DWORD cCount )
651 DC * dc = get_dc_ptr( hdc );
652 PHYSDEV physdev;
653 BOOL ret;
655 TRACE( "%p, %p, %u\n", hdc, pt, cCount );
657 if(!dc) return FALSE;
659 update_dc( dc );
660 physdev = GET_DC_PHYSDEV( dc, pPolylineTo );
661 ret = physdev->funcs->pPolylineTo( physdev, pt, cCount );
663 if (ret && cCount)
664 dc->cur_pos = pt[cCount - 1];
666 release_dc_ptr( dc );
667 return ret;
671 /**********************************************************************
672 * Polygon (GDI32.@)
674 BOOL WINAPI Polygon( HDC hdc, const POINT* pt, INT count )
676 PHYSDEV physdev;
677 BOOL ret;
678 DC * dc = get_dc_ptr( hdc );
680 TRACE( "%p, %p, %d\n", hdc, pt, count );
682 if (!dc) return FALSE;
683 update_dc( dc );
684 physdev = GET_DC_PHYSDEV( dc, pPolygon );
685 ret = physdev->funcs->pPolygon( physdev, pt, count );
686 release_dc_ptr( dc );
687 return ret;
691 /**********************************************************************
692 * PolyPolygon (GDI32.@)
694 BOOL WINAPI PolyPolygon( HDC hdc, const POINT* pt, const INT* counts,
695 UINT polygons )
697 PHYSDEV physdev;
698 BOOL ret;
699 DC * dc = get_dc_ptr( hdc );
701 TRACE( "%p, %p, %p, %u\n", hdc, pt, counts, polygons );
703 if (!dc) return FALSE;
704 update_dc( dc );
705 physdev = GET_DC_PHYSDEV( dc, pPolyPolygon );
706 ret = physdev->funcs->pPolyPolygon( physdev, pt, counts, polygons );
707 release_dc_ptr( dc );
708 return ret;
711 /**********************************************************************
712 * PolyPolyline (GDI32.@)
714 BOOL WINAPI PolyPolyline( HDC hdc, const POINT* pt, const DWORD* counts,
715 DWORD polylines )
717 PHYSDEV physdev;
718 BOOL ret;
719 DC * dc = get_dc_ptr( hdc );
721 TRACE( "%p, %p, %p, %u\n", hdc, pt, counts, polylines );
723 if (!dc) return FALSE;
724 update_dc( dc );
725 physdev = GET_DC_PHYSDEV( dc, pPolyPolyline );
726 ret = physdev->funcs->pPolyPolyline( physdev, pt, counts, polylines );
727 release_dc_ptr( dc );
728 return ret;
731 /**********************************************************************
732 * ExtFloodFill (GDI32.@)
734 BOOL WINAPI ExtFloodFill( HDC hdc, INT x, INT y, COLORREF color,
735 UINT fillType )
737 PHYSDEV physdev;
738 BOOL ret;
739 DC * dc = get_dc_ptr( hdc );
741 TRACE( "%p, (%d, %d), %08x, %x\n", hdc, x, y, color, fillType );
743 if (!dc) return FALSE;
744 update_dc( dc );
745 physdev = GET_DC_PHYSDEV( dc, pExtFloodFill );
746 ret = physdev->funcs->pExtFloodFill( physdev, x, y, color, fillType );
747 release_dc_ptr( dc );
748 return ret;
752 /**********************************************************************
753 * FloodFill (GDI32.@)
755 BOOL WINAPI FloodFill( HDC hdc, INT x, INT y, COLORREF color )
757 return ExtFloodFill( hdc, x, y, color, FLOODFILLBORDER );
761 /******************************************************************************
762 * PolyBezier [GDI32.@]
763 * Draws one or more Bezier curves
765 * PARAMS
766 * hDc [I] Handle to device context
767 * lppt [I] Pointer to endpoints and control points
768 * cPoints [I] Count of endpoints and control points
770 * RETURNS
771 * Success: TRUE
772 * Failure: FALSE
774 BOOL WINAPI PolyBezier( HDC hdc, const POINT* lppt, DWORD cPoints )
776 PHYSDEV physdev;
777 BOOL ret;
778 DC * dc;
780 TRACE( "%p, %p, %u\n", hdc, lppt, cPoints );
782 /* cPoints must be 3 * n + 1 (where n>=1) */
783 if (cPoints == 1 || (cPoints % 3) != 1) return FALSE;
785 dc = get_dc_ptr( hdc );
786 if(!dc) return FALSE;
788 update_dc( dc );
789 physdev = GET_DC_PHYSDEV( dc, pPolyBezier );
790 ret = physdev->funcs->pPolyBezier( physdev, lppt, cPoints );
791 release_dc_ptr( dc );
792 return ret;
795 /******************************************************************************
796 * PolyBezierTo [GDI32.@]
797 * Draws one or more Bezier curves
799 * PARAMS
800 * hDc [I] Handle to device context
801 * lppt [I] Pointer to endpoints and control points
802 * cPoints [I] Count of endpoints and control points
804 * RETURNS
805 * Success: TRUE
806 * Failure: FALSE
808 BOOL WINAPI PolyBezierTo( HDC hdc, const POINT* lppt, DWORD cPoints )
810 DC * dc;
811 BOOL ret;
812 PHYSDEV physdev;
814 TRACE( "%p, %p, %u\n", hdc, lppt, cPoints );
816 /* cbPoints must be 3 * n (where n>=1) */
817 if (!cPoints || (cPoints % 3) != 0) return FALSE;
819 dc = get_dc_ptr( hdc );
820 if(!dc) return FALSE;
822 update_dc( dc );
823 physdev = GET_DC_PHYSDEV( dc, pPolyBezierTo );
824 ret = physdev->funcs->pPolyBezierTo( physdev, lppt, cPoints );
826 if(ret)
827 dc->cur_pos = lppt[cPoints - 1];
829 release_dc_ptr( dc );
830 return ret;
833 /***********************************************************************
834 * AngleArc (GDI32.@)
836 BOOL WINAPI AngleArc(HDC hdc, INT x, INT y, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle)
838 PHYSDEV physdev;
839 BOOL result;
840 DC *dc;
842 TRACE( "%p, (%d, %d), %u, %f, %f\n", hdc, x, y, dwRadius, eStartAngle, eSweepAngle );
844 if( (signed int)dwRadius < 0 )
845 return FALSE;
847 dc = get_dc_ptr( hdc );
848 if(!dc) return FALSE;
850 update_dc( dc );
851 physdev = GET_DC_PHYSDEV( dc, pAngleArc );
852 result = physdev->funcs->pAngleArc( physdev, x, y, dwRadius, eStartAngle, eSweepAngle );
854 if (result)
856 dc->cur_pos.x = GDI_ROUND( x + cos( (eStartAngle + eSweepAngle) * M_PI / 180 ) * dwRadius );
857 dc->cur_pos.y = GDI_ROUND( y - sin( (eStartAngle + eSweepAngle) * M_PI / 180 ) * dwRadius );
859 release_dc_ptr( dc );
860 return result;
863 /***********************************************************************
864 * PolyDraw (GDI32.@)
866 BOOL WINAPI PolyDraw(HDC hdc, const POINT *lppt, const BYTE *lpbTypes,
867 DWORD cCount)
869 DC *dc = get_dc_ptr( hdc );
870 PHYSDEV physdev;
871 BOOL result;
873 TRACE( "%p, %p, %p, %u\n", hdc, lppt, lpbTypes, cCount );
875 if(!dc) return FALSE;
877 update_dc( dc );
878 physdev = GET_DC_PHYSDEV( dc, pPolyDraw );
879 result = physdev->funcs->pPolyDraw( physdev, lppt, lpbTypes, cCount );
880 if (result && cCount)
881 dc->cur_pos = lppt[cCount - 1];
883 release_dc_ptr( dc );
884 return result;
888 /**********************************************************************
889 * LineDDA (GDI32.@)
891 BOOL WINAPI LineDDA(INT nXStart, INT nYStart, INT nXEnd, INT nYEnd,
892 LINEDDAPROC callback, LPARAM lParam )
894 INT xadd = 1, yadd = 1;
895 INT err,erradd;
896 INT cnt;
897 INT dx = nXEnd - nXStart;
898 INT dy = nYEnd - nYStart;
900 TRACE( "(%d, %d), (%d, %d), %p, %lx\n", nXStart, nYStart, nXEnd, nYEnd, callback, lParam );
902 if (dx < 0)
904 dx = -dx;
905 xadd = -1;
907 if (dy < 0)
909 dy = -dy;
910 yadd = -1;
912 if (dx > dy) /* line is "more horizontal" */
914 err = 2*dy - dx; erradd = 2*dy - 2*dx;
915 for(cnt = 0;cnt < dx; cnt++)
917 callback(nXStart,nYStart,lParam);
918 if (err > 0)
920 nYStart += yadd;
921 err += erradd;
923 else err += 2*dy;
924 nXStart += xadd;
927 else /* line is "more vertical" */
929 err = 2*dx - dy; erradd = 2*dx - 2*dy;
930 for(cnt = 0;cnt < dy; cnt++)
932 callback(nXStart,nYStart,lParam);
933 if (err > 0)
935 nXStart += xadd;
936 err += erradd;
938 else err += 2*dx;
939 nYStart += yadd;
942 return TRUE;
946 /******************************************************************
948 * *Very* simple bezier drawing code,
950 * It uses a recursive algorithm to divide the curve in a series
951 * of straight line segments. Not ideal but sufficient for me.
952 * If you are in need for something better look for some incremental
953 * algorithm.
955 * 7 July 1998 Rein Klazes
959 * some macro definitions for bezier drawing
961 * to avoid truncation errors the coordinates are
962 * shifted upwards. When used in drawing they are
963 * shifted down again, including correct rounding
964 * and avoiding floating point arithmetic
965 * 4 bits should allow 27 bits coordinates which I saw
966 * somewhere in the win32 doc's
970 #define BEZIERSHIFTBITS 4
971 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
972 #define BEZIERPIXEL BEZIERSHIFTUP(1)
973 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
974 /* maximum depth of recursion */
975 #define BEZIERMAXDEPTH 8
977 /* size of array to store points on */
978 /* enough for one curve */
979 #define BEZIER_INITBUFSIZE (150)
981 /* calculate Bezier average, in this case the middle
982 * correctly rounded...
983 * */
985 #define BEZIERMIDDLE(Mid, P1, P2) \
986 (Mid).x=((P1).x+(P2).x + 1)/2;\
987 (Mid).y=((P1).y+(P2).y + 1)/2;
989 /**********************************************************
990 * BezierCheck helper function to check
991 * that recursion can be terminated
992 * Points[0] and Points[3] are begin and endpoint
993 * Points[1] and Points[2] are control points
994 * level is the recursion depth
995 * returns true if the recursion can be terminated
997 static BOOL BezierCheck( int level, POINT *Points)
999 INT dx, dy;
1000 dx=Points[3].x-Points[0].x;
1001 dy=Points[3].y-Points[0].y;
1002 if(abs(dy)<=abs(dx)){/* shallow line */
1003 /* check that control points are between begin and end */
1004 if(Points[1].x < Points[0].x){
1005 if(Points[1].x < Points[3].x)
1006 return FALSE;
1007 }else
1008 if(Points[1].x > Points[3].x)
1009 return FALSE;
1010 if(Points[2].x < Points[0].x){
1011 if(Points[2].x < Points[3].x)
1012 return FALSE;
1013 }else
1014 if(Points[2].x > Points[3].x)
1015 return FALSE;
1016 dx=BEZIERSHIFTDOWN(dx);
1017 if(!dx) return TRUE;
1018 if(abs(Points[1].y-Points[0].y-(dy/dx)*
1019 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
1020 abs(Points[2].y-Points[0].y-(dy/dx)*
1021 BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
1022 return FALSE;
1023 else
1024 return TRUE;
1025 }else{ /* steep line */
1026 /* check that control points are between begin and end */
1027 if(Points[1].y < Points[0].y){
1028 if(Points[1].y < Points[3].y)
1029 return FALSE;
1030 }else
1031 if(Points[1].y > Points[3].y)
1032 return FALSE;
1033 if(Points[2].y < Points[0].y){
1034 if(Points[2].y < Points[3].y)
1035 return FALSE;
1036 }else
1037 if(Points[2].y > Points[3].y)
1038 return FALSE;
1039 dy=BEZIERSHIFTDOWN(dy);
1040 if(!dy) return TRUE;
1041 if(abs(Points[1].x-Points[0].x-(dx/dy)*
1042 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
1043 abs(Points[2].x-Points[0].x-(dx/dy)*
1044 BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
1045 return FALSE;
1046 else
1047 return TRUE;
1051 /* Helper for GDI_Bezier.
1052 * Just handles one Bezier, so Points should point to four POINTs
1054 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
1055 INT *nPtsOut, INT level )
1057 if(*nPtsOut == *dwOut) {
1058 *dwOut *= 2;
1059 *PtsOut = HeapReAlloc( GetProcessHeap(), 0, *PtsOut,
1060 *dwOut * sizeof(POINT) );
1063 if(!level || BezierCheck(level, Points)) {
1064 if(*nPtsOut == 0) {
1065 (*PtsOut)[0].x = BEZIERSHIFTDOWN(Points[0].x);
1066 (*PtsOut)[0].y = BEZIERSHIFTDOWN(Points[0].y);
1067 *nPtsOut = 1;
1069 (*PtsOut)[*nPtsOut].x = BEZIERSHIFTDOWN(Points[3].x);
1070 (*PtsOut)[*nPtsOut].y = BEZIERSHIFTDOWN(Points[3].y);
1071 (*nPtsOut) ++;
1072 } else {
1073 POINT Points2[4]; /* for the second recursive call */
1074 Points2[3]=Points[3];
1075 BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
1076 BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
1077 BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
1079 BEZIERMIDDLE(Points[1], Points[0], Points[1]);
1080 BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1081 BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1083 Points2[0]=Points[3];
1085 /* do the two halves */
1086 GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
1087 GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
1093 /***********************************************************************
1094 * GDI_Bezier [INTERNAL]
1095 * Calculate line segments that approximate -what microsoft calls- a bezier
1096 * curve.
1097 * The routine recursively divides the curve in two parts until a straight
1098 * line can be drawn
1100 * PARAMS
1102 * Points [I] Ptr to count POINTs which are the end and control points
1103 * of the set of Bezier curves to flatten.
1104 * count [I] Number of Points. Must be 3n+1.
1105 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
1106 * lines+1).
1108 * RETURNS
1110 * Ptr to an array of POINTs that contain the lines that approximate the
1111 * Beziers. The array is allocated on the process heap and it is the caller's
1112 * responsibility to HeapFree it. [this is not a particularly nice interface
1113 * but since we can't know in advance how many points we will generate, the
1114 * alternative would be to call the function twice, once to determine the size
1115 * and a second time to do the work - I decided this was too much of a pain].
1117 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
1119 POINT *out;
1120 INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
1122 if (count == 1 || (count - 1) % 3 != 0) {
1123 ERR("Invalid no. of points %d\n", count);
1124 return NULL;
1126 *nPtsOut = 0;
1127 out = HeapAlloc( GetProcessHeap(), 0, dwOut * sizeof(POINT));
1128 for(Bezier = 0; Bezier < (count-1)/3; Bezier++) {
1129 POINT ptBuf[4];
1130 memcpy(ptBuf, Points + Bezier * 3, sizeof(POINT) * 4);
1131 for(i = 0; i < 4; i++) {
1132 ptBuf[i].x = BEZIERSHIFTUP(ptBuf[i].x);
1133 ptBuf[i].y = BEZIERSHIFTUP(ptBuf[i].y);
1135 GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
1137 TRACE("Produced %d points\n", *nPtsOut);
1138 return out;
1141 /******************************************************************************
1142 * GdiGradientFill (GDI32.@)
1144 BOOL WINAPI GdiGradientFill( HDC hdc, TRIVERTEX *vert_array, ULONG nvert,
1145 void *grad_array, ULONG ngrad, ULONG mode )
1147 DC *dc;
1148 PHYSDEV physdev;
1149 BOOL ret;
1150 ULONG i;
1152 TRACE("%p vert_array:%p nvert:%d grad_array:%p ngrad:%d\n", hdc, vert_array, nvert, grad_array, ngrad);
1154 if (!vert_array || !nvert || !grad_array || !ngrad || mode > GRADIENT_FILL_TRIANGLE)
1156 SetLastError( ERROR_INVALID_PARAMETER );
1157 return FALSE;
1159 for (i = 0; i < ngrad * (mode == GRADIENT_FILL_TRIANGLE ? 3 : 2); i++)
1160 if (((ULONG *)grad_array)[i] >= nvert) return FALSE;
1162 if (!(dc = get_dc_ptr( hdc )))
1164 SetLastError( ERROR_INVALID_PARAMETER );
1165 return FALSE;
1167 update_dc( dc );
1168 physdev = GET_DC_PHYSDEV( dc, pGradientFill );
1169 ret = physdev->funcs->pGradientFill( physdev, vert_array, nvert, grad_array, ngrad, mode );
1170 release_dc_ptr( dc );
1171 return ret;
1174 /******************************************************************************
1175 * GdiDrawStream (GDI32.@)
1178 BOOL WINAPI GdiDrawStream( HDC hdc, ULONG in, void * pvin )
1180 FIXME("stub: %p, %d, %p\n", hdc, in, pvin);
1181 return FALSE;