gdi32: Only store the path in the DC when it's closed.
[wine/multimedia.git] / dlls / gdi32 / path.c
blobc9b72626b95dc4157efddb390f1611fdab2d23c7
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 */
78 /* A floating point version of the POINT structure */
79 typedef struct tagFLOAT_POINT
81 double x, y;
82 } FLOAT_POINT;
84 typedef enum
86 PATH_Null,
87 PATH_Open,
88 PATH_Closed
89 } GdiPathState;
91 typedef struct gdi_path
93 GdiPathState state;
94 POINT *pPoints;
95 BYTE *pFlags;
96 int numEntriesUsed, numEntriesAllocated;
97 BOOL newStroke;
98 } GdiPath;
100 struct path_physdev
102 struct gdi_physdev dev;
103 struct gdi_path *path;
106 static inline struct path_physdev *get_path_physdev( PHYSDEV dev )
108 return (struct path_physdev *)dev;
111 static inline void pop_path_driver( DC *dc, struct path_physdev *physdev )
113 PHYSDEV *dev = &dc->physDev;
114 while (*dev != &physdev->dev) dev = &(*dev)->next;
115 *dev = physdev->dev.next;
116 HeapFree( GetProcessHeap(), 0, physdev );
119 static inline struct path_physdev *find_path_physdev( DC *dc )
121 PHYSDEV dev;
123 for (dev = dc->physDev; dev->funcs != &null_driver; dev = dev->next)
124 if (dev->funcs == &path_driver) return get_path_physdev( dev );
125 return NULL;
128 void free_gdi_path( struct gdi_path *path )
130 HeapFree( GetProcessHeap(), 0, path->pPoints );
131 HeapFree( GetProcessHeap(), 0, path->pFlags );
132 HeapFree( GetProcessHeap(), 0, path );
135 static struct gdi_path *alloc_gdi_path(void)
137 struct gdi_path *path = HeapAlloc( GetProcessHeap(), 0, sizeof(*path) );
139 if (!path)
141 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
142 return NULL;
144 path->pPoints = HeapAlloc( GetProcessHeap(), 0, NUM_ENTRIES_INITIAL * sizeof(*path->pPoints) );
145 path->pFlags = HeapAlloc( GetProcessHeap(), 0, NUM_ENTRIES_INITIAL * sizeof(*path->pFlags) );
146 if (!path->pPoints || !path->pFlags)
148 free_gdi_path( path );
149 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
150 return NULL;
152 path->state = PATH_Open;
153 path->numEntriesUsed = 0;
154 path->numEntriesAllocated = NUM_ENTRIES_INITIAL;
155 path->newStroke = TRUE;
156 return path;
159 static struct gdi_path *copy_gdi_path( const struct gdi_path *src_path )
161 struct gdi_path *path = HeapAlloc( GetProcessHeap(), 0, sizeof(*path) );
163 if (!path)
165 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
166 return NULL;
168 path->state = src_path->state;
169 path->numEntriesUsed = path->numEntriesAllocated = src_path->numEntriesUsed;
170 path->newStroke = src_path->newStroke;
171 path->pPoints = HeapAlloc( GetProcessHeap(), 0, path->numEntriesUsed * sizeof(*path->pPoints) );
172 path->pFlags = HeapAlloc( GetProcessHeap(), 0, path->numEntriesUsed * sizeof(*path->pFlags) );
173 if (!path->pPoints || !path->pFlags)
175 free_gdi_path( path );
176 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
177 return NULL;
179 memcpy( path->pPoints, src_path->pPoints, path->numEntriesUsed * sizeof(*path->pPoints) );
180 memcpy( path->pFlags, src_path->pFlags, path->numEntriesUsed * sizeof(*path->pFlags) );
181 return path;
184 /* Performs a world-to-viewport transformation on the specified point (which
185 * is in floating point format).
187 static inline void INTERNAL_LPTODP_FLOAT( HDC hdc, FLOAT_POINT *point, int count )
189 DC *dc = get_dc_ptr( hdc );
190 double x, y;
192 while (count--)
194 x = point->x;
195 y = point->y;
196 point->x = x * dc->xformWorld2Vport.eM11 + y * dc->xformWorld2Vport.eM21 + dc->xformWorld2Vport.eDx;
197 point->y = x * dc->xformWorld2Vport.eM12 + y * dc->xformWorld2Vport.eM22 + dc->xformWorld2Vport.eDy;
198 point++;
200 release_dc_ptr( dc );
203 static inline INT int_from_fixed(FIXED f)
205 return (f.fract >= 0x8000) ? (f.value + 1) : f.value;
209 /* PATH_ReserveEntries
211 * Ensures that at least "numEntries" entries (for points and flags) have
212 * been allocated; allocates larger arrays and copies the existing entries
213 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
215 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT count)
217 POINT *pPointsNew;
218 BYTE *pFlagsNew;
220 assert(count>=0);
222 /* Do we have to allocate more memory? */
223 if(count > pPath->numEntriesAllocated)
225 /* Find number of entries to allocate. We let the size of the array
226 * grow exponentially, since that will guarantee linear time
227 * complexity. */
228 count = max( pPath->numEntriesAllocated * 2, count );
230 pPointsNew = HeapReAlloc( GetProcessHeap(), 0, pPath->pPoints, count * sizeof(POINT) );
231 if (!pPointsNew) return FALSE;
232 pPath->pPoints = pPointsNew;
234 pFlagsNew = HeapReAlloc( GetProcessHeap(), 0, pPath->pFlags, count * sizeof(BYTE) );
235 if (!pFlagsNew) return FALSE;
236 pPath->pFlags = pFlagsNew;
238 pPath->numEntriesAllocated = count;
240 return TRUE;
243 /* PATH_AddEntry
245 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
246 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
247 * successful, FALSE otherwise (e.g. if not enough memory was available).
249 static BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
251 /* FIXME: If newStroke is true, perhaps we want to check that we're
252 * getting a PT_MOVETO
254 TRACE("(%d,%d) - %d\n", pPoint->x, pPoint->y, flags);
256 /* Check that path is open */
257 if(pPath->state!=PATH_Open)
258 return FALSE;
260 /* Reserve enough memory for an extra path entry */
261 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
262 return FALSE;
264 /* Store information in path entry */
265 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
266 pPath->pFlags[pPath->numEntriesUsed]=flags;
268 pPath->numEntriesUsed++;
270 return TRUE;
273 /* add a number of points, converting them to device coords */
274 /* return a pointer to the first type byte so it can be fixed up if necessary */
275 static BYTE *add_log_points( struct path_physdev *physdev, const POINT *points, DWORD count, BYTE type )
277 BYTE *ret;
278 GdiPath *path = physdev->path;
280 if (!PATH_ReserveEntries( path, path->numEntriesUsed + count )) return NULL;
282 ret = &path->pFlags[path->numEntriesUsed];
283 memcpy( &path->pPoints[path->numEntriesUsed], points, count * sizeof(*points) );
284 LPtoDP( physdev->dev.hdc, &path->pPoints[path->numEntriesUsed], count );
285 memset( ret, type, count );
286 path->numEntriesUsed += count;
287 return ret;
290 /* start a new path stroke if necessary */
291 static BOOL start_new_stroke( struct path_physdev *physdev )
293 POINT pos;
294 GdiPath *path = physdev->path;
296 if (!path->newStroke && path->numEntriesUsed &&
297 !(path->pFlags[path->numEntriesUsed - 1] & PT_CLOSEFIGURE))
298 return TRUE;
300 path->newStroke = FALSE;
301 GetCurrentPositionEx( physdev->dev.hdc, &pos );
302 return add_log_points( physdev, &pos, 1, PT_MOVETO ) != NULL;
305 /* PATH_CheckCorners
307 * Helper function for RoundRect() and Rectangle()
309 static void PATH_CheckCorners( HDC hdc, POINT corners[], INT x1, INT y1, INT x2, INT y2 )
311 INT temp;
313 /* Convert points to device coordinates */
314 corners[0].x=x1;
315 corners[0].y=y1;
316 corners[1].x=x2;
317 corners[1].y=y2;
318 LPtoDP( hdc, corners, 2 );
320 /* Make sure first corner is top left and second corner is bottom right */
321 if(corners[0].x>corners[1].x)
323 temp=corners[0].x;
324 corners[0].x=corners[1].x;
325 corners[1].x=temp;
327 if(corners[0].y>corners[1].y)
329 temp=corners[0].y;
330 corners[0].y=corners[1].y;
331 corners[1].y=temp;
334 /* In GM_COMPATIBLE, don't include bottom and right edges */
335 if (GetGraphicsMode( hdc ) == GM_COMPATIBLE)
337 corners[1].x--;
338 corners[1].y--;
342 /* PATH_AddFlatBezier
344 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
346 POINT *pts;
347 INT no, i;
349 pts = GDI_Bezier( pt, 4, &no );
350 if(!pts) return FALSE;
352 for(i = 1; i < no; i++)
353 PATH_AddEntry(pPath, &pts[i], (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
354 HeapFree( GetProcessHeap(), 0, pts );
355 return TRUE;
358 /* PATH_FlattenPath
360 * Replaces Beziers with line segments
363 static struct gdi_path *PATH_FlattenPath(const struct gdi_path *pPath)
365 struct gdi_path *new_path;
366 INT srcpt;
368 if (!(new_path = alloc_gdi_path())) return NULL;
370 for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
371 switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
372 case PT_MOVETO:
373 case PT_LINETO:
374 if (!PATH_AddEntry(new_path, &pPath->pPoints[srcpt], pPath->pFlags[srcpt]))
376 free_gdi_path( new_path );
377 return NULL;
379 break;
380 case PT_BEZIERTO:
381 if (!PATH_AddFlatBezier(new_path, &pPath->pPoints[srcpt-1],
382 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE))
384 free_gdi_path( new_path );
385 return NULL;
387 srcpt += 2;
388 break;
391 new_path->state = PATH_Closed;
392 return new_path;
395 /* PATH_PathToRegion
397 * Creates a region from the specified path using the specified polygon
398 * filling mode. The path is left unchanged.
400 static HRGN PATH_PathToRegion(const struct gdi_path *pPath, INT nPolyFillMode)
402 struct gdi_path *rgn_path;
403 int numStrokes, iStroke, i;
404 INT *pNumPointsInStroke;
405 HRGN hrgn;
407 if (!(rgn_path = PATH_FlattenPath( pPath ))) return 0;
409 /* FIXME: What happens when number of points is zero? */
411 /* First pass: Find out how many strokes there are in the path */
412 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
413 numStrokes=0;
414 for(i=0; i<rgn_path->numEntriesUsed; i++)
415 if((rgn_path->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
416 numStrokes++;
418 /* Allocate memory for number-of-points-in-stroke array */
419 pNumPointsInStroke=HeapAlloc( GetProcessHeap(), 0, sizeof(int) * numStrokes );
420 if(!pNumPointsInStroke)
422 free_gdi_path( rgn_path );
423 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
424 return 0;
427 /* Second pass: remember number of points in each polygon */
428 iStroke=-1; /* Will get incremented to 0 at beginning of first stroke */
429 for(i=0; i<rgn_path->numEntriesUsed; i++)
431 /* Is this the beginning of a new stroke? */
432 if((rgn_path->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
434 iStroke++;
435 pNumPointsInStroke[iStroke]=0;
438 pNumPointsInStroke[iStroke]++;
441 /* Create a region from the strokes */
442 hrgn=CreatePolyPolygonRgn(rgn_path->pPoints, pNumPointsInStroke,
443 numStrokes, nPolyFillMode);
445 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
446 free_gdi_path( rgn_path );
447 return hrgn;
450 /* PATH_ScaleNormalizedPoint
452 * Scales a normalized point (x, y) with respect to the box whose corners are
453 * passed in "corners". The point is stored in "*pPoint". The normalized
454 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
455 * (1.0, 1.0) correspond to corners[1].
457 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
458 double y, POINT *pPoint)
460 pPoint->x=GDI_ROUND( (double)corners[0].x + (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
461 pPoint->y=GDI_ROUND( (double)corners[0].y + (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
464 /* PATH_NormalizePoint
466 * Normalizes a point with respect to the box whose corners are passed in
467 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
469 static void PATH_NormalizePoint(FLOAT_POINT corners[],
470 const FLOAT_POINT *pPoint,
471 double *pX, double *pY)
473 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) * 2.0 - 1.0;
474 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) * 2.0 - 1.0;
477 /* PATH_DoArcPart
479 * Creates a Bezier spline that corresponds to part of an arc and appends the
480 * corresponding points to the path. The start and end angles are passed in
481 * "angleStart" and "angleEnd"; these angles should span a quarter circle
482 * at most. If "startEntryType" is non-zero, an entry of that type for the first
483 * control point is added to the path; otherwise, it is assumed that the current
484 * position is equal to the first control point.
486 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
487 double angleStart, double angleEnd, BYTE startEntryType)
489 double halfAngle, a;
490 double xNorm[4], yNorm[4];
491 POINT point;
492 int i;
494 assert(fabs(angleEnd-angleStart)<=M_PI_2);
496 /* FIXME: Is there an easier way of computing this? */
498 /* Compute control points */
499 halfAngle=(angleEnd-angleStart)/2.0;
500 if(fabs(halfAngle)>1e-8)
502 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
503 xNorm[0]=cos(angleStart);
504 yNorm[0]=sin(angleStart);
505 xNorm[1]=xNorm[0] - a*yNorm[0];
506 yNorm[1]=yNorm[0] + a*xNorm[0];
507 xNorm[3]=cos(angleEnd);
508 yNorm[3]=sin(angleEnd);
509 xNorm[2]=xNorm[3] + a*yNorm[3];
510 yNorm[2]=yNorm[3] - a*xNorm[3];
512 else
513 for(i=0; i<4; i++)
515 xNorm[i]=cos(angleStart);
516 yNorm[i]=sin(angleStart);
519 /* Add starting point to path if desired */
520 if(startEntryType)
522 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
523 if(!PATH_AddEntry(pPath, &point, startEntryType))
524 return FALSE;
527 /* Add remaining control points */
528 for(i=1; i<4; i++)
530 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
531 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
532 return FALSE;
535 return TRUE;
539 /***********************************************************************
540 * BeginPath (GDI32.@)
542 BOOL WINAPI BeginPath(HDC hdc)
544 BOOL ret = FALSE;
545 DC *dc = get_dc_ptr( hdc );
547 if (dc)
549 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pBeginPath );
550 ret = physdev->funcs->pBeginPath( physdev );
551 release_dc_ptr( dc );
553 return ret;
557 /***********************************************************************
558 * EndPath (GDI32.@)
560 BOOL WINAPI EndPath(HDC hdc)
562 BOOL ret = FALSE;
563 DC *dc = get_dc_ptr( hdc );
565 if (dc)
567 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pEndPath );
568 ret = physdev->funcs->pEndPath( physdev );
569 release_dc_ptr( dc );
571 return ret;
575 /******************************************************************************
576 * AbortPath [GDI32.@]
577 * Closes and discards paths from device context
579 * NOTES
580 * Check that SetLastError is being called correctly
582 * PARAMS
583 * hdc [I] Handle to device context
585 * RETURNS
586 * Success: TRUE
587 * Failure: FALSE
589 BOOL WINAPI AbortPath( HDC hdc )
591 BOOL ret = FALSE;
592 DC *dc = get_dc_ptr( hdc );
594 if (dc)
596 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pAbortPath );
597 ret = physdev->funcs->pAbortPath( physdev );
598 release_dc_ptr( dc );
600 return ret;
604 /***********************************************************************
605 * CloseFigure (GDI32.@)
607 * FIXME: Check that SetLastError is being called correctly
609 BOOL WINAPI CloseFigure(HDC hdc)
611 BOOL ret = FALSE;
612 DC *dc = get_dc_ptr( hdc );
614 if (dc)
616 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pCloseFigure );
617 ret = physdev->funcs->pCloseFigure( physdev );
618 release_dc_ptr( dc );
620 return ret;
624 /***********************************************************************
625 * GetPath (GDI32.@)
627 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes, INT nSize)
629 INT ret = -1;
630 DC *dc = get_dc_ptr( hdc );
632 if(!dc) return -1;
634 if (!dc->path)
636 SetLastError(ERROR_CAN_NOT_COMPLETE);
637 goto done;
640 if(nSize==0)
641 ret = dc->path->numEntriesUsed;
642 else if(nSize<dc->path->numEntriesUsed)
644 SetLastError(ERROR_INVALID_PARAMETER);
645 goto done;
647 else
649 memcpy(pPoints, dc->path->pPoints, sizeof(POINT)*dc->path->numEntriesUsed);
650 memcpy(pTypes, dc->path->pFlags, sizeof(BYTE)*dc->path->numEntriesUsed);
652 /* Convert the points to logical coordinates */
653 if(!DPtoLP(hdc, pPoints, dc->path->numEntriesUsed))
655 /* FIXME: Is this the correct value? */
656 SetLastError(ERROR_CAN_NOT_COMPLETE);
657 goto done;
659 else ret = dc->path->numEntriesUsed;
661 done:
662 release_dc_ptr( dc );
663 return ret;
667 /***********************************************************************
668 * PathToRegion (GDI32.@)
670 * FIXME
671 * Check that SetLastError is being called correctly
673 * The documentation does not state this explicitly, but a test under Windows
674 * shows that the region which is returned should be in device coordinates.
676 HRGN WINAPI PathToRegion(HDC hdc)
678 HRGN hrgnRval = 0;
679 DC *dc = get_dc_ptr( hdc );
681 /* Get pointer to path */
682 if(!dc) return 0;
684 if (!dc->path) SetLastError(ERROR_CAN_NOT_COMPLETE);
685 else
687 if ((hrgnRval = PATH_PathToRegion(dc->path, GetPolyFillMode(hdc))))
689 /* FIXME: Should we empty the path even if conversion failed? */
690 free_gdi_path( dc->path );
691 dc->path = NULL;
694 release_dc_ptr( dc );
695 return hrgnRval;
698 static BOOL PATH_FillPath( HDC hdc, GdiPath *pPath )
700 INT mapMode, graphicsMode;
701 SIZE ptViewportExt, ptWindowExt;
702 POINT ptViewportOrg, ptWindowOrg;
703 XFORM xform;
704 HRGN hrgn;
706 /* Construct a region from the path and fill it */
707 if ((hrgn = PATH_PathToRegion(pPath, GetPolyFillMode(hdc))))
709 /* Since PaintRgn interprets the region as being in logical coordinates
710 * but the points we store for the path are already in device
711 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
712 * Using SaveDC to save information about the mapping mode / world
713 * transform would be easier but would require more overhead, especially
714 * now that SaveDC saves the current path.
717 /* Save the information about the old mapping mode */
718 mapMode=GetMapMode(hdc);
719 GetViewportExtEx(hdc, &ptViewportExt);
720 GetViewportOrgEx(hdc, &ptViewportOrg);
721 GetWindowExtEx(hdc, &ptWindowExt);
722 GetWindowOrgEx(hdc, &ptWindowOrg);
724 /* Save world transform
725 * NB: The Windows documentation on world transforms would lead one to
726 * believe that this has to be done only in GM_ADVANCED; however, my
727 * tests show that resetting the graphics mode to GM_COMPATIBLE does
728 * not reset the world transform.
730 GetWorldTransform(hdc, &xform);
732 /* Set MM_TEXT */
733 SetMapMode(hdc, MM_TEXT);
734 SetViewportOrgEx(hdc, 0, 0, NULL);
735 SetWindowOrgEx(hdc, 0, 0, NULL);
736 graphicsMode=GetGraphicsMode(hdc);
737 SetGraphicsMode(hdc, GM_ADVANCED);
738 ModifyWorldTransform(hdc, &xform, MWT_IDENTITY);
739 SetGraphicsMode(hdc, graphicsMode);
741 /* Paint the region */
742 PaintRgn(hdc, hrgn);
743 DeleteObject(hrgn);
744 /* Restore the old mapping mode */
745 SetMapMode(hdc, mapMode);
746 SetViewportExtEx(hdc, ptViewportExt.cx, ptViewportExt.cy, NULL);
747 SetViewportOrgEx(hdc, ptViewportOrg.x, ptViewportOrg.y, NULL);
748 SetWindowExtEx(hdc, ptWindowExt.cx, ptWindowExt.cy, NULL);
749 SetWindowOrgEx(hdc, ptWindowOrg.x, ptWindowOrg.y, NULL);
751 /* Go to GM_ADVANCED temporarily to restore the world transform */
752 graphicsMode=GetGraphicsMode(hdc);
753 SetGraphicsMode(hdc, GM_ADVANCED);
754 SetWorldTransform(hdc, &xform);
755 SetGraphicsMode(hdc, graphicsMode);
756 return TRUE;
758 return FALSE;
762 /***********************************************************************
763 * FillPath (GDI32.@)
765 * FIXME
766 * Check that SetLastError is being called correctly
768 BOOL WINAPI FillPath(HDC hdc)
770 BOOL ret = FALSE;
771 DC *dc = get_dc_ptr( hdc );
773 if (dc)
775 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pFillPath );
776 ret = physdev->funcs->pFillPath( physdev );
777 release_dc_ptr( dc );
779 return ret;
783 /***********************************************************************
784 * SelectClipPath (GDI32.@)
785 * FIXME
786 * Check that SetLastError is being called correctly
788 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
790 BOOL ret = FALSE;
791 DC *dc = get_dc_ptr( hdc );
793 if (dc)
795 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectClipPath );
796 ret = physdev->funcs->pSelectClipPath( physdev, iMode );
797 release_dc_ptr( dc );
799 return ret;
803 /***********************************************************************
804 * pathdrv_BeginPath
806 static BOOL pathdrv_BeginPath( PHYSDEV dev )
808 /* path already open, nothing to do */
809 return TRUE;
813 /***********************************************************************
814 * pathdrv_AbortPath
816 static BOOL pathdrv_AbortPath( PHYSDEV dev )
818 struct path_physdev *physdev = get_path_physdev( dev );
819 DC *dc = get_dc_ptr( dev->hdc );
821 if (!dc) return FALSE;
822 free_gdi_path( physdev->path );
823 pop_path_driver( dc, physdev );
824 release_dc_ptr( dc );
825 return TRUE;
829 /***********************************************************************
830 * pathdrv_EndPath
832 static BOOL pathdrv_EndPath( PHYSDEV dev )
834 struct path_physdev *physdev = get_path_physdev( dev );
835 DC *dc = get_dc_ptr( dev->hdc );
837 if (!dc) return FALSE;
838 dc->path = physdev->path;
839 dc->path->state = PATH_Closed;
840 pop_path_driver( dc, physdev );
841 release_dc_ptr( dc );
842 return TRUE;
846 /***********************************************************************
847 * pathdrv_CreateDC
849 static BOOL pathdrv_CreateDC( PHYSDEV *dev, LPCWSTR driver, LPCWSTR device,
850 LPCWSTR output, const DEVMODEW *devmode )
852 struct path_physdev *physdev = HeapAlloc( GetProcessHeap(), 0, sizeof(*physdev) );
853 DC *dc;
855 if (!physdev) return FALSE;
856 dc = get_dc_ptr( (*dev)->hdc );
857 push_dc_driver( dev, &physdev->dev, &path_driver );
858 release_dc_ptr( dc );
859 return TRUE;
863 /*************************************************************
864 * pathdrv_DeleteDC
866 static BOOL pathdrv_DeleteDC( PHYSDEV dev )
868 assert( 0 ); /* should never be called */
869 return TRUE;
873 BOOL PATH_SavePath( DC *dst, DC *src )
875 struct path_physdev *physdev;
877 if (src->path)
879 if (!(dst->path = copy_gdi_path( src->path ))) return FALSE;
881 else if ((physdev = find_path_physdev( src )))
883 if (!(dst->path = copy_gdi_path( physdev->path ))) return FALSE;
884 dst->flags |= DC_PATH_OPEN;
886 else dst->path = NULL;
887 return TRUE;
890 BOOL PATH_RestorePath( DC *dst, DC *src )
892 struct path_physdev *physdev = find_path_physdev( dst );
894 if (src->path && (src->flags & DC_PATH_OPEN))
896 if (!physdev)
898 if (!path_driver.pCreateDC( &dst->physDev, NULL, NULL, NULL, NULL )) return FALSE;
899 physdev = get_path_physdev( dst->physDev );
901 else free_gdi_path( physdev->path );
903 physdev->path = src->path;
904 src->flags &= ~DC_PATH_OPEN;
905 src->path = NULL;
907 else if (physdev)
909 free_gdi_path( physdev->path );
910 pop_path_driver( dst, physdev );
912 if (dst->path) free_gdi_path( dst->path );
913 dst->path = src->path;
914 src->path = NULL;
915 return TRUE;
919 /*************************************************************
920 * pathdrv_MoveTo
922 static BOOL pathdrv_MoveTo( PHYSDEV dev, INT x, INT y )
924 struct path_physdev *physdev = get_path_physdev( dev );
925 physdev->path->newStroke = TRUE;
926 return TRUE;
930 /*************************************************************
931 * pathdrv_LineTo
933 static BOOL pathdrv_LineTo( PHYSDEV dev, INT x, INT y )
935 struct path_physdev *physdev = get_path_physdev( dev );
936 POINT point;
938 if (!start_new_stroke( physdev )) return FALSE;
939 point.x = x;
940 point.y = y;
941 return add_log_points( physdev, &point, 1, PT_LINETO ) != NULL;
945 /*************************************************************
946 * pathdrv_RoundRect
948 * FIXME: it adds the same entries to the path as windows does, but there
949 * is an error in the bezier drawing code so that there are small pixel-size
950 * gaps when the resulting path is drawn by StrokePath()
952 static BOOL pathdrv_RoundRect( PHYSDEV dev, INT x1, INT y1, INT x2, INT y2, INT ell_width, INT ell_height )
954 struct path_physdev *physdev = get_path_physdev( dev );
955 POINT corners[2], pointTemp;
956 FLOAT_POINT ellCorners[2];
958 PATH_CheckCorners(dev->hdc,corners,x1,y1,x2,y2);
960 /* Add points to the roundrect path */
961 ellCorners[0].x = corners[1].x-ell_width;
962 ellCorners[0].y = corners[0].y;
963 ellCorners[1].x = corners[1].x;
964 ellCorners[1].y = corners[0].y+ell_height;
965 if(!PATH_DoArcPart(physdev->path, ellCorners, 0, -M_PI_2, PT_MOVETO))
966 return FALSE;
967 pointTemp.x = corners[0].x+ell_width/2;
968 pointTemp.y = corners[0].y;
969 if(!PATH_AddEntry(physdev->path, &pointTemp, PT_LINETO))
970 return FALSE;
971 ellCorners[0].x = corners[0].x;
972 ellCorners[1].x = corners[0].x+ell_width;
973 if(!PATH_DoArcPart(physdev->path, ellCorners, -M_PI_2, -M_PI, FALSE))
974 return FALSE;
975 pointTemp.x = corners[0].x;
976 pointTemp.y = corners[1].y-ell_height/2;
977 if(!PATH_AddEntry(physdev->path, &pointTemp, PT_LINETO))
978 return FALSE;
979 ellCorners[0].y = corners[1].y-ell_height;
980 ellCorners[1].y = corners[1].y;
981 if(!PATH_DoArcPart(physdev->path, ellCorners, M_PI, M_PI_2, FALSE))
982 return FALSE;
983 pointTemp.x = corners[1].x-ell_width/2;
984 pointTemp.y = corners[1].y;
985 if(!PATH_AddEntry(physdev->path, &pointTemp, PT_LINETO))
986 return FALSE;
987 ellCorners[0].x = corners[1].x-ell_width;
988 ellCorners[1].x = corners[1].x;
989 if(!PATH_DoArcPart(physdev->path, ellCorners, M_PI_2, 0, FALSE))
990 return FALSE;
992 /* Close the roundrect figure */
993 return CloseFigure( dev->hdc );
997 /*************************************************************
998 * pathdrv_Rectangle
1000 static BOOL pathdrv_Rectangle( PHYSDEV dev, INT x1, INT y1, INT x2, INT y2 )
1002 struct path_physdev *physdev = get_path_physdev( dev );
1003 POINT corners[2], pointTemp;
1005 PATH_CheckCorners(dev->hdc,corners,x1,y1,x2,y2);
1007 /* Add four points to the path */
1008 pointTemp.x=corners[1].x;
1009 pointTemp.y=corners[0].y;
1010 if(!PATH_AddEntry(physdev->path, &pointTemp, PT_MOVETO))
1011 return FALSE;
1012 if(!PATH_AddEntry(physdev->path, corners, PT_LINETO))
1013 return FALSE;
1014 pointTemp.x=corners[0].x;
1015 pointTemp.y=corners[1].y;
1016 if(!PATH_AddEntry(physdev->path, &pointTemp, PT_LINETO))
1017 return FALSE;
1018 if(!PATH_AddEntry(physdev->path, corners+1, PT_LINETO))
1019 return FALSE;
1021 /* Close the rectangle figure */
1022 return CloseFigure( dev->hdc );
1026 /* PATH_Arc
1028 * Should be called when a call to Arc is performed on a DC that has
1029 * an open path. This adds up to five Bezier splines representing the arc
1030 * to the path. When 'lines' is 1, we add 1 extra line to get a chord,
1031 * when 'lines' is 2, we add 2 extra lines to get a pie, and when 'lines' is
1032 * -1 we add 1 extra line from the current DC position to the starting position
1033 * of the arc before drawing the arc itself (arcto). Returns TRUE if successful,
1034 * else FALSE.
1036 static BOOL PATH_Arc( PHYSDEV dev, INT x1, INT y1, INT x2, INT y2,
1037 INT xStart, INT yStart, INT xEnd, INT yEnd, INT lines )
1039 struct path_physdev *physdev = get_path_physdev( dev );
1040 double angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
1041 /* Initialize angleEndQuadrant to silence gcc's warning */
1042 double x, y;
1043 FLOAT_POINT corners[2], pointStart, pointEnd;
1044 POINT centre;
1045 BOOL start, end;
1046 INT temp, direction = GetArcDirection(dev->hdc);
1048 /* FIXME: Do we have to respect newStroke? */
1050 /* Check for zero height / width */
1051 /* FIXME: Only in GM_COMPATIBLE? */
1052 if(x1==x2 || y1==y2)
1053 return TRUE;
1055 /* Convert points to device coordinates */
1056 corners[0].x = x1;
1057 corners[0].y = y1;
1058 corners[1].x = x2;
1059 corners[1].y = y2;
1060 pointStart.x = xStart;
1061 pointStart.y = yStart;
1062 pointEnd.x = xEnd;
1063 pointEnd.y = yEnd;
1064 INTERNAL_LPTODP_FLOAT(dev->hdc, corners, 2);
1065 INTERNAL_LPTODP_FLOAT(dev->hdc, &pointStart, 1);
1066 INTERNAL_LPTODP_FLOAT(dev->hdc, &pointEnd, 1);
1068 /* Make sure first corner is top left and second corner is bottom right */
1069 if(corners[0].x>corners[1].x)
1071 temp=corners[0].x;
1072 corners[0].x=corners[1].x;
1073 corners[1].x=temp;
1075 if(corners[0].y>corners[1].y)
1077 temp=corners[0].y;
1078 corners[0].y=corners[1].y;
1079 corners[1].y=temp;
1082 /* Compute start and end angle */
1083 PATH_NormalizePoint(corners, &pointStart, &x, &y);
1084 angleStart=atan2(y, x);
1085 PATH_NormalizePoint(corners, &pointEnd, &x, &y);
1086 angleEnd=atan2(y, x);
1088 /* Make sure the end angle is "on the right side" of the start angle */
1089 if (direction == AD_CLOCKWISE)
1091 if(angleEnd<=angleStart)
1093 angleEnd+=2*M_PI;
1094 assert(angleEnd>=angleStart);
1097 else
1099 if(angleEnd>=angleStart)
1101 angleEnd-=2*M_PI;
1102 assert(angleEnd<=angleStart);
1106 /* In GM_COMPATIBLE, don't include bottom and right edges */
1107 if (GetGraphicsMode(dev->hdc) == GM_COMPATIBLE)
1109 corners[1].x--;
1110 corners[1].y--;
1113 /* arcto: Add a PT_MOVETO only if this is the first entry in a stroke */
1114 if (lines==-1 && !start_new_stroke( physdev )) return FALSE;
1116 /* Add the arc to the path with one Bezier spline per quadrant that the
1117 * arc spans */
1118 start=TRUE;
1119 end=FALSE;
1122 /* Determine the start and end angles for this quadrant */
1123 if(start)
1125 angleStartQuadrant=angleStart;
1126 if (direction == AD_CLOCKWISE)
1127 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
1128 else
1129 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
1131 else
1133 angleStartQuadrant=angleEndQuadrant;
1134 if (direction == AD_CLOCKWISE)
1135 angleEndQuadrant+=M_PI_2;
1136 else
1137 angleEndQuadrant-=M_PI_2;
1140 /* Have we reached the last part of the arc? */
1141 if((direction == AD_CLOCKWISE && angleEnd<angleEndQuadrant) ||
1142 (direction == AD_COUNTERCLOCKWISE && angleEnd>angleEndQuadrant))
1144 /* Adjust the end angle for this quadrant */
1145 angleEndQuadrant=angleEnd;
1146 end=TRUE;
1149 /* Add the Bezier spline to the path */
1150 PATH_DoArcPart(physdev->path, corners, angleStartQuadrant, angleEndQuadrant,
1151 start ? (lines==-1 ? PT_LINETO : PT_MOVETO) : FALSE);
1152 start=FALSE;
1153 } while(!end);
1155 /* chord: close figure. pie: add line and close figure */
1156 if(lines==1)
1158 return CloseFigure(dev->hdc);
1160 else if(lines==2)
1162 centre.x = (corners[0].x+corners[1].x)/2;
1163 centre.y = (corners[0].y+corners[1].y)/2;
1164 if(!PATH_AddEntry(physdev->path, &centre, PT_LINETO | PT_CLOSEFIGURE))
1165 return FALSE;
1168 return TRUE;
1172 /*************************************************************
1173 * pathdrv_AngleArc
1175 static BOOL pathdrv_AngleArc( PHYSDEV dev, INT x, INT y, DWORD radius, FLOAT eStartAngle, FLOAT eSweepAngle)
1177 INT x1, y1, x2, y2, arcdir;
1178 BOOL ret;
1180 x1 = GDI_ROUND( x + cos(eStartAngle*M_PI/180) * radius );
1181 y1 = GDI_ROUND( y - sin(eStartAngle*M_PI/180) * radius );
1182 x2 = GDI_ROUND( x + cos((eStartAngle+eSweepAngle)*M_PI/180) * radius );
1183 y2 = GDI_ROUND( y - sin((eStartAngle+eSweepAngle)*M_PI/180) * radius );
1184 arcdir = SetArcDirection( dev->hdc, eSweepAngle >= 0 ? AD_COUNTERCLOCKWISE : AD_CLOCKWISE);
1185 ret = PATH_Arc( dev, x-radius, y-radius, x+radius, y+radius, x1, y1, x2, y2, -1 );
1186 SetArcDirection( dev->hdc, arcdir );
1187 return ret;
1191 /*************************************************************
1192 * pathdrv_Arc
1194 static BOOL pathdrv_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
1195 INT xstart, INT ystart, INT xend, INT yend )
1197 return PATH_Arc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
1201 /*************************************************************
1202 * pathdrv_ArcTo
1204 static BOOL pathdrv_ArcTo( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
1205 INT xstart, INT ystart, INT xend, INT yend )
1207 return PATH_Arc( dev, left, top, right, bottom, xstart, ystart, xend, yend, -1 );
1211 /*************************************************************
1212 * pathdrv_Chord
1214 static BOOL pathdrv_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
1215 INT xstart, INT ystart, INT xend, INT yend )
1217 return PATH_Arc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 1);
1221 /*************************************************************
1222 * pathdrv_Pie
1224 static BOOL pathdrv_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
1225 INT xstart, INT ystart, INT xend, INT yend )
1227 return PATH_Arc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
1231 /*************************************************************
1232 * pathdrv_Ellipse
1234 static BOOL pathdrv_Ellipse( PHYSDEV dev, INT x1, INT y1, INT x2, INT y2 )
1236 return PATH_Arc( dev, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2, 0 ) && CloseFigure( dev->hdc );
1240 /*************************************************************
1241 * pathdrv_PolyBezierTo
1243 static BOOL pathdrv_PolyBezierTo( PHYSDEV dev, const POINT *pts, DWORD cbPoints )
1245 struct path_physdev *physdev = get_path_physdev( dev );
1247 if (!start_new_stroke( physdev )) return FALSE;
1248 return add_log_points( physdev, pts, cbPoints, PT_BEZIERTO ) != NULL;
1252 /*************************************************************
1253 * pathdrv_PolyBezier
1255 static BOOL pathdrv_PolyBezier( PHYSDEV dev, const POINT *pts, DWORD cbPoints )
1257 struct path_physdev *physdev = get_path_physdev( dev );
1258 BYTE *type = add_log_points( physdev, pts, cbPoints, PT_BEZIERTO );
1260 if (!type) return FALSE;
1261 type[0] = PT_MOVETO;
1262 return TRUE;
1266 /*************************************************************
1267 * pathdrv_PolyDraw
1269 static BOOL pathdrv_PolyDraw( PHYSDEV dev, const POINT *pts, const BYTE *types, DWORD cbPoints )
1271 struct path_physdev *physdev = get_path_physdev( dev );
1272 POINT lastmove, orig_pos;
1273 INT i;
1275 GetCurrentPositionEx( dev->hdc, &orig_pos );
1276 lastmove = orig_pos;
1278 for(i = physdev->path->numEntriesUsed - 1; i >= 0; i--){
1279 if(physdev->path->pFlags[i] == PT_MOVETO){
1280 lastmove = physdev->path->pPoints[i];
1281 DPtoLP(dev->hdc, &lastmove, 1);
1282 break;
1286 for(i = 0; i < cbPoints; i++)
1288 switch (types[i])
1290 case PT_MOVETO:
1291 MoveToEx( dev->hdc, pts[i].x, pts[i].y, NULL );
1292 break;
1293 case PT_LINETO:
1294 case PT_LINETO | PT_CLOSEFIGURE:
1295 LineTo( dev->hdc, pts[i].x, pts[i].y );
1296 break;
1297 case PT_BEZIERTO:
1298 if ((i + 2 < cbPoints) && (types[i + 1] == PT_BEZIERTO) &&
1299 (types[i + 2] & ~PT_CLOSEFIGURE) == PT_BEZIERTO)
1301 PolyBezierTo( dev->hdc, &pts[i], 3 );
1302 i += 2;
1303 break;
1305 /* fall through */
1306 default:
1307 if (i) /* restore original position */
1309 if (!(types[i - 1] & PT_CLOSEFIGURE)) lastmove = pts[i - 1];
1310 if (lastmove.x != orig_pos.x || lastmove.y != orig_pos.y)
1311 MoveToEx( dev->hdc, orig_pos.x, orig_pos.y, NULL );
1313 return FALSE;
1316 if(types[i] & PT_CLOSEFIGURE){
1317 physdev->path->pFlags[physdev->path->numEntriesUsed-1] |= PT_CLOSEFIGURE;
1318 MoveToEx( dev->hdc, lastmove.x, lastmove.y, NULL );
1322 return TRUE;
1326 /*************************************************************
1327 * pathdrv_Polyline
1329 static BOOL pathdrv_Polyline( PHYSDEV dev, const POINT *pts, INT cbPoints )
1331 struct path_physdev *physdev = get_path_physdev( dev );
1332 BYTE *type = add_log_points( physdev, pts, cbPoints, PT_LINETO );
1334 if (!type) return FALSE;
1335 if (cbPoints) type[0] = PT_MOVETO;
1336 return TRUE;
1340 /*************************************************************
1341 * pathdrv_PolylineTo
1343 static BOOL pathdrv_PolylineTo( PHYSDEV dev, const POINT *pts, INT cbPoints )
1345 struct path_physdev *physdev = get_path_physdev( dev );
1347 if (!start_new_stroke( physdev )) return FALSE;
1348 return add_log_points( physdev, pts, cbPoints, PT_LINETO ) != NULL;
1352 /*************************************************************
1353 * pathdrv_Polygon
1355 static BOOL pathdrv_Polygon( PHYSDEV dev, const POINT *pts, INT cbPoints )
1357 struct path_physdev *physdev = get_path_physdev( dev );
1358 BYTE *type = add_log_points( physdev, pts, cbPoints, PT_LINETO );
1360 if (!type) return FALSE;
1361 if (cbPoints) type[0] = PT_MOVETO;
1362 if (cbPoints > 1) type[cbPoints - 1] = PT_LINETO | PT_CLOSEFIGURE;
1363 return TRUE;
1367 /*************************************************************
1368 * pathdrv_PolyPolygon
1370 static BOOL pathdrv_PolyPolygon( PHYSDEV dev, const POINT* pts, const INT* counts, UINT polygons )
1372 struct path_physdev *physdev = get_path_physdev( dev );
1373 UINT poly;
1374 BYTE *type;
1376 for(poly = 0; poly < polygons; poly++) {
1377 type = add_log_points( physdev, pts, counts[poly], PT_LINETO );
1378 if (!type) return FALSE;
1379 type[0] = PT_MOVETO;
1380 /* win98 adds an extra line to close the figure for some reason */
1381 add_log_points( physdev, pts, 1, PT_LINETO | PT_CLOSEFIGURE );
1382 pts += counts[poly];
1384 return TRUE;
1388 /*************************************************************
1389 * pathdrv_PolyPolyline
1391 static BOOL pathdrv_PolyPolyline( PHYSDEV dev, const POINT* pts, const DWORD* counts, DWORD polylines )
1393 struct path_physdev *physdev = get_path_physdev( dev );
1394 UINT poly, count;
1395 BYTE *type;
1397 for (poly = count = 0; poly < polylines; poly++) count += counts[poly];
1399 type = add_log_points( physdev, pts, count, PT_LINETO );
1400 if (!type) return FALSE;
1402 /* make the first point of each polyline a PT_MOVETO */
1403 for (poly = 0; poly < polylines; poly++, type += counts[poly]) *type = PT_MOVETO;
1404 return TRUE;
1408 /**********************************************************************
1409 * PATH_BezierTo
1411 * internally used by PATH_add_outline
1413 static void PATH_BezierTo(GdiPath *pPath, POINT *lppt, INT n)
1415 if (n < 2) return;
1417 if (n == 2)
1419 PATH_AddEntry(pPath, &lppt[1], PT_LINETO);
1421 else if (n == 3)
1423 PATH_AddEntry(pPath, &lppt[0], PT_BEZIERTO);
1424 PATH_AddEntry(pPath, &lppt[1], PT_BEZIERTO);
1425 PATH_AddEntry(pPath, &lppt[2], PT_BEZIERTO);
1427 else
1429 POINT pt[3];
1430 INT i = 0;
1432 pt[2] = lppt[0];
1433 n--;
1435 while (n > 2)
1437 pt[0] = pt[2];
1438 pt[1] = lppt[i+1];
1439 pt[2].x = (lppt[i+2].x + lppt[i+1].x) / 2;
1440 pt[2].y = (lppt[i+2].y + lppt[i+1].y) / 2;
1441 PATH_BezierTo(pPath, pt, 3);
1442 n--;
1443 i++;
1446 pt[0] = pt[2];
1447 pt[1] = lppt[i+1];
1448 pt[2] = lppt[i+2];
1449 PATH_BezierTo(pPath, pt, 3);
1453 static BOOL PATH_add_outline(struct path_physdev *physdev, INT x, INT y,
1454 TTPOLYGONHEADER *header, DWORD size)
1456 TTPOLYGONHEADER *start;
1457 POINT pt;
1459 start = header;
1461 while ((char *)header < (char *)start + size)
1463 TTPOLYCURVE *curve;
1465 if (header->dwType != TT_POLYGON_TYPE)
1467 FIXME("Unknown header type %d\n", header->dwType);
1468 return FALSE;
1471 pt.x = x + int_from_fixed(header->pfxStart.x);
1472 pt.y = y - int_from_fixed(header->pfxStart.y);
1473 PATH_AddEntry(physdev->path, &pt, PT_MOVETO);
1475 curve = (TTPOLYCURVE *)(header + 1);
1477 while ((char *)curve < (char *)header + header->cb)
1479 /*TRACE("curve->wType %d\n", curve->wType);*/
1481 switch(curve->wType)
1483 case TT_PRIM_LINE:
1485 WORD i;
1487 for (i = 0; i < curve->cpfx; i++)
1489 pt.x = x + int_from_fixed(curve->apfx[i].x);
1490 pt.y = y - int_from_fixed(curve->apfx[i].y);
1491 PATH_AddEntry(physdev->path, &pt, PT_LINETO);
1493 break;
1496 case TT_PRIM_QSPLINE:
1497 case TT_PRIM_CSPLINE:
1499 WORD i;
1500 POINTFX ptfx;
1501 POINT *pts = HeapAlloc(GetProcessHeap(), 0, (curve->cpfx + 1) * sizeof(POINT));
1503 if (!pts) return FALSE;
1505 ptfx = *(POINTFX *)((char *)curve - sizeof(POINTFX));
1507 pts[0].x = x + int_from_fixed(ptfx.x);
1508 pts[0].y = y - int_from_fixed(ptfx.y);
1510 for(i = 0; i < curve->cpfx; i++)
1512 pts[i + 1].x = x + int_from_fixed(curve->apfx[i].x);
1513 pts[i + 1].y = y - int_from_fixed(curve->apfx[i].y);
1516 PATH_BezierTo(physdev->path, pts, curve->cpfx + 1);
1518 HeapFree(GetProcessHeap(), 0, pts);
1519 break;
1522 default:
1523 FIXME("Unknown curve type %04x\n", curve->wType);
1524 return FALSE;
1527 curve = (TTPOLYCURVE *)&curve->apfx[curve->cpfx];
1530 header = (TTPOLYGONHEADER *)((char *)header + header->cb);
1533 return CloseFigure(physdev->dev.hdc);
1536 /*************************************************************
1537 * pathdrv_ExtTextOut
1539 static BOOL pathdrv_ExtTextOut( PHYSDEV dev, INT x, INT y, UINT flags, const RECT *lprc,
1540 LPCWSTR str, UINT count, const INT *dx )
1542 struct path_physdev *physdev = get_path_physdev( dev );
1543 unsigned int idx;
1544 POINT offset = {0, 0};
1546 if (!count) return TRUE;
1548 for (idx = 0; idx < count; idx++)
1550 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
1551 GLYPHMETRICS gm;
1552 DWORD dwSize;
1553 void *outline;
1555 dwSize = GetGlyphOutlineW(dev->hdc, str[idx], GGO_GLYPH_INDEX | GGO_NATIVE,
1556 &gm, 0, NULL, &identity);
1557 if (dwSize == GDI_ERROR) return FALSE;
1559 /* add outline only if char is printable */
1560 if(dwSize)
1562 outline = HeapAlloc(GetProcessHeap(), 0, dwSize);
1563 if (!outline) return FALSE;
1565 GetGlyphOutlineW(dev->hdc, str[idx], GGO_GLYPH_INDEX | GGO_NATIVE,
1566 &gm, dwSize, outline, &identity);
1568 PATH_add_outline(physdev, x + offset.x, y + offset.y, outline, dwSize);
1570 HeapFree(GetProcessHeap(), 0, outline);
1573 if (dx)
1575 if(flags & ETO_PDY)
1577 offset.x += dx[idx * 2];
1578 offset.y += dx[idx * 2 + 1];
1580 else
1581 offset.x += dx[idx];
1583 else
1585 offset.x += gm.gmCellIncX;
1586 offset.y += gm.gmCellIncY;
1589 return TRUE;
1593 /*************************************************************
1594 * pathdrv_CloseFigure
1596 static BOOL pathdrv_CloseFigure( PHYSDEV dev )
1598 struct path_physdev *physdev = get_path_physdev( dev );
1600 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
1601 /* It is not necessary to draw a line, PT_CLOSEFIGURE is a virtual closing line itself */
1602 if (physdev->path->numEntriesUsed)
1603 physdev->path->pFlags[physdev->path->numEntriesUsed - 1] |= PT_CLOSEFIGURE;
1604 return TRUE;
1608 /*******************************************************************
1609 * FlattenPath [GDI32.@]
1613 BOOL WINAPI FlattenPath(HDC hdc)
1615 BOOL ret = FALSE;
1616 DC *dc = get_dc_ptr( hdc );
1618 if (dc)
1620 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pFlattenPath );
1621 ret = physdev->funcs->pFlattenPath( physdev );
1622 release_dc_ptr( dc );
1624 return ret;
1628 static BOOL PATH_StrokePath( HDC hdc, GdiPath *pPath )
1630 INT i, nLinePts, nAlloc;
1631 POINT *pLinePts;
1632 POINT ptViewportOrg, ptWindowOrg;
1633 SIZE szViewportExt, szWindowExt;
1634 DWORD mapMode, graphicsMode;
1635 XFORM xform;
1636 BOOL ret = TRUE;
1638 /* Save the mapping mode info */
1639 mapMode=GetMapMode(hdc);
1640 GetViewportExtEx(hdc, &szViewportExt);
1641 GetViewportOrgEx(hdc, &ptViewportOrg);
1642 GetWindowExtEx(hdc, &szWindowExt);
1643 GetWindowOrgEx(hdc, &ptWindowOrg);
1644 GetWorldTransform(hdc, &xform);
1646 /* Set MM_TEXT */
1647 SetMapMode(hdc, MM_TEXT);
1648 SetViewportOrgEx(hdc, 0, 0, NULL);
1649 SetWindowOrgEx(hdc, 0, 0, NULL);
1650 graphicsMode=GetGraphicsMode(hdc);
1651 SetGraphicsMode(hdc, GM_ADVANCED);
1652 ModifyWorldTransform(hdc, &xform, MWT_IDENTITY);
1653 SetGraphicsMode(hdc, graphicsMode);
1655 /* Allocate enough memory for the worst case without beziers (one PT_MOVETO
1656 * and the rest PT_LINETO with PT_CLOSEFIGURE at the end) plus some buffer
1657 * space in case we get one to keep the number of reallocations small. */
1658 nAlloc = pPath->numEntriesUsed + 1 + 300;
1659 pLinePts = HeapAlloc(GetProcessHeap(), 0, nAlloc * sizeof(POINT));
1660 nLinePts = 0;
1662 for(i = 0; i < pPath->numEntriesUsed; i++) {
1663 if((i == 0 || (pPath->pFlags[i-1] & PT_CLOSEFIGURE)) &&
1664 (pPath->pFlags[i] != PT_MOVETO)) {
1665 ERR("Expected PT_MOVETO %s, got path flag %d\n",
1666 i == 0 ? "as first point" : "after PT_CLOSEFIGURE",
1667 (INT)pPath->pFlags[i]);
1668 ret = FALSE;
1669 goto end;
1671 switch(pPath->pFlags[i]) {
1672 case PT_MOVETO:
1673 TRACE("Got PT_MOVETO (%d, %d)\n",
1674 pPath->pPoints[i].x, pPath->pPoints[i].y);
1675 if(nLinePts >= 2)
1676 Polyline(hdc, pLinePts, nLinePts);
1677 nLinePts = 0;
1678 pLinePts[nLinePts++] = pPath->pPoints[i];
1679 break;
1680 case PT_LINETO:
1681 case (PT_LINETO | PT_CLOSEFIGURE):
1682 TRACE("Got PT_LINETO (%d, %d)\n",
1683 pPath->pPoints[i].x, pPath->pPoints[i].y);
1684 pLinePts[nLinePts++] = pPath->pPoints[i];
1685 break;
1686 case PT_BEZIERTO:
1687 TRACE("Got PT_BEZIERTO\n");
1688 if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1689 (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1690 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1691 ret = FALSE;
1692 goto end;
1693 } else {
1694 INT nBzrPts, nMinAlloc;
1695 POINT *pBzrPts = GDI_Bezier(&pPath->pPoints[i-1], 4, &nBzrPts);
1696 /* Make sure we have allocated enough memory for the lines of
1697 * this bezier and the rest of the path, assuming we won't get
1698 * another one (since we won't reallocate again then). */
1699 nMinAlloc = nLinePts + (pPath->numEntriesUsed - i) + nBzrPts;
1700 if(nAlloc < nMinAlloc)
1702 nAlloc = nMinAlloc * 2;
1703 pLinePts = HeapReAlloc(GetProcessHeap(), 0, pLinePts,
1704 nAlloc * sizeof(POINT));
1706 memcpy(&pLinePts[nLinePts], &pBzrPts[1],
1707 (nBzrPts - 1) * sizeof(POINT));
1708 nLinePts += nBzrPts - 1;
1709 HeapFree(GetProcessHeap(), 0, pBzrPts);
1710 i += 2;
1712 break;
1713 default:
1714 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1715 ret = FALSE;
1716 goto end;
1718 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1719 pLinePts[nLinePts++] = pLinePts[0];
1721 if(nLinePts >= 2)
1722 Polyline(hdc, pLinePts, nLinePts);
1724 end:
1725 HeapFree(GetProcessHeap(), 0, pLinePts);
1727 /* Restore the old mapping mode */
1728 SetMapMode(hdc, mapMode);
1729 SetWindowExtEx(hdc, szWindowExt.cx, szWindowExt.cy, NULL);
1730 SetWindowOrgEx(hdc, ptWindowOrg.x, ptWindowOrg.y, NULL);
1731 SetViewportExtEx(hdc, szViewportExt.cx, szViewportExt.cy, NULL);
1732 SetViewportOrgEx(hdc, ptViewportOrg.x, ptViewportOrg.y, NULL);
1734 /* Go to GM_ADVANCED temporarily to restore the world transform */
1735 graphicsMode=GetGraphicsMode(hdc);
1736 SetGraphicsMode(hdc, GM_ADVANCED);
1737 SetWorldTransform(hdc, &xform);
1738 SetGraphicsMode(hdc, graphicsMode);
1740 /* If we've moved the current point then get its new position
1741 which will be in device (MM_TEXT) co-ords, convert it to
1742 logical co-ords and re-set it. This basically updates
1743 dc->CurPosX|Y so that their values are in the correct mapping
1744 mode.
1746 if(i > 0) {
1747 POINT pt;
1748 GetCurrentPositionEx(hdc, &pt);
1749 DPtoLP(hdc, &pt, 1);
1750 MoveToEx(hdc, pt.x, pt.y, NULL);
1753 return ret;
1756 #define round(x) ((int)((x)>0?(x)+0.5:(x)-0.5))
1758 static struct gdi_path *PATH_WidenPath(DC *dc)
1760 INT i, j, numStrokes, penWidth, penWidthIn, penWidthOut, size, penStyle;
1761 struct gdi_path *flat_path, *pNewPath, **pStrokes = NULL, *pUpPath, *pDownPath;
1762 EXTLOGPEN *elp;
1763 DWORD obj_type, joint, endcap, penType;
1765 size = GetObjectW( dc->hPen, 0, NULL );
1766 if (!size) {
1767 SetLastError(ERROR_CAN_NOT_COMPLETE);
1768 return NULL;
1771 elp = HeapAlloc( GetProcessHeap(), 0, size );
1772 GetObjectW( dc->hPen, size, elp );
1774 obj_type = GetObjectType(dc->hPen);
1775 if(obj_type == OBJ_PEN) {
1776 penStyle = ((LOGPEN*)elp)->lopnStyle;
1778 else if(obj_type == OBJ_EXTPEN) {
1779 penStyle = elp->elpPenStyle;
1781 else {
1782 SetLastError(ERROR_CAN_NOT_COMPLETE);
1783 HeapFree( GetProcessHeap(), 0, elp );
1784 return NULL;
1787 penWidth = elp->elpWidth;
1788 HeapFree( GetProcessHeap(), 0, elp );
1790 endcap = (PS_ENDCAP_MASK & penStyle);
1791 joint = (PS_JOIN_MASK & penStyle);
1792 penType = (PS_TYPE_MASK & penStyle);
1794 /* The function cannot apply to cosmetic pens */
1795 if(obj_type == OBJ_EXTPEN && penType == PS_COSMETIC) {
1796 SetLastError(ERROR_CAN_NOT_COMPLETE);
1797 return NULL;
1800 if (!(flat_path = PATH_FlattenPath( dc->path ))) return NULL;
1802 penWidthIn = penWidth / 2;
1803 penWidthOut = penWidth / 2;
1804 if(penWidthIn + penWidthOut < penWidth)
1805 penWidthOut++;
1807 numStrokes = 0;
1809 for(i = 0, j = 0; i < flat_path->numEntriesUsed; i++, j++) {
1810 POINT point;
1811 if((i == 0 || (flat_path->pFlags[i-1] & PT_CLOSEFIGURE)) &&
1812 (flat_path->pFlags[i] != PT_MOVETO)) {
1813 ERR("Expected PT_MOVETO %s, got path flag %c\n",
1814 i == 0 ? "as first point" : "after PT_CLOSEFIGURE",
1815 flat_path->pFlags[i]);
1816 free_gdi_path( flat_path );
1817 return NULL;
1819 switch(flat_path->pFlags[i]) {
1820 case PT_MOVETO:
1821 if(numStrokes > 0) {
1822 pStrokes[numStrokes - 1]->state = PATH_Closed;
1824 numStrokes++;
1825 j = 0;
1826 if(numStrokes == 1)
1827 pStrokes = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath*));
1828 else
1829 pStrokes = HeapReAlloc(GetProcessHeap(), 0, pStrokes, numStrokes * sizeof(GdiPath*));
1830 if(!pStrokes) return NULL;
1831 pStrokes[numStrokes - 1] = alloc_gdi_path();
1832 /* fall through */
1833 case PT_LINETO:
1834 case (PT_LINETO | PT_CLOSEFIGURE):
1835 point.x = flat_path->pPoints[i].x;
1836 point.y = flat_path->pPoints[i].y;
1837 PATH_AddEntry(pStrokes[numStrokes - 1], &point, flat_path->pFlags[i]);
1838 break;
1839 case PT_BEZIERTO:
1840 /* should never happen because of the FlattenPath call */
1841 ERR("Should never happen\n");
1842 break;
1843 default:
1844 ERR("Got path flag %c\n", flat_path->pFlags[i]);
1845 return NULL;
1849 pNewPath = alloc_gdi_path();
1851 for(i = 0; i < numStrokes; i++) {
1852 pUpPath = alloc_gdi_path();
1853 pDownPath = alloc_gdi_path();
1855 for(j = 0; j < pStrokes[i]->numEntriesUsed; j++) {
1856 /* Beginning or end of the path if not closed */
1857 if((!(pStrokes[i]->pFlags[pStrokes[i]->numEntriesUsed - 1] & PT_CLOSEFIGURE)) && (j == 0 || j == pStrokes[i]->numEntriesUsed - 1) ) {
1858 /* Compute segment angle */
1859 double xo, yo, xa, ya, theta;
1860 POINT pt;
1861 FLOAT_POINT corners[2];
1862 if(j == 0) {
1863 xo = pStrokes[i]->pPoints[j].x;
1864 yo = pStrokes[i]->pPoints[j].y;
1865 xa = pStrokes[i]->pPoints[1].x;
1866 ya = pStrokes[i]->pPoints[1].y;
1868 else {
1869 xa = pStrokes[i]->pPoints[j - 1].x;
1870 ya = pStrokes[i]->pPoints[j - 1].y;
1871 xo = pStrokes[i]->pPoints[j].x;
1872 yo = pStrokes[i]->pPoints[j].y;
1874 theta = atan2( ya - yo, xa - xo );
1875 switch(endcap) {
1876 case PS_ENDCAP_SQUARE :
1877 pt.x = xo + round(sqrt(2) * penWidthOut * cos(M_PI_4 + theta));
1878 pt.y = yo + round(sqrt(2) * penWidthOut * sin(M_PI_4 + theta));
1879 PATH_AddEntry(pUpPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO) );
1880 pt.x = xo + round(sqrt(2) * penWidthIn * cos(- M_PI_4 + theta));
1881 pt.y = yo + round(sqrt(2) * penWidthIn * sin(- M_PI_4 + theta));
1882 PATH_AddEntry(pUpPath, &pt, PT_LINETO);
1883 break;
1884 case PS_ENDCAP_FLAT :
1885 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
1886 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
1887 PATH_AddEntry(pUpPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO));
1888 pt.x = xo - round( penWidthIn * cos(theta + M_PI_2) );
1889 pt.y = yo - round( penWidthIn * sin(theta + M_PI_2) );
1890 PATH_AddEntry(pUpPath, &pt, PT_LINETO);
1891 break;
1892 case PS_ENDCAP_ROUND :
1893 default :
1894 corners[0].x = xo - penWidthIn;
1895 corners[0].y = yo - penWidthIn;
1896 corners[1].x = xo + penWidthOut;
1897 corners[1].y = yo + penWidthOut;
1898 PATH_DoArcPart(pUpPath ,corners, theta + M_PI_2 , theta + 3 * M_PI_4, (j == 0 ? PT_MOVETO : FALSE));
1899 PATH_DoArcPart(pUpPath ,corners, theta + 3 * M_PI_4 , theta + M_PI, FALSE);
1900 PATH_DoArcPart(pUpPath ,corners, theta + M_PI, theta + 5 * M_PI_4, FALSE);
1901 PATH_DoArcPart(pUpPath ,corners, theta + 5 * M_PI_4 , theta + 3 * M_PI_2, FALSE);
1902 break;
1905 /* Corpse of the path */
1906 else {
1907 /* Compute angle */
1908 INT previous, next;
1909 double xa, ya, xb, yb, xo, yo;
1910 double alpha, theta, miterWidth;
1911 DWORD _joint = joint;
1912 POINT pt;
1913 GdiPath *pInsidePath, *pOutsidePath;
1914 if(j > 0 && j < pStrokes[i]->numEntriesUsed - 1) {
1915 previous = j - 1;
1916 next = j + 1;
1918 else if (j == 0) {
1919 previous = pStrokes[i]->numEntriesUsed - 1;
1920 next = j + 1;
1922 else {
1923 previous = j - 1;
1924 next = 0;
1926 xo = pStrokes[i]->pPoints[j].x;
1927 yo = pStrokes[i]->pPoints[j].y;
1928 xa = pStrokes[i]->pPoints[previous].x;
1929 ya = pStrokes[i]->pPoints[previous].y;
1930 xb = pStrokes[i]->pPoints[next].x;
1931 yb = pStrokes[i]->pPoints[next].y;
1932 theta = atan2( yo - ya, xo - xa );
1933 alpha = atan2( yb - yo, xb - xo ) - theta;
1934 if (alpha > 0) alpha -= M_PI;
1935 else alpha += M_PI;
1936 if(_joint == PS_JOIN_MITER && dc->miterLimit < fabs(1 / sin(alpha/2))) {
1937 _joint = PS_JOIN_BEVEL;
1939 if(alpha > 0) {
1940 pInsidePath = pUpPath;
1941 pOutsidePath = pDownPath;
1943 else if(alpha < 0) {
1944 pInsidePath = pDownPath;
1945 pOutsidePath = pUpPath;
1947 else {
1948 continue;
1950 /* Inside angle points */
1951 if(alpha > 0) {
1952 pt.x = xo - round( penWidthIn * cos(theta + M_PI_2) );
1953 pt.y = yo - round( penWidthIn * sin(theta + M_PI_2) );
1955 else {
1956 pt.x = xo + round( penWidthIn * cos(theta + M_PI_2) );
1957 pt.y = yo + round( penWidthIn * sin(theta + M_PI_2) );
1959 PATH_AddEntry(pInsidePath, &pt, PT_LINETO);
1960 if(alpha > 0) {
1961 pt.x = xo + round( penWidthIn * cos(M_PI_2 + alpha + theta) );
1962 pt.y = yo + round( penWidthIn * sin(M_PI_2 + alpha + theta) );
1964 else {
1965 pt.x = xo - round( penWidthIn * cos(M_PI_2 + alpha + theta) );
1966 pt.y = yo - round( penWidthIn * sin(M_PI_2 + alpha + theta) );
1968 PATH_AddEntry(pInsidePath, &pt, PT_LINETO);
1969 /* Outside angle point */
1970 switch(_joint) {
1971 case PS_JOIN_MITER :
1972 miterWidth = fabs(penWidthOut / cos(M_PI_2 - fabs(alpha) / 2));
1973 pt.x = xo + round( miterWidth * cos(theta + alpha / 2) );
1974 pt.y = yo + round( miterWidth * sin(theta + alpha / 2) );
1975 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
1976 break;
1977 case PS_JOIN_BEVEL :
1978 if(alpha > 0) {
1979 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
1980 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
1982 else {
1983 pt.x = xo - round( penWidthOut * cos(theta + M_PI_2) );
1984 pt.y = yo - round( penWidthOut * sin(theta + M_PI_2) );
1986 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
1987 if(alpha > 0) {
1988 pt.x = xo - round( penWidthOut * cos(M_PI_2 + alpha + theta) );
1989 pt.y = yo - round( penWidthOut * sin(M_PI_2 + alpha + theta) );
1991 else {
1992 pt.x = xo + round( penWidthOut * cos(M_PI_2 + alpha + theta) );
1993 pt.y = yo + round( penWidthOut * sin(M_PI_2 + alpha + theta) );
1995 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
1996 break;
1997 case PS_JOIN_ROUND :
1998 default :
1999 if(alpha > 0) {
2000 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
2001 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
2003 else {
2004 pt.x = xo - round( penWidthOut * cos(theta + M_PI_2) );
2005 pt.y = yo - round( penWidthOut * sin(theta + M_PI_2) );
2007 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
2008 pt.x = xo + round( penWidthOut * cos(theta + alpha / 2) );
2009 pt.y = yo + round( penWidthOut * sin(theta + alpha / 2) );
2010 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
2011 if(alpha > 0) {
2012 pt.x = xo - round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2013 pt.y = yo - round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2015 else {
2016 pt.x = xo + round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2017 pt.y = yo + round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2019 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
2020 break;
2024 for(j = 0; j < pUpPath->numEntriesUsed; j++) {
2025 POINT pt;
2026 pt.x = pUpPath->pPoints[j].x;
2027 pt.y = pUpPath->pPoints[j].y;
2028 PATH_AddEntry(pNewPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO));
2030 for(j = 0; j < pDownPath->numEntriesUsed; j++) {
2031 POINT pt;
2032 pt.x = pDownPath->pPoints[pDownPath->numEntriesUsed - j - 1].x;
2033 pt.y = pDownPath->pPoints[pDownPath->numEntriesUsed - j - 1].y;
2034 PATH_AddEntry(pNewPath, &pt, ( (j == 0 && (pStrokes[i]->pFlags[pStrokes[i]->numEntriesUsed - 1] & PT_CLOSEFIGURE)) ? PT_MOVETO : PT_LINETO));
2037 free_gdi_path( pStrokes[i] );
2038 free_gdi_path( pUpPath );
2039 free_gdi_path( pDownPath );
2041 HeapFree(GetProcessHeap(), 0, pStrokes);
2042 free_gdi_path( flat_path );
2044 pNewPath->state = PATH_Closed;
2045 return pNewPath;
2049 /*******************************************************************
2050 * StrokeAndFillPath [GDI32.@]
2054 BOOL WINAPI StrokeAndFillPath(HDC hdc)
2056 BOOL ret = FALSE;
2057 DC *dc = get_dc_ptr( hdc );
2059 if (dc)
2061 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pStrokeAndFillPath );
2062 ret = physdev->funcs->pStrokeAndFillPath( physdev );
2063 release_dc_ptr( dc );
2065 return ret;
2069 /*******************************************************************
2070 * StrokePath [GDI32.@]
2074 BOOL WINAPI StrokePath(HDC hdc)
2076 BOOL ret = FALSE;
2077 DC *dc = get_dc_ptr( hdc );
2079 if (dc)
2081 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pStrokePath );
2082 ret = physdev->funcs->pStrokePath( physdev );
2083 release_dc_ptr( dc );
2085 return ret;
2089 /*******************************************************************
2090 * WidenPath [GDI32.@]
2094 BOOL WINAPI WidenPath(HDC hdc)
2096 BOOL ret = FALSE;
2097 DC *dc = get_dc_ptr( hdc );
2099 if (dc)
2101 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pWidenPath );
2102 ret = physdev->funcs->pWidenPath( physdev );
2103 release_dc_ptr( dc );
2105 return ret;
2109 /***********************************************************************
2110 * null driver fallback implementations
2113 BOOL nulldrv_BeginPath( PHYSDEV dev )
2115 DC *dc = get_nulldrv_dc( dev );
2116 struct path_physdev *physdev;
2117 struct gdi_path *path = alloc_gdi_path();
2119 if (!path) return FALSE;
2120 if (!path_driver.pCreateDC( &dc->physDev, NULL, NULL, NULL, NULL ))
2122 free_gdi_path( path );
2123 return FALSE;
2125 physdev = get_path_physdev( dc->physDev );
2126 physdev->path = path;
2127 if (dc->path) free_gdi_path( dc->path );
2128 dc->path = NULL;
2129 return TRUE;
2132 BOOL nulldrv_EndPath( PHYSDEV dev )
2134 SetLastError( ERROR_CAN_NOT_COMPLETE );
2135 return FALSE;
2138 BOOL nulldrv_AbortPath( PHYSDEV dev )
2140 DC *dc = get_nulldrv_dc( dev );
2142 if (dc->path) free_gdi_path( dc->path );
2143 dc->path = NULL;
2144 return TRUE;
2147 BOOL nulldrv_CloseFigure( PHYSDEV dev )
2149 SetLastError( ERROR_CAN_NOT_COMPLETE );
2150 return FALSE;
2153 BOOL nulldrv_SelectClipPath( PHYSDEV dev, INT mode )
2155 BOOL ret;
2156 HRGN hrgn;
2157 DC *dc = get_nulldrv_dc( dev );
2159 if (!dc->path)
2161 SetLastError( ERROR_CAN_NOT_COMPLETE );
2162 return FALSE;
2164 if (!(hrgn = PATH_PathToRegion( dc->path, GetPolyFillMode(dev->hdc)))) return FALSE;
2165 ret = ExtSelectClipRgn( dev->hdc, hrgn, mode ) != ERROR;
2166 if (ret)
2168 free_gdi_path( dc->path );
2169 dc->path = NULL;
2171 /* FIXME: Should this function delete the path even if it failed? */
2172 DeleteObject( hrgn );
2173 return ret;
2176 BOOL nulldrv_FillPath( PHYSDEV dev )
2178 DC *dc = get_nulldrv_dc( dev );
2180 if (!dc->path)
2182 SetLastError( ERROR_CAN_NOT_COMPLETE );
2183 return FALSE;
2185 if (!PATH_FillPath( dev->hdc, dc->path )) return FALSE;
2186 /* FIXME: Should the path be emptied even if conversion failed? */
2187 free_gdi_path( dc->path );
2188 dc->path = NULL;
2189 return TRUE;
2192 BOOL nulldrv_StrokeAndFillPath( PHYSDEV dev )
2194 DC *dc = get_nulldrv_dc( dev );
2196 if (!dc->path)
2198 SetLastError( ERROR_CAN_NOT_COMPLETE );
2199 return FALSE;
2201 if (!PATH_FillPath( dev->hdc, dc->path )) return FALSE;
2202 if (!PATH_StrokePath( dev->hdc, dc->path )) return FALSE;
2203 free_gdi_path( dc->path );
2204 dc->path = NULL;
2205 return TRUE;
2208 BOOL nulldrv_StrokePath( PHYSDEV dev )
2210 DC *dc = get_nulldrv_dc( dev );
2212 if (!dc->path)
2214 SetLastError( ERROR_CAN_NOT_COMPLETE );
2215 return FALSE;
2217 if (!PATH_StrokePath( dev->hdc, dc->path )) return FALSE;
2218 free_gdi_path( dc->path );
2219 dc->path = NULL;
2220 return TRUE;
2223 BOOL nulldrv_FlattenPath( PHYSDEV dev )
2225 DC *dc = get_nulldrv_dc( dev );
2226 struct gdi_path *path;
2228 if (!dc->path)
2230 SetLastError( ERROR_CAN_NOT_COMPLETE );
2231 return FALSE;
2233 if (!(path = PATH_FlattenPath( dc->path ))) return FALSE;
2234 free_gdi_path( dc->path );
2235 dc->path = path;
2236 return TRUE;
2239 BOOL nulldrv_WidenPath( PHYSDEV dev )
2241 DC *dc = get_nulldrv_dc( dev );
2242 struct gdi_path *path;
2244 if (!dc->path)
2246 SetLastError( ERROR_CAN_NOT_COMPLETE );
2247 return FALSE;
2249 if (!(path = PATH_WidenPath( dc ))) return FALSE;
2250 free_gdi_path( dc->path );
2251 dc->path = path;
2252 return TRUE;
2255 const struct gdi_dc_funcs path_driver =
2257 NULL, /* pAbortDoc */
2258 pathdrv_AbortPath, /* pAbortPath */
2259 NULL, /* pAlphaBlend */
2260 pathdrv_AngleArc, /* pAngleArc */
2261 pathdrv_Arc, /* pArc */
2262 pathdrv_ArcTo, /* pArcTo */
2263 pathdrv_BeginPath, /* pBeginPath */
2264 NULL, /* pBlendImage */
2265 NULL, /* pChoosePixelFormat */
2266 pathdrv_Chord, /* pChord */
2267 pathdrv_CloseFigure, /* pCloseFigure */
2268 NULL, /* pCopyBitmap */
2269 NULL, /* pCreateBitmap */
2270 NULL, /* pCreateCompatibleDC */
2271 pathdrv_CreateDC, /* pCreateDC */
2272 NULL, /* pCreateDIBSection */
2273 NULL, /* pDeleteBitmap */
2274 pathdrv_DeleteDC, /* pDeleteDC */
2275 NULL, /* pDeleteObject */
2276 NULL, /* pDescribePixelFormat */
2277 NULL, /* pDeviceCapabilities */
2278 pathdrv_Ellipse, /* pEllipse */
2279 NULL, /* pEndDoc */
2280 NULL, /* pEndPage */
2281 pathdrv_EndPath, /* pEndPath */
2282 NULL, /* pEnumFonts */
2283 NULL, /* pEnumICMProfiles */
2284 NULL, /* pExcludeClipRect */
2285 NULL, /* pExtDeviceMode */
2286 NULL, /* pExtEscape */
2287 NULL, /* pExtFloodFill */
2288 NULL, /* pExtSelectClipRgn */
2289 pathdrv_ExtTextOut, /* pExtTextOut */
2290 NULL, /* pFillPath */
2291 NULL, /* pFillRgn */
2292 NULL, /* pFlattenPath */
2293 NULL, /* pFontIsLinked */
2294 NULL, /* pFrameRgn */
2295 NULL, /* pGdiComment */
2296 NULL, /* pGdiRealizationInfo */
2297 NULL, /* pGetCharABCWidths */
2298 NULL, /* pGetCharABCWidthsI */
2299 NULL, /* pGetCharWidth */
2300 NULL, /* pGetDeviceCaps */
2301 NULL, /* pGetDeviceGammaRamp */
2302 NULL, /* pGetFontData */
2303 NULL, /* pGetFontUnicodeRanges */
2304 NULL, /* pGetGlyphIndices */
2305 NULL, /* pGetGlyphOutline */
2306 NULL, /* pGetICMProfile */
2307 NULL, /* pGetImage */
2308 NULL, /* pGetKerningPairs */
2309 NULL, /* pGetNearestColor */
2310 NULL, /* pGetOutlineTextMetrics */
2311 NULL, /* pGetPixel */
2312 NULL, /* pGetPixelFormat */
2313 NULL, /* pGetSystemPaletteEntries */
2314 NULL, /* pGetTextCharsetInfo */
2315 NULL, /* pGetTextExtentExPoint */
2316 NULL, /* pGetTextExtentExPointI */
2317 NULL, /* pGetTextFace */
2318 NULL, /* pGetTextMetrics */
2319 NULL, /* pGradientFill */
2320 NULL, /* pIntersectClipRect */
2321 NULL, /* pInvertRgn */
2322 pathdrv_LineTo, /* pLineTo */
2323 NULL, /* pModifyWorldTransform */
2324 pathdrv_MoveTo, /* pMoveTo */
2325 NULL, /* pOffsetClipRgn */
2326 NULL, /* pOffsetViewportOrg */
2327 NULL, /* pOffsetWindowOrg */
2328 NULL, /* pPaintRgn */
2329 NULL, /* pPatBlt */
2330 pathdrv_Pie, /* pPie */
2331 pathdrv_PolyBezier, /* pPolyBezier */
2332 pathdrv_PolyBezierTo, /* pPolyBezierTo */
2333 pathdrv_PolyDraw, /* pPolyDraw */
2334 pathdrv_PolyPolygon, /* pPolyPolygon */
2335 pathdrv_PolyPolyline, /* pPolyPolyline */
2336 pathdrv_Polygon, /* pPolygon */
2337 pathdrv_Polyline, /* pPolyline */
2338 pathdrv_PolylineTo, /* pPolylineTo */
2339 NULL, /* pPutImage */
2340 NULL, /* pRealizeDefaultPalette */
2341 NULL, /* pRealizePalette */
2342 pathdrv_Rectangle, /* pRectangle */
2343 NULL, /* pResetDC */
2344 NULL, /* pRestoreDC */
2345 pathdrv_RoundRect, /* pRoundRect */
2346 NULL, /* pSaveDC */
2347 NULL, /* pScaleViewportExt */
2348 NULL, /* pScaleWindowExt */
2349 NULL, /* pSelectBitmap */
2350 NULL, /* pSelectBrush */
2351 NULL, /* pSelectClipPath */
2352 NULL, /* pSelectFont */
2353 NULL, /* pSelectPalette */
2354 NULL, /* pSelectPen */
2355 NULL, /* pSetArcDirection */
2356 NULL, /* pSetBkColor */
2357 NULL, /* pSetBkMode */
2358 NULL, /* pSetDCBrushColor */
2359 NULL, /* pSetDCPenColor */
2360 NULL, /* pSetDIBColorTable */
2361 NULL, /* pSetDIBitsToDevice */
2362 NULL, /* pSetDeviceClipping */
2363 NULL, /* pSetDeviceGammaRamp */
2364 NULL, /* pSetLayout */
2365 NULL, /* pSetMapMode */
2366 NULL, /* pSetMapperFlags */
2367 NULL, /* pSetPixel */
2368 NULL, /* pSetPixelFormat */
2369 NULL, /* pSetPolyFillMode */
2370 NULL, /* pSetROP2 */
2371 NULL, /* pSetRelAbs */
2372 NULL, /* pSetStretchBltMode */
2373 NULL, /* pSetTextAlign */
2374 NULL, /* pSetTextCharacterExtra */
2375 NULL, /* pSetTextColor */
2376 NULL, /* pSetTextJustification */
2377 NULL, /* pSetViewportExt */
2378 NULL, /* pSetViewportOrg */
2379 NULL, /* pSetWindowExt */
2380 NULL, /* pSetWindowOrg */
2381 NULL, /* pSetWorldTransform */
2382 NULL, /* pStartDoc */
2383 NULL, /* pStartPage */
2384 NULL, /* pStretchBlt */
2385 NULL, /* pStretchDIBits */
2386 NULL, /* pStrokeAndFillPath */
2387 NULL, /* pStrokePath */
2388 NULL, /* pSwapBuffers */
2389 NULL, /* pUnrealizePalette */
2390 NULL, /* pWidenPath */
2391 /* OpenGL not supported */