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_GetPathFromHDC(HDC hdc
, GdiPath
**ppPath
);
77 static BOOL
PATH_DoArcPart(GdiPath
*pPath
, FLOAT_POINT corners
[],
78 double angleStart
, double angleEnd
, BOOL addMoveTo
);
79 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners
[], double x
,
80 double y
, POINT
*pPoint
);
81 static void PATH_NormalizePoint(FLOAT_POINT corners
[], const FLOAT_POINT
82 *pPoint
, double *pX
, double *pY
);
85 /***********************************************************************
86 * BeginPath16 (GDI.512)
88 BOOL16 WINAPI
BeginPath16(HDC16 hdc
)
90 return (BOOL16
)BeginPath((HDC
)hdc
);
94 /***********************************************************************
97 BOOL WINAPI
BeginPath(HDC hdc
)
99 DC
*dc
= DC_GetDCPtr( hdc
);
103 SetLastError(ERROR_INVALID_HANDLE
);
107 if(dc
->funcs
->pBeginPath
)
108 return dc
->funcs
->pBeginPath(dc
);
112 /* If path is already open, do nothing */
113 if(pPath
->state
==PATH_Open
)
116 /* Make sure that path is empty */
117 PATH_EmptyPath(pPath
);
119 /* Initialize variables for new path */
120 pPath
->newStroke
=TRUE
;
121 pPath
->state
=PATH_Open
;
127 /***********************************************************************
128 * EndPath16 (GDI.514)
130 BOOL16 WINAPI
EndPath16(HDC16 hdc
)
132 return (BOOL16
)EndPath((HDC
)hdc
);
136 /***********************************************************************
139 BOOL WINAPI
EndPath(HDC hdc
)
141 DC
*dc
= DC_GetDCPtr( hdc
);
145 SetLastError(ERROR_INVALID_HANDLE
);
149 if(dc
->funcs
->pEndPath
)
150 return dc
->funcs
->pEndPath(dc
);
154 /* Check that path is currently being constructed */
155 if(pPath
->state
!=PATH_Open
)
157 SetLastError(ERROR_CAN_NOT_COMPLETE
);
161 /* Set flag to indicate that path is finished */
162 pPath
->state
=PATH_Closed
;
168 /***********************************************************************
169 * AbortPath16 (GDI.511)
171 BOOL16 WINAPI
AbortPath16(HDC16 hdc
)
173 return (BOOL16
)AbortPath((HDC
)hdc
);
177 /******************************************************************************
178 * AbortPath [GDI32.1]
179 * Closes and discards paths from device context
182 * Check that SetLastError is being called correctly
185 * hdc [I] Handle to device context
189 BOOL WINAPI
AbortPath( HDC hdc
)
191 DC
*dc
= DC_GetDCPtr( hdc
);
195 SetLastError(ERROR_INVALID_HANDLE
);
199 if(dc
->funcs
->pAbortPath
)
200 return dc
->funcs
->pAbortPath(dc
);
204 /* Remove all entries from the path */
205 PATH_EmptyPath(pPath
);
211 /***********************************************************************
212 * CloseFigure16 (GDI.513)
214 BOOL16 WINAPI
CloseFigure16(HDC16 hdc
)
216 return (BOOL16
)CloseFigure((HDC
)hdc
);
220 /***********************************************************************
221 * CloseFigure (GDI32.16)
223 * FIXME: Check that SetLastError is being called correctly
225 BOOL WINAPI
CloseFigure(HDC hdc
)
227 DC
*dc
= DC_GetDCPtr( hdc
);
231 SetLastError(ERROR_INVALID_HANDLE
);
235 if(dc
->funcs
->pCloseFigure
)
236 return dc
->funcs
->pCloseFigure(dc
);
240 /* Check that path is open */
241 if(pPath
->state
!=PATH_Open
)
243 SetLastError(ERROR_CAN_NOT_COMPLETE
);
247 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
248 if(pPath
->numEntriesUsed
)
250 pPath
->pFlags
[pPath
->numEntriesUsed
-1]|=PT_CLOSEFIGURE
;
251 pPath
->newStroke
=TRUE
;
258 /***********************************************************************
259 * GetPath16 (GDI.517)
261 INT16 WINAPI
GetPath16(HDC16 hdc
, LPPOINT16 pPoints
, LPBYTE pTypes
,
264 FIXME("(%d,%p,%p): stub\n",hdc
,pPoints
,pTypes
);
270 /***********************************************************************
271 * GetPath (GDI32.210)
273 INT WINAPI
GetPath(HDC hdc
, LPPOINT pPoints
, LPBYTE pTypes
,
278 /* Get pointer to path */
279 if(!PATH_GetPathFromHDC(hdc
, &pPath
))
281 SetLastError(ERROR_INVALID_PARAMETER
);
285 /* Check that path is closed */
286 if(pPath
->state
!=PATH_Closed
)
288 SetLastError(ERROR_CAN_NOT_COMPLETE
);
293 return pPath
->numEntriesUsed
;
294 else if(nSize
<pPath
->numEntriesUsed
)
296 SetLastError(ERROR_INVALID_PARAMETER
);
301 memcpy(pPoints
, pPath
->pPoints
, sizeof(POINT
)*pPath
->numEntriesUsed
);
302 memcpy(pTypes
, pPath
->pFlags
, sizeof(BYTE
)*pPath
->numEntriesUsed
);
304 /* Convert the points to logical coordinates */
305 if(!DPtoLP(hdc
, pPoints
, pPath
->numEntriesUsed
))
307 /* FIXME: Is this the correct value? */
308 SetLastError(ERROR_CAN_NOT_COMPLETE
);
312 return pPath
->numEntriesUsed
;
316 /***********************************************************************
317 * PathToRegion16 (GDI.518)
319 HRGN16 WINAPI
PathToRegion16(HDC16 hdc
)
321 return (HRGN16
) PathToRegion((HDC
) hdc
);
324 /***********************************************************************
325 * PathToRegion (GDI32.261)
328 * Check that SetLastError is being called correctly
330 * The documentation does not state this explicitly, but a test under Windows
331 * shows that the region which is returned should be in device coordinates.
333 HRGN WINAPI
PathToRegion(HDC hdc
)
338 /* Get pointer to path */
339 if(!PATH_GetPathFromHDC(hdc
, &pPath
))
341 SetLastError(ERROR_INVALID_PARAMETER
);
345 /* Check that path is closed */
346 if(pPath
->state
!=PATH_Closed
)
348 SetLastError(ERROR_CAN_NOT_COMPLETE
);
352 /* FIXME: Should we empty the path even if conversion failed? */
353 if(PATH_PathToRegion(pPath
, GetPolyFillMode(hdc
), &hrgnRval
))
354 PATH_EmptyPath(pPath
);
361 static BOOL
PATH_FillPath(HDC hdc
, GdiPath
*pPath
)
363 INT mapMode
, graphicsMode
;
364 SIZE ptViewportExt
, ptWindowExt
;
365 POINT ptViewportOrg
, ptWindowOrg
;
369 /* Check that path is closed */
370 if(pPath
->state
!=PATH_Closed
)
372 SetLastError(ERROR_CAN_NOT_COMPLETE
);
376 /* Construct a region from the path and fill it */
377 if(PATH_PathToRegion(pPath
, GetPolyFillMode(hdc
), &hrgn
))
379 /* Since PaintRgn interprets the region as being in logical coordinates
380 * but the points we store for the path are already in device
381 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
382 * Using SaveDC to save information about the mapping mode / world
383 * transform would be easier but would require more overhead, especially
384 * now that SaveDC saves the current path.
387 /* Save the information about the old mapping mode */
388 mapMode
=GetMapMode(hdc
);
389 GetViewportExtEx(hdc
, &ptViewportExt
);
390 GetViewportOrgEx(hdc
, &ptViewportOrg
);
391 GetWindowExtEx(hdc
, &ptWindowExt
);
392 GetWindowOrgEx(hdc
, &ptWindowOrg
);
394 /* Save world transform
395 * NB: The Windows documentation on world transforms would lead one to
396 * believe that this has to be done only in GM_ADVANCED; however, my
397 * tests show that resetting the graphics mode to GM_COMPATIBLE does
398 * not reset the world transform.
400 GetWorldTransform(hdc
, &xform
);
403 SetMapMode(hdc
, MM_TEXT
);
404 SetViewportOrgEx(hdc
, 0, 0, NULL
);
405 SetWindowOrgEx(hdc
, 0, 0, NULL
);
407 /* Paint the region */
410 /* Restore the old mapping mode */
411 SetMapMode(hdc
, mapMode
);
412 SetViewportExtEx(hdc
, ptViewportExt
.cx
, ptViewportExt
.cy
, NULL
);
413 SetViewportOrgEx(hdc
, ptViewportOrg
.x
, ptViewportOrg
.y
, NULL
);
414 SetWindowExtEx(hdc
, ptWindowExt
.cx
, ptWindowExt
.cy
, NULL
);
415 SetWindowOrgEx(hdc
, ptWindowOrg
.x
, ptWindowOrg
.y
, NULL
);
417 /* Go to GM_ADVANCED temporarily to restore the world transform */
418 graphicsMode
=GetGraphicsMode(hdc
);
419 SetGraphicsMode(hdc
, GM_ADVANCED
);
420 SetWorldTransform(hdc
, &xform
);
421 SetGraphicsMode(hdc
, graphicsMode
);
427 /***********************************************************************
428 * FillPath16 (GDI.515)
430 BOOL16 WINAPI
FillPath16(HDC16 hdc
)
432 return (BOOL16
) FillPath((HDC
) hdc
);
435 /***********************************************************************
436 * FillPath (GDI32.100)
439 * Check that SetLastError is being called correctly
441 BOOL WINAPI
FillPath(HDC hdc
)
443 DC
*dc
= DC_GetDCPtr( hdc
);
446 SetLastError(ERROR_INVALID_HANDLE
);
450 if(dc
->funcs
->pFillPath
)
451 return dc
->funcs
->pFillPath(dc
);
453 if(!PATH_FillPath(hdc
, &dc
->w
.path
))
456 /* FIXME: Should the path be emptied even if conversion failed? */
457 PATH_EmptyPath(&dc
->w
.path
);
461 /***********************************************************************
462 * SelectClipPath16 (GDI.519)
464 BOOL16 WINAPI
SelectClipPath16(HDC16 hdc
, INT16 iMode
)
466 return (BOOL16
) SelectClipPath((HDC
) hdc
, iMode
);
469 /***********************************************************************
470 * SelectClipPath (GDI32.296)
472 * Check that SetLastError is being called correctly
474 BOOL WINAPI
SelectClipPath(HDC hdc
, INT iMode
)
479 DC
*dc
= DC_GetDCPtr( hdc
);
482 SetLastError(ERROR_INVALID_HANDLE
);
486 if(dc
->funcs
->pSelectClipPath
)
487 return dc
->funcs
->pSelectClipPath(dc
, iMode
);
491 /* Check that path is closed */
492 if(pPath
->state
!=PATH_Closed
)
494 SetLastError(ERROR_CAN_NOT_COMPLETE
);
498 /* Construct a region from the path */
499 if(PATH_PathToRegion(pPath
, GetPolyFillMode(hdc
), &hrgnPath
))
501 success
= ExtSelectClipRgn( hdc
, hrgnPath
, iMode
) != ERROR
;
502 DeleteObject(hrgnPath
);
506 PATH_EmptyPath(pPath
);
507 /* FIXME: Should this function delete the path even if it failed? */
516 /***********************************************************************
522 * Initializes the GdiPath structure.
524 void PATH_InitGdiPath(GdiPath
*pPath
)
528 pPath
->state
=PATH_Null
;
531 pPath
->numEntriesUsed
=0;
532 pPath
->numEntriesAllocated
=0;
535 /* PATH_DestroyGdiPath
537 * Destroys a GdiPath structure (frees the memory in the arrays).
539 void PATH_DestroyGdiPath(GdiPath
*pPath
)
543 HeapFree( GetProcessHeap(), 0, pPath
->pPoints
);
544 HeapFree( GetProcessHeap(), 0, pPath
->pFlags
);
547 /* PATH_AssignGdiPath
549 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
550 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
551 * not just the pointers. Since this means that the arrays in pPathDest may
552 * need to be resized, pPathDest should have been initialized using
553 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
554 * not a copy constructor).
555 * Returns TRUE if successful, else FALSE.
557 BOOL
PATH_AssignGdiPath(GdiPath
*pPathDest
, const GdiPath
*pPathSrc
)
559 assert(pPathDest
!=NULL
&& pPathSrc
!=NULL
);
561 /* Make sure destination arrays are big enough */
562 if(!PATH_ReserveEntries(pPathDest
, pPathSrc
->numEntriesUsed
))
565 /* Perform the copy operation */
566 memcpy(pPathDest
->pPoints
, pPathSrc
->pPoints
,
567 sizeof(POINT
)*pPathSrc
->numEntriesUsed
);
568 memcpy(pPathDest
->pFlags
, pPathSrc
->pFlags
,
569 sizeof(BYTE
)*pPathSrc
->numEntriesUsed
);
571 pPathDest
->state
=pPathSrc
->state
;
572 pPathDest
->numEntriesUsed
=pPathSrc
->numEntriesUsed
;
573 pPathDest
->newStroke
=pPathSrc
->newStroke
;
580 * Should be called when a MoveTo is performed on a DC that has an
581 * open path. This starts a new stroke. Returns TRUE if successful, else
584 BOOL
PATH_MoveTo(HDC hdc
)
588 /* Get pointer to path */
589 if(!PATH_GetPathFromHDC(hdc
, &pPath
))
592 /* Check that path is open */
593 if(pPath
->state
!=PATH_Open
)
594 /* FIXME: Do we have to call SetLastError? */
597 /* Start a new stroke */
598 pPath
->newStroke
=TRUE
;
605 * Should be called when a LineTo is performed on a DC that has an
606 * open path. This adds a PT_LINETO entry to the path (and possibly
607 * a PT_MOVETO entry, if this is the first LineTo in a stroke).
608 * Returns TRUE if successful, else FALSE.
610 BOOL
PATH_LineTo(HDC hdc
, INT x
, INT y
)
613 POINT point
, pointCurPos
;
615 /* Get pointer to path */
616 if(!PATH_GetPathFromHDC(hdc
, &pPath
))
619 /* Check that path is open */
620 if(pPath
->state
!=PATH_Open
)
623 /* Convert point to device coordinates */
626 if(!LPtoDP(hdc
, &point
, 1))
629 /* Add a PT_MOVETO if necessary */
632 pPath
->newStroke
=FALSE
;
633 if(!GetCurrentPositionEx(hdc
, &pointCurPos
) ||
634 !LPtoDP(hdc
, &pointCurPos
, 1))
636 if(!PATH_AddEntry(pPath
, &pointCurPos
, PT_MOVETO
))
640 /* Add a PT_LINETO entry */
641 return PATH_AddEntry(pPath
, &point
, PT_LINETO
);
646 * Should be called when a call to Rectangle is performed on a DC that has
647 * an open path. Returns TRUE if successful, else FALSE.
649 BOOL
PATH_Rectangle(HDC hdc
, INT x1
, INT y1
, INT x2
, INT y2
)
652 POINT corners
[2], pointTemp
;
655 /* Get pointer to path */
656 if(!PATH_GetPathFromHDC(hdc
, &pPath
))
659 /* Check that path is open */
660 if(pPath
->state
!=PATH_Open
)
663 /* Convert points to device coordinates */
668 if(!LPtoDP(hdc
, corners
, 2))
671 /* Make sure first corner is top left and second corner is bottom right */
672 if(corners
[0].x
>corners
[1].x
)
675 corners
[0].x
=corners
[1].x
;
678 if(corners
[0].y
>corners
[1].y
)
681 corners
[0].y
=corners
[1].y
;
685 /* In GM_COMPATIBLE, don't include bottom and right edges */
686 if(GetGraphicsMode(hdc
)==GM_COMPATIBLE
)
692 /* Close any previous figure */
693 if(!CloseFigure(hdc
))
695 /* The CloseFigure call shouldn't have failed */
700 /* Add four points to the path */
701 pointTemp
.x
=corners
[1].x
;
702 pointTemp
.y
=corners
[0].y
;
703 if(!PATH_AddEntry(pPath
, &pointTemp
, PT_MOVETO
))
705 if(!PATH_AddEntry(pPath
, corners
, PT_LINETO
))
707 pointTemp
.x
=corners
[0].x
;
708 pointTemp
.y
=corners
[1].y
;
709 if(!PATH_AddEntry(pPath
, &pointTemp
, PT_LINETO
))
711 if(!PATH_AddEntry(pPath
, corners
+1, PT_LINETO
))
714 /* Close the rectangle figure */
715 if(!CloseFigure(hdc
))
717 /* The CloseFigure call shouldn't have failed */
727 * Should be called when a call to Ellipse is performed on a DC that has
728 * an open path. This adds four Bezier splines representing the ellipse
729 * to the path. Returns TRUE if successful, else FALSE.
731 BOOL
PATH_Ellipse(HDC hdc
, INT x1
, INT y1
, INT x2
, INT y2
)
733 /* TODO: This should probably be revised to call PATH_AngleArc */
734 /* (once it exists) */
735 return PATH_Arc(hdc
, x1
, y1
, x2
, y2
, x1
, (y1
+y2
)/2, x1
, (y1
+y2
)/2);
740 * Should be called when a call to Arc is performed on a DC that has
741 * an open path. This adds up to five Bezier splines representing the arc
742 * to the path. Returns TRUE if successful, else FALSE.
744 BOOL
PATH_Arc(HDC hdc
, INT x1
, INT y1
, INT x2
, INT y2
,
745 INT xStart
, INT yStart
, INT xEnd
, INT yEnd
)
749 double angleStart
, angleEnd
, angleStartQuadrant
, angleEndQuadrant
=0.0;
750 /* Initialize angleEndQuadrant to silence gcc's warning */
752 FLOAT_POINT corners
[2], pointStart
, pointEnd
;
756 /* FIXME: This function should check for all possible error returns */
757 /* FIXME: Do we have to respect newStroke? */
759 /* Get pointer to DC */
760 pDC
=DC_GetDCPtr(hdc
);
764 /* Get pointer to path */
765 if(!PATH_GetPathFromHDC(hdc
, &pPath
))
768 /* Check that path is open */
769 if(pPath
->state
!=PATH_Open
)
772 /* FIXME: Do we have to close the current figure? */
774 /* Check for zero height / width */
775 /* FIXME: Only in GM_COMPATIBLE? */
779 /* Convert points to device coordinates */
780 corners
[0].x
=(FLOAT
)x1
;
781 corners
[0].y
=(FLOAT
)y1
;
782 corners
[1].x
=(FLOAT
)x2
;
783 corners
[1].y
=(FLOAT
)y2
;
784 pointStart
.x
=(FLOAT
)xStart
;
785 pointStart
.y
=(FLOAT
)yStart
;
786 pointEnd
.x
=(FLOAT
)xEnd
;
787 pointEnd
.y
=(FLOAT
)yEnd
;
788 INTERNAL_LPTODP_FLOAT(pDC
, corners
);
789 INTERNAL_LPTODP_FLOAT(pDC
, corners
+1);
790 INTERNAL_LPTODP_FLOAT(pDC
, &pointStart
);
791 INTERNAL_LPTODP_FLOAT(pDC
, &pointEnd
);
793 /* Make sure first corner is top left and second corner is bottom right */
794 if(corners
[0].x
>corners
[1].x
)
797 corners
[0].x
=corners
[1].x
;
800 if(corners
[0].y
>corners
[1].y
)
803 corners
[0].y
=corners
[1].y
;
807 /* Compute start and end angle */
808 PATH_NormalizePoint(corners
, &pointStart
, &x
, &y
);
809 angleStart
=atan2(y
, x
);
810 PATH_NormalizePoint(corners
, &pointEnd
, &x
, &y
);
811 angleEnd
=atan2(y
, x
);
813 /* Make sure the end angle is "on the right side" of the start angle */
814 if(GetArcDirection(hdc
)==AD_CLOCKWISE
)
816 if(angleEnd
<=angleStart
)
819 assert(angleEnd
>=angleStart
);
824 if(angleEnd
>=angleStart
)
827 assert(angleEnd
<=angleStart
);
831 /* In GM_COMPATIBLE, don't include bottom and right edges */
832 if(GetGraphicsMode(hdc
)==GM_COMPATIBLE
)
838 /* Add the arc to the path with one Bezier spline per quadrant that the
844 /* Determine the start and end angles for this quadrant */
847 angleStartQuadrant
=angleStart
;
848 if(GetArcDirection(hdc
)==AD_CLOCKWISE
)
849 angleEndQuadrant
=(floor(angleStart
/M_PI_2
)+1.0)*M_PI_2
;
851 angleEndQuadrant
=(ceil(angleStart
/M_PI_2
)-1.0)*M_PI_2
;
855 angleStartQuadrant
=angleEndQuadrant
;
856 if(GetArcDirection(hdc
)==AD_CLOCKWISE
)
857 angleEndQuadrant
+=M_PI_2
;
859 angleEndQuadrant
-=M_PI_2
;
862 /* Have we reached the last part of the arc? */
863 if((GetArcDirection(hdc
)==AD_CLOCKWISE
&&
864 angleEnd
<angleEndQuadrant
) ||
865 (GetArcDirection(hdc
)==AD_COUNTERCLOCKWISE
&&
866 angleEnd
>angleEndQuadrant
))
868 /* Adjust the end angle for this quadrant */
869 angleEndQuadrant
=angleEnd
;
873 /* Add the Bezier spline to the path */
874 PATH_DoArcPart(pPath
, corners
, angleStartQuadrant
, angleEndQuadrant
,
882 BOOL
PATH_PolyBezierTo(HDC hdc
, const POINT
*pts
, DWORD cbPoints
)
888 if(!PATH_GetPathFromHDC(hdc
, &pPath
))
891 /* Check that path is open */
892 if(pPath
->state
!=PATH_Open
)
895 /* Add a PT_MOVETO if necessary */
898 pPath
->newStroke
=FALSE
;
899 if(!GetCurrentPositionEx(hdc
, &pt
) ||
900 !LPtoDP(hdc
, &pt
, 1))
902 if(!PATH_AddEntry(pPath
, &pt
, PT_MOVETO
))
906 for(i
= 0; i
< cbPoints
; i
++) {
908 if(!LPtoDP(hdc
, &pt
, 1))
910 PATH_AddEntry(pPath
, &pt
, PT_BEZIERTO
);
915 BOOL
PATH_PolyBezier(HDC hdc
, const POINT
*pts
, DWORD cbPoints
)
921 if(!PATH_GetPathFromHDC(hdc
, &pPath
))
924 /* Check that path is open */
925 if(pPath
->state
!=PATH_Open
)
928 for(i
= 0; i
< cbPoints
; i
++) {
930 if(!LPtoDP(hdc
, &pt
, 1))
932 PATH_AddEntry(pPath
, &pt
, (i
== 0) ? PT_MOVETO
: PT_BEZIERTO
);
937 BOOL
PATH_Polyline(HDC hdc
, const POINT
*pts
, DWORD cbPoints
)
943 if(!PATH_GetPathFromHDC(hdc
, &pPath
))
946 /* Check that path is open */
947 if(pPath
->state
!=PATH_Open
)
950 for(i
= 0; i
< cbPoints
; i
++) {
952 if(!LPtoDP(hdc
, &pt
, 1))
954 PATH_AddEntry(pPath
, &pt
, (i
== 0) ? PT_MOVETO
: PT_LINETO
);
959 BOOL
PATH_PolylineTo(HDC hdc
, const POINT
*pts
, DWORD cbPoints
)
965 if(!PATH_GetPathFromHDC(hdc
, &pPath
))
968 /* Check that path is open */
969 if(pPath
->state
!=PATH_Open
)
972 /* Add a PT_MOVETO if necessary */
975 pPath
->newStroke
=FALSE
;
976 if(!GetCurrentPositionEx(hdc
, &pt
) ||
977 !LPtoDP(hdc
, &pt
, 1))
979 if(!PATH_AddEntry(pPath
, &pt
, PT_MOVETO
))
983 for(i
= 0; i
< cbPoints
; i
++) {
985 if(!LPtoDP(hdc
, &pt
, 1))
987 PATH_AddEntry(pPath
, &pt
, PT_LINETO
);
994 BOOL
PATH_Polygon(HDC hdc
, const POINT
*pts
, DWORD cbPoints
)
1000 if(!PATH_GetPathFromHDC(hdc
, &pPath
))
1003 /* Check that path is open */
1004 if(pPath
->state
!=PATH_Open
)
1007 for(i
= 0; i
< cbPoints
; i
++) {
1009 if(!LPtoDP(hdc
, &pt
, 1))
1011 PATH_AddEntry(pPath
, &pt
, (i
== 0) ? PT_MOVETO
:
1012 ((i
== cbPoints
-1) ? PT_LINETO
| PT_CLOSEFIGURE
:
1018 BOOL
PATH_PolyPolygon( HDC hdc
, const POINT
* pts
, const INT
* counts
,
1025 if(!PATH_GetPathFromHDC(hdc
, &pPath
))
1028 /* Check that path is open */
1029 if(pPath
->state
!=PATH_Open
)
1032 for(i
= 0, poly
= 0; poly
< polygons
; poly
++) {
1033 for(point
= 0; point
< counts
[poly
]; point
++, i
++) {
1035 if(!LPtoDP(hdc
, &pt
, 1))
1037 if(point
== 0) startpt
= pt
;
1038 PATH_AddEntry(pPath
, &pt
, (point
== 0) ? PT_MOVETO
: PT_LINETO
);
1040 /* win98 adds an extra line to close the figure for some reason */
1041 PATH_AddEntry(pPath
, &startpt
, PT_LINETO
| PT_CLOSEFIGURE
);
1046 BOOL
PATH_PolyPolyline( HDC hdc
, const POINT
* pts
, const DWORD
* counts
,
1053 if(!PATH_GetPathFromHDC(hdc
, &pPath
))
1056 /* Check that path is open */
1057 if(pPath
->state
!=PATH_Open
)
1060 for(i
= 0, poly
= 0; poly
< polylines
; poly
++) {
1061 for(point
= 0; point
< counts
[poly
]; point
++, i
++) {
1063 if(!LPtoDP(hdc
, &pt
, 1))
1065 PATH_AddEntry(pPath
, &pt
, (point
== 0) ? PT_MOVETO
: PT_LINETO
);
1071 /***********************************************************************
1072 * Internal functions
1076 /* PATH_AddFlatBezier
1079 static BOOL
PATH_AddFlatBezier(GdiPath
*pPath
, POINT
*pt
, BOOL closed
)
1084 pts
= GDI_Bezier( pt
, 4, &no
);
1085 if(!pts
) return FALSE
;
1087 for(i
= 1; i
< no
; i
++)
1088 PATH_AddEntry(pPath
, &pts
[i
],
1089 (i
== no
-1 && closed
) ? PT_LINETO
| PT_CLOSEFIGURE
: PT_LINETO
);
1090 HeapFree( GetProcessHeap(), 0, pts
);
1096 * Replaces Beziers with line segments
1099 static BOOL
PATH_FlattenPath(GdiPath
*pPath
)
1104 memset(&newPath
, 0, sizeof(newPath
));
1105 newPath
.state
= PATH_Open
;
1106 for(srcpt
= 0; srcpt
< pPath
->numEntriesUsed
; srcpt
++) {
1107 switch(pPath
->pFlags
[srcpt
] & ~PT_CLOSEFIGURE
) {
1110 PATH_AddEntry(&newPath
, &pPath
->pPoints
[srcpt
],
1111 pPath
->pFlags
[srcpt
]);
1114 PATH_AddFlatBezier(&newPath
, &pPath
->pPoints
[srcpt
-1],
1115 pPath
->pFlags
[srcpt
+2] & PT_CLOSEFIGURE
);
1120 newPath
.state
= PATH_Closed
;
1121 PATH_AssignGdiPath(pPath
, &newPath
);
1122 PATH_EmptyPath(&newPath
);
1126 /* PATH_PathToRegion
1128 * Creates a region from the specified path using the specified polygon
1129 * filling mode. The path is left unchanged. A handle to the region that
1130 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
1131 * error occurs, SetLastError is called with the appropriate value and
1132 * FALSE is returned.
1134 static BOOL
PATH_PathToRegion(GdiPath
*pPath
, INT nPolyFillMode
,
1137 int numStrokes
, iStroke
, i
;
1138 INT
*pNumPointsInStroke
;
1141 assert(pPath
!=NULL
);
1142 assert(pHrgn
!=NULL
);
1144 PATH_FlattenPath(pPath
);
1146 /* FIXME: What happens when number of points is zero? */
1148 /* First pass: Find out how many strokes there are in the path */
1149 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
1151 for(i
=0; i
<pPath
->numEntriesUsed
; i
++)
1152 if((pPath
->pFlags
[i
] & ~PT_CLOSEFIGURE
) == PT_MOVETO
)
1155 /* Allocate memory for number-of-points-in-stroke array */
1156 pNumPointsInStroke
=(int *)HeapAlloc( GetProcessHeap(), 0,
1157 sizeof(int) * numStrokes
);
1158 if(!pNumPointsInStroke
)
1160 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1164 /* Second pass: remember number of points in each polygon */
1165 iStroke
=-1; /* Will get incremented to 0 at beginning of first stroke */
1166 for(i
=0; i
<pPath
->numEntriesUsed
; i
++)
1168 /* Is this the beginning of a new stroke? */
1169 if((pPath
->pFlags
[i
] & ~PT_CLOSEFIGURE
) == PT_MOVETO
)
1172 pNumPointsInStroke
[iStroke
]=0;
1175 pNumPointsInStroke
[iStroke
]++;
1178 /* Create a region from the strokes */
1179 hrgn
=CreatePolyPolygonRgn(pPath
->pPoints
, pNumPointsInStroke
,
1180 numStrokes
, nPolyFillMode
);
1183 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
1187 /* Free memory for number-of-points-in-stroke array */
1188 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke
);
1197 * Removes all entries from the path and sets the path state to PATH_Null.
1199 static void PATH_EmptyPath(GdiPath
*pPath
)
1201 assert(pPath
!=NULL
);
1203 pPath
->state
=PATH_Null
;
1204 pPath
->numEntriesUsed
=0;
1209 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1210 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1211 * successful, FALSE otherwise (e.g. if not enough memory was available).
1213 BOOL
PATH_AddEntry(GdiPath
*pPath
, const POINT
*pPoint
, BYTE flags
)
1215 assert(pPath
!=NULL
);
1217 /* FIXME: If newStroke is true, perhaps we want to check that we're
1218 * getting a PT_MOVETO
1220 TRACE("(%ld,%ld) - %d\n", pPoint
->x
, pPoint
->y
, flags
);
1222 /* Check that path is open */
1223 if(pPath
->state
!=PATH_Open
)
1226 /* Reserve enough memory for an extra path entry */
1227 if(!PATH_ReserveEntries(pPath
, pPath
->numEntriesUsed
+1))
1230 /* Store information in path entry */
1231 pPath
->pPoints
[pPath
->numEntriesUsed
]=*pPoint
;
1232 pPath
->pFlags
[pPath
->numEntriesUsed
]=flags
;
1234 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1235 if((flags
& PT_CLOSEFIGURE
) == PT_CLOSEFIGURE
)
1236 pPath
->newStroke
=TRUE
;
1238 /* Increment entry count */
1239 pPath
->numEntriesUsed
++;
1244 /* PATH_ReserveEntries
1246 * Ensures that at least "numEntries" entries (for points and flags) have
1247 * been allocated; allocates larger arrays and copies the existing entries
1248 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1250 static BOOL
PATH_ReserveEntries(GdiPath
*pPath
, INT numEntries
)
1252 INT numEntriesToAllocate
;
1256 assert(pPath
!=NULL
);
1257 assert(numEntries
>=0);
1259 /* Do we have to allocate more memory? */
1260 if(numEntries
> pPath
->numEntriesAllocated
)
1262 /* Find number of entries to allocate. We let the size of the array
1263 * grow exponentially, since that will guarantee linear time
1265 if(pPath
->numEntriesAllocated
)
1267 numEntriesToAllocate
=pPath
->numEntriesAllocated
;
1268 while(numEntriesToAllocate
<numEntries
)
1269 numEntriesToAllocate
=numEntriesToAllocate
*GROW_FACTOR_NUMER
/
1273 numEntriesToAllocate
=numEntries
;
1275 /* Allocate new arrays */
1276 pPointsNew
=(POINT
*)HeapAlloc( GetProcessHeap(), 0,
1277 numEntriesToAllocate
* sizeof(POINT
) );
1280 pFlagsNew
=(BYTE
*)HeapAlloc( GetProcessHeap(), 0,
1281 numEntriesToAllocate
* sizeof(BYTE
) );
1284 HeapFree( GetProcessHeap(), 0, pPointsNew
);
1288 /* Copy old arrays to new arrays and discard old arrays */
1291 assert(pPath
->pFlags
);
1293 memcpy(pPointsNew
, pPath
->pPoints
,
1294 sizeof(POINT
)*pPath
->numEntriesUsed
);
1295 memcpy(pFlagsNew
, pPath
->pFlags
,
1296 sizeof(BYTE
)*pPath
->numEntriesUsed
);
1298 HeapFree( GetProcessHeap(), 0, pPath
->pPoints
);
1299 HeapFree( GetProcessHeap(), 0, pPath
->pFlags
);
1301 pPath
->pPoints
=pPointsNew
;
1302 pPath
->pFlags
=pFlagsNew
;
1303 pPath
->numEntriesAllocated
=numEntriesToAllocate
;
1309 /* PATH_GetPathFromHDC
1311 * Retrieves a pointer to the GdiPath structure contained in an HDC and
1312 * places it in *ppPath. TRUE is returned if successful, FALSE otherwise.
1314 static BOOL
PATH_GetPathFromHDC(HDC hdc
, GdiPath
**ppPath
)
1318 pDC
=DC_GetDCPtr(hdc
);
1321 *ppPath
=&pDC
->w
.path
;
1330 * Creates a Bezier spline that corresponds to part of an arc and appends the
1331 * corresponding points to the path. The start and end angles are passed in
1332 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1333 * at most. If "addMoveTo" is true, a PT_MOVETO entry for the first control
1334 * point is added to the path; otherwise, it is assumed that the current
1335 * position is equal to the first control point.
1337 static BOOL
PATH_DoArcPart(GdiPath
*pPath
, FLOAT_POINT corners
[],
1338 double angleStart
, double angleEnd
, BOOL addMoveTo
)
1340 double halfAngle
, a
;
1341 double xNorm
[4], yNorm
[4];
1345 assert(fabs(angleEnd
-angleStart
)<=M_PI_2
);
1347 /* FIXME: Is there an easier way of computing this? */
1349 /* Compute control points */
1350 halfAngle
=(angleEnd
-angleStart
)/2.0;
1351 if(fabs(halfAngle
)>1e-8)
1353 a
=4.0/3.0*(1-cos(halfAngle
))/sin(halfAngle
);
1354 xNorm
[0]=cos(angleStart
);
1355 yNorm
[0]=sin(angleStart
);
1356 xNorm
[1]=xNorm
[0] - a
*yNorm
[0];
1357 yNorm
[1]=yNorm
[0] + a
*xNorm
[0];
1358 xNorm
[3]=cos(angleEnd
);
1359 yNorm
[3]=sin(angleEnd
);
1360 xNorm
[2]=xNorm
[3] + a
*yNorm
[3];
1361 yNorm
[2]=yNorm
[3] - a
*xNorm
[3];
1366 xNorm
[i
]=cos(angleStart
);
1367 yNorm
[i
]=sin(angleStart
);
1370 /* Add starting point to path if desired */
1373 PATH_ScaleNormalizedPoint(corners
, xNorm
[0], yNorm
[0], &point
);
1374 if(!PATH_AddEntry(pPath
, &point
, PT_MOVETO
))
1378 /* Add remaining control points */
1381 PATH_ScaleNormalizedPoint(corners
, xNorm
[i
], yNorm
[i
], &point
);
1382 if(!PATH_AddEntry(pPath
, &point
, PT_BEZIERTO
))
1389 /* PATH_ScaleNormalizedPoint
1391 * Scales a normalized point (x, y) with respect to the box whose corners are
1392 * passed in "corners". The point is stored in "*pPoint". The normalized
1393 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1394 * (1.0, 1.0) correspond to corners[1].
1396 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners
[], double x
,
1397 double y
, POINT
*pPoint
)
1399 pPoint
->x
=GDI_ROUND( (double)corners
[0].x
+
1400 (double)(corners
[1].x
-corners
[0].x
)*0.5*(x
+1.0) );
1401 pPoint
->y
=GDI_ROUND( (double)corners
[0].y
+
1402 (double)(corners
[1].y
-corners
[0].y
)*0.5*(y
+1.0) );
1405 /* PATH_NormalizePoint
1407 * Normalizes a point with respect to the box whose corners are passed in
1408 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1410 static void PATH_NormalizePoint(FLOAT_POINT corners
[],
1411 const FLOAT_POINT
*pPoint
,
1412 double *pX
, double *pY
)
1414 *pX
=(double)(pPoint
->x
-corners
[0].x
)/(double)(corners
[1].x
-corners
[0].x
) *
1416 *pY
=(double)(pPoint
->y
-corners
[0].y
)/(double)(corners
[1].y
-corners
[0].y
) *
1420 /*******************************************************************
1421 * FlattenPath16 [GDI.516]
1425 BOOL16 WINAPI
FlattenPath16(HDC16 hdc
)
1427 return (BOOL16
) FlattenPath((HDC
) hdc
);
1430 /*******************************************************************
1431 * FlattenPath [GDI32.103]
1435 BOOL WINAPI
FlattenPath(HDC hdc
)
1437 DC
*dc
= DC_GetDCPtr( hdc
);
1439 TRACE("%08x\n", hdc
);
1442 SetLastError(ERROR_INVALID_HANDLE
);
1446 if(dc
->funcs
->pFlattenPath
)
1447 return dc
->funcs
->pFlattenPath(dc
);
1449 pPath
= &dc
->w
.path
;
1450 if(pPath
->state
!= PATH_Closed
)
1452 return PATH_FlattenPath(pPath
);
1456 static BOOL
PATH_StrokePath(HDC hdc
, GdiPath
*pPath
)
1459 POINT ptLastMove
= {0,0};
1461 if(pPath
->state
!= PATH_Closed
)
1465 SetMapMode(hdc
, MM_TEXT
);
1466 SetViewportOrgEx(hdc
, 0, 0, NULL
);
1467 SetWindowOrgEx(hdc
, 0, 0, NULL
);
1468 for(i
= 0; i
< pPath
->numEntriesUsed
; i
++) {
1469 switch(pPath
->pFlags
[i
]) {
1471 TRACE("Got PT_MOVETO (%ld, %ld)\n",
1472 pPath
->pPoints
[i
].x
, pPath
->pPoints
[i
].y
);
1473 MoveToEx(hdc
, pPath
->pPoints
[i
].x
, pPath
->pPoints
[i
].y
, NULL
);
1474 ptLastMove
= pPath
->pPoints
[i
];
1477 case (PT_LINETO
| PT_CLOSEFIGURE
):
1478 TRACE("Got PT_LINETO (%ld, %ld)\n",
1479 pPath
->pPoints
[i
].x
, pPath
->pPoints
[i
].y
);
1480 LineTo(hdc
, pPath
->pPoints
[i
].x
, pPath
->pPoints
[i
].y
);
1483 TRACE("Got PT_BEZIERTO\n");
1484 if(pPath
->pFlags
[i
+1] != PT_BEZIERTO
||
1485 (pPath
->pFlags
[i
+2] & ~PT_CLOSEFIGURE
) != PT_BEZIERTO
) {
1486 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1489 PolyBezierTo(hdc
, &pPath
->pPoints
[i
], 3);
1493 ERR("Got path flag %d\n", (INT
)pPath
->pFlags
[i
]);
1496 if(pPath
->pFlags
[i
] & PT_CLOSEFIGURE
)
1497 LineTo(hdc
, ptLastMove
.x
, ptLastMove
.y
);
1504 /*******************************************************************
1505 * StrokeAndFillPath16 [GDI.520]
1509 BOOL16 WINAPI
StrokeAndFillPath16(HDC16 hdc
)
1511 return (BOOL16
) StrokeAndFillPath((HDC
) hdc
);
1514 /*******************************************************************
1515 * StrokeAndFillPath [GDI32.352]
1519 BOOL WINAPI
StrokeAndFillPath(HDC hdc
)
1521 DC
*dc
= DC_GetDCPtr( hdc
);
1525 SetLastError(ERROR_INVALID_HANDLE
);
1529 if(dc
->funcs
->pStrokeAndFillPath
)
1530 return dc
->funcs
->pStrokeAndFillPath(dc
);
1532 bRet
= PATH_FillPath(hdc
, &dc
->w
.path
);
1533 if(bRet
) bRet
= PATH_StrokePath(hdc
, &dc
->w
.path
);
1534 if(bRet
) PATH_EmptyPath(&dc
->w
.path
);
1538 /*******************************************************************
1539 * StrokePath16 [GDI.521]
1543 BOOL16 WINAPI
StrokePath16(HDC16 hdc
)
1545 return (BOOL16
) StrokePath((HDC
) hdc
);
1548 /*******************************************************************
1549 * StrokePath [GDI32.353]
1553 BOOL WINAPI
StrokePath(HDC hdc
)
1555 DC
*dc
= DC_GetDCPtr( hdc
);
1558 TRACE("(%08x)\n", hdc
);
1560 SetLastError(ERROR_INVALID_HANDLE
);
1564 if(dc
->funcs
->pStrokePath
)
1565 return dc
->funcs
->pStrokePath(dc
);
1567 pPath
= &dc
->w
.path
;
1568 PATH_StrokePath(hdc
, pPath
);
1569 PATH_EmptyPath(pPath
);
1573 /*******************************************************************
1574 * WidenPath16 [GDI.522]
1578 BOOL16 WINAPI
WidenPath16(HDC16 hdc
)
1580 return (BOOL16
) WidenPath((HDC
) hdc
);
1583 /*******************************************************************
1584 * WidenPath [GDI32.360]
1588 BOOL WINAPI
WidenPath(HDC hdc
)
1590 DC
*dc
= DC_GetDCPtr( hdc
);
1593 SetLastError(ERROR_INVALID_HANDLE
);
1597 if(dc
->funcs
->pWidenPath
)
1598 return dc
->funcs
->pWidenPath(dc
);