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
23 #include "wine/port.h"
29 #if defined(HAVE_FLOAT_H)
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
43 /* Notes on the implementation
45 * The implementation is based on dynamically resizable arrays of points and
46 * flags. I dithered for a bit before deciding on this implementation, and
47 * I had even done a bit of work on a linked list version before switching
48 * to arrays. It's a bit of a tradeoff. When you use linked lists, the
49 * implementation of FlattenPath is easier, because you can rip the
50 * PT_BEZIERTO entries out of the middle of the list and link the
51 * corresponding PT_LINETO entries in. However, when you use arrays,
52 * PathToRegion becomes easier, since you can essentially just pass your array
53 * of points to CreatePolyPolygonRgn. Also, if I'd used linked lists, I would
54 * have had the extra effort of creating a chunk-based allocation scheme
55 * in order to use memory effectively. That's why I finally decided to use
56 * arrays. Note by the way that the array based implementation has the same
57 * linear time complexity that linked lists would have since the arrays grow
60 * The points are stored in the path in device coordinates. This is
61 * consistent with the way Windows does things (for instance, see the Win32
62 * SDK documentation for GetPath).
64 * The word "stroke" appears in several places (e.g. in the flag
65 * GdiPath.newStroke). A stroke consists of a PT_MOVETO followed by one or
66 * more PT_LINETOs or PT_BEZIERTOs, up to, but not including, the next
67 * PT_MOVETO. Note that this is not the same as the definition of a figure;
68 * a figure can contain several strokes.
70 * I modified the drawing functions (MoveTo, LineTo etc.) to test whether
71 * the path is open and to call the corresponding function in path.c if this
72 * is the case. A more elegant approach would be to modify the function
73 * pointers in the DC_FUNCTIONS structure; however, this would be a lot more
74 * complex. Also, the performance degradation caused by my approach in the
75 * case where no path is open is so small that it cannot be measured.
80 /* FIXME: A lot of stuff isn't implemented yet. There is much more to come. */
82 #define NUM_ENTRIES_INITIAL 16 /* Initial size of points / flags arrays */
83 #define GROW_FACTOR_NUMER 2 /* Numerator of grow factor for the array */
84 #define GROW_FACTOR_DENOM 1 /* Denominator of grow factor */
86 /* A floating point version of the POINT structure */
87 typedef struct tagFLOAT_POINT
93 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
95 static void PATH_EmptyPath(GdiPath *pPath);
96 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries);
97 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
98 double angleStart, double angleEnd, BOOL addMoveTo);
99 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
100 double y, POINT *pPoint);
101 static void PATH_NormalizePoint(FLOAT_POINT corners[], const FLOAT_POINT
102 *pPoint, double *pX, double *pY);
103 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2);
105 /* Performs a world-to-viewport transformation on the specified point (which
106 * is in floating point format).
108 static inline void WINE_UNUSED INTERNAL_LPTODP_FLOAT(DC *dc, FLOAT_POINT *point)
112 /* Perform the transformation */
115 point->x = x * dc->xformWorld2Vport.eM11 +
116 y * dc->xformWorld2Vport.eM21 +
117 dc->xformWorld2Vport.eDx;
118 point->y = x * dc->xformWorld2Vport.eM12 +
119 y * dc->xformWorld2Vport.eM22 +
120 dc->xformWorld2Vport.eDy;
124 /***********************************************************************
125 * BeginPath (GDI32.@)
127 BOOL WINAPI BeginPath(HDC hdc)
130 DC *dc = DC_GetDCPtr( hdc );
132 if(!dc) return FALSE;
134 if(dc->funcs->pBeginPath)
135 ret = dc->funcs->pBeginPath(dc->physDev);
138 /* If path is already open, do nothing */
139 if(dc->path.state != PATH_Open)
141 /* Make sure that path is empty */
142 PATH_EmptyPath(&dc->path);
144 /* Initialize variables for new path */
145 dc->path.newStroke=TRUE;
146 dc->path.state=PATH_Open;
149 GDI_ReleaseObj( hdc );
154 /***********************************************************************
157 BOOL WINAPI EndPath(HDC hdc)
160 DC *dc = DC_GetDCPtr( hdc );
162 if(!dc) return FALSE;
164 if(dc->funcs->pEndPath)
165 ret = dc->funcs->pEndPath(dc->physDev);
168 /* Check that path is currently being constructed */
169 if(dc->path.state!=PATH_Open)
171 SetLastError(ERROR_CAN_NOT_COMPLETE);
174 /* Set flag to indicate that path is finished */
175 else dc->path.state=PATH_Closed;
177 GDI_ReleaseObj( hdc );
182 /******************************************************************************
183 * AbortPath [GDI32.@]
184 * Closes and discards paths from device context
187 * Check that SetLastError is being called correctly
190 * hdc [I] Handle to device context
194 BOOL WINAPI AbortPath( HDC hdc )
197 DC *dc = DC_GetDCPtr( hdc );
199 if(!dc) return FALSE;
201 if(dc->funcs->pAbortPath)
202 ret = dc->funcs->pAbortPath(dc->physDev);
203 else /* Remove all entries from the path */
204 PATH_EmptyPath( &dc->path );
205 GDI_ReleaseObj( hdc );
210 /***********************************************************************
211 * CloseFigure (GDI32.@)
213 * FIXME: Check that SetLastError is being called correctly
215 BOOL WINAPI CloseFigure(HDC hdc)
218 DC *dc = DC_GetDCPtr( hdc );
220 if(!dc) return FALSE;
222 if(dc->funcs->pCloseFigure)
223 ret = dc->funcs->pCloseFigure(dc->physDev);
226 /* Check that path is open */
227 if(dc->path.state!=PATH_Open)
229 SetLastError(ERROR_CAN_NOT_COMPLETE);
234 /* FIXME: Shouldn't we draw a line to the beginning of the
236 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
237 if(dc->path.numEntriesUsed)
239 dc->path.pFlags[dc->path.numEntriesUsed-1]|=PT_CLOSEFIGURE;
240 dc->path.newStroke=TRUE;
244 GDI_ReleaseObj( hdc );
249 /***********************************************************************
252 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
257 DC *dc = DC_GetDCPtr( hdc );
263 /* Check that path is closed */
264 if(pPath->state!=PATH_Closed)
266 SetLastError(ERROR_CAN_NOT_COMPLETE);
271 ret = pPath->numEntriesUsed;
272 else if(nSize<pPath->numEntriesUsed)
274 SetLastError(ERROR_INVALID_PARAMETER);
279 memcpy(pPoints, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
280 memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
282 /* Convert the points to logical coordinates */
283 if(!DPtoLP(hdc, pPoints, pPath->numEntriesUsed))
285 /* FIXME: Is this the correct value? */
286 SetLastError(ERROR_CAN_NOT_COMPLETE);
289 else ret = pPath->numEntriesUsed;
292 GDI_ReleaseObj( hdc );
297 /***********************************************************************
298 * PathToRegion (GDI32.@)
301 * Check that SetLastError is being called correctly
303 * The documentation does not state this explicitly, but a test under Windows
304 * shows that the region which is returned should be in device coordinates.
306 HRGN WINAPI PathToRegion(HDC hdc)
310 DC *dc = DC_GetDCPtr( hdc );
312 /* Get pointer to path */
317 /* Check that path is closed */
318 if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
321 /* FIXME: Should we empty the path even if conversion failed? */
322 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
323 PATH_EmptyPath(pPath);
327 GDI_ReleaseObj( hdc );
331 static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
333 INT mapMode, graphicsMode;
334 SIZE ptViewportExt, ptWindowExt;
335 POINT ptViewportOrg, ptWindowOrg;
339 if(dc->funcs->pFillPath)
340 return dc->funcs->pFillPath(dc->physDev);
342 /* Check that path is closed */
343 if(pPath->state!=PATH_Closed)
345 SetLastError(ERROR_CAN_NOT_COMPLETE);
349 /* Construct a region from the path and fill it */
350 if(PATH_PathToRegion(pPath, dc->polyFillMode, &hrgn))
352 /* Since PaintRgn interprets the region as being in logical coordinates
353 * but the points we store for the path are already in device
354 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
355 * Using SaveDC to save information about the mapping mode / world
356 * transform would be easier but would require more overhead, especially
357 * now that SaveDC saves the current path.
360 /* Save the information about the old mapping mode */
361 mapMode=GetMapMode(dc->hSelf);
362 GetViewportExtEx(dc->hSelf, &ptViewportExt);
363 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
364 GetWindowExtEx(dc->hSelf, &ptWindowExt);
365 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
367 /* Save world transform
368 * NB: The Windows documentation on world transforms would lead one to
369 * believe that this has to be done only in GM_ADVANCED; however, my
370 * tests show that resetting the graphics mode to GM_COMPATIBLE does
371 * not reset the world transform.
373 GetWorldTransform(dc->hSelf, &xform);
376 SetMapMode(dc->hSelf, MM_TEXT);
377 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
378 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
379 graphicsMode=GetGraphicsMode(dc->hSelf);
380 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
381 ModifyWorldTransform(dc->hSelf, &xform, MWT_IDENTITY);
382 SetGraphicsMode(dc->hSelf, graphicsMode);
384 /* Paint the region */
385 PaintRgn(dc->hSelf, hrgn);
387 /* Restore the old mapping mode */
388 SetMapMode(dc->hSelf, mapMode);
389 SetViewportExtEx(dc->hSelf, ptViewportExt.cx, ptViewportExt.cy, NULL);
390 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
391 SetWindowExtEx(dc->hSelf, ptWindowExt.cx, ptWindowExt.cy, NULL);
392 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
394 /* Go to GM_ADVANCED temporarily to restore the world transform */
395 graphicsMode=GetGraphicsMode(dc->hSelf);
396 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
397 SetWorldTransform(dc->hSelf, &xform);
398 SetGraphicsMode(dc->hSelf, graphicsMode);
405 /***********************************************************************
409 * Check that SetLastError is being called correctly
411 BOOL WINAPI FillPath(HDC hdc)
413 DC *dc = DC_GetDCPtr( hdc );
416 if(!dc) return FALSE;
418 if(dc->funcs->pFillPath)
419 bRet = dc->funcs->pFillPath(dc->physDev);
422 bRet = PATH_FillPath(dc, &dc->path);
425 /* FIXME: Should the path be emptied even if conversion
427 PATH_EmptyPath(&dc->path);
430 GDI_ReleaseObj( hdc );
435 /***********************************************************************
436 * SelectClipPath (GDI32.@)
438 * Check that SetLastError is being called correctly
440 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
444 BOOL success = FALSE;
445 DC *dc = DC_GetDCPtr( hdc );
447 if(!dc) return FALSE;
449 if(dc->funcs->pSelectClipPath)
450 success = dc->funcs->pSelectClipPath(dc->physDev, iMode);
455 /* Check that path is closed */
456 if(pPath->state!=PATH_Closed)
457 SetLastError(ERROR_CAN_NOT_COMPLETE);
458 /* Construct a region from the path */
459 else if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnPath))
461 success = ExtSelectClipRgn( hdc, hrgnPath, iMode ) != ERROR;
462 DeleteObject(hrgnPath);
466 PATH_EmptyPath(pPath);
467 /* FIXME: Should this function delete the path even if it failed? */
470 GDI_ReleaseObj( hdc );
475 /***********************************************************************
481 * Initializes the GdiPath structure.
483 void PATH_InitGdiPath(GdiPath *pPath)
487 pPath->state=PATH_Null;
490 pPath->numEntriesUsed=0;
491 pPath->numEntriesAllocated=0;
494 /* PATH_DestroyGdiPath
496 * Destroys a GdiPath structure (frees the memory in the arrays).
498 void PATH_DestroyGdiPath(GdiPath *pPath)
502 if (pPath->pPoints) HeapFree( GetProcessHeap(), 0, pPath->pPoints );
503 if (pPath->pFlags) HeapFree( GetProcessHeap(), 0, pPath->pFlags );
506 /* PATH_AssignGdiPath
508 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
509 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
510 * not just the pointers. Since this means that the arrays in pPathDest may
511 * need to be resized, pPathDest should have been initialized using
512 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
513 * not a copy constructor).
514 * Returns TRUE if successful, else FALSE.
516 BOOL PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
518 assert(pPathDest!=NULL && pPathSrc!=NULL);
520 /* Make sure destination arrays are big enough */
521 if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
524 /* Perform the copy operation */
525 memcpy(pPathDest->pPoints, pPathSrc->pPoints,
526 sizeof(POINT)*pPathSrc->numEntriesUsed);
527 memcpy(pPathDest->pFlags, pPathSrc->pFlags,
528 sizeof(BYTE)*pPathSrc->numEntriesUsed);
530 pPathDest->state=pPathSrc->state;
531 pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
532 pPathDest->newStroke=pPathSrc->newStroke;
539 * Should be called when a MoveTo is performed on a DC that has an
540 * open path. This starts a new stroke. Returns TRUE if successful, else
543 BOOL PATH_MoveTo(DC *dc)
545 GdiPath *pPath = &dc->path;
547 /* Check that path is open */
548 if(pPath->state!=PATH_Open)
549 /* FIXME: Do we have to call SetLastError? */
552 /* Start a new stroke */
553 pPath->newStroke=TRUE;
560 * Should be called when a LineTo is performed on a DC that has an
561 * open path. This adds a PT_LINETO entry to the path (and possibly
562 * a PT_MOVETO entry, if this is the first LineTo in a stroke).
563 * Returns TRUE if successful, else FALSE.
565 BOOL PATH_LineTo(DC *dc, INT x, INT y)
567 GdiPath *pPath = &dc->path;
568 POINT point, pointCurPos;
570 /* Check that path is open */
571 if(pPath->state!=PATH_Open)
574 /* Convert point to device coordinates */
577 if(!LPtoDP(dc->hSelf, &point, 1))
580 /* Add a PT_MOVETO if necessary */
583 pPath->newStroke=FALSE;
584 pointCurPos.x = dc->CursPosX;
585 pointCurPos.y = dc->CursPosY;
586 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
588 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
592 /* Add a PT_LINETO entry */
593 return PATH_AddEntry(pPath, &point, PT_LINETO);
598 * Should be called when a call to RoundRect is performed on a DC that has
599 * an open path. Returns TRUE if successful, else FALSE.
601 * FIXME: it adds the same entries to the path as windows does, but there
602 * is an error in the bezier drawing code so that there are small pixel-size
603 * gaps when the resulting path is drawn by StrokePath()
605 BOOL PATH_RoundRect(DC *dc, INT x1, INT y1, INT x2, INT y2, INT ell_width, INT ell_height)
607 GdiPath *pPath = &dc->path;
608 POINT corners[2], pointTemp;
609 FLOAT_POINT ellCorners[2];
611 /* Check that path is open */
612 if(pPath->state!=PATH_Open)
615 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
618 /* Add points to the roundrect path */
619 ellCorners[0].x = corners[1].x-ell_width;
620 ellCorners[0].y = corners[0].y;
621 ellCorners[1].x = corners[1].x;
622 ellCorners[1].y = corners[0].y+ell_height;
623 if(!PATH_DoArcPart(pPath, ellCorners, 0, -M_PI_2, TRUE))
625 pointTemp.x = corners[0].x+ell_width/2;
626 pointTemp.y = corners[0].y;
627 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
629 ellCorners[0].x = corners[0].x;
630 ellCorners[1].x = corners[0].x+ell_width;
631 if(!PATH_DoArcPart(pPath, ellCorners, -M_PI_2, -M_PI, FALSE))
633 pointTemp.x = corners[0].x;
634 pointTemp.y = corners[1].y-ell_height/2;
635 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
637 ellCorners[0].y = corners[1].y-ell_height;
638 ellCorners[1].y = corners[1].y;
639 if(!PATH_DoArcPart(pPath, ellCorners, M_PI, M_PI_2, FALSE))
641 pointTemp.x = corners[1].x-ell_width/2;
642 pointTemp.y = corners[1].y;
643 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
645 ellCorners[0].x = corners[1].x-ell_width;
646 ellCorners[1].x = corners[1].x;
647 if(!PATH_DoArcPart(pPath, ellCorners, M_PI_2, 0, FALSE))
650 /* Close the roundrect figure */
651 if(!CloseFigure(dc->hSelf))
659 * Should be called when a call to Rectangle is performed on a DC that has
660 * an open path. Returns TRUE if successful, else FALSE.
662 BOOL PATH_Rectangle(DC *dc, INT x1, INT y1, INT x2, INT y2)
664 GdiPath *pPath = &dc->path;
665 POINT corners[2], pointTemp;
667 /* Check that path is open */
668 if(pPath->state!=PATH_Open)
671 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
674 /* Close any previous figure */
675 if(!CloseFigure(dc->hSelf))
677 /* The CloseFigure call shouldn't have failed */
682 /* Add four points to the path */
683 pointTemp.x=corners[1].x;
684 pointTemp.y=corners[0].y;
685 if(!PATH_AddEntry(pPath, &pointTemp, PT_MOVETO))
687 if(!PATH_AddEntry(pPath, corners, PT_LINETO))
689 pointTemp.x=corners[0].x;
690 pointTemp.y=corners[1].y;
691 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
693 if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
696 /* Close the rectangle figure */
697 if(!CloseFigure(dc->hSelf))
699 /* The CloseFigure call shouldn't have failed */
709 * Should be called when a call to Ellipse is performed on a DC that has
710 * an open path. This adds four Bezier splines representing the ellipse
711 * to the path. Returns TRUE if successful, else FALSE.
713 BOOL PATH_Ellipse(DC *dc, INT x1, INT y1, INT x2, INT y2)
715 return( PATH_Arc(dc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2,0) &&
716 CloseFigure(dc->hSelf) );
721 * Should be called when a call to Arc is performed on a DC that has
722 * an open path. This adds up to five Bezier splines representing the arc
723 * to the path. When 'lines' is 1, we add 1 extra line to get a chord,
724 * and when 'lines' is 2, we add 2 extra lines to get a pie.
725 * Returns TRUE if successful, else FALSE.
727 BOOL PATH_Arc(DC *dc, INT x1, INT y1, INT x2, INT y2,
728 INT xStart, INT yStart, INT xEnd, INT yEnd, INT lines)
730 GdiPath *pPath = &dc->path;
731 double angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
732 /* Initialize angleEndQuadrant to silence gcc's warning */
734 FLOAT_POINT corners[2], pointStart, pointEnd;
739 /* FIXME: This function should check for all possible error returns */
740 /* FIXME: Do we have to respect newStroke? */
742 /* Check that path is open */
743 if(pPath->state!=PATH_Open)
746 /* Check for zero height / width */
747 /* FIXME: Only in GM_COMPATIBLE? */
751 /* Convert points to device coordinates */
752 corners[0].x=(FLOAT)x1;
753 corners[0].y=(FLOAT)y1;
754 corners[1].x=(FLOAT)x2;
755 corners[1].y=(FLOAT)y2;
756 pointStart.x=(FLOAT)xStart;
757 pointStart.y=(FLOAT)yStart;
758 pointEnd.x=(FLOAT)xEnd;
759 pointEnd.y=(FLOAT)yEnd;
760 INTERNAL_LPTODP_FLOAT(dc, corners);
761 INTERNAL_LPTODP_FLOAT(dc, corners+1);
762 INTERNAL_LPTODP_FLOAT(dc, &pointStart);
763 INTERNAL_LPTODP_FLOAT(dc, &pointEnd);
765 /* Make sure first corner is top left and second corner is bottom right */
766 if(corners[0].x>corners[1].x)
769 corners[0].x=corners[1].x;
772 if(corners[0].y>corners[1].y)
775 corners[0].y=corners[1].y;
779 /* Compute start and end angle */
780 PATH_NormalizePoint(corners, &pointStart, &x, &y);
781 angleStart=atan2(y, x);
782 PATH_NormalizePoint(corners, &pointEnd, &x, &y);
783 angleEnd=atan2(y, x);
785 /* Make sure the end angle is "on the right side" of the start angle */
786 if(dc->ArcDirection==AD_CLOCKWISE)
788 if(angleEnd<=angleStart)
791 assert(angleEnd>=angleStart);
796 if(angleEnd>=angleStart)
799 assert(angleEnd<=angleStart);
803 /* In GM_COMPATIBLE, don't include bottom and right edges */
804 if(dc->GraphicsMode==GM_COMPATIBLE)
810 /* Add the arc to the path with one Bezier spline per quadrant that the
816 /* Determine the start and end angles for this quadrant */
819 angleStartQuadrant=angleStart;
820 if(dc->ArcDirection==AD_CLOCKWISE)
821 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
823 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
827 angleStartQuadrant=angleEndQuadrant;
828 if(dc->ArcDirection==AD_CLOCKWISE)
829 angleEndQuadrant+=M_PI_2;
831 angleEndQuadrant-=M_PI_2;
834 /* Have we reached the last part of the arc? */
835 if((dc->ArcDirection==AD_CLOCKWISE &&
836 angleEnd<angleEndQuadrant) ||
837 (dc->ArcDirection==AD_COUNTERCLOCKWISE &&
838 angleEnd>angleEndQuadrant))
840 /* Adjust the end angle for this quadrant */
841 angleEndQuadrant=angleEnd;
845 /* Add the Bezier spline to the path */
846 PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
851 /* chord: close figure. pie: add line and close figure */
854 if(!CloseFigure(dc->hSelf))
859 centre.x = (corners[0].x+corners[1].x)/2;
860 centre.y = (corners[0].y+corners[1].y)/2;
861 if(!PATH_AddEntry(pPath, ¢re, PT_LINETO | PT_CLOSEFIGURE))
868 BOOL PATH_PolyBezierTo(DC *dc, const POINT *pts, DWORD cbPoints)
870 GdiPath *pPath = &dc->path;
874 /* Check that path is open */
875 if(pPath->state!=PATH_Open)
878 /* Add a PT_MOVETO if necessary */
881 pPath->newStroke=FALSE;
884 if(!LPtoDP(dc->hSelf, &pt, 1))
886 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
890 for(i = 0; i < cbPoints; i++) {
892 if(!LPtoDP(dc->hSelf, &pt, 1))
894 PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
899 BOOL PATH_PolyBezier(DC *dc, const POINT *pts, DWORD cbPoints)
901 GdiPath *pPath = &dc->path;
905 /* Check that path is open */
906 if(pPath->state!=PATH_Open)
909 for(i = 0; i < cbPoints; i++) {
911 if(!LPtoDP(dc->hSelf, &pt, 1))
913 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
918 BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
920 GdiPath *pPath = &dc->path;
924 /* Check that path is open */
925 if(pPath->state!=PATH_Open)
928 for(i = 0; i < cbPoints; i++) {
930 if(!LPtoDP(dc->hSelf, &pt, 1))
932 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
937 BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
939 GdiPath *pPath = &dc->path;
943 /* Check that path is open */
944 if(pPath->state!=PATH_Open)
947 /* Add a PT_MOVETO if necessary */
950 pPath->newStroke=FALSE;
953 if(!LPtoDP(dc->hSelf, &pt, 1))
955 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
959 for(i = 0; i < cbPoints; i++) {
961 if(!LPtoDP(dc->hSelf, &pt, 1))
963 PATH_AddEntry(pPath, &pt, PT_LINETO);
970 BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
972 GdiPath *pPath = &dc->path;
976 /* Check that path is open */
977 if(pPath->state!=PATH_Open)
980 for(i = 0; i < cbPoints; i++) {
982 if(!LPtoDP(dc->hSelf, &pt, 1))
984 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
985 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
991 BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
994 GdiPath *pPath = &dc->path;
998 /* Check that path is open */
999 if(pPath->state!=PATH_Open)
1002 for(i = 0, poly = 0; poly < polygons; poly++) {
1003 for(point = 0; point < counts[poly]; point++, i++) {
1005 if(!LPtoDP(dc->hSelf, &pt, 1))
1007 if(point == 0) startpt = pt;
1008 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1010 /* win98 adds an extra line to close the figure for some reason */
1011 PATH_AddEntry(pPath, &startpt, PT_LINETO | PT_CLOSEFIGURE);
1016 BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1019 GdiPath *pPath = &dc->path;
1023 /* Check that path is open */
1024 if(pPath->state!=PATH_Open)
1027 for(i = 0, poly = 0; poly < polylines; poly++) {
1028 for(point = 0; point < counts[poly]; point++, i++) {
1030 if(!LPtoDP(dc->hSelf, &pt, 1))
1032 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1038 /***********************************************************************
1039 * Internal functions
1042 /* PATH_CheckCorners
1044 * Helper function for PATH_RoundRect() and PATH_Rectangle()
1046 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2)
1050 /* Convert points to device coordinates */
1055 if(!LPtoDP(dc->hSelf, corners, 2))
1058 /* Make sure first corner is top left and second corner is bottom right */
1059 if(corners[0].x>corners[1].x)
1062 corners[0].x=corners[1].x;
1065 if(corners[0].y>corners[1].y)
1068 corners[0].y=corners[1].y;
1072 /* In GM_COMPATIBLE, don't include bottom and right edges */
1073 if(dc->GraphicsMode==GM_COMPATIBLE)
1082 /* PATH_AddFlatBezier
1084 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
1089 pts = GDI_Bezier( pt, 4, &no );
1090 if(!pts) return FALSE;
1092 for(i = 1; i < no; i++)
1093 PATH_AddEntry(pPath, &pts[i],
1094 (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
1095 HeapFree( GetProcessHeap(), 0, pts );
1101 * Replaces Beziers with line segments
1104 static BOOL PATH_FlattenPath(GdiPath *pPath)
1109 memset(&newPath, 0, sizeof(newPath));
1110 newPath.state = PATH_Open;
1111 for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
1112 switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
1115 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
1116 pPath->pFlags[srcpt]);
1119 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
1120 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
1125 newPath.state = PATH_Closed;
1126 PATH_AssignGdiPath(pPath, &newPath);
1127 PATH_DestroyGdiPath(&newPath);
1131 /* PATH_PathToRegion
1133 * Creates a region from the specified path using the specified polygon
1134 * filling mode. The path is left unchanged. A handle to the region that
1135 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
1136 * error occurs, SetLastError is called with the appropriate value and
1137 * FALSE is returned.
1139 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
1142 int numStrokes, iStroke, i;
1143 INT *pNumPointsInStroke;
1146 assert(pPath!=NULL);
1147 assert(pHrgn!=NULL);
1149 PATH_FlattenPath(pPath);
1151 /* FIXME: What happens when number of points is zero? */
1153 /* First pass: Find out how many strokes there are in the path */
1154 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
1156 for(i=0; i<pPath->numEntriesUsed; i++)
1157 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1160 /* Allocate memory for number-of-points-in-stroke array */
1161 pNumPointsInStroke=(int *)HeapAlloc( GetProcessHeap(), 0,
1162 sizeof(int) * numStrokes );
1163 if(!pNumPointsInStroke)
1165 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1169 /* Second pass: remember number of points in each polygon */
1170 iStroke=-1; /* Will get incremented to 0 at beginning of first stroke */
1171 for(i=0; i<pPath->numEntriesUsed; i++)
1173 /* Is this the beginning of a new stroke? */
1174 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1177 pNumPointsInStroke[iStroke]=0;
1180 pNumPointsInStroke[iStroke]++;
1183 /* Create a region from the strokes */
1184 hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
1185 numStrokes, nPolyFillMode);
1187 /* Free memory for number-of-points-in-stroke array */
1188 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
1192 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1203 * Removes all entries from the path and sets the path state to PATH_Null.
1205 static void PATH_EmptyPath(GdiPath *pPath)
1207 assert(pPath!=NULL);
1209 pPath->state=PATH_Null;
1210 pPath->numEntriesUsed=0;
1215 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1216 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1217 * successful, FALSE otherwise (e.g. if not enough memory was available).
1219 BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
1221 assert(pPath!=NULL);
1223 /* FIXME: If newStroke is true, perhaps we want to check that we're
1224 * getting a PT_MOVETO
1226 TRACE("(%ld,%ld) - %d\n", pPoint->x, pPoint->y, flags);
1228 /* Check that path is open */
1229 if(pPath->state!=PATH_Open)
1232 /* Reserve enough memory for an extra path entry */
1233 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
1236 /* Store information in path entry */
1237 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
1238 pPath->pFlags[pPath->numEntriesUsed]=flags;
1240 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1241 if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
1242 pPath->newStroke=TRUE;
1244 /* Increment entry count */
1245 pPath->numEntriesUsed++;
1250 /* PATH_ReserveEntries
1252 * Ensures that at least "numEntries" entries (for points and flags) have
1253 * been allocated; allocates larger arrays and copies the existing entries
1254 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1256 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries)
1258 INT numEntriesToAllocate;
1262 assert(pPath!=NULL);
1263 assert(numEntries>=0);
1265 /* Do we have to allocate more memory? */
1266 if(numEntries > pPath->numEntriesAllocated)
1268 /* Find number of entries to allocate. We let the size of the array
1269 * grow exponentially, since that will guarantee linear time
1271 if(pPath->numEntriesAllocated)
1273 numEntriesToAllocate=pPath->numEntriesAllocated;
1274 while(numEntriesToAllocate<numEntries)
1275 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
1279 numEntriesToAllocate=numEntries;
1281 /* Allocate new arrays */
1282 pPointsNew=(POINT *)HeapAlloc( GetProcessHeap(), 0,
1283 numEntriesToAllocate * sizeof(POINT) );
1286 pFlagsNew=(BYTE *)HeapAlloc( GetProcessHeap(), 0,
1287 numEntriesToAllocate * sizeof(BYTE) );
1290 HeapFree( GetProcessHeap(), 0, pPointsNew );
1294 /* Copy old arrays to new arrays and discard old arrays */
1297 assert(pPath->pFlags);
1299 memcpy(pPointsNew, pPath->pPoints,
1300 sizeof(POINT)*pPath->numEntriesUsed);
1301 memcpy(pFlagsNew, pPath->pFlags,
1302 sizeof(BYTE)*pPath->numEntriesUsed);
1304 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
1305 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
1307 pPath->pPoints=pPointsNew;
1308 pPath->pFlags=pFlagsNew;
1309 pPath->numEntriesAllocated=numEntriesToAllocate;
1317 * Creates a Bezier spline that corresponds to part of an arc and appends the
1318 * corresponding points to the path. The start and end angles are passed in
1319 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1320 * at most. If "addMoveTo" is true, a PT_MOVETO entry for the first control
1321 * point is added to the path; otherwise, it is assumed that the current
1322 * position is equal to the first control point.
1324 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
1325 double angleStart, double angleEnd, BOOL addMoveTo)
1327 double halfAngle, a;
1328 double xNorm[4], yNorm[4];
1332 assert(fabs(angleEnd-angleStart)<=M_PI_2);
1334 /* FIXME: Is there an easier way of computing this? */
1336 /* Compute control points */
1337 halfAngle=(angleEnd-angleStart)/2.0;
1338 if(fabs(halfAngle)>1e-8)
1340 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
1341 xNorm[0]=cos(angleStart);
1342 yNorm[0]=sin(angleStart);
1343 xNorm[1]=xNorm[0] - a*yNorm[0];
1344 yNorm[1]=yNorm[0] + a*xNorm[0];
1345 xNorm[3]=cos(angleEnd);
1346 yNorm[3]=sin(angleEnd);
1347 xNorm[2]=xNorm[3] + a*yNorm[3];
1348 yNorm[2]=yNorm[3] - a*xNorm[3];
1353 xNorm[i]=cos(angleStart);
1354 yNorm[i]=sin(angleStart);
1357 /* Add starting point to path if desired */
1360 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1361 if(!PATH_AddEntry(pPath, &point, PT_MOVETO))
1365 /* Add remaining control points */
1368 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1369 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1376 /* PATH_ScaleNormalizedPoint
1378 * Scales a normalized point (x, y) with respect to the box whose corners are
1379 * passed in "corners". The point is stored in "*pPoint". The normalized
1380 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1381 * (1.0, 1.0) correspond to corners[1].
1383 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
1384 double y, POINT *pPoint)
1386 pPoint->x=GDI_ROUND( (double)corners[0].x +
1387 (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
1388 pPoint->y=GDI_ROUND( (double)corners[0].y +
1389 (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
1392 /* PATH_NormalizePoint
1394 * Normalizes a point with respect to the box whose corners are passed in
1395 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1397 static void PATH_NormalizePoint(FLOAT_POINT corners[],
1398 const FLOAT_POINT *pPoint,
1399 double *pX, double *pY)
1401 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) *
1403 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1408 /*******************************************************************
1409 * FlattenPath [GDI32.@]
1413 BOOL WINAPI FlattenPath(HDC hdc)
1416 DC *dc = DC_GetDCPtr( hdc );
1418 if(!dc) return FALSE;
1420 if(dc->funcs->pFlattenPath) ret = dc->funcs->pFlattenPath(dc->physDev);
1423 GdiPath *pPath = &dc->path;
1424 if(pPath->state != PATH_Closed)
1425 ret = PATH_FlattenPath(pPath);
1427 GDI_ReleaseObj( hdc );
1432 static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1435 POINT ptLastMove = {0,0};
1436 POINT ptViewportOrg, ptWindowOrg;
1437 SIZE szViewportExt, szWindowExt;
1438 DWORD mapMode, graphicsMode;
1442 if(dc->funcs->pStrokePath)
1443 return dc->funcs->pStrokePath(dc->physDev);
1445 if(pPath->state != PATH_Closed)
1448 /* Save the mapping mode info */
1449 mapMode=GetMapMode(dc->hSelf);
1450 GetViewportExtEx(dc->hSelf, &szViewportExt);
1451 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
1452 GetWindowExtEx(dc->hSelf, &szWindowExt);
1453 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
1454 GetWorldTransform(dc->hSelf, &xform);
1457 SetMapMode(dc->hSelf, MM_TEXT);
1458 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
1459 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
1460 graphicsMode=GetGraphicsMode(dc->hSelf);
1461 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1462 ModifyWorldTransform(dc->hSelf, &xform, MWT_IDENTITY);
1463 SetGraphicsMode(dc->hSelf, graphicsMode);
1465 for(i = 0; i < pPath->numEntriesUsed; i++) {
1466 switch(pPath->pFlags[i]) {
1468 TRACE("Got PT_MOVETO (%ld, %ld)\n",
1469 pPath->pPoints[i].x, pPath->pPoints[i].y);
1470 MoveToEx(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y, NULL);
1471 ptLastMove = pPath->pPoints[i];
1474 case (PT_LINETO | PT_CLOSEFIGURE):
1475 TRACE("Got PT_LINETO (%ld, %ld)\n",
1476 pPath->pPoints[i].x, pPath->pPoints[i].y);
1477 LineTo(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y);
1480 TRACE("Got PT_BEZIERTO\n");
1481 if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1482 (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1483 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1487 PolyBezierTo(dc->hSelf, &pPath->pPoints[i], 3);
1491 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1495 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1496 LineTo(dc->hSelf, ptLastMove.x, ptLastMove.y);
1501 /* Restore the old mapping mode */
1502 SetMapMode(dc->hSelf, mapMode);
1503 SetViewportExtEx(dc->hSelf, szViewportExt.cx, szViewportExt.cy, NULL);
1504 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
1505 SetWindowExtEx(dc->hSelf, szWindowExt.cx, szWindowExt.cy, NULL);
1506 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
1508 /* Go to GM_ADVANCED temporarily to restore the world transform */
1509 graphicsMode=GetGraphicsMode(dc->hSelf);
1510 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1511 SetWorldTransform(dc->hSelf, &xform);
1512 SetGraphicsMode(dc->hSelf, graphicsMode);
1514 /* If we've moved the current point then get its new position
1515 which will be in device (MM_TEXT) co-ords, convert it to
1516 logical co-ords and re-set it. This basically updates
1517 dc->CurPosX|Y so that their values are in the correct mapping
1522 GetCurrentPositionEx(dc->hSelf, &pt);
1523 DPtoLP(dc->hSelf, &pt, 1);
1524 MoveToEx(dc->hSelf, pt.x, pt.y, NULL);
1530 /*******************************************************************
1531 * StrokeAndFillPath [GDI32.@]
1535 BOOL WINAPI StrokeAndFillPath(HDC hdc)
1537 DC *dc = DC_GetDCPtr( hdc );
1540 if(!dc) return FALSE;
1542 if(dc->funcs->pStrokeAndFillPath)
1543 bRet = dc->funcs->pStrokeAndFillPath(dc->physDev);
1546 bRet = PATH_FillPath(dc, &dc->path);
1547 if(bRet) bRet = PATH_StrokePath(dc, &dc->path);
1548 if(bRet) PATH_EmptyPath(&dc->path);
1550 GDI_ReleaseObj( hdc );
1555 /*******************************************************************
1556 * StrokePath [GDI32.@]
1560 BOOL WINAPI StrokePath(HDC hdc)
1562 DC *dc = DC_GetDCPtr( hdc );
1566 TRACE("(%p)\n", hdc);
1567 if(!dc) return FALSE;
1569 if(dc->funcs->pStrokePath)
1570 bRet = dc->funcs->pStrokePath(dc->physDev);
1574 bRet = PATH_StrokePath(dc, pPath);
1575 PATH_EmptyPath(pPath);
1577 GDI_ReleaseObj( hdc );
1582 /*******************************************************************
1583 * WidenPath [GDI32.@]
1587 BOOL WINAPI WidenPath(HDC hdc)
1589 DC *dc = DC_GetDCPtr( hdc );
1592 if(!dc) return FALSE;
1594 if(dc->funcs->pWidenPath)
1595 ret = dc->funcs->pWidenPath(dc->physDev);
1598 GDI_ReleaseObj( hdc );