path.c 67.1 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3
/*
 * Graphics paths (BeginPath, EndPath etc.)
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
4
 * Copyright 1997, 1998 Martin Boehme
5
 *                 1999 Huw D M Davies
6
 * Copyright 2005 Dmitry Timoshkov
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Alexandre Julliard's avatar
Alexandre Julliard committed
21 22
 */

23
#include "config.h"
24
#include "wine/port.h"
25

Alexandre Julliard's avatar
Alexandre Julliard committed
26
#include <assert.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
27
#include <math.h>
28
#include <stdarg.h>
29
#include <string.h>
30
#include <stdlib.h>
31 32 33
#if defined(HAVE_FLOAT_H)
#include <float.h>
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
34

35
#include "windef.h"
36 37
#include "winbase.h"
#include "wingdi.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
38 39
#include "winerror.h"

40
#include "gdi_private.h"
41
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
42

43
WINE_DEFAULT_DEBUG_CHANNEL(gdi);
44

Alexandre Julliard's avatar
Alexandre Julliard committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/* Notes on the implementation
 *
 * The implementation is based on dynamically resizable arrays of points and
 * flags. I dithered for a bit before deciding on this implementation, and
 * I had even done a bit of work on a linked list version before switching
 * to arrays. It's a bit of a tradeoff. When you use linked lists, the
 * implementation of FlattenPath is easier, because you can rip the
 * PT_BEZIERTO entries out of the middle of the list and link the
 * corresponding PT_LINETO entries in. However, when you use arrays,
 * PathToRegion becomes easier, since you can essentially just pass your array
 * of points to CreatePolyPolygonRgn. Also, if I'd used linked lists, I would
 * have had the extra effort of creating a chunk-based allocation scheme
 * in order to use memory effectively. That's why I finally decided to use
 * arrays. Note by the way that the array based implementation has the same
 * linear time complexity that linked lists would have since the arrays grow
 * exponentially.
 *
 * The points are stored in the path in device coordinates. This is
 * consistent with the way Windows does things (for instance, see the Win32
 * SDK documentation for GetPath).
 *
 * The word "stroke" appears in several places (e.g. in the flag
 * GdiPath.newStroke). A stroke consists of a PT_MOVETO followed by one or
 * more PT_LINETOs or PT_BEZIERTOs, up to, but not including, the next
 * PT_MOVETO. Note that this is not the same as the definition of a figure;
 * a figure can contain several strokes.
 *
 * I modified the drawing functions (MoveTo, LineTo etc.) to test whether
 * the path is open and to call the corresponding function in path.c if this
 * is the case. A more elegant approach would be to modify the function
 * pointers in the DC_FUNCTIONS structure; however, this would be a lot more
 * complex. Also, the performance degradation caused by my approach in the
 * case where no path is open is so small that it cannot be measured.
 *
 * Martin Boehme
 */

/* FIXME: A lot of stuff isn't implemented yet. There is much more to come. */

#define NUM_ENTRIES_INITIAL 16  /* Initial size of points / flags arrays  */
#define GROW_FACTOR_NUMER    2  /* Numerator of grow factor for the array */
#define GROW_FACTOR_DENOM    1  /* Denominator of grow factor             */

88 89 90 91 92 93
/* A floating point version of the POINT structure */
typedef struct tagFLOAT_POINT
{
   FLOAT x, y;
} FLOAT_POINT;

Alexandre Julliard's avatar
Alexandre Julliard committed
94

95
static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
96
   HRGN *pHrgn);
Alexandre Julliard's avatar
Alexandre Julliard committed
97
static void   PATH_EmptyPath(GdiPath *pPath);
98 99
static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries);
static BOOL PATH_DoArcPart(GdiPath *pPath, FLOAT_POINT corners[],
100
   double angleStart, double angleEnd, BYTE startEntryType);
Alexandre Julliard's avatar
Alexandre Julliard committed
101
static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
102
   double y, POINT *pPoint);
Alexandre Julliard's avatar
Alexandre Julliard committed
103 104
static void PATH_NormalizePoint(FLOAT_POINT corners[], const FLOAT_POINT
   *pPoint, double *pX, double *pY);
105
static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2);
Alexandre Julliard's avatar
Alexandre Julliard committed
106

107 108 109
/* Performs a world-to-viewport transformation on the specified point (which
 * is in floating point format).
 */
