gdi32: Add a helper function to start a new path stroke.
[wine/multimedia.git] / dlls / gdi32 / path.c
blob8ddff53fd82487b12903f6e248ef70b9d8d1bef8
1 /*
2 * Graphics paths (BeginPath, EndPath etc.)
4 * Copyright 1997, 1998 Martin Boehme
5 * 1999 Huw D M Davies
6 * Copyright 2005 Dmitry Timoshkov
7 * Copyright 2011 Alexandre Julliard
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wine/port.h"
27 #include <assert.h>
28 #include <math.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #if defined(HAVE_FLOAT_H)
33 #include <float.h>
34 #endif
36 #include "windef.h"
37 #include "winbase.h"
38 #include "wingdi.h"
39 #include "winerror.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 * Martin Boehme
76 #define NUM_ENTRIES_INITIAL 16 /* Initial size of points / flags arrays */
77 #define GROW_FACTOR_NUMER 2 /* Numerator of grow factor for the array */
78 #define GROW_FACTOR_DENOM 1 /* Denominator of grow factor */
80 /* A floating point version of the POINT structure */
81 typedef struct tagFLOAT_POINT
83 double x, y;
84 } FLOAT_POINT;
87 struct path_physdev
89 struct gdi_physdev dev;
90 GdiPath *path;
93 static inline struct path_physdev *get_path_physdev( PHYSDEV dev )
95 return (struct path_physdev *)dev;
98 static inline void pop_path_driver( DC *dc )
100 PHYSDEV dev = pop_dc_driver( &dc->physDev );
101 assert( dev->funcs == &path_driver );
102 HeapFree( GetProcessHeap(), 0, dev );
106 /* Performs a world-to-viewport transformation on the specified point (which
107 * is in floating point format).
109 static inline void INTERNAL_LPTODP_FLOAT(DC *dc, FLOAT_POINT *point)
111 double x, y;
113 /* Perform the transformation */
114 x = point->x;
115 y = point->y;
116 point->x = x * dc->xformWorld2Vport.eM11 + y * dc->xformWorld2Vport.eM21 + dc->xformWorld2Vport.eDx;
117 point->y = x * dc->xformWorld2Vport.eM12 + y * dc->xformWorld2Vport.eM22 + dc->xformWorld2Vport.eDy;
120 static inline INT int_from_fixed(FIXED f)
122 return (f.fract >= 0x8000) ? (f.value + 1) : f.value;
126 /* PATH_EmptyPath
128 * Removes all entries from the path and sets the path state to PATH_Null.
130 static void PATH_EmptyPath(GdiPath *pPath)
132 pPath->state=PATH_Null;
133 pPath->numEntriesUsed=0;
136 /* PATH_ReserveEntries
138 * Ensures that at least "numEntries" entries (for points and flags) have
139 * been allocated; allocates larger arrays and copies the existing entries
140 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
142 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries)
144 INT numEntriesToAllocate;
145 POINT *pPointsNew;
146 BYTE *pFlagsNew;
148 assert(numEntries>=0);
150 /* Do we have to allocate more memory? */
151 if(numEntries > pPath->numEntriesAllocated)
153 /* Find number of entries to allocate. We let the size of the array
154 * grow exponentially, since that will guarantee linear time
155 * complexity. */
156 if(pPath->numEntriesAllocated)
158 numEntriesToAllocate=pPath->numEntriesAllocated;
159 while(numEntriesToAllocate<numEntries)
160 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
161 GROW_FACTOR_DENOM;
163 else
164 numEntriesToAllocate=numEntries;
166 /* Allocate new arrays */
167 pPointsNew=HeapAlloc( GetProcessHeap(), 0, numEntriesToAllocate * sizeof(POINT) );
168 if(!pPointsNew)
169 return FALSE;
170 pFlagsNew=HeapAlloc( GetProcessHeap(), 0, numEntriesToAllocate * sizeof(BYTE) );
171 if(!pFlagsNew)
173 HeapFree( GetProcessHeap(), 0, pPointsNew );
174 return FALSE;
177 /* Copy old arrays to new arrays and discard old arrays */
178 if(pPath->pPoints)
180 assert(pPath->pFlags);
182 memcpy(pPointsNew, pPath->pPoints,
183 sizeof(POINT)*pPath->numEntriesUsed);
184 memcpy(pFlagsNew, pPath->pFlags,
185 sizeof(BYTE)*pPath->numEntriesUsed);
187 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
188 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
190 pPath->pPoints=pPointsNew;
191 pPath->pFlags=pFlagsNew;
192 pPath->numEntriesAllocated=numEntriesToAllocate;
195 return TRUE;
198 /* PATH_AddEntry
200 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
201 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
202 * successful, FALSE otherwise (e.g. if not enough memory was available).
204 static BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
206 /* FIXME: If newStroke is true, perhaps we want to check that we're
207 * getting a PT_MOVETO
209 TRACE("(%d,%d) - %d\n", pPoint->x, pPoint->y, flags);
211 /* Check that path is open */
212 if(pPath->state!=PATH_Open)
213 return FALSE;
215 /* Reserve enough memory for an extra path entry */
216 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
217 return FALSE;
219 /* Store information in path entry */
220 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
221 pPath->pFlags[pPath->numEntriesUsed]=flags;
223 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
224 if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
225 pPath->newStroke=TRUE;
227 /* Increment entry count */
228 pPath->numEntriesUsed++;
230 return TRUE;
233 /* start a new path stroke if necessary */
234 static BOOL start_new_stroke( struct path_physdev *physdev )
236 POINT pos;
238 if (!physdev->path->newStroke) return TRUE;
239 physdev->path->newStroke = FALSE;
240 GetCurrentPositionEx( physdev->dev.hdc, &pos );
241 LPtoDP( physdev->dev.hdc, &pos, 1 );
242 return PATH_AddEntry( physdev->path, &pos, PT_MOVETO );
245 /* PATH_AssignGdiPath
247 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
248 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
249 * not just the pointers. Since this means that the arrays in pPathDest may
250 * need to be resized, pPathDest should have been initialized using
251 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
252 * not a copy constructor).
253 * Returns TRUE if successful, else FALSE.
255 static BOOL PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
257 /* Make sure destination arrays are big enough */
258 if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
259 return FALSE;
261 /* Perform the copy operation */
262 memcpy(pPathDest->pPoints, pPathSrc->pPoints,
263 sizeof(POINT)*pPathSrc->numEntriesUsed);
264 memcpy(pPathDest->pFlags, pPathSrc->pFlags,
265 sizeof(BYTE)*pPathSrc->numEntriesUsed);
267 pPathDest->state=pPathSrc->state;
268 pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
269 pPathDest->newStroke=pPathSrc->newStroke;
271 return TRUE;
274 /* PATH_CheckCorners
276 * Helper function for PATH_RoundRect() and PATH_Rectangle()
278 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2)
280 INT temp;
282 /* Convert points to device coordinates */
283 corners[0].x=x1;
284 corners[0].y=y1;
285 corners[1].x=x2;
286 corners[1].y=y2;
287 if(!LPtoDP(dc->hSelf, corners, 2))
288 return FALSE;
290 /* Make sure first corner is top left and second corner is bottom right */
291 if(corners[0].x>corners[1].x)
293 temp=corners[0].x;
294 corners[0].x=corners[1].x;
295 corners[1].x=temp;
297 if(corners[0].y>corners[1].y)
299 temp=corners[0].y;
300 corners[0].y=corners[1].y;
301 corners[1].y=temp;
304 /* In GM_COMPATIBLE, don't include bottom and right edges */
305 if(dc->GraphicsMode==GM_COMPATIBLE)
307 corners[1].x--;
308 corners[1].y--;
311 return TRUE;
314 /* PATH_AddFlatBezier
316 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
318 POINT *pts;
319 INT no, i;
321 pts = GDI_Bezier( pt, 4, &no );
322 if(!pts) return FALSE;
324 for(i = 1; i < no; i++)
325 PATH_AddEntry(pPath, &pts[i], (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
326 HeapFree( GetProcessHeap(), 0, pts );
327 return TRUE;
330 /* PATH_FlattenPath
332 * Replaces Beziers with line segments
335 static BOOL PATH_FlattenPath(GdiPath *pPath)
337 GdiPath newPath;
338 INT srcpt;
340 memset(&newPath, 0, sizeof(newPath));
341 newPath.state = PATH_Open;
342 for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
343 switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
344 case PT_MOVETO:
345 case PT_LINETO:
346 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
347 pPath->pFlags[srcpt]);
348 break;
349 case PT_BEZIERTO:
350 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
351 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
352 srcpt += 2;
353 break;
356 newPath.state = PATH_Closed;
357 PATH_AssignGdiPath(pPath, &newPath);
358 PATH_DestroyGdiPath(&newPath);
359 return TRUE;
362 /* PATH_PathToRegion
364 * Creates a region from the specified path using the specified polygon
365 * filling mode. The path is left unchanged. A handle to the region that
366 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
367 * error occurs, SetLastError is called with the appropriate value and
368 * FALSE is returned.
370 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
371 HRGN *pHrgn)
373 int numStrokes, iStroke, i;
374 INT *pNumPointsInStroke;
375 HRGN hrgn;
377 PATH_FlattenPath(pPath);
379 /* FIXME: What happens when number of points is zero? */
381 /* First pass: Find out how many strokes there are in the path */
382 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
383 numStrokes=0;
384 for(i=0; i<pPath->numEntriesUsed; i++)
385 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
386 numStrokes++;
388 /* Allocate memory for number-of-points-in-stroke array */
389 pNumPointsInStroke=HeapAlloc( GetProcessHeap(), 0, sizeof(int) * numStrokes );
390 if(!pNumPointsInStroke)
392 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
393 return FALSE;
396 /* Second pass: remember number of points in each polygon */
397 iStroke=-1; /* Will get incremented to 0 at beginning of first stroke */
398 for(i=0; i<pPath->numEntriesUsed; i++)
400 /* Is this the beginning of a new stroke? */
401 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
403 iStroke++;
404 pNumPointsInStroke[iStroke]=0;
407 pNumPointsInStroke[iStroke]++;
410 /* Create a region from the strokes */
411 hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
412 numStrokes, nPolyFillMode);
414 /* Free memory for number-of-points-in-stroke array */
415 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
417 if(hrgn==NULL)
419 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
420 return FALSE;
423 /* Success! */
424 *pHrgn=hrgn;
425 return TRUE;
428 /* PATH_ScaleNormalizedPoint
430 * Scales a normalized point (x, y) with respect to the box whose corners are
431 * passed in "corners". The point is stored in "*pPoint". The normalized
432 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
433 * (1.0, 1.0) correspond to corners[1].
435 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
436 double y, POINT *pPoint)
438 pPoint->x=GDI_ROUND( (double)corners[0].x + (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
439 pPoint->y=GDI_ROUND( (double)corners[0].y + (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
442 /* PATH_NormalizePoint
444 * Normalizes a point with respect to the box whose corners are passed in
445 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
447 static void PATH_NormalizePoint(FLOAT_POINT corners[],
448 const FLOAT_POINT *pPoint,
449 double *pX, double *pY)
451 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) * 2.0 - 1.0;
452 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) * 2.0 - 1.0;
455 /* PATH_DoArcPart
457 * Creates a Bezier spline that corresponds to part of an arc and appends the
458 * corresponding points to the path. The start and end angles are passed in
459 * "angleStart" and "angleEnd"; these angles should span a quarter circle
460 * at most. If "startEntryType" is non-zero, an entry of that type for the first
461 * control point is added to the path; otherwise, it is assumed that the current
462 * position is equal to the first control point.
464 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
465 double angleStart, double angleEnd, BYTE startEntryType)
467 double halfAngle, a;
468 double xNorm[4], yNorm[4];
469 POINT point;
470 int i;
472 assert(fabs(angleEnd-angleStart)<=M_PI_2);
474 /* FIXME: Is there an easier way of computing this? */
476 /* Compute control points */
477 halfAngle=(angleEnd-angleStart)/2.0;
478 if(fabs(halfAngle)>1e-8)
480 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
481 xNorm[0]=cos(angleStart);
482 yNorm[0]=sin(angleStart);
483 xNorm[1]=xNorm[0] - a*yNorm[0];
484 yNorm[1]=yNorm[0] + a*xNorm[0];
485 xNorm[3]=cos(angleEnd);
486 yNorm[3]=sin(angleEnd);
487 xNorm[2]=xNorm[3] + a*yNorm[3];
488 yNorm[2]=yNorm[3] - a*xNorm[3];
490 else
491 for(i=0; i<4; i++)
493 xNorm[i]=cos(angleStart);
494 yNorm[i]=sin(angleStart);
497 /* Add starting point to path if desired */
498 if(startEntryType)
500 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
501 if(!PATH_AddEntry(pPath, &point, startEntryType))
502 return FALSE;
505 /* Add remaining control points */
506 for(i=1; i<4; i++)
508 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
509 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
510 return FALSE;
513 return TRUE;
517 /***********************************************************************
518 * BeginPath (GDI32.@)
520 BOOL WINAPI BeginPath(HDC hdc)
522 BOOL ret = FALSE;
523 DC *dc = get_dc_ptr( hdc );
525 if (dc)
527 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pBeginPath );
528 ret = physdev->funcs->pBeginPath( physdev );
529 release_dc_ptr( dc );
531 return ret;
535 /***********************************************************************
536 * EndPath (GDI32.@)
538 BOOL WINAPI EndPath(HDC hdc)
540 BOOL ret = FALSE;
541 DC *dc = get_dc_ptr( hdc );
543 if (dc)
545 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pEndPath );
546 ret = physdev->funcs->pEndPath( physdev );
547 release_dc_ptr( dc );
549 return ret;
553 /******************************************************************************
554 * AbortPath [GDI32.@]
555 * Closes and discards paths from device context
557 * NOTES
558 * Check that SetLastError is being called correctly
560 * PARAMS
561 * hdc [I] Handle to device context
563 * RETURNS
564 * Success: TRUE
565 * Failure: FALSE
567 BOOL WINAPI AbortPath( HDC hdc )
569 BOOL ret = FALSE;
570 DC *dc = get_dc_ptr( hdc );
572 if (dc)
574 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pAbortPath );
575 ret = physdev->funcs->pAbortPath( physdev );
576 release_dc_ptr( dc );
578 return ret;
582 /***********************************************************************
583 * CloseFigure (GDI32.@)
585 * FIXME: Check that SetLastError is being called correctly
587 BOOL WINAPI CloseFigure(HDC hdc)
589 BOOL ret = FALSE;
590 DC *dc = get_dc_ptr( hdc );
592 if (dc)
594 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pCloseFigure );
595 ret = physdev->funcs->pCloseFigure( physdev );
596 release_dc_ptr( dc );
598 return ret;
602 /***********************************************************************
603 * GetPath (GDI32.@)
605 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
606 INT nSize)
608 INT ret = -1;
609 GdiPath *pPath;
610 DC *dc = get_dc_ptr( hdc );
612 if(!dc) return -1;
614 pPath = &dc->path;
616 /* Check that path is closed */
617 if(pPath->state!=PATH_Closed)
619 SetLastError(ERROR_CAN_NOT_COMPLETE);
620 goto done;
623 if(nSize==0)
624 ret = pPath->numEntriesUsed;
625 else if(nSize<pPath->numEntriesUsed)
627 SetLastError(ERROR_INVALID_PARAMETER);
628 goto done;
630 else
632 memcpy(pPoints, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
633 memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
635 /* Convert the points to logical coordinates */
636 if(!DPtoLP(hdc, pPoints, pPath->numEntriesUsed))
638 /* FIXME: Is this the correct value? */
639 SetLastError(ERROR_CAN_NOT_COMPLETE);
640 goto done;
642 else ret = pPath->numEntriesUsed;
644 done:
645 release_dc_ptr( dc );
646 return ret;
650 /***********************************************************************
651 * PathToRegion (GDI32.@)
653 * FIXME
654 * Check that SetLastError is being called correctly
656 * The documentation does not state this explicitly, but a test under Windows
657 * shows that the region which is returned should be in device coordinates.
659 HRGN WINAPI PathToRegion(HDC hdc)
661 GdiPath *pPath;
662 HRGN hrgnRval = 0;
663 DC *dc = get_dc_ptr( hdc );
665 /* Get pointer to path */
666 if(!dc) return 0;
668 pPath = &dc->path;
670 /* Check that path is closed */
671 if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
672 else
674 /* FIXME: Should we empty the path even if conversion failed? */
675 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
676 PATH_EmptyPath(pPath);
677 else
678 hrgnRval=0;
680 release_dc_ptr( dc );
681 return hrgnRval;
684 static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
686 INT mapMode, graphicsMode;
687 SIZE ptViewportExt, ptWindowExt;
688 POINT ptViewportOrg, ptWindowOrg;
689 XFORM xform;
690 HRGN hrgn;
692 /* Construct a region from the path and fill it */
693 if(PATH_PathToRegion(pPath, dc->polyFillMode, &hrgn))
695 /* Since PaintRgn interprets the region as being in logical coordinates
696 * but the points we store for the path are already in device
697 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
698 * Using SaveDC to save information about the mapping mode / world
699 * transform would be easier but would require more overhead, especially
700 * now that SaveDC saves the current path.
703 /* Save the information about the old mapping mode */
704 mapMode=GetMapMode(dc->hSelf);
705 GetViewportExtEx(dc->hSelf, &ptViewportExt);
706 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
707 GetWindowExtEx(dc->hSelf, &ptWindowExt);
708 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
710 /* Save world transform
711 * NB: The Windows documentation on world transforms would lead one to
712 * believe that this has to be done only in GM_ADVANCED; however, my
713 * tests show that resetting the graphics mode to GM_COMPATIBLE does
714 * not reset the world transform.
716 GetWorldTransform(dc->hSelf, &xform);
718 /* Set MM_TEXT */
719 SetMapMode(dc->hSelf, MM_TEXT);
720 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
721 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
722 graphicsMode=GetGraphicsMode(dc->hSelf);
723 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
724 ModifyWorldTransform(dc->hSelf, &xform, MWT_IDENTITY);
725 SetGraphicsMode(dc->hSelf, graphicsMode);
727 /* Paint the region */
728 PaintRgn(dc->hSelf, hrgn);
729 DeleteObject(hrgn);
730 /* Restore the old mapping mode */
731 SetMapMode(dc->hSelf, mapMode);
732 SetViewportExtEx(dc->hSelf, ptViewportExt.cx, ptViewportExt.cy, NULL);
733 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
734 SetWindowExtEx(dc->hSelf, ptWindowExt.cx, ptWindowExt.cy, NULL);
735 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
737 /* Go to GM_ADVANCED temporarily to restore the world transform */
738 graphicsMode=GetGraphicsMode(dc->hSelf);
739 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
740 SetWorldTransform(dc->hSelf, &xform);
741 SetGraphicsMode(dc->hSelf, graphicsMode);
742 return TRUE;
744 return FALSE;
748 /***********************************************************************
749 * FillPath (GDI32.@)
751 * FIXME
752 * Check that SetLastError is being called correctly
754 BOOL WINAPI FillPath(HDC hdc)
756 BOOL ret = FALSE;
757 DC *dc = get_dc_ptr( hdc );
759 if (dc)
761 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pFillPath );
762 ret = physdev->funcs->pFillPath( physdev );
763 release_dc_ptr( dc );
765 return ret;
769 /***********************************************************************
770 * SelectClipPath (GDI32.@)
771 * FIXME
772 * Check that SetLastError is being called correctly
774 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
776 BOOL ret = FALSE;
777 DC *dc = get_dc_ptr( hdc );
779 if (dc)
781 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectClipPath );
782 ret = physdev->funcs->pSelectClipPath( physdev, iMode );
783 release_dc_ptr( dc );
785 return ret;
789 /***********************************************************************
790 * pathdrv_AbortPath
792 static BOOL pathdrv_AbortPath( PHYSDEV dev )
794 DC *dc = get_dc_ptr( dev->hdc );
796 if (!dc) return FALSE;
797 PATH_EmptyPath( &dc->path );
798 pop_path_driver( dc );
799 release_dc_ptr( dc );
800 return TRUE;
804 /***********************************************************************
805 * pathdrv_EndPath
807 static BOOL pathdrv_EndPath( PHYSDEV dev )
809 DC *dc = get_dc_ptr( dev->hdc );
811 if (!dc) return FALSE;
812 dc->path.state = PATH_Closed;
813 pop_path_driver( dc );
814 release_dc_ptr( dc );
815 return TRUE;
819 /***********************************************************************
820 * pathdrv_CreateDC
822 static BOOL pathdrv_CreateDC( PHYSDEV *dev, LPCWSTR driver, LPCWSTR device,
823 LPCWSTR output, const DEVMODEW *devmode )
825 struct path_physdev *physdev = HeapAlloc( GetProcessHeap(), 0, sizeof(*physdev) );
826 DC *dc;
828 if (!physdev) return FALSE;
829 dc = get_dc_ptr( (*dev)->hdc );
830 physdev->path = &dc->path;
831 push_dc_driver( dev, &physdev->dev, &path_driver );
832 release_dc_ptr( dc );
833 return TRUE;
837 /*************************************************************
838 * pathdrv_DeleteDC
840 static BOOL pathdrv_DeleteDC( PHYSDEV dev )
842 assert( 0 ); /* should never be called */
843 return TRUE;
847 /* PATH_InitGdiPath
849 * Initializes the GdiPath structure.
851 void PATH_InitGdiPath(GdiPath *pPath)
853 assert(pPath!=NULL);
855 pPath->state=PATH_Null;
856 pPath->pPoints=NULL;
857 pPath->pFlags=NULL;
858 pPath->numEntriesUsed=0;
859 pPath->numEntriesAllocated=0;
862 /* PATH_DestroyGdiPath
864 * Destroys a GdiPath structure (frees the memory in the arrays).
866 void PATH_DestroyGdiPath(GdiPath *pPath)
868 assert(pPath!=NULL);
870 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
871 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
874 BOOL PATH_SavePath( DC *dst, DC *src )
876 PATH_InitGdiPath( &dst->path );
877 return PATH_AssignGdiPath( &dst->path, &src->path );
880 BOOL PATH_RestorePath( DC *dst, DC *src )
882 BOOL ret;
884 if (src->path.state == PATH_Open && dst->path.state != PATH_Open)
886 if (!path_driver.pCreateDC( &dst->physDev, NULL, NULL, NULL, NULL )) return FALSE;
887 ret = PATH_AssignGdiPath( &dst->path, &src->path );
888 if (!ret) pop_path_driver( dst );
890 else if (src->path.state != PATH_Open && dst->path.state == PATH_Open)
892 ret = PATH_AssignGdiPath( &dst->path, &src->path );
893 if (ret) pop_path_driver( dst );
895 else ret = PATH_AssignGdiPath( &dst->path, &src->path );
896 return ret;
900 /*************************************************************
901 * pathdrv_MoveTo
903 static BOOL pathdrv_MoveTo( PHYSDEV dev, INT x, INT y )
905 struct path_physdev *physdev = get_path_physdev( dev );
906 physdev->path->newStroke = TRUE;
907 return TRUE;
911 /*************************************************************
912 * pathdrv_LineTo
914 static BOOL pathdrv_LineTo( PHYSDEV dev, INT x, INT y )
916 struct path_physdev *physdev = get_path_physdev( dev );
917 POINT point;
919 /* Convert point to device coordinates */
920 point.x = x;
921 point.y = y;
922 LPtoDP( dev->hdc, &point, 1 );
924 if (!start_new_stroke( physdev )) return FALSE;
926 return PATH_AddEntry(physdev->path, &point, PT_LINETO);
929 /* PATH_RoundRect
931 * Should be called when a call to RoundRect is performed on a DC that has
932 * an open path. Returns TRUE if successful, else FALSE.
934 * FIXME: it adds the same entries to the path as windows does, but there
935 * is an error in the bezier drawing code so that there are small pixel-size
936 * gaps when the resulting path is drawn by StrokePath()
938 BOOL PATH_RoundRect(DC *dc, INT x1, INT y1, INT x2, INT y2, INT ell_width, INT ell_height)
940 GdiPath *pPath = &dc->path;
941 POINT corners[2], pointTemp;
942 FLOAT_POINT ellCorners[2];
944 /* Check that path is open */
945 if(pPath->state!=PATH_Open)
946 return FALSE;
948 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
949 return FALSE;
951 /* Add points to the roundrect path */
952 ellCorners[0].x = corners[1].x-ell_width;
953 ellCorners[0].y = corners[0].y;
954 ellCorners[1].x = corners[1].x;
955 ellCorners[1].y = corners[0].y+ell_height;
956 if(!PATH_DoArcPart(pPath, ellCorners, 0, -M_PI_2, PT_MOVETO))
957 return FALSE;
958 pointTemp.x = corners[0].x+ell_width/2;
959 pointTemp.y = corners[0].y;
960 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
961 return FALSE;
962 ellCorners[0].x = corners[0].x;
963 ellCorners[1].x = corners[0].x+ell_width;
964 if(!PATH_DoArcPart(pPath, ellCorners, -M_PI_2, -M_PI, FALSE))
965 return FALSE;
966 pointTemp.x = corners[0].x;
967 pointTemp.y = corners[1].y-ell_height/2;
968 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
969 return FALSE;
970 ellCorners[0].y = corners[1].y-ell_height;
971 ellCorners[1].y = corners[1].y;
972 if(!PATH_DoArcPart(pPath, ellCorners, M_PI, M_PI_2, FALSE))
973 return FALSE;
974 pointTemp.x = corners[1].x-ell_width/2;
975 pointTemp.y = corners[1].y;
976 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
977 return FALSE;
978 ellCorners[0].x = corners[1].x-ell_width;
979 ellCorners[1].x = corners[1].x;
980 if(!PATH_DoArcPart(pPath, ellCorners, M_PI_2, 0, FALSE))
981 return FALSE;
983 /* Close the roundrect figure */
984 if(!CloseFigure(dc->hSelf))
985 return FALSE;
987 return TRUE;
990 /* PATH_Rectangle
992 * Should be called when a call to Rectangle is performed on a DC that has
993 * an open path. Returns TRUE if successful, else FALSE.
995 BOOL PATH_Rectangle(DC *dc, INT x1, INT y1, INT x2, INT y2)
997 GdiPath *pPath = &dc->path;
998 POINT corners[2], pointTemp;
1000 /* Check that path is open */
1001 if(pPath->state!=PATH_Open)
1002 return FALSE;
1004 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
1005 return FALSE;
1007 /* Close any previous figure */
1008 if(!CloseFigure(dc->hSelf))
1010 /* The CloseFigure call shouldn't have failed */
1011 assert(FALSE);
1012 return FALSE;
1015 /* Add four points to the path */
1016 pointTemp.x=corners[1].x;
1017 pointTemp.y=corners[0].y;
1018 if(!PATH_AddEntry(pPath, &pointTemp, PT_MOVETO))
1019 return FALSE;
1020 if(!PATH_AddEntry(pPath, corners, PT_LINETO))
1021 return FALSE;
1022 pointTemp.x=corners[0].x;
1023 pointTemp.y=corners[1].y;
1024 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
1025 return FALSE;
1026 if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
1027 return FALSE;
1029 /* Close the rectangle figure */
1030 if(!CloseFigure(dc->hSelf))
1032 /* The CloseFigure call shouldn't have failed */
1033 assert(FALSE);
1034 return FALSE;
1037 return TRUE;
1040 /* PATH_Ellipse
1042 * Should be called when a call to Ellipse is performed on a DC that has
1043 * an open path. This adds four Bezier splines representing the ellipse
1044 * to the path. Returns TRUE if successful, else FALSE.
1046 BOOL PATH_Ellipse(DC *dc, INT x1, INT y1, INT x2, INT y2)
1048 return( PATH_Arc(dc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2,0) &&
1049 CloseFigure(dc->hSelf) );
1052 /* PATH_Arc
1054 * Should be called when a call to Arc is performed on a DC that has
1055 * an open path. This adds up to five Bezier splines representing the arc
1056 * to the path. When 'lines' is 1, we add 1 extra line to get a chord,
1057 * when 'lines' is 2, we add 2 extra lines to get a pie, and when 'lines' is
1058 * -1 we add 1 extra line from the current DC position to the starting position
1059 * of the arc before drawing the arc itself (arcto). Returns TRUE if successful,
1060 * else FALSE.
1062 BOOL PATH_Arc(DC *dc, INT x1, INT y1, INT x2, INT y2,
1063 INT xStart, INT yStart, INT xEnd, INT yEnd, INT lines)
1065 GdiPath *pPath = &dc->path;
1066 double angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
1067 /* Initialize angleEndQuadrant to silence gcc's warning */
1068 double x, y;
1069 FLOAT_POINT corners[2], pointStart, pointEnd;
1070 POINT centre, pointCurPos;
1071 BOOL start, end;
1072 INT temp;
1074 /* FIXME: This function should check for all possible error returns */
1075 /* FIXME: Do we have to respect newStroke? */
1077 /* Check that path is open */
1078 if(pPath->state!=PATH_Open)
1079 return FALSE;
1081 /* Check for zero height / width */
1082 /* FIXME: Only in GM_COMPATIBLE? */
1083 if(x1==x2 || y1==y2)
1084 return TRUE;
1086 /* Convert points to device coordinates */
1087 corners[0].x = x1;
1088 corners[0].y = y1;
1089 corners[1].x = x2;
1090 corners[1].y = y2;
1091 pointStart.x = xStart;
1092 pointStart.y = yStart;
1093 pointEnd.x = xEnd;
1094 pointEnd.y = yEnd;
1095 INTERNAL_LPTODP_FLOAT(dc, corners);
1096 INTERNAL_LPTODP_FLOAT(dc, corners+1);
1097 INTERNAL_LPTODP_FLOAT(dc, &pointStart);
1098 INTERNAL_LPTODP_FLOAT(dc, &pointEnd);
1100 /* Make sure first corner is top left and second corner is bottom right */
1101 if(corners[0].x>corners[1].x)
1103 temp=corners[0].x;
1104 corners[0].x=corners[1].x;
1105 corners[1].x=temp;
1107 if(corners[0].y>corners[1].y)
1109 temp=corners[0].y;
1110 corners[0].y=corners[1].y;
1111 corners[1].y=temp;
1114 /* Compute start and end angle */
1115 PATH_NormalizePoint(corners, &pointStart, &x, &y);
1116 angleStart=atan2(y, x);
1117 PATH_NormalizePoint(corners, &pointEnd, &x, &y);
1118 angleEnd=atan2(y, x);
1120 /* Make sure the end angle is "on the right side" of the start angle */
1121 if(dc->ArcDirection==AD_CLOCKWISE)
1123 if(angleEnd<=angleStart)
1125 angleEnd+=2*M_PI;
1126 assert(angleEnd>=angleStart);
1129 else
1131 if(angleEnd>=angleStart)
1133 angleEnd-=2*M_PI;
1134 assert(angleEnd<=angleStart);
1138 /* In GM_COMPATIBLE, don't include bottom and right edges */
1139 if(dc->GraphicsMode==GM_COMPATIBLE)
1141 corners[1].x--;
1142 corners[1].y--;
1145 /* arcto: Add a PT_MOVETO only if this is the first entry in a stroke */
1146 if(lines==-1 && pPath->newStroke)
1148 pPath->newStroke=FALSE;
1149 pointCurPos.x = dc->CursPosX;
1150 pointCurPos.y = dc->CursPosY;
1151 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
1152 return FALSE;
1153 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
1154 return FALSE;
1157 /* Add the arc to the path with one Bezier spline per quadrant that the
1158 * arc spans */
1159 start=TRUE;
1160 end=FALSE;
1163 /* Determine the start and end angles for this quadrant */
1164 if(start)
1166 angleStartQuadrant=angleStart;
1167 if(dc->ArcDirection==AD_CLOCKWISE)
1168 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
1169 else
1170 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
1172 else
1174 angleStartQuadrant=angleEndQuadrant;
1175 if(dc->ArcDirection==AD_CLOCKWISE)
1176 angleEndQuadrant+=M_PI_2;
1177 else
1178 angleEndQuadrant-=M_PI_2;
1181 /* Have we reached the last part of the arc? */
1182 if((dc->ArcDirection==AD_CLOCKWISE &&
1183 angleEnd<angleEndQuadrant) ||
1184 (dc->ArcDirection==AD_COUNTERCLOCKWISE &&
1185 angleEnd>angleEndQuadrant))
1187 /* Adjust the end angle for this quadrant */
1188 angleEndQuadrant=angleEnd;
1189 end=TRUE;
1192 /* Add the Bezier spline to the path */
1193 PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
1194 start ? (lines==-1 ? PT_LINETO : PT_MOVETO) : FALSE);
1195 start=FALSE;
1196 } while(!end);
1198 /* chord: close figure. pie: add line and close figure */
1199 if(lines==1)
1201 if(!CloseFigure(dc->hSelf))
1202 return FALSE;
1204 else if(lines==2)
1206 centre.x = (corners[0].x+corners[1].x)/2;
1207 centre.y = (corners[0].y+corners[1].y)/2;
1208 if(!PATH_AddEntry(pPath, &centre, PT_LINETO | PT_CLOSEFIGURE))
1209 return FALSE;
1212 return TRUE;
1215 BOOL PATH_PolyBezierTo(DC *dc, const POINT *pts, DWORD cbPoints)
1217 GdiPath *pPath = &dc->path;
1218 POINT pt;
1219 UINT i;
1221 /* Check that path is open */
1222 if(pPath->state!=PATH_Open)
1223 return FALSE;
1225 /* Add a PT_MOVETO if necessary */
1226 if(pPath->newStroke)
1228 pPath->newStroke=FALSE;
1229 pt.x = dc->CursPosX;
1230 pt.y = dc->CursPosY;
1231 if(!LPtoDP(dc->hSelf, &pt, 1))
1232 return FALSE;
1233 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
1234 return FALSE;
1237 for(i = 0; i < cbPoints; i++) {
1238 pt = pts[i];
1239 if(!LPtoDP(dc->hSelf, &pt, 1))
1240 return FALSE;
1241 PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
1243 return TRUE;
1246 BOOL PATH_PolyBezier(DC *dc, const POINT *pts, DWORD cbPoints)
1248 GdiPath *pPath = &dc->path;
1249 POINT pt;
1250 UINT i;
1252 /* Check that path is open */
1253 if(pPath->state!=PATH_Open)
1254 return FALSE;
1256 for(i = 0; i < cbPoints; i++) {
1257 pt = pts[i];
1258 if(!LPtoDP(dc->hSelf, &pt, 1))
1259 return FALSE;
1260 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
1262 return TRUE;
1265 /* PATH_PolyDraw
1267 * Should be called when a call to PolyDraw is performed on a DC that has
1268 * an open path. Returns TRUE if successful, else FALSE.
1270 BOOL PATH_PolyDraw(DC *dc, const POINT *pts, const BYTE *types,
1271 DWORD cbPoints)
1273 GdiPath *pPath = &dc->path;
1274 POINT lastmove, orig_pos;
1275 INT i;
1277 GetCurrentPositionEx( dc->hSelf, &orig_pos );
1278 lastmove = orig_pos;
1280 for(i = pPath->numEntriesUsed - 1; i >= 0; i--){
1281 if(pPath->pFlags[i] == PT_MOVETO){
1282 lastmove = pPath->pPoints[i];
1283 DPtoLP(dc->hSelf, &lastmove, 1);
1284 break;
1288 for(i = 0; i < cbPoints; i++)
1290 switch (types[i])
1292 case PT_MOVETO:
1293 MoveToEx( dc->hSelf, pts[i].x, pts[i].y, NULL );
1294 break;
1295 case PT_LINETO:
1296 case PT_LINETO | PT_CLOSEFIGURE:
1297 LineTo( dc->hSelf, pts[i].x, pts[i].y );
1298 break;
1299 case PT_BEZIERTO:
1300 if ((i + 2 < cbPoints) && (types[i + 1] == PT_BEZIERTO) &&
1301 (types[i + 2] & ~PT_CLOSEFIGURE) == PT_BEZIERTO)
1303 PolyBezierTo( dc->hSelf, &pts[i], 3 );
1304 i += 2;
1305 break;
1307 /* fall through */
1308 default:
1309 if (i) /* restore original position */
1311 if (!(types[i - 1] & PT_CLOSEFIGURE)) lastmove = pts[i - 1];
1312 if (lastmove.x != orig_pos.x || lastmove.y != orig_pos.y)
1313 MoveToEx( dc->hSelf, orig_pos.x, orig_pos.y, NULL );
1315 return FALSE;
1318 if(types[i] & PT_CLOSEFIGURE){
1319 pPath->pFlags[pPath->numEntriesUsed-1] |= PT_CLOSEFIGURE;
1320 MoveToEx( dc->hSelf, lastmove.x, lastmove.y, NULL );
1324 return TRUE;
1327 BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
1329 GdiPath *pPath = &dc->path;
1330 POINT pt;
1331 UINT i;
1333 /* Check that path is open */
1334 if(pPath->state!=PATH_Open)
1335 return FALSE;
1337 for(i = 0; i < cbPoints; i++) {
1338 pt = pts[i];
1339 if(!LPtoDP(dc->hSelf, &pt, 1))
1340 return FALSE;
1341 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
1343 return TRUE;
1346 BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
1348 GdiPath *pPath = &dc->path;
1349 POINT pt;
1350 UINT i;
1352 /* Check that path is open */
1353 if(pPath->state!=PATH_Open)
1354 return FALSE;
1356 /* Add a PT_MOVETO if necessary */
1357 if(pPath->newStroke)
1359 pPath->newStroke=FALSE;
1360 pt.x = dc->CursPosX;
1361 pt.y = dc->CursPosY;
1362 if(!LPtoDP(dc->hSelf, &pt, 1))
1363 return FALSE;
1364 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
1365 return FALSE;
1368 for(i = 0; i < cbPoints; i++) {
1369 pt = pts[i];
1370 if(!LPtoDP(dc->hSelf, &pt, 1))
1371 return FALSE;
1372 PATH_AddEntry(pPath, &pt, PT_LINETO);
1375 return TRUE;
1379 BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
1381 GdiPath *pPath = &dc->path;
1382 POINT pt;
1383 UINT i;
1385 /* Check that path is open */
1386 if(pPath->state!=PATH_Open)
1387 return FALSE;
1389 for(i = 0; i < cbPoints; i++) {
1390 pt = pts[i];
1391 if(!LPtoDP(dc->hSelf, &pt, 1))
1392 return FALSE;
1393 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
1394 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
1395 PT_LINETO));
1397 return TRUE;
1400 BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
1401 UINT polygons )
1403 GdiPath *pPath = &dc->path;
1404 POINT pt, startpt;
1405 UINT poly, i;
1406 INT point;
1408 /* Check that path is open */
1409 if(pPath->state!=PATH_Open)
1410 return FALSE;
1412 for(i = 0, poly = 0; poly < polygons; poly++) {
1413 for(point = 0; point < counts[poly]; point++, i++) {
1414 pt = pts[i];
1415 if(!LPtoDP(dc->hSelf, &pt, 1))
1416 return FALSE;
1417 if(point == 0) startpt = pt;
1418 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1420 /* win98 adds an extra line to close the figure for some reason */
1421 PATH_AddEntry(pPath, &startpt, PT_LINETO | PT_CLOSEFIGURE);
1423 return TRUE;
1426 BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1427 DWORD polylines )
1429 GdiPath *pPath = &dc->path;
1430 POINT pt;
1431 UINT poly, point, i;
1433 /* Check that path is open */
1434 if(pPath->state!=PATH_Open)
1435 return FALSE;
1437 for(i = 0, poly = 0; poly < polylines; poly++) {
1438 for(point = 0; point < counts[poly]; point++, i++) {
1439 pt = pts[i];
1440 if(!LPtoDP(dc->hSelf, &pt, 1))
1441 return FALSE;
1442 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1445 return TRUE;
1449 /**********************************************************************
1450 * PATH_BezierTo
1452 * internally used by PATH_add_outline
1454 static void PATH_BezierTo(GdiPath *pPath, POINT *lppt, INT n)
1456 if (n < 2) return;
1458 if (n == 2)
1460 PATH_AddEntry(pPath, &lppt[1], PT_LINETO);
1462 else if (n == 3)
1464 PATH_AddEntry(pPath, &lppt[0], PT_BEZIERTO);
1465 PATH_AddEntry(pPath, &lppt[1], PT_BEZIERTO);
1466 PATH_AddEntry(pPath, &lppt[2], PT_BEZIERTO);
1468 else
1470 POINT pt[3];
1471 INT i = 0;
1473 pt[2] = lppt[0];
1474 n--;
1476 while (n > 2)
1478 pt[0] = pt[2];
1479 pt[1] = lppt[i+1];
1480 pt[2].x = (lppt[i+2].x + lppt[i+1].x) / 2;
1481 pt[2].y = (lppt[i+2].y + lppt[i+1].y) / 2;
1482 PATH_BezierTo(pPath, pt, 3);
1483 n--;
1484 i++;
1487 pt[0] = pt[2];
1488 pt[1] = lppt[i+1];
1489 pt[2] = lppt[i+2];
1490 PATH_BezierTo(pPath, pt, 3);
1494 static BOOL PATH_add_outline(DC *dc, INT x, INT y, TTPOLYGONHEADER *header, DWORD size)
1496 GdiPath *pPath = &dc->path;
1497 TTPOLYGONHEADER *start;
1498 POINT pt;
1500 start = header;
1502 while ((char *)header < (char *)start + size)
1504 TTPOLYCURVE *curve;
1506 if (header->dwType != TT_POLYGON_TYPE)
1508 FIXME("Unknown header type %d\n", header->dwType);
1509 return FALSE;
1512 pt.x = x + int_from_fixed(header->pfxStart.x);
1513 pt.y = y - int_from_fixed(header->pfxStart.y);
1514 PATH_AddEntry(pPath, &pt, PT_MOVETO);
1516 curve = (TTPOLYCURVE *)(header + 1);
1518 while ((char *)curve < (char *)header + header->cb)
1520 /*TRACE("curve->wType %d\n", curve->wType);*/
1522 switch(curve->wType)
1524 case TT_PRIM_LINE:
1526 WORD i;
1528 for (i = 0; i < curve->cpfx; i++)
1530 pt.x = x + int_from_fixed(curve->apfx[i].x);
1531 pt.y = y - int_from_fixed(curve->apfx[i].y);
1532 PATH_AddEntry(pPath, &pt, PT_LINETO);
1534 break;
1537 case TT_PRIM_QSPLINE:
1538 case TT_PRIM_CSPLINE:
1540 WORD i;
1541 POINTFX ptfx;
1542 POINT *pts = HeapAlloc(GetProcessHeap(), 0, (curve->cpfx + 1) * sizeof(POINT));
1544 if (!pts) return FALSE;
1546 ptfx = *(POINTFX *)((char *)curve - sizeof(POINTFX));
1548 pts[0].x = x + int_from_fixed(ptfx.x);
1549 pts[0].y = y - int_from_fixed(ptfx.y);
1551 for(i = 0; i < curve->cpfx; i++)
1553 pts[i + 1].x = x + int_from_fixed(curve->apfx[i].x);
1554 pts[i + 1].y = y - int_from_fixed(curve->apfx[i].y);
1557 PATH_BezierTo(pPath, pts, curve->cpfx + 1);
1559 HeapFree(GetProcessHeap(), 0, pts);
1560 break;
1563 default:
1564 FIXME("Unknown curve type %04x\n", curve->wType);
1565 return FALSE;
1568 curve = (TTPOLYCURVE *)&curve->apfx[curve->cpfx];
1571 header = (TTPOLYGONHEADER *)((char *)header + header->cb);
1574 return CloseFigure(dc->hSelf);
1577 /**********************************************************************
1578 * PATH_ExtTextOut
1580 BOOL PATH_ExtTextOut(DC *dc, INT x, INT y, UINT flags, const RECT *lprc,
1581 LPCWSTR str, UINT count, const INT *dx)
1583 unsigned int idx;
1584 HDC hdc = dc->hSelf;
1585 POINT offset = {0, 0};
1587 TRACE("%p, %d, %d, %08x, %s, %s, %d, %p)\n", hdc, x, y, flags,
1588 wine_dbgstr_rect(lprc), debugstr_wn(str, count), count, dx);
1590 if (!count) return TRUE;
1592 for (idx = 0; idx < count; idx++)
1594 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
1595 GLYPHMETRICS gm;
1596 DWORD dwSize;
1597 void *outline;
1599 dwSize = GetGlyphOutlineW(hdc, str[idx], GGO_GLYPH_INDEX | GGO_NATIVE, &gm, 0, NULL, &identity);
1600 if (dwSize == GDI_ERROR) return FALSE;
1602 /* add outline only if char is printable */
1603 if(dwSize)
1605 outline = HeapAlloc(GetProcessHeap(), 0, dwSize);
1606 if (!outline) return FALSE;
1608 GetGlyphOutlineW(hdc, str[idx], GGO_GLYPH_INDEX | GGO_NATIVE, &gm, dwSize, outline, &identity);
1610 PATH_add_outline(dc, x + offset.x, y + offset.y, outline, dwSize);
1612 HeapFree(GetProcessHeap(), 0, outline);
1615 if (dx)
1617 if(flags & ETO_PDY)
1619 offset.x += dx[idx * 2];
1620 offset.y += dx[idx * 2 + 1];
1622 else
1623 offset.x += dx[idx];
1625 else
1627 offset.x += gm.gmCellIncX;
1628 offset.y += gm.gmCellIncY;
1631 return TRUE;
1635 /*******************************************************************
1636 * FlattenPath [GDI32.@]
1640 BOOL WINAPI FlattenPath(HDC hdc)
1642 BOOL ret = FALSE;
1643 DC *dc = get_dc_ptr( hdc );
1645 if (dc)
1647 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pFlattenPath );
1648 ret = physdev->funcs->pFlattenPath( physdev );
1649 release_dc_ptr( dc );
1651 return ret;
1655 static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1657 INT i, nLinePts, nAlloc;
1658 POINT *pLinePts;
1659 POINT ptViewportOrg, ptWindowOrg;
1660 SIZE szViewportExt, szWindowExt;
1661 DWORD mapMode, graphicsMode;
1662 XFORM xform;
1663 BOOL ret = TRUE;
1665 /* Save the mapping mode info */
1666 mapMode=GetMapMode(dc->hSelf);
1667 GetViewportExtEx(dc->hSelf, &szViewportExt);
1668 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
1669 GetWindowExtEx(dc->hSelf, &szWindowExt);
1670 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
1671 GetWorldTransform(dc->hSelf, &xform);
1673 /* Set MM_TEXT */
1674 SetMapMode(dc->hSelf, MM_TEXT);
1675 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
1676 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
1677 graphicsMode=GetGraphicsMode(dc->hSelf);
1678 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1679 ModifyWorldTransform(dc->hSelf, &xform, MWT_IDENTITY);
1680 SetGraphicsMode(dc->hSelf, graphicsMode);
1682 /* Allocate enough memory for the worst case without beziers (one PT_MOVETO
1683 * and the rest PT_LINETO with PT_CLOSEFIGURE at the end) plus some buffer
1684 * space in case we get one to keep the number of reallocations small. */
1685 nAlloc = pPath->numEntriesUsed + 1 + 300;
1686 pLinePts = HeapAlloc(GetProcessHeap(), 0, nAlloc * sizeof(POINT));
1687 nLinePts = 0;
1689 for(i = 0; i < pPath->numEntriesUsed; i++) {
1690 if((i == 0 || (pPath->pFlags[i-1] & PT_CLOSEFIGURE)) &&
1691 (pPath->pFlags[i] != PT_MOVETO)) {
1692 ERR("Expected PT_MOVETO %s, got path flag %d\n",
1693 i == 0 ? "as first point" : "after PT_CLOSEFIGURE",
1694 (INT)pPath->pFlags[i]);
1695 ret = FALSE;
1696 goto end;
1698 switch(pPath->pFlags[i]) {
1699 case PT_MOVETO:
1700 TRACE("Got PT_MOVETO (%d, %d)\n",
1701 pPath->pPoints[i].x, pPath->pPoints[i].y);
1702 if(nLinePts >= 2)
1703 Polyline(dc->hSelf, pLinePts, nLinePts);
1704 nLinePts = 0;
1705 pLinePts[nLinePts++] = pPath->pPoints[i];
1706 break;
1707 case PT_LINETO:
1708 case (PT_LINETO | PT_CLOSEFIGURE):
1709 TRACE("Got PT_LINETO (%d, %d)\n",
1710 pPath->pPoints[i].x, pPath->pPoints[i].y);
1711 pLinePts[nLinePts++] = pPath->pPoints[i];
1712 break;
1713 case PT_BEZIERTO:
1714 TRACE("Got PT_BEZIERTO\n");
1715 if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1716 (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1717 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1718 ret = FALSE;
1719 goto end;
1720 } else {
1721 INT nBzrPts, nMinAlloc;
1722 POINT *pBzrPts = GDI_Bezier(&pPath->pPoints[i-1], 4, &nBzrPts);
1723 /* Make sure we have allocated enough memory for the lines of
1724 * this bezier and the rest of the path, assuming we won't get
1725 * another one (since we won't reallocate again then). */
1726 nMinAlloc = nLinePts + (pPath->numEntriesUsed - i) + nBzrPts;
1727 if(nAlloc < nMinAlloc)
1729 nAlloc = nMinAlloc * 2;
1730 pLinePts = HeapReAlloc(GetProcessHeap(), 0, pLinePts,
1731 nAlloc * sizeof(POINT));
1733 memcpy(&pLinePts[nLinePts], &pBzrPts[1],
1734 (nBzrPts - 1) * sizeof(POINT));
1735 nLinePts += nBzrPts - 1;
1736 HeapFree(GetProcessHeap(), 0, pBzrPts);
1737 i += 2;
1739 break;
1740 default:
1741 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1742 ret = FALSE;
1743 goto end;
1745 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1746 pLinePts[nLinePts++] = pLinePts[0];
1748 if(nLinePts >= 2)
1749 Polyline(dc->hSelf, pLinePts, nLinePts);
1751 end:
1752 HeapFree(GetProcessHeap(), 0, pLinePts);
1754 /* Restore the old mapping mode */
1755 SetMapMode(dc->hSelf, mapMode);
1756 SetWindowExtEx(dc->hSelf, szWindowExt.cx, szWindowExt.cy, NULL);
1757 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
1758 SetViewportExtEx(dc->hSelf, szViewportExt.cx, szViewportExt.cy, NULL);
1759 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
1761 /* Go to GM_ADVANCED temporarily to restore the world transform */
1762 graphicsMode=GetGraphicsMode(dc->hSelf);
1763 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1764 SetWorldTransform(dc->hSelf, &xform);
1765 SetGraphicsMode(dc->hSelf, graphicsMode);
1767 /* If we've moved the current point then get its new position
1768 which will be in device (MM_TEXT) co-ords, convert it to
1769 logical co-ords and re-set it. This basically updates
1770 dc->CurPosX|Y so that their values are in the correct mapping
1771 mode.
1773 if(i > 0) {
1774 POINT pt;
1775 GetCurrentPositionEx(dc->hSelf, &pt);
1776 DPtoLP(dc->hSelf, &pt, 1);
1777 MoveToEx(dc->hSelf, pt.x, pt.y, NULL);
1780 return ret;
1783 #define round(x) ((int)((x)>0?(x)+0.5:(x)-0.5))
1785 static BOOL PATH_WidenPath(DC *dc)
1787 INT i, j, numStrokes, penWidth, penWidthIn, penWidthOut, size, penStyle;
1788 BOOL ret = FALSE;
1789 GdiPath *pPath, *pNewPath, **pStrokes = NULL, *pUpPath, *pDownPath;
1790 EXTLOGPEN *elp;
1791 DWORD obj_type, joint, endcap, penType;
1793 pPath = &dc->path;
1795 PATH_FlattenPath(pPath);
1797 size = GetObjectW( dc->hPen, 0, NULL );
1798 if (!size) {
1799 SetLastError(ERROR_CAN_NOT_COMPLETE);
1800 return FALSE;
1803 elp = HeapAlloc( GetProcessHeap(), 0, size );
1804 GetObjectW( dc->hPen, size, elp );
1806 obj_type = GetObjectType(dc->hPen);
1807 if(obj_type == OBJ_PEN) {
1808 penStyle = ((LOGPEN*)elp)->lopnStyle;
1810 else if(obj_type == OBJ_EXTPEN) {
1811 penStyle = elp->elpPenStyle;
1813 else {
1814 SetLastError(ERROR_CAN_NOT_COMPLETE);
1815 HeapFree( GetProcessHeap(), 0, elp );
1816 return FALSE;
1819 penWidth = elp->elpWidth;
1820 HeapFree( GetProcessHeap(), 0, elp );
1822 endcap = (PS_ENDCAP_MASK & penStyle);
1823 joint = (PS_JOIN_MASK & penStyle);
1824 penType = (PS_TYPE_MASK & penStyle);
1826 /* The function cannot apply to cosmetic pens */
1827 if(obj_type == OBJ_EXTPEN && penType == PS_COSMETIC) {
1828 SetLastError(ERROR_CAN_NOT_COMPLETE);
1829 return FALSE;
1832 penWidthIn = penWidth / 2;
1833 penWidthOut = penWidth / 2;
1834 if(penWidthIn + penWidthOut < penWidth)
1835 penWidthOut++;
1837 numStrokes = 0;
1839 for(i = 0, j = 0; i < pPath->numEntriesUsed; i++, j++) {
1840 POINT point;
1841 if((i == 0 || (pPath->pFlags[i-1] & PT_CLOSEFIGURE)) &&
1842 (pPath->pFlags[i] != PT_MOVETO)) {
1843 ERR("Expected PT_MOVETO %s, got path flag %c\n",
1844 i == 0 ? "as first point" : "after PT_CLOSEFIGURE",
1845 pPath->pFlags[i]);
1846 return FALSE;
1848 switch(pPath->pFlags[i]) {
1849 case PT_MOVETO:
1850 if(numStrokes > 0) {
1851 pStrokes[numStrokes - 1]->state = PATH_Closed;
1853 numStrokes++;
1854 j = 0;
1855 if(numStrokes == 1)
1856 pStrokes = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath*));
1857 else
1858 pStrokes = HeapReAlloc(GetProcessHeap(), 0, pStrokes, numStrokes * sizeof(GdiPath*));
1859 if(!pStrokes) return FALSE;
1860 pStrokes[numStrokes - 1] = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1861 PATH_InitGdiPath(pStrokes[numStrokes - 1]);
1862 pStrokes[numStrokes - 1]->state = PATH_Open;
1863 /* fall through */
1864 case PT_LINETO:
1865 case (PT_LINETO | PT_CLOSEFIGURE):
1866 point.x = pPath->pPoints[i].x;
1867 point.y = pPath->pPoints[i].y;
1868 PATH_AddEntry(pStrokes[numStrokes - 1], &point, pPath->pFlags[i]);
1869 break;
1870 case PT_BEZIERTO:
1871 /* should never happen because of the FlattenPath call */
1872 ERR("Should never happen\n");
1873 break;
1874 default:
1875 ERR("Got path flag %c\n", pPath->pFlags[i]);
1876 return FALSE;
1880 pNewPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1881 PATH_InitGdiPath(pNewPath);
1882 pNewPath->state = PATH_Open;
1884 for(i = 0; i < numStrokes; i++) {
1885 pUpPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1886 PATH_InitGdiPath(pUpPath);
1887 pUpPath->state = PATH_Open;
1888 pDownPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1889 PATH_InitGdiPath(pDownPath);
1890 pDownPath->state = PATH_Open;
1892 for(j = 0; j < pStrokes[i]->numEntriesUsed; j++) {
1893 /* Beginning or end of the path if not closed */
1894 if((!(pStrokes[i]->pFlags[pStrokes[i]->numEntriesUsed - 1] & PT_CLOSEFIGURE)) && (j == 0 || j == pStrokes[i]->numEntriesUsed - 1) ) {
1895 /* Compute segment angle */
1896 double xo, yo, xa, ya, theta;
1897 POINT pt;
1898 FLOAT_POINT corners[2];
1899 if(j == 0) {
1900 xo = pStrokes[i]->pPoints[j].x;
1901 yo = pStrokes[i]->pPoints[j].y;
1902 xa = pStrokes[i]->pPoints[1].x;
1903 ya = pStrokes[i]->pPoints[1].y;
1905 else {
1906 xa = pStrokes[i]->pPoints[j - 1].x;
1907 ya = pStrokes[i]->pPoints[j - 1].y;
1908 xo = pStrokes[i]->pPoints[j].x;
1909 yo = pStrokes[i]->pPoints[j].y;
1911 theta = atan2( ya - yo, xa - xo );
1912 switch(endcap) {
1913 case PS_ENDCAP_SQUARE :
1914 pt.x = xo + round(sqrt(2) * penWidthOut * cos(M_PI_4 + theta));
1915 pt.y = yo + round(sqrt(2) * penWidthOut * sin(M_PI_4 + theta));
1916 PATH_AddEntry(pUpPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO) );
1917 pt.x = xo + round(sqrt(2) * penWidthIn * cos(- M_PI_4 + theta));
1918 pt.y = yo + round(sqrt(2) * penWidthIn * sin(- M_PI_4 + theta));
1919 PATH_AddEntry(pUpPath, &pt, PT_LINETO);
1920 break;
1921 case PS_ENDCAP_FLAT :
1922 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
1923 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
1924 PATH_AddEntry(pUpPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO));
1925 pt.x = xo - round( penWidthIn * cos(theta + M_PI_2) );
1926 pt.y = yo - round( penWidthIn * sin(theta + M_PI_2) );
1927 PATH_AddEntry(pUpPath, &pt, PT_LINETO);
1928 break;
1929 case PS_ENDCAP_ROUND :
1930 default :
1931 corners[0].x = xo - penWidthIn;
1932 corners[0].y = yo - penWidthIn;
1933 corners[1].x = xo + penWidthOut;
1934 corners[1].y = yo + penWidthOut;
1935 PATH_DoArcPart(pUpPath ,corners, theta + M_PI_2 , theta + 3 * M_PI_4, (j == 0 ? PT_MOVETO : FALSE));
1936 PATH_DoArcPart(pUpPath ,corners, theta + 3 * M_PI_4 , theta + M_PI, FALSE);
1937 PATH_DoArcPart(pUpPath ,corners, theta + M_PI, theta + 5 * M_PI_4, FALSE);
1938 PATH_DoArcPart(pUpPath ,corners, theta + 5 * M_PI_4 , theta + 3 * M_PI_2, FALSE);
1939 break;
1942 /* Corpse of the path */
1943 else {
1944 /* Compute angle */
1945 INT previous, next;
1946 double xa, ya, xb, yb, xo, yo;
1947 double alpha, theta, miterWidth;
1948 DWORD _joint = joint;
1949 POINT pt;
1950 GdiPath *pInsidePath, *pOutsidePath;
1951 if(j > 0 && j < pStrokes[i]->numEntriesUsed - 1) {
1952 previous = j - 1;
1953 next = j + 1;
1955 else if (j == 0) {
1956 previous = pStrokes[i]->numEntriesUsed - 1;
1957 next = j + 1;
1959 else {
1960 previous = j - 1;
1961 next = 0;
1963 xo = pStrokes[i]->pPoints[j].x;
1964 yo = pStrokes[i]->pPoints[j].y;
1965 xa = pStrokes[i]->pPoints[previous].x;
1966 ya = pStrokes[i]->pPoints[previous].y;
1967 xb = pStrokes[i]->pPoints[next].x;
1968 yb = pStrokes[i]->pPoints[next].y;
1969 theta = atan2( yo - ya, xo - xa );
1970 alpha = atan2( yb - yo, xb - xo ) - theta;
1971 if (alpha > 0) alpha -= M_PI;
1972 else alpha += M_PI;
1973 if(_joint == PS_JOIN_MITER && dc->miterLimit < fabs(1 / sin(alpha/2))) {
1974 _joint = PS_JOIN_BEVEL;
1976 if(alpha > 0) {
1977 pInsidePath = pUpPath;
1978 pOutsidePath = pDownPath;
1980 else if(alpha < 0) {
1981 pInsidePath = pDownPath;
1982 pOutsidePath = pUpPath;
1984 else {
1985 continue;
1987 /* Inside angle points */
1988 if(alpha > 0) {
1989 pt.x = xo - round( penWidthIn * cos(theta + M_PI_2) );
1990 pt.y = yo - round( penWidthIn * sin(theta + M_PI_2) );
1992 else {
1993 pt.x = xo + round( penWidthIn * cos(theta + M_PI_2) );
1994 pt.y = yo + round( penWidthIn * sin(theta + M_PI_2) );
1996 PATH_AddEntry(pInsidePath, &pt, PT_LINETO);
1997 if(alpha > 0) {
1998 pt.x = xo + round( penWidthIn * cos(M_PI_2 + alpha + theta) );
1999 pt.y = yo + round( penWidthIn * sin(M_PI_2 + alpha + theta) );
2001 else {
2002 pt.x = xo - round( penWidthIn * cos(M_PI_2 + alpha + theta) );
2003 pt.y = yo - round( penWidthIn * sin(M_PI_2 + alpha + theta) );
2005 PATH_AddEntry(pInsidePath, &pt, PT_LINETO);
2006 /* Outside angle point */
2007 switch(_joint) {
2008 case PS_JOIN_MITER :
2009 miterWidth = fabs(penWidthOut / cos(M_PI_2 - fabs(alpha) / 2));
2010 pt.x = xo + round( miterWidth * cos(theta + alpha / 2) );
2011 pt.y = yo + round( miterWidth * sin(theta + alpha / 2) );
2012 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
2013 break;
2014 case PS_JOIN_BEVEL :
2015 if(alpha > 0) {
2016 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
2017 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
2019 else {
2020 pt.x = xo - round( penWidthOut * cos(theta + M_PI_2) );
2021 pt.y = yo - round( penWidthOut * sin(theta + M_PI_2) );
2023 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
2024 if(alpha > 0) {
2025 pt.x = xo - round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2026 pt.y = yo - round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2028 else {
2029 pt.x = xo + round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2030 pt.y = yo + round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2032 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
2033 break;
2034 case PS_JOIN_ROUND :
2035 default :
2036 if(alpha > 0) {
2037 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
2038 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
2040 else {
2041 pt.x = xo - round( penWidthOut * cos(theta + M_PI_2) );
2042 pt.y = yo - round( penWidthOut * sin(theta + M_PI_2) );
2044 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
2045 pt.x = xo + round( penWidthOut * cos(theta + alpha / 2) );
2046 pt.y = yo + round( penWidthOut * sin(theta + alpha / 2) );
2047 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
2048 if(alpha > 0) {
2049 pt.x = xo - round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2050 pt.y = yo - round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2052 else {
2053 pt.x = xo + round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2054 pt.y = yo + round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2056 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
2057 break;
2061 for(j = 0; j < pUpPath->numEntriesUsed; j++) {
2062 POINT pt;
2063 pt.x = pUpPath->pPoints[j].x;
2064 pt.y = pUpPath->pPoints[j].y;
2065 PATH_AddEntry(pNewPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO));
2067 for(j = 0; j < pDownPath->numEntriesUsed; j++) {
2068 POINT pt;
2069 pt.x = pDownPath->pPoints[pDownPath->numEntriesUsed - j - 1].x;
2070 pt.y = pDownPath->pPoints[pDownPath->numEntriesUsed - j - 1].y;
2071 PATH_AddEntry(pNewPath, &pt, ( (j == 0 && (pStrokes[i]->pFlags[pStrokes[i]->numEntriesUsed - 1] & PT_CLOSEFIGURE)) ? PT_MOVETO : PT_LINETO));
2074 PATH_DestroyGdiPath(pStrokes[i]);
2075 HeapFree(GetProcessHeap(), 0, pStrokes[i]);
2076 PATH_DestroyGdiPath(pUpPath);
2077 HeapFree(GetProcessHeap(), 0, pUpPath);
2078 PATH_DestroyGdiPath(pDownPath);
2079 HeapFree(GetProcessHeap(), 0, pDownPath);
2081 HeapFree(GetProcessHeap(), 0, pStrokes);
2083 pNewPath->state = PATH_Closed;
2084 if (!(ret = PATH_AssignGdiPath(pPath, pNewPath)))
2085 ERR("Assign path failed\n");
2086 PATH_DestroyGdiPath(pNewPath);
2087 HeapFree(GetProcessHeap(), 0, pNewPath);
2088 return ret;
2092 /*******************************************************************
2093 * StrokeAndFillPath [GDI32.@]
2097 BOOL WINAPI StrokeAndFillPath(HDC hdc)
2099 BOOL ret = FALSE;
2100 DC *dc = get_dc_ptr( hdc );
2102 if (dc)
2104 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pStrokeAndFillPath );
2105 ret = physdev->funcs->pStrokeAndFillPath( physdev );
2106 release_dc_ptr( dc );
2108 return ret;
2112 /*******************************************************************
2113 * StrokePath [GDI32.@]
2117 BOOL WINAPI StrokePath(HDC hdc)
2119 BOOL ret = FALSE;
2120 DC *dc = get_dc_ptr( hdc );
2122 if (dc)
2124 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pStrokePath );
2125 ret = physdev->funcs->pStrokePath( physdev );
2126 release_dc_ptr( dc );
2128 return ret;
2132 /*******************************************************************
2133 * WidenPath [GDI32.@]
2137 BOOL WINAPI WidenPath(HDC hdc)
2139 BOOL ret = FALSE;
2140 DC *dc = get_dc_ptr( hdc );
2142 if (dc)
2144 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pWidenPath );
2145 ret = physdev->funcs->pWidenPath( physdev );
2146 release_dc_ptr( dc );
2148 return ret;
2152 /***********************************************************************
2153 * null driver fallback implementations
2156 BOOL nulldrv_BeginPath( PHYSDEV dev )
2158 DC *dc = get_nulldrv_dc( dev );
2160 /* If path is already open, do nothing */
2161 if (dc->path.state != PATH_Open)
2163 if (!path_driver.pCreateDC( &dc->physDev, NULL, NULL, NULL, NULL )) return FALSE;
2164 PATH_EmptyPath(&dc->path);
2165 dc->path.newStroke = TRUE;
2166 dc->path.state = PATH_Open;
2168 return TRUE;
2171 BOOL nulldrv_EndPath( PHYSDEV dev )
2173 SetLastError( ERROR_CAN_NOT_COMPLETE );
2174 return FALSE;
2177 BOOL nulldrv_AbortPath( PHYSDEV dev )
2179 DC *dc = get_nulldrv_dc( dev );
2181 PATH_EmptyPath( &dc->path );
2182 return TRUE;
2185 BOOL nulldrv_CloseFigure( PHYSDEV dev )
2187 DC *dc = get_nulldrv_dc( dev );
2189 if (dc->path.state != PATH_Open)
2191 SetLastError( ERROR_CAN_NOT_COMPLETE );
2192 return FALSE;
2194 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
2195 /* It is not necessary to draw a line, PT_CLOSEFIGURE is a virtual closing line itself */
2196 if (dc->path.numEntriesUsed)
2198 dc->path.pFlags[dc->path.numEntriesUsed - 1] |= PT_CLOSEFIGURE;
2199 dc->path.newStroke = TRUE;
2201 return TRUE;
2204 BOOL nulldrv_SelectClipPath( PHYSDEV dev, INT mode )
2206 BOOL ret;
2207 HRGN hrgn;
2208 DC *dc = get_nulldrv_dc( dev );
2210 if (dc->path.state != PATH_Closed)
2212 SetLastError( ERROR_CAN_NOT_COMPLETE );
2213 return FALSE;
2215 if (!PATH_PathToRegion( &dc->path, GetPolyFillMode(dev->hdc), &hrgn )) return FALSE;
2216 ret = ExtSelectClipRgn( dev->hdc, hrgn, mode ) != ERROR;
2217 if (ret) PATH_EmptyPath( &dc->path );
2218 /* FIXME: Should this function delete the path even if it failed? */
2219 DeleteObject( hrgn );
2220 return ret;
2223 BOOL nulldrv_FillPath( PHYSDEV dev )
2225 DC *dc = get_nulldrv_dc( dev );
2227 if (dc->path.state != PATH_Closed)
2229 SetLastError( ERROR_CAN_NOT_COMPLETE );
2230 return FALSE;
2232 if (!PATH_FillPath( dc, &dc->path )) return FALSE;
2233 /* FIXME: Should the path be emptied even if conversion failed? */
2234 PATH_EmptyPath( &dc->path );
2235 return TRUE;
2238 BOOL nulldrv_StrokeAndFillPath( PHYSDEV dev )
2240 DC *dc = get_nulldrv_dc( dev );
2242 if (dc->path.state != PATH_Closed)
2244 SetLastError( ERROR_CAN_NOT_COMPLETE );
2245 return FALSE;
2247 if (!PATH_FillPath( dc, &dc->path )) return FALSE;
2248 if (!PATH_StrokePath( dc, &dc->path )) return FALSE;
2249 PATH_EmptyPath( &dc->path );
2250 return TRUE;
2253 BOOL nulldrv_StrokePath( PHYSDEV dev )
2255 DC *dc = get_nulldrv_dc( dev );
2257 if (dc->path.state != PATH_Closed)
2259 SetLastError( ERROR_CAN_NOT_COMPLETE );
2260 return FALSE;
2262 if (!PATH_StrokePath( dc, &dc->path )) return FALSE;
2263 PATH_EmptyPath( &dc->path );
2264 return TRUE;
2267 BOOL nulldrv_FlattenPath( PHYSDEV dev )
2269 DC *dc = get_nulldrv_dc( dev );
2271 if (dc->path.state != PATH_Closed)
2273 SetLastError( ERROR_CAN_NOT_COMPLETE );
2274 return FALSE;
2276 return PATH_FlattenPath( &dc->path );
2279 BOOL nulldrv_WidenPath( PHYSDEV dev )
2281 DC *dc = get_nulldrv_dc( dev );
2283 if (dc->path.state != PATH_Closed)
2285 SetLastError( ERROR_CAN_NOT_COMPLETE );
2286 return FALSE;
2288 return PATH_WidenPath( dc );
2291 const struct gdi_dc_funcs path_driver =
2293 NULL, /* pAbortDoc */
2294 pathdrv_AbortPath, /* pAbortPath */
2295 NULL, /* pAlphaBlend */
2296 NULL, /* pAngleArc */
2297 NULL, /* pArc */
2298 NULL, /* pArcTo */
2299 NULL, /* pBeginPath */
2300 NULL, /* pBlendImage */
2301 NULL, /* pChoosePixelFormat */
2302 NULL, /* pChord */
2303 NULL, /* pCloseFigure */
2304 NULL, /* pCreateBitmap */
2305 NULL, /* pCreateCompatibleDC */
2306 pathdrv_CreateDC, /* pCreateDC */
2307 NULL, /* pCreateDIBSection */
2308 NULL, /* pDeleteBitmap */
2309 pathdrv_DeleteDC, /* pDeleteDC */
2310 NULL, /* pDeleteObject */
2311 NULL, /* pDescribePixelFormat */
2312 NULL, /* pDeviceCapabilities */
2313 NULL, /* pEllipse */
2314 NULL, /* pEndDoc */
2315 NULL, /* pEndPage */
2316 pathdrv_EndPath, /* pEndPath */
2317 NULL, /* pEnumFonts */
2318 NULL, /* pEnumICMProfiles */
2319 NULL, /* pExcludeClipRect */
2320 NULL, /* pExtDeviceMode */
2321 NULL, /* pExtEscape */
2322 NULL, /* pExtFloodFill */
2323 NULL, /* pExtSelectClipRgn */
2324 NULL, /* pExtTextOut */
2325 NULL, /* pFillPath */
2326 NULL, /* pFillRgn */
2327 NULL, /* pFlattenPath */
2328 NULL, /* pFontIsLinked */
2329 NULL, /* pFrameRgn */
2330 NULL, /* pGdiComment */
2331 NULL, /* pGdiRealizationInfo */
2332 NULL, /* pGetCharABCWidths */
2333 NULL, /* pGetCharABCWidthsI */
2334 NULL, /* pGetCharWidth */
2335 NULL, /* pGetDeviceCaps */
2336 NULL, /* pGetDeviceGammaRamp */
2337 NULL, /* pGetFontData */
2338 NULL, /* pGetFontUnicodeRanges */
2339 NULL, /* pGetGlyphIndices */
2340 NULL, /* pGetGlyphOutline */
2341 NULL, /* pGetICMProfile */
2342 NULL, /* pGetImage */
2343 NULL, /* pGetKerningPairs */
2344 NULL, /* pGetNearestColor */
2345 NULL, /* pGetOutlineTextMetrics */
2346 NULL, /* pGetPixel */
2347 NULL, /* pGetPixelFormat */
2348 NULL, /* pGetSystemPaletteEntries */
2349 NULL, /* pGetTextCharsetInfo */
2350 NULL, /* pGetTextExtentExPoint */
2351 NULL, /* pGetTextExtentExPointI */
2352 NULL, /* pGetTextFace */
2353 NULL, /* pGetTextMetrics */
2354 NULL, /* pIntersectClipRect */
2355 NULL, /* pInvertRgn */
2356 pathdrv_LineTo, /* pLineTo */
2357 NULL, /* pModifyWorldTransform */
2358 pathdrv_MoveTo, /* pMoveTo */
2359 NULL, /* pOffsetClipRgn */
2360 NULL, /* pOffsetViewportOrg */
2361 NULL, /* pOffsetWindowOrg */
2362 NULL, /* pPaintRgn */
2363 NULL, /* pPatBlt */
2364 NULL, /* pPie */
2365 NULL, /* pPolyBezier */
2366 NULL, /* pPolyBezierTo */
2367 NULL, /* pPolyDraw */
2368 NULL, /* pPolyPolygon */
2369 NULL, /* pPolyPolyline */
2370 NULL, /* pPolygon */
2371 NULL, /* pPolyline */
2372 NULL, /* pPolylineTo */
2373 NULL, /* pPutImage */
2374 NULL, /* pRealizeDefaultPalette */
2375 NULL, /* pRealizePalette */
2376 NULL, /* pRectangle */
2377 NULL, /* pResetDC */
2378 NULL, /* pRestoreDC */
2379 NULL, /* pRoundRect */
2380 NULL, /* pSaveDC */
2381 NULL, /* pScaleViewportExt */
2382 NULL, /* pScaleWindowExt */
2383 NULL, /* pSelectBitmap */
2384 NULL, /* pSelectBrush */
2385 NULL, /* pSelectClipPath */
2386 NULL, /* pSelectFont */
2387 NULL, /* pSelectPalette */
2388 NULL, /* pSelectPen */
2389 NULL, /* pSetArcDirection */
2390 NULL, /* pSetBkColor */
2391 NULL, /* pSetBkMode */
2392 NULL, /* pSetDCBrushColor */
2393 NULL, /* pSetDCPenColor */
2394 NULL, /* pSetDIBColorTable */
2395 NULL, /* pSetDIBitsToDevice */
2396 NULL, /* pSetDeviceClipping */
2397 NULL, /* pSetDeviceGammaRamp */
2398 NULL, /* pSetLayout */
2399 NULL, /* pSetMapMode */
2400 NULL, /* pSetMapperFlags */
2401 NULL, /* pSetPixel */
2402 NULL, /* pSetPixelFormat */
2403 NULL, /* pSetPolyFillMode */
2404 NULL, /* pSetROP2 */
2405 NULL, /* pSetRelAbs */
2406 NULL, /* pSetStretchBltMode */
2407 NULL, /* pSetTextAlign */
2408 NULL, /* pSetTextCharacterExtra */
2409 NULL, /* pSetTextColor */
2410 NULL, /* pSetTextJustification */
2411 NULL, /* pSetViewportExt */
2412 NULL, /* pSetViewportOrg */
2413 NULL, /* pSetWindowExt */
2414 NULL, /* pSetWindowOrg */
2415 NULL, /* pSetWorldTransform */
2416 NULL, /* pStartDoc */
2417 NULL, /* pStartPage */
2418 NULL, /* pStretchBlt */
2419 NULL, /* pStretchDIBits */
2420 NULL, /* pStrokeAndFillPath */
2421 NULL, /* pStrokePath */
2422 NULL, /* pSwapBuffers */
2423 NULL, /* pUnrealizePalette */
2424 NULL, /* pWidenPath */
2425 /* OpenGL not supported */