#ifdef guard the declaration of type IID to be compatible with
[wine.git] / graphics / painting.c
blobcd37f90eeab116431a9319bcc278bca993f2a67f
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;
700 /* cPoints must be 3 * n + 1 (where n>=1) */
701 if (cPoints == 1 || (cPoints % 3) != 1) return FALSE;
703 dc = DC_GetDCUpdate( hdc );
704 if(!dc) return FALSE;
706 if(PATH_IsPathOpen(dc->path))
707 ret = PATH_PolyBezier(dc, lppt, cPoints);
708 else if (dc->funcs->pPolyBezier)
709 ret = dc->funcs->pPolyBezier(dc->physDev, lppt, cPoints);
710 else /* We'll convert it into line segments and draw them using Polyline */
712 POINT *Pts;
713 INT nOut;
715 if ((Pts = GDI_Bezier( lppt, cPoints, &nOut )))
717 TRACE("Pts = %p, no = %d\n", Pts, nOut);
718 ret = Polyline( dc->hSelf, Pts, nOut );
719 HeapFree( GetProcessHeap(), 0, Pts );
723 GDI_ReleaseObj( hdc );
724 return ret;
727 /******************************************************************************
728 * PolyBezierTo [GDI32.@]
729 * Draws one or more Bezier curves
731 * PARAMS
732 * hDc [I] Handle to device context
733 * lppt [I] Pointer to endpoints and control points
734 * cPoints [I] Count of endpoints and control points
736 * RETURNS STD
738 BOOL WINAPI PolyBezierTo( HDC hdc, const POINT* lppt, DWORD cPoints )
740 DC * dc = DC_GetDCUpdate( hdc );
741 BOOL ret;
743 if(!dc) return FALSE;
745 if(PATH_IsPathOpen(dc->path))
746 ret = PATH_PolyBezierTo(dc, lppt, cPoints);
747 else if(dc->funcs->pPolyBezierTo)
748 ret = dc->funcs->pPolyBezierTo(dc->physDev, lppt, cPoints);
749 else { /* We'll do it using PolyBezier */
750 POINT *pt;
751 pt = HeapAlloc( GetProcessHeap(), 0, sizeof(POINT) * (cPoints + 1) );
752 if(!pt) return FALSE;
753 pt[0].x = dc->CursPosX;
754 pt[0].y = dc->CursPosY;
755 memcpy(pt + 1, lppt, sizeof(POINT) * cPoints);
756 ret = PolyBezier(dc->hSelf, pt, cPoints+1);
757 HeapFree( GetProcessHeap(), 0, pt );
759 if(ret) {
760 dc->CursPosX = lppt[cPoints-1].x;
761 dc->CursPosY = lppt[cPoints-1].y;
763 GDI_ReleaseObj( hdc );
764 return ret;
767 /***********************************************************************
768 * AngleArc (GDI32.@)
770 BOOL WINAPI AngleArc(HDC hdc, INT x, INT y, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle)
772 INT x1,y1,x2,y2, arcdir;
773 BOOL result;
774 DC *dc;
776 if( (signed int)dwRadius < 0 )
777 return FALSE;
779 dc = DC_GetDCUpdate( hdc );
780 if(!dc) return FALSE;
782 if(dc->funcs->pAngleArc)
784 result = dc->funcs->pAngleArc( dc->physDev, x, y, dwRadius, eStartAngle, eSweepAngle );
786 GDI_ReleaseObj( hdc );
787 return result;
789 GDI_ReleaseObj( hdc );
791 /* AngleArc always works counterclockwise */
792 arcdir = GetArcDirection( hdc );
793 SetArcDirection( hdc, AD_COUNTERCLOCKWISE );
795 x1 = x + cos(eStartAngle*M_PI/180) * dwRadius;
796 y1 = y - sin(eStartAngle*M_PI/180) * dwRadius;
797 x2 = x + cos((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius;
798 y2 = x - sin((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius;
800 LineTo( hdc, x1, y1 );
801 if( eSweepAngle >= 0 )
802 result = Arc( hdc, x-dwRadius, y-dwRadius, x+dwRadius, y+dwRadius,
803 x1, y1, x2, y2 );
804 else
805 result = Arc( hdc, x-dwRadius, y-dwRadius, x+dwRadius, y+dwRadius,
806 x2, y2, x1, y1 );
808 if( result ) MoveToEx( hdc, x2, y2, NULL );
809 SetArcDirection( hdc, arcdir );
810 return result;
813 /***********************************************************************
814 * PolyDraw (GDI32.@)
816 BOOL WINAPI PolyDraw(HDC hdc, const POINT *lppt, const BYTE *lpbTypes,
817 DWORD cCount)
819 DC *dc;
820 BOOL result;
821 POINT lastmove;
822 int i;
824 dc = DC_GetDCUpdate( hdc );
825 if(!dc) return FALSE;
827 if(dc->funcs->pPolyDraw)
829 result = dc->funcs->pPolyDraw( dc->physDev, lppt, lpbTypes, cCount );
830 GDI_ReleaseObj( hdc );
831 return result;
833 GDI_ReleaseObj( hdc );
835 /* check for each bezierto if there are two more points */
836 for( i = 0; i < cCount; i++ )
837 if( lpbTypes[i] != PT_MOVETO &&
838 lpbTypes[i] & PT_BEZIERTO )
840 if( cCount < i+3 )
841 return FALSE;
842 else
843 i += 2;
846 /* if no moveto occurs, we will close the figure here */
847 lastmove.x = dc->CursPosX;
848 lastmove.y = dc->CursPosY;
850 /* now let's draw */
851 for( i = 0; i < cCount; i++ )
853 if( lpbTypes[i] == PT_MOVETO )
855 MoveToEx( hdc, lppt[i].x, lppt[i].y, NULL );
856 lastmove.x = dc->CursPosX;
857 lastmove.y = dc->CursPosY;
859 else if( lpbTypes[i] & PT_LINETO )
860 LineTo( hdc, lppt[i].x, lppt[i].y );
861 else if( lpbTypes[i] & PT_BEZIERTO )
863 PolyBezierTo( hdc, &lppt[i], 3 );
864 i += 2;
866 else
867 return FALSE;
869 if( lpbTypes[i] & PT_CLOSEFIGURE )
871 if( PATH_IsPathOpen( dc->path ) )
872 CloseFigure( hdc );
873 else
874 LineTo( hdc, lastmove.x, lastmove.y );
878 return TRUE;
881 /******************************************************************
883 * *Very* simple bezier drawing code,
885 * It uses a recursive algorithm to divide the curve in a series
886 * of straight line segements. Not ideal but for me sufficient.
887 * If you are in need for something better look for some incremental
888 * algorithm.
890 * 7 July 1998 Rein Klazes
894 * some macro definitions for bezier drawing
896 * to avoid truncation errors the coordinates are
897 * shifted upwards. When used in drawing they are
898 * shifted down again, including correct rounding
899 * and avoiding floating point arithmetic
900 * 4 bits should allow 27 bits coordinates which I saw
901 * somewhere in the win32 doc's
905 #define BEZIERSHIFTBITS 4
906 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
907 #define BEZIERPIXEL BEZIERSHIFTUP(1)
908 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
909 /* maximum depth of recursion */
910 #define BEZIERMAXDEPTH 8
912 /* size of array to store points on */
913 /* enough for one curve */
914 #define BEZIER_INITBUFSIZE (150)
916 /* calculate Bezier average, in this case the middle
917 * correctly rounded...
918 * */
920 #define BEZIERMIDDLE(Mid, P1, P2) \
921 (Mid).x=((P1).x+(P2).x + 1)/2;\
922 (Mid).y=((P1).y+(P2).y + 1)/2;
924 /**********************************************************
925 * BezierCheck helper function to check
926 * that recursion can be terminated
927 * Points[0] and Points[3] are begin and endpoint
928 * Points[1] and Points[2] are control points
929 * level is the recursion depth
930 * returns true if the recusion can be terminated
932 static BOOL BezierCheck( int level, POINT *Points)
934 INT dx, dy;
935 dx=Points[3].x-Points[0].x;
936 dy=Points[3].y-Points[0].y;
937 if(abs(dy)<=abs(dx)){/* shallow line */
938 /* check that control points are between begin and end */
939 if(Points[1].x < Points[0].x){
940 if(Points[1].x < Points[3].x)
941 return FALSE;
942 }else
943 if(Points[1].x > Points[3].x)
944 return FALSE;
945 if(Points[2].x < Points[0].x){
946 if(Points[2].x < Points[3].x)
947 return FALSE;
948 }else
949 if(Points[2].x > Points[3].x)
950 return FALSE;
951 dx=BEZIERSHIFTDOWN(dx);
952 if(!dx) return TRUE;
953 if(abs(Points[1].y-Points[0].y-(dy/dx)*
954 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
955 abs(Points[2].y-Points[0].y-(dy/dx)*
956 BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
957 return FALSE;
958 else
959 return TRUE;
960 }else{ /* steep line */
961 /* check that control points are between begin and end */
962 if(Points[1].y < Points[0].y){
963 if(Points[1].y < Points[3].y)
964 return FALSE;
965 }else
966 if(Points[1].y > Points[3].y)
967 return FALSE;
968 if(Points[2].y < Points[0].y){
969 if(Points[2].y < Points[3].y)
970 return FALSE;
971 }else
972 if(Points[2].y > Points[3].y)
973 return FALSE;
974 dy=BEZIERSHIFTDOWN(dy);
975 if(!dy) return TRUE;
976 if(abs(Points[1].x-Points[0].x-(dx/dy)*
977 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
978 abs(Points[2].x-Points[0].x-(dx/dy)*
979 BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
980 return FALSE;
981 else
982 return TRUE;
986 /* Helper for GDI_Bezier.
987 * Just handles one Bezier, so Points should point to four POINTs
989 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
990 INT *nPtsOut, INT level )
992 if(*nPtsOut == *dwOut) {
993 *dwOut *= 2;
994 *PtsOut = HeapReAlloc( GetProcessHeap(), 0, *PtsOut,
995 *dwOut * sizeof(POINT) );
998 if(!level || BezierCheck(level, Points)) {
999 if(*nPtsOut == 0) {
1000 (*PtsOut)[0].x = BEZIERSHIFTDOWN(Points[0].x);
1001 (*PtsOut)[0].y = BEZIERSHIFTDOWN(Points[0].y);
1002 *nPtsOut = 1;
1004 (*PtsOut)[*nPtsOut].x = BEZIERSHIFTDOWN(Points[3].x);
1005 (*PtsOut)[*nPtsOut].y = BEZIERSHIFTDOWN(Points[3].y);
1006 (*nPtsOut) ++;
1007 } else {
1008 POINT Points2[4]; /* for the second recursive call */
1009 Points2[3]=Points[3];
1010 BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
1011 BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
1012 BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
1014 BEZIERMIDDLE(Points[1], Points[0], Points[1]);
1015 BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1016 BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1018 Points2[0]=Points[3];
1020 /* do the two halves */
1021 GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
1022 GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
1028 /***********************************************************************
1029 * GDI_Bezier [INTERNAL]
1030 * Calculate line segments that approximate -what microsoft calls- a bezier
1031 * curve.
1032 * The routine recursively divides the curve in two parts until a straight
1033 * line can be drawn
1035 * PARAMS
1037 * Points [I] Ptr to count POINTs which are the end and control points
1038 * of the set of Bezier curves to flatten.
1039 * count [I] Number of Points. Must be 3n+1.
1040 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
1041 * lines+1).
1043 * RETURNS
1045 * Ptr to an array of POINTs that contain the lines that approximinate the
1046 * Beziers. The array is allocated on the process heap and it is the caller's
1047 * responsibility to HeapFree it. [this is not a particularly nice interface
1048 * but since we can't know in advance how many points will generate, the
1049 * alternative would be to call the function twice, once to determine the size
1050 * and a second time to do the work - I decided this was too much of a pain].
1052 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
1054 POINT *out;
1055 INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
1057 if((count - 1) % 3 != 0) {
1058 ERR("Invalid no. of points\n");
1059 return NULL;
1061 *nPtsOut = 0;
1062 out = HeapAlloc( GetProcessHeap(), 0, dwOut * sizeof(POINT));
1063 for(Bezier = 0; Bezier < (count-1)/3; Bezier++) {
1064 POINT ptBuf[4];
1065 memcpy(ptBuf, Points + Bezier * 3, sizeof(POINT) * 4);
1066 for(i = 0; i < 4; i++) {
1067 ptBuf[i].x = BEZIERSHIFTUP(ptBuf[i].x);
1068 ptBuf[i].y = BEZIERSHIFTUP(ptBuf[i].y);
1070 GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
1072 TRACE("Produced %d points\n", *nPtsOut);
1073 return out;
1076 /******************************************************************************
1077 * GradientFill (GDI32.@)
1079 BOOL WINAPI GdiGradientFill( HDC hdc, TRIVERTEX *vert_array, ULONG nvert,
1080 void * grad_array, ULONG ngrad, ULONG mode )
1082 int i;
1084 TRACE("vert_array:0x%08lx nvert:%ld grad_array:0x%08lx ngrad:%ld\n",
1085 (long)vert_array, nvert, (long)grad_array, ngrad);
1087 switch(mode)
1089 case GRADIENT_FILL_RECT_H:
1090 for(i = 0; i < ngrad; i++)
1092 GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1093 TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1094 TRIVERTEX *v2 = vert_array + rect->LowerRight;
1095 int y1 = v1->y < v2->y ? v1->y : v2->y;
1096 int y2 = v2->y > v1->y ? v2->y : v1->y;
1097 int x, y, dx;
1098 if (v1->x > v2->x)
1100 TRIVERTEX *t = v2;
1101 v2 = v1;
1102 v1 = t;
1104 dx = v2->x - v1->x;
1105 for (x = 0; x < dx; x++)
1107 for (y = y1; y < y2; y++)
1108 SetPixel (hdc, x + v1->x, y, RGB(
1109 (v1->Red * (dx - x) + v2->Red * x) / dx >> 8,
1110 (v1->Green * (dx - x) + v2->Green * x) / dx >> 8,
1111 (v1->Blue * (dx - x) + v2->Blue * x) / dx >> 8));
1114 break;
1115 case GRADIENT_FILL_RECT_V:
1116 for(i = 0; i < ngrad; i++)
1118 GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1119 TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1120 TRIVERTEX *v2 = vert_array + rect->LowerRight;
1121 int x1 = v1->x < v2->x ? v1->x : v2->x;
1122 int x2 = v2->x > v1->x ? v2->x : v1->x;
1123 int x, y, dy;
1124 if (v1->y > v2->y)
1126 TRIVERTEX *t = v2;
1127 v2 = v1;
1128 v1 = t;
1130 dy = v2->y - v1->y;
1131 for (y = 0; y < dy; y++)
1133 for (x = x1; x < x2; x++)
1134 SetPixel (hdc, x, y + v1->y, RGB(
1135 (v1->Red * (dy - y) + v2->Red * y) / dy >> 8,
1136 (v1->Green * (dy - y) + v2->Green * y) / dy >> 8,
1137 (v1->Blue * (dy - y) + v2->Blue * y) / dy >> 8));
1140 break;
1141 case GRADIENT_FILL_TRIANGLE:
1142 for (i = 0; i < ngrad; i++)
1144 GRADIENT_TRIANGLE *tri = ((GRADIENT_TRIANGLE *)grad_array) + i;
1145 TRIVERTEX *v1 = vert_array + tri->Vertex1;
1146 TRIVERTEX *v2 = vert_array + tri->Vertex2;
1147 TRIVERTEX *v3 = vert_array + tri->Vertex3;
1148 int y, dy;
1150 if (v1->y > v2->y)
1151 { TRIVERTEX *t = v1; v1 = v2; v2 = t; }
1152 if (v2->y > v3->y)
1154 TRIVERTEX *t = v2; v2 = v3; v3 = t;
1155 if (v1->y > v2->y)
1156 { t = v1; v1 = v2; v2 = t; }
1158 /* v1->y <= v2->y <= v3->y */
1160 dy = v3->y - v1->y;
1161 for (y = 0; y < dy; y++)
1163 /* v1->y <= y < v3->y */
1164 TRIVERTEX *v = y < (v2->y - v1->y) ? v1 : v3;
1165 /* (v->y <= y < v2->y) || (v2->y <= y < v->y) */
1166 int dy2 = v2->y - v->y;
1167 int y2 = y + v1->y - v->y;
1169 int x1 = (v3->x * y + v1->x * (dy - y )) / dy;
1170 int x2 = (v2->x * y2 + v-> x * (dy2 - y2)) / dy2;
1171 int r1 = (v3->Red * y + v1->Red * (dy - y )) / dy;
1172 int r2 = (v2->Red * y2 + v-> Red * (dy2 - y2)) / dy2;
1173 int g1 = (v3->Green * y + v1->Green * (dy - y )) / dy;
1174 int g2 = (v2->Green * y2 + v-> Green * (dy2 - y2)) / dy2;
1175 int b1 = (v3->Blue * y + v1->Blue * (dy - y )) / dy;
1176 int b2 = (v2->Blue * y2 + v-> Blue * (dy2 - y2)) / dy2;
1178 int x;
1179 if (x1 < x2)
1181 int dx = x2 - x1;
1182 for (x = 0; x < dx; x++)
1183 SetPixel (hdc, x + x1, y + v1->y, RGB(
1184 (r1 * (dx - x) + r2 * x) / dx >> 8,
1185 (g1 * (dx - x) + g2 * x) / dx >> 8,
1186 (b1 * (dx - x) + b2 * x) / dx >> 8));
1188 else
1190 int dx = x1 - x2;
1191 for (x = 0; x < dx; x++)
1192 SetPixel (hdc, x + x2, y + v1->y, RGB(
1193 (r2 * (dx - x) + r1 * x) / dx >> 8,
1194 (g2 * (dx - x) + g1 * x) / dx >> 8,
1195 (b2 * (dx - x) + b1 * x) / dx >> 8));
1199 break;
1200 default:
1201 return FALSE;
1204 return TRUE;