110
static inline void INTERNAL_LPTODP_FLOAT(DC *dc, FLOAT_POINT *point)
111 112 113 114 115 116 117 118 119 120 121 122 123
{
    FLOAT x, y;

    /* Perform the transformation */
    x = point->x;
    y = point->y;
    point->x = x * dc->xformWorld2Vport.eM11 +
               y * dc->xformWorld2Vport.eM21 +
               dc->xformWorld2Vport.eDx;
    point->y = x * dc->xformWorld2Vport.eM12 +
               y * dc->xformWorld2Vport.eM22 +
               dc->xformWorld2Vport.eDy;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
124

125

Alexandre Julliard's avatar
Alexandre Julliard committed
126
/***********************************************************************
127
 *           BeginPath    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
128
 */
129
BOOL WINAPI BeginPath(HDC hdc)
Alexandre Julliard's avatar
Alexandre Julliard committed
130
{
131
    BOOL ret = TRUE;
132
    DC *dc = get_dc_ptr( hdc );
133

134 135 136
    if(!dc) return FALSE;

    if(dc->funcs->pBeginPath)
137
        ret = dc->funcs->pBeginPath(dc->physDev);
138 139 140
    else
    {
        /* If path is already open, do nothing */
141
        if(dc->path.state != PATH_Open)
142 143
        {
            /* Make sure that path is empty */
144
            PATH_EmptyPath(&dc->path);
145 146

            /* Initialize variables for new path */
147 148
            dc->path.newStroke=TRUE;
            dc->path.state=PATH_Open;
149 150
        }
    }
151
    release_dc_ptr( dc );
152
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
153 154 155 156
}


/***********************************************************************
157
 *           EndPath    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
158
 */
159
BOOL WINAPI EndPath(HDC hdc)
Alexandre Julliard's avatar
Alexandre Julliard committed
160
{
161
    BOOL ret = TRUE;
162
    DC *dc = get_dc_ptr( hdc );
163

164 165 166
    if(!dc) return FALSE;

    if(dc->funcs->pEndPath)
167
        ret = dc->funcs->pEndPath(dc->physDev);
168 169 170
    else
    {
        /* Check that path is currently being constructed */
171
        if(dc->path.state!=PATH_Open)
172 173 174 175 176
        {
            SetLastError(ERROR_CAN_NOT_COMPLETE);
            ret = FALSE;
        }
        /* Set flag to indicate that path is finished */
177
        else dc->path.state=PATH_Closed;
178
    }
179
    release_dc_ptr( dc );
180
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
181 182 183
}


Alexandre Julliard's avatar
Alexandre Julliard committed
184
/******************************************************************************
185
 * AbortPath [GDI32.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
186 187 188 189 190 191 192 193
 * Closes and discards paths from device context
 *
 * NOTES
 *    Check that SetLastError is being called correctly
 *
 * PARAMS
 *    hdc [I] Handle to device context
 *
194 195 196
 * RETURNS
 *    Success: TRUE
 *    Failure: FALSE
Alexandre Julliard's avatar
Alexandre Julliard committed
197
 */
198
BOOL WINAPI AbortPath( HDC hdc )
Alexandre Julliard's avatar
Alexandre Julliard committed
199
{
200
    BOOL ret = TRUE;
201
    DC *dc = get_dc_ptr( hdc );
202

203 204 205
    if(!dc) return FALSE;

    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;
Alexandre Julliard's avatar
Alexandre Julliard committed
211 212 213 214
}


/***********************************************************************
215
 *           CloseFigure    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
216
 *
217
 * FIXME: Check that SetLastError is being called correctly
Alexandre Julliard's avatar
Alexandre Julliard committed
218
 */
219
BOOL WINAPI CloseFigure(HDC hdc)
Alexandre Julliard's avatar
Alexandre Julliard committed
220
{
221
    BOOL ret = TRUE;
222
    DC *dc = get_dc_ptr( hdc );
223

224 225 226
    if(!dc) return FALSE;

    if(dc->funcs->pCloseFigure)
227
        ret = dc->funcs->pCloseFigure(dc->physDev);
228 229 230
    else
    {
        /* Check that path is open */
231
        if(dc->path.state!=PATH_Open)
232 233 234 235 236 237 238
        {
            SetLastError(ERROR_CAN_NOT_COMPLETE);
            ret = FALSE;
        }
        else
        {
            /* 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)
241
            {
242 243
                dc->path.pFlags[dc->path.numEntriesUsed-1]|=PT_CLOSEFIGURE;
                dc->path.newStroke=TRUE;
244 245 246
            }
        }
    }
247
    release_dc_ptr( dc );
248
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
249 250 251 252
}


/***********************************************************************
253
 *           GetPath    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
254
 */
255 256
INT WINAPI GetPath(HDC hdc, LPPOINT pPoints, LPBYTE pTypes,
   INT nSize)
Alexandre Julliard's avatar
Alexandre Julliard committed
257
{
258
   INT ret = -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
259
   GdiPath *pPath;
260
   DC *dc = get_dc_ptr( hdc );
261

262
   if(!dc) return -1;
263

264
   pPath = &dc->path;
265

Alexandre Julliard's avatar
Alexandre Julliard committed
266 267 268 269
   /* Check that path is closed */
   if(pPath->state!=PATH_Closed)
   {
      SetLastError(ERROR_CAN_NOT_COMPLETE);
270
      goto done;
Alexandre Julliard's avatar
Alexandre Julliard committed
271
   }
272

Alexandre Julliard's avatar
Alexandre Julliard committed
273
   if(nSize==0)
274
      ret = pPath->numEntriesUsed;
Alexandre Julliard's avatar
Alexandre Julliard committed
275 276 277
   else if(nSize<pPath->numEntriesUsed)
   {
      SetLastError(ERROR_INVALID_PARAMETER);
278
      goto done;
Alexandre Julliard's avatar
Alexandre Julliard committed
279 280 281
   }
   else
   {
282
      memcpy(pPoints, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
Alexandre Julliard's avatar
Alexandre Julliard committed
283 284 285
      memcpy(pTypes, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);

      /* Convert the points to logical coordinates */
286
      if(!DPtoLP(hdc, pPoints, pPath->numEntriesUsed))
Alexandre Julliard's avatar
Alexandre Julliard committed
287 288 289
      {
	 /* FIXME: Is this the correct value? */
         SetLastError(ERROR_CAN_NOT_COMPLETE);
290
	goto done;
Alexandre Julliard's avatar
Alexandre Julliard committed
291
      }
292
     else ret = pPath->numEntriesUsed;
Alexandre Julliard's avatar
Alexandre Julliard committed
293
   }
294
 done:
295
   release_dc_ptr( dc );
296
   return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
297 298 299 300
}


/***********************************************************************
301
 *           PathToRegion    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
302
 *
303 304
 * FIXME
 *   Check that SetLastError is being called correctly
Alexandre Julliard's avatar
Alexandre Julliard committed
305 306
 *
 * The documentation does not state this explicitly, but a test under Windows
Alexandre Julliard's avatar
Alexandre Julliard committed
307 308
 * shows that the region which is returned should be in device coordinates.
 */
309
HRGN WINAPI PathToRegion(HDC hdc)
Alexandre Julliard's avatar
Alexandre Julliard committed
310 311
{
   GdiPath *pPath;
312
   HRGN  hrgnRval = 0;
313
   DC *dc = get_dc_ptr( hdc );
Alexandre Julliard's avatar
Alexandre Julliard committed
314 315

   /* Get pointer to path */
316
   if(!dc) return 0;
317

318
    pPath = &dc->path;
319

Alexandre Julliard's avatar
Alexandre Julliard committed
320
   /* Check that path is closed */
321 322
   if(pPath->state!=PATH_Closed) SetLastError(ERROR_CAN_NOT_COMPLETE);
   else
Alexandre Julliard's avatar
Alexandre Julliard committed
323
   {
324 325 326 327 328
       /* FIXME: Should we empty the path even if conversion failed? */
       if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnRval))
           PATH_EmptyPath(pPath);
       else
           hrgnRval=0;
Alexandre Julliard's avatar
Alexandre Julliard committed
329
   }
330
   release_dc_ptr( dc );
Alexandre Julliard's avatar
Alexandre Julliard committed
331 332 333
   return hrgnRval;
}

334
static BOOL PATH_FillPath(DC *dc, GdiPath *pPath)
Alexandre Julliard's avatar
Alexandre Julliard committed
335
{
336 337 338
   INT   mapMode, graphicsMode;
   SIZE  ptViewportExt, ptWindowExt;
   POINT ptViewportOrg, ptWindowOrg;
339
   XFORM xform;
340
   HRGN  hrgn;
341

342
   if(dc->funcs->pFillPath)
343
       return dc->funcs->pFillPath(dc->physDev);
344

Alexandre Julliard's avatar
Alexandre Julliard committed
345 346 347 348 349 350
   /* Check that path is closed */
   if(pPath->state!=PATH_Closed)
   {
      SetLastError(ERROR_CAN_NOT_COMPLETE);
      return FALSE;
   }
351

Alexandre Julliard's avatar
Alexandre Julliard committed
352
   /* Construct a region from the path and fill it */
353
   if(PATH_PathToRegion(pPath, dc->polyFillMode, &hrgn))
Alexandre Julliard's avatar
Alexandre Julliard committed
354 355 356 357
   {
      /* Since PaintRgn interprets the region as being in logical coordinates
       * but the points we store for the path are already in device
       * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
Alexandre Julliard's avatar
Alexandre Julliard committed
358 359 360
       * Using SaveDC to save information about the mapping mode / world
       * transform would be easier but would require more overhead, especially
       * now that SaveDC saves the current path.
Alexandre Julliard's avatar
Alexandre Julliard committed
361
       */
362

Alexandre Julliard's avatar
Alexandre Julliard committed
363
      /* Save the information about the old mapping mode */
364 365 366 367 368
      mapMode=GetMapMode(dc->hSelf);
      GetViewportExtEx(dc->hSelf, &ptViewportExt);
      GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
      GetWindowExtEx(dc->hSelf, &ptWindowExt);
      GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
369

Alexandre Julliard's avatar
Alexandre Julliard committed
370 371 372 373 374
      /* Save world transform
       * NB: The Windows documentation on world transforms would lead one to
       * believe that this has to be done only in GM_ADVANCED; however, my
       * tests show that resetting the graphics mode to GM_COMPATIBLE does
       * not reset the world transform.
Alexandre Julliard's avatar
Alexandre Julliard committed
375
       */
376
      GetWorldTransform(dc->hSelf, &xform);
377

Alexandre Julliard's avatar
Alexandre Julliard committed
378
      /* Set MM_TEXT */
379 380 381
      SetMapMode(dc->hSelf, MM_TEXT);
      SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
      SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
382 383 384 385
      graphicsMode=GetGraphicsMode(dc->hSelf);
      SetGraphicsMode(dc->hSelf, GM_ADVANCED);
      ModifyWorldTransform(dc->hSelf, &xform, MWT_IDENTITY);
      SetGraphicsMode(dc->hSelf, graphicsMode);
386

Alexandre Julliard's avatar
Alexandre Julliard committed
387
      /* Paint the region */
388
      PaintRgn(dc->hSelf, hrgn);
389
      DeleteObject(hrgn);
Alexandre Julliard's avatar
Alexandre Julliard committed
390
      /* Restore the old mapping mode */
391 392 393 394 395
      SetMapMode(dc->hSelf, mapMode);
      SetViewportExtEx(dc->hSelf, ptViewportExt.cx, ptViewportExt.cy, NULL);
      SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
      SetWindowExtEx(dc->hSelf, ptWindowExt.cx, ptWindowExt.cy, NULL);
      SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
396

Alexandre Julliard's avatar
Alexandre Julliard committed
397
      /* Go to GM_ADVANCED temporarily to restore the world transform */
398 399 400 401
      graphicsMode=GetGraphicsMode(dc->hSelf);
      SetGraphicsMode(dc->hSelf, GM_ADVANCED);
      SetWorldTransform(dc->hSelf, &xform);
      SetGraphicsMode(dc->hSelf, graphicsMode);
Alexandre Julliard's avatar
Alexandre Julliard committed
402 403
      return TRUE;
   }
404 405 406 407 408
   return FALSE;
}


/***********************************************************************
409
 *           FillPath    (GDI32.@)
410 411
 *
 * FIXME
412
 *    Check that SetLastError is being called correctly
413 414 415
 */
BOOL WINAPI FillPath(HDC hdc)
{
416
    DC *dc = get_dc_ptr( hdc );
417
    BOOL bRet = FALSE;
418

419 420 421
    if(!dc) return FALSE;

    if(dc->funcs->pFillPath)
422
        bRet = dc->funcs->pFillPath(dc->physDev);
423 424
    else
    {
425
        bRet = PATH_FillPath(dc, &dc->path);
426 427 428 429
        if(bRet)
        {
            /* FIXME: Should the path be emptied even if conversion
               failed? */
430
            PATH_EmptyPath(&dc->path);
431 432
        }
    }
433
    release_dc_ptr( dc );
434
    return bRet;
Alexandre Julliard's avatar
Alexandre Julliard committed
435 436 437 438
}


/***********************************************************************
439
 *           SelectClipPath    (GDI32.@)
440 441
 * FIXME
 *  Check that SetLastError is being called correctly
Alexandre Julliard's avatar
Alexandre Julliard committed
442
 */
443
BOOL WINAPI SelectClipPath(HDC hdc, INT iMode)
Alexandre Julliard's avatar
Alexandre Julliard committed
444 445
{
   GdiPath *pPath;
446
   HRGN  hrgnPath;
447
   BOOL  success = FALSE;
448
   DC *dc = get_dc_ptr( hdc );
449

450
   if(!dc) return FALSE;
451 452

   if(dc->funcs->pSelectClipPath)
453
     success = dc->funcs->pSelectClipPath(dc->physDev, iMode);
454
   else
Alexandre Julliard's avatar
Alexandre Julliard committed
455
   {
456
       pPath = &dc->path;
457

458 459 460 461 462 463 464 465 466 467 468 469 470 471
       /* Check that path is closed */
       if(pPath->state!=PATH_Closed)
           SetLastError(ERROR_CAN_NOT_COMPLETE);
       /* Construct a region from the path */
       else if(PATH_PathToRegion(pPath, GetPolyFillMode(hdc), &hrgnPath))
       {
           success = ExtSelectClipRgn( hdc, hrgnPath, iMode ) != ERROR;
           DeleteObject(hrgnPath);

           /* Empty the path */
           if(success)
               PATH_EmptyPath(pPath);
           /* FIXME: Should this function delete the path even if it failed? */
       }
Alexandre Julliard's avatar
Alexandre Julliard committed
472
   }
473
   release_dc_ptr( dc );
474
   return success;
Alexandre Julliard's avatar
Alexandre Julliard committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
}


/***********************************************************************
 * Exported functions
 */

/* PATH_InitGdiPath
 *
 * Initializes the GdiPath structure.
 */
void PATH_InitGdiPath(GdiPath *pPath)
{
   assert(pPath!=NULL);

   pPath->state=PATH_Null;
   pPath->pPoints=NULL;
   pPath->pFlags=NULL;
   pPath->numEntriesUsed=0;
   pPath->numEntriesAllocated=0;
}

/* PATH_DestroyGdiPath
 *
 * Destroys a GdiPath structure (frees the memory in the arrays).
 */
void PATH_DestroyGdiPath(GdiPath *pPath)
{
   assert(pPath!=NULL);

505 506
   HeapFree( GetProcessHeap(), 0, pPath->pPoints );
   HeapFree( GetProcessHeap(), 0, pPath->pFlags );
Alexandre Julliard's avatar
Alexandre Julliard committed
507 508 509 510 511 512 513 514 515 516 517 518
}

/* PATH_AssignGdiPath
 *
 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
 * not just the pointers. Since this means that the arrays in pPathDest may
 * need to be resized, pPathDest should have been initialized using
 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
 * not a copy constructor).
 * Returns TRUE if successful, else FALSE.
 */
519
BOOL PATH_AssignGdiPath(GdiPath *pPathDest, const GdiPath *pPathSrc)
Alexandre Julliard's avatar
Alexandre Julliard committed
520 521 522 523 524 525 526 527 528
{
   assert(pPathDest!=NULL && pPathSrc!=NULL);

   /* Make sure destination arrays are big enough */
   if(!PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed))
      return FALSE;

   /* Perform the copy operation */
   memcpy(pPathDest->pPoints, pPathSrc->pPoints,
529
      sizeof(POINT)*pPathSrc->numEntriesUsed);
Alexandre Julliard's avatar
Alexandre Julliard committed
530
   memcpy(pPathDest->pFlags, pPathSrc->pFlags,
531 532
      sizeof(BYTE)*pPathSrc->numEntriesUsed);

Alexandre Julliard's avatar
Alexandre Julliard committed
533 534 535 536 537 538 539 540 541 542 543 544 545
   pPathDest->state=pPathSrc->state;
   pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
   pPathDest->newStroke=pPathSrc->newStroke;

   return TRUE;
}

/* PATH_MoveTo
 *
 * Should be called when a MoveTo is performed on a DC that has an
 * open path. This starts a new stroke. Returns TRUE if successful, else
 * FALSE.
 */
546
BOOL PATH_MoveTo(DC *dc)
Alexandre Julliard's avatar
Alexandre Julliard committed
547
{
548
   GdiPath *pPath = &dc->path;
549

Alexandre Julliard's avatar
Alexandre Julliard committed
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
   /* Check that path is open */
   if(pPath->state!=PATH_Open)
      /* FIXME: Do we have to call SetLastError? */
      return FALSE;

   /* Start a new stroke */
   pPath->newStroke=TRUE;

   return TRUE;
}

/* PATH_LineTo
 *
 * Should be called when a LineTo is performed on a DC that has an
 * open path. This adds a PT_LINETO entry to the path (and possibly
 * a PT_MOVETO entry, if this is the first LineTo in a stroke).
 * Returns TRUE if successful, else FALSE.
 */
568
BOOL PATH_LineTo(DC *dc, INT x, INT y)
Alexandre Julliard's avatar
Alexandre Julliard committed
569
{
570
   GdiPath *pPath = &dc->path;
571
   POINT point, pointCurPos;
572

Alexandre Julliard's avatar
Alexandre Julliard committed
573 574 575 576 577 578 579
   /* Check that path is open */
   if(pPath->state!=PATH_Open)
      return FALSE;

   /* Convert point to device coordinates */
   point.x=x;
   point.y=y;
580
   if(!LPtoDP(dc->hSelf, &point, 1))
Alexandre Julliard's avatar
Alexandre Julliard committed
581
      return FALSE;
582

Alexandre Julliard's avatar
Alexandre Julliard committed
583 584 585 586
   /* Add a PT_MOVETO if necessary */
   if(pPath->newStroke)
   {
      pPath->newStroke=FALSE;
587 588
      pointCurPos.x = dc->CursPosX;
      pointCurPos.y = dc->CursPosY;
589
      if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
Alexandre Julliard's avatar
Alexandre Julliard committed
590
         return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
591
      if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
Alexandre Julliard's avatar
Alexandre Julliard committed
592 593
         return FALSE;
   }
594

Alexandre Julliard's avatar
Alexandre Julliard committed
595
   /* Add a PT_LINETO entry */
Alexandre Julliard's avatar
Alexandre Julliard committed
596 597 598
   return PATH_AddEntry(pPath, &point, PT_LINETO);
}

599 600 601 602 603
/* PATH_RoundRect
 *
 * Should be called when a call to RoundRect is performed on a DC that has
 * 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 606 607 608 609
 * is an error in the bezier drawing code so that there are small pixel-size
 * gaps when the resulting path is drawn by StrokePath()
 */
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 612
   POINT corners[2], pointTemp;
   FLOAT_POINT ellCorners[2];
613

614 615 616 617 618 619 620
   /* Check that path is open */
   if(pPath->state!=PATH_Open)
      return FALSE;

   if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
      return FALSE;

621
   /* Add points to the roundrect path */
622
   ellCorners[0].x = corners[1].x-ell_width;
623 624 625
   ellCorners[0].y = corners[0].y;
   ellCorners[1].x = corners[1].x;
   ellCorners[1].y = corners[0].y+ell_height;
626
   if(!PATH_DoArcPart(pPath, ellCorners, 0, -M_PI_2, PT_MOVETO))
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
      return FALSE;
   pointTemp.x = corners[0].x+ell_width/2;
   pointTemp.y = corners[0].y;
   if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
      return FALSE;
   ellCorners[0].x = corners[0].x;
   ellCorners[1].x = corners[0].x+ell_width;
   if(!PATH_DoArcPart(pPath, ellCorners, -M_PI_2, -M_PI, FALSE))
      return FALSE;
   pointTemp.x = corners[0].x;
   pointTemp.y = corners[1].y-ell_height/2;
   if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
      return FALSE;
   ellCorners[0].y = corners[1].y-ell_height;
   ellCorners[1].y = corners[1].y;
   if(!PATH_DoArcPart(pPath, ellCorners, M_PI, M_PI_2, FALSE))
      return FALSE;
   pointTemp.x = corners[1].x-ell_width/2;
   pointTemp.y = corners[1].y;
   if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
      return FALSE;
   ellCorners[0].x = corners[1].x-ell_width;
   ellCorners[1].x = corners[1].x;
   if(!PATH_DoArcPart(pPath, ellCorners, M_PI_2, 0, FALSE))
651 652
      return FALSE;

653 654 655 656 657 658 659
   /* Close the roundrect figure */
   if(!CloseFigure(dc->hSelf))
      return FALSE;

   return TRUE;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
660 661 662 663 664
/* PATH_Rectangle
 *
 * Should be called when a call to Rectangle is performed on a DC that has
 * an open path. Returns TRUE if successful, else FALSE.
 */
665
BOOL PATH_Rectangle(DC *dc, INT x1, INT y1, INT x2, INT y2)
Alexandre Julliard's avatar
Alexandre Julliard committed
666
{
667
   GdiPath *pPath = &dc->path;
668
   POINT corners[2], pointTemp;
Alexandre Julliard's avatar
Alexandre Julliard committed
669 670 671 672 673

   /* Check that path is open */
   if(pPath->state!=PATH_Open)
      return FALSE;

674
   if(!PATH_CheckCorners(dc,corners,x1,y1,x2,y2))
Alexandre Julliard's avatar
Alexandre Julliard committed
675 676 677
      return FALSE;

   /* Close any previous figure */
678
   if(!CloseFigure(dc->hSelf))
Alexandre Julliard's avatar
Alexandre Julliard committed
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
   {
      /* The CloseFigure call shouldn't have failed */
      assert(FALSE);
      return FALSE;
   }

   /* Add four points to the path */
   pointTemp.x=corners[1].x;
   pointTemp.y=corners[0].y;
   if(!PATH_AddEntry(pPath, &pointTemp, PT_MOVETO))
      return FALSE;
   if(!PATH_AddEntry(pPath, corners, PT_LINETO))
      return FALSE;
   pointTemp.x=corners[0].x;
   pointTemp.y=corners[1].y;
   if(!PATH_AddEntry(pPath, &pointTemp, PT_LINETO))
      return FALSE;
   if(!PATH_AddEntry(pPath, corners+1, PT_LINETO))
      return FALSE;

   /* Close the rectangle figure */
700
   if(!CloseFigure(dc->hSelf))
Alexandre Julliard's avatar
Alexandre Julliard committed
701 702 703 704 705 706 707
   {
      /* The CloseFigure call shouldn't have failed */
      assert(FALSE);
      return FALSE;
   }

   return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
708 709
}

Alexandre Julliard's avatar
Alexandre Julliard committed
710
/* PATH_Ellipse
711
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
712 713 714 715
 * Should be called when a call to Ellipse is performed on a DC that has
 * an open path. This adds four Bezier splines representing the ellipse
 * to the path. Returns TRUE if successful, else FALSE.
 */
716
BOOL PATH_Ellipse(DC *dc, INT x1, INT y1, INT x2, INT y2)
Alexandre Julliard's avatar
Alexandre Julliard committed
717
{
718
   return( PATH_Arc(dc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2,0) &&
719
           CloseFigure(dc->hSelf) );
Alexandre Julliard's avatar
Alexandre Julliard committed
720 721 722 723 724 725
}

/* PATH_Arc
 *
 * Should be called when a call to Arc is performed on a DC that has
 * 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 728 729 730
 * when 'lines' is 2, we add 2 extra lines to get a pie, and when 'lines' is
 * -1 we add 1 extra line from the current DC position to the starting position
 * of the arc before drawing the arc itself (arcto). Returns TRUE if successful,
 * else FALSE.
Alexandre Julliard's avatar
Alexandre Julliard committed
731
 */
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)
Alexandre Julliard's avatar
Alexandre Julliard committed
734
{
735
   GdiPath     *pPath = &dc->path;
Alexandre Julliard's avatar
Alexandre Julliard committed
736 737 738 739
   double      angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
               /* Initialize angleEndQuadrant to silence gcc's warning */
   double      x, y;
   FLOAT_POINT corners[2], pointStart, pointEnd;
740
   POINT       centre, pointCurPos;
741 742
   BOOL      start, end;
   INT       temp;
Alexandre Julliard's avatar
Alexandre Julliard committed
743 744

   /* FIXME: This function should check for all possible error returns */
Alexandre Julliard's avatar
Alexandre Julliard committed
745
   /* FIXME: Do we have to respect newStroke? */
746

Alexandre Julliard's avatar
Alexandre Julliard committed
747 748 749 750 751
   /* Check that path is open */
   if(pPath->state!=PATH_Open)
      return FALSE;

   /* Check for zero height / width */
Alexandre Julliard's avatar
Alexandre Julliard committed
752
   /* FIXME: Only in GM_COMPATIBLE? */
Alexandre Julliard's avatar
Alexandre Julliard committed
753 754
   if(x1==x2 || y1==y2)
      return TRUE;
755

Alexandre Julliard's avatar
Alexandre Julliard committed
756
   /* Convert points to device coordinates */
Alexandre Julliard's avatar
Alexandre Julliard committed
757 758 759 760 761 762 763 764
   corners[0].x=(FLOAT)x1;
   corners[0].y=(FLOAT)y1;
   corners[1].x=(FLOAT)x2;
   corners[1].y=(FLOAT)y2;
   pointStart.x=(FLOAT)xStart;
   pointStart.y=(FLOAT)yStart;
   pointEnd.x=(FLOAT)xEnd;
   pointEnd.y=(FLOAT)yEnd;
765 766 767 768
   INTERNAL_LPTODP_FLOAT(dc, corners);
   INTERNAL_LPTODP_FLOAT(dc, corners+1);
   INTERNAL_LPTODP_FLOAT(dc, &pointStart);
   INTERNAL_LPTODP_FLOAT(dc, &pointEnd);
Alexandre Julliard's avatar
Alexandre Julliard committed
769 770

   /* Make sure first corner is top left and second corner is bottom right */
Alexandre Julliard's avatar
Alexandre Julliard committed
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
   if(corners[0].x>corners[1].x)
   {
      temp=corners[0].x;
      corners[0].x=corners[1].x;
      corners[1].x=temp;
   }
   if(corners[0].y>corners[1].y)
   {
      temp=corners[0].y;
      corners[0].y=corners[1].y;
      corners[1].y=temp;
   }

   /* Compute start and end angle */
   PATH_NormalizePoint(corners, &pointStart, &x, &y);
   angleStart=atan2(y, x);
   PATH_NormalizePoint(corners, &pointEnd, &x, &y);
   angleEnd=atan2(y, x);

   /* Make sure the end angle is "on the right side" of the start angle */
791
   if(dc->ArcDirection==AD_CLOCKWISE)
Alexandre Julliard's avatar
Alexandre Julliard committed
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
   {
      if(angleEnd<=angleStart)
      {
         angleEnd+=2*M_PI;
	 assert(angleEnd>=angleStart);
      }
   }
   else
   {
      if(angleEnd>=angleStart)
      {
         angleEnd-=2*M_PI;
	 assert(angleEnd<=angleStart);
      }
   }

Alexandre Julliard's avatar
Alexandre Julliard committed
808
   /* In GM_COMPATIBLE, don't include bottom and right edges */
809
   if(dc->GraphicsMode==GM_COMPATIBLE)
Alexandre Julliard's avatar
Alexandre Julliard committed
810 811 812 813
   {
      corners[1].x--;
      corners[1].y--;
   }
814

815 816 817 818 819 820 821 822 823 824 825 826
   /* arcto: Add a PT_MOVETO only if this is the first entry in a stroke */
   if(lines==-1 && pPath->newStroke)
   {
      pPath->newStroke=FALSE;
      pointCurPos.x = dc->CursPosX;
      pointCurPos.y = dc->CursPosY;
      if(!LPtoDP(dc->hSelf, &pointCurPos, 1))
         return FALSE;
      if(!PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO))
         return FALSE;
   }

Alexandre Julliard's avatar
Alexandre Julliard committed
827 828 829 830 831 832 833 834 835 836
   /* Add the arc to the path with one Bezier spline per quadrant that the
    * arc spans */
   start=TRUE;
   end=FALSE;
   do
   {
      /* Determine the start and end angles for this quadrant */
      if(start)
      {
         angleStartQuadrant=angleStart;
837
	 if(dc->ArcDirection==AD_CLOCKWISE)
Alexandre Julliard's avatar
Alexandre Julliard committed
838 839 840 841 842 843 844
	    angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
	 else
	    angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
      }
      else
      {
	 angleStartQuadrant=angleEndQuadrant;
845
	 if(dc->ArcDirection==AD_CLOCKWISE)
Alexandre Julliard's avatar
Alexandre Julliard committed
846 847 848 849 850 851
	    angleEndQuadrant+=M_PI_2;
	 else
	    angleEndQuadrant-=M_PI_2;
      }

      /* Have we reached the last part of the arc? */
852
      if((dc->ArcDirection==AD_CLOCKWISE &&
Alexandre Julliard's avatar
Alexandre Julliard committed
853
         angleEnd<angleEndQuadrant) ||
854
	 (dc->ArcDirection==AD_COUNTERCLOCKWISE &&
Alexandre Julliard's avatar
Alexandre Julliard committed
855
	 angleEnd>angleEndQuadrant))
Alexandre Julliard's avatar
Alexandre Julliard committed
856 857 858 859 860 861 862 863
      {
	 /* Adjust the end angle for this quadrant */
         angleEndQuadrant=angleEnd;
	 end=TRUE;
      }

      /* Add the Bezier spline to the path */
      PATH_DoArcPart(pPath, corners, angleStartQuadrant, angleEndQuadrant,
864
         start ? (lines==-1 ? PT_LINETO : PT_MOVETO) : FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
865 866 867
      start=FALSE;
   }  while(!end);

868 869 870 871 872 873 874 875
   /* chord: close figure. pie: add line and close figure */
   if(lines==1)
   {
      if(!CloseFigure(dc->hSelf))
         return FALSE;
   }
   else if(lines==2)
   {
876
      centre.x = (corners[0].x+corners[1].x)/2;
877 878 879 880
      centre.y = (corners[0].y+corners[1].y)/2;
      if(!PATH_AddEntry(pPath, &centre, PT_LINETO | PT_CLOSEFIGURE))
         return FALSE;
   }
881

Alexandre Julliard's avatar
Alexandre Julliard committed
882 883
   return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
884

885
BOOL PATH_PolyBezierTo(DC *dc, const POINT *pts, DWORD cbPoints)
886
{
887
   GdiPath     *pPath = &dc->path;
888
   POINT       pt;
889
   UINT        i;
890 891 892 893 894 895 896 897 898

   /* Check that path is open */
   if(pPath->state!=PATH_Open)
      return FALSE;

   /* Add a PT_MOVETO if necessary */
   if(pPath->newStroke)
   {
      pPath->newStroke=FALSE;
899 900
      pt.x = dc->CursPosX;
      pt.y = dc->CursPosY;
901
      if(!LPtoDP(dc->hSelf, &pt, 1))
902 903 904 905
         return FALSE;
      if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
         return FALSE;
   }
906

907 908
   for(i = 0; i < cbPoints; i++) {
       pt = pts[i];
909
       if(!LPtoDP(dc->hSelf, &pt, 1))
910 911 912 913 914
	   return FALSE;
       PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
   }
   return TRUE;
}
915

916
BOOL PATH_PolyBezier(DC *dc, const POINT *pts, DWORD cbPoints)
917
{
918
   GdiPath     *pPath = &dc->path;
919
   POINT       pt;
920
   UINT        i;
921 922 923 924 925 926 927

   /* Check that path is open */
   if(pPath->state!=PATH_Open)
      return FALSE;

   for(i = 0; i < cbPoints; i++) {
       pt = pts[i];
928
       if(!LPtoDP(dc->hSelf, &pt, 1))
929 930 931 932 933 934
	   return FALSE;
       PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO);
   }
   return TRUE;
}

Evan Stade's avatar
Evan Stade committed
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
/* PATH_PolyDraw
 *
 * Should be called when a call to PolyDraw is performed on a DC that has
 * an open path. Returns TRUE if successful, else FALSE.
 */
BOOL PATH_PolyDraw(DC *dc, const POINT *pts, const BYTE *types,
    DWORD cbPoints)
{
        GdiPath     *pPath = &dc->path;
        POINT       lastmove, orig_pos;
        INT         i;

        lastmove.x = orig_pos.x = dc->CursPosX;
        lastmove.y = orig_pos.y = dc->CursPosY;

        for(i = pPath->numEntriesUsed - 1; i >= 0; i--){
            if(pPath->pFlags[i] == PT_MOVETO){
                lastmove.x = pPath->pPoints[i].x;
                lastmove.y = pPath->pPoints[i].y;
                if(!DPtoLP(dc->hSelf, &lastmove, 1))
                    return FALSE;
                break;
            }
        }

        for(i = 0; i < cbPoints; i++){
            if(types[i] == PT_MOVETO){
                pPath->newStroke = TRUE;
                lastmove.x = pts[i].x;
                lastmove.y = pts[i].y;
            }
            else if((types[i] & ~PT_CLOSEFIGURE) == PT_LINETO){
                PATH_LineTo(dc, pts[i].x, pts[i].y);
            }
            else if(types[i] == PT_BEZIERTO){
                if(!((i + 2 < cbPoints) && (types[i + 1] == PT_BEZIERTO)
                    && ((types[i + 2] & ~PT_CLOSEFIGURE) == PT_BEZIERTO)))
                    goto err;
                PATH_PolyBezierTo(dc, &(pts[i]), 3);
                i += 2;
            }
            else
                goto err;

            dc->CursPosX = pts[i].x;
            dc->CursPosY = pts[i].y;

            if(types[i] & PT_CLOSEFIGURE){
                pPath->pFlags[pPath->numEntriesUsed-1] |= PT_CLOSEFIGURE;
                pPath->newStroke = TRUE;
                dc->CursPosX = lastmove.x;
                dc->CursPosY = lastmove.y;
            }
        }

        return TRUE;

err:
        if((dc->CursPosX != orig_pos.x) || (dc->CursPosY != orig_pos.y)){
            pPath->newStroke = TRUE;
            dc->CursPosX = orig_pos.x;
            dc->CursPosY = orig_pos.y;
        }

        return FALSE;
}

1002
BOOL PATH_Polyline(DC *dc, const POINT *pts, DWORD cbPoints)
1003
{
1004
   GdiPath     *pPath = &dc->path;
1005
   POINT       pt;
1006
   UINT        i;
1007 1008 1009 1010 1011 1012 1013

   /* Check that path is open */
   if(pPath->state!=PATH_Open)
      return FALSE;

   for(i = 0; i < cbPoints; i++) {
       pt = pts[i];
1014
       if(!LPtoDP(dc->hSelf, &pt, 1))
1015 1016 1017 1018 1019
	   return FALSE;
       PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
   }
   return TRUE;
}
1020

1021
BOOL PATH_PolylineTo(DC *dc, const POINT *pts, DWORD cbPoints)
1022
{
1023
   GdiPath     *pPath = &dc->path;
1024
   POINT       pt;
1025
   UINT        i;
1026 1027 1028 1029 1030 1031 1032 1033 1034

   /* Check that path is open */
   if(pPath->state!=PATH_Open)
      return FALSE;

   /* Add a PT_MOVETO if necessary */
   if(pPath->newStroke)
   {
      pPath->newStroke=FALSE;
1035 1036
      pt.x = dc->CursPosX;
      pt.y = dc->CursPosY;
1037
      if(!LPtoDP(dc->hSelf, &pt, 1))
1038 1039 1040 1041 1042 1043 1044
         return FALSE;
      if(!PATH_AddEntry(pPath, &pt, PT_MOVETO))
         return FALSE;
   }

   for(i = 0; i < cbPoints; i++) {
       pt = pts[i];
1045
       if(!LPtoDP(dc->hSelf, &pt, 1))
1046 1047 1048 1049 1050 1051 1052 1053
	   return FALSE;
       PATH_AddEntry(pPath, &pt, PT_LINETO);
   }

   return TRUE;
}


1054
BOOL PATH_Polygon(DC *dc, const POINT *pts, DWORD cbPoints)
1055
{
1056
   GdiPath     *pPath = &dc->path;
1057
   POINT       pt;
1058
   UINT        i;
1059 1060 1061 1062 1063 1064 1065

   /* Check that path is open */
   if(pPath->state!=PATH_Open)
      return FALSE;

   for(i = 0; i < cbPoints; i++) {
       pt = pts[i];
1066
       if(!LPtoDP(dc->hSelf, &pt, 1))
1067 1068 1069 1070 1071 1072 1073 1074
	   return FALSE;
       PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
		     ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
		      PT_LINETO));
   }
   return TRUE;
}

