Implemented the OLE Drag and Drop target registration mechanism and
[wine] / graphics / path.c
1 /*
2  * Graphics paths (BeginPath, EndPath etc.)
3  *
4  * Copyright 1997, 1998 Martin Boehme
5  */
6
7 #include <assert.h>
8 #include <malloc.h>
9 #include <math.h>
10
11 #include "windows.h"
12 #include "winerror.h"
13
14 #include "dc.h"
15 #include "debug.h"
16 #include "path.h"
17
18 /* Notes on the implementation
19  *
20  * The implementation is based on dynamically resizable arrays of points and
21  * flags. I dithered for a bit before deciding on this implementation, and
22  * I had even done a bit of work on a linked list version before switching
23  * to arrays. It's a bit of a tradeoff. When you use linked lists, the
24  * implementation of FlattenPath is easier, because you can rip the
25  * PT_BEZIERTO entries out of the middle of the list and link the
26  * corresponding PT_LINETO entries in. However, when you use arrays,
27  * PathToRegion becomes easier, since you can essentially just pass your array
28  * of points to CreatePolyPolygonRgn. Also, if I'd used linked lists, I would
29  * have had the extra effort of creating a chunk-based allocation scheme
30  * in order to use memory effectively. That's why I finally decided to use
31  * arrays. Note by the way that the array based implementation has the same
32  * linear time complexity that linked lists would have since the arrays grow
33  * exponentially.
34  *
35  * The points are stored in the path in device coordinates. This is
36  * consistent with the way Windows does things (for instance, see the Win32
37  * SDK documentation for GetPath).
38  *
39  * The word "stroke" appears in several places (e.g. in the flag
40  * GdiPath.newStroke). A stroke consists of a PT_MOVETO followed by one or
41  * more PT_LINETOs or PT_BEZIERTOs, up to, but not including, the next
42  * PT_MOVETO. Note that this is not the same as the definition of a figure;
43  * a figure can contain several strokes.
44  *
45  * I modified the drawing functions (MoveTo, LineTo etc.) to test whether
46  * the path is open and to call the corresponding function in path.c if this
47  * is the case. A more elegant approach would be to modify the function
48  * pointers in the DC_FUNCTIONS structure; however, this would be a lot more
49  * complex. Also, the performance degradation caused by my approach in the
50  * case where no path is open is so small that it cannot be measured.
51  *
52  * Martin Boehme
53  */
54
55 /* FIXME: A lot of stuff isn't implemented yet. There is much more to come. */
56
57 #define NUM_ENTRIES_INITIAL 16  /* Initial size of points / flags arrays  */
58 #define GROW_FACTOR_NUMER    2  /* Numerator of grow factor for the array */
59 #define GROW_FACTOR_DENOM    1  /* Denominator of grow factor             */
60
61
62 static BOOL32 PATH_PathToRegion(const GdiPath *pPath, INT32 nPolyFillMode,
63    HRGN32 *pHrgn);
64 static void   PATH_EmptyPath(GdiPath *pPath);
65 static BOOL32 PATH_AddEntry(GdiPath *pPath, const POINT32 *pPoint,
66    BYTE flags);
67 static BOOL32 PATH_ReserveEntries(GdiPath *pPath, INT32 numEntries);
68 static BOOL32 PATH_GetPathFromHDC(HDC32 hdc, GdiPath **ppPath);
69 static BOOL32 PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
70    double angleStart, double angleEnd, BOOL32 addMoveTo);
71 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
72    double y, POINT32 *pPoint);
73 static void PATH_NormalizePoint(FLOAT_POINT corners[], const FLOAT_POINT
74    *pPoint, double *pX, double *pY);
75
76
77 /***********************************************************************
78  *           BeginPath16    (GDI.512)
79  */
80 BOOL16 WINAPI BeginPath16(HDC16 hdc)
81 {
82    return (BOOL16)BeginPath32((HDC32)hdc);
83 }
84
85
86 /***********************************************************************
87  *           BeginPath32    (GDI32.9)
88  */
89 BOOL32 WINAPI BeginPath32(HDC32 hdc)
90 {
91    GdiPath *pPath;
92    
93    /* Get pointer to path */
94    if(!PATH_GetPathFromHDC(hdc, &pPath))
95    {
96       SetLastError(ERROR_INVALID_HANDLE);
97       return FALSE;
98    }
99    
100    /* If path is already open, do nothing */
101    if(pPath->state==PATH_Open)
102       return TRUE;
103
104    /* Make sure that path is empty */
105    PATH_EmptyPath(pPath);
106
107    /* Initialize variables for new path */
108    pPath->newStroke=TRUE;
109    pPath->state=PATH_Open;
110    
111    return TRUE;
112 }
113
114
115 /***********************************************************************
116  *           EndPath16    (GDI.514)
117  */
118 BOOL16 WINAPI EndPath16(HDC16 hdc)
119 {
120    return (BOOL16)EndPath32((HDC32)hdc);
121 }
122
123
124 /***********************************************************************
125  *           EndPath32    (GDI32.78)
126  */
127 BOOL32 WINAPI EndPath32(HDC32 hdc)
128 {
129    GdiPath *pPath;
130    
131    /* Get pointer to path */
132    if(!PATH_GetPathFromHDC(hdc, &pPath))
133    {
134       SetLastError(ERROR_INVALID_HANDLE);
135       return FALSE;
136    }
137    
138    /* Check that path is currently being constructed */
139    if(pPath->state!=PATH_Open)
140    {
141       SetLastError(ERROR_CAN_NOT_COMPLETE);
142       return FALSE;
143    }
144    
145    /* Set flag to indicate that path is finished */
146    pPath->state=PATH_Closed;
147    
148    return TRUE;
149 }
150
151
152 /***********************************************************************
153  *           AbortPath16    (GDI.511)
154  */
155 BOOL16 WINAPI AbortPath16(HDC16 hdc)
156 {
157    return (BOOL16)AbortPath32((HDC32)hdc);
158 }
159
160
161 /******************************************************************************
162  * AbortPath32 [GDI32.1]
163  * Closes and discards paths from device context
164  *
165  * NOTES
166  *    Check that SetLastError is being called correctly
167  *
168  * PARAMS
169  *    hdc [I] Handle to device context
170  *
171  * RETURNS STD
172  */
173 BOOL32 WINAPI AbortPath32( HDC32 hdc )
174 {
175    GdiPath *pPath;
176    
177    /* Get pointer to path */
178    if(!PATH_GetPathFromHDC(hdc, &pPath))
179    {
180       SetLastError(ERROR_INVALID_PARAMETER);
181       return FALSE;
182    }
183    
184    /* Remove all entries from the path */
185    PATH_EmptyPath(pPath);
186
187    return TRUE;
188 }
189
190
191 /***********************************************************************
192  *           CloseFigure16    (GDI.513)
193  */
194 BOOL16 WINAPI CloseFigure16(HDC16 hdc)
195 {
196    return (BOOL16)CloseFigure32((HDC32)hdc);
197 }
198
199
200 /***********************************************************************
201  *           CloseFigure32    (GDI32.16)
202  *
203  * FIXME: Check that SetLastError is being called correctly 
204  */
205 BOOL32 WINAPI CloseFigure32(HDC32 hdc)
206 {
207    GdiPath *pPath;
208    
209    /* Get pointer to path */
210    if(!PATH_GetPathFromHDC(hdc, &pPath))
211    {
212       SetLastError(ERROR_INVALID_PARAMETER);
213       return FALSE;
214    }
215    
216    /* Check that path is open */
217    if(pPath->state!=PATH_Open)
218    {
219       SetLastError(ERROR_CAN_NOT_COMPLETE);
220       return FALSE;
221    }
222    
223    /* FIXME: Shouldn't we draw a line to the beginning of the figure? */
224    
225    /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
226    if(pPath->numEntriesUsed)
227    {
228       pPath->pFlags[pPath->numEntriesUsed-1]|=PT_CLOSEFIGURE;
229       pPath->newStroke=TRUE;
230    }
231
232    return TRUE;
233 }
234
235
236 /***********************************************************************
237  *           GetPath16    (GDI.517)
238  */
239 INT16 WINAPI GetPath16(HDC16 hdc, LPPOINT16 pPoints, LPBYTE pTypes,
240    INT16 nSize)
241 {
242    FIXME(gdi, "(%d,%p,%p): stub\n",hdc,pPoints,pTypes);
243
244    return 0;
245 }
246
247
248 /***********************************************************************
249  *           GetPath32    (GDI32.210)
250  */
251 INT32 WINAPI GetPath32(HDC32 hdc, LPPOINT32 pPoints, LPBYTE pTypes,
252    INT32 nSize)
253 {
254    GdiPath *pPath;
255    
256    /* Get pointer to path */
257    if(!PATH_GetPathFromHDC(hdc, &pPath))
258    {
259       SetLastError(ERROR_INVALID_PARAMETER);
260       return -1;
261    }
262    
263    /* Check that path is closed */
264    if(pPath->state!=PATH_Closed)
265    {
266       SetLastError(ERROR_CAN_NOT_COMPLETE);
267       return -1;
268    }
269    
270    if(nSize==0)
271       return pPath->numEntriesUsed;
272    else if(nSize<pPath->numEntriesUsed)
273    {
274       SetLastError(ERROR_INVALID_PARAMETER);
275       return -1;
276    }
277    else
278    {
279       memcpy(pPoints, pPath->pPoints, sizeof(POINT32)*pPath->numEntriesUsed);
280       memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
281
282       /* Convert the points to logical coordinates */
283       if(!DPtoLP32(hdc, pPoints, pPath->numEntriesUsed))
284       {
285          /* FIXME: Is this the correct value? */
286          SetLastError(ERROR_CAN_NOT_COMPLETE);
287          return -1;
288       }
289       
290       return pPath->numEntriesUsed;
291    }
292 }
293
294
295 /***********************************************************************
296  *           PathToRegion32    (GDI32.261)
297  *
298  * FIXME 
299  *   Check that SetLastError is being called correctly 
300  *
301  * The documentation does not state this explicitly, but a test under Windows
302  * shows that the region which is returned should be in device coordinates.
303  */
304 HRGN32 WINAPI PathToRegion32(HDC32 hdc)
305 {
306    GdiPath *pPath;
307    HRGN32  hrgnRval;
308
309    /* Get pointer to path */
310    if(!PATH_GetPathFromHDC(hdc, &pPath))
311    {
312       SetLastError(ERROR_INVALID_PARAMETER);
313       return 0;
314    }
315    
316    /* Check that path is closed */
317    if(pPath->state!=PATH_Closed)
318    {
319       SetLastError(ERROR_CAN_NOT_COMPLETE);
320       return 0;
321    }
322    
323    /* FIXME: Should we empty the path even if conversion failed? */
324    if(PATH_PathToRegion(pPath, GetPolyFillMode32(hdc), &hrgnRval))
325       PATH_EmptyPath(pPath);
326    else
327       hrgnRval=0;
328
329    return hrgnRval;
330 }
331
332
333 /***********************************************************************
334  *           FillPath32    (GDI32.100)
335  *
336  * FIXME
337  *    Check that SetLastError is being called correctly 
338  */
339 BOOL32 WINAPI FillPath32(HDC32 hdc)
340 {
341    GdiPath *pPath;
342    INT32   mapMode, graphicsMode;
343    SIZE32  ptViewportExt, ptWindowExt;
344    POINT32 ptViewportOrg, ptWindowOrg;
345    XFORM   xform;
346    HRGN32  hrgn;
347    
348    /* Get pointer to path */
349    if(!PATH_GetPathFromHDC(hdc, &pPath))
350    {
351       SetLastError(ERROR_INVALID_PARAMETER);
352       return FALSE;
353    }
354    
355    /* Check that path is closed */
356    if(pPath->state!=PATH_Closed)
357    {
358       SetLastError(ERROR_CAN_NOT_COMPLETE);
359       return FALSE;
360    }
361    
362    /* Construct a region from the path and fill it */
363    if(PATH_PathToRegion(pPath, GetPolyFillMode32(hdc), &hrgn))
364    {
365       /* Since PaintRgn interprets the region as being in logical coordinates
366        * but the points we store for the path are already in device
367        * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
368        * Using SaveDC to save information about the mapping mode / world
369        * transform would be easier but would require more overhead, especially
370        * now that SaveDC saves the current path.
371        */
372        
373       /* Save the information about the old mapping mode */
374       mapMode=GetMapMode32(hdc);
375       GetViewportExtEx32(hdc, &ptViewportExt);
376       GetViewportOrgEx32(hdc, &ptViewportOrg);
377       GetWindowExtEx32(hdc, &ptWindowExt);
378       GetWindowOrgEx32(hdc, &ptWindowOrg);
379       
380       /* Save world transform
381        * NB: The Windows documentation on world transforms would lead one to
382        * believe that this has to be done only in GM_ADVANCED; however, my
383        * tests show that resetting the graphics mode to GM_COMPATIBLE does
384        * not reset the world transform.
385        */
386       GetWorldTransform(hdc, &xform);
387       
388       /* Set MM_TEXT */
389       SetMapMode32(hdc, MM_TEXT);
390       
391       /* Paint the region */
392       PaintRgn32(hdc, hrgn);
393
394       /* Restore the old mapping mode */
395       SetMapMode32(hdc, mapMode);
396       SetViewportExtEx32(hdc, ptViewportExt.cx, ptViewportExt.cy, NULL);
397       SetViewportOrgEx32(hdc, ptViewportOrg.x, ptViewportOrg.y, NULL);
398       SetWindowExtEx32(hdc, ptWindowExt.cx, ptWindowExt.cy, NULL);
399       SetWindowOrgEx32(hdc, ptWindowOrg.x, ptWindowOrg.y, NULL);
400
401       /* Go to GM_ADVANCED temporarily to restore the world transform */
402       graphicsMode=GetGraphicsMode(hdc);
403       SetGraphicsMode(hdc, GM_ADVANCED);
404       SetWorldTransform(hdc, &xform);
405       SetGraphicsMode(hdc, graphicsMode);
406
407       /* Empty the path */
408       PATH_EmptyPath(pPath);
409       return TRUE;
410    }
411    else
412    {
413       /* FIXME: Should the path be emptied even if conversion failed? */
414       /* PATH_EmptyPath(pPath); */
415       return FALSE;
416    }
417 }
418
419
420 /***********************************************************************
421  *           SelectClipPath32    (GDI32.296)
422  * FIXME 
423  *  Check that SetLastError is being called correctly 
424  */
425 BOOL32 WINAPI SelectClipPath32(HDC32 hdc, INT32 iMode)
426 {
427    GdiPath *pPath;
428    HRGN32  hrgnPath;
429    BOOL32  success;
430    
431    /* Get pointer to path */
432    if(!PATH_GetPathFromHDC(hdc, &pPath))
433    {
434       SetLastError(ERROR_INVALID_PARAMETER);
435       return FALSE;
436    }
437    
438    /* Check that path is closed */
439    if(pPath->state!=PATH_Closed)
440    {
441       SetLastError(ERROR_CAN_NOT_COMPLETE);
442       return FALSE;
443    }
444    
445    /* Construct a region from the path */
446    if(PATH_PathToRegion(pPath, GetPolyFillMode32(hdc), &hrgnPath))
447    {
448       success = ExtSelectClipRgn( hdc, hrgnPath, iMode ) != ERROR;
449       DeleteObject32(hrgnPath);
450
451       /* Empty the path */
452       if(success)
453          PATH_EmptyPath(pPath);
454       /* FIXME: Should this function delete the path even if it failed? */
455
456       return success;
457    }
458    else
459       return FALSE;
460 }
461
462
463 /***********************************************************************
464  * Exported functions
465  */
466
467 /* PATH_InitGdiPath
468  *
469  * Initializes the GdiPath structure.
470  */
471 void PATH_InitGdiPath(GdiPath *pPath)
472 {
473    assert(pPath!=NULL);
474
475    pPath->state=PATH_Null;
476    pPath->pPoints=NULL;
477    pPath->pFlags=NULL;
478    pPath->numEntriesUsed=0;
479    pPath->numEntriesAllocated=0;
480 }
481
482 /* PATH_DestroyGdiPath
483  *
484  * Destroys a GdiPath structure (frees the memory in the arrays).
485  */
486 void PATH_DestroyGdiPath(GdiPath *pPath)
487 {
488    assert(pPath!=NULL);
489
490    free(pPath->pPoints);
491    free(pPath->pFlags);
492 }
493
494 /* PATH_AssignGdiPath
495  *
496  * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
497  * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
498  * not just the pointers. Since this means that the arrays in pPathDest may
499  * need to be resized, pPathDest should have been initialized using
500  * PATH_InitGdiPath (in C++, this function would be an assignment operator,
501  * not a copy constructor).
502  * Returns TRUE if successful, else FALSE.
503  */
504 BOOL32 PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
505 {
506    assert(pPathDest!=NULL && pPathSrc!=NULL);
507
508    /* Make sure destination arrays are big enough */
509    if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
510       return FALSE;
511
512    /* Perform the copy operation */
513    memcpy(pPathDest->pPoints, pPathSrc->pPoints,
514       sizeof(POINT32)*pPathSrc->numEntriesUsed);
515    memcpy(pPathDest->pFlags, pPathSrc->pFlags,
516       sizeof(INT32)*pPathSrc->numEntriesUsed);
517    pPathDest->state=pPathSrc->state;
518    pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
519    pPathDest->newStroke=pPathSrc->newStroke;
520
521    return TRUE;
522 }
523
524 /* PATH_MoveTo
525  *
526  * Should be called when a MoveTo is performed on a DC that has an
527  * open path. This starts a new stroke. Returns TRUE if successful, else
528  * FALSE.
529  */
530 BOOL32 PATH_MoveTo(HDC32 hdc)
531 {
532    GdiPath *pPath;
533    
534    /* Get pointer to path */
535    if(!PATH_GetPathFromHDC(hdc, &pPath))
536       return FALSE;
537    
538    /* Check that path is open */
539    if(pPath->state!=PATH_Open)
540       /* FIXME: Do we have to call SetLastError? */
541       return FALSE;
542
543    /* Start a new stroke */
544    pPath->newStroke=TRUE;
545
546    return TRUE;
547 }
548
549 /* PATH_LineTo
550  *
551  * Should be called when a LineTo is performed on a DC that has an
552  * open path. This adds a PT_LINETO entry to the path (and possibly
553  * a PT_MOVETO entry, if this is the first LineTo in a stroke).
554  * Returns TRUE if successful, else FALSE.
555  */
556 BOOL32 PATH_LineTo(HDC32 hdc, INT32 x, INT32 y)
557 {
558    GdiPath *pPath;
559    POINT32 point, pointCurPos;
560    
561    /* Get pointer to path */
562    if(!PATH_GetPathFromHDC(hdc, &pPath))
563       return FALSE;
564    
565    /* Check that path is open */
566    if(pPath->state!=PATH_Open)
567       return FALSE;
568
569    /* Convert point to device coordinates */
570    point.x=x;
571    point.y=y;
572    if(!LPtoDP32(hdc, &point, 1))
573       return FALSE;
574    
575    /* Add a PT_MOVETO if necessary */
576    if(pPath->newStroke)
577    {
578       pPath->newStroke=FALSE;
579       if(!GetCurrentPositionEx32(hdc, &pointCurPos) ||
580          !LPtoDP32(hdc, &pointCurPos, 1))
581          return FALSE;
582       if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
583          return FALSE;
584    }
585    
586    /* Add a PT_LINETO entry */
587    return PATH_AddEntry(pPath, &point, PT_LINETO);
588 }
589
590 /* PATH_Rectangle
591  *
592  * Should be called when a call to Rectangle is performed on a DC that has
593  * an open path. Returns TRUE if successful, else FALSE.
594  */
595 BOOL32 PATH_Rectangle(HDC32 hdc, INT32 x1, INT32 y1, INT32 x2, INT32 y2)
596 {
597    GdiPath *pPath;
598    POINT32 corners[2], pointTemp;
599    INT32   temp;
600
601    /* Get pointer to path */
602    if(!PATH_GetPathFromHDC(hdc, &pPath))
603       return FALSE;
604    
605    /* Check that path is open */
606    if(pPath->state!=PATH_Open)
607       return FALSE;
608
609    /* Convert points to device coordinates */
610    corners[0].x=x1;
611    corners[0].y=y1;
612    corners[1].x=x2;
613    corners[1].y=y2;
614    if(!LPtoDP32(hdc, corners, 2))
615       return FALSE;
616    
617    /* Make sure first corner is top left and second corner is bottom right */
618    if(corners[0].x>corners[1].x)
619    {
620       temp=corners[0].x;
621       corners[0].x=corners[1].x;
622       corners[1].x=temp;
623    }
624    if(corners[0].y>corners[1].y)
625    {
626       temp=corners[0].y;
627       corners[0].y=corners[1].y;
628       corners[1].y=temp;
629    }
630    
631    /* In GM_COMPATIBLE, don't include bottom and right edges */
632    if(GetGraphicsMode(hdc)==GM_COMPATIBLE)
633    {
634       corners[1].x--;
635       corners[1].y--;
636    }
637
638    /* Close any previous figure */
639    if(!CloseFigure32(hdc))
640    {
641       /* The CloseFigure call shouldn't have failed */
642       assert(FALSE);
643       return FALSE;
644    }
645
646    /* Add four points to the path */
647    pointTemp.x=corners[1].x;
648    pointTemp.y=corners[0].y;
649    if(!PATH_AddEntry(pPath, &pointTemp, PT_MOVETO))
650       return FALSE;
651    if(!PATH_AddEntry(pPath, corners, PT_LINETO))
652       return FALSE;
653    pointTemp.x=corners[0].x;
654    pointTemp.y=corners[1].y;
655    if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
656       return FALSE;
657    if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
658       return FALSE;
659
660    /* Close the rectangle figure */
661    if(!CloseFigure32(hdc))
662    {
663       /* The CloseFigure call shouldn't have failed */
664       assert(FALSE);
665       return FALSE;
666    }
667
668    return TRUE;
669 }
670
671 /* PATH_Ellipse
672  * 
673  * Should be called when a call to Ellipse is performed on a DC that has
674  * an open path. This adds four Bezier splines representing the ellipse
675  * to the path. Returns TRUE if successful, else FALSE.
676  */
677 BOOL32 PATH_Ellipse(HDC32 hdc, INT32 x1, INT32 y1, INT32 x2, INT32 y2)
678 {
679    /* TODO: This should probably be revised to call PATH_AngleArc */
680    /* (once it exists) */
681    return PATH_Arc(hdc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2);
682 }
683
684 /* PATH_Arc
685  *
686  * Should be called when a call to Arc is performed on a DC that has
687  * an open path. This adds up to five Bezier splines representing the arc
688  * to the path. Returns TRUE if successful, else FALSE.
689  */
690 BOOL32 PATH_Arc(HDC32 hdc, INT32 x1, INT32 y1, INT32 x2, INT32 y2,
691    INT32 xStart, INT32 yStart, INT32 xEnd, INT32 yEnd)
692 {
693    GdiPath     *pPath;
694    DC          *pDC;
695    double      angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
696                /* Initialize angleEndQuadrant to silence gcc's warning */
697    double      x, y;
698    FLOAT_POINT corners[2], pointStart, pointEnd;
699    BOOL32      start, end;
700    INT32       temp;
701
702    /* FIXME: This function should check for all possible error returns */
703    /* FIXME: Do we have to respect newStroke? */
704    
705    /* Get pointer to DC */
706    pDC=DC_GetDCPtr(hdc);
707    if(pDC==NULL)
708       return FALSE;
709
710    /* Get pointer to path */
711    if(!PATH_GetPathFromHDC(hdc, &pPath))
712       return FALSE;
713    
714    /* Check that path is open */
715    if(pPath->state!=PATH_Open)
716       return FALSE;
717
718    /* FIXME: Do we have to close the current figure? */
719    
720    /* Check for zero height / width */
721    /* FIXME: Only in GM_COMPATIBLE? */
722    if(x1==x2 || y1==y2)
723       return TRUE;
724    
725    /* Convert points to device coordinates */
726    corners[0].x=(FLOAT)x1;
727    corners[0].y=(FLOAT)y1;
728    corners[1].x=(FLOAT)x2;
729    corners[1].y=(FLOAT)y2;
730    pointStart.x=(FLOAT)xStart;
731    pointStart.y=(FLOAT)yStart;
732    pointEnd.x=(FLOAT)xEnd;
733    pointEnd.y=(FLOAT)yEnd;
734    INTERNAL_LPTODP_FLOAT(pDC, corners);
735    INTERNAL_LPTODP_FLOAT(pDC, corners+1);
736    INTERNAL_LPTODP_FLOAT(pDC, &pointStart);
737    INTERNAL_LPTODP_FLOAT(pDC, &pointEnd);
738
739    /* Make sure first corner is top left and second corner is bottom right */
740    if(corners[0].x>corners[1].x)
741    {
742       temp=corners[0].x;
743       corners[0].x=corners[1].x;
744       corners[1].x=temp;
745    }
746    if(corners[0].y>corners[1].y)
747    {
748       temp=corners[0].y;
749       corners[0].y=corners[1].y;
750       corners[1].y=temp;
751    }
752
753    /* Compute start and end angle */
754    PATH_NormalizePoint(corners, &pointStart, &x, &y);
755    angleStart=atan2(y, x);
756    PATH_NormalizePoint(corners, &pointEnd, &x, &y);
757    angleEnd=atan2(y, x);
758
759    /* Make sure the end angle is "on the right side" of the start angle */
760    if(GetArcDirection32(hdc)==AD_CLOCKWISE)
761    {
762       if(angleEnd<=angleStart)
763       {
764          angleEnd+=2*M_PI;
765          assert(angleEnd>=angleStart);
766       }
767    }
768    else
769    {
770       if(angleEnd>=angleStart)
771       {
772          angleEnd-=2*M_PI;
773          assert(angleEnd<=angleStart);
774       }
775    }
776
777    /* In GM_COMPATIBLE, don't include bottom and right edges */
778    if(GetGraphicsMode(hdc)==GM_COMPATIBLE)
779    {
780       corners[1].x--;
781       corners[1].y--;
782    }
783    
784    /* Add the arc to the path with one Bezier spline per quadrant that the
785     * arc spans */
786    start=TRUE;
787    end=FALSE;
788    do
789    {
790       /* Determine the start and end angles for this quadrant */
791       if(start)
792       {
793          angleStartQuadrant=angleStart;
794          if(GetArcDirection32(hdc)==AD_CLOCKWISE)
795             angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
796          else
797             angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
798       }
799       else
800       {
801          angleStartQuadrant=angleEndQuadrant;
802          if(GetArcDirection32(hdc)==AD_CLOCKWISE)
803             angleEndQuadrant+=M_PI_2;
804          else
805             angleEndQuadrant-=M_PI_2;
806       }
807
808       /* Have we reached the last part of the arc? */
809       if((GetArcDirection32(hdc)==AD_CLOCKWISE &&
810          angleEnd<angleEndQuadrant) ||
811          (GetArcDirection32(hdc)==AD_COUNTERCLOCKWISE &&
812          angleEnd>angleEndQuadrant))
813       {
814          /* Adjust the end angle for this quadrant */
815          angleEndQuadrant=angleEnd;
816          end=TRUE;
817       }
818
819       /* Add the Bezier spline to the path */
820       PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
821          start);
822       start=FALSE;
823    }  while(!end);
824
825    return TRUE;
826 }
827
828 /***********************************************************************
829  * Internal functions
830  */
831
832 /* PATH_PathToRegion
833  *
834  * Creates a region from the specified path using the specified polygon
835  * filling mode. The path is left unchanged. A handle to the region that
836  * was created is stored in *pHrgn. If successful, TRUE is returned; if an
837  * error occurs, SetLastError is called with the appropriate value and
838  * FALSE is returned.
839  */
840 static BOOL32 PATH_PathToRegion(const GdiPath *pPath, INT32 nPolyFillMode,
841    HRGN32 *pHrgn)
842 {
843    int    numStrokes, iStroke, i;
844    INT32  *pNumPointsInStroke;
845    HRGN32 hrgn;
846
847    assert(pPath!=NULL);
848    assert(pHrgn!=NULL);
849    
850    /* FIXME: What happens when number of points is zero? */
851    
852    /* First pass: Find out how many strokes there are in the path */
853    /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
854    numStrokes=0;
855    for(i=0; i<pPath->numEntriesUsed; i++)
856       if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
857          numStrokes++;
858
859    /* Allocate memory for number-of-points-in-stroke array */
860    pNumPointsInStroke=(int *)malloc(sizeof(int)*numStrokes);
861    if(!pNumPointsInStroke)
862    {
863       SetLastError(ERROR_NOT_ENOUGH_MEMORY);
864       return FALSE;
865    }
866    
867    /* Second pass: remember number of points in each polygon */
868    iStroke=-1;  /* Will get incremented to 0 at beginning of first stroke */
869    for(i=0; i<pPath->numEntriesUsed; i++)
870    {
871       /* Is this the beginning of a new stroke? */
872       if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
873       {
874          iStroke++;
875          pNumPointsInStroke[iStroke]=0;
876       }
877
878       pNumPointsInStroke[iStroke]++;
879    }
880
881    /* Create a region from the strokes */
882    hrgn=CreatePolyPolygonRgn32(pPath->pPoints, pNumPointsInStroke,
883       numStrokes, nPolyFillMode);
884    if(hrgn==(HRGN32)0)
885    {
886       SetLastError(ERROR_NOT_ENOUGH_MEMORY);
887       return FALSE;
888    }
889
890    /* Free memory for number-of-points-in-stroke array */
891    free(pNumPointsInStroke);
892
893    /* Success! */
894    *pHrgn=hrgn;
895    return TRUE;
896 }
897
898 /* PATH_EmptyPath
899  *
900  * Removes all entries from the path and sets the path state to PATH_Null.
901  */
902 static void PATH_EmptyPath(GdiPath *pPath)
903 {
904    assert(pPath!=NULL);
905
906    pPath->state=PATH_Null;
907    pPath->numEntriesUsed=0;
908 }
909
910 /* PATH_AddEntry
911  *
912  * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
913  * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
914  * successful, FALSE otherwise (e.g. if not enough memory was available).
915  */
916 BOOL32 PATH_AddEntry(GdiPath *pPath, const POINT32 *pPoint, BYTE flags)
917 {
918    assert(pPath!=NULL);
919    
920    /* FIXME: If newStroke is true, perhaps we want to check that we're
921     * getting a PT_MOVETO
922     */
923
924    /* Check that path is open */
925    if(pPath->state!=PATH_Open)
926       return FALSE;
927    
928    /* Reserve enough memory for an extra path entry */
929    if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
930       return FALSE;
931
932    /* Store information in path entry */
933    pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
934    pPath->pFlags[pPath->numEntriesUsed]=flags;
935
936    /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
937    if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
938       pPath->newStroke=TRUE;
939
940    /* Increment entry count */
941    pPath->numEntriesUsed++;
942
943    return TRUE;
944 }
945
946 /* PATH_ReserveEntries
947  *
948  * Ensures that at least "numEntries" entries (for points and flags) have
949  * been allocated; allocates larger arrays and copies the existing entries
950  * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
951  */
952 static BOOL32 PATH_ReserveEntries(GdiPath *pPath, INT32 numEntries)
953 {
954    INT32   numEntriesToAllocate;
955    POINT32 *pPointsNew;
956    BYTE    *pFlagsNew;
957    
958    assert(pPath!=NULL);
959    assert(numEntries>=0);
960
961    /* Do we have to allocate more memory? */
962    if(numEntries > pPath->numEntriesAllocated)
963    {
964       /* Find number of entries to allocate. We let the size of the array
965        * grow exponentially, since that will guarantee linear time
966        * complexity. */
967       if(pPath->numEntriesAllocated)
968       {
969          numEntriesToAllocate=pPath->numEntriesAllocated;
970          while(numEntriesToAllocate<numEntries)
971             numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
972                GROW_FACTOR_DENOM;
973       }
974       else
975          numEntriesToAllocate=NUM_ENTRIES_INITIAL;
976
977       /* Allocate new arrays */
978       pPointsNew=(POINT32 *)malloc(numEntriesToAllocate * sizeof(POINT32));
979       if(!pPointsNew)
980          return FALSE;
981       pFlagsNew=(BYTE *)malloc(numEntriesToAllocate * sizeof(BYTE));
982       if(!pFlagsNew)
983       {
984          free(pPointsNew);
985          return FALSE;
986       }
987
988       /* Copy old arrays to new arrays and discard old arrays */
989       if(pPath->pPoints)
990       {
991          assert(pPath->pFlags);
992
993          memcpy(pPointsNew, pPath->pPoints,
994              sizeof(POINT32)*pPath->numEntriesUsed);
995          memcpy(pFlagsNew, pPath->pFlags,
996              sizeof(BYTE)*pPath->numEntriesUsed);
997
998          free(pPath->pPoints);
999          free(pPath->pFlags);
1000       }
1001       pPath->pPoints=pPointsNew;
1002       pPath->pFlags=pFlagsNew;
1003       pPath->numEntriesAllocated=numEntriesToAllocate;
1004    }
1005
1006    return TRUE;
1007 }
1008
1009 /* PATH_GetPathFromHDC
1010  *
1011  * Retrieves a pointer to the GdiPath structure contained in an HDC and
1012  * places it in *ppPath. TRUE is returned if successful, FALSE otherwise.
1013  */
1014 static BOOL32 PATH_GetPathFromHDC(HDC32 hdc, GdiPath **ppPath)
1015 {
1016    DC *pDC;
1017
1018    pDC=DC_GetDCPtr(hdc);
1019    if(pDC)
1020    {
1021       *ppPath=&pDC->w.path;
1022       return TRUE;
1023    }
1024    else
1025       return FALSE;
1026 }
1027
1028 /* PATH_DoArcPart
1029  *
1030  * Creates a Bezier spline that corresponds to part of an arc and appends the
1031  * corresponding points to the path. The start and end angles are passed in
1032  * "angleStart" and "angleEnd"; these angles should span a quarter circle
1033  * at most. If "addMoveTo" is true, a PT_MOVETO entry for the first control
1034  * point is added to the path; otherwise, it is assumed that the current
1035  * position is equal to the first control point.
1036  */
1037 static BOOL32 PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
1038    double angleStart, double angleEnd, BOOL32 addMoveTo)
1039 {
1040    double  halfAngle, a;
1041    double  xNorm[4], yNorm[4];
1042    POINT32 point;
1043    int     i;
1044
1045    assert(fabs(angleEnd-angleStart)<=M_PI_2);
1046
1047    /* FIXME: Is there an easier way of computing this? */
1048
1049    /* Compute control points */
1050    halfAngle=(angleEnd-angleStart)/2.0;
1051    if(fabs(halfAngle)>1e-8)
1052    {
1053       a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
1054       xNorm[0]=cos(angleStart);
1055       yNorm[0]=sin(angleStart);
1056       xNorm[1]=xNorm[0] - a*yNorm[0];
1057       yNorm[1]=yNorm[0] + a*xNorm[0];
1058       xNorm[3]=cos(angleEnd);
1059       yNorm[3]=sin(angleEnd);
1060       xNorm[2]=xNorm[3] + a*yNorm[3];
1061       yNorm[2]=yNorm[3] - a*xNorm[3];
1062    }
1063    else
1064       for(i=0; i<4; i++)
1065       {
1066          xNorm[i]=cos(angleStart);
1067          yNorm[i]=sin(angleStart);
1068       }
1069    
1070    /* Add starting point to path if desired */
1071    if(addMoveTo)
1072    {
1073       PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1074       if(!PATH_AddEntry(pPath, &point, PT_MOVETO))
1075          return FALSE;
1076    }
1077
1078    /* Add remaining control points */
1079    for(i=1; i<4; i++)
1080    {
1081       PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1082       if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1083          return FALSE;
1084    }
1085
1086    return TRUE;
1087 }
1088
1089 /* PATH_ScaleNormalizedPoint
1090  *
1091  * Scales a normalized point (x, y) with respect to the box whose corners are
1092  * passed in "corners". The point is stored in "*pPoint". The normalized
1093  * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1094  * (1.0, 1.0) correspond to corners[1].
1095  */
1096 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
1097    double y, POINT32 *pPoint)
1098 {
1099    pPoint->x=GDI_ROUND( (double)corners[0].x +
1100       (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
1101    pPoint->y=GDI_ROUND( (double)corners[0].y +
1102       (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
1103 }
1104
1105 /* PATH_NormalizePoint
1106  *
1107  * Normalizes a point with respect to the box whose corners are passed in
1108  * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1109  */
1110 static void PATH_NormalizePoint(FLOAT_POINT corners[],
1111    const FLOAT_POINT *pPoint,
1112    double *pX, double *pY)
1113 {
1114    *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) *
1115       2.0 - 1.0;
1116    *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1117       2.0 - 1.0;
1118 }
1119
1120 /*******************************************************************
1121  *      FlattenPath32 [GDI32.103]
1122  *
1123  *
1124  */
1125 BOOL32 WINAPI FlattenPath32(HDC32 hdc)
1126 {
1127         FIXME(gdi, "FlattenPath, stub\n");
1128         return 0;
1129 }
1130
1131 /*******************************************************************
1132  *      StrokeAndFillPath [GDI32.352]
1133  *
1134  *
1135  */
1136 BOOL32 WINAPI StrokeAndFillPath32(HDC32 hdc)
1137 {
1138         FIXME(gdi, "StrokeAndFillPath, stub\n");
1139         return 0;
1140 }
1141
1142 /*******************************************************************
1143  *      StrokePath [GDI32.353]
1144  *
1145  *
1146  */
1147 BOOL32 WINAPI StrokePath32(HDC32 hdc)
1148 {
1149         FIXME(gdi, "StrokePath, stub\n");
1150         return 0;
1151 }
1152
1153 /*******************************************************************
1154  *      WidenPath [GDI32.360]
1155  *
1156  *
1157  */
1158 BOOL32 WINAPI WidenPath32(HDC32 hdc)
1159 {
1160         FIXME(gdi, "WidenPath, stub\n");
1161         return 0;
1162 }
1163