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 "gdi_private.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
44 /* Notes on the implementation
46 * The implementation is based on dynamically resizable arrays of points and
47 * flags. I dithered for a bit before deciding on this implementation, and
48 * I had even done a bit of work on a linked list version before switching
49 * to arrays. It's a bit of a tradeoff. When you use linked lists, the
50 * implementation of FlattenPath is easier, because you can rip the
51 * PT_BEZIERTO entries out of the middle of the list and link the
52 * corresponding PT_LINETO entries in. However, when you use arrays,
53 * PathToRegion becomes easier, since you can essentially just pass your array
54 * of points to CreatePolyPolygonRgn. Also, if I'd used linked lists, I would
55 * have had the extra effort of creating a chunk-based allocation scheme
56 * in order to use memory effectively. That's why I finally decided to use
57 * arrays. Note by the way that the array based implementation has the same
58 * linear time complexity that linked lists would have since the arrays grow
61 * The points are stored in the path in device coordinates. This is
62 * consistent with the way Windows does things (for instance, see the Win32
63 * SDK documentation for GetPath).
65 * The word "stroke" appears in several places (e.g. in the flag
66 * GdiPath.newStroke). A stroke consists of a PT_MOVETO followed by one or
67 * more PT_LINETOs or PT_BEZIERTOs, up to, but not including, the next
68 * PT_MOVETO. Note that this is not the same as the definition of a figure;
69 * a figure can contain several strokes.
71 * I modified the drawing functions (MoveTo, LineTo etc.) to test whether
72 * the path is open and to call the corresponding function in path.c if this
73 * is the case. A more elegant approach would be to modify the function
74 * pointers in the DC_FUNCTIONS structure; however, this would be a lot more
75 * complex. Also, the performance degradation caused by my approach in the
76 * case where no path is open is so small that it cannot be measured.
81 /* FIXME: A lot of stuff isn't implemented yet. There is much more to come. */
83 #define NUM_ENTRIES_INITIAL 16 /* Initial size of points / flags arrays */
84 #define GROW_FACTOR_NUMER 2 /* Numerator of grow factor for the array */
85 #define GROW_FACTOR_DENOM 1 /* Denominator of grow factor */
87 /* A floating point version of the POINT structure */
88 typedef struct tagFLOAT_POINT
94 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
96 static void PATH_EmptyPath(GdiPath *pPath);
97 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries);
98 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
99 double angleStart, double angleEnd, BOOL addMoveTo);
100 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
101 double y, POINT *pPoint);
102 static void PATH_NormalizePoint(FLOAT_POINT corners[], const FLOAT_POINT
103 *pPoint, double *pX, double *pY);
104 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2);
106 /* Performs a world-to-viewport transformation on the specified point (which
107 * is in floating point format).
109 static inline void INTERNAL_LPTODP_FLOAT(DC *dc, FLOAT_POINT *point)
113 /* Perform the transformation */
116 point->x = x * dc->xformWorld2Vport.eM11 +
117 y * dc->xformWorld2Vport.eM21 +
118 dc->xformWorld2Vport.eDx;
119 point->y = x * dc->xformWorld2Vport.eM12 +
120 y * dc->xformWorld2Vport.eM22 +
121 dc->xformWorld2Vport.eDy;
125 /***********************************************************************
126 * BeginPath (GDI32.@)
128 BOOL WINAPI BeginPath(HDC hdc)
131 DC *dc = DC_GetDCPtr( hdc );
133 if(!dc) return FALSE;
135 if(dc->funcs->pBeginPath)
136 ret = dc->funcs->pBeginPath(dc->physDev);
139 /* If path is already open, do nothing */
140 if(dc->path.state != PATH_Open)
142 /* Make sure that path is empty */
143 PATH_EmptyPath(&dc->path);
145 /* Initialize variables for new path */
146 dc->path.newStroke=TRUE;
147 dc->path.state=PATH_Open;
150 GDI_ReleaseObj( hdc );
155 /***********************************************************************
158 BOOL WINAPI EndPath(HDC hdc)
161 DC *dc = DC_GetDCPtr( hdc );
163 if(!dc) return FALSE;
165 if(dc->funcs->pEndPath)
166 ret = dc->funcs->pEndPath(dc->physDev);
169 /* Check that path is currently being constructed */
170 if(dc->path.state!=PATH_Open)
172 SetLastError(ERROR_CAN_NOT_COMPLETE);
175 /* Set flag to indicate that path is finished */
176 else dc->path.state=PATH_Closed;
178 GDI_ReleaseObj( hdc );
183 /******************************************************************************
184 * AbortPath [GDI32.@]
185 * Closes and discards paths from device context
188 * Check that SetLastError is being called correctly
191 * hdc [I] Handle to device context
195 BOOL WINAPI AbortPath( HDC hdc )
198 DC *dc = DC_GetDCPtr( hdc );
200 if(!dc) return FALSE;
202 if(dc->funcs->pAbortPath)
203 ret = dc->funcs->pAbortPath(dc->physDev);
204 else /* Remove all entries from the path */
205 PATH_EmptyPath( &dc->path );
206 GDI_ReleaseObj( hdc );
211 /***********************************************************************
212 * CloseFigure (GDI32.@)
214 * FIXME: Check that SetLastError is being called correctly
216 BOOL WINAPI CloseFigure(HDC hdc)
219 DC *dc = DC_GetDCPtr( hdc );
221 if(!dc) return FALSE;
223 if(dc->funcs->pCloseFigure)
224 ret = dc->funcs->pCloseFigure(dc->physDev);
227 /* Check that path is open */
228 if(dc->path.state!=PATH_Open)
230 SetLastError(ERROR_CAN_NOT_COMPLETE);
235 /* FIXME: Shouldn't we draw a line to the beginning of the
237 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
238 if(dc->path.numEntriesUsed)
240 dc->path.pFlags[dc->path.numEntriesUsed-1]|=PT_CLOSEFIGURE;
241 dc->path.newStroke=TRUE;
245 GDI_ReleaseObj( hdc );
250 /***********************************************************************
253 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
258 DC *dc = DC_GetDCPtr( hdc );
264 /* Check that path is closed */
265 if(pPath->state!=PATH_Closed)
267 SetLastError(ERROR_CAN_NOT_COMPLETE);
272 ret = pPath->numEntriesUsed;
273 else if(nSize<pPath->numEntriesUsed)
275 SetLastError(ERROR_INVALID_PARAMETER);
280 memcpy(pPoints, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
281 memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
283 /* Convert the points to logical coordinates */
284 if(!DPtoLP(hdc, pPoints, pPath->numEntriesUsed))
286 /* FIXME: Is this the correct value? */
287 SetLastError(ERROR_CAN_NOT_COMPLETE);
290 else ret = pPath->numEntriesUsed;
293 GDI_ReleaseObj( hdc );
298 /***********************************************************************
299 * PathToRegion (GDI32.@)
302 * Check that SetLastError is being called correctly
304 * The documentation does not state this explicitly, but a test under Windows
305 * shows that the region which is returned should be in device coordinates.
307 HRGN WINAPI PathToRegion(HDC hdc)
311 DC *dc = DC_GetDCPtr( hdc );
313 /* Get pointer to path */
318 /* Check that path is closed */
319 if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
322 /* FIXME: Should we empty the path even if conversion failed? */
323 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
324 PATH_EmptyPath(pPath);
328 GDI_ReleaseObj( hdc );
332 static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
334 INT mapMode, graphicsMode;
335 SIZE ptViewportExt, ptWindowExt;
336 POINT ptViewportOrg, ptWindowOrg;
340 if(dc->funcs->pFillPath)
341 return dc->funcs->pFillPath(dc->physDev);
343 /* Check that path is closed */
344 if(pPath->state!=PATH_Closed)
346 SetLastError(ERROR_CAN_NOT_COMPLETE);
350 /* Construct a region from the path and fill it */
351 if(PATH_PathToRegion(pPath, dc->polyFillMode, &hrgn))
353 /* Since PaintRgn interprets the region as being in logical coordinates
354 * but the points we store for the path are already in device
355 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
356 * Using SaveDC to save information about the mapping mode / world
357 * transform would be easier but would require more overhead, especially
358 * now that SaveDC saves the current path.
361 /* Save the information about the old mapping mode */
362 mapMode=GetMapMode(dc->hSelf);
363 GetViewportExtEx(dc->hSelf, &ptViewportExt);
364 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
365 GetWindowExtEx(dc->hSelf, &ptWindowExt);
366 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
368 /* Save world transform
369 * NB: The Windows documentation on world transforms would lead one to
370 * believe that this has to be done only in GM_ADVANCED; however, my
371 * tests show that resetting the graphics mode to GM_COMPATIBLE does
372 * not reset the world transform.
374 GetWorldTransform(dc->hSelf, &xform);
377 SetMapMode(dc->hSelf, MM_TEXT);
378 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
379 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
380 graphicsMode=GetGraphicsMode(dc->hSelf);
381 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
382 ModifyWorldTransform(dc->hSelf, &xform, MWT_IDENTITY);
383 SetGraphicsMode(dc->hSelf, graphicsMode);
385 /* Paint the region */
386 PaintRgn(dc->hSelf, hrgn);
388 /* Restore the old mapping mode */
389 SetMapMode(dc->hSelf, mapMode);
390 SetViewportExtEx(dc->hSelf, ptViewportExt.cx, ptViewportExt.cy, NULL);
391 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
392 SetWindowExtEx(dc->hSelf, ptWindowExt.cx, ptWindowExt.cy, NULL);
393 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
395 /* Go to GM_ADVANCED temporarily to restore the world transform */
396 graphicsMode=GetGraphicsMode(dc->hSelf);
397 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
398 SetWorldTransform(dc->hSelf, &xform);
399 SetGraphicsMode(dc->hSelf, graphicsMode);
406 /***********************************************************************
410 * Check that SetLastError is being called correctly
412 BOOL WINAPI FillPath(HDC hdc)
414 DC *dc = DC_GetDCPtr( hdc );
417 if(!dc) return FALSE;
419 if(dc->funcs->pFillPath)
420 bRet = dc->funcs->pFillPath(dc->physDev);
423 bRet = PATH_FillPath(dc, &dc->path);
426 /* FIXME: Should the path be emptied even if conversion
428 PATH_EmptyPath(&dc->path);
431 GDI_ReleaseObj( hdc );
436 /***********************************************************************
437 * SelectClipPath (GDI32.@)
439 * Check that SetLastError is being called correctly
441 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
445 BOOL success = FALSE;
446 DC *dc = DC_GetDCPtr( hdc );
448 if(!dc) return FALSE;
450 if(dc->funcs->pSelectClipPath)
451 success = dc->funcs->pSelectClipPath(dc->physDev, iMode);
456 /* Check that path is closed */
457 if(pPath->state!=PATH_Closed)
458 SetLastError(ERROR_CAN_NOT_COMPLETE);
459 /* Construct a region from the path */
460 else if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnPath))
462 success = ExtSelectClipRgn( hdc, hrgnPath, iMode ) != ERROR;
463 DeleteObject(hrgnPath);
467 PATH_EmptyPath(pPath);
468 /* FIXME: Should this function delete the path even if it failed? */
471 GDI_ReleaseObj( hdc );
476 /***********************************************************************
482 * Initializes the GdiPath structure.
484 void PATH_InitGdiPath(GdiPath *pPath)
488 pPath->state=PATH_Null;
491 pPath->numEntriesUsed=0;
492 pPath->numEntriesAllocated=0;
495 /* PATH_DestroyGdiPath
497 * Destroys a GdiPath structure (frees the memory in the arrays).
499 void PATH_DestroyGdiPath(GdiPath *pPath)
503 if (pPath->pPoints) HeapFree( GetProcessHeap(), 0, pPath->pPoints );
504 if (pPath->pFlags) HeapFree( GetProcessHeap(), 0, pPath->pFlags );
507 /* PATH_AssignGdiPath
509 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
510 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
511 * not just the pointers. Since this means that the arrays in pPathDest may
512 * need to be resized, pPathDest should have been initialized using
513 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
514 * not a copy constructor).
515 * Returns TRUE if successful, else FALSE.
517 BOOL PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
519 assert(pPathDest!=NULL && pPathSrc!=NULL);
521 /* Make sure destination arrays are big enough */
522 if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
525 /* Perform the copy operation */
526 memcpy(pPathDest->pPoints, pPathSrc->pPoints,
527 sizeof(POINT)*pPathSrc->numEntriesUsed);
528 memcpy(pPathDest->pFlags, pPathSrc->pFlags,
529 sizeof(BYTE)*pPathSrc->numEntriesUsed);
531 pPathDest->state=pPathSrc->state;
532 pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
533 pPathDest->newStroke=pPathSrc->newStroke;
540 * Should be called when a MoveTo is performed on a DC that has an
541 * open path. This starts a new stroke. Returns TRUE if successful, else
544 BOOL PATH_MoveTo(DC *dc)
546 GdiPath *pPath = &dc->path;
548 /* Check that path is open */
549 if(pPath->state!=PATH_Open)
550 /* FIXME: Do we have to call SetLastError? */
553 /* Start a new stroke */
554 pPath->newStroke=TRUE;
561 * Should be called when a LineTo is performed on a DC that has an
562 * open path. This adds a PT_LINETO entry to the path (and possibly
563 * a PT_MOVETO entry, if this is the first LineTo in a stroke).
564 * Returns TRUE if successful, else FALSE.
566 BOOL PATH_LineTo(DC *dc, INT x, INT y)
568 GdiPath *pPath = &dc->path;
569 POINT point, pointCurPos;
571 /* Check that path is open */
572 if(pPath->state!=PATH_Open)
575 /* Convert point to device coordinates */
578 if(!LPtoDP(dc->hSelf, &point, 1))
581 /* Add a PT_MOVETO if necessary */
584 pPath->newStroke=FALSE;
585 pointCurPos.x = dc->CursPosX;
586 pointCurPos.y = dc->CursPosY;
587 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
589 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
593 /* Add a PT_LINETO entry */
594 return PATH_AddEntry(pPath, &point, PT_LINETO);
599 * Should be called when a call to RoundRect is performed on a DC that has
600 * an open path. Returns TRUE if successful, else FALSE.
602 * FIXME: it adds the same entries to the path as windows does, but there
603 * is an error in the bezier drawing code so that there are small pixel-size
604 * gaps when the resulting path is drawn by StrokePath()
606 BOOL PATH_RoundRect(DC *dc, INT x1, INT y1, INT x2, INT y2, INT ell_width, INT ell_height)
608 GdiPath *pPath = &dc->path;
609 POINT corners[2], pointTemp;
610 FLOAT_POINT ellCorners[2];
612 /* Check that path is open */
613 if(pPath->state!=PATH_Open)
616 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
619 /* Add points to the roundrect path */
620 ellCorners[0].x = corners[1].x-ell_width;
621 ellCorners[0].y = corners[0].y;
622 ellCorners[1].x = corners[1].x;
623 ellCorners[1].y = corners[0].y+ell_height;
624 if(!PATH_DoArcPart(pPath, ellCorners, 0, -M_PI_2, TRUE))
626 pointTemp.x = corners[0].x+ell_width/2;
627 pointTemp.y = corners[0].y;
628 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
630 ellCorners[0].x = corners[0].x;
631 ellCorners[1].x = corners[0].x+ell_width;
632 if(!PATH_DoArcPart(pPath, ellCorners, -M_PI_2, -M_PI, FALSE))
634 pointTemp.x = corners[0].x;
635 pointTemp.y = corners[1].y-ell_height/2;
636 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
638 ellCorners[0].y = corners[1].y-ell_height;
639 ellCorners[1].y = corners[1].y;
640 if(!PATH_DoArcPart(pPath, ellCorners, M_PI, M_PI_2, FALSE))
642 pointTemp.x = corners[1].x-ell_width/2;
643 pointTemp.y = corners[1].y;
644 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
646 ellCorners[0].x = corners[1].x-ell_width;
647 ellCorners[1].x = corners[1].x;
648 if(!PATH_DoArcPart(pPath, ellCorners, M_PI_2, 0, FALSE))
651 /* Close the roundrect figure */
652 if(!CloseFigure(dc->hSelf))
660 * Should be called when a call to Rectangle is performed on a DC that has
661 * an open path. Returns TRUE if successful, else FALSE.
663 BOOL PATH_Rectangle(DC *dc, INT x1, INT y1, INT x2, INT y2)
665 GdiPath *pPath = &dc->path;
666 POINT corners[2], pointTemp;
668 /* Check that path is open */
669 if(pPath->state!=PATH_Open)
672 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
675 /* Close any previous figure */
676 if(!CloseFigure(dc->hSelf))
678 /* The CloseFigure call shouldn't have failed */
683 /* Add four points to the path */
684 pointTemp.x=corners[1].x;
685 pointTemp.y=corners[0].y;
686 if(!PATH_AddEntry(pPath, &pointTemp, PT_MOVETO))
688 if(!PATH_AddEntry(pPath, corners, PT_LINETO))
690 pointTemp.x=corners[0].x;
691 pointTemp.y=corners[1].y;
692 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
694 if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
697 /* Close the rectangle figure */
698 if(!CloseFigure(dc->hSelf))
700 /* The CloseFigure call shouldn't have failed */
710 * Should be called when a call to Ellipse is performed on a DC that has
711 * an open path. This adds four Bezier splines representing the ellipse
712 * to the path. Returns TRUE if successful, else FALSE.
714 BOOL PATH_Ellipse(DC *dc, INT x1, INT y1, INT x2, INT y2)
716 return( PATH_Arc(dc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2,0) &&
717 CloseFigure(dc->hSelf) );
722 * Should be called when a call to Arc is performed on a DC that has
723 * an open path. This adds up to five Bezier splines representing the arc
724 * to the path. When 'lines' is 1, we add 1 extra line to get a chord,
725 * and when 'lines' is 2, we add 2 extra lines to get a pie.
726 * Returns TRUE if successful, else FALSE.
728 BOOL PATH_Arc(DC *dc, INT x1, INT y1, INT x2, INT y2,
729 INT xStart, INT yStart, INT xEnd, INT yEnd, INT lines)
731 GdiPath *pPath = &dc->path;
732 double angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
733 /* Initialize angleEndQuadrant to silence gcc's warning */
735 FLOAT_POINT corners[2], pointStart, pointEnd;
740 /* FIXME: This function should check for all possible error returns */
741 /* FIXME: Do we have to respect newStroke? */
743 /* Check that path is open */
744 if(pPath->state!=PATH_Open)
747 /* Check for zero height / width */
748 /* FIXME: Only in GM_COMPATIBLE? */
752 /* Convert points to device coordinates */
753 corners[0].x=(FLOAT)x1;
754 corners[0].y=(FLOAT)y1;
755 corners[1].x=(FLOAT)x2;
756 corners[1].y=(FLOAT)y2;
757 pointStart.x=(FLOAT)xStart;
758 pointStart.y=(FLOAT)yStart;
759 pointEnd.x=(FLOAT)xEnd;
760 pointEnd.y=(FLOAT)yEnd;
761 INTERNAL_LPTODP_FLOAT(dc, corners);
762 INTERNAL_LPTODP_FLOAT(dc, corners+1);
763 INTERNAL_LPTODP_FLOAT(dc, &pointStart);
764 INTERNAL_LPTODP_FLOAT(dc, &pointEnd);
766 /* Make sure first corner is top left and second corner is bottom right */
767 if(corners[0].x>corners[1].x)
770 corners[0].x=corners[1].x;
773 if(corners[0].y>corners[1].y)
776 corners[0].y=corners[1].y;
780 /* Compute start and end angle */
781 PATH_NormalizePoint(corners, &pointStart, &x, &y);
782 angleStart=atan2(y, x);
783 PATH_NormalizePoint(corners, &pointEnd, &x, &y);
784 angleEnd=atan2(y, x);
786 /* Make sure the end angle is "on the right side" of the start angle */
787 if(dc->ArcDirection==AD_CLOCKWISE)
789 if(angleEnd<=angleStart)
792 assert(angleEnd>=angleStart);
797 if(angleEnd>=angleStart)
800 assert(angleEnd<=angleStart);
804 /* In GM_COMPATIBLE, don't include bottom and right edges */
805 if(dc->GraphicsMode==GM_COMPATIBLE)
811 /* Add the arc to the path with one Bezier spline per quadrant that the
817 /* Determine the start and end angles for this quadrant */
820 angleStartQuadrant=angleStart;
821 if(dc->ArcDirection==AD_CLOCKWISE)
822 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
824 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
828 angleStartQuadrant=angleEndQuadrant;
829 if(dc->ArcDirection==AD_CLOCKWISE)
830 angleEndQuadrant+=M_PI_2;
832 angleEndQuadrant-=M_PI_2;
835 /* Have we reached the last part of the arc? */
836 if((dc->ArcDirection==AD_CLOCKWISE &&
837 angleEnd<angleEndQuadrant) ||
838 (dc->ArcDirection==AD_COUNTERCLOCKWISE &&
839 angleEnd>angleEndQuadrant))
841 /* Adjust the end angle for this quadrant */
842 angleEndQuadrant=angleEnd;
846 /* Add the Bezier spline to the path */
847 PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
852 /* chord: close figure. pie: add line and close figure */
855 if(!CloseFigure(dc->hSelf))
860 centre.x = (corners[0].x+corners[1].x)/2;
861 centre.y = (corners[0].y+corners[1].y)/2;
862 if(!PATH_AddEntry(pPath, ¢re, PT_LINETO | PT_CLOSEFIGURE))
869 BOOL PATH_PolyBezierTo(DC *dc, const POINT *pts, DWORD cbPoints)
871 GdiPath *pPath = &dc->path;
875 /* Check that path is open */
876 if(pPath->state!=PATH_Open)
879 /* Add a PT_MOVETO if necessary */
882 pPath->newStroke=FALSE;
885 if(!LPtoDP(dc->hSelf, &pt, 1))
887 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
891 for(i = 0; i < cbPoints; i++) {
893 if(!LPtoDP(dc->hSelf, &pt, 1))
895 PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
900 BOOL PATH_PolyBezier(DC *dc, const POINT *pts, DWORD cbPoints)
902 GdiPath *pPath = &dc->path;
906 /* Check that path is open */
907 if(pPath->state!=PATH_Open)
910 for(i = 0; i < cbPoints; i++) {
912 if(!LPtoDP(dc->hSelf, &pt, 1))
914 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
919 BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
921 GdiPath *pPath = &dc->path;
925 /* Check that path is open */
926 if(pPath->state!=PATH_Open)
929 for(i = 0; i < cbPoints; i++) {
931 if(!LPtoDP(dc->hSelf, &pt, 1))
933 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
938 BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
940 GdiPath *pPath = &dc->path;
944 /* Check that path is open */
945 if(pPath->state!=PATH_Open)
948 /* Add a PT_MOVETO if necessary */
951 pPath->newStroke=FALSE;
954 if(!LPtoDP(dc->hSelf, &pt, 1))
956 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
960 for(i = 0; i < cbPoints; i++) {
962 if(!LPtoDP(dc->hSelf, &pt, 1))
964 PATH_AddEntry(pPath, &pt, PT_LINETO);
971 BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
973 GdiPath *pPath = &dc->path;
977 /* Check that path is open */
978 if(pPath->state!=PATH_Open)
981 for(i = 0; i < cbPoints; i++) {
983 if(!LPtoDP(dc->hSelf, &pt, 1))
985 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
986 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
992 BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
995 GdiPath *pPath = &dc->path;
999 /* Check that path is open */
1000 if(pPath->state!=PATH_Open)
1003 for(i = 0, poly = 0; poly < polygons; poly++) {
1004 for(point = 0; point < counts[poly]; point++, i++) {
1006 if(!LPtoDP(dc->hSelf, &pt, 1))
1008 if(point == 0) startpt = pt;
1009 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1011 /* win98 adds an extra line to close the figure for some reason */
1012 PATH_AddEntry(pPath, &startpt, PT_LINETO | PT_CLOSEFIGURE);
1017 BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1020 GdiPath *pPath = &dc->path;
1024 /* Check that path is open */
1025 if(pPath->state!=PATH_Open)
1028 for(i = 0, poly = 0; poly < polylines; poly++) {
1029 for(point = 0; point < counts[poly]; point++, i++) {
1031 if(!LPtoDP(dc->hSelf, &pt, 1))
1033 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1039 /***********************************************************************
1040 * Internal functions
1043 /* PATH_CheckCorners
1045 * Helper function for PATH_RoundRect() and PATH_Rectangle()
1047 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2)
1051 /* Convert points to device coordinates */
1056 if(!LPtoDP(dc->hSelf, corners, 2))
1059 /* Make sure first corner is top left and second corner is bottom right */
1060 if(corners[0].x>corners[1].x)
1063 corners[0].x=corners[1].x;
1066 if(corners[0].y>corners[1].y)
1069 corners[0].y=corners[1].y;
1073 /* In GM_COMPATIBLE, don't include bottom and right edges */
1074 if(dc->GraphicsMode==GM_COMPATIBLE)
1083 /* PATH_AddFlatBezier
1085 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
1090 pts = GDI_Bezier( pt, 4, &no );
1091 if(!pts) return FALSE;
1093 for(i = 1; i < no; i++)
1094 PATH_AddEntry(pPath, &pts[i],
1095 (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
1096 HeapFree( GetProcessHeap(), 0, pts );
1102 * Replaces Beziers with line segments
1105 static BOOL PATH_FlattenPath(GdiPath *pPath)
1110 memset(&newPath, 0, sizeof(newPath));
1111 newPath.state = PATH_Open;
1112 for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
1113 switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
1116 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
1117 pPath->pFlags[srcpt]);
1120 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
1121 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
1126 newPath.state = PATH_Closed;
1127 PATH_AssignGdiPath(pPath, &newPath);
1128 PATH_DestroyGdiPath(&newPath);
1132 /* PATH_PathToRegion
1134 * Creates a region from the specified path using the specified polygon
1135 * filling mode. The path is left unchanged. A handle to the region that
1136 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
1137 * error occurs, SetLastError is called with the appropriate value and
1138 * FALSE is returned.
1140 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
1143 int numStrokes, iStroke, i;
1144 INT *pNumPointsInStroke;
1147 assert(pPath!=NULL);
1148 assert(pHrgn!=NULL);
1150 PATH_FlattenPath(pPath);
1152 /* FIXME: What happens when number of points is zero? */
1154 /* First pass: Find out how many strokes there are in the path */
1155 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
1157 for(i=0; i<pPath->numEntriesUsed; i++)
1158 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1161 /* Allocate memory for number-of-points-in-stroke array */
1162 pNumPointsInStroke=(int *)HeapAlloc( GetProcessHeap(), 0,
1163 sizeof(int) * numStrokes );
1164 if(!pNumPointsInStroke)
1166 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1170 /* Second pass: remember number of points in each polygon */
1171 iStroke=-1; /* Will get incremented to 0 at beginning of first stroke */
1172 for(i=0; i<pPath->numEntriesUsed; i++)
1174 /* Is this the beginning of a new stroke? */
1175 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1178 pNumPointsInStroke[iStroke]=0;
1181 pNumPointsInStroke[iStroke]++;
1184 /* Create a region from the strokes */
1185 hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
1186 numStrokes, nPolyFillMode);
1188 /* Free memory for number-of-points-in-stroke array */
1189 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
1193 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1204 * Removes all entries from the path and sets the path state to PATH_Null.
1206 static void PATH_EmptyPath(GdiPath *pPath)
1208 assert(pPath!=NULL);
1210 pPath->state=PATH_Null;
1211 pPath->numEntriesUsed=0;
1216 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1217 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1218 * successful, FALSE otherwise (e.g. if not enough memory was available).
1220 BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
1222 assert(pPath!=NULL);
1224 /* FIXME: If newStroke is true, perhaps we want to check that we're
1225 * getting a PT_MOVETO
1227 TRACE("(%ld,%ld) - %d\n", pPoint->x, pPoint->y, flags);
1229 /* Check that path is open */
1230 if(pPath->state!=PATH_Open)
1233 /* Reserve enough memory for an extra path entry */
1234 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
1237 /* Store information in path entry */
1238 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
1239 pPath->pFlags[pPath->numEntriesUsed]=flags;
1241 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1242 if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
1243 pPath->newStroke=TRUE;
1245 /* Increment entry count */
1246 pPath->numEntriesUsed++;
1251 /* PATH_ReserveEntries
1253 * Ensures that at least "numEntries" entries (for points and flags) have
1254 * been allocated; allocates larger arrays and copies the existing entries
1255 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1257 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries)
1259 INT numEntriesToAllocate;
1263 assert(pPath!=NULL);
1264 assert(numEntries>=0);
1266 /* Do we have to allocate more memory? */
1267 if(numEntries > pPath->numEntriesAllocated)
1269 /* Find number of entries to allocate. We let the size of the array
1270 * grow exponentially, since that will guarantee linear time
1272 if(pPath->numEntriesAllocated)
1274 numEntriesToAllocate=pPath->numEntriesAllocated;
1275 while(numEntriesToAllocate<numEntries)
1276 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
1280 numEntriesToAllocate=numEntries;
1282 /* Allocate new arrays */
1283 pPointsNew=(POINT *)HeapAlloc( GetProcessHeap(), 0,
1284 numEntriesToAllocate * sizeof(POINT) );
1287 pFlagsNew=(BYTE *)HeapAlloc( GetProcessHeap(), 0,
1288 numEntriesToAllocate * sizeof(BYTE) );
1291 HeapFree( GetProcessHeap(), 0, pPointsNew );
1295 /* Copy old arrays to new arrays and discard old arrays */
1298 assert(pPath->pFlags);
1300 memcpy(pPointsNew, pPath->pPoints,
1301 sizeof(POINT)*pPath->numEntriesUsed);
1302 memcpy(pFlagsNew, pPath->pFlags,
1303 sizeof(BYTE)*pPath->numEntriesUsed);
1305 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
1306 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
1308 pPath->pPoints=pPointsNew;
1309 pPath->pFlags=pFlagsNew;
1310 pPath->numEntriesAllocated=numEntriesToAllocate;
1318 * Creates a Bezier spline that corresponds to part of an arc and appends the
1319 * corresponding points to the path. The start and end angles are passed in
1320 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1321 * at most. If "addMoveTo" is true, a PT_MOVETO entry for the first control
1322 * point is added to the path; otherwise, it is assumed that the current
1323 * position is equal to the first control point.
1325 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
1326 double angleStart, double angleEnd, BOOL addMoveTo)
1328 double halfAngle, a;
1329 double xNorm[4], yNorm[4];
1333 assert(fabs(angleEnd-angleStart)<=M_PI_2);
1335 /* FIXME: Is there an easier way of computing this? */
1337 /* Compute control points */
1338 halfAngle=(angleEnd-angleStart)/2.0;
1339 if(fabs(halfAngle)>1e-8)
1341 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
1342 xNorm[0]=cos(angleStart);
1343 yNorm[0]=sin(angleStart);
1344 xNorm[1]=xNorm[0] - a*yNorm[0];
1345 yNorm[1]=yNorm[0] + a*xNorm[0];
1346 xNorm[3]=cos(angleEnd);
1347 yNorm[3]=sin(angleEnd);
1348 xNorm[2]=xNorm[3] + a*yNorm[3];
1349 yNorm[2]=yNorm[3] - a*xNorm[3];
1354 xNorm[i]=cos(angleStart);
1355 yNorm[i]=sin(angleStart);
1358 /* Add starting point to path if desired */
1361 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1362 if(!PATH_AddEntry(pPath, &point, PT_MOVETO))
1366 /* Add remaining control points */
1369 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1370 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1377 /* PATH_ScaleNormalizedPoint
1379 * Scales a normalized point (x, y) with respect to the box whose corners are
1380 * passed in "corners". The point is stored in "*pPoint". The normalized
1381 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1382 * (1.0, 1.0) correspond to corners[1].
1384 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
1385 double y, POINT *pPoint)
1387 pPoint->x=GDI_ROUND( (double)corners[0].x +
1388 (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
1389 pPoint->y=GDI_ROUND( (double)corners[0].y +
1390 (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
1393 /* PATH_NormalizePoint
1395 * Normalizes a point with respect to the box whose corners are passed in
1396 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1398 static void PATH_NormalizePoint(FLOAT_POINT corners[],
1399 const FLOAT_POINT *pPoint,
1400 double *pX, double *pY)
1402 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) *
1404 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1409 /*******************************************************************
1410 * FlattenPath [GDI32.@]
1414 BOOL WINAPI FlattenPath(HDC hdc)
1417 DC *dc = DC_GetDCPtr( hdc );
1419 if(!dc) return FALSE;
1421 if(dc->funcs->pFlattenPath) ret = dc->funcs->pFlattenPath(dc->physDev);
1424 GdiPath *pPath = &dc->path;
1425 if(pPath->state != PATH_Closed)
1426 ret = PATH_FlattenPath(pPath);
1428 GDI_ReleaseObj( hdc );
1433 static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1436 POINT ptLastMove = {0,0};
1437 POINT ptViewportOrg, ptWindowOrg;
1438 SIZE szViewportExt, szWindowExt;
1439 DWORD mapMode, graphicsMode;
1443 if(dc->funcs->pStrokePath)
1444 return dc->funcs->pStrokePath(dc->physDev);
1446 if(pPath->state != PATH_Closed)
1449 /* Save the mapping mode info */
1450 mapMode=GetMapMode(dc->hSelf);
1451 GetViewportExtEx(dc->hSelf, &szViewportExt);
1452 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
1453 GetWindowExtEx(dc->hSelf, &szWindowExt);
1454 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
1455 GetWorldTransform(dc->hSelf, &xform);
1458 SetMapMode(dc->hSelf, MM_TEXT);
1459 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
1460 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
1461 graphicsMode=GetGraphicsMode(dc->hSelf);
1462 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1463 ModifyWorldTransform(dc->hSelf, &xform, MWT_IDENTITY);
1464 SetGraphicsMode(dc->hSelf, graphicsMode);
1466 for(i = 0; i < pPath->numEntriesUsed; i++) {
1467 switch(pPath->pFlags[i]) {
1469 TRACE("Got PT_MOVETO (%ld, %ld)\n",
1470 pPath->pPoints[i].x, pPath->pPoints[i].y);
1471 MoveToEx(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y, NULL);
1472 ptLastMove = pPath->pPoints[i];
1475 case (PT_LINETO | PT_CLOSEFIGURE):
1476 TRACE("Got PT_LINETO (%ld, %ld)\n",
1477 pPath->pPoints[i].x, pPath->pPoints[i].y);
1478 LineTo(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y);
1481 TRACE("Got PT_BEZIERTO\n");
1482 if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1483 (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1484 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1488 PolyBezierTo(dc->hSelf, &pPath->pPoints[i], 3);
1492 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1496 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1497 LineTo(dc->hSelf, ptLastMove.x, ptLastMove.y);
1502 /* Restore the old mapping mode */
1503 SetMapMode(dc->hSelf, mapMode);
1504 SetViewportExtEx(dc->hSelf, szViewportExt.cx, szViewportExt.cy, NULL);
1505 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
1506 SetWindowExtEx(dc->hSelf, szWindowExt.cx, szWindowExt.cy, NULL);
1507 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
1509 /* Go to GM_ADVANCED temporarily to restore the world transform */
1510 graphicsMode=GetGraphicsMode(dc->hSelf);
1511 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1512 SetWorldTransform(dc->hSelf, &xform);
1513 SetGraphicsMode(dc->hSelf, graphicsMode);
1515 /* If we've moved the current point then get its new position
1516 which will be in device (MM_TEXT) co-ords, convert it to
1517 logical co-ords and re-set it. This basically updates
1518 dc->CurPosX|Y so that their values are in the correct mapping
1523 GetCurrentPositionEx(dc->hSelf, &pt);
1524 DPtoLP(dc->hSelf, &pt, 1);
1525 MoveToEx(dc->hSelf, pt.x, pt.y, NULL);
1531 /*******************************************************************
1532 * StrokeAndFillPath [GDI32.@]
1536 BOOL WINAPI StrokeAndFillPath(HDC hdc)
1538 DC *dc = DC_GetDCPtr( hdc );
1541 if(!dc) return FALSE;
1543 if(dc->funcs->pStrokeAndFillPath)
1544 bRet = dc->funcs->pStrokeAndFillPath(dc->physDev);
1547 bRet = PATH_FillPath(dc, &dc->path);
1548 if(bRet) bRet = PATH_StrokePath(dc, &dc->path);
1549 if(bRet) PATH_EmptyPath(&dc->path);
1551 GDI_ReleaseObj( hdc );
1556 /*******************************************************************
1557 * StrokePath [GDI32.@]
1561 BOOL WINAPI StrokePath(HDC hdc)
1563 DC *dc = DC_GetDCPtr( hdc );
1567 TRACE("(%p)\n", hdc);
1568 if(!dc) return FALSE;
1570 if(dc->funcs->pStrokePath)
1571 bRet = dc->funcs->pStrokePath(dc->physDev);
1575 bRet = PATH_StrokePath(dc, pPath);
1576 PATH_EmptyPath(pPath);
1578 GDI_ReleaseObj( hdc );
1583 /*******************************************************************
1584 * WidenPath [GDI32.@]
1588 BOOL WINAPI WidenPath(HDC hdc)
1590 DC *dc = DC_GetDCPtr( hdc );
1593 if(!dc) return FALSE;
1595 if(dc->funcs->pWidenPath)
1596 ret = dc->funcs->pWidenPath(dc->physDev);
1599 GDI_ReleaseObj( hdc );