1075
BOOL PATH_PolyPolygon( DC *dc, const POINT* pts, const INT* counts,
1076 1077
		       UINT polygons )
{
1078
   GdiPath     *pPath = &dc->path;
1079
   POINT       pt, startpt;
1080 1081
   UINT        poly, i;
   INT         point;
1082 1083 1084 1085 1086 1087 1088 1089

   /* Check that path is open */
   if(pPath->state!=PATH_Open)
      return FALSE;

   for(i = 0, poly = 0; poly < polygons; poly++) {
       for(point = 0; point < counts[poly]; point++, i++) {
	   pt = pts[i];
1090
	   if(!LPtoDP(dc->hSelf, &pt, 1))
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
	       return FALSE;
	   if(point == 0) startpt = pt;
	   PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
       }
       /* win98 adds an extra line to close the figure for some reason */
       PATH_AddEntry(pPath, &startpt, PT_LINETO | PT_CLOSEFIGURE);
   }
   return TRUE;
}

1101
BOOL PATH_PolyPolyline( DC *dc, const POINT* pts, const DWORD* counts,
1102 1103
			DWORD polylines )
{
1104
   GdiPath     *pPath = &dc->path;
1105
   POINT       pt;
1106
   UINT        poly, point, i;
1107 1108 1109 1110 1111 1112 1113 1114

   /* Check that path is open */
   if(pPath->state!=PATH_Open)
      return FALSE;

   for(i = 0, poly = 0; poly < polylines; poly++) {
       for(point = 0; point < counts[poly]; point++, i++) {
	   pt = pts[i];
1115
	   if(!LPtoDP(dc->hSelf, &pt, 1))
1116 1117 1118 1119 1120 1121
	       return FALSE;
	   PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
       }
   }
   return TRUE;
}
1122

