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) && (types[i + 1] == PT_BEZIERTO) &&
156 ((types[i + 2] & ~PT_CLOSEFIGURE) == PT_BEZIERTO))
167 line_pts = HeapAlloc( GetProcessHeap(), 0, space * sizeof(POINT) );
170 GetCurrentPositionEx( dev->hdc, &line_pts[0] );
171 for (i = 0; i < count; i++)
176 if (num_pts >= 2) Polyline( dev->hdc, line_pts, num_pts );
178 line_pts[num_pts++] = points[i];
181 case (PT_LINETO | PT_CLOSEFIGURE):
182 line_pts[num_pts++] = points[i];
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;
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 );
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 );
213 BOOL nulldrv_PolylineTo( PHYSDEV dev, const POINT *points, INT count )
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 );
229 /***********************************************************************
232 BOOL WINAPI LineTo( HDC hdc, INT x, INT y )
234 DC * dc = get_dc_ptr( hdc );
238 if(!dc) return FALSE;
241 physdev = GET_DC_PHYSDEV( dc, pLineTo );
242 ret = physdev->funcs->pLineTo( physdev, x, y );
248 release_dc_ptr( dc );
253 /***********************************************************************
256 BOOL WINAPI MoveToEx( HDC hdc, INT x, INT y, LPPOINT pt )
260 DC * dc = get_dc_ptr( hdc );
262 if(!dc) return FALSE;
265 pt->x = dc->CursPosX;
266 pt->y = dc->CursPosY;
271 physdev = GET_DC_PHYSDEV( dc, pMoveTo );
272 ret = physdev->funcs->pMoveTo( physdev, x, y );
273 release_dc_ptr( dc );
278 /***********************************************************************
281 BOOL WINAPI Arc( HDC hdc, INT left, INT top, INT right,
282 INT bottom, INT xstart, INT ystart,
286 DC * dc = get_dc_ptr( hdc );
290 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pArc );
292 ret = physdev->funcs->pArc( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
293 release_dc_ptr( dc );
298 /***********************************************************************
301 BOOL WINAPI ArcTo( HDC hdc,
303 INT right, INT bottom,
304 INT xstart, INT ystart,
307 double width = fabs(right-left),
308 height = fabs(bottom-top),
311 xcenter = right > left ? left+xradius : right+xradius,
312 ycenter = bottom > top ? top+yradius : bottom+yradius,
316 DC * dc = get_dc_ptr( hdc );
317 if(!dc) return FALSE;
320 physdev = GET_DC_PHYSDEV( dc, pArcTo );
321 result = physdev->funcs->pArcTo( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
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 );
334 /***********************************************************************
337 BOOL WINAPI Pie( HDC hdc, INT left, INT top,
338 INT right, INT bottom, INT xstart, INT ystart,
343 DC * dc = get_dc_ptr( hdc );
344 if (!dc) return FALSE;
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 );
354 /***********************************************************************
357 BOOL WINAPI Chord( HDC hdc, INT left, INT top,
358 INT right, INT bottom, INT xstart, INT ystart,
363 DC * dc = get_dc_ptr( hdc );
364 if (!dc) return FALSE;
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 );
374 /***********************************************************************
377 BOOL WINAPI Ellipse( HDC hdc, INT left, INT top,
378 INT right, INT bottom )
382 DC * dc = get_dc_ptr( hdc );
383 if (!dc) return FALSE;
386 physdev = GET_DC_PHYSDEV( dc, pEllipse );
387 ret = physdev->funcs->pEllipse( physdev, left, top, right, bottom );
388 release_dc_ptr( dc );
393 /***********************************************************************
394 * Rectangle (GDI32.@)
396 BOOL WINAPI Rectangle( HDC hdc, INT left, INT top,
397 INT right, INT bottom )
400 DC * dc = get_dc_ptr( hdc );
404 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRectangle );
406 ret = physdev->funcs->pRectangle( physdev, left, top, right, bottom );
407 release_dc_ptr( dc );
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 )
420 DC *dc = get_dc_ptr( hdc );
424 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRoundRect );
426 ret = physdev->funcs->pRoundRect( physdev, left, top, right, bottom, ell_width, ell_height );
427 release_dc_ptr( dc );
432 /***********************************************************************
435 COLORREF WINAPI SetPixel( HDC hdc, INT x, INT y, COLORREF color )
438 DC * dc = get_dc_ptr( hdc );
442 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetPixel );
444 ret = physdev->funcs->pSetPixel( physdev, x, y, color );
445 release_dc_ptr( dc );
450 /***********************************************************************
451 * SetPixelV (GDI32.@)
453 BOOL WINAPI SetPixelV( HDC hdc, INT x, INT y, COLORREF color )
456 DC * dc = get_dc_ptr( hdc );
460 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetPixel );
462 physdev->funcs->pSetPixel( physdev, x, y, color );
464 release_dc_ptr( dc );
469 /***********************************************************************
472 COLORREF WINAPI GetPixel( HDC hdc, INT x, INT y )
474 COLORREF ret = CLR_INVALID;
475 DC * dc = get_dc_ptr( hdc );
479 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetPixel );
481 ret = physdev->funcs->pGetPixel( physdev, x, y );
482 release_dc_ptr( dc );
488 /******************************************************************************
489 * GdiSetPixelFormat [GDI32.@]
491 * Probably not the correct semantics, it's supposed to be an internal backend for SetPixelFormat.
493 BOOL WINAPI GdiSetPixelFormat( HDC hdc, INT iPixelFormat, const PIXELFORMATDESCRIPTOR *ppfd)
496 DC * dc = get_dc_ptr( hdc );
498 TRACE("(%p,%d,%p)\n",hdc,iPixelFormat,ppfd);
502 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetPixelFormat );
504 bRet = physdev->funcs->pSetPixelFormat( physdev, iPixelFormat, ppfd );
505 release_dc_ptr( dc );
511 /******************************************************************************
512 * GetPixelFormat [GDI32.@]
513 * Gets index of pixel format of DC
516 * hdc [I] Device context whose pixel format index is sought
519 * Success: Currently selected pixel format
522 INT WINAPI GetPixelFormat( HDC hdc )
525 DC * dc = get_dc_ptr( hdc );
531 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetPixelFormat );
533 ret = physdev->funcs->pGetPixelFormat( physdev );
534 release_dc_ptr( dc );
540 /******************************************************************************
541 * GdiDescribePixelFormat [GDI32.@]
543 * Probably not the correct semantics, it's supposed to be an internal backend for DescribePixelFormat.
545 INT WINAPI GdiDescribePixelFormat( HDC hdc, INT iPixelFormat, UINT nBytes,
546 LPPIXELFORMATDESCRIPTOR ppfd )
549 DC * dc = get_dc_ptr( hdc );
551 TRACE("(%p,%d,%d,%p): stub\n",hdc,iPixelFormat,nBytes,ppfd);
555 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pDescribePixelFormat );
557 ret = physdev->funcs->pDescribePixelFormat( physdev, iPixelFormat, nBytes, ppfd );
558 release_dc_ptr( dc );
564 /******************************************************************************
565 * SwapBuffers [GDI32.@]
566 * Exchanges front and back buffers of window
569 * hdc [I] Device context whose buffers get swapped
575 BOOL WINAPI SwapBuffers( HDC hdc )
578 DC * dc = get_dc_ptr( hdc );
584 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSwapBuffers );
586 bRet = physdev->funcs->pSwapBuffers( physdev );
587 release_dc_ptr( dc );
593 /***********************************************************************
596 BOOL WINAPI PaintRgn( HDC hdc, HRGN hrgn )
599 DC * dc = get_dc_ptr( hdc );
603 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPaintRgn );
605 ret = physdev->funcs->pPaintRgn( physdev, hrgn );
606 release_dc_ptr( dc );
612 /***********************************************************************
615 BOOL WINAPI FillRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush )
618 DC * dc = get_dc_ptr( hdc );
622 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pFillRgn );
624 retval = physdev->funcs->pFillRgn( physdev, hrgn, hbrush );
625 release_dc_ptr( dc );
631 /***********************************************************************
634 BOOL WINAPI FrameRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush,
635 INT nWidth, INT nHeight )
638 DC *dc = get_dc_ptr( hdc );
642 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pFrameRgn );
644 ret = physdev->funcs->pFrameRgn( physdev, hrgn, hbrush, nWidth, nHeight );
645 release_dc_ptr( dc );
651 /***********************************************************************
652 * InvertRgn (GDI32.@)
654 BOOL WINAPI InvertRgn( HDC hdc, HRGN hrgn )
657 DC *dc = get_dc_ptr( hdc );
661 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pInvertRgn );
663 ret = physdev->funcs->pInvertRgn( physdev, hrgn );
664 release_dc_ptr( dc );
670 /**********************************************************************
673 BOOL WINAPI Polyline( HDC hdc, const POINT* pt, INT count )
676 DC * dc = get_dc_ptr( hdc );
680 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPolyline );
682 ret = physdev->funcs->pPolyline( physdev, pt, count );
683 release_dc_ptr( dc );
688 /**********************************************************************
689 * PolylineTo (GDI32.@)
691 BOOL WINAPI PolylineTo( HDC hdc, const POINT* pt, DWORD cCount )
693 DC * dc = get_dc_ptr( hdc );
697 if(!dc) return FALSE;
700 physdev = GET_DC_PHYSDEV( dc, pPolylineTo );
701 ret = physdev->funcs->pPolylineTo( physdev, pt, cCount );
705 dc->CursPosX = pt[cCount-1].x;
706 dc->CursPosY = pt[cCount-1].y;
708 release_dc_ptr( dc );
713 /**********************************************************************
716 BOOL WINAPI Polygon( HDC hdc, const POINT* pt, INT count )
719 DC * dc = get_dc_ptr( hdc );
723 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPolygon );
725 ret = physdev->funcs->pPolygon( physdev, pt, count );
726 release_dc_ptr( dc );
732 /**********************************************************************
733 * PolyPolygon (GDI32.@)
735 BOOL WINAPI PolyPolygon( HDC hdc, const POINT* pt, const INT* counts,
739 DC * dc = get_dc_ptr( hdc );
743 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPolyPolygon );
745 ret = physdev->funcs->pPolyPolygon( physdev, pt, counts, polygons );
746 release_dc_ptr( dc );
751 /**********************************************************************
752 * PolyPolyline (GDI32.@)
754 BOOL WINAPI PolyPolyline( HDC hdc, const POINT* pt, const DWORD* counts,
758 DC * dc = get_dc_ptr( hdc );
762 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPolyPolyline );
764 ret = physdev->funcs->pPolyPolyline( physdev, pt, counts, polylines );
765 release_dc_ptr( dc );
770 /**********************************************************************
771 * ExtFloodFill (GDI32.@)
773 BOOL WINAPI ExtFloodFill( HDC hdc, INT x, INT y, COLORREF color,
777 DC * dc = get_dc_ptr( hdc );
781 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pExtFloodFill );
784 ret = physdev->funcs->pExtFloodFill( physdev, x, y, color, fillType );
785 release_dc_ptr( dc );
791 /**********************************************************************
792 * FloodFill (GDI32.@)
794 BOOL WINAPI FloodFill( HDC hdc, INT x, INT y, COLORREF color )
796 return ExtFloodFill( hdc, x, y, color, FLOODFILLBORDER );
800 /******************************************************************************
801 * PolyBezier [GDI32.@]
802 * Draws one or more Bezier curves
805 * hDc [I] Handle to device context
806 * lppt [I] Pointer to endpoints and control points
807 * cPoints [I] Count of endpoints and control points
813 BOOL WINAPI PolyBezier( HDC hdc, const POINT* lppt, DWORD cPoints )
819 /* cPoints must be 3 * n + 1 (where n>=1) */
820 if (cPoints == 1 || (cPoints % 3) != 1) return FALSE;
822 dc = get_dc_ptr( hdc );
823 if(!dc) return FALSE;
826 physdev = GET_DC_PHYSDEV( dc, pPolyBezier );
827 ret = physdev->funcs->pPolyBezier( physdev, lppt, cPoints );
828 release_dc_ptr( dc );
832 /******************************************************************************
833 * PolyBezierTo [GDI32.@]
834 * Draws one or more Bezier curves
837 * hDc [I] Handle to device context
838 * lppt [I] Pointer to endpoints and control points
839 * cPoints [I] Count of endpoints and control points
845 BOOL WINAPI PolyBezierTo( HDC hdc, const POINT* lppt, DWORD cPoints )
851 /* cbPoints must be 3 * n (where n>=1) */
852 if (!cPoints || (cPoints % 3) != 0) return FALSE;
854 dc = get_dc_ptr( hdc );
855 if(!dc) return FALSE;
858 physdev = GET_DC_PHYSDEV( dc, pPolyBezierTo );
859 ret = physdev->funcs->pPolyBezierTo( physdev, lppt, cPoints );
862 dc->CursPosX = lppt[cPoints-1].x;
863 dc->CursPosY = lppt[cPoints-1].y;
865 release_dc_ptr( dc );
869 /***********************************************************************
872 BOOL WINAPI AngleArc(HDC hdc, INT x, INT y, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle)
878 if( (signed int)dwRadius < 0 )
881 dc = get_dc_ptr( hdc );
882 if(!dc) return FALSE;
885 physdev = GET_DC_PHYSDEV( dc, pAngleArc );
886 result = physdev->funcs->pAngleArc( physdev, x, y, dwRadius, eStartAngle, eSweepAngle );
889 dc->CursPosX = GDI_ROUND( x + cos((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius );
890 dc->CursPosY = GDI_ROUND( y - sin((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius );
892 release_dc_ptr( dc );
896 /***********************************************************************
899 BOOL WINAPI PolyDraw(HDC hdc, const POINT *lppt, const BYTE *lpbTypes,
902 DC *dc = get_dc_ptr( hdc );
906 if(!dc) return FALSE;
909 physdev = GET_DC_PHYSDEV( dc, pPolyDraw );
910 result = physdev->funcs->pPolyDraw( physdev, lppt, lpbTypes, cCount );
911 release_dc_ptr( dc );
916 /**********************************************************************
919 BOOL WINAPI LineDDA(INT nXStart, INT nYStart, INT nXEnd, INT nYEnd,
920 LINEDDAPROC callback, LPARAM lParam )
922 INT xadd = 1, yadd = 1;
925 INT dx = nXEnd - nXStart;
926 INT dy = nYEnd - nYStart;
938 if (dx > dy) /* line is "more horizontal" */
940 err = 2*dy - dx; erradd = 2*dy - 2*dx;
941 for(cnt = 0;cnt < dx; cnt++)
943 callback(nXStart,nYStart,lParam);
953 else /* line is "more vertical" */
955 err = 2*dx - dy; erradd = 2*dx - 2*dy;
956 for(cnt = 0;cnt < dy; cnt++)
958 callback(nXStart,nYStart,lParam);
972 /******************************************************************
974 * *Very* simple bezier drawing code,
976 * It uses a recursive algorithm to divide the curve in a series
977 * of straight line segments. Not ideal but sufficient for me.
978 * If you are in need for something better look for some incremental
981 * 7 July 1998 Rein Klazes
985 * some macro definitions for bezier drawing
987 * to avoid truncation errors the coordinates are
988 * shifted upwards. When used in drawing they are
989 * shifted down again, including correct rounding
990 * and avoiding floating point arithmetic
991 * 4 bits should allow 27 bits coordinates which I saw
992 * somewhere in the win32 doc's
996 #define BEZIERSHIFTBITS 4
997 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
998 #define BEZIERPIXEL BEZIERSHIFTUP(1)
999 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
1000 /* maximum depth of recursion */
1001 #define BEZIERMAXDEPTH 8
1003 /* size of array to store points on */
1004 /* enough for one curve */
1005 #define BEZIER_INITBUFSIZE (150)
1007 /* calculate Bezier average, in this case the middle
1008 * correctly rounded...
1011 #define BEZIERMIDDLE(Mid, P1, P2) \
1012 (Mid).x=((P1).x+(P2).x + 1)/2;\
1013 (Mid).y=((P1).y+(P2).y + 1)/2;
1015 /**********************************************************
1016 * BezierCheck helper function to check
1017 * that recursion can be terminated
1018 * Points[0] and Points[3] are begin and endpoint
1019 * Points[1] and Points[2] are control points
1020 * level is the recursion depth
1021 * returns true if the recursion can be terminated
1023 static BOOL BezierCheck( int level, POINT *Points)
1026 dx=Points[3].x-Points[0].x;
1027 dy=Points[3].y-Points[0].y;
1028 if(abs(dy)<=abs(dx)){/* shallow line */
1029 /* check that control points are between begin and end */
1030 if(Points[1].x < Points[0].x){
1031 if(Points[1].x < Points[3].x)
1034 if(Points[1].x > Points[3].x)
1036 if(Points[2].x < Points[0].x){
1037 if(Points[2].x < Points[3].x)
1040 if(Points[2].x > Points[3].x)
1042 dx=BEZIERSHIFTDOWN(dx);
1043 if(!dx) return TRUE;
1044 if(abs(Points[1].y-Points[0].y-(dy/dx)*
1045 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
1046 abs(Points[2].y-Points[0].y-(dy/dx)*
1047 BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
1051 }else{ /* steep line */
1052 /* check that control points are between begin and end */
1053 if(Points[1].y < Points[0].y){
1054 if(Points[1].y < Points[3].y)
1057 if(Points[1].y > Points[3].y)
1059 if(Points[2].y < Points[0].y){
1060 if(Points[2].y < Points[3].y)
1063 if(Points[2].y > Points[3].y)
1065 dy=BEZIERSHIFTDOWN(dy);
1066 if(!dy) return TRUE;
1067 if(abs(Points[1].x-Points[0].x-(dx/dy)*
1068 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
1069 abs(Points[2].x-Points[0].x-(dx/dy)*
1070 BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
1077 /* Helper for GDI_Bezier.
1078 * Just handles one Bezier, so Points should point to four POINTs
1080 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
1081 INT *nPtsOut, INT level )
1083 if(*nPtsOut == *dwOut) {
1085 *PtsOut = HeapReAlloc( GetProcessHeap(), 0, *PtsOut,
1086 *dwOut * sizeof(POINT) );
1089 if(!level || BezierCheck(level, Points)) {
1091 (*PtsOut)[0].x = BEZIERSHIFTDOWN(Points[0].x);
1092 (*PtsOut)[0].y = BEZIERSHIFTDOWN(Points[0].y);
1095 (*PtsOut)[*nPtsOut].x = BEZIERSHIFTDOWN(Points[3].x);
1096 (*PtsOut)[*nPtsOut].y = BEZIERSHIFTDOWN(Points[3].y);
1099 POINT Points2[4]; /* for the second recursive call */
1100 Points2[3]=Points[3];
1101 BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
1102 BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
1103 BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
1105 BEZIERMIDDLE(Points[1], Points[0], Points[1]);
1106 BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1107 BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1109 Points2[0]=Points[3];
1111 /* do the two halves */
1112 GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
1113 GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
1119 /***********************************************************************
1120 * GDI_Bezier [INTERNAL]
1121 * Calculate line segments that approximate -what microsoft calls- a bezier
1123 * The routine recursively divides the curve in two parts until a straight
1128 * Points [I] Ptr to count POINTs which are the end and control points
1129 * of the set of Bezier curves to flatten.
1130 * count [I] Number of Points. Must be 3n+1.
1131 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
1136 * Ptr to an array of POINTs that contain the lines that approximate the
1137 * Beziers. The array is allocated on the process heap and it is the caller's
1138 * responsibility to HeapFree it. [this is not a particularly nice interface
1139 * but since we can't know in advance how many points we will generate, the
1140 * alternative would be to call the function twice, once to determine the size
1141 * and a second time to do the work - I decided this was too much of a pain].
1143 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
1146 INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
1148 if (count == 1 || (count - 1) % 3 != 0) {
1149 ERR("Invalid no. of points %d\n", count);
1153 out = HeapAlloc( GetProcessHeap(), 0, dwOut * sizeof(POINT));
1154 for(Bezier = 0; Bezier < (count-1)/3; Bezier++) {
1156 memcpy(ptBuf, Points + Bezier * 3, sizeof(POINT) * 4);
1157 for(i = 0; i < 4; i++) {
1158 ptBuf[i].x = BEZIERSHIFTUP(ptBuf[i].x);
1159 ptBuf[i].y = BEZIERSHIFTUP(ptBuf[i].y);
1161 GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
1163 TRACE("Produced %d points\n", *nPtsOut);
1167 /******************************************************************************
1168 * GdiGradientFill (GDI32.@)
1170 BOOL WINAPI GdiGradientFill( HDC hdc, TRIVERTEX *vert_array, ULONG nvert,
1171 void *grad_array, ULONG ngrad, ULONG mode )
1178 TRACE("%p vert_array:%p nvert:%d grad_array:%p ngrad:%d\n", hdc, vert_array, nvert, grad_array, ngrad);
1180 if (!vert_array || !nvert || !grad_array || !ngrad || mode > GRADIENT_FILL_TRIANGLE)
1182 SetLastError( ERROR_INVALID_PARAMETER );
1185 for (i = 0; i < ngrad * (mode == GRADIENT_FILL_TRIANGLE ? 3 : 2); i++)
1186 if (((ULONG *)grad_array)[i] >= nvert) return FALSE;
1188 if (!(dc = get_dc_ptr( hdc )))
1190 SetLastError( ERROR_INVALID_PARAMETER );
1194 physdev = GET_DC_PHYSDEV( dc, pGradientFill );
1195 ret = physdev->funcs->pGradientFill( physdev, vert_array, nvert, grad_array, ngrad, mode );
1196 release_dc_ptr( dc );
1200 /******************************************************************************
1201 * GdiDrawStream (GDI32.@)
1204 BOOL WINAPI GdiDrawStream( HDC hdc, ULONG in, void * pvin )
1206 FIXME("stub: %p, %d, %p\n", hdc, in, pvin);