2 * Graphics paths (BeginPath, EndPath etc.)
4 * Copyright 1997, 1998 Martin Boehme
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
24 #include "wine/port.h"
31 #if defined(HAVE_FLOAT_H)
40 #include "gdi_private.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
45 /* Notes on the implementation
47 * The implementation is based on dynamically resizable arrays of points and
48 * flags. I dithered for a bit before deciding on this implementation, and
49 * I had even done a bit of work on a linked list version before switching
50 * to arrays. It's a bit of a tradeoff. When you use linked lists, the
51 * implementation of FlattenPath is easier, because you can rip the
52 * PT_BEZIERTO entries out of the middle of the list and link the
53 * corresponding PT_LINETO entries in. However, when you use arrays,
54 * PathToRegion becomes easier, since you can essentially just pass your array
55 * of points to CreatePolyPolygonRgn. Also, if I'd used linked lists, I would
56 * have had the extra effort of creating a chunk-based allocation scheme
57 * in order to use memory effectively. That's why I finally decided to use
58 * arrays. Note by the way that the array based implementation has the same
59 * linear time complexity that linked lists would have since the arrays grow
62 * The points are stored in the path in device coordinates. This is
63 * consistent with the way Windows does things (for instance, see the Win32
64 * SDK documentation for GetPath).
66 * The word "stroke" appears in several places (e.g. in the flag
67 * GdiPath.newStroke). A stroke consists of a PT_MOVETO followed by one or
68 * more PT_LINETOs or PT_BEZIERTOs, up to, but not including, the next
69 * PT_MOVETO. Note that this is not the same as the definition of a figure;
70 * a figure can contain several strokes.
72 * I modified the drawing functions (MoveTo, LineTo etc.) to test whether
73 * the path is open and to call the corresponding function in path.c if this
74 * is the case. A more elegant approach would be to modify the function
75 * pointers in the DC_FUNCTIONS structure; however, this would be a lot more
76 * complex. Also, the performance degradation caused by my approach in the
77 * case where no path is open is so small that it cannot be measured.
82 /* FIXME: A lot of stuff isn't implemented yet. There is much more to come. */
84 #define NUM_ENTRIES_INITIAL 16 /* Initial size of points / flags arrays */
85 #define GROW_FACTOR_NUMER 2 /* Numerator of grow factor for the array */
86 #define GROW_FACTOR_DENOM 1 /* Denominator of grow factor */
88 /* A floating point version of the POINT structure */
89 typedef struct tagFLOAT_POINT
95 static BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags);
96 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
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, BYTE startEntryType);
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)
115 /* Perform the transformation */
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;
127 /***********************************************************************
128 * BeginPath (GDI32.@)
130 BOOL WINAPI BeginPath(HDC hdc)
133 DC *dc = get_dc_ptr( hdc );
137 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pBeginPath );
138 ret = physdev->funcs->pBeginPath( physdev );
139 release_dc_ptr( dc );
145 /***********************************************************************
148 BOOL WINAPI EndPath(HDC hdc)
151 DC *dc = get_dc_ptr( hdc );
155 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pEndPath );
156 ret = physdev->funcs->pEndPath( physdev );
157 release_dc_ptr( dc );
163 /******************************************************************************
164 * AbortPath [GDI32.@]
165 * Closes and discards paths from device context
168 * Check that SetLastError is being called correctly
171 * hdc [I] Handle to device context
177 BOOL WINAPI AbortPath( HDC hdc )
180 DC *dc = get_dc_ptr( hdc );
184 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pAbortPath );
185 ret = physdev->funcs->pAbortPath( physdev );
186 release_dc_ptr( dc );
192 /***********************************************************************
193 * CloseFigure (GDI32.@)
195 * FIXME: Check that SetLastError is being called correctly
197 BOOL WINAPI CloseFigure(HDC hdc)
200 DC *dc = get_dc_ptr( hdc );
204 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pCloseFigure );
205 ret = physdev->funcs->pCloseFigure( physdev );
206 release_dc_ptr( dc );
212 /***********************************************************************
215 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
220 DC *dc = get_dc_ptr( hdc );
226 /* Check that path is closed */
227 if(pPath->state!=PATH_Closed)
229 SetLastError(ERROR_CAN_NOT_COMPLETE);
234 ret = pPath->numEntriesUsed;
235 else if(nSize<pPath->numEntriesUsed)
237 SetLastError(ERROR_INVALID_PARAMETER);
242 memcpy(pPoints, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
243 memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
245 /* Convert the points to logical coordinates */
246 if(!DPtoLP(hdc, pPoints, pPath->numEntriesUsed))
248 /* FIXME: Is this the correct value? */
249 SetLastError(ERROR_CAN_NOT_COMPLETE);
252 else ret = pPath->numEntriesUsed;
255 release_dc_ptr( dc );
260 /***********************************************************************
261 * PathToRegion (GDI32.@)
264 * Check that SetLastError is being called correctly
266 * The documentation does not state this explicitly, but a test under Windows
267 * shows that the region which is returned should be in device coordinates.
269 HRGN WINAPI PathToRegion(HDC hdc)
273 DC *dc = get_dc_ptr( hdc );
275 /* Get pointer to path */
280 /* Check that path is closed */
281 if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
284 /* FIXME: Should we empty the path even if conversion failed? */
285 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
286 PATH_EmptyPath(pPath);
290 release_dc_ptr( dc );
294 static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
296 INT mapMode, graphicsMode;
297 SIZE ptViewportExt, ptWindowExt;
298 POINT ptViewportOrg, ptWindowOrg;
302 /* Construct a region from the path and fill it */
303 if(PATH_PathToRegion(pPath, dc->polyFillMode, &hrgn))
305 /* Since PaintRgn interprets the region as being in logical coordinates
306 * but the points we store for the path are already in device
307 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
308 * Using SaveDC to save information about the mapping mode / world
309 * transform would be easier but would require more overhead, especially
310 * now that SaveDC saves the current path.
313 /* Save the information about the old mapping mode */
314 mapMode=GetMapMode(dc->hSelf);
315 GetViewportExtEx(dc->hSelf, &ptViewportExt);
316 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
317 GetWindowExtEx(dc->hSelf, &ptWindowExt);
318 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
320 /* Save world transform
321 * NB: The Windows documentation on world transforms would lead one to
322 * believe that this has to be done only in GM_ADVANCED; however, my
323 * tests show that resetting the graphics mode to GM_COMPATIBLE does
324 * not reset the world transform.
326 GetWorldTransform(dc->hSelf, &xform);
329 SetMapMode(dc->hSelf, MM_TEXT);
330 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
331 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
332 graphicsMode=GetGraphicsMode(dc->hSelf);
333 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
334 ModifyWorldTransform(dc->hSelf, &xform, MWT_IDENTITY);
335 SetGraphicsMode(dc->hSelf, graphicsMode);
337 /* Paint the region */
338 PaintRgn(dc->hSelf, hrgn);
340 /* Restore the old mapping mode */
341 SetMapMode(dc->hSelf, mapMode);
342 SetViewportExtEx(dc->hSelf, ptViewportExt.cx, ptViewportExt.cy, NULL);
343 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
344 SetWindowExtEx(dc->hSelf, ptWindowExt.cx, ptWindowExt.cy, NULL);
345 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
347 /* Go to GM_ADVANCED temporarily to restore the world transform */
348 graphicsMode=GetGraphicsMode(dc->hSelf);
349 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
350 SetWorldTransform(dc->hSelf, &xform);
351 SetGraphicsMode(dc->hSelf, graphicsMode);
358 /***********************************************************************
362 * Check that SetLastError is being called correctly
364 BOOL WINAPI FillPath(HDC hdc)
367 DC *dc = get_dc_ptr( hdc );
371 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pFillPath );
372 ret = physdev->funcs->pFillPath( physdev );
373 release_dc_ptr( dc );
379 /***********************************************************************
380 * SelectClipPath (GDI32.@)
382 * Check that SetLastError is being called correctly
384 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
387 DC *dc = get_dc_ptr( hdc );
391 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectClipPath );
392 ret = physdev->funcs->pSelectClipPath( physdev, iMode );
393 release_dc_ptr( dc );
399 /***********************************************************************
405 * Initializes the GdiPath structure.
407 void PATH_InitGdiPath(GdiPath *pPath)
411 pPath->state=PATH_Null;
414 pPath->numEntriesUsed=0;
415 pPath->numEntriesAllocated=0;
418 /* PATH_DestroyGdiPath
420 * Destroys a GdiPath structure (frees the memory in the arrays).
422 void PATH_DestroyGdiPath(GdiPath *pPath)
426 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
427 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
430 /* PATH_AssignGdiPath
432 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
433 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
434 * not just the pointers. Since this means that the arrays in pPathDest may
435 * need to be resized, pPathDest should have been initialized using
436 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
437 * not a copy constructor).
438 * Returns TRUE if successful, else FALSE.
440 BOOL PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
442 assert(pPathDest!=NULL && pPathSrc!=NULL);
444 /* Make sure destination arrays are big enough */
445 if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
448 /* Perform the copy operation */
449 memcpy(pPathDest->pPoints, pPathSrc->pPoints,
450 sizeof(POINT)*pPathSrc->numEntriesUsed);
451 memcpy(pPathDest->pFlags, pPathSrc->pFlags,
452 sizeof(BYTE)*pPathSrc->numEntriesUsed);
454 pPathDest->state=pPathSrc->state;
455 pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
456 pPathDest->newStroke=pPathSrc->newStroke;
463 * Should be called when a MoveTo is performed on a DC that has an
464 * open path. This starts a new stroke. Returns TRUE if successful, else
467 BOOL PATH_MoveTo(DC *dc)
469 GdiPath *pPath = &dc->path;
471 /* Check that path is open */
472 if(pPath->state!=PATH_Open)
473 /* FIXME: Do we have to call SetLastError? */
476 /* Start a new stroke */
477 pPath->newStroke=TRUE;
484 * Should be called when a LineTo is performed on a DC that has an
485 * open path. This adds a PT_LINETO entry to the path (and possibly
486 * a PT_MOVETO entry, if this is the first LineTo in a stroke).
487 * Returns TRUE if successful, else FALSE.
489 BOOL PATH_LineTo(DC *dc, INT x, INT y)
491 GdiPath *pPath = &dc->path;
492 POINT point, pointCurPos;
494 /* Check that path is open */
495 if(pPath->state!=PATH_Open)
498 /* Convert point to device coordinates */
501 if(!LPtoDP(dc->hSelf, &point, 1))
504 /* Add a PT_MOVETO if necessary */
507 pPath->newStroke=FALSE;
508 pointCurPos.x = dc->CursPosX;
509 pointCurPos.y = dc->CursPosY;
510 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
512 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
516 /* Add a PT_LINETO entry */
517 return PATH_AddEntry(pPath, &point, PT_LINETO);
522 * Should be called when a call to RoundRect is performed on a DC that has
523 * an open path. Returns TRUE if successful, else FALSE.
525 * FIXME: it adds the same entries to the path as windows does, but there
526 * is an error in the bezier drawing code so that there are small pixel-size
527 * gaps when the resulting path is drawn by StrokePath()
529 BOOL PATH_RoundRect(DC *dc, INT x1, INT y1, INT x2, INT y2, INT ell_width, INT ell_height)
531 GdiPath *pPath = &dc->path;
532 POINT corners[2], pointTemp;
533 FLOAT_POINT ellCorners[2];
535 /* Check that path is open */
536 if(pPath->state!=PATH_Open)
539 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
542 /* Add points to the roundrect path */
543 ellCorners[0].x = corners[1].x-ell_width;
544 ellCorners[0].y = corners[0].y;
545 ellCorners[1].x = corners[1].x;
546 ellCorners[1].y = corners[0].y+ell_height;
547 if(!PATH_DoArcPart(pPath, ellCorners, 0, -M_PI_2, PT_MOVETO))
549 pointTemp.x = corners[0].x+ell_width/2;
550 pointTemp.y = corners[0].y;
551 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
553 ellCorners[0].x = corners[0].x;
554 ellCorners[1].x = corners[0].x+ell_width;
555 if(!PATH_DoArcPart(pPath, ellCorners, -M_PI_2, -M_PI, FALSE))
557 pointTemp.x = corners[0].x;
558 pointTemp.y = corners[1].y-ell_height/2;
559 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
561 ellCorners[0].y = corners[1].y-ell_height;
562 ellCorners[1].y = corners[1].y;
563 if(!PATH_DoArcPart(pPath, ellCorners, M_PI, M_PI_2, FALSE))
565 pointTemp.x = corners[1].x-ell_width/2;
566 pointTemp.y = corners[1].y;
567 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
569 ellCorners[0].x = corners[1].x-ell_width;
570 ellCorners[1].x = corners[1].x;
571 if(!PATH_DoArcPart(pPath, ellCorners, M_PI_2, 0, FALSE))
574 /* Close the roundrect figure */
575 if(!CloseFigure(dc->hSelf))
583 * Should be called when a call to Rectangle is performed on a DC that has
584 * an open path. Returns TRUE if successful, else FALSE.
586 BOOL PATH_Rectangle(DC *dc, INT x1, INT y1, INT x2, INT y2)
588 GdiPath *pPath = &dc->path;
589 POINT corners[2], pointTemp;
591 /* Check that path is open */
592 if(pPath->state!=PATH_Open)
595 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
598 /* Close any previous figure */
599 if(!CloseFigure(dc->hSelf))
601 /* The CloseFigure call shouldn't have failed */
606 /* Add four points to the path */
607 pointTemp.x=corners[1].x;
608 pointTemp.y=corners[0].y;
609 if(!PATH_AddEntry(pPath, &pointTemp, PT_MOVETO))
611 if(!PATH_AddEntry(pPath, corners, PT_LINETO))
613 pointTemp.x=corners[0].x;
614 pointTemp.y=corners[1].y;
615 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
617 if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
620 /* Close the rectangle figure */
621 if(!CloseFigure(dc->hSelf))
623 /* The CloseFigure call shouldn't have failed */
633 * Should be called when a call to Ellipse is performed on a DC that has
634 * an open path. This adds four Bezier splines representing the ellipse
635 * to the path. Returns TRUE if successful, else FALSE.
637 BOOL PATH_Ellipse(DC *dc, INT x1, INT y1, INT x2, INT y2)
639 return( PATH_Arc(dc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2,0) &&
640 CloseFigure(dc->hSelf) );
645 * Should be called when a call to Arc is performed on a DC that has
646 * an open path. This adds up to five Bezier splines representing the arc
647 * to the path. When 'lines' is 1, we add 1 extra line to get a chord,
648 * when 'lines' is 2, we add 2 extra lines to get a pie, and when 'lines' is
649 * -1 we add 1 extra line from the current DC position to the starting position
650 * of the arc before drawing the arc itself (arcto). Returns TRUE if successful,
653 BOOL PATH_Arc(DC *dc, INT x1, INT y1, INT x2, INT y2,
654 INT xStart, INT yStart, INT xEnd, INT yEnd, INT lines)
656 GdiPath *pPath = &dc->path;
657 double angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
658 /* Initialize angleEndQuadrant to silence gcc's warning */
660 FLOAT_POINT corners[2], pointStart, pointEnd;
661 POINT centre, pointCurPos;
665 /* FIXME: This function should check for all possible error returns */
666 /* FIXME: Do we have to respect newStroke? */
668 /* Check that path is open */
669 if(pPath->state!=PATH_Open)
672 /* Check for zero height / width */
673 /* FIXME: Only in GM_COMPATIBLE? */
677 /* Convert points to device coordinates */
682 pointStart.x = xStart;
683 pointStart.y = yStart;
686 INTERNAL_LPTODP_FLOAT(dc, corners);
687 INTERNAL_LPTODP_FLOAT(dc, corners+1);
688 INTERNAL_LPTODP_FLOAT(dc, &pointStart);
689 INTERNAL_LPTODP_FLOAT(dc, &pointEnd);
691 /* Make sure first corner is top left and second corner is bottom right */
692 if(corners[0].x>corners[1].x)
695 corners[0].x=corners[1].x;
698 if(corners[0].y>corners[1].y)
701 corners[0].y=corners[1].y;
705 /* Compute start and end angle */
706 PATH_NormalizePoint(corners, &pointStart, &x, &y);
707 angleStart=atan2(y, x);
708 PATH_NormalizePoint(corners, &pointEnd, &x, &y);
709 angleEnd=atan2(y, x);
711 /* Make sure the end angle is "on the right side" of the start angle */
712 if(dc->ArcDirection==AD_CLOCKWISE)
714 if(angleEnd<=angleStart)
717 assert(angleEnd>=angleStart);
722 if(angleEnd>=angleStart)
725 assert(angleEnd<=angleStart);
729 /* In GM_COMPATIBLE, don't include bottom and right edges */
730 if(dc->GraphicsMode==GM_COMPATIBLE)
736 /* arcto: Add a PT_MOVETO only if this is the first entry in a stroke */
737 if(lines==-1 && pPath->newStroke)
739 pPath->newStroke=FALSE;
740 pointCurPos.x = dc->CursPosX;
741 pointCurPos.y = dc->CursPosY;
742 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
744 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
748 /* Add the arc to the path with one Bezier spline per quadrant that the
754 /* Determine the start and end angles for this quadrant */
757 angleStartQuadrant=angleStart;
758 if(dc->ArcDirection==AD_CLOCKWISE)
759 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
761 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
765 angleStartQuadrant=angleEndQuadrant;
766 if(dc->ArcDirection==AD_CLOCKWISE)
767 angleEndQuadrant+=M_PI_2;
769 angleEndQuadrant-=M_PI_2;
772 /* Have we reached the last part of the arc? */
773 if((dc->ArcDirection==AD_CLOCKWISE &&
774 angleEnd<angleEndQuadrant) ||
775 (dc->ArcDirection==AD_COUNTERCLOCKWISE &&
776 angleEnd>angleEndQuadrant))
778 /* Adjust the end angle for this quadrant */
779 angleEndQuadrant=angleEnd;
783 /* Add the Bezier spline to the path */
784 PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
785 start ? (lines==-1 ? PT_LINETO : PT_MOVETO) : FALSE);
789 /* chord: close figure. pie: add line and close figure */
792 if(!CloseFigure(dc->hSelf))
797 centre.x = (corners[0].x+corners[1].x)/2;
798 centre.y = (corners[0].y+corners[1].y)/2;
799 if(!PATH_AddEntry(pPath, ¢re, PT_LINETO | PT_CLOSEFIGURE))
806 BOOL PATH_PolyBezierTo(DC *dc, const POINT *pts, DWORD cbPoints)
808 GdiPath *pPath = &dc->path;
812 /* Check that path is open */
813 if(pPath->state!=PATH_Open)
816 /* Add a PT_MOVETO if necessary */
819 pPath->newStroke=FALSE;
822 if(!LPtoDP(dc->hSelf, &pt, 1))
824 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
828 for(i = 0; i < cbPoints; i++) {
830 if(!LPtoDP(dc->hSelf, &pt, 1))
832 PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
837 BOOL PATH_PolyBezier(DC *dc, const POINT *pts, DWORD cbPoints)
839 GdiPath *pPath = &dc->path;
843 /* Check that path is open */
844 if(pPath->state!=PATH_Open)
847 for(i = 0; i < cbPoints; i++) {
849 if(!LPtoDP(dc->hSelf, &pt, 1))
851 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
858 * Should be called when a call to PolyDraw is performed on a DC that has
859 * an open path. Returns TRUE if successful, else FALSE.
861 BOOL PATH_PolyDraw(DC *dc, const POINT *pts, const BYTE *types,
864 GdiPath *pPath = &dc->path;
865 POINT lastmove, orig_pos;
868 lastmove.x = orig_pos.x = dc->CursPosX;
869 lastmove.y = orig_pos.y = dc->CursPosY;
871 for(i = pPath->numEntriesUsed - 1; i >= 0; i--){
872 if(pPath->pFlags[i] == PT_MOVETO){
873 lastmove.x = pPath->pPoints[i].x;
874 lastmove.y = pPath->pPoints[i].y;
875 if(!DPtoLP(dc->hSelf, &lastmove, 1))
881 for(i = 0; i < cbPoints; i++){
882 if(types[i] == PT_MOVETO){
883 pPath->newStroke = TRUE;
884 lastmove.x = pts[i].x;
885 lastmove.y = pts[i].y;
887 else if((types[i] & ~PT_CLOSEFIGURE) == PT_LINETO){
888 PATH_LineTo(dc, pts[i].x, pts[i].y);
890 else if(types[i] == PT_BEZIERTO){
891 if(!((i + 2 < cbPoints) && (types[i + 1] == PT_BEZIERTO)
892 && ((types[i + 2] & ~PT_CLOSEFIGURE) == PT_BEZIERTO)))
894 PATH_PolyBezierTo(dc, &(pts[i]), 3);
900 dc->CursPosX = pts[i].x;
901 dc->CursPosY = pts[i].y;
903 if(types[i] & PT_CLOSEFIGURE){
904 pPath->pFlags[pPath->numEntriesUsed-1] |= PT_CLOSEFIGURE;
905 pPath->newStroke = TRUE;
906 dc->CursPosX = lastmove.x;
907 dc->CursPosY = lastmove.y;
914 if((dc->CursPosX != orig_pos.x) || (dc->CursPosY != orig_pos.y)){
915 pPath->newStroke = TRUE;
916 dc->CursPosX = orig_pos.x;
917 dc->CursPosY = orig_pos.y;
923 BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
925 GdiPath *pPath = &dc->path;
929 /* Check that path is open */
930 if(pPath->state!=PATH_Open)
933 for(i = 0; i < cbPoints; i++) {
935 if(!LPtoDP(dc->hSelf, &pt, 1))
937 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
942 BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
944 GdiPath *pPath = &dc->path;
948 /* Check that path is open */
949 if(pPath->state!=PATH_Open)
952 /* Add a PT_MOVETO if necessary */
955 pPath->newStroke=FALSE;
958 if(!LPtoDP(dc->hSelf, &pt, 1))
960 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
964 for(i = 0; i < cbPoints; i++) {
966 if(!LPtoDP(dc->hSelf, &pt, 1))
968 PATH_AddEntry(pPath, &pt, PT_LINETO);
975 BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
977 GdiPath *pPath = &dc->path;
981 /* Check that path is open */
982 if(pPath->state!=PATH_Open)
985 for(i = 0; i < cbPoints; i++) {
987 if(!LPtoDP(dc->hSelf, &pt, 1))
989 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
990 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
996 BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
999 GdiPath *pPath = &dc->path;
1004 /* Check that path is open */
1005 if(pPath->state!=PATH_Open)
1008 for(i = 0, poly = 0; poly < polygons; poly++) {
1009 for(point = 0; point < counts[poly]; point++, i++) {
1011 if(!LPtoDP(dc->hSelf, &pt, 1))
1013 if(point == 0) startpt = pt;
1014 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1016 /* win98 adds an extra line to close the figure for some reason */
1017 PATH_AddEntry(pPath, &startpt, PT_LINETO | PT_CLOSEFIGURE);
1022 BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1025 GdiPath *pPath = &dc->path;
1027 UINT poly, point, i;
1029 /* Check that path is open */
1030 if(pPath->state!=PATH_Open)
1033 for(i = 0, poly = 0; poly < polylines; poly++) {
1034 for(point = 0; point < counts[poly]; point++, i++) {
1036 if(!LPtoDP(dc->hSelf, &pt, 1))
1038 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1044 /***********************************************************************
1045 * Internal functions
1048 /* PATH_CheckCorners
1050 * Helper function for PATH_RoundRect() and PATH_Rectangle()
1052 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2)
1056 /* Convert points to device coordinates */
1061 if(!LPtoDP(dc->hSelf, corners, 2))
1064 /* Make sure first corner is top left and second corner is bottom right */
1065 if(corners[0].x>corners[1].x)
1068 corners[0].x=corners[1].x;
1071 if(corners[0].y>corners[1].y)
1074 corners[0].y=corners[1].y;
1078 /* In GM_COMPATIBLE, don't include bottom and right edges */
1079 if(dc->GraphicsMode==GM_COMPATIBLE)
1088 /* PATH_AddFlatBezier
1090 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
1095 pts = GDI_Bezier( pt, 4, &no );
1096 if(!pts) return FALSE;
1098 for(i = 1; i < no; i++)
1099 PATH_AddEntry(pPath, &pts[i],
1100 (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
1101 HeapFree( GetProcessHeap(), 0, pts );
1107 * Replaces Beziers with line segments
1110 static BOOL PATH_FlattenPath(GdiPath *pPath)
1115 memset(&newPath, 0, sizeof(newPath));
1116 newPath.state = PATH_Open;
1117 for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
1118 switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
1121 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
1122 pPath->pFlags[srcpt]);
1125 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
1126 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
1131 newPath.state = PATH_Closed;
1132 PATH_AssignGdiPath(pPath, &newPath);
1133 PATH_DestroyGdiPath(&newPath);
1137 /* PATH_PathToRegion
1139 * Creates a region from the specified path using the specified polygon
1140 * filling mode. The path is left unchanged. A handle to the region that
1141 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
1142 * error occurs, SetLastError is called with the appropriate value and
1143 * FALSE is returned.
1145 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
1148 int numStrokes, iStroke, i;
1149 INT *pNumPointsInStroke;
1152 assert(pPath!=NULL);
1153 assert(pHrgn!=NULL);
1155 PATH_FlattenPath(pPath);
1157 /* FIXME: What happens when number of points is zero? */
1159 /* First pass: Find out how many strokes there are in the path */
1160 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
1162 for(i=0; i<pPath->numEntriesUsed; i++)
1163 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1166 /* Allocate memory for number-of-points-in-stroke array */
1167 pNumPointsInStroke=HeapAlloc( GetProcessHeap(), 0, sizeof(int) * numStrokes );
1168 if(!pNumPointsInStroke)
1170 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1174 /* Second pass: remember number of points in each polygon */
1175 iStroke=-1; /* Will get incremented to 0 at beginning of first stroke */
1176 for(i=0; i<pPath->numEntriesUsed; i++)
1178 /* Is this the beginning of a new stroke? */
1179 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1182 pNumPointsInStroke[iStroke]=0;
1185 pNumPointsInStroke[iStroke]++;
1188 /* Create a region from the strokes */
1189 hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
1190 numStrokes, nPolyFillMode);
1192 /* Free memory for number-of-points-in-stroke array */
1193 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
1197 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1206 static inline INT int_from_fixed(FIXED f)
1208 return (f.fract >= 0x8000) ? (f.value + 1) : f.value;
1211 /**********************************************************************
1214 * internally used by PATH_add_outline
1216 static void PATH_BezierTo(GdiPath *pPath, POINT *lppt, INT n)
1222 PATH_AddEntry(pPath, &lppt[1], PT_LINETO);
1226 PATH_AddEntry(pPath, &lppt[0], PT_BEZIERTO);
1227 PATH_AddEntry(pPath, &lppt[1], PT_BEZIERTO);
1228 PATH_AddEntry(pPath, &lppt[2], PT_BEZIERTO);
1242 pt[2].x = (lppt[i+2].x + lppt[i+1].x) / 2;
1243 pt[2].y = (lppt[i+2].y + lppt[i+1].y) / 2;
1244 PATH_BezierTo(pPath, pt, 3);
1252 PATH_BezierTo(pPath, pt, 3);
1256 static BOOL PATH_add_outline(DC *dc, INT x, INT y, TTPOLYGONHEADER *header, DWORD size)
1258 GdiPath *pPath = &dc->path;
1259 TTPOLYGONHEADER *start;
1264 while ((char *)header < (char *)start + size)
1268 if (header->dwType != TT_POLYGON_TYPE)
1270 FIXME("Unknown header type %d\n", header->dwType);
1274 pt.x = x + int_from_fixed(header->pfxStart.x);
1275 pt.y = y - int_from_fixed(header->pfxStart.y);
1276 PATH_AddEntry(pPath, &pt, PT_MOVETO);
1278 curve = (TTPOLYCURVE *)(header + 1);
1280 while ((char *)curve < (char *)header + header->cb)
1282 /*TRACE("curve->wType %d\n", curve->wType);*/
1284 switch(curve->wType)
1290 for (i = 0; i < curve->cpfx; i++)
1292 pt.x = x + int_from_fixed(curve->apfx[i].x);
1293 pt.y = y - int_from_fixed(curve->apfx[i].y);
1294 PATH_AddEntry(pPath, &pt, PT_LINETO);
1299 case TT_PRIM_QSPLINE:
1300 case TT_PRIM_CSPLINE:
1304 POINT *pts = HeapAlloc(GetProcessHeap(), 0, (curve->cpfx + 1) * sizeof(POINT));
1306 if (!pts) return FALSE;
1308 ptfx = *(POINTFX *)((char *)curve - sizeof(POINTFX));
1310 pts[0].x = x + int_from_fixed(ptfx.x);
1311 pts[0].y = y - int_from_fixed(ptfx.y);
1313 for(i = 0; i < curve->cpfx; i++)
1315 pts[i + 1].x = x + int_from_fixed(curve->apfx[i].x);
1316 pts[i + 1].y = y - int_from_fixed(curve->apfx[i].y);
1319 PATH_BezierTo(pPath, pts, curve->cpfx + 1);
1321 HeapFree(GetProcessHeap(), 0, pts);
1326 FIXME("Unknown curve type %04x\n", curve->wType);
1330 curve = (TTPOLYCURVE *)&curve->apfx[curve->cpfx];
1333 header = (TTPOLYGONHEADER *)((char *)header + header->cb);
1336 return CloseFigure(dc->hSelf);
1339 /**********************************************************************
1342 BOOL PATH_ExtTextOut(DC *dc, INT x, INT y, UINT flags, const RECT *lprc,
1343 LPCWSTR str, UINT count, const INT *dx)
1346 HDC hdc = dc->hSelf;
1347 POINT offset = {0, 0};
1349 TRACE("%p, %d, %d, %08x, %s, %s, %d, %p)\n", hdc, x, y, flags,
1350 wine_dbgstr_rect(lprc), debugstr_wn(str, count), count, dx);
1352 if (!count) return TRUE;
1354 for (idx = 0; idx < count; idx++)
1356 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
1361 dwSize = GetGlyphOutlineW(hdc, str[idx], GGO_GLYPH_INDEX | GGO_NATIVE, &gm, 0, NULL, &identity);
1362 if (dwSize == GDI_ERROR) return FALSE;
1364 /* add outline only if char is printable */
1367 outline = HeapAlloc(GetProcessHeap(), 0, dwSize);
1368 if (!outline) return FALSE;
1370 GetGlyphOutlineW(hdc, str[idx], GGO_GLYPH_INDEX | GGO_NATIVE, &gm, dwSize, outline, &identity);
1372 PATH_add_outline(dc, x + offset.x, y + offset.y, outline, dwSize);
1374 HeapFree(GetProcessHeap(), 0, outline);
1381 offset.x += dx[idx * 2];
1382 offset.y += dx[idx * 2 + 1];
1385 offset.x += dx[idx];
1389 offset.x += gm.gmCellIncX;
1390 offset.y += gm.gmCellIncY;
1398 * Removes all entries from the path and sets the path state to PATH_Null.
1400 static void PATH_EmptyPath(GdiPath *pPath)
1402 assert(pPath!=NULL);
1404 pPath->state=PATH_Null;
1405 pPath->numEntriesUsed=0;
1410 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1411 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1412 * successful, FALSE otherwise (e.g. if not enough memory was available).
1414 static BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
1416 assert(pPath!=NULL);
1418 /* FIXME: If newStroke is true, perhaps we want to check that we're
1419 * getting a PT_MOVETO
1421 TRACE("(%d,%d) - %d\n", pPoint->x, pPoint->y, flags);
1423 /* Check that path is open */
1424 if(pPath->state!=PATH_Open)
1427 /* Reserve enough memory for an extra path entry */
1428 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
1431 /* Store information in path entry */
1432 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
1433 pPath->pFlags[pPath->numEntriesUsed]=flags;
1435 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1436 if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
1437 pPath->newStroke=TRUE;
1439 /* Increment entry count */
1440 pPath->numEntriesUsed++;
1445 /* PATH_ReserveEntries
1447 * Ensures that at least "numEntries" entries (for points and flags) have
1448 * been allocated; allocates larger arrays and copies the existing entries
1449 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1451 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries)
1453 INT numEntriesToAllocate;
1457 assert(pPath!=NULL);
1458 assert(numEntries>=0);
1460 /* Do we have to allocate more memory? */
1461 if(numEntries > pPath->numEntriesAllocated)
1463 /* Find number of entries to allocate. We let the size of the array
1464 * grow exponentially, since that will guarantee linear time
1466 if(pPath->numEntriesAllocated)
1468 numEntriesToAllocate=pPath->numEntriesAllocated;
1469 while(numEntriesToAllocate<numEntries)
1470 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
1474 numEntriesToAllocate=numEntries;
1476 /* Allocate new arrays */
1477 pPointsNew=HeapAlloc( GetProcessHeap(), 0, numEntriesToAllocate * sizeof(POINT) );
1480 pFlagsNew=HeapAlloc( GetProcessHeap(), 0, numEntriesToAllocate * sizeof(BYTE) );
1483 HeapFree( GetProcessHeap(), 0, pPointsNew );
1487 /* Copy old arrays to new arrays and discard old arrays */
1490 assert(pPath->pFlags);
1492 memcpy(pPointsNew, pPath->pPoints,
1493 sizeof(POINT)*pPath->numEntriesUsed);
1494 memcpy(pFlagsNew, pPath->pFlags,
1495 sizeof(BYTE)*pPath->numEntriesUsed);
1497 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
1498 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
1500 pPath->pPoints=pPointsNew;
1501 pPath->pFlags=pFlagsNew;
1502 pPath->numEntriesAllocated=numEntriesToAllocate;
1510 * Creates a Bezier spline that corresponds to part of an arc and appends the
1511 * corresponding points to the path. The start and end angles are passed in
1512 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1513 * at most. If "startEntryType" is non-zero, an entry of that type for the first
1514 * control point is added to the path; otherwise, it is assumed that the current
1515 * position is equal to the first control point.
1517 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
1518 double angleStart, double angleEnd, BYTE startEntryType)
1520 double halfAngle, a;
1521 double xNorm[4], yNorm[4];
1525 assert(fabs(angleEnd-angleStart)<=M_PI_2);
1527 /* FIXME: Is there an easier way of computing this? */
1529 /* Compute control points */
1530 halfAngle=(angleEnd-angleStart)/2.0;
1531 if(fabs(halfAngle)>1e-8)
1533 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
1534 xNorm[0]=cos(angleStart);
1535 yNorm[0]=sin(angleStart);
1536 xNorm[1]=xNorm[0] - a*yNorm[0];
1537 yNorm[1]=yNorm[0] + a*xNorm[0];
1538 xNorm[3]=cos(angleEnd);
1539 yNorm[3]=sin(angleEnd);
1540 xNorm[2]=xNorm[3] + a*yNorm[3];
1541 yNorm[2]=yNorm[3] - a*xNorm[3];
1546 xNorm[i]=cos(angleStart);
1547 yNorm[i]=sin(angleStart);
1550 /* Add starting point to path if desired */
1553 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1554 if(!PATH_AddEntry(pPath, &point, startEntryType))
1558 /* Add remaining control points */
1561 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1562 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1569 /* PATH_ScaleNormalizedPoint
1571 * Scales a normalized point (x, y) with respect to the box whose corners are
1572 * passed in "corners". The point is stored in "*pPoint". The normalized
1573 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1574 * (1.0, 1.0) correspond to corners[1].
1576 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
1577 double y, POINT *pPoint)
1579 pPoint->x=GDI_ROUND( (double)corners[0].x +
1580 (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
1581 pPoint->y=GDI_ROUND( (double)corners[0].y +
1582 (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
1585 /* PATH_NormalizePoint
1587 * Normalizes a point with respect to the box whose corners are passed in
1588 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1590 static void PATH_NormalizePoint(FLOAT_POINT corners[],
1591 const FLOAT_POINT *pPoint,
1592 double *pX, double *pY)
1594 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) *
1596 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1601 /*******************************************************************
1602 * FlattenPath [GDI32.@]
1606 BOOL WINAPI FlattenPath(HDC hdc)
1609 DC *dc = get_dc_ptr( hdc );
1613 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pFlattenPath );
1614 ret = physdev->funcs->pFlattenPath( physdev );
1615 release_dc_ptr( dc );
1621 static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1623 INT i, nLinePts, nAlloc;
1625 POINT ptViewportOrg, ptWindowOrg;
1626 SIZE szViewportExt, szWindowExt;
1627 DWORD mapMode, graphicsMode;
1631 /* Save the mapping mode info */
1632 mapMode=GetMapMode(dc->hSelf);
1633 GetViewportExtEx(dc->hSelf, &szViewportExt);
1634 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
1635 GetWindowExtEx(dc->hSelf, &szWindowExt);
1636 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
1637 GetWorldTransform(dc->hSelf, &xform);
1640 SetMapMode(dc->hSelf, MM_TEXT);
1641 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
1642 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
1643 graphicsMode=GetGraphicsMode(dc->hSelf);
1644 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1645 ModifyWorldTransform(dc->hSelf, &xform, MWT_IDENTITY);
1646 SetGraphicsMode(dc->hSelf, graphicsMode);
1648 /* Allocate enough memory for the worst case without beziers (one PT_MOVETO
1649 * and the rest PT_LINETO with PT_CLOSEFIGURE at the end) plus some buffer
1650 * space in case we get one to keep the number of reallocations small. */
1651 nAlloc = pPath->numEntriesUsed + 1 + 300;
1652 pLinePts = HeapAlloc(GetProcessHeap(), 0, nAlloc * sizeof(POINT));
1655 for(i = 0; i < pPath->numEntriesUsed; i++) {
1656 if((i == 0 || (pPath->pFlags[i-1] & PT_CLOSEFIGURE)) &&
1657 (pPath->pFlags[i] != PT_MOVETO)) {
1658 ERR("Expected PT_MOVETO %s, got path flag %d\n",
1659 i == 0 ? "as first point" : "after PT_CLOSEFIGURE",
1660 (INT)pPath->pFlags[i]);
1664 switch(pPath->pFlags[i]) {
1666 TRACE("Got PT_MOVETO (%d, %d)\n",
1667 pPath->pPoints[i].x, pPath->pPoints[i].y);
1669 Polyline(dc->hSelf, pLinePts, nLinePts);
1671 pLinePts[nLinePts++] = pPath->pPoints[i];
1674 case (PT_LINETO | PT_CLOSEFIGURE):
1675 TRACE("Got PT_LINETO (%d, %d)\n",
1676 pPath->pPoints[i].x, pPath->pPoints[i].y);
1677 pLinePts[nLinePts++] = pPath->pPoints[i];
1680 TRACE("Got PT_BEZIERTO\n");
1681 if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1682 (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1683 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1687 INT nBzrPts, nMinAlloc;
1688 POINT *pBzrPts = GDI_Bezier(&pPath->pPoints[i-1], 4, &nBzrPts);
1689 /* Make sure we have allocated enough memory for the lines of
1690 * this bezier and the rest of the path, assuming we won't get
1691 * another one (since we won't reallocate again then). */
1692 nMinAlloc = nLinePts + (pPath->numEntriesUsed - i) + nBzrPts;
1693 if(nAlloc < nMinAlloc)
1695 nAlloc = nMinAlloc * 2;
1696 pLinePts = HeapReAlloc(GetProcessHeap(), 0, pLinePts,
1697 nAlloc * sizeof(POINT));
1699 memcpy(&pLinePts[nLinePts], &pBzrPts[1],
1700 (nBzrPts - 1) * sizeof(POINT));
1701 nLinePts += nBzrPts - 1;
1702 HeapFree(GetProcessHeap(), 0, pBzrPts);
1707 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1711 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1712 pLinePts[nLinePts++] = pLinePts[0];
1715 Polyline(dc->hSelf, pLinePts, nLinePts);
1718 HeapFree(GetProcessHeap(), 0, pLinePts);
1720 /* Restore the old mapping mode */
1721 SetMapMode(dc->hSelf, mapMode);
1722 SetWindowExtEx(dc->hSelf, szWindowExt.cx, szWindowExt.cy, NULL);
1723 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
1724 SetViewportExtEx(dc->hSelf, szViewportExt.cx, szViewportExt.cy, NULL);
1725 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
1727 /* Go to GM_ADVANCED temporarily to restore the world transform */
1728 graphicsMode=GetGraphicsMode(dc->hSelf);
1729 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1730 SetWorldTransform(dc->hSelf, &xform);
1731 SetGraphicsMode(dc->hSelf, graphicsMode);
1733 /* If we've moved the current point then get its new position
1734 which will be in device (MM_TEXT) co-ords, convert it to
1735 logical co-ords and re-set it. This basically updates
1736 dc->CurPosX|Y so that their values are in the correct mapping
1741 GetCurrentPositionEx(dc->hSelf, &pt);
1742 DPtoLP(dc->hSelf, &pt, 1);
1743 MoveToEx(dc->hSelf, pt.x, pt.y, NULL);
1749 #define round(x) ((int)((x)>0?(x)+0.5:(x)-0.5))
1751 static BOOL PATH_WidenPath(DC *dc)
1753 INT i, j, numStrokes, penWidth, penWidthIn, penWidthOut, size, penStyle;
1755 GdiPath *pPath, *pNewPath, **pStrokes = NULL, *pUpPath, *pDownPath;
1757 DWORD obj_type, joint, endcap, penType;
1761 PATH_FlattenPath(pPath);
1763 size = GetObjectW( dc->hPen, 0, NULL );
1765 SetLastError(ERROR_CAN_NOT_COMPLETE);
1769 elp = HeapAlloc( GetProcessHeap(), 0, size );
1770 GetObjectW( dc->hPen, size, elp );
1772 obj_type = GetObjectType(dc->hPen);
1773 if(obj_type == OBJ_PEN) {
1774 penStyle = ((LOGPEN*)elp)->lopnStyle;
1776 else if(obj_type == OBJ_EXTPEN) {
1777 penStyle = elp->elpPenStyle;
1780 SetLastError(ERROR_CAN_NOT_COMPLETE);
1781 HeapFree( GetProcessHeap(), 0, elp );
1785 penWidth = elp->elpWidth;
1786 HeapFree( GetProcessHeap(), 0, elp );
1788 endcap = (PS_ENDCAP_MASK & penStyle);
1789 joint = (PS_JOIN_MASK & penStyle);
1790 penType = (PS_TYPE_MASK & penStyle);
1792 /* The function cannot apply to cosmetic pens */
1793 if(obj_type == OBJ_EXTPEN && penType == PS_COSMETIC) {
1794 SetLastError(ERROR_CAN_NOT_COMPLETE);
1798 penWidthIn = penWidth / 2;
1799 penWidthOut = penWidth / 2;
1800 if(penWidthIn + penWidthOut < penWidth)
1805 for(i = 0, j = 0; i < pPath->numEntriesUsed; i++, j++) {
1807 if((i == 0 || (pPath->pFlags[i-1] & PT_CLOSEFIGURE)) &&
1808 (pPath->pFlags[i] != PT_MOVETO)) {
1809 ERR("Expected PT_MOVETO %s, got path flag %c\n",
1810 i == 0 ? "as first point" : "after PT_CLOSEFIGURE",
1814 switch(pPath->pFlags[i]) {
1816 if(numStrokes > 0) {
1817 pStrokes[numStrokes - 1]->state = PATH_Closed;
1822 pStrokes = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath*));
1824 pStrokes = HeapReAlloc(GetProcessHeap(), 0, pStrokes, numStrokes * sizeof(GdiPath*));
1825 if(!pStrokes) return FALSE;
1826 pStrokes[numStrokes - 1] = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1827 PATH_InitGdiPath(pStrokes[numStrokes - 1]);
1828 pStrokes[numStrokes - 1]->state = PATH_Open;
1831 case (PT_LINETO | PT_CLOSEFIGURE):
1832 point.x = pPath->pPoints[i].x;
1833 point.y = pPath->pPoints[i].y;
1834 PATH_AddEntry(pStrokes[numStrokes - 1], &point, pPath->pFlags[i]);
1837 /* should never happen because of the FlattenPath call */
1838 ERR("Should never happen\n");
1841 ERR("Got path flag %c\n", pPath->pFlags[i]);
1846 pNewPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1847 PATH_InitGdiPath(pNewPath);
1848 pNewPath->state = PATH_Open;
1850 for(i = 0; i < numStrokes; i++) {
1851 pUpPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1852 PATH_InitGdiPath(pUpPath);
1853 pUpPath->state = PATH_Open;
1854 pDownPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1855 PATH_InitGdiPath(pDownPath);
1856 pDownPath->state = PATH_Open;
1858 for(j = 0; j < pStrokes[i]->numEntriesUsed; j++) {
1859 /* Beginning or end of the path if not closed */
1860 if((!(pStrokes[i]->pFlags[pStrokes[i]->numEntriesUsed - 1] & PT_CLOSEFIGURE)) && (j == 0 || j == pStrokes[i]->numEntriesUsed - 1) ) {
1861 /* Compute segment angle */
1862 double xo, yo, xa, ya, theta;
1864 FLOAT_POINT corners[2];
1866 xo = pStrokes[i]->pPoints[j].x;
1867 yo = pStrokes[i]->pPoints[j].y;
1868 xa = pStrokes[i]->pPoints[1].x;
1869 ya = pStrokes[i]->pPoints[1].y;
1872 xa = pStrokes[i]->pPoints[j - 1].x;
1873 ya = pStrokes[i]->pPoints[j - 1].y;
1874 xo = pStrokes[i]->pPoints[j].x;
1875 yo = pStrokes[i]->pPoints[j].y;
1877 theta = atan2( ya - yo, xa - xo );
1879 case PS_ENDCAP_SQUARE :
1880 pt.x = xo + round(sqrt(2) * penWidthOut * cos(M_PI_4 + theta));
1881 pt.y = yo + round(sqrt(2) * penWidthOut * sin(M_PI_4 + theta));
1882 PATH_AddEntry(pUpPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO) );
1883 pt.x = xo + round(sqrt(2) * penWidthIn * cos(- M_PI_4 + theta));
1884 pt.y = yo + round(sqrt(2) * penWidthIn * sin(- M_PI_4 + theta));
1885 PATH_AddEntry(pUpPath, &pt, PT_LINETO);
1887 case PS_ENDCAP_FLAT :
1888 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
1889 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
1890 PATH_AddEntry(pUpPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO));
1891 pt.x = xo - round( penWidthIn * cos(theta + M_PI_2) );
1892 pt.y = yo - round( penWidthIn * sin(theta + M_PI_2) );
1893 PATH_AddEntry(pUpPath, &pt, PT_LINETO);
1895 case PS_ENDCAP_ROUND :
1897 corners[0].x = xo - penWidthIn;
1898 corners[0].y = yo - penWidthIn;
1899 corners[1].x = xo + penWidthOut;
1900 corners[1].y = yo + penWidthOut;
1901 PATH_DoArcPart(pUpPath ,corners, theta + M_PI_2 , theta + 3 * M_PI_4, (j == 0 ? PT_MOVETO : FALSE));
1902 PATH_DoArcPart(pUpPath ,corners, theta + 3 * M_PI_4 , theta + M_PI, FALSE);
1903 PATH_DoArcPart(pUpPath ,corners, theta + M_PI, theta + 5 * M_PI_4, FALSE);
1904 PATH_DoArcPart(pUpPath ,corners, theta + 5 * M_PI_4 , theta + 3 * M_PI_2, FALSE);
1908 /* Corpse of the path */
1912 double xa, ya, xb, yb, xo, yo;
1913 double alpha, theta, miterWidth;
1914 DWORD _joint = joint;
1916 GdiPath *pInsidePath, *pOutsidePath;
1917 if(j > 0 && j < pStrokes[i]->numEntriesUsed - 1) {
1922 previous = pStrokes[i]->numEntriesUsed - 1;
1929 xo = pStrokes[i]->pPoints[j].x;
1930 yo = pStrokes[i]->pPoints[j].y;
1931 xa = pStrokes[i]->pPoints[previous].x;
1932 ya = pStrokes[i]->pPoints[previous].y;
1933 xb = pStrokes[i]->pPoints[next].x;
1934 yb = pStrokes[i]->pPoints[next].y;
1935 theta = atan2( yo - ya, xo - xa );
1936 alpha = atan2( yb - yo, xb - xo ) - theta;
1937 if (alpha > 0) alpha -= M_PI;
1939 if(_joint == PS_JOIN_MITER && dc->miterLimit < fabs(1 / sin(alpha/2))) {
1940 _joint = PS_JOIN_BEVEL;
1943 pInsidePath = pUpPath;
1944 pOutsidePath = pDownPath;
1946 else if(alpha < 0) {
1947 pInsidePath = pDownPath;
1948 pOutsidePath = pUpPath;
1953 /* Inside angle points */
1955 pt.x = xo - round( penWidthIn * cos(theta + M_PI_2) );
1956 pt.y = yo - round( penWidthIn * sin(theta + M_PI_2) );
1959 pt.x = xo + round( penWidthIn * cos(theta + M_PI_2) );
1960 pt.y = yo + round( penWidthIn * sin(theta + M_PI_2) );
1962 PATH_AddEntry(pInsidePath, &pt, PT_LINETO);
1964 pt.x = xo + round( penWidthIn * cos(M_PI_2 + alpha + theta) );
1965 pt.y = yo + round( penWidthIn * sin(M_PI_2 + alpha + theta) );
1968 pt.x = xo - round( penWidthIn * cos(M_PI_2 + alpha + theta) );
1969 pt.y = yo - round( penWidthIn * sin(M_PI_2 + alpha + theta) );
1971 PATH_AddEntry(pInsidePath, &pt, PT_LINETO);
1972 /* Outside angle point */
1974 case PS_JOIN_MITER :
1975 miterWidth = fabs(penWidthOut / cos(M_PI_2 - fabs(alpha) / 2));
1976 pt.x = xo + round( miterWidth * cos(theta + alpha / 2) );
1977 pt.y = yo + round( miterWidth * sin(theta + alpha / 2) );
1978 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
1980 case PS_JOIN_BEVEL :
1982 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
1983 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
1986 pt.x = xo - round( penWidthOut * cos(theta + M_PI_2) );
1987 pt.y = yo - round( penWidthOut * sin(theta + M_PI_2) );
1989 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
1991 pt.x = xo - round( penWidthOut * cos(M_PI_2 + alpha + theta) );
1992 pt.y = yo - round( penWidthOut * sin(M_PI_2 + alpha + theta) );
1995 pt.x = xo + round( penWidthOut * cos(M_PI_2 + alpha + theta) );
1996 pt.y = yo + round( penWidthOut * sin(M_PI_2 + alpha + theta) );
1998 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
2000 case PS_JOIN_ROUND :
2003 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
2004 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
2007 pt.x = xo - round( penWidthOut * cos(theta + M_PI_2) );
2008 pt.y = yo - round( penWidthOut * sin(theta + M_PI_2) );
2010 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
2011 pt.x = xo + round( penWidthOut * cos(theta + alpha / 2) );
2012 pt.y = yo + round( penWidthOut * sin(theta + alpha / 2) );
2013 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
2015 pt.x = xo - round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2016 pt.y = yo - round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2019 pt.x = xo + round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2020 pt.y = yo + round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2022 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
2027 for(j = 0; j < pUpPath->numEntriesUsed; j++) {
2029 pt.x = pUpPath->pPoints[j].x;
2030 pt.y = pUpPath->pPoints[j].y;
2031 PATH_AddEntry(pNewPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO));
2033 for(j = 0; j < pDownPath->numEntriesUsed; j++) {
2035 pt.x = pDownPath->pPoints[pDownPath->numEntriesUsed - j - 1].x;
2036 pt.y = pDownPath->pPoints[pDownPath->numEntriesUsed - j - 1].y;
2037 PATH_AddEntry(pNewPath, &pt, ( (j == 0 && (pStrokes[i]->pFlags[pStrokes[i]->numEntriesUsed - 1] & PT_CLOSEFIGURE)) ? PT_MOVETO : PT_LINETO));
2040 PATH_DestroyGdiPath(pStrokes[i]);
2041 HeapFree(GetProcessHeap(), 0, pStrokes[i]);
2042 PATH_DestroyGdiPath(pUpPath);
2043 HeapFree(GetProcessHeap(), 0, pUpPath);
2044 PATH_DestroyGdiPath(pDownPath);
2045 HeapFree(GetProcessHeap(), 0, pDownPath);
2047 HeapFree(GetProcessHeap(), 0, pStrokes);
2049 pNewPath->state = PATH_Closed;
2050 if (!(ret = PATH_AssignGdiPath(pPath, pNewPath)))
2051 ERR("Assign path failed\n");
2052 PATH_DestroyGdiPath(pNewPath);
2053 HeapFree(GetProcessHeap(), 0, pNewPath);
2058 /*******************************************************************
2059 * StrokeAndFillPath [GDI32.@]
2063 BOOL WINAPI StrokeAndFillPath(HDC hdc)
2066 DC *dc = get_dc_ptr( hdc );
2070 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pStrokeAndFillPath );
2071 ret = physdev->funcs->pStrokeAndFillPath( physdev );
2072 release_dc_ptr( dc );
2078 /*******************************************************************
2079 * StrokePath [GDI32.@]
2083 BOOL WINAPI StrokePath(HDC hdc)
2086 DC *dc = get_dc_ptr( hdc );
2090 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pStrokePath );
2091 ret = physdev->funcs->pStrokePath( physdev );
2092 release_dc_ptr( dc );
2098 /*******************************************************************
2099 * WidenPath [GDI32.@]
2103 BOOL WINAPI WidenPath(HDC hdc)
2106 DC *dc = get_dc_ptr( hdc );
2110 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pWidenPath );
2111 ret = physdev->funcs->pWidenPath( physdev );
2112 release_dc_ptr( dc );
2118 /***********************************************************************
2119 * null driver fallback implementations
2122 BOOL nulldrv_BeginPath( PHYSDEV dev )
2124 DC *dc = get_nulldrv_dc( dev );
2126 /* If path is already open, do nothing */
2127 if (dc->path.state != PATH_Open)
2129 PATH_EmptyPath(&dc->path);
2130 dc->path.newStroke = TRUE;
2131 dc->path.state = PATH_Open;
2136 BOOL nulldrv_EndPath( PHYSDEV dev )
2138 DC *dc = get_nulldrv_dc( dev );
2140 if (dc->path.state != PATH_Open)
2142 SetLastError( ERROR_CAN_NOT_COMPLETE );
2145 dc->path.state = PATH_Closed;
2149 BOOL nulldrv_AbortPath( PHYSDEV dev )
2151 DC *dc = get_nulldrv_dc( dev );
2153 PATH_EmptyPath( &dc->path );
2157 BOOL nulldrv_CloseFigure( PHYSDEV dev )
2159 DC *dc = get_nulldrv_dc( dev );
2161 if (dc->path.state != PATH_Open)
2163 SetLastError( ERROR_CAN_NOT_COMPLETE );
2166 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
2167 /* It is not necessary to draw a line, PT_CLOSEFIGURE is a virtual closing line itself */
2168 if (dc->path.numEntriesUsed)
2170 dc->path.pFlags[dc->path.numEntriesUsed - 1] |= PT_CLOSEFIGURE;
2171 dc->path.newStroke = TRUE;
2176 BOOL nulldrv_SelectClipPath( PHYSDEV dev, INT mode )
2180 DC *dc = get_nulldrv_dc( dev );
2182 if (dc->path.state != PATH_Closed)
2184 SetLastError( ERROR_CAN_NOT_COMPLETE );
2187 if (!PATH_PathToRegion( &dc->path, GetPolyFillMode(dev->hdc), &hrgn )) return FALSE;
2188 ret = ExtSelectClipRgn( dev->hdc, hrgn, mode ) != ERROR;
2189 if (ret) PATH_EmptyPath( &dc->path );
2190 /* FIXME: Should this function delete the path even if it failed? */
2191 DeleteObject( hrgn );
2195 BOOL nulldrv_FillPath( PHYSDEV dev )
2197 DC *dc = get_nulldrv_dc( dev );
2199 if (dc->path.state != PATH_Closed)
2201 SetLastError( ERROR_CAN_NOT_COMPLETE );
2204 if (!PATH_FillPath( dc, &dc->path )) return FALSE;
2205 /* FIXME: Should the path be emptied even if conversion failed? */
2206 PATH_EmptyPath( &dc->path );
2210 BOOL nulldrv_StrokeAndFillPath( PHYSDEV dev )
2212 DC *dc = get_nulldrv_dc( dev );
2214 if (dc->path.state != PATH_Closed)
2216 SetLastError( ERROR_CAN_NOT_COMPLETE );
2219 if (!PATH_FillPath( dc, &dc->path )) return FALSE;
2220 if (!PATH_StrokePath( dc, &dc->path )) return FALSE;
2221 PATH_EmptyPath( &dc->path );
2225 BOOL nulldrv_StrokePath( PHYSDEV dev )
2227 DC *dc = get_nulldrv_dc( dev );
2229 if (dc->path.state != PATH_Closed)
2231 SetLastError( ERROR_CAN_NOT_COMPLETE );
2234 if (!PATH_StrokePath( dc, &dc->path )) return FALSE;
2235 PATH_EmptyPath( &dc->path );
2239 BOOL nulldrv_FlattenPath( PHYSDEV dev )
2241 DC *dc = get_nulldrv_dc( dev );
2243 if (dc->path.state == PATH_Closed)
2245 SetLastError( ERROR_CAN_NOT_COMPLETE );
2248 return PATH_FlattenPath( &dc->path );
2251 BOOL nulldrv_WidenPath( PHYSDEV dev )
2253 DC *dc = get_nulldrv_dc( dev );
2255 if (dc->path.state == PATH_Open)
2257 SetLastError( ERROR_CAN_NOT_COMPLETE );
2260 return PATH_WidenPath( dc );