Real mode context should always have V86 flag turned on.
[wine/multimedia.git] / graphics / painting.c
blobb251dacc7666155da94d5b37f659ef4b44dcd652
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 <string.h>
27 #include <stdlib.h>
29 #include "windef.h"
30 #include "wingdi.h"
31 #include "winerror.h"
32 #include "gdi.h"
33 #include "bitmap.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
39 /***********************************************************************
40 * LineTo (GDI32.@)
42 BOOL WINAPI LineTo( HDC hdc, INT x, INT y )
44 DC * dc = DC_GetDCUpdate( hdc );
45 BOOL ret;
47 if(!dc) return FALSE;
49 if(PATH_IsPathOpen(dc->path))
50 ret = PATH_LineTo(dc, x, y);
51 else
52 ret = dc->funcs->pLineTo && dc->funcs->pLineTo(dc->physDev,x,y);
53 if(ret) {
54 dc->CursPosX = x;
55 dc->CursPosY = y;
57 GDI_ReleaseObj( hdc );
58 return ret;
62 /***********************************************************************
63 * MoveToEx (GDI32.@)
65 BOOL WINAPI MoveToEx( HDC hdc, INT x, INT y, LPPOINT pt )
67 BOOL ret = TRUE;
68 DC * dc = DC_GetDCPtr( hdc );
70 if(!dc) return FALSE;
72 if(pt) {
73 pt->x = dc->CursPosX;
74 pt->y = dc->CursPosY;
76 dc->CursPosX = x;
77 dc->CursPosY = y;
79 if(PATH_IsPathOpen(dc->path)) ret = PATH_MoveTo(dc);
80 else if (dc->funcs->pMoveTo) ret = dc->funcs->pMoveTo(dc->physDev,x,y);
81 GDI_ReleaseObj( hdc );
82 return ret;
86 /***********************************************************************
87 * Arc (GDI32.@)
89 BOOL WINAPI Arc( HDC hdc, INT left, INT top, INT right,
90 INT bottom, INT xstart, INT ystart,
91 INT xend, INT yend )
93 BOOL ret = FALSE;
94 DC * dc = DC_GetDCUpdate( hdc );
95 if (dc)
97 if(PATH_IsPathOpen(dc->path))
98 ret = PATH_Arc(dc, left, top, right, bottom, xstart, ystart, xend, yend,0);
99 else if (dc->funcs->pArc)
100 ret = dc->funcs->pArc(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
101 GDI_ReleaseObj( hdc );
103 return ret;
106 /***********************************************************************
107 * ArcTo (GDI32.@)
109 BOOL WINAPI ArcTo( HDC hdc,
110 INT left, INT top,
111 INT right, INT bottom,
112 INT xstart, INT ystart,
113 INT xend, INT yend )
115 BOOL result;
116 DC * dc = DC_GetDCUpdate( hdc );
117 if(!dc) return FALSE;
119 if(dc->funcs->pArcTo)
121 result = dc->funcs->pArcTo( dc->physDev, left, top, right, bottom,
122 xstart, ystart, xend, yend );
123 GDI_ReleaseObj( hdc );
124 return result;
126 GDI_ReleaseObj( hdc );
128 * Else emulate it.
129 * According to the documentation, a line is drawn from the current
130 * position to the starting point of the arc.
132 LineTo(hdc, xstart, ystart);
134 * Then the arc is drawn.
136 result = Arc(hdc, left, top, right, bottom, xstart, ystart, xend, yend);
138 * If no error occurred, the current position is moved to the ending
139 * point of the arc.
141 if (result) MoveToEx(hdc, xend, yend, NULL);
142 return result;
146 /***********************************************************************
147 * Pie (GDI32.@)
149 BOOL WINAPI Pie( HDC hdc, INT left, INT top,
150 INT right, INT bottom, INT xstart, INT ystart,
151 INT xend, INT yend )
153 BOOL ret = FALSE;
154 DC * dc = DC_GetDCUpdate( hdc );
155 if (!dc) return FALSE;
157 if(PATH_IsPathOpen(dc->path))
158 ret = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,2);
159 else if(dc->funcs->pPie)
160 ret = dc->funcs->pPie(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
162 GDI_ReleaseObj( hdc );
163 return ret;
167 /***********************************************************************
168 * Chord (GDI32.@)
170 BOOL WINAPI Chord( HDC hdc, INT left, INT top,
171 INT right, INT bottom, INT xstart, INT ystart,
172 INT xend, INT yend )
174 BOOL ret = FALSE;
175 DC * dc = DC_GetDCUpdate( hdc );
176 if (!dc) return FALSE;
178 if(PATH_IsPathOpen(dc->path))
179 ret = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,1);
180 else if(dc->funcs->pChord)
181 ret = dc->funcs->pChord(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
183 GDI_ReleaseObj( hdc );
184 return ret;
188 /***********************************************************************
189 * Ellipse (GDI32.@)
191 BOOL WINAPI Ellipse( HDC hdc, INT left, INT top,
192 INT right, INT bottom )
194 BOOL ret = FALSE;
195 DC * dc = DC_GetDCUpdate( hdc );
196 if (!dc) return FALSE;
198 if(PATH_IsPathOpen(dc->path))
199 ret = PATH_Ellipse(dc,left,top,right,bottom);
200 else if (dc->funcs->pEllipse)
201 ret = dc->funcs->pEllipse(dc->physDev,left,top,right,bottom);
203 GDI_ReleaseObj( hdc );
204 return ret;
208 /***********************************************************************
209 * Rectangle (GDI32.@)
211 BOOL WINAPI Rectangle( HDC hdc, INT left, INT top,
212 INT right, INT bottom )
214 BOOL ret = FALSE;
215 DC * dc = DC_GetDCUpdate( hdc );
216 if (dc)
218 if(PATH_IsPathOpen(dc->path))
219 ret = PATH_Rectangle(dc, left, top, right, bottom);
220 else if (dc->funcs->pRectangle)
221 ret = dc->funcs->pRectangle(dc->physDev,left,top,right,bottom);
222 GDI_ReleaseObj( hdc );
224 return ret;
228 /***********************************************************************
229 * RoundRect (GDI32.@)
231 BOOL WINAPI RoundRect( HDC hdc, INT left, INT top, INT right,
232 INT bottom, INT ell_width, INT ell_height )
234 BOOL ret = FALSE;
235 DC *dc = DC_GetDCUpdate( hdc );
237 if (dc)
239 if(PATH_IsPathOpen(dc->path))
240 ret = PATH_RoundRect(dc,left,top,right,bottom,ell_width,ell_height);
241 else if (dc->funcs->pRoundRect)
242 ret = dc->funcs->pRoundRect(dc->physDev,left,top,right,bottom,ell_width,ell_height);
243 GDI_ReleaseObj( hdc );
245 return ret;
248 /***********************************************************************
249 * SetPixel (GDI32.@)
251 COLORREF WINAPI SetPixel( HDC hdc, INT x, INT y, COLORREF color )
253 COLORREF ret = 0;
254 DC * dc = DC_GetDCUpdate( hdc );
255 if (dc)
257 if (dc->funcs->pSetPixel) ret = dc->funcs->pSetPixel(dc->physDev,x,y,color);
258 GDI_ReleaseObj( hdc );
260 return ret;
263 /***********************************************************************
264 * SetPixelV (GDI32.@)
266 BOOL WINAPI SetPixelV( HDC hdc, INT x, INT y, COLORREF color )
268 BOOL ret = FALSE;
269 DC * dc = DC_GetDCUpdate( hdc );
270 if (dc)
272 if (dc->funcs->pSetPixel)
274 dc->funcs->pSetPixel(dc->physDev,x,y,color);
275 ret = TRUE;
277 GDI_ReleaseObj( hdc );
279 return ret;
282 /***********************************************************************
283 * GetPixel (GDI32.@)
285 COLORREF WINAPI GetPixel( HDC hdc, INT x, INT y )
287 COLORREF ret = CLR_INVALID;
288 DC * dc = DC_GetDCUpdate( hdc );
290 if (dc)
292 /* FIXME: should this be in the graphics driver? */
293 if (PtVisible( hdc, x, y ))
295 if (dc->funcs->pGetPixel) ret = dc->funcs->pGetPixel(dc->physDev,x,y);
297 GDI_ReleaseObj( hdc );
299 return ret;
303 /******************************************************************************
304 * ChoosePixelFormat [GDI32.@]
305 * Matches a pixel format to given format
307 * PARAMS
308 * hdc [I] Device context to search for best pixel match
309 * ppfd [I] Pixel format for which a match is sought
311 * RETURNS
312 * Success: Pixel format index closest to given format
313 * Failure: 0
315 INT WINAPI ChoosePixelFormat( HDC hdc, const LPPIXELFORMATDESCRIPTOR ppfd )
317 INT ret = 0;
318 DC * dc = DC_GetDCPtr( hdc );
320 TRACE("(%p,%p)\n",hdc,ppfd);
322 if (!dc) return 0;
324 if (!dc->funcs->pChoosePixelFormat) FIXME(" :stub\n");
325 else ret = dc->funcs->pChoosePixelFormat(dc->physDev,ppfd);
327 GDI_ReleaseObj( hdc );
328 return ret;
332 /******************************************************************************
333 * SetPixelFormat [GDI32.@]
334 * Sets pixel format of device context
336 * PARAMS
337 * hdc [I] Device context to search for best pixel match
338 * iPixelFormat [I] Pixel format index
339 * ppfd [I] Pixel format for which a match is sought
341 * RETURNS STD
343 BOOL WINAPI SetPixelFormat( HDC hdc, INT iPixelFormat,
344 const PIXELFORMATDESCRIPTOR *ppfd)
346 INT bRet = FALSE;
347 DC * dc = DC_GetDCPtr( hdc );
349 TRACE("(%p,%d,%p)\n",hdc,iPixelFormat,ppfd);
351 if (!dc) return 0;
353 if (!dc->funcs->pSetPixelFormat) FIXME(" :stub\n");
354 else bRet = dc->funcs->pSetPixelFormat(dc->physDev,iPixelFormat,ppfd);
356 GDI_ReleaseObj( hdc );
357 return bRet;
361 /******************************************************************************
362 * GetPixelFormat [GDI32.@]
363 * Gets index of pixel format of DC
365 * PARAMETERS
366 * hdc [I] Device context whose pixel format index is sought
368 * RETURNS
369 * Success: Currently selected pixel format
370 * Failure: 0
372 INT WINAPI GetPixelFormat( HDC hdc )
374 INT ret = 0;
375 DC * dc = DC_GetDCPtr( hdc );
377 TRACE("(%p)\n",hdc);
379 if (!dc) return 0;
381 if (!dc->funcs->pGetPixelFormat) FIXME(" :stub\n");
382 else ret = dc->funcs->pGetPixelFormat(dc->physDev);
384 GDI_ReleaseObj( hdc );
385 return ret;
389 /******************************************************************************
390 * DescribePixelFormat [GDI32.@]
391 * Gets info about pixel format from DC
393 * PARAMS
394 * hdc [I] Device context
395 * iPixelFormat [I] Pixel format selector
396 * nBytes [I] Size of buffer
397 * ppfd [O] Pointer to structure to receive pixel format data
399 * RETURNS
400 * Success: Maximum pixel format index of the device context
401 * Failure: 0
403 INT WINAPI DescribePixelFormat( HDC hdc, INT iPixelFormat, UINT nBytes,
404 LPPIXELFORMATDESCRIPTOR ppfd )
406 INT ret = 0;
407 DC * dc = DC_GetDCPtr( hdc );
409 TRACE("(%p,%d,%d,%p): stub\n",hdc,iPixelFormat,nBytes,ppfd);
411 if (!dc) return 0;
413 if (!dc->funcs->pDescribePixelFormat)
415 FIXME(" :stub\n");
416 ppfd->nSize = nBytes;
417 ppfd->nVersion = 1;
418 ret = 3;
420 else ret = dc->funcs->pDescribePixelFormat(dc->physDev,iPixelFormat,nBytes,ppfd);
422 GDI_ReleaseObj( hdc );
423 return ret;
427 /******************************************************************************
428 * SwapBuffers [GDI32.@]
429 * Exchanges front and back buffers of window
431 * PARAMS
432 * hdc [I] Device context whose buffers get swapped
434 * RETURNS STD
436 BOOL WINAPI SwapBuffers( HDC hdc )
438 INT bRet = FALSE;
439 DC * dc = DC_GetDCPtr( hdc );
441 TRACE("(%p)\n",hdc);
443 if (!dc) return TRUE;
445 if (!dc->funcs->pSwapBuffers)
447 FIXME(" :stub\n");
448 bRet = TRUE;
450 else bRet = dc->funcs->pSwapBuffers(dc->physDev);
452 GDI_ReleaseObj( hdc );
453 return bRet;
457 /***********************************************************************
458 * PaintRgn (GDI32.@)
460 BOOL WINAPI PaintRgn( HDC hdc, HRGN hrgn )
462 BOOL ret = FALSE;
463 DC * dc = DC_GetDCUpdate( hdc );
464 if (dc)
466 if (dc->funcs->pPaintRgn) ret = dc->funcs->pPaintRgn(dc->physDev,hrgn);
467 GDI_ReleaseObj( hdc );
469 return ret;
473 /***********************************************************************
474 * FillRgn (GDI32.@)
476 BOOL WINAPI FillRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush )
478 BOOL retval = FALSE;
479 HBRUSH prevBrush;
480 DC * dc = DC_GetDCUpdate( hdc );
482 if (!dc) return FALSE;
483 if(dc->funcs->pFillRgn)
484 retval = dc->funcs->pFillRgn(dc->physDev, hrgn, hbrush);
485 else if ((prevBrush = SelectObject( hdc, hbrush )))
487 retval = PaintRgn( hdc, hrgn );
488 SelectObject( hdc, prevBrush );
490 GDI_ReleaseObj( hdc );
491 return retval;
495 /***********************************************************************
496 * FrameRgn (GDI32.@)
498 BOOL WINAPI FrameRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush,
499 INT nWidth, INT nHeight )
501 BOOL ret = FALSE;
502 DC *dc = DC_GetDCUpdate( hdc );
504 if (!dc) return FALSE;
505 if(dc->funcs->pFrameRgn)
506 ret = dc->funcs->pFrameRgn( dc->physDev, hrgn, hbrush, nWidth, nHeight );
507 else
509 HRGN tmp = CreateRectRgn( 0, 0, 0, 0 );
510 if (tmp)
512 if (REGION_FrameRgn( tmp, hrgn, nWidth, nHeight ))
514 FillRgn( hdc, tmp, hbrush );
515 ret = TRUE;
517 DeleteObject( tmp );
520 GDI_ReleaseObj( hdc );
521 return ret;
525 /***********************************************************************
526 * InvertRgn (GDI32.@)
528 BOOL WINAPI InvertRgn( HDC hdc, HRGN hrgn )
530 HBRUSH prevBrush;
531 INT prevROP;
532 BOOL retval;
533 DC *dc = DC_GetDCUpdate( hdc );
534 if (!dc) return FALSE;
536 if(dc->funcs->pInvertRgn)
537 retval = dc->funcs->pInvertRgn( dc->physDev, hrgn );
538 else
540 prevBrush = SelectObject( hdc, GetStockObject(BLACK_BRUSH) );
541 prevROP = SetROP2( hdc, R2_NOT );
542 retval = PaintRgn( hdc, hrgn );
543 SelectObject( hdc, prevBrush );
544 SetROP2( hdc, prevROP );
546 GDI_ReleaseObj( hdc );
547 return retval;
551 /**********************************************************************
552 * Polyline (GDI32.@)
554 BOOL WINAPI Polyline( HDC hdc, const POINT* pt, INT count )
556 BOOL ret = FALSE;
557 DC * dc = DC_GetDCUpdate( hdc );
558 if (dc)
560 if (PATH_IsPathOpen(dc->path)) ret = PATH_Polyline(dc, pt, count);
561 else if (dc->funcs->pPolyline) ret = dc->funcs->pPolyline(dc->physDev,pt,count);
562 GDI_ReleaseObj( hdc );
564 return ret;
567 /**********************************************************************
568 * PolylineTo (GDI32.@)
570 BOOL WINAPI PolylineTo( HDC hdc, const POINT* pt, DWORD cCount )
572 DC * dc = DC_GetDCUpdate( hdc );
573 BOOL ret = FALSE;
575 if(!dc) return FALSE;
577 if(PATH_IsPathOpen(dc->path))
578 ret = PATH_PolylineTo(dc, pt, cCount);
580 else if(dc->funcs->pPolylineTo)
581 ret = dc->funcs->pPolylineTo(dc->physDev, pt, cCount);
583 else { /* do it using Polyline */
584 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
585 sizeof(POINT) * (cCount + 1) );
586 if (pts)
588 pts[0].x = dc->CursPosX;
589 pts[0].y = dc->CursPosY;
590 memcpy( pts + 1, pt, sizeof(POINT) * cCount );
591 ret = Polyline( hdc, pts, cCount + 1 );
592 HeapFree( GetProcessHeap(), 0, pts );
595 if(ret) {
596 dc->CursPosX = pt[cCount-1].x;
597 dc->CursPosY = pt[cCount-1].y;
599 GDI_ReleaseObj( hdc );
600 return ret;
604 /**********************************************************************
605 * Polygon (GDI32.@)
607 BOOL WINAPI Polygon( HDC hdc, const POINT* pt, INT count )
609 BOOL ret = FALSE;
610 DC * dc = DC_GetDCUpdate( hdc );
611 if (dc)
613 if (PATH_IsPathOpen(dc->path)) ret = PATH_Polygon(dc, pt, count);
614 else if (dc->funcs->pPolygon) ret = dc->funcs->pPolygon(dc->physDev,pt,count);
615 GDI_ReleaseObj( hdc );
617 return ret;
621 /**********************************************************************
622 * PolyPolygon (GDI32.@)
624 BOOL WINAPI PolyPolygon( HDC hdc, const POINT* pt, const INT* counts,
625 UINT polygons )
627 BOOL ret = FALSE;
628 DC * dc = DC_GetDCUpdate( hdc );
629 if (dc)
631 if (PATH_IsPathOpen(dc->path)) ret = PATH_PolyPolygon(dc, pt, counts, polygons);
632 else if (dc->funcs->pPolyPolygon) ret = dc->funcs->pPolyPolygon(dc->physDev,pt,counts,polygons);
633 GDI_ReleaseObj( hdc );
635 return ret;
638 /**********************************************************************
639 * PolyPolyline (GDI32.@)
641 BOOL WINAPI PolyPolyline( HDC hdc, const POINT* pt, const DWORD* counts,
642 DWORD polylines )
644 BOOL ret = FALSE;
645 DC * dc = DC_GetDCUpdate( hdc );
646 if (dc)
648 if (PATH_IsPathOpen(dc->path)) ret = PATH_PolyPolyline(dc, pt, counts, polylines);
649 else if (dc->funcs->pPolyPolyline) ret = dc->funcs->pPolyPolyline(dc->physDev,pt,counts,polylines);
650 GDI_ReleaseObj( hdc );
652 return ret;
655 /**********************************************************************
656 * ExtFloodFill (GDI32.@)
658 BOOL WINAPI ExtFloodFill( HDC hdc, INT x, INT y, COLORREF color,
659 UINT fillType )
661 BOOL ret = FALSE;
662 DC * dc = DC_GetDCUpdate( hdc );
663 if (dc)
665 if (dc->funcs->pExtFloodFill) ret = dc->funcs->pExtFloodFill(dc->physDev,x,y,color,fillType);
666 GDI_ReleaseObj( hdc );
668 return ret;
672 /**********************************************************************
673 * FloodFill (GDI32.@)
675 BOOL WINAPI FloodFill( HDC hdc, INT x, INT y, COLORREF color )
677 return ExtFloodFill( hdc, x, y, color, FLOODFILLBORDER );
681 /******************************************************************************
682 * PolyBezier [GDI32.@]
683 * Draws one or more Bezier curves
685 * PARAMS
686 * hDc [I] Handle to device context
687 * lppt [I] Pointer to endpoints and control points
688 * cPoints [I] Count of endpoints and control points
690 * RETURNS STD
692 BOOL WINAPI PolyBezier( HDC hdc, const POINT* lppt, DWORD cPoints )
694 BOOL ret = FALSE;
695 DC * dc = DC_GetDCUpdate( hdc );
697 if(!dc) return FALSE;
699 if(PATH_IsPathOpen(dc->path))
700 ret = PATH_PolyBezier(dc, lppt, cPoints);
701 else if (dc->funcs->pPolyBezier)
702 ret = dc->funcs->pPolyBezier(dc->physDev, lppt, cPoints);
703 else /* We'll convert it into line segments and draw them using Polyline */
705 POINT *Pts;
706 INT nOut;
708 if ((Pts = GDI_Bezier( lppt, cPoints, &nOut )))
710 TRACE("Pts = %p, no = %d\n", Pts, nOut);
711 ret = Polyline( dc->hSelf, Pts, nOut );
712 HeapFree( GetProcessHeap(), 0, Pts );
716 GDI_ReleaseObj( hdc );
717 return ret;
720 /******************************************************************************
721 * PolyBezierTo [GDI32.@]
722 * Draws one or more Bezier curves
724 * PARAMS
725 * hDc [I] Handle to device context
726 * lppt [I] Pointer to endpoints and control points
727 * cPoints [I] Count of endpoints and control points
729 * RETURNS STD
731 BOOL WINAPI PolyBezierTo( HDC hdc, const POINT* lppt, DWORD cPoints )
733 DC * dc = DC_GetDCUpdate( hdc );
734 BOOL ret;
736 if(!dc) return FALSE;
738 if(PATH_IsPathOpen(dc->path))
739 ret = PATH_PolyBezierTo(dc, lppt, cPoints);
740 else if(dc->funcs->pPolyBezierTo)
741 ret = dc->funcs->pPolyBezierTo(dc->physDev, lppt, cPoints);
742 else { /* We'll do it using PolyBezier */
743 POINT *pt;
744 pt = HeapAlloc( GetProcessHeap(), 0, sizeof(POINT) * (cPoints + 1) );
745 if(!pt) return FALSE;
746 pt[0].x = dc->CursPosX;
747 pt[0].y = dc->CursPosY;
748 memcpy(pt + 1, lppt, sizeof(POINT) * cPoints);
749 ret = PolyBezier(dc->hSelf, pt, cPoints+1);
750 HeapFree( GetProcessHeap(), 0, pt );
752 if(ret) {
753 dc->CursPosX = lppt[cPoints-1].x;
754 dc->CursPosY = lppt[cPoints-1].y;
756 GDI_ReleaseObj( hdc );
757 return ret;
760 /***********************************************************************
761 * AngleArc (GDI32.@)
763 BOOL WINAPI AngleArc(HDC hdc, INT x, INT y, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle)
765 INT x1,y1,x2,y2, arcdir;
766 BOOL result;
767 DC *dc;
769 if( (signed int)dwRadius < 0 )
770 return FALSE;
772 dc = DC_GetDCUpdate( hdc );
773 if(!dc) return FALSE;
775 if(dc->funcs->pAngleArc)
777 result = dc->funcs->pAngleArc( dc->physDev, x, y, dwRadius, eStartAngle, eSweepAngle );
779 GDI_ReleaseObj( hdc );
780 return result;
782 GDI_ReleaseObj( hdc );
784 /* AngleArc always works counterclockwise */
785 arcdir = GetArcDirection( hdc );
786 SetArcDirection( hdc, AD_COUNTERCLOCKWISE );
788 x1 = x + cos(eStartAngle*M_PI/180) * dwRadius;
789 y1 = y - sin(eStartAngle*M_PI/180) * dwRadius;
790 x2 = x + cos((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius;
791 y2 = x - sin((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius;
793 LineTo( hdc, x1, y1 );
794 if( eSweepAngle >= 0 )
795 result = Arc( hdc, x-dwRadius, y-dwRadius, x+dwRadius, y+dwRadius,
796 x1, y1, x2, y2 );
797 else
798 result = Arc( hdc, x-dwRadius, y-dwRadius, x+dwRadius, y+dwRadius,
799 x2, y2, x1, y1 );
801 if( result ) MoveToEx( hdc, x2, y2, NULL );
802 SetArcDirection( hdc, arcdir );
803 return result;
806 /***********************************************************************
807 * PolyDraw (GDI32.@)
809 BOOL WINAPI PolyDraw(HDC hdc, const POINT *lppt, const BYTE *lpbTypes,
810 DWORD cCount)
812 DC *dc;
813 BOOL result;
814 POINT lastmove;
815 int i;
817 dc = DC_GetDCUpdate( hdc );
818 if(!dc) return FALSE;
820 if(dc->funcs->pPolyDraw)
822 result = dc->funcs->pPolyDraw( dc->physDev, lppt, lpbTypes, cCount );
823 GDI_ReleaseObj( hdc );
824 return result;
826 GDI_ReleaseObj( hdc );
828 /* check for each bezierto if there are two more points */
829 for( i = 0; i < cCount; i++ )
830 if( lpbTypes[i] != PT_MOVETO &&
831 lpbTypes[i] & PT_BEZIERTO )
833 if( cCount < i+3 )
834 return FALSE;
835 else
836 i += 2;
839 /* if no moveto occurs, we will close the figure here */
840 lastmove.x = dc->CursPosX;
841 lastmove.y = dc->CursPosY;
843 /* now let's draw */
844 for( i = 0; i < cCount; i++ )
846 if( lpbTypes[i] == PT_MOVETO )
848 MoveToEx( hdc, lppt[i].x, lppt[i].y, NULL );
849 lastmove.x = dc->CursPosX;
850 lastmove.y = dc->CursPosY;
852 else if( lpbTypes[i] & PT_LINETO )
853 LineTo( hdc, lppt[i].x, lppt[i].y );
854 else if( lpbTypes[i] & PT_BEZIERTO )
856 PolyBezierTo( hdc, &lppt[i], 3 );
857 i += 2;
859 else
860 return FALSE;
862 if( lpbTypes[i] & PT_CLOSEFIGURE )
864 if( PATH_IsPathOpen( dc->path ) )
865 CloseFigure( hdc );
866 else
867 LineTo( hdc, lastmove.x, lastmove.y );
871 return TRUE;
874 /******************************************************************
876 * *Very* simple bezier drawing code,
878 * It uses a recursive algorithm to divide the curve in a series
879 * of straight line segements. Not ideal but for me sufficient.
880 * If you are in need for something better look for some incremental
881 * algorithm.
883 * 7 July 1998 Rein Klazes
887 * some macro definitions for bezier drawing
889 * to avoid truncation errors the coordinates are
890 * shifted upwards. When used in drawing they are
891 * shifted down again, including correct rounding
892 * and avoiding floating point arithmetic
893 * 4 bits should allow 27 bits coordinates which I saw
894 * somewhere in the win32 doc's
898 #define BEZIERSHIFTBITS 4
899 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
900 #define BEZIERPIXEL BEZIERSHIFTUP(1)
901 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
902 /* maximum depth of recursion */
903 #define BEZIERMAXDEPTH 8
905 /* size of array to store points on */
906 /* enough for one curve */
907 #define BEZIER_INITBUFSIZE (150)
909 /* calculate Bezier average, in this case the middle
910 * correctly rounded...
911 * */
913 #define BEZIERMIDDLE(Mid, P1, P2) \
914 (Mid).x=((P1).x+(P2).x + 1)/2;\
915 (Mid).y=((P1).y+(P2).y + 1)/2;
917 /**********************************************************
918 * BezierCheck helper function to check
919 * that recursion can be terminated
920 * Points[0] and Points[3] are begin and endpoint
921 * Points[1] and Points[2] are control points
922 * level is the recursion depth
923 * returns true if the recusion can be terminated
925 static BOOL BezierCheck( int level, POINT *Points)
927 INT dx, dy;
928 dx=Points[3].x-Points[0].x;
929 dy=Points[3].y-Points[0].y;
930 if(abs(dy)<=abs(dx)){/* shallow line */
931 /* check that control points are between begin and end */
932 if(Points[1].x < Points[0].x){
933 if(Points[1].x < Points[3].x)
934 return FALSE;
935 }else
936 if(Points[1].x > Points[3].x)
937 return FALSE;
938 if(Points[2].x < Points[0].x){
939 if(Points[2].x < Points[3].x)
940 return FALSE;
941 }else
942 if(Points[2].x > Points[3].x)
943 return FALSE;
944 dx=BEZIERSHIFTDOWN(dx);
945 if(!dx) return TRUE;
946 if(abs(Points[1].y-Points[0].y-(dy/dx)*
947 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
948 abs(Points[2].y-Points[0].y-(dy/dx)*
949 BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
950 return FALSE;
951 else
952 return TRUE;
953 }else{ /* steep line */
954 /* check that control points are between begin and end */
955 if(Points[1].y < Points[0].y){
956 if(Points[1].y < Points[3].y)
957 return FALSE;
958 }else
959 if(Points[1].y > Points[3].y)
960 return FALSE;
961 if(Points[2].y < Points[0].y){
962 if(Points[2].y < Points[3].y)
963 return FALSE;
964 }else
965 if(Points[2].y > Points[3].y)
966 return FALSE;
967 dy=BEZIERSHIFTDOWN(dy);
968 if(!dy) return TRUE;
969 if(abs(Points[1].x-Points[0].x-(dx/dy)*
970 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
971 abs(Points[2].x-Points[0].x-(dx/dy)*
972 BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
973 return FALSE;
974 else
975 return TRUE;
979 /* Helper for GDI_Bezier.
980 * Just handles one Bezier, so Points should point to four POINTs
982 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
983 INT *nPtsOut, INT level )
985 if(*nPtsOut == *dwOut) {
986 *dwOut *= 2;
987 *PtsOut = HeapReAlloc( GetProcessHeap(), 0, *PtsOut,
988 *dwOut * sizeof(POINT) );
991 if(!level || BezierCheck(level, Points)) {
992 if(*nPtsOut == 0) {
993 (*PtsOut)[0].x = BEZIERSHIFTDOWN(Points[0].x);
994 (*PtsOut)[0].y = BEZIERSHIFTDOWN(Points[0].y);
995 *nPtsOut = 1;
997 (*PtsOut)[*nPtsOut].x = BEZIERSHIFTDOWN(Points[3].x);
998 (*PtsOut)[*nPtsOut].y = BEZIERSHIFTDOWN(Points[3].y);
999 (*nPtsOut) ++;
1000 } else {
1001 POINT Points2[4]; /* for the second recursive call */
1002 Points2[3]=Points[3];
1003 BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
1004 BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
1005 BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
1007 BEZIERMIDDLE(Points[1], Points[0], Points[1]);
1008 BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1009 BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1011 Points2[0]=Points[3];
1013 /* do the two halves */
1014 GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
1015 GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
1021 /***********************************************************************
1022 * GDI_Bezier [INTERNAL]
1023 * Calculate line segments that approximate -what microsoft calls- a bezier
1024 * curve.
1025 * The routine recursively divides the curve in two parts until a straight
1026 * line can be drawn
1028 * PARAMS
1030 * Points [I] Ptr to count POINTs which are the end and control points
1031 * of the set of Bezier curves to flatten.
1032 * count [I] Number of Points. Must be 3n+1.
1033 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
1034 * lines+1).
1036 * RETURNS
1038 * Ptr to an array of POINTs that contain the lines that approximinate the
1039 * Beziers. The array is allocated on the process heap and it is the caller's
1040 * responsibility to HeapFree it. [this is not a particularly nice interface
1041 * but since we can't know in advance how many points will generate, the
1042 * alternative would be to call the function twice, once to determine the size
1043 * and a second time to do the work - I decided this was too much of a pain].
1045 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
1047 POINT *out;
1048 INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
1050 if((count - 1) % 3 != 0) {
1051 ERR("Invalid no. of points\n");
1052 return NULL;
1054 *nPtsOut = 0;
1055 out = HeapAlloc( GetProcessHeap(), 0, dwOut * sizeof(POINT));
1056 for(Bezier = 0; Bezier < (count-1)/3; Bezier++) {
1057 POINT ptBuf[4];
1058 memcpy(ptBuf, Points + Bezier * 3, sizeof(POINT) * 4);
1059 for(i = 0; i < 4; i++) {
1060 ptBuf[i].x = BEZIERSHIFTUP(ptBuf[i].x);
1061 ptBuf[i].y = BEZIERSHIFTUP(ptBuf[i].y);
1063 GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
1065 TRACE("Produced %d points\n", *nPtsOut);
1066 return out;
1069 /******************************************************************************
1070 * GradientFill (GDI32.@)
1072 BOOL WINAPI GdiGradientFill( HDC hdc, TRIVERTEX *vert_array, ULONG nvert,
1073 void * grad_array, ULONG ngrad, ULONG mode )
1075 int i;
1077 TRACE("vert_array:0x%08lx nvert:%ld grad_array:0x%08lx ngrad:%ld\n",
1078 (long)vert_array, nvert, (long)grad_array, ngrad);
1080 switch(mode)
1082 case GRADIENT_FILL_RECT_H:
1083 for(i = 0; i < ngrad; i++)
1085 GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1086 TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1087 TRIVERTEX *v2 = vert_array + rect->LowerRight;
1088 int y1 = v1->y < v2->y ? v1->y : v2->y;
1089 int y2 = v2->y > v1->y ? v2->y : v1->y;
1090 int x, y, dx;
1091 if (v1->x > v2->x)
1093 TRIVERTEX *t = v2;
1094 v2 = v1;
1095 v1 = t;
1097 dx = v2->x - v1->x;
1098 for (x = 0; x < dx; x++)
1100 for (y = y1; y < y2; y++)
1101 SetPixel (hdc, x + v1->x, y, RGB(
1102 (v1->Red * (dx - x) + v2->Red * x) / dx >> 8,
1103 (v1->Green * (dx - x) + v2->Green * x) / dx >> 8,
1104 (v1->Blue * (dx - x) + v2->Blue * x) / dx >> 8));
1107 break;
1108 case GRADIENT_FILL_RECT_V:
1109 for(i = 0; i < ngrad; i++)
1111 GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1112 TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1113 TRIVERTEX *v2 = vert_array + rect->LowerRight;
1114 int x1 = v1->x < v2->x ? v1->x : v2->x;
1115 int x2 = v2->x > v1->x ? v2->x : v1->x;
1116 int x, y, dy;
1117 if (v1->y > v2->y)
1119 TRIVERTEX *t = v2;
1120 v2 = v1;
1121 v1 = t;
1123 dy = v2->y - v1->y;
1124 for (y = 0; y < dy; y++)
1126 for (x = x1; x < x2; x++)
1127 SetPixel (hdc, x, y + v1->y, RGB(
1128 (v1->Red * (dy - y) + v2->Red * y) / dy >> 8,
1129 (v1->Green * (dy - y) + v2->Green * y) / dy >> 8,
1130 (v1->Blue * (dy - y) + v2->Blue * y) / dy >> 8));
1133 break;
1134 case GRADIENT_FILL_TRIANGLE:
1135 for (i = 0; i < ngrad; i++)
1137 GRADIENT_TRIANGLE *tri = ((GRADIENT_TRIANGLE *)grad_array) + i;
1138 TRIVERTEX *v1 = vert_array + tri->Vertex1;
1139 TRIVERTEX *v2 = vert_array + tri->Vertex2;
1140 TRIVERTEX *v3 = vert_array + tri->Vertex3;
1141 int y, dy;
1143 if (v1->y > v2->y)
1144 { TRIVERTEX *t = v1; v1 = v2; v2 = t; }
1145 if (v2->y > v3->y)
1147 TRIVERTEX *t = v2; v2 = v3; v3 = t;
1148 if (v1->y > v2->y)
1149 { t = v1; v1 = v2; v2 = t; }
1151 /* v1->y <= v2->y <= v3->y */
1153 dy = v3->y - v1->y;
1154 for (y = 0; y < dy; y++)
1156 /* v1->y <= y < v3->y */
1157 TRIVERTEX *v = y < (v2->y - v1->y) ? v1 : v3;
1158 /* (v->y <= y < v2->y) || (v2->y <= y < v->y) */
1159 int dy2 = v2->y - v->y;
1160 int y2 = y + v1->y - v->y;
1162 int x1 = (v3->x * y + v1->x * (dy - y )) / dy;
1163 int x2 = (v2->x * y2 + v-> x * (dy2 - y2)) / dy2;
1164 int r1 = (v3->Red * y + v1->Red * (dy - y )) / dy;
1165 int r2 = (v2->Red * y2 + v-> Red * (dy2 - y2)) / dy2;
1166 int g1 = (v3->Green * y + v1->Green * (dy - y )) / dy;
1167 int g2 = (v2->Green * y2 + v-> Green * (dy2 - y2)) / dy2;
1168 int b1 = (v3->Blue * y + v1->Blue * (dy - y )) / dy;
1169 int b2 = (v2->Blue * y2 + v-> Blue * (dy2 - y2)) / dy2;
1171 int x;
1172 if (x1 < x2)
1174 int dx = x2 - x1;
1175 for (x = 0; x < dx; x++)
1176 SetPixel (hdc, x + x1, y + v1->y, RGB(
1177 (r1 * (dx - x) + r2 * x) / dx >> 8,
1178 (g1 * (dx - x) + g2 * x) / dx >> 8,
1179 (b1 * (dx - x) + b2 * x) / dx >> 8));
1181 else
1183 int dx = x1 - x2;
1184 for (x = 0; x < dx; x++)
1185 SetPixel (hdc, x + x2, y + v1->y, RGB(
1186 (r2 * (dx - x) + r1 * x) / dx >> 8,
1187 (g2 * (dx - x) + g1 * x) / dx >> 8,
1188 (b2 * (dx - x) + b1 * x) / dx >> 8));
1192 break;
1193 default:
1194 return FALSE;
1197 return TRUE;