Alexandre Julliard's avatar
Alexandre Julliard committed
1123 1124 1125 1126
/***********************************************************************
 * Internal functions
 */

1127 1128
/* PATH_CheckCorners
 *
1129
 * Helper function for PATH_RoundRect() and PATH_Rectangle()
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
 */
static BOOL PATH_CheckCorners(DC *dc, POINT corners[], INT x1, INT y1, INT x2, INT y2)
{
   INT temp;

   /* Convert points to device coordinates */
   corners[0].x=x1;
   corners[0].y=y1;
   corners[1].x=x2;
   corners[1].y=y2;
   if(!LPtoDP(dc->hSelf, corners, 2))
      return FALSE;
1142

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
   /* Make sure first corner is top left and second corner is bottom right */
   if(corners[0].x>corners[1].x)
   {
      temp=corners[0].x;
      corners[0].x=corners[1].x;
      corners[1].x=temp;
   }
   if(corners[0].y>corners[1].y)
   {
      temp=corners[0].y;
      corners[0].y=corners[1].y;
      corners[1].y=temp;
   }
1156

1157
   /* In GM_COMPATIBLE, don't include bottom and right edges */
1158
   if(dc->GraphicsMode==GM_COMPATIBLE)
1159 1160 1161 1162 1163 1164 1165
   {
      corners[1].x--;
      corners[1].y--;
   }

   return TRUE;
}
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177

