Inline functions don't need WINE_UNUSED.
[wine.git] / dlls / gdi / path.c
blob4c469d1a79930c0f37569155fddcce15e0b89787
1 /*
2 * Graphics paths (BeginPath, EndPath etc.)
4 * Copyright 1997, 1998 Martin Boehme
5 * 1999 Huw D M Davies
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <math.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #if defined(HAVE_FLOAT_H)
30 #include <float.h>
31 #endif
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wingdi.h"
36 #include "winerror.h"
38 #include "gdi.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
59 * exponentially.
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.
78 * Martin Boehme
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
90 FLOAT x, y;
91 } FLOAT_POINT;
94 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
95 HRGN *pHrgn);
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)
111 FLOAT x, y;
113 /* Perform the transformation */
114 x = point->x;
115 y = point->y;
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)
130 BOOL ret = TRUE;
131 DC *dc = DC_GetDCPtr( hdc );
133 if(!dc) return FALSE;
135 if(dc->funcs->pBeginPath)
136 ret = dc->funcs->pBeginPath(dc->physDev);
137 else
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 );
151 return ret;
155 /***********************************************************************
156 * EndPath (GDI32.@)
158 BOOL WINAPI EndPath(HDC hdc)
160 BOOL ret = TRUE;
161 DC *dc = DC_GetDCPtr( hdc );
163 if(!dc) return FALSE;
165 if(dc->funcs->pEndPath)
166 ret = dc->funcs->pEndPath(dc->physDev);
167 else
169 /* Check that path is currently being constructed */
170 if(dc->path.state!=PATH_Open)
172 SetLastError(ERROR_CAN_NOT_COMPLETE);
173 ret = FALSE;
175 /* Set flag to indicate that path is finished */
176 else dc->path.state=PATH_Closed;
178 GDI_ReleaseObj( hdc );
179 return ret;
183 /******************************************************************************
184 * AbortPath [GDI32.@]
185 * Closes and discards paths from device context
187 * NOTES
188 * Check that SetLastError is being called correctly
190 * PARAMS
191 * hdc [I] Handle to device context
193 * RETURNS STD
195 BOOL WINAPI AbortPath( HDC hdc )
197 BOOL ret = TRUE;
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 );
207 return ret;
211 /***********************************************************************
212 * CloseFigure (GDI32.@)
214 * FIXME: Check that SetLastError is being called correctly
216 BOOL WINAPI CloseFigure(HDC hdc)
218 BOOL ret = TRUE;
219 DC *dc = DC_GetDCPtr( hdc );
221 if(!dc) return FALSE;
223 if(dc->funcs->pCloseFigure)
224 ret = dc->funcs->pCloseFigure(dc->physDev);
225 else
227 /* Check that path is open */
228 if(dc->path.state!=PATH_Open)
230 SetLastError(ERROR_CAN_NOT_COMPLETE);
231 ret = FALSE;
233 else
235 /* FIXME: Shouldn't we draw a line to the beginning of the
236 figure? */
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 );
246 return ret;
250 /***********************************************************************
251 * GetPath (GDI32.@)
253 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
254 INT nSize)
256 INT ret = -1;
257 GdiPath *pPath;
258 DC *dc = DC_GetDCPtr( hdc );
260 if(!dc) return -1;
262 pPath = &dc->path;
264 /* Check that path is closed */
265 if(pPath->state!=PATH_Closed)
267 SetLastError(ERROR_CAN_NOT_COMPLETE);
268 goto done;
271 if(nSize==0)
272 ret = pPath->numEntriesUsed;
273 else if(nSize<pPath->numEntriesUsed)
275 SetLastError(ERROR_INVALID_PARAMETER);
276 goto done;
278 else
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);
288 goto done;
290 else ret = pPath->numEntriesUsed;
292 done:
293 GDI_ReleaseObj( hdc );
294 return ret;
298 /***********************************************************************
299 * PathToRegion (GDI32.@)
301 * FIXME
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)
309 GdiPath *pPath;
310 HRGN hrgnRval = 0;
311 DC *dc = DC_GetDCPtr( hdc );
313 /* Get pointer to path */
314 if(!dc) return 0;
316 pPath = &dc->path;
318 /* Check that path is closed */
319 if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
320 else
322 /* FIXME: Should we empty the path even if conversion failed? */
323 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
324 PATH_EmptyPath(pPath);
325 else
326 hrgnRval=0;
328 GDI_ReleaseObj( hdc );
329 return hrgnRval;
332 static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
334 INT mapMode, graphicsMode;
335 SIZE ptViewportExt, ptWindowExt;
336 POINT ptViewportOrg, ptWindowOrg;
337 XFORM xform;
338 HRGN hrgn;
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);
347 return FALSE;
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);
376 /* Set MM_TEXT */
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);
387 DeleteObject(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);
400 return TRUE;
402 return FALSE;
406 /***********************************************************************
407 * FillPath (GDI32.@)
409 * FIXME
410 * Check that SetLastError is being called correctly
412 BOOL WINAPI FillPath(HDC hdc)
414 DC *dc = DC_GetDCPtr( hdc );
415 BOOL bRet = FALSE;
417 if(!dc) return FALSE;
419 if(dc->funcs->pFillPath)
420 bRet = dc->funcs->pFillPath(dc->physDev);
421 else
423 bRet = PATH_FillPath(dc, &dc->path);
424 if(bRet)
426 /* FIXME: Should the path be emptied even if conversion
427 failed? */
428 PATH_EmptyPath(&dc->path);
431 GDI_ReleaseObj( hdc );
432 return bRet;
436 /***********************************************************************
437 * SelectClipPath (GDI32.@)
438 * FIXME
439 * Check that SetLastError is being called correctly
441 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
443 GdiPath *pPath;
444 HRGN hrgnPath;
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);
452 else
454 pPath = &dc->path;
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);
465 /* Empty the path */
466 if(success)
467 PATH_EmptyPath(pPath);
468 /* FIXME: Should this function delete the path even if it failed? */
471 GDI_ReleaseObj( hdc );
472 return success;
476 /***********************************************************************
477 * Exported functions
480 /* PATH_InitGdiPath
482 * Initializes the GdiPath structure.
484 void PATH_InitGdiPath(GdiPath *pPath)
486 assert(pPath!=NULL);
488 pPath->state=PATH_Null;
489 pPath->pPoints=NULL;
490 pPath->pFlags=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)
501 assert(pPath!=NULL);
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))
523 return FALSE;
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;
535 return TRUE;
538 /* PATH_MoveTo
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
542 * FALSE.
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? */
551 return FALSE;
553 /* Start a new stroke */
554 pPath->newStroke=TRUE;
556 return TRUE;
559 /* PATH_LineTo
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)
573 return FALSE;
575 /* Convert point to device coordinates */
576 point.x=x;
577 point.y=y;
578 if(!LPtoDP(dc->hSelf, &point, 1))
579 return FALSE;
581 /* Add a PT_MOVETO if necessary */
582 if(pPath->newStroke)
584 pPath->newStroke=FALSE;
585 pointCurPos.x = dc->CursPosX;
586 pointCurPos.y = dc->CursPosY;
587 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
588 return FALSE;
589 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
590 return FALSE;
593 /* Add a PT_LINETO entry */
594 return PATH_AddEntry(pPath, &point, PT_LINETO);
597 /* PATH_RoundRect
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)
614 return FALSE;
616 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
617 return FALSE;
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))
625 return FALSE;
626 pointTemp.x = corners[0].x+ell_width/2;
627 pointTemp.y = corners[0].y;
628 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
629 return FALSE;
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))
633 return FALSE;
634 pointTemp.x = corners[0].x;
635 pointTemp.y = corners[1].y-ell_height/2;
636 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
637 return FALSE;
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))
641 return FALSE;
642 pointTemp.x = corners[1].x-ell_width/2;
643 pointTemp.y = corners[1].y;
644 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
645 return FALSE;
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))
649 return FALSE;
651 /* Close the roundrect figure */
652 if(!CloseFigure(dc->hSelf))
653 return FALSE;
655 return TRUE;
658 /* PATH_Rectangle
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)
670 return FALSE;
672 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
673 return FALSE;
675 /* Close any previous figure */
676 if(!CloseFigure(dc->hSelf))
678 /* The CloseFigure call shouldn't have failed */
679 assert(FALSE);
680 return FALSE;
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))
687 return FALSE;
688 if(!PATH_AddEntry(pPath, corners, PT_LINETO))
689 return FALSE;
690 pointTemp.x=corners[0].x;
691 pointTemp.y=corners[1].y;
692 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
693 return FALSE;
694 if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
695 return FALSE;
697 /* Close the rectangle figure */
698 if(!CloseFigure(dc->hSelf))
700 /* The CloseFigure call shouldn't have failed */
701 assert(FALSE);
702 return FALSE;
705 return TRUE;
708 /* PATH_Ellipse
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) );
720 /* PATH_Arc
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 */
734 double x, y;
735 FLOAT_POINT corners[2], pointStart, pointEnd;
736 POINT centre;
737 BOOL start, end;
738 INT temp;
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)
745 return FALSE;
747 /* Check for zero height / width */
748 /* FIXME: Only in GM_COMPATIBLE? */
749 if(x1==x2 || y1==y2)
750 return TRUE;
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)
769 temp=corners[0].x;
770 corners[0].x=corners[1].x;
771 corners[1].x=temp;
773 if(corners[0].y>corners[1].y)
775 temp=corners[0].y;
776 corners[0].y=corners[1].y;
777 corners[1].y=temp;
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)
791 angleEnd+=2*M_PI;
792 assert(angleEnd>=angleStart);
795 else
797 if(angleEnd>=angleStart)
799 angleEnd-=2*M_PI;
800 assert(angleEnd<=angleStart);
804 /* In GM_COMPATIBLE, don't include bottom and right edges */
805 if(dc->GraphicsMode==GM_COMPATIBLE)
807 corners[1].x--;
808 corners[1].y--;
811 /* Add the arc to the path with one Bezier spline per quadrant that the
812 * arc spans */
813 start=TRUE;
814 end=FALSE;
817 /* Determine the start and end angles for this quadrant */
818 if(start)
820 angleStartQuadrant=angleStart;
821 if(dc->ArcDirection==AD_CLOCKWISE)
822 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
823 else
824 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
826 else
828 angleStartQuadrant=angleEndQuadrant;
829 if(dc->ArcDirection==AD_CLOCKWISE)
830 angleEndQuadrant+=M_PI_2;
831 else
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;
843 end=TRUE;
846 /* Add the Bezier spline to the path */
847 PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
848 start);
849 start=FALSE;
850 } while(!end);
852 /* chord: close figure. pie: add line and close figure */
853 if(lines==1)
855 if(!CloseFigure(dc->hSelf))
856 return FALSE;
858 else if(lines==2)
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, &centre, PT_LINETO | PT_CLOSEFIGURE))
863 return FALSE;
866 return TRUE;
869 BOOL PATH_PolyBezierTo(DC *dc, const POINT *pts, DWORD cbPoints)
871 GdiPath *pPath = &dc->path;
872 POINT pt;
873 INT i;
875 /* Check that path is open */
876 if(pPath->state!=PATH_Open)
877 return FALSE;
879 /* Add a PT_MOVETO if necessary */
880 if(pPath->newStroke)
882 pPath->newStroke=FALSE;
883 pt.x = dc->CursPosX;
884 pt.y = dc->CursPosY;
885 if(!LPtoDP(dc->hSelf, &pt, 1))
886 return FALSE;
887 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
888 return FALSE;
891 for(i = 0; i < cbPoints; i++) {
892 pt = pts[i];
893 if(!LPtoDP(dc->hSelf, &pt, 1))
894 return FALSE;
895 PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
897 return TRUE;
900 BOOL PATH_PolyBezier(DC *dc, const POINT *pts, DWORD cbPoints)
902 GdiPath *pPath = &dc->path;
903 POINT pt;
904 INT i;
906 /* Check that path is open */
907 if(pPath->state!=PATH_Open)
908 return FALSE;
910 for(i = 0; i < cbPoints; i++) {
911 pt = pts[i];
912 if(!LPtoDP(dc->hSelf, &pt, 1))
913 return FALSE;
914 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
916 return TRUE;
919 BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
921 GdiPath *pPath = &dc->path;
922 POINT pt;
923 INT i;
925 /* Check that path is open */
926 if(pPath->state!=PATH_Open)
927 return FALSE;
929 for(i = 0; i < cbPoints; i++) {
930 pt = pts[i];
931 if(!LPtoDP(dc->hSelf, &pt, 1))
932 return FALSE;
933 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
935 return TRUE;
938 BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
940 GdiPath *pPath = &dc->path;
941 POINT pt;
942 INT i;
944 /* Check that path is open */
945 if(pPath->state!=PATH_Open)
946 return FALSE;
948 /* Add a PT_MOVETO if necessary */
949 if(pPath->newStroke)
951 pPath->newStroke=FALSE;
952 pt.x = dc->CursPosX;
953 pt.y = dc->CursPosY;
954 if(!LPtoDP(dc->hSelf, &pt, 1))
955 return FALSE;
956 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
957 return FALSE;
960 for(i = 0; i < cbPoints; i++) {
961 pt = pts[i];
962 if(!LPtoDP(dc->hSelf, &pt, 1))
963 return FALSE;
964 PATH_AddEntry(pPath, &pt, PT_LINETO);
967 return TRUE;
971 BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
973 GdiPath *pPath = &dc->path;
974 POINT pt;
975 INT i;
977 /* Check that path is open */
978 if(pPath->state!=PATH_Open)
979 return FALSE;
981 for(i = 0; i < cbPoints; i++) {
982 pt = pts[i];
983 if(!LPtoDP(dc->hSelf, &pt, 1))
984 return FALSE;
985 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
986 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
987 PT_LINETO));
989 return TRUE;
992 BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
993 UINT polygons )
995 GdiPath *pPath = &dc->path;
996 POINT pt, startpt;
997 INT poly, point, i;
999 /* Check that path is open */
1000 if(pPath->state!=PATH_Open)
1001 return FALSE;
1003 for(i = 0, poly = 0; poly < polygons; poly++) {
1004 for(point = 0; point < counts[poly]; point++, i++) {
1005 pt = pts[i];
1006 if(!LPtoDP(dc->hSelf, &pt, 1))
1007 return FALSE;
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);
1014 return TRUE;
1017 BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1018 DWORD polylines )
1020 GdiPath *pPath = &dc->path;
1021 POINT pt;
1022 INT poly, point, i;
1024 /* Check that path is open */
1025 if(pPath->state!=PATH_Open)
1026 return FALSE;
1028 for(i = 0, poly = 0; poly < polylines; poly++) {
1029 for(point = 0; point < counts[poly]; point++, i++) {
1030 pt = pts[i];
1031 if(!LPtoDP(dc->hSelf, &pt, 1))
1032 return FALSE;
1033 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1036 return TRUE;
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)
1049 INT temp;
1051 /* Convert points to device coordinates */
1052 corners[0].x=x1;
1053 corners[0].y=y1;
1054 corners[1].x=x2;
1055 corners[1].y=y2;
1056 if(!LPtoDP(dc->hSelf, corners, 2))
1057 return FALSE;
1059 /* Make sure first corner is top left and second corner is bottom right */
1060 if(corners[0].x>corners[1].x)
1062 temp=corners[0].x;
1063 corners[0].x=corners[1].x;
1064 corners[1].x=temp;
1066 if(corners[0].y>corners[1].y)
1068 temp=corners[0].y;
1069 corners[0].y=corners[1].y;
1070 corners[1].y=temp;
1073 /* In GM_COMPATIBLE, don't include bottom and right edges */
1074 if(dc->GraphicsMode==GM_COMPATIBLE)
1076 corners[1].x--;
1077 corners[1].y--;
1080 return TRUE;
1083 /* PATH_AddFlatBezier
1085 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
1087 POINT *pts;
1088 INT no, i;
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 );
1097 return TRUE;
1100 /* PATH_FlattenPath
1102 * Replaces Beziers with line segments
1105 static BOOL PATH_FlattenPath(GdiPath *pPath)
1107 GdiPath newPath;
1108 INT srcpt;
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) {
1114 case PT_MOVETO:
1115 case PT_LINETO:
1116 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
1117 pPath->pFlags[srcpt]);
1118 break;
1119 case PT_BEZIERTO:
1120 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
1121 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
1122 srcpt += 2;
1123 break;
1126 newPath.state = PATH_Closed;
1127 PATH_AssignGdiPath(pPath, &newPath);
1128 PATH_DestroyGdiPath(&newPath);
1129 return TRUE;
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,
1141 HRGN *pHrgn)
1143 int numStrokes, iStroke, i;
1144 INT *pNumPointsInStroke;
1145 HRGN hrgn;
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 */
1156 numStrokes=0;
1157 for(i=0; i<pPath->numEntriesUsed; i++)
1158 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1159 numStrokes++;
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);
1167 return FALSE;
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)
1177 iStroke++;
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 );
1191 if(hrgn==NULL)
1193 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1194 return FALSE;
1197 /* Success! */
1198 *pHrgn=hrgn;
1199 return TRUE;
1202 /* PATH_EmptyPath
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;
1214 /* PATH_AddEntry
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)
1231 return FALSE;
1233 /* Reserve enough memory for an extra path entry */
1234 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
1235 return FALSE;
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++;
1248 return TRUE;
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;
1260 POINT *pPointsNew;
1261 BYTE *pFlagsNew;
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
1271 * complexity. */
1272 if(pPath->numEntriesAllocated)
1274 numEntriesToAllocate=pPath->numEntriesAllocated;
1275 while(numEntriesToAllocate<numEntries)
1276 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
1277 GROW_FACTOR_DENOM;
1279 else
1280 numEntriesToAllocate=numEntries;
1282 /* Allocate new arrays */
1283 pPointsNew=(POINT *)HeapAlloc( GetProcessHeap(), 0,
1284 numEntriesToAllocate * sizeof(POINT) );
1285 if(!pPointsNew)
1286 return FALSE;
1287 pFlagsNew=(BYTE *)HeapAlloc( GetProcessHeap(), 0,
1288 numEntriesToAllocate * sizeof(BYTE) );
1289 if(!pFlagsNew)
1291 HeapFree( GetProcessHeap(), 0, pPointsNew );
1292 return FALSE;
1295 /* Copy old arrays to new arrays and discard old arrays */
1296 if(pPath->pPoints)
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;
1313 return TRUE;
1316 /* PATH_DoArcPart
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];
1330 POINT point;
1331 int i;
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];
1351 else
1352 for(i=0; i<4; i++)
1354 xNorm[i]=cos(angleStart);
1355 yNorm[i]=sin(angleStart);
1358 /* Add starting point to path if desired */
1359 if(addMoveTo)
1361 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1362 if(!PATH_AddEntry(pPath, &point, PT_MOVETO))
1363 return FALSE;
1366 /* Add remaining control points */
1367 for(i=1; i<4; i++)
1369 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1370 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1371 return FALSE;
1374 return TRUE;
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) *
1403 2.0 - 1.0;
1404 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1405 2.0 - 1.0;
1409 /*******************************************************************
1410 * FlattenPath [GDI32.@]
1414 BOOL WINAPI FlattenPath(HDC hdc)
1416 BOOL ret = FALSE;
1417 DC *dc = DC_GetDCPtr( hdc );
1419 if(!dc) return FALSE;
1421 if(dc->funcs->pFlattenPath) ret = dc->funcs->pFlattenPath(dc->physDev);
1422 else
1424 GdiPath *pPath = &dc->path;
1425 if(pPath->state != PATH_Closed)
1426 ret = PATH_FlattenPath(pPath);
1428 GDI_ReleaseObj( hdc );
1429 return ret;
1433 static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1435 INT i;
1436 POINT ptLastMove = {0,0};
1437 POINT ptViewportOrg, ptWindowOrg;
1438 SIZE szViewportExt, szWindowExt;
1439 DWORD mapMode, graphicsMode;
1440 XFORM xform;
1441 BOOL ret = TRUE;
1443 if(dc->funcs->pStrokePath)
1444 return dc->funcs->pStrokePath(dc->physDev);
1446 if(pPath->state != PATH_Closed)
1447 return FALSE;
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);
1457 /* Set MM_TEXT */
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]) {
1468 case PT_MOVETO:
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];
1473 break;
1474 case PT_LINETO:
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);
1479 break;
1480 case PT_BEZIERTO:
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");
1485 ret = FALSE;
1486 goto end;
1488 PolyBezierTo(dc->hSelf, &pPath->pPoints[i], 3);
1489 i += 2;
1490 break;
1491 default:
1492 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1493 ret = FALSE;
1494 goto end;
1496 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1497 LineTo(dc->hSelf, ptLastMove.x, ptLastMove.y);
1500 end:
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
1519 mode.
1521 if(i > 0) {
1522 POINT pt;
1523 GetCurrentPositionEx(dc->hSelf, &pt);
1524 DPtoLP(dc->hSelf, &pt, 1);
1525 MoveToEx(dc->hSelf, pt.x, pt.y, NULL);
1527 return ret;
1531 /*******************************************************************
1532 * StrokeAndFillPath [GDI32.@]
1536 BOOL WINAPI StrokeAndFillPath(HDC hdc)
1538 DC *dc = DC_GetDCPtr( hdc );
1539 BOOL bRet = FALSE;
1541 if(!dc) return FALSE;
1543 if(dc->funcs->pStrokeAndFillPath)
1544 bRet = dc->funcs->pStrokeAndFillPath(dc->physDev);
1545 else
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 );
1552 return bRet;
1556 /*******************************************************************
1557 * StrokePath [GDI32.@]
1561 BOOL WINAPI StrokePath(HDC hdc)
1563 DC *dc = DC_GetDCPtr( hdc );
1564 GdiPath *pPath;
1565 BOOL bRet = FALSE;
1567 TRACE("(%p)\n", hdc);
1568 if(!dc) return FALSE;
1570 if(dc->funcs->pStrokePath)
1571 bRet = dc->funcs->pStrokePath(dc->physDev);
1572 else
1574 pPath = &dc->path;
1575 bRet = PATH_StrokePath(dc, pPath);
1576 PATH_EmptyPath(pPath);
1578 GDI_ReleaseObj( hdc );
1579 return bRet;
1583 /*******************************************************************
1584 * WidenPath [GDI32.@]
1588 BOOL WINAPI WidenPath(HDC hdc)
1590 DC *dc = DC_GetDCPtr( hdc );
1591 BOOL ret = FALSE;
1593 if(!dc) return FALSE;
1595 if(dc->funcs->pWidenPath)
1596 ret = dc->funcs->pWidenPath(dc->physDev);
1598 FIXME("stub\n");
1599 GDI_ReleaseObj( hdc );
1600 return ret;