We don't handle STRETCH_HALFTONE mode yet; make this do the same as
[wine] / graphics / path.c
1 /*
2  * Graphics paths (BeginPath, EndPath etc.)
3  *
4  * Copyright 1997, 1998 Martin Boehme
5  *                 1999 Huw D M Davies
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23
24 #include <assert.h>
25 #include <math.h>
26 #include <string.h>
27 #if defined(HAVE_FLOAT_H)
28 #include <float.h>
29 #endif
30
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winerror.h"
34
35 #include "gdi.h"
36 #include "wine/debug.h"
37 #include "path.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
40
41 /* Notes on the implementation
42  *
43  * The implementation is based on dynamically resizable arrays of points and
44  * flags. I dithered for a bit before deciding on this implementation, and
45  * I had even done a bit of work on a linked list version before switching
46  * to arrays. It's a bit of a tradeoff. When you use linked lists, the
47  * implementation of FlattenPath is easier, because you can rip the
48  * PT_BEZIERTO entries out of the middle of the list and link the
49  * corresponding PT_LINETO entries in. However, when you use arrays,
50  * PathToRegion becomes easier, since you can essentially just pass your array
51  * of points to CreatePolyPolygonRgn. Also, if I'd used linked lists, I would
52  * have had the extra effort of creating a chunk-based allocation scheme
53  * in order to use memory effectively. That's why I finally decided to use
54  * arrays. Note by the way that the array based implementation has the same
55  * linear time complexity that linked lists would have since the arrays grow
56  * exponentially.
57  *
58  * The points are stored in the path in device coordinates. This is
59  * consistent with the way Windows does things (for instance, see the Win32
60  * SDK documentation for GetPath).
61  *
62  * The word "stroke" appears in several places (e.g. in the flag
63  * GdiPath.newStroke). A stroke consists of a PT_MOVETO followed by one or
64  * more PT_LINETOs or PT_BEZIERTOs, up to, but not including, the next
65  * PT_MOVETO. Note that this is not the same as the definition of a figure;
66  * a figure can contain several strokes.
67  *
68  * I modified the drawing functions (MoveTo, LineTo etc.) to test whether
69  * the path is open and to call the corresponding function in path.c if this
70  * is the case. A more elegant approach would be to modify the function
71  * pointers in the DC_FUNCTIONS structure; however, this would be a lot more
72  * complex. Also, the performance degradation caused by my approach in the
73  * case where no path is open is so small that it cannot be measured.
74  *
75  * Martin Boehme
76  */
77
78 /* FIXME: A lot of stuff isn't implemented yet. There is much more to come. */
79
80 #define NUM_ENTRIES_INITIAL 16  /* Initial size of points / flags arrays  */
81 #define GROW_FACTOR_NUMER    2  /* Numerator of grow factor for the array */
82 #define GROW_FACTOR_DENOM    1  /* Denominator of grow factor             */
83
84
85 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
86    HRGN *pHrgn);
87 static void   PATH_EmptyPath(GdiPath *pPath);
88 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries);
89 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
90    double angleStart, double angleEnd, BOOL addMoveTo);
91 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
92    double y, POINT *pPoint);
93 static void PATH_NormalizePoint(FLOAT_POINT corners[], const FLOAT_POINT
94    *pPoint, double *pX, double *pY);
95 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2);
96
97
98 /***********************************************************************
99  *           BeginPath    (GDI.512)
100  */
101 BOOL16 WINAPI BeginPath16(HDC16 hdc)
102 {
103    return (BOOL16)BeginPath((HDC)hdc);
104 }
105
106
107 /***********************************************************************
108  *           BeginPath    (GDI32.@)
109  */
110 BOOL WINAPI BeginPath(HDC hdc)
111 {
112     BOOL ret = TRUE;
113     DC *dc = DC_GetDCPtr( hdc );
114    
115     if(!dc) return FALSE;
116
117     if(dc->funcs->pBeginPath)
118         ret = dc->funcs->pBeginPath(dc->physDev);
119     else
120     {
121         /* If path is already open, do nothing */
122         if(dc->path.state != PATH_Open)
123         {
124             /* Make sure that path is empty */
125             PATH_EmptyPath(&dc->path);
126
127             /* Initialize variables for new path */
128             dc->path.newStroke=TRUE;
129             dc->path.state=PATH_Open;
130         }
131     }
132     GDI_ReleaseObj( hdc );
133     return ret;
134 }
135
136
137 /***********************************************************************
138  *           EndPath    (GDI.514)
139  */
140 BOOL16 WINAPI EndPath16(HDC16 hdc)
141 {
142    return (BOOL16)EndPath((HDC)hdc);
143 }
144
145
146 /***********************************************************************
147  *           EndPath    (GDI32.@)
148  */
149 BOOL WINAPI EndPath(HDC hdc)
150 {
151     BOOL ret = TRUE;
152     DC *dc = DC_GetDCPtr( hdc );
153    
154     if(!dc) return FALSE;
155
156     if(dc->funcs->pEndPath)
157         ret = dc->funcs->pEndPath(dc->physDev);
158     else
159     {
160         /* Check that path is currently being constructed */
161         if(dc->path.state!=PATH_Open)
162         {
163             SetLastError(ERROR_CAN_NOT_COMPLETE);
164             ret = FALSE;
165         }
166         /* Set flag to indicate that path is finished */
167         else dc->path.state=PATH_Closed;
168     }
169     GDI_ReleaseObj( hdc );
170     return ret;
171 }
172
173
174 /***********************************************************************
175  *           AbortPath    (GDI.511)
176  */
177 BOOL16 WINAPI AbortPath16(HDC16 hdc)
178 {
179    return (BOOL16)AbortPath((HDC)hdc);
180 }
181
182
183 /******************************************************************************
184  * AbortPath [GDI32.@]
185  * Closes and discards paths from device context
186  *
187  * NOTES
188  *    Check that SetLastError is being called correctly
189  *
190  * PARAMS
191  *    hdc [I] Handle to device context
192  *
193  * RETURNS STD
194  */
195 BOOL WINAPI AbortPath( HDC hdc )
196 {
197     BOOL ret = TRUE;
198     DC *dc = DC_GetDCPtr( hdc );
199    
200     if(!dc) return FALSE;
201
202     if(dc->funcs->pAbortPath)
203         ret = dc->funcs->pAbortPath(dc->physDev);
204     else /* Remove all entries from the path */
205         PATH_EmptyPath( &dc->path );
206     GDI_ReleaseObj( hdc );
207     return ret;
208 }
209
210
211 /***********************************************************************
212  *           CloseFigure    (GDI.513)
213  */
214 BOOL16 WINAPI CloseFigure16(HDC16 hdc)
215 {
216    return (BOOL16)CloseFigure((HDC)hdc);
217 }
218
219
220 /***********************************************************************
221  *           CloseFigure    (GDI32.@)
222  *
223  * FIXME: Check that SetLastError is being called correctly 
224  */
225 BOOL WINAPI CloseFigure(HDC hdc)
226 {
227     BOOL ret = TRUE;
228     DC *dc = DC_GetDCPtr( hdc );
229    
230     if(!dc) return FALSE;
231
232     if(dc->funcs->pCloseFigure)
233         ret = dc->funcs->pCloseFigure(dc->physDev);
234     else
235     {
236         /* Check that path is open */
237         if(dc->path.state!=PATH_Open)
238         {
239             SetLastError(ERROR_CAN_NOT_COMPLETE);
240             ret = FALSE;
241         }
242         else
243         {
244             /* FIXME: Shouldn't we draw a line to the beginning of the
245                figure? */
246             /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
247             if(dc->path.numEntriesUsed)
248             {
249                 dc->path.pFlags[dc->path.numEntriesUsed-1]|=PT_CLOSEFIGURE;
250                 dc->path.newStroke=TRUE;
251             }
252         }
253     }
254     GDI_ReleaseObj( hdc );
255     return ret;
256 }
257
258
259 /***********************************************************************
260  *           GetPath    (GDI.517)
261  */
262 INT16 WINAPI GetPath16(HDC16 hdc, LPPOINT16 pPoints, LPBYTE pTypes,
263    INT16 nSize)
264 {
265    FIXME("(%d,%p,%p): stub\n",hdc,pPoints,pTypes);
266
267    return 0;
268 }
269
270
271 /***********************************************************************
272  *           GetPath    (GDI32.@)
273  */
274 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
275    INT nSize)
276 {
277    INT ret = -1;
278    GdiPath *pPath;
279    DC *dc = DC_GetDCPtr( hdc );
280    
281    if(!dc) return -1;
282    
283    pPath = &dc->path;
284    
285    /* Check that path is closed */
286    if(pPath->state!=PATH_Closed)
287    {
288       SetLastError(ERROR_CAN_NOT_COMPLETE);
289       goto done;
290    }
291    
292    if(nSize==0)
293       ret = pPath->numEntriesUsed;
294    else if(nSize<pPath->numEntriesUsed)
295    {
296       SetLastError(ERROR_INVALID_PARAMETER);
297       goto done;
298    }
299    else
300    {
301       memcpy(pPoints, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
302       memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
303
304       /* Convert the points to logical coordinates */
305       if(!DPtoLP(hdc, pPoints, pPath->numEntriesUsed))
306       {
307          /* FIXME: Is this the correct value? */
308          SetLastError(ERROR_CAN_NOT_COMPLETE);
309         goto done;
310       }
311      else ret = pPath->numEntriesUsed;
312    }
313  done:
314    GDI_ReleaseObj( hdc );
315    return ret;
316 }
317
318 /***********************************************************************
319  *           PathToRegion    (GDI.518)
320  */
321 HRGN16 WINAPI PathToRegion16(HDC16 hdc)
322 {
323   return (HRGN16) PathToRegion((HDC) hdc);
324 }
325
326 /***********************************************************************
327  *           PathToRegion    (GDI32.@)
328  *
329  * FIXME 
330  *   Check that SetLastError is being called correctly 
331  *
332  * The documentation does not state this explicitly, but a test under Windows
333  * shows that the region which is returned should be in device coordinates.
334  */
335 HRGN WINAPI PathToRegion(HDC hdc)
336 {
337    GdiPath *pPath;
338    HRGN  hrgnRval = 0;
339    DC *dc = DC_GetDCPtr( hdc );
340
341    /* Get pointer to path */
342    if(!dc) return -1;
343    
344     pPath = &dc->path;
345    
346    /* Check that path is closed */
347    if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
348    else
349    {
350        /* FIXME: Should we empty the path even if conversion failed? */
351        if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
352            PATH_EmptyPath(pPath);
353        else
354            hrgnRval=0;
355    }
356    GDI_ReleaseObj( hdc );
357    return hrgnRval;
358 }
359
360 static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
361 {
362    INT   mapMode, graphicsMode;
363    SIZE  ptViewportExt, ptWindowExt;
364    POINT ptViewportOrg, ptWindowOrg;
365    XFORM xform;
366    HRGN  hrgn;
367
368    if(dc->funcs->pFillPath)
369        return dc->funcs->pFillPath(dc->physDev);
370
371    /* Check that path is closed */
372    if(pPath->state!=PATH_Closed)
373    {
374       SetLastError(ERROR_CAN_NOT_COMPLETE);
375       return FALSE;
376    }
377    
378    /* Construct a region from the path and fill it */
379    if(PATH_PathToRegion(pPath, dc->polyFillMode, &hrgn))
380    {
381       /* Since PaintRgn interprets the region as being in logical coordinates
382        * but the points we store for the path are already in device
383        * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
384        * Using SaveDC to save information about the mapping mode / world
385        * transform would be easier but would require more overhead, especially
386        * now that SaveDC saves the current path.
387        */
388        
389       /* Save the information about the old mapping mode */
390       mapMode=GetMapMode(dc->hSelf);
391       GetViewportExtEx(dc->hSelf, &ptViewportExt);
392       GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
393       GetWindowExtEx(dc->hSelf, &ptWindowExt);
394       GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
395       
396       /* Save world transform
397        * NB: The Windows documentation on world transforms would lead one to
398        * believe that this has to be done only in GM_ADVANCED; however, my
399        * tests show that resetting the graphics mode to GM_COMPATIBLE does
400        * not reset the world transform.
401        */
402       GetWorldTransform(dc->hSelf, &xform);
403       
404       /* Set MM_TEXT */
405       SetMapMode(dc->hSelf, MM_TEXT);
406       SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
407       SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
408
409       /* Paint the region */
410       PaintRgn(dc->hSelf, hrgn);
411       DeleteObject(hrgn);
412       /* Restore the old mapping mode */
413       SetMapMode(dc->hSelf, mapMode);
414       SetViewportExtEx(dc->hSelf, ptViewportExt.cx, ptViewportExt.cy, NULL);
415       SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
416       SetWindowExtEx(dc->hSelf, ptWindowExt.cx, ptWindowExt.cy, NULL);
417       SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
418
419       /* Go to GM_ADVANCED temporarily to restore the world transform */
420       graphicsMode=GetGraphicsMode(dc->hSelf);
421       SetGraphicsMode(dc->hSelf, GM_ADVANCED);
422       SetWorldTransform(dc->hSelf, &xform);
423       SetGraphicsMode(dc->hSelf, graphicsMode);
424       return TRUE;
425    }
426    return FALSE;
427 }
428
429 /***********************************************************************
430  *           FillPath    (GDI.515)
431  */
432 BOOL16 WINAPI FillPath16(HDC16 hdc)
433 {
434   return (BOOL16) FillPath((HDC) hdc); 
435 }
436
437 /***********************************************************************
438  *           FillPath    (GDI32.@)
439  *
440  * FIXME
441  *    Check that SetLastError is being called correctly 
442  */
443 BOOL WINAPI FillPath(HDC hdc)
444 {
445     DC *dc = DC_GetDCPtr( hdc );
446     BOOL bRet = FALSE;
447    
448     if(!dc) return FALSE;
449
450     if(dc->funcs->pFillPath)
451         bRet = dc->funcs->pFillPath(dc->physDev);
452     else
453     {
454         bRet = PATH_FillPath(dc, &dc->path);
455         if(bRet)
456         {
457             /* FIXME: Should the path be emptied even if conversion
458                failed? */
459             PATH_EmptyPath(&dc->path);
460         }
461     }
462     GDI_ReleaseObj( hdc );
463     return bRet;
464 }
465
466 /***********************************************************************
467  *           SelectClipPath    (GDI.519)
468  */
469 BOOL16 WINAPI SelectClipPath16(HDC16 hdc, INT16 iMode)
470 {
471   return (BOOL16) SelectClipPath((HDC) hdc, iMode);
472 }
473
474 /***********************************************************************
475  *           SelectClipPath    (GDI32.@)
476  * FIXME 
477  *  Check that SetLastError is being called correctly 
478  */
479 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
480 {
481    GdiPath *pPath;
482    HRGN  hrgnPath;
483    BOOL  success = FALSE;
484    DC *dc = DC_GetDCPtr( hdc );
485    
486    if(!dc) return FALSE;
487
488    if(dc->funcs->pSelectClipPath)
489      success = dc->funcs->pSelectClipPath(dc->physDev, iMode);
490    else
491    {
492        pPath = &dc->path;
493    
494        /* Check that path is closed */
495        if(pPath->state!=PATH_Closed)
496            SetLastError(ERROR_CAN_NOT_COMPLETE);
497        /* Construct a region from the path */
498        else if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnPath))
499        {
500            success = ExtSelectClipRgn( hdc, hrgnPath, iMode ) != ERROR;
501            DeleteObject(hrgnPath);
502
503            /* Empty the path */
504            if(success)
505                PATH_EmptyPath(pPath);
506            /* FIXME: Should this function delete the path even if it failed? */
507        }
508    }
509    GDI_ReleaseObj( hdc );
510    return success;
511 }
512
513
514 /***********************************************************************
515  * Exported functions
516  */
517
518 /* PATH_InitGdiPath
519  *
520  * Initializes the GdiPath structure.
521  */
522 void PATH_InitGdiPath(GdiPath *pPath)
523 {
524    assert(pPath!=NULL);
525
526    pPath->state=PATH_Null;
527    pPath->pPoints=NULL;
528    pPath->pFlags=NULL;
529    pPath->numEntriesUsed=0;
530    pPath->numEntriesAllocated=0;
531 }
532
533 /* PATH_DestroyGdiPath
534  *
535  * Destroys a GdiPath structure (frees the memory in the arrays).
536  */
537 void PATH_DestroyGdiPath(GdiPath *pPath)
538 {
539    assert(pPath!=NULL);
540
541    if (pPath->pPoints) HeapFree( GetProcessHeap(), 0, pPath->pPoints );
542    if (pPath->pFlags) HeapFree( GetProcessHeap(), 0, pPath->pFlags );
543 }
544
545 /* PATH_AssignGdiPath
546  *
547  * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
548  * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
549  * not just the pointers. Since this means that the arrays in pPathDest may
550  * need to be resized, pPathDest should have been initialized using
551  * PATH_InitGdiPath (in C++, this function would be an assignment operator,
552  * not a copy constructor).
553  * Returns TRUE if successful, else FALSE.
554  */
555 BOOL PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
556 {
557    assert(pPathDest!=NULL && pPathSrc!=NULL);
558
559    /* Make sure destination arrays are big enough */
560    if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
561       return FALSE;
562
563    /* Perform the copy operation */
564    memcpy(pPathDest->pPoints, pPathSrc->pPoints,
565       sizeof(POINT)*pPathSrc->numEntriesUsed);
566    memcpy(pPathDest->pFlags, pPathSrc->pFlags,
567       sizeof(BYTE)*pPathSrc->numEntriesUsed);
568
569    pPathDest->state=pPathSrc->state;
570    pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
571    pPathDest->newStroke=pPathSrc->newStroke;
572
573    return TRUE;
574 }
575
576 /* PATH_MoveTo
577  *
578  * Should be called when a MoveTo is performed on a DC that has an
579  * open path. This starts a new stroke. Returns TRUE if successful, else
580  * FALSE.
581  */
582 BOOL PATH_MoveTo(DC *dc)
583 {
584    GdiPath *pPath = &dc->path;
585    
586    /* Check that path is open */
587    if(pPath->state!=PATH_Open)
588       /* FIXME: Do we have to call SetLastError? */
589       return FALSE;
590
591    /* Start a new stroke */
592    pPath->newStroke=TRUE;
593
594    return TRUE;
595 }
596
597 /* PATH_LineTo
598  *
599  * Should be called when a LineTo is performed on a DC that has an
600  * open path. This adds a PT_LINETO entry to the path (and possibly
601  * a PT_MOVETO entry, if this is the first LineTo in a stroke).
602  * Returns TRUE if successful, else FALSE.
603  */
604 BOOL PATH_LineTo(DC *dc, INT x, INT y)
605 {
606    GdiPath *pPath = &dc->path;
607    POINT point, pointCurPos;
608    
609    /* Check that path is open */
610    if(pPath->state!=PATH_Open)
611       return FALSE;
612
613    /* Convert point to device coordinates */
614    point.x=x;
615    point.y=y;
616    if(!LPtoDP(dc->hSelf, &point, 1))
617       return FALSE;
618    
619    /* Add a PT_MOVETO if necessary */
620    if(pPath->newStroke)
621    {
622       pPath->newStroke=FALSE;
623       pointCurPos.x = dc->CursPosX;
624       pointCurPos.y = dc->CursPosY;
625       if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
626          return FALSE;
627       if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
628          return FALSE;
629    }
630    
631    /* Add a PT_LINETO entry */
632    return PATH_AddEntry(pPath, &point, PT_LINETO);
633 }
634
635 /* PATH_RoundRect
636  *
637  * Should be called when a call to RoundRect is performed on a DC that has
638  * an open path. Returns TRUE if successful, else FALSE.
639  *
640  * FIXME: it adds the same entries to the path as windows does, but there 
641  * is an error in the bezier drawing code so that there are small pixel-size
642  * gaps when the resulting path is drawn by StrokePath()
643  */
644 BOOL PATH_RoundRect(DC *dc, INT x1, INT y1, INT x2, INT y2, INT ell_width, INT ell_height)
645 {
646    GdiPath *pPath = &dc->path;
647    POINT corners[2], pointTemp;
648    FLOAT_POINT ellCorners[2];
649  
650    /* Check that path is open */
651    if(pPath->state!=PATH_Open)
652       return FALSE;
653
654    if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
655       return FALSE;
656
657    /* Add points to the roundrect path */ 
658    ellCorners[0].x = corners[1].x-ell_width;
659    ellCorners[0].y = corners[0].y; 
660    ellCorners[1].x = corners[1].x; 
661    ellCorners[1].y = corners[0].y+ell_height; 
662    if(!PATH_DoArcPart(pPath, ellCorners, 0, -M_PI_2, TRUE))
663       return FALSE;
664    pointTemp.x = corners[0].x+ell_width/2;
665    pointTemp.y = corners[0].y;
666    if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
667       return FALSE;
668    ellCorners[0].x = corners[0].x;
669    ellCorners[1].x = corners[0].x+ell_width;
670    if(!PATH_DoArcPart(pPath, ellCorners, -M_PI_2, -M_PI, FALSE))
671       return FALSE;
672    pointTemp.x = corners[0].x;
673    pointTemp.y = corners[1].y-ell_height/2;
674    if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
675       return FALSE;
676    ellCorners[0].y = corners[1].y-ell_height;
677    ellCorners[1].y = corners[1].y;
678    if(!PATH_DoArcPart(pPath, ellCorners, M_PI, M_PI_2, FALSE))
679       return FALSE;
680    pointTemp.x = corners[1].x-ell_width/2;
681    pointTemp.y = corners[1].y;
682    if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
683       return FALSE;
684    ellCorners[0].x = corners[1].x-ell_width;
685    ellCorners[1].x = corners[1].x;
686    if(!PATH_DoArcPart(pPath, ellCorners, M_PI_2, 0, FALSE))
687       return FALSE;    
688    
689    /* Close the roundrect figure */
690    if(!CloseFigure(dc->hSelf))
691       return FALSE;
692
693    return TRUE;
694 }
695
696 /* PATH_Rectangle
697  *
698  * Should be called when a call to Rectangle is performed on a DC that has
699  * an open path. Returns TRUE if successful, else FALSE.
700  */
701 BOOL PATH_Rectangle(DC *dc, INT x1, INT y1, INT x2, INT y2)
702 {
703    GdiPath *pPath = &dc->path;
704    POINT corners[2], pointTemp;
705
706    /* Check that path is open */
707    if(pPath->state!=PATH_Open)
708       return FALSE;
709
710    if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
711       return FALSE;
712
713    /* Close any previous figure */
714    if(!CloseFigure(dc->hSelf))
715    {
716       /* The CloseFigure call shouldn't have failed */
717       assert(FALSE);
718       return FALSE;
719    }
720
721    /* Add four points to the path */
722    pointTemp.x=corners[1].x;
723    pointTemp.y=corners[0].y;
724    if(!PATH_AddEntry(pPath, &pointTemp, PT_MOVETO))
725       return FALSE;
726    if(!PATH_AddEntry(pPath, corners, PT_LINETO))
727       return FALSE;
728    pointTemp.x=corners[0].x;
729    pointTemp.y=corners[1].y;
730    if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
731       return FALSE;
732    if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
733       return FALSE;
734
735    /* Close the rectangle figure */
736    if(!CloseFigure(dc->hSelf))
737    {
738       /* The CloseFigure call shouldn't have failed */
739       assert(FALSE);
740       return FALSE;
741    }
742
743    return TRUE;
744 }
745
746 /* PATH_Ellipse
747  * 
748  * Should be called when a call to Ellipse is performed on a DC that has
749  * an open path. This adds four Bezier splines representing the ellipse
750  * to the path. Returns TRUE if successful, else FALSE.
751  */
752 BOOL PATH_Ellipse(DC *dc, INT x1, INT y1, INT x2, INT y2)
753 {
754    return( PATH_Arc(dc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2,0) && 
755            CloseFigure(dc->hSelf) );
756 }
757
758 /* PATH_Arc
759  *
760  * Should be called when a call to Arc is performed on a DC that has
761  * an open path. This adds up to five Bezier splines representing the arc
762  * to the path. When 'lines' is 1, we add 1 extra line to get a chord,
763  * and when 'lines' is 2, we add 2 extra lines to get a pie.
764  * Returns TRUE if successful, else FALSE.
765  */
766 BOOL PATH_Arc(DC *dc, INT x1, INT y1, INT x2, INT y2,
767    INT xStart, INT yStart, INT xEnd, INT yEnd, INT lines)
768 {
769    GdiPath     *pPath = &dc->path;
770    double      angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
771                /* Initialize angleEndQuadrant to silence gcc's warning */
772    double      x, y;
773    FLOAT_POINT corners[2], pointStart, pointEnd;
774    POINT       centre;
775    BOOL      start, end;
776    INT       temp;
777
778    /* FIXME: This function should check for all possible error returns */
779    /* FIXME: Do we have to respect newStroke? */
780    
781    /* Check that path is open */
782    if(pPath->state!=PATH_Open)
783       return FALSE;
784
785    /* Check for zero height / width */
786    /* FIXME: Only in GM_COMPATIBLE? */
787    if(x1==x2 || y1==y2)
788       return TRUE;
789    
790    /* Convert points to device coordinates */
791    corners[0].x=(FLOAT)x1;
792    corners[0].y=(FLOAT)y1;
793    corners[1].x=(FLOAT)x2;
794    corners[1].y=(FLOAT)y2;
795    pointStart.x=(FLOAT)xStart;
796    pointStart.y=(FLOAT)yStart;
797    pointEnd.x=(FLOAT)xEnd;
798    pointEnd.y=(FLOAT)yEnd;
799    INTERNAL_LPTODP_FLOAT(dc, corners);
800    INTERNAL_LPTODP_FLOAT(dc, corners+1);
801    INTERNAL_LPTODP_FLOAT(dc, &pointStart);
802    INTERNAL_LPTODP_FLOAT(dc, &pointEnd);
803
804    /* Make sure first corner is top left and second corner is bottom right */
805    if(corners[0].x>corners[1].x)
806    {
807       temp=corners[0].x;
808       corners[0].x=corners[1].x;
809       corners[1].x=temp;
810    }
811    if(corners[0].y>corners[1].y)
812    {
813       temp=corners[0].y;
814       corners[0].y=corners[1].y;
815       corners[1].y=temp;
816    }
817
818    /* Compute start and end angle */
819    PATH_NormalizePoint(corners, &pointStart, &x, &y);
820    angleStart=atan2(y, x);
821    PATH_NormalizePoint(corners, &pointEnd, &x, &y);
822    angleEnd=atan2(y, x);
823
824    /* Make sure the end angle is "on the right side" of the start angle */
825    if(dc->ArcDirection==AD_CLOCKWISE)
826    {
827       if(angleEnd<=angleStart)
828       {
829          angleEnd+=2*M_PI;
830          assert(angleEnd>=angleStart);
831       }
832    }
833    else
834    {
835       if(angleEnd>=angleStart)
836       {
837          angleEnd-=2*M_PI;
838          assert(angleEnd<=angleStart);
839       }
840    }
841
842    /* In GM_COMPATIBLE, don't include bottom and right edges */
843    if(dc->GraphicsMode==GM_COMPATIBLE)
844    {
845       corners[1].x--;
846       corners[1].y--;
847    }
848    
849    /* Add the arc to the path with one Bezier spline per quadrant that the
850     * arc spans */
851    start=TRUE;
852    end=FALSE;
853    do
854    {
855       /* Determine the start and end angles for this quadrant */
856       if(start)
857       {
858          angleStartQuadrant=angleStart;
859          if(dc->ArcDirection==AD_CLOCKWISE)
860             angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
861          else
862             angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
863       }
864       else
865       {
866          angleStartQuadrant=angleEndQuadrant;
867          if(dc->ArcDirection==AD_CLOCKWISE)
868             angleEndQuadrant+=M_PI_2;
869          else
870             angleEndQuadrant-=M_PI_2;
871       }
872
873       /* Have we reached the last part of the arc? */
874       if((dc->ArcDirection==AD_CLOCKWISE &&
875          angleEnd<angleEndQuadrant) ||
876          (dc->ArcDirection==AD_COUNTERCLOCKWISE &&
877          angleEnd>angleEndQuadrant))
878       {
879          /* Adjust the end angle for this quadrant */
880          angleEndQuadrant=angleEnd;
881          end=TRUE;
882       }
883
884       /* Add the Bezier spline to the path */
885       PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
886          start);
887       start=FALSE;
888    }  while(!end);
889
890    /* chord: close figure. pie: add line and close figure */
891    if(lines==1)
892    {
893       if(!CloseFigure(dc->hSelf))
894          return FALSE;
895    }
896    else if(lines==2)
897    {
898       centre.x = (corners[0].x+corners[1].x)/2; 
899       centre.y = (corners[0].y+corners[1].y)/2;
900       if(!PATH_AddEntry(pPath, &centre, PT_LINETO | PT_CLOSEFIGURE))
901          return FALSE;
902    }
903         
904    return TRUE;
905 }
906
907 BOOL PATH_PolyBezierTo(DC *dc, const POINT *pts, DWORD cbPoints)
908 {
909    GdiPath     *pPath = &dc->path;
910    POINT       pt;
911    INT         i;
912
913    /* Check that path is open */
914    if(pPath->state!=PATH_Open)
915       return FALSE;
916
917    /* Add a PT_MOVETO if necessary */
918    if(pPath->newStroke)
919    {
920       pPath->newStroke=FALSE;
921       pt.x = dc->CursPosX;
922       pt.y = dc->CursPosY;
923       if(!LPtoDP(dc->hSelf, &pt, 1))
924          return FALSE;
925       if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
926          return FALSE;
927    }
928
929    for(i = 0; i < cbPoints; i++) {
930        pt = pts[i];
931        if(!LPtoDP(dc->hSelf, &pt, 1))
932            return FALSE;
933        PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
934    }
935    return TRUE;
936 }
937    
938 BOOL PATH_PolyBezier(DC *dc, const POINT *pts, DWORD cbPoints)
939 {
940    GdiPath     *pPath = &dc->path;
941    POINT       pt;
942    INT         i;
943
944    /* Check that path is open */
945    if(pPath->state!=PATH_Open)
946       return FALSE;
947
948    for(i = 0; i < cbPoints; i++) {
949        pt = pts[i];
950        if(!LPtoDP(dc->hSelf, &pt, 1))
951            return FALSE;
952        PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
953    }
954    return TRUE;
955 }
956
957 BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
958 {
959    GdiPath     *pPath = &dc->path;
960    POINT       pt;
961    INT         i;
962
963    /* Check that path is open */
964    if(pPath->state!=PATH_Open)
965       return FALSE;
966
967    for(i = 0; i < cbPoints; i++) {
968        pt = pts[i];
969        if(!LPtoDP(dc->hSelf, &pt, 1))
970            return FALSE;
971        PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
972    }
973    return TRUE;
974 }
975    
976 BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
977 {
978    GdiPath     *pPath = &dc->path;
979    POINT       pt;
980    INT         i;
981
982    /* Check that path is open */
983    if(pPath->state!=PATH_Open)
984       return FALSE;
985
986    /* Add a PT_MOVETO if necessary */
987    if(pPath->newStroke)
988    {
989       pPath->newStroke=FALSE;
990       pt.x = dc->CursPosX;
991       pt.y = dc->CursPosY;
992       if(!LPtoDP(dc->hSelf, &pt, 1))
993          return FALSE;
994       if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
995          return FALSE;
996    }
997
998    for(i = 0; i < cbPoints; i++) {
999        pt = pts[i];
1000        if(!LPtoDP(dc->hSelf, &pt, 1))
1001            return FALSE;
1002        PATH_AddEntry(pPath, &pt, PT_LINETO);
1003    }
1004
1005    return TRUE;
1006 }
1007
1008
1009 BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
1010 {
1011    GdiPath     *pPath = &dc->path;
1012    POINT       pt;
1013    INT         i;
1014
1015    /* Check that path is open */
1016    if(pPath->state!=PATH_Open)
1017       return FALSE;
1018
1019    for(i = 0; i < cbPoints; i++) {
1020        pt = pts[i];
1021        if(!LPtoDP(dc->hSelf, &pt, 1))
1022            return FALSE;
1023        PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
1024                      ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
1025                       PT_LINETO));
1026    }
1027    return TRUE;
1028 }
1029
1030 BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
1031                        UINT polygons )
1032 {
1033    GdiPath     *pPath = &dc->path;
1034    POINT       pt, startpt;
1035    INT         poly, point, i;
1036
1037    /* Check that path is open */
1038    if(pPath->state!=PATH_Open)
1039       return FALSE;
1040
1041    for(i = 0, poly = 0; poly < polygons; poly++) {
1042        for(point = 0; point < counts[poly]; point++, i++) {
1043            pt = pts[i];
1044            if(!LPtoDP(dc->hSelf, &pt, 1))
1045                return FALSE;
1046            if(point == 0) startpt = pt;
1047            PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1048        }
1049        /* win98 adds an extra line to close the figure for some reason */
1050        PATH_AddEntry(pPath, &startpt, PT_LINETO | PT_CLOSEFIGURE);
1051    }
1052    return TRUE;
1053 }
1054
1055 BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1056                         DWORD polylines )
1057 {
1058    GdiPath     *pPath = &dc->path;
1059    POINT       pt;
1060    INT         poly, point, i;
1061
1062    /* Check that path is open */
1063    if(pPath->state!=PATH_Open)
1064       return FALSE;
1065
1066    for(i = 0, poly = 0; poly < polylines; poly++) {
1067        for(point = 0; point < counts[poly]; point++, i++) {
1068            pt = pts[i];
1069            if(!LPtoDP(dc->hSelf, &pt, 1))
1070                return FALSE;
1071            PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1072        }
1073    }
1074    return TRUE;
1075 }
1076    
1077 /***********************************************************************
1078  * Internal functions
1079  */
1080
1081 /* PATH_CheckCorners
1082  *
1083  * Helper function for PATH_RoundRect() and PATH_Rectangle() 
1084  */
1085 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2)
1086 {
1087    INT temp;
1088
1089    /* Convert points to device coordinates */
1090    corners[0].x=x1;
1091    corners[0].y=y1;
1092    corners[1].x=x2;
1093    corners[1].y=y2;
1094    if(!LPtoDP(dc->hSelf, corners, 2))
1095       return FALSE;
1096    
1097    /* Make sure first corner is top left and second corner is bottom right */
1098    if(corners[0].x>corners[1].x)
1099    {
1100       temp=corners[0].x;
1101       corners[0].x=corners[1].x;
1102       corners[1].x=temp;
1103    }
1104    if(corners[0].y>corners[1].y)
1105    {
1106       temp=corners[0].y;
1107       corners[0].y=corners[1].y;
1108       corners[1].y=temp;
1109    }
1110    
1111    /* In GM_COMPATIBLE, don't include bottom and right edges */
1112    if(dc->GraphicsMode==GM_COMPATIBLE)
1113    {
1114       corners[1].x--;
1115       corners[1].y--;
1116    }
1117
1118    return TRUE;
1119 }
1120
1121 /* PATH_AddFlatBezier
1122  */
1123 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
1124 {
1125     POINT *pts;
1126     INT no, i;
1127
1128     pts = GDI_Bezier( pt, 4, &no );
1129     if(!pts) return FALSE;
1130
1131     for(i = 1; i < no; i++)
1132         PATH_AddEntry(pPath, &pts[i], 
1133             (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
1134     HeapFree( GetProcessHeap(), 0, pts );
1135     return TRUE;
1136 }
1137
1138 /* PATH_FlattenPath
1139  *
1140  * Replaces Beziers with line segments
1141  *
1142  */
1143 static BOOL PATH_FlattenPath(GdiPath *pPath)
1144 {
1145     GdiPath newPath;
1146     INT srcpt;
1147
1148     memset(&newPath, 0, sizeof(newPath));
1149     newPath.state = PATH_Open;
1150     for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
1151         switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
1152         case PT_MOVETO:
1153         case PT_LINETO:
1154             PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
1155                           pPath->pFlags[srcpt]);
1156             break;
1157         case PT_BEZIERTO:
1158           PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
1159                              pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
1160             srcpt += 2;
1161             break;
1162         }
1163     }
1164     newPath.state = PATH_Closed;
1165     PATH_AssignGdiPath(pPath, &newPath);
1166     PATH_EmptyPath(&newPath);
1167     return TRUE;
1168 }
1169           
1170 /* PATH_PathToRegion
1171  *
1172  * Creates a region from the specified path using the specified polygon
1173  * filling mode. The path is left unchanged. A handle to the region that
1174  * was created is stored in *pHrgn. If successful, TRUE is returned; if an
1175  * error occurs, SetLastError is called with the appropriate value and
1176  * FALSE is returned.
1177  */
1178 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
1179    HRGN *pHrgn)
1180 {
1181    int    numStrokes, iStroke, i;
1182    INT  *pNumPointsInStroke;
1183    HRGN hrgn;
1184
1185    assert(pPath!=NULL);
1186    assert(pHrgn!=NULL);
1187
1188    PATH_FlattenPath(pPath);
1189
1190    /* FIXME: What happens when number of points is zero? */
1191    
1192    /* First pass: Find out how many strokes there are in the path */
1193    /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
1194    numStrokes=0;
1195    for(i=0; i<pPath->numEntriesUsed; i++)
1196       if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1197          numStrokes++;
1198
1199    /* Allocate memory for number-of-points-in-stroke array */
1200    pNumPointsInStroke=(int *)HeapAlloc( GetProcessHeap(), 0, 
1201                                         sizeof(int) * numStrokes );
1202    if(!pNumPointsInStroke)
1203    {
1204       SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1205       return FALSE;
1206    }
1207    
1208    /* Second pass: remember number of points in each polygon */
1209    iStroke=-1;  /* Will get incremented to 0 at beginning of first stroke */
1210    for(i=0; i<pPath->numEntriesUsed; i++)
1211    {
1212       /* Is this the beginning of a new stroke? */
1213       if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1214       {
1215          iStroke++;
1216          pNumPointsInStroke[iStroke]=0;
1217       }
1218
1219       pNumPointsInStroke[iStroke]++;
1220    }
1221
1222    /* Create a region from the strokes */
1223    hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
1224       numStrokes, nPolyFillMode);
1225    if(hrgn==(HRGN)0)
1226    {
1227       SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1228       return FALSE;
1229    }
1230
1231    /* Free memory for number-of-points-in-stroke array */
1232    HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
1233
1234    /* Success! */
1235    *pHrgn=hrgn;
1236    return TRUE;
1237 }
1238
1239 /* PATH_EmptyPath
1240  *
1241  * Removes all entries from the path and sets the path state to PATH_Null.
1242  */
1243 static void PATH_EmptyPath(GdiPath *pPath)
1244 {
1245    assert(pPath!=NULL);
1246
1247    pPath->state=PATH_Null;
1248    pPath->numEntriesUsed=0;
1249 }
1250
1251 /* PATH_AddEntry
1252  *
1253  * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1254  * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1255  * successful, FALSE otherwise (e.g. if not enough memory was available).
1256  */
1257 BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
1258 {
1259    assert(pPath!=NULL);
1260    
1261    /* FIXME: If newStroke is true, perhaps we want to check that we're
1262     * getting a PT_MOVETO
1263     */
1264    TRACE("(%ld,%ld) - %d\n", pPoint->x, pPoint->y, flags);
1265
1266    /* Check that path is open */
1267    if(pPath->state!=PATH_Open)
1268       return FALSE;
1269    
1270    /* Reserve enough memory for an extra path entry */
1271    if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
1272       return FALSE;
1273
1274    /* Store information in path entry */
1275    pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
1276    pPath->pFlags[pPath->numEntriesUsed]=flags;
1277
1278    /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1279    if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
1280       pPath->newStroke=TRUE;
1281
1282    /* Increment entry count */
1283    pPath->numEntriesUsed++;
1284
1285    return TRUE;
1286 }
1287
1288 /* PATH_ReserveEntries
1289  *
1290  * Ensures that at least "numEntries" entries (for points and flags) have
1291  * been allocated; allocates larger arrays and copies the existing entries
1292  * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1293  */
1294 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries)
1295 {
1296    INT   numEntriesToAllocate;
1297    POINT *pPointsNew;
1298    BYTE    *pFlagsNew;
1299    
1300    assert(pPath!=NULL);
1301    assert(numEntries>=0);
1302
1303    /* Do we have to allocate more memory? */
1304    if(numEntries > pPath->numEntriesAllocated)
1305    {
1306       /* Find number of entries to allocate. We let the size of the array
1307        * grow exponentially, since that will guarantee linear time
1308        * complexity. */
1309       if(pPath->numEntriesAllocated)
1310       {
1311          numEntriesToAllocate=pPath->numEntriesAllocated;
1312          while(numEntriesToAllocate<numEntries)
1313             numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
1314                GROW_FACTOR_DENOM;
1315       }
1316       else
1317          numEntriesToAllocate=numEntries;
1318
1319       /* Allocate new arrays */
1320       pPointsNew=(POINT *)HeapAlloc( GetProcessHeap(), 0, 
1321                                      numEntriesToAllocate * sizeof(POINT) );
1322       if(!pPointsNew)
1323          return FALSE;
1324       pFlagsNew=(BYTE *)HeapAlloc( GetProcessHeap(), 0, 
1325                                    numEntriesToAllocate * sizeof(BYTE) );
1326       if(!pFlagsNew)
1327       {
1328          HeapFree( GetProcessHeap(), 0, pPointsNew );
1329          return FALSE;
1330       }
1331
1332       /* Copy old arrays to new arrays and discard old arrays */
1333       if(pPath->pPoints)
1334       {
1335          assert(pPath->pFlags);
1336
1337          memcpy(pPointsNew, pPath->pPoints,
1338              sizeof(POINT)*pPath->numEntriesUsed);
1339          memcpy(pFlagsNew, pPath->pFlags,
1340              sizeof(BYTE)*pPath->numEntriesUsed);
1341
1342          HeapFree( GetProcessHeap(), 0, pPath->pPoints );
1343          HeapFree( GetProcessHeap(), 0, pPath->pFlags );
1344       }
1345       pPath->pPoints=pPointsNew;
1346       pPath->pFlags=pFlagsNew;
1347       pPath->numEntriesAllocated=numEntriesToAllocate;
1348    }
1349
1350    return TRUE;
1351 }
1352
1353 /* PATH_DoArcPart
1354  *
1355  * Creates a Bezier spline that corresponds to part of an arc and appends the
1356  * corresponding points to the path. The start and end angles are passed in
1357  * "angleStart" and "angleEnd"; these angles should span a quarter circle
1358  * at most. If "addMoveTo" is true, a PT_MOVETO entry for the first control
1359  * point is added to the path; otherwise, it is assumed that the current
1360  * position is equal to the first control point.
1361  */
1362 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
1363    double angleStart, double angleEnd, BOOL addMoveTo)
1364 {
1365    double  halfAngle, a;
1366    double  xNorm[4], yNorm[4];
1367    POINT point;
1368    int     i;
1369
1370    assert(fabs(angleEnd-angleStart)<=M_PI_2);
1371
1372    /* FIXME: Is there an easier way of computing this? */
1373
1374    /* Compute control points */
1375    halfAngle=(angleEnd-angleStart)/2.0;
1376    if(fabs(halfAngle)>1e-8)
1377    {
1378       a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
1379       xNorm[0]=cos(angleStart);
1380       yNorm[0]=sin(angleStart);
1381       xNorm[1]=xNorm[0] - a*yNorm[0];
1382       yNorm[1]=yNorm[0] + a*xNorm[0];
1383       xNorm[3]=cos(angleEnd);
1384       yNorm[3]=sin(angleEnd);
1385       xNorm[2]=xNorm[3] + a*yNorm[3];
1386       yNorm[2]=yNorm[3] - a*xNorm[3];
1387    }
1388    else
1389       for(i=0; i<4; i++)
1390       {
1391          xNorm[i]=cos(angleStart);
1392          yNorm[i]=sin(angleStart);
1393       }
1394    
1395    /* Add starting point to path if desired */
1396    if(addMoveTo)
1397    {
1398       PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1399       if(!PATH_AddEntry(pPath, &point, PT_MOVETO))
1400          return FALSE;
1401    }
1402
1403    /* Add remaining control points */
1404    for(i=1; i<4; i++)
1405    {
1406       PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1407       if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1408          return FALSE;
1409    }
1410
1411    return TRUE;
1412 }
1413
1414 /* PATH_ScaleNormalizedPoint
1415  *
1416  * Scales a normalized point (x, y) with respect to the box whose corners are
1417  * passed in "corners". The point is stored in "*pPoint". The normalized
1418  * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1419  * (1.0, 1.0) correspond to corners[1].
1420  */
1421 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
1422    double y, POINT *pPoint)
1423 {
1424    pPoint->x=GDI_ROUND( (double)corners[0].x +
1425       (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
1426    pPoint->y=GDI_ROUND( (double)corners[0].y +
1427       (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
1428 }
1429
1430 /* PATH_NormalizePoint
1431  *
1432  * Normalizes a point with respect to the box whose corners are passed in
1433  * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1434  */
1435 static void PATH_NormalizePoint(FLOAT_POINT corners[],
1436    const FLOAT_POINT *pPoint,
1437    double *pX, double *pY)
1438 {
1439    *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) *
1440       2.0 - 1.0;
1441    *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1442       2.0 - 1.0;
1443 }
1444
1445 /*******************************************************************
1446  *      FlattenPath [GDI.516]
1447  *
1448  *
1449  */
1450 BOOL16 WINAPI FlattenPath16(HDC16 hdc)
1451 {
1452   return (BOOL16) FlattenPath((HDC) hdc);
1453 }
1454
1455 /*******************************************************************
1456  *      FlattenPath [GDI32.@]
1457  *
1458  *
1459  */
1460 BOOL WINAPI FlattenPath(HDC hdc)
1461 {
1462     BOOL ret = FALSE;
1463     DC *dc = DC_GetDCPtr( hdc );
1464
1465     if(!dc) return FALSE;
1466
1467     if(dc->funcs->pFlattenPath) ret = dc->funcs->pFlattenPath(dc->physDev);
1468     else 
1469     {
1470         GdiPath *pPath = &dc->path;
1471         if(pPath->state != PATH_Closed)
1472             ret = PATH_FlattenPath(pPath);
1473     }
1474     GDI_ReleaseObj( hdc );
1475     return ret;
1476 }
1477
1478
1479 static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1480 {
1481     INT i;
1482     POINT ptLastMove = {0,0};
1483     POINT ptViewportOrg, ptWindowOrg;
1484     SIZE szViewportExt, szWindowExt;
1485     DWORD mapMode, graphicsMode;
1486     XFORM xform;
1487     BOOL ret = TRUE;
1488
1489     if(dc->funcs->pStrokePath)
1490         return dc->funcs->pStrokePath(dc->physDev);
1491
1492     if(pPath->state != PATH_Closed)
1493         return FALSE;
1494
1495     /* Save the mapping mode info */
1496     mapMode=GetMapMode(dc->hSelf);
1497     GetViewportExtEx(dc->hSelf, &szViewportExt);
1498     GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
1499     GetWindowExtEx(dc->hSelf, &szWindowExt);
1500     GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
1501     GetWorldTransform(dc->hSelf, &xform);
1502       
1503     /* Set MM_TEXT */
1504     SetMapMode(dc->hSelf, MM_TEXT);
1505     SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
1506     SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
1507
1508
1509     for(i = 0; i < pPath->numEntriesUsed; i++) {
1510         switch(pPath->pFlags[i]) {
1511         case PT_MOVETO:
1512             TRACE("Got PT_MOVETO (%ld, %ld)\n",
1513                   pPath->pPoints[i].x, pPath->pPoints[i].y);
1514             MoveToEx(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y, NULL);
1515             ptLastMove = pPath->pPoints[i];
1516             break;
1517         case PT_LINETO:
1518         case (PT_LINETO | PT_CLOSEFIGURE):
1519             TRACE("Got PT_LINETO (%ld, %ld)\n",
1520                   pPath->pPoints[i].x, pPath->pPoints[i].y);
1521             LineTo(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y);
1522             break;
1523         case PT_BEZIERTO:
1524             TRACE("Got PT_BEZIERTO\n");
1525             if(pPath->pFlags[i+1] != PT_BEZIERTO || 
1526                (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1527                 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1528                 ret = FALSE;
1529                 goto end;
1530             }
1531             PolyBezierTo(dc->hSelf, &pPath->pPoints[i], 3);
1532             i += 2;
1533             break;
1534         default:
1535             ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1536             ret = FALSE;
1537             goto end;
1538         }
1539         if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1540             LineTo(dc->hSelf, ptLastMove.x, ptLastMove.y);
1541     }
1542
1543  end:
1544
1545     /* Restore the old mapping mode */
1546     SetMapMode(dc->hSelf, mapMode);
1547     SetViewportExtEx(dc->hSelf, szViewportExt.cx, szViewportExt.cy, NULL);
1548     SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
1549     SetWindowExtEx(dc->hSelf, szWindowExt.cx, szWindowExt.cy, NULL);
1550     SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
1551
1552     /* Go to GM_ADVANCED temporarily to restore the world transform */
1553     graphicsMode=GetGraphicsMode(dc->hSelf);
1554     SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1555     SetWorldTransform(dc->hSelf, &xform);
1556     SetGraphicsMode(dc->hSelf, graphicsMode);
1557     return ret;
1558 }
1559
1560
1561 /*******************************************************************
1562  *      StrokeAndFillPath [GDI.520]
1563  *
1564  *
1565  */
1566 BOOL16 WINAPI StrokeAndFillPath16(HDC16 hdc)
1567 {
1568   return (BOOL16) StrokeAndFillPath((HDC) hdc);
1569 }
1570
1571 /*******************************************************************
1572  *      StrokeAndFillPath [GDI32.@]
1573  *
1574  *
1575  */
1576 BOOL WINAPI StrokeAndFillPath(HDC hdc)
1577 {
1578    DC *dc = DC_GetDCPtr( hdc );
1579    BOOL bRet = FALSE;
1580
1581    if(!dc) return FALSE;
1582
1583    if(dc->funcs->pStrokeAndFillPath)
1584        bRet = dc->funcs->pStrokeAndFillPath(dc->physDev);
1585    else
1586    {
1587        bRet = PATH_FillPath(dc, &dc->path);
1588        if(bRet) bRet = PATH_StrokePath(dc, &dc->path);
1589        if(bRet) PATH_EmptyPath(&dc->path);
1590    }
1591    GDI_ReleaseObj( hdc );
1592    return bRet;
1593 }
1594
1595 /*******************************************************************
1596  *      StrokePath [GDI.521]
1597  *
1598  *
1599  */
1600 BOOL16 WINAPI StrokePath16(HDC16 hdc)
1601 {
1602   return (BOOL16) StrokePath((HDC) hdc);
1603 }
1604
1605 /*******************************************************************
1606  *      StrokePath [GDI32.@]
1607  *
1608  *
1609  */
1610 BOOL WINAPI StrokePath(HDC hdc)
1611 {
1612     DC *dc = DC_GetDCPtr( hdc );
1613     GdiPath *pPath;
1614     BOOL bRet = FALSE;
1615
1616     TRACE("(%08x)\n", hdc);
1617     if(!dc) return FALSE;
1618
1619     if(dc->funcs->pStrokePath)
1620         bRet = dc->funcs->pStrokePath(dc->physDev);
1621     else
1622     {
1623         pPath = &dc->path;
1624         bRet = PATH_StrokePath(dc, pPath);
1625         PATH_EmptyPath(pPath);
1626     }
1627     GDI_ReleaseObj( hdc );
1628     return bRet;
1629 }
1630
1631 /*******************************************************************
1632  *      WidenPath [GDI.522]
1633  *
1634  *
1635  */
1636 BOOL16 WINAPI WidenPath16(HDC16 hdc)
1637 {
1638   return (BOOL16) WidenPath((HDC) hdc);
1639 }
1640
1641 /*******************************************************************
1642  *      WidenPath [GDI32.@]
1643  *
1644  *
1645  */
1646 BOOL WINAPI WidenPath(HDC hdc)
1647 {
1648    DC *dc = DC_GetDCPtr( hdc );
1649    BOOL ret = FALSE;
1650    
1651    if(!dc) return FALSE;
1652
1653    if(dc->funcs->pWidenPath)
1654      ret = dc->funcs->pWidenPath(dc->physDev);
1655
1656    FIXME("stub\n");
1657    GDI_ReleaseObj( hdc );
1658    return ret;
1659 }