/* PATH_AddFlatBezier
 */
static BOOL PATH_AddFlatBezier(GdiPath *pPath, POINT *pt, BOOL closed)
{
    POINT *pts;
    INT no, i;

    pts = GDI_Bezier( pt, 4, &no );
    if(!pts) return FALSE;

    for(i = 1; i < no; i++)
1178
        PATH_AddEntry(pPath, &pts[i],
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
	    (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
    HeapFree( GetProcessHeap(), 0, pts );
    return TRUE;
}

/* PATH_FlattenPath
 *
 * Replaces Beziers with line segments
 *
 */
static BOOL PATH_FlattenPath(GdiPath *pPath)
{
    GdiPath newPath;
    INT srcpt;

    memset(&newPath, 0, sizeof(newPath));
    newPath.state = PATH_Open;
    for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
        switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
	case PT_MOVETO:
	case PT_LINETO:
	    PATH_AddEntry(&newPath, &pPath->pPoints[srcpt],
			  pPath->pFlags[srcpt]);
	    break;
	case PT_BEZIERTO:
	  PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1],
			     pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
	    srcpt += 2;
	    break;
	}
    }
    newPath.state = PATH_Closed;
    PATH_AssignGdiPath(pPath, &newPath);
1212
    PATH_DestroyGdiPath(&newPath);
1213 1214
    return TRUE;
}
1215

Alexandre Julliard's avatar
Alexandre Julliard committed
1216 1217 1218 1219 1220 1221 1222 1223
/* PATH_PathToRegion
 *
 * Creates a region from the specified path using the specified polygon
 * filling mode. The path is left unchanged. A handle to the region that
 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
 * error occurs, SetLastError is called with the appropriate value and
 * FALSE is returned.
 */
1224
static BOOL PATH_PathToRegion(GdiPath *pPath, INT nPolyFillMode,
1225
   HRGN *pHrgn)
Alexandre Julliard's avatar
Alexandre Julliard committed
1226 1227
{
   int    numStrokes, iStroke, i;
1228 1229
   INT  *pNumPointsInStroke;
   HRGN hrgn;
Alexandre Julliard's avatar
Alexandre Julliard committed
1230 1231 1232

   assert(pPath!=NULL);
   assert(pHrgn!=NULL);
1233

1234
   PATH_FlattenPath(pPath);
1235

Alexandre Julliard's avatar
Alexandre Julliard committed
1236
   /* FIXME: What happens when number of points is zero? */
1237

Alexandre Julliard's avatar
Alexandre Julliard committed
1238 1239 1240 1241 1242 1243 1244 1245
   /* First pass: Find out how many strokes there are in the path */
   /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
   numStrokes=0;
   for(i=0; i<pPath->numEntriesUsed; i++)
      if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
         numStrokes++;

   /* Allocate memory for number-of-points-in-stroke array */
1246
   pNumPointsInStroke=HeapAlloc( GetProcessHeap(), 0, sizeof(int) * numStrokes );
Alexandre Julliard's avatar
Alexandre Julliard committed
1247 1248 1249 1250 1251
   if(!pNumPointsInStroke)
   {
      SetLastError(ERROR_NOT_ENOUGH_MEMORY);
      return FALSE;
   }
1252

Alexandre Julliard's avatar
Alexandre Julliard committed
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
   /* Second pass: remember number of points in each polygon */
   iStroke=-1;  /* Will get incremented to 0 at beginning of first stroke */
   for(i=0; i<pPath->numEntriesUsed; i++)
   {
      /* Is this the beginning of a new stroke? */
      if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
      {
         iStroke++;
	 pNumPointsInStroke[iStroke]=0;
      }

      pNumPointsInStroke[iStroke]++;
   }

   /* Create a region from the strokes */
1268
   hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
Alexandre Julliard's avatar
Alexandre Julliard committed
1269
      numStrokes, nPolyFillMode);
1270 1271 1272 1273

   /* Free memory for number-of-points-in-stroke array */
   HeapFree( GetProcessHeap(), 0, pNumPointsInStroke );

1274
   if(hrgn==NULL)
Alexandre Julliard's avatar
Alexandre Julliard committed
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
   {
      SetLastError(ERROR_NOT_ENOUGH_MEMORY);
      return FALSE;
   }

   /* Success! */
   *pHrgn=hrgn;
   return TRUE;
}

1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
static inline INT int_from_fixed(FIXED f)
{
    return (f.fract >= 0x8000) ? (f.value + 1) : f.value;
}

/**********************************************************************
 *      PATH_BezierTo
 *
 * internally used by PATH_add_outline
 */
static void PATH_BezierTo(GdiPath *pPath, POINT *lppt, INT n)
{
    if (n < 2) return;

    if (n == 2)
    {
        PATH_AddEntry(pPath, &lppt[1], PT_LINETO);
    }
    else if (n == 3)
    {
        PATH_AddEntry(pPath, &lppt[0], PT_BEZIERTO);
        PATH_AddEntry(pPath, &lppt[1], PT_BEZIERTO);
        PATH_AddEntry(pPath, &lppt[2], PT_BEZIERTO);
    }
    else
    {
        POINT pt[3];
        INT i = 0;

        pt[2] = lppt[0];
        n--;

        while (n > 2)
        {
            pt[0] = pt[2];
            pt[1] = lppt[i+1];
            pt[2].x = (lppt[i+2].x + lppt[i+1].x) / 2;
            pt[2].y = (lppt[i+2].y + lppt[i+1].y) / 2;
            PATH_BezierTo(pPath, pt, 3);
            n--;
            i++;
        }

        pt[0] = pt[2];
        pt[1] = lppt[i+1];
        pt[2] = lppt[i+2];
        PATH_BezierTo(pPath, pt, 3);
    }
}

static BOOL PATH_add_outline(DC *dc, INT x, INT y, TTPOLYGONHEADER *header, DWORD size)
{
    GdiPath *pPath = &dc->path;
    TTPOLYGONHEADER *start;
    POINT pt;

    start = header;

    while ((char *)header < (char *)start + size)
    {
        TTPOLYCURVE *curve;

        if (header->dwType != TT_POLYGON_TYPE)
        {
1349
            FIXME("Unknown header type %d\n", header->dwType);
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
            return FALSE;
        }

        pt.x = x + int_from_fixed(header->pfxStart.x);
        pt.y = y - int_from_fixed(header->pfxStart.y);
        LPtoDP(dc->hSelf, &pt, 1);
        PATH_AddEntry(pPath, &pt, PT_MOVETO);

        curve = (TTPOLYCURVE *)(header + 1);

        while ((char *)curve < (char *)header + header->cb)
        {
            /*TRACE("curve->wType %d\n", curve->wType);*/

            switch(curve->wType)
            {
            case TT_PRIM_LINE:
            {
                WORD i;

                for (i = 0; i < curve->cpfx; i++)
                {
                    pt.x = x + int_from_fixed(curve->apfx[i].x);
                    pt.y = y - int_from_fixed(curve->apfx[i].y);
                    LPtoDP(dc->hSelf, &pt, 1);
                    PATH_AddEntry(pPath, &pt, PT_LINETO);
                }
                break;
            }

            case TT_PRIM_QSPLINE:
            case TT_PRIM_CSPLINE:
            {
                WORD i;
                POINTFX ptfx;
                POINT *pts = HeapAlloc(GetProcessHeap(), 0, (curve->cpfx + 1) * sizeof(POINT));

                if (!pts) return FALSE;

                ptfx = *(POINTFX *)((char *)curve - sizeof(POINTFX));

                pts[0].x = x + int_from_fixed(ptfx.x);
                pts[0].y = y - int_from_fixed(ptfx.y);
                LPtoDP(dc->hSelf, &pts[0], 1);

                for(i = 0; i < curve->cpfx; i++)
                {
                    pts[i + 1].x = x + int_from_fixed(curve->apfx[i].x);
                    pts[i + 1].y = y - int_from_fixed(curve->apfx[i].y);
                    LPtoDP(dc->hSelf, &pts[i + 1], 1);
                }

                PATH_BezierTo(pPath, pts, curve->cpfx + 1);

                HeapFree(GetProcessHeap(), 0, pts);
                break;
            }

            default:
                FIXME("Unknown curve type %04x\n", curve->wType);
                return FALSE;
            }

            curve = (TTPOLYCURVE *)&curve->apfx[curve->cpfx];
        }

        header = (TTPOLYGONHEADER *)((char *)header + header->cb);
    }

    return CloseFigure(dc->hSelf);
}

/**********************************************************************
 *      PATH_ExtTextOut
 */
BOOL PATH_ExtTextOut(DC *dc, INT x, INT y, UINT flags, const RECT *lprc,
                     LPCWSTR str, UINT count, const INT *dx)
{
    unsigned int idx;
    double cosEsc, sinEsc;
    LOGFONTW lf;
    POINT org;
    HDC hdc = dc->hSelf;
1433
    INT offset = 0, xoff = 0, yoff = 0;
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486

    TRACE("%p, %d, %d, %08x, %s, %s, %d, %p)\n", hdc, x, y, flags,
	  wine_dbgstr_rect(lprc), debugstr_wn(str, count), count, dx);

    if (!count) return TRUE;

    GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);

    if (lf.lfEscapement != 0)
    {
        cosEsc = cos(lf.lfEscapement * M_PI / 1800);
        sinEsc = sin(lf.lfEscapement * M_PI / 1800);
    } else
    {
        cosEsc = 1;
        sinEsc = 0;
    }

    GetDCOrgEx(hdc, &org);

    for (idx = 0; idx < count; idx++)
    {
        GLYPHMETRICS gm;
        DWORD dwSize;
        void *outline;

        dwSize = GetGlyphOutlineW(hdc, str[idx], GGO_GLYPH_INDEX | GGO_NATIVE, &gm, 0, NULL, NULL);
        if (!dwSize) return FALSE;

        outline = HeapAlloc(GetProcessHeap(), 0, dwSize);
        if (!outline) return FALSE;

        GetGlyphOutlineW(hdc, str[idx], GGO_GLYPH_INDEX | GGO_NATIVE, &gm, dwSize, outline, NULL);

        PATH_add_outline(dc, org.x + x + xoff, org.x + y + yoff, outline, dwSize);

        HeapFree(GetProcessHeap(), 0, outline);

        if (dx)
        {
            offset += dx[idx];
            xoff = offset * cosEsc;
            yoff = offset * -sinEsc;
        }
        else
        {
            xoff += gm.gmCellIncX;
            yoff += gm.gmCellIncY;
        }
    }
    return TRUE;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
/* PATH_EmptyPath
 *
 * Removes all entries from the path and sets the path state to PATH_Null.
 */
