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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
35 #include "gdi_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
41 /***********************************************************************
44 BOOL WINAPI LineTo( HDC hdc, INT x, INT y )
46 DC * dc = DC_GetDCUpdate( hdc );
51 if(PATH_IsPathOpen(dc->path))
52 ret = PATH_LineTo(dc, x, y);
54 ret = dc->funcs->pLineTo && dc->funcs->pLineTo(dc->physDev,x,y);
59 GDI_ReleaseObj( hdc );
64 /***********************************************************************
67 BOOL WINAPI MoveToEx( HDC hdc, INT x, INT y, LPPOINT pt )
70 DC * dc = DC_GetDCPtr( hdc );
81 if(PATH_IsPathOpen(dc->path)) ret = PATH_MoveTo(dc);
82 else if (dc->funcs->pMoveTo) ret = dc->funcs->pMoveTo(dc->physDev,x,y);
83 GDI_ReleaseObj( hdc );
88 /***********************************************************************
91 BOOL WINAPI Arc( HDC hdc, INT left, INT top, INT right,
92 INT bottom, INT xstart, INT ystart,
96 DC * dc = DC_GetDCUpdate( hdc );
99 if(PATH_IsPathOpen(dc->path))
100 ret = PATH_Arc(dc, left, top, right, bottom, xstart, ystart, xend, yend,0);
101 else if (dc->funcs->pArc)
102 ret = dc->funcs->pArc(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
103 GDI_ReleaseObj( hdc );
108 /***********************************************************************
111 BOOL WINAPI ArcTo( HDC hdc,
113 INT right, INT bottom,
114 INT xstart, INT ystart,
118 DC * dc = DC_GetDCUpdate( hdc );
119 if(!dc) return FALSE;
121 if(dc->funcs->pArcTo)
123 result = dc->funcs->pArcTo( dc->physDev, left, top, right, bottom,
124 xstart, ystart, xend, yend );
125 GDI_ReleaseObj( hdc );
128 GDI_ReleaseObj( hdc );
131 * According to the documentation, a line is drawn from the current
132 * position to the starting point of the arc.
134 LineTo(hdc, xstart, ystart);
136 * Then the arc is drawn.
138 result = Arc(hdc, left, top, right, bottom, xstart, ystart, xend, yend);
140 * If no error occurred, the current position is moved to the ending
143 if (result) MoveToEx(hdc, xend, yend, NULL);
148 /***********************************************************************
151 BOOL WINAPI Pie( HDC hdc, INT left, INT top,
152 INT right, INT bottom, INT xstart, INT ystart,
156 DC * dc = DC_GetDCUpdate( hdc );
157 if (!dc) return FALSE;
159 if(PATH_IsPathOpen(dc->path))
160 ret = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,2);
161 else if(dc->funcs->pPie)
162 ret = dc->funcs->pPie(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
164 GDI_ReleaseObj( hdc );
169 /***********************************************************************
172 BOOL WINAPI Chord( HDC hdc, INT left, INT top,
173 INT right, INT bottom, INT xstart, INT ystart,
177 DC * dc = DC_GetDCUpdate( hdc );
178 if (!dc) return FALSE;
180 if(PATH_IsPathOpen(dc->path))
181 ret = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,1);
182 else if(dc->funcs->pChord)
183 ret = dc->funcs->pChord(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
185 GDI_ReleaseObj( hdc );
190 /***********************************************************************
193 BOOL WINAPI Ellipse( HDC hdc, INT left, INT top,
194 INT right, INT bottom )
197 DC * dc = DC_GetDCUpdate( hdc );
198 if (!dc) return FALSE;
200 if(PATH_IsPathOpen(dc->path))
201 ret = PATH_Ellipse(dc,left,top,right,bottom);
202 else if (dc->funcs->pEllipse)
203 ret = dc->funcs->pEllipse(dc->physDev,left,top,right,bottom);
205 GDI_ReleaseObj( hdc );
210 /***********************************************************************
211 * Rectangle (GDI32.@)
213 BOOL WINAPI Rectangle( HDC hdc, INT left, INT top,
214 INT right, INT bottom )
217 DC * dc = DC_GetDCUpdate( hdc );
220 if(PATH_IsPathOpen(dc->path))
221 ret = PATH_Rectangle(dc, left, top, right, bottom);
222 else if (dc->funcs->pRectangle)
223 ret = dc->funcs->pRectangle(dc->physDev,left,top,right,bottom);
224 GDI_ReleaseObj( hdc );
230 /***********************************************************************
231 * RoundRect (GDI32.@)
233 BOOL WINAPI RoundRect( HDC hdc, INT left, INT top, INT right,
234 INT bottom, INT ell_width, INT ell_height )
237 DC *dc = DC_GetDCUpdate( hdc );
241 if(PATH_IsPathOpen(dc->path))
242 ret = PATH_RoundRect(dc,left,top,right,bottom,ell_width,ell_height);
243 else if (dc->funcs->pRoundRect)
244 ret = dc->funcs->pRoundRect(dc->physDev,left,top,right,bottom,ell_width,ell_height);
245 GDI_ReleaseObj( hdc );
250 /***********************************************************************
253 COLORREF WINAPI SetPixel( HDC hdc, INT x, INT y, COLORREF color )
256 DC * dc = DC_GetDCUpdate( hdc );
259 if (dc->funcs->pSetPixel) ret = dc->funcs->pSetPixel(dc->physDev,x,y,color);
260 GDI_ReleaseObj( hdc );
265 /***********************************************************************
266 * SetPixelV (GDI32.@)
268 BOOL WINAPI SetPixelV( HDC hdc, INT x, INT y, COLORREF color )
271 DC * dc = DC_GetDCUpdate( hdc );
274 if (dc->funcs->pSetPixel)
276 dc->funcs->pSetPixel(dc->physDev,x,y,color);
279 GDI_ReleaseObj( hdc );
284 /***********************************************************************
287 COLORREF WINAPI GetPixel( HDC hdc, INT x, INT y )
289 COLORREF ret = CLR_INVALID;
290 DC * dc = DC_GetDCUpdate( hdc );
294 /* FIXME: should this be in the graphics driver? */
295 if (PtVisible( hdc, x, y ))
297 if (dc->funcs->pGetPixel) ret = dc->funcs->pGetPixel(dc->physDev,x,y);
299 GDI_ReleaseObj( hdc );
305 /******************************************************************************
306 * ChoosePixelFormat [GDI32.@]
307 * Matches a pixel format to given format
310 * hdc [I] Device context to search for best pixel match
311 * ppfd [I] Pixel format for which a match is sought
314 * Success: Pixel format index closest to given format
317 INT WINAPI ChoosePixelFormat( HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd )
320 DC * dc = DC_GetDCPtr( hdc );
322 TRACE("(%p,%p)\n",hdc,ppfd);
326 if (!dc->funcs->pChoosePixelFormat) FIXME(" :stub\n");
327 else ret = dc->funcs->pChoosePixelFormat(dc->physDev,ppfd);
329 GDI_ReleaseObj( hdc );
334 /******************************************************************************
335 * SetPixelFormat [GDI32.@]
336 * Sets pixel format of device context
339 * hdc [I] Device context to search for best pixel match
340 * iPixelFormat [I] Pixel format index
341 * ppfd [I] Pixel format for which a match is sought
345 BOOL WINAPI SetPixelFormat( HDC hdc, INT iPixelFormat,
346 const PIXELFORMATDESCRIPTOR *ppfd)
349 DC * dc = DC_GetDCPtr( hdc );
351 TRACE("(%p,%d,%p)\n",hdc,iPixelFormat,ppfd);
355 if (!dc->funcs->pSetPixelFormat) FIXME(" :stub\n");
356 else bRet = dc->funcs->pSetPixelFormat(dc->physDev,iPixelFormat,ppfd);
358 GDI_ReleaseObj( hdc );
363 /******************************************************************************
364 * GetPixelFormat [GDI32.@]
365 * Gets index of pixel format of DC
368 * hdc [I] Device context whose pixel format index is sought
371 * Success: Currently selected pixel format
374 INT WINAPI GetPixelFormat( HDC hdc )
377 DC * dc = DC_GetDCPtr( hdc );
383 if (!dc->funcs->pGetPixelFormat) FIXME(" :stub\n");
384 else ret = dc->funcs->pGetPixelFormat(dc->physDev);
386 GDI_ReleaseObj( hdc );
391 /******************************************************************************
392 * DescribePixelFormat [GDI32.@]
393 * Gets info about pixel format from DC
396 * hdc [I] Device context
397 * iPixelFormat [I] Pixel format selector
398 * nBytes [I] Size of buffer
399 * ppfd [O] Pointer to structure to receive pixel format data
402 * Success: Maximum pixel format index of the device context
405 INT WINAPI DescribePixelFormat( HDC hdc, INT iPixelFormat, UINT nBytes,
406 LPPIXELFORMATDESCRIPTOR ppfd )
409 DC * dc = DC_GetDCPtr( hdc );
411 TRACE("(%p,%d,%d,%p): stub\n",hdc,iPixelFormat,nBytes,ppfd);
415 if (!dc->funcs->pDescribePixelFormat)
418 ppfd->nSize = nBytes;
422 else ret = dc->funcs->pDescribePixelFormat(dc->physDev,iPixelFormat,nBytes,ppfd);
424 GDI_ReleaseObj( hdc );
429 /******************************************************************************
430 * SwapBuffers [GDI32.@]
431 * Exchanges front and back buffers of window
434 * hdc [I] Device context whose buffers get swapped
438 BOOL WINAPI SwapBuffers( HDC hdc )
441 DC * dc = DC_GetDCPtr( hdc );
445 if (!dc) return TRUE;
447 if (!dc->funcs->pSwapBuffers)
452 else bRet = dc->funcs->pSwapBuffers(dc->physDev);
454 GDI_ReleaseObj( hdc );
459 /***********************************************************************
462 BOOL WINAPI PaintRgn( HDC hdc, HRGN hrgn )
465 DC * dc = DC_GetDCUpdate( hdc );
468 if (dc->funcs->pPaintRgn) ret = dc->funcs->pPaintRgn(dc->physDev,hrgn);
469 GDI_ReleaseObj( hdc );
475 /***********************************************************************
478 BOOL WINAPI FillRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush )
482 DC * dc = DC_GetDCUpdate( hdc );
484 if (!dc) return FALSE;
485 if(dc->funcs->pFillRgn)
486 retval = dc->funcs->pFillRgn(dc->physDev, hrgn, hbrush);
487 else if ((prevBrush = SelectObject( hdc, hbrush )))
489 retval = PaintRgn( hdc, hrgn );
490 SelectObject( hdc, prevBrush );
492 GDI_ReleaseObj( hdc );
497 /***********************************************************************
500 BOOL WINAPI FrameRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush,
501 INT nWidth, INT nHeight )
504 DC *dc = DC_GetDCUpdate( hdc );
506 if (!dc) return FALSE;
507 if(dc->funcs->pFrameRgn)
508 ret = dc->funcs->pFrameRgn( dc->physDev, hrgn, hbrush, nWidth, nHeight );
511 HRGN tmp = CreateRectRgn( 0, 0, 0, 0 );
514 if (REGION_FrameRgn( tmp, hrgn, nWidth, nHeight ))
516 FillRgn( hdc, tmp, hbrush );
522 GDI_ReleaseObj( hdc );
527 /***********************************************************************
528 * InvertRgn (GDI32.@)
530 BOOL WINAPI InvertRgn( HDC hdc, HRGN hrgn )
535 DC *dc = DC_GetDCUpdate( hdc );
536 if (!dc) return FALSE;
538 if(dc->funcs->pInvertRgn)
539 retval = dc->funcs->pInvertRgn( dc->physDev, hrgn );
542 prevBrush = SelectObject( hdc, GetStockObject(BLACK_BRUSH) );
543 prevROP = SetROP2( hdc, R2_NOT );
544 retval = PaintRgn( hdc, hrgn );
545 SelectObject( hdc, prevBrush );
546 SetROP2( hdc, prevROP );
548 GDI_ReleaseObj( hdc );
553 /**********************************************************************
556 BOOL WINAPI Polyline( HDC hdc, const POINT* pt, INT count )
559 DC * dc = DC_GetDCUpdate( hdc );
562 if (PATH_IsPathOpen(dc->path)) ret = PATH_Polyline(dc, pt, count);
563 else if (dc->funcs->pPolyline) ret = dc->funcs->pPolyline(dc->physDev,pt,count);
564 GDI_ReleaseObj( hdc );
569 /**********************************************************************
570 * PolylineTo (GDI32.@)
572 BOOL WINAPI PolylineTo( HDC hdc, const POINT* pt, DWORD cCount )
574 DC * dc = DC_GetDCUpdate( hdc );
577 if(!dc) return FALSE;
579 if(PATH_IsPathOpen(dc->path))
580 ret = PATH_PolylineTo(dc, pt, cCount);
582 else if(dc->funcs->pPolylineTo)
583 ret = dc->funcs->pPolylineTo(dc->physDev, pt, cCount);
585 else { /* do it using Polyline */
586 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
587 sizeof(POINT) * (cCount + 1) );
590 pts[0].x = dc->CursPosX;
591 pts[0].y = dc->CursPosY;
592 memcpy( pts + 1, pt, sizeof(POINT) * cCount );
593 ret = Polyline( hdc, pts, cCount + 1 );
594 HeapFree( GetProcessHeap(), 0, pts );
598 dc->CursPosX = pt[cCount-1].x;
599 dc->CursPosY = pt[cCount-1].y;
601 GDI_ReleaseObj( hdc );
606 /**********************************************************************
609 BOOL WINAPI Polygon( HDC hdc, const POINT* pt, INT count )
612 DC * dc = DC_GetDCUpdate( hdc );
615 if (PATH_IsPathOpen(dc->path)) ret = PATH_Polygon(dc, pt, count);
616 else if (dc->funcs->pPolygon) ret = dc->funcs->pPolygon(dc->physDev,pt,count);
617 GDI_ReleaseObj( hdc );
623 /**********************************************************************
624 * PolyPolygon (GDI32.@)
626 BOOL WINAPI PolyPolygon( HDC hdc, const POINT* pt, const INT* counts,
630 DC * dc = DC_GetDCUpdate( hdc );
633 if (PATH_IsPathOpen(dc->path)) ret = PATH_PolyPolygon(dc, pt, counts, polygons);
634 else if (dc->funcs->pPolyPolygon) ret = dc->funcs->pPolyPolygon(dc->physDev,pt,counts,polygons);
635 GDI_ReleaseObj( hdc );
640 /**********************************************************************
641 * PolyPolyline (GDI32.@)
643 BOOL WINAPI PolyPolyline( HDC hdc, const POINT* pt, const DWORD* counts,
647 DC * dc = DC_GetDCUpdate( hdc );
650 if (PATH_IsPathOpen(dc->path)) ret = PATH_PolyPolyline(dc, pt, counts, polylines);
651 else if (dc->funcs->pPolyPolyline) ret = dc->funcs->pPolyPolyline(dc->physDev,pt,counts,polylines);
652 GDI_ReleaseObj( hdc );
657 /**********************************************************************
658 * ExtFloodFill (GDI32.@)
660 BOOL WINAPI ExtFloodFill( HDC hdc, INT x, INT y, COLORREF color,
664 DC * dc = DC_GetDCUpdate( hdc );
667 if (dc->funcs->pExtFloodFill) ret = dc->funcs->pExtFloodFill(dc->physDev,x,y,color,fillType);
668 GDI_ReleaseObj( hdc );
674 /**********************************************************************
675 * FloodFill (GDI32.@)
677 BOOL WINAPI FloodFill( HDC hdc, INT x, INT y, COLORREF color )
679 return ExtFloodFill( hdc, x, y, color, FLOODFILLBORDER );
683 /******************************************************************************
684 * PolyBezier [GDI32.@]
685 * Draws one or more Bezier curves
688 * hDc [I] Handle to device context
689 * lppt [I] Pointer to endpoints and control points
690 * cPoints [I] Count of endpoints and control points
694 BOOL WINAPI PolyBezier( HDC hdc, const POINT* lppt, DWORD cPoints )
699 /* cPoints must be 3 * n + 1 (where n>=1) */
700 if (cPoints == 1 || (cPoints % 3) != 1) return FALSE;
702 dc = DC_GetDCUpdate( hdc );
703 if(!dc) return FALSE;
705 if(PATH_IsPathOpen(dc->path))
706 ret = PATH_PolyBezier(dc, lppt, cPoints);
707 else if (dc->funcs->pPolyBezier)
708 ret = dc->funcs->pPolyBezier(dc->physDev, lppt, cPoints);
709 else /* We'll convert it into line segments and draw them using Polyline */
714 if ((Pts = GDI_Bezier( lppt, cPoints, &nOut )))
716 TRACE("Pts = %p, no = %d\n", Pts, nOut);
717 ret = Polyline( dc->hSelf, Pts, nOut );
718 HeapFree( GetProcessHeap(), 0, Pts );
722 GDI_ReleaseObj( hdc );
726 /******************************************************************************
727 * PolyBezierTo [GDI32.@]
728 * Draws one or more Bezier curves
731 * hDc [I] Handle to device context
732 * lppt [I] Pointer to endpoints and control points
733 * cPoints [I] Count of endpoints and control points
737 BOOL WINAPI PolyBezierTo( HDC hdc, const POINT* lppt, DWORD cPoints )
739 DC * dc = DC_GetDCUpdate( hdc );
742 if(!dc) return FALSE;
744 if(PATH_IsPathOpen(dc->path))
745 ret = PATH_PolyBezierTo(dc, lppt, cPoints);
746 else if(dc->funcs->pPolyBezierTo)
747 ret = dc->funcs->pPolyBezierTo(dc->physDev, lppt, cPoints);
748 else { /* We'll do it using PolyBezier */
750 pt = HeapAlloc( GetProcessHeap(), 0, sizeof(POINT) * (cPoints + 1) );
751 if(!pt) return FALSE;
752 pt[0].x = dc->CursPosX;
753 pt[0].y = dc->CursPosY;
754 memcpy(pt + 1, lppt, sizeof(POINT) * cPoints);
755 ret = PolyBezier(dc->hSelf, pt, cPoints+1);
756 HeapFree( GetProcessHeap(), 0, pt );
759 dc->CursPosX = lppt[cPoints-1].x;
760 dc->CursPosY = lppt[cPoints-1].y;
762 GDI_ReleaseObj( hdc );
766 /***********************************************************************
769 BOOL WINAPI AngleArc(HDC hdc, INT x, INT y, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle)
771 INT x1,y1,x2,y2, arcdir;
775 if( (signed int)dwRadius < 0 )
778 dc = DC_GetDCUpdate( hdc );
779 if(!dc) return FALSE;
781 if(dc->funcs->pAngleArc)
783 result = dc->funcs->pAngleArc( dc->physDev, x, y, dwRadius, eStartAngle, eSweepAngle );
785 GDI_ReleaseObj( hdc );
788 GDI_ReleaseObj( hdc );
790 /* AngleArc always works counterclockwise */
791 arcdir = GetArcDirection( hdc );
792 SetArcDirection( hdc, AD_COUNTERCLOCKWISE );
794 x1 = x + cos(eStartAngle*M_PI/180) * dwRadius;
795 y1 = y - sin(eStartAngle*M_PI/180) * dwRadius;
796 x2 = x + cos((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius;
797 y2 = x - sin((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius;
799 LineTo( hdc, x1, y1 );
800 if( eSweepAngle >= 0 )
801 result = Arc( hdc, x-dwRadius, y-dwRadius, x+dwRadius, y+dwRadius,
804 result = Arc( hdc, x-dwRadius, y-dwRadius, x+dwRadius, y+dwRadius,
807 if( result ) MoveToEx( hdc, x2, y2, NULL );
808 SetArcDirection( hdc, arcdir );
812 /***********************************************************************
815 BOOL WINAPI PolyDraw(HDC hdc, const POINT *lppt, const BYTE *lpbTypes,
823 dc = DC_GetDCUpdate( hdc );
824 if(!dc) return FALSE;
826 if(dc->funcs->pPolyDraw)
828 result = dc->funcs->pPolyDraw( dc->physDev, lppt, lpbTypes, cCount );
829 GDI_ReleaseObj( hdc );
832 GDI_ReleaseObj( hdc );
834 /* check for each bezierto if there are two more points */
835 for( i = 0; i < cCount; i++ )
836 if( lpbTypes[i] != PT_MOVETO &&
837 lpbTypes[i] & PT_BEZIERTO )
845 /* if no moveto occurs, we will close the figure here */
846 lastmove.x = dc->CursPosX;
847 lastmove.y = dc->CursPosY;
850 for( i = 0; i < cCount; i++ )
852 if( lpbTypes[i] == PT_MOVETO )
854 MoveToEx( hdc, lppt[i].x, lppt[i].y, NULL );
855 lastmove.x = dc->CursPosX;
856 lastmove.y = dc->CursPosY;
858 else if( lpbTypes[i] & PT_LINETO )
859 LineTo( hdc, lppt[i].x, lppt[i].y );
860 else if( lpbTypes[i] & PT_BEZIERTO )
862 PolyBezierTo( hdc, &lppt[i], 3 );
868 if( lpbTypes[i] & PT_CLOSEFIGURE )
870 if( PATH_IsPathOpen( dc->path ) )
873 LineTo( hdc, lastmove.x, lastmove.y );
881 /**********************************************************************
884 BOOL WINAPI LineDDA(INT nXStart, INT nYStart, INT nXEnd, INT nYEnd,
885 LINEDDAPROC callback, LPARAM lParam )
887 INT xadd = 1, yadd = 1;
890 INT dx = nXEnd - nXStart;
891 INT dy = nYEnd - nYStart;
903 if (dx > dy) /* line is "more horizontal" */
905 err = 2*dy - dx; erradd = 2*dy - 2*dx;
906 for(cnt = 0;cnt <= dx; cnt++)
908 callback(nXStart,nYStart,lParam);
918 else /* line is "more vertical" */
920 err = 2*dx - dy; erradd = 2*dx - 2*dy;
921 for(cnt = 0;cnt <= dy; cnt++)
923 callback(nXStart,nYStart,lParam);
937 /******************************************************************
939 * *Very* simple bezier drawing code,
941 * It uses a recursive algorithm to divide the curve in a series
942 * of straight line segements. Not ideal but for me sufficient.
943 * If you are in need for something better look for some incremental
946 * 7 July 1998 Rein Klazes
950 * some macro definitions for bezier drawing
952 * to avoid truncation errors the coordinates are
953 * shifted upwards. When used in drawing they are
954 * shifted down again, including correct rounding
955 * and avoiding floating point arithmetic
956 * 4 bits should allow 27 bits coordinates which I saw
957 * somewhere in the win32 doc's
961 #define BEZIERSHIFTBITS 4
962 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
963 #define BEZIERPIXEL BEZIERSHIFTUP(1)
964 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
965 /* maximum depth of recursion */
966 #define BEZIERMAXDEPTH 8
968 /* size of array to store points on */
969 /* enough for one curve */
970 #define BEZIER_INITBUFSIZE (150)
972 /* calculate Bezier average, in this case the middle
973 * correctly rounded...
976 #define BEZIERMIDDLE(Mid, P1, P2) \
977 (Mid).x=((P1).x+(P2).x + 1)/2;\
978 (Mid).y=((P1).y+(P2).y + 1)/2;
980 /**********************************************************
981 * BezierCheck helper function to check
982 * that recursion can be terminated
983 * Points[0] and Points[3] are begin and endpoint
984 * Points[1] and Points[2] are control points
985 * level is the recursion depth
986 * returns true if the recusion can be terminated
988 static BOOL BezierCheck( int level, POINT *Points)
991 dx=Points[3].x-Points[0].x;
992 dy=Points[3].y-Points[0].y;
993 if(abs(dy)<=abs(dx)){/* shallow line */
994 /* check that control points are between begin and end */
995 if(Points[1].x < Points[0].x){
996 if(Points[1].x < Points[3].x)
999 if(Points[1].x > Points[3].x)
1001 if(Points[2].x < Points[0].x){
1002 if(Points[2].x < Points[3].x)
1005 if(Points[2].x > Points[3].x)
1007 dx=BEZIERSHIFTDOWN(dx);
1008 if(!dx) return TRUE;
1009 if(abs(Points[1].y-Points[0].y-(dy/dx)*
1010 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
1011 abs(Points[2].y-Points[0].y-(dy/dx)*
1012 BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
1016 }else{ /* steep line */
1017 /* check that control points are between begin and end */
1018 if(Points[1].y < Points[0].y){
1019 if(Points[1].y < Points[3].y)
1022 if(Points[1].y > Points[3].y)
1024 if(Points[2].y < Points[0].y){
1025 if(Points[2].y < Points[3].y)
1028 if(Points[2].y > Points[3].y)
1030 dy=BEZIERSHIFTDOWN(dy);
1031 if(!dy) return TRUE;
1032 if(abs(Points[1].x-Points[0].x-(dx/dy)*
1033 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
1034 abs(Points[2].x-Points[0].x-(dx/dy)*
1035 BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
1042 /* Helper for GDI_Bezier.
1043 * Just handles one Bezier, so Points should point to four POINTs
1045 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
1046 INT *nPtsOut, INT level )
1048 if(*nPtsOut == *dwOut) {
1050 *PtsOut = HeapReAlloc( GetProcessHeap(), 0, *PtsOut,
1051 *dwOut * sizeof(POINT) );
1054 if(!level || BezierCheck(level, Points)) {
1056 (*PtsOut)[0].x = BEZIERSHIFTDOWN(Points[0].x);
1057 (*PtsOut)[0].y = BEZIERSHIFTDOWN(Points[0].y);
1060 (*PtsOut)[*nPtsOut].x = BEZIERSHIFTDOWN(Points[3].x);
1061 (*PtsOut)[*nPtsOut].y = BEZIERSHIFTDOWN(Points[3].y);
1064 POINT Points2[4]; /* for the second recursive call */
1065 Points2[3]=Points[3];
1066 BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
1067 BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
1068 BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
1070 BEZIERMIDDLE(Points[1], Points[0], Points[1]);
1071 BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1072 BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1074 Points2[0]=Points[3];
1076 /* do the two halves */
1077 GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
1078 GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
1084 /***********************************************************************
1085 * GDI_Bezier [INTERNAL]
1086 * Calculate line segments that approximate -what microsoft calls- a bezier
1088 * The routine recursively divides the curve in two parts until a straight
1093 * Points [I] Ptr to count POINTs which are the end and control points
1094 * of the set of Bezier curves to flatten.
1095 * count [I] Number of Points. Must be 3n+1.
1096 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
1101 * Ptr to an array of POINTs that contain the lines that approximinate the
1102 * Beziers. The array is allocated on the process heap and it is the caller's
1103 * responsibility to HeapFree it. [this is not a particularly nice interface
1104 * but since we can't know in advance how many points will generate, the
1105 * alternative would be to call the function twice, once to determine the size
1106 * and a second time to do the work - I decided this was too much of a pain].
1108 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
1111 INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
1113 if((count - 1) % 3 != 0) {
1114 ERR("Invalid no. of points\n");
1118 out = HeapAlloc( GetProcessHeap(), 0, dwOut * sizeof(POINT));
1119 for(Bezier = 0; Bezier < (count-1)/3; Bezier++) {
1121 memcpy(ptBuf, Points + Bezier * 3, sizeof(POINT) * 4);
1122 for(i = 0; i < 4; i++) {
1123 ptBuf[i].x = BEZIERSHIFTUP(ptBuf[i].x);
1124 ptBuf[i].y = BEZIERSHIFTUP(ptBuf[i].y);
1126 GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
1128 TRACE("Produced %d points\n", *nPtsOut);
1132 /******************************************************************************
1133 * GradientFill (GDI32.@)
1135 * FIXME: we don't support the Alpha channel properly
1137 BOOL WINAPI GdiGradientFill( HDC hdc, TRIVERTEX *vert_array, ULONG nvert,
1138 void * grad_array, ULONG ngrad, ULONG mode )
1142 TRACE("vert_array:0x%08lx nvert:%ld grad_array:0x%08lx ngrad:%ld\n",
1143 (long)vert_array, nvert, (long)grad_array, ngrad);
1147 case GRADIENT_FILL_RECT_H:
1148 for(i = 0; i < ngrad; i++)
1150 GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1151 TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1152 TRIVERTEX *v2 = vert_array + rect->LowerRight;
1153 int y1 = v1->y < v2->y ? v1->y : v2->y;
1154 int y2 = v2->y > v1->y ? v2->y : v1->y;
1163 for (x = 0; x < dx; x++)
1168 hPen = CreatePen( PS_SOLID, 1, RGB(
1169 (v1->Red * (dx - x) + v2->Red * x) / dx >> 8,
1170 (v1->Green * (dx - x) + v2->Green * x) / dx >> 8,
1171 (v1->Blue * (dx - x) + v2->Blue * x) / dx >> 8));
1172 hOldPen = SelectObject( hdc, hPen );
1173 pts[0].x = v1->x + x;
1175 pts[1].x = v1->x + x;
1177 Polyline( hdc, &pts[0], 2 );
1178 DeleteObject( SelectObject(hdc, hOldPen ) );
1182 case GRADIENT_FILL_RECT_V:
1183 for(i = 0; i < ngrad; i++)
1185 GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1186 TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1187 TRIVERTEX *v2 = vert_array + rect->LowerRight;
1188 int x1 = v1->x < v2->x ? v1->x : v2->x;
1189 int x2 = v2->x > v1->x ? v2->x : v1->x;
1198 for (y = 0; y < dy; y++)
1203 hPen = CreatePen( PS_SOLID, 1, RGB(
1204 (v1->Red * (dy - y) + v2->Red * y) / dy >> 8,
1205 (v1->Green * (dy - y) + v2->Green * y) / dy >> 8,
1206 (v1->Blue * (dy - y) + v2->Blue * y) / dy >> 8));
1207 hOldPen = SelectObject( hdc, hPen );
1209 pts[0].y = v1->y + y;
1211 pts[1].y = v1->y + y;
1212 Polyline( hdc, &pts[0], 2 );
1213 DeleteObject( SelectObject(hdc, hOldPen ) );
1217 case GRADIENT_FILL_TRIANGLE:
1218 for (i = 0; i < ngrad; i++)
1220 GRADIENT_TRIANGLE *tri = ((GRADIENT_TRIANGLE *)grad_array) + i;
1221 TRIVERTEX *v1 = vert_array + tri->Vertex1;
1222 TRIVERTEX *v2 = vert_array + tri->Vertex2;
1223 TRIVERTEX *v3 = vert_array + tri->Vertex3;
1227 { TRIVERTEX *t = v1; v1 = v2; v2 = t; }
1230 TRIVERTEX *t = v2; v2 = v3; v3 = t;
1232 { t = v1; v1 = v2; v2 = t; }
1234 /* v1->y <= v2->y <= v3->y */
1237 for (y = 0; y < dy; y++)
1239 /* v1->y <= y < v3->y */
1240 TRIVERTEX *v = y < (v2->y - v1->y) ? v1 : v3;
1241 /* (v->y <= y < v2->y) || (v2->y <= y < v->y) */
1242 int dy2 = v2->y - v->y;
1243 int y2 = y + v1->y - v->y;
1245 int x1 = (v3->x * y + v1->x * (dy - y )) / dy;
1246 int x2 = (v2->x * y2 + v-> x * (dy2 - y2)) / dy2;
1247 int r1 = (v3->Red * y + v1->Red * (dy - y )) / dy;
1248 int r2 = (v2->Red * y2 + v-> Red * (dy2 - y2)) / dy2;
1249 int g1 = (v3->Green * y + v1->Green * (dy - y )) / dy;
1250 int g2 = (v2->Green * y2 + v-> Green * (dy2 - y2)) / dy2;
1251 int b1 = (v3->Blue * y + v1->Blue * (dy - y )) / dy;
1252 int b2 = (v2->Blue * y2 + v-> Blue * (dy2 - y2)) / dy2;
1258 for (x = 0; x < dx; x++)
1259 SetPixel (hdc, x + x1, y + v1->y, RGB(
1260 (r1 * (dx - x) + r2 * x) / dx >> 8,
1261 (g1 * (dx - x) + g2 * x) / dx >> 8,
1262 (b1 * (dx - x) + b2 * x) / dx >> 8));
1267 for (x = 0; x < dx; x++)
1268 SetPixel (hdc, x + x2, y + v1->y, RGB(
1269 (r2 * (dx - x) + r1 * x) / dx >> 8,
1270 (g2 * (dx - x) + g1 * x) / dx >> 8,
1271 (b2 * (dx - x) + b1 * x) / dx >> 8));