jscript: Use compiler for handling if statement.
[wine.git] / dlls / gdi32 / painting.c
blob241f9c335de6a5889d954983bf7cf5ca52ef8d0d
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 BOOL ret = FALSE;
128 POINT *pts = HeapAlloc( GetProcessHeap(), 0, sizeof(POINT) * (count + 1) );
130 if (pts)
132 GetCurrentPositionEx( dev->hdc, &pts[0] );
133 memcpy( pts + 1, points, sizeof(POINT) * count );
134 ret = PolyBezier( dev->hdc, pts, count + 1 );
135 HeapFree( GetProcessHeap(), 0, pts );
137 return ret;
140 BOOL nulldrv_PolyDraw( PHYSDEV dev, const POINT *points, const BYTE *types, DWORD count )
142 POINT *line_pts = NULL, *bzr_pts = NULL, bzr[4];
143 INT i, num_pts, num_bzr_pts, space, size;
145 /* check for valid point types */
146 for (i = 0; i < count; i++)
148 switch (types[i])
150 case PT_MOVETO:
151 case PT_LINETO | PT_CLOSEFIGURE:
152 case PT_LINETO:
153 break;
154 case PT_BEZIERTO:
155 if((i + 2 < count) && (types[i + 1] == PT_BEZIERTO) &&
156 ((types[i + 2] & ~PT_CLOSEFIGURE) == PT_BEZIERTO))
158 i += 2;
159 break;
161 default:
162 return FALSE;
166 space = count + 300;
167 line_pts = HeapAlloc( GetProcessHeap(), 0, space * sizeof(POINT) );
168 num_pts = 1;
170 GetCurrentPositionEx( dev->hdc, &line_pts[0] );
171 for (i = 0; i < count; i++)
173 switch (types[i])
175 case PT_MOVETO:
176 if (num_pts >= 2) Polyline( dev->hdc, line_pts, num_pts );
177 num_pts = 0;
178 line_pts[num_pts++] = points[i];
179 break;
180 case PT_LINETO:
181 case (PT_LINETO | PT_CLOSEFIGURE):
182 line_pts[num_pts++] = points[i];
183 break;
184 case PT_BEZIERTO:
185 bzr[0].x = line_pts[num_pts - 1].x;
186 bzr[0].y = line_pts[num_pts - 1].y;
187 memcpy( &bzr[1], &points[i], 3 * sizeof(POINT) );
189 if ((bzr_pts = GDI_Bezier( bzr, 4, &num_bzr_pts )))
191 size = num_pts + (count - i) + num_bzr_pts;
192 if (space < size)
194 space = size * 2;
195 line_pts = HeapReAlloc( GetProcessHeap(), 0, line_pts, space * sizeof(POINT) );
197 memcpy( &line_pts[num_pts], &bzr_pts[1], (num_bzr_pts - 1) * sizeof(POINT) );
198 num_pts += num_bzr_pts - 1;
199 HeapFree( GetProcessHeap(), 0, bzr_pts );
201 i += 2;
202 break;
204 if (types[i] & PT_CLOSEFIGURE) line_pts[num_pts++] = line_pts[0];
207 if (num_pts >= 2) Polyline( dev->hdc, line_pts, num_pts );
208 MoveToEx( dev->hdc, line_pts[num_pts - 1].x, line_pts[num_pts - 1].y, NULL );
209 HeapFree( GetProcessHeap(), 0, line_pts );
210 return TRUE;
213 BOOL nulldrv_PolylineTo( PHYSDEV dev, const POINT *points, INT count )
215 BOOL ret = FALSE;
216 POINT *pts;
218 if (!count) return FALSE;
219 if ((pts = HeapAlloc( GetProcessHeap(), 0, sizeof(POINT) * (count + 1) )))
221 GetCurrentPositionEx( dev->hdc, &pts[0] );
222 memcpy( pts + 1, points, sizeof(POINT) * count );
223 ret = Polyline( dev->hdc, pts, count + 1 );
224 HeapFree( GetProcessHeap(), 0, pts );
226 return ret;
229 /***********************************************************************
230 * LineTo (GDI32.@)
232 BOOL WINAPI LineTo( HDC hdc, INT x, INT y )
234 DC * dc = get_dc_ptr( hdc );
235 PHYSDEV physdev;
236 BOOL ret;
238 if(!dc) return FALSE;
240 update_dc( dc );
241 physdev = GET_DC_PHYSDEV( dc, pLineTo );
242 ret = physdev->funcs->pLineTo( physdev, x, y );
244 if(ret) {
245 dc->CursPosX = x;
246 dc->CursPosY = y;
248 release_dc_ptr( dc );
249 return ret;
253 /***********************************************************************
254 * MoveToEx (GDI32.@)
256 BOOL WINAPI MoveToEx( HDC hdc, INT x, INT y, LPPOINT pt )
258 BOOL ret;
259 PHYSDEV physdev;
260 DC * dc = get_dc_ptr( hdc );
262 if(!dc) return FALSE;
264 if(pt) {
265 pt->x = dc->CursPosX;
266 pt->y = dc->CursPosY;
268 dc->CursPosX = x;
269 dc->CursPosY = y;
271 physdev = GET_DC_PHYSDEV( dc, pMoveTo );
272 ret = physdev->funcs->pMoveTo( physdev, x, y );
273 release_dc_ptr( dc );
274 return ret;
278 /***********************************************************************
279 * Arc (GDI32.@)
281 BOOL WINAPI Arc( HDC hdc, INT left, INT top, INT right,
282 INT bottom, INT xstart, INT ystart,
283 INT xend, INT yend )
285 BOOL ret = FALSE;
286 DC * dc = get_dc_ptr( hdc );
288 if (dc)
290 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pArc );
291 update_dc( dc );
292 ret = physdev->funcs->pArc( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
293 release_dc_ptr( dc );
295 return ret;
298 /***********************************************************************
299 * ArcTo (GDI32.@)
301 BOOL WINAPI ArcTo( HDC hdc,
302 INT left, INT top,
303 INT right, INT bottom,
304 INT xstart, INT ystart,
305 INT xend, INT yend )
307 double width = fabs(right-left),
308 height = fabs(bottom-top),
309 xradius = width/2,
310 yradius = height/2,
311 xcenter = right > left ? left+xradius : right+xradius,
312 ycenter = bottom > top ? top+yradius : bottom+yradius,
313 angle;
314 PHYSDEV physdev;
315 BOOL result;
316 DC * dc = get_dc_ptr( hdc );
317 if(!dc) return FALSE;
319 update_dc( dc );
320 physdev = GET_DC_PHYSDEV( dc, pArcTo );
321 result = physdev->funcs->pArcTo( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
323 if (result) {
324 angle = atan2(((yend-ycenter)/height),
325 ((xend-xcenter)/width));
326 dc->CursPosX = GDI_ROUND(xcenter+(cos(angle)*xradius));
327 dc->CursPosY = GDI_ROUND(ycenter+(sin(angle)*yradius));
329 release_dc_ptr( dc );
330 return result;
334 /***********************************************************************
335 * Pie (GDI32.@)
337 BOOL WINAPI Pie( HDC hdc, INT left, INT top,
338 INT right, INT bottom, INT xstart, INT ystart,
339 INT xend, INT yend )
341 BOOL ret;
342 PHYSDEV physdev;
343 DC * dc = get_dc_ptr( hdc );
344 if (!dc) return FALSE;
346 update_dc( dc );
347 physdev = GET_DC_PHYSDEV( dc, pPie );
348 ret = physdev->funcs->pPie( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
349 release_dc_ptr( dc );
350 return ret;
354 /***********************************************************************
355 * Chord (GDI32.@)
357 BOOL WINAPI Chord( HDC hdc, INT left, INT top,
358 INT right, INT bottom, INT xstart, INT ystart,
359 INT xend, INT yend )
361 BOOL ret;
362 PHYSDEV physdev;
363 DC * dc = get_dc_ptr( hdc );
364 if (!dc) return FALSE;
366 update_dc( dc );
367 physdev = GET_DC_PHYSDEV( dc, pChord );
368 ret = physdev->funcs->pChord( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
369 release_dc_ptr( dc );
370 return ret;
374 /***********************************************************************
375 * Ellipse (GDI32.@)
377 BOOL WINAPI Ellipse( HDC hdc, INT left, INT top,
378 INT right, INT bottom )
380 BOOL ret;
381 PHYSDEV physdev;
382 DC * dc = get_dc_ptr( hdc );
383 if (!dc) return FALSE;
385 update_dc( dc );
386 physdev = GET_DC_PHYSDEV( dc, pEllipse );
387 ret = physdev->funcs->pEllipse( physdev, left, top, right, bottom );
388 release_dc_ptr( dc );
389 return ret;
393 /***********************************************************************
394 * Rectangle (GDI32.@)
396 BOOL WINAPI Rectangle( HDC hdc, INT left, INT top,
397 INT right, INT bottom )
399 BOOL ret = FALSE;
400 DC * dc = get_dc_ptr( hdc );
402 if (dc)
404 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRectangle );
405 update_dc( dc );
406 ret = physdev->funcs->pRectangle( physdev, left, top, right, bottom );
407 release_dc_ptr( dc );
409 return ret;
413 /***********************************************************************
414 * RoundRect (GDI32.@)
416 BOOL WINAPI RoundRect( HDC hdc, INT left, INT top, INT right,
417 INT bottom, INT ell_width, INT ell_height )
419 BOOL ret = FALSE;
420 DC *dc = get_dc_ptr( hdc );
422 if (dc)
424 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRoundRect );
425 update_dc( dc );
426 ret = physdev->funcs->pRoundRect( physdev, left, top, right, bottom, ell_width, ell_height );
427 release_dc_ptr( dc );
429 return ret;
432 /***********************************************************************
433 * SetPixel (GDI32.@)
435 COLORREF WINAPI SetPixel( HDC hdc, INT x, INT y, COLORREF color )
437 COLORREF ret = 0;
438 DC * dc = get_dc_ptr( hdc );
440 if (dc)
442 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetPixel );
443 update_dc( dc );
444 ret = physdev->funcs->pSetPixel( physdev, x, y, color );
445 release_dc_ptr( dc );
447 return ret;
450 /***********************************************************************
451 * SetPixelV (GDI32.@)
453 BOOL WINAPI SetPixelV( HDC hdc, INT x, INT y, COLORREF color )
455 BOOL ret = FALSE;
456 DC * dc = get_dc_ptr( hdc );
458 if (dc)
460 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetPixel );
461 update_dc( dc );
462 physdev->funcs->pSetPixel( physdev, x, y, color );
463 ret = TRUE;
464 release_dc_ptr( dc );
466 return ret;
469 /***********************************************************************
470 * GetPixel (GDI32.@)
472 COLORREF WINAPI GetPixel( HDC hdc, INT x, INT y )
474 COLORREF ret = CLR_INVALID;
475 DC * dc = get_dc_ptr( hdc );
477 if (dc)
479 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetPixel );
480 update_dc( dc );
481 ret = physdev->funcs->pGetPixel( physdev, x, y );
482 release_dc_ptr( dc );
484 return ret;
488 /******************************************************************************
489 * ChoosePixelFormat [GDI32.@]
490 * Matches a pixel format to given format
492 * PARAMS
493 * hdc [I] Device context to search for best pixel match
494 * ppfd [I] Pixel format for which a match is sought
496 * RETURNS
497 * Success: Pixel format index closest to given format
498 * Failure: 0
500 INT WINAPI ChoosePixelFormat( HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd )
502 INT ret = 0;
503 DC * dc = get_dc_ptr( hdc );
505 TRACE("(%p,%p)\n",hdc,ppfd);
507 if (dc)
509 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pChoosePixelFormat );
510 ret = physdev->funcs->pChoosePixelFormat( physdev, ppfd );
511 release_dc_ptr( dc );
513 return ret;
517 /******************************************************************************
518 * SetPixelFormat [GDI32.@]
519 * Sets pixel format of device context
521 * PARAMS
522 * hdc [I] Device context to search for best pixel match
523 * iPixelFormat [I] Pixel format index
524 * ppfd [I] Pixel format for which a match is sought
526 * RETURNS
527 * Success: TRUE
528 * Failure: FALSE
530 BOOL WINAPI SetPixelFormat( HDC hdc, INT iPixelFormat,
531 const PIXELFORMATDESCRIPTOR *ppfd)
533 INT bRet = FALSE;
534 DC * dc = get_dc_ptr( hdc );
536 TRACE("(%p,%d,%p)\n",hdc,iPixelFormat,ppfd);
538 if (dc)
540 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetPixelFormat );
541 update_dc( dc );
542 bRet = physdev->funcs->pSetPixelFormat( physdev, iPixelFormat, ppfd );
543 release_dc_ptr( dc );
545 return bRet;
549 /******************************************************************************
550 * GetPixelFormat [GDI32.@]
551 * Gets index of pixel format of DC
553 * PARAMETERS
554 * hdc [I] Device context whose pixel format index is sought
556 * RETURNS
557 * Success: Currently selected pixel format
558 * Failure: 0
560 INT WINAPI GetPixelFormat( HDC hdc )
562 INT ret = 0;
563 DC * dc = get_dc_ptr( hdc );
565 TRACE("(%p)\n",hdc);
567 if (dc)
569 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetPixelFormat );
570 update_dc( dc );
571 ret = physdev->funcs->pGetPixelFormat( physdev );
572 release_dc_ptr( dc );
574 return ret;
578 /******************************************************************************
579 * DescribePixelFormat [GDI32.@]
580 * Gets info about pixel format from DC
582 * PARAMS
583 * hdc [I] Device context
584 * iPixelFormat [I] Pixel format selector
585 * nBytes [I] Size of buffer
586 * ppfd [O] Pointer to structure to receive pixel format data
588 * RETURNS
589 * Success: Maximum pixel format index of the device context
590 * Failure: 0
592 INT WINAPI DescribePixelFormat( HDC hdc, INT iPixelFormat, UINT nBytes,
593 LPPIXELFORMATDESCRIPTOR ppfd )
595 INT ret = 0;
596 DC * dc = get_dc_ptr( hdc );
598 TRACE("(%p,%d,%d,%p): stub\n",hdc,iPixelFormat,nBytes,ppfd);
600 if (dc)
602 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pDescribePixelFormat );
603 update_dc( dc );
604 ret = physdev->funcs->pDescribePixelFormat( physdev, iPixelFormat, nBytes, ppfd );
605 release_dc_ptr( dc );
607 return ret;
611 /******************************************************************************
612 * SwapBuffers [GDI32.@]
613 * Exchanges front and back buffers of window
615 * PARAMS
616 * hdc [I] Device context whose buffers get swapped
618 * RETURNS
619 * Success: TRUE
620 * Failure: FALSE
622 BOOL WINAPI SwapBuffers( HDC hdc )
624 INT bRet = FALSE;
625 DC * dc = get_dc_ptr( hdc );
627 TRACE("(%p)\n",hdc);
629 if (dc)
631 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSwapBuffers );
632 update_dc( dc );
633 bRet = physdev->funcs->pSwapBuffers( physdev );
634 release_dc_ptr( dc );
636 return bRet;
640 /***********************************************************************
641 * PaintRgn (GDI32.@)
643 BOOL WINAPI PaintRgn( HDC hdc, HRGN hrgn )
645 BOOL ret = FALSE;
646 DC * dc = get_dc_ptr( hdc );
648 if (dc)
650 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPaintRgn );
651 update_dc( dc );
652 ret = physdev->funcs->pPaintRgn( physdev, hrgn );
653 release_dc_ptr( dc );
655 return ret;
659 /***********************************************************************
660 * FillRgn (GDI32.@)
662 BOOL WINAPI FillRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush )
664 BOOL retval = FALSE;
665 DC * dc = get_dc_ptr( hdc );
667 if (dc)
669 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pFillRgn );
670 update_dc( dc );
671 retval = physdev->funcs->pFillRgn( physdev, hrgn, hbrush );
672 release_dc_ptr( dc );
674 return retval;
678 /***********************************************************************
679 * FrameRgn (GDI32.@)
681 BOOL WINAPI FrameRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush,
682 INT nWidth, INT nHeight )
684 BOOL ret = FALSE;
685 DC *dc = get_dc_ptr( hdc );
687 if (dc)
689 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pFrameRgn );
690 update_dc( dc );
691 ret = physdev->funcs->pFrameRgn( physdev, hrgn, hbrush, nWidth, nHeight );
692 release_dc_ptr( dc );
694 return ret;
698 /***********************************************************************
699 * InvertRgn (GDI32.@)
701 BOOL WINAPI InvertRgn( HDC hdc, HRGN hrgn )
703 BOOL ret = FALSE;
704 DC *dc = get_dc_ptr( hdc );
706 if (dc)
708 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pInvertRgn );
709 update_dc( dc );
710 ret = physdev->funcs->pInvertRgn( physdev, hrgn );
711 release_dc_ptr( dc );
713 return ret;
717 /**********************************************************************
718 * Polyline (GDI32.@)
720 BOOL WINAPI Polyline( HDC hdc, const POINT* pt, INT count )
722 BOOL ret = FALSE;
723 DC * dc = get_dc_ptr( hdc );
725 if (dc)
727 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPolyline );
728 update_dc( dc );
729 ret = physdev->funcs->pPolyline( physdev, pt, count );
730 release_dc_ptr( dc );
732 return ret;
735 /**********************************************************************
736 * PolylineTo (GDI32.@)
738 BOOL WINAPI PolylineTo( HDC hdc, const POINT* pt, DWORD cCount )
740 DC * dc = get_dc_ptr( hdc );
741 PHYSDEV physdev;
742 BOOL ret;
744 if(!dc) return FALSE;
746 update_dc( dc );
747 physdev = GET_DC_PHYSDEV( dc, pPolylineTo );
748 ret = physdev->funcs->pPolylineTo( physdev, pt, cCount );
750 if (ret && cCount)
752 dc->CursPosX = pt[cCount-1].x;
753 dc->CursPosY = pt[cCount-1].y;
755 release_dc_ptr( dc );
756 return ret;
760 /**********************************************************************
761 * Polygon (GDI32.@)
763 BOOL WINAPI Polygon( HDC hdc, const POINT* pt, INT count )
765 BOOL ret = FALSE;
766 DC * dc = get_dc_ptr( hdc );
768 if (dc)
770 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPolygon );
771 update_dc( dc );
772 ret = physdev->funcs->pPolygon( physdev, pt, count );
773 release_dc_ptr( dc );
775 return ret;
779 /**********************************************************************
780 * PolyPolygon (GDI32.@)
782 BOOL WINAPI PolyPolygon( HDC hdc, const POINT* pt, const INT* counts,
783 UINT polygons )
785 BOOL ret = FALSE;
786 DC * dc = get_dc_ptr( hdc );
788 if (dc)
790 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPolyPolygon );
791 update_dc( dc );
792 ret = physdev->funcs->pPolyPolygon( physdev, pt, counts, polygons );
793 release_dc_ptr( dc );
795 return ret;
798 /**********************************************************************
799 * PolyPolyline (GDI32.@)
801 BOOL WINAPI PolyPolyline( HDC hdc, const POINT* pt, const DWORD* counts,
802 DWORD polylines )
804 BOOL ret = FALSE;
805 DC * dc = get_dc_ptr( hdc );
807 if (dc)
809 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPolyPolyline );
810 update_dc( dc );
811 ret = physdev->funcs->pPolyPolyline( physdev, pt, counts, polylines );
812 release_dc_ptr( dc );
814 return ret;
817 /**********************************************************************
818 * ExtFloodFill (GDI32.@)
820 BOOL WINAPI ExtFloodFill( HDC hdc, INT x, INT y, COLORREF color,
821 UINT fillType )
823 BOOL ret = FALSE;
824 DC * dc = get_dc_ptr( hdc );
826 if (dc)
828 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pExtFloodFill );
830 update_dc( dc );
831 ret = physdev->funcs->pExtFloodFill( physdev, x, y, color, fillType );
832 release_dc_ptr( dc );
834 return ret;
838 /**********************************************************************
839 * FloodFill (GDI32.@)
841 BOOL WINAPI FloodFill( HDC hdc, INT x, INT y, COLORREF color )
843 return ExtFloodFill( hdc, x, y, color, FLOODFILLBORDER );
847 /******************************************************************************
848 * PolyBezier [GDI32.@]
849 * Draws one or more Bezier curves
851 * PARAMS
852 * hDc [I] Handle to device context
853 * lppt [I] Pointer to endpoints and control points
854 * cPoints [I] Count of endpoints and control points
856 * RETURNS
857 * Success: TRUE
858 * Failure: FALSE
860 BOOL WINAPI PolyBezier( HDC hdc, const POINT* lppt, DWORD cPoints )
862 PHYSDEV physdev;
863 BOOL ret;
864 DC * dc;
866 /* cPoints must be 3 * n + 1 (where n>=1) */
867 if (cPoints == 1 || (cPoints % 3) != 1) return FALSE;
869 dc = get_dc_ptr( hdc );
870 if(!dc) return FALSE;
872 update_dc( dc );
873 physdev = GET_DC_PHYSDEV( dc, pPolyBezier );
874 ret = physdev->funcs->pPolyBezier( physdev, lppt, cPoints );
875 release_dc_ptr( dc );
876 return ret;
879 /******************************************************************************
880 * PolyBezierTo [GDI32.@]
881 * Draws one or more Bezier curves
883 * PARAMS
884 * hDc [I] Handle to device context
885 * lppt [I] Pointer to endpoints and control points
886 * cPoints [I] Count of endpoints and control points
888 * RETURNS
889 * Success: TRUE
890 * Failure: FALSE
892 BOOL WINAPI PolyBezierTo( HDC hdc, const POINT* lppt, DWORD cPoints )
894 DC * dc;
895 BOOL ret;
896 PHYSDEV physdev;
898 /* cbPoints must be 3 * n (where n>=1) */
899 if (!cPoints || (cPoints % 3) != 0) return FALSE;
901 dc = get_dc_ptr( hdc );
902 if(!dc) return FALSE;
904 update_dc( dc );
905 physdev = GET_DC_PHYSDEV( dc, pPolyBezierTo );
906 ret = physdev->funcs->pPolyBezierTo( physdev, lppt, cPoints );
908 if(ret) {
909 dc->CursPosX = lppt[cPoints-1].x;
910 dc->CursPosY = lppt[cPoints-1].y;
912 release_dc_ptr( dc );
913 return ret;
916 /***********************************************************************
917 * AngleArc (GDI32.@)
919 BOOL WINAPI AngleArc(HDC hdc, INT x, INT y, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle)
921 PHYSDEV physdev;
922 BOOL result;
923 DC *dc;
925 if( (signed int)dwRadius < 0 )
926 return FALSE;
928 dc = get_dc_ptr( hdc );
929 if(!dc) return FALSE;
931 update_dc( dc );
932 physdev = GET_DC_PHYSDEV( dc, pAngleArc );
933 result = physdev->funcs->pAngleArc( physdev, x, y, dwRadius, eStartAngle, eSweepAngle );
935 if (result) {
936 dc->CursPosX = GDI_ROUND( x + cos((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius );
937 dc->CursPosY = GDI_ROUND( y - sin((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius );
939 release_dc_ptr( dc );
940 return result;
943 /***********************************************************************
944 * PolyDraw (GDI32.@)
946 BOOL WINAPI PolyDraw(HDC hdc, const POINT *lppt, const BYTE *lpbTypes,
947 DWORD cCount)
949 DC *dc = get_dc_ptr( hdc );
950 PHYSDEV physdev;
951 BOOL result;
953 if(!dc) return FALSE;
955 update_dc( dc );
956 physdev = GET_DC_PHYSDEV( dc, pPolyDraw );
957 result = physdev->funcs->pPolyDraw( physdev, lppt, lpbTypes, cCount );
958 release_dc_ptr( dc );
959 return result;
963 /**********************************************************************
964 * LineDDA (GDI32.@)
966 BOOL WINAPI LineDDA(INT nXStart, INT nYStart, INT nXEnd, INT nYEnd,
967 LINEDDAPROC callback, LPARAM lParam )
969 INT xadd = 1, yadd = 1;
970 INT err,erradd;
971 INT cnt;
972 INT dx = nXEnd - nXStart;
973 INT dy = nYEnd - nYStart;
975 if (dx < 0)
977 dx = -dx;
978 xadd = -1;
980 if (dy < 0)
982 dy = -dy;
983 yadd = -1;
985 if (dx > dy) /* line is "more horizontal" */
987 err = 2*dy - dx; erradd = 2*dy - 2*dx;
988 for(cnt = 0;cnt < dx; cnt++)
990 callback(nXStart,nYStart,lParam);
991 if (err > 0)
993 nYStart += yadd;
994 err += erradd;
996 else err += 2*dy;
997 nXStart += xadd;
1000 else /* line is "more vertical" */
1002 err = 2*dx - dy; erradd = 2*dx - 2*dy;
1003 for(cnt = 0;cnt < dy; cnt++)
1005 callback(nXStart,nYStart,lParam);
1006 if (err > 0)
1008 nXStart += xadd;
1009 err += erradd;
1011 else err += 2*dx;
1012 nYStart += yadd;
1015 return TRUE;
1019 /******************************************************************
1021 * *Very* simple bezier drawing code,
1023 * It uses a recursive algorithm to divide the curve in a series
1024 * of straight line segments. Not ideal but sufficient for me.
1025 * If you are in need for something better look for some incremental
1026 * algorithm.
1028 * 7 July 1998 Rein Klazes
1032 * some macro definitions for bezier drawing
1034 * to avoid truncation errors the coordinates are
1035 * shifted upwards. When used in drawing they are
1036 * shifted down again, including correct rounding
1037 * and avoiding floating point arithmetic
1038 * 4 bits should allow 27 bits coordinates which I saw
1039 * somewhere in the win32 doc's
1043 #define BEZIERSHIFTBITS 4
1044 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
1045 #define BEZIERPIXEL BEZIERSHIFTUP(1)
1046 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
1047 /* maximum depth of recursion */
1048 #define BEZIERMAXDEPTH 8
1050 /* size of array to store points on */
1051 /* enough for one curve */
1052 #define BEZIER_INITBUFSIZE (150)
1054 /* calculate Bezier average, in this case the middle
1055 * correctly rounded...
1056 * */
1058 #define BEZIERMIDDLE(Mid, P1, P2) \
1059 (Mid).x=((P1).x+(P2).x + 1)/2;\
1060 (Mid).y=((P1).y+(P2).y + 1)/2;
1062 /**********************************************************
1063 * BezierCheck helper function to check
1064 * that recursion can be terminated
1065 * Points[0] and Points[3] are begin and endpoint
1066 * Points[1] and Points[2] are control points
1067 * level is the recursion depth
1068 * returns true if the recursion can be terminated
1070 static BOOL BezierCheck( int level, POINT *Points)
1072 INT dx, dy;
1073 dx=Points[3].x-Points[0].x;
1074 dy=Points[3].y-Points[0].y;
1075 if(abs(dy)<=abs(dx)){/* shallow line */
1076 /* check that control points are between begin and end */
1077 if(Points[1].x < Points[0].x){
1078 if(Points[1].x < Points[3].x)
1079 return FALSE;
1080 }else
1081 if(Points[1].x > Points[3].x)
1082 return FALSE;
1083 if(Points[2].x < Points[0].x){
1084 if(Points[2].x < Points[3].x)
1085 return FALSE;
1086 }else
1087 if(Points[2].x > Points[3].x)
1088 return FALSE;
1089 dx=BEZIERSHIFTDOWN(dx);
1090 if(!dx) return TRUE;
1091 if(abs(Points[1].y-Points[0].y-(dy/dx)*
1092 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
1093 abs(Points[2].y-Points[0].y-(dy/dx)*
1094 BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
1095 return FALSE;
1096 else
1097 return TRUE;
1098 }else{ /* steep line */
1099 /* check that control points are between begin and end */
1100 if(Points[1].y < Points[0].y){
1101 if(Points[1].y < Points[3].y)
1102 return FALSE;
1103 }else
1104 if(Points[1].y > Points[3].y)
1105 return FALSE;
1106 if(Points[2].y < Points[0].y){
1107 if(Points[2].y < Points[3].y)
1108 return FALSE;
1109 }else
1110 if(Points[2].y > Points[3].y)
1111 return FALSE;
1112 dy=BEZIERSHIFTDOWN(dy);
1113 if(!dy) return TRUE;
1114 if(abs(Points[1].x-Points[0].x-(dx/dy)*
1115 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
1116 abs(Points[2].x-Points[0].x-(dx/dy)*
1117 BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
1118 return FALSE;
1119 else
1120 return TRUE;
1124 /* Helper for GDI_Bezier.
1125 * Just handles one Bezier, so Points should point to four POINTs
1127 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
1128 INT *nPtsOut, INT level )
1130 if(*nPtsOut == *dwOut) {
1131 *dwOut *= 2;
1132 *PtsOut = HeapReAlloc( GetProcessHeap(), 0, *PtsOut,
1133 *dwOut * sizeof(POINT) );
1136 if(!level || BezierCheck(level, Points)) {
1137 if(*nPtsOut == 0) {
1138 (*PtsOut)[0].x = BEZIERSHIFTDOWN(Points[0].x);
1139 (*PtsOut)[0].y = BEZIERSHIFTDOWN(Points[0].y);
1140 *nPtsOut = 1;
1142 (*PtsOut)[*nPtsOut].x = BEZIERSHIFTDOWN(Points[3].x);
1143 (*PtsOut)[*nPtsOut].y = BEZIERSHIFTDOWN(Points[3].y);
1144 (*nPtsOut) ++;
1145 } else {
1146 POINT Points2[4]; /* for the second recursive call */
1147 Points2[3]=Points[3];
1148 BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
1149 BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
1150 BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
1152 BEZIERMIDDLE(Points[1], Points[0], Points[1]);
1153 BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1154 BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1156 Points2[0]=Points[3];
1158 /* do the two halves */
1159 GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
1160 GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
1166 /***********************************************************************
1167 * GDI_Bezier [INTERNAL]
1168 * Calculate line segments that approximate -what microsoft calls- a bezier
1169 * curve.
1170 * The routine recursively divides the curve in two parts until a straight
1171 * line can be drawn
1173 * PARAMS
1175 * Points [I] Ptr to count POINTs which are the end and control points
1176 * of the set of Bezier curves to flatten.
1177 * count [I] Number of Points. Must be 3n+1.
1178 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
1179 * lines+1).
1181 * RETURNS
1183 * Ptr to an array of POINTs that contain the lines that approximate the
1184 * Beziers. The array is allocated on the process heap and it is the caller's
1185 * responsibility to HeapFree it. [this is not a particularly nice interface
1186 * but since we can't know in advance how many points we will generate, the
1187 * alternative would be to call the function twice, once to determine the size
1188 * and a second time to do the work - I decided this was too much of a pain].
1190 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
1192 POINT *out;
1193 INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
1195 if (count == 1 || (count - 1) % 3 != 0) {
1196 ERR("Invalid no. of points %d\n", count);
1197 return NULL;
1199 *nPtsOut = 0;
1200 out = HeapAlloc( GetProcessHeap(), 0, dwOut * sizeof(POINT));
1201 for(Bezier = 0; Bezier < (count-1)/3; Bezier++) {
1202 POINT ptBuf[4];
1203 memcpy(ptBuf, Points + Bezier * 3, sizeof(POINT) * 4);
1204 for(i = 0; i < 4; i++) {
1205 ptBuf[i].x = BEZIERSHIFTUP(ptBuf[i].x);
1206 ptBuf[i].y = BEZIERSHIFTUP(ptBuf[i].y);
1208 GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
1210 TRACE("Produced %d points\n", *nPtsOut);
1211 return out;
1214 /******************************************************************************
1215 * GdiGradientFill (GDI32.@)
1217 BOOL WINAPI GdiGradientFill( HDC hdc, TRIVERTEX *vert_array, ULONG nvert,
1218 void *grad_array, ULONG ngrad, ULONG mode )
1220 DC *dc;
1221 PHYSDEV physdev;
1222 BOOL ret;
1223 ULONG i;
1225 TRACE("%p vert_array:%p nvert:%d grad_array:%p ngrad:%d\n", hdc, vert_array, nvert, grad_array, ngrad);
1227 if (!vert_array || !nvert || !grad_array || !ngrad || mode > GRADIENT_FILL_TRIANGLE)
1229 SetLastError( ERROR_INVALID_PARAMETER );
1230 return FALSE;
1232 for (i = 0; i < ngrad * (mode == GRADIENT_FILL_TRIANGLE ? 3 : 2); i++)
1233 if (((ULONG *)grad_array)[i] >= nvert) return FALSE;
1235 if (!(dc = get_dc_ptr( hdc )))
1237 SetLastError( ERROR_INVALID_PARAMETER );
1238 return FALSE;
1240 update_dc( dc );
1241 physdev = GET_DC_PHYSDEV( dc, pGradientFill );
1242 ret = physdev->funcs->pGradientFill( physdev, vert_array, nvert, grad_array, ngrad, mode );
1243 release_dc_ptr( dc );
1244 return ret;
1247 /******************************************************************************
1248 * GdiDrawStream (GDI32.@)
1251 BOOL WINAPI GdiDrawStream( HDC hdc, ULONG in, void * pvin )
1253 FIXME("stub: %p, %d, %p\n", hdc, in, pvin);
1254 return FALSE;