Correct Word breaking in centred/right justified mode; it was leaving a
[wine/dcerpc.git] / graphics / path.c
blob9841a8ce4fd5364c317a5e09d69a9aeb0bd027c1
1 /*
2 * Graphics paths (BeginPath, EndPath etc.)
4 * Copyright 1997, 1998 Martin Boehme
5 * 1999 Huw D M Davies
6 */
8 #include "config.h"
10 #include <assert.h>
11 #include <math.h>
12 #include <string.h>
13 #if defined(HAVE_FLOAT_H)
14 #include <float.h>
15 #endif
17 #include "winbase.h"
18 #include "wingdi.h"
19 #include "winerror.h"
21 #include "gdi.h"
22 #include "debugtools.h"
23 #include "path.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
42 * exponentially.
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.
61 * Martin Boehme
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,
72 HRGN *pHrgn);
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 /***********************************************************************
85 * BeginPath (GDI.512)
87 BOOL16 WINAPI BeginPath16(HDC16 hdc)
89 return (BOOL16)BeginPath((HDC)hdc);
93 /***********************************************************************
94 * BeginPath (GDI32.@)
96 BOOL WINAPI BeginPath(HDC hdc)
98 BOOL ret = TRUE;
99 DC *dc = DC_GetDCPtr( hdc );
101 if(!dc) return FALSE;
103 if(dc->funcs->pBeginPath)
104 ret = dc->funcs->pBeginPath(dc);
105 else
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 );
119 return ret;
123 /***********************************************************************
124 * EndPath (GDI.514)
126 BOOL16 WINAPI EndPath16(HDC16 hdc)
128 return (BOOL16)EndPath((HDC)hdc);
132 /***********************************************************************
133 * EndPath (GDI32.@)
135 BOOL WINAPI EndPath(HDC hdc)
137 BOOL ret = TRUE;
138 DC *dc = DC_GetDCPtr( hdc );
140 if(!dc) return FALSE;
142 if(dc->funcs->pEndPath)
143 ret = dc->funcs->pEndPath(dc);
144 else
146 /* Check that path is currently being constructed */
147 if(dc->path.state!=PATH_Open)
149 SetLastError(ERROR_CAN_NOT_COMPLETE);
150 ret = FALSE;
152 /* Set flag to indicate that path is finished */
153 else dc->path.state=PATH_Closed;
155 GDI_ReleaseObj( hdc );
156 return ret;
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
173 * NOTES
174 * Check that SetLastError is being called correctly
176 * PARAMS
177 * hdc [I] Handle to device context
179 * RETURNS STD
181 BOOL WINAPI AbortPath( HDC hdc )
183 BOOL ret = TRUE;
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 );
193 return ret;
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)
213 BOOL ret = TRUE;
214 DC *dc = DC_GetDCPtr( hdc );
216 if(!dc) return FALSE;
218 if(dc->funcs->pCloseFigure)
219 ret = dc->funcs->pCloseFigure(dc);
220 else
222 /* Check that path is open */
223 if(dc->path.state!=PATH_Open)
225 SetLastError(ERROR_CAN_NOT_COMPLETE);
226 ret = FALSE;
228 else
230 /* FIXME: Shouldn't we draw a line to the beginning of the
231 figure? */
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 );
241 return ret;
245 /***********************************************************************
246 * GetPath (GDI.517)
248 INT16 WINAPI GetPath16(HDC16 hdc, LPPOINT16 pPoints, LPBYTE pTypes,
249 INT16 nSize)
251 FIXME("(%d,%p,%p): stub\n",hdc,pPoints,pTypes);
253 return 0;
257 /***********************************************************************
258 * GetPath (GDI32.@)
260 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
261 INT nSize)
263 INT ret = -1;
264 GdiPath *pPath;
265 DC *dc = DC_GetDCPtr( hdc );
267 if(!dc) return -1;
269 pPath = &dc->path;
271 /* Check that path is closed */
272 if(pPath->state!=PATH_Closed)
274 SetLastError(ERROR_CAN_NOT_COMPLETE);
275 goto done;
278 if(nSize==0)
279 ret = pPath->numEntriesUsed;
280 else if(nSize<pPath->numEntriesUsed)
282 SetLastError(ERROR_INVALID_PARAMETER);
283 goto done;
285 else
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);
295 goto done;
297 else ret = pPath->numEntriesUsed;
299 done:
300 GDI_ReleaseObj( hdc );
301 return ret;
304 /***********************************************************************
305 * PathToRegion (GDI.518)
307 HRGN16 WINAPI PathToRegion16(HDC16 hdc)
309 return (HRGN16) PathToRegion((HDC) hdc);
312 /***********************************************************************
313 * PathToRegion (GDI32.@)
315 * FIXME
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)
323 GdiPath *pPath;
324 HRGN hrgnRval = 0;
325 DC *dc = DC_GetDCPtr( hdc );
327 /* Get pointer to path */
328 if(!dc) return -1;
330 pPath = &dc->path;
332 /* Check that path is closed */
333 if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
334 else
336 /* FIXME: Should we empty the path even if conversion failed? */
337 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
338 PATH_EmptyPath(pPath);
339 else
340 hrgnRval=0;
342 GDI_ReleaseObj( hdc );
343 return hrgnRval;
346 static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
348 INT mapMode, graphicsMode;
349 SIZE ptViewportExt, ptWindowExt;
350 POINT ptViewportOrg, ptWindowOrg;
351 XFORM xform;
352 HRGN hrgn;
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);
361 return FALSE;
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);
390 /* Set MM_TEXT */
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);
397 DeleteObject(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);
410 return TRUE;
412 return FALSE;
415 /***********************************************************************
416 * FillPath (GDI.515)
418 BOOL16 WINAPI FillPath16(HDC16 hdc)
420 return (BOOL16) FillPath((HDC) hdc);
423 /***********************************************************************
424 * FillPath (GDI32.@)
426 * FIXME
427 * Check that SetLastError is being called correctly
429 BOOL WINAPI FillPath(HDC hdc)
431 DC *dc = DC_GetDCPtr( hdc );
432 BOOL bRet = FALSE;
434 if(!dc) return FALSE;
436 if(dc->funcs->pFillPath)
437 bRet = dc->funcs->pFillPath(dc);
438 else
440 bRet = PATH_FillPath(dc, &dc->path);
441 if(bRet)
443 /* FIXME: Should the path be emptied even if conversion
444 failed? */
445 PATH_EmptyPath(&dc->path);
448 GDI_ReleaseObj( hdc );
449 return bRet;
452 /***********************************************************************
453 * SelectClipPath (GDI.519)
455 BOOL16 WINAPI SelectClipPath16(HDC16 hdc, INT16 iMode)
457 return (BOOL16) SelectClipPath((HDC) hdc, iMode);
460 /***********************************************************************
461 * SelectClipPath (GDI32.@)
462 * FIXME
463 * Check that SetLastError is being called correctly
465 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
467 GdiPath *pPath;
468 HRGN hrgnPath;
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);
476 else
478 pPath = &dc->path;
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);
489 /* Empty the path */
490 if(success)
491 PATH_EmptyPath(pPath);
492 /* FIXME: Should this function delete the path even if it failed? */
495 GDI_ReleaseObj( hdc );
496 return success;
500 /***********************************************************************
501 * Exported functions
504 /* PATH_InitGdiPath
506 * Initializes the GdiPath structure.
508 void PATH_InitGdiPath(GdiPath *pPath)
510 assert(pPath!=NULL);
512 pPath->state=PATH_Null;
513 pPath->pPoints=NULL;
514 pPath->pFlags=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)
525 assert(pPath!=NULL);
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))
547 return FALSE;
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;
559 return TRUE;
562 /* PATH_MoveTo
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
566 * FALSE.
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? */
575 return FALSE;
577 /* Start a new stroke */
578 pPath->newStroke=TRUE;
580 return TRUE;
583 /* PATH_LineTo
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)
597 return FALSE;
599 /* Convert point to device coordinates */
600 point.x=x;
601 point.y=y;
602 if(!LPtoDP(dc->hSelf, &point, 1))
603 return FALSE;
605 /* Add a PT_MOVETO if necessary */
606 if(pPath->newStroke)
608 pPath->newStroke=FALSE;
609 pointCurPos.x = dc->CursPosX;
610 pointCurPos.y = dc->CursPosY;
611 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
612 return FALSE;
613 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
614 return FALSE;
617 /* Add a PT_LINETO entry */
618 return PATH_AddEntry(pPath, &point, PT_LINETO);
621 /* PATH_RoundRect
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)
638 return FALSE;
640 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
641 return FALSE;
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))
649 return FALSE;
650 pointTemp.x = corners[0].x+ell_width/2;
651 pointTemp.y = corners[0].y;
652 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
653 return FALSE;
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))
657 return FALSE;
658 pointTemp.x = corners[0].x;
659 pointTemp.y = corners[1].y-ell_height/2;
660 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
661 return FALSE;
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))
665 return FALSE;
666 pointTemp.x = corners[1].x-ell_width/2;
667 pointTemp.y = corners[1].y;
668 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
669 return FALSE;
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))
673 return FALSE;
675 /* Close the roundrect figure */
676 if(!CloseFigure(dc->hSelf))
677 return FALSE;
679 return TRUE;
682 /* PATH_Rectangle
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)
694 return FALSE;
696 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
697 return FALSE;
699 /* Close any previous figure */
700 if(!CloseFigure(dc->hSelf))
702 /* The CloseFigure call shouldn't have failed */
703 assert(FALSE);
704 return FALSE;
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))
711 return FALSE;
712 if(!PATH_AddEntry(pPath, corners, PT_LINETO))
713 return FALSE;
714 pointTemp.x=corners[0].x;
715 pointTemp.y=corners[1].y;
716 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
717 return FALSE;
718 if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
719 return FALSE;
721 /* Close the rectangle figure */
722 if(!CloseFigure(dc->hSelf))
724 /* The CloseFigure call shouldn't have failed */
725 assert(FALSE);
726 return FALSE;
729 return TRUE;
732 /* PATH_Ellipse
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) );
744 /* PATH_Arc
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 */
758 double x, y;
759 FLOAT_POINT corners[2], pointStart, pointEnd;
760 POINT centre;
761 BOOL start, end;
762 INT temp;
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)
769 return FALSE;
771 /* Check for zero height / width */
772 /* FIXME: Only in GM_COMPATIBLE? */
773 if(x1==x2 || y1==y2)
774 return TRUE;
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)
793 temp=corners[0].x;
794 corners[0].x=corners[1].x;
795 corners[1].x=temp;
797 if(corners[0].y>corners[1].y)
799 temp=corners[0].y;
800 corners[0].y=corners[1].y;
801 corners[1].y=temp;
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)
815 angleEnd+=2*M_PI;
816 assert(angleEnd>=angleStart);
819 else
821 if(angleEnd>=angleStart)
823 angleEnd-=2*M_PI;
824 assert(angleEnd<=angleStart);
828 /* In GM_COMPATIBLE, don't include bottom and right edges */
829 if(dc->GraphicsMode==GM_COMPATIBLE)
831 corners[1].x--;
832 corners[1].y--;
835 /* Add the arc to the path with one Bezier spline per quadrant that the
836 * arc spans */
837 start=TRUE;
838 end=FALSE;
841 /* Determine the start and end angles for this quadrant */
842 if(start)
844 angleStartQuadrant=angleStart;
845 if(dc->ArcDirection==AD_CLOCKWISE)
846 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
847 else
848 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
850 else
852 angleStartQuadrant=angleEndQuadrant;
853 if(dc->ArcDirection==AD_CLOCKWISE)
854 angleEndQuadrant+=M_PI_2;
855 else
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;
867 end=TRUE;
870 /* Add the Bezier spline to the path */
871 PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
872 start);
873 start=FALSE;
874 } while(!end);
876 /* chord: close figure. pie: add line and close figure */
877 if(lines==1)
879 if(!CloseFigure(dc->hSelf))
880 return FALSE;
882 else if(lines==2)
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, &centre, PT_LINETO | PT_CLOSEFIGURE))
887 return FALSE;
890 return TRUE;
893 BOOL PATH_PolyBezierTo(DC *dc, const POINT *pts, DWORD cbPoints)
895 GdiPath *pPath = &dc->path;
896 POINT pt;
897 INT i;
899 /* Check that path is open */
900 if(pPath->state!=PATH_Open)
901 return FALSE;
903 /* Add a PT_MOVETO if necessary */
904 if(pPath->newStroke)
906 pPath->newStroke=FALSE;
907 pt.x = dc->CursPosX;
908 pt.y = dc->CursPosY;
909 if(!LPtoDP(dc->hSelf, &pt, 1))
910 return FALSE;
911 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
912 return FALSE;
915 for(i = 0; i < cbPoints; i++) {
916 pt = pts[i];
917 if(!LPtoDP(dc->hSelf, &pt, 1))
918 return FALSE;
919 PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
921 return TRUE;
924 BOOL PATH_PolyBezier(DC *dc, const POINT *pts, DWORD cbPoints)
926 GdiPath *pPath = &dc->path;
927 POINT pt;
928 INT i;
930 /* Check that path is open */
931 if(pPath->state!=PATH_Open)
932 return FALSE;
934 for(i = 0; i < cbPoints; i++) {
935 pt = pts[i];
936 if(!LPtoDP(dc->hSelf, &pt, 1))
937 return FALSE;
938 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
940 return TRUE;
943 BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
945 GdiPath *pPath = &dc->path;
946 POINT pt;
947 INT i;
949 /* Check that path is open */
950 if(pPath->state!=PATH_Open)
951 return FALSE;
953 for(i = 0; i < cbPoints; i++) {
954 pt = pts[i];
955 if(!LPtoDP(dc->hSelf, &pt, 1))
956 return FALSE;
957 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
959 return TRUE;
962 BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
964 GdiPath *pPath = &dc->path;
965 POINT pt;
966 INT i;
968 /* Check that path is open */
969 if(pPath->state!=PATH_Open)
970 return FALSE;
972 /* Add a PT_MOVETO if necessary */
973 if(pPath->newStroke)
975 pPath->newStroke=FALSE;
976 pt.x = dc->CursPosX;
977 pt.y = dc->CursPosY;
978 if(!LPtoDP(dc->hSelf, &pt, 1))
979 return FALSE;
980 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
981 return FALSE;
984 for(i = 0; i < cbPoints; i++) {
985 pt = pts[i];
986 if(!LPtoDP(dc->hSelf, &pt, 1))
987 return FALSE;
988 PATH_AddEntry(pPath, &pt, PT_LINETO);
991 return TRUE;
995 BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
997 GdiPath *pPath = &dc->path;
998 POINT pt;
999 INT i;
1001 /* Check that path is open */
1002 if(pPath->state!=PATH_Open)
1003 return FALSE;
1005 for(i = 0; i < cbPoints; i++) {
1006 pt = pts[i];
1007 if(!LPtoDP(dc->hSelf, &pt, 1))
1008 return FALSE;
1009 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
1010 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
1011 PT_LINETO));
1013 return TRUE;
1016 BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
1017 UINT polygons )
1019 GdiPath *pPath = &dc->path;
1020 POINT pt, startpt;
1021 INT poly, point, i;
1023 /* Check that path is open */
1024 if(pPath->state!=PATH_Open)
1025 return FALSE;
1027 for(i = 0, poly = 0; poly < polygons; poly++) {
1028 for(point = 0; point < counts[poly]; point++, i++) {
1029 pt = pts[i];
1030 if(!LPtoDP(dc->hSelf, &pt, 1))
1031 return FALSE;
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);
1038 return TRUE;
1041 BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1042 DWORD polylines )
1044 GdiPath *pPath = &dc->path;
1045 POINT pt;
1046 INT poly, point, i;
1048 /* Check that path is open */
1049 if(pPath->state!=PATH_Open)
1050 return FALSE;
1052 for(i = 0, poly = 0; poly < polylines; poly++) {
1053 for(point = 0; point < counts[poly]; point++, i++) {
1054 pt = pts[i];
1055 if(!LPtoDP(dc->hSelf, &pt, 1))
1056 return FALSE;
1057 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1060 return TRUE;
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)
1073 INT temp;
1075 /* Convert points to device coordinates */
1076 corners[0].x=x1;
1077 corners[0].y=y1;
1078 corners[1].x=x2;
1079 corners[1].y=y2;
1080 if(!LPtoDP(dc->hSelf, corners, 2))
1081 return FALSE;
1083 /* Make sure first corner is top left and second corner is bottom right */
1084 if(corners[0].x>corners[1].x)
1086 temp=corners[0].x;
1087 corners[0].x=corners[1].x;
1088 corners[1].x=temp;
1090 if(corners[0].y>corners[1].y)
1092 temp=corners[0].y;
1093 corners[0].y=corners[1].y;
1094 corners[1].y=temp;
1097 /* In GM_COMPATIBLE, don't include bottom and right edges */
1098 if(dc->GraphicsMode==GM_COMPATIBLE)
1100 corners[1].x--;
1101 corners[1].y--;
1104 return TRUE;
1107 /* PATH_AddFlatBezier
1109 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
1111 POINT *pts;
1112 INT no, i;
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 );
1121 return TRUE;
1124 /* PATH_FlattenPath
1126 * Replaces Beziers with line segments
1129 static BOOL PATH_FlattenPath(GdiPath *pPath)
1131 GdiPath newPath;
1132 INT srcpt;
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) {
1138 case PT_MOVETO:
1139 case PT_LINETO:
1140 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
1141 pPath->pFlags[srcpt]);
1142 break;
1143 case PT_BEZIERTO:
1144 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
1145 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
1146 srcpt += 2;
1147 break;
1150 newPath.state = PATH_Closed;
1151 PATH_AssignGdiPath(pPath, &newPath);
1152 PATH_EmptyPath(&newPath);
1153 return TRUE;
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,
1165 HRGN *pHrgn)
1167 int numStrokes, iStroke, i;
1168 INT *pNumPointsInStroke;
1169 HRGN hrgn;
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 */
1180 numStrokes=0;
1181 for(i=0; i<pPath->numEntriesUsed; i++)
1182 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1183 numStrokes++;
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);
1191 return FALSE;
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)
1201 iStroke++;
1202 pNumPointsInStroke[iStroke]=0;
1205 pNumPointsInStroke[iStroke]++;
1208 /* Create a region from the strokes */
1209 hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
1210 numStrokes, nPolyFillMode);
1211 if(hrgn==(HRGN)0)
1213 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1214 return FALSE;
1217 /* Free memory for number-of-points-in-stroke array */
1218 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
1220 /* Success! */
1221 *pHrgn=hrgn;
1222 return TRUE;
1225 /* PATH_EmptyPath
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;
1237 /* PATH_AddEntry
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)
1254 return FALSE;
1256 /* Reserve enough memory for an extra path entry */
1257 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
1258 return FALSE;
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++;
1271 return TRUE;
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;
1283 POINT *pPointsNew;
1284 BYTE *pFlagsNew;
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
1294 * complexity. */
1295 if(pPath->numEntriesAllocated)
1297 numEntriesToAllocate=pPath->numEntriesAllocated;
1298 while(numEntriesToAllocate<numEntries)
1299 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
1300 GROW_FACTOR_DENOM;
1302 else
1303 numEntriesToAllocate=numEntries;
1305 /* Allocate new arrays */
1306 pPointsNew=(POINT *)HeapAlloc( GetProcessHeap(), 0,
1307 numEntriesToAllocate * sizeof(POINT) );
1308 if(!pPointsNew)
1309 return FALSE;
1310 pFlagsNew=(BYTE *)HeapAlloc( GetProcessHeap(), 0,
1311 numEntriesToAllocate * sizeof(BYTE) );
1312 if(!pFlagsNew)
1314 HeapFree( GetProcessHeap(), 0, pPointsNew );
1315 return FALSE;
1318 /* Copy old arrays to new arrays and discard old arrays */
1319 if(pPath->pPoints)
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;
1336 return TRUE;
1339 /* PATH_DoArcPart
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];
1353 POINT point;
1354 int i;
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];
1374 else
1375 for(i=0; i<4; i++)
1377 xNorm[i]=cos(angleStart);
1378 yNorm[i]=sin(angleStart);
1381 /* Add starting point to path if desired */
1382 if(addMoveTo)
1384 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1385 if(!PATH_AddEntry(pPath, &point, PT_MOVETO))
1386 return FALSE;
1389 /* Add remaining control points */
1390 for(i=1; i<4; i++)
1392 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1393 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1394 return FALSE;
1397 return TRUE;
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) *
1426 2.0 - 1.0;
1427 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1428 2.0 - 1.0;
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)
1448 BOOL ret = FALSE;
1449 DC *dc = DC_GetDCPtr( hdc );
1451 if(!dc) return FALSE;
1453 if(dc->funcs->pFlattenPath) ret = dc->funcs->pFlattenPath(dc);
1454 else
1456 GdiPath *pPath = &dc->path;
1457 if(pPath->state != PATH_Closed)
1458 ret = PATH_FlattenPath(pPath);
1460 GDI_ReleaseObj( hdc );
1461 return ret;
1465 static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1467 INT i;
1468 POINT ptLastMove = {0,0};
1469 POINT ptViewportOrg, ptWindowOrg;
1470 SIZE szViewportExt, szWindowExt;
1471 DWORD mapMode, graphicsMode;
1472 XFORM xform;
1473 BOOL ret = TRUE;
1475 if(dc->funcs->pStrokePath)
1476 return dc->funcs->pStrokePath(dc);
1478 if(pPath->state != PATH_Closed)
1479 return FALSE;
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);
1489 /* Set MM_TEXT */
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]) {
1497 case PT_MOVETO:
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];
1502 break;
1503 case PT_LINETO:
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);
1508 break;
1509 case PT_BEZIERTO:
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");
1514 ret = FALSE;
1515 goto end;
1517 PolyBezierTo(dc->hSelf, &pPath->pPoints[i], 3);
1518 i += 2;
1519 break;
1520 default:
1521 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1522 ret = FALSE;
1523 goto end;
1525 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1526 LineTo(dc->hSelf, ptLastMove.x, ptLastMove.y);
1529 end:
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);
1543 return ret;
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 );
1565 BOOL bRet = FALSE;
1567 if(!dc) return FALSE;
1569 if(dc->funcs->pStrokeAndFillPath)
1570 bRet = dc->funcs->pStrokeAndFillPath(dc);
1571 else
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 );
1578 return bRet;
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 );
1599 GdiPath *pPath;
1600 BOOL bRet = FALSE;
1602 TRACE("(%08x)\n", hdc);
1603 if(!dc) return FALSE;
1605 if(dc->funcs->pStrokePath)
1606 bRet = dc->funcs->pStrokePath(dc);
1607 else
1609 pPath = &dc->path;
1610 bRet = PATH_StrokePath(dc, pPath);
1611 PATH_EmptyPath(pPath);
1613 GDI_ReleaseObj( hdc );
1614 return bRet;
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 );
1635 BOOL ret = FALSE;
1637 if(!dc) return FALSE;
1639 if(dc->funcs->pWidenPath)
1640 ret = dc->funcs->pWidenPath(dc);
1642 FIXME("stub\n");
1643 GDI_ReleaseObj( hdc );
1644 return ret;