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 CDECL 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 CDECL 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 CDECL 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 CDECL 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 CDECL 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 CDECL 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 CDECL 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 CDECL 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 CDECL 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 );
237 if(!dc) return FALSE;
240 if(PATH_IsPathOpen(dc->path))
241 ret = PATH_LineTo(dc, x, y);
244 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pLineTo );
245 ret = physdev->funcs->pLineTo( physdev, x, y );
251 release_dc_ptr( dc );
256 /***********************************************************************
259 BOOL WINAPI MoveToEx( HDC hdc, INT x, INT y, LPPOINT pt )
262 DC * dc = get_dc_ptr( hdc );
264 if(!dc) return FALSE;
267 pt->x = dc->CursPosX;
268 pt->y = dc->CursPosY;
273 if(PATH_IsPathOpen(dc->path)) ret = PATH_MoveTo(dc);
276 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pMoveTo );
277 ret = physdev->funcs->pMoveTo( physdev, x, y );
279 release_dc_ptr( dc );
284 /***********************************************************************
287 BOOL WINAPI Arc( HDC hdc, INT left, INT top, INT right,
288 INT bottom, INT xstart, INT ystart,
292 DC * dc = get_dc_ptr( hdc );
297 if(PATH_IsPathOpen(dc->path))
298 ret = PATH_Arc(dc, left, top, right, bottom, xstart, ystart, xend, yend,0);
301 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pArc );
302 ret = physdev->funcs->pArc( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
304 release_dc_ptr( dc );
309 /***********************************************************************
312 BOOL WINAPI ArcTo( HDC hdc,
314 INT right, INT bottom,
315 INT xstart, INT ystart,
318 double width = fabs(right-left),
319 height = fabs(bottom-top),
322 xcenter = right > left ? left+xradius : right+xradius,
323 ycenter = bottom > top ? top+yradius : bottom+yradius,
326 DC * dc = get_dc_ptr( hdc );
327 if(!dc) return FALSE;
330 if(PATH_IsPathOpen(dc->path))
331 result = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,-1);
334 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pArcTo );
335 result = physdev->funcs->pArcTo( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
338 angle = atan2(((yend-ycenter)/height),
339 ((xend-xcenter)/width));
340 dc->CursPosX = GDI_ROUND(xcenter+(cos(angle)*xradius));
341 dc->CursPosY = GDI_ROUND(ycenter+(sin(angle)*yradius));
343 release_dc_ptr( dc );
348 /***********************************************************************
351 BOOL WINAPI Pie( HDC hdc, INT left, INT top,
352 INT right, INT bottom, INT xstart, INT ystart,
356 DC * dc = get_dc_ptr( hdc );
357 if (!dc) return FALSE;
360 if(PATH_IsPathOpen(dc->path))
361 ret = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,2);
364 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPie );
365 ret = physdev->funcs->pPie( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
367 release_dc_ptr( dc );
372 /***********************************************************************
375 BOOL WINAPI Chord( HDC hdc, INT left, INT top,
376 INT right, INT bottom, INT xstart, INT ystart,
380 DC * dc = get_dc_ptr( hdc );
381 if (!dc) return FALSE;
384 if(PATH_IsPathOpen(dc->path))
385 ret = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,1);
388 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pChord );
389 ret = physdev->funcs->pChord( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
391 release_dc_ptr( dc );
396 /***********************************************************************
399 BOOL WINAPI Ellipse( HDC hdc, INT left, INT top,
400 INT right, INT bottom )
403 DC * dc = get_dc_ptr( hdc );
404 if (!dc) return FALSE;
407 if(PATH_IsPathOpen(dc->path))
408 ret = PATH_Ellipse(dc,left,top,right,bottom);
411 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pEllipse );
412 ret = physdev->funcs->pEllipse( physdev, left, top, right, bottom );
415 release_dc_ptr( dc );
420 /***********************************************************************
421 * Rectangle (GDI32.@)
423 BOOL WINAPI Rectangle( HDC hdc, INT left, INT top,
424 INT right, INT bottom )
427 DC * dc = get_dc_ptr( hdc );
432 if(PATH_IsPathOpen(dc->path))
433 ret = PATH_Rectangle(dc, left, top, right, bottom);
436 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRectangle );
437 ret = physdev->funcs->pRectangle( physdev, left, top, right, bottom );
439 release_dc_ptr( dc );
445 /***********************************************************************
446 * RoundRect (GDI32.@)
448 BOOL WINAPI RoundRect( HDC hdc, INT left, INT top, INT right,
449 INT bottom, INT ell_width, INT ell_height )
452 DC *dc = get_dc_ptr( hdc );
457 if(PATH_IsPathOpen(dc->path))
458 ret = PATH_RoundRect(dc,left,top,right,bottom,ell_width,ell_height);
461 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRoundRect );
462 ret = physdev->funcs->pRoundRect( physdev, left, top, right, bottom, ell_width, ell_height );
464 release_dc_ptr( dc );
469 /***********************************************************************
472 COLORREF WINAPI SetPixel( HDC hdc, INT x, INT y, COLORREF color )
475 DC * dc = get_dc_ptr( hdc );
479 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetPixel );
481 ret = physdev->funcs->pSetPixel( physdev, x, y, color );
482 release_dc_ptr( dc );
487 /***********************************************************************
488 * SetPixelV (GDI32.@)
490 BOOL WINAPI SetPixelV( HDC hdc, INT x, INT y, COLORREF color )
493 DC * dc = get_dc_ptr( hdc );
497 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetPixel );
499 physdev->funcs->pSetPixel( physdev, x, y, color );
501 release_dc_ptr( dc );
506 /***********************************************************************
509 COLORREF WINAPI GetPixel( HDC hdc, INT x, INT y )
511 COLORREF ret = CLR_INVALID;
512 DC * dc = get_dc_ptr( hdc );
517 /* FIXME: should this be in the graphics driver? */
518 if (PtVisible( hdc, x, y ))
520 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetPixel );
521 ret = physdev->funcs->pGetPixel( physdev, x, y );
523 release_dc_ptr( dc );
529 /******************************************************************************
530 * ChoosePixelFormat [GDI32.@]
531 * Matches a pixel format to given format
534 * hdc [I] Device context to search for best pixel match
535 * ppfd [I] Pixel format for which a match is sought
538 * Success: Pixel format index closest to given format
541 INT WINAPI ChoosePixelFormat( HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd )
544 DC * dc = get_dc_ptr( hdc );
546 TRACE("(%p,%p)\n",hdc,ppfd);
550 if (!dc->funcs->pChoosePixelFormat) FIXME(" :stub\n");
551 else ret = dc->funcs->pChoosePixelFormat(dc->physDev,ppfd);
553 release_dc_ptr( dc );
558 /******************************************************************************
559 * SetPixelFormat [GDI32.@]
560 * Sets pixel format of device context
563 * hdc [I] Device context to search for best pixel match
564 * iPixelFormat [I] Pixel format index
565 * ppfd [I] Pixel format for which a match is sought
571 BOOL WINAPI SetPixelFormat( HDC hdc, INT iPixelFormat,
572 const PIXELFORMATDESCRIPTOR *ppfd)
575 DC * dc = get_dc_ptr( hdc );
577 TRACE("(%p,%d,%p)\n",hdc,iPixelFormat,ppfd);
582 if (!dc->funcs->pSetPixelFormat) FIXME(" :stub\n");
583 else bRet = dc->funcs->pSetPixelFormat(dc->physDev,iPixelFormat,ppfd);
585 release_dc_ptr( dc );
590 /******************************************************************************
591 * GetPixelFormat [GDI32.@]
592 * Gets index of pixel format of DC
595 * hdc [I] Device context whose pixel format index is sought
598 * Success: Currently selected pixel format
601 INT WINAPI GetPixelFormat( HDC hdc )
604 DC * dc = get_dc_ptr( hdc );
611 if (!dc->funcs->pGetPixelFormat) FIXME(" :stub\n");
612 else ret = dc->funcs->pGetPixelFormat(dc->physDev);
614 release_dc_ptr( dc );
619 /******************************************************************************
620 * DescribePixelFormat [GDI32.@]
621 * Gets info about pixel format from DC
624 * hdc [I] Device context
625 * iPixelFormat [I] Pixel format selector
626 * nBytes [I] Size of buffer
627 * ppfd [O] Pointer to structure to receive pixel format data
630 * Success: Maximum pixel format index of the device context
633 INT WINAPI DescribePixelFormat( HDC hdc, INT iPixelFormat, UINT nBytes,
634 LPPIXELFORMATDESCRIPTOR ppfd )
637 DC * dc = get_dc_ptr( hdc );
639 TRACE("(%p,%d,%d,%p): stub\n",hdc,iPixelFormat,nBytes,ppfd);
644 if (!dc->funcs->pDescribePixelFormat)
647 ppfd->nSize = nBytes;
651 else ret = dc->funcs->pDescribePixelFormat(dc->physDev,iPixelFormat,nBytes,ppfd);
653 release_dc_ptr( dc );
658 /******************************************************************************
659 * SwapBuffers [GDI32.@]
660 * Exchanges front and back buffers of window
663 * hdc [I] Device context whose buffers get swapped
669 BOOL WINAPI SwapBuffers( HDC hdc )
672 DC * dc = get_dc_ptr( hdc );
676 if (!dc) return TRUE;
679 if (!dc->funcs->pSwapBuffers)
684 else bRet = dc->funcs->pSwapBuffers(dc->physDev);
686 release_dc_ptr( dc );
691 /***********************************************************************
694 BOOL WINAPI PaintRgn( HDC hdc, HRGN hrgn )
697 DC * dc = get_dc_ptr( hdc );
701 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPaintRgn );
703 ret = physdev->funcs->pPaintRgn( physdev, hrgn );
704 release_dc_ptr( dc );
710 /***********************************************************************
713 BOOL WINAPI FillRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush )
716 DC * dc = get_dc_ptr( hdc );
720 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pFillRgn );
722 retval = physdev->funcs->pFillRgn( physdev, hrgn, hbrush );
723 release_dc_ptr( dc );
729 /***********************************************************************
732 BOOL WINAPI FrameRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush,
733 INT nWidth, INT nHeight )
736 DC *dc = get_dc_ptr( hdc );
740 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pFrameRgn );
742 ret = physdev->funcs->pFrameRgn( physdev, hrgn, hbrush, nWidth, nHeight );
743 release_dc_ptr( dc );
749 /***********************************************************************
750 * InvertRgn (GDI32.@)
752 BOOL WINAPI InvertRgn( HDC hdc, HRGN hrgn )
755 DC *dc = get_dc_ptr( hdc );
759 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pInvertRgn );
761 ret = physdev->funcs->pInvertRgn( physdev, hrgn );
762 release_dc_ptr( dc );
768 /**********************************************************************
771 BOOL WINAPI Polyline( HDC hdc, const POINT* pt, INT count )
774 DC * dc = get_dc_ptr( hdc );
779 if (PATH_IsPathOpen(dc->path)) ret = PATH_Polyline(dc, pt, count);
782 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPolyline );
783 ret = physdev->funcs->pPolyline( physdev, pt, count );
785 release_dc_ptr( dc );
790 /**********************************************************************
791 * PolylineTo (GDI32.@)
793 BOOL WINAPI PolylineTo( HDC hdc, const POINT* pt, DWORD cCount )
795 DC * dc = get_dc_ptr( hdc );
798 if(!dc) return FALSE;
801 if(PATH_IsPathOpen(dc->path)) ret = PATH_PolylineTo(dc, pt, cCount);
804 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPolylineTo );
805 ret = physdev->funcs->pPolylineTo( physdev, pt, cCount );
809 dc->CursPosX = pt[cCount-1].x;
810 dc->CursPosY = pt[cCount-1].y;
812 release_dc_ptr( dc );
817 /**********************************************************************
820 BOOL WINAPI Polygon( HDC hdc, const POINT* pt, INT count )
823 DC * dc = get_dc_ptr( hdc );
828 if (PATH_IsPathOpen(dc->path)) ret = PATH_Polygon(dc, pt, count);
831 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPolygon );
832 ret = physdev->funcs->pPolygon( physdev, pt, count );
834 release_dc_ptr( dc );
840 /**********************************************************************
841 * PolyPolygon (GDI32.@)
843 BOOL WINAPI PolyPolygon( HDC hdc, const POINT* pt, const INT* counts,
847 DC * dc = get_dc_ptr( hdc );
852 if (PATH_IsPathOpen(dc->path)) ret = PATH_PolyPolygon(dc, pt, counts, polygons);
855 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPolyPolygon );
856 ret = physdev->funcs->pPolyPolygon( physdev, pt, counts, polygons );
858 release_dc_ptr( dc );
863 /**********************************************************************
864 * PolyPolyline (GDI32.@)
866 BOOL WINAPI PolyPolyline( HDC hdc, const POINT* pt, const DWORD* counts,
870 DC * dc = get_dc_ptr( hdc );
875 if (PATH_IsPathOpen(dc->path)) ret = PATH_PolyPolyline(dc, pt, counts, polylines);
878 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPolyPolyline );
879 ret = physdev->funcs->pPolyPolyline( physdev, pt, counts, polylines );
881 release_dc_ptr( dc );
886 /**********************************************************************
887 * ExtFloodFill (GDI32.@)
889 BOOL WINAPI ExtFloodFill( HDC hdc, INT x, INT y, COLORREF color,
893 DC * dc = get_dc_ptr( hdc );
897 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pExtFloodFill );
900 ret = physdev->funcs->pExtFloodFill( physdev, x, y, color, fillType );
901 release_dc_ptr( dc );
907 /**********************************************************************
908 * FloodFill (GDI32.@)
910 BOOL WINAPI FloodFill( HDC hdc, INT x, INT y, COLORREF color )
912 return ExtFloodFill( hdc, x, y, color, FLOODFILLBORDER );
916 /******************************************************************************
917 * PolyBezier [GDI32.@]
918 * Draws one or more Bezier curves
921 * hDc [I] Handle to device context
922 * lppt [I] Pointer to endpoints and control points
923 * cPoints [I] Count of endpoints and control points
929 BOOL WINAPI PolyBezier( HDC hdc, const POINT* lppt, DWORD cPoints )
934 /* cPoints must be 3 * n + 1 (where n>=1) */
935 if (cPoints == 1 || (cPoints % 3) != 1) return FALSE;
937 dc = get_dc_ptr( hdc );
938 if(!dc) return FALSE;
941 if(PATH_IsPathOpen(dc->path)) ret = PATH_PolyBezier(dc, lppt, cPoints);
944 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPolyBezier );
945 ret = physdev->funcs->pPolyBezier( physdev, lppt, cPoints );
947 release_dc_ptr( dc );
951 /******************************************************************************
952 * PolyBezierTo [GDI32.@]
953 * Draws one or more Bezier curves
956 * hDc [I] Handle to device context
957 * lppt [I] Pointer to endpoints and control points
958 * cPoints [I] Count of endpoints and control points
964 BOOL WINAPI PolyBezierTo( HDC hdc, const POINT* lppt, DWORD cPoints )
969 /* cbPoints must be 3 * n (where n>=1) */
970 if (!cPoints || (cPoints % 3) != 0) return FALSE;
972 dc = get_dc_ptr( hdc );
973 if(!dc) return FALSE;
976 if(PATH_IsPathOpen(dc->path)) ret = PATH_PolyBezierTo(dc, lppt, cPoints);
979 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPolyBezierTo );
980 ret = physdev->funcs->pPolyBezierTo( physdev, lppt, cPoints );
983 dc->CursPosX = lppt[cPoints-1].x;
984 dc->CursPosY = lppt[cPoints-1].y;
986 release_dc_ptr( dc );
990 /***********************************************************************
993 BOOL WINAPI AngleArc(HDC hdc, INT x, INT y, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle)
995 INT x1,y1,x2,y2, arcdir;
999 if( (signed int)dwRadius < 0 )
1002 dc = get_dc_ptr( hdc );
1003 if(!dc) return FALSE;
1005 /* Calculate the end point */
1006 x2 = GDI_ROUND( x + cos((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius );
1007 y2 = GDI_ROUND( y - sin((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius );
1010 if(!PATH_IsPathOpen(dc->path))
1012 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pAngleArc );
1013 result = physdev->funcs->pAngleArc( physdev, x, y, dwRadius, eStartAngle, eSweepAngle );
1015 else { /* do it using ArcTo */
1016 x1 = GDI_ROUND( x + cos(eStartAngle*M_PI/180) * dwRadius );
1017 y1 = GDI_ROUND( y - sin(eStartAngle*M_PI/180) * dwRadius );
1019 arcdir = SetArcDirection( hdc, eSweepAngle >= 0 ? AD_COUNTERCLOCKWISE : AD_CLOCKWISE);
1020 result = ArcTo( hdc, x-dwRadius, y-dwRadius, x+dwRadius, y+dwRadius,
1022 SetArcDirection( hdc, arcdir );
1028 release_dc_ptr( dc );
1032 /***********************************************************************
1033 * PolyDraw (GDI32.@)
1035 BOOL WINAPI PolyDraw(HDC hdc, const POINT *lppt, const BYTE *lpbTypes,
1038 DC *dc = get_dc_ptr( hdc );
1039 BOOL result = FALSE;
1041 if(!dc) return FALSE;
1044 if( PATH_IsPathOpen( dc->path ) ) result = PATH_PolyDraw(dc, lppt, lpbTypes, cCount);
1047 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pPolyDraw );
1048 result = physdev->funcs->pPolyDraw( physdev, lppt, lpbTypes, cCount );
1050 release_dc_ptr( dc );
1055 /**********************************************************************
1058 BOOL WINAPI LineDDA(INT nXStart, INT nYStart, INT nXEnd, INT nYEnd,
1059 LINEDDAPROC callback, LPARAM lParam )
1061 INT xadd = 1, yadd = 1;
1064 INT dx = nXEnd - nXStart;
1065 INT dy = nYEnd - nYStart;
1077 if (dx > dy) /* line is "more horizontal" */
1079 err = 2*dy - dx; erradd = 2*dy - 2*dx;
1080 for(cnt = 0;cnt < dx; cnt++)
1082 callback(nXStart,nYStart,lParam);
1092 else /* line is "more vertical" */
1094 err = 2*dx - dy; erradd = 2*dx - 2*dy;
1095 for(cnt = 0;cnt < dy; cnt++)
1097 callback(nXStart,nYStart,lParam);
1111 /******************************************************************
1113 * *Very* simple bezier drawing code,
1115 * It uses a recursive algorithm to divide the curve in a series
1116 * of straight line segments. Not ideal but sufficient for me.
1117 * If you are in need for something better look for some incremental
1120 * 7 July 1998 Rein Klazes
1124 * some macro definitions for bezier drawing
1126 * to avoid truncation errors the coordinates are
1127 * shifted upwards. When used in drawing they are
1128 * shifted down again, including correct rounding
1129 * and avoiding floating point arithmetic
1130 * 4 bits should allow 27 bits coordinates which I saw
1131 * somewhere in the win32 doc's
1135 #define BEZIERSHIFTBITS 4
1136 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
1137 #define BEZIERPIXEL BEZIERSHIFTUP(1)
1138 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
1139 /* maximum depth of recursion */
1140 #define BEZIERMAXDEPTH 8
1142 /* size of array to store points on */
1143 /* enough for one curve */
1144 #define BEZIER_INITBUFSIZE (150)
1146 /* calculate Bezier average, in this case the middle
1147 * correctly rounded...
1150 #define BEZIERMIDDLE(Mid, P1, P2) \
1151 (Mid).x=((P1).x+(P2).x + 1)/2;\
1152 (Mid).y=((P1).y+(P2).y + 1)/2;
1154 /**********************************************************
1155 * BezierCheck helper function to check
1156 * that recursion can be terminated
1157 * Points[0] and Points[3] are begin and endpoint
1158 * Points[1] and Points[2] are control points
1159 * level is the recursion depth
1160 * returns true if the recursion can be terminated
1162 static BOOL BezierCheck( int level, POINT *Points)
1165 dx=Points[3].x-Points[0].x;
1166 dy=Points[3].y-Points[0].y;
1167 if(abs(dy)<=abs(dx)){/* shallow line */
1168 /* check that control points are between begin and end */
1169 if(Points[1].x < Points[0].x){
1170 if(Points[1].x < Points[3].x)
1173 if(Points[1].x > Points[3].x)
1175 if(Points[2].x < Points[0].x){
1176 if(Points[2].x < Points[3].x)
1179 if(Points[2].x > Points[3].x)
1181 dx=BEZIERSHIFTDOWN(dx);
1182 if(!dx) return TRUE;
1183 if(abs(Points[1].y-Points[0].y-(dy/dx)*
1184 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
1185 abs(Points[2].y-Points[0].y-(dy/dx)*
1186 BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
1190 }else{ /* steep line */
1191 /* check that control points are between begin and end */
1192 if(Points[1].y < Points[0].y){
1193 if(Points[1].y < Points[3].y)
1196 if(Points[1].y > Points[3].y)
1198 if(Points[2].y < Points[0].y){
1199 if(Points[2].y < Points[3].y)
1202 if(Points[2].y > Points[3].y)
1204 dy=BEZIERSHIFTDOWN(dy);
1205 if(!dy) return TRUE;
1206 if(abs(Points[1].x-Points[0].x-(dx/dy)*
1207 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
1208 abs(Points[2].x-Points[0].x-(dx/dy)*
1209 BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
1216 /* Helper for GDI_Bezier.
1217 * Just handles one Bezier, so Points should point to four POINTs
1219 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
1220 INT *nPtsOut, INT level )
1222 if(*nPtsOut == *dwOut) {
1224 *PtsOut = HeapReAlloc( GetProcessHeap(), 0, *PtsOut,
1225 *dwOut * sizeof(POINT) );
1228 if(!level || BezierCheck(level, Points)) {
1230 (*PtsOut)[0].x = BEZIERSHIFTDOWN(Points[0].x);
1231 (*PtsOut)[0].y = BEZIERSHIFTDOWN(Points[0].y);
1234 (*PtsOut)[*nPtsOut].x = BEZIERSHIFTDOWN(Points[3].x);
1235 (*PtsOut)[*nPtsOut].y = BEZIERSHIFTDOWN(Points[3].y);
1238 POINT Points2[4]; /* for the second recursive call */
1239 Points2[3]=Points[3];
1240 BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
1241 BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
1242 BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
1244 BEZIERMIDDLE(Points[1], Points[0], Points[1]);
1245 BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1246 BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1248 Points2[0]=Points[3];
1250 /* do the two halves */
1251 GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
1252 GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
1258 /***********************************************************************
1259 * GDI_Bezier [INTERNAL]
1260 * Calculate line segments that approximate -what microsoft calls- a bezier
1262 * The routine recursively divides the curve in two parts until a straight
1267 * Points [I] Ptr to count POINTs which are the end and control points
1268 * of the set of Bezier curves to flatten.
1269 * count [I] Number of Points. Must be 3n+1.
1270 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
1275 * Ptr to an array of POINTs that contain the lines that approximate the
1276 * Beziers. The array is allocated on the process heap and it is the caller's
1277 * responsibility to HeapFree it. [this is not a particularly nice interface
1278 * but since we can't know in advance how many points we will generate, the
1279 * alternative would be to call the function twice, once to determine the size
1280 * and a second time to do the work - I decided this was too much of a pain].
1282 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
1285 INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
1287 if (count == 1 || (count - 1) % 3 != 0) {
1288 ERR("Invalid no. of points %d\n", count);
1292 out = HeapAlloc( GetProcessHeap(), 0, dwOut * sizeof(POINT));
1293 for(Bezier = 0; Bezier < (count-1)/3; Bezier++) {
1295 memcpy(ptBuf, Points + Bezier * 3, sizeof(POINT) * 4);
1296 for(i = 0; i < 4; i++) {
1297 ptBuf[i].x = BEZIERSHIFTUP(ptBuf[i].x);
1298 ptBuf[i].y = BEZIERSHIFTUP(ptBuf[i].y);
1300 GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
1302 TRACE("Produced %d points\n", *nPtsOut);
1306 /******************************************************************************
1307 * GdiGradientFill (GDI32.@)
1309 * FIXME: we don't support the Alpha channel properly
1311 BOOL WINAPI GdiGradientFill( HDC hdc, TRIVERTEX *vert_array, ULONG nvert,
1312 void * grad_array, ULONG ngrad, ULONG mode )
1316 TRACE("vert_array:%p nvert:%d grad_array:%p ngrad:%d\n",
1317 vert_array, nvert, grad_array, ngrad);
1321 case GRADIENT_FILL_RECT_H:
1322 for(i = 0; i < ngrad; i++)
1324 GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1325 TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1326 TRIVERTEX *v2 = vert_array + rect->LowerRight;
1327 int y1 = v1->y < v2->y ? v1->y : v2->y;
1328 int y2 = v2->y > v1->y ? v2->y : v1->y;
1337 for (x = 0; x < dx; x++)
1342 hPen = CreatePen( PS_SOLID, 1, RGB(
1343 (v1->Red * (dx - x) + v2->Red * x) / dx >> 8,
1344 (v1->Green * (dx - x) + v2->Green * x) / dx >> 8,
1345 (v1->Blue * (dx - x) + v2->Blue * x) / dx >> 8));
1346 hOldPen = SelectObject( hdc, hPen );
1347 pts[0].x = v1->x + x;
1349 pts[1].x = v1->x + x;
1351 Polyline( hdc, &pts[0], 2 );
1352 DeleteObject( SelectObject(hdc, hOldPen ) );
1356 case GRADIENT_FILL_RECT_V:
1357 for(i = 0; i < ngrad; i++)
1359 GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1360 TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1361 TRIVERTEX *v2 = vert_array + rect->LowerRight;
1362 int x1 = v1->x < v2->x ? v1->x : v2->x;
1363 int x2 = v2->x > v1->x ? v2->x : v1->x;
1372 for (y = 0; y < dy; y++)
1377 hPen = CreatePen( PS_SOLID, 1, RGB(
1378 (v1->Red * (dy - y) + v2->Red * y) / dy >> 8,
1379 (v1->Green * (dy - y) + v2->Green * y) / dy >> 8,
1380 (v1->Blue * (dy - y) + v2->Blue * y) / dy >> 8));
1381 hOldPen = SelectObject( hdc, hPen );
1383 pts[0].y = v1->y + y;
1385 pts[1].y = v1->y + y;
1386 Polyline( hdc, &pts[0], 2 );
1387 DeleteObject( SelectObject(hdc, hOldPen ) );
1391 case GRADIENT_FILL_TRIANGLE:
1392 for (i = 0; i < ngrad; i++)
1394 GRADIENT_TRIANGLE *tri = ((GRADIENT_TRIANGLE *)grad_array) + i;
1395 TRIVERTEX *v1 = vert_array + tri->Vertex1;
1396 TRIVERTEX *v2 = vert_array + tri->Vertex2;
1397 TRIVERTEX *v3 = vert_array + tri->Vertex3;
1401 { TRIVERTEX *t = v1; v1 = v2; v2 = t; }
1404 TRIVERTEX *t = v2; v2 = v3; v3 = t;
1406 { t = v1; v1 = v2; v2 = t; }
1408 /* v1->y <= v2->y <= v3->y */
1411 for (y = 0; y < dy; y++)
1413 /* v1->y <= y < v3->y */
1414 TRIVERTEX *v = y < (v2->y - v1->y) ? v1 : v3;
1415 /* (v->y <= y < v2->y) || (v2->y <= y < v->y) */
1416 int dy2 = v2->y - v->y;
1417 int y2 = y + v1->y - v->y;
1419 int x1 = (v3->x * y + v1->x * (dy - y )) / dy;
1420 int x2 = (v2->x * y2 + v-> x * (dy2 - y2)) / dy2;
1421 int r1 = (v3->Red * y + v1->Red * (dy - y )) / dy;
1422 int r2 = (v2->Red * y2 + v-> Red * (dy2 - y2)) / dy2;
1423 int g1 = (v3->Green * y + v1->Green * (dy - y )) / dy;
1424 int g2 = (v2->Green * y2 + v-> Green * (dy2 - y2)) / dy2;
1425 int b1 = (v3->Blue * y + v1->Blue * (dy - y )) / dy;
1426 int b2 = (v2->Blue * y2 + v-> Blue * (dy2 - y2)) / dy2;
1432 for (x = 0; x < dx; x++)
1433 SetPixel (hdc, x + x1, y + v1->y, RGB(
1434 (r1 * (dx - x) + r2 * x) / dx >> 8,
1435 (g1 * (dx - x) + g2 * x) / dx >> 8,
1436 (b1 * (dx - x) + b2 * x) / dx >> 8));
1441 for (x = 0; x < dx; x++)
1442 SetPixel (hdc, x + x2, y + v1->y, RGB(
1443 (r2 * (dx - x) + r1 * x) / dx >> 8,
1444 (g2 * (dx - x) + g1 * x) / dx >> 8,
1445 (b2 * (dx - x) + b1 * x) / dx >> 8));
1457 /******************************************************************************
1458 * GdiDrawStream (GDI32.@)
1461 BOOL WINAPI GdiDrawStream( HDC hdc, ULONG in, void * pvin )
1463 FIXME("stub: %p, %d, %p\n", hdc, in, pvin);