Release 0.9.14.
[wine/multimedia.git] / dlls / gdi / path.c
blobb7d8f5d1e77255f848967b33e5510a22116099fa
1 /*
2 * Graphics paths (BeginPath, EndPath etc.)
4 * Copyright 1997, 1998 Martin Boehme
5 * 1999 Huw D M Davies
6 * Copyright 2005 Dmitry Timoshkov
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <math.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #if defined(HAVE_FLOAT_H)
32 #include <float.h>
33 #endif
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wingdi.h"
38 #include "winerror.h"
40 #include "gdi.h"
41 #include "gdi_private.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
46 /* Notes on the implementation
48 * The implementation is based on dynamically resizable arrays of points and
49 * flags. I dithered for a bit before deciding on this implementation, and
50 * I had even done a bit of work on a linked list version before switching
51 * to arrays. It's a bit of a tradeoff. When you use linked lists, the
52 * implementation of FlattenPath is easier, because you can rip the
53 * PT_BEZIERTO entries out of the middle of the list and link the
54 * corresponding PT_LINETO entries in. However, when you use arrays,
55 * PathToRegion becomes easier, since you can essentially just pass your array
56 * of points to CreatePolyPolygonRgn. Also, if I'd used linked lists, I would
57 * have had the extra effort of creating a chunk-based allocation scheme
58 * in order to use memory effectively. That's why I finally decided to use
59 * arrays. Note by the way that the array based implementation has the same
60 * linear time complexity that linked lists would have since the arrays grow
61 * exponentially.
63 * The points are stored in the path in device coordinates. This is
64 * consistent with the way Windows does things (for instance, see the Win32
65 * SDK documentation for GetPath).
67 * The word "stroke" appears in several places (e.g. in the flag
68 * GdiPath.newStroke). A stroke consists of a PT_MOVETO followed by one or
69 * more PT_LINETOs or PT_BEZIERTOs, up to, but not including, the next
70 * PT_MOVETO. Note that this is not the same as the definition of a figure;
71 * a figure can contain several strokes.
73 * I modified the drawing functions (MoveTo, LineTo etc.) to test whether
74 * the path is open and to call the corresponding function in path.c if this
75 * is the case. A more elegant approach would be to modify the function
76 * pointers in the DC_FUNCTIONS structure; however, this would be a lot more
77 * complex. Also, the performance degradation caused by my approach in the
78 * case where no path is open is so small that it cannot be measured.
80 * Martin Boehme
83 /* FIXME: A lot of stuff isn't implemented yet. There is much more to come. */
85 #define NUM_ENTRIES_INITIAL 16 /* Initial size of points / flags arrays */
86 #define GROW_FACTOR_NUMER 2 /* Numerator of grow factor for the array */
87 #define GROW_FACTOR_DENOM 1 /* Denominator of grow factor */
89 /* A floating point version of the POINT structure */
90 typedef struct tagFLOAT_POINT
92 FLOAT x, y;
93 } FLOAT_POINT;
96 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
97 HRGN *pHrgn);
98 static void PATH_EmptyPath(GdiPath *pPath);
99 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries);
100 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
101 double angleStart, double angleEnd, BOOL addMoveTo);
102 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
103 double y, POINT *pPoint);
104 static void PATH_NormalizePoint(FLOAT_POINT corners[], const FLOAT_POINT
105 *pPoint, double *pX, double *pY);
106 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2);
108 /* Performs a world-to-viewport transformation on the specified point (which
109 * is in floating point format).
111 static inline void INTERNAL_LPTODP_FLOAT(DC *dc, FLOAT_POINT *point)
113 FLOAT x, y;
115 /* Perform the transformation */
116 x = point->x;
117 y = point->y;
118 point->x = x * dc->xformWorld2Vport.eM11 +
119 y * dc->xformWorld2Vport.eM21 +
120 dc->xformWorld2Vport.eDx;
121 point->y = x * dc->xformWorld2Vport.eM12 +
122 y * dc->xformWorld2Vport.eM22 +
123 dc->xformWorld2Vport.eDy;
126 /* Performs a world-to-viewport transformation on the specified width.
128 static inline void INTERNAL_WSTODS(DC *dc, DWORD *width)
130 POINT pt[2];
131 pt[0].x = pt[0].y = 0;
132 pt[1].x = *width;
133 pt[1].y = 0;
134 LPtoDP(dc->hSelf, pt, 2);
135 *width = pt[1].x - pt[0].x;
138 /***********************************************************************
139 * BeginPath (GDI32.@)
141 BOOL WINAPI BeginPath(HDC hdc)
143 BOOL ret = TRUE;
144 DC *dc = DC_GetDCPtr( hdc );
146 if(!dc) return FALSE;
148 if(dc->funcs->pBeginPath)
149 ret = dc->funcs->pBeginPath(dc->physDev);
150 else
152 /* If path is already open, do nothing */
153 if(dc->path.state != PATH_Open)
155 /* Make sure that path is empty */
156 PATH_EmptyPath(&dc->path);
158 /* Initialize variables for new path */
159 dc->path.newStroke=TRUE;
160 dc->path.state=PATH_Open;
163 GDI_ReleaseObj( hdc );
164 return ret;
168 /***********************************************************************
169 * EndPath (GDI32.@)
171 BOOL WINAPI EndPath(HDC hdc)
173 BOOL ret = TRUE;
174 DC *dc = DC_GetDCPtr( hdc );
176 if(!dc) return FALSE;
178 if(dc->funcs->pEndPath)
179 ret = dc->funcs->pEndPath(dc->physDev);
180 else
182 /* Check that path is currently being constructed */
183 if(dc->path.state!=PATH_Open)
185 SetLastError(ERROR_CAN_NOT_COMPLETE);
186 ret = FALSE;
188 /* Set flag to indicate that path is finished */
189 else dc->path.state=PATH_Closed;
191 GDI_ReleaseObj( hdc );
192 return ret;
196 /******************************************************************************
197 * AbortPath [GDI32.@]
198 * Closes and discards paths from device context
200 * NOTES
201 * Check that SetLastError is being called correctly
203 * PARAMS
204 * hdc [I] Handle to device context
206 * RETURNS
207 * Success: TRUE
208 * Failure: FALSE
210 BOOL WINAPI AbortPath( HDC hdc )
212 BOOL ret = TRUE;
213 DC *dc = DC_GetDCPtr( hdc );
215 if(!dc) return FALSE;
217 if(dc->funcs->pAbortPath)
218 ret = dc->funcs->pAbortPath(dc->physDev);
219 else /* Remove all entries from the path */
220 PATH_EmptyPath( &dc->path );
221 GDI_ReleaseObj( hdc );
222 return ret;
226 /***********************************************************************
227 * CloseFigure (GDI32.@)
229 * FIXME: Check that SetLastError is being called correctly
231 BOOL WINAPI CloseFigure(HDC hdc)
233 BOOL ret = TRUE;
234 DC *dc = DC_GetDCPtr( hdc );
236 if(!dc) return FALSE;
238 if(dc->funcs->pCloseFigure)
239 ret = dc->funcs->pCloseFigure(dc->physDev);
240 else
242 /* Check that path is open */
243 if(dc->path.state!=PATH_Open)
245 SetLastError(ERROR_CAN_NOT_COMPLETE);
246 ret = FALSE;
248 else
250 /* FIXME: Shouldn't we draw a line to the beginning of the
251 figure? */
252 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
253 if(dc->path.numEntriesUsed)
255 dc->path.pFlags[dc->path.numEntriesUsed-1]|=PT_CLOSEFIGURE;
256 dc->path.newStroke=TRUE;
260 GDI_ReleaseObj( hdc );
261 return ret;
265 /***********************************************************************
266 * GetPath (GDI32.@)
268 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
269 INT nSize)
271 INT ret = -1;
272 GdiPath *pPath;
273 DC *dc = DC_GetDCPtr( hdc );
275 if(!dc) return -1;
277 pPath = &dc->path;
279 /* Check that path is closed */
280 if(pPath->state!=PATH_Closed)
282 SetLastError(ERROR_CAN_NOT_COMPLETE);
283 goto done;
286 if(nSize==0)
287 ret = pPath->numEntriesUsed;
288 else if(nSize<pPath->numEntriesUsed)
290 SetLastError(ERROR_INVALID_PARAMETER);
291 goto done;
293 else
295 memcpy(pPoints, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
296 memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
298 /* Convert the points to logical coordinates */
299 if(!DPtoLP(hdc, pPoints, pPath->numEntriesUsed))
301 /* FIXME: Is this the correct value? */
302 SetLastError(ERROR_CAN_NOT_COMPLETE);
303 goto done;
305 else ret = pPath->numEntriesUsed;
307 done:
308 GDI_ReleaseObj( hdc );
309 return ret;
313 /***********************************************************************
314 * PathToRegion (GDI32.@)
316 * FIXME
317 * Check that SetLastError is being called correctly
319 * The documentation does not state this explicitly, but a test under Windows
320 * shows that the region which is returned should be in device coordinates.
322 HRGN WINAPI PathToRegion(HDC hdc)
324 GdiPath *pPath;
325 HRGN hrgnRval = 0;
326 DC *dc = DC_GetDCPtr( hdc );
328 /* Get pointer to path */
329 if(!dc) return 0;
331 pPath = &dc->path;
333 /* Check that path is closed */
334 if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
335 else
337 /* FIXME: Should we empty the path even if conversion failed? */
338 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
339 PATH_EmptyPath(pPath);
340 else
341 hrgnRval=0;
343 GDI_ReleaseObj( hdc );
344 return hrgnRval;
347 static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
349 INT mapMode, graphicsMode;
350 SIZE ptViewportExt, ptWindowExt;
351 POINT ptViewportOrg, ptWindowOrg;
352 XFORM xform;
353 HRGN hrgn;
355 if(dc->funcs->pFillPath)
356 return dc->funcs->pFillPath(dc->physDev);
358 /* Check that path is closed */
359 if(pPath->state!=PATH_Closed)
361 SetLastError(ERROR_CAN_NOT_COMPLETE);
362 return FALSE;
365 /* Construct a region from the path and fill it */
366 if(PATH_PathToRegion(pPath, dc->polyFillMode, &hrgn))
368 /* Since PaintRgn interprets the region as being in logical coordinates
369 * but the points we store for the path are already in device
370 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
371 * Using SaveDC to save information about the mapping mode / world
372 * transform would be easier but would require more overhead, especially
373 * now that SaveDC saves the current path.
376 /* Save the information about the old mapping mode */
377 mapMode=GetMapMode(dc->hSelf);
378 GetViewportExtEx(dc->hSelf, &ptViewportExt);
379 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
380 GetWindowExtEx(dc->hSelf, &ptWindowExt);
381 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
383 /* Save world transform
384 * NB: The Windows documentation on world transforms would lead one to
385 * believe that this has to be done only in GM_ADVANCED; however, my
386 * tests show that resetting the graphics mode to GM_COMPATIBLE does
387 * not reset the world transform.
389 GetWorldTransform(dc->hSelf, &xform);
391 /* Set MM_TEXT */
392 SetMapMode(dc->hSelf, MM_TEXT);
393 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
394 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
395 graphicsMode=GetGraphicsMode(dc->hSelf);
396 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
397 ModifyWorldTransform(dc->hSelf, &xform, MWT_IDENTITY);
398 SetGraphicsMode(dc->hSelf, graphicsMode);
400 /* Paint the region */
401 PaintRgn(dc->hSelf, hrgn);
402 DeleteObject(hrgn);
403 /* Restore the old mapping mode */
404 SetMapMode(dc->hSelf, mapMode);
405 SetViewportExtEx(dc->hSelf, ptViewportExt.cx, ptViewportExt.cy, NULL);
406 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
407 SetWindowExtEx(dc->hSelf, ptWindowExt.cx, ptWindowExt.cy, NULL);
408 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
410 /* Go to GM_ADVANCED temporarily to restore the world transform */
411 graphicsMode=GetGraphicsMode(dc->hSelf);
412 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
413 SetWorldTransform(dc->hSelf, &xform);
414 SetGraphicsMode(dc->hSelf, graphicsMode);
415 return TRUE;
417 return FALSE;
421 /***********************************************************************
422 * FillPath (GDI32.@)
424 * FIXME
425 * Check that SetLastError is being called correctly
427 BOOL WINAPI FillPath(HDC hdc)
429 DC *dc = DC_GetDCPtr( hdc );
430 BOOL bRet = FALSE;
432 if(!dc) return FALSE;
434 if(dc->funcs->pFillPath)
435 bRet = dc->funcs->pFillPath(dc->physDev);
436 else
438 bRet = PATH_FillPath(dc, &dc->path);
439 if(bRet)
441 /* FIXME: Should the path be emptied even if conversion
442 failed? */
443 PATH_EmptyPath(&dc->path);
446 GDI_ReleaseObj( hdc );
447 return bRet;
451 /***********************************************************************
452 * SelectClipPath (GDI32.@)
453 * FIXME
454 * Check that SetLastError is being called correctly
456 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
458 GdiPath *pPath;
459 HRGN hrgnPath;
460 BOOL success = FALSE;
461 DC *dc = DC_GetDCPtr( hdc );
463 if(!dc) return FALSE;
465 if(dc->funcs->pSelectClipPath)
466 success = dc->funcs->pSelectClipPath(dc->physDev, iMode);
467 else
469 pPath = &dc->path;
471 /* Check that path is closed */
472 if(pPath->state!=PATH_Closed)
473 SetLastError(ERROR_CAN_NOT_COMPLETE);
474 /* Construct a region from the path */
475 else if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnPath))
477 success = ExtSelectClipRgn( hdc, hrgnPath, iMode ) != ERROR;
478 DeleteObject(hrgnPath);
480 /* Empty the path */
481 if(success)
482 PATH_EmptyPath(pPath);
483 /* FIXME: Should this function delete the path even if it failed? */
486 GDI_ReleaseObj( hdc );
487 return success;
491 /***********************************************************************
492 * Exported functions
495 /* PATH_InitGdiPath
497 * Initializes the GdiPath structure.
499 void PATH_InitGdiPath(GdiPath *pPath)
501 assert(pPath!=NULL);
503 pPath->state=PATH_Null;
504 pPath->pPoints=NULL;
505 pPath->pFlags=NULL;
506 pPath->numEntriesUsed=0;
507 pPath->numEntriesAllocated=0;
510 /* PATH_DestroyGdiPath
512 * Destroys a GdiPath structure (frees the memory in the arrays).
514 void PATH_DestroyGdiPath(GdiPath *pPath)
516 assert(pPath!=NULL);
518 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
519 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
522 /* PATH_AssignGdiPath
524 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
525 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
526 * not just the pointers. Since this means that the arrays in pPathDest may
527 * need to be resized, pPathDest should have been initialized using
528 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
529 * not a copy constructor).
530 * Returns TRUE if successful, else FALSE.
532 BOOL PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
534 assert(pPathDest!=NULL && pPathSrc!=NULL);
536 /* Make sure destination arrays are big enough */
537 if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
538 return FALSE;
540 /* Perform the copy operation */
541 memcpy(pPathDest->pPoints, pPathSrc->pPoints,
542 sizeof(POINT)*pPathSrc->numEntriesUsed);
543 memcpy(pPathDest->pFlags, pPathSrc->pFlags,
544 sizeof(BYTE)*pPathSrc->numEntriesUsed);
546 pPathDest->state=pPathSrc->state;
547 pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
548 pPathDest->newStroke=pPathSrc->newStroke;
550 return TRUE;
553 /* PATH_MoveTo
555 * Should be called when a MoveTo is performed on a DC that has an
556 * open path. This starts a new stroke. Returns TRUE if successful, else
557 * FALSE.
559 BOOL PATH_MoveTo(DC *dc)
561 GdiPath *pPath = &dc->path;
563 /* Check that path is open */
564 if(pPath->state!=PATH_Open)
565 /* FIXME: Do we have to call SetLastError? */
566 return FALSE;
568 /* Start a new stroke */
569 pPath->newStroke=TRUE;
571 return TRUE;
574 /* PATH_LineTo
576 * Should be called when a LineTo is performed on a DC that has an
577 * open path. This adds a PT_LINETO entry to the path (and possibly
578 * a PT_MOVETO entry, if this is the first LineTo in a stroke).
579 * Returns TRUE if successful, else FALSE.
581 BOOL PATH_LineTo(DC *dc, INT x, INT y)
583 GdiPath *pPath = &dc->path;
584 POINT point, pointCurPos;
586 /* Check that path is open */
587 if(pPath->state!=PATH_Open)
588 return FALSE;
590 /* Convert point to device coordinates */
591 point.x=x;
592 point.y=y;
593 if(!LPtoDP(dc->hSelf, &point, 1))
594 return FALSE;
596 /* Add a PT_MOVETO if necessary */
597 if(pPath->newStroke)
599 pPath->newStroke=FALSE;
600 pointCurPos.x = dc->CursPosX;
601 pointCurPos.y = dc->CursPosY;
602 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
603 return FALSE;
604 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
605 return FALSE;
608 /* Add a PT_LINETO entry */
609 return PATH_AddEntry(pPath, &point, PT_LINETO);
612 /* PATH_RoundRect
614 * Should be called when a call to RoundRect is performed on a DC that has
615 * an open path. Returns TRUE if successful, else FALSE.
617 * FIXME: it adds the same entries to the path as windows does, but there
618 * is an error in the bezier drawing code so that there are small pixel-size
619 * gaps when the resulting path is drawn by StrokePath()
621 BOOL PATH_RoundRect(DC *dc, INT x1, INT y1, INT x2, INT y2, INT ell_width, INT ell_height)
623 GdiPath *pPath = &dc->path;
624 POINT corners[2], pointTemp;
625 FLOAT_POINT ellCorners[2];
627 /* Check that path is open */
628 if(pPath->state!=PATH_Open)
629 return FALSE;
631 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
632 return FALSE;
634 /* Add points to the roundrect path */
635 ellCorners[0].x = corners[1].x-ell_width;
636 ellCorners[0].y = corners[0].y;
637 ellCorners[1].x = corners[1].x;
638 ellCorners[1].y = corners[0].y+ell_height;
639 if(!PATH_DoArcPart(pPath, ellCorners, 0, -M_PI_2, TRUE))
640 return FALSE;
641 pointTemp.x = corners[0].x+ell_width/2;
642 pointTemp.y = corners[0].y;
643 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
644 return FALSE;
645 ellCorners[0].x = corners[0].x;
646 ellCorners[1].x = corners[0].x+ell_width;
647 if(!PATH_DoArcPart(pPath, ellCorners, -M_PI_2, -M_PI, FALSE))
648 return FALSE;
649 pointTemp.x = corners[0].x;
650 pointTemp.y = corners[1].y-ell_height/2;
651 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
652 return FALSE;
653 ellCorners[0].y = corners[1].y-ell_height;
654 ellCorners[1].y = corners[1].y;
655 if(!PATH_DoArcPart(pPath, ellCorners, M_PI, M_PI_2, FALSE))
656 return FALSE;
657 pointTemp.x = corners[1].x-ell_width/2;
658 pointTemp.y = corners[1].y;
659 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
660 return FALSE;
661 ellCorners[0].x = corners[1].x-ell_width;
662 ellCorners[1].x = corners[1].x;
663 if(!PATH_DoArcPart(pPath, ellCorners, M_PI_2, 0, FALSE))
664 return FALSE;
666 /* Close the roundrect figure */
667 if(!CloseFigure(dc->hSelf))
668 return FALSE;
670 return TRUE;
673 /* PATH_Rectangle
675 * Should be called when a call to Rectangle is performed on a DC that has
676 * an open path. Returns TRUE if successful, else FALSE.
678 BOOL PATH_Rectangle(DC *dc, INT x1, INT y1, INT x2, INT y2)
680 GdiPath *pPath = &dc->path;
681 POINT corners[2], pointTemp;
683 /* Check that path is open */
684 if(pPath->state!=PATH_Open)
685 return FALSE;
687 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
688 return FALSE;
690 /* Close any previous figure */
691 if(!CloseFigure(dc->hSelf))
693 /* The CloseFigure call shouldn't have failed */
694 assert(FALSE);
695 return FALSE;
698 /* Add four points to the path */
699 pointTemp.x=corners[1].x;
700 pointTemp.y=corners[0].y;
701 if(!PATH_AddEntry(pPath, &pointTemp, PT_MOVETO))
702 return FALSE;
703 if(!PATH_AddEntry(pPath, corners, PT_LINETO))
704 return FALSE;
705 pointTemp.x=corners[0].x;
706 pointTemp.y=corners[1].y;
707 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
708 return FALSE;
709 if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
710 return FALSE;
712 /* Close the rectangle figure */
713 if(!CloseFigure(dc->hSelf))
715 /* The CloseFigure call shouldn't have failed */
716 assert(FALSE);
717 return FALSE;
720 return TRUE;
723 /* PATH_Ellipse
725 * Should be called when a call to Ellipse is performed on a DC that has
726 * an open path. This adds four Bezier splines representing the ellipse
727 * to the path. Returns TRUE if successful, else FALSE.
729 BOOL PATH_Ellipse(DC *dc, INT x1, INT y1, INT x2, INT y2)
731 return( PATH_Arc(dc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2,0) &&
732 CloseFigure(dc->hSelf) );
735 /* PATH_Arc
737 * Should be called when a call to Arc is performed on a DC that has
738 * an open path. This adds up to five Bezier splines representing the arc
739 * to the path. When 'lines' is 1, we add 1 extra line to get a chord,
740 * and when 'lines' is 2, we add 2 extra lines to get a pie.
741 * Returns TRUE if successful, else FALSE.
743 BOOL PATH_Arc(DC *dc, INT x1, INT y1, INT x2, INT y2,
744 INT xStart, INT yStart, INT xEnd, INT yEnd, INT lines)
746 GdiPath *pPath = &dc->path;
747 double angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
748 /* Initialize angleEndQuadrant to silence gcc's warning */
749 double x, y;
750 FLOAT_POINT corners[2], pointStart, pointEnd;
751 POINT centre;
752 BOOL start, end;
753 INT temp;
755 /* FIXME: This function should check for all possible error returns */
756 /* FIXME: Do we have to respect newStroke? */
758 /* Check that path is open */
759 if(pPath->state!=PATH_Open)
760 return FALSE;
762 /* Check for zero height / width */
763 /* FIXME: Only in GM_COMPATIBLE? */
764 if(x1==x2 || y1==y2)
765 return TRUE;
767 /* Convert points to device coordinates */
768 corners[0].x=(FLOAT)x1;
769 corners[0].y=(FLOAT)y1;
770 corners[1].x=(FLOAT)x2;
771 corners[1].y=(FLOAT)y2;
772 pointStart.x=(FLOAT)xStart;
773 pointStart.y=(FLOAT)yStart;
774 pointEnd.x=(FLOAT)xEnd;
775 pointEnd.y=(FLOAT)yEnd;
776 INTERNAL_LPTODP_FLOAT(dc, corners);
777 INTERNAL_LPTODP_FLOAT(dc, corners+1);
778 INTERNAL_LPTODP_FLOAT(dc, &pointStart);
779 INTERNAL_LPTODP_FLOAT(dc, &pointEnd);
781 /* Make sure first corner is top left and second corner is bottom right */
782 if(corners[0].x>corners[1].x)
784 temp=corners[0].x;
785 corners[0].x=corners[1].x;
786 corners[1].x=temp;
788 if(corners[0].y>corners[1].y)
790 temp=corners[0].y;
791 corners[0].y=corners[1].y;
792 corners[1].y=temp;
795 /* Compute start and end angle */
796 PATH_NormalizePoint(corners, &pointStart, &x, &y);
797 angleStart=atan2(y, x);
798 PATH_NormalizePoint(corners, &pointEnd, &x, &y);
799 angleEnd=atan2(y, x);
801 /* Make sure the end angle is "on the right side" of the start angle */
802 if(dc->ArcDirection==AD_CLOCKWISE)
804 if(angleEnd<=angleStart)
806 angleEnd+=2*M_PI;
807 assert(angleEnd>=angleStart);
810 else
812 if(angleEnd>=angleStart)
814 angleEnd-=2*M_PI;
815 assert(angleEnd<=angleStart);
819 /* In GM_COMPATIBLE, don't include bottom and right edges */
820 if(dc->GraphicsMode==GM_COMPATIBLE)
822 corners[1].x--;
823 corners[1].y--;
826 /* Add the arc to the path with one Bezier spline per quadrant that the
827 * arc spans */
828 start=TRUE;
829 end=FALSE;
832 /* Determine the start and end angles for this quadrant */
833 if(start)
835 angleStartQuadrant=angleStart;
836 if(dc->ArcDirection==AD_CLOCKWISE)
837 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
838 else
839 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
841 else
843 angleStartQuadrant=angleEndQuadrant;
844 if(dc->ArcDirection==AD_CLOCKWISE)
845 angleEndQuadrant+=M_PI_2;
846 else
847 angleEndQuadrant-=M_PI_2;
850 /* Have we reached the last part of the arc? */
851 if((dc->ArcDirection==AD_CLOCKWISE &&
852 angleEnd<angleEndQuadrant) ||
853 (dc->ArcDirection==AD_COUNTERCLOCKWISE &&
854 angleEnd>angleEndQuadrant))
856 /* Adjust the end angle for this quadrant */
857 angleEndQuadrant=angleEnd;
858 end=TRUE;
861 /* Add the Bezier spline to the path */
862 PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
863 start);
864 start=FALSE;
865 } while(!end);
867 /* chord: close figure. pie: add line and close figure */
868 if(lines==1)
870 if(!CloseFigure(dc->hSelf))
871 return FALSE;
873 else if(lines==2)
875 centre.x = (corners[0].x+corners[1].x)/2;
876 centre.y = (corners[0].y+corners[1].y)/2;
877 if(!PATH_AddEntry(pPath, &centre, PT_LINETO | PT_CLOSEFIGURE))
878 return FALSE;
881 return TRUE;
884 BOOL PATH_PolyBezierTo(DC *dc, const POINT *pts, DWORD cbPoints)
886 GdiPath *pPath = &dc->path;
887 POINT pt;
888 UINT i;
890 /* Check that path is open */
891 if(pPath->state!=PATH_Open)
892 return FALSE;
894 /* Add a PT_MOVETO if necessary */
895 if(pPath->newStroke)
897 pPath->newStroke=FALSE;
898 pt.x = dc->CursPosX;
899 pt.y = dc->CursPosY;
900 if(!LPtoDP(dc->hSelf, &pt, 1))
901 return FALSE;
902 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
903 return FALSE;
906 for(i = 0; i < cbPoints; i++) {
907 pt = pts[i];
908 if(!LPtoDP(dc->hSelf, &pt, 1))
909 return FALSE;
910 PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
912 return TRUE;
915 BOOL PATH_PolyBezier(DC *dc, const POINT *pts, DWORD cbPoints)
917 GdiPath *pPath = &dc->path;
918 POINT pt;
919 UINT i;
921 /* Check that path is open */
922 if(pPath->state!=PATH_Open)
923 return FALSE;
925 for(i = 0; i < cbPoints; i++) {
926 pt = pts[i];
927 if(!LPtoDP(dc->hSelf, &pt, 1))
928 return FALSE;
929 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
931 return TRUE;
934 BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
936 GdiPath *pPath = &dc->path;
937 POINT pt;
938 UINT i;
940 /* Check that path is open */
941 if(pPath->state!=PATH_Open)
942 return FALSE;
944 for(i = 0; i < cbPoints; i++) {
945 pt = pts[i];
946 if(!LPtoDP(dc->hSelf, &pt, 1))
947 return FALSE;
948 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
950 return TRUE;
953 BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
955 GdiPath *pPath = &dc->path;
956 POINT pt;
957 UINT i;
959 /* Check that path is open */
960 if(pPath->state!=PATH_Open)
961 return FALSE;
963 /* Add a PT_MOVETO if necessary */
964 if(pPath->newStroke)
966 pPath->newStroke=FALSE;
967 pt.x = dc->CursPosX;
968 pt.y = dc->CursPosY;
969 if(!LPtoDP(dc->hSelf, &pt, 1))
970 return FALSE;
971 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
972 return FALSE;
975 for(i = 0; i < cbPoints; i++) {
976 pt = pts[i];
977 if(!LPtoDP(dc->hSelf, &pt, 1))
978 return FALSE;
979 PATH_AddEntry(pPath, &pt, PT_LINETO);
982 return TRUE;
986 BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
988 GdiPath *pPath = &dc->path;
989 POINT pt;
990 UINT i;
992 /* Check that path is open */
993 if(pPath->state!=PATH_Open)
994 return FALSE;
996 for(i = 0; i < cbPoints; i++) {
997 pt = pts[i];
998 if(!LPtoDP(dc->hSelf, &pt, 1))
999 return FALSE;
1000 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
1001 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
1002 PT_LINETO));
1004 return TRUE;
1007 BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
1008 UINT polygons )
1010 GdiPath *pPath = &dc->path;
1011 POINT pt, startpt;
1012 UINT poly, i;
1013 INT point;
1015 /* Check that path is open */
1016 if(pPath->state!=PATH_Open)
1017 return FALSE;
1019 for(i = 0, poly = 0; poly < polygons; poly++) {
1020 for(point = 0; point < counts[poly]; point++, i++) {
1021 pt = pts[i];
1022 if(!LPtoDP(dc->hSelf, &pt, 1))
1023 return FALSE;
1024 if(point == 0) startpt = pt;
1025 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1027 /* win98 adds an extra line to close the figure for some reason */
1028 PATH_AddEntry(pPath, &startpt, PT_LINETO | PT_CLOSEFIGURE);
1030 return TRUE;
1033 BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1034 DWORD polylines )
1036 GdiPath *pPath = &dc->path;
1037 POINT pt;
1038 UINT poly, point, i;
1040 /* Check that path is open */
1041 if(pPath->state!=PATH_Open)
1042 return FALSE;
1044 for(i = 0, poly = 0; poly < polylines; poly++) {
1045 for(point = 0; point < counts[poly]; point++, i++) {
1046 pt = pts[i];
1047 if(!LPtoDP(dc->hSelf, &pt, 1))
1048 return FALSE;
1049 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1052 return TRUE;
1055 /***********************************************************************
1056 * Internal functions
1059 /* PATH_CheckCorners
1061 * Helper function for PATH_RoundRect() and PATH_Rectangle()
1063 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2)
1065 INT temp;
1067 /* Convert points to device coordinates */
1068 corners[0].x=x1;
1069 corners[0].y=y1;
1070 corners[1].x=x2;
1071 corners[1].y=y2;
1072 if(!LPtoDP(dc->hSelf, corners, 2))
1073 return FALSE;
1075 /* Make sure first corner is top left and second corner is bottom right */
1076 if(corners[0].x>corners[1].x)
1078 temp=corners[0].x;
1079 corners[0].x=corners[1].x;
1080 corners[1].x=temp;
1082 if(corners[0].y>corners[1].y)
1084 temp=corners[0].y;
1085 corners[0].y=corners[1].y;
1086 corners[1].y=temp;
1089 /* In GM_COMPATIBLE, don't include bottom and right edges */
1090 if(dc->GraphicsMode==GM_COMPATIBLE)
1092 corners[1].x--;
1093 corners[1].y--;
1096 return TRUE;
1099 /* PATH_AddFlatBezier
1101 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
1103 POINT *pts;
1104 INT no, i;
1106 pts = GDI_Bezier( pt, 4, &no );
1107 if(!pts) return FALSE;
1109 for(i = 1; i < no; i++)
1110 PATH_AddEntry(pPath, &pts[i],
1111 (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
1112 HeapFree( GetProcessHeap(), 0, pts );
1113 return TRUE;
1116 /* PATH_FlattenPath
1118 * Replaces Beziers with line segments
1121 static BOOL PATH_FlattenPath(GdiPath *pPath)
1123 GdiPath newPath;
1124 INT srcpt;
1126 memset(&newPath, 0, sizeof(newPath));
1127 newPath.state = PATH_Open;
1128 for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
1129 switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
1130 case PT_MOVETO:
1131 case PT_LINETO:
1132 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
1133 pPath->pFlags[srcpt]);
1134 break;
1135 case PT_BEZIERTO:
1136 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
1137 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
1138 srcpt += 2;
1139 break;
1142 newPath.state = PATH_Closed;
1143 PATH_AssignGdiPath(pPath, &newPath);
1144 PATH_DestroyGdiPath(&newPath);
1145 return TRUE;
1148 /* PATH_PathToRegion
1150 * Creates a region from the specified path using the specified polygon
1151 * filling mode. The path is left unchanged. A handle to the region that
1152 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
1153 * error occurs, SetLastError is called with the appropriate value and
1154 * FALSE is returned.
1156 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
1157 HRGN *pHrgn)
1159 int numStrokes, iStroke, i;
1160 INT *pNumPointsInStroke;
1161 HRGN hrgn;
1163 assert(pPath!=NULL);
1164 assert(pHrgn!=NULL);
1166 PATH_FlattenPath(pPath);
1168 /* FIXME: What happens when number of points is zero? */
1170 /* First pass: Find out how many strokes there are in the path */
1171 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
1172 numStrokes=0;
1173 for(i=0; i<pPath->numEntriesUsed; i++)
1174 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1175 numStrokes++;
1177 /* Allocate memory for number-of-points-in-stroke array */
1178 pNumPointsInStroke=HeapAlloc( GetProcessHeap(), 0, sizeof(int) * numStrokes );
1179 if(!pNumPointsInStroke)
1181 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1182 return FALSE;
1185 /* Second pass: remember number of points in each polygon */
1186 iStroke=-1; /* Will get incremented to 0 at beginning of first stroke */
1187 for(i=0; i<pPath->numEntriesUsed; i++)
1189 /* Is this the beginning of a new stroke? */
1190 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1192 iStroke++;
1193 pNumPointsInStroke[iStroke]=0;
1196 pNumPointsInStroke[iStroke]++;
1199 /* Create a region from the strokes */
1200 hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
1201 numStrokes, nPolyFillMode);
1203 /* Free memory for number-of-points-in-stroke array */
1204 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
1206 if(hrgn==NULL)
1208 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1209 return FALSE;
1212 /* Success! */
1213 *pHrgn=hrgn;
1214 return TRUE;
1217 static inline INT int_from_fixed(FIXED f)
1219 return (f.fract >= 0x8000) ? (f.value + 1) : f.value;
1222 /**********************************************************************
1223 * PATH_BezierTo
1225 * internally used by PATH_add_outline
1227 static void PATH_BezierTo(GdiPath *pPath, POINT *lppt, INT n)
1229 if (n < 2) return;
1231 if (n == 2)
1233 PATH_AddEntry(pPath, &lppt[1], PT_LINETO);
1235 else if (n == 3)
1237 PATH_AddEntry(pPath, &lppt[0], PT_BEZIERTO);
1238 PATH_AddEntry(pPath, &lppt[1], PT_BEZIERTO);
1239 PATH_AddEntry(pPath, &lppt[2], PT_BEZIERTO);
1241 else
1243 POINT pt[3];
1244 INT i = 0;
1246 pt[2] = lppt[0];
1247 n--;
1249 while (n > 2)
1251 pt[0] = pt[2];
1252 pt[1] = lppt[i+1];
1253 pt[2].x = (lppt[i+2].x + lppt[i+1].x) / 2;
1254 pt[2].y = (lppt[i+2].y + lppt[i+1].y) / 2;
1255 PATH_BezierTo(pPath, pt, 3);
1256 n--;
1257 i++;
1260 pt[0] = pt[2];
1261 pt[1] = lppt[i+1];
1262 pt[2] = lppt[i+2];
1263 PATH_BezierTo(pPath, pt, 3);
1267 static BOOL PATH_add_outline(DC *dc, INT x, INT y, TTPOLYGONHEADER *header, DWORD size)
1269 GdiPath *pPath = &dc->path;
1270 TTPOLYGONHEADER *start;
1271 POINT pt;
1273 start = header;
1275 while ((char *)header < (char *)start + size)
1277 TTPOLYCURVE *curve;
1279 if (header->dwType != TT_POLYGON_TYPE)
1281 FIXME("Unknown header type %ld\n", header->dwType);
1282 return FALSE;
1285 pt.x = x + int_from_fixed(header->pfxStart.x);
1286 pt.y = y - int_from_fixed(header->pfxStart.y);
1287 LPtoDP(dc->hSelf, &pt, 1);
1288 PATH_AddEntry(pPath, &pt, PT_MOVETO);
1290 curve = (TTPOLYCURVE *)(header + 1);
1292 while ((char *)curve < (char *)header + header->cb)
1294 /*TRACE("curve->wType %d\n", curve->wType);*/
1296 switch(curve->wType)
1298 case TT_PRIM_LINE:
1300 WORD i;
1302 for (i = 0; i < curve->cpfx; i++)
1304 pt.x = x + int_from_fixed(curve->apfx[i].x);
1305 pt.y = y - int_from_fixed(curve->apfx[i].y);
1306 LPtoDP(dc->hSelf, &pt, 1);
1307 PATH_AddEntry(pPath, &pt, PT_LINETO);
1309 break;
1312 case TT_PRIM_QSPLINE:
1313 case TT_PRIM_CSPLINE:
1315 WORD i;
1316 POINTFX ptfx;
1317 POINT *pts = HeapAlloc(GetProcessHeap(), 0, (curve->cpfx + 1) * sizeof(POINT));
1319 if (!pts) return FALSE;
1321 ptfx = *(POINTFX *)((char *)curve - sizeof(POINTFX));
1323 pts[0].x = x + int_from_fixed(ptfx.x);
1324 pts[0].y = y - int_from_fixed(ptfx.y);
1325 LPtoDP(dc->hSelf, &pts[0], 1);
1327 for(i = 0; i < curve->cpfx; i++)
1329 pts[i + 1].x = x + int_from_fixed(curve->apfx[i].x);
1330 pts[i + 1].y = y - int_from_fixed(curve->apfx[i].y);
1331 LPtoDP(dc->hSelf, &pts[i + 1], 1);
1334 PATH_BezierTo(pPath, pts, curve->cpfx + 1);
1336 HeapFree(GetProcessHeap(), 0, pts);
1337 break;
1340 default:
1341 FIXME("Unknown curve type %04x\n", curve->wType);
1342 return FALSE;
1345 curve = (TTPOLYCURVE *)&curve->apfx[curve->cpfx];
1348 header = (TTPOLYGONHEADER *)((char *)header + header->cb);
1351 return CloseFigure(dc->hSelf);
1354 /**********************************************************************
1355 * PATH_ExtTextOut
1357 BOOL PATH_ExtTextOut(DC *dc, INT x, INT y, UINT flags, const RECT *lprc,
1358 LPCWSTR str, UINT count, const INT *dx)
1360 unsigned int idx;
1361 double cosEsc, sinEsc;
1362 LOGFONTW lf;
1363 POINT org;
1364 HDC hdc = dc->hSelf;
1366 TRACE("%p, %d, %d, %08x, %s, %s, %d, %p)\n", hdc, x, y, flags,
1367 wine_dbgstr_rect(lprc), debugstr_wn(str, count), count, dx);
1369 if (!count) return TRUE;
1371 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1373 if (lf.lfEscapement != 0)
1375 cosEsc = cos(lf.lfEscapement * M_PI / 1800);
1376 sinEsc = sin(lf.lfEscapement * M_PI / 1800);
1377 } else
1379 cosEsc = 1;
1380 sinEsc = 0;
1383 GetDCOrgEx(hdc, &org);
1385 for (idx = 0; idx < count; idx++)
1387 INT offset = 0, xoff = 0, yoff = 0;
1388 GLYPHMETRICS gm;
1389 DWORD dwSize;
1390 void *outline;
1392 dwSize = GetGlyphOutlineW(hdc, str[idx], GGO_GLYPH_INDEX | GGO_NATIVE, &gm, 0, NULL, NULL);
1393 if (!dwSize) return FALSE;
1395 outline = HeapAlloc(GetProcessHeap(), 0, dwSize);
1396 if (!outline) return FALSE;
1398 GetGlyphOutlineW(hdc, str[idx], GGO_GLYPH_INDEX | GGO_NATIVE, &gm, dwSize, outline, NULL);
1400 PATH_add_outline(dc, org.x + x + xoff, org.x + y + yoff, outline, dwSize);
1402 HeapFree(GetProcessHeap(), 0, outline);
1404 if (dx)
1406 offset += dx[idx];
1407 xoff = offset * cosEsc;
1408 yoff = offset * -sinEsc;
1410 else
1412 xoff += gm.gmCellIncX;
1413 yoff += gm.gmCellIncY;
1416 return TRUE;
1419 /* PATH_EmptyPath
1421 * Removes all entries from the path and sets the path state to PATH_Null.
1423 static void PATH_EmptyPath(GdiPath *pPath)
1425 assert(pPath!=NULL);
1427 pPath->state=PATH_Null;
1428 pPath->numEntriesUsed=0;
1431 /* PATH_AddEntry
1433 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1434 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1435 * successful, FALSE otherwise (e.g. if not enough memory was available).
1437 BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
1439 assert(pPath!=NULL);
1441 /* FIXME: If newStroke is true, perhaps we want to check that we're
1442 * getting a PT_MOVETO
1444 TRACE("(%ld,%ld) - %d\n", pPoint->x, pPoint->y, flags);
1446 /* Check that path is open */
1447 if(pPath->state!=PATH_Open)
1448 return FALSE;
1450 /* Reserve enough memory for an extra path entry */
1451 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
1452 return FALSE;
1454 /* Store information in path entry */
1455 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
1456 pPath->pFlags[pPath->numEntriesUsed]=flags;
1458 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1459 if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
1460 pPath->newStroke=TRUE;
1462 /* Increment entry count */
1463 pPath->numEntriesUsed++;
1465 return TRUE;
1468 /* PATH_ReserveEntries
1470 * Ensures that at least "numEntries" entries (for points and flags) have
1471 * been allocated; allocates larger arrays and copies the existing entries
1472 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1474 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries)
1476 INT numEntriesToAllocate;
1477 POINT *pPointsNew;
1478 BYTE *pFlagsNew;
1480 assert(pPath!=NULL);
1481 assert(numEntries>=0);
1483 /* Do we have to allocate more memory? */
1484 if(numEntries > pPath->numEntriesAllocated)
1486 /* Find number of entries to allocate. We let the size of the array
1487 * grow exponentially, since that will guarantee linear time
1488 * complexity. */
1489 if(pPath->numEntriesAllocated)
1491 numEntriesToAllocate=pPath->numEntriesAllocated;
1492 while(numEntriesToAllocate<numEntries)
1493 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
1494 GROW_FACTOR_DENOM;
1496 else
1497 numEntriesToAllocate=numEntries;
1499 /* Allocate new arrays */
1500 pPointsNew=HeapAlloc( GetProcessHeap(), 0, numEntriesToAllocate * sizeof(POINT) );
1501 if(!pPointsNew)
1502 return FALSE;
1503 pFlagsNew=HeapAlloc( GetProcessHeap(), 0, numEntriesToAllocate * sizeof(BYTE) );
1504 if(!pFlagsNew)
1506 HeapFree( GetProcessHeap(), 0, pPointsNew );
1507 return FALSE;
1510 /* Copy old arrays to new arrays and discard old arrays */
1511 if(pPath->pPoints)
1513 assert(pPath->pFlags);
1515 memcpy(pPointsNew, pPath->pPoints,
1516 sizeof(POINT)*pPath->numEntriesUsed);
1517 memcpy(pFlagsNew, pPath->pFlags,
1518 sizeof(BYTE)*pPath->numEntriesUsed);
1520 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
1521 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
1523 pPath->pPoints=pPointsNew;
1524 pPath->pFlags=pFlagsNew;
1525 pPath->numEntriesAllocated=numEntriesToAllocate;
1528 return TRUE;
1531 /* PATH_DoArcPart
1533 * Creates a Bezier spline that corresponds to part of an arc and appends the
1534 * corresponding points to the path. The start and end angles are passed in
1535 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1536 * at most. If "addMoveTo" is true, a PT_MOVETO entry for the first control
1537 * point is added to the path; otherwise, it is assumed that the current
1538 * position is equal to the first control point.
1540 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
1541 double angleStart, double angleEnd, BOOL addMoveTo)
1543 double halfAngle, a;
1544 double xNorm[4], yNorm[4];
1545 POINT point;
1546 int i;
1548 assert(fabs(angleEnd-angleStart)<=M_PI_2);
1550 /* FIXME: Is there an easier way of computing this? */
1552 /* Compute control points */
1553 halfAngle=(angleEnd-angleStart)/2.0;
1554 if(fabs(halfAngle)>1e-8)
1556 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
1557 xNorm[0]=cos(angleStart);
1558 yNorm[0]=sin(angleStart);
1559 xNorm[1]=xNorm[0] - a*yNorm[0];
1560 yNorm[1]=yNorm[0] + a*xNorm[0];
1561 xNorm[3]=cos(angleEnd);
1562 yNorm[3]=sin(angleEnd);
1563 xNorm[2]=xNorm[3] + a*yNorm[3];
1564 yNorm[2]=yNorm[3] - a*xNorm[3];
1566 else
1567 for(i=0; i<4; i++)
1569 xNorm[i]=cos(angleStart);
1570 yNorm[i]=sin(angleStart);
1573 /* Add starting point to path if desired */
1574 if(addMoveTo)
1576 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1577 if(!PATH_AddEntry(pPath, &point, PT_MOVETO))
1578 return FALSE;
1581 /* Add remaining control points */
1582 for(i=1; i<4; i++)
1584 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1585 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1586 return FALSE;
1589 return TRUE;
1592 /* PATH_ScaleNormalizedPoint
1594 * Scales a normalized point (x, y) with respect to the box whose corners are
1595 * passed in "corners". The point is stored in "*pPoint". The normalized
1596 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1597 * (1.0, 1.0) correspond to corners[1].
1599 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
1600 double y, POINT *pPoint)
1602 pPoint->x=GDI_ROUND( (double)corners[0].x +
1603 (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
1604 pPoint->y=GDI_ROUND( (double)corners[0].y +
1605 (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
1608 /* PATH_NormalizePoint
1610 * Normalizes a point with respect to the box whose corners are passed in
1611 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1613 static void PATH_NormalizePoint(FLOAT_POINT corners[],
1614 const FLOAT_POINT *pPoint,
1615 double *pX, double *pY)
1617 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) *
1618 2.0 - 1.0;
1619 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1620 2.0 - 1.0;
1624 /*******************************************************************
1625 * FlattenPath [GDI32.@]
1629 BOOL WINAPI FlattenPath(HDC hdc)
1631 BOOL ret = FALSE;
1632 DC *dc = DC_GetDCPtr( hdc );
1634 if(!dc) return FALSE;
1636 if(dc->funcs->pFlattenPath) ret = dc->funcs->pFlattenPath(dc->physDev);
1637 else
1639 GdiPath *pPath = &dc->path;
1640 if(pPath->state != PATH_Closed)
1641 ret = PATH_FlattenPath(pPath);
1643 GDI_ReleaseObj( hdc );
1644 return ret;
1648 static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1650 INT i, nLinePts, nAlloc;
1651 POINT *pLinePts;
1652 POINT ptViewportOrg, ptWindowOrg;
1653 SIZE szViewportExt, szWindowExt;
1654 DWORD mapMode, graphicsMode;
1655 XFORM xform;
1656 BOOL ret = TRUE;
1658 if(dc->funcs->pStrokePath)
1659 return dc->funcs->pStrokePath(dc->physDev);
1661 if(pPath->state != PATH_Closed)
1662 return FALSE;
1664 /* Save the mapping mode info */
1665 mapMode=GetMapMode(dc->hSelf);
1666 GetViewportExtEx(dc->hSelf, &szViewportExt);
1667 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
1668 GetWindowExtEx(dc->hSelf, &szWindowExt);
1669 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
1670 GetWorldTransform(dc->hSelf, &xform);
1672 /* Set MM_TEXT */
1673 SetMapMode(dc->hSelf, MM_TEXT);
1674 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
1675 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
1676 graphicsMode=GetGraphicsMode(dc->hSelf);
1677 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1678 ModifyWorldTransform(dc->hSelf, &xform, MWT_IDENTITY);
1679 SetGraphicsMode(dc->hSelf, graphicsMode);
1681 /* Allocate enough memory for the worst case without beziers (one PT_MOVETO
1682 * and the rest PT_LINETO with PT_CLOSEFIGURE at the end) plus some buffer
1683 * space in case we get one to keep the number of reallocations small. */
1684 nAlloc = pPath->numEntriesUsed + 1 + 300;
1685 pLinePts = HeapAlloc(GetProcessHeap(), 0, nAlloc * sizeof(POINT));
1686 nLinePts = 0;
1688 for(i = 0; i < pPath->numEntriesUsed; i++) {
1689 if((i == 0 || (pPath->pFlags[i-1] & PT_CLOSEFIGURE)) &&
1690 (pPath->pFlags[i] != PT_MOVETO)) {
1691 ERR("Expected PT_MOVETO %s, got path flag %d\n",
1692 i == 0 ? "as first point" : "after PT_CLOSEFIGURE",
1693 (INT)pPath->pFlags[i]);
1694 ret = FALSE;
1695 goto end;
1697 switch(pPath->pFlags[i]) {
1698 case PT_MOVETO:
1699 TRACE("Got PT_MOVETO (%ld, %ld)\n",
1700 pPath->pPoints[i].x, pPath->pPoints[i].y);
1701 if(nLinePts >= 2)
1702 Polyline(dc->hSelf, pLinePts, nLinePts);
1703 nLinePts = 0;
1704 pLinePts[nLinePts++] = pPath->pPoints[i];
1705 break;
1706 case PT_LINETO:
1707 case (PT_LINETO | PT_CLOSEFIGURE):
1708 TRACE("Got PT_LINETO (%ld, %ld)\n",
1709 pPath->pPoints[i].x, pPath->pPoints[i].y);
1710 pLinePts[nLinePts++] = pPath->pPoints[i];
1711 break;
1712 case PT_BEZIERTO:
1713 TRACE("Got PT_BEZIERTO\n");
1714 if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1715 (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1716 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1717 ret = FALSE;
1718 goto end;
1719 } else {
1720 INT nBzrPts, nMinAlloc;
1721 POINT *pBzrPts = GDI_Bezier(&pPath->pPoints[i-1], 4, &nBzrPts);
1722 /* Make sure we have allocated enough memory for the lines of
1723 * this bezier and the rest of the path, assuming we won't get
1724 * another one (since we won't reallocate again then). */
1725 nMinAlloc = nLinePts + (pPath->numEntriesUsed - i) + nBzrPts;
1726 if(nAlloc < nMinAlloc)
1728 nAlloc = nMinAlloc * 2;
1729 pLinePts = HeapReAlloc(GetProcessHeap(), 0, pLinePts,
1730 nAlloc * sizeof(POINT));
1732 memcpy(&pLinePts[nLinePts], &pBzrPts[1],
1733 (nBzrPts - 1) * sizeof(POINT));
1734 nLinePts += nBzrPts - 1;
1735 HeapFree(GetProcessHeap(), 0, pBzrPts);
1736 i += 2;
1738 break;
1739 default:
1740 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1741 ret = FALSE;
1742 goto end;
1744 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1745 pLinePts[nLinePts++] = pLinePts[0];
1747 if(nLinePts >= 2)
1748 Polyline(dc->hSelf, pLinePts, nLinePts);
1750 end:
1751 HeapFree(GetProcessHeap(), 0, pLinePts);
1753 /* Restore the old mapping mode */
1754 SetMapMode(dc->hSelf, mapMode);
1755 SetWindowExtEx(dc->hSelf, szWindowExt.cx, szWindowExt.cy, NULL);
1756 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
1757 SetViewportExtEx(dc->hSelf, szViewportExt.cx, szViewportExt.cy, NULL);
1758 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
1760 /* Go to GM_ADVANCED temporarily to restore the world transform */
1761 graphicsMode=GetGraphicsMode(dc->hSelf);
1762 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1763 SetWorldTransform(dc->hSelf, &xform);
1764 SetGraphicsMode(dc->hSelf, graphicsMode);
1766 /* If we've moved the current point then get its new position
1767 which will be in device (MM_TEXT) co-ords, convert it to
1768 logical co-ords and re-set it. This basically updates
1769 dc->CurPosX|Y so that their values are in the correct mapping
1770 mode.
1772 if(i > 0) {
1773 POINT pt;
1774 GetCurrentPositionEx(dc->hSelf, &pt);
1775 DPtoLP(dc->hSelf, &pt, 1);
1776 MoveToEx(dc->hSelf, pt.x, pt.y, NULL);
1779 return ret;
1783 /*******************************************************************
1784 * StrokeAndFillPath [GDI32.@]
1788 BOOL WINAPI StrokeAndFillPath(HDC hdc)
1790 DC *dc = DC_GetDCPtr( hdc );
1791 BOOL bRet = FALSE;
1793 if(!dc) return FALSE;
1795 if(dc->funcs->pStrokeAndFillPath)
1796 bRet = dc->funcs->pStrokeAndFillPath(dc->physDev);
1797 else
1799 bRet = PATH_FillPath(dc, &dc->path);
1800 if(bRet) bRet = PATH_StrokePath(dc, &dc->path);
1801 if(bRet) PATH_EmptyPath(&dc->path);
1803 GDI_ReleaseObj( hdc );
1804 return bRet;
1808 /*******************************************************************
1809 * StrokePath [GDI32.@]
1813 BOOL WINAPI StrokePath(HDC hdc)
1815 DC *dc = DC_GetDCPtr( hdc );
1816 GdiPath *pPath;
1817 BOOL bRet = FALSE;
1819 TRACE("(%p)\n", hdc);
1820 if(!dc) return FALSE;
1822 if(dc->funcs->pStrokePath)
1823 bRet = dc->funcs->pStrokePath(dc->physDev);
1824 else
1826 pPath = &dc->path;
1827 bRet = PATH_StrokePath(dc, pPath);
1828 PATH_EmptyPath(pPath);
1830 GDI_ReleaseObj( hdc );
1831 return bRet;
1835 /*******************************************************************
1836 * WidenPath [GDI32.@]
1840 BOOL WINAPI WidenPath(HDC hdc)
1842 DC *dc = DC_GetDCPtr( hdc );
1843 BOOL ret = FALSE;
1845 if(!dc) return FALSE;
1847 if(dc->funcs->pWidenPath)
1848 ret = dc->funcs->pWidenPath(dc->physDev);
1850 FIXME("stub\n");
1851 GDI_ReleaseObj( hdc );
1852 return ret;