2 * Graphics paths (BeginPath, EndPath etc.)
4 * Copyright 1997, 1998 Martin Boehme
13 #if defined(HAVE_FLOAT_H)
22 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(gdi);
27 /* Notes on the implementation
29 * The implementation is based on dynamically resizable arrays of points and
30 * flags. I dithered for a bit before deciding on this implementation, and
31 * I had even done a bit of work on a linked list version before switching
32 * to arrays. It's a bit of a tradeoff. When you use linked lists, the
33 * implementation of FlattenPath is easier, because you can rip the
34 * PT_BEZIERTO entries out of the middle of the list and link the
35 * corresponding PT_LINETO entries in. However, when you use arrays,
36 * PathToRegion becomes easier, since you can essentially just pass your array
37 * of points to CreatePolyPolygonRgn. Also, if I'd used linked lists, I would
38 * have had the extra effort of creating a chunk-based allocation scheme
39 * in order to use memory effectively. That's why I finally decided to use
40 * arrays. Note by the way that the array based implementation has the same
41 * linear time complexity that linked lists would have since the arrays grow
44 * The points are stored in the path in device coordinates. This is
45 * consistent with the way Windows does things (for instance, see the Win32
46 * SDK documentation for GetPath).
48 * The word "stroke" appears in several places (e.g. in the flag
49 * GdiPath.newStroke). A stroke consists of a PT_MOVETO followed by one or
50 * more PT_LINETOs or PT_BEZIERTOs, up to, but not including, the next
51 * PT_MOVETO. Note that this is not the same as the definition of a figure;
52 * a figure can contain several strokes.
54 * I modified the drawing functions (MoveTo, LineTo etc.) to test whether
55 * the path is open and to call the corresponding function in path.c if this
56 * is the case. A more elegant approach would be to modify the function
57 * pointers in the DC_FUNCTIONS structure; however, this would be a lot more
58 * complex. Also, the performance degradation caused by my approach in the
59 * case where no path is open is so small that it cannot be measured.
64 /* FIXME: A lot of stuff isn't implemented yet. There is much more to come. */
66 #define NUM_ENTRIES_INITIAL 16 /* Initial size of points / flags arrays */
67 #define GROW_FACTOR_NUMER 2 /* Numerator of grow factor for the array */
68 #define GROW_FACTOR_DENOM 1 /* Denominator of grow factor */
71 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
73 static void PATH_EmptyPath(GdiPath *pPath);
74 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries);
75 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
76 double angleStart, double angleEnd, BOOL addMoveTo);
77 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
78 double y, POINT *pPoint);
79 static void PATH_NormalizePoint(FLOAT_POINT corners[], const FLOAT_POINT
80 *pPoint, double *pX, double *pY);
81 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2);
84 /***********************************************************************
87 BOOL16 WINAPI BeginPath16(HDC16 hdc)
89 return (BOOL16)BeginPath((HDC)hdc);
93 /***********************************************************************
96 BOOL WINAPI BeginPath(HDC hdc)
99 DC *dc = DC_GetDCPtr( hdc );
101 if(!dc) return FALSE;
103 if(dc->funcs->pBeginPath)
104 ret = dc->funcs->pBeginPath(dc);
107 /* If path is already open, do nothing */
108 if(dc->path.state != PATH_Open)
110 /* Make sure that path is empty */
111 PATH_EmptyPath(&dc->path);
113 /* Initialize variables for new path */
114 dc->path.newStroke=TRUE;
115 dc->path.state=PATH_Open;
118 GDI_ReleaseObj( hdc );
123 /***********************************************************************
126 BOOL16 WINAPI EndPath16(HDC16 hdc)
128 return (BOOL16)EndPath((HDC)hdc);
132 /***********************************************************************
135 BOOL WINAPI EndPath(HDC hdc)
138 DC *dc = DC_GetDCPtr( hdc );
140 if(!dc) return FALSE;
142 if(dc->funcs->pEndPath)
143 ret = dc->funcs->pEndPath(dc);
146 /* Check that path is currently being constructed */
147 if(dc->path.state!=PATH_Open)
149 SetLastError(ERROR_CAN_NOT_COMPLETE);
152 /* Set flag to indicate that path is finished */
153 else dc->path.state=PATH_Closed;
155 GDI_ReleaseObj( hdc );
160 /***********************************************************************
161 * AbortPath (GDI.511)
163 BOOL16 WINAPI AbortPath16(HDC16 hdc)
165 return (BOOL16)AbortPath((HDC)hdc);
169 /******************************************************************************
170 * AbortPath [GDI32.@]
171 * Closes and discards paths from device context
174 * Check that SetLastError is being called correctly
177 * hdc [I] Handle to device context
181 BOOL WINAPI AbortPath( HDC hdc )
184 DC *dc = DC_GetDCPtr( hdc );
186 if(!dc) return FALSE;
188 if(dc->funcs->pAbortPath)
189 ret = dc->funcs->pAbortPath(dc);
190 else /* Remove all entries from the path */
191 PATH_EmptyPath( &dc->path );
192 GDI_ReleaseObj( hdc );
197 /***********************************************************************
198 * CloseFigure (GDI.513)
200 BOOL16 WINAPI CloseFigure16(HDC16 hdc)
202 return (BOOL16)CloseFigure((HDC)hdc);
206 /***********************************************************************
207 * CloseFigure (GDI32.@)
209 * FIXME: Check that SetLastError is being called correctly
211 BOOL WINAPI CloseFigure(HDC hdc)
214 DC *dc = DC_GetDCPtr( hdc );
216 if(!dc) return FALSE;
218 if(dc->funcs->pCloseFigure)
219 ret = dc->funcs->pCloseFigure(dc);
222 /* Check that path is open */
223 if(dc->path.state!=PATH_Open)
225 SetLastError(ERROR_CAN_NOT_COMPLETE);
230 /* FIXME: Shouldn't we draw a line to the beginning of the
232 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
233 if(dc->path.numEntriesUsed)
235 dc->path.pFlags[dc->path.numEntriesUsed-1]|=PT_CLOSEFIGURE;
236 dc->path.newStroke=TRUE;
240 GDI_ReleaseObj( hdc );
245 /***********************************************************************
248 INT16 WINAPI GetPath16(HDC16 hdc, LPPOINT16 pPoints, LPBYTE pTypes,
251 FIXME("(%d,%p,%p): stub\n",hdc,pPoints,pTypes);
257 /***********************************************************************
260 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
265 DC *dc = DC_GetDCPtr( hdc );
271 /* Check that path is closed */
272 if(pPath->state!=PATH_Closed)
274 SetLastError(ERROR_CAN_NOT_COMPLETE);
279 ret = pPath->numEntriesUsed;
280 else if(nSize<pPath->numEntriesUsed)
282 SetLastError(ERROR_INVALID_PARAMETER);
287 memcpy(pPoints, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
288 memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
290 /* Convert the points to logical coordinates */
291 if(!DPtoLP(hdc, pPoints, pPath->numEntriesUsed))
293 /* FIXME: Is this the correct value? */
294 SetLastError(ERROR_CAN_NOT_COMPLETE);
297 else ret = pPath->numEntriesUsed;
300 GDI_ReleaseObj( hdc );
304 /***********************************************************************
305 * PathToRegion (GDI.518)
307 HRGN16 WINAPI PathToRegion16(HDC16 hdc)
309 return (HRGN16) PathToRegion((HDC) hdc);
312 /***********************************************************************
313 * PathToRegion (GDI32.@)
316 * Check that SetLastError is being called correctly
318 * The documentation does not state this explicitly, but a test under Windows
319 * shows that the region which is returned should be in device coordinates.
321 HRGN WINAPI PathToRegion(HDC hdc)
325 DC *dc = DC_GetDCPtr( hdc );
327 /* Get pointer to path */
332 /* Check that path is closed */
333 if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
336 /* FIXME: Should we empty the path even if conversion failed? */
337 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
338 PATH_EmptyPath(pPath);
342 GDI_ReleaseObj( hdc );
346 static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
348 INT mapMode, graphicsMode;
349 SIZE ptViewportExt, ptWindowExt;
350 POINT ptViewportOrg, ptWindowOrg;
354 if(dc->funcs->pFillPath)
355 return dc->funcs->pFillPath(dc);
357 /* Check that path is closed */
358 if(pPath->state!=PATH_Closed)
360 SetLastError(ERROR_CAN_NOT_COMPLETE);
364 /* Construct a region from the path and fill it */
365 if(PATH_PathToRegion(pPath, dc->polyFillMode, &hrgn))
367 /* Since PaintRgn interprets the region as being in logical coordinates
368 * but the points we store for the path are already in device
369 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
370 * Using SaveDC to save information about the mapping mode / world
371 * transform would be easier but would require more overhead, especially
372 * now that SaveDC saves the current path.
375 /* Save the information about the old mapping mode */
376 mapMode=GetMapMode(dc->hSelf);
377 GetViewportExtEx(dc->hSelf, &ptViewportExt);
378 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
379 GetWindowExtEx(dc->hSelf, &ptWindowExt);
380 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
382 /* Save world transform
383 * NB: The Windows documentation on world transforms would lead one to
384 * believe that this has to be done only in GM_ADVANCED; however, my
385 * tests show that resetting the graphics mode to GM_COMPATIBLE does
386 * not reset the world transform.
388 GetWorldTransform(dc->hSelf, &xform);
391 SetMapMode(dc->hSelf, MM_TEXT);
392 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
393 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
395 /* Paint the region */
396 PaintRgn(dc->hSelf, hrgn);
398 /* Restore the old mapping mode */
399 SetMapMode(dc->hSelf, mapMode);
400 SetViewportExtEx(dc->hSelf, ptViewportExt.cx, ptViewportExt.cy, NULL);
401 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
402 SetWindowExtEx(dc->hSelf, ptWindowExt.cx, ptWindowExt.cy, NULL);
403 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
405 /* Go to GM_ADVANCED temporarily to restore the world transform */
406 graphicsMode=GetGraphicsMode(dc->hSelf);
407 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
408 SetWorldTransform(dc->hSelf, &xform);
409 SetGraphicsMode(dc->hSelf, graphicsMode);
415 /***********************************************************************
418 BOOL16 WINAPI FillPath16(HDC16 hdc)
420 return (BOOL16) FillPath((HDC) hdc);
423 /***********************************************************************
427 * Check that SetLastError is being called correctly
429 BOOL WINAPI FillPath(HDC hdc)
431 DC *dc = DC_GetDCPtr( hdc );
434 if(!dc) return FALSE;
436 if(dc->funcs->pFillPath)
437 bRet = dc->funcs->pFillPath(dc);
440 bRet = PATH_FillPath(dc, &dc->path);
443 /* FIXME: Should the path be emptied even if conversion
445 PATH_EmptyPath(&dc->path);
448 GDI_ReleaseObj( hdc );
452 /***********************************************************************
453 * SelectClipPath (GDI.519)
455 BOOL16 WINAPI SelectClipPath16(HDC16 hdc, INT16 iMode)
457 return (BOOL16) SelectClipPath((HDC) hdc, iMode);
460 /***********************************************************************
461 * SelectClipPath (GDI32.@)
463 * Check that SetLastError is being called correctly
465 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
469 BOOL success = FALSE;
470 DC *dc = DC_GetDCPtr( hdc );
472 if(!dc) return FALSE;
474 if(dc->funcs->pSelectClipPath)
475 success = dc->funcs->pSelectClipPath(dc, iMode);
480 /* Check that path is closed */
481 if(pPath->state!=PATH_Closed)
482 SetLastError(ERROR_CAN_NOT_COMPLETE);
483 /* Construct a region from the path */
484 else if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnPath))
486 success = ExtSelectClipRgn( hdc, hrgnPath, iMode ) != ERROR;
487 DeleteObject(hrgnPath);
491 PATH_EmptyPath(pPath);
492 /* FIXME: Should this function delete the path even if it failed? */
495 GDI_ReleaseObj( hdc );
500 /***********************************************************************
506 * Initializes the GdiPath structure.
508 void PATH_InitGdiPath(GdiPath *pPath)
512 pPath->state=PATH_Null;
515 pPath->numEntriesUsed=0;
516 pPath->numEntriesAllocated=0;
519 /* PATH_DestroyGdiPath
521 * Destroys a GdiPath structure (frees the memory in the arrays).
523 void PATH_DestroyGdiPath(GdiPath *pPath)
527 if (pPath->pPoints) HeapFree( GetProcessHeap(), 0, pPath->pPoints );
528 if (pPath->pFlags) HeapFree( GetProcessHeap(), 0, pPath->pFlags );
531 /* PATH_AssignGdiPath
533 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
534 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
535 * not just the pointers. Since this means that the arrays in pPathDest may
536 * need to be resized, pPathDest should have been initialized using
537 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
538 * not a copy constructor).
539 * Returns TRUE if successful, else FALSE.
541 BOOL PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
543 assert(pPathDest!=NULL && pPathSrc!=NULL);
545 /* Make sure destination arrays are big enough */
546 if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
549 /* Perform the copy operation */
550 memcpy(pPathDest->pPoints, pPathSrc->pPoints,
551 sizeof(POINT)*pPathSrc->numEntriesUsed);
552 memcpy(pPathDest->pFlags, pPathSrc->pFlags,
553 sizeof(BYTE)*pPathSrc->numEntriesUsed);
555 pPathDest->state=pPathSrc->state;
556 pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
557 pPathDest->newStroke=pPathSrc->newStroke;
564 * Should be called when a MoveTo is performed on a DC that has an
565 * open path. This starts a new stroke. Returns TRUE if successful, else
568 BOOL PATH_MoveTo(DC *dc)
570 GdiPath *pPath = &dc->path;
572 /* Check that path is open */
573 if(pPath->state!=PATH_Open)
574 /* FIXME: Do we have to call SetLastError? */
577 /* Start a new stroke */
578 pPath->newStroke=TRUE;
585 * Should be called when a LineTo is performed on a DC that has an
586 * open path. This adds a PT_LINETO entry to the path (and possibly
587 * a PT_MOVETO entry, if this is the first LineTo in a stroke).
588 * Returns TRUE if successful, else FALSE.
590 BOOL PATH_LineTo(DC *dc, INT x, INT y)
592 GdiPath *pPath = &dc->path;
593 POINT point, pointCurPos;
595 /* Check that path is open */
596 if(pPath->state!=PATH_Open)
599 /* Convert point to device coordinates */
602 if(!LPtoDP(dc->hSelf, &point, 1))
605 /* Add a PT_MOVETO if necessary */
608 pPath->newStroke=FALSE;
609 pointCurPos.x = dc->CursPosX;
610 pointCurPos.y = dc->CursPosY;
611 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
613 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
617 /* Add a PT_LINETO entry */
618 return PATH_AddEntry(pPath, &point, PT_LINETO);
623 * Should be called when a call to RoundRect is performed on a DC that has
624 * an open path. Returns TRUE if successful, else FALSE.
626 * FIXME: it adds the same entries to the path as windows does, but there
627 * is an error in the bezier drawing code so that there are small pixel-size
628 * gaps when the resulting path is drawn by StrokePath()
630 BOOL PATH_RoundRect(DC *dc, INT x1, INT y1, INT x2, INT y2, INT ell_width, INT ell_height)
632 GdiPath *pPath = &dc->path;
633 POINT corners[2], pointTemp;
634 FLOAT_POINT ellCorners[2];
636 /* Check that path is open */
637 if(pPath->state!=PATH_Open)
640 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
643 /* Add points to the roundrect path */
644 ellCorners[0].x = corners[1].x-ell_width;
645 ellCorners[0].y = corners[0].y;
646 ellCorners[1].x = corners[1].x;
647 ellCorners[1].y = corners[0].y+ell_height;
648 if(!PATH_DoArcPart(pPath, ellCorners, 0, -M_PI_2, TRUE))
650 pointTemp.x = corners[0].x+ell_width/2;
651 pointTemp.y = corners[0].y;
652 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
654 ellCorners[0].x = corners[0].x;
655 ellCorners[1].x = corners[0].x+ell_width;
656 if(!PATH_DoArcPart(pPath, ellCorners, -M_PI_2, -M_PI, FALSE))
658 pointTemp.x = corners[0].x;
659 pointTemp.y = corners[1].y-ell_height/2;
660 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
662 ellCorners[0].y = corners[1].y-ell_height;
663 ellCorners[1].y = corners[1].y;
664 if(!PATH_DoArcPart(pPath, ellCorners, M_PI, M_PI_2, FALSE))
666 pointTemp.x = corners[1].x-ell_width/2;
667 pointTemp.y = corners[1].y;
668 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
670 ellCorners[0].x = corners[1].x-ell_width;
671 ellCorners[1].x = corners[1].x;
672 if(!PATH_DoArcPart(pPath, ellCorners, M_PI_2, 0, FALSE))
675 /* Close the roundrect figure */
676 if(!CloseFigure(dc->hSelf))
684 * Should be called when a call to Rectangle is performed on a DC that has
685 * an open path. Returns TRUE if successful, else FALSE.
687 BOOL PATH_Rectangle(DC *dc, INT x1, INT y1, INT x2, INT y2)
689 GdiPath *pPath = &dc->path;
690 POINT corners[2], pointTemp;
692 /* Check that path is open */
693 if(pPath->state!=PATH_Open)
696 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
699 /* Close any previous figure */
700 if(!CloseFigure(dc->hSelf))
702 /* The CloseFigure call shouldn't have failed */
707 /* Add four points to the path */
708 pointTemp.x=corners[1].x;
709 pointTemp.y=corners[0].y;
710 if(!PATH_AddEntry(pPath, &pointTemp, PT_MOVETO))
712 if(!PATH_AddEntry(pPath, corners, PT_LINETO))
714 pointTemp.x=corners[0].x;
715 pointTemp.y=corners[1].y;
716 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
718 if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
721 /* Close the rectangle figure */
722 if(!CloseFigure(dc->hSelf))
724 /* The CloseFigure call shouldn't have failed */
734 * Should be called when a call to Ellipse is performed on a DC that has
735 * an open path. This adds four Bezier splines representing the ellipse
736 * to the path. Returns TRUE if successful, else FALSE.
738 BOOL PATH_Ellipse(DC *dc, INT x1, INT y1, INT x2, INT y2)
740 return( PATH_Arc(dc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2,0) &&
741 CloseFigure(dc->hSelf) );
746 * Should be called when a call to Arc is performed on a DC that has
747 * an open path. This adds up to five Bezier splines representing the arc
748 * to the path. When 'lines' is 1, we add 1 extra line to get a chord,
749 * and when 'lines' is 2, we add 2 extra lines to get a pie.
750 * Returns TRUE if successful, else FALSE.
752 BOOL PATH_Arc(DC *dc, INT x1, INT y1, INT x2, INT y2,
753 INT xStart, INT yStart, INT xEnd, INT yEnd, INT lines)
755 GdiPath *pPath = &dc->path;
756 double angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
757 /* Initialize angleEndQuadrant to silence gcc's warning */
759 FLOAT_POINT corners[2], pointStart, pointEnd;
764 /* FIXME: This function should check for all possible error returns */
765 /* FIXME: Do we have to respect newStroke? */
767 /* Check that path is open */
768 if(pPath->state!=PATH_Open)
771 /* Check for zero height / width */
772 /* FIXME: Only in GM_COMPATIBLE? */
776 /* Convert points to device coordinates */
777 corners[0].x=(FLOAT)x1;
778 corners[0].y=(FLOAT)y1;
779 corners[1].x=(FLOAT)x2;
780 corners[1].y=(FLOAT)y2;
781 pointStart.x=(FLOAT)xStart;
782 pointStart.y=(FLOAT)yStart;
783 pointEnd.x=(FLOAT)xEnd;
784 pointEnd.y=(FLOAT)yEnd;
785 INTERNAL_LPTODP_FLOAT(dc, corners);
786 INTERNAL_LPTODP_FLOAT(dc, corners+1);
787 INTERNAL_LPTODP_FLOAT(dc, &pointStart);
788 INTERNAL_LPTODP_FLOAT(dc, &pointEnd);
790 /* Make sure first corner is top left and second corner is bottom right */
791 if(corners[0].x>corners[1].x)
794 corners[0].x=corners[1].x;
797 if(corners[0].y>corners[1].y)
800 corners[0].y=corners[1].y;
804 /* Compute start and end angle */
805 PATH_NormalizePoint(corners, &pointStart, &x, &y);
806 angleStart=atan2(y, x);
807 PATH_NormalizePoint(corners, &pointEnd, &x, &y);
808 angleEnd=atan2(y, x);
810 /* Make sure the end angle is "on the right side" of the start angle */
811 if(dc->ArcDirection==AD_CLOCKWISE)
813 if(angleEnd<=angleStart)
816 assert(angleEnd>=angleStart);
821 if(angleEnd>=angleStart)
824 assert(angleEnd<=angleStart);
828 /* In GM_COMPATIBLE, don't include bottom and right edges */
829 if(dc->GraphicsMode==GM_COMPATIBLE)
835 /* Add the arc to the path with one Bezier spline per quadrant that the
841 /* Determine the start and end angles for this quadrant */
844 angleStartQuadrant=angleStart;
845 if(dc->ArcDirection==AD_CLOCKWISE)
846 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
848 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
852 angleStartQuadrant=angleEndQuadrant;
853 if(dc->ArcDirection==AD_CLOCKWISE)
854 angleEndQuadrant+=M_PI_2;
856 angleEndQuadrant-=M_PI_2;
859 /* Have we reached the last part of the arc? */
860 if((dc->ArcDirection==AD_CLOCKWISE &&
861 angleEnd<angleEndQuadrant) ||
862 (dc->ArcDirection==AD_COUNTERCLOCKWISE &&
863 angleEnd>angleEndQuadrant))
865 /* Adjust the end angle for this quadrant */
866 angleEndQuadrant=angleEnd;
870 /* Add the Bezier spline to the path */
871 PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
876 /* chord: close figure. pie: add line and close figure */
879 if(!CloseFigure(dc->hSelf))
884 centre.x = (corners[0].x+corners[1].x)/2;
885 centre.y = (corners[0].y+corners[1].y)/2;
886 if(!PATH_AddEntry(pPath, ¢re, PT_LINETO | PT_CLOSEFIGURE))
893 BOOL PATH_PolyBezierTo(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 /* Add a PT_MOVETO if necessary */
906 pPath->newStroke=FALSE;
909 if(!LPtoDP(dc->hSelf, &pt, 1))
911 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
915 for(i = 0; i < cbPoints; i++) {
917 if(!LPtoDP(dc->hSelf, &pt, 1))
919 PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
924 BOOL PATH_PolyBezier(DC *dc, const POINT *pts, DWORD cbPoints)
926 GdiPath *pPath = &dc->path;
930 /* Check that path is open */
931 if(pPath->state!=PATH_Open)
934 for(i = 0; i < cbPoints; i++) {
936 if(!LPtoDP(dc->hSelf, &pt, 1))
938 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
943 BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
945 GdiPath *pPath = &dc->path;
949 /* Check that path is open */
950 if(pPath->state!=PATH_Open)
953 for(i = 0; i < cbPoints; i++) {
955 if(!LPtoDP(dc->hSelf, &pt, 1))
957 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
962 BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
964 GdiPath *pPath = &dc->path;
968 /* Check that path is open */
969 if(pPath->state!=PATH_Open)
972 /* Add a PT_MOVETO if necessary */
975 pPath->newStroke=FALSE;
978 if(!LPtoDP(dc->hSelf, &pt, 1))
980 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
984 for(i = 0; i < cbPoints; i++) {
986 if(!LPtoDP(dc->hSelf, &pt, 1))
988 PATH_AddEntry(pPath, &pt, PT_LINETO);
995 BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
997 GdiPath *pPath = &dc->path;
1001 /* Check that path is open */
1002 if(pPath->state!=PATH_Open)
1005 for(i = 0; i < cbPoints; i++) {
1007 if(!LPtoDP(dc->hSelf, &pt, 1))
1009 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
1010 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
1016 BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
1019 GdiPath *pPath = &dc->path;
1023 /* Check that path is open */
1024 if(pPath->state!=PATH_Open)
1027 for(i = 0, poly = 0; poly < polygons; poly++) {
1028 for(point = 0; point < counts[poly]; point++, i++) {
1030 if(!LPtoDP(dc->hSelf, &pt, 1))
1032 if(point == 0) startpt = pt;
1033 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1035 /* win98 adds an extra line to close the figure for some reason */
1036 PATH_AddEntry(pPath, &startpt, PT_LINETO | PT_CLOSEFIGURE);
1041 BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1044 GdiPath *pPath = &dc->path;
1048 /* Check that path is open */
1049 if(pPath->state!=PATH_Open)
1052 for(i = 0, poly = 0; poly < polylines; poly++) {
1053 for(point = 0; point < counts[poly]; point++, i++) {
1055 if(!LPtoDP(dc->hSelf, &pt, 1))
1057 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1063 /***********************************************************************
1064 * Internal functions
1067 /* PATH_CheckCorners
1069 * Helper function for PATH_RoundRect() and PATH_Rectangle()
1071 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2)
1075 /* Convert points to device coordinates */
1080 if(!LPtoDP(dc->hSelf, corners, 2))
1083 /* Make sure first corner is top left and second corner is bottom right */
1084 if(corners[0].x>corners[1].x)
1087 corners[0].x=corners[1].x;
1090 if(corners[0].y>corners[1].y)
1093 corners[0].y=corners[1].y;
1097 /* In GM_COMPATIBLE, don't include bottom and right edges */
1098 if(dc->GraphicsMode==GM_COMPATIBLE)
1107 /* PATH_AddFlatBezier
1109 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
1114 pts = GDI_Bezier( pt, 4, &no );
1115 if(!pts) return FALSE;
1117 for(i = 1; i < no; i++)
1118 PATH_AddEntry(pPath, &pts[i],
1119 (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
1120 HeapFree( GetProcessHeap(), 0, pts );
1126 * Replaces Beziers with line segments
1129 static BOOL PATH_FlattenPath(GdiPath *pPath)
1134 memset(&newPath, 0, sizeof(newPath));
1135 newPath.state = PATH_Open;
1136 for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
1137 switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
1140 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
1141 pPath->pFlags[srcpt]);
1144 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
1145 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
1150 newPath.state = PATH_Closed;
1151 PATH_AssignGdiPath(pPath, &newPath);
1152 PATH_EmptyPath(&newPath);
1156 /* PATH_PathToRegion
1158 * Creates a region from the specified path using the specified polygon
1159 * filling mode. The path is left unchanged. A handle to the region that
1160 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
1161 * error occurs, SetLastError is called with the appropriate value and
1162 * FALSE is returned.
1164 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
1167 int numStrokes, iStroke, i;
1168 INT *pNumPointsInStroke;
1171 assert(pPath!=NULL);
1172 assert(pHrgn!=NULL);
1174 PATH_FlattenPath(pPath);
1176 /* FIXME: What happens when number of points is zero? */
1178 /* First pass: Find out how many strokes there are in the path */
1179 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
1181 for(i=0; i<pPath->numEntriesUsed; i++)
1182 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1185 /* Allocate memory for number-of-points-in-stroke array */
1186 pNumPointsInStroke=(int *)HeapAlloc( GetProcessHeap(), 0,
1187 sizeof(int) * numStrokes );
1188 if(!pNumPointsInStroke)
1190 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1194 /* Second pass: remember number of points in each polygon */
1195 iStroke=-1; /* Will get incremented to 0 at beginning of first stroke */
1196 for(i=0; i<pPath->numEntriesUsed; i++)
1198 /* Is this the beginning of a new stroke? */
1199 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1202 pNumPointsInStroke[iStroke]=0;
1205 pNumPointsInStroke[iStroke]++;
1208 /* Create a region from the strokes */
1209 hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
1210 numStrokes, nPolyFillMode);
1213 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1217 /* Free memory for number-of-points-in-stroke array */
1218 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
1227 * Removes all entries from the path and sets the path state to PATH_Null.
1229 static void PATH_EmptyPath(GdiPath *pPath)
1231 assert(pPath!=NULL);
1233 pPath->state=PATH_Null;
1234 pPath->numEntriesUsed=0;
1239 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1240 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1241 * successful, FALSE otherwise (e.g. if not enough memory was available).
1243 BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
1245 assert(pPath!=NULL);
1247 /* FIXME: If newStroke is true, perhaps we want to check that we're
1248 * getting a PT_MOVETO
1250 TRACE("(%ld,%ld) - %d\n", pPoint->x, pPoint->y, flags);
1252 /* Check that path is open */
1253 if(pPath->state!=PATH_Open)
1256 /* Reserve enough memory for an extra path entry */
1257 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
1260 /* Store information in path entry */
1261 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
1262 pPath->pFlags[pPath->numEntriesUsed]=flags;
1264 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1265 if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
1266 pPath->newStroke=TRUE;
1268 /* Increment entry count */
1269 pPath->numEntriesUsed++;
1274 /* PATH_ReserveEntries
1276 * Ensures that at least "numEntries" entries (for points and flags) have
1277 * been allocated; allocates larger arrays and copies the existing entries
1278 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1280 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries)
1282 INT numEntriesToAllocate;
1286 assert(pPath!=NULL);
1287 assert(numEntries>=0);
1289 /* Do we have to allocate more memory? */
1290 if(numEntries > pPath->numEntriesAllocated)
1292 /* Find number of entries to allocate. We let the size of the array
1293 * grow exponentially, since that will guarantee linear time
1295 if(pPath->numEntriesAllocated)
1297 numEntriesToAllocate=pPath->numEntriesAllocated;
1298 while(numEntriesToAllocate<numEntries)
1299 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
1303 numEntriesToAllocate=numEntries;
1305 /* Allocate new arrays */
1306 pPointsNew=(POINT *)HeapAlloc( GetProcessHeap(), 0,
1307 numEntriesToAllocate * sizeof(POINT) );
1310 pFlagsNew=(BYTE *)HeapAlloc( GetProcessHeap(), 0,
1311 numEntriesToAllocate * sizeof(BYTE) );
1314 HeapFree( GetProcessHeap(), 0, pPointsNew );
1318 /* Copy old arrays to new arrays and discard old arrays */
1321 assert(pPath->pFlags);
1323 memcpy(pPointsNew, pPath->pPoints,
1324 sizeof(POINT)*pPath->numEntriesUsed);
1325 memcpy(pFlagsNew, pPath->pFlags,
1326 sizeof(BYTE)*pPath->numEntriesUsed);
1328 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
1329 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
1331 pPath->pPoints=pPointsNew;
1332 pPath->pFlags=pFlagsNew;
1333 pPath->numEntriesAllocated=numEntriesToAllocate;
1341 * Creates a Bezier spline that corresponds to part of an arc and appends the
1342 * corresponding points to the path. The start and end angles are passed in
1343 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1344 * at most. If "addMoveTo" is true, a PT_MOVETO entry for the first control
1345 * point is added to the path; otherwise, it is assumed that the current
1346 * position is equal to the first control point.
1348 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
1349 double angleStart, double angleEnd, BOOL addMoveTo)
1351 double halfAngle, a;
1352 double xNorm[4], yNorm[4];
1356 assert(fabs(angleEnd-angleStart)<=M_PI_2);
1358 /* FIXME: Is there an easier way of computing this? */
1360 /* Compute control points */
1361 halfAngle=(angleEnd-angleStart)/2.0;
1362 if(fabs(halfAngle)>1e-8)
1364 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
1365 xNorm[0]=cos(angleStart);
1366 yNorm[0]=sin(angleStart);
1367 xNorm[1]=xNorm[0] - a*yNorm[0];
1368 yNorm[1]=yNorm[0] + a*xNorm[0];
1369 xNorm[3]=cos(angleEnd);
1370 yNorm[3]=sin(angleEnd);
1371 xNorm[2]=xNorm[3] + a*yNorm[3];
1372 yNorm[2]=yNorm[3] - a*xNorm[3];
1377 xNorm[i]=cos(angleStart);
1378 yNorm[i]=sin(angleStart);
1381 /* Add starting point to path if desired */
1384 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1385 if(!PATH_AddEntry(pPath, &point, PT_MOVETO))
1389 /* Add remaining control points */
1392 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1393 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1400 /* PATH_ScaleNormalizedPoint
1402 * Scales a normalized point (x, y) with respect to the box whose corners are
1403 * passed in "corners". The point is stored in "*pPoint". The normalized
1404 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1405 * (1.0, 1.0) correspond to corners[1].
1407 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
1408 double y, POINT *pPoint)
1410 pPoint->x=GDI_ROUND( (double)corners[0].x +
1411 (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
1412 pPoint->y=GDI_ROUND( (double)corners[0].y +
1413 (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
1416 /* PATH_NormalizePoint
1418 * Normalizes a point with respect to the box whose corners are passed in
1419 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1421 static void PATH_NormalizePoint(FLOAT_POINT corners[],
1422 const FLOAT_POINT *pPoint,
1423 double *pX, double *pY)
1425 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) *
1427 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1431 /*******************************************************************
1432 * FlattenPath [GDI.516]
1436 BOOL16 WINAPI FlattenPath16(HDC16 hdc)
1438 return (BOOL16) FlattenPath((HDC) hdc);
1441 /*******************************************************************
1442 * FlattenPath [GDI32.@]
1446 BOOL WINAPI FlattenPath(HDC hdc)
1449 DC *dc = DC_GetDCPtr( hdc );
1451 if(!dc) return FALSE;
1453 if(dc->funcs->pFlattenPath) ret = dc->funcs->pFlattenPath(dc);
1456 GdiPath *pPath = &dc->path;
1457 if(pPath->state != PATH_Closed)
1458 ret = PATH_FlattenPath(pPath);
1460 GDI_ReleaseObj( hdc );
1465 static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1468 POINT ptLastMove = {0,0};
1469 POINT ptViewportOrg, ptWindowOrg;
1470 SIZE szViewportExt, szWindowExt;
1471 DWORD mapMode, graphicsMode;
1475 if(dc->funcs->pStrokePath)
1476 return dc->funcs->pStrokePath(dc);
1478 if(pPath->state != PATH_Closed)
1481 /* Save the mapping mode info */
1482 mapMode=GetMapMode(dc->hSelf);
1483 GetViewportExtEx(dc->hSelf, &szViewportExt);
1484 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
1485 GetWindowExtEx(dc->hSelf, &szWindowExt);
1486 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
1487 GetWorldTransform(dc->hSelf, &xform);
1490 SetMapMode(dc->hSelf, MM_TEXT);
1491 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
1492 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
1495 for(i = 0; i < pPath->numEntriesUsed; i++) {
1496 switch(pPath->pFlags[i]) {
1498 TRACE("Got PT_MOVETO (%ld, %ld)\n",
1499 pPath->pPoints[i].x, pPath->pPoints[i].y);
1500 MoveToEx(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y, NULL);
1501 ptLastMove = pPath->pPoints[i];
1504 case (PT_LINETO | PT_CLOSEFIGURE):
1505 TRACE("Got PT_LINETO (%ld, %ld)\n",
1506 pPath->pPoints[i].x, pPath->pPoints[i].y);
1507 LineTo(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y);
1510 TRACE("Got PT_BEZIERTO\n");
1511 if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1512 (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1513 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1517 PolyBezierTo(dc->hSelf, &pPath->pPoints[i], 3);
1521 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1525 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1526 LineTo(dc->hSelf, ptLastMove.x, ptLastMove.y);
1531 /* Restore the old mapping mode */
1532 SetMapMode(dc->hSelf, mapMode);
1533 SetViewportExtEx(dc->hSelf, szViewportExt.cx, szViewportExt.cy, NULL);
1534 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
1535 SetWindowExtEx(dc->hSelf, szWindowExt.cx, szWindowExt.cy, NULL);
1536 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
1538 /* Go to GM_ADVANCED temporarily to restore the world transform */
1539 graphicsMode=GetGraphicsMode(dc->hSelf);
1540 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1541 SetWorldTransform(dc->hSelf, &xform);
1542 SetGraphicsMode(dc->hSelf, graphicsMode);
1547 /*******************************************************************
1548 * StrokeAndFillPath [GDI.520]
1552 BOOL16 WINAPI StrokeAndFillPath16(HDC16 hdc)
1554 return (BOOL16) StrokeAndFillPath((HDC) hdc);
1557 /*******************************************************************
1558 * StrokeAndFillPath [GDI32.@]
1562 BOOL WINAPI StrokeAndFillPath(HDC hdc)
1564 DC *dc = DC_GetDCPtr( hdc );
1567 if(!dc) return FALSE;
1569 if(dc->funcs->pStrokeAndFillPath)
1570 bRet = dc->funcs->pStrokeAndFillPath(dc);
1573 bRet = PATH_FillPath(dc, &dc->path);
1574 if(bRet) bRet = PATH_StrokePath(dc, &dc->path);
1575 if(bRet) PATH_EmptyPath(&dc->path);
1577 GDI_ReleaseObj( hdc );
1581 /*******************************************************************
1582 * StrokePath [GDI.521]
1586 BOOL16 WINAPI StrokePath16(HDC16 hdc)
1588 return (BOOL16) StrokePath((HDC) hdc);
1591 /*******************************************************************
1592 * StrokePath [GDI32.@]
1596 BOOL WINAPI StrokePath(HDC hdc)
1598 DC *dc = DC_GetDCPtr( hdc );
1602 TRACE("(%08x)\n", hdc);
1603 if(!dc) return FALSE;
1605 if(dc->funcs->pStrokePath)
1606 bRet = dc->funcs->pStrokePath(dc);
1610 bRet = PATH_StrokePath(dc, pPath);
1611 PATH_EmptyPath(pPath);
1613 GDI_ReleaseObj( hdc );
1617 /*******************************************************************
1618 * WidenPath [GDI.522]
1622 BOOL16 WINAPI WidenPath16(HDC16 hdc)
1624 return (BOOL16) WidenPath((HDC) hdc);
1627 /*******************************************************************
1628 * WidenPath [GDI32.@]
1632 BOOL WINAPI WidenPath(HDC hdc)
1634 DC *dc = DC_GetDCPtr( hdc );
1637 if(!dc) return FALSE;
1639 if(dc->funcs->pWidenPath)
1640 ret = dc->funcs->pWidenPath(dc);
1643 GDI_ReleaseObj( hdc );