static void PATH_EmptyPath(GdiPath *pPath)
{
   assert(pPath!=NULL);

   pPath->state=PATH_Null;
   pPath->numEntriesUsed=0;
}

/* PATH_AddEntry
 *
 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
 * successful, FALSE otherwise (e.g. if not enough memory was available).
 */
1505
BOOL PATH_AddEntry(GdiPath *pPath, const POINT *pPoint, BYTE flags)
Alexandre Julliard's avatar
Alexandre Julliard committed
1506 1507
{
   assert(pPath!=NULL);
1508

Alexandre Julliard's avatar
Alexandre Julliard committed
1509 1510 1511
   /* FIXME: If newStroke is true, perhaps we want to check that we're
    * getting a PT_MOVETO
    */
1512
   TRACE("(%d,%d) - %d\n", pPoint->x, pPoint->y, flags);
Alexandre Julliard's avatar
Alexandre Julliard committed
1513

Alexandre Julliard's avatar
Alexandre Julliard committed
1514 1515 1516
   /* Check that path is open */
   if(pPath->state!=PATH_Open)
      return FALSE;
1517

Alexandre Julliard's avatar
Alexandre Julliard committed
1518 1519 1520 1521 1522
   /* Reserve enough memory for an extra path entry */
   if(!PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1))
      return FALSE;

   /* Store information in path entry */
Alexandre Julliard's avatar
Alexandre Julliard committed
1523
   pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
Alexandre Julliard's avatar
Alexandre Julliard committed
1524 1525
   pPath->pFlags[pPath->numEntriesUsed]=flags;

Alexandre Julliard's avatar
Alexandre Julliard committed
1526 1527 1528 1529
   /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
   if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
      pPath->newStroke=TRUE;

Alexandre Julliard's avatar
Alexandre Julliard committed
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
   /* Increment entry count */
   pPath->numEntriesUsed++;

   return TRUE;
}

/* PATH_ReserveEntries
 *
 * Ensures that at least "numEntries" entries (for points and flags) have
 * been allocated; allocates larger arrays and copies the existing entries
 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
 */
