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 /***********************************************************************
43 BOOL WINAPI LineTo( HDC hdc, INT x, INT y )
45 DC * dc = DC_GetDCUpdate( hdc );
50 if(PATH_IsPathOpen(dc->path))
51 ret = PATH_LineTo(dc, x, y);
53 ret = dc->funcs->pLineTo && dc->funcs->pLineTo(dc->physDev,x,y);
58 GDI_ReleaseObj( hdc );
63 /***********************************************************************
66 BOOL WINAPI MoveToEx( HDC hdc, INT x, INT y, LPPOINT pt )
69 DC * dc = DC_GetDCPtr( hdc );
80 if(PATH_IsPathOpen(dc->path)) ret = PATH_MoveTo(dc);
81 else if (dc->funcs->pMoveTo) ret = dc->funcs->pMoveTo(dc->physDev,x,y);
82 GDI_ReleaseObj( hdc );
87 /***********************************************************************
90 BOOL WINAPI Arc( HDC hdc, INT left, INT top, INT right,
91 INT bottom, INT xstart, INT ystart,
95 DC * dc = DC_GetDCUpdate( hdc );
98 if(PATH_IsPathOpen(dc->path))
99 ret = PATH_Arc(dc, left, top, right, bottom, xstart, ystart, xend, yend,0);
100 else if (dc->funcs->pArc)
101 ret = dc->funcs->pArc(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
102 GDI_ReleaseObj( hdc );
107 /***********************************************************************
110 BOOL WINAPI ArcTo( HDC hdc,
112 INT right, INT bottom,
113 INT xstart, INT ystart,
117 DC * dc = DC_GetDCUpdate( hdc );
118 if(!dc) return FALSE;
120 if(dc->funcs->pArcTo)
122 result = dc->funcs->pArcTo( dc->physDev, left, top, right, bottom,
123 xstart, ystart, xend, yend );
124 GDI_ReleaseObj( hdc );
127 GDI_ReleaseObj( hdc );
130 * According to the documentation, a line is drawn from the current
131 * position to the starting point of the arc.
133 LineTo(hdc, xstart, ystart);
135 * Then the arc is drawn.
137 result = Arc(hdc, left, top, right, bottom, xstart, ystart, xend, yend);
139 * If no error occurred, the current position is moved to the ending
142 if (result) MoveToEx(hdc, xend, yend, NULL);
147 /***********************************************************************
150 BOOL WINAPI Pie( HDC hdc, INT left, INT top,
151 INT right, INT bottom, INT xstart, INT ystart,
155 DC * dc = DC_GetDCUpdate( hdc );
156 if (!dc) return FALSE;
158 if(PATH_IsPathOpen(dc->path))
159 ret = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,2);
160 else if(dc->funcs->pPie)
161 ret = dc->funcs->pPie(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
163 GDI_ReleaseObj( hdc );
168 /***********************************************************************
171 BOOL WINAPI Chord( HDC hdc, INT left, INT top,
172 INT right, INT bottom, INT xstart, INT ystart,
176 DC * dc = DC_GetDCUpdate( hdc );
177 if (!dc) return FALSE;
179 if(PATH_IsPathOpen(dc->path))
180 ret = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,1);
181 else if(dc->funcs->pChord)
182 ret = dc->funcs->pChord(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
184 GDI_ReleaseObj( hdc );
189 /***********************************************************************
192 BOOL WINAPI Ellipse( HDC hdc, INT left, INT top,
193 INT right, INT bottom )
196 DC * dc = DC_GetDCUpdate( hdc );
197 if (!dc) return FALSE;
199 if(PATH_IsPathOpen(dc->path))
200 ret = PATH_Ellipse(dc,left,top,right,bottom);
201 else if (dc->funcs->pEllipse)
202 ret = dc->funcs->pEllipse(dc->physDev,left,top,right,bottom);
204 GDI_ReleaseObj( hdc );
209 /***********************************************************************
210 * Rectangle (GDI32.@)
212 BOOL WINAPI Rectangle( HDC hdc, INT left, INT top,
213 INT right, INT bottom )
216 DC * dc = DC_GetDCUpdate( hdc );
219 if(PATH_IsPathOpen(dc->path))
220 ret = PATH_Rectangle(dc, left, top, right, bottom);
221 else if (dc->funcs->pRectangle)
222 ret = dc->funcs->pRectangle(dc->physDev,left,top,right,bottom);
223 GDI_ReleaseObj( hdc );
229 /***********************************************************************
230 * RoundRect (GDI32.@)
232 BOOL WINAPI RoundRect( HDC hdc, INT left, INT top, INT right,
233 INT bottom, INT ell_width, INT ell_height )
236 DC *dc = DC_GetDCUpdate( hdc );
240 if(PATH_IsPathOpen(dc->path))
241 ret = PATH_RoundRect(dc,left,top,right,bottom,ell_width,ell_height);
242 else if (dc->funcs->pRoundRect)
243 ret = dc->funcs->pRoundRect(dc->physDev,left,top,right,bottom,ell_width,ell_height);
244 GDI_ReleaseObj( hdc );
249 /***********************************************************************
252 COLORREF WINAPI SetPixel( HDC hdc, INT x, INT y, COLORREF color )
255 DC * dc = DC_GetDCUpdate( hdc );
258 if (dc->funcs->pSetPixel) ret = dc->funcs->pSetPixel(dc->physDev,x,y,color);
259 GDI_ReleaseObj( hdc );
264 /***********************************************************************
265 * SetPixelV (GDI32.@)
267 BOOL WINAPI SetPixelV( HDC hdc, INT x, INT y, COLORREF color )
270 DC * dc = DC_GetDCUpdate( hdc );
273 if (dc->funcs->pSetPixel)
275 dc->funcs->pSetPixel(dc->physDev,x,y,color);
278 GDI_ReleaseObj( hdc );
283 /***********************************************************************
286 COLORREF WINAPI GetPixel( HDC hdc, INT x, INT y )
288 COLORREF ret = CLR_INVALID;
289 DC * dc = DC_GetDCUpdate( hdc );
293 /* FIXME: should this be in the graphics driver? */
294 if (PtVisible( hdc, x, y ))
296 if (dc->funcs->pGetPixel) ret = dc->funcs->pGetPixel(dc->physDev,x,y);
298 GDI_ReleaseObj( hdc );
304 /******************************************************************************
305 * ChoosePixelFormat [GDI32.@]
306 * Matches a pixel format to given format
309 * hdc [I] Device context to search for best pixel match
310 * ppfd [I] Pixel format for which a match is sought
313 * Success: Pixel format index closest to given format
316 INT WINAPI ChoosePixelFormat( HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd )
319 DC * dc = DC_GetDCPtr( hdc );
321 TRACE("(%p,%p)\n",hdc,ppfd);
325 if (!dc->funcs->pChoosePixelFormat) FIXME(" :stub\n");
326 else ret = dc->funcs->pChoosePixelFormat(dc->physDev,ppfd);
328 GDI_ReleaseObj( hdc );
333 /******************************************************************************
334 * SetPixelFormat [GDI32.@]
335 * Sets pixel format of device context
338 * hdc [I] Device context to search for best pixel match
339 * iPixelFormat [I] Pixel format index
340 * ppfd [I] Pixel format for which a match is sought
346 BOOL WINAPI SetPixelFormat( HDC hdc, INT iPixelFormat,
347 const PIXELFORMATDESCRIPTOR *ppfd)
350 DC * dc = DC_GetDCPtr( hdc );
352 TRACE("(%p,%d,%p)\n",hdc,iPixelFormat,ppfd);
356 if (!dc->funcs->pSetPixelFormat) FIXME(" :stub\n");
357 else bRet = dc->funcs->pSetPixelFormat(dc->physDev,iPixelFormat,ppfd);
359 GDI_ReleaseObj( hdc );
364 /******************************************************************************
365 * GetPixelFormat [GDI32.@]
366 * Gets index of pixel format of DC
369 * hdc [I] Device context whose pixel format index is sought
372 * Success: Currently selected pixel format
375 INT WINAPI GetPixelFormat( HDC hdc )
378 DC * dc = DC_GetDCPtr( hdc );
384 if (!dc->funcs->pGetPixelFormat) FIXME(" :stub\n");
385 else ret = dc->funcs->pGetPixelFormat(dc->physDev);
387 GDI_ReleaseObj( hdc );
392 /******************************************************************************
393 * DescribePixelFormat [GDI32.@]
394 * Gets info about pixel format from DC
397 * hdc [I] Device context
398 * iPixelFormat [I] Pixel format selector
399 * nBytes [I] Size of buffer
400 * ppfd [O] Pointer to structure to receive pixel format data
403 * Success: Maximum pixel format index of the device context
406 INT WINAPI DescribePixelFormat( HDC hdc, INT iPixelFormat, UINT nBytes,
407 LPPIXELFORMATDESCRIPTOR ppfd )
410 DC * dc = DC_GetDCPtr( hdc );
412 TRACE("(%p,%d,%d,%p): stub\n",hdc,iPixelFormat,nBytes,ppfd);
416 if (!dc->funcs->pDescribePixelFormat)
419 ppfd->nSize = nBytes;
423 else ret = dc->funcs->pDescribePixelFormat(dc->physDev,iPixelFormat,nBytes,ppfd);
425 GDI_ReleaseObj( hdc );
430 /******************************************************************************
431 * SwapBuffers [GDI32.@]
432 * Exchanges front and back buffers of window
435 * hdc [I] Device context whose buffers get swapped
441 BOOL WINAPI SwapBuffers( HDC hdc )
444 DC * dc = DC_GetDCPtr( hdc );
448 if (!dc) return TRUE;
450 if (!dc->funcs->pSwapBuffers)
455 else bRet = dc->funcs->pSwapBuffers(dc->physDev);
457 GDI_ReleaseObj( hdc );
462 /***********************************************************************
465 BOOL WINAPI PaintRgn( HDC hdc, HRGN hrgn )
468 DC * dc = DC_GetDCUpdate( hdc );
471 if (dc->funcs->pPaintRgn) ret = dc->funcs->pPaintRgn(dc->physDev,hrgn);
472 GDI_ReleaseObj( hdc );
478 /***********************************************************************
481 BOOL WINAPI FillRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush )
485 DC * dc = DC_GetDCUpdate( hdc );
487 if (!dc) return FALSE;
488 if(dc->funcs->pFillRgn)
489 retval = dc->funcs->pFillRgn(dc->physDev, hrgn, hbrush);
490 else if ((prevBrush = SelectObject( hdc, hbrush )))
492 retval = PaintRgn( hdc, hrgn );
493 SelectObject( hdc, prevBrush );
495 GDI_ReleaseObj( hdc );
500 /***********************************************************************
503 BOOL WINAPI FrameRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush,
504 INT nWidth, INT nHeight )
507 DC *dc = DC_GetDCUpdate( hdc );
509 if (!dc) return FALSE;
510 if(dc->funcs->pFrameRgn)
511 ret = dc->funcs->pFrameRgn( dc->physDev, hrgn, hbrush, nWidth, nHeight );
514 HRGN tmp = CreateRectRgn( 0, 0, 0, 0 );
517 if (REGION_FrameRgn( tmp, hrgn, nWidth, nHeight ))
519 FillRgn( hdc, tmp, hbrush );
525 GDI_ReleaseObj( hdc );
530 /***********************************************************************
531 * InvertRgn (GDI32.@)
533 BOOL WINAPI InvertRgn( HDC hdc, HRGN hrgn )
538 DC *dc = DC_GetDCUpdate( hdc );
539 if (!dc) return FALSE;
541 if(dc->funcs->pInvertRgn)
542 retval = dc->funcs->pInvertRgn( dc->physDev, hrgn );
545 prevBrush = SelectObject( hdc, GetStockObject(BLACK_BRUSH) );
546 prevROP = SetROP2( hdc, R2_NOT );
547 retval = PaintRgn( hdc, hrgn );
548 SelectObject( hdc, prevBrush );
549 SetROP2( hdc, prevROP );
551 GDI_ReleaseObj( hdc );
556 /**********************************************************************
559 BOOL WINAPI Polyline( HDC hdc, const POINT* pt, INT count )
562 DC * dc = DC_GetDCUpdate( hdc );
565 if (PATH_IsPathOpen(dc->path)) ret = PATH_Polyline(dc, pt, count);
566 else if (dc->funcs->pPolyline) ret = dc->funcs->pPolyline(dc->physDev,pt,count);
567 GDI_ReleaseObj( hdc );
572 /**********************************************************************
573 * PolylineTo (GDI32.@)
575 BOOL WINAPI PolylineTo( HDC hdc, const POINT* pt, DWORD cCount )
577 DC * dc = DC_GetDCUpdate( hdc );
580 if(!dc) return FALSE;
582 if(PATH_IsPathOpen(dc->path))
583 ret = PATH_PolylineTo(dc, pt, cCount);
585 else if(dc->funcs->pPolylineTo)
586 ret = dc->funcs->pPolylineTo(dc->physDev, pt, cCount);
588 else { /* do it using Polyline */
589 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
590 sizeof(POINT) * (cCount + 1) );
593 pts[0].x = dc->CursPosX;
594 pts[0].y = dc->CursPosY;
595 memcpy( pts + 1, pt, sizeof(POINT) * cCount );
596 ret = Polyline( hdc, pts, cCount + 1 );
597 HeapFree( GetProcessHeap(), 0, pts );
601 dc->CursPosX = pt[cCount-1].x;
602 dc->CursPosY = pt[cCount-1].y;
604 GDI_ReleaseObj( hdc );
609 /**********************************************************************
612 BOOL WINAPI Polygon( HDC hdc, const POINT* pt, INT count )
615 DC * dc = DC_GetDCUpdate( hdc );
618 if (PATH_IsPathOpen(dc->path)) ret = PATH_Polygon(dc, pt, count);
619 else if (dc->funcs->pPolygon) ret = dc->funcs->pPolygon(dc->physDev,pt,count);
620 GDI_ReleaseObj( hdc );
626 /**********************************************************************
627 * PolyPolygon (GDI32.@)
629 BOOL WINAPI PolyPolygon( HDC hdc, const POINT* pt, const INT* counts,
633 DC * dc = DC_GetDCUpdate( hdc );
636 if (PATH_IsPathOpen(dc->path)) ret = PATH_PolyPolygon(dc, pt, counts, polygons);
637 else if (dc->funcs->pPolyPolygon) ret = dc->funcs->pPolyPolygon(dc->physDev,pt,counts,polygons);
638 GDI_ReleaseObj( hdc );
643 /**********************************************************************
644 * PolyPolyline (GDI32.@)
646 BOOL WINAPI PolyPolyline( HDC hdc, const POINT* pt, const DWORD* counts,
650 DC * dc = DC_GetDCUpdate( hdc );
653 if (PATH_IsPathOpen(dc->path)) ret = PATH_PolyPolyline(dc, pt, counts, polylines);
654 else if (dc->funcs->pPolyPolyline) ret = dc->funcs->pPolyPolyline(dc->physDev,pt,counts,polylines);
655 GDI_ReleaseObj( hdc );
660 /**********************************************************************
661 * ExtFloodFill (GDI32.@)
663 BOOL WINAPI ExtFloodFill( HDC hdc, INT x, INT y, COLORREF color,
667 DC * dc = DC_GetDCUpdate( hdc );
670 if (dc->funcs->pExtFloodFill) ret = dc->funcs->pExtFloodFill(dc->physDev,x,y,color,fillType);
671 GDI_ReleaseObj( hdc );
677 /**********************************************************************
678 * FloodFill (GDI32.@)
680 BOOL WINAPI FloodFill( HDC hdc, INT x, INT y, COLORREF color )
682 return ExtFloodFill( hdc, x, y, color, FLOODFILLBORDER );
686 /******************************************************************************
687 * PolyBezier [GDI32.@]
688 * Draws one or more Bezier curves
691 * hDc [I] Handle to device context
692 * lppt [I] Pointer to endpoints and control points
693 * cPoints [I] Count of endpoints and control points
699 BOOL WINAPI PolyBezier( HDC hdc, const POINT* lppt, DWORD cPoints )
704 /* cPoints must be 3 * n + 1 (where n>=1) */
705 if (cPoints == 1 || (cPoints % 3) != 1) return FALSE;
707 dc = DC_GetDCUpdate( hdc );
708 if(!dc) return FALSE;
710 if(PATH_IsPathOpen(dc->path))
711 ret = PATH_PolyBezier(dc, lppt, cPoints);
712 else if (dc->funcs->pPolyBezier)
713 ret = dc->funcs->pPolyBezier(dc->physDev, lppt, cPoints);
714 else /* We'll convert it into line segments and draw them using Polyline */
719 if ((Pts = GDI_Bezier( lppt, cPoints, &nOut )))
721 TRACE("Pts = %p, no = %d\n", Pts, nOut);
722 ret = Polyline( dc->hSelf, Pts, nOut );
723 HeapFree( GetProcessHeap(), 0, Pts );
727 GDI_ReleaseObj( hdc );
731 /******************************************************************************
732 * PolyBezierTo [GDI32.@]
733 * Draws one or more Bezier curves
736 * hDc [I] Handle to device context
737 * lppt [I] Pointer to endpoints and control points
738 * cPoints [I] Count of endpoints and control points
744 BOOL WINAPI PolyBezierTo( HDC hdc, const POINT* lppt, DWORD cPoints )
749 /* cbPoints must be 3 * n (where n>=1) */
750 if (!cPoints || (cPoints % 3) != 0) return FALSE;
752 dc = DC_GetDCUpdate( hdc );
753 if(!dc) return FALSE;
755 if(PATH_IsPathOpen(dc->path))
756 ret = PATH_PolyBezierTo(dc, lppt, cPoints);
757 else if(dc->funcs->pPolyBezierTo)
758 ret = dc->funcs->pPolyBezierTo(dc->physDev, lppt, cPoints);
759 else { /* We'll do it using PolyBezier */
761 pt = HeapAlloc( GetProcessHeap(), 0, sizeof(POINT) * (cPoints + 1) );
762 if(!pt) return FALSE;
763 pt[0].x = dc->CursPosX;
764 pt[0].y = dc->CursPosY;
765 memcpy(pt + 1, lppt, sizeof(POINT) * cPoints);
766 ret = PolyBezier(dc->hSelf, pt, cPoints+1);
767 HeapFree( GetProcessHeap(), 0, pt );
770 dc->CursPosX = lppt[cPoints-1].x;
771 dc->CursPosY = lppt[cPoints-1].y;
773 GDI_ReleaseObj( hdc );
777 /***********************************************************************
780 BOOL WINAPI AngleArc(HDC hdc, INT x, INT y, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle)
782 INT x1,y1,x2,y2, arcdir;
786 if( (signed int)dwRadius < 0 )
789 dc = DC_GetDCUpdate( hdc );
790 if(!dc) return FALSE;
792 if(dc->funcs->pAngleArc)
794 result = dc->funcs->pAngleArc( dc->physDev, x, y, dwRadius, eStartAngle, eSweepAngle );
796 GDI_ReleaseObj( hdc );
799 GDI_ReleaseObj( hdc );
801 /* AngleArc always works counterclockwise */
802 arcdir = GetArcDirection( hdc );
803 SetArcDirection( hdc, AD_COUNTERCLOCKWISE );
805 x1 = x + cos(eStartAngle*M_PI/180) * dwRadius;
806 y1 = y - sin(eStartAngle*M_PI/180) * dwRadius;
807 x2 = x + cos((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius;
808 y2 = x - sin((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius;
810 LineTo( hdc, x1, y1 );
811 if( eSweepAngle >= 0 )
812 result = Arc( hdc, x-dwRadius, y-dwRadius, x+dwRadius, y+dwRadius,
815 result = Arc( hdc, x-dwRadius, y-dwRadius, x+dwRadius, y+dwRadius,
818 if( result ) MoveToEx( hdc, x2, y2, NULL );
819 SetArcDirection( hdc, arcdir );
823 /***********************************************************************
826 BOOL WINAPI PolyDraw(HDC hdc, const POINT *lppt, const BYTE *lpbTypes,
834 dc = DC_GetDCUpdate( hdc );
835 if(!dc) return FALSE;
837 if(dc->funcs->pPolyDraw)
839 result = dc->funcs->pPolyDraw( dc->physDev, lppt, lpbTypes, cCount );
840 GDI_ReleaseObj( hdc );
843 GDI_ReleaseObj( hdc );
845 /* check for each bezierto if there are two more points */
846 for( i = 0; i < cCount; i++ )
847 if( lpbTypes[i] != PT_MOVETO &&
848 lpbTypes[i] & PT_BEZIERTO )
856 /* if no moveto occurs, we will close the figure here */
857 lastmove.x = dc->CursPosX;
858 lastmove.y = dc->CursPosY;
861 for( i = 0; i < cCount; i++ )
863 if( lpbTypes[i] == PT_MOVETO )
865 MoveToEx( hdc, lppt[i].x, lppt[i].y, NULL );
866 lastmove.x = dc->CursPosX;
867 lastmove.y = dc->CursPosY;
869 else if( lpbTypes[i] & PT_LINETO )
870 LineTo( hdc, lppt[i].x, lppt[i].y );
871 else if( lpbTypes[i] & PT_BEZIERTO )
873 PolyBezierTo( hdc, &lppt[i], 3 );
879 if( lpbTypes[i] & PT_CLOSEFIGURE )
881 if( PATH_IsPathOpen( dc->path ) )
884 LineTo( hdc, lastmove.x, lastmove.y );
892 /**********************************************************************
895 BOOL WINAPI LineDDA(INT nXStart, INT nYStart, INT nXEnd, INT nYEnd,
896 LINEDDAPROC callback, LPARAM lParam )
898 INT xadd = 1, yadd = 1;
901 INT dx = nXEnd - nXStart;
902 INT dy = nYEnd - nYStart;
914 if (dx > dy) /* line is "more horizontal" */
916 err = 2*dy - dx; erradd = 2*dy - 2*dx;
917 for(cnt = 0;cnt <= dx; cnt++)
919 callback(nXStart,nYStart,lParam);
929 else /* line is "more vertical" */
931 err = 2*dx - dy; erradd = 2*dx - 2*dy;
932 for(cnt = 0;cnt <= dy; cnt++)
934 callback(nXStart,nYStart,lParam);
948 /******************************************************************
950 * *Very* simple bezier drawing code,
952 * It uses a recursive algorithm to divide the curve in a series
953 * of straight line segements. Not ideal but for me sufficient.
954 * If you are in need for something better look for some incremental
957 * 7 July 1998 Rein Klazes
961 * some macro definitions for bezier drawing
963 * to avoid truncation errors the coordinates are
964 * shifted upwards. When used in drawing they are
965 * shifted down again, including correct rounding
966 * and avoiding floating point arithmetic
967 * 4 bits should allow 27 bits coordinates which I saw
968 * somewhere in the win32 doc's
972 #define BEZIERSHIFTBITS 4
973 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
974 #define BEZIERPIXEL BEZIERSHIFTUP(1)
975 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
976 /* maximum depth of recursion */
977 #define BEZIERMAXDEPTH 8
979 /* size of array to store points on */
980 /* enough for one curve */
981 #define BEZIER_INITBUFSIZE (150)
983 /* calculate Bezier average, in this case the middle
984 * correctly rounded...
987 #define BEZIERMIDDLE(Mid, P1, P2) \
988 (Mid).x=((P1).x+(P2).x + 1)/2;\
989 (Mid).y=((P1).y+(P2).y + 1)/2;
991 /**********************************************************
992 * BezierCheck helper function to check
993 * that recursion can be terminated
994 * Points[0] and Points[3] are begin and endpoint
995 * Points[1] and Points[2] are control points
996 * level is the recursion depth
997 * returns true if the recusion can be terminated
999 static BOOL BezierCheck( int level, POINT *Points)
1002 dx=Points[3].x-Points[0].x;
1003 dy=Points[3].y-Points[0].y;
1004 if(abs(dy)<=abs(dx)){/* shallow line */
1005 /* check that control points are between begin and end */
1006 if(Points[1].x < Points[0].x){
1007 if(Points[1].x < Points[3].x)
1010 if(Points[1].x > Points[3].x)
1012 if(Points[2].x < Points[0].x){
1013 if(Points[2].x < Points[3].x)
1016 if(Points[2].x > Points[3].x)
1018 dx=BEZIERSHIFTDOWN(dx);
1019 if(!dx) return TRUE;
1020 if(abs(Points[1].y-Points[0].y-(dy/dx)*
1021 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
1022 abs(Points[2].y-Points[0].y-(dy/dx)*
1023 BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
1027 }else{ /* steep line */
1028 /* check that control points are between begin and end */
1029 if(Points[1].y < Points[0].y){
1030 if(Points[1].y < Points[3].y)
1033 if(Points[1].y > Points[3].y)
1035 if(Points[2].y < Points[0].y){
1036 if(Points[2].y < Points[3].y)
1039 if(Points[2].y > Points[3].y)
1041 dy=BEZIERSHIFTDOWN(dy);
1042 if(!dy) return TRUE;
1043 if(abs(Points[1].x-Points[0].x-(dx/dy)*
1044 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
1045 abs(Points[2].x-Points[0].x-(dx/dy)*
1046 BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
1053 /* Helper for GDI_Bezier.
1054 * Just handles one Bezier, so Points should point to four POINTs
1056 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
1057 INT *nPtsOut, INT level )
1059 if(*nPtsOut == *dwOut) {
1061 *PtsOut = HeapReAlloc( GetProcessHeap(), 0, *PtsOut,
1062 *dwOut * sizeof(POINT) );
1065 if(!level || BezierCheck(level, Points)) {
1067 (*PtsOut)[0].x = BEZIERSHIFTDOWN(Points[0].x);
1068 (*PtsOut)[0].y = BEZIERSHIFTDOWN(Points[0].y);
1071 (*PtsOut)[*nPtsOut].x = BEZIERSHIFTDOWN(Points[3].x);
1072 (*PtsOut)[*nPtsOut].y = BEZIERSHIFTDOWN(Points[3].y);
1075 POINT Points2[4]; /* for the second recursive call */
1076 Points2[3]=Points[3];
1077 BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
1078 BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
1079 BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
1081 BEZIERMIDDLE(Points[1], Points[0], Points[1]);
1082 BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1083 BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1085 Points2[0]=Points[3];
1087 /* do the two halves */
1088 GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
1089 GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
1095 /***********************************************************************
1096 * GDI_Bezier [INTERNAL]
1097 * Calculate line segments that approximate -what microsoft calls- a bezier
1099 * The routine recursively divides the curve in two parts until a straight
1104 * Points [I] Ptr to count POINTs which are the end and control points
1105 * of the set of Bezier curves to flatten.
1106 * count [I] Number of Points. Must be 3n+1.
1107 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
1112 * Ptr to an array of POINTs that contain the lines that approximinate the
1113 * Beziers. The array is allocated on the process heap and it is the caller's
1114 * responsibility to HeapFree it. [this is not a particularly nice interface
1115 * but since we can't know in advance how many points will generate, the
1116 * alternative would be to call the function twice, once to determine the size
1117 * and a second time to do the work - I decided this was too much of a pain].
1119 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
1122 INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
1124 if (count == 1 || (count - 1) % 3 != 0) {
1125 ERR("Invalid no. of points %d\n", count);
1129 out = HeapAlloc( GetProcessHeap(), 0, dwOut * sizeof(POINT));
1130 for(Bezier = 0; Bezier < (count-1)/3; Bezier++) {
1132 memcpy(ptBuf, Points + Bezier * 3, sizeof(POINT) * 4);
1133 for(i = 0; i < 4; i++) {
1134 ptBuf[i].x = BEZIERSHIFTUP(ptBuf[i].x);
1135 ptBuf[i].y = BEZIERSHIFTUP(ptBuf[i].y);
1137 GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
1139 TRACE("Produced %d points\n", *nPtsOut);
1143 /******************************************************************************
1144 * GdiGradientFill (GDI32.@)
1146 * FIXME: we don't support the Alpha channel properly
1148 BOOL WINAPI GdiGradientFill( HDC hdc, TRIVERTEX *vert_array, ULONG nvert,
1149 void * grad_array, ULONG ngrad, ULONG mode )
1153 TRACE("vert_array:0x%08lx nvert:%d grad_array:0x%08lx ngrad:%d\n",
1154 (long)vert_array, nvert, (long)grad_array, ngrad);
1158 case GRADIENT_FILL_RECT_H:
1159 for(i = 0; i < ngrad; i++)
1161 GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1162 TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1163 TRIVERTEX *v2 = vert_array + rect->LowerRight;
1164 int y1 = v1->y < v2->y ? v1->y : v2->y;
1165 int y2 = v2->y > v1->y ? v2->y : v1->y;
1174 for (x = 0; x < dx; x++)
1179 hPen = CreatePen( PS_SOLID, 1, RGB(
1180 (v1->Red * (dx - x) + v2->Red * x) / dx >> 8,
1181 (v1->Green * (dx - x) + v2->Green * x) / dx >> 8,
1182 (v1->Blue * (dx - x) + v2->Blue * x) / dx >> 8));
1183 hOldPen = SelectObject( hdc, hPen );
1184 pts[0].x = v1->x + x;
1186 pts[1].x = v1->x + x;
1188 Polyline( hdc, &pts[0], 2 );
1189 DeleteObject( SelectObject(hdc, hOldPen ) );
1193 case GRADIENT_FILL_RECT_V:
1194 for(i = 0; i < ngrad; i++)
1196 GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1197 TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1198 TRIVERTEX *v2 = vert_array + rect->LowerRight;
1199 int x1 = v1->x < v2->x ? v1->x : v2->x;
1200 int x2 = v2->x > v1->x ? v2->x : v1->x;
1209 for (y = 0; y < dy; y++)
1214 hPen = CreatePen( PS_SOLID, 1, RGB(
1215 (v1->Red * (dy - y) + v2->Red * y) / dy >> 8,
1216 (v1->Green * (dy - y) + v2->Green * y) / dy >> 8,
1217 (v1->Blue * (dy - y) + v2->Blue * y) / dy >> 8));
1218 hOldPen = SelectObject( hdc, hPen );
1220 pts[0].y = v1->y + y;
1222 pts[1].y = v1->y + y;
1223 Polyline( hdc, &pts[0], 2 );
1224 DeleteObject( SelectObject(hdc, hOldPen ) );
1228 case GRADIENT_FILL_TRIANGLE:
1229 for (i = 0; i < ngrad; i++)
1231 GRADIENT_TRIANGLE *tri = ((GRADIENT_TRIANGLE *)grad_array) + i;
1232 TRIVERTEX *v1 = vert_array + tri->Vertex1;
1233 TRIVERTEX *v2 = vert_array + tri->Vertex2;
1234 TRIVERTEX *v3 = vert_array + tri->Vertex3;
1238 { TRIVERTEX *t = v1; v1 = v2; v2 = t; }
1241 TRIVERTEX *t = v2; v2 = v3; v3 = t;
1243 { t = v1; v1 = v2; v2 = t; }
1245 /* v1->y <= v2->y <= v3->y */
1248 for (y = 0; y < dy; y++)
1250 /* v1->y <= y < v3->y */
1251 TRIVERTEX *v = y < (v2->y - v1->y) ? v1 : v3;
1252 /* (v->y <= y < v2->y) || (v2->y <= y < v->y) */
1253 int dy2 = v2->y - v->y;
1254 int y2 = y + v1->y - v->y;
1256 int x1 = (v3->x * y + v1->x * (dy - y )) / dy;
1257 int x2 = (v2->x * y2 + v-> x * (dy2 - y2)) / dy2;
1258 int r1 = (v3->Red * y + v1->Red * (dy - y )) / dy;
1259 int r2 = (v2->Red * y2 + v-> Red * (dy2 - y2)) / dy2;
1260 int g1 = (v3->Green * y + v1->Green * (dy - y )) / dy;
1261 int g2 = (v2->Green * y2 + v-> Green * (dy2 - y2)) / dy2;
1262 int b1 = (v3->Blue * y + v1->Blue * (dy - y )) / dy;
1263 int b2 = (v2->Blue * y2 + v-> Blue * (dy2 - y2)) / dy2;
1269 for (x = 0; x < dx; x++)
1270 SetPixel (hdc, x + x1, y + v1->y, RGB(
1271 (r1 * (dx - x) + r2 * x) / dx >> 8,
1272 (g1 * (dx - x) + g2 * x) / dx >> 8,
1273 (b1 * (dx - x) + b2 * x) / dx >> 8));
1278 for (x = 0; x < dx; x++)
1279 SetPixel (hdc, x + x2, y + v1->y, RGB(
1280 (r2 * (dx - x) + r1 * x) / dx >> 8,
1281 (g2 * (dx - x) + g1 * x) / dx >> 8,
1282 (b2 * (dx - x) + b1 * x) / dx >> 8));