gdi32: Use reallocs when growing a path instead of doing it by hand.
[wine/multimedia.git] / dlls / gdi32 / path.c
blob6cf92df111baca5e5dc42ed718feb8ee6ed362d9
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;
85 struct path_physdev
87 struct gdi_physdev dev;
88 GdiPath *path;
91 static inline struct path_physdev *get_path_physdev( PHYSDEV dev )
93 return (struct path_physdev *)dev;
96 static inline void pop_path_driver( DC *dc )
98 PHYSDEV dev = pop_dc_driver( &dc->physDev );
99 assert( dev->funcs == &path_driver );
100 HeapFree( GetProcessHeap(), 0, dev );
104 /* Performs a world-to-viewport transformation on the specified point (which
105 * is in floating point format).
107 static inline void INTERNAL_LPTODP_FLOAT( HDC hdc, FLOAT_POINT *point, int count )
109 DC *dc = get_dc_ptr( hdc );
110 double x, y;
112 while (count--)
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;
118 point++;
120 release_dc_ptr( dc );
123 static inline INT int_from_fixed(FIXED f)
125 return (f.fract >= 0x8000) ? (f.value + 1) : f.value;
129 /* PATH_EmptyPath
131 * Removes all entries from the path and sets the path state to PATH_Null.
133 static void PATH_EmptyPath(GdiPath *pPath)
135 pPath->state=PATH_Null;
136 pPath->numEntriesUsed=0;
139 /* PATH_ReserveEntries
141 * Ensures that at least "numEntries" entries (for points and flags) have
142 * been allocated; allocates larger arrays and copies the existing entries
143 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
145 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT count)
147 POINT *pPointsNew;
148 BYTE *pFlagsNew;
150 assert(count>=0);
152 /* Do we have to allocate more memory? */
153 if(count > pPath->numEntriesAllocated)
155 /* Find number of entries to allocate. We let the size of the array
156 * grow exponentially, since that will guarantee linear time
157 * complexity. */
158 count = max( pPath->numEntriesAllocated * 2, count );
160 pPointsNew = HeapReAlloc( GetProcessHeap(), 0, pPath->pPoints, count * sizeof(POINT) );
161 if (!pPointsNew) return FALSE;
162 pPath->pPoints = pPointsNew;
164 pFlagsNew = HeapReAlloc( GetProcessHeap(), 0, pPath->pFlags, count * sizeof(BYTE) );
165 if (!pFlagsNew) return FALSE;
166 pPath->pFlags = pFlagsNew;
168 pPath->numEntriesAllocated = count;
170 return TRUE;
173 /* PATH_AddEntry
175 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
176 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
177 * successful, FALSE otherwise (e.g. if not enough memory was available).
179 static BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
181 /* FIXME: If newStroke is true, perhaps we want to check that we're
182 * getting a PT_MOVETO
184 TRACE("(%d,%d) - %d\n", pPoint->x, pPoint->y, flags);
186 /* Check that path is open */
187 if(pPath->state!=PATH_Open)
188 return FALSE;
190 /* Reserve enough memory for an extra path entry */
191 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
192 return FALSE;
194 /* Store information in path entry */
195 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
196 pPath->pFlags[pPath->numEntriesUsed]=flags;
198 pPath->numEntriesUsed++;
200 return TRUE;
203 /* add a number of points, converting them to device coords */
204 /* return a pointer to the first type byte so it can be fixed up if necessary */
205 static BYTE *add_log_points( struct path_physdev *physdev, const POINT *points, DWORD count, BYTE type )
207 BYTE *ret;
208 GdiPath *path = physdev->path;
210 if (!PATH_ReserveEntries( path, path->numEntriesUsed + count )) return NULL;
212 ret = &path->pFlags[path->numEntriesUsed];
213 memcpy( &path->pPoints[path->numEntriesUsed], points, count * sizeof(*points) );
214 LPtoDP( physdev->dev.hdc, &path->pPoints[path->numEntriesUsed], count );
215 memset( ret, type, count );
216 path->numEntriesUsed += count;
217 return ret;
220 /* start a new path stroke if necessary */
221 static BOOL start_new_stroke( struct path_physdev *physdev )
223 POINT pos;
224 GdiPath *path = physdev->path;
226 if (!path->newStroke && path->numEntriesUsed &&
227 !(path->pFlags[path->numEntriesUsed - 1] & PT_CLOSEFIGURE))
228 return TRUE;
230 path->newStroke = FALSE;
231 GetCurrentPositionEx( physdev->dev.hdc, &pos );
232 return add_log_points( physdev, &pos, 1, PT_MOVETO ) != NULL;
235 /* PATH_AssignGdiPath
237 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
238 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
239 * not just the pointers. Since this means that the arrays in pPathDest may
240 * need to be resized, pPathDest should have been initialized using
241 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
242 * not a copy constructor).
243 * Returns TRUE if successful, else FALSE.
245 static BOOL PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
247 /* Make sure destination arrays are big enough */
248 if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
249 return FALSE;
251 /* Perform the copy operation */
252 memcpy(pPathDest->pPoints, pPathSrc->pPoints,
253 sizeof(POINT)*pPathSrc->numEntriesUsed);
254 memcpy(pPathDest->pFlags, pPathSrc->pFlags,
255 sizeof(BYTE)*pPathSrc->numEntriesUsed);
257 pPathDest->state=pPathSrc->state;
258 pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
259 pPathDest->newStroke=pPathSrc->newStroke;
261 return TRUE;
264 /* PATH_CheckCorners
266 * Helper function for RoundRect() and Rectangle()
268 static void PATH_CheckCorners( HDC hdc, POINT corners[], INT x1, INT y1, INT x2, INT y2 )
270 INT temp;
272 /* Convert points to device coordinates */
273 corners[0].x=x1;
274 corners[0].y=y1;
275 corners[1].x=x2;
276 corners[1].y=y2;
277 LPtoDP( hdc, corners, 2 );
279 /* Make sure first corner is top left and second corner is bottom right */
280 if(corners[0].x>corners[1].x)
282 temp=corners[0].x;
283 corners[0].x=corners[1].x;
284 corners[1].x=temp;
286 if(corners[0].y>corners[1].y)
288 temp=corners[0].y;
289 corners[0].y=corners[1].y;
290 corners[1].y=temp;
293 /* In GM_COMPATIBLE, don't include bottom and right edges */
294 if (GetGraphicsMode( hdc ) == GM_COMPATIBLE)
296 corners[1].x--;
297 corners[1].y--;
301 /* PATH_AddFlatBezier
303 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
305 POINT *pts;
306 INT no, i;
308 pts = GDI_Bezier( pt, 4, &no );
309 if(!pts) return FALSE;
311 for(i = 1; i < no; i++)
312 PATH_AddEntry(pPath, &pts[i], (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
313 HeapFree( GetProcessHeap(), 0, pts );
314 return TRUE;
317 /* PATH_FlattenPath
319 * Replaces Beziers with line segments
322 static BOOL PATH_FlattenPath(GdiPath *pPath)
324 GdiPath newPath;
325 INT srcpt;
327 memset(&newPath, 0, sizeof(newPath));
328 newPath.state = PATH_Open;
329 for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
330 switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
331 case PT_MOVETO:
332 case PT_LINETO:
333 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
334 pPath->pFlags[srcpt]);
335 break;
336 case PT_BEZIERTO:
337 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
338 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
339 srcpt += 2;
340 break;
343 newPath.state = PATH_Closed;
344 PATH_AssignGdiPath(pPath, &newPath);
345 PATH_DestroyGdiPath(&newPath);
346 return TRUE;
349 /* PATH_PathToRegion
351 * Creates a region from the specified path using the specified polygon
352 * filling mode. The path is left unchanged. A handle to the region that
353 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
354 * error occurs, SetLastError is called with the appropriate value and
355 * FALSE is returned.
357 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
358 HRGN *pHrgn)
360 int numStrokes, iStroke, i;
361 INT *pNumPointsInStroke;
362 HRGN hrgn;
364 PATH_FlattenPath(pPath);
366 /* FIXME: What happens when number of points is zero? */
368 /* First pass: Find out how many strokes there are in the path */
369 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
370 numStrokes=0;
371 for(i=0; i<pPath->numEntriesUsed; i++)
372 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
373 numStrokes++;
375 /* Allocate memory for number-of-points-in-stroke array */
376 pNumPointsInStroke=HeapAlloc( GetProcessHeap(), 0, sizeof(int) * numStrokes );
377 if(!pNumPointsInStroke)
379 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
380 return FALSE;
383 /* Second pass: remember number of points in each polygon */
384 iStroke=-1; /* Will get incremented to 0 at beginning of first stroke */
385 for(i=0; i<pPath->numEntriesUsed; i++)
387 /* Is this the beginning of a new stroke? */
388 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
390 iStroke++;
391 pNumPointsInStroke[iStroke]=0;
394 pNumPointsInStroke[iStroke]++;
397 /* Create a region from the strokes */
398 hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
399 numStrokes, nPolyFillMode);
401 /* Free memory for number-of-points-in-stroke array */
402 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
404 if(hrgn==NULL)
406 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
407 return FALSE;
410 /* Success! */
411 *pHrgn=hrgn;
412 return TRUE;
415 /* PATH_ScaleNormalizedPoint
417 * Scales a normalized point (x, y) with respect to the box whose corners are
418 * passed in "corners". The point is stored in "*pPoint". The normalized
419 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
420 * (1.0, 1.0) correspond to corners[1].
422 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
423 double y, POINT *pPoint)
425 pPoint->x=GDI_ROUND( (double)corners[0].x + (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
426 pPoint->y=GDI_ROUND( (double)corners[0].y + (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
429 /* PATH_NormalizePoint
431 * Normalizes a point with respect to the box whose corners are passed in
432 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
434 static void PATH_NormalizePoint(FLOAT_POINT corners[],
435 const FLOAT_POINT *pPoint,
436 double *pX, double *pY)
438 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) * 2.0 - 1.0;
439 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) * 2.0 - 1.0;
442 /* PATH_DoArcPart
444 * Creates a Bezier spline that corresponds to part of an arc and appends the
445 * corresponding points to the path. The start and end angles are passed in
446 * "angleStart" and "angleEnd"; these angles should span a quarter circle
447 * at most. If "startEntryType" is non-zero, an entry of that type for the first
448 * control point is added to the path; otherwise, it is assumed that the current
449 * position is equal to the first control point.
451 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
452 double angleStart, double angleEnd, BYTE startEntryType)
454 double halfAngle, a;
455 double xNorm[4], yNorm[4];
456 POINT point;
457 int i;
459 assert(fabs(angleEnd-angleStart)<=M_PI_2);
461 /* FIXME: Is there an easier way of computing this? */
463 /* Compute control points */
464 halfAngle=(angleEnd-angleStart)/2.0;
465 if(fabs(halfAngle)>1e-8)
467 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
468 xNorm[0]=cos(angleStart);
469 yNorm[0]=sin(angleStart);
470 xNorm[1]=xNorm[0] - a*yNorm[0];
471 yNorm[1]=yNorm[0] + a*xNorm[0];
472 xNorm[3]=cos(angleEnd);
473 yNorm[3]=sin(angleEnd);
474 xNorm[2]=xNorm[3] + a*yNorm[3];
475 yNorm[2]=yNorm[3] - a*xNorm[3];
477 else
478 for(i=0; i<4; i++)
480 xNorm[i]=cos(angleStart);
481 yNorm[i]=sin(angleStart);
484 /* Add starting point to path if desired */
485 if(startEntryType)
487 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
488 if(!PATH_AddEntry(pPath, &point, startEntryType))
489 return FALSE;
492 /* Add remaining control points */
493 for(i=1; i<4; i++)
495 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
496 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
497 return FALSE;
500 return TRUE;
504 /***********************************************************************
505 * BeginPath (GDI32.@)
507 BOOL WINAPI BeginPath(HDC hdc)
509 BOOL ret = FALSE;
510 DC *dc = get_dc_ptr( hdc );
512 if (dc)
514 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pBeginPath );
515 ret = physdev->funcs->pBeginPath( physdev );
516 release_dc_ptr( dc );
518 return ret;
522 /***********************************************************************
523 * EndPath (GDI32.@)
525 BOOL WINAPI EndPath(HDC hdc)
527 BOOL ret = FALSE;
528 DC *dc = get_dc_ptr( hdc );
530 if (dc)
532 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pEndPath );
533 ret = physdev->funcs->pEndPath( physdev );
534 release_dc_ptr( dc );
536 return ret;
540 /******************************************************************************
541 * AbortPath [GDI32.@]
542 * Closes and discards paths from device context
544 * NOTES
545 * Check that SetLastError is being called correctly
547 * PARAMS
548 * hdc [I] Handle to device context
550 * RETURNS
551 * Success: TRUE
552 * Failure: FALSE
554 BOOL WINAPI AbortPath( HDC hdc )
556 BOOL ret = FALSE;
557 DC *dc = get_dc_ptr( hdc );
559 if (dc)
561 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pAbortPath );
562 ret = physdev->funcs->pAbortPath( physdev );
563 release_dc_ptr( dc );
565 return ret;
569 /***********************************************************************
570 * CloseFigure (GDI32.@)
572 * FIXME: Check that SetLastError is being called correctly
574 BOOL WINAPI CloseFigure(HDC hdc)
576 BOOL ret = FALSE;
577 DC *dc = get_dc_ptr( hdc );
579 if (dc)
581 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pCloseFigure );
582 ret = physdev->funcs->pCloseFigure( physdev );
583 release_dc_ptr( dc );
585 return ret;
589 /***********************************************************************
590 * GetPath (GDI32.@)
592 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
593 INT nSize)
595 INT ret = -1;
596 GdiPath *pPath;
597 DC *dc = get_dc_ptr( hdc );
599 if(!dc) return -1;
601 pPath = &dc->path;
603 /* Check that path is closed */
604 if(pPath->state!=PATH_Closed)
606 SetLastError(ERROR_CAN_NOT_COMPLETE);
607 goto done;
610 if(nSize==0)
611 ret = pPath->numEntriesUsed;
612 else if(nSize<pPath->numEntriesUsed)
614 SetLastError(ERROR_INVALID_PARAMETER);
615 goto done;
617 else
619 memcpy(pPoints, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
620 memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
622 /* Convert the points to logical coordinates */
623 if(!DPtoLP(hdc, pPoints, pPath->numEntriesUsed))
625 /* FIXME: Is this the correct value? */
626 SetLastError(ERROR_CAN_NOT_COMPLETE);
627 goto done;
629 else ret = pPath->numEntriesUsed;
631 done:
632 release_dc_ptr( dc );
633 return ret;
637 /***********************************************************************
638 * PathToRegion (GDI32.@)
640 * FIXME
641 * Check that SetLastError is being called correctly
643 * The documentation does not state this explicitly, but a test under Windows
644 * shows that the region which is returned should be in device coordinates.
646 HRGN WINAPI PathToRegion(HDC hdc)
648 GdiPath *pPath;
649 HRGN hrgnRval = 0;
650 DC *dc = get_dc_ptr( hdc );
652 /* Get pointer to path */
653 if(!dc) return 0;
655 pPath = &dc->path;
657 /* Check that path is closed */
658 if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
659 else
661 /* FIXME: Should we empty the path even if conversion failed? */
662 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
663 PATH_EmptyPath(pPath);
664 else
665 hrgnRval=0;
667 release_dc_ptr( dc );
668 return hrgnRval;
671 static BOOL PATH_FillPath( HDC hdc, GdiPath *pPath )
673 INT mapMode, graphicsMode;
674 SIZE ptViewportExt, ptWindowExt;
675 POINT ptViewportOrg, ptWindowOrg;
676 XFORM xform;
677 HRGN hrgn;
679 /* Construct a region from the path and fill it */
680 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgn))
682 /* Since PaintRgn interprets the region as being in logical coordinates
683 * but the points we store for the path are already in device
684 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
685 * Using SaveDC to save information about the mapping mode / world
686 * transform would be easier but would require more overhead, especially
687 * now that SaveDC saves the current path.
690 /* Save the information about the old mapping mode */
691 mapMode=GetMapMode(hdc);
692 GetViewportExtEx(hdc, &ptViewportExt);
693 GetViewportOrgEx(hdc, &ptViewportOrg);
694 GetWindowExtEx(hdc, &ptWindowExt);
695 GetWindowOrgEx(hdc, &ptWindowOrg);
697 /* Save world transform
698 * NB: The Windows documentation on world transforms would lead one to
699 * believe that this has to be done only in GM_ADVANCED; however, my
700 * tests show that resetting the graphics mode to GM_COMPATIBLE does
701 * not reset the world transform.
703 GetWorldTransform(hdc, &xform);
705 /* Set MM_TEXT */
706 SetMapMode(hdc, MM_TEXT);
707 SetViewportOrgEx(hdc, 0, 0, NULL);
708 SetWindowOrgEx(hdc, 0, 0, NULL);
709 graphicsMode=GetGraphicsMode(hdc);
710 SetGraphicsMode(hdc, GM_ADVANCED);
711 ModifyWorldTransform(hdc, &xform, MWT_IDENTITY);
712 SetGraphicsMode(hdc, graphicsMode);
714 /* Paint the region */
715 PaintRgn(hdc, hrgn);
716 DeleteObject(hrgn);
717 /* Restore the old mapping mode */
718 SetMapMode(hdc, mapMode);
719 SetViewportExtEx(hdc, ptViewportExt.cx, ptViewportExt.cy, NULL);
720 SetViewportOrgEx(hdc, ptViewportOrg.x, ptViewportOrg.y, NULL);
721 SetWindowExtEx(hdc, ptWindowExt.cx, ptWindowExt.cy, NULL);
722 SetWindowOrgEx(hdc, ptWindowOrg.x, ptWindowOrg.y, NULL);
724 /* Go to GM_ADVANCED temporarily to restore the world transform */
725 graphicsMode=GetGraphicsMode(hdc);
726 SetGraphicsMode(hdc, GM_ADVANCED);
727 SetWorldTransform(hdc, &xform);
728 SetGraphicsMode(hdc, graphicsMode);
729 return TRUE;
731 return FALSE;
735 /***********************************************************************
736 * FillPath (GDI32.@)
738 * FIXME
739 * Check that SetLastError is being called correctly
741 BOOL WINAPI FillPath(HDC hdc)
743 BOOL ret = FALSE;
744 DC *dc = get_dc_ptr( hdc );
746 if (dc)
748 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pFillPath );
749 ret = physdev->funcs->pFillPath( physdev );
750 release_dc_ptr( dc );
752 return ret;
756 /***********************************************************************
757 * SelectClipPath (GDI32.@)
758 * FIXME
759 * Check that SetLastError is being called correctly
761 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
763 BOOL ret = FALSE;
764 DC *dc = get_dc_ptr( hdc );
766 if (dc)
768 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectClipPath );
769 ret = physdev->funcs->pSelectClipPath( physdev, iMode );
770 release_dc_ptr( dc );
772 return ret;
776 /***********************************************************************
777 * pathdrv_BeginPath
779 static BOOL pathdrv_BeginPath( PHYSDEV dev )
781 /* path already open, nothing to do */
782 return TRUE;
786 /***********************************************************************
787 * pathdrv_AbortPath
789 static BOOL pathdrv_AbortPath( PHYSDEV dev )
791 DC *dc = get_dc_ptr( dev->hdc );
793 if (!dc) return FALSE;
794 PATH_EmptyPath( &dc->path );
795 pop_path_driver( dc );
796 release_dc_ptr( dc );
797 return TRUE;
801 /***********************************************************************
802 * pathdrv_EndPath
804 static BOOL pathdrv_EndPath( PHYSDEV dev )
806 DC *dc = get_dc_ptr( dev->hdc );
808 if (!dc) return FALSE;
809 dc->path.state = PATH_Closed;
810 pop_path_driver( dc );
811 release_dc_ptr( dc );
812 return TRUE;
816 /***********************************************************************
817 * pathdrv_CreateDC
819 static BOOL pathdrv_CreateDC( PHYSDEV *dev, LPCWSTR driver, LPCWSTR device,
820 LPCWSTR output, const DEVMODEW *devmode )
822 struct path_physdev *physdev = HeapAlloc( GetProcessHeap(), 0, sizeof(*physdev) );
823 DC *dc;
825 if (!physdev) return FALSE;
826 dc = get_dc_ptr( (*dev)->hdc );
827 physdev->path = &dc->path;
828 push_dc_driver( dev, &physdev->dev, &path_driver );
829 release_dc_ptr( dc );
830 return TRUE;
834 /*************************************************************
835 * pathdrv_DeleteDC
837 static BOOL pathdrv_DeleteDC( PHYSDEV dev )
839 assert( 0 ); /* should never be called */
840 return TRUE;
844 /* PATH_InitGdiPath
846 * Initializes the GdiPath structure.
848 void PATH_InitGdiPath(GdiPath *pPath)
850 assert(pPath!=NULL);
852 pPath->state=PATH_Null;
853 pPath->pPoints=NULL;
854 pPath->pFlags=NULL;
855 pPath->numEntriesUsed=0;
856 pPath->numEntriesAllocated=0;
859 /* PATH_DestroyGdiPath
861 * Destroys a GdiPath structure (frees the memory in the arrays).
863 void PATH_DestroyGdiPath(GdiPath *pPath)
865 assert(pPath!=NULL);
867 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
868 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
871 BOOL PATH_SavePath( DC *dst, DC *src )
873 PATH_InitGdiPath( &dst->path );
874 return PATH_AssignGdiPath( &dst->path, &src->path );
877 BOOL PATH_RestorePath( DC *dst, DC *src )
879 BOOL ret;
881 if (src->path.state == PATH_Open && dst->path.state != PATH_Open)
883 if (!path_driver.pCreateDC( &dst->physDev, NULL, NULL, NULL, NULL )) return FALSE;
884 ret = PATH_AssignGdiPath( &dst->path, &src->path );
885 if (!ret) pop_path_driver( dst );
887 else if (src->path.state != PATH_Open && dst->path.state == PATH_Open)
889 ret = PATH_AssignGdiPath( &dst->path, &src->path );
890 if (ret) pop_path_driver( dst );
892 else ret = PATH_AssignGdiPath( &dst->path, &src->path );
893 return ret;
897 /*************************************************************
898 * pathdrv_MoveTo
900 static BOOL pathdrv_MoveTo( PHYSDEV dev, INT x, INT y )
902 struct path_physdev *physdev = get_path_physdev( dev );
903 physdev->path->newStroke = TRUE;
904 return TRUE;
908 /*************************************************************
909 * pathdrv_LineTo
911 static BOOL pathdrv_LineTo( PHYSDEV dev, INT x, INT y )
913 struct path_physdev *physdev = get_path_physdev( dev );
914 POINT point;
916 if (!start_new_stroke( physdev )) return FALSE;
917 point.x = x;
918 point.y = y;
919 return add_log_points( physdev, &point, 1, PT_LINETO ) != NULL;
923 /*************************************************************
924 * pathdrv_RoundRect
926 * FIXME: it adds the same entries to the path as windows does, but there
927 * is an error in the bezier drawing code so that there are small pixel-size
928 * gaps when the resulting path is drawn by StrokePath()
930 static BOOL pathdrv_RoundRect( PHYSDEV dev, INT x1, INT y1, INT x2, INT y2, INT ell_width, INT ell_height )
932 struct path_physdev *physdev = get_path_physdev( dev );
933 POINT corners[2], pointTemp;
934 FLOAT_POINT ellCorners[2];
936 PATH_CheckCorners(dev->hdc,corners,x1,y1,x2,y2);
938 /* Add points to the roundrect path */
939 ellCorners[0].x = corners[1].x-ell_width;
940 ellCorners[0].y = corners[0].y;
941 ellCorners[1].x = corners[1].x;
942 ellCorners[1].y = corners[0].y+ell_height;
943 if(!PATH_DoArcPart(physdev->path, ellCorners, 0, -M_PI_2, PT_MOVETO))
944 return FALSE;
945 pointTemp.x = corners[0].x+ell_width/2;
946 pointTemp.y = corners[0].y;
947 if(!PATH_AddEntry(physdev->path, &pointTemp, PT_LINETO))
948 return FALSE;
949 ellCorners[0].x = corners[0].x;
950 ellCorners[1].x = corners[0].x+ell_width;
951 if(!PATH_DoArcPart(physdev->path, ellCorners, -M_PI_2, -M_PI, FALSE))
952 return FALSE;
953 pointTemp.x = corners[0].x;
954 pointTemp.y = corners[1].y-ell_height/2;
955 if(!PATH_AddEntry(physdev->path, &pointTemp, PT_LINETO))
956 return FALSE;
957 ellCorners[0].y = corners[1].y-ell_height;
958 ellCorners[1].y = corners[1].y;
959 if(!PATH_DoArcPart(physdev->path, ellCorners, M_PI, M_PI_2, FALSE))
960 return FALSE;
961 pointTemp.x = corners[1].x-ell_width/2;
962 pointTemp.y = corners[1].y;
963 if(!PATH_AddEntry(physdev->path, &pointTemp, PT_LINETO))
964 return FALSE;
965 ellCorners[0].x = corners[1].x-ell_width;
966 ellCorners[1].x = corners[1].x;
967 if(!PATH_DoArcPart(physdev->path, ellCorners, M_PI_2, 0, FALSE))
968 return FALSE;
970 /* Close the roundrect figure */
971 return CloseFigure( dev->hdc );
975 /*************************************************************
976 * pathdrv_Rectangle
978 static BOOL pathdrv_Rectangle( PHYSDEV dev, INT x1, INT y1, INT x2, INT y2 )
980 struct path_physdev *physdev = get_path_physdev( dev );
981 POINT corners[2], pointTemp;
983 PATH_CheckCorners(dev->hdc,corners,x1,y1,x2,y2);
985 /* Add four points to the path */
986 pointTemp.x=corners[1].x;
987 pointTemp.y=corners[0].y;
988 if(!PATH_AddEntry(physdev->path, &pointTemp, PT_MOVETO))
989 return FALSE;
990 if(!PATH_AddEntry(physdev->path, corners, PT_LINETO))
991 return FALSE;
992 pointTemp.x=corners[0].x;
993 pointTemp.y=corners[1].y;
994 if(!PATH_AddEntry(physdev->path, &pointTemp, PT_LINETO))
995 return FALSE;
996 if(!PATH_AddEntry(physdev->path, corners+1, PT_LINETO))
997 return FALSE;
999 /* Close the rectangle figure */
1000 return CloseFigure( dev->hdc );
1004 /* PATH_Arc
1006 * Should be called when a call to Arc is performed on a DC that has
1007 * an open path. This adds up to five Bezier splines representing the arc
1008 * to the path. When 'lines' is 1, we add 1 extra line to get a chord,
1009 * when 'lines' is 2, we add 2 extra lines to get a pie, and when 'lines' is
1010 * -1 we add 1 extra line from the current DC position to the starting position
1011 * of the arc before drawing the arc itself (arcto). Returns TRUE if successful,
1012 * else FALSE.
1014 static BOOL PATH_Arc( PHYSDEV dev, INT x1, INT y1, INT x2, INT y2,
1015 INT xStart, INT yStart, INT xEnd, INT yEnd, INT lines )
1017 struct path_physdev *physdev = get_path_physdev( dev );
1018 double angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
1019 /* Initialize angleEndQuadrant to silence gcc's warning */
1020 double x, y;
1021 FLOAT_POINT corners[2], pointStart, pointEnd;
1022 POINT centre;
1023 BOOL start, end;
1024 INT temp, direction = GetArcDirection(dev->hdc);
1026 /* FIXME: Do we have to respect newStroke? */
1028 /* Check for zero height / width */
1029 /* FIXME: Only in GM_COMPATIBLE? */
1030 if(x1==x2 || y1==y2)
1031 return TRUE;
1033 /* Convert points to device coordinates */
1034 corners[0].x = x1;
1035 corners[0].y = y1;
1036 corners[1].x = x2;
1037 corners[1].y = y2;
1038 pointStart.x = xStart;
1039 pointStart.y = yStart;
1040 pointEnd.x = xEnd;
1041 pointEnd.y = yEnd;
1042 INTERNAL_LPTODP_FLOAT(dev->hdc, corners, 2);
1043 INTERNAL_LPTODP_FLOAT(dev->hdc, &pointStart, 1);
1044 INTERNAL_LPTODP_FLOAT(dev->hdc, &pointEnd, 1);
1046 /* Make sure first corner is top left and second corner is bottom right */
1047 if(corners[0].x>corners[1].x)
1049 temp=corners[0].x;
1050 corners[0].x=corners[1].x;
1051 corners[1].x=temp;
1053 if(corners[0].y>corners[1].y)
1055 temp=corners[0].y;
1056 corners[0].y=corners[1].y;
1057 corners[1].y=temp;
1060 /* Compute start and end angle */
1061 PATH_NormalizePoint(corners, &pointStart, &x, &y);
1062 angleStart=atan2(y, x);
1063 PATH_NormalizePoint(corners, &pointEnd, &x, &y);
1064 angleEnd=atan2(y, x);
1066 /* Make sure the end angle is "on the right side" of the start angle */
1067 if (direction == AD_CLOCKWISE)
1069 if(angleEnd<=angleStart)
1071 angleEnd+=2*M_PI;
1072 assert(angleEnd>=angleStart);
1075 else
1077 if(angleEnd>=angleStart)
1079 angleEnd-=2*M_PI;
1080 assert(angleEnd<=angleStart);
1084 /* In GM_COMPATIBLE, don't include bottom and right edges */
1085 if (GetGraphicsMode(dev->hdc) == GM_COMPATIBLE)
1087 corners[1].x--;
1088 corners[1].y--;
1091 /* arcto: Add a PT_MOVETO only if this is the first entry in a stroke */
1092 if (lines==-1 && !start_new_stroke( physdev )) return FALSE;
1094 /* Add the arc to the path with one Bezier spline per quadrant that the
1095 * arc spans */
1096 start=TRUE;
1097 end=FALSE;
1100 /* Determine the start and end angles for this quadrant */
1101 if(start)
1103 angleStartQuadrant=angleStart;
1104 if (direction == AD_CLOCKWISE)
1105 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
1106 else
1107 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
1109 else
1111 angleStartQuadrant=angleEndQuadrant;
1112 if (direction == AD_CLOCKWISE)
1113 angleEndQuadrant+=M_PI_2;
1114 else
1115 angleEndQuadrant-=M_PI_2;
1118 /* Have we reached the last part of the arc? */
1119 if((direction == AD_CLOCKWISE && angleEnd<angleEndQuadrant) ||
1120 (direction == AD_COUNTERCLOCKWISE && angleEnd>angleEndQuadrant))
1122 /* Adjust the end angle for this quadrant */
1123 angleEndQuadrant=angleEnd;
1124 end=TRUE;
1127 /* Add the Bezier spline to the path */
1128 PATH_DoArcPart(physdev->path, corners, angleStartQuadrant, angleEndQuadrant,
1129 start ? (lines==-1 ? PT_LINETO : PT_MOVETO) : FALSE);
1130 start=FALSE;
1131 } while(!end);
1133 /* chord: close figure. pie: add line and close figure */
1134 if(lines==1)
1136 return CloseFigure(dev->hdc);
1138 else if(lines==2)
1140 centre.x = (corners[0].x+corners[1].x)/2;
1141 centre.y = (corners[0].y+corners[1].y)/2;
1142 if(!PATH_AddEntry(physdev->path, &centre, PT_LINETO | PT_CLOSEFIGURE))
1143 return FALSE;
1146 return TRUE;
1150 /*************************************************************
1151 * pathdrv_AngleArc
1153 static BOOL pathdrv_AngleArc( PHYSDEV dev, INT x, INT y, DWORD radius, FLOAT eStartAngle, FLOAT eSweepAngle)
1155 INT x1, y1, x2, y2, arcdir;
1156 BOOL ret;
1158 x1 = GDI_ROUND( x + cos(eStartAngle*M_PI/180) * radius );
1159 y1 = GDI_ROUND( y - sin(eStartAngle*M_PI/180) * radius );
1160 x2 = GDI_ROUND( x + cos((eStartAngle+eSweepAngle)*M_PI/180) * radius );
1161 y2 = GDI_ROUND( y - sin((eStartAngle+eSweepAngle)*M_PI/180) * radius );
1162 arcdir = SetArcDirection( dev->hdc, eSweepAngle >= 0 ? AD_COUNTERCLOCKWISE : AD_CLOCKWISE);
1163 ret = PATH_Arc( dev, x-radius, y-radius, x+radius, y+radius, x1, y1, x2, y2, -1 );
1164 SetArcDirection( dev->hdc, arcdir );
1165 return ret;
1169 /*************************************************************
1170 * pathdrv_Arc
1172 static BOOL pathdrv_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
1173 INT xstart, INT ystart, INT xend, INT yend )
1175 return PATH_Arc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
1179 /*************************************************************
1180 * pathdrv_ArcTo
1182 static BOOL pathdrv_ArcTo( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
1183 INT xstart, INT ystart, INT xend, INT yend )
1185 return PATH_Arc( dev, left, top, right, bottom, xstart, ystart, xend, yend, -1 );
1189 /*************************************************************
1190 * pathdrv_Chord
1192 static BOOL pathdrv_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
1193 INT xstart, INT ystart, INT xend, INT yend )
1195 return PATH_Arc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 1);
1199 /*************************************************************
1200 * pathdrv_Pie
1202 static BOOL pathdrv_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
1203 INT xstart, INT ystart, INT xend, INT yend )
1205 return PATH_Arc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
1209 /*************************************************************
1210 * pathdrv_Ellipse
1212 static BOOL pathdrv_Ellipse( PHYSDEV dev, INT x1, INT y1, INT x2, INT y2 )
1214 return PATH_Arc( dev, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2, 0 ) && CloseFigure( dev->hdc );
1218 /*************************************************************
1219 * pathdrv_PolyBezierTo
1221 static BOOL pathdrv_PolyBezierTo( PHYSDEV dev, const POINT *pts, DWORD cbPoints )
1223 struct path_physdev *physdev = get_path_physdev( dev );
1225 if (!start_new_stroke( physdev )) return FALSE;
1226 return add_log_points( physdev, pts, cbPoints, PT_BEZIERTO ) != NULL;
1230 /*************************************************************
1231 * pathdrv_PolyBezier
1233 static BOOL pathdrv_PolyBezier( PHYSDEV dev, const POINT *pts, DWORD cbPoints )
1235 struct path_physdev *physdev = get_path_physdev( dev );
1236 BYTE *type = add_log_points( physdev, pts, cbPoints, PT_BEZIERTO );
1238 if (!type) return FALSE;
1239 type[0] = PT_MOVETO;
1240 return TRUE;
1244 /*************************************************************
1245 * pathdrv_PolyDraw
1247 static BOOL pathdrv_PolyDraw( PHYSDEV dev, const POINT *pts, const BYTE *types, DWORD cbPoints )
1249 struct path_physdev *physdev = get_path_physdev( dev );
1250 POINT lastmove, orig_pos;
1251 INT i;
1253 GetCurrentPositionEx( dev->hdc, &orig_pos );
1254 lastmove = orig_pos;
1256 for(i = physdev->path->numEntriesUsed - 1; i >= 0; i--){
1257 if(physdev->path->pFlags[i] == PT_MOVETO){
1258 lastmove = physdev->path->pPoints[i];
1259 DPtoLP(dev->hdc, &lastmove, 1);
1260 break;
1264 for(i = 0; i < cbPoints; i++)
1266 switch (types[i])
1268 case PT_MOVETO:
1269 MoveToEx( dev->hdc, pts[i].x, pts[i].y, NULL );
1270 break;
1271 case PT_LINETO:
1272 case PT_LINETO | PT_CLOSEFIGURE:
1273 LineTo( dev->hdc, pts[i].x, pts[i].y );
1274 break;
1275 case PT_BEZIERTO:
1276 if ((i + 2 < cbPoints) && (types[i + 1] == PT_BEZIERTO) &&
1277 (types[i + 2] & ~PT_CLOSEFIGURE) == PT_BEZIERTO)
1279 PolyBezierTo( dev->hdc, &pts[i], 3 );
1280 i += 2;
1281 break;
1283 /* fall through */
1284 default:
1285 if (i) /* restore original position */
1287 if (!(types[i - 1] & PT_CLOSEFIGURE)) lastmove = pts[i - 1];
1288 if (lastmove.x != orig_pos.x || lastmove.y != orig_pos.y)
1289 MoveToEx( dev->hdc, orig_pos.x, orig_pos.y, NULL );
1291 return FALSE;
1294 if(types[i] & PT_CLOSEFIGURE){
1295 physdev->path->pFlags[physdev->path->numEntriesUsed-1] |= PT_CLOSEFIGURE;
1296 MoveToEx( dev->hdc, lastmove.x, lastmove.y, NULL );
1300 return TRUE;
1304 /*************************************************************
1305 * pathdrv_Polyline
1307 static BOOL pathdrv_Polyline( PHYSDEV dev, const POINT *pts, INT cbPoints )
1309 struct path_physdev *physdev = get_path_physdev( dev );
1310 BYTE *type = add_log_points( physdev, pts, cbPoints, PT_LINETO );
1312 if (!type) return FALSE;
1313 if (cbPoints) type[0] = PT_MOVETO;
1314 return TRUE;
1318 /*************************************************************
1319 * pathdrv_PolylineTo
1321 static BOOL pathdrv_PolylineTo( PHYSDEV dev, const POINT *pts, INT cbPoints )
1323 struct path_physdev *physdev = get_path_physdev( dev );
1325 if (!start_new_stroke( physdev )) return FALSE;
1326 return add_log_points( physdev, pts, cbPoints, PT_LINETO ) != NULL;
1330 /*************************************************************
1331 * pathdrv_Polygon
1333 static BOOL pathdrv_Polygon( PHYSDEV dev, const POINT *pts, INT cbPoints )
1335 struct path_physdev *physdev = get_path_physdev( dev );
1336 BYTE *type = add_log_points( physdev, pts, cbPoints, PT_LINETO );
1338 if (!type) return FALSE;
1339 if (cbPoints) type[0] = PT_MOVETO;
1340 if (cbPoints > 1) type[cbPoints - 1] = PT_LINETO | PT_CLOSEFIGURE;
1341 return TRUE;
1345 /*************************************************************
1346 * pathdrv_PolyPolygon
1348 static BOOL pathdrv_PolyPolygon( PHYSDEV dev, const POINT* pts, const INT* counts, UINT polygons )
1350 struct path_physdev *physdev = get_path_physdev( dev );
1351 UINT poly;
1352 BYTE *type;
1354 for(poly = 0; poly < polygons; poly++) {
1355 type = add_log_points( physdev, pts, counts[poly], PT_LINETO );
1356 if (!type) return FALSE;
1357 type[0] = PT_MOVETO;
1358 /* win98 adds an extra line to close the figure for some reason */
1359 add_log_points( physdev, pts, 1, PT_LINETO | PT_CLOSEFIGURE );
1360 pts += counts[poly];
1362 return TRUE;
1366 /*************************************************************
1367 * pathdrv_PolyPolyline
1369 static BOOL pathdrv_PolyPolyline( PHYSDEV dev, const POINT* pts, const DWORD* counts, DWORD polylines )
1371 struct path_physdev *physdev = get_path_physdev( dev );
1372 UINT poly, count;
1373 BYTE *type;
1375 for (poly = count = 0; poly < polylines; poly++) count += counts[poly];
1377 type = add_log_points( physdev, pts, count, PT_LINETO );
1378 if (!type) return FALSE;
1380 /* make the first point of each polyline a PT_MOVETO */
1381 for (poly = 0; poly < polylines; poly++, type += counts[poly]) *type = PT_MOVETO;
1382 return TRUE;
1386 /**********************************************************************
1387 * PATH_BezierTo
1389 * internally used by PATH_add_outline
1391 static void PATH_BezierTo(GdiPath *pPath, POINT *lppt, INT n)
1393 if (n < 2) return;
1395 if (n == 2)
1397 PATH_AddEntry(pPath, &lppt[1], PT_LINETO);
1399 else if (n == 3)
1401 PATH_AddEntry(pPath, &lppt[0], PT_BEZIERTO);
1402 PATH_AddEntry(pPath, &lppt[1], PT_BEZIERTO);
1403 PATH_AddEntry(pPath, &lppt[2], PT_BEZIERTO);
1405 else
1407 POINT pt[3];
1408 INT i = 0;
1410 pt[2] = lppt[0];
1411 n--;
1413 while (n > 2)
1415 pt[0] = pt[2];
1416 pt[1] = lppt[i+1];
1417 pt[2].x = (lppt[i+2].x + lppt[i+1].x) / 2;
1418 pt[2].y = (lppt[i+2].y + lppt[i+1].y) / 2;
1419 PATH_BezierTo(pPath, pt, 3);
1420 n--;
1421 i++;
1424 pt[0] = pt[2];
1425 pt[1] = lppt[i+1];
1426 pt[2] = lppt[i+2];
1427 PATH_BezierTo(pPath, pt, 3);
1431 static BOOL PATH_add_outline(struct path_physdev *physdev, INT x, INT y,
1432 TTPOLYGONHEADER *header, DWORD size)
1434 TTPOLYGONHEADER *start;
1435 POINT pt;
1437 start = header;
1439 while ((char *)header < (char *)start + size)
1441 TTPOLYCURVE *curve;
1443 if (header->dwType != TT_POLYGON_TYPE)
1445 FIXME("Unknown header type %d\n", header->dwType);
1446 return FALSE;
1449 pt.x = x + int_from_fixed(header->pfxStart.x);
1450 pt.y = y - int_from_fixed(header->pfxStart.y);
1451 PATH_AddEntry(physdev->path, &pt, PT_MOVETO);
1453 curve = (TTPOLYCURVE *)(header + 1);
1455 while ((char *)curve < (char *)header + header->cb)
1457 /*TRACE("curve->wType %d\n", curve->wType);*/
1459 switch(curve->wType)
1461 case TT_PRIM_LINE:
1463 WORD i;
1465 for (i = 0; i < curve->cpfx; i++)
1467 pt.x = x + int_from_fixed(curve->apfx[i].x);
1468 pt.y = y - int_from_fixed(curve->apfx[i].y);
1469 PATH_AddEntry(physdev->path, &pt, PT_LINETO);
1471 break;
1474 case TT_PRIM_QSPLINE:
1475 case TT_PRIM_CSPLINE:
1477 WORD i;
1478 POINTFX ptfx;
1479 POINT *pts = HeapAlloc(GetProcessHeap(), 0, (curve->cpfx + 1) * sizeof(POINT));
1481 if (!pts) return FALSE;
1483 ptfx = *(POINTFX *)((char *)curve - sizeof(POINTFX));
1485 pts[0].x = x + int_from_fixed(ptfx.x);
1486 pts[0].y = y - int_from_fixed(ptfx.y);
1488 for(i = 0; i < curve->cpfx; i++)
1490 pts[i + 1].x = x + int_from_fixed(curve->apfx[i].x);
1491 pts[i + 1].y = y - int_from_fixed(curve->apfx[i].y);
1494 PATH_BezierTo(physdev->path, pts, curve->cpfx + 1);
1496 HeapFree(GetProcessHeap(), 0, pts);
1497 break;
1500 default:
1501 FIXME("Unknown curve type %04x\n", curve->wType);
1502 return FALSE;
1505 curve = (TTPOLYCURVE *)&curve->apfx[curve->cpfx];
1508 header = (TTPOLYGONHEADER *)((char *)header + header->cb);
1511 return CloseFigure(physdev->dev.hdc);
1514 /*************************************************************
1515 * pathdrv_ExtTextOut
1517 static BOOL pathdrv_ExtTextOut( PHYSDEV dev, INT x, INT y, UINT flags, const RECT *lprc,
1518 LPCWSTR str, UINT count, const INT *dx )
1520 struct path_physdev *physdev = get_path_physdev( dev );
1521 unsigned int idx;
1522 POINT offset = {0, 0};
1524 if (!count) return TRUE;
1526 for (idx = 0; idx < count; idx++)
1528 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
1529 GLYPHMETRICS gm;
1530 DWORD dwSize;
1531 void *outline;
1533 dwSize = GetGlyphOutlineW(dev->hdc, str[idx], GGO_GLYPH_INDEX | GGO_NATIVE,
1534 &gm, 0, NULL, &identity);
1535 if (dwSize == GDI_ERROR) return FALSE;
1537 /* add outline only if char is printable */
1538 if(dwSize)
1540 outline = HeapAlloc(GetProcessHeap(), 0, dwSize);
1541 if (!outline) return FALSE;
1543 GetGlyphOutlineW(dev->hdc, str[idx], GGO_GLYPH_INDEX | GGO_NATIVE,
1544 &gm, dwSize, outline, &identity);
1546 PATH_add_outline(physdev, x + offset.x, y + offset.y, outline, dwSize);
1548 HeapFree(GetProcessHeap(), 0, outline);
1551 if (dx)
1553 if(flags & ETO_PDY)
1555 offset.x += dx[idx * 2];
1556 offset.y += dx[idx * 2 + 1];
1558 else
1559 offset.x += dx[idx];
1561 else
1563 offset.x += gm.gmCellIncX;
1564 offset.y += gm.gmCellIncY;
1567 return TRUE;
1571 /*************************************************************
1572 * pathdrv_CloseFigure
1574 static BOOL pathdrv_CloseFigure( PHYSDEV dev )
1576 struct path_physdev *physdev = get_path_physdev( dev );
1578 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
1579 /* It is not necessary to draw a line, PT_CLOSEFIGURE is a virtual closing line itself */
1580 if (physdev->path->numEntriesUsed)
1581 physdev->path->pFlags[physdev->path->numEntriesUsed - 1] |= PT_CLOSEFIGURE;
1582 return TRUE;
1586 /*******************************************************************
1587 * FlattenPath [GDI32.@]
1591 BOOL WINAPI FlattenPath(HDC hdc)
1593 BOOL ret = FALSE;
1594 DC *dc = get_dc_ptr( hdc );
1596 if (dc)
1598 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pFlattenPath );
1599 ret = physdev->funcs->pFlattenPath( physdev );
1600 release_dc_ptr( dc );
1602 return ret;
1606 static BOOL PATH_StrokePath( HDC hdc, GdiPath *pPath )
1608 INT i, nLinePts, nAlloc;
1609 POINT *pLinePts;
1610 POINT ptViewportOrg, ptWindowOrg;
1611 SIZE szViewportExt, szWindowExt;
1612 DWORD mapMode, graphicsMode;
1613 XFORM xform;
1614 BOOL ret = TRUE;
1616 /* Save the mapping mode info */
1617 mapMode=GetMapMode(hdc);
1618 GetViewportExtEx(hdc, &szViewportExt);
1619 GetViewportOrgEx(hdc, &ptViewportOrg);
1620 GetWindowExtEx(hdc, &szWindowExt);
1621 GetWindowOrgEx(hdc, &ptWindowOrg);
1622 GetWorldTransform(hdc, &xform);
1624 /* Set MM_TEXT */
1625 SetMapMode(hdc, MM_TEXT);
1626 SetViewportOrgEx(hdc, 0, 0, NULL);
1627 SetWindowOrgEx(hdc, 0, 0, NULL);
1628 graphicsMode=GetGraphicsMode(hdc);
1629 SetGraphicsMode(hdc, GM_ADVANCED);
1630 ModifyWorldTransform(hdc, &xform, MWT_IDENTITY);
1631 SetGraphicsMode(hdc, graphicsMode);
1633 /* Allocate enough memory for the worst case without beziers (one PT_MOVETO
1634 * and the rest PT_LINETO with PT_CLOSEFIGURE at the end) plus some buffer
1635 * space in case we get one to keep the number of reallocations small. */
1636 nAlloc = pPath->numEntriesUsed + 1 + 300;
1637 pLinePts = HeapAlloc(GetProcessHeap(), 0, nAlloc * sizeof(POINT));
1638 nLinePts = 0;
1640 for(i = 0; i < pPath->numEntriesUsed; i++) {
1641 if((i == 0 || (pPath->pFlags[i-1] & PT_CLOSEFIGURE)) &&
1642 (pPath->pFlags[i] != PT_MOVETO)) {
1643 ERR("Expected PT_MOVETO %s, got path flag %d\n",
1644 i == 0 ? "as first point" : "after PT_CLOSEFIGURE",
1645 (INT)pPath->pFlags[i]);
1646 ret = FALSE;
1647 goto end;
1649 switch(pPath->pFlags[i]) {
1650 case PT_MOVETO:
1651 TRACE("Got PT_MOVETO (%d, %d)\n",
1652 pPath->pPoints[i].x, pPath->pPoints[i].y);
1653 if(nLinePts >= 2)
1654 Polyline(hdc, pLinePts, nLinePts);
1655 nLinePts = 0;
1656 pLinePts[nLinePts++] = pPath->pPoints[i];
1657 break;
1658 case PT_LINETO:
1659 case (PT_LINETO | PT_CLOSEFIGURE):
1660 TRACE("Got PT_LINETO (%d, %d)\n",
1661 pPath->pPoints[i].x, pPath->pPoints[i].y);
1662 pLinePts[nLinePts++] = pPath->pPoints[i];
1663 break;
1664 case PT_BEZIERTO:
1665 TRACE("Got PT_BEZIERTO\n");
1666 if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1667 (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1668 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1669 ret = FALSE;
1670 goto end;
1671 } else {
1672 INT nBzrPts, nMinAlloc;
1673 POINT *pBzrPts = GDI_Bezier(&pPath->pPoints[i-1], 4, &nBzrPts);
1674 /* Make sure we have allocated enough memory for the lines of
1675 * this bezier and the rest of the path, assuming we won't get
1676 * another one (since we won't reallocate again then). */
1677 nMinAlloc = nLinePts + (pPath->numEntriesUsed - i) + nBzrPts;
1678 if(nAlloc < nMinAlloc)
1680 nAlloc = nMinAlloc * 2;
1681 pLinePts = HeapReAlloc(GetProcessHeap(), 0, pLinePts,
1682 nAlloc * sizeof(POINT));
1684 memcpy(&pLinePts[nLinePts], &pBzrPts[1],
1685 (nBzrPts - 1) * sizeof(POINT));
1686 nLinePts += nBzrPts - 1;
1687 HeapFree(GetProcessHeap(), 0, pBzrPts);
1688 i += 2;
1690 break;
1691 default:
1692 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1693 ret = FALSE;
1694 goto end;
1696 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1697 pLinePts[nLinePts++] = pLinePts[0];
1699 if(nLinePts >= 2)
1700 Polyline(hdc, pLinePts, nLinePts);
1702 end:
1703 HeapFree(GetProcessHeap(), 0, pLinePts);
1705 /* Restore the old mapping mode */
1706 SetMapMode(hdc, mapMode);
1707 SetWindowExtEx(hdc, szWindowExt.cx, szWindowExt.cy, NULL);
1708 SetWindowOrgEx(hdc, ptWindowOrg.x, ptWindowOrg.y, NULL);
1709 SetViewportExtEx(hdc, szViewportExt.cx, szViewportExt.cy, NULL);
1710 SetViewportOrgEx(hdc, ptViewportOrg.x, ptViewportOrg.y, NULL);
1712 /* Go to GM_ADVANCED temporarily to restore the world transform */
1713 graphicsMode=GetGraphicsMode(hdc);
1714 SetGraphicsMode(hdc, GM_ADVANCED);
1715 SetWorldTransform(hdc, &xform);
1716 SetGraphicsMode(hdc, graphicsMode);
1718 /* If we've moved the current point then get its new position
1719 which will be in device (MM_TEXT) co-ords, convert it to
1720 logical co-ords and re-set it. This basically updates
1721 dc->CurPosX|Y so that their values are in the correct mapping
1722 mode.
1724 if(i > 0) {
1725 POINT pt;
1726 GetCurrentPositionEx(hdc, &pt);
1727 DPtoLP(hdc, &pt, 1);
1728 MoveToEx(hdc, pt.x, pt.y, NULL);
1731 return ret;
1734 #define round(x) ((int)((x)>0?(x)+0.5:(x)-0.5))
1736 static BOOL PATH_WidenPath(DC *dc)
1738 INT i, j, numStrokes, penWidth, penWidthIn, penWidthOut, size, penStyle;
1739 BOOL ret = FALSE;
1740 GdiPath *pPath, *pNewPath, **pStrokes = NULL, *pUpPath, *pDownPath;
1741 EXTLOGPEN *elp;
1742 DWORD obj_type, joint, endcap, penType;
1744 pPath = &dc->path;
1746 PATH_FlattenPath(pPath);
1748 size = GetObjectW( dc->hPen, 0, NULL );
1749 if (!size) {
1750 SetLastError(ERROR_CAN_NOT_COMPLETE);
1751 return FALSE;
1754 elp = HeapAlloc( GetProcessHeap(), 0, size );
1755 GetObjectW( dc->hPen, size, elp );
1757 obj_type = GetObjectType(dc->hPen);
1758 if(obj_type == OBJ_PEN) {
1759 penStyle = ((LOGPEN*)elp)->lopnStyle;
1761 else if(obj_type == OBJ_EXTPEN) {
1762 penStyle = elp->elpPenStyle;
1764 else {
1765 SetLastError(ERROR_CAN_NOT_COMPLETE);
1766 HeapFree( GetProcessHeap(), 0, elp );
1767 return FALSE;
1770 penWidth = elp->elpWidth;
1771 HeapFree( GetProcessHeap(), 0, elp );
1773 endcap = (PS_ENDCAP_MASK & penStyle);
1774 joint = (PS_JOIN_MASK & penStyle);
1775 penType = (PS_TYPE_MASK & penStyle);
1777 /* The function cannot apply to cosmetic pens */
1778 if(obj_type == OBJ_EXTPEN && penType == PS_COSMETIC) {
1779 SetLastError(ERROR_CAN_NOT_COMPLETE);
1780 return FALSE;
1783 penWidthIn = penWidth / 2;
1784 penWidthOut = penWidth / 2;
1785 if(penWidthIn + penWidthOut < penWidth)
1786 penWidthOut++;
1788 numStrokes = 0;
1790 for(i = 0, j = 0; i < pPath->numEntriesUsed; i++, j++) {
1791 POINT point;
1792 if((i == 0 || (pPath->pFlags[i-1] & PT_CLOSEFIGURE)) &&
1793 (pPath->pFlags[i] != PT_MOVETO)) {
1794 ERR("Expected PT_MOVETO %s, got path flag %c\n",
1795 i == 0 ? "as first point" : "after PT_CLOSEFIGURE",
1796 pPath->pFlags[i]);
1797 return FALSE;
1799 switch(pPath->pFlags[i]) {
1800 case PT_MOVETO:
1801 if(numStrokes > 0) {
1802 pStrokes[numStrokes - 1]->state = PATH_Closed;
1804 numStrokes++;
1805 j = 0;
1806 if(numStrokes == 1)
1807 pStrokes = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath*));
1808 else
1809 pStrokes = HeapReAlloc(GetProcessHeap(), 0, pStrokes, numStrokes * sizeof(GdiPath*));
1810 if(!pStrokes) return FALSE;
1811 pStrokes[numStrokes - 1] = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1812 PATH_InitGdiPath(pStrokes[numStrokes - 1]);
1813 pStrokes[numStrokes - 1]->state = PATH_Open;
1814 /* fall through */
1815 case PT_LINETO:
1816 case (PT_LINETO | PT_CLOSEFIGURE):
1817 point.x = pPath->pPoints[i].x;
1818 point.y = pPath->pPoints[i].y;
1819 PATH_AddEntry(pStrokes[numStrokes - 1], &point, pPath->pFlags[i]);
1820 break;
1821 case PT_BEZIERTO:
1822 /* should never happen because of the FlattenPath call */
1823 ERR("Should never happen\n");
1824 break;
1825 default:
1826 ERR("Got path flag %c\n", pPath->pFlags[i]);
1827 return FALSE;
1831 pNewPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1832 PATH_InitGdiPath(pNewPath);
1833 pNewPath->state = PATH_Open;
1835 for(i = 0; i < numStrokes; i++) {
1836 pUpPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1837 PATH_InitGdiPath(pUpPath);
1838 pUpPath->state = PATH_Open;
1839 pDownPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1840 PATH_InitGdiPath(pDownPath);
1841 pDownPath->state = PATH_Open;
1843 for(j = 0; j < pStrokes[i]->numEntriesUsed; j++) {
1844 /* Beginning or end of the path if not closed */
1845 if((!(pStrokes[i]->pFlags[pStrokes[i]->numEntriesUsed - 1] & PT_CLOSEFIGURE)) && (j == 0 || j == pStrokes[i]->numEntriesUsed - 1) ) {
1846 /* Compute segment angle */
1847 double xo, yo, xa, ya, theta;
1848 POINT pt;
1849 FLOAT_POINT corners[2];
1850 if(j == 0) {
1851 xo = pStrokes[i]->pPoints[j].x;
1852 yo = pStrokes[i]->pPoints[j].y;
1853 xa = pStrokes[i]->pPoints[1].x;
1854 ya = pStrokes[i]->pPoints[1].y;
1856 else {
1857 xa = pStrokes[i]->pPoints[j - 1].x;
1858 ya = pStrokes[i]->pPoints[j - 1].y;
1859 xo = pStrokes[i]->pPoints[j].x;
1860 yo = pStrokes[i]->pPoints[j].y;
1862 theta = atan2( ya - yo, xa - xo );
1863 switch(endcap) {
1864 case PS_ENDCAP_SQUARE :
1865 pt.x = xo + round(sqrt(2) * penWidthOut * cos(M_PI_4 + theta));
1866 pt.y = yo + round(sqrt(2) * penWidthOut * sin(M_PI_4 + theta));
1867 PATH_AddEntry(pUpPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO) );
1868 pt.x = xo + round(sqrt(2) * penWidthIn * cos(- M_PI_4 + theta));
1869 pt.y = yo + round(sqrt(2) * penWidthIn * sin(- M_PI_4 + theta));
1870 PATH_AddEntry(pUpPath, &pt, PT_LINETO);
1871 break;
1872 case PS_ENDCAP_FLAT :
1873 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
1874 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
1875 PATH_AddEntry(pUpPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO));
1876 pt.x = xo - round( penWidthIn * cos(theta + M_PI_2) );
1877 pt.y = yo - round( penWidthIn * sin(theta + M_PI_2) );
1878 PATH_AddEntry(pUpPath, &pt, PT_LINETO);
1879 break;
1880 case PS_ENDCAP_ROUND :
1881 default :
1882 corners[0].x = xo - penWidthIn;
1883 corners[0].y = yo - penWidthIn;
1884 corners[1].x = xo + penWidthOut;
1885 corners[1].y = yo + penWidthOut;
1886 PATH_DoArcPart(pUpPath ,corners, theta + M_PI_2 , theta + 3 * M_PI_4, (j == 0 ? PT_MOVETO : FALSE));
1887 PATH_DoArcPart(pUpPath ,corners, theta + 3 * M_PI_4 , theta + M_PI, FALSE);
1888 PATH_DoArcPart(pUpPath ,corners, theta + M_PI, theta + 5 * M_PI_4, FALSE);
1889 PATH_DoArcPart(pUpPath ,corners, theta + 5 * M_PI_4 , theta + 3 * M_PI_2, FALSE);
1890 break;
1893 /* Corpse of the path */
1894 else {
1895 /* Compute angle */
1896 INT previous, next;
1897 double xa, ya, xb, yb, xo, yo;
1898 double alpha, theta, miterWidth;
1899 DWORD _joint = joint;
1900 POINT pt;
1901 GdiPath *pInsidePath, *pOutsidePath;
1902 if(j > 0 && j < pStrokes[i]->numEntriesUsed - 1) {
1903 previous = j - 1;
1904 next = j + 1;
1906 else if (j == 0) {
1907 previous = pStrokes[i]->numEntriesUsed - 1;
1908 next = j + 1;
1910 else {
1911 previous = j - 1;
1912 next = 0;
1914 xo = pStrokes[i]->pPoints[j].x;
1915 yo = pStrokes[i]->pPoints[j].y;
1916 xa = pStrokes[i]->pPoints[previous].x;
1917 ya = pStrokes[i]->pPoints[previous].y;
1918 xb = pStrokes[i]->pPoints[next].x;
1919 yb = pStrokes[i]->pPoints[next].y;
1920 theta = atan2( yo - ya, xo - xa );
1921 alpha = atan2( yb - yo, xb - xo ) - theta;
1922 if (alpha > 0) alpha -= M_PI;
1923 else alpha += M_PI;
1924 if(_joint == PS_JOIN_MITER && dc->miterLimit < fabs(1 / sin(alpha/2))) {
1925 _joint = PS_JOIN_BEVEL;
1927 if(alpha > 0) {
1928 pInsidePath = pUpPath;
1929 pOutsidePath = pDownPath;
1931 else if(alpha < 0) {
1932 pInsidePath = pDownPath;
1933 pOutsidePath = pUpPath;
1935 else {
1936 continue;
1938 /* Inside angle points */
1939 if(alpha > 0) {
1940 pt.x = xo - round( penWidthIn * cos(theta + M_PI_2) );
1941 pt.y = yo - round( penWidthIn * sin(theta + M_PI_2) );
1943 else {
1944 pt.x = xo + round( penWidthIn * cos(theta + M_PI_2) );
1945 pt.y = yo + round( penWidthIn * sin(theta + M_PI_2) );
1947 PATH_AddEntry(pInsidePath, &pt, PT_LINETO);
1948 if(alpha > 0) {
1949 pt.x = xo + round( penWidthIn * cos(M_PI_2 + alpha + theta) );
1950 pt.y = yo + round( penWidthIn * sin(M_PI_2 + alpha + theta) );
1952 else {
1953 pt.x = xo - round( penWidthIn * cos(M_PI_2 + alpha + theta) );
1954 pt.y = yo - round( penWidthIn * sin(M_PI_2 + alpha + theta) );
1956 PATH_AddEntry(pInsidePath, &pt, PT_LINETO);
1957 /* Outside angle point */
1958 switch(_joint) {
1959 case PS_JOIN_MITER :
1960 miterWidth = fabs(penWidthOut / cos(M_PI_2 - fabs(alpha) / 2));
1961 pt.x = xo + round( miterWidth * cos(theta + alpha / 2) );
1962 pt.y = yo + round( miterWidth * sin(theta + alpha / 2) );
1963 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
1964 break;
1965 case PS_JOIN_BEVEL :
1966 if(alpha > 0) {
1967 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
1968 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
1970 else {
1971 pt.x = xo - round( penWidthOut * cos(theta + M_PI_2) );
1972 pt.y = yo - round( penWidthOut * sin(theta + M_PI_2) );
1974 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
1975 if(alpha > 0) {
1976 pt.x = xo - round( penWidthOut * cos(M_PI_2 + alpha + theta) );
1977 pt.y = yo - round( penWidthOut * sin(M_PI_2 + alpha + theta) );
1979 else {
1980 pt.x = xo + round( penWidthOut * cos(M_PI_2 + alpha + theta) );
1981 pt.y = yo + round( penWidthOut * sin(M_PI_2 + alpha + theta) );
1983 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
1984 break;
1985 case PS_JOIN_ROUND :
1986 default :
1987 if(alpha > 0) {
1988 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
1989 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
1991 else {
1992 pt.x = xo - round( penWidthOut * cos(theta + M_PI_2) );
1993 pt.y = yo - round( penWidthOut * sin(theta + M_PI_2) );
1995 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
1996 pt.x = xo + round( penWidthOut * cos(theta + alpha / 2) );
1997 pt.y = yo + round( penWidthOut * sin(theta + alpha / 2) );
1998 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
1999 if(alpha > 0) {
2000 pt.x = xo - round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2001 pt.y = yo - round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2003 else {
2004 pt.x = xo + round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2005 pt.y = yo + round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2007 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
2008 break;
2012 for(j = 0; j < pUpPath->numEntriesUsed; j++) {
2013 POINT pt;
2014 pt.x = pUpPath->pPoints[j].x;
2015 pt.y = pUpPath->pPoints[j].y;
2016 PATH_AddEntry(pNewPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO));
2018 for(j = 0; j < pDownPath->numEntriesUsed; j++) {
2019 POINT pt;
2020 pt.x = pDownPath->pPoints[pDownPath->numEntriesUsed - j - 1].x;
2021 pt.y = pDownPath->pPoints[pDownPath->numEntriesUsed - j - 1].y;
2022 PATH_AddEntry(pNewPath, &pt, ( (j == 0 && (pStrokes[i]->pFlags[pStrokes[i]->numEntriesUsed - 1] & PT_CLOSEFIGURE)) ? PT_MOVETO : PT_LINETO));
2025 PATH_DestroyGdiPath(pStrokes[i]);
2026 HeapFree(GetProcessHeap(), 0, pStrokes[i]);
2027 PATH_DestroyGdiPath(pUpPath);
2028 HeapFree(GetProcessHeap(), 0, pUpPath);
2029 PATH_DestroyGdiPath(pDownPath);
2030 HeapFree(GetProcessHeap(), 0, pDownPath);
2032 HeapFree(GetProcessHeap(), 0, pStrokes);
2034 pNewPath->state = PATH_Closed;
2035 if (!(ret = PATH_AssignGdiPath(pPath, pNewPath)))
2036 ERR("Assign path failed\n");
2037 PATH_DestroyGdiPath(pNewPath);
2038 HeapFree(GetProcessHeap(), 0, pNewPath);
2039 return ret;
2043 /*******************************************************************
2044 * StrokeAndFillPath [GDI32.@]
2048 BOOL WINAPI StrokeAndFillPath(HDC hdc)
2050 BOOL ret = FALSE;
2051 DC *dc = get_dc_ptr( hdc );
2053 if (dc)
2055 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pStrokeAndFillPath );
2056 ret = physdev->funcs->pStrokeAndFillPath( physdev );
2057 release_dc_ptr( dc );
2059 return ret;
2063 /*******************************************************************
2064 * StrokePath [GDI32.@]
2068 BOOL WINAPI StrokePath(HDC hdc)
2070 BOOL ret = FALSE;
2071 DC *dc = get_dc_ptr( hdc );
2073 if (dc)
2075 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pStrokePath );
2076 ret = physdev->funcs->pStrokePath( physdev );
2077 release_dc_ptr( dc );
2079 return ret;
2083 /*******************************************************************
2084 * WidenPath [GDI32.@]
2088 BOOL WINAPI WidenPath(HDC hdc)
2090 BOOL ret = FALSE;
2091 DC *dc = get_dc_ptr( hdc );
2093 if (dc)
2095 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pWidenPath );
2096 ret = physdev->funcs->pWidenPath( physdev );
2097 release_dc_ptr( dc );
2099 return ret;
2103 /***********************************************************************
2104 * null driver fallback implementations
2107 BOOL nulldrv_BeginPath( PHYSDEV dev )
2109 DC *dc = get_nulldrv_dc( dev );
2111 if (!path_driver.pCreateDC( &dc->physDev, NULL, NULL, NULL, NULL )) return FALSE;
2112 PATH_EmptyPath(&dc->path);
2113 dc->path.newStroke = TRUE;
2114 dc->path.state = PATH_Open;
2115 return TRUE;
2118 BOOL nulldrv_EndPath( PHYSDEV dev )
2120 SetLastError( ERROR_CAN_NOT_COMPLETE );
2121 return FALSE;
2124 BOOL nulldrv_AbortPath( PHYSDEV dev )
2126 DC *dc = get_nulldrv_dc( dev );
2128 PATH_EmptyPath( &dc->path );
2129 return TRUE;
2132 BOOL nulldrv_CloseFigure( PHYSDEV dev )
2134 SetLastError( ERROR_CAN_NOT_COMPLETE );
2135 return FALSE;
2138 BOOL nulldrv_SelectClipPath( PHYSDEV dev, INT mode )
2140 BOOL ret;
2141 HRGN hrgn;
2142 DC *dc = get_nulldrv_dc( dev );
2144 if (dc->path.state != PATH_Closed)
2146 SetLastError( ERROR_CAN_NOT_COMPLETE );
2147 return FALSE;
2149 if (!PATH_PathToRegion( &dc->path, GetPolyFillMode(dev->hdc), &hrgn )) return FALSE;
2150 ret = ExtSelectClipRgn( dev->hdc, hrgn, mode ) != ERROR;
2151 if (ret) PATH_EmptyPath( &dc->path );
2152 /* FIXME: Should this function delete the path even if it failed? */
2153 DeleteObject( hrgn );
2154 return ret;
2157 BOOL nulldrv_FillPath( PHYSDEV dev )
2159 DC *dc = get_nulldrv_dc( dev );
2161 if (dc->path.state != PATH_Closed)
2163 SetLastError( ERROR_CAN_NOT_COMPLETE );
2164 return FALSE;
2166 if (!PATH_FillPath( dev->hdc, &dc->path )) return FALSE;
2167 /* FIXME: Should the path be emptied even if conversion failed? */
2168 PATH_EmptyPath( &dc->path );
2169 return TRUE;
2172 BOOL nulldrv_StrokeAndFillPath( PHYSDEV dev )
2174 DC *dc = get_nulldrv_dc( dev );
2176 if (dc->path.state != PATH_Closed)
2178 SetLastError( ERROR_CAN_NOT_COMPLETE );
2179 return FALSE;
2181 if (!PATH_FillPath( dev->hdc, &dc->path )) return FALSE;
2182 if (!PATH_StrokePath( dev->hdc, &dc->path )) return FALSE;
2183 PATH_EmptyPath( &dc->path );
2184 return TRUE;
2187 BOOL nulldrv_StrokePath( PHYSDEV dev )
2189 DC *dc = get_nulldrv_dc( dev );
2191 if (dc->path.state != PATH_Closed)
2193 SetLastError( ERROR_CAN_NOT_COMPLETE );
2194 return FALSE;
2196 if (!PATH_StrokePath( dev->hdc, &dc->path )) return FALSE;
2197 PATH_EmptyPath( &dc->path );
2198 return TRUE;
2201 BOOL nulldrv_FlattenPath( PHYSDEV dev )
2203 DC *dc = get_nulldrv_dc( dev );
2205 if (dc->path.state != PATH_Closed)
2207 SetLastError( ERROR_CAN_NOT_COMPLETE );
2208 return FALSE;
2210 return PATH_FlattenPath( &dc->path );
2213 BOOL nulldrv_WidenPath( PHYSDEV dev )
2215 DC *dc = get_nulldrv_dc( dev );
2217 if (dc->path.state != PATH_Closed)
2219 SetLastError( ERROR_CAN_NOT_COMPLETE );
2220 return FALSE;
2222 return PATH_WidenPath( dc );
2225 const struct gdi_dc_funcs path_driver =
2227 NULL, /* pAbortDoc */
2228 pathdrv_AbortPath, /* pAbortPath */
2229 NULL, /* pAlphaBlend */
2230 pathdrv_AngleArc, /* pAngleArc */
2231 pathdrv_Arc, /* pArc */
2232 pathdrv_ArcTo, /* pArcTo */
2233 pathdrv_BeginPath, /* pBeginPath */
2234 NULL, /* pBlendImage */
2235 NULL, /* pChoosePixelFormat */
2236 pathdrv_Chord, /* pChord */
2237 pathdrv_CloseFigure, /* pCloseFigure */
2238 NULL, /* pCopyBitmap */
2239 NULL, /* pCreateBitmap */
2240 NULL, /* pCreateCompatibleDC */
2241 pathdrv_CreateDC, /* pCreateDC */
2242 NULL, /* pCreateDIBSection */
2243 NULL, /* pDeleteBitmap */
2244 pathdrv_DeleteDC, /* pDeleteDC */
2245 NULL, /* pDeleteObject */
2246 NULL, /* pDescribePixelFormat */
2247 NULL, /* pDeviceCapabilities */
2248 pathdrv_Ellipse, /* pEllipse */
2249 NULL, /* pEndDoc */
2250 NULL, /* pEndPage */
2251 pathdrv_EndPath, /* pEndPath */
2252 NULL, /* pEnumFonts */
2253 NULL, /* pEnumICMProfiles */
2254 NULL, /* pExcludeClipRect */
2255 NULL, /* pExtDeviceMode */
2256 NULL, /* pExtEscape */
2257 NULL, /* pExtFloodFill */
2258 NULL, /* pExtSelectClipRgn */
2259 pathdrv_ExtTextOut, /* pExtTextOut */
2260 NULL, /* pFillPath */
2261 NULL, /* pFillRgn */
2262 NULL, /* pFlattenPath */
2263 NULL, /* pFontIsLinked */
2264 NULL, /* pFrameRgn */
2265 NULL, /* pGdiComment */
2266 NULL, /* pGdiRealizationInfo */
2267 NULL, /* pGetCharABCWidths */
2268 NULL, /* pGetCharABCWidthsI */
2269 NULL, /* pGetCharWidth */
2270 NULL, /* pGetDeviceCaps */
2271 NULL, /* pGetDeviceGammaRamp */
2272 NULL, /* pGetFontData */
2273 NULL, /* pGetFontUnicodeRanges */
2274 NULL, /* pGetGlyphIndices */
2275 NULL, /* pGetGlyphOutline */
2276 NULL, /* pGetICMProfile */
2277 NULL, /* pGetImage */
2278 NULL, /* pGetKerningPairs */
2279 NULL, /* pGetNearestColor */
2280 NULL, /* pGetOutlineTextMetrics */
2281 NULL, /* pGetPixel */
2282 NULL, /* pGetPixelFormat */
2283 NULL, /* pGetSystemPaletteEntries */
2284 NULL, /* pGetTextCharsetInfo */
2285 NULL, /* pGetTextExtentExPoint */
2286 NULL, /* pGetTextExtentExPointI */
2287 NULL, /* pGetTextFace */
2288 NULL, /* pGetTextMetrics */
2289 NULL, /* pGradientFill */
2290 NULL, /* pIntersectClipRect */
2291 NULL, /* pInvertRgn */
2292 pathdrv_LineTo, /* pLineTo */
2293 NULL, /* pModifyWorldTransform */
2294 pathdrv_MoveTo, /* pMoveTo */
2295 NULL, /* pOffsetClipRgn */
2296 NULL, /* pOffsetViewportOrg */
2297 NULL, /* pOffsetWindowOrg */
2298 NULL, /* pPaintRgn */
2299 NULL, /* pPatBlt */
2300 pathdrv_Pie, /* pPie */
2301 pathdrv_PolyBezier, /* pPolyBezier */
2302 pathdrv_PolyBezierTo, /* pPolyBezierTo */
2303 pathdrv_PolyDraw, /* pPolyDraw */
2304 pathdrv_PolyPolygon, /* pPolyPolygon */
2305 pathdrv_PolyPolyline, /* pPolyPolyline */
2306 pathdrv_Polygon, /* pPolygon */
2307 pathdrv_Polyline, /* pPolyline */
2308 pathdrv_PolylineTo, /* pPolylineTo */
2309 NULL, /* pPutImage */
2310 NULL, /* pRealizeDefaultPalette */
2311 NULL, /* pRealizePalette */
2312 pathdrv_Rectangle, /* pRectangle */
2313 NULL, /* pResetDC */
2314 NULL, /* pRestoreDC */
2315 pathdrv_RoundRect, /* pRoundRect */
2316 NULL, /* pSaveDC */
2317 NULL, /* pScaleViewportExt */
2318 NULL, /* pScaleWindowExt */
2319 NULL, /* pSelectBitmap */
2320 NULL, /* pSelectBrush */
2321 NULL, /* pSelectClipPath */
2322 NULL, /* pSelectFont */
2323 NULL, /* pSelectPalette */
2324 NULL, /* pSelectPen */
2325 NULL, /* pSetArcDirection */
2326 NULL, /* pSetBkColor */
2327 NULL, /* pSetBkMode */
2328 NULL, /* pSetDCBrushColor */
2329 NULL, /* pSetDCPenColor */
2330 NULL, /* pSetDIBColorTable */
2331 NULL, /* pSetDIBitsToDevice */
2332 NULL, /* pSetDeviceClipping */
2333 NULL, /* pSetDeviceGammaRamp */
2334 NULL, /* pSetLayout */
2335 NULL, /* pSetMapMode */
2336 NULL, /* pSetMapperFlags */
2337 NULL, /* pSetPixel */
2338 NULL, /* pSetPixelFormat */
2339 NULL, /* pSetPolyFillMode */
2340 NULL, /* pSetROP2 */
2341 NULL, /* pSetRelAbs */
2342 NULL, /* pSetStretchBltMode */
2343 NULL, /* pSetTextAlign */
2344 NULL, /* pSetTextCharacterExtra */
2345 NULL, /* pSetTextColor */
2346 NULL, /* pSetTextJustification */
2347 NULL, /* pSetViewportExt */
2348 NULL, /* pSetViewportOrg */
2349 NULL, /* pSetWindowExt */
2350 NULL, /* pSetWindowOrg */
2351 NULL, /* pSetWorldTransform */
2352 NULL, /* pStartDoc */
2353 NULL, /* pStartPage */
2354 NULL, /* pStretchBlt */
2355 NULL, /* pStretchDIBits */
2356 NULL, /* pStrokeAndFillPath */
2357 NULL, /* pStrokePath */
2358 NULL, /* pSwapBuffers */
2359 NULL, /* pUnrealizePalette */
2360 NULL, /* pWidenPath */
2361 /* OpenGL not supported */