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