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 /* FIXME: Shouldn't we draw a line to the beginning of the figure? */
249 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
250 if(pPath->numEntriesUsed)
252 pPath->pFlags[pPath->numEntriesUsed-1]|=PT_CLOSEFIGURE;
253 pPath->newStroke=TRUE;
260 /***********************************************************************
261 * GetPath16 (GDI.517)
263 INT16 WINAPI GetPath16(HDC16 hdc, LPPOINT16 pPoints, LPBYTE pTypes,
266 FIXME("(%d,%p,%p): stub\n",hdc,pPoints,pTypes);
272 /***********************************************************************
273 * GetPath (GDI32.210)
275 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
280 /* Get pointer to path */
281 if(!PATH_GetPathFromHDC(hdc, &pPath))
283 SetLastError(ERROR_INVALID_PARAMETER);
287 /* Check that path is closed */
288 if(pPath->state!=PATH_Closed)
290 SetLastError(ERROR_CAN_NOT_COMPLETE);
295 return pPath->numEntriesUsed;
296 else if(nSize<pPath->numEntriesUsed)
298 SetLastError(ERROR_INVALID_PARAMETER);
303 memcpy(pPoints, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
304 memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
306 /* Convert the points to logical coordinates */
307 if(!DPtoLP(hdc, pPoints, pPath->numEntriesUsed))
309 /* FIXME: Is this the correct value? */
310 SetLastError(ERROR_CAN_NOT_COMPLETE);
314 return pPath->numEntriesUsed;
318 /***********************************************************************
319 * PathToRegion16 (GDI.518)
321 HRGN16 WINAPI PathToRegion16(HDC16 hdc)
323 return (HRGN16) PathToRegion((HDC) hdc);
326 /***********************************************************************
327 * PathToRegion (GDI32.261)
330 * Check that SetLastError is being called correctly
332 * The documentation does not state this explicitly, but a test under Windows
333 * shows that the region which is returned should be in device coordinates.
335 HRGN WINAPI PathToRegion(HDC hdc)
340 /* Get pointer to path */
341 if(!PATH_GetPathFromHDC(hdc, &pPath))
343 SetLastError(ERROR_INVALID_PARAMETER);
347 /* Check that path is closed */
348 if(pPath->state!=PATH_Closed)
350 SetLastError(ERROR_CAN_NOT_COMPLETE);
354 /* FIXME: Should we empty the path even if conversion failed? */
355 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
356 PATH_EmptyPath(pPath);
363 /***********************************************************************
364 * FillPath16 (GDI.515)
366 BOOL16 WINAPI FillPath16(HDC16 hdc)
368 return (BOOL16) FillPath((HDC) hdc);
371 /***********************************************************************
372 * FillPath (GDI32.100)
375 * Check that SetLastError is being called correctly
377 BOOL WINAPI FillPath(HDC hdc)
380 INT mapMode, graphicsMode;
381 SIZE ptViewportExt, ptWindowExt;
382 POINT ptViewportOrg, ptWindowOrg;
385 DC *dc = DC_GetDCPtr( hdc );
388 SetLastError(ERROR_INVALID_HANDLE);
392 if(dc->funcs->pFillPath)
393 return dc->funcs->pFillPath(dc);
397 /* Check that path is closed */
398 if(pPath->state!=PATH_Closed)
400 SetLastError(ERROR_CAN_NOT_COMPLETE);
404 /* Construct a region from the path and fill it */
405 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgn))
407 /* Since PaintRgn interprets the region as being in logical coordinates
408 * but the points we store for the path are already in device
409 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
410 * Using SaveDC to save information about the mapping mode / world
411 * transform would be easier but would require more overhead, especially
412 * now that SaveDC saves the current path.
415 /* Save the information about the old mapping mode */
416 mapMode=GetMapMode(hdc);
417 GetViewportExtEx(hdc, &ptViewportExt);
418 GetViewportOrgEx(hdc, &ptViewportOrg);
419 GetWindowExtEx(hdc, &ptWindowExt);
420 GetWindowOrgEx(hdc, &ptWindowOrg);
422 /* Save world transform
423 * NB: The Windows documentation on world transforms would lead one to
424 * believe that this has to be done only in GM_ADVANCED; however, my
425 * tests show that resetting the graphics mode to GM_COMPATIBLE does
426 * not reset the world transform.
428 GetWorldTransform(hdc, &xform);
431 SetMapMode(hdc, MM_TEXT);
432 SetViewportOrgEx(hdc, 0, 0, NULL);
433 SetWindowOrgEx(hdc, 0, 0, NULL);
435 /* Paint the region */
438 /* Restore the old mapping mode */
439 SetMapMode(hdc, mapMode);
440 SetViewportExtEx(hdc, ptViewportExt.cx, ptViewportExt.cy, NULL);
441 SetViewportOrgEx(hdc, ptViewportOrg.x, ptViewportOrg.y, NULL);
442 SetWindowExtEx(hdc, ptWindowExt.cx, ptWindowExt.cy, NULL);
443 SetWindowOrgEx(hdc, ptWindowOrg.x, ptWindowOrg.y, NULL);
445 /* Go to GM_ADVANCED temporarily to restore the world transform */
446 graphicsMode=GetGraphicsMode(hdc);
447 SetGraphicsMode(hdc, GM_ADVANCED);
448 SetWorldTransform(hdc, &xform);
449 SetGraphicsMode(hdc, graphicsMode);
452 PATH_EmptyPath(pPath);
457 /* FIXME: Should the path be emptied even if conversion failed? */
458 /* PATH_EmptyPath(pPath); */
463 /***********************************************************************
464 * SelectClipPath16 (GDI.519)
466 BOOL16 WINAPI SelectClipPath16(HDC16 hdc, INT16 iMode)
468 return (BOOL16) SelectClipPath((HDC) hdc, iMode);
471 /***********************************************************************
472 * SelectClipPath (GDI32.296)
474 * Check that SetLastError is being called correctly
476 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
481 DC *dc = DC_GetDCPtr( hdc );
484 SetLastError(ERROR_INVALID_HANDLE);
488 if(dc->funcs->pSelectClipPath)
489 return dc->funcs->pSelectClipPath(dc, iMode);
493 /* Check that path is closed */
494 if(pPath->state!=PATH_Closed)
496 SetLastError(ERROR_CAN_NOT_COMPLETE);
500 /* Construct a region from the path */
501 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnPath))
503 success = ExtSelectClipRgn( hdc, hrgnPath, iMode ) != ERROR;
504 DeleteObject(hrgnPath);
508 PATH_EmptyPath(pPath);
509 /* FIXME: Should this function delete the path even if it failed? */
518 /***********************************************************************
524 * Initializes the GdiPath structure.
526 void PATH_InitGdiPath(GdiPath *pPath)
530 pPath->state=PATH_Null;
533 pPath->numEntriesUsed=0;
534 pPath->numEntriesAllocated=0;
537 /* PATH_DestroyGdiPath
539 * Destroys a GdiPath structure (frees the memory in the arrays).
541 void PATH_DestroyGdiPath(GdiPath *pPath)
545 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
546 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
549 /* PATH_AssignGdiPath
551 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
552 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
553 * not just the pointers. Since this means that the arrays in pPathDest may
554 * need to be resized, pPathDest should have been initialized using
555 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
556 * not a copy constructor).
557 * Returns TRUE if successful, else FALSE.
559 BOOL PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
561 assert(pPathDest!=NULL && pPathSrc!=NULL);
563 /* Make sure destination arrays are big enough */
564 if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
567 /* Perform the copy operation */
568 memcpy(pPathDest->pPoints, pPathSrc->pPoints,
569 sizeof(POINT)*pPathSrc->numEntriesUsed);
570 memcpy(pPathDest->pFlags, pPathSrc->pFlags,
571 sizeof(BYTE)*pPathSrc->numEntriesUsed);
573 pPathDest->state=pPathSrc->state;
574 pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
575 pPathDest->newStroke=pPathSrc->newStroke;
582 * Should be called when a MoveTo is performed on a DC that has an
583 * open path. This starts a new stroke. Returns TRUE if successful, else
586 BOOL PATH_MoveTo(HDC hdc)
590 /* Get pointer to path */
591 if(!PATH_GetPathFromHDC(hdc, &pPath))
594 /* Check that path is open */
595 if(pPath->state!=PATH_Open)
596 /* FIXME: Do we have to call SetLastError? */
599 /* Start a new stroke */
600 pPath->newStroke=TRUE;
607 * Should be called when a LineTo is performed on a DC that has an
608 * open path. This adds a PT_LINETO entry to the path (and possibly
609 * a PT_MOVETO entry, if this is the first LineTo in a stroke).
610 * Returns TRUE if successful, else FALSE.
612 BOOL PATH_LineTo(HDC hdc, INT x, INT y)
615 POINT point, pointCurPos;
617 /* Get pointer to path */
618 if(!PATH_GetPathFromHDC(hdc, &pPath))
621 /* Check that path is open */
622 if(pPath->state!=PATH_Open)
625 /* Convert point to device coordinates */
628 if(!LPtoDP(hdc, &point, 1))
631 /* Add a PT_MOVETO if necessary */
634 pPath->newStroke=FALSE;
635 if(!GetCurrentPositionEx(hdc, &pointCurPos) ||
636 !LPtoDP(hdc, &pointCurPos, 1))
638 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
642 /* Add a PT_LINETO entry */
643 return PATH_AddEntry(pPath, &point, PT_LINETO);
648 * Should be called when a call to Rectangle is performed on a DC that has
649 * an open path. Returns TRUE if successful, else FALSE.
651 BOOL PATH_Rectangle(HDC hdc, INT x1, INT y1, INT x2, INT y2)
654 POINT corners[2], pointTemp;
657 /* Get pointer to path */
658 if(!PATH_GetPathFromHDC(hdc, &pPath))
661 /* Check that path is open */
662 if(pPath->state!=PATH_Open)
665 /* Convert points to device coordinates */
670 if(!LPtoDP(hdc, corners, 2))
673 /* Make sure first corner is top left and second corner is bottom right */
674 if(corners[0].x>corners[1].x)
677 corners[0].x=corners[1].x;
680 if(corners[0].y>corners[1].y)
683 corners[0].y=corners[1].y;
687 /* In GM_COMPATIBLE, don't include bottom and right edges */
688 if(GetGraphicsMode(hdc)==GM_COMPATIBLE)
694 /* Close any previous figure */
695 if(!CloseFigure(hdc))
697 /* The CloseFigure call shouldn't have failed */
702 /* Add four points to the path */
703 pointTemp.x=corners[1].x;
704 pointTemp.y=corners[0].y;
705 if(!PATH_AddEntry(pPath, &pointTemp, PT_MOVETO))
707 if(!PATH_AddEntry(pPath, corners, PT_LINETO))
709 pointTemp.x=corners[0].x;
710 pointTemp.y=corners[1].y;
711 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
713 if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
716 /* Close the rectangle figure */
717 if(!CloseFigure(hdc))
719 /* The CloseFigure call shouldn't have failed */
729 * Should be called when a call to Ellipse is performed on a DC that has
730 * an open path. This adds four Bezier splines representing the ellipse
731 * to the path. Returns TRUE if successful, else FALSE.
733 BOOL PATH_Ellipse(HDC hdc, INT x1, INT y1, INT x2, INT y2)
735 /* TODO: This should probably be revised to call PATH_AngleArc */
736 /* (once it exists) */
737 return PATH_Arc(hdc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2);
742 * Should be called when a call to Arc is performed on a DC that has
743 * an open path. This adds up to five Bezier splines representing the arc
744 * to the path. Returns TRUE if successful, else FALSE.
746 BOOL PATH_Arc(HDC hdc, INT x1, INT y1, INT x2, INT y2,
747 INT xStart, INT yStart, INT xEnd, INT yEnd)
751 double angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
752 /* Initialize angleEndQuadrant to silence gcc's warning */
754 FLOAT_POINT corners[2], pointStart, pointEnd;
758 /* FIXME: This function should check for all possible error returns */
759 /* FIXME: Do we have to respect newStroke? */
761 /* Get pointer to DC */
762 pDC=DC_GetDCPtr(hdc);
766 /* Get pointer to path */
767 if(!PATH_GetPathFromHDC(hdc, &pPath))
770 /* Check that path is open */
771 if(pPath->state!=PATH_Open)
774 /* FIXME: Do we have to close the current figure? */
776 /* Check for zero height / width */
777 /* FIXME: Only in GM_COMPATIBLE? */
781 /* Convert points to device coordinates */
782 corners[0].x=(FLOAT)x1;
783 corners[0].y=(FLOAT)y1;
784 corners[1].x=(FLOAT)x2;
785 corners[1].y=(FLOAT)y2;
786 pointStart.x=(FLOAT)xStart;
787 pointStart.y=(FLOAT)yStart;
788 pointEnd.x=(FLOAT)xEnd;
789 pointEnd.y=(FLOAT)yEnd;
790 INTERNAL_LPTODP_FLOAT(pDC, corners);
791 INTERNAL_LPTODP_FLOAT(pDC, corners+1);
792 INTERNAL_LPTODP_FLOAT(pDC, &pointStart);
793 INTERNAL_LPTODP_FLOAT(pDC, &pointEnd);
795 /* Make sure first corner is top left and second corner is bottom right */
796 if(corners[0].x>corners[1].x)
799 corners[0].x=corners[1].x;
802 if(corners[0].y>corners[1].y)
805 corners[0].y=corners[1].y;
809 /* Compute start and end angle */
810 PATH_NormalizePoint(corners, &pointStart, &x, &y);
811 angleStart=atan2(y, x);
812 PATH_NormalizePoint(corners, &pointEnd, &x, &y);
813 angleEnd=atan2(y, x);
815 /* Make sure the end angle is "on the right side" of the start angle */
816 if(GetArcDirection(hdc)==AD_CLOCKWISE)
818 if(angleEnd<=angleStart)
821 assert(angleEnd>=angleStart);
826 if(angleEnd>=angleStart)
829 assert(angleEnd<=angleStart);
833 /* In GM_COMPATIBLE, don't include bottom and right edges */
834 if(GetGraphicsMode(hdc)==GM_COMPATIBLE)
840 /* Add the arc to the path with one Bezier spline per quadrant that the
846 /* Determine the start and end angles for this quadrant */
849 angleStartQuadrant=angleStart;
850 if(GetArcDirection(hdc)==AD_CLOCKWISE)
851 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
853 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
857 angleStartQuadrant=angleEndQuadrant;
858 if(GetArcDirection(hdc)==AD_CLOCKWISE)
859 angleEndQuadrant+=M_PI_2;
861 angleEndQuadrant-=M_PI_2;
864 /* Have we reached the last part of the arc? */
865 if((GetArcDirection(hdc)==AD_CLOCKWISE &&
866 angleEnd<angleEndQuadrant) ||
867 (GetArcDirection(hdc)==AD_COUNTERCLOCKWISE &&
868 angleEnd>angleEndQuadrant))
870 /* Adjust the end angle for this quadrant */
871 angleEndQuadrant=angleEnd;
875 /* Add the Bezier spline to the path */
876 PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
884 BOOL PATH_PolyBezierTo(HDC hdc, const POINT *pts, DWORD cbPoints)
890 if(!PATH_GetPathFromHDC(hdc, &pPath))
893 /* Check that path is open */
894 if(pPath->state!=PATH_Open)
897 /* Add a PT_MOVETO if necessary */
900 pPath->newStroke=FALSE;
901 if(!GetCurrentPositionEx(hdc, &pt) ||
902 !LPtoDP(hdc, &pt, 1))
904 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
908 for(i = 0; i < cbPoints; i++) {
910 if(!LPtoDP(hdc, &pt, 1))
912 PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
917 BOOL PATH_PolyBezier(HDC hdc, const POINT *pts, DWORD cbPoints)
923 if(!PATH_GetPathFromHDC(hdc, &pPath))
926 /* Check that path is open */
927 if(pPath->state!=PATH_Open)
930 for(i = 0; i < cbPoints; i++) {
932 if(!LPtoDP(hdc, &pt, 1))
934 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
939 BOOL PATH_Polyline(HDC hdc, const POINT *pts, DWORD cbPoints)
945 if(!PATH_GetPathFromHDC(hdc, &pPath))
948 /* Check that path is open */
949 if(pPath->state!=PATH_Open)
952 for(i = 0; i < cbPoints; i++) {
954 if(!LPtoDP(hdc, &pt, 1))
956 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
961 BOOL PATH_PolylineTo(HDC hdc, const POINT *pts, DWORD cbPoints)
967 if(!PATH_GetPathFromHDC(hdc, &pPath))
970 /* Check that path is open */
971 if(pPath->state!=PATH_Open)
974 /* Add a PT_MOVETO if necessary */
977 pPath->newStroke=FALSE;
978 if(!GetCurrentPositionEx(hdc, &pt) ||
979 !LPtoDP(hdc, &pt, 1))
981 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
985 for(i = 0; i < cbPoints; i++) {
987 if(!LPtoDP(hdc, &pt, 1))
989 PATH_AddEntry(pPath, &pt, PT_LINETO);
996 BOOL PATH_Polygon(HDC hdc, const POINT *pts, DWORD cbPoints)
1002 if(!PATH_GetPathFromHDC(hdc, &pPath))
1005 /* Check that path is open */
1006 if(pPath->state!=PATH_Open)
1009 for(i = 0; i < cbPoints; i++) {
1011 if(!LPtoDP(hdc, &pt, 1))
1013 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
1014 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
1020 BOOL PATH_PolyPolygon( HDC hdc, const POINT* pts, const INT* counts,
1027 if(!PATH_GetPathFromHDC(hdc, &pPath))
1030 /* Check that path is open */
1031 if(pPath->state!=PATH_Open)
1034 for(i = 0, poly = 0; poly < polygons; poly++) {
1035 for(point = 0; point < counts[poly]; point++, i++) {
1037 if(!LPtoDP(hdc, &pt, 1))
1039 if(point == 0) startpt = pt;
1040 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1042 /* win98 adds an extra line to close the figure for some reason */
1043 PATH_AddEntry(pPath, &startpt, PT_LINETO | PT_CLOSEFIGURE);
1048 BOOL PATH_PolyPolyline( HDC hdc, const POINT* pts, const DWORD* counts,
1055 if(!PATH_GetPathFromHDC(hdc, &pPath))
1058 /* Check that path is open */
1059 if(pPath->state!=PATH_Open)
1062 for(i = 0, poly = 0; poly < polylines; poly++) {
1063 for(point = 0; point < counts[poly]; point++, i++) {
1065 if(!LPtoDP(hdc, &pt, 1))
1067 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1073 /***********************************************************************
1074 * Internal functions
1078 /* PATH_AddFlatBezier
1081 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
1086 pts = GDI_Bezier( pt, 4, &no );
1087 if(!pts) return FALSE;
1089 for(i = 1; i < no; i++)
1090 PATH_AddEntry(pPath, &pts[i],
1091 (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
1092 HeapFree( GetProcessHeap(), 0, pts );
1098 * Replaces Beziers with line segments
1101 static BOOL PATH_FlattenPath(GdiPath *pPath)
1106 memset(&newPath, 0, sizeof(newPath));
1107 newPath.state = PATH_Open;
1108 for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
1109 switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
1112 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
1113 pPath->pFlags[srcpt]);
1116 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
1117 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
1122 newPath.state = PATH_Closed;
1123 PATH_AssignGdiPath(pPath, &newPath);
1124 PATH_EmptyPath(&newPath);
1128 /* PATH_PathToRegion
1130 * Creates a region from the specified path using the specified polygon
1131 * filling mode. The path is left unchanged. A handle to the region that
1132 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
1133 * error occurs, SetLastError is called with the appropriate value and
1134 * FALSE is returned.
1136 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
1139 int numStrokes, iStroke, i;
1140 INT *pNumPointsInStroke;
1143 assert(pPath!=NULL);
1144 assert(pHrgn!=NULL);
1146 PATH_FlattenPath(pPath);
1148 /* FIXME: What happens when number of points is zero? */
1150 /* First pass: Find out how many strokes there are in the path */
1151 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
1153 for(i=0; i<pPath->numEntriesUsed; i++)
1154 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1157 /* Allocate memory for number-of-points-in-stroke array */
1158 pNumPointsInStroke=(int *)HeapAlloc( GetProcessHeap(), 0,
1159 sizeof(int) * numStrokes );
1160 if(!pNumPointsInStroke)
1162 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1166 /* Second pass: remember number of points in each polygon */
1167 iStroke=-1; /* Will get incremented to 0 at beginning of first stroke */
1168 for(i=0; i<pPath->numEntriesUsed; i++)
1170 /* Is this the beginning of a new stroke? */
1171 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1174 pNumPointsInStroke[iStroke]=0;
1177 pNumPointsInStroke[iStroke]++;
1180 /* Create a region from the strokes */
1181 hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
1182 numStrokes, nPolyFillMode);
1185 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1189 /* Free memory for number-of-points-in-stroke array */
1190 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
1199 * Removes all entries from the path and sets the path state to PATH_Null.
1201 static void PATH_EmptyPath(GdiPath *pPath)
1203 assert(pPath!=NULL);
1205 pPath->state=PATH_Null;
1206 pPath->numEntriesUsed=0;
1211 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1212 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1213 * successful, FALSE otherwise (e.g. if not enough memory was available).
1215 BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
1217 assert(pPath!=NULL);
1219 /* FIXME: If newStroke is true, perhaps we want to check that we're
1220 * getting a PT_MOVETO
1222 TRACE("(%ld,%ld) - %d\n", pPoint->x, pPoint->y, flags);
1224 /* Check that path is open */
1225 if(pPath->state!=PATH_Open)
1228 /* Reserve enough memory for an extra path entry */
1229 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
1232 /* Store information in path entry */
1233 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
1234 pPath->pFlags[pPath->numEntriesUsed]=flags;
1236 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1237 if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
1238 pPath->newStroke=TRUE;
1240 /* Increment entry count */
1241 pPath->numEntriesUsed++;
1246 /* PATH_ReserveEntries
1248 * Ensures that at least "numEntries" entries (for points and flags) have
1249 * been allocated; allocates larger arrays and copies the existing entries
1250 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1252 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries)
1254 INT numEntriesToAllocate;
1258 assert(pPath!=NULL);
1259 assert(numEntries>=0);
1261 /* Do we have to allocate more memory? */
1262 if(numEntries > pPath->numEntriesAllocated)
1264 /* Find number of entries to allocate. We let the size of the array
1265 * grow exponentially, since that will guarantee linear time
1267 if(pPath->numEntriesAllocated)
1269 numEntriesToAllocate=pPath->numEntriesAllocated;
1270 while(numEntriesToAllocate<numEntries)
1271 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
1275 numEntriesToAllocate=numEntries;
1277 /* Allocate new arrays */
1278 pPointsNew=(POINT *)HeapAlloc( GetProcessHeap(), 0,
1279 numEntriesToAllocate * sizeof(POINT) );
1282 pFlagsNew=(BYTE *)HeapAlloc( GetProcessHeap(), 0,
1283 numEntriesToAllocate * sizeof(BYTE) );
1286 HeapFree( GetProcessHeap(), 0, pPointsNew );
1290 /* Copy old arrays to new arrays and discard old arrays */
1293 assert(pPath->pFlags);
1295 memcpy(pPointsNew, pPath->pPoints,
1296 sizeof(POINT)*pPath->numEntriesUsed);
1297 memcpy(pFlagsNew, pPath->pFlags,
1298 sizeof(BYTE)*pPath->numEntriesUsed);
1300 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
1301 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
1303 pPath->pPoints=pPointsNew;
1304 pPath->pFlags=pFlagsNew;
1305 pPath->numEntriesAllocated=numEntriesToAllocate;
1311 /* PATH_GetPathFromHDC
1313 * Retrieves a pointer to the GdiPath structure contained in an HDC and
1314 * places it in *ppPath. TRUE is returned if successful, FALSE otherwise.
1316 static BOOL PATH_GetPathFromHDC(HDC hdc, GdiPath **ppPath)
1320 pDC=DC_GetDCPtr(hdc);
1323 *ppPath=&pDC->w.path;
1332 * Creates a Bezier spline that corresponds to part of an arc and appends the
1333 * corresponding points to the path. The start and end angles are passed in
1334 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1335 * at most. If "addMoveTo" is true, a PT_MOVETO entry for the first control
1336 * point is added to the path; otherwise, it is assumed that the current
1337 * position is equal to the first control point.
1339 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
1340 double angleStart, double angleEnd, BOOL addMoveTo)
1342 double halfAngle, a;
1343 double xNorm[4], yNorm[4];
1347 assert(fabs(angleEnd-angleStart)<=M_PI_2);
1349 /* FIXME: Is there an easier way of computing this? */
1351 /* Compute control points */
1352 halfAngle=(angleEnd-angleStart)/2.0;
1353 if(fabs(halfAngle)>1e-8)
1355 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
1356 xNorm[0]=cos(angleStart);
1357 yNorm[0]=sin(angleStart);
1358 xNorm[1]=xNorm[0] - a*yNorm[0];
1359 yNorm[1]=yNorm[0] + a*xNorm[0];
1360 xNorm[3]=cos(angleEnd);
1361 yNorm[3]=sin(angleEnd);
1362 xNorm[2]=xNorm[3] + a*yNorm[3];
1363 yNorm[2]=yNorm[3] - a*xNorm[3];
1368 xNorm[i]=cos(angleStart);
1369 yNorm[i]=sin(angleStart);
1372 /* Add starting point to path if desired */
1375 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1376 if(!PATH_AddEntry(pPath, &point, PT_MOVETO))
1380 /* Add remaining control points */
1383 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1384 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1391 /* PATH_ScaleNormalizedPoint
1393 * Scales a normalized point (x, y) with respect to the box whose corners are
1394 * passed in "corners". The point is stored in "*pPoint". The normalized
1395 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1396 * (1.0, 1.0) correspond to corners[1].
1398 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
1399 double y, POINT *pPoint)
1401 pPoint->x=GDI_ROUND( (double)corners[0].x +
1402 (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
1403 pPoint->y=GDI_ROUND( (double)corners[0].y +
1404 (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
1407 /* PATH_NormalizePoint
1409 * Normalizes a point with respect to the box whose corners are passed in
1410 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1412 static void PATH_NormalizePoint(FLOAT_POINT corners[],
1413 const FLOAT_POINT *pPoint,
1414 double *pX, double *pY)
1416 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) *
1418 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1422 /*******************************************************************
1423 * FlattenPath16 [GDI.516]
1427 BOOL16 WINAPI FlattenPath16(HDC16 hdc)
1429 return (BOOL16) FlattenPath((HDC) hdc);
1432 /*******************************************************************
1433 * FlattenPath [GDI32.103]
1437 BOOL WINAPI FlattenPath(HDC hdc)
1439 DC *dc = DC_GetDCPtr( hdc );
1441 TRACE("%08x\n", hdc);
1444 SetLastError(ERROR_INVALID_HANDLE);
1448 if(dc->funcs->pFlattenPath)
1449 return dc->funcs->pFlattenPath(dc);
1451 pPath = &dc->w.path;
1452 if(pPath->state != PATH_Closed)
1454 return PATH_FlattenPath(pPath);
1457 /*******************************************************************
1458 * StrokeAndFillPath16 [GDI.520]
1462 BOOL16 WINAPI StrokeAndFillPath16(HDC16 hdc)
1464 return (BOOL16) StrokeAndFillPath((HDC) hdc);
1467 /*******************************************************************
1468 * StrokeAndFillPath [GDI32.352]
1472 BOOL WINAPI StrokeAndFillPath(HDC hdc)
1474 DC *dc = DC_GetDCPtr( hdc );
1477 SetLastError(ERROR_INVALID_HANDLE);
1481 if(dc->funcs->pStrokeAndFillPath)
1482 return dc->funcs->pStrokeAndFillPath(dc);
1485 return StrokePath(hdc);
1488 /*******************************************************************
1489 * StrokePath16 [GDI.521]
1493 BOOL16 WINAPI StrokePath16(HDC16 hdc)
1495 return (BOOL16) StrokePath((HDC) hdc);
1498 /*******************************************************************
1499 * StrokePath [GDI32.353]
1503 BOOL WINAPI StrokePath(HDC hdc)
1505 DC *dc = DC_GetDCPtr( hdc );
1508 POINT ptLastMove = {0,0};
1510 TRACE("(%08x)\n", hdc);
1512 SetLastError(ERROR_INVALID_HANDLE);
1516 if(dc->funcs->pStrokePath)
1517 return dc->funcs->pStrokePath(dc);
1519 pPath = &dc->w.path;
1520 if(pPath->state != PATH_Closed)
1524 SetMapMode(hdc, MM_TEXT);
1525 SetViewportOrgEx(hdc, 0, 0, NULL);
1526 SetWindowOrgEx(hdc, 0, 0, NULL);
1527 for(i = 0; i < pPath->numEntriesUsed; i++) {
1528 switch(pPath->pFlags[i]) {
1530 TRACE("Got PT_MOVETO (%ld, %ld)\n",
1531 pPath->pPoints[i].x, pPath->pPoints[i].y);
1532 MoveToEx(hdc, pPath->pPoints[i].x, pPath->pPoints[i].y, NULL);
1533 ptLastMove = pPath->pPoints[i];
1536 case (PT_LINETO | PT_CLOSEFIGURE):
1537 TRACE("Got PT_LINETO (%ld, %ld)\n",
1538 pPath->pPoints[i].x, pPath->pPoints[i].y);
1539 LineTo(hdc, pPath->pPoints[i].x, pPath->pPoints[i].y);
1542 TRACE("Got PT_BEZIERTO\n");
1543 if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1544 (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1545 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1548 PolyBezierTo(hdc, &pPath->pPoints[i], 3);
1552 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1555 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1556 LineTo(hdc, ptLastMove.x, ptLastMove.y);
1559 PATH_EmptyPath(pPath);
1563 /*******************************************************************
1564 * WidenPath16 [GDI.522]
1568 BOOL16 WINAPI WidenPath16(HDC16 hdc)
1570 return (BOOL16) WidenPath((HDC) hdc);
1573 /*******************************************************************
1574 * WidenPath [GDI32.360]
1578 BOOL WINAPI WidenPath(HDC hdc)
1580 DC *dc = DC_GetDCPtr( hdc );
1583 SetLastError(ERROR_INVALID_HANDLE);
1587 if(dc->funcs->pWidenPath)
1588 return dc->funcs->pWidenPath(dc);