jscript: Added RegExp contruction implementation.
[wine.git] / dlls / gdi32 / path.c
blob1e953cba9de2efdbd8b7a7d283498238ff4e7c1b
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_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
96 HRGN *pHrgn);
97 static void PATH_EmptyPath(GdiPath *pPath);
98 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries);
99 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
100 double angleStart, double angleEnd, BYTE startEntryType);
101 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
102 double y, POINT *pPoint);
103 static void PATH_NormalizePoint(FLOAT_POINT corners[], const FLOAT_POINT
104 *pPoint, double *pX, double *pY);
105 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2);
107 /* Performs a world-to-viewport transformation on the specified point (which
108 * is in floating point format).
110 static inline void INTERNAL_LPTODP_FLOAT(DC *dc, FLOAT_POINT *point)
112 double x, y;
114 /* Perform the transformation */
115 x = point->x;
116 y = point->y;
117 point->x = x * dc->xformWorld2Vport.eM11 +
118 y * dc->xformWorld2Vport.eM21 +
119 dc->xformWorld2Vport.eDx;
120 point->y = x * dc->xformWorld2Vport.eM12 +
121 y * dc->xformWorld2Vport.eM22 +
122 dc->xformWorld2Vport.eDy;
126 /***********************************************************************
127 * BeginPath (GDI32.@)
129 BOOL WINAPI BeginPath(HDC hdc)
131 BOOL ret = TRUE;
132 DC *dc = get_dc_ptr( hdc );
134 if(!dc) return FALSE;
136 if(dc->funcs->pBeginPath)
137 ret = dc->funcs->pBeginPath(dc->physDev);
138 else
140 /* If path is already open, do nothing */
141 if(dc->path.state != PATH_Open)
143 /* Make sure that path is empty */
144 PATH_EmptyPath(&dc->path);
146 /* Initialize variables for new path */
147 dc->path.newStroke=TRUE;
148 dc->path.state=PATH_Open;
151 release_dc_ptr( dc );
152 return ret;
156 /***********************************************************************
157 * EndPath (GDI32.@)
159 BOOL WINAPI EndPath(HDC hdc)
161 BOOL ret = TRUE;
162 DC *dc = get_dc_ptr( hdc );
164 if(!dc) return FALSE;
166 if(dc->funcs->pEndPath)
167 ret = dc->funcs->pEndPath(dc->physDev);
168 else
170 /* Check that path is currently being constructed */
171 if(dc->path.state!=PATH_Open)
173 SetLastError(ERROR_CAN_NOT_COMPLETE);
174 ret = FALSE;
176 /* Set flag to indicate that path is finished */
177 else dc->path.state=PATH_Closed;
179 release_dc_ptr( dc );
180 return ret;
184 /******************************************************************************
185 * AbortPath [GDI32.@]
186 * Closes and discards paths from device context
188 * NOTES
189 * Check that SetLastError is being called correctly
191 * PARAMS
192 * hdc [I] Handle to device context
194 * RETURNS
195 * Success: TRUE
196 * Failure: FALSE
198 BOOL WINAPI AbortPath( HDC hdc )
200 BOOL ret = TRUE;
201 DC *dc = get_dc_ptr( hdc );
203 if(!dc) return FALSE;
205 if(dc->funcs->pAbortPath)
206 ret = dc->funcs->pAbortPath(dc->physDev);
207 else /* Remove all entries from the path */
208 PATH_EmptyPath( &dc->path );
209 release_dc_ptr( dc );
210 return ret;
214 /***********************************************************************
215 * CloseFigure (GDI32.@)
217 * FIXME: Check that SetLastError is being called correctly
219 BOOL WINAPI CloseFigure(HDC hdc)
221 BOOL ret = TRUE;
222 DC *dc = get_dc_ptr( hdc );
224 if(!dc) return FALSE;
226 if(dc->funcs->pCloseFigure)
227 ret = dc->funcs->pCloseFigure(dc->physDev);
228 else
230 /* Check that path is open */
231 if(dc->path.state!=PATH_Open)
233 SetLastError(ERROR_CAN_NOT_COMPLETE);
234 ret = FALSE;
236 else
238 /* Set PT_CLOSEFIGURE on the last entry and start a new stroke */
239 /* It is not necessary to draw a line, PT_CLOSEFIGURE is a virtual closing line itself */
240 if(dc->path.numEntriesUsed)
242 dc->path.pFlags[dc->path.numEntriesUsed-1]|=PT_CLOSEFIGURE;
243 dc->path.newStroke=TRUE;
247 release_dc_ptr( dc );
248 return ret;
252 /***********************************************************************
253 * GetPath (GDI32.@)
255 INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
256 INT nSize)
258 INT ret = -1;
259 GdiPath *pPath;
260 DC *dc = get_dc_ptr( hdc );
262 if(!dc) return -1;
264 pPath = &dc->path;
266 /* Check that path is closed */
267 if(pPath->state!=PATH_Closed)
269 SetLastError(ERROR_CAN_NOT_COMPLETE);
270 goto done;
273 if(nSize==0)
274 ret = pPath->numEntriesUsed;
275 else if(nSize<pPath->numEntriesUsed)
277 SetLastError(ERROR_INVALID_PARAMETER);
278 goto done;
280 else
282 memcpy(pPoints, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
283 memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
285 /* Convert the points to logical coordinates */
286 if(!DPtoLP(hdc, pPoints, pPath->numEntriesUsed))
288 /* FIXME: Is this the correct value? */
289 SetLastError(ERROR_CAN_NOT_COMPLETE);
290 goto done;
292 else ret = pPath->numEntriesUsed;
294 done:
295 release_dc_ptr( dc );
296 return ret;
300 /***********************************************************************
301 * PathToRegion (GDI32.@)
303 * FIXME
304 * Check that SetLastError is being called correctly
306 * The documentation does not state this explicitly, but a test under Windows
307 * shows that the region which is returned should be in device coordinates.
309 HRGN WINAPI PathToRegion(HDC hdc)
311 GdiPath *pPath;
312 HRGN hrgnRval = 0;
313 DC *dc = get_dc_ptr( hdc );
315 /* Get pointer to path */
316 if(!dc) return 0;
318 pPath = &dc->path;
320 /* Check that path is closed */
321 if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
322 else
324 /* FIXME: Should we empty the path even if conversion failed? */
325 if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
326 PATH_EmptyPath(pPath);
327 else
328 hrgnRval=0;
330 release_dc_ptr( dc );
331 return hrgnRval;
334 static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
336 INT mapMode, graphicsMode;
337 SIZE ptViewportExt, ptWindowExt;
338 POINT ptViewportOrg, ptWindowOrg;
339 XFORM xform;
340 HRGN hrgn;
342 if(dc->funcs->pFillPath)
343 return dc->funcs->pFillPath(dc->physDev);
345 /* Check that path is closed */
346 if(pPath->state!=PATH_Closed)
348 SetLastError(ERROR_CAN_NOT_COMPLETE);
349 return FALSE;
352 /* Construct a region from the path and fill it */
353 if(PATH_PathToRegion(pPath, dc->polyFillMode, &hrgn))
355 /* Since PaintRgn interprets the region as being in logical coordinates
356 * but the points we store for the path are already in device
357 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
358 * Using SaveDC to save information about the mapping mode / world
359 * transform would be easier but would require more overhead, especially
360 * now that SaveDC saves the current path.
363 /* Save the information about the old mapping mode */
364 mapMode=GetMapMode(dc->hSelf);
365 GetViewportExtEx(dc->hSelf, &ptViewportExt);
366 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
367 GetWindowExtEx(dc->hSelf, &ptWindowExt);
368 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
370 /* Save world transform
371 * NB: The Windows documentation on world transforms would lead one to
372 * believe that this has to be done only in GM_ADVANCED; however, my
373 * tests show that resetting the graphics mode to GM_COMPATIBLE does
374 * not reset the world transform.
376 GetWorldTransform(dc->hSelf, &xform);
378 /* Set MM_TEXT */
379 SetMapMode(dc->hSelf, MM_TEXT);
380 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
381 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
382 graphicsMode=GetGraphicsMode(dc->hSelf);
383 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
384 ModifyWorldTransform(dc->hSelf, &xform, MWT_IDENTITY);
385 SetGraphicsMode(dc->hSelf, graphicsMode);
387 /* Paint the region */
388 PaintRgn(dc->hSelf, hrgn);
389 DeleteObject(hrgn);
390 /* Restore the old mapping mode */
391 SetMapMode(dc->hSelf, mapMode);
392 SetViewportExtEx(dc->hSelf, ptViewportExt.cx, ptViewportExt.cy, NULL);
393 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
394 SetWindowExtEx(dc->hSelf, ptWindowExt.cx, ptWindowExt.cy, NULL);
395 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
397 /* Go to GM_ADVANCED temporarily to restore the world transform */
398 graphicsMode=GetGraphicsMode(dc->hSelf);
399 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
400 SetWorldTransform(dc->hSelf, &xform);
401 SetGraphicsMode(dc->hSelf, graphicsMode);
402 return TRUE;
404 return FALSE;
408 /***********************************************************************
409 * FillPath (GDI32.@)
411 * FIXME
412 * Check that SetLastError is being called correctly
414 BOOL WINAPI FillPath(HDC hdc)
416 DC *dc = get_dc_ptr( hdc );
417 BOOL bRet = FALSE;
419 if(!dc) return FALSE;
421 if(dc->funcs->pFillPath)
422 bRet = dc->funcs->pFillPath(dc->physDev);
423 else
425 bRet = PATH_FillPath(dc, &dc->path);
426 if(bRet)
428 /* FIXME: Should the path be emptied even if conversion
429 failed? */
430 PATH_EmptyPath(&dc->path);
433 release_dc_ptr( dc );
434 return bRet;
438 /***********************************************************************
439 * SelectClipPath (GDI32.@)
440 * FIXME
441 * Check that SetLastError is being called correctly
443 BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
445 GdiPath *pPath;
446 HRGN hrgnPath;
447 BOOL success = FALSE;
448 DC *dc = get_dc_ptr( hdc );
450 if(!dc) return FALSE;
452 if(dc->funcs->pSelectClipPath)
453 success = dc->funcs->pSelectClipPath(dc->physDev, iMode);
454 else
456 pPath = &dc->path;
458 /* Check that path is closed */
459 if(pPath->state!=PATH_Closed)
460 SetLastError(ERROR_CAN_NOT_COMPLETE);
461 /* Construct a region from the path */
462 else if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnPath))
464 success = ExtSelectClipRgn( hdc, hrgnPath, iMode ) != ERROR;
465 DeleteObject(hrgnPath);
467 /* Empty the path */
468 if(success)
469 PATH_EmptyPath(pPath);
470 /* FIXME: Should this function delete the path even if it failed? */
473 release_dc_ptr( dc );
474 return success;
478 /***********************************************************************
479 * Exported functions
482 /* PATH_InitGdiPath
484 * Initializes the GdiPath structure.
486 void PATH_InitGdiPath(GdiPath *pPath)
488 assert(pPath!=NULL);
490 pPath->state=PATH_Null;
491 pPath->pPoints=NULL;
492 pPath->pFlags=NULL;
493 pPath->numEntriesUsed=0;
494 pPath->numEntriesAllocated=0;
497 /* PATH_DestroyGdiPath
499 * Destroys a GdiPath structure (frees the memory in the arrays).
501 void PATH_DestroyGdiPath(GdiPath *pPath)
503 assert(pPath!=NULL);
505 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
506 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
509 /* PATH_AssignGdiPath
511 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
512 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
513 * not just the pointers. Since this means that the arrays in pPathDest may
514 * need to be resized, pPathDest should have been initialized using
515 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
516 * not a copy constructor).
517 * Returns TRUE if successful, else FALSE.
519 BOOL PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
521 assert(pPathDest!=NULL && pPathSrc!=NULL);
523 /* Make sure destination arrays are big enough */
524 if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
525 return FALSE;
527 /* Perform the copy operation */
528 memcpy(pPathDest->pPoints, pPathSrc->pPoints,
529 sizeof(POINT)*pPathSrc->numEntriesUsed);
530 memcpy(pPathDest->pFlags, pPathSrc->pFlags,
531 sizeof(BYTE)*pPathSrc->numEntriesUsed);
533 pPathDest->state=pPathSrc->state;
534 pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
535 pPathDest->newStroke=pPathSrc->newStroke;
537 return TRUE;
540 /* PATH_MoveTo
542 * Should be called when a MoveTo is performed on a DC that has an
543 * open path. This starts a new stroke. Returns TRUE if successful, else
544 * FALSE.
546 BOOL PATH_MoveTo(DC *dc)
548 GdiPath *pPath = &dc->path;
550 /* Check that path is open */
551 if(pPath->state!=PATH_Open)
552 /* FIXME: Do we have to call SetLastError? */
553 return FALSE;
555 /* Start a new stroke */
556 pPath->newStroke=TRUE;
558 return TRUE;
561 /* PATH_LineTo
563 * Should be called when a LineTo is performed on a DC that has an
564 * open path. This adds a PT_LINETO entry to the path (and possibly
565 * a PT_MOVETO entry, if this is the first LineTo in a stroke).
566 * Returns TRUE if successful, else FALSE.
568 BOOL PATH_LineTo(DC *dc, INT x, INT y)
570 GdiPath *pPath = &dc->path;
571 POINT point, pointCurPos;
573 /* Check that path is open */
574 if(pPath->state!=PATH_Open)
575 return FALSE;
577 /* Convert point to device coordinates */
578 point.x=x;
579 point.y=y;
580 if(!LPtoDP(dc->hSelf, &point, 1))
581 return FALSE;
583 /* Add a PT_MOVETO if necessary */
584 if(pPath->newStroke)
586 pPath->newStroke=FALSE;
587 pointCurPos.x = dc->CursPosX;
588 pointCurPos.y = dc->CursPosY;
589 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
590 return FALSE;
591 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
592 return FALSE;
595 /* Add a PT_LINETO entry */
596 return PATH_AddEntry(pPath, &point, PT_LINETO);
599 /* PATH_RoundRect
601 * Should be called when a call to RoundRect is performed on a DC that has
602 * an open path. Returns TRUE if successful, else FALSE.
604 * FIXME: it adds the same entries to the path as windows does, but there
605 * is an error in the bezier drawing code so that there are small pixel-size
606 * gaps when the resulting path is drawn by StrokePath()
608 BOOL PATH_RoundRect(DC *dc, INT x1, INT y1, INT x2, INT y2, INT ell_width, INT ell_height)
610 GdiPath *pPath = &dc->path;
611 POINT corners[2], pointTemp;
612 FLOAT_POINT ellCorners[2];
614 /* Check that path is open */
615 if(pPath->state!=PATH_Open)
616 return FALSE;
618 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
619 return FALSE;
621 /* Add points to the roundrect path */
622 ellCorners[0].x = corners[1].x-ell_width;
623 ellCorners[0].y = corners[0].y;
624 ellCorners[1].x = corners[1].x;
625 ellCorners[1].y = corners[0].y+ell_height;
626 if(!PATH_DoArcPart(pPath, ellCorners, 0, -M_PI_2, PT_MOVETO))
627 return FALSE;
628 pointTemp.x = corners[0].x+ell_width/2;
629 pointTemp.y = corners[0].y;
630 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
631 return FALSE;
632 ellCorners[0].x = corners[0].x;
633 ellCorners[1].x = corners[0].x+ell_width;
634 if(!PATH_DoArcPart(pPath, ellCorners, -M_PI_2, -M_PI, FALSE))
635 return FALSE;
636 pointTemp.x = corners[0].x;
637 pointTemp.y = corners[1].y-ell_height/2;
638 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
639 return FALSE;
640 ellCorners[0].y = corners[1].y-ell_height;
641 ellCorners[1].y = corners[1].y;
642 if(!PATH_DoArcPart(pPath, ellCorners, M_PI, M_PI_2, FALSE))
643 return FALSE;
644 pointTemp.x = corners[1].x-ell_width/2;
645 pointTemp.y = corners[1].y;
646 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
647 return FALSE;
648 ellCorners[0].x = corners[1].x-ell_width;
649 ellCorners[1].x = corners[1].x;
650 if(!PATH_DoArcPart(pPath, ellCorners, M_PI_2, 0, FALSE))
651 return FALSE;
653 /* Close the roundrect figure */
654 if(!CloseFigure(dc->hSelf))
655 return FALSE;
657 return TRUE;
660 /* PATH_Rectangle
662 * Should be called when a call to Rectangle is performed on a DC that has
663 * an open path. Returns TRUE if successful, else FALSE.
665 BOOL PATH_Rectangle(DC *dc, INT x1, INT y1, INT x2, INT y2)
667 GdiPath *pPath = &dc->path;
668 POINT corners[2], pointTemp;
670 /* Check that path is open */
671 if(pPath->state!=PATH_Open)
672 return FALSE;
674 if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
675 return FALSE;
677 /* Close any previous figure */
678 if(!CloseFigure(dc->hSelf))
680 /* The CloseFigure call shouldn't have failed */
681 assert(FALSE);
682 return FALSE;
685 /* Add four points to the path */
686 pointTemp.x=corners[1].x;
687 pointTemp.y=corners[0].y;
688 if(!PATH_AddEntry(pPath, &pointTemp, PT_MOVETO))
689 return FALSE;
690 if(!PATH_AddEntry(pPath, corners, PT_LINETO))
691 return FALSE;
692 pointTemp.x=corners[0].x;
693 pointTemp.y=corners[1].y;
694 if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
695 return FALSE;
696 if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
697 return FALSE;
699 /* Close the rectangle figure */
700 if(!CloseFigure(dc->hSelf))
702 /* The CloseFigure call shouldn't have failed */
703 assert(FALSE);
704 return FALSE;
707 return TRUE;
710 /* PATH_Ellipse
712 * Should be called when a call to Ellipse is performed on a DC that has
713 * an open path. This adds four Bezier splines representing the ellipse
714 * to the path. Returns TRUE if successful, else FALSE.
716 BOOL PATH_Ellipse(DC *dc, INT x1, INT y1, INT x2, INT y2)
718 return( PATH_Arc(dc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2,0) &&
719 CloseFigure(dc->hSelf) );
722 /* PATH_Arc
724 * Should be called when a call to Arc is performed on a DC that has
725 * an open path. This adds up to five Bezier splines representing the arc
726 * to the path. When 'lines' is 1, we add 1 extra line to get a chord,
727 * when 'lines' is 2, we add 2 extra lines to get a pie, and when 'lines' is
728 * -1 we add 1 extra line from the current DC position to the starting position
729 * of the arc before drawing the arc itself (arcto). Returns TRUE if successful,
730 * else FALSE.
732 BOOL PATH_Arc(DC *dc, INT x1, INT y1, INT x2, INT y2,
733 INT xStart, INT yStart, INT xEnd, INT yEnd, INT lines)
735 GdiPath *pPath = &dc->path;
736 double angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
737 /* Initialize angleEndQuadrant to silence gcc's warning */
738 double x, y;
739 FLOAT_POINT corners[2], pointStart, pointEnd;
740 POINT centre, pointCurPos;
741 BOOL start, end;
742 INT temp;
744 /* FIXME: This function should check for all possible error returns */
745 /* FIXME: Do we have to respect newStroke? */
747 /* Check that path is open */
748 if(pPath->state!=PATH_Open)
749 return FALSE;
751 /* Check for zero height / width */
752 /* FIXME: Only in GM_COMPATIBLE? */
753 if(x1==x2 || y1==y2)
754 return TRUE;
756 /* Convert points to device coordinates */
757 corners[0].x = x1;
758 corners[0].y = y1;
759 corners[1].x = x2;
760 corners[1].y = y2;
761 pointStart.x = xStart;
762 pointStart.y = yStart;
763 pointEnd.x = xEnd;
764 pointEnd.y = yEnd;
765 INTERNAL_LPTODP_FLOAT(dc, corners);
766 INTERNAL_LPTODP_FLOAT(dc, corners+1);
767 INTERNAL_LPTODP_FLOAT(dc, &pointStart);
768 INTERNAL_LPTODP_FLOAT(dc, &pointEnd);
770 /* Make sure first corner is top left and second corner is bottom right */
771 if(corners[0].x>corners[1].x)
773 temp=corners[0].x;
774 corners[0].x=corners[1].x;
775 corners[1].x=temp;
777 if(corners[0].y>corners[1].y)
779 temp=corners[0].y;
780 corners[0].y=corners[1].y;
781 corners[1].y=temp;
784 /* Compute start and end angle */
785 PATH_NormalizePoint(corners, &pointStart, &x, &y);
786 angleStart=atan2(y, x);
787 PATH_NormalizePoint(corners, &pointEnd, &x, &y);
788 angleEnd=atan2(y, x);
790 /* Make sure the end angle is "on the right side" of the start angle */
791 if(dc->ArcDirection==AD_CLOCKWISE)
793 if(angleEnd<=angleStart)
795 angleEnd+=2*M_PI;
796 assert(angleEnd>=angleStart);
799 else
801 if(angleEnd>=angleStart)
803 angleEnd-=2*M_PI;
804 assert(angleEnd<=angleStart);
808 /* In GM_COMPATIBLE, don't include bottom and right edges */
809 if(dc->GraphicsMode==GM_COMPATIBLE)
811 corners[1].x--;
812 corners[1].y--;
815 /* arcto: Add a PT_MOVETO only if this is the first entry in a stroke */
816 if(lines==-1 && pPath->newStroke)
818 pPath->newStroke=FALSE;
819 pointCurPos.x = dc->CursPosX;
820 pointCurPos.y = dc->CursPosY;
821 if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
822 return FALSE;
823 if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
824 return FALSE;
827 /* Add the arc to the path with one Bezier spline per quadrant that the
828 * arc spans */
829 start=TRUE;
830 end=FALSE;
833 /* Determine the start and end angles for this quadrant */
834 if(start)
836 angleStartQuadrant=angleStart;
837 if(dc->ArcDirection==AD_CLOCKWISE)
838 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
839 else
840 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
842 else
844 angleStartQuadrant=angleEndQuadrant;
845 if(dc->ArcDirection==AD_CLOCKWISE)
846 angleEndQuadrant+=M_PI_2;
847 else
848 angleEndQuadrant-=M_PI_2;
851 /* Have we reached the last part of the arc? */
852 if((dc->ArcDirection==AD_CLOCKWISE &&
853 angleEnd<angleEndQuadrant) ||
854 (dc->ArcDirection==AD_COUNTERCLOCKWISE &&
855 angleEnd>angleEndQuadrant))
857 /* Adjust the end angle for this quadrant */
858 angleEndQuadrant=angleEnd;
859 end=TRUE;
862 /* Add the Bezier spline to the path */
863 PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
864 start ? (lines==-1 ? PT_LINETO : PT_MOVETO) : FALSE);
865 start=FALSE;
866 } while(!end);
868 /* chord: close figure. pie: add line and close figure */
869 if(lines==1)
871 if(!CloseFigure(dc->hSelf))
872 return FALSE;
874 else if(lines==2)
876 centre.x = (corners[0].x+corners[1].x)/2;
877 centre.y = (corners[0].y+corners[1].y)/2;
878 if(!PATH_AddEntry(pPath, &centre, PT_LINETO | PT_CLOSEFIGURE))
879 return FALSE;
882 return TRUE;
885 BOOL PATH_PolyBezierTo(DC *dc, const POINT *pts, DWORD cbPoints)
887 GdiPath *pPath = &dc->path;
888 POINT pt;
889 UINT i;
891 /* Check that path is open */
892 if(pPath->state!=PATH_Open)
893 return FALSE;
895 /* Add a PT_MOVETO if necessary */
896 if(pPath->newStroke)
898 pPath->newStroke=FALSE;
899 pt.x = dc->CursPosX;
900 pt.y = dc->CursPosY;
901 if(!LPtoDP(dc->hSelf, &pt, 1))
902 return FALSE;
903 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
904 return FALSE;
907 for(i = 0; i < cbPoints; i++) {
908 pt = pts[i];
909 if(!LPtoDP(dc->hSelf, &pt, 1))
910 return FALSE;
911 PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
913 return TRUE;
916 BOOL PATH_PolyBezier(DC *dc, const POINT *pts, DWORD cbPoints)
918 GdiPath *pPath = &dc->path;
919 POINT pt;
920 UINT i;
922 /* Check that path is open */
923 if(pPath->state!=PATH_Open)
924 return FALSE;
926 for(i = 0; i < cbPoints; i++) {
927 pt = pts[i];
928 if(!LPtoDP(dc->hSelf, &pt, 1))
929 return FALSE;
930 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
932 return TRUE;
935 /* PATH_PolyDraw
937 * Should be called when a call to PolyDraw is performed on a DC that has
938 * an open path. Returns TRUE if successful, else FALSE.
940 BOOL PATH_PolyDraw(DC *dc, const POINT *pts, const BYTE *types,
941 DWORD cbPoints)
943 GdiPath *pPath = &dc->path;
944 POINT lastmove, orig_pos;
945 INT i;
947 lastmove.x = orig_pos.x = dc->CursPosX;
948 lastmove.y = orig_pos.y = dc->CursPosY;
950 for(i = pPath->numEntriesUsed - 1; i >= 0; i--){
951 if(pPath->pFlags[i] == PT_MOVETO){
952 lastmove.x = pPath->pPoints[i].x;
953 lastmove.y = pPath->pPoints[i].y;
954 if(!DPtoLP(dc->hSelf, &lastmove, 1))
955 return FALSE;
956 break;
960 for(i = 0; i < cbPoints; i++){
961 if(types[i] == PT_MOVETO){
962 pPath->newStroke = TRUE;
963 lastmove.x = pts[i].x;
964 lastmove.y = pts[i].y;
966 else if((types[i] & ~PT_CLOSEFIGURE) == PT_LINETO){
967 PATH_LineTo(dc, pts[i].x, pts[i].y);
969 else if(types[i] == PT_BEZIERTO){
970 if(!((i + 2 < cbPoints) && (types[i + 1] == PT_BEZIERTO)
971 && ((types[i + 2] & ~PT_CLOSEFIGURE) == PT_BEZIERTO)))
972 goto err;
973 PATH_PolyBezierTo(dc, &(pts[i]), 3);
974 i += 2;
976 else
977 goto err;
979 dc->CursPosX = pts[i].x;
980 dc->CursPosY = pts[i].y;
982 if(types[i] & PT_CLOSEFIGURE){
983 pPath->pFlags[pPath->numEntriesUsed-1] |= PT_CLOSEFIGURE;
984 pPath->newStroke = TRUE;
985 dc->CursPosX = lastmove.x;
986 dc->CursPosY = lastmove.y;
990 return TRUE;
992 err:
993 if((dc->CursPosX != orig_pos.x) || (dc->CursPosY != orig_pos.y)){
994 pPath->newStroke = TRUE;
995 dc->CursPosX = orig_pos.x;
996 dc->CursPosY = orig_pos.y;
999 return FALSE;
1002 BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
1004 GdiPath *pPath = &dc->path;
1005 POINT pt;
1006 UINT i;
1008 /* Check that path is open */
1009 if(pPath->state!=PATH_Open)
1010 return FALSE;
1012 for(i = 0; i < cbPoints; i++) {
1013 pt = pts[i];
1014 if(!LPtoDP(dc->hSelf, &pt, 1))
1015 return FALSE;
1016 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
1018 return TRUE;
1021 BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
1023 GdiPath *pPath = &dc->path;
1024 POINT pt;
1025 UINT i;
1027 /* Check that path is open */
1028 if(pPath->state!=PATH_Open)
1029 return FALSE;
1031 /* Add a PT_MOVETO if necessary */
1032 if(pPath->newStroke)
1034 pPath->newStroke=FALSE;
1035 pt.x = dc->CursPosX;
1036 pt.y = dc->CursPosY;
1037 if(!LPtoDP(dc->hSelf, &pt, 1))
1038 return FALSE;
1039 if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
1040 return FALSE;
1043 for(i = 0; i < cbPoints; i++) {
1044 pt = pts[i];
1045 if(!LPtoDP(dc->hSelf, &pt, 1))
1046 return FALSE;
1047 PATH_AddEntry(pPath, &pt, PT_LINETO);
1050 return TRUE;
1054 BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
1056 GdiPath *pPath = &dc->path;
1057 POINT pt;
1058 UINT i;
1060 /* Check that path is open */
1061 if(pPath->state!=PATH_Open)
1062 return FALSE;
1064 for(i = 0; i < cbPoints; i++) {
1065 pt = pts[i];
1066 if(!LPtoDP(dc->hSelf, &pt, 1))
1067 return FALSE;
1068 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
1069 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
1070 PT_LINETO));
1072 return TRUE;
1075 BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
1076 UINT polygons )
1078 GdiPath *pPath = &dc->path;
1079 POINT pt, startpt;
1080 UINT poly, i;
1081 INT point;
1083 /* Check that path is open */
1084 if(pPath->state!=PATH_Open)
1085 return FALSE;
1087 for(i = 0, poly = 0; poly < polygons; poly++) {
1088 for(point = 0; point < counts[poly]; point++, i++) {
1089 pt = pts[i];
1090 if(!LPtoDP(dc->hSelf, &pt, 1))
1091 return FALSE;
1092 if(point == 0) startpt = pt;
1093 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1095 /* win98 adds an extra line to close the figure for some reason */
1096 PATH_AddEntry(pPath, &startpt, PT_LINETO | PT_CLOSEFIGURE);
1098 return TRUE;
1101 BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1102 DWORD polylines )
1104 GdiPath *pPath = &dc->path;
1105 POINT pt;
1106 UINT poly, point, i;
1108 /* Check that path is open */
1109 if(pPath->state!=PATH_Open)
1110 return FALSE;
1112 for(i = 0, poly = 0; poly < polylines; poly++) {
1113 for(point = 0; point < counts[poly]; point++, i++) {
1114 pt = pts[i];
1115 if(!LPtoDP(dc->hSelf, &pt, 1))
1116 return FALSE;
1117 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
1120 return TRUE;
1123 /***********************************************************************
1124 * Internal functions
1127 /* PATH_CheckCorners
1129 * Helper function for PATH_RoundRect() and PATH_Rectangle()
1131 static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2)
1133 INT temp;
1135 /* Convert points to device coordinates */
1136 corners[0].x=x1;
1137 corners[0].y=y1;
1138 corners[1].x=x2;
1139 corners[1].y=y2;
1140 if(!LPtoDP(dc->hSelf, corners, 2))
1141 return FALSE;
1143 /* Make sure first corner is top left and second corner is bottom right */
1144 if(corners[0].x>corners[1].x)
1146 temp=corners[0].x;
1147 corners[0].x=corners[1].x;
1148 corners[1].x=temp;
1150 if(corners[0].y>corners[1].y)
1152 temp=corners[0].y;
1153 corners[0].y=corners[1].y;
1154 corners[1].y=temp;
1157 /* In GM_COMPATIBLE, don't include bottom and right edges */
1158 if(dc->GraphicsMode==GM_COMPATIBLE)
1160 corners[1].x--;
1161 corners[1].y--;
1164 return TRUE;
1167 /* PATH_AddFlatBezier
1169 static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
1171 POINT *pts;
1172 INT no, i;
1174 pts = GDI_Bezier( pt, 4, &no );
1175 if(!pts) return FALSE;
1177 for(i = 1; i < no; i++)
1178 PATH_AddEntry(pPath, &pts[i],
1179 (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
1180 HeapFree( GetProcessHeap(), 0, pts );
1181 return TRUE;
1184 /* PATH_FlattenPath
1186 * Replaces Beziers with line segments
1189 static BOOL PATH_FlattenPath(GdiPath *pPath)
1191 GdiPath newPath;
1192 INT srcpt;
1194 memset(&newPath, 0, sizeof(newPath));
1195 newPath.state = PATH_Open;
1196 for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
1197 switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
1198 case PT_MOVETO:
1199 case PT_LINETO:
1200 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
1201 pPath->pFlags[srcpt]);
1202 break;
1203 case PT_BEZIERTO:
1204 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
1205 pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
1206 srcpt += 2;
1207 break;
1210 newPath.state = PATH_Closed;
1211 PATH_AssignGdiPath(pPath, &newPath);
1212 PATH_DestroyGdiPath(&newPath);
1213 return TRUE;
1216 /* PATH_PathToRegion
1218 * Creates a region from the specified path using the specified polygon
1219 * filling mode. The path is left unchanged. A handle to the region that
1220 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
1221 * error occurs, SetLastError is called with the appropriate value and
1222 * FALSE is returned.
1224 static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
1225 HRGN *pHrgn)
1227 int numStrokes, iStroke, i;
1228 INT *pNumPointsInStroke;
1229 HRGN hrgn;
1231 assert(pPath!=NULL);
1232 assert(pHrgn!=NULL);
1234 PATH_FlattenPath(pPath);
1236 /* FIXME: What happens when number of points is zero? */
1238 /* First pass: Find out how many strokes there are in the path */
1239 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
1240 numStrokes=0;
1241 for(i=0; i<pPath->numEntriesUsed; i++)
1242 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1243 numStrokes++;
1245 /* Allocate memory for number-of-points-in-stroke array */
1246 pNumPointsInStroke=HeapAlloc( GetProcessHeap(), 0, sizeof(int) * numStrokes );
1247 if(!pNumPointsInStroke)
1249 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1250 return FALSE;
1253 /* Second pass: remember number of points in each polygon */
1254 iStroke=-1; /* Will get incremented to 0 at beginning of first stroke */
1255 for(i=0; i<pPath->numEntriesUsed; i++)
1257 /* Is this the beginning of a new stroke? */
1258 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1260 iStroke++;
1261 pNumPointsInStroke[iStroke]=0;
1264 pNumPointsInStroke[iStroke]++;
1267 /* Create a region from the strokes */
1268 hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
1269 numStrokes, nPolyFillMode);
1271 /* Free memory for number-of-points-in-stroke array */
1272 HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );
1274 if(hrgn==NULL)
1276 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1277 return FALSE;
1280 /* Success! */
1281 *pHrgn=hrgn;
1282 return TRUE;
1285 static inline INT int_from_fixed(FIXED f)
1287 return (f.fract >= 0x8000) ? (f.value + 1) : f.value;
1290 /**********************************************************************
1291 * PATH_BezierTo
1293 * internally used by PATH_add_outline
1295 static void PATH_BezierTo(GdiPath *pPath, POINT *lppt, INT n)
1297 if (n < 2) return;
1299 if (n == 2)
1301 PATH_AddEntry(pPath, &lppt[1], PT_LINETO);
1303 else if (n == 3)
1305 PATH_AddEntry(pPath, &lppt[0], PT_BEZIERTO);
1306 PATH_AddEntry(pPath, &lppt[1], PT_BEZIERTO);
1307 PATH_AddEntry(pPath, &lppt[2], PT_BEZIERTO);
1309 else
1311 POINT pt[3];
1312 INT i = 0;
1314 pt[2] = lppt[0];
1315 n--;
1317 while (n > 2)
1319 pt[0] = pt[2];
1320 pt[1] = lppt[i+1];
1321 pt[2].x = (lppt[i+2].x + lppt[i+1].x) / 2;
1322 pt[2].y = (lppt[i+2].y + lppt[i+1].y) / 2;
1323 PATH_BezierTo(pPath, pt, 3);
1324 n--;
1325 i++;
1328 pt[0] = pt[2];
1329 pt[1] = lppt[i+1];
1330 pt[2] = lppt[i+2];
1331 PATH_BezierTo(pPath, pt, 3);
1335 static BOOL PATH_add_outline(DC *dc, INT x, INT y, TTPOLYGONHEADER *header, DWORD size)
1337 GdiPath *pPath = &dc->path;
1338 TTPOLYGONHEADER *start;
1339 POINT pt;
1341 start = header;
1343 while ((char *)header < (char *)start + size)
1345 TTPOLYCURVE *curve;
1347 if (header->dwType != TT_POLYGON_TYPE)
1349 FIXME("Unknown header type %d\n", header->dwType);
1350 return FALSE;
1353 pt.x = x + int_from_fixed(header->pfxStart.x);
1354 pt.y = y - int_from_fixed(header->pfxStart.y);
1355 LPtoDP(dc->hSelf, &pt, 1);
1356 PATH_AddEntry(pPath, &pt, PT_MOVETO);
1358 curve = (TTPOLYCURVE *)(header + 1);
1360 while ((char *)curve < (char *)header + header->cb)
1362 /*TRACE("curve->wType %d\n", curve->wType);*/
1364 switch(curve->wType)
1366 case TT_PRIM_LINE:
1368 WORD i;
1370 for (i = 0; i < curve->cpfx; i++)
1372 pt.x = x + int_from_fixed(curve->apfx[i].x);
1373 pt.y = y - int_from_fixed(curve->apfx[i].y);
1374 LPtoDP(dc->hSelf, &pt, 1);
1375 PATH_AddEntry(pPath, &pt, PT_LINETO);
1377 break;
1380 case TT_PRIM_QSPLINE:
1381 case TT_PRIM_CSPLINE:
1383 WORD i;
1384 POINTFX ptfx;
1385 POINT *pts = HeapAlloc(GetProcessHeap(), 0, (curve->cpfx + 1) * sizeof(POINT));
1387 if (!pts) return FALSE;
1389 ptfx = *(POINTFX *)((char *)curve - sizeof(POINTFX));
1391 pts[0].x = x + int_from_fixed(ptfx.x);
1392 pts[0].y = y - int_from_fixed(ptfx.y);
1393 LPtoDP(dc->hSelf, &pts[0], 1);
1395 for(i = 0; i < curve->cpfx; i++)
1397 pts[i + 1].x = x + int_from_fixed(curve->apfx[i].x);
1398 pts[i + 1].y = y - int_from_fixed(curve->apfx[i].y);
1399 LPtoDP(dc->hSelf, &pts[i + 1], 1);
1402 PATH_BezierTo(pPath, pts, curve->cpfx + 1);
1404 HeapFree(GetProcessHeap(), 0, pts);
1405 break;
1408 default:
1409 FIXME("Unknown curve type %04x\n", curve->wType);
1410 return FALSE;
1413 curve = (TTPOLYCURVE *)&curve->apfx[curve->cpfx];
1416 header = (TTPOLYGONHEADER *)((char *)header + header->cb);
1419 return CloseFigure(dc->hSelf);
1422 /**********************************************************************
1423 * PATH_ExtTextOut
1425 BOOL PATH_ExtTextOut(DC *dc, INT x, INT y, UINT flags, const RECT *lprc,
1426 LPCWSTR str, UINT count, const INT *dx)
1428 unsigned int idx;
1429 double cosEsc, sinEsc;
1430 LOGFONTW lf;
1431 POINT org;
1432 HDC hdc = dc->hSelf;
1433 INT offset = 0, xoff = 0, yoff = 0;
1435 TRACE("%p, %d, %d, %08x, %s, %s, %d, %p)\n", hdc, x, y, flags,
1436 wine_dbgstr_rect(lprc), debugstr_wn(str, count), count, dx);
1438 if (!count) return TRUE;
1440 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1442 if (lf.lfEscapement != 0)
1444 cosEsc = cos(lf.lfEscapement * M_PI / 1800);
1445 sinEsc = sin(lf.lfEscapement * M_PI / 1800);
1446 } else
1448 cosEsc = 1;
1449 sinEsc = 0;
1452 GetDCOrgEx(hdc, &org);
1454 for (idx = 0; idx < count; idx++)
1456 GLYPHMETRICS gm;
1457 DWORD dwSize;
1458 void *outline;
1460 dwSize = GetGlyphOutlineW(hdc, str[idx], GGO_GLYPH_INDEX | GGO_NATIVE, &gm, 0, NULL, NULL);
1461 if (!dwSize) return FALSE;
1463 outline = HeapAlloc(GetProcessHeap(), 0, dwSize);
1464 if (!outline) return FALSE;
1466 GetGlyphOutlineW(hdc, str[idx], GGO_GLYPH_INDEX | GGO_NATIVE, &gm, dwSize, outline, NULL);
1468 PATH_add_outline(dc, org.x + x + xoff, org.x + y + yoff, outline, dwSize);
1470 HeapFree(GetProcessHeap(), 0, outline);
1472 if (dx)
1474 offset += dx[idx];
1475 xoff = offset * cosEsc;
1476 yoff = offset * -sinEsc;
1478 else
1480 xoff += gm.gmCellIncX;
1481 yoff += gm.gmCellIncY;
1484 return TRUE;
1487 /* PATH_EmptyPath
1489 * Removes all entries from the path and sets the path state to PATH_Null.
1491 static void PATH_EmptyPath(GdiPath *pPath)
1493 assert(pPath!=NULL);
1495 pPath->state=PATH_Null;
1496 pPath->numEntriesUsed=0;
1499 /* PATH_AddEntry
1501 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1502 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1503 * successful, FALSE otherwise (e.g. if not enough memory was available).
1505 BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
1507 assert(pPath!=NULL);
1509 /* FIXME: If newStroke is true, perhaps we want to check that we're
1510 * getting a PT_MOVETO
1512 TRACE("(%d,%d) - %d\n", pPoint->x, pPoint->y, flags);
1514 /* Check that path is open */
1515 if(pPath->state!=PATH_Open)
1516 return FALSE;
1518 /* Reserve enough memory for an extra path entry */
1519 if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
1520 return FALSE;
1522 /* Store information in path entry */
1523 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
1524 pPath->pFlags[pPath->numEntriesUsed]=flags;
1526 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1527 if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
1528 pPath->newStroke=TRUE;
1530 /* Increment entry count */
1531 pPath->numEntriesUsed++;
1533 return TRUE;
1536 /* PATH_ReserveEntries
1538 * Ensures that at least "numEntries" entries (for points and flags) have
1539 * been allocated; allocates larger arrays and copies the existing entries
1540 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1542 static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries)
1544 INT numEntriesToAllocate;
1545 POINT *pPointsNew;
1546 BYTE *pFlagsNew;
1548 assert(pPath!=NULL);
1549 assert(numEntries>=0);
1551 /* Do we have to allocate more memory? */
1552 if(numEntries > pPath->numEntriesAllocated)
1554 /* Find number of entries to allocate. We let the size of the array
1555 * grow exponentially, since that will guarantee linear time
1556 * complexity. */
1557 if(pPath->numEntriesAllocated)
1559 numEntriesToAllocate=pPath->numEntriesAllocated;
1560 while(numEntriesToAllocate<numEntries)
1561 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
1562 GROW_FACTOR_DENOM;
1564 else
1565 numEntriesToAllocate=numEntries;
1567 /* Allocate new arrays */
1568 pPointsNew=HeapAlloc( GetProcessHeap(), 0, numEntriesToAllocate * sizeof(POINT) );
1569 if(!pPointsNew)
1570 return FALSE;
1571 pFlagsNew=HeapAlloc( GetProcessHeap(), 0, numEntriesToAllocate * sizeof(BYTE) );
1572 if(!pFlagsNew)
1574 HeapFree( GetProcessHeap(), 0, pPointsNew );
1575 return FALSE;
1578 /* Copy old arrays to new arrays and discard old arrays */
1579 if(pPath->pPoints)
1581 assert(pPath->pFlags);
1583 memcpy(pPointsNew, pPath->pPoints,
1584 sizeof(POINT)*pPath->numEntriesUsed);
1585 memcpy(pFlagsNew, pPath->pFlags,
1586 sizeof(BYTE)*pPath->numEntriesUsed);
1588 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
1589 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
1591 pPath->pPoints=pPointsNew;
1592 pPath->pFlags=pFlagsNew;
1593 pPath->numEntriesAllocated=numEntriesToAllocate;
1596 return TRUE;
1599 /* PATH_DoArcPart
1601 * Creates a Bezier spline that corresponds to part of an arc and appends the
1602 * corresponding points to the path. The start and end angles are passed in
1603 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1604 * at most. If "startEntryType" is non-zero, an entry of that type for the first
1605 * control point is added to the path; otherwise, it is assumed that the current
1606 * position is equal to the first control point.
1608 static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
1609 double angleStart, double angleEnd, BYTE startEntryType)
1611 double halfAngle, a;
1612 double xNorm[4], yNorm[4];
1613 POINT point;
1614 int i;
1616 assert(fabs(angleEnd-angleStart)<=M_PI_2);
1618 /* FIXME: Is there an easier way of computing this? */
1620 /* Compute control points */
1621 halfAngle=(angleEnd-angleStart)/2.0;
1622 if(fabs(halfAngle)>1e-8)
1624 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
1625 xNorm[0]=cos(angleStart);
1626 yNorm[0]=sin(angleStart);
1627 xNorm[1]=xNorm[0] - a*yNorm[0];
1628 yNorm[1]=yNorm[0] + a*xNorm[0];
1629 xNorm[3]=cos(angleEnd);
1630 yNorm[3]=sin(angleEnd);
1631 xNorm[2]=xNorm[3] + a*yNorm[3];
1632 yNorm[2]=yNorm[3] - a*xNorm[3];
1634 else
1635 for(i=0; i<4; i++)
1637 xNorm[i]=cos(angleStart);
1638 yNorm[i]=sin(angleStart);
1641 /* Add starting point to path if desired */
1642 if(startEntryType)
1644 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1645 if(!PATH_AddEntry(pPath, &point, startEntryType))
1646 return FALSE;
1649 /* Add remaining control points */
1650 for(i=1; i<4; i++)
1652 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1653 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1654 return FALSE;
1657 return TRUE;
1660 /* PATH_ScaleNormalizedPoint
1662 * Scales a normalized point (x, y) with respect to the box whose corners are
1663 * passed in "corners". The point is stored in "*pPoint". The normalized
1664 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1665 * (1.0, 1.0) correspond to corners[1].
1667 static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
1668 double y, POINT *pPoint)
1670 pPoint->x=GDI_ROUND( (double)corners[0].x +
1671 (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
1672 pPoint->y=GDI_ROUND( (double)corners[0].y +
1673 (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
1676 /* PATH_NormalizePoint
1678 * Normalizes a point with respect to the box whose corners are passed in
1679 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
1681 static void PATH_NormalizePoint(FLOAT_POINT corners[],
1682 const FLOAT_POINT *pPoint,
1683 double *pX, double *pY)
1685 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) *
1686 2.0 - 1.0;
1687 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
1688 2.0 - 1.0;
1692 /*******************************************************************
1693 * FlattenPath [GDI32.@]
1697 BOOL WINAPI FlattenPath(HDC hdc)
1699 BOOL ret = FALSE;
1700 DC *dc = get_dc_ptr( hdc );
1702 if(!dc) return FALSE;
1704 if(dc->funcs->pFlattenPath) ret = dc->funcs->pFlattenPath(dc->physDev);
1705 else
1707 GdiPath *pPath = &dc->path;
1708 if(pPath->state != PATH_Closed)
1709 ret = PATH_FlattenPath(pPath);
1711 release_dc_ptr( dc );
1712 return ret;
1716 static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1718 INT i, nLinePts, nAlloc;
1719 POINT *pLinePts;
1720 POINT ptViewportOrg, ptWindowOrg;
1721 SIZE szViewportExt, szWindowExt;
1722 DWORD mapMode, graphicsMode;
1723 XFORM xform;
1724 BOOL ret = TRUE;
1726 if(dc->funcs->pStrokePath)
1727 return dc->funcs->pStrokePath(dc->physDev);
1729 if(pPath->state != PATH_Closed)
1730 return FALSE;
1732 /* Save the mapping mode info */
1733 mapMode=GetMapMode(dc->hSelf);
1734 GetViewportExtEx(dc->hSelf, &szViewportExt);
1735 GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
1736 GetWindowExtEx(dc->hSelf, &szWindowExt);
1737 GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
1738 GetWorldTransform(dc->hSelf, &xform);
1740 /* Set MM_TEXT */
1741 SetMapMode(dc->hSelf, MM_TEXT);
1742 SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
1743 SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
1744 graphicsMode=GetGraphicsMode(dc->hSelf);
1745 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1746 ModifyWorldTransform(dc->hSelf, &xform, MWT_IDENTITY);
1747 SetGraphicsMode(dc->hSelf, graphicsMode);
1749 /* Allocate enough memory for the worst case without beziers (one PT_MOVETO
1750 * and the rest PT_LINETO with PT_CLOSEFIGURE at the end) plus some buffer
1751 * space in case we get one to keep the number of reallocations small. */
1752 nAlloc = pPath->numEntriesUsed + 1 + 300;
1753 pLinePts = HeapAlloc(GetProcessHeap(), 0, nAlloc * sizeof(POINT));
1754 nLinePts = 0;
1756 for(i = 0; i < pPath->numEntriesUsed; i++) {
1757 if((i == 0 || (pPath->pFlags[i-1] & PT_CLOSEFIGURE)) &&
1758 (pPath->pFlags[i] != PT_MOVETO)) {
1759 ERR("Expected PT_MOVETO %s, got path flag %d\n",
1760 i == 0 ? "as first point" : "after PT_CLOSEFIGURE",
1761 (INT)pPath->pFlags[i]);
1762 ret = FALSE;
1763 goto end;
1765 switch(pPath->pFlags[i]) {
1766 case PT_MOVETO:
1767 TRACE("Got PT_MOVETO (%d, %d)\n",
1768 pPath->pPoints[i].x, pPath->pPoints[i].y);
1769 if(nLinePts >= 2)
1770 Polyline(dc->hSelf, pLinePts, nLinePts);
1771 nLinePts = 0;
1772 pLinePts[nLinePts++] = pPath->pPoints[i];
1773 break;
1774 case PT_LINETO:
1775 case (PT_LINETO | PT_CLOSEFIGURE):
1776 TRACE("Got PT_LINETO (%d, %d)\n",
1777 pPath->pPoints[i].x, pPath->pPoints[i].y);
1778 pLinePts[nLinePts++] = pPath->pPoints[i];
1779 break;
1780 case PT_BEZIERTO:
1781 TRACE("Got PT_BEZIERTO\n");
1782 if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1783 (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
1784 ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1785 ret = FALSE;
1786 goto end;
1787 } else {
1788 INT nBzrPts, nMinAlloc;
1789 POINT *pBzrPts = GDI_Bezier(&pPath->pPoints[i-1], 4, &nBzrPts);
1790 /* Make sure we have allocated enough memory for the lines of
1791 * this bezier and the rest of the path, assuming we won't get
1792 * another one (since we won't reallocate again then). */
1793 nMinAlloc = nLinePts + (pPath->numEntriesUsed - i) + nBzrPts;
1794 if(nAlloc < nMinAlloc)
1796 nAlloc = nMinAlloc * 2;
1797 pLinePts = HeapReAlloc(GetProcessHeap(), 0, pLinePts,
1798 nAlloc * sizeof(POINT));
1800 memcpy(&pLinePts[nLinePts], &pBzrPts[1],
1801 (nBzrPts - 1) * sizeof(POINT));
1802 nLinePts += nBzrPts - 1;
1803 HeapFree(GetProcessHeap(), 0, pBzrPts);
1804 i += 2;
1806 break;
1807 default:
1808 ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1809 ret = FALSE;
1810 goto end;
1812 if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1813 pLinePts[nLinePts++] = pLinePts[0];
1815 if(nLinePts >= 2)
1816 Polyline(dc->hSelf, pLinePts, nLinePts);
1818 end:
1819 HeapFree(GetProcessHeap(), 0, pLinePts);
1821 /* Restore the old mapping mode */
1822 SetMapMode(dc->hSelf, mapMode);
1823 SetWindowExtEx(dc->hSelf, szWindowExt.cx, szWindowExt.cy, NULL);
1824 SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
1825 SetViewportExtEx(dc->hSelf, szViewportExt.cx, szViewportExt.cy, NULL);
1826 SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
1828 /* Go to GM_ADVANCED temporarily to restore the world transform */
1829 graphicsMode=GetGraphicsMode(dc->hSelf);
1830 SetGraphicsMode(dc->hSelf, GM_ADVANCED);
1831 SetWorldTransform(dc->hSelf, &xform);
1832 SetGraphicsMode(dc->hSelf, graphicsMode);
1834 /* If we've moved the current point then get its new position
1835 which will be in device (MM_TEXT) co-ords, convert it to
1836 logical co-ords and re-set it. This basically updates
1837 dc->CurPosX|Y so that their values are in the correct mapping
1838 mode.
1840 if(i > 0) {
1841 POINT pt;
1842 GetCurrentPositionEx(dc->hSelf, &pt);
1843 DPtoLP(dc->hSelf, &pt, 1);
1844 MoveToEx(dc->hSelf, pt.x, pt.y, NULL);
1847 return ret;
1850 #define round(x) ((int)((x)>0?(x)+0.5:(x)-0.5))
1852 static BOOL PATH_WidenPath(DC *dc)
1854 INT i, j, numStrokes, penWidth, penWidthIn, penWidthOut, size, penStyle;
1855 BOOL ret = FALSE;
1856 GdiPath *pPath, *pNewPath, **pStrokes, *pUpPath, *pDownPath;
1857 EXTLOGPEN *elp;
1858 DWORD obj_type, joint, endcap, penType;
1860 pPath = &dc->path;
1862 if(pPath->state == PATH_Open) {
1863 SetLastError(ERROR_CAN_NOT_COMPLETE);
1864 return FALSE;
1867 PATH_FlattenPath(pPath);
1869 size = GetObjectW( dc->hPen, 0, NULL );
1870 if (!size) {
1871 SetLastError(ERROR_CAN_NOT_COMPLETE);
1872 return FALSE;
1875 elp = HeapAlloc( GetProcessHeap(), 0, size );
1876 GetObjectW( dc->hPen, size, elp );
1878 obj_type = GetObjectType(dc->hPen);
1879 if(obj_type == OBJ_PEN) {
1880 penStyle = ((LOGPEN*)elp)->lopnStyle;
1882 else if(obj_type == OBJ_EXTPEN) {
1883 penStyle = elp->elpPenStyle;
1885 else {
1886 SetLastError(ERROR_CAN_NOT_COMPLETE);
1887 HeapFree( GetProcessHeap(), 0, elp );
1888 return FALSE;
1891 penWidth = elp->elpWidth;
1892 HeapFree( GetProcessHeap(), 0, elp );
1894 endcap = (PS_ENDCAP_MASK & penStyle);
1895 joint = (PS_JOIN_MASK & penStyle);
1896 penType = (PS_TYPE_MASK & penStyle);
1898 /* The function cannot apply to cosmetic pens */
1899 if(obj_type == OBJ_EXTPEN && penType == PS_COSMETIC) {
1900 SetLastError(ERROR_CAN_NOT_COMPLETE);
1901 return FALSE;
1904 penWidthIn = penWidth / 2;
1905 penWidthOut = penWidth / 2;
1906 if(penWidthIn + penWidthOut < penWidth)
1907 penWidthOut++;
1909 numStrokes = 0;
1911 pStrokes = HeapAlloc(GetProcessHeap(), 0, numStrokes * sizeof(GdiPath*));
1912 pStrokes[0] = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1913 PATH_InitGdiPath(pStrokes[0]);
1914 pStrokes[0]->pFlags = HeapAlloc(GetProcessHeap(), 0, pPath->numEntriesUsed * sizeof(INT));
1915 pStrokes[0]->pPoints = HeapAlloc(GetProcessHeap(), 0, pPath->numEntriesUsed * sizeof(POINT));
1916 pStrokes[0]->numEntriesUsed = 0;
1918 for(i = 0, j = 0; i < pPath->numEntriesUsed; i++, j++) {
1919 POINT point;
1920 if((i == 0 || (pPath->pFlags[i-1] & PT_CLOSEFIGURE)) &&
1921 (pPath->pFlags[i] != PT_MOVETO)) {
1922 ERR("Expected PT_MOVETO %s, got path flag %c\n",
1923 i == 0 ? "as first point" : "after PT_CLOSEFIGURE",
1924 pPath->pFlags[i]);
1925 return FALSE;
1927 switch(pPath->pFlags[i]) {
1928 case PT_MOVETO:
1929 if(numStrokes > 0) {
1930 pStrokes[numStrokes - 1]->state = PATH_Closed;
1932 numStrokes++;
1933 j = 0;
1934 pStrokes = HeapReAlloc(GetProcessHeap(), 0, pStrokes, numStrokes * sizeof(GdiPath*));
1935 pStrokes[numStrokes - 1] = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1936 PATH_InitGdiPath(pStrokes[numStrokes - 1]);
1937 pStrokes[numStrokes - 1]->state = PATH_Open;
1938 case PT_LINETO:
1939 case (PT_LINETO | PT_CLOSEFIGURE):
1940 point.x = pPath->pPoints[i].x;
1941 point.y = pPath->pPoints[i].y;
1942 PATH_AddEntry(pStrokes[numStrokes - 1], &point, pPath->pFlags[i]);
1943 break;
1944 case PT_BEZIERTO:
1945 /* should never happen because of the FlattenPath call */
1946 ERR("Should never happen\n");
1947 break;
1948 default:
1949 ERR("Got path flag %c\n", pPath->pFlags[i]);
1950 return FALSE;
1954 pNewPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1955 PATH_InitGdiPath(pNewPath);
1956 pNewPath->state = PATH_Open;
1958 for(i = 0; i < numStrokes; i++) {
1959 pUpPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1960 PATH_InitGdiPath(pUpPath);
1961 pUpPath->state = PATH_Open;
1962 pDownPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
1963 PATH_InitGdiPath(pDownPath);
1964 pDownPath->state = PATH_Open;
1966 for(j = 0; j < pStrokes[i]->numEntriesUsed; j++) {
1967 /* Beginning or end of the path if not closed */
1968 if((!(pStrokes[i]->pFlags[pStrokes[i]->numEntriesUsed - 1] & PT_CLOSEFIGURE)) && (j == 0 || j == pStrokes[i]->numEntriesUsed - 1) ) {
1969 /* Compute segment angle */
1970 double xo, yo, xa, ya, theta;
1971 POINT pt;
1972 FLOAT_POINT corners[2];
1973 if(j == 0) {
1974 xo = pStrokes[i]->pPoints[j].x;
1975 yo = pStrokes[i]->pPoints[j].y;
1976 xa = pStrokes[i]->pPoints[1].x;
1977 ya = pStrokes[i]->pPoints[1].y;
1979 else {
1980 xa = pStrokes[i]->pPoints[j - 1].x;
1981 ya = pStrokes[i]->pPoints[j - 1].y;
1982 xo = pStrokes[i]->pPoints[j].x;
1983 yo = pStrokes[i]->pPoints[j].y;
1985 theta = atan2( ya - yo, xa - xo );
1986 switch(endcap) {
1987 case PS_ENDCAP_SQUARE :
1988 pt.x = xo + round(sqrt(2) * penWidthOut * cos(M_PI_4 + theta));
1989 pt.y = yo + round(sqrt(2) * penWidthOut * sin(M_PI_4 + theta));
1990 PATH_AddEntry(pUpPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO) );
1991 pt.x = xo + round(sqrt(2) * penWidthIn * cos(- M_PI_4 + theta));
1992 pt.y = yo + round(sqrt(2) * penWidthIn * sin(- M_PI_4 + theta));
1993 PATH_AddEntry(pUpPath, &pt, PT_LINETO);
1994 break;
1995 case PS_ENDCAP_FLAT :
1996 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
1997 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
1998 PATH_AddEntry(pUpPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO));
1999 pt.x = xo - round( penWidthIn * cos(theta + M_PI_2) );
2000 pt.y = yo - round( penWidthIn * sin(theta + M_PI_2) );
2001 PATH_AddEntry(pUpPath, &pt, PT_LINETO);
2002 break;
2003 case PS_ENDCAP_ROUND :
2004 default :
2005 corners[0].x = xo - penWidthIn;
2006 corners[0].y = yo - penWidthIn;
2007 corners[1].x = xo + penWidthOut;
2008 corners[1].y = yo + penWidthOut;
2009 PATH_DoArcPart(pUpPath ,corners, theta + M_PI_2 , theta + 3 * M_PI_4, (j == 0 ? PT_MOVETO : FALSE));
2010 PATH_DoArcPart(pUpPath ,corners, theta + 3 * M_PI_4 , theta + M_PI, FALSE);
2011 PATH_DoArcPart(pUpPath ,corners, theta + M_PI, theta + 5 * M_PI_4, FALSE);
2012 PATH_DoArcPart(pUpPath ,corners, theta + 5 * M_PI_4 , theta + 3 * M_PI_2, FALSE);
2013 break;
2016 /* Corpse of the path */
2017 else {
2018 /* Compute angle */
2019 INT previous, next;
2020 double xa, ya, xb, yb, xo, yo;
2021 double alpha, theta, miterWidth;
2022 DWORD _joint = joint;
2023 POINT pt;
2024 GdiPath *pInsidePath, *pOutsidePath;
2025 if(j > 0 && j < pStrokes[i]->numEntriesUsed - 1) {
2026 previous = j - 1;
2027 next = j + 1;
2029 else if (j == 0) {
2030 previous = pStrokes[i]->numEntriesUsed - 1;
2031 next = j + 1;
2033 else {
2034 previous = j - 1;
2035 next = 0;
2037 xo = pStrokes[i]->pPoints[j].x;
2038 yo = pStrokes[i]->pPoints[j].y;
2039 xa = pStrokes[i]->pPoints[previous].x;
2040 ya = pStrokes[i]->pPoints[previous].y;
2041 xb = pStrokes[i]->pPoints[next].x;
2042 yb = pStrokes[i]->pPoints[next].y;
2043 theta = atan2( yo - ya, xo - xa );
2044 alpha = atan2( yb - yo, xb - xo ) - theta;
2045 if (alpha > 0) alpha -= M_PI;
2046 else alpha += M_PI;
2047 if(_joint == PS_JOIN_MITER && dc->miterLimit < fabs(1 / sin(alpha/2))) {
2048 _joint = PS_JOIN_BEVEL;
2050 if(alpha > 0) {
2051 pInsidePath = pUpPath;
2052 pOutsidePath = pDownPath;
2054 else if(alpha < 0) {
2055 pInsidePath = pDownPath;
2056 pOutsidePath = pUpPath;
2058 else {
2059 continue;
2061 /* Inside angle points */
2062 if(alpha > 0) {
2063 pt.x = xo - round( penWidthIn * cos(theta + M_PI_2) );
2064 pt.y = yo - round( penWidthIn * sin(theta + M_PI_2) );
2066 else {
2067 pt.x = xo + round( penWidthIn * cos(theta + M_PI_2) );
2068 pt.y = yo + round( penWidthIn * sin(theta + M_PI_2) );
2070 PATH_AddEntry(pInsidePath, &pt, PT_LINETO);
2071 if(alpha > 0) {
2072 pt.x = xo + round( penWidthIn * cos(M_PI_2 + alpha + theta) );
2073 pt.y = yo + round( penWidthIn * sin(M_PI_2 + alpha + theta) );
2075 else {
2076 pt.x = xo - round( penWidthIn * cos(M_PI_2 + alpha + theta) );
2077 pt.y = yo - round( penWidthIn * sin(M_PI_2 + alpha + theta) );
2079 PATH_AddEntry(pInsidePath, &pt, PT_LINETO);
2080 /* Outside angle point */
2081 switch(_joint) {
2082 case PS_JOIN_MITER :
2083 miterWidth = fabs(penWidthOut / cos(M_PI_2 - fabs(alpha) / 2));
2084 pt.x = xo + round( miterWidth * cos(theta + alpha / 2) );
2085 pt.y = yo + round( miterWidth * sin(theta + alpha / 2) );
2086 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
2087 break;
2088 case PS_JOIN_BEVEL :
2089 if(alpha > 0) {
2090 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
2091 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
2093 else {
2094 pt.x = xo - round( penWidthOut * cos(theta + M_PI_2) );
2095 pt.y = yo - round( penWidthOut * sin(theta + M_PI_2) );
2097 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
2098 if(alpha > 0) {
2099 pt.x = xo - round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2100 pt.y = yo - round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2102 else {
2103 pt.x = xo + round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2104 pt.y = yo + round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2106 PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
2107 break;
2108 case PS_JOIN_ROUND :
2109 default :
2110 if(alpha > 0) {
2111 pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
2112 pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
2114 else {
2115 pt.x = xo - round( penWidthOut * cos(theta + M_PI_2) );
2116 pt.y = yo - round( penWidthOut * sin(theta + M_PI_2) );
2118 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
2119 pt.x = xo + round( penWidthOut * cos(theta + alpha / 2) );
2120 pt.y = yo + round( penWidthOut * sin(theta + alpha / 2) );
2121 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
2122 if(alpha > 0) {
2123 pt.x = xo - round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2124 pt.y = yo - round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2126 else {
2127 pt.x = xo + round( penWidthOut * cos(M_PI_2 + alpha + theta) );
2128 pt.y = yo + round( penWidthOut * sin(M_PI_2 + alpha + theta) );
2130 PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
2131 break;
2135 for(j = 0; j < pUpPath->numEntriesUsed; j++) {
2136 POINT pt;
2137 pt.x = pUpPath->pPoints[j].x;
2138 pt.y = pUpPath->pPoints[j].y;
2139 PATH_AddEntry(pNewPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO));
2141 for(j = 0; j < pDownPath->numEntriesUsed; j++) {
2142 POINT pt;
2143 pt.x = pDownPath->pPoints[pDownPath->numEntriesUsed - j - 1].x;
2144 pt.y = pDownPath->pPoints[pDownPath->numEntriesUsed - j - 1].y;
2145 PATH_AddEntry(pNewPath, &pt, ( (j == 0 && (pStrokes[i]->pFlags[pStrokes[i]->numEntriesUsed - 1] & PT_CLOSEFIGURE)) ? PT_MOVETO : PT_LINETO));
2148 PATH_DestroyGdiPath(pStrokes[i]);
2149 HeapFree(GetProcessHeap(), 0, pStrokes[i]);
2150 PATH_DestroyGdiPath(pUpPath);
2151 HeapFree(GetProcessHeap(), 0, pUpPath);
2152 PATH_DestroyGdiPath(pDownPath);
2153 HeapFree(GetProcessHeap(), 0, pDownPath);
2155 HeapFree(GetProcessHeap(), 0, pStrokes);
2157 pNewPath->state = PATH_Closed;
2158 if (!(ret = PATH_AssignGdiPath(pPath, pNewPath)))
2159 ERR("Assign path failed\n");
2160 PATH_DestroyGdiPath(pNewPath);
2161 HeapFree(GetProcessHeap(), 0, pNewPath);
2162 return ret;
2166 /*******************************************************************
2167 * StrokeAndFillPath [GDI32.@]
2171 BOOL WINAPI StrokeAndFillPath(HDC hdc)
2173 DC *dc = get_dc_ptr( hdc );
2174 BOOL bRet = FALSE;
2176 if(!dc) return FALSE;
2178 if(dc->funcs->pStrokeAndFillPath)
2179 bRet = dc->funcs->pStrokeAndFillPath(dc->physDev);
2180 else
2182 bRet = PATH_FillPath(dc, &dc->path);
2183 if(bRet) bRet = PATH_StrokePath(dc, &dc->path);
2184 if(bRet) PATH_EmptyPath(&dc->path);
2186 release_dc_ptr( dc );
2187 return bRet;
2191 /*******************************************************************
2192 * StrokePath [GDI32.@]
2196 BOOL WINAPI StrokePath(HDC hdc)
2198 DC *dc = get_dc_ptr( hdc );
2199 GdiPath *pPath;
2200 BOOL bRet = FALSE;
2202 TRACE("(%p)\n", hdc);
2203 if(!dc) return FALSE;
2205 if(dc->funcs->pStrokePath)
2206 bRet = dc->funcs->pStrokePath(dc->physDev);
2207 else
2209 pPath = &dc->path;
2210 bRet = PATH_StrokePath(dc, pPath);
2211 PATH_EmptyPath(pPath);
2213 release_dc_ptr( dc );
2214 return bRet;
2218 /*******************************************************************
2219 * WidenPath [GDI32.@]
2223 BOOL WINAPI WidenPath(HDC hdc)
2225 DC *dc = get_dc_ptr( hdc );
2226 BOOL ret = FALSE;
2228 if(!dc) return FALSE;
2230 if(dc->funcs->pWidenPath)
2231 ret = dc->funcs->pWidenPath(dc->physDev);
2232 else
2233 ret = PATH_WidenPath(dc);
2234 release_dc_ptr( dc );
2235 return ret;