2 * Graphics paths (BeginPath, EndPath etc.)
4 * Copyright 1997, 1998 Martin Boehme
12 #if defined(HAVE_FLOAT_H)
21 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(gdi
)
26 /* Notes on the implementation
28 * The implementation is based on dynamically resizable arrays of points and
29 * flags. I dithered for a bit before deciding on this implementation, and
30 * I had even done a bit of work on a linked list version before switching
31 * to arrays. It's a bit of a tradeoff. When you use linked lists, the
32 * implementation of FlattenPath is easier, because you can rip the
33 * PT_BEZIERTO entries out of the middle of the list and link the
34 * corresponding PT_LINETO entries in. However, when you use arrays,
35 * PathToRegion becomes easier, since you can essentially just pass your array
36 * of points to CreatePolyPolygonRgn. Also, if I'd used linked lists, I would
37 * have had the extra effort of creating a chunk-based allocation scheme
38 * in order to use memory effectively. That's why I finally decided to use
39 * arrays. Note by the way that the array based implementation has the same
40 * linear time complexity that linked lists would have since the arrays grow
43 * The points are stored in the path in device coordinates. This is
44 * consistent with the way Windows does things (for instance, see the Win32
45 * SDK documentation for GetPath).
47 * The word "stroke" appears in several places (e.g. in the flag
48 * GdiPath.newStroke). A stroke consists of a PT_MOVETO followed by one or
49 * more PT_LINETOs or PT_BEZIERTOs, up to, but not including, the next
50 * PT_MOVETO. Note that this is not the same as the definition of a figure;
51 * a figure can contain several strokes.
53 * I modified the drawing functions (MoveTo, LineTo etc.) to test whether
54 * the path is open and to call the corresponding function in path.c if this
55 * is the case. A more elegant approach would be to modify the function
56 * pointers in the DC_FUNCTIONS structure; however, this would be a lot more
57 * complex. Also, the performance degradation caused by my approach in the
58 * case where no path is open is so small that it cannot be measured.
63 /* FIXME: A lot of stuff isn't implemented yet. There is much more to come. */
65 #define NUM_ENTRIES_INITIAL 16 /* Initial size of points / flags arrays */
66 #define GROW_FACTOR_NUMER 2 /* Numerator of grow factor for the array */
67 #define GROW_FACTOR_DENOM 1 /* Denominator of grow factor */
70 static BOOL
PATH_PathToRegion(GdiPath
*pPath
, INT nPolyFillMode
,
72 static void PATH_EmptyPath(GdiPath
*pPath
);
73 static BOOL
PATH_AddEntry(GdiPath
*pPath
, const POINT
*pPoint
,
75 static BOOL
PATH_ReserveEntries(GdiPath
*pPath
, INT numEntries
);
76 static BOOL
PATH_DoArcPart(GdiPath
*pPath
, FLOAT_POINT corners
[],
77 double angleStart
, double angleEnd
, BOOL addMoveTo
);
78 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners
[], double x
,
79 double y
, POINT
*pPoint
);
80 static void PATH_NormalizePoint(FLOAT_POINT corners
[], const FLOAT_POINT
81 *pPoint
, double *pX
, double *pY
);
84 /***********************************************************************
85 * BeginPath16 (GDI.512)
87 BOOL16 WINAPI
BeginPath16(HDC16 hdc
)
89 return (BOOL16
)BeginPath((HDC
)hdc
);
93 /***********************************************************************
96 BOOL WINAPI
BeginPath(HDC hdc
)
99 DC
*dc
= DC_GetDCPtr( hdc
);
101 if(!dc
) return FALSE
;
103 if(dc
->funcs
->pBeginPath
)
104 ret
= dc
->funcs
->pBeginPath(dc
);
107 /* If path is already open, do nothing */
108 if(dc
->w
.path
.state
!= PATH_Open
)
110 /* Make sure that path is empty */
111 PATH_EmptyPath(&dc
->w
.path
);
113 /* Initialize variables for new path */
114 dc
->w
.path
.newStroke
=TRUE
;
115 dc
->w
.path
.state
=PATH_Open
;
118 GDI_ReleaseObj( hdc
);
123 /***********************************************************************
124 * EndPath16 (GDI.514)
126 BOOL16 WINAPI
EndPath16(HDC16 hdc
)
128 return (BOOL16
)EndPath((HDC
)hdc
);
132 /***********************************************************************
135 BOOL WINAPI
EndPath(HDC hdc
)
138 DC
*dc
= DC_GetDCPtr( hdc
);
140 if(!dc
) return FALSE
;
142 if(dc
->funcs
->pEndPath
)
143 ret
= dc
->funcs
->pEndPath(dc
);
146 /* Check that path is currently being constructed */
147 if(dc
->w
.path
.state
!=PATH_Open
)
149 SetLastError(ERROR_CAN_NOT_COMPLETE
);
152 /* Set flag to indicate that path is finished */
153 else dc
->w
.path
.state
=PATH_Closed
;
155 GDI_ReleaseObj( hdc
);
160 /***********************************************************************
161 * AbortPath16 (GDI.511)
163 BOOL16 WINAPI
AbortPath16(HDC16 hdc
)
165 return (BOOL16
)AbortPath((HDC
)hdc
);
169 /******************************************************************************
170 * AbortPath [GDI32.1]
171 * Closes and discards paths from device context
174 * Check that SetLastError is being called correctly
177 * hdc [I] Handle to device context
181 BOOL WINAPI
AbortPath( HDC hdc
)
184 DC
*dc
= DC_GetDCPtr( hdc
);
186 if(!dc
) return FALSE
;
188 if(dc
->funcs
->pAbortPath
)
189 ret
= dc
->funcs
->pAbortPath(dc
);
190 else /* Remove all entries from the path */
191 PATH_EmptyPath( &dc
->w
.path
);
192 GDI_ReleaseObj( hdc
);
197 /***********************************************************************
198 * CloseFigure16 (GDI.513)
200 BOOL16 WINAPI
CloseFigure16(HDC16 hdc
)
202 return (BOOL16
)CloseFigure((HDC
)hdc
);
206 /***********************************************************************
207 * CloseFigure (GDI32.16)
209 * FIXME: Check that SetLastError is being called correctly
211 BOOL WINAPI
CloseFigure(HDC hdc
)
214 DC
*dc
= DC_GetDCPtr( hdc
);
216 if(!dc
) return FALSE
;
218 if(dc
->funcs
->pCloseFigure
)
219 ret
= dc
->funcs
->pCloseFigure(dc
);
222 /* Check that path is open */
223 if(dc
->w
.path
.state
!=PATH_Open
)
225 SetLastError(ERROR_CAN_NOT_COMPLETE
);
230 /* FIXME: Shouldn't we draw a line to the beginning of the
232 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
233 if(dc
->w
.path
.numEntriesUsed
)
235 dc
->w
.path
.pFlags
[dc
->w
.path
.numEntriesUsed
-1]|=PT_CLOSEFIGURE
;
236 dc
->w
.path
.newStroke
=TRUE
;
240 GDI_ReleaseObj( hdc
);
245 /***********************************************************************
246 * GetPath16 (GDI.517)
248 INT16 WINAPI
GetPath16(HDC16 hdc
, LPPOINT16 pPoints
, LPBYTE pTypes
,
251 FIXME("(%d,%p,%p): stub\n",hdc
,pPoints
,pTypes
);
257 /***********************************************************************
258 * GetPath (GDI32.210)
260 INT WINAPI
GetPath(HDC hdc
, LPPOINT pPoints
, LPBYTE pTypes
,
265 DC
*dc
= DC_GetDCPtr( hdc
);
271 /* Check that path is closed */
272 if(pPath
->state
!=PATH_Closed
)
274 SetLastError(ERROR_CAN_NOT_COMPLETE
);
279 ret
= pPath
->numEntriesUsed
;
280 else if(nSize
<pPath
->numEntriesUsed
)
282 SetLastError(ERROR_INVALID_PARAMETER
);
287 memcpy(pPoints
, pPath
->pPoints
, sizeof(POINT
)*pPath
->numEntriesUsed
);
288 memcpy(pTypes
, pPath
->pFlags
, sizeof(BYTE
)*pPath
->numEntriesUsed
);
290 /* Convert the points to logical coordinates */
291 if(!DPtoLP(hdc
, pPoints
, pPath
->numEntriesUsed
))
293 /* FIXME: Is this the correct value? */
294 SetLastError(ERROR_CAN_NOT_COMPLETE
);
297 else ret
= pPath
->numEntriesUsed
;
300 GDI_ReleaseObj( hdc
);
304 /***********************************************************************
305 * PathToRegion16 (GDI.518)
307 HRGN16 WINAPI
PathToRegion16(HDC16 hdc
)
309 return (HRGN16
) PathToRegion((HDC
) hdc
);
312 /***********************************************************************
313 * PathToRegion (GDI32.261)
316 * Check that SetLastError is being called correctly
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.
321 HRGN WINAPI
PathToRegion(HDC hdc
)
325 DC
*dc
= DC_GetDCPtr( hdc
);
327 /* Get pointer to path */
332 /* Check that path is closed */
333 if(pPath
->state
!=PATH_Closed
) SetLastError(ERROR_CAN_NOT_COMPLETE
);
336 /* FIXME: Should we empty the path even if conversion failed? */
337 if(PATH_PathToRegion(pPath
, GetPolyFillMode(hdc
), &hrgnRval
))
338 PATH_EmptyPath(pPath
);
342 GDI_ReleaseObj( hdc
);
346 static BOOL
PATH_FillPath(DC
*dc
, GdiPath
*pPath
)
348 INT mapMode
, graphicsMode
;
349 SIZE ptViewportExt
, ptWindowExt
;
350 POINT ptViewportOrg
, ptWindowOrg
;
354 if(dc
->funcs
->pFillPath
)
355 return dc
->funcs
->pFillPath(dc
);
357 /* Check that path is closed */
358 if(pPath
->state
!=PATH_Closed
)
360 SetLastError(ERROR_CAN_NOT_COMPLETE
);
364 /* Construct a region from the path and fill it */
365 if(PATH_PathToRegion(pPath
, dc
->w
.polyFillMode
, &hrgn
))
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.
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
);
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.
388 GetWorldTransform(dc
->hSelf
, &xform
);
391 SetMapMode(dc
->hSelf
, MM_TEXT
);
392 SetViewportOrgEx(dc
->hSelf
, 0, 0, NULL
);
393 SetWindowOrgEx(dc
->hSelf
, 0, 0, NULL
);
395 /* Paint the region */
396 PaintRgn(dc
->hSelf
, 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
);
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
);
415 /***********************************************************************
416 * FillPath16 (GDI.515)
418 BOOL16 WINAPI
FillPath16(HDC16 hdc
)
420 return (BOOL16
) FillPath((HDC
) hdc
);
423 /***********************************************************************
424 * FillPath (GDI32.100)
427 * Check that SetLastError is being called correctly
429 BOOL WINAPI
FillPath(HDC hdc
)
431 DC
*dc
= DC_GetDCPtr( hdc
);
434 if(!dc
) return FALSE
;
436 if(dc
->funcs
->pFillPath
)
437 bRet
= dc
->funcs
->pFillPath(dc
);
440 bRet
= PATH_FillPath(dc
, &dc
->w
.path
);
443 /* FIXME: Should the path be emptied even if conversion
445 PATH_EmptyPath(&dc
->w
.path
);
448 GDI_ReleaseObj( hdc
);
452 /***********************************************************************
453 * SelectClipPath16 (GDI.519)
455 BOOL16 WINAPI
SelectClipPath16(HDC16 hdc
, INT16 iMode
)
457 return (BOOL16
) SelectClipPath((HDC
) hdc
, iMode
);
460 /***********************************************************************
461 * SelectClipPath (GDI32.296)
463 * Check that SetLastError is being called correctly
465 BOOL WINAPI
SelectClipPath(HDC hdc
, INT iMode
)
469 BOOL success
= FALSE
;
470 DC
*dc
= DC_GetDCPtr( hdc
);
472 if(!dc
) return FALSE
;
474 if(dc
->funcs
->pSelectClipPath
)
475 success
= dc
->funcs
->pSelectClipPath(dc
, iMode
);
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
))
486 success
= ExtSelectClipRgn( hdc
, hrgnPath
, iMode
) != ERROR
;
487 DeleteObject(hrgnPath
);
491 PATH_EmptyPath(pPath
);
492 /* FIXME: Should this function delete the path even if it failed? */
495 GDI_ReleaseObj( hdc
);
500 /***********************************************************************
506 * Initializes the GdiPath structure.
508 void PATH_InitGdiPath(GdiPath
*pPath
)
512 pPath
->state
=PATH_Null
;
515 pPath
->numEntriesUsed
=0;
516 pPath
->numEntriesAllocated
=0;
519 /* PATH_DestroyGdiPath
521 * Destroys a GdiPath structure (frees the memory in the arrays).
523 void PATH_DestroyGdiPath(GdiPath
*pPath
)
527 if (pPath
->pPoints
) HeapFree( GetProcessHeap(), 0, pPath
->pPoints
);
528 if (pPath
->pFlags
) HeapFree( GetProcessHeap(), 0, pPath
->pFlags
);
531 /* PATH_AssignGdiPath
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.
541 BOOL
PATH_AssignGdiPath(GdiPath
*pPathDest
, const GdiPath
*pPathSrc
)
543 assert(pPathDest
!=NULL
&& pPathSrc
!=NULL
);
545 /* Make sure destination arrays are big enough */
546 if(!PATH_ReserveEntries(pPathDest
, pPathSrc
->numEntriesUsed
))
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
);
555 pPathDest
->state
=pPathSrc
->state
;
556 pPathDest
->numEntriesUsed
=pPathSrc
->numEntriesUsed
;
557 pPathDest
->newStroke
=pPathSrc
->newStroke
;
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
568 BOOL
PATH_MoveTo(DC
*dc
)
570 GdiPath
*pPath
= &dc
->w
.path
;
572 /* Check that path is open */
573 if(pPath
->state
!=PATH_Open
)
574 /* FIXME: Do we have to call SetLastError? */
577 /* Start a new stroke */
578 pPath
->newStroke
=TRUE
;
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.
590 BOOL
PATH_LineTo(DC
*dc
, INT x
, INT y
)
592 GdiPath
*pPath
= &dc
->w
.path
;
593 POINT point
, pointCurPos
;
595 /* Check that path is open */
596 if(pPath
->state
!=PATH_Open
)
599 /* Convert point to device coordinates */
602 if(!LPtoDP(dc
->hSelf
, &point
, 1))
605 /* Add a PT_MOVETO if necessary */
608 pPath
->newStroke
=FALSE
;
609 pointCurPos
.x
= dc
->w
.CursPosX
;
610 pointCurPos
.y
= dc
->w
.CursPosY
;
611 if(!LPtoDP(dc
->hSelf
, &pointCurPos
, 1))
613 if(!PATH_AddEntry(pPath
, &pointCurPos
, PT_MOVETO
))
617 /* Add a PT_LINETO entry */
618 return PATH_AddEntry(pPath
, &point
, PT_LINETO
);
623 * Should be called when a call to Rectangle is performed on a DC that has
624 * an open path. Returns TRUE if successful, else FALSE.
626 BOOL
PATH_Rectangle(DC
*dc
, INT x1
, INT y1
, INT x2
, INT y2
)
628 GdiPath
*pPath
= &dc
->w
.path
;
629 POINT corners
[2], pointTemp
;
632 /* Check that path is open */
633 if(pPath
->state
!=PATH_Open
)
636 /* Convert points to device coordinates */
641 if(!LPtoDP(dc
->hSelf
, corners
, 2))
644 /* Make sure first corner is top left and second corner is bottom right */
645 if(corners
[0].x
>corners
[1].x
)
648 corners
[0].x
=corners
[1].x
;
651 if(corners
[0].y
>corners
[1].y
)
654 corners
[0].y
=corners
[1].y
;
658 /* In GM_COMPATIBLE, don't include bottom and right edges */
659 if(dc
->w
.GraphicsMode
==GM_COMPATIBLE
)
665 /* Close any previous figure */
666 if(!CloseFigure(dc
->hSelf
))
668 /* The CloseFigure call shouldn't have failed */
673 /* Add four points to the path */
674 pointTemp
.x
=corners
[1].x
;
675 pointTemp
.y
=corners
[0].y
;
676 if(!PATH_AddEntry(pPath
, &pointTemp
, PT_MOVETO
))
678 if(!PATH_AddEntry(pPath
, corners
, PT_LINETO
))
680 pointTemp
.x
=corners
[0].x
;
681 pointTemp
.y
=corners
[1].y
;
682 if(!PATH_AddEntry(pPath
, &pointTemp
, PT_LINETO
))
684 if(!PATH_AddEntry(pPath
, corners
+1, PT_LINETO
))
687 /* Close the rectangle figure */
688 if(!CloseFigure(dc
->hSelf
))
690 /* The CloseFigure call shouldn't have failed */
700 * Should be called when a call to Ellipse is performed on a DC that has
701 * an open path. This adds four Bezier splines representing the ellipse
702 * to the path. Returns TRUE if successful, else FALSE.
704 BOOL
PATH_Ellipse(DC
*dc
, INT x1
, INT y1
, INT x2
, INT y2
)
706 /* TODO: This should probably be revised to call PATH_AngleArc */
707 /* (once it exists) */
708 return PATH_Arc(dc
, x1
, y1
, x2
, y2
, x1
, (y1
+y2
)/2, x1
, (y1
+y2
)/2);
713 * Should be called when a call to Arc is performed on a DC that has
714 * an open path. This adds up to five Bezier splines representing the arc
715 * to the path. Returns TRUE if successful, else FALSE.
717 BOOL
PATH_Arc(DC
*dc
, INT x1
, INT y1
, INT x2
, INT y2
,
718 INT xStart
, INT yStart
, INT xEnd
, INT yEnd
)
720 GdiPath
*pPath
= &dc
->w
.path
;
721 double angleStart
, angleEnd
, angleStartQuadrant
, angleEndQuadrant
=0.0;
722 /* Initialize angleEndQuadrant to silence gcc's warning */
724 FLOAT_POINT corners
[2], pointStart
, pointEnd
;
728 /* FIXME: This function should check for all possible error returns */
729 /* FIXME: Do we have to respect newStroke? */
731 /* Check that path is open */
732 if(pPath
->state
!=PATH_Open
)
735 /* FIXME: Do we have to close the current figure? */
737 /* Check for zero height / width */
738 /* FIXME: Only in GM_COMPATIBLE? */
742 /* Convert points to device coordinates */
743 corners
[0].x
=(FLOAT
)x1
;
744 corners
[0].y
=(FLOAT
)y1
;
745 corners
[1].x
=(FLOAT
)x2
;
746 corners
[1].y
=(FLOAT
)y2
;
747 pointStart
.x
=(FLOAT
)xStart
;
748 pointStart
.y
=(FLOAT
)yStart
;
749 pointEnd
.x
=(FLOAT
)xEnd
;
750 pointEnd
.y
=(FLOAT
)yEnd
;
751 INTERNAL_LPTODP_FLOAT(dc
, corners
);
752 INTERNAL_LPTODP_FLOAT(dc
, corners
+1);
753 INTERNAL_LPTODP_FLOAT(dc
, &pointStart
);
754 INTERNAL_LPTODP_FLOAT(dc
, &pointEnd
);
756 /* Make sure first corner is top left and second corner is bottom right */
757 if(corners
[0].x
>corners
[1].x
)
760 corners
[0].x
=corners
[1].x
;
763 if(corners
[0].y
>corners
[1].y
)
766 corners
[0].y
=corners
[1].y
;
770 /* Compute start and end angle */
771 PATH_NormalizePoint(corners
, &pointStart
, &x
, &y
);
772 angleStart
=atan2(y
, x
);
773 PATH_NormalizePoint(corners
, &pointEnd
, &x
, &y
);
774 angleEnd
=atan2(y
, x
);
776 /* Make sure the end angle is "on the right side" of the start angle */
777 if(dc
->w
.ArcDirection
==AD_CLOCKWISE
)
779 if(angleEnd
<=angleStart
)
782 assert(angleEnd
>=angleStart
);
787 if(angleEnd
>=angleStart
)
790 assert(angleEnd
<=angleStart
);
794 /* In GM_COMPATIBLE, don't include bottom and right edges */
795 if(dc
->w
.GraphicsMode
==GM_COMPATIBLE
)
801 /* Add the arc to the path with one Bezier spline per quadrant that the
807 /* Determine the start and end angles for this quadrant */
810 angleStartQuadrant
=angleStart
;
811 if(dc
->w
.ArcDirection
==AD_CLOCKWISE
)
812 angleEndQuadrant
=(floor(angleStart
/M_PI_2
)+1.0)*M_PI_2
;
814 angleEndQuadrant
=(ceil(angleStart
/M_PI_2
)-1.0)*M_PI_2
;
818 angleStartQuadrant
=angleEndQuadrant
;
819 if(dc
->w
.ArcDirection
==AD_CLOCKWISE
)
820 angleEndQuadrant
+=M_PI_2
;
822 angleEndQuadrant
-=M_PI_2
;
825 /* Have we reached the last part of the arc? */
826 if((dc
->w
.ArcDirection
==AD_CLOCKWISE
&&
827 angleEnd
<angleEndQuadrant
) ||
828 (dc
->w
.ArcDirection
==AD_COUNTERCLOCKWISE
&&
829 angleEnd
>angleEndQuadrant
))
831 /* Adjust the end angle for this quadrant */
832 angleEndQuadrant
=angleEnd
;
836 /* Add the Bezier spline to the path */
837 PATH_DoArcPart(pPath
, corners
, angleStartQuadrant
, angleEndQuadrant
,
845 BOOL
PATH_PolyBezierTo(DC
*dc
, const POINT
*pts
, DWORD cbPoints
)
847 GdiPath
*pPath
= &dc
->w
.path
;
851 /* Check that path is open */
852 if(pPath
->state
!=PATH_Open
)
855 /* Add a PT_MOVETO if necessary */
858 pPath
->newStroke
=FALSE
;
859 pt
.x
= dc
->w
.CursPosX
;
860 pt
.y
= dc
->w
.CursPosY
;
861 if(!LPtoDP(dc
->hSelf
, &pt
, 1))
863 if(!PATH_AddEntry(pPath
, &pt
, PT_MOVETO
))
867 for(i
= 0; i
< cbPoints
; i
++) {
869 if(!LPtoDP(dc
->hSelf
, &pt
, 1))
871 PATH_AddEntry(pPath
, &pt
, PT_BEZIERTO
);
876 BOOL
PATH_PolyBezier(DC
*dc
, const POINT
*pts
, DWORD cbPoints
)
878 GdiPath
*pPath
= &dc
->w
.path
;
882 /* Check that path is open */
883 if(pPath
->state
!=PATH_Open
)
886 for(i
= 0; i
< cbPoints
; i
++) {
888 if(!LPtoDP(dc
->hSelf
, &pt
, 1))
890 PATH_AddEntry(pPath
, &pt
, (i
== 0) ? PT_MOVETO
: PT_BEZIERTO
);
895 BOOL
PATH_Polyline(DC
*dc
, const POINT
*pts
, DWORD cbPoints
)
897 GdiPath
*pPath
= &dc
->w
.path
;
901 /* Check that path is open */
902 if(pPath
->state
!=PATH_Open
)
905 for(i
= 0; i
< cbPoints
; i
++) {
907 if(!LPtoDP(dc
->hSelf
, &pt
, 1))
909 PATH_AddEntry(pPath
, &pt
, (i
== 0) ? PT_MOVETO
: PT_LINETO
);
914 BOOL
PATH_PolylineTo(DC
*dc
, const POINT
*pts
, DWORD cbPoints
)
916 GdiPath
*pPath
= &dc
->w
.path
;
920 /* Check that path is open */
921 if(pPath
->state
!=PATH_Open
)
924 /* Add a PT_MOVETO if necessary */
927 pPath
->newStroke
=FALSE
;
928 pt
.x
= dc
->w
.CursPosX
;
929 pt
.y
= dc
->w
.CursPosY
;
930 if(!LPtoDP(dc
->hSelf
, &pt
, 1))
932 if(!PATH_AddEntry(pPath
, &pt
, PT_MOVETO
))
936 for(i
= 0; i
< cbPoints
; i
++) {
938 if(!LPtoDP(dc
->hSelf
, &pt
, 1))
940 PATH_AddEntry(pPath
, &pt
, PT_LINETO
);
947 BOOL
PATH_Polygon(DC
*dc
, const POINT
*pts
, DWORD cbPoints
)
949 GdiPath
*pPath
= &dc
->w
.path
;
953 /* Check that path is open */
954 if(pPath
->state
!=PATH_Open
)
957 for(i
= 0; i
< cbPoints
; i
++) {
959 if(!LPtoDP(dc
->hSelf
, &pt
, 1))
961 PATH_AddEntry(pPath
, &pt
, (i
== 0) ? PT_MOVETO
:
962 ((i
== cbPoints
-1) ? PT_LINETO
| PT_CLOSEFIGURE
:
968 BOOL
PATH_PolyPolygon( DC
*dc
, const POINT
* pts
, const INT
* counts
,
971 GdiPath
*pPath
= &dc
->w
.path
;
975 /* Check that path is open */
976 if(pPath
->state
!=PATH_Open
)
979 for(i
= 0, poly
= 0; poly
< polygons
; poly
++) {
980 for(point
= 0; point
< counts
[poly
]; point
++, i
++) {
982 if(!LPtoDP(dc
->hSelf
, &pt
, 1))
984 if(point
== 0) startpt
= pt
;
985 PATH_AddEntry(pPath
, &pt
, (point
== 0) ? PT_MOVETO
: PT_LINETO
);
987 /* win98 adds an extra line to close the figure for some reason */
988 PATH_AddEntry(pPath
, &startpt
, PT_LINETO
| PT_CLOSEFIGURE
);
993 BOOL
PATH_PolyPolyline( DC
*dc
, const POINT
* pts
, const DWORD
* counts
,
996 GdiPath
*pPath
= &dc
->w
.path
;
1000 /* Check that path is open */
1001 if(pPath
->state
!=PATH_Open
)
1004 for(i
= 0, poly
= 0; poly
< polylines
; poly
++) {
1005 for(point
= 0; point
< counts
[poly
]; point
++, i
++) {
1007 if(!LPtoDP(dc
->hSelf
, &pt
, 1))
1009 PATH_AddEntry(pPath
, &pt
, (point
== 0) ? PT_MOVETO
: PT_LINETO
);
1015 /***********************************************************************
1016 * Internal functions
1020 /* PATH_AddFlatBezier
1023 static BOOL
PATH_AddFlatBezier(GdiPath
*pPath
, POINT
*pt
, BOOL closed
)
1028 pts
= GDI_Bezier( pt
, 4, &no
);
1029 if(!pts
) return FALSE
;
1031 for(i
= 1; i
< no
; i
++)
1032 PATH_AddEntry(pPath
, &pts
[i
],
1033 (i
== no
-1 && closed
) ? PT_LINETO
| PT_CLOSEFIGURE
: PT_LINETO
);
1034 HeapFree( GetProcessHeap(), 0, pts
);
1040 * Replaces Beziers with line segments
1043 static BOOL
PATH_FlattenPath(GdiPath
*pPath
)
1048 memset(&newPath
, 0, sizeof(newPath
));
1049 newPath
.state
= PATH_Open
;
1050 for(srcpt
= 0; srcpt
< pPath
->numEntriesUsed
; srcpt
++) {
1051 switch(pPath
->pFlags
[srcpt
] & ~PT_CLOSEFIGURE
) {
1054 PATH_AddEntry(&newPath
, &pPath
->pPoints
[srcpt
],
1055 pPath
->pFlags
[srcpt
]);
1058 PATH_AddFlatBezier(&newPath
, &pPath
->pPoints
[srcpt
-1],
1059 pPath
->pFlags
[srcpt
+2] & PT_CLOSEFIGURE
);
1064 newPath
.state
= PATH_Closed
;
1065 PATH_AssignGdiPath(pPath
, &newPath
);
1066 PATH_EmptyPath(&newPath
);
1070 /* PATH_PathToRegion
1072 * Creates a region from the specified path using the specified polygon
1073 * filling mode. The path is left unchanged. A handle to the region that
1074 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
1075 * error occurs, SetLastError is called with the appropriate value and
1076 * FALSE is returned.
1078 static BOOL
PATH_PathToRegion(GdiPath
*pPath
, INT nPolyFillMode
,
1081 int numStrokes
, iStroke
, i
;
1082 INT
*pNumPointsInStroke
;
1085 assert(pPath
!=NULL
);
1086 assert(pHrgn
!=NULL
);
1088 PATH_FlattenPath(pPath
);
1090 /* FIXME: What happens when number of points is zero? */
1092 /* First pass: Find out how many strokes there are in the path */
1093 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
1095 for(i
=0; i
<pPath
->numEntriesUsed
; i
++)
1096 if((pPath
->pFlags
[i
] & ~PT_CLOSEFIGURE
) == PT_MOVETO
)
1099 /* Allocate memory for number-of-points-in-stroke array */
1100 pNumPointsInStroke
=(int *)HeapAlloc( GetProcessHeap(), 0,
1101 sizeof(int) * numStrokes
);
1102 if(!pNumPointsInStroke
)
1104 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1108 /* Second pass: remember number of points in each polygon */
1109 iStroke
=-1; /* Will get incremented to 0 at beginning of first stroke */
1110 for(i
=0; i
<pPath
->numEntriesUsed
; i
++)
1112 /* Is this the beginning of a new stroke? */
1113 if((pPath
->pFlags
[i
] & ~PT_CLOSEFIGURE
) == PT_MOVETO
)
1116 pNumPointsInStroke
[iStroke
]=0;
1119 pNumPointsInStroke
[iStroke
]++;
1122 /* Create a region from the strokes */
1123 hrgn
=CreatePolyPolygonRgn(pPath
->pPoints
, pNumPointsInStroke
,
1124 numStrokes
, nPolyFillMode
);
1127 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1131 /* Free memory for number-of-points-in-stroke array */
1132 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke
);
1141 * Removes all entries from the path and sets the path state to PATH_Null.
1143 static void PATH_EmptyPath(GdiPath
*pPath
)
1145 assert(pPath
!=NULL
);
1147 pPath
->state
=PATH_Null
;
1148 pPath
->numEntriesUsed
=0;
1153 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1154 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1155 * successful, FALSE otherwise (e.g. if not enough memory was available).
1157 BOOL
PATH_AddEntry(GdiPath
*pPath
, const POINT
*pPoint
, BYTE flags
)
1159 assert(pPath
!=NULL
);
1161 /* FIXME: If newStroke is true, perhaps we want to check that we're
1162 * getting a PT_MOVETO
1164 TRACE("(%ld,%ld) - %d\n", pPoint
->x
, pPoint
->y
, flags
);
1166 /* Check that path is open */
1167 if(pPath
->state
!=PATH_Open
)
1170 /* Reserve enough memory for an extra path entry */
1171 if(!PATH_ReserveEntries(pPath
, pPath
->numEntriesUsed
+1))
1174 /* Store information in path entry */
1175 pPath
->pPoints
[pPath
->numEntriesUsed
]=*pPoint
;
1176 pPath
->pFlags
[pPath
->numEntriesUsed
]=flags
;
1178 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1179 if((flags
& PT_CLOSEFIGURE
) == PT_CLOSEFIGURE
)
1180 pPath
->newStroke
=TRUE
;
1182 /* Increment entry count */
1183 pPath
->numEntriesUsed
++;
1188 /* PATH_ReserveEntries
1190 * Ensures that at least "numEntries" entries (for points and flags) have
1191 * been allocated; allocates larger arrays and copies the existing entries
1192 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1194 static BOOL
PATH_ReserveEntries(GdiPath
*pPath
, INT numEntries
)
1196 INT numEntriesToAllocate
;
1200 assert(pPath
!=NULL
);
1201 assert(numEntries
>=0);
1203 /* Do we have to allocate more memory? */
1204 if(numEntries
> pPath
->numEntriesAllocated
)
1206 /* Find number of entries to allocate. We let the size of the array
1207 * grow exponentially, since that will guarantee linear time
1209 if(pPath
->numEntriesAllocated
)
1211 numEntriesToAllocate
=pPath
->numEntriesAllocated
;
1212 while(numEntriesToAllocate
<numEntries
)
1213 numEntriesToAllocate
=numEntriesToAllocate
*GROW_FACTOR_NUMER
/
1217 numEntriesToAllocate
=numEntries
;
1219 /* Allocate new arrays */
1220 pPointsNew
=(POINT
*)HeapAlloc( GetProcessHeap(), 0,
1221 numEntriesToAllocate
* sizeof(POINT
) );
1224 pFlagsNew
=(BYTE
*)HeapAlloc( GetProcessHeap(), 0,
1225 numEntriesToAllocate
* sizeof(BYTE
) );
1228 HeapFree( GetProcessHeap(), 0, pPointsNew
);
1232 /* Copy old arrays to new arrays and discard old arrays */
1235 assert(pPath
->pFlags
);
1237 memcpy(pPointsNew
, pPath
->pPoints
,
1238 sizeof(POINT
)*pPath
->numEntriesUsed
);
1239 memcpy(pFlagsNew
, pPath
->pFlags
,
1240 sizeof(BYTE
)*pPath
->numEntriesUsed
);
1242 HeapFree( GetProcessHeap(), 0, pPath
->pPoints
);
1243 HeapFree( GetProcessHeap(), 0, pPath
->pFlags
);
1245 pPath
->pPoints
=pPointsNew
;
1246 pPath
->pFlags
=pFlagsNew
;
1247 pPath
->numEntriesAllocated
=numEntriesToAllocate
;
1255 * Creates a Bezier spline that corresponds to part of an arc and appends the
1256 * corresponding points to the path. The start and end angles are passed in
1257 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1258 * at most. If "addMoveTo" is true, a PT_MOVETO entry for the first control
1259 * point is added to the path; otherwise, it is assumed that the current
1260 * position is equal to the first control point.
1262 static BOOL
PATH_DoArcPart(GdiPath
*pPath
, FLOAT_POINT corners
[],
1263 double angleStart
, double angleEnd
, BOOL addMoveTo
)
1265 double halfAngle
, a
;
1266 double xNorm
[4], yNorm
[4];
1270 assert(fabs(angleEnd
-angleStart
)<=M_PI_2
);
1272 /* FIXME: Is there an easier way of computing this? */
1274 /* Compute control points */
1275 halfAngle
=(angleEnd
-angleStart
)/2.0;
1276 if(fabs(halfAngle
)>1e-8)
1278 a
=4.0/3.0*(1-cos(halfAngle
))/sin(halfAngle
);
1279 xNorm
[0]=cos(angleStart
);
1280 yNorm
[0]=sin(angleStart
);
1281 xNorm
[1]=xNorm
[0] - a
*yNorm
[0];
1282 yNorm
[1]=yNorm
[0] + a
*xNorm
[0];
1283 xNorm
[3]=cos(angleEnd
);
1284 yNorm
[3]=sin(angleEnd
);
1285 xNorm
[2]=xNorm
[3] + a
*yNorm
[3];
1286 yNorm
[2]=yNorm
[3] - a
*xNorm
[3];
1291 xNorm
[i
]=cos(angleStart
);
1292 yNorm
[i
]=sin(angleStart
);
1295 /* Add starting point to path if desired */
1298 PATH_ScaleNormalizedPoint(corners
, xNorm
[0], yNorm
[0], &point
);
1299 if(!PATH_AddEntry(pPath
, &point
, PT_MOVETO
))
1303 /* Add remaining control points */
1306 PATH_ScaleNormalizedPoint(corners
, xNorm
[i
], yNorm
[i
], &point
);
1307 if(!PATH_AddEntry(pPath
, &point
, PT_BEZIERTO
))
1314 /* PATH_ScaleNormalizedPoint
1316 * Scales a normalized point (x, y) with respect to the box whose corners are
1317 * passed in "corners". The point is stored in "*pPoint". The normalized
1318 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1319 * (1.0, 1.0) correspond to corners[1].
1321 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners
[], double x
,
1322 double y
, POINT
*pPoint
)
1324 pPoint
->x
=GDI_ROUND( (double)corners
[0].x
+
1325 (double)(corners
[1].x
-corners
[0].x
)*0.5*(x
+1.0) );
1326 pPoint
->y
=GDI_ROUND( (double)corners
[0].y
+
1327 (double)(corners
[1].y
-corners
[0].y
)*0.5*(y
+1.0) );
1330 /* PATH_NormalizePoint
1332 * Normalizes a point with respect to the box whose corners are passed in
1333 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1335 static void PATH_NormalizePoint(FLOAT_POINT corners
[],
1336 const FLOAT_POINT
*pPoint
,
1337 double *pX
, double *pY
)
1339 *pX
=(double)(pPoint
->x
-corners
[0].x
)/(double)(corners
[1].x
-corners
[0].x
) *
1341 *pY
=(double)(pPoint
->y
-corners
[0].y
)/(double)(corners
[1].y
-corners
[0].y
) *
1345 /*******************************************************************
1346 * FlattenPath16 [GDI.516]
1350 BOOL16 WINAPI
FlattenPath16(HDC16 hdc
)
1352 return (BOOL16
) FlattenPath((HDC
) hdc
);
1355 /*******************************************************************
1356 * FlattenPath [GDI32.103]
1360 BOOL WINAPI
FlattenPath(HDC hdc
)
1363 DC
*dc
= DC_GetDCPtr( hdc
);
1365 if(!dc
) return FALSE
;
1367 if(dc
->funcs
->pFlattenPath
) ret
= dc
->funcs
->pFlattenPath(dc
);
1370 GdiPath
*pPath
= &dc
->w
.path
;
1371 if(pPath
->state
!= PATH_Closed
)
1372 ret
= PATH_FlattenPath(pPath
);
1374 GDI_ReleaseObj( hdc
);
1379 static BOOL
PATH_StrokePath(DC
*dc
, GdiPath
*pPath
)
1382 POINT ptLastMove
= {0,0};
1384 if(dc
->funcs
->pStrokePath
)
1385 return dc
->funcs
->pStrokePath(dc
);
1387 if(pPath
->state
!= PATH_Closed
)
1391 SetMapMode(dc
->hSelf
, MM_TEXT
);
1392 SetViewportOrgEx(dc
->hSelf
, 0, 0, NULL
);
1393 SetWindowOrgEx(dc
->hSelf
, 0, 0, NULL
);
1394 for(i
= 0; i
< pPath
->numEntriesUsed
; i
++) {
1395 switch(pPath
->pFlags
[i
]) {
1397 TRACE("Got PT_MOVETO (%ld, %ld)\n",
1398 pPath
->pPoints
[i
].x
, pPath
->pPoints
[i
].y
);
1399 MoveToEx(dc
->hSelf
, pPath
->pPoints
[i
].x
, pPath
->pPoints
[i
].y
, NULL
);
1400 ptLastMove
= pPath
->pPoints
[i
];
1403 case (PT_LINETO
| PT_CLOSEFIGURE
):
1404 TRACE("Got PT_LINETO (%ld, %ld)\n",
1405 pPath
->pPoints
[i
].x
, pPath
->pPoints
[i
].y
);
1406 LineTo(dc
->hSelf
, pPath
->pPoints
[i
].x
, pPath
->pPoints
[i
].y
);
1409 TRACE("Got PT_BEZIERTO\n");
1410 if(pPath
->pFlags
[i
+1] != PT_BEZIERTO
||
1411 (pPath
->pFlags
[i
+2] & ~PT_CLOSEFIGURE
) != PT_BEZIERTO
) {
1412 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1415 PolyBezierTo(dc
->hSelf
, &pPath
->pPoints
[i
], 3);
1419 ERR("Got path flag %d\n", (INT
)pPath
->pFlags
[i
]);
1422 if(pPath
->pFlags
[i
] & PT_CLOSEFIGURE
)
1423 LineTo(dc
->hSelf
, ptLastMove
.x
, ptLastMove
.y
);
1425 RestoreDC(dc
->hSelf
, -1);
1430 /*******************************************************************
1431 * StrokeAndFillPath16 [GDI.520]
1435 BOOL16 WINAPI
StrokeAndFillPath16(HDC16 hdc
)
1437 return (BOOL16
) StrokeAndFillPath((HDC
) hdc
);
1440 /*******************************************************************
1441 * StrokeAndFillPath [GDI32.352]
1445 BOOL WINAPI
StrokeAndFillPath(HDC hdc
)
1447 DC
*dc
= DC_GetDCPtr( hdc
);
1450 if(!dc
) return FALSE
;
1452 if(dc
->funcs
->pStrokeAndFillPath
)
1453 bRet
= dc
->funcs
->pStrokeAndFillPath(dc
);
1456 bRet
= PATH_FillPath(dc
, &dc
->w
.path
);
1457 if(bRet
) bRet
= PATH_StrokePath(dc
, &dc
->w
.path
);
1458 if(bRet
) PATH_EmptyPath(&dc
->w
.path
);
1460 GDI_ReleaseObj( hdc
);
1464 /*******************************************************************
1465 * StrokePath16 [GDI.521]
1469 BOOL16 WINAPI
StrokePath16(HDC16 hdc
)
1471 return (BOOL16
) StrokePath((HDC
) hdc
);
1474 /*******************************************************************
1475 * StrokePath [GDI32.353]
1479 BOOL WINAPI
StrokePath(HDC hdc
)
1481 DC
*dc
= DC_GetDCPtr( hdc
);
1485 TRACE("(%08x)\n", hdc
);
1486 if(!dc
) return FALSE
;
1488 if(dc
->funcs
->pStrokePath
)
1489 bRet
= dc
->funcs
->pStrokePath(dc
);
1492 pPath
= &dc
->w
.path
;
1493 bRet
= PATH_StrokePath(dc
, pPath
);
1494 PATH_EmptyPath(pPath
);
1496 GDI_ReleaseObj( hdc
);
1500 /*******************************************************************
1501 * WidenPath16 [GDI.522]
1505 BOOL16 WINAPI
WidenPath16(HDC16 hdc
)
1507 return (BOOL16
) WidenPath((HDC
) hdc
);
1510 /*******************************************************************
1511 * WidenPath [GDI32.360]
1515 BOOL WINAPI
WidenPath(HDC hdc
)
1517 DC
*dc
= DC_GetDCPtr( hdc
);
1520 if(!dc
) return FALSE
;
1522 if(dc
->funcs
->pWidenPath
)
1523 ret
= dc
->funcs
->pWidenPath(dc
);
1526 GDI_ReleaseObj( hdc
);