2 * Graphics paths (BeginPath, EndPath etc.)
4 * Copyright 1997, 1998 Martin Boehme
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #if defined(HAVE_FLOAT_H)
36 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
41 /* Notes on the implementation
43 * The implementation is based on dynamically resizable arrays of points and
44 * flags. I dithered for a bit before deciding on this implementation, and
45 * I had even done a bit of work on a linked list version before switching
46 * to arrays. It's a bit of a tradeoff. When you use linked lists, the
47 * implementation of FlattenPath is easier, because you can rip the
48 * PT_BEZIERTO entries out of the middle of the list and link the
49 * corresponding PT_LINETO entries in. However, when you use arrays,
50 * PathToRegion becomes easier, since you can essentially just pass your array
51 * of points to CreatePolyPolygonRgn. Also, if I'd used linked lists, I would
52 * have had the extra effort of creating a chunk-based allocation scheme
53 * in order to use memory effectively. That's why I finally decided to use
54 * arrays. Note by the way that the array based implementation has the same
55 * linear time complexity that linked lists would have since the arrays grow
58 * The points are stored in the path in device coordinates. This is
59 * consistent with the way Windows does things (for instance, see the Win32
60 * SDK documentation for GetPath).
62 * The word "stroke" appears in several places (e.g. in the flag
63 * GdiPath.newStroke). A stroke consists of a PT_MOVETO followed by one or
64 * more PT_LINETOs or PT_BEZIERTOs, up to, but not including, the next
65 * PT_MOVETO. Note that this is not the same as the definition of a figure;
66 * a figure can contain several strokes.
68 * I modified the drawing functions (MoveTo, LineTo etc.) to test whether
69 * the path is open and to call the corresponding function in path.c if this
70 * is the case. A more elegant approach would be to modify the function
71 * pointers in the DC_FUNCTIONS structure; however, this would be a lot more
72 * complex. Also, the performance degradation caused by my approach in the
73 * case where no path is open is so small that it cannot be measured.
78 /* FIXME: A lot of stuff isn't implemented yet. There is much more to come. */
80 #define NUM_ENTRIES_INITIAL 16 /* Initial size of points / flags arrays */
81 #define GROW_FACTOR_NUMER 2 /* Numerator of grow factor for the array */
82 #define GROW_FACTOR_DENOM 1 /* Denominator of grow factor */
84 /* A floating point version of the POINT structure */
85 typedef struct tagFLOAT_POINT
91 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
93 static void PATH_EmptyPath(GdiPath *pPath);
94 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries);
95 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
96 double angleStart, double angleEnd, BOOL addMoveTo);
97 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
98 double y, POINT *pPoint);
99 static void PATH_NormalizePoint(FLOAT_POINT corners[], const FLOAT_POINT
100 *pPoint, double *pX, double *pY);
101 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2);
103 /* Performs a world-to-viewport transformation on the specified point (which
104 * is in floating point format).
106 static inline void WINE_UNUSED INTERNAL_LPTODP_FLOAT(DC *dc, FLOAT_POINT *point)
110 /* Perform the transformation */
113 point->x = x * dc->xformWorld2Vport.eM11 +
114 y * dc->xformWorld2Vport.eM21 +
115 dc->xformWorld2Vport.eDx;
116 point->y = x * dc->xformWorld2Vport.eM12 +
117 y * dc->xformWorld2Vport.eM22 +
118 dc->xformWorld2Vport.eDy;
121 /***********************************************************************
122 * BeginPath (GDI.512)
124 BOOL16 WINAPI BeginPath16(HDC16 hdc)
126 return (BOOL16)BeginPath((HDC)hdc);
130 /***********************************************************************
131 * BeginPath (GDI32.@)
133 BOOL WINAPI BeginPath(HDC hdc)
136 DC *dc = DC_GetDCPtr( hdc );
138 if(!dc) return FALSE;
140 if(dc->funcs->pBeginPath)
141 ret = dc->funcs->pBeginPath(dc->physDev);
144 /* If path is already open, do nothing */
145 if(dc->path.state != PATH_Open)
147 /* Make sure that path is empty */
148 PATH_EmptyPath(&dc->path);
150 /* Initialize variables for new path */
151 dc->path.newStroke=TRUE;
152 dc->path.state=PATH_Open;
155 GDI_ReleaseObj( hdc );
160 /***********************************************************************
163 BOOL16 WINAPI EndPath16(HDC16 hdc)
165 return (BOOL16)EndPath((HDC)hdc);
169 /***********************************************************************
172 BOOL WINAPI EndPath(HDC hdc)
175 DC *dc = DC_GetDCPtr( hdc );
177 if(!dc) return FALSE;
179 if(dc->funcs->pEndPath)
180 ret = dc->funcs->pEndPath(dc->physDev);
183 /* Check that path is currently being constructed */
184 if(dc->path.state!=PATH_Open)
186 SetLastError(ERROR_CAN_NOT_COMPLETE);
189 /* Set flag to indicate that path is finished */
190 else dc->path.state=PATH_Closed;
192 GDI_ReleaseObj( hdc );
197 /***********************************************************************
198 * AbortPath (GDI.511)
200 BOOL16 WINAPI AbortPath16(HDC16 hdc)
202 return (BOOL16)AbortPath((HDC)hdc);
206 /******************************************************************************
207 * AbortPath [GDI32.@]
208 * Closes and discards paths from device context
211 * Check that SetLastError is being called correctly
214 * hdc [I] Handle to device context
218 BOOL WINAPI AbortPath( HDC hdc )
221 DC *dc = DC_GetDCPtr( hdc );
223 if(!dc) return FALSE;
225 if(dc->funcs->pAbortPath)
226 ret = dc->funcs->pAbortPath(dc->physDev);
227 else /* Remove all entries from the path */
228 PATH_EmptyPath( &dc->path );
229 GDI_ReleaseObj( hdc );
234 /***********************************************************************
235 * CloseFigure (GDI.513)
237 BOOL16 WINAPI CloseFigure16(HDC16 hdc)
239 return (BOOL16)CloseFigure((HDC)hdc);
243 /***********************************************************************
244 * CloseFigure (GDI32.@)
246 * FIXME: Check that SetLastError is being called correctly
248 BOOL WINAPI CloseFigure(HDC hdc)
251 DC *dc = DC_GetDCPtr( hdc );
253 if(!dc) return FALSE;
255 if(dc->funcs->pCloseFigure)
256 ret = dc->funcs->pCloseFigure(dc->physDev);
259 /* Check that path is open */
260 if(dc->path.state!=PATH_Open)
262 SetLastError(ERROR_CAN_NOT_COMPLETE);
267 /* FIXME: Shouldn't we draw a line to the beginning of the
269 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
270 if(dc->path.numEntriesUsed)
272 dc->path.pFlags[dc->path.numEntriesUsed-1]|=PT_CLOSEFIGURE;
273 dc->path.newStroke=TRUE;
277 GDI_ReleaseObj( hdc );
282 /***********************************************************************
285 INT16 WINAPI GetPath16(HDC16 hdc, LPPOINT16 pPoints, LPBYTE pTypes,
288 FIXME("(%d,%p,%p): stub\n",hdc,pPoints,pTypes);
294 /***********************************************************************
297 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
302 DC *dc = DC_GetDCPtr( hdc );
308 /* Check that path is closed */
309 if(pPath->state!=PATH_Closed)
311 SetLastError(ERROR_CAN_NOT_COMPLETE);
316 ret = pPath->numEntriesUsed;
317 else if(nSize<pPath->numEntriesUsed)
319 SetLastError(ERROR_INVALID_PARAMETER);
324 memcpy(pPoints, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
325 memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
327 /* Convert the points to logical coordinates */
328 if(!DPtoLP(hdc, pPoints, pPath->numEntriesUsed))
330 /* FIXME: Is this the correct value? */
331 SetLastError(ERROR_CAN_NOT_COMPLETE);
334 else ret = pPath->numEntriesUsed;
337 GDI_ReleaseObj( hdc );
341 /***********************************************************************
342 * PathToRegion (GDI.518)
344 HRGN16 WINAPI PathToRegion16(HDC16 hdc)
346 return (HRGN16) PathToRegion((HDC) hdc);
349 /***********************************************************************
350 * PathToRegion (GDI32.@)
353 * Check that SetLastError is being called correctly
355 * The documentation does not state this explicitly, but a test under Windows
356 * shows that the region which is returned should be in device coordinates.
358 HRGN WINAPI PathToRegion(HDC hdc)
362 DC *dc = DC_GetDCPtr( hdc );
364 /* Get pointer to path */
369 /* Check that path is closed */
370 if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
373 /* FIXME: Should we empty the path even if conversion failed? */
374 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
375 PATH_EmptyPath(pPath);
379 GDI_ReleaseObj( hdc );
383 static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
385 INT mapMode, graphicsMode;
386 SIZE ptViewportExt, ptWindowExt;
387 POINT ptViewportOrg, ptWindowOrg;
391 if(dc->funcs->pFillPath)
392 return dc->funcs->pFillPath(dc->physDev);
394 /* Check that path is closed */
395 if(pPath->state!=PATH_Closed)
397 SetLastError(ERROR_CAN_NOT_COMPLETE);
401 /* Construct a region from the path and fill it */
402 if(PATH_PathToRegion(pPath, dc->polyFillMode, &hrgn))
404 /* Since PaintRgn interprets the region as being in logical coordinates
405 * but the points we store for the path are already in device
406 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
407 * Using SaveDC to save information about the mapping mode / world
408 * transform would be easier but would require more overhead, especially
409 * now that SaveDC saves the current path.
412 /* Save the information about the old mapping mode */
413 mapMode=GetMapMode(dc->hSelf);
414 GetViewportExtEx(dc->hSelf, &ptViewportExt);
415 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
416 GetWindowExtEx(dc->hSelf, &ptWindowExt);
417 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
419 /* Save world transform
420 * NB: The Windows documentation on world transforms would lead one to
421 * believe that this has to be done only in GM_ADVANCED; however, my
422 * tests show that resetting the graphics mode to GM_COMPATIBLE does
423 * not reset the world transform.
425 GetWorldTransform(dc->hSelf, &xform);
428 SetMapMode(dc->hSelf, MM_TEXT);
429 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
430 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
432 /* Paint the region */
433 PaintRgn(dc->hSelf, hrgn);
435 /* Restore the old mapping mode */
436 SetMapMode(dc->hSelf, mapMode);
437 SetViewportExtEx(dc->hSelf, ptViewportExt.cx, ptViewportExt.cy, NULL);
438 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
439 SetWindowExtEx(dc->hSelf, ptWindowExt.cx, ptWindowExt.cy, NULL);
440 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
442 /* Go to GM_ADVANCED temporarily to restore the world transform */
443 graphicsMode=GetGraphicsMode(dc->hSelf);
444 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
445 SetWorldTransform(dc->hSelf, &xform);
446 SetGraphicsMode(dc->hSelf, graphicsMode);
452 /***********************************************************************
455 BOOL16 WINAPI FillPath16(HDC16 hdc)
457 return (BOOL16) FillPath((HDC) hdc);
460 /***********************************************************************
464 * Check that SetLastError is being called correctly
466 BOOL WINAPI FillPath(HDC hdc)
468 DC *dc = DC_GetDCPtr( hdc );
471 if(!dc) return FALSE;
473 if(dc->funcs->pFillPath)
474 bRet = dc->funcs->pFillPath(dc->physDev);
477 bRet = PATH_FillPath(dc, &dc->path);
480 /* FIXME: Should the path be emptied even if conversion
482 PATH_EmptyPath(&dc->path);
485 GDI_ReleaseObj( hdc );
489 /***********************************************************************
490 * SelectClipPath (GDI.519)
492 BOOL16 WINAPI SelectClipPath16(HDC16 hdc, INT16 iMode)
494 return (BOOL16) SelectClipPath((HDC) hdc, iMode);
497 /***********************************************************************
498 * SelectClipPath (GDI32.@)
500 * Check that SetLastError is being called correctly
502 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
506 BOOL success = FALSE;
507 DC *dc = DC_GetDCPtr( hdc );
509 if(!dc) return FALSE;
511 if(dc->funcs->pSelectClipPath)
512 success = dc->funcs->pSelectClipPath(dc->physDev, iMode);
517 /* Check that path is closed */
518 if(pPath->state!=PATH_Closed)
519 SetLastError(ERROR_CAN_NOT_COMPLETE);
520 /* Construct a region from the path */
521 else if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnPath))
523 success = ExtSelectClipRgn( hdc, hrgnPath, iMode ) != ERROR;
524 DeleteObject(hrgnPath);
528 PATH_EmptyPath(pPath);
529 /* FIXME: Should this function delete the path even if it failed? */
532 GDI_ReleaseObj( hdc );
537 /***********************************************************************
543 * Initializes the GdiPath structure.
545 void PATH_InitGdiPath(GdiPath *pPath)
549 pPath->state=PATH_Null;
552 pPath->numEntriesUsed=0;
553 pPath->numEntriesAllocated=0;
556 /* PATH_DestroyGdiPath
558 * Destroys a GdiPath structure (frees the memory in the arrays).
560 void PATH_DestroyGdiPath(GdiPath *pPath)
564 if (pPath->pPoints) HeapFree( GetProcessHeap(), 0, pPath->pPoints );
565 if (pPath->pFlags) HeapFree( GetProcessHeap(), 0, pPath->pFlags );
568 /* PATH_AssignGdiPath
570 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
571 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
572 * not just the pointers. Since this means that the arrays in pPathDest may
573 * need to be resized, pPathDest should have been initialized using
574 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
575 * not a copy constructor).
576 * Returns TRUE if successful, else FALSE.
578 BOOL PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
580 assert(pPathDest!=NULL && pPathSrc!=NULL);
582 /* Make sure destination arrays are big enough */
583 if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
586 /* Perform the copy operation */
587 memcpy(pPathDest->pPoints, pPathSrc->pPoints,
588 sizeof(POINT)*pPathSrc->numEntriesUsed);
589 memcpy(pPathDest->pFlags, pPathSrc->pFlags,
590 sizeof(BYTE)*pPathSrc->numEntriesUsed);
592 pPathDest->state=pPathSrc->state;
593 pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
594 pPathDest->newStroke=pPathSrc->newStroke;
601 * Should be called when a MoveTo is performed on a DC that has an
602 * open path. This starts a new stroke. Returns TRUE if successful, else
605 BOOL PATH_MoveTo(DC *dc)
607 GdiPath *pPath = &dc->path;
609 /* Check that path is open */
610 if(pPath->state!=PATH_Open)
611 /* FIXME: Do we have to call SetLastError? */
614 /* Start a new stroke */
615 pPath->newStroke=TRUE;
622 * Should be called when a LineTo is performed on a DC that has an
623 * open path. This adds a PT_LINETO entry to the path (and possibly
624 * a PT_MOVETO entry, if this is the first LineTo in a stroke).
625 * Returns TRUE if successful, else FALSE.
627 BOOL PATH_LineTo(DC *dc, INT x, INT y)
629 GdiPath *pPath = &dc->path;
630 POINT point, pointCurPos;
632 /* Check that path is open */
633 if(pPath->state!=PATH_Open)
636 /* Convert point to device coordinates */
639 if(!LPtoDP(dc->hSelf, &point, 1))
642 /* Add a PT_MOVETO if necessary */
645 pPath->newStroke=FALSE;
646 pointCurPos.x = dc->CursPosX;
647 pointCurPos.y = dc->CursPosY;
648 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
650 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
654 /* Add a PT_LINETO entry */
655 return PATH_AddEntry(pPath, &point, PT_LINETO);
660 * Should be called when a call to RoundRect is performed on a DC that has
661 * an open path. Returns TRUE if successful, else FALSE.
663 * FIXME: it adds the same entries to the path as windows does, but there
664 * is an error in the bezier drawing code so that there are small pixel-size
665 * gaps when the resulting path is drawn by StrokePath()
667 BOOL PATH_RoundRect(DC *dc, INT x1, INT y1, INT x2, INT y2, INT ell_width, INT ell_height)
669 GdiPath *pPath = &dc->path;
670 POINT corners[2], pointTemp;
671 FLOAT_POINT ellCorners[2];
673 /* Check that path is open */
674 if(pPath->state!=PATH_Open)
677 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
680 /* Add points to the roundrect path */
681 ellCorners[0].x = corners[1].x-ell_width;
682 ellCorners[0].y = corners[0].y;
683 ellCorners[1].x = corners[1].x;
684 ellCorners[1].y = corners[0].y+ell_height;
685 if(!PATH_DoArcPart(pPath, ellCorners, 0, -M_PI_2, TRUE))
687 pointTemp.x = corners[0].x+ell_width/2;
688 pointTemp.y = corners[0].y;
689 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
691 ellCorners[0].x = corners[0].x;
692 ellCorners[1].x = corners[0].x+ell_width;
693 if(!PATH_DoArcPart(pPath, ellCorners, -M_PI_2, -M_PI, FALSE))
695 pointTemp.x = corners[0].x;
696 pointTemp.y = corners[1].y-ell_height/2;
697 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
699 ellCorners[0].y = corners[1].y-ell_height;
700 ellCorners[1].y = corners[1].y;
701 if(!PATH_DoArcPart(pPath, ellCorners, M_PI, M_PI_2, FALSE))
703 pointTemp.x = corners[1].x-ell_width/2;
704 pointTemp.y = corners[1].y;
705 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
707 ellCorners[0].x = corners[1].x-ell_width;
708 ellCorners[1].x = corners[1].x;
709 if(!PATH_DoArcPart(pPath, ellCorners, M_PI_2, 0, FALSE))
712 /* Close the roundrect figure */
713 if(!CloseFigure(dc->hSelf))
721 * Should be called when a call to Rectangle is performed on a DC that has
722 * an open path. Returns TRUE if successful, else FALSE.
724 BOOL PATH_Rectangle(DC *dc, INT x1, INT y1, INT x2, INT y2)
726 GdiPath *pPath = &dc->path;
727 POINT corners[2], pointTemp;
729 /* Check that path is open */
730 if(pPath->state!=PATH_Open)
733 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
736 /* Close any previous figure */
737 if(!CloseFigure(dc->hSelf))
739 /* The CloseFigure call shouldn't have failed */
744 /* Add four points to the path */
745 pointTemp.x=corners[1].x;
746 pointTemp.y=corners[0].y;
747 if(!PATH_AddEntry(pPath, &pointTemp, PT_MOVETO))
749 if(!PATH_AddEntry(pPath, corners, PT_LINETO))
751 pointTemp.x=corners[0].x;
752 pointTemp.y=corners[1].y;
753 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
755 if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
758 /* Close the rectangle figure */
759 if(!CloseFigure(dc->hSelf))
761 /* The CloseFigure call shouldn't have failed */
771 * Should be called when a call to Ellipse is performed on a DC that has
772 * an open path. This adds four Bezier splines representing the ellipse
773 * to the path. Returns TRUE if successful, else FALSE.
775 BOOL PATH_Ellipse(DC *dc, INT x1, INT y1, INT x2, INT y2)
777 return( PATH_Arc(dc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2,0) &&
778 CloseFigure(dc->hSelf) );
783 * Should be called when a call to Arc is performed on a DC that has
784 * an open path. This adds up to five Bezier splines representing the arc
785 * to the path. When 'lines' is 1, we add 1 extra line to get a chord,
786 * and when 'lines' is 2, we add 2 extra lines to get a pie.
787 * Returns TRUE if successful, else FALSE.
789 BOOL PATH_Arc(DC *dc, INT x1, INT y1, INT x2, INT y2,
790 INT xStart, INT yStart, INT xEnd, INT yEnd, INT lines)
792 GdiPath *pPath = &dc->path;
793 double angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
794 /* Initialize angleEndQuadrant to silence gcc's warning */
796 FLOAT_POINT corners[2], pointStart, pointEnd;
801 /* FIXME: This function should check for all possible error returns */
802 /* FIXME: Do we have to respect newStroke? */
804 /* Check that path is open */
805 if(pPath->state!=PATH_Open)
808 /* Check for zero height / width */
809 /* FIXME: Only in GM_COMPATIBLE? */
813 /* Convert points to device coordinates */
814 corners[0].x=(FLOAT)x1;
815 corners[0].y=(FLOAT)y1;
816 corners[1].x=(FLOAT)x2;
817 corners[1].y=(FLOAT)y2;
818 pointStart.x=(FLOAT)xStart;
819 pointStart.y=(FLOAT)yStart;
820 pointEnd.x=(FLOAT)xEnd;
821 pointEnd.y=(FLOAT)yEnd;
822 INTERNAL_LPTODP_FLOAT(dc, corners);
823 INTERNAL_LPTODP_FLOAT(dc, corners+1);
824 INTERNAL_LPTODP_FLOAT(dc, &pointStart);
825 INTERNAL_LPTODP_FLOAT(dc, &pointEnd);
827 /* Make sure first corner is top left and second corner is bottom right */
828 if(corners[0].x>corners[1].x)
831 corners[0].x=corners[1].x;
834 if(corners[0].y>corners[1].y)
837 corners[0].y=corners[1].y;
841 /* Compute start and end angle */
842 PATH_NormalizePoint(corners, &pointStart, &x, &y);
843 angleStart=atan2(y, x);
844 PATH_NormalizePoint(corners, &pointEnd, &x, &y);
845 angleEnd=atan2(y, x);
847 /* Make sure the end angle is "on the right side" of the start angle */
848 if(dc->ArcDirection==AD_CLOCKWISE)
850 if(angleEnd<=angleStart)
853 assert(angleEnd>=angleStart);
858 if(angleEnd>=angleStart)
861 assert(angleEnd<=angleStart);
865 /* In GM_COMPATIBLE, don't include bottom and right edges */
866 if(dc->GraphicsMode==GM_COMPATIBLE)
872 /* Add the arc to the path with one Bezier spline per quadrant that the
878 /* Determine the start and end angles for this quadrant */
881 angleStartQuadrant=angleStart;
882 if(dc->ArcDirection==AD_CLOCKWISE)
883 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
885 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
889 angleStartQuadrant=angleEndQuadrant;
890 if(dc->ArcDirection==AD_CLOCKWISE)
891 angleEndQuadrant+=M_PI_2;
893 angleEndQuadrant-=M_PI_2;
896 /* Have we reached the last part of the arc? */
897 if((dc->ArcDirection==AD_CLOCKWISE &&
898 angleEnd<angleEndQuadrant) ||
899 (dc->ArcDirection==AD_COUNTERCLOCKWISE &&
900 angleEnd>angleEndQuadrant))
902 /* Adjust the end angle for this quadrant */
903 angleEndQuadrant=angleEnd;
907 /* Add the Bezier spline to the path */
908 PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
913 /* chord: close figure. pie: add line and close figure */
916 if(!CloseFigure(dc->hSelf))
921 centre.x = (corners[0].x+corners[1].x)/2;
922 centre.y = (corners[0].y+corners[1].y)/2;
923 if(!PATH_AddEntry(pPath, ¢re, PT_LINETO | PT_CLOSEFIGURE))
930 BOOL PATH_PolyBezierTo(DC *dc, const POINT *pts, DWORD cbPoints)
932 GdiPath *pPath = &dc->path;
936 /* Check that path is open */
937 if(pPath->state!=PATH_Open)
940 /* Add a PT_MOVETO if necessary */
943 pPath->newStroke=FALSE;
946 if(!LPtoDP(dc->hSelf, &pt, 1))
948 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
952 for(i = 0; i < cbPoints; i++) {
954 if(!LPtoDP(dc->hSelf, &pt, 1))
956 PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
961 BOOL PATH_PolyBezier(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 for(i = 0; i < cbPoints; i++) {
973 if(!LPtoDP(dc->hSelf, &pt, 1))
975 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
980 BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
982 GdiPath *pPath = &dc->path;
986 /* Check that path is open */
987 if(pPath->state!=PATH_Open)
990 for(i = 0; i < cbPoints; i++) {
992 if(!LPtoDP(dc->hSelf, &pt, 1))
994 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
999 BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
1001 GdiPath *pPath = &dc->path;
1005 /* Check that path is open */
1006 if(pPath->state!=PATH_Open)
1009 /* Add a PT_MOVETO if necessary */
1010 if(pPath->newStroke)
1012 pPath->newStroke=FALSE;
1013 pt.x = dc->CursPosX;
1014 pt.y = dc->CursPosY;
1015 if(!LPtoDP(dc->hSelf, &pt, 1))
1017 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
1021 for(i = 0; i < cbPoints; i++) {
1023 if(!LPtoDP(dc->hSelf, &pt, 1))
1025 PATH_AddEntry(pPath, &pt, PT_LINETO);
1032 BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
1034 GdiPath *pPath = &dc->path;
1038 /* Check that path is open */
1039 if(pPath->state!=PATH_Open)
1042 for(i = 0; i < cbPoints; i++) {
1044 if(!LPtoDP(dc->hSelf, &pt, 1))
1046 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
1047 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
1053 BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
1056 GdiPath *pPath = &dc->path;
1060 /* Check that path is open */
1061 if(pPath->state!=PATH_Open)
1064 for(i = 0, poly = 0; poly < polygons; poly++) {
1065 for(point = 0; point < counts[poly]; point++, i++) {
1067 if(!LPtoDP(dc->hSelf, &pt, 1))
1069 if(point == 0) startpt = pt;
1070 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1072 /* win98 adds an extra line to close the figure for some reason */
1073 PATH_AddEntry(pPath, &startpt, PT_LINETO | PT_CLOSEFIGURE);
1078 BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1081 GdiPath *pPath = &dc->path;
1085 /* Check that path is open */
1086 if(pPath->state!=PATH_Open)
1089 for(i = 0, poly = 0; poly < polylines; poly++) {
1090 for(point = 0; point < counts[poly]; point++, i++) {
1092 if(!LPtoDP(dc->hSelf, &pt, 1))
1094 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1100 /***********************************************************************
1101 * Internal functions
1104 /* PATH_CheckCorners
1106 * Helper function for PATH_RoundRect() and PATH_Rectangle()
1108 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2)
1112 /* Convert points to device coordinates */
1117 if(!LPtoDP(dc->hSelf, corners, 2))
1120 /* Make sure first corner is top left and second corner is bottom right */
1121 if(corners[0].x>corners[1].x)
1124 corners[0].x=corners[1].x;
1127 if(corners[0].y>corners[1].y)
1130 corners[0].y=corners[1].y;
1134 /* In GM_COMPATIBLE, don't include bottom and right edges */
1135 if(dc->GraphicsMode==GM_COMPATIBLE)
1144 /* PATH_AddFlatBezier
1146 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
1151 pts = GDI_Bezier( pt, 4, &no );
1152 if(!pts) return FALSE;
1154 for(i = 1; i < no; i++)
1155 PATH_AddEntry(pPath, &pts[i],
1156 (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
1157 HeapFree( GetProcessHeap(), 0, pts );
1163 * Replaces Beziers with line segments
1166 static BOOL PATH_FlattenPath(GdiPath *pPath)
1171 memset(&newPath, 0, sizeof(newPath));
1172 newPath.state = PATH_Open;
1173 for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
1174 switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
1177 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
1178 pPath->pFlags[srcpt]);
1181 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
1182 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
1187 newPath.state = PATH_Closed;
1188 PATH_AssignGdiPath(pPath, &newPath);
1189 PATH_EmptyPath(&newPath);
1193 /* PATH_PathToRegion
1195 * Creates a region from the specified path using the specified polygon
1196 * filling mode. The path is left unchanged. A handle to the region that
1197 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
1198 * error occurs, SetLastError is called with the appropriate value and
1199 * FALSE is returned.
1201 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
1204 int numStrokes, iStroke, i;
1205 INT *pNumPointsInStroke;
1208 assert(pPath!=NULL);
1209 assert(pHrgn!=NULL);
1211 PATH_FlattenPath(pPath);
1213 /* FIXME: What happens when number of points is zero? */
1215 /* First pass: Find out how many strokes there are in the path */
1216 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
1218 for(i=0; i<pPath->numEntriesUsed; i++)
1219 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1222 /* Allocate memory for number-of-points-in-stroke array */
1223 pNumPointsInStroke=(int *)HeapAlloc( GetProcessHeap(), 0,
1224 sizeof(int) * numStrokes );
1225 if(!pNumPointsInStroke)
1227 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1231 /* Second pass: remember number of points in each polygon */
1232 iStroke=-1; /* Will get incremented to 0 at beginning of first stroke */
1233 for(i=0; i<pPath->numEntriesUsed; i++)
1235 /* Is this the beginning of a new stroke? */
1236 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1239 pNumPointsInStroke[iStroke]=0;
1242 pNumPointsInStroke[iStroke]++;
1245 /* Create a region from the strokes */
1246 hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
1247 numStrokes, nPolyFillMode);
1249 /* Free memory for number-of-points-in-stroke array */
1250 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
1254 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1265 * Removes all entries from the path and sets the path state to PATH_Null.
1267 static void PATH_EmptyPath(GdiPath *pPath)
1269 assert(pPath!=NULL);
1271 pPath->state=PATH_Null;
1272 pPath->numEntriesUsed=0;
1277 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1278 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1279 * successful, FALSE otherwise (e.g. if not enough memory was available).
1281 BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
1283 assert(pPath!=NULL);
1285 /* FIXME: If newStroke is true, perhaps we want to check that we're
1286 * getting a PT_MOVETO
1288 TRACE("(%ld,%ld) - %d\n", pPoint->x, pPoint->y, flags);
1290 /* Check that path is open */
1291 if(pPath->state!=PATH_Open)
1294 /* Reserve enough memory for an extra path entry */
1295 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
1298 /* Store information in path entry */
1299 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
1300 pPath->pFlags[pPath->numEntriesUsed]=flags;
1302 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1303 if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
1304 pPath->newStroke=TRUE;
1306 /* Increment entry count */
1307 pPath->numEntriesUsed++;
1312 /* PATH_ReserveEntries
1314 * Ensures that at least "numEntries" entries (for points and flags) have
1315 * been allocated; allocates larger arrays and copies the existing entries
1316 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1318 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries)
1320 INT numEntriesToAllocate;
1324 assert(pPath!=NULL);
1325 assert(numEntries>=0);
1327 /* Do we have to allocate more memory? */
1328 if(numEntries > pPath->numEntriesAllocated)
1330 /* Find number of entries to allocate. We let the size of the array
1331 * grow exponentially, since that will guarantee linear time
1333 if(pPath->numEntriesAllocated)
1335 numEntriesToAllocate=pPath->numEntriesAllocated;
1336 while(numEntriesToAllocate<numEntries)
1337 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
1341 numEntriesToAllocate=numEntries;
1343 /* Allocate new arrays */
1344 pPointsNew=(POINT *)HeapAlloc( GetProcessHeap(), 0,
1345 numEntriesToAllocate * sizeof(POINT) );
1348 pFlagsNew=(BYTE *)HeapAlloc( GetProcessHeap(), 0,
1349 numEntriesToAllocate * sizeof(BYTE) );
1352 HeapFree( GetProcessHeap(), 0, pPointsNew );
1356 /* Copy old arrays to new arrays and discard old arrays */
1359 assert(pPath->pFlags);
1361 memcpy(pPointsNew, pPath->pPoints,
1362 sizeof(POINT)*pPath->numEntriesUsed);
1363 memcpy(pFlagsNew, pPath->pFlags,
1364 sizeof(BYTE)*pPath->numEntriesUsed);
1366 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
1367 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
1369 pPath->pPoints=pPointsNew;
1370 pPath->pFlags=pFlagsNew;
1371 pPath->numEntriesAllocated=numEntriesToAllocate;
1379 * Creates a Bezier spline that corresponds to part of an arc and appends the
1380 * corresponding points to the path. The start and end angles are passed in
1381 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1382 * at most. If "addMoveTo" is true, a PT_MOVETO entry for the first control
1383 * point is added to the path; otherwise, it is assumed that the current
1384 * position is equal to the first control point.
1386 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
1387 double angleStart, double angleEnd, BOOL addMoveTo)
1389 double halfAngle, a;
1390 double xNorm[4], yNorm[4];
1394 assert(fabs(angleEnd-angleStart)<=M_PI_2);
1396 /* FIXME: Is there an easier way of computing this? */
1398 /* Compute control points */
1399 halfAngle=(angleEnd-angleStart)/2.0;
1400 if(fabs(halfAngle)>1e-8)
1402 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
1403 xNorm[0]=cos(angleStart);
1404 yNorm[0]=sin(angleStart);
1405 xNorm[1]=xNorm[0] - a*yNorm[0];
1406 yNorm[1]=yNorm[0] + a*xNorm[0];
1407 xNorm[3]=cos(angleEnd);
1408 yNorm[3]=sin(angleEnd);
1409 xNorm[2]=xNorm[3] + a*yNorm[3];
1410 yNorm[2]=yNorm[3] - a*xNorm[3];
1415 xNorm[i]=cos(angleStart);
1416 yNorm[i]=sin(angleStart);
1419 /* Add starting point to path if desired */
1422 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1423 if(!PATH_AddEntry(pPath, &point, PT_MOVETO))
1427 /* Add remaining control points */
1430 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1431 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1438 /* PATH_ScaleNormalizedPoint
1440 * Scales a normalized point (x, y) with respect to the box whose corners are
1441 * passed in "corners". The point is stored in "*pPoint". The normalized
1442 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1443 * (1.0, 1.0) correspond to corners[1].
1445 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
1446 double y, POINT *pPoint)
1448 pPoint->x=GDI_ROUND( (double)corners[0].x +
1449 (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
1450 pPoint->y=GDI_ROUND( (double)corners[0].y +
1451 (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
1454 /* PATH_NormalizePoint
1456 * Normalizes a point with respect to the box whose corners are passed in
1457 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1459 static void PATH_NormalizePoint(FLOAT_POINT corners[],
1460 const FLOAT_POINT *pPoint,
1461 double *pX, double *pY)
1463 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) *
1465 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1469 /*******************************************************************
1470 * FlattenPath [GDI.516]
1474 BOOL16 WINAPI FlattenPath16(HDC16 hdc)
1476 return (BOOL16) FlattenPath((HDC) hdc);
1479 /*******************************************************************
1480 * FlattenPath [GDI32.@]
1484 BOOL WINAPI FlattenPath(HDC hdc)
1487 DC *dc = DC_GetDCPtr( hdc );
1489 if(!dc) return FALSE;
1491 if(dc->funcs->pFlattenPath) ret = dc->funcs->pFlattenPath(dc->physDev);
1494 GdiPath *pPath = &dc->path;
1495 if(pPath->state != PATH_Closed)
1496 ret = PATH_FlattenPath(pPath);
1498 GDI_ReleaseObj( hdc );
1503 static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1506 POINT ptLastMove = {0,0};
1507 POINT ptViewportOrg, ptWindowOrg;
1508 SIZE szViewportExt, szWindowExt;
1509 DWORD mapMode, graphicsMode;
1513 if(dc->funcs->pStrokePath)
1514 return dc->funcs->pStrokePath(dc->physDev);
1516 if(pPath->state != PATH_Closed)
1519 /* Save the mapping mode info */
1520 mapMode=GetMapMode(dc->hSelf);
1521 GetViewportExtEx(dc->hSelf, &szViewportExt);
1522 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
1523 GetWindowExtEx(dc->hSelf, &szWindowExt);
1524 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
1525 GetWorldTransform(dc->hSelf, &xform);
1528 SetMapMode(dc->hSelf, MM_TEXT);
1529 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
1530 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
1533 for(i = 0; i < pPath->numEntriesUsed; i++) {
1534 switch(pPath->pFlags[i]) {
1536 TRACE("Got PT_MOVETO (%ld, %ld)\n",
1537 pPath->pPoints[i].x, pPath->pPoints[i].y);
1538 MoveToEx(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y, NULL);
1539 ptLastMove = pPath->pPoints[i];
1542 case (PT_LINETO | PT_CLOSEFIGURE):
1543 TRACE("Got PT_LINETO (%ld, %ld)\n",
1544 pPath->pPoints[i].x, pPath->pPoints[i].y);
1545 LineTo(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y);
1548 TRACE("Got PT_BEZIERTO\n");
1549 if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1550 (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1551 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1555 PolyBezierTo(dc->hSelf, &pPath->pPoints[i], 3);
1559 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1563 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1564 LineTo(dc->hSelf, ptLastMove.x, ptLastMove.y);
1569 /* Restore the old mapping mode */
1570 SetMapMode(dc->hSelf, mapMode);
1571 SetViewportExtEx(dc->hSelf, szViewportExt.cx, szViewportExt.cy, NULL);
1572 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
1573 SetWindowExtEx(dc->hSelf, szWindowExt.cx, szWindowExt.cy, NULL);
1574 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
1576 /* Go to GM_ADVANCED temporarily to restore the world transform */
1577 graphicsMode=GetGraphicsMode(dc->hSelf);
1578 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1579 SetWorldTransform(dc->hSelf, &xform);
1580 SetGraphicsMode(dc->hSelf, graphicsMode);
1585 /*******************************************************************
1586 * StrokeAndFillPath [GDI.520]
1590 BOOL16 WINAPI StrokeAndFillPath16(HDC16 hdc)
1592 return (BOOL16) StrokeAndFillPath((HDC) hdc);
1595 /*******************************************************************
1596 * StrokeAndFillPath [GDI32.@]
1600 BOOL WINAPI StrokeAndFillPath(HDC hdc)
1602 DC *dc = DC_GetDCPtr( hdc );
1605 if(!dc) return FALSE;
1607 if(dc->funcs->pStrokeAndFillPath)
1608 bRet = dc->funcs->pStrokeAndFillPath(dc->physDev);
1611 bRet = PATH_FillPath(dc, &dc->path);
1612 if(bRet) bRet = PATH_StrokePath(dc, &dc->path);
1613 if(bRet) PATH_EmptyPath(&dc->path);
1615 GDI_ReleaseObj( hdc );
1619 /*******************************************************************
1620 * StrokePath [GDI.521]
1624 BOOL16 WINAPI StrokePath16(HDC16 hdc)
1626 return (BOOL16) StrokePath((HDC) hdc);
1629 /*******************************************************************
1630 * StrokePath [GDI32.@]
1634 BOOL WINAPI StrokePath(HDC hdc)
1636 DC *dc = DC_GetDCPtr( hdc );
1640 TRACE("(%08x)\n", hdc);
1641 if(!dc) return FALSE;
1643 if(dc->funcs->pStrokePath)
1644 bRet = dc->funcs->pStrokePath(dc->physDev);
1648 bRet = PATH_StrokePath(dc, pPath);
1649 PATH_EmptyPath(pPath);
1651 GDI_ReleaseObj( hdc );
1655 /*******************************************************************
1656 * WidenPath [GDI.522]
1660 BOOL16 WINAPI WidenPath16(HDC16 hdc)
1662 return (BOOL16) WidenPath((HDC) hdc);
1665 /*******************************************************************
1666 * WidenPath [GDI32.@]
1670 BOOL WINAPI WidenPath(HDC hdc)
1672 DC *dc = DC_GetDCPtr( hdc );
1675 if(!dc) return FALSE;
1677 if(dc->funcs->pWidenPath)
1678 ret = dc->funcs->pWidenPath(dc->physDev);
1681 GDI_ReleaseObj( hdc );