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_ReserveEntries(GdiPath *pPath, INT numEntries);
74 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
75 double angleStart, double angleEnd, BOOL addMoveTo);
76 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
77 double y, POINT *pPoint);
78 static void PATH_NormalizePoint(FLOAT_POINT corners[], const FLOAT_POINT
79 *pPoint, double *pX, double *pY);
80 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2);
83 /***********************************************************************
84 * BeginPath16 (GDI.512)
86 BOOL16 WINAPI BeginPath16(HDC16 hdc)
88 return (BOOL16)BeginPath((HDC)hdc);
92 /***********************************************************************
95 BOOL WINAPI BeginPath(HDC hdc)
98 DC *dc = DC_GetDCPtr( hdc );
100 if(!dc) return FALSE;
102 if(dc->funcs->pBeginPath)
103 ret = dc->funcs->pBeginPath(dc);
106 /* If path is already open, do nothing */
107 if(dc->path.state != PATH_Open)
109 /* Make sure that path is empty */
110 PATH_EmptyPath(&dc->path);
112 /* Initialize variables for new path */
113 dc->path.newStroke=TRUE;
114 dc->path.state=PATH_Open;
117 GDI_ReleaseObj( hdc );
122 /***********************************************************************
123 * EndPath16 (GDI.514)
125 BOOL16 WINAPI EndPath16(HDC16 hdc)
127 return (BOOL16)EndPath((HDC)hdc);
131 /***********************************************************************
134 BOOL WINAPI EndPath(HDC hdc)
137 DC *dc = DC_GetDCPtr( hdc );
139 if(!dc) return FALSE;
141 if(dc->funcs->pEndPath)
142 ret = dc->funcs->pEndPath(dc);
145 /* Check that path is currently being constructed */
146 if(dc->path.state!=PATH_Open)
148 SetLastError(ERROR_CAN_NOT_COMPLETE);
151 /* Set flag to indicate that path is finished */
152 else dc->path.state=PATH_Closed;
154 GDI_ReleaseObj( hdc );
159 /***********************************************************************
160 * AbortPath16 (GDI.511)
162 BOOL16 WINAPI AbortPath16(HDC16 hdc)
164 return (BOOL16)AbortPath((HDC)hdc);
168 /******************************************************************************
169 * AbortPath [GDI32.@]
170 * Closes and discards paths from device context
173 * Check that SetLastError is being called correctly
176 * hdc [I] Handle to device context
180 BOOL WINAPI AbortPath( HDC hdc )
183 DC *dc = DC_GetDCPtr( hdc );
185 if(!dc) return FALSE;
187 if(dc->funcs->pAbortPath)
188 ret = dc->funcs->pAbortPath(dc);
189 else /* Remove all entries from the path */
190 PATH_EmptyPath( &dc->path );
191 GDI_ReleaseObj( hdc );
196 /***********************************************************************
197 * CloseFigure16 (GDI.513)
199 BOOL16 WINAPI CloseFigure16(HDC16 hdc)
201 return (BOOL16)CloseFigure((HDC)hdc);
205 /***********************************************************************
206 * CloseFigure (GDI32.@)
208 * FIXME: Check that SetLastError is being called correctly
210 BOOL WINAPI CloseFigure(HDC hdc)
213 DC *dc = DC_GetDCPtr( hdc );
215 if(!dc) return FALSE;
217 if(dc->funcs->pCloseFigure)
218 ret = dc->funcs->pCloseFigure(dc);
221 /* Check that path is open */
222 if(dc->path.state!=PATH_Open)
224 SetLastError(ERROR_CAN_NOT_COMPLETE);
229 /* FIXME: Shouldn't we draw a line to the beginning of the
231 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
232 if(dc->path.numEntriesUsed)
234 dc->path.pFlags[dc->path.numEntriesUsed-1]|=PT_CLOSEFIGURE;
235 dc->path.newStroke=TRUE;
239 GDI_ReleaseObj( hdc );
244 /***********************************************************************
245 * GetPath16 (GDI.517)
247 INT16 WINAPI GetPath16(HDC16 hdc, LPPOINT16 pPoints, LPBYTE pTypes,
250 FIXME("(%d,%p,%p): stub\n",hdc,pPoints,pTypes);
256 /***********************************************************************
259 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
264 DC *dc = DC_GetDCPtr( hdc );
270 /* Check that path is closed */
271 if(pPath->state!=PATH_Closed)
273 SetLastError(ERROR_CAN_NOT_COMPLETE);
278 ret = pPath->numEntriesUsed;
279 else if(nSize<pPath->numEntriesUsed)
281 SetLastError(ERROR_INVALID_PARAMETER);
286 memcpy(pPoints, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
287 memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
289 /* Convert the points to logical coordinates */
290 if(!DPtoLP(hdc, pPoints, pPath->numEntriesUsed))
292 /* FIXME: Is this the correct value? */
293 SetLastError(ERROR_CAN_NOT_COMPLETE);
296 else ret = pPath->numEntriesUsed;
299 GDI_ReleaseObj( hdc );
303 /***********************************************************************
304 * PathToRegion16 (GDI.518)
306 HRGN16 WINAPI PathToRegion16(HDC16 hdc)
308 return (HRGN16) PathToRegion((HDC) hdc);
311 /***********************************************************************
312 * PathToRegion (GDI32.@)
315 * Check that SetLastError is being called correctly
317 * The documentation does not state this explicitly, but a test under Windows
318 * shows that the region which is returned should be in device coordinates.
320 HRGN WINAPI PathToRegion(HDC hdc)
324 DC *dc = DC_GetDCPtr( hdc );
326 /* Get pointer to path */
331 /* Check that path is closed */
332 if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
335 /* FIXME: Should we empty the path even if conversion failed? */
336 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
337 PATH_EmptyPath(pPath);
341 GDI_ReleaseObj( hdc );
345 static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
347 INT mapMode, graphicsMode;
348 SIZE ptViewportExt, ptWindowExt;
349 POINT ptViewportOrg, ptWindowOrg;
353 if(dc->funcs->pFillPath)
354 return dc->funcs->pFillPath(dc);
356 /* Check that path is closed */
357 if(pPath->state!=PATH_Closed)
359 SetLastError(ERROR_CAN_NOT_COMPLETE);
363 /* Construct a region from the path and fill it */
364 if(PATH_PathToRegion(pPath, dc->polyFillMode, &hrgn))
366 /* Since PaintRgn interprets the region as being in logical coordinates
367 * but the points we store for the path are already in device
368 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
369 * Using SaveDC to save information about the mapping mode / world
370 * transform would be easier but would require more overhead, especially
371 * now that SaveDC saves the current path.
374 /* Save the information about the old mapping mode */
375 mapMode=GetMapMode(dc->hSelf);
376 GetViewportExtEx(dc->hSelf, &ptViewportExt);
377 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
378 GetWindowExtEx(dc->hSelf, &ptWindowExt);
379 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
381 /* Save world transform
382 * NB: The Windows documentation on world transforms would lead one to
383 * believe that this has to be done only in GM_ADVANCED; however, my
384 * tests show that resetting the graphics mode to GM_COMPATIBLE does
385 * not reset the world transform.
387 GetWorldTransform(dc->hSelf, &xform);
390 SetMapMode(dc->hSelf, MM_TEXT);
391 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
392 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
394 /* Paint the region */
395 PaintRgn(dc->hSelf, hrgn);
397 /* Restore the old mapping mode */
398 SetMapMode(dc->hSelf, mapMode);
399 SetViewportExtEx(dc->hSelf, ptViewportExt.cx, ptViewportExt.cy, NULL);
400 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
401 SetWindowExtEx(dc->hSelf, ptWindowExt.cx, ptWindowExt.cy, NULL);
402 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
404 /* Go to GM_ADVANCED temporarily to restore the world transform */
405 graphicsMode=GetGraphicsMode(dc->hSelf);
406 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
407 SetWorldTransform(dc->hSelf, &xform);
408 SetGraphicsMode(dc->hSelf, graphicsMode);
414 /***********************************************************************
415 * FillPath16 (GDI.515)
417 BOOL16 WINAPI FillPath16(HDC16 hdc)
419 return (BOOL16) FillPath((HDC) hdc);
422 /***********************************************************************
426 * Check that SetLastError is being called correctly
428 BOOL WINAPI FillPath(HDC hdc)
430 DC *dc = DC_GetDCPtr( hdc );
433 if(!dc) return FALSE;
435 if(dc->funcs->pFillPath)
436 bRet = dc->funcs->pFillPath(dc);
439 bRet = PATH_FillPath(dc, &dc->path);
442 /* FIXME: Should the path be emptied even if conversion
444 PATH_EmptyPath(&dc->path);
447 GDI_ReleaseObj( hdc );
451 /***********************************************************************
452 * SelectClipPath16 (GDI.519)
454 BOOL16 WINAPI SelectClipPath16(HDC16 hdc, INT16 iMode)
456 return (BOOL16) SelectClipPath((HDC) hdc, iMode);
459 /***********************************************************************
460 * SelectClipPath (GDI32.@)
462 * Check that SetLastError is being called correctly
464 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
468 BOOL success = FALSE;
469 DC *dc = DC_GetDCPtr( hdc );
471 if(!dc) return FALSE;
473 if(dc->funcs->pSelectClipPath)
474 success = dc->funcs->pSelectClipPath(dc, iMode);
479 /* Check that path is closed */
480 if(pPath->state!=PATH_Closed)
481 SetLastError(ERROR_CAN_NOT_COMPLETE);
482 /* Construct a region from the path */
483 else if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnPath))
485 success = ExtSelectClipRgn( hdc, hrgnPath, iMode ) != ERROR;
486 DeleteObject(hrgnPath);
490 PATH_EmptyPath(pPath);
491 /* FIXME: Should this function delete the path even if it failed? */
494 GDI_ReleaseObj( hdc );
499 /***********************************************************************
505 * Initializes the GdiPath structure.
507 void PATH_InitGdiPath(GdiPath *pPath)
511 pPath->state=PATH_Null;
514 pPath->numEntriesUsed=0;
515 pPath->numEntriesAllocated=0;
518 /* PATH_DestroyGdiPath
520 * Destroys a GdiPath structure (frees the memory in the arrays).
522 void PATH_DestroyGdiPath(GdiPath *pPath)
526 if (pPath->pPoints) HeapFree( GetProcessHeap(), 0, pPath->pPoints );
527 if (pPath->pFlags) HeapFree( GetProcessHeap(), 0, pPath->pFlags );
530 /* PATH_AssignGdiPath
532 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
533 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
534 * not just the pointers. Since this means that the arrays in pPathDest may
535 * need to be resized, pPathDest should have been initialized using
536 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
537 * not a copy constructor).
538 * Returns TRUE if successful, else FALSE.
540 BOOL PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
542 assert(pPathDest!=NULL && pPathSrc!=NULL);
544 /* Make sure destination arrays are big enough */
545 if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
548 /* Perform the copy operation */
549 memcpy(pPathDest->pPoints, pPathSrc->pPoints,
550 sizeof(POINT)*pPathSrc->numEntriesUsed);
551 memcpy(pPathDest->pFlags, pPathSrc->pFlags,
552 sizeof(BYTE)*pPathSrc->numEntriesUsed);
554 pPathDest->state=pPathSrc->state;
555 pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
556 pPathDest->newStroke=pPathSrc->newStroke;
563 * Should be called when a MoveTo is performed on a DC that has an
564 * open path. This starts a new stroke. Returns TRUE if successful, else
567 BOOL PATH_MoveTo(DC *dc)
569 GdiPath *pPath = &dc->path;
571 /* Check that path is open */
572 if(pPath->state!=PATH_Open)
573 /* FIXME: Do we have to call SetLastError? */
576 /* Start a new stroke */
577 pPath->newStroke=TRUE;
584 * Should be called when a LineTo is performed on a DC that has an
585 * open path. This adds a PT_LINETO entry to the path (and possibly
586 * a PT_MOVETO entry, if this is the first LineTo in a stroke).
587 * Returns TRUE if successful, else FALSE.
589 BOOL PATH_LineTo(DC *dc, INT x, INT y)
591 GdiPath *pPath = &dc->path;
592 POINT point, pointCurPos;
594 /* Check that path is open */
595 if(pPath->state!=PATH_Open)
598 /* Convert point to device coordinates */
601 if(!LPtoDP(dc->hSelf, &point, 1))
604 /* Add a PT_MOVETO if necessary */
607 pPath->newStroke=FALSE;
608 pointCurPos.x = dc->CursPosX;
609 pointCurPos.y = dc->CursPosY;
610 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
612 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
616 /* Add a PT_LINETO entry */
617 return PATH_AddEntry(pPath, &point, PT_LINETO);
622 * Should be called when a call to RoundRect is performed on a DC that has
623 * an open path. Returns TRUE if successful, else FALSE.
625 * FIXME: it adds the same entries to the path as windows does, but there
626 * is an error in the bezier drawing code so that there are small pixel-size
627 * gaps when the resulting path is drawn by StrokePath()
629 BOOL PATH_RoundRect(DC *dc, INT x1, INT y1, INT x2, INT y2, INT ell_width, INT ell_height)
631 GdiPath *pPath = &dc->path;
632 POINT corners[2], pointTemp;
633 FLOAT_POINT ellCorners[2];
635 /* Check that path is open */
636 if(pPath->state!=PATH_Open)
639 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
642 /* Add points to the roundrect path */
643 ellCorners[0].x = corners[1].x-ell_width;
644 ellCorners[0].y = corners[0].y;
645 ellCorners[1].x = corners[1].x;
646 ellCorners[1].y = corners[0].y+ell_height;
647 if(!PATH_DoArcPart(pPath, ellCorners, 0, -M_PI_2, TRUE))
649 pointTemp.x = corners[0].x+ell_width/2;
650 pointTemp.y = corners[0].y;
651 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
653 ellCorners[0].x = corners[0].x;
654 ellCorners[1].x = corners[0].x+ell_width;
655 if(!PATH_DoArcPart(pPath, ellCorners, -M_PI_2, -M_PI, FALSE))
657 pointTemp.x = corners[0].x;
658 pointTemp.y = corners[1].y-ell_height/2;
659 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
661 ellCorners[0].y = corners[1].y-ell_height;
662 ellCorners[1].y = corners[1].y;
663 if(!PATH_DoArcPart(pPath, ellCorners, M_PI, M_PI_2, FALSE))
665 pointTemp.x = corners[1].x-ell_width/2;
666 pointTemp.y = corners[1].y;
667 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
669 ellCorners[0].x = corners[1].x-ell_width;
670 ellCorners[1].x = corners[1].x;
671 if(!PATH_DoArcPart(pPath, ellCorners, M_PI_2, 0, FALSE))
674 /* Close the roundrect figure */
675 if(!CloseFigure(dc->hSelf))
683 * Should be called when a call to Rectangle is performed on a DC that has
684 * an open path. Returns TRUE if successful, else FALSE.
686 BOOL PATH_Rectangle(DC *dc, INT x1, INT y1, INT x2, INT y2)
688 GdiPath *pPath = &dc->path;
689 POINT corners[2], pointTemp;
691 /* Check that path is open */
692 if(pPath->state!=PATH_Open)
695 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
698 /* Close any previous figure */
699 if(!CloseFigure(dc->hSelf))
701 /* The CloseFigure call shouldn't have failed */
706 /* Add four points to the path */
707 pointTemp.x=corners[1].x;
708 pointTemp.y=corners[0].y;
709 if(!PATH_AddEntry(pPath, &pointTemp, PT_MOVETO))
711 if(!PATH_AddEntry(pPath, corners, PT_LINETO))
713 pointTemp.x=corners[0].x;
714 pointTemp.y=corners[1].y;
715 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
717 if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
720 /* Close the rectangle figure */
721 if(!CloseFigure(dc->hSelf))
723 /* The CloseFigure call shouldn't have failed */
733 * Should be called when a call to Ellipse is performed on a DC that has
734 * an open path. This adds four Bezier splines representing the ellipse
735 * to the path. Returns TRUE if successful, else FALSE.
737 BOOL PATH_Ellipse(DC *dc, INT x1, INT y1, INT x2, INT y2)
739 return( PATH_Arc(dc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2,0) &&
740 CloseFigure(dc->hSelf) );
745 * Should be called when a call to Arc is performed on a DC that has
746 * an open path. This adds up to five Bezier splines representing the arc
747 * to the path. When 'lines' is 1, we add 1 extra line to get a chord,
748 * and when 'lines' is 2, we add 2 extra lines to get a pie.
749 * Returns TRUE if successful, else FALSE.
751 BOOL PATH_Arc(DC *dc, INT x1, INT y1, INT x2, INT y2,
752 INT xStart, INT yStart, INT xEnd, INT yEnd, INT lines)
754 GdiPath *pPath = &dc->path;
755 double angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
756 /* Initialize angleEndQuadrant to silence gcc's warning */
758 FLOAT_POINT corners[2], pointStart, pointEnd;
763 /* FIXME: This function should check for all possible error returns */
764 /* FIXME: Do we have to respect newStroke? */
766 /* Check that path is open */
767 if(pPath->state!=PATH_Open)
770 /* Check for zero height / width */
771 /* FIXME: Only in GM_COMPATIBLE? */
775 /* Convert points to device coordinates */
776 corners[0].x=(FLOAT)x1;
777 corners[0].y=(FLOAT)y1;
778 corners[1].x=(FLOAT)x2;
779 corners[1].y=(FLOAT)y2;
780 pointStart.x=(FLOAT)xStart;
781 pointStart.y=(FLOAT)yStart;
782 pointEnd.x=(FLOAT)xEnd;
783 pointEnd.y=(FLOAT)yEnd;
784 INTERNAL_LPTODP_FLOAT(dc, corners);
785 INTERNAL_LPTODP_FLOAT(dc, corners+1);
786 INTERNAL_LPTODP_FLOAT(dc, &pointStart);
787 INTERNAL_LPTODP_FLOAT(dc, &pointEnd);
789 /* Make sure first corner is top left and second corner is bottom right */
790 if(corners[0].x>corners[1].x)
793 corners[0].x=corners[1].x;
796 if(corners[0].y>corners[1].y)
799 corners[0].y=corners[1].y;
803 /* Compute start and end angle */
804 PATH_NormalizePoint(corners, &pointStart, &x, &y);
805 angleStart=atan2(y, x);
806 PATH_NormalizePoint(corners, &pointEnd, &x, &y);
807 angleEnd=atan2(y, x);
809 /* Make sure the end angle is "on the right side" of the start angle */
810 if(dc->ArcDirection==AD_CLOCKWISE)
812 if(angleEnd<=angleStart)
815 assert(angleEnd>=angleStart);
820 if(angleEnd>=angleStart)
823 assert(angleEnd<=angleStart);
827 /* In GM_COMPATIBLE, don't include bottom and right edges */
828 if(dc->GraphicsMode==GM_COMPATIBLE)
834 /* Add the arc to the path with one Bezier spline per quadrant that the
840 /* Determine the start and end angles for this quadrant */
843 angleStartQuadrant=angleStart;
844 if(dc->ArcDirection==AD_CLOCKWISE)
845 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
847 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
851 angleStartQuadrant=angleEndQuadrant;
852 if(dc->ArcDirection==AD_CLOCKWISE)
853 angleEndQuadrant+=M_PI_2;
855 angleEndQuadrant-=M_PI_2;
858 /* Have we reached the last part of the arc? */
859 if((dc->ArcDirection==AD_CLOCKWISE &&
860 angleEnd<angleEndQuadrant) ||
861 (dc->ArcDirection==AD_COUNTERCLOCKWISE &&
862 angleEnd>angleEndQuadrant))
864 /* Adjust the end angle for this quadrant */
865 angleEndQuadrant=angleEnd;
869 /* Add the Bezier spline to the path */
870 PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
875 /* chord: close figure. pie: add line and close figure */
878 if(!CloseFigure(dc->hSelf))
883 centre.x = (corners[0].x+corners[1].x)/2;
884 centre.y = (corners[0].y+corners[1].y)/2;
885 if(!PATH_AddEntry(pPath, ¢re, PT_LINETO | PT_CLOSEFIGURE))
892 BOOL PATH_PolyBezierTo(DC *dc, const POINT *pts, DWORD cbPoints)
894 GdiPath *pPath = &dc->path;
898 /* Check that path is open */
899 if(pPath->state!=PATH_Open)
902 /* Add a PT_MOVETO if necessary */
905 pPath->newStroke=FALSE;
908 if(!LPtoDP(dc->hSelf, &pt, 1))
910 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
914 for(i = 0; i < cbPoints; i++) {
916 if(!LPtoDP(dc->hSelf, &pt, 1))
918 PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
923 BOOL PATH_PolyBezier(DC *dc, const POINT *pts, DWORD cbPoints)
925 GdiPath *pPath = &dc->path;
929 /* Check that path is open */
930 if(pPath->state!=PATH_Open)
933 for(i = 0; i < cbPoints; i++) {
935 if(!LPtoDP(dc->hSelf, &pt, 1))
937 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
942 BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
944 GdiPath *pPath = &dc->path;
948 /* Check that path is open */
949 if(pPath->state!=PATH_Open)
952 for(i = 0; i < cbPoints; i++) {
954 if(!LPtoDP(dc->hSelf, &pt, 1))
956 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
961 BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
963 GdiPath *pPath = &dc->path;
967 /* Check that path is open */
968 if(pPath->state!=PATH_Open)
971 /* Add a PT_MOVETO if necessary */
974 pPath->newStroke=FALSE;
977 if(!LPtoDP(dc->hSelf, &pt, 1))
979 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
983 for(i = 0; i < cbPoints; i++) {
985 if(!LPtoDP(dc->hSelf, &pt, 1))
987 PATH_AddEntry(pPath, &pt, PT_LINETO);
994 BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
996 GdiPath *pPath = &dc->path;
1000 /* Check that path is open */
1001 if(pPath->state!=PATH_Open)
1004 for(i = 0; i < cbPoints; i++) {
1006 if(!LPtoDP(dc->hSelf, &pt, 1))
1008 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
1009 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
1015 BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
1018 GdiPath *pPath = &dc->path;
1022 /* Check that path is open */
1023 if(pPath->state!=PATH_Open)
1026 for(i = 0, poly = 0; poly < polygons; poly++) {
1027 for(point = 0; point < counts[poly]; point++, i++) {
1029 if(!LPtoDP(dc->hSelf, &pt, 1))
1031 if(point == 0) startpt = pt;
1032 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1034 /* win98 adds an extra line to close the figure for some reason */
1035 PATH_AddEntry(pPath, &startpt, PT_LINETO | PT_CLOSEFIGURE);
1040 BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1043 GdiPath *pPath = &dc->path;
1047 /* Check that path is open */
1048 if(pPath->state!=PATH_Open)
1051 for(i = 0, poly = 0; poly < polylines; poly++) {
1052 for(point = 0; point < counts[poly]; point++, i++) {
1054 if(!LPtoDP(dc->hSelf, &pt, 1))
1056 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1062 /***********************************************************************
1063 * Internal functions
1066 /* PATH_CheckCorners
1068 * Helper function for PATH_RoundRect() and PATH_Rectangle()
1070 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2)
1074 /* Convert points to device coordinates */
1079 if(!LPtoDP(dc->hSelf, corners, 2))
1082 /* Make sure first corner is top left and second corner is bottom right */
1083 if(corners[0].x>corners[1].x)
1086 corners[0].x=corners[1].x;
1089 if(corners[0].y>corners[1].y)
1092 corners[0].y=corners[1].y;
1096 /* In GM_COMPATIBLE, don't include bottom and right edges */
1097 if(dc->GraphicsMode==GM_COMPATIBLE)
1106 /* PATH_AddFlatBezier
1108 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
1113 pts = GDI_Bezier( pt, 4, &no );
1114 if(!pts) return FALSE;
1116 for(i = 1; i < no; i++)
1117 PATH_AddEntry(pPath, &pts[i],
1118 (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
1119 HeapFree( GetProcessHeap(), 0, pts );
1125 * Replaces Beziers with line segments
1128 static BOOL PATH_FlattenPath(GdiPath *pPath)
1133 memset(&newPath, 0, sizeof(newPath));
1134 newPath.state = PATH_Open;
1135 for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
1136 switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
1139 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
1140 pPath->pFlags[srcpt]);
1143 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
1144 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
1149 newPath.state = PATH_Closed;
1150 PATH_AssignGdiPath(pPath, &newPath);
1151 PATH_EmptyPath(&newPath);
1155 /* PATH_PathToRegion
1157 * Creates a region from the specified path using the specified polygon
1158 * filling mode. The path is left unchanged. A handle to the region that
1159 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
1160 * error occurs, SetLastError is called with the appropriate value and
1161 * FALSE is returned.
1163 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
1166 int numStrokes, iStroke, i;
1167 INT *pNumPointsInStroke;
1170 assert(pPath!=NULL);
1171 assert(pHrgn!=NULL);
1173 PATH_FlattenPath(pPath);
1175 /* FIXME: What happens when number of points is zero? */
1177 /* First pass: Find out how many strokes there are in the path */
1178 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
1180 for(i=0; i<pPath->numEntriesUsed; i++)
1181 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1184 /* Allocate memory for number-of-points-in-stroke array */
1185 pNumPointsInStroke=(int *)HeapAlloc( GetProcessHeap(), 0,
1186 sizeof(int) * numStrokes );
1187 if(!pNumPointsInStroke)
1189 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1193 /* Second pass: remember number of points in each polygon */
1194 iStroke=-1; /* Will get incremented to 0 at beginning of first stroke */
1195 for(i=0; i<pPath->numEntriesUsed; i++)
1197 /* Is this the beginning of a new stroke? */
1198 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1201 pNumPointsInStroke[iStroke]=0;
1204 pNumPointsInStroke[iStroke]++;
1207 /* Create a region from the strokes */
1208 hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
1209 numStrokes, nPolyFillMode);
1212 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1216 /* Free memory for number-of-points-in-stroke array */
1217 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
1226 * Removes all entries from the path and sets the path state to PATH_Null.
1228 static void PATH_EmptyPath(GdiPath *pPath)
1230 assert(pPath!=NULL);
1232 pPath->state=PATH_Null;
1233 pPath->numEntriesUsed=0;
1238 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1239 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1240 * successful, FALSE otherwise (e.g. if not enough memory was available).
1242 BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
1244 assert(pPath!=NULL);
1246 /* FIXME: If newStroke is true, perhaps we want to check that we're
1247 * getting a PT_MOVETO
1249 TRACE("(%ld,%ld) - %d\n", pPoint->x, pPoint->y, flags);
1251 /* Check that path is open */
1252 if(pPath->state!=PATH_Open)
1255 /* Reserve enough memory for an extra path entry */
1256 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
1259 /* Store information in path entry */
1260 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
1261 pPath->pFlags[pPath->numEntriesUsed]=flags;
1263 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1264 if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
1265 pPath->newStroke=TRUE;
1267 /* Increment entry count */
1268 pPath->numEntriesUsed++;
1273 /* PATH_ReserveEntries
1275 * Ensures that at least "numEntries" entries (for points and flags) have
1276 * been allocated; allocates larger arrays and copies the existing entries
1277 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1279 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries)
1281 INT numEntriesToAllocate;
1285 assert(pPath!=NULL);
1286 assert(numEntries>=0);
1288 /* Do we have to allocate more memory? */
1289 if(numEntries > pPath->numEntriesAllocated)
1291 /* Find number of entries to allocate. We let the size of the array
1292 * grow exponentially, since that will guarantee linear time
1294 if(pPath->numEntriesAllocated)
1296 numEntriesToAllocate=pPath->numEntriesAllocated;
1297 while(numEntriesToAllocate<numEntries)
1298 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
1302 numEntriesToAllocate=numEntries;
1304 /* Allocate new arrays */
1305 pPointsNew=(POINT *)HeapAlloc( GetProcessHeap(), 0,
1306 numEntriesToAllocate * sizeof(POINT) );
1309 pFlagsNew=(BYTE *)HeapAlloc( GetProcessHeap(), 0,
1310 numEntriesToAllocate * sizeof(BYTE) );
1313 HeapFree( GetProcessHeap(), 0, pPointsNew );
1317 /* Copy old arrays to new arrays and discard old arrays */
1320 assert(pPath->pFlags);
1322 memcpy(pPointsNew, pPath->pPoints,
1323 sizeof(POINT)*pPath->numEntriesUsed);
1324 memcpy(pFlagsNew, pPath->pFlags,
1325 sizeof(BYTE)*pPath->numEntriesUsed);
1327 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
1328 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
1330 pPath->pPoints=pPointsNew;
1331 pPath->pFlags=pFlagsNew;
1332 pPath->numEntriesAllocated=numEntriesToAllocate;
1340 * Creates a Bezier spline that corresponds to part of an arc and appends the
1341 * corresponding points to the path. The start and end angles are passed in
1342 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1343 * at most. If "addMoveTo" is true, a PT_MOVETO entry for the first control
1344 * point is added to the path; otherwise, it is assumed that the current
1345 * position is equal to the first control point.
1347 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
1348 double angleStart, double angleEnd, BOOL addMoveTo)
1350 double halfAngle, a;
1351 double xNorm[4], yNorm[4];
1355 assert(fabs(angleEnd-angleStart)<=M_PI_2);
1357 /* FIXME: Is there an easier way of computing this? */
1359 /* Compute control points */
1360 halfAngle=(angleEnd-angleStart)/2.0;
1361 if(fabs(halfAngle)>1e-8)
1363 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
1364 xNorm[0]=cos(angleStart);
1365 yNorm[0]=sin(angleStart);
1366 xNorm[1]=xNorm[0] - a*yNorm[0];
1367 yNorm[1]=yNorm[0] + a*xNorm[0];
1368 xNorm[3]=cos(angleEnd);
1369 yNorm[3]=sin(angleEnd);
1370 xNorm[2]=xNorm[3] + a*yNorm[3];
1371 yNorm[2]=yNorm[3] - a*xNorm[3];
1376 xNorm[i]=cos(angleStart);
1377 yNorm[i]=sin(angleStart);
1380 /* Add starting point to path if desired */
1383 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1384 if(!PATH_AddEntry(pPath, &point, PT_MOVETO))
1388 /* Add remaining control points */
1391 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1392 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1399 /* PATH_ScaleNormalizedPoint
1401 * Scales a normalized point (x, y) with respect to the box whose corners are
1402 * passed in "corners". The point is stored in "*pPoint". The normalized
1403 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1404 * (1.0, 1.0) correspond to corners[1].
1406 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
1407 double y, POINT *pPoint)
1409 pPoint->x=GDI_ROUND( (double)corners[0].x +
1410 (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
1411 pPoint->y=GDI_ROUND( (double)corners[0].y +
1412 (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
1415 /* PATH_NormalizePoint
1417 * Normalizes a point with respect to the box whose corners are passed in
1418 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1420 static void PATH_NormalizePoint(FLOAT_POINT corners[],
1421 const FLOAT_POINT *pPoint,
1422 double *pX, double *pY)
1424 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) *
1426 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1430 /*******************************************************************
1431 * FlattenPath16 [GDI.516]
1435 BOOL16 WINAPI FlattenPath16(HDC16 hdc)
1437 return (BOOL16) FlattenPath((HDC) hdc);
1440 /*******************************************************************
1441 * FlattenPath [GDI32.@]
1445 BOOL WINAPI FlattenPath(HDC hdc)
1448 DC *dc = DC_GetDCPtr( hdc );
1450 if(!dc) return FALSE;
1452 if(dc->funcs->pFlattenPath) ret = dc->funcs->pFlattenPath(dc);
1455 GdiPath *pPath = &dc->path;
1456 if(pPath->state != PATH_Closed)
1457 ret = PATH_FlattenPath(pPath);
1459 GDI_ReleaseObj( hdc );
1464 static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1467 POINT ptLastMove = {0,0};
1469 if(dc->funcs->pStrokePath)
1470 return dc->funcs->pStrokePath(dc);
1472 if(pPath->state != PATH_Closed)
1476 SetMapMode(dc->hSelf, MM_TEXT);
1477 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
1478 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
1479 for(i = 0; i < pPath->numEntriesUsed; i++) {
1480 switch(pPath->pFlags[i]) {
1482 TRACE("Got PT_MOVETO (%ld, %ld)\n",
1483 pPath->pPoints[i].x, pPath->pPoints[i].y);
1484 MoveToEx(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y, NULL);
1485 ptLastMove = pPath->pPoints[i];
1488 case (PT_LINETO | PT_CLOSEFIGURE):
1489 TRACE("Got PT_LINETO (%ld, %ld)\n",
1490 pPath->pPoints[i].x, pPath->pPoints[i].y);
1491 LineTo(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y);
1494 TRACE("Got PT_BEZIERTO\n");
1495 if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1496 (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1497 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1500 PolyBezierTo(dc->hSelf, &pPath->pPoints[i], 3);
1504 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1507 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1508 LineTo(dc->hSelf, ptLastMove.x, ptLastMove.y);
1510 RestoreDC(dc->hSelf , -1);
1515 /*******************************************************************
1516 * StrokeAndFillPath16 [GDI.520]
1520 BOOL16 WINAPI StrokeAndFillPath16(HDC16 hdc)
1522 return (BOOL16) StrokeAndFillPath((HDC) hdc);
1525 /*******************************************************************
1526 * StrokeAndFillPath [GDI32.@]
1530 BOOL WINAPI StrokeAndFillPath(HDC hdc)
1532 DC *dc = DC_GetDCPtr( hdc );
1535 if(!dc) return FALSE;
1537 if(dc->funcs->pStrokeAndFillPath)
1538 bRet = dc->funcs->pStrokeAndFillPath(dc);
1541 bRet = PATH_FillPath(dc, &dc->path);
1542 if(bRet) bRet = PATH_StrokePath(dc, &dc->path);
1543 if(bRet) PATH_EmptyPath(&dc->path);
1545 GDI_ReleaseObj( hdc );
1549 /*******************************************************************
1550 * StrokePath16 [GDI.521]
1554 BOOL16 WINAPI StrokePath16(HDC16 hdc)
1556 return (BOOL16) StrokePath((HDC) hdc);
1559 /*******************************************************************
1560 * StrokePath [GDI32.@]
1564 BOOL WINAPI StrokePath(HDC hdc)
1566 DC *dc = DC_GetDCPtr( hdc );
1570 TRACE("(%08x)\n", hdc);
1571 if(!dc) return FALSE;
1573 if(dc->funcs->pStrokePath)
1574 bRet = dc->funcs->pStrokePath(dc);
1578 bRet = PATH_StrokePath(dc, pPath);
1579 PATH_EmptyPath(pPath);
1581 GDI_ReleaseObj( hdc );
1585 /*******************************************************************
1586 * WidenPath16 [GDI.522]
1590 BOOL16 WINAPI WidenPath16(HDC16 hdc)
1592 return (BOOL16) WidenPath((HDC) hdc);
1595 /*******************************************************************
1596 * WidenPath [GDI32.@]
1600 BOOL WINAPI WidenPath(HDC hdc)
1602 DC *dc = DC_GetDCPtr( hdc );
1605 if(!dc) return FALSE;
1607 if(dc->funcs->pWidenPath)
1608 ret = dc->funcs->pWidenPath(dc);
1611 GDI_ReleaseObj( hdc );