gdi32: Correctly handle space char on Path_ExtTextOut().
[wine/hacks.git] / dlls / gdi32 / path.c
blob195f4138a65836876f68c079090a1285c0e3cf9f
1 /*
2 * Graphics paths (BeginPath, EndPath etc.)
4 * Copyright 1997, 1998 Martin Boehme
5 * 1999 Huw D M Davies
6 * Copyright 2005 Dmitry Timoshkov
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <math.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #if defined(HAVE_FLOAT_H)
32 #include <float.h>
33 #endif
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wingdi.h"
38 #include "winerror.h"
40 #include "gdi_private.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
45 /* Notes on the implementation
47 * The implementation is based on dynamically resizable arrays of points and
48 * flags. I dithered for a bit before deciding on this implementation, and
49 * I had even done a bit of work on a linked list version before switching
50 * to arrays. It's a bit of a tradeoff. When you use linked lists, the
51 * implementation of FlattenPath is easier, because you can rip the
52 * PT_BEZIERTO entries out of the middle of the list and link the
53 * corresponding PT_LINETO entries in. However, when you use arrays,
54 * PathToRegion becomes easier, since you can essentially just pass your array
55 * of points to CreatePolyPolygonRgn. Also, if I'd used linked lists, I would
56 * have had the extra effort of creating a chunk-based allocation scheme
57 * in order to use memory effectively. That's why I finally decided to use
58 * arrays. Note by the way that the array based implementation has the same
59 * linear time complexity that linked lists would have since the arrays grow
60 * exponentially.
62 * The points are stored in the path in device coordinates. This is
63 * consistent with the way Windows does things (for instance, see the Win32
64 * SDK documentation for GetPath).
66 * The word "stroke" appears in several places (e.g. in the flag
67 * GdiPath.newStroke). A stroke consists of a PT_MOVETO followed by one or
68 * more PT_LINETOs or PT_BEZIERTOs, up to, but not including, the next
69 * PT_MOVETO. Note that this is not the same as the definition of a figure;
70 * a figure can contain several strokes.
72 * I modified the drawing functions (MoveTo, LineTo etc.) to test whether
73 * the path is open and to call the corresponding function in path.c if this
74 * is the case. A more elegant approach would be to modify the function
75 * pointers in the DC_FUNCTIONS structure; however, this would be a lot more
76 * complex. Also, the performance degradation caused by my approach in the
77 * case where no path is open is so small that it cannot be measured.
79 * Martin Boehme
82 /* FIXME: A lot of stuff isn't implemented yet. There is much more to come. */
84 #define NUM_ENTRIES_INITIAL 16 /* Initial size of points / flags arrays */
85 #define GROW_FACTOR_NUMER 2 /* Numerator of grow factor for the array */
86 #define GROW_FACTOR_DENOM 1 /* Denominator of grow factor */
88 /* A floating point version of the POINT structure */
89 typedef struct tagFLOAT_POINT
91 double x, y;
92 } FLOAT_POINT;
95 static BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags);
96 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
97 HRGN *pHrgn);
98 static void PATH_EmptyPath(GdiPath *pPath);
99 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries);
100 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
101 double angleStart, double angleEnd, BYTE startEntryType);
102 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
103 double y, POINT *pPoint);
104 static void PATH_NormalizePoint(FLOAT_POINT corners[], const FLOAT_POINT
105 *pPoint, double *pX, double *pY);
106 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2);
108 /* Performs a world-to-viewport transformation on the specified point (which
109 * is in floating point format).
111 static inline void INTERNAL_LPTODP_FLOAT(DC *dc, FLOAT_POINT *point)
113 double x, y;
115 /* Perform the transformation */
116 x = point->x;
117 y = point->y;
118 point->x = x * dc->xformWorld2Vport.eM11 +
119 y * dc->xformWorld2Vport.eM21 +
120 dc->xformWorld2Vport.eDx;
121 point->y = x * dc->xformWorld2Vport.eM12 +
122 y * dc->xformWorld2Vport.eM22 +
123 dc->xformWorld2Vport.eDy;
127 /***********************************************************************
128 * BeginPath (GDI32.@)
130 BOOL WINAPI BeginPath(HDC hdc)
132 BOOL ret = TRUE;
133 DC *dc = get_dc_ptr( hdc );
135 if(!dc) return FALSE;
137 if(dc->funcs->pBeginPath)
138 ret = dc->funcs->pBeginPath(dc->physDev);
139 else
141 /* If path is already open, do nothing */
142 if(dc->path.state != PATH_Open)
144 /* Make sure that path is empty */
145 PATH_EmptyPath(&dc->path);
147 /* Initialize variables for new path */
148 dc->path.newStroke=TRUE;
149 dc->path.state=PATH_Open;
152 release_dc_ptr( dc );
153 return ret;
157 /***********************************************************************
158 * EndPath (GDI32.@)
160 BOOL WINAPI EndPath(HDC hdc)
162 BOOL ret = TRUE;
163 DC *dc = get_dc_ptr( hdc );
165 if(!dc) return FALSE;
167 if(dc->funcs->pEndPath)
168 ret = dc->funcs->pEndPath(dc->physDev);
169 else
171 /* Check that path is currently being constructed */
172 if(dc->path.state!=PATH_Open)
174 SetLastError(ERROR_CAN_NOT_COMPLETE);
175 ret = FALSE;
177 /* Set flag to indicate that path is finished */
178 else dc->path.state=PATH_Closed;
180 release_dc_ptr( dc );
181 return ret;
185 /******************************************************************************
186 * AbortPath [GDI32.@]
187 * Closes and discards paths from device context
189 * NOTES
190 * Check that SetLastError is being called correctly
192 * PARAMS
193 * hdc [I] Handle to device context
195 * RETURNS
196 * Success: TRUE
197 * Failure: FALSE
199 BOOL WINAPI AbortPath( HDC hdc )
201 BOOL ret = TRUE;
202 DC *dc = get_dc_ptr( hdc );
204 if(!dc) return FALSE;
206 if(dc->funcs->pAbortPath)
207 ret = dc->funcs->pAbortPath(dc->physDev);
208 else /* Remove all entries from the path */
209 PATH_EmptyPath( &dc->path );
210 release_dc_ptr( dc );
211 return ret;
215 /***********************************************************************
216 * CloseFigure (GDI32.@)
218 * FIXME: Check that SetLastError is being called correctly
220 BOOL WINAPI CloseFigure(HDC hdc)
222 BOOL ret = TRUE;
223 DC *dc = get_dc_ptr( hdc );
225 if(!dc) return FALSE;
227 if(dc->funcs->pCloseFigure)
228 ret = dc->funcs->pCloseFigure(dc->physDev);
229 else
231 /* Check that path is open */
232 if(dc->path.state!=PATH_Open)
234 SetLastError(ERROR_CAN_NOT_COMPLETE);
235 ret = FALSE;
237 else
239 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
240 /* It is not necessary to draw a line, PT_CLOSEFIGURE is a virtual closing line itself */
241 if(dc->path.numEntriesUsed)
243 dc->path.pFlags[dc->path.numEntriesUsed-1]|=PT_CLOSEFIGURE;
244 dc->path.newStroke=TRUE;
248 release_dc_ptr( dc );
249 return ret;
253 /***********************************************************************
254 * GetPath (GDI32.@)
256 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
257 INT nSize)
259 INT ret = -1;
260 GdiPath *pPath;
261 DC *dc = get_dc_ptr( hdc );
263 if(!dc) return -1;
265 pPath = &dc->path;
267 /* Check that path is closed */
268 if(pPath->state!=PATH_Closed)
270 SetLastError(ERROR_CAN_NOT_COMPLETE);
271 goto done;
274 if(nSize==0)
275 ret = pPath->numEntriesUsed;
276 else if(nSize<pPath->numEntriesUsed)
278 SetLastError(ERROR_INVALID_PARAMETER);
279 goto done;
281 else
283 memcpy(pPoints, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
284 memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
286 /* Convert the points to logical coordinates */
287 if(!DPtoLP(hdc, pPoints, pPath->numEntriesUsed))
289 /* FIXME: Is this the correct value? */
290 SetLastError(ERROR_CAN_NOT_COMPLETE);
291 goto done;
293 else ret = pPath->numEntriesUsed;
295 done:
296 release_dc_ptr( dc );
297 return ret;
301 /***********************************************************************
302 * PathToRegion (GDI32.@)
304 * FIXME
305 * Check that SetLastError is being called correctly
307 * The documentation does not state this explicitly, but a test under Windows
308 * shows that the region which is returned should be in device coordinates.
310 HRGN WINAPI PathToRegion(HDC hdc)
312 GdiPath *pPath;
313 HRGN hrgnRval = 0;
314 DC *dc = get_dc_ptr( hdc );
316 /* Get pointer to path */
317 if(!dc) return 0;
319 pPath = &dc->path;
321 /* Check that path is closed */
322 if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
323 else
325 /* FIXME: Should we empty the path even if conversion failed? */
326 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
327 PATH_EmptyPath(pPath);
328 else
329 hrgnRval=0;
331 release_dc_ptr( dc );
332 return hrgnRval;
335 static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
337 INT mapMode, graphicsMode;
338 SIZE ptViewportExt, ptWindowExt;
339 POINT ptViewportOrg, ptWindowOrg;
340 XFORM xform;
341 HRGN hrgn;
343 if(dc->funcs->pFillPath)
344 return dc->funcs->pFillPath(dc->physDev);
346 /* Check that path is closed */
347 if(pPath->state!=PATH_Closed)
349 SetLastError(ERROR_CAN_NOT_COMPLETE);
350 return FALSE;
353 /* Construct a region from the path and fill it */
354 if(PATH_PathToRegion(pPath, dc->polyFillMode, &hrgn))
356 /* Since PaintRgn interprets the region as being in logical coordinates
357 * but the points we store for the path are already in device
358 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
359 * Using SaveDC to save information about the mapping mode / world
360 * transform would be easier but would require more overhead, especially
361 * now that SaveDC saves the current path.
364 /* Save the information about the old mapping mode */
365 mapMode=GetMapMode(dc->hSelf);
366 GetViewportExtEx(dc->hSelf, &ptViewportExt);
367 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
368 GetWindowExtEx(dc->hSelf, &ptWindowExt);
369 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
371 /* Save world transform
372 * NB: The Windows documentation on world transforms would lead one to
373 * believe that this has to be done only in GM_ADVANCED; however, my
374 * tests show that resetting the graphics mode to GM_COMPATIBLE does
375 * not reset the world transform.
377 GetWorldTransform(dc->hSelf, &xform);
379 /* Set MM_TEXT */
380 SetMapMode(dc->hSelf, MM_TEXT);
381 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
382 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
383 graphicsMode=GetGraphicsMode(dc->hSelf);
384 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
385 ModifyWorldTransform(dc->hSelf, &xform, MWT_IDENTITY);
386 SetGraphicsMode(dc->hSelf, graphicsMode);
388 /* Paint the region */
389 PaintRgn(dc->hSelf, hrgn);
390 DeleteObject(hrgn);
391 /* Restore the old mapping mode */
392 SetMapMode(dc->hSelf, mapMode);
393 SetViewportExtEx(dc->hSelf, ptViewportExt.cx, ptViewportExt.cy, NULL);
394 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
395 SetWindowExtEx(dc->hSelf, ptWindowExt.cx, ptWindowExt.cy, NULL);
396 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
398 /* Go to GM_ADVANCED temporarily to restore the world transform */
399 graphicsMode=GetGraphicsMode(dc->hSelf);
400 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
401 SetWorldTransform(dc->hSelf, &xform);
402 SetGraphicsMode(dc->hSelf, graphicsMode);
403 return TRUE;
405 return FALSE;
409 /***********************************************************************
410 * FillPath (GDI32.@)
412 * FIXME
413 * Check that SetLastError is being called correctly
415 BOOL WINAPI FillPath(HDC hdc)
417 DC *dc = get_dc_ptr( hdc );
418 BOOL bRet = FALSE;
420 if(!dc) return FALSE;
422 if(dc->funcs->pFillPath)
423 bRet = dc->funcs->pFillPath(dc->physDev);
424 else
426 bRet = PATH_FillPath(dc, &dc->path);
427 if(bRet)
429 /* FIXME: Should the path be emptied even if conversion
430 failed? */
431 PATH_EmptyPath(&dc->path);
434 release_dc_ptr( dc );
435 return bRet;
439 /***********************************************************************
440 * SelectClipPath (GDI32.@)
441 * FIXME
442 * Check that SetLastError is being called correctly
444 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
446 GdiPath *pPath;
447 HRGN hrgnPath;
448 BOOL success = FALSE;
449 DC *dc = get_dc_ptr( hdc );
451 if(!dc) return FALSE;
453 if(dc->funcs->pSelectClipPath)
454 success = dc->funcs->pSelectClipPath(dc->physDev, iMode);
455 else
457 pPath = &dc->path;
459 /* Check that path is closed */
460 if(pPath->state!=PATH_Closed)
461 SetLastError(ERROR_CAN_NOT_COMPLETE);
462 /* Construct a region from the path */
463 else if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnPath))
465 success = ExtSelectClipRgn( hdc, hrgnPath, iMode ) != ERROR;
466 DeleteObject(hrgnPath);
468 /* Empty the path */
469 if(success)
470 PATH_EmptyPath(pPath);
471 /* FIXME: Should this function delete the path even if it failed? */
474 release_dc_ptr( dc );
475 return success;
479 /***********************************************************************
480 * Exported functions
483 /* PATH_InitGdiPath
485 * Initializes the GdiPath structure.
487 void PATH_InitGdiPath(GdiPath *pPath)
489 assert(pPath!=NULL);
491 pPath->state=PATH_Null;
492 pPath->pPoints=NULL;
493 pPath->pFlags=NULL;
494 pPath->numEntriesUsed=0;
495 pPath->numEntriesAllocated=0;
498 /* PATH_DestroyGdiPath
500 * Destroys a GdiPath structure (frees the memory in the arrays).
502 void PATH_DestroyGdiPath(GdiPath *pPath)
504 assert(pPath!=NULL);
506 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
507 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
510 /* PATH_AssignGdiPath
512 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
513 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
514 * not just the pointers. Since this means that the arrays in pPathDest may
515 * need to be resized, pPathDest should have been initialized using
516 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
517 * not a copy constructor).
518 * Returns TRUE if successful, else FALSE.
520 BOOL PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
522 assert(pPathDest!=NULL && pPathSrc!=NULL);
524 /* Make sure destination arrays are big enough */
525 if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
526 return FALSE;
528 /* Perform the copy operation */
529 memcpy(pPathDest->pPoints, pPathSrc->pPoints,
530 sizeof(POINT)*pPathSrc->numEntriesUsed);
531 memcpy(pPathDest->pFlags, pPathSrc->pFlags,
532 sizeof(BYTE)*pPathSrc->numEntriesUsed);
534 pPathDest->state=pPathSrc->state;
535 pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
536 pPathDest->newStroke=pPathSrc->newStroke;
538 return TRUE;
541 /* PATH_MoveTo
543 * Should be called when a MoveTo is performed on a DC that has an
544 * open path. This starts a new stroke. Returns TRUE if successful, else
545 * FALSE.
547 BOOL PATH_MoveTo(DC *dc)
549 GdiPath *pPath = &dc->path;
551 /* Check that path is open */
552 if(pPath->state!=PATH_Open)
553 /* FIXME: Do we have to call SetLastError? */
554 return FALSE;
556 /* Start a new stroke */
557 pPath->newStroke=TRUE;
559 return TRUE;
562 /* PATH_LineTo
564 * Should be called when a LineTo is performed on a DC that has an
565 * open path. This adds a PT_LINETO entry to the path (and possibly
566 * a PT_MOVETO entry, if this is the first LineTo in a stroke).
567 * Returns TRUE if successful, else FALSE.
569 BOOL PATH_LineTo(DC *dc, INT x, INT y)
571 GdiPath *pPath = &dc->path;
572 POINT point, pointCurPos;
574 /* Check that path is open */
575 if(pPath->state!=PATH_Open)
576 return FALSE;
578 /* Convert point to device coordinates */
579 point.x=x;
580 point.y=y;
581 if(!LPtoDP(dc->hSelf, &point, 1))
582 return FALSE;
584 /* Add a PT_MOVETO if necessary */
585 if(pPath->newStroke)
587 pPath->newStroke=FALSE;
588 pointCurPos.x = dc->CursPosX;
589 pointCurPos.y = dc->CursPosY;
590 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
591 return FALSE;
592 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
593 return FALSE;
596 /* Add a PT_LINETO entry */
597 return PATH_AddEntry(pPath, &point, PT_LINETO);
600 /* PATH_RoundRect
602 * Should be called when a call to RoundRect is performed on a DC that has
603 * an open path. Returns TRUE if successful, else FALSE.
605 * FIXME: it adds the same entries to the path as windows does, but there
606 * is an error in the bezier drawing code so that there are small pixel-size
607 * gaps when the resulting path is drawn by StrokePath()
609 BOOL PATH_RoundRect(DC *dc, INT x1, INT y1, INT x2, INT y2, INT ell_width, INT ell_height)
611 GdiPath *pPath = &dc->path;
612 POINT corners[2], pointTemp;
613 FLOAT_POINT ellCorners[2];
615 /* Check that path is open */
616 if(pPath->state!=PATH_Open)
617 return FALSE;
619 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
620 return FALSE;
622 /* Add points to the roundrect path */
623 ellCorners[0].x = corners[1].x-ell_width;
624 ellCorners[0].y = corners[0].y;
625 ellCorners[1].x = corners[1].x;
626 ellCorners[1].y = corners[0].y+ell_height;
627 if(!PATH_DoArcPart(pPath, ellCorners, 0, -M_PI_2, PT_MOVETO))
628 return FALSE;
629 pointTemp.x = corners[0].x+ell_width/2;
630 pointTemp.y = corners[0].y;
631 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
632 return FALSE;
633 ellCorners[0].x = corners[0].x;
634 ellCorners[1].x = corners[0].x+ell_width;
635 if(!PATH_DoArcPart(pPath, ellCorners, -M_PI_2, -M_PI, FALSE))
636 return FALSE;
637 pointTemp.x = corners[0].x;
638 pointTemp.y = corners[1].y-ell_height/2;
639 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
640 return FALSE;
641 ellCorners[0].y = corners[1].y-ell_height;
642 ellCorners[1].y = corners[1].y;
643 if(!PATH_DoArcPart(pPath, ellCorners, M_PI, M_PI_2, FALSE))
644 return FALSE;
645 pointTemp.x = corners[1].x-ell_width/2;
646 pointTemp.y = corners[1].y;
647 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
648 return FALSE;
649 ellCorners[0].x = corners[1].x-ell_width;
650 ellCorners[1].x = corners[1].x;
651 if(!PATH_DoArcPart(pPath, ellCorners, M_PI_2, 0, FALSE))
652 return FALSE;
654 /* Close the roundrect figure */
655 if(!CloseFigure(dc->hSelf))
656 return FALSE;
658 return TRUE;
661 /* PATH_Rectangle
663 * Should be called when a call to Rectangle is performed on a DC that has
664 * an open path. Returns TRUE if successful, else FALSE.
666 BOOL PATH_Rectangle(DC *dc, INT x1, INT y1, INT x2, INT y2)
668 GdiPath *pPath = &dc->path;
669 POINT corners[2], pointTemp;
671 /* Check that path is open */
672 if(pPath->state!=PATH_Open)
673 return FALSE;
675 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
676 return FALSE;
678 /* Close any previous figure */
679 if(!CloseFigure(dc->hSelf))
681 /* The CloseFigure call shouldn't have failed */
682 assert(FALSE);
683 return FALSE;
686 /* Add four points to the path */
687 pointTemp.x=corners[1].x;
688 pointTemp.y=corners[0].y;
689 if(!PATH_AddEntry(pPath, &pointTemp, PT_MOVETO))
690 return FALSE;
691 if(!PATH_AddEntry(pPath, corners, PT_LINETO))
692 return FALSE;
693 pointTemp.x=corners[0].x;
694 pointTemp.y=corners[1].y;
695 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
696 return FALSE;
697 if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
698 return FALSE;
700 /* Close the rectangle figure */
701 if(!CloseFigure(dc->hSelf))
703 /* The CloseFigure call shouldn't have failed */
704 assert(FALSE);
705 return FALSE;
708 return TRUE;
711 /* PATH_Ellipse
713 * Should be called when a call to Ellipse is performed on a DC that has
714 * an open path. This adds four Bezier splines representing the ellipse
715 * to the path. Returns TRUE if successful, else FALSE.
717 BOOL PATH_Ellipse(DC *dc, INT x1, INT y1, INT x2, INT y2)
719 return( PATH_Arc(dc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2,0) &&
720 CloseFigure(dc->hSelf) );
723 /* PATH_Arc
725 * Should be called when a call to Arc is performed on a DC that has
726 * an open path. This adds up to five Bezier splines representing the arc
727 * to the path. When 'lines' is 1, we add 1 extra line to get a chord,
728 * when 'lines' is 2, we add 2 extra lines to get a pie, and when 'lines' is
729 * -1 we add 1 extra line from the current DC position to the starting position
730 * of the arc before drawing the arc itself (arcto). Returns TRUE if successful,
731 * else FALSE.
733 BOOL PATH_Arc(DC *dc, INT x1, INT y1, INT x2, INT y2,
734 INT xStart, INT yStart, INT xEnd, INT yEnd, INT lines)
736 GdiPath *pPath = &dc->path;
737 double angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
738 /* Initialize angleEndQuadrant to silence gcc's warning */
739 double x, y;
740 FLOAT_POINT corners[2], pointStart, pointEnd;
741 POINT centre, pointCurPos;
742 BOOL start, end;
743 INT temp;
745 /* FIXME: This function should check for all possible error returns */
746 /* FIXME: Do we have to respect newStroke? */
748 /* Check that path is open */
749 if(pPath->state!=PATH_Open)
750 return FALSE;
752 /* Check for zero height / width */
753 /* FIXME: Only in GM_COMPATIBLE? */
754 if(x1==x2 || y1==y2)
755 return TRUE;
757 /* Convert points to device coordinates */
758 corners[0].x = x1;
759 corners[0].y = y1;
760 corners[1].x = x2;
761 corners[1].y = y2;
762 pointStart.x = xStart;
763 pointStart.y = yStart;
764 pointEnd.x = xEnd;
765 pointEnd.y = yEnd;
766 INTERNAL_LPTODP_FLOAT(dc, corners);
767 INTERNAL_LPTODP_FLOAT(dc, corners+1);
768 INTERNAL_LPTODP_FLOAT(dc, &pointStart);
769 INTERNAL_LPTODP_FLOAT(dc, &pointEnd);
771 /* Make sure first corner is top left and second corner is bottom right */
772 if(corners[0].x>corners[1].x)
774 temp=corners[0].x;
775 corners[0].x=corners[1].x;
776 corners[1].x=temp;
778 if(corners[0].y>corners[1].y)
780 temp=corners[0].y;
781 corners[0].y=corners[1].y;
782 corners[1].y=temp;
785 /* Compute start and end angle */
786 PATH_NormalizePoint(corners, &pointStart, &x, &y);
787 angleStart=atan2(y, x);
788 PATH_NormalizePoint(corners, &pointEnd, &x, &y);
789 angleEnd=atan2(y, x);
791 /* Make sure the end angle is "on the right side" of the start angle */
792 if(dc->ArcDirection==AD_CLOCKWISE)
794 if(angleEnd<=angleStart)
796 angleEnd+=2*M_PI;
797 assert(angleEnd>=angleStart);
800 else
802 if(angleEnd>=angleStart)
804 angleEnd-=2*M_PI;
805 assert(angleEnd<=angleStart);
809 /* In GM_COMPATIBLE, don't include bottom and right edges */
810 if(dc->GraphicsMode==GM_COMPATIBLE)
812 corners[1].x--;
813 corners[1].y--;
816 /* arcto: Add a PT_MOVETO only if this is the first entry in a stroke */
817 if(lines==-1 && pPath->newStroke)
819 pPath->newStroke=FALSE;
820 pointCurPos.x = dc->CursPosX;
821 pointCurPos.y = dc->CursPosY;
822 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
823 return FALSE;
824 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
825 return FALSE;
828 /* Add the arc to the path with one Bezier spline per quadrant that the
829 * arc spans */
830 start=TRUE;
831 end=FALSE;
834 /* Determine the start and end angles for this quadrant */
835 if(start)
837 angleStartQuadrant=angleStart;
838 if(dc->ArcDirection==AD_CLOCKWISE)
839 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
840 else
841 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
843 else
845 angleStartQuadrant=angleEndQuadrant;
846 if(dc->ArcDirection==AD_CLOCKWISE)
847 angleEndQuadrant+=M_PI_2;
848 else
849 angleEndQuadrant-=M_PI_2;
852 /* Have we reached the last part of the arc? */
853 if((dc->ArcDirection==AD_CLOCKWISE &&
854 angleEnd<angleEndQuadrant) ||
855 (dc->ArcDirection==AD_COUNTERCLOCKWISE &&
856 angleEnd>angleEndQuadrant))
858 /* Adjust the end angle for this quadrant */
859 angleEndQuadrant=angleEnd;
860 end=TRUE;
863 /* Add the Bezier spline to the path */
864 PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
865 start ? (lines==-1 ? PT_LINETO : PT_MOVETO) : FALSE);
866 start=FALSE;
867 } while(!end);
869 /* chord: close figure. pie: add line and close figure */
870 if(lines==1)
872 if(!CloseFigure(dc->hSelf))
873 return FALSE;
875 else if(lines==2)
877 centre.x = (corners[0].x+corners[1].x)/2;
878 centre.y = (corners[0].y+corners[1].y)/2;
879 if(!PATH_AddEntry(pPath, &centre, PT_LINETO | PT_CLOSEFIGURE))
880 return FALSE;
883 return TRUE;
886 BOOL PATH_PolyBezierTo(DC *dc, const POINT *pts, DWORD cbPoints)
888 GdiPath *pPath = &dc->path;
889 POINT pt;
890 UINT i;
892 /* Check that path is open */
893 if(pPath->state!=PATH_Open)
894 return FALSE;
896 /* Add a PT_MOVETO if necessary */
897 if(pPath->newStroke)
899 pPath->newStroke=FALSE;
900 pt.x = dc->CursPosX;
901 pt.y = dc->CursPosY;
902 if(!LPtoDP(dc->hSelf, &pt, 1))
903 return FALSE;
904 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
905 return FALSE;
908 for(i = 0; i < cbPoints; i++) {
909 pt = pts[i];
910 if(!LPtoDP(dc->hSelf, &pt, 1))
911 return FALSE;
912 PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
914 return TRUE;
917 BOOL PATH_PolyBezier(DC *dc, const POINT *pts, DWORD cbPoints)
919 GdiPath *pPath = &dc->path;
920 POINT pt;
921 UINT i;
923 /* Check that path is open */
924 if(pPath->state!=PATH_Open)
925 return FALSE;
927 for(i = 0; i < cbPoints; i++) {
928 pt = pts[i];
929 if(!LPtoDP(dc->hSelf, &pt, 1))
930 return FALSE;
931 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
933 return TRUE;
936 /* PATH_PolyDraw
938 * Should be called when a call to PolyDraw is performed on a DC that has
939 * an open path. Returns TRUE if successful, else FALSE.
941 BOOL PATH_PolyDraw(DC *dc, const POINT *pts, const BYTE *types,
942 DWORD cbPoints)
944 GdiPath *pPath = &dc->path;
945 POINT lastmove, orig_pos;
946 INT i;
948 lastmove.x = orig_pos.x = dc->CursPosX;
949 lastmove.y = orig_pos.y = dc->CursPosY;
951 for(i = pPath->numEntriesUsed - 1; i >= 0; i--){
952 if(pPath->pFlags[i] == PT_MOVETO){
953 lastmove.x = pPath->pPoints[i].x;
954 lastmove.y = pPath->pPoints[i].y;
955 if(!DPtoLP(dc->hSelf, &lastmove, 1))
956 return FALSE;
957 break;
961 for(i = 0; i < cbPoints; i++){
962 if(types[i] == PT_MOVETO){
963 pPath->newStroke = TRUE;
964 lastmove.x = pts[i].x;
965 lastmove.y = pts[i].y;
967 else if((types[i] & ~PT_CLOSEFIGURE) == PT_LINETO){
968 PATH_LineTo(dc, pts[i].x, pts[i].y);
970 else if(types[i] == PT_BEZIERTO){
971 if(!((i + 2 < cbPoints) && (types[i + 1] == PT_BEZIERTO)
972 && ((types[i + 2] & ~PT_CLOSEFIGURE) == PT_BEZIERTO)))
973 goto err;
974 PATH_PolyBezierTo(dc, &(pts[i]), 3);
975 i += 2;
977 else
978 goto err;
980 dc->CursPosX = pts[i].x;
981 dc->CursPosY = pts[i].y;
983 if(types[i] & PT_CLOSEFIGURE){
984 pPath->pFlags[pPath->numEntriesUsed-1] |= PT_CLOSEFIGURE;
985 pPath->newStroke = TRUE;
986 dc->CursPosX = lastmove.x;
987 dc->CursPosY = lastmove.y;
991 return TRUE;
993 err:
994 if((dc->CursPosX != orig_pos.x) || (dc->CursPosY != orig_pos.y)){
995 pPath->newStroke = TRUE;
996 dc->CursPosX = orig_pos.x;
997 dc->CursPosY = orig_pos.y;
1000 return FALSE;
1003 BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
1005 GdiPath *pPath = &dc->path;
1006 POINT pt;
1007 UINT i;
1009 /* Check that path is open */
1010 if(pPath->state!=PATH_Open)
1011 return FALSE;
1013 for(i = 0; i < cbPoints; i++) {
1014 pt = pts[i];
1015 if(!LPtoDP(dc->hSelf, &pt, 1))
1016 return FALSE;
1017 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
1019 return TRUE;
1022 BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
1024 GdiPath *pPath = &dc->path;
1025 POINT pt;
1026 UINT i;
1028 /* Check that path is open */
1029 if(pPath->state!=PATH_Open)
1030 return FALSE;
1032 /* Add a PT_MOVETO if necessary */
1033 if(pPath->newStroke)
1035 pPath->newStroke=FALSE;
1036 pt.x = dc->CursPosX;
1037 pt.y = dc->CursPosY;
1038 if(!LPtoDP(dc->hSelf, &pt, 1))
1039 return FALSE;
1040 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
1041 return FALSE;
1044 for(i = 0; i < cbPoints; i++) {
1045 pt = pts[i];
1046 if(!LPtoDP(dc->hSelf, &pt, 1))
1047 return FALSE;
1048 PATH_AddEntry(pPath, &pt, PT_LINETO);
1051 return TRUE;
1055 BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
1057 GdiPath *pPath = &dc->path;
1058 POINT pt;
1059 UINT i;
1061 /* Check that path is open */
1062 if(pPath->state!=PATH_Open)
1063 return FALSE;
1065 for(i = 0; i < cbPoints; i++) {
1066 pt = pts[i];
1067 if(!LPtoDP(dc->hSelf, &pt, 1))
1068 return FALSE;
1069 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
1070 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
1071 PT_LINETO));
1073 return TRUE;
1076 BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
1077 UINT polygons )
1079 GdiPath *pPath = &dc->path;
1080 POINT pt, startpt;
1081 UINT poly, i;
1082 INT point;
1084 /* Check that path is open */
1085 if(pPath->state!=PATH_Open)
1086 return FALSE;
1088 for(i = 0, poly = 0; poly < polygons; poly++) {
1089 for(point = 0; point < counts[poly]; point++, i++) {
1090 pt = pts[i];
1091 if(!LPtoDP(dc->hSelf, &pt, 1))
1092 return FALSE;
1093 if(point == 0) startpt = pt;
1094 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1096 /* win98 adds an extra line to close the figure for some reason */
1097 PATH_AddEntry(pPath, &startpt, PT_LINETO | PT_CLOSEFIGURE);
1099 return TRUE;
1102 BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1103 DWORD polylines )
1105 GdiPath *pPath = &dc->path;
1106 POINT pt;
1107 UINT poly, point, i;
1109 /* Check that path is open */
1110 if(pPath->state!=PATH_Open)
1111 return FALSE;
1113 for(i = 0, poly = 0; poly < polylines; poly++) {
1114 for(point = 0; point < counts[poly]; point++, i++) {
1115 pt = pts[i];
1116 if(!LPtoDP(dc->hSelf, &pt, 1))
1117 return FALSE;
1118 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1121 return TRUE;
1124 /***********************************************************************
1125 * Internal functions
1128 /* PATH_CheckCorners
1130 * Helper function for PATH_RoundRect() and PATH_Rectangle()
1132 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2)
1134 INT temp;
1136 /* Convert points to device coordinates */
1137 corners[0].x=x1;
1138 corners[0].y=y1;
1139 corners[1].x=x2;
1140 corners[1].y=y2;
1141 if(!LPtoDP(dc->hSelf, corners, 2))
1142 return FALSE;
1144 /* Make sure first corner is top left and second corner is bottom right */
1145 if(corners[0].x>corners[1].x)
1147 temp=corners[0].x;
1148 corners[0].x=corners[1].x;
1149 corners[1].x=temp;
1151 if(corners[0].y>corners[1].y)
1153 temp=corners[0].y;
1154 corners[0].y=corners[1].y;
1155 corners[1].y=temp;
1158 /* In GM_COMPATIBLE, don't include bottom and right edges */
1159 if(dc->GraphicsMode==GM_COMPATIBLE)
1161 corners[1].x--;
1162 corners[1].y--;
1165 return TRUE;
1168 /* PATH_AddFlatBezier
1170 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
1172 POINT *pts;
1173 INT no, i;
1175 pts = GDI_Bezier( pt, 4, &no );
1176 if(!pts) return FALSE;
1178 for(i = 1; i < no; i++)
1179 PATH_AddEntry(pPath, &pts[i],
1180 (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
1181 HeapFree( GetProcessHeap(), 0, pts );
1182 return TRUE;
1185 /* PATH_FlattenPath
1187 * Replaces Beziers with line segments
1190 static BOOL PATH_FlattenPath(GdiPath *pPath)
1192 GdiPath newPath;
1193 INT srcpt;
1195 memset(&newPath, 0, sizeof(newPath));
1196 newPath.state = PATH_Open;
1197 for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
1198 switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
1199 case PT_MOVETO:
1200 case PT_LINETO:
1201 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
1202 pPath->pFlags[srcpt]);
1203 break;
1204 case PT_BEZIERTO:
1205 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
1206 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
1207 srcpt += 2;
1208 break;
1211 newPath.state = PATH_Closed;
1212 PATH_AssignGdiPath(pPath, &newPath);
1213 PATH_DestroyGdiPath(&newPath);
1214 return TRUE;
1217 /* PATH_PathToRegion
1219 * Creates a region from the specified path using the specified polygon
1220 * filling mode. The path is left unchanged. A handle to the region that
1221 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
1222 * error occurs, SetLastError is called with the appropriate value and
1223 * FALSE is returned.
1225 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
1226 HRGN *pHrgn)
1228 int numStrokes, iStroke, i;
1229 INT *pNumPointsInStroke;
1230 HRGN hrgn;
1232 assert(pPath!=NULL);
1233 assert(pHrgn!=NULL);
1235 PATH_FlattenPath(pPath);
1237 /* FIXME: What happens when number of points is zero? */
1239 /* First pass: Find out how many strokes there are in the path */
1240 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
1241 numStrokes=0;
1242 for(i=0; i<pPath->numEntriesUsed; i++)
1243 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1244 numStrokes++;
1246 /* Allocate memory for number-of-points-in-stroke array */
1247 pNumPointsInStroke=HeapAlloc( GetProcessHeap(), 0, sizeof(int) * numStrokes );
1248 if(!pNumPointsInStroke)
1250 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1251 return FALSE;
1254 /* Second pass: remember number of points in each polygon */
1255 iStroke=-1; /* Will get incremented to 0 at beginning of first stroke */
1256 for(i=0; i<pPath->numEntriesUsed; i++)
1258 /* Is this the beginning of a new stroke? */
1259 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1261 iStroke++;
1262 pNumPointsInStroke[iStroke]=0;
1265 pNumPointsInStroke[iStroke]++;
1268 /* Create a region from the strokes */
1269 hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
1270 numStrokes, nPolyFillMode);
1272 /* Free memory for number-of-points-in-stroke array */
1273 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
1275 if(hrgn==NULL)
1277 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1278 return FALSE;
1281 /* Success! */
1282 *pHrgn=hrgn;
1283 return TRUE;
1286 static inline INT int_from_fixed(FIXED f)
1288 return (f.fract >= 0x8000) ? (f.value + 1) : f.value;
1291 /**********************************************************************
1292 * PATH_BezierTo
1294 * internally used by PATH_add_outline
1296 static void PATH_BezierTo(GdiPath *pPath, POINT *lppt, INT n)
1298 if (n < 2) return;
1300 if (n == 2)
1302 PATH_AddEntry(pPath, &lppt[1], PT_LINETO);
1304 else if (n == 3)
1306 PATH_AddEntry(pPath, &lppt[0], PT_BEZIERTO);
1307 PATH_AddEntry(pPath, &lppt[1], PT_BEZIERTO);
1308 PATH_AddEntry(pPath, &lppt[2], PT_BEZIERTO);
1310 else
1312 POINT pt[3];
1313 INT i = 0;
1315 pt[2] = lppt[0];
1316 n--;
1318 while (n > 2)
1320 pt[0] = pt[2];
1321 pt[1] = lppt[i+1];
1322 pt[2].x = (lppt[i+2].x + lppt[i+1].x) / 2;
1323 pt[2].y = (lppt[i+2].y + lppt[i+1].y) / 2;
1324 PATH_BezierTo(pPath, pt, 3);
1325 n--;
1326 i++;
1329 pt[0] = pt[2];
1330 pt[1] = lppt[i+1];
1331 pt[2] = lppt[i+2];
1332 PATH_BezierTo(pPath, pt, 3);
1336 static BOOL PATH_add_outline(DC *dc, INT x, INT y, TTPOLYGONHEADER *header, DWORD size)
1338 GdiPath *pPath = &dc->path;
1339 TTPOLYGONHEADER *start;
1340 POINT pt;
1342 start = header;
1344 while ((char *)header < (char *)start + size)
1346 TTPOLYCURVE *curve;
1348 if (header->dwType != TT_POLYGON_TYPE)
1350 FIXME("Unknown header type %d\n", header->dwType);
1351 return FALSE;
1354 pt.x = x + int_from_fixed(header->pfxStart.x);
1355 pt.y = y - int_from_fixed(header->pfxStart.y);
1356 LPtoDP(dc->hSelf, &pt, 1);
1357 PATH_AddEntry(pPath, &pt, PT_MOVETO);
1359 curve = (TTPOLYCURVE *)(header + 1);
1361 while ((char *)curve < (char *)header + header->cb)
1363 /*TRACE("curve->wType %d\n", curve->wType);*/
1365 switch(curve->wType)
1367 case TT_PRIM_LINE:
1369 WORD i;
1371 for (i = 0; i < curve->cpfx; i++)
1373 pt.x = x + int_from_fixed(curve->apfx[i].x);
1374 pt.y = y - int_from_fixed(curve->apfx[i].y);
1375 LPtoDP(dc->hSelf, &pt, 1);
1376 PATH_AddEntry(pPath, &pt, PT_LINETO);
1378 break;
1381 case TT_PRIM_QSPLINE:
1382 case TT_PRIM_CSPLINE:
1384 WORD i;
1385 POINTFX ptfx;
1386 POINT *pts = HeapAlloc(GetProcessHeap(), 0, (curve->cpfx + 1) * sizeof(POINT));
1388 if (!pts) return FALSE;
1390 ptfx = *(POINTFX *)((char *)curve - sizeof(POINTFX));
1392 pts[0].x = x + int_from_fixed(ptfx.x);
1393 pts[0].y = y - int_from_fixed(ptfx.y);
1394 LPtoDP(dc->hSelf, &pts[0], 1);
1396 for(i = 0; i < curve->cpfx; i++)
1398 pts[i + 1].x = x + int_from_fixed(curve->apfx[i].x);
1399 pts[i + 1].y = y - int_from_fixed(curve->apfx[i].y);
1400 LPtoDP(dc->hSelf, &pts[i + 1], 1);
1403 PATH_BezierTo(pPath, pts, curve->cpfx + 1);
1405 HeapFree(GetProcessHeap(), 0, pts);
1406 break;
1409 default:
1410 FIXME("Unknown curve type %04x\n", curve->wType);
1411 return FALSE;
1414 curve = (TTPOLYCURVE *)&curve->apfx[curve->cpfx];
1417 header = (TTPOLYGONHEADER *)((char *)header + header->cb);
1420 return CloseFigure(dc->hSelf);
1423 /**********************************************************************
1424 * PATH_ExtTextOut
1426 BOOL PATH_ExtTextOut(DC *dc, INT x, INT y, UINT flags, const RECT *lprc,
1427 LPCWSTR str, UINT count, const INT *dx)
1429 unsigned int idx;
1430 double cosEsc, sinEsc;
1431 LOGFONTW lf;
1432 POINT org;
1433 HDC hdc = dc->hSelf;
1434 INT offset = 0, xoff = 0, yoff = 0;
1436 TRACE("%p, %d, %d, %08x, %s, %s, %d, %p)\n", hdc, x, y, flags,
1437 wine_dbgstr_rect(lprc), debugstr_wn(str, count), count, dx);
1439 if (!count) return TRUE;
1441 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1443 if (lf.lfEscapement != 0)
1445 cosEsc = cos(lf.lfEscapement * M_PI / 1800);
1446 sinEsc = sin(lf.lfEscapement * M_PI / 1800);
1447 } else
1449 cosEsc = 1;
1450 sinEsc = 0;
1453 GetDCOrgEx(hdc, &org);
1455 for (idx = 0; idx < count; idx++)
1457 static const MAT2 identity = { {0,1},{0,0},{0,0},{0,1} };
1458 GLYPHMETRICS gm;
1459 DWORD dwSize;
1460 void *outline;
1462 dwSize = GetGlyphOutlineW(hdc, str[idx], GGO_GLYPH_INDEX | GGO_NATIVE, &gm, 0, NULL, &identity);
1463 if (dwSize == GDI_ERROR) return FALSE;
1465 /* add outline only if char is printable */
1466 if(dwSize)
1468 outline = HeapAlloc(GetProcessHeap(), 0, dwSize);
1469 if (!outline) return FALSE;
1471 GetGlyphOutlineW(hdc, str[idx], GGO_GLYPH_INDEX | GGO_NATIVE, &gm, dwSize, outline, &identity);
1473 PATH_add_outline(dc, org.x + x + xoff, org.x + y + yoff, outline, dwSize);
1475 HeapFree(GetProcessHeap(), 0, outline);
1478 if (dx)
1480 offset += dx[idx];
1481 xoff = offset * cosEsc;
1482 yoff = offset * -sinEsc;
1484 else
1486 xoff += gm.gmCellIncX;
1487 yoff += gm.gmCellIncY;
1490 return TRUE;
1493 /* PATH_EmptyPath
1495 * Removes all entries from the path and sets the path state to PATH_Null.
1497 static void PATH_EmptyPath(GdiPath *pPath)
1499 assert(pPath!=NULL);
1501 pPath->state=PATH_Null;
1502 pPath->numEntriesUsed=0;
1505 /* PATH_AddEntry
1507 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1508 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1509 * successful, FALSE otherwise (e.g. if not enough memory was available).
1511 static BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
1513 assert(pPath!=NULL);
1515 /* FIXME: If newStroke is true, perhaps we want to check that we're
1516 * getting a PT_MOVETO
1518 TRACE("(%d,%d) - %d\n", pPoint->x, pPoint->y, flags);
1520 /* Check that path is open */
1521 if(pPath->state!=PATH_Open)
1522 return FALSE;
1524 /* Reserve enough memory for an extra path entry */
1525 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
1526 return FALSE;
1528 /* Store information in path entry */
1529 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
1530 pPath->pFlags[pPath->numEntriesUsed]=flags;
1532 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1533 if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
1534 pPath->newStroke=TRUE;
1536 /* Increment entry count */
1537 pPath->numEntriesUsed++;
1539 return TRUE;
1542 /* PATH_ReserveEntries
1544 * Ensures that at least "numEntries" entries (for points and flags) have
1545 * been allocated; allocates larger arrays and copies the existing entries
1546 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1548 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries)
1550 INT numEntriesToAllocate;
1551 POINT *pPointsNew;
1552 BYTE *pFlagsNew;
1554 assert(pPath!=NULL);
1555 assert(numEntries>=0);
1557 /* Do we have to allocate more memory? */
1558 if(numEntries > pPath->numEntriesAllocated)
1560 /* Find number of entries to allocate. We let the size of the array
1561 * grow exponentially, since that will guarantee linear time
1562 * complexity. */
1563 if(pPath->numEntriesAllocated)
1565 numEntriesToAllocate=pPath->numEntriesAllocated;
1566 while(numEntriesToAllocate<numEntries)
1567 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
1568 GROW_FACTOR_DENOM;
1570 else
1571 numEntriesToAllocate=numEntries;
1573 /* Allocate new arrays */
1574 pPointsNew=HeapAlloc( GetProcessHeap(), 0, numEntriesToAllocate * sizeof(POINT) );
1575 if(!pPointsNew)
1576 return FALSE;
1577 pFlagsNew=HeapAlloc( GetProcessHeap(), 0, numEntriesToAllocate * sizeof(BYTE) );
1578 if(!pFlagsNew)
1580 HeapFree( GetProcessHeap(), 0, pPointsNew );
1581 return FALSE;
1584 /* Copy old arrays to new arrays and discard old arrays */
1585 if(pPath->pPoints)
1587 assert(pPath->pFlags);
1589 memcpy(pPointsNew, pPath->pPoints,
1590 sizeof(POINT)*pPath->numEntriesUsed);
1591 memcpy(pFlagsNew, pPath->pFlags,
1592 sizeof(BYTE)*pPath->numEntriesUsed);
1594 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
1595 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
1597 pPath->pPoints=pPointsNew;
1598 pPath->pFlags=pFlagsNew;
1599 pPath->numEntriesAllocated=numEntriesToAllocate;
1602 return TRUE;
1605 /* PATH_DoArcPart
1607 * Creates a Bezier spline that corresponds to part of an arc and appends the
1608 * corresponding points to the path. The start and end angles are passed in
1609 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1610 * at most. If "startEntryType" is non-zero, an entry of that type for the first
1611 * control point is added to the path; otherwise, it is assumed that the current
1612 * position is equal to the first control point.
1614 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
1615 double angleStart, double angleEnd, BYTE startEntryType)
1617 double halfAngle, a;
1618 double xNorm[4], yNorm[4];
1619 POINT point;
1620 int i;
1622 assert(fabs(angleEnd-angleStart)<=M_PI_2);
1624 /* FIXME: Is there an easier way of computing this? */
1626 /* Compute control points */
1627 halfAngle=(angleEnd-angleStart)/2.0;
1628 if(fabs(halfAngle)>1e-8)
1630 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
1631 xNorm[0]=cos(angleStart);
1632 yNorm[0]=sin(angleStart);
1633 xNorm[1]=xNorm[0] - a*yNorm[0];
1634 yNorm[1]=yNorm[0] + a*xNorm[0];
1635 xNorm[3]=cos(angleEnd);
1636 yNorm[3]=sin(angleEnd);
1637 xNorm[2]=xNorm[3] + a*yNorm[3];
1638 yNorm[2]=yNorm[3] - a*xNorm[3];
1640 else
1641 for(i=0; i<4; i++)
1643 xNorm[i]=cos(angleStart);
1644 yNorm[i]=sin(angleStart);
1647 /* Add starting point to path if desired */
1648 if(startEntryType)
1650 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1651 if(!PATH_AddEntry(pPath, &point, startEntryType))
1652 return FALSE;
1655 /* Add remaining control points */
1656 for(i=1; i<4; i++)
1658 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1659 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1660 return FALSE;
1663 return TRUE;
1666 /* PATH_ScaleNormalizedPoint
1668 * Scales a normalized point (x, y) with respect to the box whose corners are
1669 * passed in "corners". The point is stored in "*pPoint". The normalized
1670 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1671 * (1.0, 1.0) correspond to corners[1].
1673 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
1674 double y, POINT *pPoint)
1676 pPoint->x=GDI_ROUND( (double)corners[0].x +
1677 (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
1678 pPoint->y=GDI_ROUND( (double)corners[0].y +
1679 (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
1682 /* PATH_NormalizePoint
1684 * Normalizes a point with respect to the box whose corners are passed in
1685 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1687 static void PATH_NormalizePoint(FLOAT_POINT corners[],
1688 const FLOAT_POINT *pPoint,
1689 double *pX, double *pY)
1691 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) *
1692 2.0 - 1.0;
1693 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1694 2.0 - 1.0;
1698 /*******************************************************************
1699 * FlattenPath [GDI32.@]
1703 BOOL WINAPI FlattenPath(HDC hdc)
1705 BOOL ret = FALSE;
1706 DC *dc = get_dc_ptr( hdc );
1708 if(!dc) return FALSE;
1710 if(dc->funcs->pFlattenPath) ret = dc->funcs->pFlattenPath(dc->physDev);
1711 else
1713 GdiPath *pPath = &dc->path;
1714 if(pPath->state != PATH_Closed)
1715 ret = PATH_FlattenPath(pPath);
1717 release_dc_ptr( dc );
1718 return ret;
1722 static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1724 INT i, nLinePts, nAlloc;
1725 POINT *pLinePts;
1726 POINT ptViewportOrg, ptWindowOrg;
1727 SIZE szViewportExt, szWindowExt;
1728 DWORD mapMode, graphicsMode;
1729 XFORM xform;
1730 BOOL ret = TRUE;
1732 if(dc->funcs->pStrokePath)
1733 return dc->funcs->pStrokePath(dc->physDev);
1735 if(pPath->state != PATH_Closed)
1736 return FALSE;
1738 /* Save the mapping mode info */
1739 mapMode=GetMapMode(dc->hSelf);
1740 GetViewportExtEx(dc->hSelf, &szViewportExt);
1741 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
1742 GetWindowExtEx(dc->hSelf, &szWindowExt);
1743 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
1744 GetWorldTransform(dc->hSelf, &xform);
1746 /* Set MM_TEXT */
1747 SetMapMode(dc->hSelf, MM_TEXT);
1748 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
1749 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
1750 graphicsMode=GetGraphicsMode(dc->hSelf);
1751 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1752 ModifyWorldTransform(dc->hSelf, &xform, MWT_IDENTITY);
1753 SetGraphicsMode(dc->hSelf, graphicsMode);
1755 /* Allocate enough memory for the worst case without beziers (one PT_MOVETO
1756 * and the rest PT_LINETO with PT_CLOSEFIGURE at the end) plus some buffer
1757 * space in case we get one to keep the number of reallocations small. */
1758 nAlloc = pPath->numEntriesUsed + 1 + 300;
1759 pLinePts = HeapAlloc(GetProcessHeap(), 0, nAlloc * sizeof(POINT));
1760 nLinePts = 0;
1762 for(i = 0; i < pPath->numEntriesUsed; i++) {
1763 if((i == 0 || (pPath->pFlags[i-1] & PT_CLOSEFIGURE)) &&
1764 (pPath->pFlags[i] != PT_MOVETO)) {
1765 ERR("Expected PT_MOVETO %s, got path flag %d\n",
1766 i == 0 ? "as first point" : "after PT_CLOSEFIGURE",
1767 (INT)pPath->pFlags[i]);
1768 ret = FALSE;
1769 goto end;
1771 switch(pPath->pFlags[i]) {
1772 case PT_MOVETO:
1773 TRACE("Got PT_MOVETO (%d, %d)\n",
1774 pPath->pPoints[i].x, pPath->pPoints[i].y);
1775 if(nLinePts >= 2)
1776 Polyline(dc->hSelf, pLinePts, nLinePts);
1777 nLinePts = 0;
1778 pLinePts[nLinePts++] = pPath->pPoints[i];
1779 break;
1780 case PT_LINETO:
1781 case (PT_LINETO | PT_CLOSEFIGURE):
1782 TRACE("Got PT_LINETO (%d, %d)\n",
1783 pPath->pPoints[i].x, pPath->pPoints[i].y);
1784 pLinePts[nLinePts++] = pPath->pPoints[i];
1785 break;
1786 case PT_BEZIERTO:
1787 TRACE("Got PT_BEZIERTO\n");
1788 if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1789 (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1790 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1791 ret = FALSE;
1792 goto end;
1793 } else {
1794 INT nBzrPts, nMinAlloc;
1795 POINT *pBzrPts = GDI_Bezier(&pPath->pPoints[i-1], 4, &nBzrPts);
1796 /* Make sure we have allocated enough memory for the lines of
1797 * this bezier and the rest of the path, assuming we won't get
1798 * another one (since we won't reallocate again then). */
1799 nMinAlloc = nLinePts + (pPath->numEntriesUsed - i) + nBzrPts;
1800 if(nAlloc < nMinAlloc)
1802 nAlloc = nMinAlloc * 2;
1803 pLinePts = HeapReAlloc(GetProcessHeap(), 0, pLinePts,
1804 nAlloc * sizeof(POINT));
1806 memcpy(&pLinePts[nLinePts], &pBzrPts[1],
1807 (nBzrPts - 1) * sizeof(POINT));
1808 nLinePts += nBzrPts - 1;
1809 HeapFree(GetProcessHeap(), 0, pBzrPts);
1810 i += 2;
1812 break;
1813 default:
1814 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1815 ret = FALSE;
1816 goto end;
1818 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1819 pLinePts[nLinePts++] = pLinePts[0];
1821 if(nLinePts >= 2)
1822 Polyline(dc->hSelf, pLinePts, nLinePts);
1824 end:
1825 HeapFree(GetProcessHeap(), 0, pLinePts);
1827 /* Restore the old mapping mode */
1828 SetMapMode(dc->hSelf, mapMode);
1829 SetWindowExtEx(dc->hSelf, szWindowExt.cx, szWindowExt.cy, NULL);
1830 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
1831 SetViewportExtEx(dc->hSelf, szViewportExt.cx, szViewportExt.cy, NULL);
1832 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
1834 /* Go to GM_ADVANCED temporarily to restore the world transform */
1835 graphicsMode=GetGraphicsMode(dc->hSelf);
1836 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1837 SetWorldTransform(dc->hSelf, &xform);
1838 SetGraphicsMode(dc->hSelf, graphicsMode);
1840 /* If we've moved the current point then get its new position
1841 which will be in device (MM_TEXT) co-ords, convert it to
1842 logical co-ords and re-set it. This basically updates
1843 dc->CurPosX|Y so that their values are in the correct mapping
1844 mode.
1846 if(i > 0) {
1847 POINT pt;
1848 GetCurrentPositionEx(dc->hSelf, &pt);
1849 DPtoLP(dc->hSelf, &pt, 1);
1850 MoveToEx(dc->hSelf, pt.x, pt.y, NULL);
1853 return ret;
1856 #define round(x) ((int)((x)>0?(x)+0.5:(x)-0.5))
1858 static BOOL PATH_WidenPath(DC *dc)
1860 INT i, j, numStrokes, penWidth, penWidthIn, penWidthOut, size, penStyle;
1861 BOOL ret = FALSE;
1862 GdiPath *pPath, *pNewPath, **pStrokes, *pUpPath, *pDownPath;
1863 EXTLOGPEN *elp;
1864 DWORD obj_type, joint, endcap, penType;
1866 pPath = &dc->path;
1868 if(pPath->state == PATH_Open) {
1869 SetLastError(ERROR_CAN_NOT_COMPLETE);
1870 return FALSE;
1873 PATH_FlattenPath(pPath);
1875 size = GetObjectW( dc->hPen, 0, NULL );
1876 if (!size) {
1877 SetLastError(ERROR_CAN_NOT_COMPLETE);
1878 return FALSE;
1881 elp = HeapAlloc( GetProcessHeap(), 0, size );
1882 GetObjectW( dc->hPen, size, elp );
1884 obj_type = GetObjectType(dc->hPen);
1885 if(obj_type == OBJ_PEN) {
1886 penStyle = ((LOGPEN*)elp)->lopnStyle;
1888 else if(obj_type == OBJ_EXTPEN) {
1889 penStyle = elp->elpPenStyle;
1891 else {
1892 SetLastError(ERROR_CAN_NOT_COMPLETE);
1893 HeapFree( GetProcessHeap(), 0, elp );
1894 return FALSE;
1897 penWidth = elp->elpWidth;
1898 HeapFree( GetProcessHeap(), 0, elp );
1900 endcap = (PS_ENDCAP_MASK & penStyle);
1901 joint = (PS_JOIN_MASK & penStyle);
1902 penType = (PS_TYPE_MASK & penStyle);
1904 /* The function cannot apply to cosmetic pens */
1905 if(obj_type == OBJ_EXTPEN && penType == PS_COSMETIC) {
1906 SetLastError(ERROR_CAN_NOT_COMPLETE);
1907 return FALSE;
1910 penWidthIn = penWidth / 2;
1911 penWidthOut = penWidth / 2;
1912 if(penWidthIn + penWidthOut < penWidth)
1913 penWidthOut++;
1915 numStrokes = 0;
1917 pStrokes = HeapAlloc(GetProcessHeap(), 0, numStrokes * sizeof(GdiPath*));
1918 pStrokes[0] = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1919 PATH_InitGdiPath(pStrokes[0]);
1920 pStrokes[0]->pFlags = HeapAlloc(GetProcessHeap(), 0, pPath->numEntriesUsed * sizeof(INT));
1921 pStrokes[0]->pPoints = HeapAlloc(GetProcessHeap(), 0, pPath->numEntriesUsed * sizeof(POINT));
1922 pStrokes[0]->numEntriesUsed = 0;
1924 for(i = 0, j = 0; i < pPath->numEntriesUsed; i++, j++) {
1925 POINT point;
1926 if((i == 0 || (pPath->pFlags[i-1] & PT_CLOSEFIGURE)) &&
1927 (pPath->pFlags[i] != PT_MOVETO)) {
1928 ERR("Expected PT_MOVETO %s, got path flag %c\n",
1929 i == 0 ? "as first point" : "after PT_CLOSEFIGURE",
1930 pPath->pFlags[i]);
1931 return FALSE;
1933 switch(pPath->pFlags[i]) {
1934 case PT_MOVETO:
1935 if(numStrokes > 0) {
1936 pStrokes[numStrokes - 1]->state = PATH_Closed;
1938 numStrokes++;
1939 j = 0;
1940 pStrokes = HeapReAlloc(GetProcessHeap(), 0, pStrokes, numStrokes * sizeof(GdiPath*));
1941 pStrokes[numStrokes - 1] = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1942 PATH_InitGdiPath(pStrokes[numStrokes - 1]);
1943 pStrokes[numStrokes - 1]->state = PATH_Open;
1944 case PT_LINETO:
1945 case (PT_LINETO | PT_CLOSEFIGURE):
1946 point.x = pPath->pPoints[i].x;
1947 point.y = pPath->pPoints[i].y;
1948 PATH_AddEntry(pStrokes[numStrokes - 1], &point, pPath->pFlags[i]);
1949 break;
1950 case PT_BEZIERTO:
1951 /* should never happen because of the FlattenPath call */
1952 ERR("Should never happen\n");
1953 break;
1954 default:
1955 ERR("Got path flag %c\n", pPath->pFlags[i]);
1956 return FALSE;
1960 pNewPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1961 PATH_InitGdiPath(pNewPath);
1962 pNewPath->state = PATH_Open;
1964 for(i = 0; i < numStrokes; i++) {
1965 pUpPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1966 PATH_InitGdiPath(pUpPath);
1967 pUpPath->state = PATH_Open;
1968 pDownPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1969 PATH_InitGdiPath(pDownPath);
1970 pDownPath->state = PATH_Open;
1972 for(j = 0; j < pStrokes[i]->numEntriesUsed; j++) {
1973 /* Beginning or end of the path if not closed */
1974 if((!(pStrokes[i]->pFlags[pStrokes[i]->numEntriesUsed - 1] & PT_CLOSEFIGURE)) && (j == 0 || j == pStrokes[i]->numEntriesUsed - 1) ) {
1975 /* Compute segment angle */
1976 double xo, yo, xa, ya, theta;
1977 POINT pt;
1978 FLOAT_POINT corners[2];
1979 if(j == 0) {
1980 xo = pStrokes[i]->pPoints[j].x;
1981 yo = pStrokes[i]->pPoints[j].y;
1982 xa = pStrokes[i]->pPoints[1].x;
1983 ya = pStrokes[i]->pPoints[1].y;
1985 else {
1986 xa = pStrokes[i]->pPoints[j - 1].x;
1987 ya = pStrokes[i]->pPoints[j - 1].y;
1988 xo = pStrokes[i]->pPoints[j].x;
1989 yo = pStrokes[i]->pPoints[j].y;
1991 theta = atan2( ya - yo, xa - xo );
1992 switch(endcap) {
1993 case PS_ENDCAP_SQUARE :
1994 pt.x = xo + round(sqrt(2) * penWidthOut * cos(M_PI_4 + theta));
1995 pt.y = yo + round(sqrt(2) * penWidthOut * sin(M_PI_4 + theta));
1996 PATH_AddEntry(pUpPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO) );
1997 pt.x = xo + round(sqrt(2) * penWidthIn * cos(- M_PI_4 + theta));
1998 pt.y = yo + round(sqrt(2) * penWidthIn * sin(- M_PI_4 + theta));
1999 PATH_AddEntry(pUpPath, &pt, PT_LINETO);
2000 break;
2001 case PS_ENDCAP_FLAT :
2002 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
2003 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
2004 PATH_AddEntry(pUpPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO));
2005 pt.x = xo - round( penWidthIn * cos(theta + M_PI_2) );
2006 pt.y = yo - round( penWidthIn * sin(theta + M_PI_2) );
2007 PATH_AddEntry(pUpPath, &pt, PT_LINETO);
2008 break;
2009 case PS_ENDCAP_ROUND :
2010 default :
2011 corners[0].x = xo - penWidthIn;
2012 corners[0].y = yo - penWidthIn;
2013 corners[1].x = xo + penWidthOut;
2014 corners[1].y = yo + penWidthOut;
2015 PATH_DoArcPart(pUpPath ,corners, theta + M_PI_2 , theta + 3 * M_PI_4, (j == 0 ? PT_MOVETO : FALSE));
2016 PATH_DoArcPart(pUpPath ,corners, theta + 3 * M_PI_4 , theta + M_PI, FALSE);
2017 PATH_DoArcPart(pUpPath ,corners, theta + M_PI, theta + 5 * M_PI_4, FALSE);
2018 PATH_DoArcPart(pUpPath ,corners, theta + 5 * M_PI_4 , theta + 3 * M_PI_2, FALSE);
2019 break;
2022 /* Corpse of the path */
2023 else {
2024 /* Compute angle */
2025 INT previous, next;
2026 double xa, ya, xb, yb, xo, yo;
2027 double alpha, theta, miterWidth;
2028 DWORD _joint = joint;
2029 POINT pt;
2030 GdiPath *pInsidePath, *pOutsidePath;
2031 if(j > 0 && j < pStrokes[i]->numEntriesUsed - 1) {
2032 previous = j - 1;
2033 next = j + 1;
2035 else if (j == 0) {
2036 previous = pStrokes[i]->numEntriesUsed - 1;
2037 next = j + 1;
2039 else {
2040 previous = j - 1;
2041 next = 0;
2043 xo = pStrokes[i]->pPoints[j].x;
2044 yo = pStrokes[i]->pPoints[j].y;
2045 xa = pStrokes[i]->pPoints[previous].x;
2046 ya = pStrokes[i]->pPoints[previous].y;
2047 xb = pStrokes[i]->pPoints[next].x;
2048 yb = pStrokes[i]->pPoints[next].y;
2049 theta = atan2( yo - ya, xo - xa );
2050 alpha = atan2( yb - yo, xb - xo ) - theta;
2051 if (alpha > 0) alpha -= M_PI;
2052 else alpha += M_PI;
2053 if(_joint == PS_JOIN_MITER && dc->miterLimit < fabs(1 / sin(alpha/2))) {
2054 _joint = PS_JOIN_BEVEL;
2056 if(alpha > 0) {
2057 pInsidePath = pUpPath;
2058 pOutsidePath = pDownPath;
2060 else if(alpha < 0) {
2061 pInsidePath = pDownPath;
2062 pOutsidePath = pUpPath;
2064 else {
2065 continue;
2067 /* Inside angle points */
2068 if(alpha > 0) {
2069 pt.x = xo - round( penWidthIn * cos(theta + M_PI_2) );
2070 pt.y = yo - round( penWidthIn * sin(theta + M_PI_2) );
2072 else {
2073 pt.x = xo + round( penWidthIn * cos(theta + M_PI_2) );
2074 pt.y = yo + round( penWidthIn * sin(theta + M_PI_2) );
2076 PATH_AddEntry(pInsidePath, &pt, PT_LINETO);
2077 if(alpha > 0) {
2078 pt.x = xo + round( penWidthIn * cos(M_PI_2 + alpha + theta) );
2079 pt.y = yo + round( penWidthIn * sin(M_PI_2 + alpha + theta) );
2081 else {
2082 pt.x = xo - round( penWidthIn * cos(M_PI_2 + alpha + theta) );
2083 pt.y = yo - round( penWidthIn * sin(M_PI_2 + alpha + theta) );
2085 PATH_AddEntry(pInsidePath, &pt, PT_LINETO);
2086 /* Outside angle point */
2087 switch(_joint) {
2088 case PS_JOIN_MITER :
2089 miterWidth = fabs(penWidthOut / cos(M_PI_2 - fabs(alpha) / 2));
2090 pt.x = xo + round( miterWidth * cos(theta + alpha / 2) );
2091 pt.y = yo + round( miterWidth * sin(theta + alpha / 2) );
2092 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
2093 break;
2094 case PS_JOIN_BEVEL :
2095 if(alpha > 0) {
2096 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
2097 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
2099 else {
2100 pt.x = xo - round( penWidthOut * cos(theta + M_PI_2) );
2101 pt.y = yo - round( penWidthOut * sin(theta + M_PI_2) );
2103 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
2104 if(alpha > 0) {
2105 pt.x = xo - round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2106 pt.y = yo - round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2108 else {
2109 pt.x = xo + round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2110 pt.y = yo + round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2112 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
2113 break;
2114 case PS_JOIN_ROUND :
2115 default :
2116 if(alpha > 0) {
2117 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
2118 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
2120 else {
2121 pt.x = xo - round( penWidthOut * cos(theta + M_PI_2) );
2122 pt.y = yo - round( penWidthOut * sin(theta + M_PI_2) );
2124 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
2125 pt.x = xo + round( penWidthOut * cos(theta + alpha / 2) );
2126 pt.y = yo + round( penWidthOut * sin(theta + alpha / 2) );
2127 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
2128 if(alpha > 0) {
2129 pt.x = xo - round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2130 pt.y = yo - round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2132 else {
2133 pt.x = xo + round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2134 pt.y = yo + round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2136 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
2137 break;
2141 for(j = 0; j < pUpPath->numEntriesUsed; j++) {
2142 POINT pt;
2143 pt.x = pUpPath->pPoints[j].x;
2144 pt.y = pUpPath->pPoints[j].y;
2145 PATH_AddEntry(pNewPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO));
2147 for(j = 0; j < pDownPath->numEntriesUsed; j++) {
2148 POINT pt;
2149 pt.x = pDownPath->pPoints[pDownPath->numEntriesUsed - j - 1].x;
2150 pt.y = pDownPath->pPoints[pDownPath->numEntriesUsed - j - 1].y;
2151 PATH_AddEntry(pNewPath, &pt, ( (j == 0 && (pStrokes[i]->pFlags[pStrokes[i]->numEntriesUsed - 1] & PT_CLOSEFIGURE)) ? PT_MOVETO : PT_LINETO));
2154 PATH_DestroyGdiPath(pStrokes[i]);
2155 HeapFree(GetProcessHeap(), 0, pStrokes[i]);
2156 PATH_DestroyGdiPath(pUpPath);
2157 HeapFree(GetProcessHeap(), 0, pUpPath);
2158 PATH_DestroyGdiPath(pDownPath);
2159 HeapFree(GetProcessHeap(), 0, pDownPath);
2161 HeapFree(GetProcessHeap(), 0, pStrokes);
2163 pNewPath->state = PATH_Closed;
2164 if (!(ret = PATH_AssignGdiPath(pPath, pNewPath)))
2165 ERR("Assign path failed\n");
2166 PATH_DestroyGdiPath(pNewPath);
2167 HeapFree(GetProcessHeap(), 0, pNewPath);
2168 return ret;
2172 /*******************************************************************
2173 * StrokeAndFillPath [GDI32.@]
2177 BOOL WINAPI StrokeAndFillPath(HDC hdc)
2179 DC *dc = get_dc_ptr( hdc );
2180 BOOL bRet = FALSE;
2182 if(!dc) return FALSE;
2184 if(dc->funcs->pStrokeAndFillPath)
2185 bRet = dc->funcs->pStrokeAndFillPath(dc->physDev);
2186 else
2188 bRet = PATH_FillPath(dc, &dc->path);
2189 if(bRet) bRet = PATH_StrokePath(dc, &dc->path);
2190 if(bRet) PATH_EmptyPath(&dc->path);
2192 release_dc_ptr( dc );
2193 return bRet;
2197 /*******************************************************************
2198 * StrokePath [GDI32.@]
2202 BOOL WINAPI StrokePath(HDC hdc)
2204 DC *dc = get_dc_ptr( hdc );
2205 GdiPath *pPath;
2206 BOOL bRet = FALSE;
2208 TRACE("(%p)\n", hdc);
2209 if(!dc) return FALSE;
2211 if(dc->funcs->pStrokePath)
2212 bRet = dc->funcs->pStrokePath(dc->physDev);
2213 else
2215 pPath = &dc->path;
2216 bRet = PATH_StrokePath(dc, pPath);
2217 PATH_EmptyPath(pPath);
2219 release_dc_ptr( dc );
2220 return bRet;
2224 /*******************************************************************
2225 * WidenPath [GDI32.@]
2229 BOOL WINAPI WidenPath(HDC hdc)
2231 DC *dc = get_dc_ptr( hdc );
2232 BOOL ret = FALSE;
2234 if(!dc) return FALSE;
2236 if(dc->funcs->pWidenPath)
2237 ret = dc->funcs->pWidenPath(dc->physDev);
2238 else
2239 ret = PATH_WidenPath(dc);
2240 release_dc_ptr( dc );
2241 return ret;