2 * GDI drawing functions.
4 * Copyright 1993, 1994 Alexandre Julliard
5 * Copyright 1997 Bertho A. Stultiens
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
24 #include "wine/port.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 );
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;
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 )
80 if ((prev = SelectObject( dev->hdc, brush )))
82 ret = PaintRgn( dev->hdc, rgn );
83 SelectObject( dev->hdc, prev );
88 BOOL nulldrv_FrameRgn( PHYSDEV dev, HRGN rgn, HBRUSH brush, INT width, INT height )
91 HRGN tmp = CreateRectRgn( 0, 0, 0, 0 );
95 if (REGION_FrameRgn( tmp, rgn, width, height )) ret = FillRgn( dev->hdc, tmp, brush );
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 );
111 BOOL nulldrv_PolyBezier( PHYSDEV dev, const POINT *points, DWORD count )
117 if ((pts = GDI_Bezier( points, count, &n )))
119 ret = Polyline( dev->hdc, pts, n );
120 HeapFree( GetProcessHeap(), 0, pts );
125 BOOL nulldrv_PolyBezierTo( PHYSDEV dev, const POINT *points, DWORD count )
128 POINT *pts = HeapAlloc( GetProcessHeap(), 0, sizeof(POINT) * (count + 1) );
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 );
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++)
151 case PT_LINETO | PT_CLOSEFIGURE:
155 if (i + 2 >= count) return FALSE;
156 if (types[i + 1] != PT_BEZIERTO) return FALSE;
157 if ((types[i + 2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) return FALSE;
166 line_pts = HeapAlloc( GetProcessHeap(), 0, space * sizeof(POINT) );
169 GetCurrentPositionEx( dev->hdc, &line_pts[0] );
170 for (i = 0; i < count; i++)
175 if (num_pts >= 2) Polyline( dev->hdc, line_pts, num_pts );
177 line_pts[num_pts++] = points[i];
180 case (PT_LINETO | PT_CLOSEFIGURE):
181 line_pts[num_pts++] = points[i];
184 bzr[0].x = line_pts[num_pts - 1].x;
185 bzr[0].y = line_pts[num_pts - 1].y;
186 memcpy( &bzr[1], &points[i], 3 * sizeof(POINT) );
188 if ((bzr_pts = GDI_Bezier( bzr, 4, &num_bzr_pts )))
190 size = num_pts + (count - i) + num_bzr_pts;
194 line_pts = HeapReAlloc( GetProcessHeap(), 0, line_pts, space * sizeof(POINT) );
196 memcpy( &line_pts[num_pts], &bzr_pts[1], (num_bzr_pts - 1) * sizeof(POINT) );
197 num_pts += num_bzr_pts - 1;
198 HeapFree( GetProcessHeap(), 0, bzr_pts );
203 if (types[i] & PT_CLOSEFIGURE) line_pts[num_pts++] = line_pts[0];
206 if (num_pts >= 2) Polyline( dev->hdc, line_pts, num_pts );
207 MoveToEx( dev->hdc, line_pts[num_pts - 1].x, line_pts[num_pts - 1].y, NULL );
208 HeapFree( GetProcessHeap(), 0, line_pts );
212 BOOL nulldrv_PolylineTo( PHYSDEV dev, const POINT *points, INT count )
217 if (!count) return FALSE;
218 if ((pts = HeapAlloc( GetProcessHeap(), 0, sizeof(POINT) * (count + 1) )))
220 GetCurrentPositionEx( dev->hdc, &pts[0] );
221 memcpy( pts + 1, points, sizeof(POINT) * count );
222 ret = Polyline( dev->hdc, pts, count + 1 );
223 HeapFree( GetProcessHeap(), 0, pts );
228 /***********************************************************************
231 BOOL WINAPI LineTo( HDC hdc, INT x, INT y )
233 DC * dc = get_dc_ptr( hdc );
237 if(!dc) return FALSE;
240 physdev = GET_DC_PHYSDEV( dc, pLineTo );
241 ret = physdev->funcs->pLineTo( physdev, x, y );
247 release_dc_ptr( dc );
252 /***********************************************************************
255 BOOL WINAPI MoveToEx( HDC hdc, INT x, INT y, LPPOINT pt )
259 DC * dc = get_dc_ptr( hdc );
261 if(!dc) return FALSE;
264 pt->x = dc->CursPosX;
265 pt->y = dc->CursPosY;
270 physdev = GET_DC_PHYSDEV( dc, pMoveTo );
271 ret = physdev->funcs->pMoveTo( physdev, x, y );
272 release_dc_ptr( dc );
277 /***********************************************************************
280 BOOL WINAPI Arc( HDC hdc, INT left, INT top, INT right,
281 INT bottom, INT xstart, INT ystart,
286 DC * dc = get_dc_ptr( hdc );
288 if (!dc) return FALSE;
290 physdev = GET_DC_PHYSDEV( dc, pArc );
291 ret = physdev->funcs->pArc( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
292 release_dc_ptr( dc );
296 /***********************************************************************
299 BOOL WINAPI ArcTo( HDC hdc,
301 INT right, INT bottom,
302 INT xstart, INT ystart,
305 double width = fabs(right-left),
306 height = fabs(bottom-top),
309 xcenter = right > left ? left+xradius : right+xradius,
310 ycenter = bottom > top ? top+yradius : bottom+yradius,
314 DC * dc = get_dc_ptr( hdc );
315 if(!dc) return FALSE;
318 physdev = GET_DC_PHYSDEV( dc, pArcTo );
319 result = physdev->funcs->pArcTo( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
322 angle = atan2(((yend-ycenter)/height),
323 ((xend-xcenter)/width));
324 dc->CursPosX = GDI_ROUND(xcenter+(cos(angle)*xradius));
325 dc->CursPosY = GDI_ROUND(ycenter+(sin(angle)*yradius));
327 release_dc_ptr( dc );
332 /***********************************************************************
335 BOOL WINAPI Pie( HDC hdc, INT left, INT top,
336 INT right, INT bottom, INT xstart, INT ystart,
341 DC * dc = get_dc_ptr( hdc );
342 if (!dc) return FALSE;
345 physdev = GET_DC_PHYSDEV( dc, pPie );
346 ret = physdev->funcs->pPie( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
347 release_dc_ptr( dc );
352 /***********************************************************************
355 BOOL WINAPI Chord( HDC hdc, INT left, INT top,
356 INT right, INT bottom, INT xstart, INT ystart,
361 DC * dc = get_dc_ptr( hdc );
362 if (!dc) return FALSE;
365 physdev = GET_DC_PHYSDEV( dc, pChord );
366 ret = physdev->funcs->pChord( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
367 release_dc_ptr( dc );
372 /***********************************************************************
375 BOOL WINAPI Ellipse( HDC hdc, INT left, INT top,
376 INT right, INT bottom )
380 DC * dc = get_dc_ptr( hdc );
381 if (!dc) return FALSE;
384 physdev = GET_DC_PHYSDEV( dc, pEllipse );
385 ret = physdev->funcs->pEllipse( physdev, left, top, right, bottom );
386 release_dc_ptr( dc );
391 /***********************************************************************
392 * Rectangle (GDI32.@)
394 BOOL WINAPI Rectangle( HDC hdc, INT left, INT top,
395 INT right, INT bottom )
399 DC * dc = get_dc_ptr( hdc );
401 if (!dc) return FALSE;
403 physdev = GET_DC_PHYSDEV( dc, pRectangle );
404 ret = physdev->funcs->pRectangle( physdev, left, top, right, bottom );
405 release_dc_ptr( dc );
410 /***********************************************************************
411 * RoundRect (GDI32.@)
413 BOOL WINAPI RoundRect( HDC hdc, INT left, INT top, INT right,
414 INT bottom, INT ell_width, INT ell_height )
418 DC *dc = get_dc_ptr( hdc );
420 if (!dc) return FALSE;
422 physdev = GET_DC_PHYSDEV( dc, pRoundRect );
423 ret = physdev->funcs->pRoundRect( physdev, left, top, right, bottom, ell_width, ell_height );
424 release_dc_ptr( dc );
428 /***********************************************************************
431 COLORREF WINAPI SetPixel( HDC hdc, INT x, INT y, COLORREF color )
435 DC * dc = get_dc_ptr( hdc );
439 physdev = GET_DC_PHYSDEV( dc, pSetPixel );
440 ret = physdev->funcs->pSetPixel( physdev, x, y, color );
441 release_dc_ptr( dc );
445 /***********************************************************************
446 * SetPixelV (GDI32.@)
448 BOOL WINAPI SetPixelV( HDC hdc, INT x, INT y, COLORREF color )
451 DC * dc = get_dc_ptr( hdc );
453 if (!dc) return FALSE;
455 physdev = GET_DC_PHYSDEV( dc, pSetPixel );
456 physdev->funcs->pSetPixel( physdev, x, y, color );
457 release_dc_ptr( dc );
461 /***********************************************************************
464 COLORREF WINAPI GetPixel( HDC hdc, INT x, INT y )
468 DC * dc = get_dc_ptr( hdc );
470 if (!dc) return CLR_INVALID;
472 physdev = GET_DC_PHYSDEV( dc, pGetPixel );
473 ret = physdev->funcs->pGetPixel( physdev, x, y );
474 release_dc_ptr( dc );
479 /******************************************************************************
480 * GdiSetPixelFormat [GDI32.@]
482 * Probably not the correct semantics, it's supposed to be an internal backend for SetPixelFormat.
484 BOOL WINAPI GdiSetPixelFormat( HDC hdc, INT format, const PIXELFORMATDESCRIPTOR *descr )
489 TRACE("(%p,%d,%p)\n", hdc, format, descr);
491 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
493 if (!dc->pixel_format) dc->pixel_format = format;
494 else ret = (dc->pixel_format == format);
495 release_dc_ptr( dc );
500 /******************************************************************************
501 * GdiDescribePixelFormat [GDI32.@]
503 * Probably not the correct semantics, it's supposed to be an internal backend for DescribePixelFormat.
505 INT WINAPI GdiDescribePixelFormat( HDC hdc, INT format, UINT size, PIXELFORMATDESCRIPTOR *descr )
507 FIXME( "(%p,%d,%d,%p): stub\n", hdc, format, size, descr );
512 /******************************************************************************
513 * GdiSwapBuffers [GDI32.@]
515 * Probably not the correct semantics, it's supposed to be an internal backend for SwapBuffers.
517 BOOL WINAPI GdiSwapBuffers( HDC hdc )
519 FIXME( "(%p): stub\n", hdc );
524 /***********************************************************************
527 BOOL WINAPI PaintRgn( HDC hdc, HRGN hrgn )
531 DC * dc = get_dc_ptr( hdc );
533 if (!dc) return FALSE;
535 physdev = GET_DC_PHYSDEV( dc, pPaintRgn );
536 ret = physdev->funcs->pPaintRgn( physdev, hrgn );
537 release_dc_ptr( dc );
542 /***********************************************************************
545 BOOL WINAPI FillRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush )
549 DC * dc = get_dc_ptr( hdc );
551 if (!dc) return FALSE;
553 physdev = GET_DC_PHYSDEV( dc, pFillRgn );
554 retval = physdev->funcs->pFillRgn( physdev, hrgn, hbrush );
555 release_dc_ptr( dc );
560 /***********************************************************************
563 BOOL WINAPI FrameRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush,
564 INT nWidth, INT nHeight )
568 DC *dc = get_dc_ptr( hdc );
570 if (!dc) return FALSE;
572 physdev = GET_DC_PHYSDEV( dc, pFrameRgn );
573 ret = physdev->funcs->pFrameRgn( physdev, hrgn, hbrush, nWidth, nHeight );
574 release_dc_ptr( dc );
579 /***********************************************************************
580 * InvertRgn (GDI32.@)
582 BOOL WINAPI InvertRgn( HDC hdc, HRGN hrgn )
586 DC *dc = get_dc_ptr( hdc );
588 if (!dc) return FALSE;
590 physdev = GET_DC_PHYSDEV( dc, pInvertRgn );
591 ret = physdev->funcs->pInvertRgn( physdev, hrgn );
592 release_dc_ptr( dc );
597 /**********************************************************************
600 BOOL WINAPI Polyline( HDC hdc, const POINT* pt, INT count )
604 DC * dc = get_dc_ptr( hdc );
606 if (!dc) return FALSE;
608 physdev = GET_DC_PHYSDEV( dc, pPolyline );
609 ret = physdev->funcs->pPolyline( physdev, pt, count );
610 release_dc_ptr( dc );
614 /**********************************************************************
615 * PolylineTo (GDI32.@)
617 BOOL WINAPI PolylineTo( HDC hdc, const POINT* pt, DWORD cCount )
619 DC * dc = get_dc_ptr( hdc );
623 if(!dc) return FALSE;
626 physdev = GET_DC_PHYSDEV( dc, pPolylineTo );
627 ret = physdev->funcs->pPolylineTo( physdev, pt, cCount );
631 dc->CursPosX = pt[cCount-1].x;
632 dc->CursPosY = pt[cCount-1].y;
634 release_dc_ptr( dc );
639 /**********************************************************************
642 BOOL WINAPI Polygon( HDC hdc, const POINT* pt, INT count )
646 DC * dc = get_dc_ptr( hdc );
648 if (!dc) return FALSE;
650 physdev = GET_DC_PHYSDEV( dc, pPolygon );
651 ret = physdev->funcs->pPolygon( physdev, pt, count );
652 release_dc_ptr( dc );
657 /**********************************************************************
658 * PolyPolygon (GDI32.@)
660 BOOL WINAPI PolyPolygon( HDC hdc, const POINT* pt, const INT* counts,
665 DC * dc = get_dc_ptr( hdc );
667 if (!dc) return FALSE;
669 physdev = GET_DC_PHYSDEV( dc, pPolyPolygon );
670 ret = physdev->funcs->pPolyPolygon( physdev, pt, counts, polygons );
671 release_dc_ptr( dc );
675 /**********************************************************************
676 * PolyPolyline (GDI32.@)
678 BOOL WINAPI PolyPolyline( HDC hdc, const POINT* pt, const DWORD* counts,
683 DC * dc = get_dc_ptr( hdc );
685 if (!dc) return FALSE;
687 physdev = GET_DC_PHYSDEV( dc, pPolyPolyline );
688 ret = physdev->funcs->pPolyPolyline( physdev, pt, counts, polylines );
689 release_dc_ptr( dc );
693 /**********************************************************************
694 * ExtFloodFill (GDI32.@)
696 BOOL WINAPI ExtFloodFill( HDC hdc, INT x, INT y, COLORREF color,
701 DC * dc = get_dc_ptr( hdc );
703 if (!dc) return FALSE;
705 physdev = GET_DC_PHYSDEV( dc, pExtFloodFill );
706 ret = physdev->funcs->pExtFloodFill( physdev, x, y, color, fillType );
707 release_dc_ptr( dc );
712 /**********************************************************************
713 * FloodFill (GDI32.@)
715 BOOL WINAPI FloodFill( HDC hdc, INT x, INT y, COLORREF color )
717 return ExtFloodFill( hdc, x, y, color, FLOODFILLBORDER );
721 /******************************************************************************
722 * PolyBezier [GDI32.@]
723 * Draws one or more Bezier curves
726 * hDc [I] Handle to device context
727 * lppt [I] Pointer to endpoints and control points
728 * cPoints [I] Count of endpoints and control points
734 BOOL WINAPI PolyBezier( HDC hdc, const POINT* lppt, DWORD cPoints )
740 /* cPoints must be 3 * n + 1 (where n>=1) */
741 if (cPoints == 1 || (cPoints % 3) != 1) return FALSE;
743 dc = get_dc_ptr( hdc );
744 if(!dc) return FALSE;
747 physdev = GET_DC_PHYSDEV( dc, pPolyBezier );
748 ret = physdev->funcs->pPolyBezier( physdev, lppt, cPoints );
749 release_dc_ptr( dc );
753 /******************************************************************************
754 * PolyBezierTo [GDI32.@]
755 * Draws one or more Bezier curves
758 * hDc [I] Handle to device context
759 * lppt [I] Pointer to endpoints and control points
760 * cPoints [I] Count of endpoints and control points
766 BOOL WINAPI PolyBezierTo( HDC hdc, const POINT* lppt, DWORD cPoints )
772 /* cbPoints must be 3 * n (where n>=1) */
773 if (!cPoints || (cPoints % 3) != 0) return FALSE;
775 dc = get_dc_ptr( hdc );
776 if(!dc) return FALSE;
779 physdev = GET_DC_PHYSDEV( dc, pPolyBezierTo );
780 ret = physdev->funcs->pPolyBezierTo( physdev, lppt, cPoints );
783 dc->CursPosX = lppt[cPoints-1].x;
784 dc->CursPosY = lppt[cPoints-1].y;
786 release_dc_ptr( dc );
790 /***********************************************************************
793 BOOL WINAPI AngleArc(HDC hdc, INT x, INT y, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle)
799 if( (signed int)dwRadius < 0 )
802 dc = get_dc_ptr( hdc );
803 if(!dc) return FALSE;
806 physdev = GET_DC_PHYSDEV( dc, pAngleArc );
807 result = physdev->funcs->pAngleArc( physdev, x, y, dwRadius, eStartAngle, eSweepAngle );
810 dc->CursPosX = GDI_ROUND( x + cos((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius );
811 dc->CursPosY = GDI_ROUND( y - sin((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius );
813 release_dc_ptr( dc );
817 /***********************************************************************
820 BOOL WINAPI PolyDraw(HDC hdc, const POINT *lppt, const BYTE *lpbTypes,
823 DC *dc = get_dc_ptr( hdc );
827 if(!dc) return FALSE;
830 physdev = GET_DC_PHYSDEV( dc, pPolyDraw );
831 result = physdev->funcs->pPolyDraw( physdev, lppt, lpbTypes, cCount );
832 release_dc_ptr( dc );
837 /**********************************************************************
840 BOOL WINAPI LineDDA(INT nXStart, INT nYStart, INT nXEnd, INT nYEnd,
841 LINEDDAPROC callback, LPARAM lParam )
843 INT xadd = 1, yadd = 1;
846 INT dx = nXEnd - nXStart;
847 INT dy = nYEnd - nYStart;
859 if (dx > dy) /* line is "more horizontal" */
861 err = 2*dy - dx; erradd = 2*dy - 2*dx;
862 for(cnt = 0;cnt < dx; cnt++)
864 callback(nXStart,nYStart,lParam);
874 else /* line is "more vertical" */
876 err = 2*dx - dy; erradd = 2*dx - 2*dy;
877 for(cnt = 0;cnt < dy; cnt++)
879 callback(nXStart,nYStart,lParam);
893 /******************************************************************
895 * *Very* simple bezier drawing code,
897 * It uses a recursive algorithm to divide the curve in a series
898 * of straight line segments. Not ideal but sufficient for me.
899 * If you are in need for something better look for some incremental
902 * 7 July 1998 Rein Klazes
906 * some macro definitions for bezier drawing
908 * to avoid truncation errors the coordinates are
909 * shifted upwards. When used in drawing they are
910 * shifted down again, including correct rounding
911 * and avoiding floating point arithmetic
912 * 4 bits should allow 27 bits coordinates which I saw
913 * somewhere in the win32 doc's
917 #define BEZIERSHIFTBITS 4
918 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
919 #define BEZIERPIXEL BEZIERSHIFTUP(1)
920 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
921 /* maximum depth of recursion */
922 #define BEZIERMAXDEPTH 8
924 /* size of array to store points on */
925 /* enough for one curve */
926 #define BEZIER_INITBUFSIZE (150)
928 /* calculate Bezier average, in this case the middle
929 * correctly rounded...
932 #define BEZIERMIDDLE(Mid, P1, P2) \
933 (Mid).x=((P1).x+(P2).x + 1)/2;\
934 (Mid).y=((P1).y+(P2).y + 1)/2;
936 /**********************************************************
937 * BezierCheck helper function to check
938 * that recursion can be terminated
939 * Points[0] and Points[3] are begin and endpoint
940 * Points[1] and Points[2] are control points
941 * level is the recursion depth
942 * returns true if the recursion can be terminated
944 static BOOL BezierCheck( int level, POINT *Points)
947 dx=Points[3].x-Points[0].x;
948 dy=Points[3].y-Points[0].y;
949 if(abs(dy)<=abs(dx)){/* shallow line */
950 /* check that control points are between begin and end */
951 if(Points[1].x < Points[0].x){
952 if(Points[1].x < Points[3].x)
955 if(Points[1].x > Points[3].x)
957 if(Points[2].x < Points[0].x){
958 if(Points[2].x < Points[3].x)
961 if(Points[2].x > Points[3].x)
963 dx=BEZIERSHIFTDOWN(dx);
965 if(abs(Points[1].y-Points[0].y-(dy/dx)*
966 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
967 abs(Points[2].y-Points[0].y-(dy/dx)*
968 BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
972 }else{ /* steep line */
973 /* check that control points are between begin and end */
974 if(Points[1].y < Points[0].y){
975 if(Points[1].y < Points[3].y)
978 if(Points[1].y > Points[3].y)
980 if(Points[2].y < Points[0].y){
981 if(Points[2].y < Points[3].y)
984 if(Points[2].y > Points[3].y)
986 dy=BEZIERSHIFTDOWN(dy);
988 if(abs(Points[1].x-Points[0].x-(dx/dy)*
989 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
990 abs(Points[2].x-Points[0].x-(dx/dy)*
991 BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
998 /* Helper for GDI_Bezier.
999 * Just handles one Bezier, so Points should point to four POINTs
1001 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
1002 INT *nPtsOut, INT level )
1004 if(*nPtsOut == *dwOut) {
1006 *PtsOut = HeapReAlloc( GetProcessHeap(), 0, *PtsOut,
1007 *dwOut * sizeof(POINT) );
1010 if(!level || BezierCheck(level, Points)) {
1012 (*PtsOut)[0].x = BEZIERSHIFTDOWN(Points[0].x);
1013 (*PtsOut)[0].y = BEZIERSHIFTDOWN(Points[0].y);
1016 (*PtsOut)[*nPtsOut].x = BEZIERSHIFTDOWN(Points[3].x);
1017 (*PtsOut)[*nPtsOut].y = BEZIERSHIFTDOWN(Points[3].y);
1020 POINT Points2[4]; /* for the second recursive call */
1021 Points2[3]=Points[3];
1022 BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
1023 BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
1024 BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
1026 BEZIERMIDDLE(Points[1], Points[0], Points[1]);
1027 BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1028 BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1030 Points2[0]=Points[3];
1032 /* do the two halves */
1033 GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
1034 GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
1040 /***********************************************************************
1041 * GDI_Bezier [INTERNAL]
1042 * Calculate line segments that approximate -what microsoft calls- a bezier
1044 * The routine recursively divides the curve in two parts until a straight
1049 * Points [I] Ptr to count POINTs which are the end and control points
1050 * of the set of Bezier curves to flatten.
1051 * count [I] Number of Points. Must be 3n+1.
1052 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
1057 * Ptr to an array of POINTs that contain the lines that approximate the
1058 * Beziers. The array is allocated on the process heap and it is the caller's
1059 * responsibility to HeapFree it. [this is not a particularly nice interface
1060 * but since we can't know in advance how many points we will generate, the
1061 * alternative would be to call the function twice, once to determine the size
1062 * and a second time to do the work - I decided this was too much of a pain].
1064 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
1067 INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
1069 if (count == 1 || (count - 1) % 3 != 0) {
1070 ERR("Invalid no. of points %d\n", count);
1074 out = HeapAlloc( GetProcessHeap(), 0, dwOut * sizeof(POINT));
1075 for(Bezier = 0; Bezier < (count-1)/3; Bezier++) {
1077 memcpy(ptBuf, Points + Bezier * 3, sizeof(POINT) * 4);
1078 for(i = 0; i < 4; i++) {
1079 ptBuf[i].x = BEZIERSHIFTUP(ptBuf[i].x);
1080 ptBuf[i].y = BEZIERSHIFTUP(ptBuf[i].y);
1082 GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
1084 TRACE("Produced %d points\n", *nPtsOut);
1088 /******************************************************************************
1089 * GdiGradientFill (GDI32.@)
1091 BOOL WINAPI GdiGradientFill( HDC hdc, TRIVERTEX *vert_array, ULONG nvert,
1092 void *grad_array, ULONG ngrad, ULONG mode )
1099 TRACE("%p vert_array:%p nvert:%d grad_array:%p ngrad:%d\n", hdc, vert_array, nvert, grad_array, ngrad);
1101 if (!vert_array || !nvert || !grad_array || !ngrad || mode > GRADIENT_FILL_TRIANGLE)
1103 SetLastError( ERROR_INVALID_PARAMETER );
1106 for (i = 0; i < ngrad * (mode == GRADIENT_FILL_TRIANGLE ? 3 : 2); i++)
1107 if (((ULONG *)grad_array)[i] >= nvert) return FALSE;
1109 if (!(dc = get_dc_ptr( hdc )))
1111 SetLastError( ERROR_INVALID_PARAMETER );
1115 physdev = GET_DC_PHYSDEV( dc, pGradientFill );
1116 ret = physdev->funcs->pGradientFill( physdev, vert_array, nvert, grad_array, ngrad, mode );
1117 release_dc_ptr( dc );
1121 /******************************************************************************
1122 * GdiDrawStream (GDI32.@)
1125 BOOL WINAPI GdiDrawStream( HDC hdc, ULONG in, void * pvin )
1127 FIXME("stub: %p, %d, %p\n", hdc, in, pvin);