- beginning of vertex matrix blending using extensions (currently
[wine] / graphics / painting.c
1 /*
2  * GDI drawing functions.
3  *
4  * Copyright 1993, 1994 Alexandre Julliard
5  * Copyright 1997 Bertho A. Stultiens
6  *           1999 Huw D M Davies
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <string.h>
27 #include <stdlib.h>
28
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"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
37
38
39 /***********************************************************************
40  *           LineTo    (GDI32.@)
41  */
42 BOOL WINAPI LineTo( HDC hdc, INT x, INT y )
43 {
44     DC * dc = DC_GetDCUpdate( hdc );
45     BOOL ret;
46
47     if(!dc) return FALSE;
48
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;
56     }
57     GDI_ReleaseObj( hdc );
58     return ret;
59 }
60
61
62 /***********************************************************************
63  *           MoveToEx    (GDI32.@)
64  */
65 BOOL WINAPI MoveToEx( HDC hdc, INT x, INT y, LPPOINT pt )
66 {
67     BOOL ret = TRUE;
68     DC * dc = DC_GetDCPtr( hdc );
69
70     if(!dc) return FALSE;
71
72     if(pt) {
73         pt->x = dc->CursPosX;
74         pt->y = dc->CursPosY;
75     }
76     dc->CursPosX = x;
77     dc->CursPosY = y;
78
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;
83 }
84
85
86 /***********************************************************************
87  *           Arc    (GDI32.@)
88  */
89 BOOL WINAPI Arc( HDC hdc, INT left, INT top, INT right,
90                      INT bottom, INT xstart, INT ystart,
91                      INT xend, INT yend )
92 {
93     BOOL ret = FALSE;
94     DC * dc = DC_GetDCUpdate( hdc );
95     if (dc)
96     {
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 );
102     }
103     return ret;
104 }
105
106 /***********************************************************************
107  *           ArcTo    (GDI32.@)
108  */
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 )
114 {
115     BOOL result;
116     DC * dc = DC_GetDCUpdate( hdc );
117     if(!dc) return FALSE;
118
119     if(dc->funcs->pArcTo)
120     {
121         result = dc->funcs->pArcTo( dc->physDev, left, top, right, bottom,
122                                   xstart, ystart, xend, yend );
123         GDI_ReleaseObj( hdc );
124         return result;
125     }
126     GDI_ReleaseObj( hdc );
127     /*
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.
131      */
132     LineTo(hdc, xstart, ystart);
133     /*
134      * Then the arc is drawn.
135      */
136     result = Arc(hdc, left, top, right, bottom, xstart, ystart, xend, yend);
137     /*
138      * If no error occurred, the current position is moved to the ending
139      * point of the arc.
140      */
141     if (result) MoveToEx(hdc, xend, yend, NULL);
142     return result;
143 }
144
145
146 /***********************************************************************
147  *           Pie   (GDI32.@)
148  */
149 BOOL WINAPI Pie( HDC hdc, INT left, INT top,
150                      INT right, INT bottom, INT xstart, INT ystart,
151                      INT xend, INT yend )
152 {
153     BOOL ret = FALSE;
154     DC * dc = DC_GetDCUpdate( hdc );
155     if (!dc) return FALSE;
156
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);
161
162     GDI_ReleaseObj( hdc );
163     return ret;
164 }
165
166
167 /***********************************************************************
168  *           Chord    (GDI32.@)
169  */
170 BOOL WINAPI Chord( HDC hdc, INT left, INT top,
171                        INT right, INT bottom, INT xstart, INT ystart,
172                        INT xend, INT yend )
173 {
174     BOOL ret = FALSE;
175     DC * dc = DC_GetDCUpdate( hdc );
176     if (!dc) return FALSE;
177
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);
182
183     GDI_ReleaseObj( hdc );
184     return ret;
185 }
186
187
188 /***********************************************************************
189  *           Ellipse    (GDI32.@)
190  */
191 BOOL WINAPI Ellipse( HDC hdc, INT left, INT top,
192                          INT right, INT bottom )
193 {
194     BOOL ret = FALSE;
195     DC * dc = DC_GetDCUpdate( hdc );
196     if (!dc) return FALSE;
197
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);
202
203     GDI_ReleaseObj( hdc );
204     return ret;
205 }
206
207
208 /***********************************************************************
209  *           Rectangle    (GDI32.@)
210  */
211 BOOL WINAPI Rectangle( HDC hdc, INT left, INT top,
212                            INT right, INT bottom )
213 {
214     BOOL ret = FALSE;
215     DC * dc = DC_GetDCUpdate( hdc );
216     if (dc)
217     {
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 );
223     }
224     return ret;
225 }
226
227
228 /***********************************************************************
229  *           RoundRect    (GDI32.@)
230  */
231 BOOL WINAPI RoundRect( HDC hdc, INT left, INT top, INT right,
232                            INT bottom, INT ell_width, INT ell_height )
233 {
234     BOOL ret = FALSE;
235     DC *dc = DC_GetDCUpdate( hdc );
236
237     if (dc)
238     {
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 );
244     }
245     return ret;
246 }
247
248 /***********************************************************************
249  *           SetPixel    (GDI32.@)
250  */
251 COLORREF WINAPI SetPixel( HDC hdc, INT x, INT y, COLORREF color )
252 {
253     COLORREF ret = 0;
254     DC * dc = DC_GetDCUpdate( hdc );
255     if (dc)
256     {
257         if (dc->funcs->pSetPixel) ret = dc->funcs->pSetPixel(dc->physDev,x,y,color);
258         GDI_ReleaseObj( hdc );
259     }
260     return ret;
261 }
262
263 /***********************************************************************
264  *           SetPixelV    (GDI32.@)
265  */
266 BOOL WINAPI SetPixelV( HDC hdc, INT x, INT y, COLORREF color )
267 {
268     BOOL ret = FALSE;
269     DC * dc = DC_GetDCUpdate( hdc );
270     if (dc)
271     {
272         if (dc->funcs->pSetPixel)
273         {
274             dc->funcs->pSetPixel(dc->physDev,x,y,color);
275             ret = TRUE;
276         }
277         GDI_ReleaseObj( hdc );
278     }
279     return ret;
280 }
281
282 /***********************************************************************
283  *           GetPixel    (GDI32.@)
284  */
285 COLORREF WINAPI GetPixel( HDC hdc, INT x, INT y )
286 {
287     COLORREF ret = CLR_INVALID;
288     DC * dc = DC_GetDCUpdate( hdc );
289
290     if (dc)
291     {
292     /* FIXME: should this be in the graphics driver? */
293         if (PtVisible( hdc, x, y ))
294         {
295             if (dc->funcs->pGetPixel) ret = dc->funcs->pGetPixel(dc->physDev,x,y);
296         }
297         GDI_ReleaseObj( hdc );
298     }
299     return ret;
300 }
301
302
303 /******************************************************************************
304  * ChoosePixelFormat [GDI32.@]
305  * Matches a pixel format to given format
306  *
307  * PARAMS
308  *    hdc  [I] Device context to search for best pixel match
309  *    ppfd [I] Pixel format for which a match is sought
310  *
311  * RETURNS
312  *    Success: Pixel format index closest to given format
313  *    Failure: 0
314  */
315 INT WINAPI ChoosePixelFormat( HDC hdc, const LPPIXELFORMATDESCRIPTOR ppfd )
316 {
317     INT ret = 0;
318     DC * dc = DC_GetDCPtr( hdc );
319
320     TRACE("(%p,%p)\n",hdc,ppfd);
321
322     if (!dc) return 0;
323
324     if (!dc->funcs->pChoosePixelFormat) FIXME(" :stub\n");
325     else ret = dc->funcs->pChoosePixelFormat(dc->physDev,ppfd);
326
327     GDI_ReleaseObj( hdc );
328     return ret;
329 }
330
331
332 /******************************************************************************
333  * SetPixelFormat [GDI32.@]
334  * Sets pixel format of device context
335  *
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
340  *
341  * RETURNS STD
342  */
343 BOOL WINAPI SetPixelFormat( HDC hdc, INT iPixelFormat,
344                             const PIXELFORMATDESCRIPTOR *ppfd)
345 {
346     INT bRet = FALSE;
347     DC * dc = DC_GetDCPtr( hdc );
348
349     TRACE("(%p,%d,%p)\n",hdc,iPixelFormat,ppfd);
350
351     if (!dc) return 0;
352
353     if (!dc->funcs->pSetPixelFormat) FIXME(" :stub\n");
354     else bRet = dc->funcs->pSetPixelFormat(dc->physDev,iPixelFormat,ppfd);
355
356     GDI_ReleaseObj( hdc );
357     return bRet;
358 }
359
360
361 /******************************************************************************
362  * GetPixelFormat [GDI32.@]
363  * Gets index of pixel format of DC
364  *
365  * PARAMETERS
366  *    hdc [I] Device context whose pixel format index is sought
367  *
368  * RETURNS
369  *    Success: Currently selected pixel format
370  *    Failure: 0
371  */
372 INT WINAPI GetPixelFormat( HDC hdc )
373 {
374     INT ret = 0;
375     DC * dc = DC_GetDCPtr( hdc );
376
377     TRACE("(%p)\n",hdc);
378
379     if (!dc) return 0;
380
381     if (!dc->funcs->pGetPixelFormat) FIXME(" :stub\n");
382     else ret = dc->funcs->pGetPixelFormat(dc->physDev);
383
384     GDI_ReleaseObj( hdc );
385     return ret;
386 }
387
388
389 /******************************************************************************
390  * DescribePixelFormat [GDI32.@]
391  * Gets info about pixel format from DC
392  *
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
398  *
399  * RETURNS
400  *    Success: Maximum pixel format index of the device context
401  *    Failure: 0
402  */
403 INT WINAPI DescribePixelFormat( HDC hdc, INT iPixelFormat, UINT nBytes,
404                                 LPPIXELFORMATDESCRIPTOR ppfd )
405 {
406     INT ret = 0;
407     DC * dc = DC_GetDCPtr( hdc );
408
409     TRACE("(%p,%d,%d,%p): stub\n",hdc,iPixelFormat,nBytes,ppfd);
410
411     if (!dc) return 0;
412
413     if (!dc->funcs->pDescribePixelFormat)
414     {
415         FIXME(" :stub\n");
416         ppfd->nSize = nBytes;
417         ppfd->nVersion = 1;
418         ret = 3;
419     }
420     else ret = dc->funcs->pDescribePixelFormat(dc->physDev,iPixelFormat,nBytes,ppfd);
421
422     GDI_ReleaseObj( hdc );
423     return ret;
424 }
425
426
427 /******************************************************************************
428  * SwapBuffers [GDI32.@]
429  * Exchanges front and back buffers of window
430  *
431  * PARAMS
432  *    hdc [I] Device context whose buffers get swapped
433  *
434  * RETURNS STD
435  */
436 BOOL WINAPI SwapBuffers( HDC hdc )
437 {
438     INT bRet = FALSE;
439     DC * dc = DC_GetDCPtr( hdc );
440
441     TRACE("(%p)\n",hdc);
442
443     if (!dc) return TRUE;
444
445     if (!dc->funcs->pSwapBuffers)
446     {
447         FIXME(" :stub\n");
448         bRet = TRUE;
449     }
450     else bRet = dc->funcs->pSwapBuffers(dc->physDev);
451
452     GDI_ReleaseObj( hdc );
453     return bRet;
454 }
455
456
457 /***********************************************************************
458  *           PaintRgn    (GDI32.@)
459  */
460 BOOL WINAPI PaintRgn( HDC hdc, HRGN hrgn )
461 {
462     BOOL ret = FALSE;
463     DC * dc = DC_GetDCUpdate( hdc );
464     if (dc)
465     {
466         if (dc->funcs->pPaintRgn) ret = dc->funcs->pPaintRgn(dc->physDev,hrgn);
467         GDI_ReleaseObj( hdc );
468     }
469     return ret;
470 }
471
472
473 /***********************************************************************
474  *           FillRgn    (GDI32.@)
475  */
476 BOOL WINAPI FillRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush )
477 {
478     BOOL retval = FALSE;
479     HBRUSH prevBrush;
480     DC * dc = DC_GetDCUpdate( hdc );
481
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 )))
486     {
487     retval = PaintRgn( hdc, hrgn );
488     SelectObject( hdc, prevBrush );
489     }
490     GDI_ReleaseObj( hdc );
491     return retval;
492 }
493
494
495 /***********************************************************************
496  *           FrameRgn     (GDI32.@)
497  */
498 BOOL WINAPI FrameRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush,
499                           INT nWidth, INT nHeight )
500 {
501     BOOL ret = FALSE;
502     DC *dc = DC_GetDCUpdate( hdc );
503
504     if (!dc) return FALSE;
505     if(dc->funcs->pFrameRgn)
506         ret = dc->funcs->pFrameRgn( dc->physDev, hrgn, hbrush, nWidth, nHeight );
507     else
508     {
509         HRGN tmp = CreateRectRgn( 0, 0, 0, 0 );
510         if (tmp)
511         {
512             if (REGION_FrameRgn( tmp, hrgn, nWidth, nHeight ))
513             {
514                 FillRgn( hdc, tmp, hbrush );
515                 ret = TRUE;
516             }
517             DeleteObject( tmp );
518         }
519     }
520     GDI_ReleaseObj( hdc );
521     return ret;
522 }
523
524
525 /***********************************************************************
526  *           InvertRgn    (GDI32.@)
527  */
528 BOOL WINAPI InvertRgn( HDC hdc, HRGN hrgn )
529 {
530     HBRUSH prevBrush;
531     INT prevROP;
532     BOOL retval;
533     DC *dc = DC_GetDCUpdate( hdc );
534     if (!dc) return FALSE;
535
536     if(dc->funcs->pInvertRgn)
537         retval = dc->funcs->pInvertRgn( dc->physDev, hrgn );
538     else
539     {
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 );
545     }
546     GDI_ReleaseObj( hdc );
547     return retval;
548 }
549
550
551 /**********************************************************************
552  *          Polyline   (GDI32.@)
553  */
554 BOOL WINAPI Polyline( HDC hdc, const POINT* pt, INT count )
555 {
556     BOOL ret = FALSE;
557     DC * dc = DC_GetDCUpdate( hdc );
558     if (dc)
559     {
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 );
563     }
564     return ret;
565 }
566
567 /**********************************************************************
568  *          PolylineTo   (GDI32.@)
569  */
570 BOOL WINAPI PolylineTo( HDC hdc, const POINT* pt, DWORD cCount )
571 {
572     DC * dc = DC_GetDCUpdate( hdc );
573     BOOL ret = FALSE;
574
575     if(!dc) return FALSE;
576
577     if(PATH_IsPathOpen(dc->path))
578         ret = PATH_PolylineTo(dc, pt, cCount);
579
580     else if(dc->funcs->pPolylineTo)
581         ret = dc->funcs->pPolylineTo(dc->physDev, pt, cCount);
582
583     else { /* do it using Polyline */
584         POINT *pts = HeapAlloc( GetProcessHeap(), 0,
585                                 sizeof(POINT) * (cCount + 1) );
586         if (pts)
587         {
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 );
593     }
594     }
595     if(ret) {
596         dc->CursPosX = pt[cCount-1].x;
597         dc->CursPosY = pt[cCount-1].y;
598     }
599     GDI_ReleaseObj( hdc );
600     return ret;
601 }
602
603
604 /**********************************************************************
605  *          Polygon  (GDI32.@)
606  */
607 BOOL WINAPI Polygon( HDC hdc, const POINT* pt, INT count )
608 {
609     BOOL ret = FALSE;
610     DC * dc = DC_GetDCUpdate( hdc );
611     if (dc)
612     {
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 );
616     }
617     return ret;
618 }
619
620
621 /**********************************************************************
622  *          PolyPolygon  (GDI32.@)
623  */
624 BOOL WINAPI PolyPolygon( HDC hdc, const POINT* pt, const INT* counts,
625                              UINT polygons )
626 {
627     BOOL ret = FALSE;
628     DC * dc = DC_GetDCUpdate( hdc );
629     if (dc)
630     {
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 );
634     }
635     return ret;
636 }
637
638 /**********************************************************************
639  *          PolyPolyline  (GDI32.@)
640  */
641 BOOL WINAPI PolyPolyline( HDC hdc, const POINT* pt, const DWORD* counts,
642                             DWORD polylines )
643 {
644     BOOL ret = FALSE;
645     DC * dc = DC_GetDCUpdate( hdc );
646     if (dc)
647     {
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 );
651     }
652     return ret;
653 }
654
655 /**********************************************************************
656  *          ExtFloodFill   (GDI32.@)
657  */
658 BOOL WINAPI ExtFloodFill( HDC hdc, INT x, INT y, COLORREF color,
659                               UINT fillType )
660 {
661     BOOL ret = FALSE;
662     DC * dc = DC_GetDCUpdate( hdc );
663     if (dc)
664     {
665         if (dc->funcs->pExtFloodFill) ret = dc->funcs->pExtFloodFill(dc->physDev,x,y,color,fillType);
666         GDI_ReleaseObj( hdc );
667     }
668     return ret;
669 }
670
671
672 /**********************************************************************
673  *          FloodFill   (GDI32.@)
674  */
675 BOOL WINAPI FloodFill( HDC hdc, INT x, INT y, COLORREF color )
676 {
677     return ExtFloodFill( hdc, x, y, color, FLOODFILLBORDER );
678 }
679
680
681 /******************************************************************************
682  * PolyBezier [GDI32.@]
683  * Draws one or more Bezier curves
684  *
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
689  *
690  * RETURNS STD
691  */
692 BOOL WINAPI PolyBezier( HDC hdc, const POINT* lppt, DWORD cPoints )
693 {
694     BOOL ret = FALSE;
695     DC * dc = DC_GetDCUpdate( hdc );
696
697     if(!dc) return FALSE;
698
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 */
704     {
705         POINT *Pts;
706         INT nOut;
707
708         if ((Pts = GDI_Bezier( lppt, cPoints, &nOut )))
709         {
710             TRACE("Pts = %p, no = %d\n", Pts, nOut);
711             ret = Polyline( dc->hSelf, Pts, nOut );
712             HeapFree( GetProcessHeap(), 0, Pts );
713         }
714     }
715
716     GDI_ReleaseObj( hdc );
717     return ret;
718 }
719
720 /******************************************************************************
721  * PolyBezierTo [GDI32.@]
722  * Draws one or more Bezier curves
723  *
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
728  *
729  * RETURNS STD
730  */
731 BOOL WINAPI PolyBezierTo( HDC hdc, const POINT* lppt, DWORD cPoints )
732 {
733     DC * dc = DC_GetDCUpdate( hdc );
734     BOOL ret;
735
736     if(!dc) return FALSE;
737
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 );
751     }
752     if(ret) {
753         dc->CursPosX = lppt[cPoints-1].x;
754         dc->CursPosY = lppt[cPoints-1].y;
755     }
756     GDI_ReleaseObj( hdc );
757     return ret;
758 }
759
760 /***********************************************************************
761  *      AngleArc (GDI32.@)
762  */
763 BOOL WINAPI AngleArc(HDC hdc, INT x, INT y, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle)
764 {
765     INT x1,y1,x2,y2, arcdir;
766     BOOL result;
767     DC *dc;
768
769     if( (signed int)dwRadius < 0 )
770         return FALSE;
771
772     dc = DC_GetDCUpdate( hdc );
773     if(!dc) return FALSE;
774
775     if(dc->funcs->pAngleArc)
776     {
777         result = dc->funcs->pAngleArc( dc->physDev, x, y, dwRadius, eStartAngle, eSweepAngle );
778
779         GDI_ReleaseObj( hdc );
780         return result;
781     }
782     GDI_ReleaseObj( hdc );
783
784     /* AngleArc always works counterclockwise */
785     arcdir = GetArcDirection( hdc );
786     SetArcDirection( hdc, AD_COUNTERCLOCKWISE );
787
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;
792
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 );
800
801     if( result ) MoveToEx( hdc, x2, y2, NULL );
802     SetArcDirection( hdc, arcdir );
803     return result;
804 }
805
806 /***********************************************************************
807  *      PolyDraw (GDI32.@)
808  */
809 BOOL WINAPI PolyDraw(HDC hdc, const POINT *lppt, const BYTE *lpbTypes,
810                        DWORD cCount)
811 {
812     DC *dc;
813     BOOL result;
814     POINT lastmove;
815     int i;
816
817     dc = DC_GetDCUpdate( hdc );
818     if(!dc) return FALSE;
819
820     if(dc->funcs->pPolyDraw)
821     {
822         result = dc->funcs->pPolyDraw( dc->physDev, lppt, lpbTypes, cCount );
823         GDI_ReleaseObj( hdc );
824         return result;
825     }
826     GDI_ReleaseObj( hdc );
827
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 )
832         {
833             if( cCount < i+3 )
834                 return FALSE;
835             else
836                 i += 2;
837         }
838
839     /* if no moveto occurs, we will close the figure here */
840     lastmove.x = dc->CursPosX;
841     lastmove.y = dc->CursPosY;
842
843     /* now let's draw */
844     for( i = 0; i < cCount; i++ )
845     {
846         if( lpbTypes[i] == PT_MOVETO )
847         {
848             MoveToEx( hdc, lppt[i].x, lppt[i].y, NULL );
849             lastmove.x = dc->CursPosX;
850             lastmove.y = dc->CursPosY;
851         }
852         else if( lpbTypes[i] & PT_LINETO )
853             LineTo( hdc, lppt[i].x, lppt[i].y );
854         else if( lpbTypes[i] & PT_BEZIERTO )
855         {
856             PolyBezierTo( hdc, &lppt[i], 3 );
857             i += 2;
858         }
859         else
860             return FALSE;
861
862         if( lpbTypes[i] & PT_CLOSEFIGURE )
863         {
864             if( PATH_IsPathOpen( dc->path ) )
865                 CloseFigure( hdc );
866             else
867                 LineTo( hdc, lastmove.x, lastmove.y );
868         }
869     }
870
871     return TRUE;
872 }
873
874 /******************************************************************
875  *
876  *   *Very* simple bezier drawing code,
877  *
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.
882  *
883  *   7 July 1998 Rein Klazes
884  */
885
886  /*
887   * some macro definitions for bezier drawing
888   *
889   * to avoid trucation 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 arithmatic
893   * 4 bits should allow 27 bits coordinates which I saw
894   * somewere in the win32 doc's
895   *
896   */
897
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
904
905 /* size of array to store points on */
906 /* enough for one curve */
907 #define BEZIER_INITBUFSIZE    (150)
908
909 /* calculate Bezier average, in this case the middle
910  * correctly rounded...
911  * */
912
913 #define BEZIERMIDDLE(Mid, P1, P2) \
914     (Mid).x=((P1).x+(P2).x + 1)/2;\
915     (Mid).y=((P1).y+(P2).y + 1)/2;
916
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
924 */
925 static BOOL BezierCheck( int level, POINT *Points)
926 {
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;
976     }
977 }
978
979 /* Helper for GDI_Bezier.
980  * Just handles one Bezier, so Points should point to four POINTs
981  */
982 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
983                                 INT *nPtsOut, INT level )
984 {
985     if(*nPtsOut == *dwOut) {
986         *dwOut *= 2;
987         *PtsOut = HeapReAlloc( GetProcessHeap(), 0, *PtsOut,
988                                *dwOut * sizeof(POINT) );
989     }
990
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;
996         }
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]);
1006
1007         BEZIERMIDDLE(Points[1], Points[0],  Points[1]);
1008         BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1009         BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1010
1011         Points2[0]=Points[3];
1012
1013         /* do the two halves */
1014         GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
1015         GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
1016     }
1017 }
1018
1019
1020
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
1027  *
1028  *  PARAMS
1029  *
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).
1035  *
1036  *  RETURNS
1037  *
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].
1044  */
1045 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
1046 {
1047     POINT *out;
1048     INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
1049
1050     if((count - 1) % 3 != 0) {
1051         ERR("Invalid no. of points\n");
1052         return NULL;
1053     }
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);
1062         }
1063         GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
1064     }
1065     TRACE("Produced %d points\n", *nPtsOut);
1066     return out;
1067 }