1542
static BOOL PATH_ReserveEntries(GdiPath *pPath, INT numEntries)
Alexandre Julliard's avatar
Alexandre Julliard committed
1543
{
1544 1545
   INT   numEntriesToAllocate;
   POINT *pPointsNew;
Alexandre Julliard's avatar
Alexandre Julliard committed
1546
   BYTE    *pFlagsNew;
1547

Alexandre Julliard's avatar
Alexandre Julliard committed
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
   assert(pPath!=NULL);
   assert(numEntries>=0);

   /* Do we have to allocate more memory? */
   if(numEntries > pPath->numEntriesAllocated)
   {
      /* Find number of entries to allocate. We let the size of the array
       * grow exponentially, since that will guarantee linear time
       * complexity. */
      if(pPath->numEntriesAllocated)
      {
	 numEntriesToAllocate=pPath->numEntriesAllocated;
	 while(numEntriesToAllocate<numEntries)
	    numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/
	       GROW_FACTOR_DENOM;
      }
      else
1565
         numEntriesToAllocate=numEntries;
Alexandre Julliard's avatar
Alexandre Julliard committed
1566 1567

      /* Allocate new arrays */
1568
      pPointsNew=HeapAlloc( GetProcessHeap(), 0, numEntriesToAllocate * sizeof(POINT) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1569 1570
      if(!pPointsNew)
         return FALSE;
1571
      pFlagsNew=HeapAlloc( GetProcessHeap(), 0, numEntriesToAllocate * sizeof(BYTE) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1572 1573
      if(!pFlagsNew)
      {
1574
         HeapFree( GetProcessHeap(), 0, pPointsNew );
Alexandre Julliard's avatar
Alexandre Julliard committed
1575 1576 1577 1578 1579 1580 1581 1582 1583
	 return FALSE;
      }

      /* Copy old arrays to new arrays and discard old arrays */
      if(pPath->pPoints)
      {
         assert(pPath->pFlags);

	 memcpy(pPointsNew, pPath->pPoints,
1584
	     sizeof(POINT)*pPath->numEntriesUsed);
Alexandre Julliard's avatar
Alexandre Julliard committed
1585 1586 1587
	 memcpy(pFlagsNew, pPath->pFlags,
	     sizeof(BYTE)*pPath->numEntriesUsed);

1588 1589
	 HeapFree( GetProcessHeap(), 0, pPath->pPoints );
	 HeapFree( GetProcessHeap(), 0, pPath->pFlags );
Alexandre Julliard's avatar
Alexandre Julliard committed
1590 1591 1592 1593 1594 1595 1596 1597 1598
      }
      pPath->pPoints=pPointsNew;
      pPath->pFlags=pFlagsNew;
      pPath->numEntriesAllocated=numEntriesToAllocate;
   }

   return TRUE;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1599 1600 1601 1602 1603
/* PATH_DoArcPart
 *
 * Creates a Bezier spline that corresponds to part of an arc and appends the
 * corresponding points to the path. The start and end angles are passed in
 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1604 1605
 * at most. If "startEntryType" is non-zero, an entry of that type for the first
 * control point is added to the path; otherwise, it is assumed that the current
Alexandre Julliard's avatar
Alexandre Julliard committed
1606 1607
 * 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)
Alexandre Julliard's avatar
Alexandre Julliard committed
1610 1611 1612
{
   double  halfAngle, a;
   double  xNorm[4], yNorm[4];
1613
   POINT point;
Alexandre Julliard's avatar
Alexandre Julliard committed
1614 1615 1616 1617 1618 1619 1620 1621
   int     i;

   assert(fabs(angleEnd-angleStart)<=M_PI_2);

   /* FIXME: Is there an easier way of computing this? */

   /* Compute control points */
   halfAngle=(angleEnd-angleStart)/2.0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
   if(fabs(halfAngle)>1e-8)
   {
      a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
      xNorm[0]=cos(angleStart);
      yNorm[0]=sin(angleStart);
      xNorm[1]=xNorm[0] - a*yNorm[0];
      yNorm[1]=yNorm[0] + a*xNorm[0];
      xNorm[3]=cos(angleEnd);
      yNorm[3]=sin(angleEnd);
      xNorm[2]=xNorm[3] + a*yNorm[3];
      yNorm[2]=yNorm[3] - a*xNorm[3];
   }
   else
      for(i=0; i<4; i++)
      {
	 xNorm[i]=cos(angleStart);
	 yNorm[i]=sin(angleStart);
      }
1640

Alexandre Julliard's avatar
Alexandre Julliard committed
1641
   /* Add starting point to path if desired */
1642
   if(startEntryType)
Alexandre Julliard's avatar
Alexandre Julliard committed
1643 1644
   {
      PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1645
      if(!PATH_AddEntry(pPath, &point, startEntryType))
Alexandre Julliard's avatar
Alexandre Julliard committed
1646 1647 1648 1649 1650 1651 1652
         return FALSE;
   }

   /* Add remaining control points */
   for(i=1; i<4; i++)
   {
      PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
Alexandre Julliard's avatar
Alexandre Julliard committed
1653
      if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
Alexandre Julliard's avatar
Alexandre Julliard committed
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
         return FALSE;
   }

   return TRUE;
}

/* PATH_ScaleNormalizedPoint
 *
 * Scales a normalized point (x, y) with respect to the box whose corners are
 * passed in "corners". The point is stored in "*pPoint". The normalized
 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
 * (1.0, 1.0) correspond to corners[1].
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1667
static void PATH_ScaleNormalizedPoint(FLOAT_POINT corners[], double x,
1668
   double y, POINT *pPoint)
Alexandre Julliard's avatar
Alexandre Julliard committed
1669
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1670
   pPoint->x=GDI_ROUND( (double)corners[0].x +
Alexandre Julliard's avatar
Alexandre Julliard committed
1671
      (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1672
   pPoint->y=GDI_ROUND( (double)corners[0].y +
Alexandre Julliard's avatar
Alexandre Julliard committed
1673 1674 1675 1676 1677 1678 1679 1680
      (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
}

/* PATH_NormalizePoint
 *
 * Normalizes a point with respect to the box whose corners are passed in
 * "corners". The normalized coordinates are stored in "*pX" and "*pY".
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1681 1682
static void PATH_NormalizePoint(FLOAT_POINT corners[],
   const FLOAT_POINT *pPoint,
Alexandre Julliard's avatar
Alexandre Julliard committed
1683 1684 1685 1686 1687 1688 1689
   double *pX, double *pY)
{
   *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) *
      2.0 - 1.0;
   *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) *
      2.0 - 1.0;
}
1690

1691

1692
/*******************************************************************
1693
 *      FlattenPath [GDI32.@]
1694 1695 1696
 *
 *
 */
1697
BOOL WINAPI FlattenPath(HDC hdc)
1698
{
1699
    BOOL ret = FALSE;
1700
    DC *dc = get_dc_ptr( hdc );
1701

1702
    if(!dc) return FALSE;
1703

1704
    if(dc->funcs->pFlattenPath) ret = dc->funcs->pFlattenPath(dc->physDev);
1705
    else
1706
    {
1707
	GdiPath *pPath = &dc->path;
1708 1709 1710
        if(pPath->state != PATH_Closed)
	    ret = PATH_FlattenPath(pPath);
    }
1711
    release_dc_ptr( dc );
1712
    return ret;
1713 1714
}

1715

1716
static BOOL PATH_StrokePath(DC *dc, GdiPath *pPath)
1717
{
1718 1719
    INT i, nLinePts, nAlloc;
    POINT *pLinePts;
1720 1721 1722 1723 1724
    POINT ptViewportOrg, ptWindowOrg;
    SIZE szViewportExt, szWindowExt;
    DWORD mapMode, graphicsMode;
    XFORM xform;
    BOOL ret = TRUE;
1725

1726
    if(dc->funcs->pStrokePath)
1727
        return dc->funcs->pStrokePath(dc->physDev);
1728

1729 1730
    if(pPath->state != PATH_Closed)
        return FALSE;
1731
    
1732 1733 1734 1735 1736 1737 1738
    /* Save the mapping mode info */
    mapMode=GetMapMode(dc->hSelf);
    GetViewportExtEx(dc->hSelf, &szViewportExt);
    GetViewportOrgEx(dc->hSelf, &ptViewportOrg);
    GetWindowExtEx(dc->hSelf, &szWindowExt);
    GetWindowOrgEx(dc->hSelf, &ptWindowOrg);
    GetWorldTransform(dc->hSelf, &xform);
1739

1740
    /* Set MM_TEXT */
1741 1742 1743
    SetMapMode(dc->hSelf, MM_TEXT);
    SetViewportOrgEx(dc->hSelf, 0, 0, NULL);
    SetWindowOrgEx(dc->hSelf, 0, 0, NULL);
1744 1745 1746 1747
    graphicsMode=GetGraphicsMode(dc->hSelf);
    SetGraphicsMode(dc->hSelf, GM_ADVANCED);
    ModifyWorldTransform(dc->hSelf, &xform, MWT_IDENTITY);
    SetGraphicsMode(dc->hSelf, graphicsMode);
1748

1749 1750 1751 1752 1753 1754 1755
    /* Allocate enough memory for the worst case without beziers (one PT_MOVETO
     * and the rest PT_LINETO with PT_CLOSEFIGURE at the end) plus some buffer 
     * space in case we get one to keep the number of reallocations small. */
    nAlloc = pPath->numEntriesUsed + 1 + 300; 
    pLinePts = HeapAlloc(GetProcessHeap(), 0, nAlloc * sizeof(POINT));
    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 1760 1761 1762 1763 1764
	    ERR("Expected PT_MOVETO %s, got path flag %d\n", 
	        i == 0 ? "as first point" : "after PT_CLOSEFIGURE",
		(INT)pPath->pFlags[i]);
	    ret = FALSE;
	    goto end;
	}
1765 1766
        switch(pPath->pFlags[i]) {
	case PT_MOVETO:
1767
            TRACE("Got PT_MOVETO (%d, %d)\n",
1768
		  pPath->pPoints[i].x, pPath->pPoints[i].y);
1769 1770 1771 1772
	    if(nLinePts >= 2)
	        Polyline(dc->hSelf, pLinePts, nLinePts);
	    nLinePts = 0;
	    pLinePts[nLinePts++] = pPath->pPoints[i];
1773 1774 1775
	    break;
	case PT_LINETO:
	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 1780 1781
	    break;
	case PT_BEZIERTO:
	    TRACE("Got PT_BEZIERTO\n");
1782
	    if(pPath->pFlags[i+1] != PT_BEZIERTO ||
1783 1784
	       (pPath->pFlags[i+2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) {
	        ERR("Path didn't contain 3 successive PT_BEZIERTOs\n");
1785 1786
		ret = FALSE;
		goto end;
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
	    } else {
	        INT nBzrPts, nMinAlloc;
	        POINT *pBzrPts = GDI_Bezier(&pPath->pPoints[i-1], 4, &nBzrPts);
		/* Make sure we have allocated enough memory for the lines of 
		 * this bezier and the rest of the path, assuming we won't get
		 * another one (since we won't reallocate again then). */
		nMinAlloc = nLinePts + (pPath->numEntriesUsed - i) + nBzrPts;
		if(nAlloc < nMinAlloc)
		{
		    nAlloc = nMinAlloc * 2;
		    pLinePts = HeapReAlloc(GetProcessHeap(), 0, pLinePts,
		                           nAlloc * sizeof(POINT));
		}
		memcpy(&pLinePts[nLinePts], &pBzrPts[1],
		       (nBzrPts - 1) * sizeof(POINT));
		nLinePts += nBzrPts - 1;
		HeapFree(GetProcessHeap(), 0, pBzrPts);
		i += 2;
1805 1806 1807 1808
	    }
	    break;
	default:
	    ERR("Got path flag %d\n", (INT)pPath->pFlags[i]);
1809 1810
	    ret = FALSE;
	    goto end;
1811 1812
	}
	if(pPath->pFlags[i] & PT_CLOSEFIGURE)
1813
	    pLinePts[nLinePts++] = pLinePts[0];
1814
    }
1815 1816
    if(nLinePts >= 2)
        Polyline(dc->hSelf, pLinePts, nLinePts);
1817 1818

 end:
1819
    HeapFree(GetProcessHeap(), 0, pLinePts);
1820 1821 1822 1823 1824

    /* Restore the old mapping mode */
    SetMapMode(dc->hSelf, mapMode);
    SetWindowExtEx(dc->hSelf, szWindowExt.cx, szWindowExt.cy, NULL);
    SetWindowOrgEx(dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL);
1825 1826
    SetViewportExtEx(dc->hSelf, szViewportExt.cx, szViewportExt.cy, NULL);
    SetViewportOrgEx(dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL);
1827 1828 1829 1830 1831 1832

    /* Go to GM_ADVANCED temporarily to restore the world transform */
    graphicsMode=GetGraphicsMode(dc->hSelf);
    SetGraphicsMode(dc->hSelf, GM_ADVANCED);
    SetWorldTransform(dc->hSelf, &xform);
    SetGraphicsMode(dc->hSelf, graphicsMode);
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845

    /* If we've moved the current point then get its new position
       which will be in device (MM_TEXT) co-ords, convert it to
       logical co-ords and re-set it.  This basically updates
       dc->CurPosX|Y so that their values are in the correct mapping
       mode.
    */
    if(i > 0) {
        POINT pt;
        GetCurrentPositionEx(dc->hSelf, &pt);
        DPtoLP(dc->hSelf, &pt, 1);
        MoveToEx(dc->hSelf, pt.x, pt.y, NULL);
    }
1846

1847
    return ret;
1848 1849
}

1850
#define round(x) ((int)((x)>0?(x)+0.5:(x)-0.5))
1851

1852 1853
static BOOL PATH_WidenPath(DC *dc)
{
1854
    INT i, j, numStrokes, nLinePts, penWidth, penWidthIn, penWidthOut, size, penStyle;
1855 1856 1857
    BOOL ret = FALSE;
    GdiPath *pPath, *pNewPath, **pStrokes, *pUpPath, *pDownPath;
    EXTLOGPEN *elp;
1858
    DWORD obj_type, joint, endcap, penType;
1859 1860 1861

    pPath = &dc->path;

1862 1863
    if(pPath->state == PATH_Open) {
       SetLastError(ERROR_CAN_NOT_COMPLETE);
1864 1865 1866
       return FALSE;
    }

1867 1868
    PATH_FlattenPath(pPath);

1869
    size = GetObjectW( dc->hPen, 0, NULL );
1870 1871 1872 1873
    if (!size) {
        SetLastError(ERROR_CAN_NOT_COMPLETE);
        return FALSE;
    }
1874 1875 1876

    elp = HeapAlloc( GetProcessHeap(), 0, size );
    GetObjectW( dc->hPen, size, elp );
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890

    obj_type = GetObjectType(dc->hPen);
    if(obj_type == OBJ_PEN) {
        penStyle = ((LOGPEN*)elp)->lopnStyle;
    }
    else if(obj_type == OBJ_EXTPEN) {
        penStyle = elp->elpPenStyle;
    }
    else {
        SetLastError(ERROR_CAN_NOT_COMPLETE);
        HeapFree( GetProcessHeap(), 0, elp );
        return FALSE;
    }

1891 1892 1893
    penWidth = elp->elpWidth;
    HeapFree( GetProcessHeap(), 0, elp );

1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
    endcap = (PS_ENDCAP_MASK & penStyle);
    joint = (PS_JOIN_MASK & penStyle);
    penType = (PS_TYPE_MASK & penStyle);

    /* The function cannot apply to cosmetic pens */
    if(obj_type == OBJ_EXTPEN && penType == PS_COSMETIC) {
        SetLastError(ERROR_CAN_NOT_COMPLETE);
        return FALSE;
    }

1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
    penWidthIn = penWidth / 2;
    penWidthOut = penWidth / 2;
    if(penWidthIn + penWidthOut < penWidth)
        penWidthOut++;

    numStrokes = 0;
    nLinePts = 0;

    pStrokes = HeapAlloc(GetProcessHeap(), 0, numStrokes * sizeof(GdiPath*));
    pStrokes[0] = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
    PATH_InitGdiPath(pStrokes[0]);
    pStrokes[0]->pFlags = HeapAlloc(GetProcessHeap(), 0, pPath->numEntriesUsed * sizeof(INT));
    pStrokes[0]->pPoints = HeapAlloc(GetProcessHeap(), 0, pPath->numEntriesUsed * sizeof(POINT));
    pStrokes[0]->numEntriesUsed = 0;

    for(i = 0, j = 0; i < pPath->numEntriesUsed; i++, j++) {
1920
        POINT point;
1921 1922 1923 1924 1925 1926 1927 1928 1929
        if((i == 0 || (pPath->pFlags[i-1] & PT_CLOSEFIGURE)) &&
            (pPath->pFlags[i] != PT_MOVETO)) {
            ERR("Expected PT_MOVETO %s, got path flag %c\n",
                i == 0 ? "as first point" : "after PT_CLOSEFIGURE",
                pPath->pFlags[i]);
            return FALSE;
        }
        switch(pPath->pFlags[i]) {
            case PT_MOVETO:
1930 1931 1932
                if(numStrokes > 0) {
                    pStrokes[numStrokes - 1]->state = PATH_Closed;
                }
1933 1934 1935 1936 1937
                numStrokes++;
                j = 0;
                pStrokes = HeapReAlloc(GetProcessHeap(), 0, pStrokes, numStrokes * sizeof(GdiPath*));
                pStrokes[numStrokes - 1] = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
                PATH_InitGdiPath(pStrokes[numStrokes - 1]);
1938
                pStrokes[numStrokes - 1]->state = PATH_Open;
1939 1940
            case PT_LINETO:
            case (PT_LINETO | PT_CLOSEFIGURE):
1941 1942 1943
                point.x = pPath->pPoints[i].x;
                point.y = pPath->pPoints[i].y;
                PATH_AddEntry(pStrokes[numStrokes - 1], &point, pPath->pFlags[i]);
1944 1945 1946
                break;
            case PT_BEZIERTO:
                /* should never happen because of the FlattenPath call */
1947
                ERR("Should never happen\n");
1948 1949 1950 1951 1952 1953 1954 1955 1956
                break;
            default:
                ERR("Got path flag %c\n", pPath->pFlags[i]);
                return FALSE;
        }
    }

    pNewPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
    PATH_InitGdiPath(pNewPath);
1957
    pNewPath->state = PATH_Open;
1958 1959 1960 1961

    for(i = 0; i < numStrokes; i++) {
        pUpPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
        PATH_InitGdiPath(pUpPath);
1962
        pUpPath->state = PATH_Open;
1963 1964
        pDownPath = HeapAlloc(GetProcessHeap(), 0, sizeof(GdiPath));
        PATH_InitGdiPath(pDownPath);
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
        pDownPath->state = PATH_Open;

        for(j = 0; j < pStrokes[i]->numEntriesUsed; j++) {
            /* Beginning or end of the path if not closed */
            if((!(pStrokes[i]->pFlags[pStrokes[i]->numEntriesUsed - 1] & PT_CLOSEFIGURE)) && (j == 0 || j == pStrokes[i]->numEntriesUsed - 1) ) {
                /* Compute segment angle */
                FLOAT xo, yo, xa, ya;
                POINT pt;
                FLOAT theta, scalarProduct;
                FLOAT_POINT corners[2];
                if(j == 0) {
                    xo = pStrokes[i]->pPoints[j].x;
                    yo = pStrokes[i]->pPoints[j].y;
                    xa = pStrokes[i]->pPoints[1].x;
                    ya = pStrokes[i]->pPoints[1].y;
                }
                else {
                    xa = pStrokes[i]->pPoints[j - 1].x;
                    ya = pStrokes[i]->pPoints[j - 1].y;
                    xo = pStrokes[i]->pPoints[j].x;
                    yo = pStrokes[i]->pPoints[j].y;
                }
                scalarProduct = (xa - xo) /sqrt(pow((xa - xo), 2) + pow((ya - yo), 2));
                theta = acos(scalarProduct);
                if( (ya - yo) < 0) {
                    theta = -theta;
                }
                switch(endcap) {
                    case PS_ENDCAP_SQUARE :
                        pt.x = xo + round(sqrt(2) * penWidthOut * cos(M_PI_4 + theta));
                        pt.y = yo + round(sqrt(2) * penWidthOut * sin(M_PI_4 + theta));
                        PATH_AddEntry(pUpPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO) );
                        pt.x = xo + round(sqrt(2) * penWidthIn * cos(- M_PI_4 + theta));
                        pt.y = yo + round(sqrt(2) * penWidthIn * sin(- M_PI_4 + theta));
                        PATH_AddEntry(pUpPath, &pt, PT_LINETO);
                        break;
                    case PS_ENDCAP_FLAT :
                        pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
                        pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
                        PATH_AddEntry(pUpPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO));
                        pt.x = xo - round( penWidthIn * cos(theta + M_PI_2) );
                        pt.y = yo - round( penWidthIn * sin(theta + M_PI_2) );
                        PATH_AddEntry(pUpPath, &pt, PT_LINETO);
                        break;
                    case PS_ENDCAP_ROUND :
                    default :
                        corners[0].x = xo - penWidthIn;
                        corners[0].y = yo - penWidthIn;
                        corners[1].x = xo + penWidthOut;
                        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 2017 2018 2019 2020
                        PATH_DoArcPart(pUpPath ,corners, theta + 3 * M_PI_4 , theta + M_PI, FALSE);
                        PATH_DoArcPart(pUpPath ,corners, theta + M_PI, theta +  5 * M_PI_4, FALSE);
                        PATH_DoArcPart(pUpPath ,corners, theta + 5 * M_PI_4 , theta + 3 * M_PI_2, FALSE);
                        break;
                }
2021
            }
2022
            /* Corpse of the path */
2023
            else {
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147
                /* Compute angle */
                INT previous, next;
                FLOAT xa, ya, xb, yb, xo, yo;
                FLOAT alpha, theta;
                FLOAT scalarProduct, oa, ob, miterWidth;
                DWORD _joint = joint;
                POINT pt;
		GdiPath *pInsidePath, *pOutsidePath;
                if(j > 0 && j < pStrokes[i]->numEntriesUsed - 1) {
                    previous = j - 1;
                    next = j + 1;
                }
                else if (j == 0) {
                    previous = pStrokes[i]->numEntriesUsed - 1;
                    next = j + 1;
                }
                else {
                    previous = j - 1;
                    next = 0;
                }
                xo = pStrokes[i]->pPoints[j].x;
                yo = pStrokes[i]->pPoints[j].y;
                xa = pStrokes[i]->pPoints[previous].x;
                ya = pStrokes[i]->pPoints[previous].y;
                xb = pStrokes[i]->pPoints[next].x;
                yb = pStrokes[i]->pPoints[next].y;
                oa = sqrt(pow((xa - xo), 2) + pow((ya - yo), 2));
                ob = sqrt(pow((xb - xo), 2) + pow((yb - yo), 2));
                scalarProduct = ((xa - xo) * (xb - xo) + (ya - yo) * (yb - yo))/ (oa * ob);
                alpha = acos(scalarProduct);
                if(( (xa - xo) * (yb - yo) - (ya - yo) * (xb - xo) ) < 0) {
                    alpha = -alpha;
                }
                scalarProduct = (xo - xa) / oa;
                theta = acos(scalarProduct);
                if( (yo - ya) < 0) {
                    theta = -theta;
                }
                if(_joint == PS_JOIN_MITER && dc->miterLimit < fabs(1 / sin(alpha/2))) {
                    _joint = PS_JOIN_BEVEL;
                }
                if(alpha > 0) {
                    pInsidePath = pUpPath;
                    pOutsidePath = pDownPath;
                }
                else if(alpha < 0) {
                    pInsidePath = pDownPath;
                    pOutsidePath = pUpPath;
                }
                else {
                    continue;
                }
                /* Inside angle points */
                if(alpha > 0) {
                    pt.x = xo - round( penWidthIn * cos(theta + M_PI_2) );
                    pt.y = yo - round( penWidthIn * sin(theta + M_PI_2) );
                }
                else {
                    pt.x = xo + round( penWidthIn * cos(theta + M_PI_2) );
                    pt.y = yo + round( penWidthIn * sin(theta + M_PI_2) );
                }
                PATH_AddEntry(pInsidePath, &pt, PT_LINETO);
                if(alpha > 0) {
                    pt.x = xo + round( penWidthIn * cos(M_PI_2 + alpha + theta) );
                    pt.y = yo + round( penWidthIn * sin(M_PI_2 + alpha + theta) );
                }
                else {
                    pt.x = xo - round( penWidthIn * cos(M_PI_2 + alpha + theta) );
                    pt.y = yo - round( penWidthIn * sin(M_PI_2 + alpha + theta) );
                }
                PATH_AddEntry(pInsidePath, &pt, PT_LINETO);
                /* Outside angle point */
                switch(_joint) {
                     case PS_JOIN_MITER :
                        miterWidth = fabs(penWidthOut / cos(M_PI_2 - fabs(alpha) / 2));
                        pt.x = xo + round( miterWidth * cos(theta + alpha / 2) );
                        pt.y = yo + round( miterWidth * sin(theta + alpha / 2) );
                        PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
                        break;
                    case PS_JOIN_BEVEL :
                        if(alpha > 0) {
                            pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
                            pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
                        }
                        else {
                            pt.x = xo - round( penWidthOut * cos(theta + M_PI_2) );
                            pt.y = yo - round( penWidthOut * sin(theta + M_PI_2) );
                        }
                        PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
                        if(alpha > 0) {
                            pt.x = xo - round( penWidthOut * cos(M_PI_2 + alpha + theta) );
                            pt.y = yo - round( penWidthOut * sin(M_PI_2 + alpha + theta) );
                        }
                        else {
                            pt.x = xo + round( penWidthOut * cos(M_PI_2 + alpha + theta) );
                            pt.y = yo + round( penWidthOut * sin(M_PI_2 + alpha + theta) );
                        }
                        PATH_AddEntry(pOutsidePath, &pt, PT_LINETO);
                        break;
                    case PS_JOIN_ROUND :
                    default :
                        if(alpha > 0) {
                            pt.x = xo + round( penWidthOut * cos(theta + M_PI_2) );
                            pt.y = yo + round( penWidthOut * sin(theta + M_PI_2) );
                        }
                        else {
                            pt.x = xo - round( penWidthOut * cos(theta + M_PI_2) );
                            pt.y = yo - round( penWidthOut * sin(theta + M_PI_2) );
                        }
                        PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
                        pt.x = xo + round( penWidthOut * cos(theta + alpha / 2) );
                        pt.y = yo + round( penWidthOut * sin(theta + alpha / 2) );
                        PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
                        if(alpha > 0) {
                            pt.x = xo - round( penWidthOut * cos(M_PI_2 + alpha + theta) );
                            pt.y = yo - round( penWidthOut * sin(M_PI_2 + alpha + theta) );
                        }
                        else {
                            pt.x = xo + round( penWidthOut * cos(M_PI_2 + alpha + theta) );
                            pt.y = yo + round( penWidthOut * sin(M_PI_2 + alpha + theta) );
                        }
                        PATH_AddEntry(pOutsidePath, &pt, PT_BEZIERTO);
                        break;
                }
2148 2149 2150
            }
        }
        for(j = 0; j < pUpPath->numEntriesUsed; j++) {
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
            POINT pt;
            pt.x = pUpPath->pPoints[j].x;
            pt.y = pUpPath->pPoints[j].y;
            PATH_AddEntry(pNewPath, &pt, (j == 0 ? PT_MOVETO : PT_LINETO));
        }
        for(j = 0; j < pDownPath->numEntriesUsed; j++) {
            POINT pt;
            pt.x = pDownPath->pPoints[pDownPath->numEntriesUsed - j - 1].x;
            pt.y = pDownPath->pPoints[pDownPath->numEntriesUsed - j - 1].y;
            PATH_AddEntry(pNewPath, &pt, ( (j == 0 && (pStrokes[i]->pFlags[pStrokes[i]->numEntriesUsed - 1] & PT_CLOSEFIGURE)) ? PT_MOVETO : PT_LINETO));
2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180
        }

        PATH_DestroyGdiPath(pStrokes[i]);
        HeapFree(GetProcessHeap(), 0, pStrokes[i]);
        PATH_DestroyGdiPath(pUpPath);
        HeapFree(GetProcessHeap(), 0, pUpPath);
        PATH_DestroyGdiPath(pDownPath);
        HeapFree(GetProcessHeap(), 0, pDownPath);
    }
    HeapFree(GetProcessHeap(), 0, pStrokes);

    pNewPath->state = PATH_Closed;
    if (!(ret = PATH_AssignGdiPath(pPath, pNewPath)))
        ERR("Assign path failed\n");
    PATH_DestroyGdiPath(pNewPath);
    HeapFree(GetProcessHeap(), 0, pNewPath);
    return ret;
}


