2 * Graphics paths (BeginPath, EndPath etc.)
4 * Copyright 1997, 1998 Martin Boehme
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #if defined(HAVE_FLOAT_H)
36 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
41 /* Notes on the implementation
43 * The implementation is based on dynamically resizable arrays of points and
44 * flags. I dithered for a bit before deciding on this implementation, and
45 * I had even done a bit of work on a linked list version before switching
46 * to arrays. It's a bit of a tradeoff. When you use linked lists, the
47 * implementation of FlattenPath is easier, because you can rip the
48 * PT_BEZIERTO entries out of the middle of the list and link the
49 * corresponding PT_LINETO entries in. However, when you use arrays,
50 * PathToRegion becomes easier, since you can essentially just pass your array
51 * of points to CreatePolyPolygonRgn. Also, if I'd used linked lists, I would
52 * have had the extra effort of creating a chunk-based allocation scheme
53 * in order to use memory effectively. That's why I finally decided to use
54 * arrays. Note by the way that the array based implementation has the same
55 * linear time complexity that linked lists would have since the arrays grow
58 * The points are stored in the path in device coordinates. This is
59 * consistent with the way Windows does things (for instance, see the Win32
60 * SDK documentation for GetPath).
62 * The word "stroke" appears in several places (e.g. in the flag
63 * GdiPath.newStroke). A stroke consists of a PT_MOVETO followed by one or
64 * more PT_LINETOs or PT_BEZIERTOs, up to, but not including, the next
65 * PT_MOVETO. Note that this is not the same as the definition of a figure;
66 * a figure can contain several strokes.
68 * I modified the drawing functions (MoveTo, LineTo etc.) to test whether
69 * the path is open and to call the corresponding function in path.c if this
70 * is the case. A more elegant approach would be to modify the function
71 * pointers in the DC_FUNCTIONS structure; however, this would be a lot more
72 * complex. Also, the performance degradation caused by my approach in the
73 * case where no path is open is so small that it cannot be measured.
78 /* FIXME: A lot of stuff isn't implemented yet. There is much more to come. */
80 #define NUM_ENTRIES_INITIAL 16 /* Initial size of points / flags arrays */
81 #define GROW_FACTOR_NUMER 2 /* Numerator of grow factor for the array */
82 #define GROW_FACTOR_DENOM 1 /* Denominator of grow factor */
85 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
87 static void PATH_EmptyPath(GdiPath *pPath);
88 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries);
89 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
90 double angleStart, double angleEnd, BOOL addMoveTo);
91 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
92 double y, POINT *pPoint);
93 static void PATH_NormalizePoint(FLOAT_POINT corners[], const FLOAT_POINT
94 *pPoint, double *pX, double *pY);
95 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2);
98 /***********************************************************************
101 BOOL16 WINAPI BeginPath16(HDC16 hdc)
103 return (BOOL16)BeginPath((HDC)hdc);
107 /***********************************************************************
108 * BeginPath (GDI32.@)
110 BOOL WINAPI BeginPath(HDC hdc)
113 DC *dc = DC_GetDCPtr( hdc );
115 if(!dc) return FALSE;
117 if(dc->funcs->pBeginPath)
118 ret = dc->funcs->pBeginPath(dc->physDev);
121 /* If path is already open, do nothing */
122 if(dc->path.state != PATH_Open)
124 /* Make sure that path is empty */
125 PATH_EmptyPath(&dc->path);
127 /* Initialize variables for new path */
128 dc->path.newStroke=TRUE;
129 dc->path.state=PATH_Open;
132 GDI_ReleaseObj( hdc );
137 /***********************************************************************
140 BOOL16 WINAPI EndPath16(HDC16 hdc)
142 return (BOOL16)EndPath((HDC)hdc);
146 /***********************************************************************
149 BOOL WINAPI EndPath(HDC hdc)
152 DC *dc = DC_GetDCPtr( hdc );
154 if(!dc) return FALSE;
156 if(dc->funcs->pEndPath)
157 ret = dc->funcs->pEndPath(dc->physDev);
160 /* Check that path is currently being constructed */
161 if(dc->path.state!=PATH_Open)
163 SetLastError(ERROR_CAN_NOT_COMPLETE);
166 /* Set flag to indicate that path is finished */
167 else dc->path.state=PATH_Closed;
169 GDI_ReleaseObj( hdc );
174 /***********************************************************************
175 * AbortPath (GDI.511)
177 BOOL16 WINAPI AbortPath16(HDC16 hdc)
179 return (BOOL16)AbortPath((HDC)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 (GDI.513)
214 BOOL16 WINAPI CloseFigure16(HDC16 hdc)
216 return (BOOL16)CloseFigure((HDC)hdc);
220 /***********************************************************************
221 * CloseFigure (GDI32.@)
223 * FIXME: Check that SetLastError is being called correctly
225 BOOL WINAPI CloseFigure(HDC hdc)
228 DC *dc = DC_GetDCPtr( hdc );
230 if(!dc) return FALSE;
232 if(dc->funcs->pCloseFigure)
233 ret = dc->funcs->pCloseFigure(dc->physDev);
236 /* Check that path is open */
237 if(dc->path.state!=PATH_Open)
239 SetLastError(ERROR_CAN_NOT_COMPLETE);
244 /* FIXME: Shouldn't we draw a line to the beginning of the
246 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
247 if(dc->path.numEntriesUsed)
249 dc->path.pFlags[dc->path.numEntriesUsed-1]|=PT_CLOSEFIGURE;
250 dc->path.newStroke=TRUE;
254 GDI_ReleaseObj( hdc );
259 /***********************************************************************
262 INT16 WINAPI GetPath16(HDC16 hdc, LPPOINT16 pPoints, LPBYTE pTypes,
265 FIXME("(%d,%p,%p): stub\n",hdc,pPoints,pTypes);
271 /***********************************************************************
274 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
279 DC *dc = DC_GetDCPtr( hdc );
285 /* Check that path is closed */
286 if(pPath->state!=PATH_Closed)
288 SetLastError(ERROR_CAN_NOT_COMPLETE);
293 ret = pPath->numEntriesUsed;
294 else if(nSize<pPath->numEntriesUsed)
296 SetLastError(ERROR_INVALID_PARAMETER);
301 memcpy(pPoints, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
302 memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
304 /* Convert the points to logical coordinates */
305 if(!DPtoLP(hdc, pPoints, pPath->numEntriesUsed))
307 /* FIXME: Is this the correct value? */
308 SetLastError(ERROR_CAN_NOT_COMPLETE);
311 else ret = pPath->numEntriesUsed;
314 GDI_ReleaseObj( hdc );
318 /***********************************************************************
319 * PathToRegion (GDI.518)
321 HRGN16 WINAPI PathToRegion16(HDC16 hdc)
323 return (HRGN16) PathToRegion((HDC) hdc);
326 /***********************************************************************
327 * PathToRegion (GDI32.@)
330 * Check that SetLastError is being called correctly
332 * The documentation does not state this explicitly, but a test under Windows
333 * shows that the region which is returned should be in device coordinates.
335 HRGN WINAPI PathToRegion(HDC hdc)
339 DC *dc = DC_GetDCPtr( hdc );
341 /* Get pointer to path */
346 /* Check that path is closed */
347 if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
350 /* FIXME: Should we empty the path even if conversion failed? */
351 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
352 PATH_EmptyPath(pPath);
356 GDI_ReleaseObj( hdc );
360 static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
362 INT mapMode, graphicsMode;
363 SIZE ptViewportExt, ptWindowExt;
364 POINT ptViewportOrg, ptWindowOrg;
368 if(dc->funcs->pFillPath)
369 return dc->funcs->pFillPath(dc->physDev);
371 /* Check that path is closed */
372 if(pPath->state!=PATH_Closed)
374 SetLastError(ERROR_CAN_NOT_COMPLETE);
378 /* Construct a region from the path and fill it */
379 if(PATH_PathToRegion(pPath, dc->polyFillMode, &hrgn))
381 /* Since PaintRgn interprets the region as being in logical coordinates
382 * but the points we store for the path are already in device
383 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
384 * Using SaveDC to save information about the mapping mode / world
385 * transform would be easier but would require more overhead, especially
386 * now that SaveDC saves the current path.
389 /* Save the information about the old mapping mode */
390 mapMode=GetMapMode(dc->hSelf);
391 GetViewportExtEx(dc->hSelf, &ptViewportExt);
392 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
393 GetWindowExtEx(dc->hSelf, &ptWindowExt);
394 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
396 /* Save world transform
397 * NB: The Windows documentation on world transforms would lead one to
398 * believe that this has to be done only in GM_ADVANCED; however, my
399 * tests show that resetting the graphics mode to GM_COMPATIBLE does
400 * not reset the world transform.
402 GetWorldTransform(dc->hSelf, &xform);
405 SetMapMode(dc->hSelf, MM_TEXT);
406 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
407 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
409 /* Paint the region */
410 PaintRgn(dc->hSelf, hrgn);
412 /* Restore the old mapping mode */
413 SetMapMode(dc->hSelf, mapMode);
414 SetViewportExtEx(dc->hSelf, ptViewportExt.cx, ptViewportExt.cy, NULL);
415 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
416 SetWindowExtEx(dc->hSelf, ptWindowExt.cx, ptWindowExt.cy, NULL);
417 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
419 /* Go to GM_ADVANCED temporarily to restore the world transform */
420 graphicsMode=GetGraphicsMode(dc->hSelf);
421 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
422 SetWorldTransform(dc->hSelf, &xform);
423 SetGraphicsMode(dc->hSelf, graphicsMode);
429 /***********************************************************************
432 BOOL16 WINAPI FillPath16(HDC16 hdc)
434 return (BOOL16) FillPath((HDC) hdc);
437 /***********************************************************************
441 * Check that SetLastError is being called correctly
443 BOOL WINAPI FillPath(HDC hdc)
445 DC *dc = DC_GetDCPtr( hdc );
448 if(!dc) return FALSE;
450 if(dc->funcs->pFillPath)
451 bRet = dc->funcs->pFillPath(dc->physDev);
454 bRet = PATH_FillPath(dc, &dc->path);
457 /* FIXME: Should the path be emptied even if conversion
459 PATH_EmptyPath(&dc->path);
462 GDI_ReleaseObj( hdc );
466 /***********************************************************************
467 * SelectClipPath (GDI.519)
469 BOOL16 WINAPI SelectClipPath16(HDC16 hdc, INT16 iMode)
471 return (BOOL16) SelectClipPath((HDC) hdc, iMode);
474 /***********************************************************************
475 * SelectClipPath (GDI32.@)
477 * Check that SetLastError is being called correctly
479 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
483 BOOL success = FALSE;
484 DC *dc = DC_GetDCPtr( hdc );
486 if(!dc) return FALSE;
488 if(dc->funcs->pSelectClipPath)
489 success = dc->funcs->pSelectClipPath(dc->physDev, iMode);
494 /* Check that path is closed */
495 if(pPath->state!=PATH_Closed)
496 SetLastError(ERROR_CAN_NOT_COMPLETE);
497 /* Construct a region from the path */
498 else if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnPath))
500 success = ExtSelectClipRgn( hdc, hrgnPath, iMode ) != ERROR;
501 DeleteObject(hrgnPath);
505 PATH_EmptyPath(pPath);
506 /* FIXME: Should this function delete the path even if it failed? */
509 GDI_ReleaseObj( hdc );
514 /***********************************************************************
520 * Initializes the GdiPath structure.
522 void PATH_InitGdiPath(GdiPath *pPath)
526 pPath->state=PATH_Null;
529 pPath->numEntriesUsed=0;
530 pPath->numEntriesAllocated=0;
533 /* PATH_DestroyGdiPath
535 * Destroys a GdiPath structure (frees the memory in the arrays).
537 void PATH_DestroyGdiPath(GdiPath *pPath)
541 if (pPath->pPoints) HeapFree( GetProcessHeap(), 0, pPath->pPoints );
542 if (pPath->pFlags) HeapFree( GetProcessHeap(), 0, pPath->pFlags );
545 /* PATH_AssignGdiPath
547 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
548 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
549 * not just the pointers. Since this means that the arrays in pPathDest may
550 * need to be resized, pPathDest should have been initialized using
551 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
552 * not a copy constructor).
553 * Returns TRUE if successful, else FALSE.
555 BOOL PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
557 assert(pPathDest!=NULL && pPathSrc!=NULL);
559 /* Make sure destination arrays are big enough */
560 if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
563 /* Perform the copy operation */
564 memcpy(pPathDest->pPoints, pPathSrc->pPoints,
565 sizeof(POINT)*pPathSrc->numEntriesUsed);
566 memcpy(pPathDest->pFlags, pPathSrc->pFlags,
567 sizeof(BYTE)*pPathSrc->numEntriesUsed);
569 pPathDest->state=pPathSrc->state;
570 pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
571 pPathDest->newStroke=pPathSrc->newStroke;
578 * Should be called when a MoveTo is performed on a DC that has an
579 * open path. This starts a new stroke. Returns TRUE if successful, else
582 BOOL PATH_MoveTo(DC *dc)
584 GdiPath *pPath = &dc->path;
586 /* Check that path is open */
587 if(pPath->state!=PATH_Open)
588 /* FIXME: Do we have to call SetLastError? */
591 /* Start a new stroke */
592 pPath->newStroke=TRUE;
599 * Should be called when a LineTo is performed on a DC that has an
600 * open path. This adds a PT_LINETO entry to the path (and possibly
601 * a PT_MOVETO entry, if this is the first LineTo in a stroke).
602 * Returns TRUE if successful, else FALSE.
604 BOOL PATH_LineTo(DC *dc, INT x, INT y)
606 GdiPath *pPath = &dc->path;
607 POINT point, pointCurPos;
609 /* Check that path is open */
610 if(pPath->state!=PATH_Open)
613 /* Convert point to device coordinates */
616 if(!LPtoDP(dc->hSelf, &point, 1))
619 /* Add a PT_MOVETO if necessary */
622 pPath->newStroke=FALSE;
623 pointCurPos.x = dc->CursPosX;
624 pointCurPos.y = dc->CursPosY;
625 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
627 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
631 /* Add a PT_LINETO entry */
632 return PATH_AddEntry(pPath, &point, PT_LINETO);
637 * Should be called when a call to RoundRect is performed on a DC that has
638 * an open path. Returns TRUE if successful, else FALSE.
640 * FIXME: it adds the same entries to the path as windows does, but there
641 * is an error in the bezier drawing code so that there are small pixel-size
642 * gaps when the resulting path is drawn by StrokePath()
644 BOOL PATH_RoundRect(DC *dc, INT x1, INT y1, INT x2, INT y2, INT ell_width, INT ell_height)
646 GdiPath *pPath = &dc->path;
647 POINT corners[2], pointTemp;
648 FLOAT_POINT ellCorners[2];
650 /* Check that path is open */
651 if(pPath->state!=PATH_Open)
654 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
657 /* Add points to the roundrect path */
658 ellCorners[0].x = corners[1].x-ell_width;
659 ellCorners[0].y = corners[0].y;
660 ellCorners[1].x = corners[1].x;
661 ellCorners[1].y = corners[0].y+ell_height;
662 if(!PATH_DoArcPart(pPath, ellCorners, 0, -M_PI_2, TRUE))
664 pointTemp.x = corners[0].x+ell_width/2;
665 pointTemp.y = corners[0].y;
666 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
668 ellCorners[0].x = corners[0].x;
669 ellCorners[1].x = corners[0].x+ell_width;
670 if(!PATH_DoArcPart(pPath, ellCorners, -M_PI_2, -M_PI, FALSE))
672 pointTemp.x = corners[0].x;
673 pointTemp.y = corners[1].y-ell_height/2;
674 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
676 ellCorners[0].y = corners[1].y-ell_height;
677 ellCorners[1].y = corners[1].y;
678 if(!PATH_DoArcPart(pPath, ellCorners, M_PI, M_PI_2, FALSE))
680 pointTemp.x = corners[1].x-ell_width/2;
681 pointTemp.y = corners[1].y;
682 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
684 ellCorners[0].x = corners[1].x-ell_width;
685 ellCorners[1].x = corners[1].x;
686 if(!PATH_DoArcPart(pPath, ellCorners, M_PI_2, 0, FALSE))
689 /* Close the roundrect figure */
690 if(!CloseFigure(dc->hSelf))
698 * Should be called when a call to Rectangle is performed on a DC that has
699 * an open path. Returns TRUE if successful, else FALSE.
701 BOOL PATH_Rectangle(DC *dc, INT x1, INT y1, INT x2, INT y2)
703 GdiPath *pPath = &dc->path;
704 POINT corners[2], pointTemp;
706 /* Check that path is open */
707 if(pPath->state!=PATH_Open)
710 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
713 /* Close any previous figure */
714 if(!CloseFigure(dc->hSelf))
716 /* The CloseFigure call shouldn't have failed */
721 /* Add four points to the path */
722 pointTemp.x=corners[1].x;
723 pointTemp.y=corners[0].y;
724 if(!PATH_AddEntry(pPath, &pointTemp, PT_MOVETO))
726 if(!PATH_AddEntry(pPath, corners, PT_LINETO))
728 pointTemp.x=corners[0].x;
729 pointTemp.y=corners[1].y;
730 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
732 if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
735 /* Close the rectangle figure */
736 if(!CloseFigure(dc->hSelf))
738 /* The CloseFigure call shouldn't have failed */
748 * Should be called when a call to Ellipse is performed on a DC that has
749 * an open path. This adds four Bezier splines representing the ellipse
750 * to the path. Returns TRUE if successful, else FALSE.
752 BOOL PATH_Ellipse(DC *dc, INT x1, INT y1, INT x2, INT y2)
754 return( PATH_Arc(dc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2,0) &&
755 CloseFigure(dc->hSelf) );
760 * Should be called when a call to Arc is performed on a DC that has
761 * an open path. This adds up to five Bezier splines representing the arc
762 * to the path. When 'lines' is 1, we add 1 extra line to get a chord,
763 * and when 'lines' is 2, we add 2 extra lines to get a pie.
764 * Returns TRUE if successful, else FALSE.
766 BOOL PATH_Arc(DC *dc, INT x1, INT y1, INT x2, INT y2,
767 INT xStart, INT yStart, INT xEnd, INT yEnd, INT lines)
769 GdiPath *pPath = &dc->path;
770 double angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
771 /* Initialize angleEndQuadrant to silence gcc's warning */
773 FLOAT_POINT corners[2], pointStart, pointEnd;
778 /* FIXME: This function should check for all possible error returns */
779 /* FIXME: Do we have to respect newStroke? */
781 /* Check that path is open */
782 if(pPath->state!=PATH_Open)
785 /* Check for zero height / width */
786 /* FIXME: Only in GM_COMPATIBLE? */
790 /* Convert points to device coordinates */
791 corners[0].x=(FLOAT)x1;
792 corners[0].y=(FLOAT)y1;
793 corners[1].x=(FLOAT)x2;
794 corners[1].y=(FLOAT)y2;
795 pointStart.x=(FLOAT)xStart;
796 pointStart.y=(FLOAT)yStart;
797 pointEnd.x=(FLOAT)xEnd;
798 pointEnd.y=(FLOAT)yEnd;
799 INTERNAL_LPTODP_FLOAT(dc, corners);
800 INTERNAL_LPTODP_FLOAT(dc, corners+1);
801 INTERNAL_LPTODP_FLOAT(dc, &pointStart);
802 INTERNAL_LPTODP_FLOAT(dc, &pointEnd);
804 /* Make sure first corner is top left and second corner is bottom right */
805 if(corners[0].x>corners[1].x)
808 corners[0].x=corners[1].x;
811 if(corners[0].y>corners[1].y)
814 corners[0].y=corners[1].y;
818 /* Compute start and end angle */
819 PATH_NormalizePoint(corners, &pointStart, &x, &y);
820 angleStart=atan2(y, x);
821 PATH_NormalizePoint(corners, &pointEnd, &x, &y);
822 angleEnd=atan2(y, x);
824 /* Make sure the end angle is "on the right side" of the start angle */
825 if(dc->ArcDirection==AD_CLOCKWISE)
827 if(angleEnd<=angleStart)
830 assert(angleEnd>=angleStart);
835 if(angleEnd>=angleStart)
838 assert(angleEnd<=angleStart);
842 /* In GM_COMPATIBLE, don't include bottom and right edges */
843 if(dc->GraphicsMode==GM_COMPATIBLE)
849 /* Add the arc to the path with one Bezier spline per quadrant that the
855 /* Determine the start and end angles for this quadrant */
858 angleStartQuadrant=angleStart;
859 if(dc->ArcDirection==AD_CLOCKWISE)
860 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
862 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
866 angleStartQuadrant=angleEndQuadrant;
867 if(dc->ArcDirection==AD_CLOCKWISE)
868 angleEndQuadrant+=M_PI_2;
870 angleEndQuadrant-=M_PI_2;
873 /* Have we reached the last part of the arc? */
874 if((dc->ArcDirection==AD_CLOCKWISE &&
875 angleEnd<angleEndQuadrant) ||
876 (dc->ArcDirection==AD_COUNTERCLOCKWISE &&
877 angleEnd>angleEndQuadrant))
879 /* Adjust the end angle for this quadrant */
880 angleEndQuadrant=angleEnd;
884 /* Add the Bezier spline to the path */
885 PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
890 /* chord: close figure. pie: add line and close figure */
893 if(!CloseFigure(dc->hSelf))
898 centre.x = (corners[0].x+corners[1].x)/2;
899 centre.y = (corners[0].y+corners[1].y)/2;
900 if(!PATH_AddEntry(pPath, ¢re, PT_LINETO | PT_CLOSEFIGURE))
907 BOOL PATH_PolyBezierTo(DC *dc, const POINT *pts, DWORD cbPoints)
909 GdiPath *pPath = &dc->path;
913 /* Check that path is open */
914 if(pPath->state!=PATH_Open)
917 /* Add a PT_MOVETO if necessary */
920 pPath->newStroke=FALSE;
923 if(!LPtoDP(dc->hSelf, &pt, 1))
925 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
929 for(i = 0; i < cbPoints; i++) {
931 if(!LPtoDP(dc->hSelf, &pt, 1))
933 PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
938 BOOL PATH_PolyBezier(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 for(i = 0; i < cbPoints; i++) {
950 if(!LPtoDP(dc->hSelf, &pt, 1))
952 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
957 BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
959 GdiPath *pPath = &dc->path;
963 /* Check that path is open */
964 if(pPath->state!=PATH_Open)
967 for(i = 0; i < cbPoints; i++) {
969 if(!LPtoDP(dc->hSelf, &pt, 1))
971 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
976 BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
978 GdiPath *pPath = &dc->path;
982 /* Check that path is open */
983 if(pPath->state!=PATH_Open)
986 /* Add a PT_MOVETO if necessary */
989 pPath->newStroke=FALSE;
992 if(!LPtoDP(dc->hSelf, &pt, 1))
994 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
998 for(i = 0; i < cbPoints; i++) {
1000 if(!LPtoDP(dc->hSelf, &pt, 1))
1002 PATH_AddEntry(pPath, &pt, PT_LINETO);
1009 BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
1011 GdiPath *pPath = &dc->path;
1015 /* Check that path is open */
1016 if(pPath->state!=PATH_Open)
1019 for(i = 0; i < cbPoints; i++) {
1021 if(!LPtoDP(dc->hSelf, &pt, 1))
1023 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
1024 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
1030 BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
1033 GdiPath *pPath = &dc->path;
1037 /* Check that path is open */
1038 if(pPath->state!=PATH_Open)
1041 for(i = 0, poly = 0; poly < polygons; poly++) {
1042 for(point = 0; point < counts[poly]; point++, i++) {
1044 if(!LPtoDP(dc->hSelf, &pt, 1))
1046 if(point == 0) startpt = pt;
1047 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1049 /* win98 adds an extra line to close the figure for some reason */
1050 PATH_AddEntry(pPath, &startpt, PT_LINETO | PT_CLOSEFIGURE);
1055 BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1058 GdiPath *pPath = &dc->path;
1062 /* Check that path is open */
1063 if(pPath->state!=PATH_Open)
1066 for(i = 0, poly = 0; poly < polylines; poly++) {
1067 for(point = 0; point < counts[poly]; point++, i++) {
1069 if(!LPtoDP(dc->hSelf, &pt, 1))
1071 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1077 /***********************************************************************
1078 * Internal functions
1081 /* PATH_CheckCorners
1083 * Helper function for PATH_RoundRect() and PATH_Rectangle()
1085 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2)
1089 /* Convert points to device coordinates */
1094 if(!LPtoDP(dc->hSelf, corners, 2))
1097 /* Make sure first corner is top left and second corner is bottom right */
1098 if(corners[0].x>corners[1].x)
1101 corners[0].x=corners[1].x;
1104 if(corners[0].y>corners[1].y)
1107 corners[0].y=corners[1].y;
1111 /* In GM_COMPATIBLE, don't include bottom and right edges */
1112 if(dc->GraphicsMode==GM_COMPATIBLE)
1121 /* PATH_AddFlatBezier
1123 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
1128 pts = GDI_Bezier( pt, 4, &no );
1129 if(!pts) return FALSE;
1131 for(i = 1; i < no; i++)
1132 PATH_AddEntry(pPath, &pts[i],
1133 (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
1134 HeapFree( GetProcessHeap(), 0, pts );
1140 * Replaces Beziers with line segments
1143 static BOOL PATH_FlattenPath(GdiPath *pPath)
1148 memset(&newPath, 0, sizeof(newPath));
1149 newPath.state = PATH_Open;
1150 for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
1151 switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
1154 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
1155 pPath->pFlags[srcpt]);
1158 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
1159 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
1164 newPath.state = PATH_Closed;
1165 PATH_AssignGdiPath(pPath, &newPath);
1166 PATH_EmptyPath(&newPath);
1170 /* PATH_PathToRegion
1172 * Creates a region from the specified path using the specified polygon
1173 * filling mode. The path is left unchanged. A handle to the region that
1174 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
1175 * error occurs, SetLastError is called with the appropriate value and
1176 * FALSE is returned.
1178 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
1181 int numStrokes, iStroke, i;
1182 INT *pNumPointsInStroke;
1185 assert(pPath!=NULL);
1186 assert(pHrgn!=NULL);
1188 PATH_FlattenPath(pPath);
1190 /* FIXME: What happens when number of points is zero? */
1192 /* First pass: Find out how many strokes there are in the path */
1193 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
1195 for(i=0; i<pPath->numEntriesUsed; i++)
1196 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1199 /* Allocate memory for number-of-points-in-stroke array */
1200 pNumPointsInStroke=(int *)HeapAlloc( GetProcessHeap(), 0,
1201 sizeof(int) * numStrokes );
1202 if(!pNumPointsInStroke)
1204 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1208 /* Second pass: remember number of points in each polygon */
1209 iStroke=-1; /* Will get incremented to 0 at beginning of first stroke */
1210 for(i=0; i<pPath->numEntriesUsed; i++)
1212 /* Is this the beginning of a new stroke? */
1213 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1216 pNumPointsInStroke[iStroke]=0;
1219 pNumPointsInStroke[iStroke]++;
1222 /* Create a region from the strokes */
1223 hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
1224 numStrokes, nPolyFillMode);
1226 /* Free memory for number-of-points-in-stroke array */
1227 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
1231 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1242 * Removes all entries from the path and sets the path state to PATH_Null.
1244 static void PATH_EmptyPath(GdiPath *pPath)
1246 assert(pPath!=NULL);
1248 pPath->state=PATH_Null;
1249 pPath->numEntriesUsed=0;
1254 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1255 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1256 * successful, FALSE otherwise (e.g. if not enough memory was available).
1258 BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
1260 assert(pPath!=NULL);
1262 /* FIXME: If newStroke is true, perhaps we want to check that we're
1263 * getting a PT_MOVETO
1265 TRACE("(%ld,%ld) - %d\n", pPoint->x, pPoint->y, flags);
1267 /* Check that path is open */
1268 if(pPath->state!=PATH_Open)
1271 /* Reserve enough memory for an extra path entry */
1272 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
1275 /* Store information in path entry */
1276 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
1277 pPath->pFlags[pPath->numEntriesUsed]=flags;
1279 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1280 if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
1281 pPath->newStroke=TRUE;
1283 /* Increment entry count */
1284 pPath->numEntriesUsed++;
1289 /* PATH_ReserveEntries
1291 * Ensures that at least "numEntries" entries (for points and flags) have
1292 * been allocated; allocates larger arrays and copies the existing entries
1293 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1295 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries)
1297 INT numEntriesToAllocate;
1301 assert(pPath!=NULL);
1302 assert(numEntries>=0);
1304 /* Do we have to allocate more memory? */
1305 if(numEntries > pPath->numEntriesAllocated)
1307 /* Find number of entries to allocate. We let the size of the array
1308 * grow exponentially, since that will guarantee linear time
1310 if(pPath->numEntriesAllocated)
1312 numEntriesToAllocate=pPath->numEntriesAllocated;
1313 while(numEntriesToAllocate<numEntries)
1314 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
1318 numEntriesToAllocate=numEntries;
1320 /* Allocate new arrays */
1321 pPointsNew=(POINT *)HeapAlloc( GetProcessHeap(), 0,
1322 numEntriesToAllocate * sizeof(POINT) );
1325 pFlagsNew=(BYTE *)HeapAlloc( GetProcessHeap(), 0,
1326 numEntriesToAllocate * sizeof(BYTE) );
1329 HeapFree( GetProcessHeap(), 0, pPointsNew );
1333 /* Copy old arrays to new arrays and discard old arrays */
1336 assert(pPath->pFlags);
1338 memcpy(pPointsNew, pPath->pPoints,
1339 sizeof(POINT)*pPath->numEntriesUsed);
1340 memcpy(pFlagsNew, pPath->pFlags,
1341 sizeof(BYTE)*pPath->numEntriesUsed);
1343 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
1344 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
1346 pPath->pPoints=pPointsNew;
1347 pPath->pFlags=pFlagsNew;
1348 pPath->numEntriesAllocated=numEntriesToAllocate;
1356 * Creates a Bezier spline that corresponds to part of an arc and appends the
1357 * corresponding points to the path. The start and end angles are passed in
1358 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1359 * at most. If "addMoveTo" is true, a PT_MOVETO entry for the first control
1360 * point is added to the path; otherwise, it is assumed that the current
1361 * position is equal to the first control point.
1363 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
1364 double angleStart, double angleEnd, BOOL addMoveTo)
1366 double halfAngle, a;
1367 double xNorm[4], yNorm[4];
1371 assert(fabs(angleEnd-angleStart)<=M_PI_2);
1373 /* FIXME: Is there an easier way of computing this? */
1375 /* Compute control points */
1376 halfAngle=(angleEnd-angleStart)/2.0;
1377 if(fabs(halfAngle)>1e-8)
1379 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
1380 xNorm[0]=cos(angleStart);
1381 yNorm[0]=sin(angleStart);
1382 xNorm[1]=xNorm[0] - a*yNorm[0];
1383 yNorm[1]=yNorm[0] + a*xNorm[0];
1384 xNorm[3]=cos(angleEnd);
1385 yNorm[3]=sin(angleEnd);
1386 xNorm[2]=xNorm[3] + a*yNorm[3];
1387 yNorm[2]=yNorm[3] - a*xNorm[3];
1392 xNorm[i]=cos(angleStart);
1393 yNorm[i]=sin(angleStart);
1396 /* Add starting point to path if desired */
1399 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1400 if(!PATH_AddEntry(pPath, &point, PT_MOVETO))
1404 /* Add remaining control points */
1407 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1408 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1415 /* PATH_ScaleNormalizedPoint
1417 * Scales a normalized point (x, y) with respect to the box whose corners are
1418 * passed in "corners". The point is stored in "*pPoint". The normalized
1419 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1420 * (1.0, 1.0) correspond to corners[1].
1422 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
1423 double y, POINT *pPoint)
1425 pPoint->x=GDI_ROUND( (double)corners[0].x +
1426 (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
1427 pPoint->y=GDI_ROUND( (double)corners[0].y +
1428 (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
1431 /* PATH_NormalizePoint
1433 * Normalizes a point with respect to the box whose corners are passed in
1434 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1436 static void PATH_NormalizePoint(FLOAT_POINT corners[],
1437 const FLOAT_POINT *pPoint,
1438 double *pX, double *pY)
1440 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) *
1442 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1446 /*******************************************************************
1447 * FlattenPath [GDI.516]
1451 BOOL16 WINAPI FlattenPath16(HDC16 hdc)
1453 return (BOOL16) FlattenPath((HDC) hdc);
1456 /*******************************************************************
1457 * FlattenPath [GDI32.@]
1461 BOOL WINAPI FlattenPath(HDC hdc)
1464 DC *dc = DC_GetDCPtr( hdc );
1466 if(!dc) return FALSE;
1468 if(dc->funcs->pFlattenPath) ret = dc->funcs->pFlattenPath(dc->physDev);
1471 GdiPath *pPath = &dc->path;
1472 if(pPath->state != PATH_Closed)
1473 ret = PATH_FlattenPath(pPath);
1475 GDI_ReleaseObj( hdc );
1480 static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1483 POINT ptLastMove = {0,0};
1484 POINT ptViewportOrg, ptWindowOrg;
1485 SIZE szViewportExt, szWindowExt;
1486 DWORD mapMode, graphicsMode;
1490 if(dc->funcs->pStrokePath)
1491 return dc->funcs->pStrokePath(dc->physDev);
1493 if(pPath->state != PATH_Closed)
1496 /* Save the mapping mode info */
1497 mapMode=GetMapMode(dc->hSelf);
1498 GetViewportExtEx(dc->hSelf, &szViewportExt);
1499 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
1500 GetWindowExtEx(dc->hSelf, &szWindowExt);
1501 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
1502 GetWorldTransform(dc->hSelf, &xform);
1505 SetMapMode(dc->hSelf, MM_TEXT);
1506 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
1507 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
1510 for(i = 0; i < pPath->numEntriesUsed; i++) {
1511 switch(pPath->pFlags[i]) {
1513 TRACE("Got PT_MOVETO (%ld, %ld)\n",
1514 pPath->pPoints[i].x, pPath->pPoints[i].y);
1515 MoveToEx(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y, NULL);
1516 ptLastMove = pPath->pPoints[i];
1519 case (PT_LINETO | PT_CLOSEFIGURE):
1520 TRACE("Got PT_LINETO (%ld, %ld)\n",
1521 pPath->pPoints[i].x, pPath->pPoints[i].y);
1522 LineTo(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y);
1525 TRACE("Got PT_BEZIERTO\n");
1526 if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1527 (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1528 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1532 PolyBezierTo(dc->hSelf, &pPath->pPoints[i], 3);
1536 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1540 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1541 LineTo(dc->hSelf, ptLastMove.x, ptLastMove.y);
1546 /* Restore the old mapping mode */
1547 SetMapMode(dc->hSelf, mapMode);
1548 SetViewportExtEx(dc->hSelf, szViewportExt.cx, szViewportExt.cy, NULL);
1549 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
1550 SetWindowExtEx(dc->hSelf, szWindowExt.cx, szWindowExt.cy, NULL);
1551 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
1553 /* Go to GM_ADVANCED temporarily to restore the world transform */
1554 graphicsMode=GetGraphicsMode(dc->hSelf);
1555 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1556 SetWorldTransform(dc->hSelf, &xform);
1557 SetGraphicsMode(dc->hSelf, graphicsMode);
1562 /*******************************************************************
1563 * StrokeAndFillPath [GDI.520]
1567 BOOL16 WINAPI StrokeAndFillPath16(HDC16 hdc)
1569 return (BOOL16) StrokeAndFillPath((HDC) hdc);
1572 /*******************************************************************
1573 * StrokeAndFillPath [GDI32.@]
1577 BOOL WINAPI StrokeAndFillPath(HDC hdc)
1579 DC *dc = DC_GetDCPtr( hdc );
1582 if(!dc) return FALSE;
1584 if(dc->funcs->pStrokeAndFillPath)
1585 bRet = dc->funcs->pStrokeAndFillPath(dc->physDev);
1588 bRet = PATH_FillPath(dc, &dc->path);
1589 if(bRet) bRet = PATH_StrokePath(dc, &dc->path);
1590 if(bRet) PATH_EmptyPath(&dc->path);
1592 GDI_ReleaseObj( hdc );
1596 /*******************************************************************
1597 * StrokePath [GDI.521]
1601 BOOL16 WINAPI StrokePath16(HDC16 hdc)
1603 return (BOOL16) StrokePath((HDC) hdc);
1606 /*******************************************************************
1607 * StrokePath [GDI32.@]
1611 BOOL WINAPI StrokePath(HDC hdc)
1613 DC *dc = DC_GetDCPtr( hdc );
1617 TRACE("(%08x)\n", hdc);
1618 if(!dc) return FALSE;
1620 if(dc->funcs->pStrokePath)
1621 bRet = dc->funcs->pStrokePath(dc->physDev);
1625 bRet = PATH_StrokePath(dc, pPath);
1626 PATH_EmptyPath(pPath);
1628 GDI_ReleaseObj( hdc );
1632 /*******************************************************************
1633 * WidenPath [GDI.522]
1637 BOOL16 WINAPI WidenPath16(HDC16 hdc)
1639 return (BOOL16) WidenPath((HDC) hdc);
1642 /*******************************************************************
1643 * WidenPath [GDI32.@]
1647 BOOL WINAPI WidenPath(HDC hdc)
1649 DC *dc = DC_GetDCPtr( hdc );
1652 if(!dc) return FALSE;
1654 if(dc->funcs->pWidenPath)
1655 ret = dc->funcs->pWidenPath(dc->physDev);
1658 GDI_ReleaseObj( hdc );