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