2181
/*******************************************************************
2182
 *      StrokeAndFillPath [GDI32.@]
2183 2184 2185
 *
 *
 */
2186
BOOL WINAPI StrokeAndFillPath(HDC hdc)
2187
{
2188
   DC *dc = get_dc_ptr( hdc );
2189
   BOOL bRet = FALSE;
2190

2191
   if(!dc) return FALSE;
2192 2193

   if(dc->funcs->pStrokeAndFillPath)
2194
       bRet = dc->funcs->pStrokeAndFillPath(dc->physDev);
2195 2196
   else
   {
2197 2198 2199
       bRet = PATH_FillPath(dc, &dc->path);
       if(bRet) bRet = PATH_StrokePath(dc, &dc->path);
       if(bRet) PATH_EmptyPath(&dc->path);
2200
   }
2201
   release_dc_ptr( dc );
2202
   return bRet;
2203 2204
}

2205

2206
/*******************************************************************
2207
 *      StrokePath [GDI32.@]
2208 2209 2210
 *
 *
 */
2211
BOOL WINAPI StrokePath(HDC hdc)
2212
{
2213
    DC *dc = get_dc_ptr( hdc );
2214
    GdiPath *pPath;
2215
    BOOL bRet = FALSE;
2216

2217
    TRACE("(%p)\n", hdc);
2218
    if(!dc) return FALSE;
2219 2220

    if(dc->funcs->pStrokePath)
2221
        bRet = dc->funcs->pStrokePath(dc->physDev);
2222 2223
    else
    {
2224
        pPath = &dc->path;
2225 2226 2227
        bRet = PATH_StrokePath(dc, pPath);
        PATH_EmptyPath(pPath);
    }
2228
    release_dc_ptr( dc );
2229
    return bRet;
2230 2231
}

2232

2233
/*******************************************************************
2234
 *      WidenPath [GDI32.@]
2235 2236 2237
 *
 *
 */
2238
BOOL WINAPI WidenPath(HDC hdc)
2239
{
2240
   DC *dc = get_dc_ptr( hdc );
2241
   BOOL ret = FALSE;
2242

2243
   if(!dc) return FALSE;
2244 2245

   if(dc->funcs->pWidenPath)
2246 2247 2248
      ret = dc->funcs->pWidenPath(dc->physDev);
   else
      ret = PATH_WidenPath(dc);
2249
   release_dc_ptr( dc );
2250
   return ret;
2251
}