Moved a bunch of definitions from gdi.h into a new gdi_private.h to
[wine/multimedia.git] / graphics / painting.c
blob5ee1a1ca246c38b145f2655087e86a424e992e8f
1 /*
2 * GDI drawing functions.
4 * Copyright 1993, 1994 Alexandre Julliard
5 * Copyright 1997 Bertho A. Stultiens
6 * 1999 Huw D M Davies
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winerror.h"
34 #include "gdi.h"
35 #include "bitmap.h"
36 #include "gdi_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
42 /***********************************************************************
43 * LineTo (GDI32.@)
45 BOOL WINAPI LineTo( HDC hdc, INT x, INT y )
47 DC * dc = DC_GetDCUpdate( hdc );
48 BOOL ret;
50 if(!dc) return FALSE;
52 if(PATH_IsPathOpen(dc->path))
53 ret = PATH_LineTo(dc, x, y);
54 else
55 ret = dc->funcs->pLineTo && dc->funcs->pLineTo(dc->physDev,x,y);
56 if(ret) {
57 dc->CursPosX = x;
58 dc->CursPosY = y;
60 GDI_ReleaseObj( hdc );
61 return ret;
65 /***********************************************************************
66 * MoveToEx (GDI32.@)
68 BOOL WINAPI MoveToEx( HDC hdc, INT x, INT y, LPPOINT pt )
70 BOOL ret = TRUE;
71 DC * dc = DC_GetDCPtr( hdc );
73 if(!dc) return FALSE;
75 if(pt) {
76 pt->x = dc->CursPosX;
77 pt->y = dc->CursPosY;
79 dc->CursPosX = x;
80 dc->CursPosY = y;
82 if(PATH_IsPathOpen(dc->path)) ret = PATH_MoveTo(dc);
83 else if (dc->funcs->pMoveTo) ret = dc->funcs->pMoveTo(dc->physDev,x,y);
84 GDI_ReleaseObj( hdc );
85 return ret;
89 /***********************************************************************
90 * Arc (GDI32.@)
92 BOOL WINAPI Arc( HDC hdc, INT left, INT top, INT right,
93 INT bottom, INT xstart, INT ystart,
94 INT xend, INT yend )
96 BOOL ret = FALSE;
97 DC * dc = DC_GetDCUpdate( hdc );
98 if (dc)
100 if(PATH_IsPathOpen(dc->path))
101 ret = PATH_Arc(dc, left, top, right, bottom, xstart, ystart, xend, yend,0);
102 else if (dc->funcs->pArc)
103 ret = dc->funcs->pArc(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
104 GDI_ReleaseObj( hdc );
106 return ret;
109 /***********************************************************************
110 * ArcTo (GDI32.@)
112 BOOL WINAPI ArcTo( HDC hdc,
113 INT left, INT top,
114 INT right, INT bottom,
115 INT xstart, INT ystart,
116 INT xend, INT yend )
118 BOOL result;
119 DC * dc = DC_GetDCUpdate( hdc );
120 if(!dc) return FALSE;
122 if(dc->funcs->pArcTo)
124 result = dc->funcs->pArcTo( dc->physDev, left, top, right, bottom,
125 xstart, ystart, xend, yend );
126 GDI_ReleaseObj( hdc );
127 return result;
129 GDI_ReleaseObj( hdc );
131 * Else emulate it.
132 * According to the documentation, a line is drawn from the current
133 * position to the starting point of the arc.
135 LineTo(hdc, xstart, ystart);
137 * Then the arc is drawn.
139 result = Arc(hdc, left, top, right, bottom, xstart, ystart, xend, yend);
141 * If no error occurred, the current position is moved to the ending
142 * point of the arc.
144 if (result) MoveToEx(hdc, xend, yend, NULL);
145 return result;
149 /***********************************************************************
150 * Pie (GDI32.@)
152 BOOL WINAPI Pie( HDC hdc, INT left, INT top,
153 INT right, INT bottom, INT xstart, INT ystart,
154 INT xend, INT yend )
156 BOOL ret = FALSE;
157 DC * dc = DC_GetDCUpdate( hdc );
158 if (!dc) return FALSE;
160 if(PATH_IsPathOpen(dc->path))
161 ret = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,2);
162 else if(dc->funcs->pPie)
163 ret = dc->funcs->pPie(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
165 GDI_ReleaseObj( hdc );
166 return ret;
170 /***********************************************************************
171 * Chord (GDI32.@)
173 BOOL WINAPI Chord( HDC hdc, INT left, INT top,
174 INT right, INT bottom, INT xstart, INT ystart,
175 INT xend, INT yend )
177 BOOL ret = FALSE;
178 DC * dc = DC_GetDCUpdate( hdc );
179 if (!dc) return FALSE;
181 if(PATH_IsPathOpen(dc->path))
182 ret = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,1);
183 else if(dc->funcs->pChord)
184 ret = dc->funcs->pChord(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
186 GDI_ReleaseObj( hdc );
187 return ret;
191 /***********************************************************************
192 * Ellipse (GDI32.@)
194 BOOL WINAPI Ellipse( HDC hdc, INT left, INT top,
195 INT right, INT bottom )
197 BOOL ret = FALSE;
198 DC * dc = DC_GetDCUpdate( hdc );
199 if (!dc) return FALSE;
201 if(PATH_IsPathOpen(dc->path))
202 ret = PATH_Ellipse(dc,left,top,right,bottom);
203 else if (dc->funcs->pEllipse)
204 ret = dc->funcs->pEllipse(dc->physDev,left,top,right,bottom);
206 GDI_ReleaseObj( hdc );
207 return ret;
211 /***********************************************************************
212 * Rectangle (GDI32.@)
214 BOOL WINAPI Rectangle( HDC hdc, INT left, INT top,
215 INT right, INT bottom )
217 BOOL ret = FALSE;
218 DC * dc = DC_GetDCUpdate( hdc );
219 if (dc)
221 if(PATH_IsPathOpen(dc->path))
222 ret = PATH_Rectangle(dc, left, top, right, bottom);
223 else if (dc->funcs->pRectangle)
224 ret = dc->funcs->pRectangle(dc->physDev,left,top,right,bottom);
225 GDI_ReleaseObj( hdc );
227 return ret;
231 /***********************************************************************
232 * RoundRect (GDI32.@)
234 BOOL WINAPI RoundRect( HDC hdc, INT left, INT top, INT right,
235 INT bottom, INT ell_width, INT ell_height )
237 BOOL ret = FALSE;
238 DC *dc = DC_GetDCUpdate( hdc );
240 if (dc)
242 if(PATH_IsPathOpen(dc->path))
243 ret = PATH_RoundRect(dc,left,top,right,bottom,ell_width,ell_height);
244 else if (dc->funcs->pRoundRect)
245 ret = dc->funcs->pRoundRect(dc->physDev,left,top,right,bottom,ell_width,ell_height);
246 GDI_ReleaseObj( hdc );
248 return ret;
251 /***********************************************************************
252 * SetPixel (GDI32.@)
254 COLORREF WINAPI SetPixel( HDC hdc, INT x, INT y, COLORREF color )
256 COLORREF ret = 0;
257 DC * dc = DC_GetDCUpdate( hdc );
258 if (dc)
260 if (dc->funcs->pSetPixel) ret = dc->funcs->pSetPixel(dc->physDev,x,y,color);
261 GDI_ReleaseObj( hdc );
263 return ret;
266 /***********************************************************************
267 * SetPixelV (GDI32.@)
269 BOOL WINAPI SetPixelV( HDC hdc, INT x, INT y, COLORREF color )
271 BOOL ret = FALSE;
272 DC * dc = DC_GetDCUpdate( hdc );
273 if (dc)
275 if (dc->funcs->pSetPixel)
277 dc->funcs->pSetPixel(dc->physDev,x,y,color);
278 ret = TRUE;
280 GDI_ReleaseObj( hdc );
282 return ret;
285 /***********************************************************************
286 * GetPixel (GDI32.@)
288 COLORREF WINAPI GetPixel( HDC hdc, INT x, INT y )
290 COLORREF ret = CLR_INVALID;
291 DC * dc = DC_GetDCUpdate( hdc );
293 if (dc)
295 /* FIXME: should this be in the graphics driver? */
296 if (PtVisible( hdc, x, y ))
298 if (dc->funcs->pGetPixel) ret = dc->funcs->pGetPixel(dc->physDev,x,y);
300 GDI_ReleaseObj( hdc );
302 return ret;
306 /******************************************************************************
307 * ChoosePixelFormat [GDI32.@]
308 * Matches a pixel format to given format
310 * PARAMS
311 * hdc [I] Device context to search for best pixel match
312 * ppfd [I] Pixel format for which a match is sought
314 * RETURNS
315 * Success: Pixel format index closest to given format
316 * Failure: 0
318 INT WINAPI ChoosePixelFormat( HDC hdc, const LPPIXELFORMATDESCRIPTOR ppfd )
320 INT ret = 0;
321 DC * dc = DC_GetDCPtr( hdc );
323 TRACE("(%p,%p)\n",hdc,ppfd);
325 if (!dc) return 0;
327 if (!dc->funcs->pChoosePixelFormat) FIXME(" :stub\n");
328 else ret = dc->funcs->pChoosePixelFormat(dc->physDev,ppfd);
330 GDI_ReleaseObj( hdc );
331 return ret;
335 /******************************************************************************
336 * SetPixelFormat [GDI32.@]
337 * Sets pixel format of device context
339 * PARAMS
340 * hdc [I] Device context to search for best pixel match
341 * iPixelFormat [I] Pixel format index
342 * ppfd [I] Pixel format for which a match is sought
344 * RETURNS STD
346 BOOL WINAPI SetPixelFormat( HDC hdc, INT iPixelFormat,
347 const PIXELFORMATDESCRIPTOR *ppfd)
349 INT bRet = FALSE;
350 DC * dc = DC_GetDCPtr( hdc );
352 TRACE("(%p,%d,%p)\n",hdc,iPixelFormat,ppfd);
354 if (!dc) return 0;
356 if (!dc->funcs->pSetPixelFormat) FIXME(" :stub\n");
357 else bRet = dc->funcs->pSetPixelFormat(dc->physDev,iPixelFormat,ppfd);
359 GDI_ReleaseObj( hdc );
360 return bRet;
364 /******************************************************************************
365 * GetPixelFormat [GDI32.@]
366 * Gets index of pixel format of DC
368 * PARAMETERS
369 * hdc [I] Device context whose pixel format index is sought
371 * RETURNS
372 * Success: Currently selected pixel format
373 * Failure: 0
375 INT WINAPI GetPixelFormat( HDC hdc )
377 INT ret = 0;
378 DC * dc = DC_GetDCPtr( hdc );
380 TRACE("(%p)\n",hdc);
382 if (!dc) return 0;
384 if (!dc->funcs->pGetPixelFormat) FIXME(" :stub\n");
385 else ret = dc->funcs->pGetPixelFormat(dc->physDev);
387 GDI_ReleaseObj( hdc );
388 return ret;
392 /******************************************************************************
393 * DescribePixelFormat [GDI32.@]
394 * Gets info about pixel format from DC
396 * PARAMS
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
402 * RETURNS
403 * Success: Maximum pixel format index of the device context
404 * Failure: 0
406 INT WINAPI DescribePixelFormat( HDC hdc, INT iPixelFormat, UINT nBytes,
407 LPPIXELFORMATDESCRIPTOR ppfd )
409 INT ret = 0;
410 DC * dc = DC_GetDCPtr( hdc );
412 TRACE("(%p,%d,%d,%p): stub\n",hdc,iPixelFormat,nBytes,ppfd);
414 if (!dc) return 0;
416 if (!dc->funcs->pDescribePixelFormat)
418 FIXME(" :stub\n");
419 ppfd->nSize = nBytes;
420 ppfd->nVersion = 1;
421 ret = 3;
423 else ret = dc->funcs->pDescribePixelFormat(dc->physDev,iPixelFormat,nBytes,ppfd);
425 GDI_ReleaseObj( hdc );
426 return ret;
430 /******************************************************************************
431 * SwapBuffers [GDI32.@]
432 * Exchanges front and back buffers of window
434 * PARAMS
435 * hdc [I] Device context whose buffers get swapped
437 * RETURNS STD
439 BOOL WINAPI SwapBuffers( HDC hdc )
441 INT bRet = FALSE;
442 DC * dc = DC_GetDCPtr( hdc );
444 TRACE("(%p)\n",hdc);
446 if (!dc) return TRUE;
448 if (!dc->funcs->pSwapBuffers)
450 FIXME(" :stub\n");
451 bRet = TRUE;
453 else bRet = dc->funcs->pSwapBuffers(dc->physDev);
455 GDI_ReleaseObj( hdc );
456 return bRet;
460 /***********************************************************************
461 * PaintRgn (GDI32.@)
463 BOOL WINAPI PaintRgn( HDC hdc, HRGN hrgn )
465 BOOL ret = FALSE;
466 DC * dc = DC_GetDCUpdate( hdc );
467 if (dc)
469 if (dc->funcs->pPaintRgn) ret = dc->funcs->pPaintRgn(dc->physDev,hrgn);
470 GDI_ReleaseObj( hdc );
472 return ret;
476 /***********************************************************************
477 * FillRgn (GDI32.@)
479 BOOL WINAPI FillRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush )
481 BOOL retval = FALSE;
482 HBRUSH prevBrush;
483 DC * dc = DC_GetDCUpdate( hdc );
485 if (!dc) return FALSE;
486 if(dc->funcs->pFillRgn)
487 retval = dc->funcs->pFillRgn(dc->physDev, hrgn, hbrush);
488 else if ((prevBrush = SelectObject( hdc, hbrush )))
490 retval = PaintRgn( hdc, hrgn );
491 SelectObject( hdc, prevBrush );
493 GDI_ReleaseObj( hdc );
494 return retval;
498 /***********************************************************************
499 * FrameRgn (GDI32.@)
501 BOOL WINAPI FrameRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush,
502 INT nWidth, INT nHeight )
504 BOOL ret = FALSE;
505 DC *dc = DC_GetDCUpdate( hdc );
507 if (!dc) return FALSE;
508 if(dc->funcs->pFrameRgn)
509 ret = dc->funcs->pFrameRgn( dc->physDev, hrgn, hbrush, nWidth, nHeight );
510 else
512 HRGN tmp = CreateRectRgn( 0, 0, 0, 0 );
513 if (tmp)
515 if (REGION_FrameRgn( tmp, hrgn, nWidth, nHeight ))
517 FillRgn( hdc, tmp, hbrush );
518 ret = TRUE;
520 DeleteObject( tmp );
523 GDI_ReleaseObj( hdc );
524 return ret;
528 /***********************************************************************
529 * InvertRgn (GDI32.@)
531 BOOL WINAPI InvertRgn( HDC hdc, HRGN hrgn )
533 HBRUSH prevBrush;
534 INT prevROP;
535 BOOL retval;
536 DC *dc = DC_GetDCUpdate( hdc );
537 if (!dc) return FALSE;
539 if(dc->funcs->pInvertRgn)
540 retval = dc->funcs->pInvertRgn( dc->physDev, hrgn );
541 else
543 prevBrush = SelectObject( hdc, GetStockObject(BLACK_BRUSH) );
544 prevROP = SetROP2( hdc, R2_NOT );
545 retval = PaintRgn( hdc, hrgn );
546 SelectObject( hdc, prevBrush );
547 SetROP2( hdc, prevROP );
549 GDI_ReleaseObj( hdc );
550 return retval;
554 /**********************************************************************
555 * Polyline (GDI32.@)
557 BOOL WINAPI Polyline( HDC hdc, const POINT* pt, INT count )
559 BOOL ret = FALSE;
560 DC * dc = DC_GetDCUpdate( hdc );
561 if (dc)
563 if (PATH_IsPathOpen(dc->path)) ret = PATH_Polyline(dc, pt, count);
564 else if (dc->funcs->pPolyline) ret = dc->funcs->pPolyline(dc->physDev,pt,count);
565 GDI_ReleaseObj( hdc );
567 return ret;
570 /**********************************************************************
571 * PolylineTo (GDI32.@)
573 BOOL WINAPI PolylineTo( HDC hdc, const POINT* pt, DWORD cCount )
575 DC * dc = DC_GetDCUpdate( hdc );
576 BOOL ret = FALSE;
578 if(!dc) return FALSE;
580 if(PATH_IsPathOpen(dc->path))
581 ret = PATH_PolylineTo(dc, pt, cCount);
583 else if(dc->funcs->pPolylineTo)
584 ret = dc->funcs->pPolylineTo(dc->physDev, pt, cCount);
586 else { /* do it using Polyline */
587 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
588 sizeof(POINT) * (cCount + 1) );
589 if (pts)
591 pts[0].x = dc->CursPosX;
592 pts[0].y = dc->CursPosY;
593 memcpy( pts + 1, pt, sizeof(POINT) * cCount );
594 ret = Polyline( hdc, pts, cCount + 1 );
595 HeapFree( GetProcessHeap(), 0, pts );
598 if(ret) {
599 dc->CursPosX = pt[cCount-1].x;
600 dc->CursPosY = pt[cCount-1].y;
602 GDI_ReleaseObj( hdc );
603 return ret;
607 /**********************************************************************
608 * Polygon (GDI32.@)
610 BOOL WINAPI Polygon( HDC hdc, const POINT* pt, INT count )
612 BOOL ret = FALSE;
613 DC * dc = DC_GetDCUpdate( hdc );
614 if (dc)
616 if (PATH_IsPathOpen(dc->path)) ret = PATH_Polygon(dc, pt, count);
617 else if (dc->funcs->pPolygon) ret = dc->funcs->pPolygon(dc->physDev,pt,count);
618 GDI_ReleaseObj( hdc );
620 return ret;
624 /**********************************************************************
625 * PolyPolygon (GDI32.@)
627 BOOL WINAPI PolyPolygon( HDC hdc, const POINT* pt, const INT* counts,
628 UINT polygons )
630 BOOL ret = FALSE;
631 DC * dc = DC_GetDCUpdate( hdc );
632 if (dc)
634 if (PATH_IsPathOpen(dc->path)) ret = PATH_PolyPolygon(dc, pt, counts, polygons);
635 else if (dc->funcs->pPolyPolygon) ret = dc->funcs->pPolyPolygon(dc->physDev,pt,counts,polygons);
636 GDI_ReleaseObj( hdc );
638 return ret;
641 /**********************************************************************
642 * PolyPolyline (GDI32.@)
644 BOOL WINAPI PolyPolyline( HDC hdc, const POINT* pt, const DWORD* counts,
645 DWORD polylines )
647 BOOL ret = FALSE;
648 DC * dc = DC_GetDCUpdate( hdc );
649 if (dc)
651 if (PATH_IsPathOpen(dc->path)) ret = PATH_PolyPolyline(dc, pt, counts, polylines);
652 else if (dc->funcs->pPolyPolyline) ret = dc->funcs->pPolyPolyline(dc->physDev,pt,counts,polylines);
653 GDI_ReleaseObj( hdc );
655 return ret;
658 /**********************************************************************
659 * ExtFloodFill (GDI32.@)
661 BOOL WINAPI ExtFloodFill( HDC hdc, INT x, INT y, COLORREF color,
662 UINT fillType )
664 BOOL ret = FALSE;
665 DC * dc = DC_GetDCUpdate( hdc );
666 if (dc)
668 if (dc->funcs->pExtFloodFill) ret = dc->funcs->pExtFloodFill(dc->physDev,x,y,color,fillType);
669 GDI_ReleaseObj( hdc );
671 return ret;
675 /**********************************************************************
676 * FloodFill (GDI32.@)
678 BOOL WINAPI FloodFill( HDC hdc, INT x, INT y, COLORREF color )
680 return ExtFloodFill( hdc, x, y, color, FLOODFILLBORDER );
684 /******************************************************************************
685 * PolyBezier [GDI32.@]
686 * Draws one or more Bezier curves
688 * PARAMS
689 * hDc [I] Handle to device context
690 * lppt [I] Pointer to endpoints and control points
691 * cPoints [I] Count of endpoints and control points
693 * RETURNS STD
695 BOOL WINAPI PolyBezier( HDC hdc, const POINT* lppt, DWORD cPoints )
697 BOOL ret = FALSE;
698 DC * dc = DC_GetDCUpdate( hdc );
700 if(!dc) return FALSE;
702 if(PATH_IsPathOpen(dc->path))
703 ret = PATH_PolyBezier(dc, lppt, cPoints);
704 else if (dc->funcs->pPolyBezier)
705 ret = dc->funcs->pPolyBezier(dc->physDev, lppt, cPoints);
706 else /* We'll convert it into line segments and draw them using Polyline */
708 POINT *Pts;
709 INT nOut;
711 if ((Pts = GDI_Bezier( lppt, cPoints, &nOut )))
713 TRACE("Pts = %p, no = %d\n", Pts, nOut);
714 ret = Polyline( dc->hSelf, Pts, nOut );
715 HeapFree( GetProcessHeap(), 0, Pts );
719 GDI_ReleaseObj( hdc );
720 return ret;
723 /******************************************************************************
724 * PolyBezierTo [GDI32.@]
725 * Draws one or more Bezier curves
727 * PARAMS
728 * hDc [I] Handle to device context
729 * lppt [I] Pointer to endpoints and control points
730 * cPoints [I] Count of endpoints and control points
732 * RETURNS STD
734 BOOL WINAPI PolyBezierTo( HDC hdc, const POINT* lppt, DWORD cPoints )
736 DC * dc = DC_GetDCUpdate( hdc );
737 BOOL ret;
739 if(!dc) return FALSE;
741 if(PATH_IsPathOpen(dc->path))
742 ret = PATH_PolyBezierTo(dc, lppt, cPoints);
743 else if(dc->funcs->pPolyBezierTo)
744 ret = dc->funcs->pPolyBezierTo(dc->physDev, lppt, cPoints);
745 else { /* We'll do it using PolyBezier */
746 POINT *pt;
747 pt = HeapAlloc( GetProcessHeap(), 0, sizeof(POINT) * (cPoints + 1) );
748 if(!pt) return FALSE;
749 pt[0].x = dc->CursPosX;
750 pt[0].y = dc->CursPosY;
751 memcpy(pt + 1, lppt, sizeof(POINT) * cPoints);
752 ret = PolyBezier(dc->hSelf, pt, cPoints+1);
753 HeapFree( GetProcessHeap(), 0, pt );
755 if(ret) {
756 dc->CursPosX = lppt[cPoints-1].x;
757 dc->CursPosY = lppt[cPoints-1].y;
759 GDI_ReleaseObj( hdc );
760 return ret;
763 /***********************************************************************
764 * AngleArc (GDI32.@)
766 BOOL WINAPI AngleArc(HDC hdc, INT x, INT y, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle)
768 INT x1,y1,x2,y2, arcdir;
769 BOOL result;
770 DC *dc;
772 if( (signed int)dwRadius < 0 )
773 return FALSE;
775 dc = DC_GetDCUpdate( hdc );
776 if(!dc) return FALSE;
778 if(dc->funcs->pAngleArc)
780 result = dc->funcs->pAngleArc( dc->physDev, x, y, dwRadius, eStartAngle, eSweepAngle );
782 GDI_ReleaseObj( hdc );
783 return result;
785 GDI_ReleaseObj( hdc );
787 /* AngleArc always works counterclockwise */
788 arcdir = GetArcDirection( hdc );
789 SetArcDirection( hdc, AD_COUNTERCLOCKWISE );
791 x1 = x + cos(eStartAngle*M_PI/180) * dwRadius;
792 y1 = y - sin(eStartAngle*M_PI/180) * dwRadius;
793 x2 = x + cos((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius;
794 y2 = x - sin((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius;
796 LineTo( hdc, x1, y1 );
797 if( eSweepAngle >= 0 )
798 result = Arc( hdc, x-dwRadius, y-dwRadius, x+dwRadius, y+dwRadius,
799 x1, y1, x2, y2 );
800 else
801 result = Arc( hdc, x-dwRadius, y-dwRadius, x+dwRadius, y+dwRadius,
802 x2, y2, x1, y1 );
804 if( result ) MoveToEx( hdc, x2, y2, NULL );
805 SetArcDirection( hdc, arcdir );
806 return result;
809 /***********************************************************************
810 * PolyDraw (GDI32.@)
812 BOOL WINAPI PolyDraw(HDC hdc, const POINT *lppt, const BYTE *lpbTypes,
813 DWORD cCount)
815 DC *dc;
816 BOOL result;
817 POINT lastmove;
818 int i;
820 dc = DC_GetDCUpdate( hdc );
821 if(!dc) return FALSE;
823 if(dc->funcs->pPolyDraw)
825 result = dc->funcs->pPolyDraw( dc->physDev, lppt, lpbTypes, cCount );
826 GDI_ReleaseObj( hdc );
827 return result;
829 GDI_ReleaseObj( hdc );
831 /* check for each bezierto if there are two more points */
832 for( i = 0; i < cCount; i++ )
833 if( lpbTypes[i] != PT_MOVETO &&
834 lpbTypes[i] & PT_BEZIERTO )
836 if( cCount < i+3 )
837 return FALSE;
838 else
839 i += 2;
842 /* if no moveto occurs, we will close the figure here */
843 lastmove.x = dc->CursPosX;
844 lastmove.y = dc->CursPosY;
846 /* now let's draw */
847 for( i = 0; i < cCount; i++ )
849 if( lpbTypes[i] == PT_MOVETO )
851 MoveToEx( hdc, lppt[i].x, lppt[i].y, NULL );
852 lastmove.x = dc->CursPosX;
853 lastmove.y = dc->CursPosY;
855 else if( lpbTypes[i] & PT_LINETO )
856 LineTo( hdc, lppt[i].x, lppt[i].y );
857 else if( lpbTypes[i] & PT_BEZIERTO )
859 PolyBezierTo( hdc, &lppt[i], 3 );
860 i += 2;
862 else
863 return FALSE;
865 if( lpbTypes[i] & PT_CLOSEFIGURE )
867 if( PATH_IsPathOpen( dc->path ) )
868 CloseFigure( hdc );
869 else
870 LineTo( hdc, lastmove.x, lastmove.y );
874 return TRUE;
877 /******************************************************************
879 * *Very* simple bezier drawing code,
881 * It uses a recursive algorithm to divide the curve in a series
882 * of straight line segements. Not ideal but for me sufficient.
883 * If you are in need for something better look for some incremental
884 * algorithm.
886 * 7 July 1998 Rein Klazes
890 * some macro definitions for bezier drawing
892 * to avoid truncation errors the coordinates are
893 * shifted upwards. When used in drawing they are
894 * shifted down again, including correct rounding
895 * and avoiding floating point arithmetic
896 * 4 bits should allow 27 bits coordinates which I saw
897 * somewhere in the win32 doc's
901 #define BEZIERSHIFTBITS 4
902 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
903 #define BEZIERPIXEL BEZIERSHIFTUP(1)
904 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
905 /* maximum depth of recursion */
906 #define BEZIERMAXDEPTH 8
908 /* size of array to store points on */
909 /* enough for one curve */
910 #define BEZIER_INITBUFSIZE (150)
912 /* calculate Bezier average, in this case the middle
913 * correctly rounded...
914 * */
916 #define BEZIERMIDDLE(Mid, P1, P2) \
917 (Mid).x=((P1).x+(P2).x + 1)/2;\
918 (Mid).y=((P1).y+(P2).y + 1)/2;
920 /**********************************************************
921 * BezierCheck helper function to check
922 * that recursion can be terminated
923 * Points[0] and Points[3] are begin and endpoint
924 * Points[1] and Points[2] are control points
925 * level is the recursion depth
926 * returns true if the recusion can be terminated
928 static BOOL BezierCheck( int level, POINT *Points)
930 INT dx, dy;
931 dx=Points[3].x-Points[0].x;
932 dy=Points[3].y-Points[0].y;
933 if(abs(dy)<=abs(dx)){/* shallow line */
934 /* check that control points are between begin and end */
935 if(Points[1].x < Points[0].x){
936 if(Points[1].x < Points[3].x)
937 return FALSE;
938 }else
939 if(Points[1].x > Points[3].x)
940 return FALSE;
941 if(Points[2].x < Points[0].x){
942 if(Points[2].x < Points[3].x)
943 return FALSE;
944 }else
945 if(Points[2].x > Points[3].x)
946 return FALSE;
947 dx=BEZIERSHIFTDOWN(dx);
948 if(!dx) return TRUE;
949 if(abs(Points[1].y-Points[0].y-(dy/dx)*
950 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
951 abs(Points[2].y-Points[0].y-(dy/dx)*
952 BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
953 return FALSE;
954 else
955 return TRUE;
956 }else{ /* steep line */
957 /* check that control points are between begin and end */
958 if(Points[1].y < Points[0].y){
959 if(Points[1].y < Points[3].y)
960 return FALSE;
961 }else
962 if(Points[1].y > Points[3].y)
963 return FALSE;
964 if(Points[2].y < Points[0].y){
965 if(Points[2].y < Points[3].y)
966 return FALSE;
967 }else
968 if(Points[2].y > Points[3].y)
969 return FALSE;
970 dy=BEZIERSHIFTDOWN(dy);
971 if(!dy) return TRUE;
972 if(abs(Points[1].x-Points[0].x-(dx/dy)*
973 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
974 abs(Points[2].x-Points[0].x-(dx/dy)*
975 BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
976 return FALSE;
977 else
978 return TRUE;
982 /* Helper for GDI_Bezier.
983 * Just handles one Bezier, so Points should point to four POINTs
985 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
986 INT *nPtsOut, INT level )
988 if(*nPtsOut == *dwOut) {
989 *dwOut *= 2;
990 *PtsOut = HeapReAlloc( GetProcessHeap(), 0, *PtsOut,
991 *dwOut * sizeof(POINT) );
994 if(!level || BezierCheck(level, Points)) {
995 if(*nPtsOut == 0) {
996 (*PtsOut)[0].x = BEZIERSHIFTDOWN(Points[0].x);
997 (*PtsOut)[0].y = BEZIERSHIFTDOWN(Points[0].y);
998 *nPtsOut = 1;
1000 (*PtsOut)[*nPtsOut].x = BEZIERSHIFTDOWN(Points[3].x);
1001 (*PtsOut)[*nPtsOut].y = BEZIERSHIFTDOWN(Points[3].y);
1002 (*nPtsOut) ++;
1003 } else {
1004 POINT Points2[4]; /* for the second recursive call */
1005 Points2[3]=Points[3];
1006 BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
1007 BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
1008 BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
1010 BEZIERMIDDLE(Points[1], Points[0], Points[1]);
1011 BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1012 BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1014 Points2[0]=Points[3];
1016 /* do the two halves */
1017 GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
1018 GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
1024 /***********************************************************************
1025 * GDI_Bezier [INTERNAL]
1026 * Calculate line segments that approximate -what microsoft calls- a bezier
1027 * curve.
1028 * The routine recursively divides the curve in two parts until a straight
1029 * line can be drawn
1031 * PARAMS
1033 * Points [I] Ptr to count POINTs which are the end and control points
1034 * of the set of Bezier curves to flatten.
1035 * count [I] Number of Points. Must be 3n+1.
1036 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
1037 * lines+1).
1039 * RETURNS
1041 * Ptr to an array of POINTs that contain the lines that approximinate the
1042 * Beziers. The array is allocated on the process heap and it is the caller's
1043 * responsibility to HeapFree it. [this is not a particularly nice interface
1044 * but since we can't know in advance how many points will generate, the
1045 * alternative would be to call the function twice, once to determine the size
1046 * and a second time to do the work - I decided this was too much of a pain].
1048 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
1050 POINT *out;
1051 INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
1053 if((count - 1) % 3 != 0) {
1054 ERR("Invalid no. of points\n");
1055 return NULL;
1057 *nPtsOut = 0;
1058 out = HeapAlloc( GetProcessHeap(), 0, dwOut * sizeof(POINT));
1059 for(Bezier = 0; Bezier < (count-1)/3; Bezier++) {
1060 POINT ptBuf[4];
1061 memcpy(ptBuf, Points + Bezier * 3, sizeof(POINT) * 4);
1062 for(i = 0; i < 4; i++) {
1063 ptBuf[i].x = BEZIERSHIFTUP(ptBuf[i].x);
1064 ptBuf[i].y = BEZIERSHIFTUP(ptBuf[i].y);
1066 GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
1068 TRACE("Produced %d points\n", *nPtsOut);
1069 return out;
1072 /******************************************************************************
1073 * GradientFill (GDI32.@)
1075 BOOL WINAPI GdiGradientFill( HDC hdc, TRIVERTEX *vert_array, ULONG nvert,
1076 void * grad_array, ULONG ngrad, ULONG mode )
1078 int i;
1080 TRACE("vert_array:0x%08lx nvert:%ld grad_array:0x%08lx ngrad:%ld\n",
1081 (long)vert_array, nvert, (long)grad_array, ngrad);
1083 switch(mode)
1085 case GRADIENT_FILL_RECT_H:
1086 for(i = 0; i < ngrad; i++)
1088 GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1089 TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1090 TRIVERTEX *v2 = vert_array + rect->LowerRight;
1091 int y1 = v1->y < v2->y ? v1->y : v2->y;
1092 int y2 = v2->y > v1->y ? v2->y : v1->y;
1093 int x, y, dx;
1094 if (v1->x > v2->x)
1096 TRIVERTEX *t = v2;
1097 v2 = v1;
1098 v1 = t;
1100 dx = v2->x - v1->x;
1101 for (x = 0; x < dx; x++)
1103 for (y = y1; y < y2; y++)
1104 SetPixel (hdc, x + v1->x, y, RGB(
1105 (v1->Red * (dx - x) + v2->Red * x) / dx >> 8,
1106 (v1->Green * (dx - x) + v2->Green * x) / dx >> 8,
1107 (v1->Blue * (dx - x) + v2->Blue * x) / dx >> 8));
1110 break;
1111 case GRADIENT_FILL_RECT_V:
1112 for(i = 0; i < ngrad; i++)
1114 GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1115 TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1116 TRIVERTEX *v2 = vert_array + rect->LowerRight;
1117 int x1 = v1->x < v2->x ? v1->x : v2->x;
1118 int x2 = v2->x > v1->x ? v2->x : v1->x;
1119 int x, y, dy;
1120 if (v1->y > v2->y)
1122 TRIVERTEX *t = v2;
1123 v2 = v1;
1124 v1 = t;
1126 dy = v2->y - v1->y;
1127 for (y = 0; y < dy; y++)
1129 for (x = x1; x < x2; x++)
1130 SetPixel (hdc, x, y + v1->y, RGB(
1131 (v1->Red * (dy - y) + v2->Red * y) / dy >> 8,
1132 (v1->Green * (dy - y) + v2->Green * y) / dy >> 8,
1133 (v1->Blue * (dy - y) + v2->Blue * y) / dy >> 8));
1136 break;
1137 case GRADIENT_FILL_TRIANGLE:
1138 for (i = 0; i < ngrad; i++)
1140 GRADIENT_TRIANGLE *tri = ((GRADIENT_TRIANGLE *)grad_array) + i;
1141 TRIVERTEX *v1 = vert_array + tri->Vertex1;
1142 TRIVERTEX *v2 = vert_array + tri->Vertex2;
1143 TRIVERTEX *v3 = vert_array + tri->Vertex3;
1144 int y, dy;
1146 if (v1->y > v2->y)
1147 { TRIVERTEX *t = v1; v1 = v2; v2 = t; }
1148 if (v2->y > v3->y)
1150 TRIVERTEX *t = v2; v2 = v3; v3 = t;
1151 if (v1->y > v2->y)
1152 { t = v1; v1 = v2; v2 = t; }
1154 /* v1->y <= v2->y <= v3->y */
1156 dy = v3->y - v1->y;
1157 for (y = 0; y < dy; y++)
1159 /* v1->y <= y < v3->y */
1160 TRIVERTEX *v = y < (v2->y - v1->y) ? v1 : v3;
1161 /* (v->y <= y < v2->y) || (v2->y <= y < v->y) */
1162 int dy2 = v2->y - v->y;
1163 int y2 = y + v1->y - v->y;
1165 int x1 = (v3->x * y + v1->x * (dy - y )) / dy;
1166 int x2 = (v2->x * y2 + v-> x * (dy2 - y2)) / dy2;
1167 int r1 = (v3->Red * y + v1->Red * (dy - y )) / dy;
1168 int r2 = (v2->Red * y2 + v-> Red * (dy2 - y2)) / dy2;
1169 int g1 = (v3->Green * y + v1->Green * (dy - y )) / dy;
1170 int g2 = (v2->Green * y2 + v-> Green * (dy2 - y2)) / dy2;
1171 int b1 = (v3->Blue * y + v1->Blue * (dy - y )) / dy;
1172 int b2 = (v2->Blue * y2 + v-> Blue * (dy2 - y2)) / dy2;
1174 int x;
1175 if (x1 < x2)
1177 int dx = x2 - x1;
1178 for (x = 0; x < dx; x++)
1179 SetPixel (hdc, x + x1, y + v1->y, RGB(
1180 (r1 * (dx - x) + r2 * x) / dx >> 8,
1181 (g1 * (dx - x) + g2 * x) / dx >> 8,
1182 (b1 * (dx - x) + b2 * x) / dx >> 8));
1184 else
1186 int dx = x1 - x2;
1187 for (x = 0; x < dx; x++)
1188 SetPixel (hdc, x + x2, y + v1->y, RGB(
1189 (r2 * (dx - x) + r1 * x) / dx >> 8,
1190 (g2 * (dx - x) + g1 * x) / dx >> 8,
1191 (b2 * (dx - x) + b1 * x) / dx >> 8));
1195 break;
1196 default:
1197 return FALSE;
1200 return TRUE;