Avoid multi-line string constants.
[wine/dcerpc.git] / graphics / path.c
blob139a7d1ea20a28b8cd3b1d2728e638ed87096476
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 */
84 /* A floating point version of the POINT structure */
85 typedef struct tagFLOAT_POINT
87 FLOAT x, y;
88 } FLOAT_POINT;
91 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
92 HRGN *pHrgn);
93 static void PATH_EmptyPath(GdiPath *pPath);
94 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries);
95 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
96 double angleStart, double angleEnd, BOOL addMoveTo);
97 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
98 double y, POINT *pPoint);
99 static void PATH_NormalizePoint(FLOAT_POINT corners[], const FLOAT_POINT
100 *pPoint, double *pX, double *pY);
101 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2);
103 /* Performs a world-to-viewport transformation on the specified point (which
104 * is in floating point format).
106 static inline void WINE_UNUSED INTERNAL_LPTODP_FLOAT(DC *dc, FLOAT_POINT *point)
108 FLOAT x, y;
110 /* Perform the transformation */
111 x = point->x;
112 y = point->y;
113 point->x = x * dc->xformWorld2Vport.eM11 +
114 y * dc->xformWorld2Vport.eM21 +
115 dc->xformWorld2Vport.eDx;
116 point->y = x * dc->xformWorld2Vport.eM12 +
117 y * dc->xformWorld2Vport.eM22 +
118 dc->xformWorld2Vport.eDy;
121 /***********************************************************************
122 * BeginPath (GDI.512)
124 BOOL16 WINAPI BeginPath16(HDC16 hdc)
126 return (BOOL16)BeginPath((HDC)hdc);
130 /***********************************************************************
131 * BeginPath (GDI32.@)
133 BOOL WINAPI BeginPath(HDC hdc)
135 BOOL ret = TRUE;
136 DC *dc = DC_GetDCPtr( hdc );
138 if(!dc) return FALSE;
140 if(dc->funcs->pBeginPath)
141 ret = dc->funcs->pBeginPath(dc->physDev);
142 else
144 /* If path is already open, do nothing */
145 if(dc->path.state != PATH_Open)
147 /* Make sure that path is empty */
148 PATH_EmptyPath(&dc->path);
150 /* Initialize variables for new path */
151 dc->path.newStroke=TRUE;
152 dc->path.state=PATH_Open;
155 GDI_ReleaseObj( hdc );
156 return ret;
160 /***********************************************************************
161 * EndPath (GDI.514)
163 BOOL16 WINAPI EndPath16(HDC16 hdc)
165 return (BOOL16)EndPath((HDC)hdc);
169 /***********************************************************************
170 * EndPath (GDI32.@)
172 BOOL WINAPI EndPath(HDC hdc)
174 BOOL ret = TRUE;
175 DC *dc = DC_GetDCPtr( hdc );
177 if(!dc) return FALSE;
179 if(dc->funcs->pEndPath)
180 ret = dc->funcs->pEndPath(dc->physDev);
181 else
183 /* Check that path is currently being constructed */
184 if(dc->path.state!=PATH_Open)
186 SetLastError(ERROR_CAN_NOT_COMPLETE);
187 ret = FALSE;
189 /* Set flag to indicate that path is finished */
190 else dc->path.state=PATH_Closed;
192 GDI_ReleaseObj( hdc );
193 return ret;
197 /***********************************************************************
198 * AbortPath (GDI.511)
200 BOOL16 WINAPI AbortPath16(HDC16 hdc)
202 return (BOOL16)AbortPath((HDC)hdc);
206 /******************************************************************************
207 * AbortPath [GDI32.@]
208 * Closes and discards paths from device context
210 * NOTES
211 * Check that SetLastError is being called correctly
213 * PARAMS
214 * hdc [I] Handle to device context
216 * RETURNS STD
218 BOOL WINAPI AbortPath( HDC hdc )
220 BOOL ret = TRUE;
221 DC *dc = DC_GetDCPtr( hdc );
223 if(!dc) return FALSE;
225 if(dc->funcs->pAbortPath)
226 ret = dc->funcs->pAbortPath(dc->physDev);
227 else /* Remove all entries from the path */
228 PATH_EmptyPath( &dc->path );
229 GDI_ReleaseObj( hdc );
230 return ret;
234 /***********************************************************************
235 * CloseFigure (GDI.513)
237 BOOL16 WINAPI CloseFigure16(HDC16 hdc)
239 return (BOOL16)CloseFigure((HDC)hdc);
243 /***********************************************************************
244 * CloseFigure (GDI32.@)
246 * FIXME: Check that SetLastError is being called correctly
248 BOOL WINAPI CloseFigure(HDC hdc)
250 BOOL ret = TRUE;
251 DC *dc = DC_GetDCPtr( hdc );
253 if(!dc) return FALSE;
255 if(dc->funcs->pCloseFigure)
256 ret = dc->funcs->pCloseFigure(dc->physDev);
257 else
259 /* Check that path is open */
260 if(dc->path.state!=PATH_Open)
262 SetLastError(ERROR_CAN_NOT_COMPLETE);
263 ret = FALSE;
265 else
267 /* FIXME: Shouldn't we draw a line to the beginning of the
268 figure? */
269 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
270 if(dc->path.numEntriesUsed)
272 dc->path.pFlags[dc->path.numEntriesUsed-1]|=PT_CLOSEFIGURE;
273 dc->path.newStroke=TRUE;
277 GDI_ReleaseObj( hdc );
278 return ret;
282 /***********************************************************************
283 * GetPath (GDI.517)
285 INT16 WINAPI GetPath16(HDC16 hdc, LPPOINT16 pPoints, LPBYTE pTypes,
286 INT16 nSize)
288 FIXME("(%d,%p,%p): stub\n",hdc,pPoints,pTypes);
290 return 0;
294 /***********************************************************************
295 * GetPath (GDI32.@)
297 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
298 INT nSize)
300 INT ret = -1;
301 GdiPath *pPath;
302 DC *dc = DC_GetDCPtr( hdc );
304 if(!dc) return -1;
306 pPath = &dc->path;
308 /* Check that path is closed */
309 if(pPath->state!=PATH_Closed)
311 SetLastError(ERROR_CAN_NOT_COMPLETE);
312 goto done;
315 if(nSize==0)
316 ret = pPath->numEntriesUsed;
317 else if(nSize<pPath->numEntriesUsed)
319 SetLastError(ERROR_INVALID_PARAMETER);
320 goto done;
322 else
324 memcpy(pPoints, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
325 memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
327 /* Convert the points to logical coordinates */
328 if(!DPtoLP(hdc, pPoints, pPath->numEntriesUsed))
330 /* FIXME: Is this the correct value? */
331 SetLastError(ERROR_CAN_NOT_COMPLETE);
332 goto done;
334 else ret = pPath->numEntriesUsed;
336 done:
337 GDI_ReleaseObj( hdc );
338 return ret;
341 /***********************************************************************
342 * PathToRegion (GDI.518)
344 HRGN16 WINAPI PathToRegion16(HDC16 hdc)
346 return (HRGN16) PathToRegion((HDC) hdc);
349 /***********************************************************************
350 * PathToRegion (GDI32.@)
352 * FIXME
353 * Check that SetLastError is being called correctly
355 * The documentation does not state this explicitly, but a test under Windows
356 * shows that the region which is returned should be in device coordinates.
358 HRGN WINAPI PathToRegion(HDC hdc)
360 GdiPath *pPath;
361 HRGN hrgnRval = 0;
362 DC *dc = DC_GetDCPtr( hdc );
364 /* Get pointer to path */
365 if(!dc) return -1;
367 pPath = &dc->path;
369 /* Check that path is closed */
370 if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
371 else
373 /* FIXME: Should we empty the path even if conversion failed? */
374 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
375 PATH_EmptyPath(pPath);
376 else
377 hrgnRval=0;
379 GDI_ReleaseObj( hdc );
380 return hrgnRval;
383 static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
385 INT mapMode, graphicsMode;
386 SIZE ptViewportExt, ptWindowExt;
387 POINT ptViewportOrg, ptWindowOrg;
388 XFORM xform;
389 HRGN hrgn;
391 if(dc->funcs->pFillPath)
392 return dc->funcs->pFillPath(dc->physDev);
394 /* Check that path is closed */
395 if(pPath->state!=PATH_Closed)
397 SetLastError(ERROR_CAN_NOT_COMPLETE);
398 return FALSE;
401 /* Construct a region from the path and fill it */
402 if(PATH_PathToRegion(pPath, dc->polyFillMode, &hrgn))
404 /* Since PaintRgn interprets the region as being in logical coordinates
405 * but the points we store for the path are already in device
406 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
407 * Using SaveDC to save information about the mapping mode / world
408 * transform would be easier but would require more overhead, especially
409 * now that SaveDC saves the current path.
412 /* Save the information about the old mapping mode */
413 mapMode=GetMapMode(dc->hSelf);
414 GetViewportExtEx(dc->hSelf, &ptViewportExt);
415 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
416 GetWindowExtEx(dc->hSelf, &ptWindowExt);
417 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
419 /* Save world transform
420 * NB: The Windows documentation on world transforms would lead one to
421 * believe that this has to be done only in GM_ADVANCED; however, my
422 * tests show that resetting the graphics mode to GM_COMPATIBLE does
423 * not reset the world transform.
425 GetWorldTransform(dc->hSelf, &xform);
427 /* Set MM_TEXT */
428 SetMapMode(dc->hSelf, MM_TEXT);
429 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
430 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
432 /* Paint the region */
433 PaintRgn(dc->hSelf, hrgn);
434 DeleteObject(hrgn);
435 /* Restore the old mapping mode */
436 SetMapMode(dc->hSelf, mapMode);
437 SetViewportExtEx(dc->hSelf, ptViewportExt.cx, ptViewportExt.cy, NULL);
438 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
439 SetWindowExtEx(dc->hSelf, ptWindowExt.cx, ptWindowExt.cy, NULL);
440 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
442 /* Go to GM_ADVANCED temporarily to restore the world transform */
443 graphicsMode=GetGraphicsMode(dc->hSelf);
444 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
445 SetWorldTransform(dc->hSelf, &xform);
446 SetGraphicsMode(dc->hSelf, graphicsMode);
447 return TRUE;
449 return FALSE;
452 /***********************************************************************
453 * FillPath (GDI.515)
455 BOOL16 WINAPI FillPath16(HDC16 hdc)
457 return (BOOL16) FillPath((HDC) hdc);
460 /***********************************************************************
461 * FillPath (GDI32.@)
463 * FIXME
464 * Check that SetLastError is being called correctly
466 BOOL WINAPI FillPath(HDC hdc)
468 DC *dc = DC_GetDCPtr( hdc );
469 BOOL bRet = FALSE;
471 if(!dc) return FALSE;
473 if(dc->funcs->pFillPath)
474 bRet = dc->funcs->pFillPath(dc->physDev);
475 else
477 bRet = PATH_FillPath(dc, &dc->path);
478 if(bRet)
480 /* FIXME: Should the path be emptied even if conversion
481 failed? */
482 PATH_EmptyPath(&dc->path);
485 GDI_ReleaseObj( hdc );
486 return bRet;
489 /***********************************************************************
490 * SelectClipPath (GDI.519)
492 BOOL16 WINAPI SelectClipPath16(HDC16 hdc, INT16 iMode)
494 return (BOOL16) SelectClipPath((HDC) hdc, iMode);
497 /***********************************************************************
498 * SelectClipPath (GDI32.@)
499 * FIXME
500 * Check that SetLastError is being called correctly
502 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
504 GdiPath *pPath;
505 HRGN hrgnPath;
506 BOOL success = FALSE;
507 DC *dc = DC_GetDCPtr( hdc );
509 if(!dc) return FALSE;
511 if(dc->funcs->pSelectClipPath)
512 success = dc->funcs->pSelectClipPath(dc->physDev, iMode);
513 else
515 pPath = &dc->path;
517 /* Check that path is closed */
518 if(pPath->state!=PATH_Closed)
519 SetLastError(ERROR_CAN_NOT_COMPLETE);
520 /* Construct a region from the path */
521 else if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnPath))
523 success = ExtSelectClipRgn( hdc, hrgnPath, iMode ) != ERROR;
524 DeleteObject(hrgnPath);
526 /* Empty the path */
527 if(success)
528 PATH_EmptyPath(pPath);
529 /* FIXME: Should this function delete the path even if it failed? */
532 GDI_ReleaseObj( hdc );
533 return success;
537 /***********************************************************************
538 * Exported functions
541 /* PATH_InitGdiPath
543 * Initializes the GdiPath structure.
545 void PATH_InitGdiPath(GdiPath *pPath)
547 assert(pPath!=NULL);
549 pPath->state=PATH_Null;
550 pPath->pPoints=NULL;
551 pPath->pFlags=NULL;
552 pPath->numEntriesUsed=0;
553 pPath->numEntriesAllocated=0;
556 /* PATH_DestroyGdiPath
558 * Destroys a GdiPath structure (frees the memory in the arrays).
560 void PATH_DestroyGdiPath(GdiPath *pPath)
562 assert(pPath!=NULL);
564 if (pPath->pPoints) HeapFree( GetProcessHeap(), 0, pPath->pPoints );
565 if (pPath->pFlags) HeapFree( GetProcessHeap(), 0, pPath->pFlags );
568 /* PATH_AssignGdiPath
570 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
571 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
572 * not just the pointers. Since this means that the arrays in pPathDest may
573 * need to be resized, pPathDest should have been initialized using
574 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
575 * not a copy constructor).
576 * Returns TRUE if successful, else FALSE.
578 BOOL PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
580 assert(pPathDest!=NULL && pPathSrc!=NULL);
582 /* Make sure destination arrays are big enough */
583 if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
584 return FALSE;
586 /* Perform the copy operation */
587 memcpy(pPathDest->pPoints, pPathSrc->pPoints,
588 sizeof(POINT)*pPathSrc->numEntriesUsed);
589 memcpy(pPathDest->pFlags, pPathSrc->pFlags,
590 sizeof(BYTE)*pPathSrc->numEntriesUsed);
592 pPathDest->state=pPathSrc->state;
593 pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
594 pPathDest->newStroke=pPathSrc->newStroke;
596 return TRUE;
599 /* PATH_MoveTo
601 * Should be called when a MoveTo is performed on a DC that has an
602 * open path. This starts a new stroke. Returns TRUE if successful, else
603 * FALSE.
605 BOOL PATH_MoveTo(DC *dc)
607 GdiPath *pPath = &dc->path;
609 /* Check that path is open */
610 if(pPath->state!=PATH_Open)
611 /* FIXME: Do we have to call SetLastError? */
612 return FALSE;
614 /* Start a new stroke */
615 pPath->newStroke=TRUE;
617 return TRUE;
620 /* PATH_LineTo
622 * Should be called when a LineTo is performed on a DC that has an
623 * open path. This adds a PT_LINETO entry to the path (and possibly
624 * a PT_MOVETO entry, if this is the first LineTo in a stroke).
625 * Returns TRUE if successful, else FALSE.
627 BOOL PATH_LineTo(DC *dc, INT x, INT y)
629 GdiPath *pPath = &dc->path;
630 POINT point, pointCurPos;
632 /* Check that path is open */
633 if(pPath->state!=PATH_Open)
634 return FALSE;
636 /* Convert point to device coordinates */
637 point.x=x;
638 point.y=y;
639 if(!LPtoDP(dc->hSelf, &point, 1))
640 return FALSE;
642 /* Add a PT_MOVETO if necessary */
643 if(pPath->newStroke)
645 pPath->newStroke=FALSE;
646 pointCurPos.x = dc->CursPosX;
647 pointCurPos.y = dc->CursPosY;
648 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
649 return FALSE;
650 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
651 return FALSE;
654 /* Add a PT_LINETO entry */
655 return PATH_AddEntry(pPath, &point, PT_LINETO);
658 /* PATH_RoundRect
660 * Should be called when a call to RoundRect is performed on a DC that has
661 * an open path. Returns TRUE if successful, else FALSE.
663 * FIXME: it adds the same entries to the path as windows does, but there
664 * is an error in the bezier drawing code so that there are small pixel-size
665 * gaps when the resulting path is drawn by StrokePath()
667 BOOL PATH_RoundRect(DC *dc, INT x1, INT y1, INT x2, INT y2, INT ell_width, INT ell_height)
669 GdiPath *pPath = &dc->path;
670 POINT corners[2], pointTemp;
671 FLOAT_POINT ellCorners[2];
673 /* Check that path is open */
674 if(pPath->state!=PATH_Open)
675 return FALSE;
677 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
678 return FALSE;
680 /* Add points to the roundrect path */
681 ellCorners[0].x = corners[1].x-ell_width;
682 ellCorners[0].y = corners[0].y;
683 ellCorners[1].x = corners[1].x;
684 ellCorners[1].y = corners[0].y+ell_height;
685 if(!PATH_DoArcPart(pPath, ellCorners, 0, -M_PI_2, TRUE))
686 return FALSE;
687 pointTemp.x = corners[0].x+ell_width/2;
688 pointTemp.y = corners[0].y;
689 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
690 return FALSE;
691 ellCorners[0].x = corners[0].x;
692 ellCorners[1].x = corners[0].x+ell_width;
693 if(!PATH_DoArcPart(pPath, ellCorners, -M_PI_2, -M_PI, FALSE))
694 return FALSE;
695 pointTemp.x = corners[0].x;
696 pointTemp.y = corners[1].y-ell_height/2;
697 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
698 return FALSE;
699 ellCorners[0].y = corners[1].y-ell_height;
700 ellCorners[1].y = corners[1].y;
701 if(!PATH_DoArcPart(pPath, ellCorners, M_PI, M_PI_2, FALSE))
702 return FALSE;
703 pointTemp.x = corners[1].x-ell_width/2;
704 pointTemp.y = corners[1].y;
705 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
706 return FALSE;
707 ellCorners[0].x = corners[1].x-ell_width;
708 ellCorners[1].x = corners[1].x;
709 if(!PATH_DoArcPart(pPath, ellCorners, M_PI_2, 0, FALSE))
710 return FALSE;
712 /* Close the roundrect figure */
713 if(!CloseFigure(dc->hSelf))
714 return FALSE;
716 return TRUE;
719 /* PATH_Rectangle
721 * Should be called when a call to Rectangle is performed on a DC that has
722 * an open path. Returns TRUE if successful, else FALSE.
724 BOOL PATH_Rectangle(DC *dc, INT x1, INT y1, INT x2, INT y2)
726 GdiPath *pPath = &dc->path;
727 POINT corners[2], pointTemp;
729 /* Check that path is open */
730 if(pPath->state!=PATH_Open)
731 return FALSE;
733 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
734 return FALSE;
736 /* Close any previous figure */
737 if(!CloseFigure(dc->hSelf))
739 /* The CloseFigure call shouldn't have failed */
740 assert(FALSE);
741 return FALSE;
744 /* Add four points to the path */
745 pointTemp.x=corners[1].x;
746 pointTemp.y=corners[0].y;
747 if(!PATH_AddEntry(pPath, &pointTemp, PT_MOVETO))
748 return FALSE;
749 if(!PATH_AddEntry(pPath, corners, PT_LINETO))
750 return FALSE;
751 pointTemp.x=corners[0].x;
752 pointTemp.y=corners[1].y;
753 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
754 return FALSE;
755 if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
756 return FALSE;
758 /* Close the rectangle figure */
759 if(!CloseFigure(dc->hSelf))
761 /* The CloseFigure call shouldn't have failed */
762 assert(FALSE);
763 return FALSE;
766 return TRUE;
769 /* PATH_Ellipse
771 * Should be called when a call to Ellipse is performed on a DC that has
772 * an open path. This adds four Bezier splines representing the ellipse
773 * to the path. Returns TRUE if successful, else FALSE.
775 BOOL PATH_Ellipse(DC *dc, INT x1, INT y1, INT x2, INT y2)
777 return( PATH_Arc(dc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2,0) &&
778 CloseFigure(dc->hSelf) );
781 /* PATH_Arc
783 * Should be called when a call to Arc is performed on a DC that has
784 * an open path. This adds up to five Bezier splines representing the arc
785 * to the path. When 'lines' is 1, we add 1 extra line to get a chord,
786 * and when 'lines' is 2, we add 2 extra lines to get a pie.
787 * Returns TRUE if successful, else FALSE.
789 BOOL PATH_Arc(DC *dc, INT x1, INT y1, INT x2, INT y2,
790 INT xStart, INT yStart, INT xEnd, INT yEnd, INT lines)
792 GdiPath *pPath = &dc->path;
793 double angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
794 /* Initialize angleEndQuadrant to silence gcc's warning */
795 double x, y;
796 FLOAT_POINT corners[2], pointStart, pointEnd;
797 POINT centre;
798 BOOL start, end;
799 INT temp;
801 /* FIXME: This function should check for all possible error returns */
802 /* FIXME: Do we have to respect newStroke? */
804 /* Check that path is open */
805 if(pPath->state!=PATH_Open)
806 return FALSE;
808 /* Check for zero height / width */
809 /* FIXME: Only in GM_COMPATIBLE? */
810 if(x1==x2 || y1==y2)
811 return TRUE;
813 /* Convert points to device coordinates */
814 corners[0].x=(FLOAT)x1;
815 corners[0].y=(FLOAT)y1;
816 corners[1].x=(FLOAT)x2;
817 corners[1].y=(FLOAT)y2;
818 pointStart.x=(FLOAT)xStart;
819 pointStart.y=(FLOAT)yStart;
820 pointEnd.x=(FLOAT)xEnd;
821 pointEnd.y=(FLOAT)yEnd;
822 INTERNAL_LPTODP_FLOAT(dc, corners);
823 INTERNAL_LPTODP_FLOAT(dc, corners+1);
824 INTERNAL_LPTODP_FLOAT(dc, &pointStart);
825 INTERNAL_LPTODP_FLOAT(dc, &pointEnd);
827 /* Make sure first corner is top left and second corner is bottom right */
828 if(corners[0].x>corners[1].x)
830 temp=corners[0].x;
831 corners[0].x=corners[1].x;
832 corners[1].x=temp;
834 if(corners[0].y>corners[1].y)
836 temp=corners[0].y;
837 corners[0].y=corners[1].y;
838 corners[1].y=temp;
841 /* Compute start and end angle */
842 PATH_NormalizePoint(corners, &pointStart, &x, &y);
843 angleStart=atan2(y, x);
844 PATH_NormalizePoint(corners, &pointEnd, &x, &y);
845 angleEnd=atan2(y, x);
847 /* Make sure the end angle is "on the right side" of the start angle */
848 if(dc->ArcDirection==AD_CLOCKWISE)
850 if(angleEnd<=angleStart)
852 angleEnd+=2*M_PI;
853 assert(angleEnd>=angleStart);
856 else
858 if(angleEnd>=angleStart)
860 angleEnd-=2*M_PI;
861 assert(angleEnd<=angleStart);
865 /* In GM_COMPATIBLE, don't include bottom and right edges */
866 if(dc->GraphicsMode==GM_COMPATIBLE)
868 corners[1].x--;
869 corners[1].y--;
872 /* Add the arc to the path with one Bezier spline per quadrant that the
873 * arc spans */
874 start=TRUE;
875 end=FALSE;
878 /* Determine the start and end angles for this quadrant */
879 if(start)
881 angleStartQuadrant=angleStart;
882 if(dc->ArcDirection==AD_CLOCKWISE)
883 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
884 else
885 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
887 else
889 angleStartQuadrant=angleEndQuadrant;
890 if(dc->ArcDirection==AD_CLOCKWISE)
891 angleEndQuadrant+=M_PI_2;
892 else
893 angleEndQuadrant-=M_PI_2;
896 /* Have we reached the last part of the arc? */
897 if((dc->ArcDirection==AD_CLOCKWISE &&
898 angleEnd<angleEndQuadrant) ||
899 (dc->ArcDirection==AD_COUNTERCLOCKWISE &&
900 angleEnd>angleEndQuadrant))
902 /* Adjust the end angle for this quadrant */
903 angleEndQuadrant=angleEnd;
904 end=TRUE;
907 /* Add the Bezier spline to the path */
908 PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
909 start);
910 start=FALSE;
911 } while(!end);
913 /* chord: close figure. pie: add line and close figure */
914 if(lines==1)
916 if(!CloseFigure(dc->hSelf))
917 return FALSE;
919 else if(lines==2)
921 centre.x = (corners[0].x+corners[1].x)/2;
922 centre.y = (corners[0].y+corners[1].y)/2;
923 if(!PATH_AddEntry(pPath, &centre, PT_LINETO | PT_CLOSEFIGURE))
924 return FALSE;
927 return TRUE;
930 BOOL PATH_PolyBezierTo(DC *dc, const POINT *pts, DWORD cbPoints)
932 GdiPath *pPath = &dc->path;
933 POINT pt;
934 INT i;
936 /* Check that path is open */
937 if(pPath->state!=PATH_Open)
938 return FALSE;
940 /* Add a PT_MOVETO if necessary */
941 if(pPath->newStroke)
943 pPath->newStroke=FALSE;
944 pt.x = dc->CursPosX;
945 pt.y = dc->CursPosY;
946 if(!LPtoDP(dc->hSelf, &pt, 1))
947 return FALSE;
948 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
949 return FALSE;
952 for(i = 0; i < cbPoints; i++) {
953 pt = pts[i];
954 if(!LPtoDP(dc->hSelf, &pt, 1))
955 return FALSE;
956 PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
958 return TRUE;
961 BOOL PATH_PolyBezier(DC *dc, const POINT *pts, DWORD cbPoints)
963 GdiPath *pPath = &dc->path;
964 POINT pt;
965 INT i;
967 /* Check that path is open */
968 if(pPath->state!=PATH_Open)
969 return FALSE;
971 for(i = 0; i < cbPoints; i++) {
972 pt = pts[i];
973 if(!LPtoDP(dc->hSelf, &pt, 1))
974 return FALSE;
975 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
977 return TRUE;
980 BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
982 GdiPath *pPath = &dc->path;
983 POINT pt;
984 INT i;
986 /* Check that path is open */
987 if(pPath->state!=PATH_Open)
988 return FALSE;
990 for(i = 0; i < cbPoints; i++) {
991 pt = pts[i];
992 if(!LPtoDP(dc->hSelf, &pt, 1))
993 return FALSE;
994 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
996 return TRUE;
999 BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
1001 GdiPath *pPath = &dc->path;
1002 POINT pt;
1003 INT i;
1005 /* Check that path is open */
1006 if(pPath->state!=PATH_Open)
1007 return FALSE;
1009 /* Add a PT_MOVETO if necessary */
1010 if(pPath->newStroke)
1012 pPath->newStroke=FALSE;
1013 pt.x = dc->CursPosX;
1014 pt.y = dc->CursPosY;
1015 if(!LPtoDP(dc->hSelf, &pt, 1))
1016 return FALSE;
1017 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
1018 return FALSE;
1021 for(i = 0; i < cbPoints; i++) {
1022 pt = pts[i];
1023 if(!LPtoDP(dc->hSelf, &pt, 1))
1024 return FALSE;
1025 PATH_AddEntry(pPath, &pt, PT_LINETO);
1028 return TRUE;
1032 BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
1034 GdiPath *pPath = &dc->path;
1035 POINT pt;
1036 INT i;
1038 /* Check that path is open */
1039 if(pPath->state!=PATH_Open)
1040 return FALSE;
1042 for(i = 0; i < cbPoints; i++) {
1043 pt = pts[i];
1044 if(!LPtoDP(dc->hSelf, &pt, 1))
1045 return FALSE;
1046 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
1047 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
1048 PT_LINETO));
1050 return TRUE;
1053 BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
1054 UINT polygons )
1056 GdiPath *pPath = &dc->path;
1057 POINT pt, startpt;
1058 INT poly, point, i;
1060 /* Check that path is open */
1061 if(pPath->state!=PATH_Open)
1062 return FALSE;
1064 for(i = 0, poly = 0; poly < polygons; poly++) {
1065 for(point = 0; point < counts[poly]; point++, i++) {
1066 pt = pts[i];
1067 if(!LPtoDP(dc->hSelf, &pt, 1))
1068 return FALSE;
1069 if(point == 0) startpt = pt;
1070 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1072 /* win98 adds an extra line to close the figure for some reason */
1073 PATH_AddEntry(pPath, &startpt, PT_LINETO | PT_CLOSEFIGURE);
1075 return TRUE;
1078 BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1079 DWORD polylines )
1081 GdiPath *pPath = &dc->path;
1082 POINT pt;
1083 INT poly, point, i;
1085 /* Check that path is open */
1086 if(pPath->state!=PATH_Open)
1087 return FALSE;
1089 for(i = 0, poly = 0; poly < polylines; poly++) {
1090 for(point = 0; point < counts[poly]; point++, i++) {
1091 pt = pts[i];
1092 if(!LPtoDP(dc->hSelf, &pt, 1))
1093 return FALSE;
1094 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1097 return TRUE;
1100 /***********************************************************************
1101 * Internal functions
1104 /* PATH_CheckCorners
1106 * Helper function for PATH_RoundRect() and PATH_Rectangle()
1108 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2)
1110 INT temp;
1112 /* Convert points to device coordinates */
1113 corners[0].x=x1;
1114 corners[0].y=y1;
1115 corners[1].x=x2;
1116 corners[1].y=y2;
1117 if(!LPtoDP(dc->hSelf, corners, 2))
1118 return FALSE;
1120 /* Make sure first corner is top left and second corner is bottom right */
1121 if(corners[0].x>corners[1].x)
1123 temp=corners[0].x;
1124 corners[0].x=corners[1].x;
1125 corners[1].x=temp;
1127 if(corners[0].y>corners[1].y)
1129 temp=corners[0].y;
1130 corners[0].y=corners[1].y;
1131 corners[1].y=temp;
1134 /* In GM_COMPATIBLE, don't include bottom and right edges */
1135 if(dc->GraphicsMode==GM_COMPATIBLE)
1137 corners[1].x--;
1138 corners[1].y--;
1141 return TRUE;
1144 /* PATH_AddFlatBezier
1146 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
1148 POINT *pts;
1149 INT no, i;
1151 pts = GDI_Bezier( pt, 4, &no );
1152 if(!pts) return FALSE;
1154 for(i = 1; i < no; i++)
1155 PATH_AddEntry(pPath, &pts[i],
1156 (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
1157 HeapFree( GetProcessHeap(), 0, pts );
1158 return TRUE;
1161 /* PATH_FlattenPath
1163 * Replaces Beziers with line segments
1166 static BOOL PATH_FlattenPath(GdiPath *pPath)
1168 GdiPath newPath;
1169 INT srcpt;
1171 memset(&newPath, 0, sizeof(newPath));
1172 newPath.state = PATH_Open;
1173 for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
1174 switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
1175 case PT_MOVETO:
1176 case PT_LINETO:
1177 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
1178 pPath->pFlags[srcpt]);
1179 break;
1180 case PT_BEZIERTO:
1181 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
1182 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
1183 srcpt += 2;
1184 break;
1187 newPath.state = PATH_Closed;
1188 PATH_AssignGdiPath(pPath, &newPath);
1189 PATH_EmptyPath(&newPath);
1190 return TRUE;
1193 /* PATH_PathToRegion
1195 * Creates a region from the specified path using the specified polygon
1196 * filling mode. The path is left unchanged. A handle to the region that
1197 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
1198 * error occurs, SetLastError is called with the appropriate value and
1199 * FALSE is returned.
1201 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
1202 HRGN *pHrgn)
1204 int numStrokes, iStroke, i;
1205 INT *pNumPointsInStroke;
1206 HRGN hrgn;
1208 assert(pPath!=NULL);
1209 assert(pHrgn!=NULL);
1211 PATH_FlattenPath(pPath);
1213 /* FIXME: What happens when number of points is zero? */
1215 /* First pass: Find out how many strokes there are in the path */
1216 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
1217 numStrokes=0;
1218 for(i=0; i<pPath->numEntriesUsed; i++)
1219 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1220 numStrokes++;
1222 /* Allocate memory for number-of-points-in-stroke array */
1223 pNumPointsInStroke=(int *)HeapAlloc( GetProcessHeap(), 0,
1224 sizeof(int) * numStrokes );
1225 if(!pNumPointsInStroke)
1227 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1228 return FALSE;
1231 /* Second pass: remember number of points in each polygon */
1232 iStroke=-1; /* Will get incremented to 0 at beginning of first stroke */
1233 for(i=0; i<pPath->numEntriesUsed; i++)
1235 /* Is this the beginning of a new stroke? */
1236 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1238 iStroke++;
1239 pNumPointsInStroke[iStroke]=0;
1242 pNumPointsInStroke[iStroke]++;
1245 /* Create a region from the strokes */
1246 hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
1247 numStrokes, nPolyFillMode);
1249 /* Free memory for number-of-points-in-stroke array */
1250 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
1252 if(hrgn==(HRGN)0)
1254 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1255 return FALSE;
1258 /* Success! */
1259 *pHrgn=hrgn;
1260 return TRUE;
1263 /* PATH_EmptyPath
1265 * Removes all entries from the path and sets the path state to PATH_Null.
1267 static void PATH_EmptyPath(GdiPath *pPath)
1269 assert(pPath!=NULL);
1271 pPath->state=PATH_Null;
1272 pPath->numEntriesUsed=0;
1275 /* PATH_AddEntry
1277 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1278 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1279 * successful, FALSE otherwise (e.g. if not enough memory was available).
1281 BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
1283 assert(pPath!=NULL);
1285 /* FIXME: If newStroke is true, perhaps we want to check that we're
1286 * getting a PT_MOVETO
1288 TRACE("(%ld,%ld) - %d\n", pPoint->x, pPoint->y, flags);
1290 /* Check that path is open */
1291 if(pPath->state!=PATH_Open)
1292 return FALSE;
1294 /* Reserve enough memory for an extra path entry */
1295 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
1296 return FALSE;
1298 /* Store information in path entry */
1299 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
1300 pPath->pFlags[pPath->numEntriesUsed]=flags;
1302 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1303 if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
1304 pPath->newStroke=TRUE;
1306 /* Increment entry count */
1307 pPath->numEntriesUsed++;
1309 return TRUE;
1312 /* PATH_ReserveEntries
1314 * Ensures that at least "numEntries" entries (for points and flags) have
1315 * been allocated; allocates larger arrays and copies the existing entries
1316 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1318 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries)
1320 INT numEntriesToAllocate;
1321 POINT *pPointsNew;
1322 BYTE *pFlagsNew;
1324 assert(pPath!=NULL);
1325 assert(numEntries>=0);
1327 /* Do we have to allocate more memory? */
1328 if(numEntries > pPath->numEntriesAllocated)
1330 /* Find number of entries to allocate. We let the size of the array
1331 * grow exponentially, since that will guarantee linear time
1332 * complexity. */
1333 if(pPath->numEntriesAllocated)
1335 numEntriesToAllocate=pPath->numEntriesAllocated;
1336 while(numEntriesToAllocate<numEntries)
1337 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
1338 GROW_FACTOR_DENOM;
1340 else
1341 numEntriesToAllocate=numEntries;
1343 /* Allocate new arrays */
1344 pPointsNew=(POINT *)HeapAlloc( GetProcessHeap(), 0,
1345 numEntriesToAllocate * sizeof(POINT) );
1346 if(!pPointsNew)
1347 return FALSE;
1348 pFlagsNew=(BYTE *)HeapAlloc( GetProcessHeap(), 0,
1349 numEntriesToAllocate * sizeof(BYTE) );
1350 if(!pFlagsNew)
1352 HeapFree( GetProcessHeap(), 0, pPointsNew );
1353 return FALSE;
1356 /* Copy old arrays to new arrays and discard old arrays */
1357 if(pPath->pPoints)
1359 assert(pPath->pFlags);
1361 memcpy(pPointsNew, pPath->pPoints,
1362 sizeof(POINT)*pPath->numEntriesUsed);
1363 memcpy(pFlagsNew, pPath->pFlags,
1364 sizeof(BYTE)*pPath->numEntriesUsed);
1366 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
1367 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
1369 pPath->pPoints=pPointsNew;
1370 pPath->pFlags=pFlagsNew;
1371 pPath->numEntriesAllocated=numEntriesToAllocate;
1374 return TRUE;
1377 /* PATH_DoArcPart
1379 * Creates a Bezier spline that corresponds to part of an arc and appends the
1380 * corresponding points to the path. The start and end angles are passed in
1381 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1382 * at most. If "addMoveTo" is true, a PT_MOVETO entry for the first control
1383 * point is added to the path; otherwise, it is assumed that the current
1384 * position is equal to the first control point.
1386 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
1387 double angleStart, double angleEnd, BOOL addMoveTo)
1389 double halfAngle, a;
1390 double xNorm[4], yNorm[4];
1391 POINT point;
1392 int i;
1394 assert(fabs(angleEnd-angleStart)<=M_PI_2);
1396 /* FIXME: Is there an easier way of computing this? */
1398 /* Compute control points */
1399 halfAngle=(angleEnd-angleStart)/2.0;
1400 if(fabs(halfAngle)>1e-8)
1402 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
1403 xNorm[0]=cos(angleStart);
1404 yNorm[0]=sin(angleStart);
1405 xNorm[1]=xNorm[0] - a*yNorm[0];
1406 yNorm[1]=yNorm[0] + a*xNorm[0];
1407 xNorm[3]=cos(angleEnd);
1408 yNorm[3]=sin(angleEnd);
1409 xNorm[2]=xNorm[3] + a*yNorm[3];
1410 yNorm[2]=yNorm[3] - a*xNorm[3];
1412 else
1413 for(i=0; i<4; i++)
1415 xNorm[i]=cos(angleStart);
1416 yNorm[i]=sin(angleStart);
1419 /* Add starting point to path if desired */
1420 if(addMoveTo)
1422 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1423 if(!PATH_AddEntry(pPath, &point, PT_MOVETO))
1424 return FALSE;
1427 /* Add remaining control points */
1428 for(i=1; i<4; i++)
1430 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1431 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1432 return FALSE;
1435 return TRUE;
1438 /* PATH_ScaleNormalizedPoint
1440 * Scales a normalized point (x, y) with respect to the box whose corners are
1441 * passed in "corners". The point is stored in "*pPoint". The normalized
1442 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1443 * (1.0, 1.0) correspond to corners[1].
1445 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
1446 double y, POINT *pPoint)
1448 pPoint->x=GDI_ROUND( (double)corners[0].x +
1449 (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
1450 pPoint->y=GDI_ROUND( (double)corners[0].y +
1451 (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
1454 /* PATH_NormalizePoint
1456 * Normalizes a point with respect to the box whose corners are passed in
1457 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1459 static void PATH_NormalizePoint(FLOAT_POINT corners[],
1460 const FLOAT_POINT *pPoint,
1461 double *pX, double *pY)
1463 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) *
1464 2.0 - 1.0;
1465 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1466 2.0 - 1.0;
1469 /*******************************************************************
1470 * FlattenPath [GDI.516]
1474 BOOL16 WINAPI FlattenPath16(HDC16 hdc)
1476 return (BOOL16) FlattenPath((HDC) hdc);
1479 /*******************************************************************
1480 * FlattenPath [GDI32.@]
1484 BOOL WINAPI FlattenPath(HDC hdc)
1486 BOOL ret = FALSE;
1487 DC *dc = DC_GetDCPtr( hdc );
1489 if(!dc) return FALSE;
1491 if(dc->funcs->pFlattenPath) ret = dc->funcs->pFlattenPath(dc->physDev);
1492 else
1494 GdiPath *pPath = &dc->path;
1495 if(pPath->state != PATH_Closed)
1496 ret = PATH_FlattenPath(pPath);
1498 GDI_ReleaseObj( hdc );
1499 return ret;
1503 static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1505 INT i;
1506 POINT ptLastMove = {0,0};
1507 POINT ptViewportOrg, ptWindowOrg;
1508 SIZE szViewportExt, szWindowExt;
1509 DWORD mapMode, graphicsMode;
1510 XFORM xform;
1511 BOOL ret = TRUE;
1513 if(dc->funcs->pStrokePath)
1514 return dc->funcs->pStrokePath(dc->physDev);
1516 if(pPath->state != PATH_Closed)
1517 return FALSE;
1519 /* Save the mapping mode info */
1520 mapMode=GetMapMode(dc->hSelf);
1521 GetViewportExtEx(dc->hSelf, &szViewportExt);
1522 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
1523 GetWindowExtEx(dc->hSelf, &szWindowExt);
1524 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
1525 GetWorldTransform(dc->hSelf, &xform);
1527 /* Set MM_TEXT */
1528 SetMapMode(dc->hSelf, MM_TEXT);
1529 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
1530 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
1533 for(i = 0; i < pPath->numEntriesUsed; i++) {
1534 switch(pPath->pFlags[i]) {
1535 case PT_MOVETO:
1536 TRACE("Got PT_MOVETO (%ld, %ld)\n",
1537 pPath->pPoints[i].x, pPath->pPoints[i].y);
1538 MoveToEx(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y, NULL);
1539 ptLastMove = pPath->pPoints[i];
1540 break;
1541 case PT_LINETO:
1542 case (PT_LINETO | PT_CLOSEFIGURE):
1543 TRACE("Got PT_LINETO (%ld, %ld)\n",
1544 pPath->pPoints[i].x, pPath->pPoints[i].y);
1545 LineTo(dc->hSelf, pPath->pPoints[i].x, pPath->pPoints[i].y);
1546 break;
1547 case PT_BEZIERTO:
1548 TRACE("Got PT_BEZIERTO\n");
1549 if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1550 (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1551 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1552 ret = FALSE;
1553 goto end;
1555 PolyBezierTo(dc->hSelf, &pPath->pPoints[i], 3);
1556 i += 2;
1557 break;
1558 default:
1559 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1560 ret = FALSE;
1561 goto end;
1563 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1564 LineTo(dc->hSelf, ptLastMove.x, ptLastMove.y);
1567 end:
1569 /* Restore the old mapping mode */
1570 SetMapMode(dc->hSelf, mapMode);
1571 SetViewportExtEx(dc->hSelf, szViewportExt.cx, szViewportExt.cy, NULL);
1572 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
1573 SetWindowExtEx(dc->hSelf, szWindowExt.cx, szWindowExt.cy, NULL);
1574 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
1576 /* Go to GM_ADVANCED temporarily to restore the world transform */
1577 graphicsMode=GetGraphicsMode(dc->hSelf);
1578 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1579 SetWorldTransform(dc->hSelf, &xform);
1580 SetGraphicsMode(dc->hSelf, graphicsMode);
1581 return ret;
1585 /*******************************************************************
1586 * StrokeAndFillPath [GDI.520]
1590 BOOL16 WINAPI StrokeAndFillPath16(HDC16 hdc)
1592 return (BOOL16) StrokeAndFillPath((HDC) hdc);
1595 /*******************************************************************
1596 * StrokeAndFillPath [GDI32.@]
1600 BOOL WINAPI StrokeAndFillPath(HDC hdc)
1602 DC *dc = DC_GetDCPtr( hdc );
1603 BOOL bRet = FALSE;
1605 if(!dc) return FALSE;
1607 if(dc->funcs->pStrokeAndFillPath)
1608 bRet = dc->funcs->pStrokeAndFillPath(dc->physDev);
1609 else
1611 bRet = PATH_FillPath(dc, &dc->path);
1612 if(bRet) bRet = PATH_StrokePath(dc, &dc->path);
1613 if(bRet) PATH_EmptyPath(&dc->path);
1615 GDI_ReleaseObj( hdc );
1616 return bRet;
1619 /*******************************************************************
1620 * StrokePath [GDI.521]
1624 BOOL16 WINAPI StrokePath16(HDC16 hdc)
1626 return (BOOL16) StrokePath((HDC) hdc);
1629 /*******************************************************************
1630 * StrokePath [GDI32.@]
1634 BOOL WINAPI StrokePath(HDC hdc)
1636 DC *dc = DC_GetDCPtr( hdc );
1637 GdiPath *pPath;
1638 BOOL bRet = FALSE;
1640 TRACE("(%08x)\n", hdc);
1641 if(!dc) return FALSE;
1643 if(dc->funcs->pStrokePath)
1644 bRet = dc->funcs->pStrokePath(dc->physDev);
1645 else
1647 pPath = &dc->path;
1648 bRet = PATH_StrokePath(dc, pPath);
1649 PATH_EmptyPath(pPath);
1651 GDI_ReleaseObj( hdc );
1652 return bRet;
1655 /*******************************************************************
1656 * WidenPath [GDI.522]
1660 BOOL16 WINAPI WidenPath16(HDC16 hdc)
1662 return (BOOL16) WidenPath((HDC) hdc);
1665 /*******************************************************************
1666 * WidenPath [GDI32.@]
1670 BOOL WINAPI WidenPath(HDC hdc)
1672 DC *dc = DC_GetDCPtr( hdc );
1673 BOOL ret = FALSE;
1675 if(!dc) return FALSE;
1677 if(dc->funcs->pWidenPath)
1678 ret = dc->funcs->pWidenPath(dc->physDev);
1680 FIXME("stub\n");
1681 GDI_ReleaseObj( hdc );
1682 return ret;