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"
28 #if defined(HAVE_FLOAT_H)
37 #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;
122 /***********************************************************************
123 * BeginPath (GDI32.@)
125 BOOL WINAPI BeginPath(HDC hdc)
128 DC *dc = DC_GetDCPtr( hdc );
130 if(!dc) return FALSE;
132 if(dc->funcs->pBeginPath)
133 ret = dc->funcs->pBeginPath(dc->physDev);
136 /* If path is already open, do nothing */
137 if(dc->path.state != PATH_Open)
139 /* Make sure that path is empty */
140 PATH_EmptyPath(&dc->path);
142 /* Initialize variables for new path */
143 dc->path.newStroke=TRUE;
144 dc->path.state=PATH_Open;
147 GDI_ReleaseObj( hdc );
152 /***********************************************************************
155 BOOL WINAPI EndPath(HDC hdc)
158 DC *dc = DC_GetDCPtr( hdc );
160 if(!dc) return FALSE;
162 if(dc->funcs->pEndPath)
163 ret = dc->funcs->pEndPath(dc->physDev);
166 /* Check that path is currently being constructed */
167 if(dc->path.state!=PATH_Open)
169 SetLastError(ERROR_CAN_NOT_COMPLETE);
172 /* Set flag to indicate that path is finished */
173 else dc->path.state=PATH_Closed;
175 GDI_ReleaseObj( hdc );
180 /******************************************************************************
181 * AbortPath [GDI32.@]
182 * Closes and discards paths from device context
185 * Check that SetLastError is being called correctly
188 * hdc [I] Handle to device context
192 BOOL WINAPI AbortPath( HDC hdc )
195 DC *dc = DC_GetDCPtr( hdc );
197 if(!dc) return FALSE;
199 if(dc->funcs->pAbortPath)
200 ret = dc->funcs->pAbortPath(dc->physDev);
201 else /* Remove all entries from the path */
202 PATH_EmptyPath( &dc->path );
203 GDI_ReleaseObj( hdc );
208 /***********************************************************************
209 * CloseFigure (GDI32.@)
211 * FIXME: Check that SetLastError is being called correctly
213 BOOL WINAPI CloseFigure(HDC hdc)
216 DC *dc = DC_GetDCPtr( hdc );
218 if(!dc) return FALSE;
220 if(dc->funcs->pCloseFigure)
221 ret = dc->funcs->pCloseFigure(dc->physDev);
224 /* Check that path is open */
225 if(dc->path.state!=PATH_Open)
227 SetLastError(ERROR_CAN_NOT_COMPLETE);
232 /* FIXME: Shouldn't we draw a line to the beginning of the
234 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
235 if(dc->path.numEntriesUsed)
237 dc->path.pFlags[dc->path.numEntriesUsed-1]|=PT_CLOSEFIGURE;
238 dc->path.newStroke=TRUE;
242 GDI_ReleaseObj( hdc );
247 /***********************************************************************
250 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
255 DC *dc = DC_GetDCPtr( hdc );
261 /* Check that path is closed */
262 if(pPath->state!=PATH_Closed)
264 SetLastError(ERROR_CAN_NOT_COMPLETE);
269 ret = pPath->numEntriesUsed;
270 else if(nSize<pPath->numEntriesUsed)
272 SetLastError(ERROR_INVALID_PARAMETER);
277 memcpy(pPoints, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
278 memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
280 /* Convert the points to logical coordinates */
281 if(!DPtoLP(hdc, pPoints, pPath->numEntriesUsed))
283 /* FIXME: Is this the correct value? */
284 SetLastError(ERROR_CAN_NOT_COMPLETE);
287 else ret = pPath->numEntriesUsed;
290 GDI_ReleaseObj( hdc );
295 /***********************************************************************
296 * PathToRegion (GDI32.@)
299 * Check that SetLastError is being called correctly
301 * The documentation does not state this explicitly, but a test under Windows
302 * shows that the region which is returned should be in device coordinates.
304 HRGN WINAPI PathToRegion(HDC hdc)
308 DC *dc = DC_GetDCPtr( hdc );
310 /* Get pointer to path */
315 /* Check that path is closed */
316 if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
319 /* FIXME: Should we empty the path even if conversion failed? */
320 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
321 PATH_EmptyPath(pPath);
325 GDI_ReleaseObj( hdc );
329 static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
331 INT mapMode, graphicsMode;
332 SIZE ptViewportExt, ptWindowExt;
333 POINT ptViewportOrg, ptWindowOrg;
337 if(dc->funcs->pFillPath)
338 return dc->funcs->pFillPath(dc->physDev);
340 /* Check that path is closed */
341 if(pPath->state!=PATH_Closed)
343 SetLastError(ERROR_CAN_NOT_COMPLETE);
347 /* Construct a region from the path and fill it */
348 if(PATH_PathToRegion(pPath, dc->polyFillMode, &hrgn))
350 /* Since PaintRgn interprets the region as being in logical coordinates
351 * but the points we store for the path are already in device
352 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
353 * Using SaveDC to save information about the mapping mode / world
354 * transform would be easier but would require more overhead, especially
355 * now that SaveDC saves the current path.
358 /* Save the information about the old mapping mode */
359 mapMode=GetMapMode(dc->hSelf);
360 GetViewportExtEx(dc->hSelf, &ptViewportExt);
361 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
362 GetWindowExtEx(dc->hSelf, &ptWindowExt);
363 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
365 /* Save world transform
366 * NB: The Windows documentation on world transforms would lead one to
367 * believe that this has to be done only in GM_ADVANCED; however, my
368 * tests show that resetting the graphics mode to GM_COMPATIBLE does
369 * not reset the world transform.
371 GetWorldTransform(dc->hSelf, &xform);
374 SetMapMode(dc->hSelf, MM_TEXT);
375 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
376 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
378 /* Paint the region */
379 PaintRgn(dc->hSelf, hrgn);
381 /* Restore the old mapping mode */
382 SetMapMode(dc->hSelf, mapMode);
383 SetViewportExtEx(dc->hSelf, ptViewportExt.cx, ptViewportExt.cy, NULL);
384 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
385 SetWindowExtEx(dc->hSelf, ptWindowExt.cx, ptWindowExt.cy, NULL);
386 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
388 /* Go to GM_ADVANCED temporarily to restore the world transform */
389 graphicsMode=GetGraphicsMode(dc->hSelf);
390 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
391 SetWorldTransform(dc->hSelf, &xform);
392 SetGraphicsMode(dc->hSelf, graphicsMode);
399 /***********************************************************************
403 * Check that SetLastError is being called correctly
405 BOOL WINAPI FillPath(HDC hdc)
407 DC *dc = DC_GetDCPtr( hdc );
410 if(!dc) return FALSE;
412 if(dc->funcs->pFillPath)
413 bRet = dc->funcs->pFillPath(dc->physDev);
416 bRet = PATH_FillPath(dc, &dc->path);
419 /* FIXME: Should the path be emptied even if conversion
421 PATH_EmptyPath(&dc->path);
424 GDI_ReleaseObj( hdc );
429 /***********************************************************************
430 * SelectClipPath (GDI32.@)
432 * Check that SetLastError is being called correctly
434 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
438 BOOL success = FALSE;
439 DC *dc = DC_GetDCPtr( hdc );
441 if(!dc) return FALSE;
443 if(dc->funcs->pSelectClipPath)
444 success = dc->funcs->pSelectClipPath(dc->physDev, iMode);
449 /* Check that path is closed */
450 if(pPath->state!=PATH_Closed)
451 SetLastError(ERROR_CAN_NOT_COMPLETE);
452 /* Construct a region from the path */
453 else if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnPath))
455 success = ExtSelectClipRgn( hdc, hrgnPath, iMode ) != ERROR;
456 DeleteObject(hrgnPath);
460 PATH_EmptyPath(pPath);
461 /* FIXME: Should this function delete the path even if it failed? */
464 GDI_ReleaseObj( hdc );
469 /***********************************************************************
475 * Initializes the GdiPath structure.
477 void PATH_InitGdiPath(GdiPath *pPath)
481 pPath->state=PATH_Null;
484 pPath->numEntriesUsed=0;
485 pPath->numEntriesAllocated=0;
488 /* PATH_DestroyGdiPath
490 * Destroys a GdiPath structure (frees the memory in the arrays).
492 void PATH_DestroyGdiPath(GdiPath *pPath)
496 if (pPath->pPoints) HeapFree( GetProcessHeap(), 0, pPath->pPoints );
497 if (pPath->pFlags) HeapFree( GetProcessHeap(), 0, pPath->pFlags );
500 /* PATH_AssignGdiPath
502 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
503 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
504 * not just the pointers. Since this means that the arrays in pPathDest may
505 * need to be resized, pPathDest should have been initialized using
506 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
507 * not a copy constructor).
508 * Returns TRUE if successful, else FALSE.
510 BOOL PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
512 assert(pPathDest!=NULL && pPathSrc!=NULL);
514 /* Make sure destination arrays are big enough */
515 if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
518 /* Perform the copy operation */
519 memcpy(pPathDest->pPoints, pPathSrc->pPoints,
520 sizeof(POINT)*pPathSrc->numEntriesUsed);
521 memcpy(pPathDest->pFlags, pPathSrc->pFlags,
522 sizeof(BYTE)*pPathSrc->numEntriesUsed);
524 pPathDest->state=pPathSrc->state;
525 pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
526 pPathDest->newStroke=pPathSrc->newStroke;
533 * Should be called when a MoveTo is performed on a DC that has an
534 * open path. This starts a new stroke. Returns TRUE if successful, else
537 BOOL PATH_MoveTo(DC *dc)
539 GdiPath *pPath = &dc->path;
541 /* Check that path is open */
542 if(pPath->state!=PATH_Open)
543 /* FIXME: Do we have to call SetLastError? */
546 /* Start a new stroke */
547 pPath->newStroke=TRUE;
554 * Should be called when a LineTo is performed on a DC that has an
555 * open path. This adds a PT_LINETO entry to the path (and possibly
556 * a PT_MOVETO entry, if this is the first LineTo in a stroke).
557 * Returns TRUE if successful, else FALSE.
559 BOOL PATH_LineTo(DC *dc, INT x, INT y)
561 GdiPath *pPath = &dc->path;
562 POINT point, pointCurPos;
564 /* Check that path is open */
565 if(pPath->state!=PATH_Open)
568 /* Convert point to device coordinates */
571 if(!LPtoDP(dc->hSelf, &point, 1))
574 /* Add a PT_MOVETO if necessary */
577 pPath->newStroke=FALSE;
578 pointCurPos.x = dc->CursPosX;
579 pointCurPos.y = dc->CursPosY;
580 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
582 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
586 /* Add a PT_LINETO entry */
587 return PATH_AddEntry(pPath, &point, PT_LINETO);
592 * Should be called when a call to RoundRect is performed on a DC that has
593 * an open path. Returns TRUE if successful, else FALSE.
595 * FIXME: it adds the same entries to the path as windows does, but there
596 * is an error in the bezier drawing code so that there are small pixel-size
597 * gaps when the resulting path is drawn by StrokePath()
599 BOOL PATH_RoundRect(DC *dc, INT x1, INT y1, INT x2, INT y2, INT ell_width, INT ell_height)
601 GdiPath *pPath = &dc->path;
602 POINT corners[2], pointTemp;
603 FLOAT_POINT ellCorners[2];
605 /* Check that path is open */
606 if(pPath->state!=PATH_Open)
609 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
612 /* Add points to the roundrect path */
613 ellCorners[0].x = corners[1].x-ell_width;
614 ellCorners[0].y = corners[0].y;
615 ellCorners[1].x = corners[1].x;
616 ellCorners[1].y = corners[0].y+ell_height;
617 if(!PATH_DoArcPart(pPath, ellCorners, 0, -M_PI_2, TRUE))
619 pointTemp.x = corners[0].x+ell_width/2;
620 pointTemp.y = corners[0].y;
621 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
623 ellCorners[0].x = corners[0].x;
624 ellCorners[1].x = corners[0].x+ell_width;
625 if(!PATH_DoArcPart(pPath, ellCorners, -M_PI_2, -M_PI, FALSE))
627 pointTemp.x = corners[0].x;
628 pointTemp.y = corners[1].y-ell_height/2;
629 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
631 ellCorners[0].y = corners[1].y-ell_height;
632 ellCorners[1].y = corners[1].y;
633 if(!PATH_DoArcPart(pPath, ellCorners, M_PI, M_PI_2, FALSE))
635 pointTemp.x = corners[1].x-ell_width/2;
636 pointTemp.y = corners[1].y;
637 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
639 ellCorners[0].x = corners[1].x-ell_width;
640 ellCorners[1].x = corners[1].x;
641 if(!PATH_DoArcPart(pPath, ellCorners, M_PI_2, 0, FALSE))
644 /* Close the roundrect figure */
645 if(!CloseFigure(dc->hSelf))
653 * Should be called when a call to Rectangle is performed on a DC that has
654 * an open path. Returns TRUE if successful, else FALSE.
656 BOOL PATH_Rectangle(DC *dc, INT x1, INT y1, INT x2, INT y2)
658 GdiPath *pPath = &dc->path;
659 POINT corners[2], pointTemp;
661 /* Check that path is open */
662 if(pPath->state!=PATH_Open)
665 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
668 /* Close any previous figure */
669 if(!CloseFigure(dc->hSelf))
671 /* The CloseFigure call shouldn't have failed */
676 /* Add four points to the path */
677 pointTemp.x=corners[1].x;
678 pointTemp.y=corners[0].y;
679 if(!PATH_AddEntry(pPath, &pointTemp, PT_MOVETO))
681 if(!PATH_AddEntry(pPath, corners, PT_LINETO))
683 pointTemp.x=corners[0].x;
684 pointTemp.y=corners[1].y;
685 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
687 if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
690 /* Close the rectangle figure */
691 if(!CloseFigure(dc->hSelf))
693 /* The CloseFigure call shouldn't have failed */
703 * Should be called when a call to Ellipse is performed on a DC that has
704 * an open path. This adds four Bezier splines representing the ellipse
705 * to the path. Returns TRUE if successful, else FALSE.
707 BOOL PATH_Ellipse(DC *dc, INT x1, INT y1, INT x2, INT y2)
709 return( PATH_Arc(dc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2,0) &&
710 CloseFigure(dc->hSelf) );
715 * Should be called when a call to Arc is performed on a DC that has
716 * an open path. This adds up to five Bezier splines representing the arc
717 * to the path. When 'lines' is 1, we add 1 extra line to get a chord,
718 * and when 'lines' is 2, we add 2 extra lines to get a pie.
719 * Returns TRUE if successful, else FALSE.
721 BOOL PATH_Arc(DC *dc, INT x1, INT y1, INT x2, INT y2,
722 INT xStart, INT yStart, INT xEnd, INT yEnd, INT lines)
724 GdiPath *pPath = &dc->path;
725 double angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
726 /* Initialize angleEndQuadrant to silence gcc's warning */
728 FLOAT_POINT corners[2], pointStart, pointEnd;
733 /* FIXME: This function should check for all possible error returns */
734 /* FIXME: Do we have to respect newStroke? */
736 /* Check that path is open */
737 if(pPath->state!=PATH_Open)
740 /* Check for zero height / width */
741 /* FIXME: Only in GM_COMPATIBLE? */
745 /* Convert points to device coordinates */
746 corners[0].x=(FLOAT)x1;
747 corners[0].y=(FLOAT)y1;
748 corners[1].x=(FLOAT)x2;
749 corners[1].y=(FLOAT)y2;
750 pointStart.x=(FLOAT)xStart;
751 pointStart.y=(FLOAT)yStart;
752 pointEnd.x=(FLOAT)xEnd;
753 pointEnd.y=(FLOAT)yEnd;
754 INTERNAL_LPTODP_FLOAT(dc, corners);
755 INTERNAL_LPTODP_FLOAT(dc, corners+1);
756 INTERNAL_LPTODP_FLOAT(dc, &pointStart);
757 INTERNAL_LPTODP_FLOAT(dc, &pointEnd);
759 /* Make sure first corner is top left and second corner is bottom right */
760 if(corners[0].x>corners[1].x)
763 corners[0].x=corners[1].x;
766 if(corners[0].y>corners[1].y)
769 corners[0].y=corners[1].y;
773 /* Compute start and end angle */
774 PATH_NormalizePoint(corners, &pointStart, &x, &y);
775 angleStart=atan2(y, x);
776 PATH_NormalizePoint(corners, &pointEnd, &x, &y);
777 angleEnd=atan2(y, x);
779 /* Make sure the end angle is "on the right side" of the start angle */
780 if(dc->ArcDirection==AD_CLOCKWISE)
782 if(angleEnd<=angleStart)
785 assert(angleEnd>=angleStart);
790 if(angleEnd>=angleStart)
793 assert(angleEnd<=angleStart);
797 /* In GM_COMPATIBLE, don't include bottom and right edges */
798 if(dc->GraphicsMode==GM_COMPATIBLE)
804 /* Add the arc to the path with one Bezier spline per quadrant that the
810 /* Determine the start and end angles for this quadrant */
813 angleStartQuadrant=angleStart;
814 if(dc->ArcDirection==AD_CLOCKWISE)
815 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
817 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
821 angleStartQuadrant=angleEndQuadrant;
822 if(dc->ArcDirection==AD_CLOCKWISE)
823 angleEndQuadrant+=M_PI_2;
825 angleEndQuadrant-=M_PI_2;
828 /* Have we reached the last part of the arc? */
829 if((dc->ArcDirection==AD_CLOCKWISE &&
830 angleEnd<angleEndQuadrant) ||
831 (dc->ArcDirection==AD_COUNTERCLOCKWISE &&
832 angleEnd>angleEndQuadrant))
834 /* Adjust the end angle for this quadrant */
835 angleEndQuadrant=angleEnd;
839 /* Add the Bezier spline to the path */
840 PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
845 /* chord: close figure. pie: add line and close figure */
848 if(!CloseFigure(dc->hSelf))
853 centre.x = (corners[0].x+corners[1].x)/2;
854 centre.y = (corners[0].y+corners[1].y)/2;
855 if(!PATH_AddEntry(pPath, ¢re, PT_LINETO | PT_CLOSEFIGURE))
862 BOOL PATH_PolyBezierTo(DC *dc, const POINT *pts, DWORD cbPoints)
864 GdiPath *pPath = &dc->path;
868 /* Check that path is open */
869 if(pPath->state!=PATH_Open)
872 /* Add a PT_MOVETO if necessary */
875 pPath->newStroke=FALSE;
878 if(!LPtoDP(dc->hSelf, &pt, 1))
880 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
884 for(i = 0; i < cbPoints; i++) {
886 if(!LPtoDP(dc->hSelf, &pt, 1))
888 PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
893 BOOL PATH_PolyBezier(DC *dc, const POINT *pts, DWORD cbPoints)
895 GdiPath *pPath = &dc->path;
899 /* Check that path is open */
900 if(pPath->state!=PATH_Open)
903 for(i = 0; i < cbPoints; i++) {
905 if(!LPtoDP(dc->hSelf, &pt, 1))
907 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
912 BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
914 GdiPath *pPath = &dc->path;
918 /* Check that path is open */
919 if(pPath->state!=PATH_Open)
922 for(i = 0; i < cbPoints; i++) {
924 if(!LPtoDP(dc->hSelf, &pt, 1))
926 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
931 BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
933 GdiPath *pPath = &dc->path;
937 /* Check that path is open */
938 if(pPath->state!=PATH_Open)
941 /* Add a PT_MOVETO if necessary */
944 pPath->newStroke=FALSE;
947 if(!LPtoDP(dc->hSelf, &pt, 1))
949 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
953 for(i = 0; i < cbPoints; i++) {
955 if(!LPtoDP(dc->hSelf, &pt, 1))
957 PATH_AddEntry(pPath, &pt, PT_LINETO);
964 BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
966 GdiPath *pPath = &dc->path;
970 /* Check that path is open */
971 if(pPath->state!=PATH_Open)
974 for(i = 0; i < cbPoints; i++) {
976 if(!LPtoDP(dc->hSelf, &pt, 1))
978 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
979 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
985 BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
988 GdiPath *pPath = &dc->path;
992 /* Check that path is open */
993 if(pPath->state!=PATH_Open)
996 for(i = 0, poly = 0; poly < polygons; poly++) {
997 for(point = 0; point < counts[poly]; point++, i++) {
999 if(!LPtoDP(dc->hSelf, &pt, 1))
1001 if(point == 0) startpt = pt;
1002 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1004 /* win98 adds an extra line to close the figure for some reason */
1005 PATH_AddEntry(pPath, &startpt, PT_LINETO | PT_CLOSEFIGURE);
1010 BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1013 GdiPath *pPath = &dc->path;
1017 /* Check that path is open */
1018 if(pPath->state!=PATH_Open)
1021 for(i = 0, poly = 0; poly < polylines; poly++) {
1022 for(point = 0; point < counts[poly]; point++, i++) {
1024 if(!LPtoDP(dc->hSelf, &pt, 1))
1026 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1032 /***********************************************************************
1033 * Internal functions
1036 /* PATH_CheckCorners
1038 * Helper function for PATH_RoundRect() and PATH_Rectangle()
1040 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2)
1044 /* Convert points to device coordinates */
1049 if(!LPtoDP(dc->hSelf, corners, 2))
1052 /* Make sure first corner is top left and second corner is bottom right */
1053 if(corners[0].x>corners[1].x)
1056 corners[0].x=corners[1].x;
1059 if(corners[0].y>corners[1].y)
1062 corners[0].y=corners[1].y;
1066 /* In GM_COMPATIBLE, don't include bottom and right edges */
1067 if(dc->GraphicsMode==GM_COMPATIBLE)
1076 /* PATH_AddFlatBezier
1078 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
1083 pts = GDI_Bezier( pt, 4, &no );
1084 if(!pts) return FALSE;
1086 for(i = 1; i < no; i++)
1087 PATH_AddEntry(pPath, &pts[i],
1088 (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
1089 HeapFree( GetProcessHeap(), 0, pts );
1095 * Replaces Beziers with line segments
1098 static BOOL PATH_FlattenPath(GdiPath *pPath)
1103 memset(&newPath, 0, sizeof(newPath));
1104 newPath.state = PATH_Open;
1105 for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
1106 switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
1109 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
1110 pPath->pFlags[srcpt]);
1113 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
1114 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
1119 newPath.state = PATH_Closed;
1120 PATH_AssignGdiPath(pPath, &newPath);
1121 PATH_EmptyPath(&newPath);
1125 /* PATH_PathToRegion
1127 * Creates a region from the specified path using the specified polygon
1128 * filling mode. The path is left unchanged. A handle to the region that
1129 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
1130 * error occurs, SetLastError is called with the appropriate value and
1131 * FALSE is returned.
1133 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
1136 int numStrokes, iStroke, i;
1137 INT *pNumPointsInStroke;
1140 assert(pPath!=NULL);
1141 assert(pHrgn!=NULL);
1143 PATH_FlattenPath(pPath);
1145 /* FIXME: What happens when number of points is zero? */
1147 /* First pass: Find out how many strokes there are in the path */
1148 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
1150 for(i=0; i<pPath->numEntriesUsed; i++)
1151 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1154 /* Allocate memory for number-of-points-in-stroke array */
1155 pNumPointsInStroke=(int *)HeapAlloc( GetProcessHeap(), 0,
1156 sizeof(int) * numStrokes );
1157 if(!pNumPointsInStroke)
1159 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1163 /* Second pass: remember number of points in each polygon */
1164 iStroke=-1; /* Will get incremented to 0 at beginning of first stroke */
1165 for(i=0; i<pPath->numEntriesUsed; i++)
1167 /* Is this the beginning of a new stroke? */
1168 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1171 pNumPointsInStroke[iStroke]=0;
1174 pNumPointsInStroke[iStroke]++;
1177 /* Create a region from the strokes */
1178 hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
1179 numStrokes, nPolyFillMode);
1181 /* Free memory for number-of-points-in-stroke array */
1182 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
1186 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1197 * Removes all entries from the path and sets the path state to PATH_Null.
1199 static void PATH_EmptyPath(GdiPath *pPath)
1201 assert(pPath!=NULL);
1203 pPath->state=PATH_Null;
1204 pPath->numEntriesUsed=0;
1209 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1210 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1211 * successful, FALSE otherwise (e.g. if not enough memory was available).
1213 BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
1215 assert(pPath!=NULL);
1217 /* FIXME: If newStroke is true, perhaps we want to check that we're
1218 * getting a PT_MOVETO
1220 TRACE("(%ld,%ld) - %d\n", pPoint->x, pPoint->y, flags);
1222 /* Check that path is open */
1223 if(pPath->state!=PATH_Open)
1226 /* Reserve enough memory for an extra path entry */
1227 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
1230 /* Store information in path entry */
1231 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
1232 pPath->pFlags[pPath->numEntriesUsed]=flags;
1234 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1235 if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
1236 pPath->newStroke=TRUE;
1238 /* Increment entry count */
1239 pPath->numEntriesUsed++;
1244 /* PATH_ReserveEntries
1246 * Ensures that at least "numEntries" entries (for points and flags) have
1247 * been allocated; allocates larger arrays and copies the existing entries
1248 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1250 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries)
1252 INT numEntriesToAllocate;
1256 assert(pPath!=NULL);
1257 assert(numEntries>=0);
1259 /* Do we have to allocate more memory? */
1260 if(numEntries > pPath->numEntriesAllocated)
1262 /* Find number of entries to allocate. We let the size of the array
1263 * grow exponentially, since that will guarantee linear time
1265 if(pPath->numEntriesAllocated)
1267 numEntriesToAllocate=pPath->numEntriesAllocated;
1268 while(numEntriesToAllocate<numEntries)
1269 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
1273 numEntriesToAllocate=numEntries;
1275 /* Allocate new arrays */
1276 pPointsNew=(POINT *)HeapAlloc( GetProcessHeap(), 0,
1277 numEntriesToAllocate * sizeof(POINT) );
1280 pFlagsNew=(BYTE *)HeapAlloc( GetProcessHeap(), 0,
1281 numEntriesToAllocate * sizeof(BYTE) );
1284 HeapFree( GetProcessHeap(), 0, pPointsNew );
1288 /* Copy old arrays to new arrays and discard old arrays */
1291 assert(pPath->pFlags);
1293 memcpy(pPointsNew, pPath->pPoints,
1294 sizeof(POINT)*pPath->numEntriesUsed);
1295 memcpy(pFlagsNew, pPath->pFlags,
1296 sizeof(BYTE)*pPath->numEntriesUsed);
1298 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
1299 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
1301 pPath->pPoints=pPointsNew;
1302 pPath->pFlags=pFlagsNew;
1303 pPath->numEntriesAllocated=numEntriesToAllocate;
1311 * Creates a Bezier spline that corresponds to part of an arc and appends the
1312 * corresponding points to the path. The start and end angles are passed in
1313 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1314 * at most. If "addMoveTo" is true, a PT_MOVETO entry for the first control
1315 * point is added to the path; otherwise, it is assumed that the current
1316 * position is equal to the first control point.
1318 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
1319 double angleStart, double angleEnd, BOOL addMoveTo)
1321 double halfAngle, a;
1322 double xNorm[4], yNorm[4];
1326 assert(fabs(angleEnd-angleStart)<=M_PI_2);
1328 /* FIXME: Is there an easier way of computing this? */
1330 /* Compute control points */
1331 halfAngle=(angleEnd-angleStart)/2.0;
1332 if(fabs(halfAngle)>1e-8)
1334 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
1335 xNorm[0]=cos(angleStart);
1336 yNorm[0]=sin(angleStart);
1337 xNorm[1]=xNorm[0] - a*yNorm[0];
1338 yNorm[1]=yNorm[0] + a*xNorm[0];
1339 xNorm[3]=cos(angleEnd);
1340 yNorm[3]=sin(angleEnd);
1341 xNorm[2]=xNorm[3] + a*yNorm[3];
1342 yNorm[2]=yNorm[3] - a*xNorm[3];
1347 xNorm[i]=cos(angleStart);
1348 yNorm[i]=sin(angleStart);
1351 /* Add starting point to path if desired */
1354 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1355 if(!PATH_AddEntry(pPath, &point, PT_MOVETO))
1359 /* Add remaining control points */
1362 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1363 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1370 /* PATH_ScaleNormalizedPoint
1372 * Scales a normalized point (x, y) with respect to the box whose corners are
1373 * passed in "corners". The point is stored in "*pPoint". The normalized
1374 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1375 * (1.0, 1.0) correspond to corners[1].
1377 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
1378 double y, POINT *pPoint)
1380 pPoint->x=GDI_ROUND( (double)corners[0].x +
1381 (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
1382 pPoint->y=GDI_ROUND( (double)corners[0].y +
1383 (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
1386 /* PATH_NormalizePoint
1388 * Normalizes a point with respect to the box whose corners are passed in
1389 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1391 static void PATH_NormalizePoint(FLOAT_POINT corners[],
1392 const FLOAT_POINT *pPoint,
1393 double *pX, double *pY)
1395 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) *
1397 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1402 /*******************************************************************
1403 * FlattenPath [GDI32.@]
1407 BOOL WINAPI FlattenPath(HDC hdc)
1410 DC *dc = DC_GetDCPtr( hdc );
1412 if(!dc) return FALSE;
1414 if(dc->funcs->pFlattenPath) ret = dc->funcs->pFlattenPath(dc->physDev);
1417 GdiPath *pPath = &dc->path;
1418 if(pPath->state != PATH_Closed)
1419 ret = PATH_FlattenPath(pPath);
1421 GDI_ReleaseObj( hdc );
1426 static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1429 POINT ptLastMove = {0,0};
1430 POINT ptViewportOrg, ptWindowOrg;
1431 SIZE szViewportExt, szWindowExt;
1432 DWORD mapMode, graphicsMode;
1436 if(dc->funcs->pStrokePath)
1437 return dc->funcs->pStrokePath(dc->physDev);
1439 if(pPath->state != PATH_Closed)
1442 /* Save the mapping mode info */
1443 mapMode=GetMapMode(dc->hSelf);
1444 GetViewportExtEx(dc->hSelf, &szViewportExt);
1445 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
1446 GetWindowExtEx(dc->hSelf, &szWindowExt);
1447 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
1448 GetWorldTransform(dc->hSelf, &xform);
1451 SetMapMode(dc->hSelf, MM_TEXT);
1452 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
1453 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
1456 for(i = 0; i < pPath->numEntriesUsed; i++) {
1457 switch(pPath->pFlags[i]) {
1459 TRACE("Got PT_MOVETO (%ld, %ld)\n",
1460 pPath->pPoints[i].x, pPath->pPoints[i].y);
1461 MoveToEx(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y, NULL);
1462 ptLastMove = pPath->pPoints[i];
1465 case (PT_LINETO | PT_CLOSEFIGURE):
1466 TRACE("Got PT_LINETO (%ld, %ld)\n",
1467 pPath->pPoints[i].x, pPath->pPoints[i].y);
1468 LineTo(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y);
1471 TRACE("Got PT_BEZIERTO\n");
1472 if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1473 (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1474 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1478 PolyBezierTo(dc->hSelf, &pPath->pPoints[i], 3);
1482 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1486 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1487 LineTo(dc->hSelf, ptLastMove.x, ptLastMove.y);
1492 /* Restore the old mapping mode */
1493 SetMapMode(dc->hSelf, mapMode);
1494 SetViewportExtEx(dc->hSelf, szViewportExt.cx, szViewportExt.cy, NULL);
1495 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
1496 SetWindowExtEx(dc->hSelf, szWindowExt.cx, szWindowExt.cy, NULL);
1497 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
1499 /* Go to GM_ADVANCED temporarily to restore the world transform */
1500 graphicsMode=GetGraphicsMode(dc->hSelf);
1501 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1502 SetWorldTransform(dc->hSelf, &xform);
1503 SetGraphicsMode(dc->hSelf, graphicsMode);
1505 /* If we've moved the current point then get its new position
1506 which will be in device (MM_TEXT) co-ords, convert it to
1507 logical co-ords and re-set it. This basically updates
1508 dc->CurPosX|Y so that their values are in the correct mapping
1513 GetCurrentPositionEx(dc->hSelf, &pt);
1514 DPtoLP(dc->hSelf, &pt, 1);
1515 MoveToEx(dc->hSelf, pt.x, pt.y, NULL);
1521 /*******************************************************************
1522 * StrokeAndFillPath [GDI32.@]
1526 BOOL WINAPI StrokeAndFillPath(HDC hdc)
1528 DC *dc = DC_GetDCPtr( hdc );
1531 if(!dc) return FALSE;
1533 if(dc->funcs->pStrokeAndFillPath)
1534 bRet = dc->funcs->pStrokeAndFillPath(dc->physDev);
1537 bRet = PATH_FillPath(dc, &dc->path);
1538 if(bRet) bRet = PATH_StrokePath(dc, &dc->path);
1539 if(bRet) PATH_EmptyPath(&dc->path);
1541 GDI_ReleaseObj( hdc );
1546 /*******************************************************************
1547 * StrokePath [GDI32.@]
1551 BOOL WINAPI StrokePath(HDC hdc)
1553 DC *dc = DC_GetDCPtr( hdc );
1557 TRACE("(%p)\n", hdc);
1558 if(!dc) return FALSE;
1560 if(dc->funcs->pStrokePath)
1561 bRet = dc->funcs->pStrokePath(dc->physDev);
1565 bRet = PATH_StrokePath(dc, pPath);
1566 PATH_EmptyPath(pPath);
1568 GDI_ReleaseObj( hdc );
1573 /*******************************************************************
1574 * WidenPath [GDI32.@]
1578 BOOL WINAPI WidenPath(HDC hdc)
1580 DC *dc = DC_GetDCPtr( hdc );
1583 if(!dc) return FALSE;
1585 if(dc->funcs->pWidenPath)
1586 ret = dc->funcs->pWidenPath(dc->physDev);
1589 GDI_ReleaseObj( hdc );