Fix use of LOCALE_NOUSEROVERRIDE flag in GetNumberFormatA and
[wine.git] / graphics / path.c
blob522066d095867a0b7e9462add09b2f458f2c4818
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"
24 #include <assert.h>
25 #include <math.h>
26 #include <string.h>
27 #if defined(HAVE_FLOAT_H)
28 #include <float.h>
29 #endif
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winerror.h"
35 #include "gdi.h"
36 #include "wine/debug.h"
37 #include "path.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
56 * exponentially.
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.
75 * Martin Boehme
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,
86 HRGN *pHrgn);
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 /***********************************************************************
99 * BeginPath (GDI.512)
101 BOOL16 WINAPI BeginPath16(HDC16 hdc)
103 return (BOOL16)BeginPath((HDC)hdc);
107 /***********************************************************************
108 * BeginPath (GDI32.@)
110 BOOL WINAPI BeginPath(HDC hdc)
112 BOOL ret = TRUE;
113 DC *dc = DC_GetDCPtr( hdc );
115 if(!dc) return FALSE;
117 if(dc->funcs->pBeginPath)
118 ret = dc->funcs->pBeginPath(dc->physDev);
119 else
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 );
133 return ret;
137 /***********************************************************************
138 * EndPath (GDI.514)
140 BOOL16 WINAPI EndPath16(HDC16 hdc)
142 return (BOOL16)EndPath((HDC)hdc);
146 /***********************************************************************
147 * EndPath (GDI32.@)
149 BOOL WINAPI EndPath(HDC hdc)
151 BOOL ret = TRUE;
152 DC *dc = DC_GetDCPtr( hdc );
154 if(!dc) return FALSE;
156 if(dc->funcs->pEndPath)
157 ret = dc->funcs->pEndPath(dc->physDev);
158 else
160 /* Check that path is currently being constructed */
161 if(dc->path.state!=PATH_Open)
163 SetLastError(ERROR_CAN_NOT_COMPLETE);
164 ret = FALSE;
166 /* Set flag to indicate that path is finished */
167 else dc->path.state=PATH_Closed;
169 GDI_ReleaseObj( hdc );
170 return ret;
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
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 (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)
227 BOOL ret = TRUE;
228 DC *dc = DC_GetDCPtr( hdc );
230 if(!dc) return FALSE;
232 if(dc->funcs->pCloseFigure)
233 ret = dc->funcs->pCloseFigure(dc->physDev);
234 else
236 /* Check that path is open */
237 if(dc->path.state!=PATH_Open)
239 SetLastError(ERROR_CAN_NOT_COMPLETE);
240 ret = FALSE;
242 else
244 /* FIXME: Shouldn't we draw a line to the beginning of the
245 figure? */
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 );
255 return ret;
259 /***********************************************************************
260 * GetPath (GDI.517)
262 INT16 WINAPI GetPath16(HDC16 hdc, LPPOINT16 pPoints, LPBYTE pTypes,
263 INT16 nSize)
265 FIXME("(%d,%p,%p): stub\n",hdc,pPoints,pTypes);
267 return 0;
271 /***********************************************************************
272 * GetPath (GDI32.@)
274 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
275 INT nSize)
277 INT ret = -1;
278 GdiPath *pPath;
279 DC *dc = DC_GetDCPtr( hdc );
281 if(!dc) return -1;
283 pPath = &dc->path;
285 /* Check that path is closed */
286 if(pPath->state!=PATH_Closed)
288 SetLastError(ERROR_CAN_NOT_COMPLETE);
289 goto done;
292 if(nSize==0)
293 ret = pPath->numEntriesUsed;
294 else if(nSize<pPath->numEntriesUsed)
296 SetLastError(ERROR_INVALID_PARAMETER);
297 goto done;
299 else
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);
309 goto done;
311 else ret = pPath->numEntriesUsed;
313 done:
314 GDI_ReleaseObj( hdc );
315 return ret;
318 /***********************************************************************
319 * PathToRegion (GDI.518)
321 HRGN16 WINAPI PathToRegion16(HDC16 hdc)
323 return (HRGN16) PathToRegion((HDC) hdc);
326 /***********************************************************************
327 * PathToRegion (GDI32.@)
329 * FIXME
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)
337 GdiPath *pPath;
338 HRGN hrgnRval = 0;
339 DC *dc = DC_GetDCPtr( hdc );
341 /* Get pointer to path */
342 if(!dc) return -1;
344 pPath = &dc->path;
346 /* Check that path is closed */
347 if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
348 else
350 /* FIXME: Should we empty the path even if conversion failed? */
351 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
352 PATH_EmptyPath(pPath);
353 else
354 hrgnRval=0;
356 GDI_ReleaseObj( hdc );
357 return hrgnRval;
360 static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
362 INT mapMode, graphicsMode;
363 SIZE ptViewportExt, ptWindowExt;
364 POINT ptViewportOrg, ptWindowOrg;
365 XFORM xform;
366 HRGN hrgn;
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);
375 return FALSE;
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);
404 /* Set MM_TEXT */
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);
411 DeleteObject(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);
424 return TRUE;
426 return FALSE;
429 /***********************************************************************
430 * FillPath (GDI.515)
432 BOOL16 WINAPI FillPath16(HDC16 hdc)
434 return (BOOL16) FillPath((HDC) hdc);
437 /***********************************************************************
438 * FillPath (GDI32.@)
440 * FIXME
441 * Check that SetLastError is being called correctly
443 BOOL WINAPI FillPath(HDC hdc)
445 DC *dc = DC_GetDCPtr( hdc );
446 BOOL bRet = FALSE;
448 if(!dc) return FALSE;
450 if(dc->funcs->pFillPath)
451 bRet = dc->funcs->pFillPath(dc->physDev);
452 else
454 bRet = PATH_FillPath(dc, &dc->path);
455 if(bRet)
457 /* FIXME: Should the path be emptied even if conversion
458 failed? */
459 PATH_EmptyPath(&dc->path);
462 GDI_ReleaseObj( hdc );
463 return bRet;
466 /***********************************************************************
467 * SelectClipPath (GDI.519)
469 BOOL16 WINAPI SelectClipPath16(HDC16 hdc, INT16 iMode)
471 return (BOOL16) SelectClipPath((HDC) hdc, iMode);
474 /***********************************************************************
475 * SelectClipPath (GDI32.@)
476 * FIXME
477 * Check that SetLastError is being called correctly
479 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
481 GdiPath *pPath;
482 HRGN hrgnPath;
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);
490 else
492 pPath = &dc->path;
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);
503 /* Empty the path */
504 if(success)
505 PATH_EmptyPath(pPath);
506 /* FIXME: Should this function delete the path even if it failed? */
509 GDI_ReleaseObj( hdc );
510 return success;
514 /***********************************************************************
515 * Exported functions
518 /* PATH_InitGdiPath
520 * Initializes the GdiPath structure.
522 void PATH_InitGdiPath(GdiPath *pPath)
524 assert(pPath!=NULL);
526 pPath->state=PATH_Null;
527 pPath->pPoints=NULL;
528 pPath->pFlags=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)
539 assert(pPath!=NULL);
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))
561 return FALSE;
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;
573 return TRUE;
576 /* PATH_MoveTo
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
580 * FALSE.
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? */
589 return FALSE;
591 /* Start a new stroke */
592 pPath->newStroke=TRUE;
594 return TRUE;
597 /* PATH_LineTo
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)
611 return FALSE;
613 /* Convert point to device coordinates */
614 point.x=x;
615 point.y=y;
616 if(!LPtoDP(dc->hSelf, &point, 1))
617 return FALSE;
619 /* Add a PT_MOVETO if necessary */
620 if(pPath->newStroke)
622 pPath->newStroke=FALSE;
623 pointCurPos.x = dc->CursPosX;
624 pointCurPos.y = dc->CursPosY;
625 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
626 return FALSE;
627 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
628 return FALSE;
631 /* Add a PT_LINETO entry */
632 return PATH_AddEntry(pPath, &point, PT_LINETO);
635 /* PATH_RoundRect
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)
652 return FALSE;
654 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
655 return FALSE;
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))
663 return FALSE;
664 pointTemp.x = corners[0].x+ell_width/2;
665 pointTemp.y = corners[0].y;
666 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
667 return FALSE;
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))
671 return FALSE;
672 pointTemp.x = corners[0].x;
673 pointTemp.y = corners[1].y-ell_height/2;
674 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
675 return FALSE;
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))
679 return FALSE;
680 pointTemp.x = corners[1].x-ell_width/2;
681 pointTemp.y = corners[1].y;
682 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
683 return FALSE;
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))
687 return FALSE;
689 /* Close the roundrect figure */
690 if(!CloseFigure(dc->hSelf))
691 return FALSE;
693 return TRUE;
696 /* PATH_Rectangle
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)
708 return FALSE;
710 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
711 return FALSE;
713 /* Close any previous figure */
714 if(!CloseFigure(dc->hSelf))
716 /* The CloseFigure call shouldn't have failed */
717 assert(FALSE);
718 return FALSE;
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))
725 return FALSE;
726 if(!PATH_AddEntry(pPath, corners, PT_LINETO))
727 return FALSE;
728 pointTemp.x=corners[0].x;
729 pointTemp.y=corners[1].y;
730 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
731 return FALSE;
732 if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
733 return FALSE;
735 /* Close the rectangle figure */
736 if(!CloseFigure(dc->hSelf))
738 /* The CloseFigure call shouldn't have failed */
739 assert(FALSE);
740 return FALSE;
743 return TRUE;
746 /* PATH_Ellipse
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) );
758 /* PATH_Arc
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 */
772 double x, y;
773 FLOAT_POINT corners[2], pointStart, pointEnd;
774 POINT centre;
775 BOOL start, end;
776 INT temp;
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)
783 return FALSE;
785 /* Check for zero height / width */
786 /* FIXME: Only in GM_COMPATIBLE? */
787 if(x1==x2 || y1==y2)
788 return TRUE;
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)
807 temp=corners[0].x;
808 corners[0].x=corners[1].x;
809 corners[1].x=temp;
811 if(corners[0].y>corners[1].y)
813 temp=corners[0].y;
814 corners[0].y=corners[1].y;
815 corners[1].y=temp;
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)
829 angleEnd+=2*M_PI;
830 assert(angleEnd>=angleStart);
833 else
835 if(angleEnd>=angleStart)
837 angleEnd-=2*M_PI;
838 assert(angleEnd<=angleStart);
842 /* In GM_COMPATIBLE, don't include bottom and right edges */
843 if(dc->GraphicsMode==GM_COMPATIBLE)
845 corners[1].x--;
846 corners[1].y--;
849 /* Add the arc to the path with one Bezier spline per quadrant that the
850 * arc spans */
851 start=TRUE;
852 end=FALSE;
855 /* Determine the start and end angles for this quadrant */
856 if(start)
858 angleStartQuadrant=angleStart;
859 if(dc->ArcDirection==AD_CLOCKWISE)
860 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
861 else
862 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
864 else
866 angleStartQuadrant=angleEndQuadrant;
867 if(dc->ArcDirection==AD_CLOCKWISE)
868 angleEndQuadrant+=M_PI_2;
869 else
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;
881 end=TRUE;
884 /* Add the Bezier spline to the path */
885 PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
886 start);
887 start=FALSE;
888 } while(!end);
890 /* chord: close figure. pie: add line and close figure */
891 if(lines==1)
893 if(!CloseFigure(dc->hSelf))
894 return FALSE;
896 else if(lines==2)
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, &centre, PT_LINETO | PT_CLOSEFIGURE))
901 return FALSE;
904 return TRUE;
907 BOOL PATH_PolyBezierTo(DC *dc, const POINT *pts, DWORD cbPoints)
909 GdiPath *pPath = &dc->path;
910 POINT pt;
911 INT i;
913 /* Check that path is open */
914 if(pPath->state!=PATH_Open)
915 return FALSE;
917 /* Add a PT_MOVETO if necessary */
918 if(pPath->newStroke)
920 pPath->newStroke=FALSE;
921 pt.x = dc->CursPosX;
922 pt.y = dc->CursPosY;
923 if(!LPtoDP(dc->hSelf, &pt, 1))
924 return FALSE;
925 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
926 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, PT_BEZIERTO);
935 return TRUE;
938 BOOL PATH_PolyBezier(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 for(i = 0; i < cbPoints; i++) {
949 pt = pts[i];
950 if(!LPtoDP(dc->hSelf, &pt, 1))
951 return FALSE;
952 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
954 return TRUE;
957 BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
959 GdiPath *pPath = &dc->path;
960 POINT pt;
961 INT i;
963 /* Check that path is open */
964 if(pPath->state!=PATH_Open)
965 return FALSE;
967 for(i = 0; i < cbPoints; i++) {
968 pt = pts[i];
969 if(!LPtoDP(dc->hSelf, &pt, 1))
970 return FALSE;
971 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
973 return TRUE;
976 BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
978 GdiPath *pPath = &dc->path;
979 POINT pt;
980 INT i;
982 /* Check that path is open */
983 if(pPath->state!=PATH_Open)
984 return FALSE;
986 /* Add a PT_MOVETO if necessary */
987 if(pPath->newStroke)
989 pPath->newStroke=FALSE;
990 pt.x = dc->CursPosX;
991 pt.y = dc->CursPosY;
992 if(!LPtoDP(dc->hSelf, &pt, 1))
993 return FALSE;
994 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
995 return FALSE;
998 for(i = 0; i < cbPoints; i++) {
999 pt = pts[i];
1000 if(!LPtoDP(dc->hSelf, &pt, 1))
1001 return FALSE;
1002 PATH_AddEntry(pPath, &pt, PT_LINETO);
1005 return TRUE;
1009 BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
1011 GdiPath *pPath = &dc->path;
1012 POINT pt;
1013 INT i;
1015 /* Check that path is open */
1016 if(pPath->state!=PATH_Open)
1017 return FALSE;
1019 for(i = 0; i < cbPoints; i++) {
1020 pt = pts[i];
1021 if(!LPtoDP(dc->hSelf, &pt, 1))
1022 return FALSE;
1023 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
1024 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
1025 PT_LINETO));
1027 return TRUE;
1030 BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
1031 UINT polygons )
1033 GdiPath *pPath = &dc->path;
1034 POINT pt, startpt;
1035 INT poly, point, i;
1037 /* Check that path is open */
1038 if(pPath->state!=PATH_Open)
1039 return FALSE;
1041 for(i = 0, poly = 0; poly < polygons; poly++) {
1042 for(point = 0; point < counts[poly]; point++, i++) {
1043 pt = pts[i];
1044 if(!LPtoDP(dc->hSelf, &pt, 1))
1045 return FALSE;
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);
1052 return TRUE;
1055 BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1056 DWORD polylines )
1058 GdiPath *pPath = &dc->path;
1059 POINT pt;
1060 INT poly, point, i;
1062 /* Check that path is open */
1063 if(pPath->state!=PATH_Open)
1064 return FALSE;
1066 for(i = 0, poly = 0; poly < polylines; poly++) {
1067 for(point = 0; point < counts[poly]; point++, i++) {
1068 pt = pts[i];
1069 if(!LPtoDP(dc->hSelf, &pt, 1))
1070 return FALSE;
1071 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1074 return TRUE;
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)
1087 INT temp;
1089 /* Convert points to device coordinates */
1090 corners[0].x=x1;
1091 corners[0].y=y1;
1092 corners[1].x=x2;
1093 corners[1].y=y2;
1094 if(!LPtoDP(dc->hSelf, corners, 2))
1095 return FALSE;
1097 /* Make sure first corner is top left and second corner is bottom right */
1098 if(corners[0].x>corners[1].x)
1100 temp=corners[0].x;
1101 corners[0].x=corners[1].x;
1102 corners[1].x=temp;
1104 if(corners[0].y>corners[1].y)
1106 temp=corners[0].y;
1107 corners[0].y=corners[1].y;
1108 corners[1].y=temp;
1111 /* In GM_COMPATIBLE, don't include bottom and right edges */
1112 if(dc->GraphicsMode==GM_COMPATIBLE)
1114 corners[1].x--;
1115 corners[1].y--;
1118 return TRUE;
1121 /* PATH_AddFlatBezier
1123 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
1125 POINT *pts;
1126 INT no, i;
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 );
1135 return TRUE;
1138 /* PATH_FlattenPath
1140 * Replaces Beziers with line segments
1143 static BOOL PATH_FlattenPath(GdiPath *pPath)
1145 GdiPath newPath;
1146 INT srcpt;
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) {
1152 case PT_MOVETO:
1153 case PT_LINETO:
1154 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
1155 pPath->pFlags[srcpt]);
1156 break;
1157 case PT_BEZIERTO:
1158 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
1159 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
1160 srcpt += 2;
1161 break;
1164 newPath.state = PATH_Closed;
1165 PATH_AssignGdiPath(pPath, &newPath);
1166 PATH_EmptyPath(&newPath);
1167 return TRUE;
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,
1179 HRGN *pHrgn)
1181 int numStrokes, iStroke, i;
1182 INT *pNumPointsInStroke;
1183 HRGN hrgn;
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 */
1194 numStrokes=0;
1195 for(i=0; i<pPath->numEntriesUsed; i++)
1196 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1197 numStrokes++;
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);
1205 return FALSE;
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)
1215 iStroke++;
1216 pNumPointsInStroke[iStroke]=0;
1219 pNumPointsInStroke[iStroke]++;
1222 /* Create a region from the strokes */
1223 hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
1224 numStrokes, nPolyFillMode);
1225 if(hrgn==(HRGN)0)
1227 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1228 return FALSE;
1231 /* Free memory for number-of-points-in-stroke array */
1232 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
1234 /* Success! */
1235 *pHrgn=hrgn;
1236 return TRUE;
1239 /* PATH_EmptyPath
1241 * Removes all entries from the path and sets the path state to PATH_Null.
1243 static void PATH_EmptyPath(GdiPath *pPath)
1245 assert(pPath!=NULL);
1247 pPath->state=PATH_Null;
1248 pPath->numEntriesUsed=0;
1251 /* PATH_AddEntry
1253 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1254 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1255 * successful, FALSE otherwise (e.g. if not enough memory was available).
1257 BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
1259 assert(pPath!=NULL);
1261 /* FIXME: If newStroke is true, perhaps we want to check that we're
1262 * getting a PT_MOVETO
1264 TRACE("(%ld,%ld) - %d\n", pPoint->x, pPoint->y, flags);
1266 /* Check that path is open */
1267 if(pPath->state!=PATH_Open)
1268 return FALSE;
1270 /* Reserve enough memory for an extra path entry */
1271 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
1272 return FALSE;
1274 /* Store information in path entry */
1275 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
1276 pPath->pFlags[pPath->numEntriesUsed]=flags;
1278 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1279 if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
1280 pPath->newStroke=TRUE;
1282 /* Increment entry count */
1283 pPath->numEntriesUsed++;
1285 return TRUE;
1288 /* PATH_ReserveEntries
1290 * Ensures that at least "numEntries" entries (for points and flags) have
1291 * been allocated; allocates larger arrays and copies the existing entries
1292 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1294 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries)
1296 INT numEntriesToAllocate;
1297 POINT *pPointsNew;
1298 BYTE *pFlagsNew;
1300 assert(pPath!=NULL);
1301 assert(numEntries>=0);
1303 /* Do we have to allocate more memory? */
1304 if(numEntries > pPath->numEntriesAllocated)
1306 /* Find number of entries to allocate. We let the size of the array
1307 * grow exponentially, since that will guarantee linear time
1308 * complexity. */
1309 if(pPath->numEntriesAllocated)
1311 numEntriesToAllocate=pPath->numEntriesAllocated;
1312 while(numEntriesToAllocate<numEntries)
1313 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
1314 GROW_FACTOR_DENOM;
1316 else
1317 numEntriesToAllocate=numEntries;
1319 /* Allocate new arrays */
1320 pPointsNew=(POINT *)HeapAlloc( GetProcessHeap(), 0,
1321 numEntriesToAllocate * sizeof(POINT) );
1322 if(!pPointsNew)
1323 return FALSE;
1324 pFlagsNew=(BYTE *)HeapAlloc( GetProcessHeap(), 0,
1325 numEntriesToAllocate * sizeof(BYTE) );
1326 if(!pFlagsNew)
1328 HeapFree( GetProcessHeap(), 0, pPointsNew );
1329 return FALSE;
1332 /* Copy old arrays to new arrays and discard old arrays */
1333 if(pPath->pPoints)
1335 assert(pPath->pFlags);
1337 memcpy(pPointsNew, pPath->pPoints,
1338 sizeof(POINT)*pPath->numEntriesUsed);
1339 memcpy(pFlagsNew, pPath->pFlags,
1340 sizeof(BYTE)*pPath->numEntriesUsed);
1342 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
1343 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
1345 pPath->pPoints=pPointsNew;
1346 pPath->pFlags=pFlagsNew;
1347 pPath->numEntriesAllocated=numEntriesToAllocate;
1350 return TRUE;
1353 /* PATH_DoArcPart
1355 * Creates a Bezier spline that corresponds to part of an arc and appends the
1356 * corresponding points to the path. The start and end angles are passed in
1357 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1358 * at most. If "addMoveTo" is true, a PT_MOVETO entry for the first control
1359 * point is added to the path; otherwise, it is assumed that the current
1360 * position is equal to the first control point.
1362 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
1363 double angleStart, double angleEnd, BOOL addMoveTo)
1365 double halfAngle, a;
1366 double xNorm[4], yNorm[4];
1367 POINT point;
1368 int i;
1370 assert(fabs(angleEnd-angleStart)<=M_PI_2);
1372 /* FIXME: Is there an easier way of computing this? */
1374 /* Compute control points */
1375 halfAngle=(angleEnd-angleStart)/2.0;
1376 if(fabs(halfAngle)>1e-8)
1378 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
1379 xNorm[0]=cos(angleStart);
1380 yNorm[0]=sin(angleStart);
1381 xNorm[1]=xNorm[0] - a*yNorm[0];
1382 yNorm[1]=yNorm[0] + a*xNorm[0];
1383 xNorm[3]=cos(angleEnd);
1384 yNorm[3]=sin(angleEnd);
1385 xNorm[2]=xNorm[3] + a*yNorm[3];
1386 yNorm[2]=yNorm[3] - a*xNorm[3];
1388 else
1389 for(i=0; i<4; i++)
1391 xNorm[i]=cos(angleStart);
1392 yNorm[i]=sin(angleStart);
1395 /* Add starting point to path if desired */
1396 if(addMoveTo)
1398 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1399 if(!PATH_AddEntry(pPath, &point, PT_MOVETO))
1400 return FALSE;
1403 /* Add remaining control points */
1404 for(i=1; i<4; i++)
1406 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1407 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1408 return FALSE;
1411 return TRUE;
1414 /* PATH_ScaleNormalizedPoint
1416 * Scales a normalized point (x, y) with respect to the box whose corners are
1417 * passed in "corners". The point is stored in "*pPoint". The normalized
1418 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1419 * (1.0, 1.0) correspond to corners[1].
1421 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
1422 double y, POINT *pPoint)
1424 pPoint->x=GDI_ROUND( (double)corners[0].x +
1425 (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
1426 pPoint->y=GDI_ROUND( (double)corners[0].y +
1427 (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
1430 /* PATH_NormalizePoint
1432 * Normalizes a point with respect to the box whose corners are passed in
1433 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1435 static void PATH_NormalizePoint(FLOAT_POINT corners[],
1436 const FLOAT_POINT *pPoint,
1437 double *pX, double *pY)
1439 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) *
1440 2.0 - 1.0;
1441 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1442 2.0 - 1.0;
1445 /*******************************************************************
1446 * FlattenPath [GDI.516]
1450 BOOL16 WINAPI FlattenPath16(HDC16 hdc)
1452 return (BOOL16) FlattenPath((HDC) hdc);
1455 /*******************************************************************
1456 * FlattenPath [GDI32.@]
1460 BOOL WINAPI FlattenPath(HDC hdc)
1462 BOOL ret = FALSE;
1463 DC *dc = DC_GetDCPtr( hdc );
1465 if(!dc) return FALSE;
1467 if(dc->funcs->pFlattenPath) ret = dc->funcs->pFlattenPath(dc->physDev);
1468 else
1470 GdiPath *pPath = &dc->path;
1471 if(pPath->state != PATH_Closed)
1472 ret = PATH_FlattenPath(pPath);
1474 GDI_ReleaseObj( hdc );
1475 return ret;
1479 static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1481 INT i;
1482 POINT ptLastMove = {0,0};
1483 POINT ptViewportOrg, ptWindowOrg;
1484 SIZE szViewportExt, szWindowExt;
1485 DWORD mapMode, graphicsMode;
1486 XFORM xform;
1487 BOOL ret = TRUE;
1489 if(dc->funcs->pStrokePath)
1490 return dc->funcs->pStrokePath(dc->physDev);
1492 if(pPath->state != PATH_Closed)
1493 return FALSE;
1495 /* Save the mapping mode info */
1496 mapMode=GetMapMode(dc->hSelf);
1497 GetViewportExtEx(dc->hSelf, &szViewportExt);
1498 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
1499 GetWindowExtEx(dc->hSelf, &szWindowExt);
1500 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
1501 GetWorldTransform(dc->hSelf, &xform);
1503 /* Set MM_TEXT */
1504 SetMapMode(dc->hSelf, MM_TEXT);
1505 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
1506 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
1509 for(i = 0; i < pPath->numEntriesUsed; i++) {
1510 switch(pPath->pFlags[i]) {
1511 case PT_MOVETO:
1512 TRACE("Got PT_MOVETO (%ld, %ld)\n",
1513 pPath->pPoints[i].x, pPath->pPoints[i].y);
1514 MoveToEx(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y, NULL);
1515 ptLastMove = pPath->pPoints[i];
1516 break;
1517 case PT_LINETO:
1518 case (PT_LINETO | PT_CLOSEFIGURE):
1519 TRACE("Got PT_LINETO (%ld, %ld)\n",
1520 pPath->pPoints[i].x, pPath->pPoints[i].y);
1521 LineTo(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y);
1522 break;
1523 case PT_BEZIERTO:
1524 TRACE("Got PT_BEZIERTO\n");
1525 if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1526 (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1527 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1528 ret = FALSE;
1529 goto end;
1531 PolyBezierTo(dc->hSelf, &pPath->pPoints[i], 3);
1532 i += 2;
1533 break;
1534 default:
1535 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1536 ret = FALSE;
1537 goto end;
1539 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1540 LineTo(dc->hSelf, ptLastMove.x, ptLastMove.y);
1543 end:
1545 /* Restore the old mapping mode */
1546 SetMapMode(dc->hSelf, mapMode);
1547 SetViewportExtEx(dc->hSelf, szViewportExt.cx, szViewportExt.cy, NULL);
1548 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
1549 SetWindowExtEx(dc->hSelf, szWindowExt.cx, szWindowExt.cy, NULL);
1550 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
1552 /* Go to GM_ADVANCED temporarily to restore the world transform */
1553 graphicsMode=GetGraphicsMode(dc->hSelf);
1554 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1555 SetWorldTransform(dc->hSelf, &xform);
1556 SetGraphicsMode(dc->hSelf, graphicsMode);
1557 return ret;
1561 /*******************************************************************
1562 * StrokeAndFillPath [GDI.520]
1566 BOOL16 WINAPI StrokeAndFillPath16(HDC16 hdc)
1568 return (BOOL16) StrokeAndFillPath((HDC) hdc);
1571 /*******************************************************************
1572 * StrokeAndFillPath [GDI32.@]
1576 BOOL WINAPI StrokeAndFillPath(HDC hdc)
1578 DC *dc = DC_GetDCPtr( hdc );
1579 BOOL bRet = FALSE;
1581 if(!dc) return FALSE;
1583 if(dc->funcs->pStrokeAndFillPath)
1584 bRet = dc->funcs->pStrokeAndFillPath(dc->physDev);
1585 else
1587 bRet = PATH_FillPath(dc, &dc->path);
1588 if(bRet) bRet = PATH_StrokePath(dc, &dc->path);
1589 if(bRet) PATH_EmptyPath(&dc->path);
1591 GDI_ReleaseObj( hdc );
1592 return bRet;
1595 /*******************************************************************
1596 * StrokePath [GDI.521]
1600 BOOL16 WINAPI StrokePath16(HDC16 hdc)
1602 return (BOOL16) StrokePath((HDC) hdc);
1605 /*******************************************************************
1606 * StrokePath [GDI32.@]
1610 BOOL WINAPI StrokePath(HDC hdc)
1612 DC *dc = DC_GetDCPtr( hdc );
1613 GdiPath *pPath;
1614 BOOL bRet = FALSE;
1616 TRACE("(%08x)\n", hdc);
1617 if(!dc) return FALSE;
1619 if(dc->funcs->pStrokePath)
1620 bRet = dc->funcs->pStrokePath(dc->physDev);
1621 else
1623 pPath = &dc->path;
1624 bRet = PATH_StrokePath(dc, pPath);
1625 PATH_EmptyPath(pPath);
1627 GDI_ReleaseObj( hdc );
1628 return bRet;
1631 /*******************************************************************
1632 * WidenPath [GDI.522]
1636 BOOL16 WINAPI WidenPath16(HDC16 hdc)
1638 return (BOOL16) WidenPath((HDC) hdc);
1641 /*******************************************************************
1642 * WidenPath [GDI32.@]
1646 BOOL WINAPI WidenPath(HDC hdc)
1648 DC *dc = DC_GetDCPtr( hdc );
1649 BOOL ret = FALSE;
1651 if(!dc) return FALSE;
1653 if(dc->funcs->pWidenPath)
1654 ret = dc->funcs->pWidenPath(dc->physDev);
1656 FIXME("stub\n");
1657 GDI_ReleaseObj( hdc );
1658 return ret;