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 = get_dc_ptr( 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);
64 /***********************************************************************
67 BOOL WINAPI MoveToEx( HDC hdc, INT x, INT y, LPPOINT pt )
70 DC * dc = get_dc_ptr( 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);
88 /***********************************************************************
91 BOOL WINAPI Arc( HDC hdc, INT left, INT top, INT right,
92 INT bottom, INT xstart, INT ystart,
96 DC * dc = get_dc_ptr( hdc );
101 if(PATH_IsPathOpen(dc->path))
102 ret = PATH_Arc(dc, left, top, right, bottom, xstart, ystart, xend, yend,0);
103 else if (dc->funcs->pArc)
104 ret = dc->funcs->pArc(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
105 release_dc_ptr( dc );
110 /***********************************************************************
113 BOOL WINAPI ArcTo( HDC hdc,
115 INT right, INT bottom,
116 INT xstart, INT ystart,
119 double width = fabs(right-left),
120 height = fabs(bottom-top),
123 xcenter = right > left ? left+xradius : right+xradius,
124 ycenter = bottom > top ? top+yradius : bottom+yradius,
127 DC * dc = get_dc_ptr( hdc );
128 if(!dc) return FALSE;
131 if(PATH_IsPathOpen(dc->path))
132 result = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,-1);
133 else if(dc->funcs->pArcTo)
134 result = dc->funcs->pArcTo( dc->physDev, left, top, right, bottom,
135 xstart, ystart, xend, yend );
136 else /* We'll draw a line from the current position to the starting point of the arc, then draw the arc */
138 angle = atan2(((ystart-ycenter)/height),
139 ((xstart-xcenter)/width));
140 LineTo(hdc, GDI_ROUND(xcenter+(cos(angle)*xradius)),
141 GDI_ROUND(ycenter+(sin(angle)*yradius)));
142 result = Arc(hdc, left, top, right, bottom, xstart, ystart, xend, yend);
145 angle = atan2(((yend-ycenter)/height),
146 ((xend-xcenter)/width));
147 dc->CursPosX = GDI_ROUND(xcenter+(cos(angle)*xradius));
148 dc->CursPosY = GDI_ROUND(ycenter+(sin(angle)*yradius));
150 release_dc_ptr( dc );
155 /***********************************************************************
158 BOOL WINAPI Pie( HDC hdc, INT left, INT top,
159 INT right, INT bottom, INT xstart, INT ystart,
163 DC * dc = get_dc_ptr( hdc );
164 if (!dc) return FALSE;
167 if(PATH_IsPathOpen(dc->path))
168 ret = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,2);
169 else if(dc->funcs->pPie)
170 ret = dc->funcs->pPie(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
172 release_dc_ptr( dc );
177 /***********************************************************************
180 BOOL WINAPI Chord( HDC hdc, INT left, INT top,
181 INT right, INT bottom, INT xstart, INT ystart,
185 DC * dc = get_dc_ptr( hdc );
186 if (!dc) return FALSE;
189 if(PATH_IsPathOpen(dc->path))
190 ret = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,1);
191 else if(dc->funcs->pChord)
192 ret = dc->funcs->pChord(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
194 release_dc_ptr( dc );
199 /***********************************************************************
202 BOOL WINAPI Ellipse( HDC hdc, INT left, INT top,
203 INT right, INT bottom )
206 DC * dc = get_dc_ptr( hdc );
207 if (!dc) return FALSE;
210 if(PATH_IsPathOpen(dc->path))
211 ret = PATH_Ellipse(dc,left,top,right,bottom);
212 else if (dc->funcs->pEllipse)
213 ret = dc->funcs->pEllipse(dc->physDev,left,top,right,bottom);
215 release_dc_ptr( dc );
220 /***********************************************************************
221 * Rectangle (GDI32.@)
223 BOOL WINAPI Rectangle( HDC hdc, INT left, INT top,
224 INT right, INT bottom )
227 DC * dc = get_dc_ptr( hdc );
232 if(PATH_IsPathOpen(dc->path))
233 ret = PATH_Rectangle(dc, left, top, right, bottom);
234 else if (dc->funcs->pRectangle)
235 ret = dc->funcs->pRectangle(dc->physDev,left,top,right,bottom);
236 release_dc_ptr( dc );
242 /***********************************************************************
243 * RoundRect (GDI32.@)
245 BOOL WINAPI RoundRect( HDC hdc, INT left, INT top, INT right,
246 INT bottom, INT ell_width, INT ell_height )
249 DC *dc = get_dc_ptr( hdc );
254 if(PATH_IsPathOpen(dc->path))
255 ret = PATH_RoundRect(dc,left,top,right,bottom,ell_width,ell_height);
256 else if (dc->funcs->pRoundRect)
257 ret = dc->funcs->pRoundRect(dc->physDev,left,top,right,bottom,ell_width,ell_height);
258 release_dc_ptr( dc );
263 /***********************************************************************
266 COLORREF WINAPI SetPixel( HDC hdc, INT x, INT y, COLORREF color )
269 DC * dc = get_dc_ptr( hdc );
274 if (dc->funcs->pSetPixel) ret = dc->funcs->pSetPixel(dc->physDev,x,y,color);
275 release_dc_ptr( dc );
280 /***********************************************************************
281 * SetPixelV (GDI32.@)
283 BOOL WINAPI SetPixelV( HDC hdc, INT x, INT y, COLORREF color )
286 DC * dc = get_dc_ptr( hdc );
291 if (dc->funcs->pSetPixel)
293 dc->funcs->pSetPixel(dc->physDev,x,y,color);
296 release_dc_ptr( dc );
301 /***********************************************************************
304 COLORREF WINAPI GetPixel( HDC hdc, INT x, INT y )
306 COLORREF ret = CLR_INVALID;
307 DC * dc = get_dc_ptr( hdc );
312 /* FIXME: should this be in the graphics driver? */
313 if (PtVisible( hdc, x, y ))
315 if (dc->funcs->pGetPixel) ret = dc->funcs->pGetPixel(dc->physDev,x,y);
317 release_dc_ptr( dc );
323 /******************************************************************************
324 * ChoosePixelFormat [GDI32.@]
325 * Matches a pixel format to given format
328 * hdc [I] Device context to search for best pixel match
329 * ppfd [I] Pixel format for which a match is sought
332 * Success: Pixel format index closest to given format
335 INT WINAPI ChoosePixelFormat( HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd )
338 DC * dc = get_dc_ptr( hdc );
340 TRACE("(%p,%p)\n",hdc,ppfd);
344 if (!dc->funcs->pChoosePixelFormat) FIXME(" :stub\n");
345 else ret = dc->funcs->pChoosePixelFormat(dc->physDev,ppfd);
347 release_dc_ptr( dc );
352 /******************************************************************************
353 * SetPixelFormat [GDI32.@]
354 * Sets pixel format of device context
357 * hdc [I] Device context to search for best pixel match
358 * iPixelFormat [I] Pixel format index
359 * ppfd [I] Pixel format for which a match is sought
365 BOOL WINAPI SetPixelFormat( HDC hdc, INT iPixelFormat,
366 const PIXELFORMATDESCRIPTOR *ppfd)
369 DC * dc = get_dc_ptr( hdc );
371 TRACE("(%p,%d,%p)\n",hdc,iPixelFormat,ppfd);
375 if (!dc->funcs->pSetPixelFormat) FIXME(" :stub\n");
376 else bRet = dc->funcs->pSetPixelFormat(dc->physDev,iPixelFormat,ppfd);
378 release_dc_ptr( dc );
383 /******************************************************************************
384 * GetPixelFormat [GDI32.@]
385 * Gets index of pixel format of DC
388 * hdc [I] Device context whose pixel format index is sought
391 * Success: Currently selected pixel format
394 INT WINAPI GetPixelFormat( HDC hdc )
397 DC * dc = get_dc_ptr( hdc );
404 if (!dc->funcs->pGetPixelFormat) FIXME(" :stub\n");
405 else ret = dc->funcs->pGetPixelFormat(dc->physDev);
407 release_dc_ptr( dc );
412 /******************************************************************************
413 * DescribePixelFormat [GDI32.@]
414 * Gets info about pixel format from DC
417 * hdc [I] Device context
418 * iPixelFormat [I] Pixel format selector
419 * nBytes [I] Size of buffer
420 * ppfd [O] Pointer to structure to receive pixel format data
423 * Success: Maximum pixel format index of the device context
426 INT WINAPI DescribePixelFormat( HDC hdc, INT iPixelFormat, UINT nBytes,
427 LPPIXELFORMATDESCRIPTOR ppfd )
430 DC * dc = get_dc_ptr( hdc );
432 TRACE("(%p,%d,%d,%p): stub\n",hdc,iPixelFormat,nBytes,ppfd);
436 if (!dc->funcs->pDescribePixelFormat)
439 ppfd->nSize = nBytes;
443 else ret = dc->funcs->pDescribePixelFormat(dc->physDev,iPixelFormat,nBytes,ppfd);
445 release_dc_ptr( dc );
450 /******************************************************************************
451 * SwapBuffers [GDI32.@]
452 * Exchanges front and back buffers of window
455 * hdc [I] Device context whose buffers get swapped
461 BOOL WINAPI SwapBuffers( HDC hdc )
464 DC * dc = get_dc_ptr( hdc );
468 if (!dc) return TRUE;
471 if (!dc->funcs->pSwapBuffers)
476 else bRet = dc->funcs->pSwapBuffers(dc->physDev);
478 release_dc_ptr( dc );
483 /***********************************************************************
486 BOOL WINAPI PaintRgn( HDC hdc, HRGN hrgn )
489 DC * dc = get_dc_ptr( hdc );
494 if (dc->funcs->pPaintRgn) ret = dc->funcs->pPaintRgn(dc->physDev,hrgn);
495 release_dc_ptr( dc );
501 /***********************************************************************
504 BOOL WINAPI FillRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush )
508 DC * dc = get_dc_ptr( hdc );
510 if (!dc) return FALSE;
511 if(dc->funcs->pFillRgn)
514 retval = dc->funcs->pFillRgn(dc->physDev, hrgn, hbrush);
516 else if ((prevBrush = SelectObject( hdc, hbrush )))
518 retval = PaintRgn( hdc, hrgn );
519 SelectObject( hdc, prevBrush );
521 release_dc_ptr( dc );
526 /***********************************************************************
529 BOOL WINAPI FrameRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush,
530 INT nWidth, INT nHeight )
533 DC *dc = get_dc_ptr( hdc );
535 if (!dc) return FALSE;
537 if(dc->funcs->pFrameRgn)
540 ret = dc->funcs->pFrameRgn( dc->physDev, hrgn, hbrush, nWidth, nHeight );
544 HRGN tmp = CreateRectRgn( 0, 0, 0, 0 );
547 if (REGION_FrameRgn( tmp, hrgn, nWidth, nHeight ))
549 FillRgn( hdc, tmp, hbrush );
555 release_dc_ptr( dc );
560 /***********************************************************************
561 * InvertRgn (GDI32.@)
563 BOOL WINAPI InvertRgn( HDC hdc, HRGN hrgn )
568 DC *dc = get_dc_ptr( hdc );
569 if (!dc) return FALSE;
571 if(dc->funcs->pInvertRgn)
574 retval = dc->funcs->pInvertRgn( dc->physDev, hrgn );
578 prevBrush = SelectObject( hdc, GetStockObject(BLACK_BRUSH) );
579 prevROP = SetROP2( hdc, R2_NOT );
580 retval = PaintRgn( hdc, hrgn );
581 SelectObject( hdc, prevBrush );
582 SetROP2( hdc, prevROP );
584 release_dc_ptr( dc );
589 /**********************************************************************
592 BOOL WINAPI Polyline( HDC hdc, const POINT* pt, INT count )
595 DC * dc = get_dc_ptr( hdc );
600 if (PATH_IsPathOpen(dc->path)) ret = PATH_Polyline(dc, pt, count);
601 else if (dc->funcs->pPolyline) ret = dc->funcs->pPolyline(dc->physDev,pt,count);
602 release_dc_ptr( dc );
607 /**********************************************************************
608 * PolylineTo (GDI32.@)
610 BOOL WINAPI PolylineTo( HDC hdc, const POINT* pt, DWORD cCount )
612 DC * dc = get_dc_ptr( hdc );
615 if(!dc) return FALSE;
617 if(PATH_IsPathOpen(dc->path))
620 ret = PATH_PolylineTo(dc, pt, cCount);
622 else if(dc->funcs->pPolylineTo)
625 ret = dc->funcs->pPolylineTo(dc->physDev, pt, cCount);
627 else /* do it using Polyline */
629 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
630 sizeof(POINT) * (cCount + 1) );
633 pts[0].x = dc->CursPosX;
634 pts[0].y = dc->CursPosY;
635 memcpy( pts + 1, pt, sizeof(POINT) * cCount );
636 ret = Polyline( hdc, pts, cCount + 1 );
637 HeapFree( GetProcessHeap(), 0, pts );
641 dc->CursPosX = pt[cCount-1].x;
642 dc->CursPosY = pt[cCount-1].y;
644 release_dc_ptr( dc );
649 /**********************************************************************
652 BOOL WINAPI Polygon( HDC hdc, const POINT* pt, INT count )
655 DC * dc = get_dc_ptr( hdc );
660 if (PATH_IsPathOpen(dc->path)) ret = PATH_Polygon(dc, pt, count);
661 else if (dc->funcs->pPolygon) ret = dc->funcs->pPolygon(dc->physDev,pt,count);
662 release_dc_ptr( dc );
668 /**********************************************************************
669 * PolyPolygon (GDI32.@)
671 BOOL WINAPI PolyPolygon( HDC hdc, const POINT* pt, const INT* counts,
675 DC * dc = get_dc_ptr( hdc );
680 if (PATH_IsPathOpen(dc->path)) ret = PATH_PolyPolygon(dc, pt, counts, polygons);
681 else if (dc->funcs->pPolyPolygon) ret = dc->funcs->pPolyPolygon(dc->physDev,pt,counts,polygons);
682 release_dc_ptr( dc );
687 /**********************************************************************
688 * PolyPolyline (GDI32.@)
690 BOOL WINAPI PolyPolyline( HDC hdc, const POINT* pt, const DWORD* counts,
694 DC * dc = get_dc_ptr( hdc );
699 if (PATH_IsPathOpen(dc->path)) ret = PATH_PolyPolyline(dc, pt, counts, polylines);
700 else if (dc->funcs->pPolyPolyline) ret = dc->funcs->pPolyPolyline(dc->physDev,pt,counts,polylines);
701 release_dc_ptr( dc );
706 /**********************************************************************
707 * ExtFloodFill (GDI32.@)
709 BOOL WINAPI ExtFloodFill( HDC hdc, INT x, INT y, COLORREF color,
713 DC * dc = get_dc_ptr( hdc );
718 if (dc->funcs->pExtFloodFill) ret = dc->funcs->pExtFloodFill(dc->physDev,x,y,color,fillType);
719 release_dc_ptr( dc );
725 /**********************************************************************
726 * FloodFill (GDI32.@)
728 BOOL WINAPI FloodFill( HDC hdc, INT x, INT y, COLORREF color )
730 return ExtFloodFill( hdc, x, y, color, FLOODFILLBORDER );
734 /******************************************************************************
735 * PolyBezier [GDI32.@]
736 * Draws one or more Bezier curves
739 * hDc [I] Handle to device context
740 * lppt [I] Pointer to endpoints and control points
741 * cPoints [I] Count of endpoints and control points
747 BOOL WINAPI PolyBezier( HDC hdc, const POINT* lppt, DWORD cPoints )
752 /* cPoints must be 3 * n + 1 (where n>=1) */
753 if (cPoints == 1 || (cPoints % 3) != 1) return FALSE;
755 dc = get_dc_ptr( hdc );
756 if(!dc) return FALSE;
758 if(PATH_IsPathOpen(dc->path))
761 ret = PATH_PolyBezier(dc, lppt, cPoints);
763 else if (dc->funcs->pPolyBezier)
766 ret = dc->funcs->pPolyBezier(dc->physDev, lppt, cPoints);
768 else /* We'll convert it into line segments and draw them using Polyline */
773 if ((Pts = GDI_Bezier( lppt, cPoints, &nOut )))
775 TRACE("Pts = %p, no = %d\n", Pts, nOut);
776 ret = Polyline( hdc, Pts, nOut );
777 HeapFree( GetProcessHeap(), 0, Pts );
781 release_dc_ptr( dc );
785 /******************************************************************************
786 * PolyBezierTo [GDI32.@]
787 * Draws one or more Bezier curves
790 * hDc [I] Handle to device context
791 * lppt [I] Pointer to endpoints and control points
792 * cPoints [I] Count of endpoints and control points
798 BOOL WINAPI PolyBezierTo( HDC hdc, const POINT* lppt, DWORD cPoints )
803 /* cbPoints must be 3 * n (where n>=1) */
804 if (!cPoints || (cPoints % 3) != 0) return FALSE;
806 dc = get_dc_ptr( hdc );
807 if(!dc) return FALSE;
809 if(PATH_IsPathOpen(dc->path))
812 ret = PATH_PolyBezierTo(dc, lppt, cPoints);
814 else if(dc->funcs->pPolyBezierTo)
817 ret = dc->funcs->pPolyBezierTo(dc->physDev, lppt, cPoints);
819 else /* We'll do it using PolyBezier */
821 POINT *pt = HeapAlloc( GetProcessHeap(), 0, sizeof(POINT) * (cPoints + 1) );
824 pt[0].x = dc->CursPosX;
825 pt[0].y = dc->CursPosY;
826 memcpy(pt + 1, lppt, sizeof(POINT) * cPoints);
827 ret = PolyBezier(hdc, pt, cPoints+1);
828 HeapFree( GetProcessHeap(), 0, pt );
832 dc->CursPosX = lppt[cPoints-1].x;
833 dc->CursPosY = lppt[cPoints-1].y;
835 release_dc_ptr( dc );
839 /***********************************************************************
842 BOOL WINAPI AngleArc(HDC hdc, INT x, INT y, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle)
844 INT x1,y1,x2,y2, arcdir;
848 if( (signed int)dwRadius < 0 )
851 dc = get_dc_ptr( hdc );
852 if(!dc) return FALSE;
854 /* Calculate the end point */
855 x2 = x + cos((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius;
856 y2 = y - sin((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius;
858 if(!PATH_IsPathOpen(dc->path) && dc->funcs->pAngleArc)
861 result = dc->funcs->pAngleArc( dc->physDev, x, y, dwRadius, eStartAngle, eSweepAngle );
863 else { /* do it using ArcTo */
864 x1 = x + cos(eStartAngle*M_PI/180) * dwRadius;
865 y1 = y - sin(eStartAngle*M_PI/180) * dwRadius;
867 arcdir = SetArcDirection( hdc, eSweepAngle >= 0 ? AD_COUNTERCLOCKWISE : AD_CLOCKWISE);
868 result = ArcTo( hdc, x-dwRadius, y-dwRadius, x+dwRadius, y+dwRadius,
870 SetArcDirection( hdc, arcdir );
876 release_dc_ptr( dc );
880 /***********************************************************************
883 BOOL WINAPI PolyDraw(HDC hdc, const POINT *lppt, const BYTE *lpbTypes,
888 POINT * line_pts = NULL, * bzr_pts = NULL, bzr[4];
889 INT i, num_pts, num_bzr_pts, space, size;
891 dc = get_dc_ptr( hdc );
892 if(!dc) return FALSE;
894 if( PATH_IsPathOpen( dc->path ) )
897 result = PATH_PolyDraw(dc, lppt, lpbTypes, cCount);
899 else if(dc->funcs->pPolyDraw)
902 result = dc->funcs->pPolyDraw( dc->physDev, lppt, lpbTypes, cCount );
905 /* check for valid point types */
906 for(i = 0; i < cCount; i++) {
907 switch(lpbTypes[i]) {
909 case PT_LINETO | PT_CLOSEFIGURE:
913 if((i + 2 < cCount) && (lpbTypes[i + 1] == PT_BEZIERTO) &&
914 ((lpbTypes[i + 2] & ~PT_CLOSEFIGURE) == PT_BEZIERTO)){
923 space = cCount + 300;
924 line_pts = HeapAlloc(GetProcessHeap(), 0, space * sizeof(POINT));
927 line_pts[0].x = dc->CursPosX;
928 line_pts[0].y = dc->CursPosY;
930 for(i = 0; i < cCount; i++) {
931 switch(lpbTypes[i]) {
934 Polyline(hdc, line_pts, num_pts);
936 line_pts[num_pts++] = lppt[i];
939 case (PT_LINETO | PT_CLOSEFIGURE):
940 line_pts[num_pts++] = lppt[i];
943 bzr[0].x = line_pts[num_pts - 1].x;
944 bzr[0].y = line_pts[num_pts - 1].y;
945 memcpy(&bzr[1], &lppt[i], 3 * sizeof(POINT));
947 bzr_pts = GDI_Bezier(bzr, 4, &num_bzr_pts);
949 size = num_pts + (cCount - i) + num_bzr_pts;
952 line_pts = HeapReAlloc(GetProcessHeap(), 0, line_pts,
953 space * sizeof(POINT));
955 memcpy(&line_pts[num_pts], &bzr_pts[1],
956 (num_bzr_pts - 1) * sizeof(POINT));
957 num_pts += num_bzr_pts - 1;
958 HeapFree(GetProcessHeap(), 0, bzr_pts);
965 if(lpbTypes[i] & PT_CLOSEFIGURE)
966 line_pts[num_pts++] = line_pts[0];
970 Polyline(hdc, line_pts, num_pts);
972 MoveToEx(hdc, line_pts[num_pts - 1].x, line_pts[num_pts - 1].y, NULL);
973 HeapFree(GetProcessHeap(), 0, line_pts);
978 release_dc_ptr( dc );
983 /**********************************************************************
986 BOOL WINAPI LineDDA(INT nXStart, INT nYStart, INT nXEnd, INT nYEnd,
987 LINEDDAPROC callback, LPARAM lParam )
989 INT xadd = 1, yadd = 1;
992 INT dx = nXEnd - nXStart;
993 INT dy = nYEnd - nYStart;
1005 if (dx > dy) /* line is "more horizontal" */
1007 err = 2*dy - dx; erradd = 2*dy - 2*dx;
1008 for(cnt = 0;cnt <= dx; cnt++)
1010 callback(nXStart,nYStart,lParam);
1020 else /* line is "more vertical" */
1022 err = 2*dx - dy; erradd = 2*dx - 2*dy;
1023 for(cnt = 0;cnt <= dy; cnt++)
1025 callback(nXStart,nYStart,lParam);
1039 /******************************************************************
1041 * *Very* simple bezier drawing code,
1043 * It uses a recursive algorithm to divide the curve in a series
1044 * of straight line segements. Not ideal but for me sufficient.
1045 * If you are in need for something better look for some incremental
1048 * 7 July 1998 Rein Klazes
1052 * some macro definitions for bezier drawing
1054 * to avoid truncation errors the coordinates are
1055 * shifted upwards. When used in drawing they are
1056 * shifted down again, including correct rounding
1057 * and avoiding floating point arithmetic
1058 * 4 bits should allow 27 bits coordinates which I saw
1059 * somewhere in the win32 doc's
1063 #define BEZIERSHIFTBITS 4
1064 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
1065 #define BEZIERPIXEL BEZIERSHIFTUP(1)
1066 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
1067 /* maximum depth of recursion */
1068 #define BEZIERMAXDEPTH 8
1070 /* size of array to store points on */
1071 /* enough for one curve */
1072 #define BEZIER_INITBUFSIZE (150)
1074 /* calculate Bezier average, in this case the middle
1075 * correctly rounded...
1078 #define BEZIERMIDDLE(Mid, P1, P2) \
1079 (Mid).x=((P1).x+(P2).x + 1)/2;\
1080 (Mid).y=((P1).y+(P2).y + 1)/2;
1082 /**********************************************************
1083 * BezierCheck helper function to check
1084 * that recursion can be terminated
1085 * Points[0] and Points[3] are begin and endpoint
1086 * Points[1] and Points[2] are control points
1087 * level is the recursion depth
1088 * returns true if the recusion can be terminated
1090 static BOOL BezierCheck( int level, POINT *Points)
1093 dx=Points[3].x-Points[0].x;
1094 dy=Points[3].y-Points[0].y;
1095 if(abs(dy)<=abs(dx)){/* shallow line */
1096 /* check that control points are between begin and end */
1097 if(Points[1].x < Points[0].x){
1098 if(Points[1].x < Points[3].x)
1101 if(Points[1].x > Points[3].x)
1103 if(Points[2].x < Points[0].x){
1104 if(Points[2].x < Points[3].x)
1107 if(Points[2].x > Points[3].x)
1109 dx=BEZIERSHIFTDOWN(dx);
1110 if(!dx) return TRUE;
1111 if(abs(Points[1].y-Points[0].y-(dy/dx)*
1112 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
1113 abs(Points[2].y-Points[0].y-(dy/dx)*
1114 BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
1118 }else{ /* steep line */
1119 /* check that control points are between begin and end */
1120 if(Points[1].y < Points[0].y){
1121 if(Points[1].y < Points[3].y)
1124 if(Points[1].y > Points[3].y)
1126 if(Points[2].y < Points[0].y){
1127 if(Points[2].y < Points[3].y)
1130 if(Points[2].y > Points[3].y)
1132 dy=BEZIERSHIFTDOWN(dy);
1133 if(!dy) return TRUE;
1134 if(abs(Points[1].x-Points[0].x-(dx/dy)*
1135 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
1136 abs(Points[2].x-Points[0].x-(dx/dy)*
1137 BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
1144 /* Helper for GDI_Bezier.
1145 * Just handles one Bezier, so Points should point to four POINTs
1147 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
1148 INT *nPtsOut, INT level )
1150 if(*nPtsOut == *dwOut) {
1152 *PtsOut = HeapReAlloc( GetProcessHeap(), 0, *PtsOut,
1153 *dwOut * sizeof(POINT) );
1156 if(!level || BezierCheck(level, Points)) {
1158 (*PtsOut)[0].x = BEZIERSHIFTDOWN(Points[0].x);
1159 (*PtsOut)[0].y = BEZIERSHIFTDOWN(Points[0].y);
1162 (*PtsOut)[*nPtsOut].x = BEZIERSHIFTDOWN(Points[3].x);
1163 (*PtsOut)[*nPtsOut].y = BEZIERSHIFTDOWN(Points[3].y);
1166 POINT Points2[4]; /* for the second recursive call */
1167 Points2[3]=Points[3];
1168 BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
1169 BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
1170 BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
1172 BEZIERMIDDLE(Points[1], Points[0], Points[1]);
1173 BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1174 BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1176 Points2[0]=Points[3];
1178 /* do the two halves */
1179 GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
1180 GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
1186 /***********************************************************************
1187 * GDI_Bezier [INTERNAL]
1188 * Calculate line segments that approximate -what microsoft calls- a bezier
1190 * The routine recursively divides the curve in two parts until a straight
1195 * Points [I] Ptr to count POINTs which are the end and control points
1196 * of the set of Bezier curves to flatten.
1197 * count [I] Number of Points. Must be 3n+1.
1198 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
1203 * Ptr to an array of POINTs that contain the lines that approximinate the
1204 * Beziers. The array is allocated on the process heap and it is the caller's
1205 * responsibility to HeapFree it. [this is not a particularly nice interface
1206 * but since we can't know in advance how many points will generate, the
1207 * alternative would be to call the function twice, once to determine the size
1208 * and a second time to do the work - I decided this was too much of a pain].
1210 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
1213 INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
1215 if (count == 1 || (count - 1) % 3 != 0) {
1216 ERR("Invalid no. of points %d\n", count);
1220 out = HeapAlloc( GetProcessHeap(), 0, dwOut * sizeof(POINT));
1221 for(Bezier = 0; Bezier < (count-1)/3; Bezier++) {
1223 memcpy(ptBuf, Points + Bezier * 3, sizeof(POINT) * 4);
1224 for(i = 0; i < 4; i++) {
1225 ptBuf[i].x = BEZIERSHIFTUP(ptBuf[i].x);
1226 ptBuf[i].y = BEZIERSHIFTUP(ptBuf[i].y);
1228 GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
1230 TRACE("Produced %d points\n", *nPtsOut);
1234 /******************************************************************************
1235 * GdiGradientFill (GDI32.@)
1237 * FIXME: we don't support the Alpha channel properly
1239 BOOL WINAPI GdiGradientFill( HDC hdc, TRIVERTEX *vert_array, ULONG nvert,
1240 void * grad_array, ULONG ngrad, ULONG mode )
1244 TRACE("vert_array:0x%08lx nvert:%d grad_array:0x%08lx ngrad:%d\n",
1245 (long)vert_array, nvert, (long)grad_array, ngrad);
1249 case GRADIENT_FILL_RECT_H:
1250 for(i = 0; i < ngrad; i++)
1252 GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1253 TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1254 TRIVERTEX *v2 = vert_array + rect->LowerRight;
1255 int y1 = v1->y < v2->y ? v1->y : v2->y;
1256 int y2 = v2->y > v1->y ? v2->y : v1->y;
1265 for (x = 0; x < dx; x++)
1270 hPen = CreatePen( PS_SOLID, 1, RGB(
1271 (v1->Red * (dx - x) + v2->Red * x) / dx >> 8,
1272 (v1->Green * (dx - x) + v2->Green * x) / dx >> 8,
1273 (v1->Blue * (dx - x) + v2->Blue * x) / dx >> 8));
1274 hOldPen = SelectObject( hdc, hPen );
1275 pts[0].x = v1->x + x;
1277 pts[1].x = v1->x + x;
1279 Polyline( hdc, &pts[0], 2 );
1280 DeleteObject( SelectObject(hdc, hOldPen ) );
1284 case GRADIENT_FILL_RECT_V:
1285 for(i = 0; i < ngrad; i++)
1287 GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1288 TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1289 TRIVERTEX *v2 = vert_array + rect->LowerRight;
1290 int x1 = v1->x < v2->x ? v1->x : v2->x;
1291 int x2 = v2->x > v1->x ? v2->x : v1->x;
1300 for (y = 0; y < dy; y++)
1305 hPen = CreatePen( PS_SOLID, 1, RGB(
1306 (v1->Red * (dy - y) + v2->Red * y) / dy >> 8,
1307 (v1->Green * (dy - y) + v2->Green * y) / dy >> 8,
1308 (v1->Blue * (dy - y) + v2->Blue * y) / dy >> 8));
1309 hOldPen = SelectObject( hdc, hPen );
1311 pts[0].y = v1->y + y;
1313 pts[1].y = v1->y + y;
1314 Polyline( hdc, &pts[0], 2 );
1315 DeleteObject( SelectObject(hdc, hOldPen ) );
1319 case GRADIENT_FILL_TRIANGLE:
1320 for (i = 0; i < ngrad; i++)
1322 GRADIENT_TRIANGLE *tri = ((GRADIENT_TRIANGLE *)grad_array) + i;
1323 TRIVERTEX *v1 = vert_array + tri->Vertex1;
1324 TRIVERTEX *v2 = vert_array + tri->Vertex2;
1325 TRIVERTEX *v3 = vert_array + tri->Vertex3;
1329 { TRIVERTEX *t = v1; v1 = v2; v2 = t; }
1332 TRIVERTEX *t = v2; v2 = v3; v3 = t;
1334 { t = v1; v1 = v2; v2 = t; }
1336 /* v1->y <= v2->y <= v3->y */
1339 for (y = 0; y < dy; y++)
1341 /* v1->y <= y < v3->y */
1342 TRIVERTEX *v = y < (v2->y - v1->y) ? v1 : v3;
1343 /* (v->y <= y < v2->y) || (v2->y <= y < v->y) */
1344 int dy2 = v2->y - v->y;
1345 int y2 = y + v1->y - v->y;
1347 int x1 = (v3->x * y + v1->x * (dy - y )) / dy;
1348 int x2 = (v2->x * y2 + v-> x * (dy2 - y2)) / dy2;
1349 int r1 = (v3->Red * y + v1->Red * (dy - y )) / dy;
1350 int r2 = (v2->Red * y2 + v-> Red * (dy2 - y2)) / dy2;
1351 int g1 = (v3->Green * y + v1->Green * (dy - y )) / dy;
1352 int g2 = (v2->Green * y2 + v-> Green * (dy2 - y2)) / dy2;
1353 int b1 = (v3->Blue * y + v1->Blue * (dy - y )) / dy;
1354 int b2 = (v2->Blue * y2 + v-> Blue * (dy2 - y2)) / dy2;
1360 for (x = 0; x < dx; x++)
1361 SetPixel (hdc, x + x1, y + v1->y, RGB(
1362 (r1 * (dx - x) + r2 * x) / dx >> 8,
1363 (g1 * (dx - x) + g2 * x) / dx >> 8,
1364 (b1 * (dx - x) + b2 * x) / dx >> 8));
1369 for (x = 0; x < dx; x++)
1370 SetPixel (hdc, x + x2, y + v1->y, RGB(
1371 (r2 * (dx - x) + r1 * x) / dx >> 8,
1372 (g2 * (dx - x) + g1 * x) / dx >> 8,
1373 (b2 * (dx - x) + b1 * x) / dx >> 8));