748cd9d3ba85c2f724b160c2951dc9b2abc32aae
[reactos.git] / reactos / subsys / win32k / objects / path.c
1 /*
2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id$ */
20
21 #include <w32k.h>
22 #include <win32k/float.h>
23
24 #define NDEBUG
25 #include <debug.h>
26
27 #define NUM_ENTRIES_INITIAL 16 /* Initial size of points / flags arrays */
28 #define GROW_FACTOR_NUMER 2 /* Numerator of grow factor for the array */
29 #define GROW_FACTOR_DENOM 1 /* Denominator of grow factor */
30
31 BOOL FASTCALL PATH_AddEntry (GdiPath *pPath, const POINT *pPoint, BYTE flags);
32 BOOL FASTCALL PATH_AddFlatBezier (GdiPath *pPath, POINT *pt, BOOL closed);
33 BOOL FASTCALL PATH_DoArcPart (GdiPath *pPath, FLOAT_POINT corners[], double angleStart, double angleEnd, BOOL addMoveTo);
34 BOOL FASTCALL PATH_FillPath( PDC dc, GdiPath *pPath );
35 BOOL FASTCALL PATH_FlattenPath (GdiPath *pPath);
36 VOID FASTCALL PATH_GetPathFromDC (PDC dc, GdiPath **ppPath);
37 VOID FASTCALL PATH_NormalizePoint (FLOAT_POINT corners[], const FLOAT_POINT *pPoint, double *pX, double *pY);
38 BOOL FASTCALL PATH_PathToRegion (GdiPath *pPath, INT nPolyFillMode, HRGN *pHrgn);
39 BOOL FASTCALL PATH_ReserveEntries (GdiPath *pPath, INT numEntries);
40 VOID FASTCALL PATH_ScaleNormalizedPoint (FLOAT_POINT corners[], double x, double y, POINT *pPoint);
41
42
43 INT FASTCALL
44 IntGdiGetArcDirection(DC *dc);
45
46 BOOL
47 STDCALL
48 NtGdiAbortPath(HDC hDC)
49 {
50 GdiPath *pPath;
51 BOOL ret = TRUE;
52 PDC dc = DC_LockDc ( hDC );
53
54 if( !dc ) return FALSE;
55
56 /* Get pointer to path */
57 PATH_GetPathFromDC ( dc, &pPath );
58
59 PATH_EmptyPath( pPath );
60
61 DC_UnlockDc ( dc );
62 return ret;
63 }
64
65 BOOL
66 STDCALL
67 NtGdiBeginPath( HDC hDC )
68 {
69 GdiPath *pPath;
70 BOOL ret = TRUE;
71 PDC dc = DC_LockDc ( hDC );
72
73 if( !dc ) return FALSE;
74
75 /* Get pointer to path */
76 PATH_GetPathFromDC ( dc, &pPath );
77
78 /* If path is already open, do nothing */
79 if ( pPath->state != PATH_Open )
80 {
81 /* Make sure that path is empty */
82 PATH_EmptyPath( pPath );
83
84 /* Initialize variables for new path */
85 pPath->newStroke = TRUE;
86 pPath->state = PATH_Open;
87 }
88
89 DC_UnlockDc ( dc );
90 return ret;
91 }
92
93 BOOL
94 FASTCALL
95 IntCloseFigure ( PDC dc )
96 {
97 UNIMPLEMENTED;
98 return FALSE;
99 }
100
101 BOOL
102 STDCALL
103 NtGdiCloseFigure ( HDC hDC )
104 {
105 PDC dc = DC_LockDc ( hDC );
106 BOOL ret = FALSE; // default to failure
107
108 if ( dc )
109 {
110 ret = IntCloseFigure ( dc );
111 DC_UnlockDc ( dc );
112 }
113
114 return ret;
115 }
116
117 BOOL
118 STDCALL
119 NtGdiEndPath(HDC hDC)
120 {
121 GdiPath *pPath;
122 BOOL ret = TRUE;
123 PDC dc = DC_LockDc ( hDC );
124
125 if ( !dc ) return FALSE;
126
127 /* Get pointer to path */
128 PATH_GetPathFromDC ( dc, &pPath );
129
130 /* Check that path is currently being constructed */
131 if( pPath->state != PATH_Open )
132 {
133 ret = FALSE;
134 }
135 /* Set flag to indicate that path is finished */
136 else pPath->state = PATH_Closed;
137
138 DC_UnlockDc ( dc );
139 return ret;
140 }
141
142 BOOL
143 STDCALL
144 NtGdiFillPath(HDC hDC)
145 {
146 GdiPath *pPath;
147 BOOL ret = TRUE;
148 PDC dc = DC_LockDc ( hDC );
149
150 if ( !dc ) return FALSE;
151
152 /* Get pointer to path */
153 PATH_GetPathFromDC ( dc, &pPath );
154
155 ret = PATH_FillPath( dc, pPath );
156 if( ret )
157 {
158 /* FIXME: Should the path be emptied even if conversion
159 failed? */
160 PATH_EmptyPath( pPath );
161 }
162
163 DC_UnlockDc ( dc );
164 return ret;
165 }
166
167 BOOL
168 STDCALL
169 NtGdiFlattenPath(HDC hDC)
170 {
171 UNIMPLEMENTED;
172 return FALSE;
173 }
174
175
176 BOOL
177 STDCALL
178 NtGdiGetMiterLimit(HDC hDC,
179 PFLOAT Limit)
180 {
181 UNIMPLEMENTED;
182 return FALSE;
183 }
184
185 INT
186 STDCALL
187 NtGdiGetPath(HDC hDC,
188 LPPOINT Points,
189 LPBYTE Types,
190 INT nSize)
191 {
192 UNIMPLEMENTED;
193 return 0;
194 }
195
196 HRGN
197 STDCALL
198 NtGdiPathToRegion(HDC hDC)
199 {
200 UNIMPLEMENTED;
201 return 0;
202 }
203
204 BOOL
205 STDCALL
206 NtGdiSetMiterLimit(HDC hDC,
207 FLOAT NewLimit,
208 PFLOAT OldLimit)
209 {
210 UNIMPLEMENTED;
211 return FALSE;
212 }
213
214 BOOL
215 STDCALL
216 NtGdiStrokeAndFillPath(HDC hDC)
217 {
218 UNIMPLEMENTED;
219 return FALSE;
220 }
221
222 BOOL
223 STDCALL
224 NtGdiStrokePath(HDC hDC)
225 {
226 UNIMPLEMENTED;
227 return FALSE;
228 }
229
230 BOOL
231 STDCALL
232 NtGdiWidenPath(HDC hDC)
233 {
234 UNIMPLEMENTED;
235 return FALSE;
236 }
237
238 BOOL STDCALL NtGdiSelectClipPath(HDC hDC,
239 int Mode)
240 {
241 GdiPath *pPath;
242 HRGN hrgnPath;
243 BOOL success = FALSE;
244 PDC dc = DC_LockDc ( hDC );
245
246 if( !dc ) return FALSE;
247
248 PATH_GetPathFromDC ( dc, &pPath );
249
250 /* Check that path is closed */
251 if( pPath->state != PATH_Closed )
252 {
253 SetLastWin32Error(ERROR_CAN_NOT_COMPLETE);
254 return FALSE;
255 }
256 /* Construct a region from the path */
257 else if( PATH_PathToRegion( pPath, dc->w.polyFillMode, &hrgnPath ) )
258 {
259 success = IntGdiExtSelectClipRgn( dc, hrgnPath, Mode ) != ERROR;
260 NtGdiDeleteObject( hrgnPath );
261
262 /* Empty the path */
263 if( success )
264 PATH_EmptyPath( pPath );
265 /* FIXME: Should this function delete the path even if it failed? */
266 }
267
268 DC_UnlockDc ( dc );
269 return success;
270 }
271
272 /***********************************************************************
273 * Exported functions
274 */
275
276
277 /* PATH_FillPath
278 *
279 *
280 */
281 BOOL
282 FASTCALL
283 PATH_FillPath( PDC dc, GdiPath *pPath )
284 {
285 INT mapMode, graphicsMode;
286 SIZE ptViewportExt, ptWindowExt;
287 POINT ptViewportOrg, ptWindowOrg;
288 XFORM xform;
289 HRGN hrgn;
290
291 if( pPath->state != PATH_Closed )
292 {
293 SetLastWin32Error(ERROR_CAN_NOT_COMPLETE);
294 return FALSE;
295 }
296
297 if( PATH_PathToRegion( pPath, dc->w.polyFillMode, &hrgn ))
298 {
299 /* Since PaintRgn interprets the region as being in logical coordinates
300 * but the points we store for the path are already in device
301 * coordinates, we have to set the mapping mode to MM_TEXT temporarily.
302 * Using SaveDC to save information about the mapping mode / world
303 * transform would be easier but would require more overhead, especially
304 * now that SaveDC saves the current path.
305 */
306
307 /* Save the information about the old mapping mode */
308 mapMode = NtGdiGetMapMode( dc->hSelf );
309 NtGdiGetViewportExtEx( dc->hSelf, &ptViewportExt );
310 NtGdiGetViewportOrgEx( dc->hSelf, &ptViewportOrg );
311 NtGdiGetWindowExtEx( dc->hSelf, &ptWindowExt );
312 NtGdiGetWindowOrgEx( dc->hSelf, &ptWindowOrg );
313
314 /* Save world transform
315 * NB: The Windows documentation on world transforms would lead one to
316 * believe that this has to be done only in GM_ADVANCED; however, my
317 * tests show that resetting the graphics mode to GM_COMPATIBLE does
318 * not reset the world transform.
319 */
320 NtGdiGetWorldTransform( dc->hSelf, &xform );
321
322 /* Set MM_TEXT */
323 NtGdiSetMapMode( dc->hSelf, MM_TEXT );
324 NtGdiSetViewportOrgEx( dc->hSelf, 0, 0, NULL );
325 NtGdiSetWindowOrgEx( dc->hSelf, 0, 0, NULL );
326 graphicsMode = NtGdiGetGraphicsMode( dc->hSelf );
327 NtGdiSetGraphicsMode( dc->hSelf, GM_ADVANCED );
328 NtGdiModifyWorldTransform( dc->hSelf, &xform, MWT_IDENTITY );
329 NtGdiSetGraphicsMode( dc->hSelf, graphicsMode );
330
331 /* Paint the region */
332 NtGdiPaintRgn( dc->hSelf, hrgn );
333 NtGdiDeleteObject( hrgn );
334 /* Restore the old mapping mode */
335 NtGdiSetMapMode( dc->hSelf, mapMode );
336 NtGdiSetViewportExtEx( dc->hSelf, ptViewportExt.cx, ptViewportExt.cy, NULL );
337 NtGdiSetViewportOrgEx( dc->hSelf, ptViewportOrg.x, ptViewportOrg.y, NULL );
338 NtGdiSetWindowExtEx( dc->hSelf, ptWindowExt.cx, ptWindowExt.cy, NULL );
339 NtGdiSetWindowOrgEx( dc->hSelf, ptWindowOrg.x, ptWindowOrg.y, NULL );
340
341 /* Go to GM_ADVANCED temporarily to restore the world transform */
342 graphicsMode = NtGdiGetGraphicsMode( dc->hSelf );
343 NtGdiSetGraphicsMode( dc->hSelf, GM_ADVANCED );
344 NtGdiSetWorldTransform( dc->hSelf, &xform );
345 NtGdiSetGraphicsMode( dc->hSelf, graphicsMode );
346 return TRUE;
347 }
348 return FALSE;
349 }
350
351 /* PATH_InitGdiPath
352 *
353 * Initializes the GdiPath structure.
354 */
355 VOID
356 FASTCALL
357 PATH_InitGdiPath ( GdiPath *pPath )
358 {
359 assert(pPath!=NULL);
360
361 pPath->state=PATH_Null;
362 pPath->pPoints=NULL;
363 pPath->pFlags=NULL;
364 pPath->numEntriesUsed=0;
365 pPath->numEntriesAllocated=0;
366 }
367
368 /* PATH_DestroyGdiPath
369 *
370 * Destroys a GdiPath structure (frees the memory in the arrays).
371 */
372 VOID
373 FASTCALL
374 PATH_DestroyGdiPath ( GdiPath *pPath )
375 {
376 assert(pPath!=NULL);
377
378 ExFreePool(pPath->pPoints);
379 ExFreePool(pPath->pFlags);
380 }
381
382 /* PATH_AssignGdiPath
383 *
384 * Copies the GdiPath structure "pPathSrc" to "pPathDest". A deep copy is
385 * performed, i.e. the contents of the pPoints and pFlags arrays are copied,
386 * not just the pointers. Since this means that the arrays in pPathDest may
387 * need to be resized, pPathDest should have been initialized using
388 * PATH_InitGdiPath (in C++, this function would be an assignment operator,
389 * not a copy constructor).
390 * Returns TRUE if successful, else FALSE.
391 */
392 BOOL
393 FASTCALL
394 PATH_AssignGdiPath ( GdiPath *pPathDest, const GdiPath *pPathSrc )
395 {
396 assert(pPathDest!=NULL && pPathSrc!=NULL);
397
398 /* Make sure destination arrays are big enough */
399 if ( !PATH_ReserveEntries(pPathDest, pPathSrc->numEntriesUsed) )
400 return FALSE;
401
402 /* Perform the copy operation */
403 memcpy(pPathDest->pPoints, pPathSrc->pPoints,
404 sizeof(POINT)*pPathSrc->numEntriesUsed);
405 memcpy(pPathDest->pFlags, pPathSrc->pFlags,
406 sizeof(BYTE)*pPathSrc->numEntriesUsed);
407
408 pPathDest->state=pPathSrc->state;
409 pPathDest->numEntriesUsed=pPathSrc->numEntriesUsed;
410 pPathDest->newStroke=pPathSrc->newStroke;
411
412 return TRUE;
413 }
414
415 /* PATH_MoveTo
416 *
417 * Should be called when a MoveTo is performed on a DC that has an
418 * open path. This starts a new stroke. Returns TRUE if successful, else
419 * FALSE.
420 */
421 BOOL
422 FASTCALL
423 PATH_MoveTo ( PDC dc )
424 {
425 GdiPath *pPath;
426
427 /* Get pointer to path */
428 PATH_GetPathFromDC ( dc, &pPath );
429
430 /* Check that path is open */
431 if ( pPath->state != PATH_Open )
432 /* FIXME: Do we have to call SetLastError? */
433 return FALSE;
434
435 /* Start a new stroke */
436 pPath->newStroke = TRUE;
437
438 return TRUE;
439 }
440
441 /* PATH_LineTo
442 *
443 * Should be called when a LineTo is performed on a DC that has an
444 * open path. This adds a PT_LINETO entry to the path (and possibly
445 * a PT_MOVETO entry, if this is the first LineTo in a stroke).
446 * Returns TRUE if successful, else FALSE.
447 */
448 BOOL
449 FASTCALL
450 PATH_LineTo ( PDC dc, INT x, INT y )
451 {
452 GdiPath *pPath;
453 POINT point, pointCurPos;
454
455 /* Get pointer to path */
456 PATH_GetPathFromDC ( dc, &pPath );
457
458 /* Check that path is open */
459 if ( pPath->state != PATH_Open )
460 return FALSE;
461
462 /* Convert point to device coordinates */
463 point.x=x;
464 point.y=y;
465 CoordLPtoDP ( dc, &point );
466
467 /* Add a PT_MOVETO if necessary */
468 if ( pPath->newStroke )
469 {
470 pPath->newStroke = FALSE;
471 IntGetCurrentPositionEx ( dc, &pointCurPos );
472 CoordLPtoDP ( dc, &pointCurPos );
473 if ( !PATH_AddEntry(pPath, &pointCurPos, PT_MOVETO) )
474 return FALSE;
475 }
476
477 /* Add a PT_LINETO entry */
478 return PATH_AddEntry(pPath, &point, PT_LINETO);
479 }
480
481 /* PATH_Rectangle
482 *
483 * Should be called when a call to Rectangle is performed on a DC that has
484 * an open path. Returns TRUE if successful, else FALSE.
485 */
486 BOOL
487 FASTCALL
488 PATH_Rectangle ( PDC dc, INT x1, INT y1, INT x2, INT y2 )
489 {
490 GdiPath *pPath;
491 POINT corners[2], pointTemp;
492 INT temp;
493
494 /* Get pointer to path */
495 PATH_GetPathFromDC ( dc, &pPath );
496
497 /* Check that path is open */
498 if ( pPath->state != PATH_Open )
499 return FALSE;
500
501 /* Convert points to device coordinates */
502 corners[0].x=x1;
503 corners[0].y=y1;
504 corners[1].x=x2;
505 corners[1].y=y2;
506 IntLPtoDP ( dc, corners, 2 );
507
508 /* Make sure first corner is top left and second corner is bottom right */
509 if ( corners[0].x > corners[1].x )
510 {
511 temp=corners[0].x;
512 corners[0].x=corners[1].x;
513 corners[1].x=temp;
514 }
515 if ( corners[0].y > corners[1].y )
516 {
517 temp=corners[0].y;
518 corners[0].y=corners[1].y;
519 corners[1].y=temp;
520 }
521
522 /* In GM_COMPATIBLE, don't include bottom and right edges */
523 if ( IntGetGraphicsMode(dc) == GM_COMPATIBLE )
524 {
525 corners[1].x--;
526 corners[1].y--;
527 }
528
529 /* Close any previous figure */
530 if ( !IntCloseFigure ( dc ) )
531 {
532 /* The NtGdiCloseFigure call shouldn't have failed */
533 assert(FALSE);
534 return FALSE;
535 }
536
537 /* Add four points to the path */
538 pointTemp.x=corners[1].x;
539 pointTemp.y=corners[0].y;
540 if ( !PATH_AddEntry(pPath, &pointTemp, PT_MOVETO) )
541 return FALSE;
542 if ( !PATH_AddEntry(pPath, corners, PT_LINETO) )
543 return FALSE;
544 pointTemp.x=corners[0].x;
545 pointTemp.y=corners[1].y;
546 if ( !PATH_AddEntry(pPath, &pointTemp, PT_LINETO) )
547 return FALSE;
548 if ( !PATH_AddEntry(pPath, corners+1, PT_LINETO) )
549 return FALSE;
550
551 /* Close the rectangle figure */
552 if ( !IntCloseFigure ( dc ) )
553 {
554 /* The IntCloseFigure call shouldn't have failed */
555 assert(FALSE);
556 return FALSE;
557 }
558
559 return TRUE;
560 }
561
562 BOOL
563 FASTCALL
564 PATH_RoundRect (PDC dc, INT x1, INT y1, INT x2, INT y2, INT xradius, INT yradius)
565 {
566 UNIMPLEMENTED;
567 return FALSE;
568 }
569
570 /* PATH_Ellipse
571 *
572 * Should be called when a call to Ellipse is performed on a DC that has
573 * an open path. This adds four Bezier splines representing the ellipse
574 * to the path. Returns TRUE if successful, else FALSE.
575 */
576 BOOL
577 FASTCALL
578 PATH_Ellipse ( PDC dc, INT x1, INT y1, INT x2, INT y2 )
579 {
580 /* TODO: This should probably be revised to call PATH_AngleArc */
581 /* (once it exists) */
582 return PATH_Arc ( dc, x1, y1, x2, y2, x1, (y1+y2)/2, x1, (y1+y2)/2 );
583 }
584
585 /* PATH_Arc
586 *
587 * Should be called when a call to Arc is performed on a DC that has
588 * an open path. This adds up to five Bezier splines representing the arc
589 * to the path. Returns TRUE if successful, else FALSE.
590 */
591 BOOL
592 FASTCALL
593 PATH_Arc ( PDC dc, INT x1, INT y1, INT x2, INT y2,
594 INT xStart, INT yStart, INT xEnd, INT yEnd)
595 {
596 GdiPath *pPath;
597 double angleStart, angleEnd, angleStartQuadrant, angleEndQuadrant=0.0;
598 /* Initialize angleEndQuadrant to silence gcc's warning */
599 double x, y;
600 FLOAT_POINT corners[2], pointStart, pointEnd;
601 BOOL start, end;
602 INT temp;
603 BOOL clockwise;
604
605 /* FIXME: This function should check for all possible error returns */
606 /* FIXME: Do we have to respect newStroke? */
607
608 ASSERT ( dc );
609
610 clockwise = ( IntGdiGetArcDirection(dc) == AD_CLOCKWISE );
611
612 /* Get pointer to path */
613 PATH_GetPathFromDC ( dc, &pPath );
614
615 /* Check that path is open */
616 if ( pPath->state != PATH_Open )
617 return FALSE;
618
619 /* FIXME: Do we have to close the current figure? */
620
621 /* Check for zero height / width */
622 /* FIXME: Only in GM_COMPATIBLE? */
623 if ( x1==x2 || y1==y2 )
624 return TRUE;
625
626 /* Convert points to device coordinates */
627 corners[0].x=(FLOAT)x1;
628 corners[0].y=(FLOAT)y1;
629 corners[1].x=(FLOAT)x2;
630 corners[1].y=(FLOAT)y2;
631 pointStart.x=(FLOAT)xStart;
632 pointStart.y=(FLOAT)yStart;
633 pointEnd.x=(FLOAT)xEnd;
634 pointEnd.y=(FLOAT)yEnd;
635 INTERNAL_LPTODP_FLOAT(dc, corners);
636 INTERNAL_LPTODP_FLOAT(dc, corners+1);
637 INTERNAL_LPTODP_FLOAT(dc, &pointStart);
638 INTERNAL_LPTODP_FLOAT(dc, &pointEnd);
639
640 /* Make sure first corner is top left and second corner is bottom right */
641 if ( corners[0].x > corners[1].x )
642 {
643 temp=corners[0].x;
644 corners[0].x=corners[1].x;
645 corners[1].x=temp;
646 }
647 if ( corners[0].y > corners[1].y )
648 {
649 temp=corners[0].y;
650 corners[0].y=corners[1].y;
651 corners[1].y=temp;
652 }
653
654 /* Compute start and end angle */
655 PATH_NormalizePoint(corners, &pointStart, &x, &y);
656 angleStart=atan2(y, x);
657 PATH_NormalizePoint(corners, &pointEnd, &x, &y);
658 angleEnd=atan2(y, x);
659
660 /* Make sure the end angle is "on the right side" of the start angle */
661 if ( clockwise )
662 {
663 if ( angleEnd <= angleStart )
664 {
665 angleEnd+=2*M_PI;
666 assert(angleEnd>=angleStart);
667 }
668 }
669 else
670 {
671 if(angleEnd>=angleStart)
672 {
673 angleEnd-=2*M_PI;
674 assert(angleEnd<=angleStart);
675 }
676 }
677
678 /* In GM_COMPATIBLE, don't include bottom and right edges */
679 if ( IntGetGraphicsMode(dc) == GM_COMPATIBLE )
680 {
681 corners[1].x--;
682 corners[1].y--;
683 }
684
685 /* Add the arc to the path with one Bezier spline per quadrant that the
686 * arc spans */
687 start=TRUE;
688 end=FALSE;
689 do
690 {
691 /* Determine the start and end angles for this quadrant */
692 if(start)
693 {
694 angleStartQuadrant=angleStart;
695 if ( clockwise )
696 angleEndQuadrant=(floor(angleStart/M_PI_2)+1.0)*M_PI_2;
697 else
698 angleEndQuadrant=(ceil(angleStart/M_PI_2)-1.0)*M_PI_2;
699 }
700 else
701 {
702 angleStartQuadrant=angleEndQuadrant;
703 if ( clockwise )
704 angleEndQuadrant+=M_PI_2;
705 else
706 angleEndQuadrant-=M_PI_2;
707 }
708
709 /* Have we reached the last part of the arc? */
710 if ( (clockwise && angleEnd<angleEndQuadrant)
711 || (!clockwise && angleEnd>angleEndQuadrant)
712 )
713 {
714 /* Adjust the end angle for this quadrant */
715 angleEndQuadrant = angleEnd;
716 end = TRUE;
717 }
718
719 /* Add the Bezier spline to the path */
720 PATH_DoArcPart ( pPath, corners, angleStartQuadrant, angleEndQuadrant, start );
721 start = FALSE;
722 } while(!end);
723
724 return TRUE;
725 }
726
727 BOOL
728 FASTCALL
729 PATH_PolyBezierTo ( PDC dc, const POINT *pts, DWORD cbPoints )
730 {
731 GdiPath *pPath;
732 POINT pt;
733 ULONG i;
734
735 ASSERT ( dc );
736 ASSERT ( pts );
737 ASSERT ( cbPoints );
738
739 PATH_GetPathFromDC ( dc, &pPath );
740
741 /* Check that path is open */
742 if ( pPath->state != PATH_Open )
743 return FALSE;
744
745 /* Add a PT_MOVETO if necessary */
746 if ( pPath->newStroke )
747 {
748 pPath->newStroke=FALSE;
749 IntGetCurrentPositionEx ( dc, &pt );
750 CoordLPtoDP ( dc, &pt );
751 if ( !PATH_AddEntry(pPath, &pt, PT_MOVETO) )
752 return FALSE;
753 }
754
755 for(i = 0; i < cbPoints; i++)
756 {
757 pt = pts[i];
758 CoordLPtoDP ( dc, &pt );
759 PATH_AddEntry(pPath, &pt, PT_BEZIERTO);
760 }
761 return TRUE;
762 }
763
764 BOOL
765 FASTCALL
766 PATH_PolyBezier ( PDC dc, const POINT *pts, DWORD cbPoints )
767 {
768 GdiPath *pPath;
769 POINT pt;
770 ULONG i;
771
772 ASSERT ( dc );
773 ASSERT ( pts );
774 ASSERT ( cbPoints );
775
776 PATH_GetPathFromDC ( dc, &pPath );
777
778 /* Check that path is open */
779 if ( pPath->state != PATH_Open )
780 return FALSE;
781
782 for ( i = 0; i < cbPoints; i++ )
783 {
784 pt = pts[i];
785 CoordLPtoDP ( dc, &pt );
786 PATH_AddEntry ( pPath, &pt, (i == 0) ? PT_MOVETO : PT_BEZIERTO );
787 }
788
789 return TRUE;
790 }
791
792 BOOL
793 FASTCALL
794 PATH_Polyline ( PDC dc, const POINT *pts, DWORD cbPoints )
795 {
796 GdiPath *pPath;
797 POINT pt;
798 ULONG i;
799
800 ASSERT ( dc );
801 ASSERT ( pts );
802 ASSERT ( cbPoints );
803
804 PATH_GetPathFromDC ( dc, &pPath );
805
806 /* Check that path is open */
807 if ( pPath->state != PATH_Open )
808 return FALSE;
809
810 for ( i = 0; i < cbPoints; i++ )
811 {
812 pt = pts[i];
813 CoordLPtoDP ( dc, &pt );
814 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO : PT_LINETO);
815 }
816 return TRUE;
817 }
818
819 BOOL
820 FASTCALL
821 PATH_PolylineTo ( PDC dc, const POINT *pts, DWORD cbPoints )
822 {
823 GdiPath *pPath;
824 POINT pt;
825 ULONG i;
826
827 ASSERT ( dc );
828 ASSERT ( pts );
829 ASSERT ( cbPoints );
830
831 PATH_GetPathFromDC ( dc, &pPath );
832
833 /* Check that path is open */
834 if ( pPath->state != PATH_Open )
835 return FALSE;
836
837 /* Add a PT_MOVETO if necessary */
838 if ( pPath->newStroke )
839 {
840 pPath->newStroke = FALSE;
841 IntGetCurrentPositionEx ( dc, &pt );
842 CoordLPtoDP ( dc, &pt );
843 if ( !PATH_AddEntry(pPath, &pt, PT_MOVETO) )
844 return FALSE;
845 }
846
847 for(i = 0; i < cbPoints; i++)
848 {
849 pt = pts[i];
850 CoordLPtoDP ( dc, &pt );
851 PATH_AddEntry(pPath, &pt, PT_LINETO);
852 }
853
854 return TRUE;
855 }
856
857
858 BOOL
859 FASTCALL
860 PATH_Polygon ( PDC dc, const POINT *pts, DWORD cbPoints )
861 {
862 GdiPath *pPath;
863 POINT pt;
864 ULONG i;
865
866 ASSERT ( dc );
867 ASSERT ( pts );
868
869 PATH_GetPathFromDC ( dc, &pPath );
870
871 /* Check that path is open */
872 if ( pPath->state != PATH_Open )
873 return FALSE;
874
875 for(i = 0; i < cbPoints; i++)
876 {
877 pt = pts[i];
878 CoordLPtoDP ( dc, &pt );
879 PATH_AddEntry(pPath, &pt, (i == 0) ? PT_MOVETO :
880 ((i == cbPoints-1) ? PT_LINETO | PT_CLOSEFIGURE :
881 PT_LINETO));
882 }
883 return TRUE;
884 }
885
886 BOOL
887 FASTCALL
888 PATH_PolyPolygon ( PDC dc, const POINT* pts, const INT* counts, UINT polygons )
889 {
890 GdiPath *pPath;
891 POINT pt, startpt;
892 ULONG poly, point, i;
893
894 ASSERT ( dc );
895 ASSERT ( pts );
896 ASSERT ( counts );
897 ASSERT ( polygons );
898
899 PATH_GetPathFromDC ( dc, &pPath );
900
901 /* Check that path is open */
902 if ( pPath->state != PATH_Open );
903 return FALSE;
904
905 for(i = 0, poly = 0; poly < polygons; poly++)
906 {
907 for(point = 0; point < (ULONG) counts[poly]; point++, i++)
908 {
909 pt = pts[i];
910 CoordLPtoDP ( dc, &pt );
911 if(point == 0) startpt = pt;
912 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
913 }
914 /* win98 adds an extra line to close the figure for some reason */
915 PATH_AddEntry(pPath, &startpt, PT_LINETO | PT_CLOSEFIGURE);
916 }
917 return TRUE;
918 }
919
920 BOOL
921 FASTCALL
922 PATH_PolyPolyline ( PDC dc, const POINT* pts, const DWORD* counts, DWORD polylines )
923 {
924 GdiPath *pPath;
925 POINT pt;
926 ULONG poly, point, i;
927
928 ASSERT ( dc );
929 ASSERT ( pts );
930 ASSERT ( counts );
931 ASSERT ( polylines );
932
933 PATH_GetPathFromDC ( dc, &pPath );
934
935 /* Check that path is open */
936 if ( pPath->state != PATH_Open )
937 return FALSE;
938
939 for(i = 0, poly = 0; poly < polylines; poly++)
940 {
941 for(point = 0; point < counts[poly]; point++, i++)
942 {
943 pt = pts[i];
944 CoordLPtoDP ( dc, &pt );
945 PATH_AddEntry(pPath, &pt, (point == 0) ? PT_MOVETO : PT_LINETO);
946 }
947 }
948 return TRUE;
949 }
950
951 /***********************************************************************
952 * Internal functions
953 */
954
955
956 /* PATH_AddFlatBezier
957 *
958 */
959 BOOL
960 FASTCALL
961 PATH_AddFlatBezier ( GdiPath *pPath, POINT *pt, BOOL closed )
962 {
963 POINT *pts;
964 INT no, i;
965
966 pts = GDI_Bezier( pt, 4, &no );
967 if ( !pts ) return FALSE;
968
969 for(i = 1; i < no; i++)
970 PATH_AddEntry(pPath, &pts[i], (i == no-1 && closed) ? PT_LINETO | PT_CLOSEFIGURE : PT_LINETO);
971
972 ExFreePool(pts);
973 return TRUE;
974 }
975
976 /* PATH_FlattenPath
977 *
978 * Replaces Beziers with line segments
979 *
980 */
981 BOOL
982 FASTCALL
983 PATH_FlattenPath(GdiPath *pPath)
984 {
985 GdiPath newPath;
986 INT srcpt;
987
988 memset(&newPath, 0, sizeof(newPath));
989 newPath.state = PATH_Open;
990 for(srcpt = 0; srcpt < pPath->numEntriesUsed; srcpt++) {
991 switch(pPath->pFlags[srcpt] & ~PT_CLOSEFIGURE) {
992 case PT_MOVETO:
993 case PT_LINETO:
994 PATH_AddEntry(&newPath, &pPath->pPoints[srcpt], pPath->pFlags[srcpt]);
995 break;
996 case PT_BEZIERTO:
997 PATH_AddFlatBezier(&newPath, &pPath->pPoints[srcpt-1], pPath->pFlags[srcpt+2] & PT_CLOSEFIGURE);
998 srcpt += 2;
999 break;
1000 }
1001 }
1002 newPath.state = PATH_Closed;
1003 PATH_AssignGdiPath(pPath, &newPath);
1004 PATH_EmptyPath(&newPath);
1005 return TRUE;
1006 }
1007
1008 /* PATH_PathToRegion
1009 *
1010 * Creates a region from the specified path using the specified polygon
1011 * filling mode. The path is left unchanged. A handle to the region that
1012 * was created is stored in *pHrgn. If successful, TRUE is returned; if an
1013 * error occurs, SetLastError is called with the appropriate value and
1014 * FALSE is returned.
1015 */
1016
1017
1018 BOOL
1019 FASTCALL
1020 PATH_PathToRegion ( GdiPath *pPath, INT nPolyFillMode, HRGN *pHrgn )
1021 {
1022 int numStrokes, iStroke, i;
1023 INT *pNumPointsInStroke;
1024 HRGN hrgn;
1025
1026 assert ( pPath!=NULL );
1027 assert ( pHrgn!=NULL );
1028
1029 PATH_FlattenPath ( pPath );
1030
1031 /* FIXME: What happens when number of points is zero? */
1032
1033 /* First pass: Find out how many strokes there are in the path */
1034 /* FIXME: We could eliminate this with some bookkeeping in GdiPath */
1035 numStrokes=0;
1036 for(i=0; i<pPath->numEntriesUsed; i++)
1037 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1038 numStrokes++;
1039
1040 /* Allocate memory for number-of-points-in-stroke array */
1041 pNumPointsInStroke=(int *)ExAllocatePoolWithTag(PagedPool, sizeof(int) * numStrokes, TAG_PATH);
1042 if(!pNumPointsInStroke)
1043 {
1044 // SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1045 return FALSE;
1046 }
1047
1048 /* Second pass: remember number of points in each polygon */
1049 iStroke=-1; /* Will get incremented to 0 at beginning of first stroke */
1050 for(i=0; i<pPath->numEntriesUsed; i++)
1051 {
1052 /* Is this the beginning of a new stroke? */
1053 if((pPath->pFlags[i] & ~PT_CLOSEFIGURE) == PT_MOVETO)
1054 {
1055 iStroke++;
1056 pNumPointsInStroke[iStroke]=0;
1057 }
1058
1059 pNumPointsInStroke[iStroke]++;
1060 }
1061
1062 /* Create a region from the strokes */
1063 /* hrgn=CreatePolyPolygonRgn(pPath->pPoints, pNumPointsInStroke,
1064 numStrokes, nPolyFillMode); FIXME: reinclude when region code implemented */
1065 if(hrgn==(HRGN)0)
1066 {
1067 // SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1068 return FALSE;
1069 }
1070
1071 /* Free memory for number-of-points-in-stroke array */
1072 ExFreePool(pNumPointsInStroke);
1073
1074 /* Success! */
1075 *pHrgn=hrgn;
1076 return TRUE;
1077 }
1078
1079 /* PATH_EmptyPath
1080 *
1081 * Removes all entries from the path and sets the path state to PATH_Null.
1082 */
1083 VOID
1084 FASTCALL
1085 PATH_EmptyPath ( GdiPath *pPath )
1086 {
1087 assert(pPath!=NULL);
1088
1089 pPath->state=PATH_Null;
1090 pPath->numEntriesUsed=0;
1091 }
1092
1093 /* PATH_AddEntry
1094 *
1095 * Adds an entry to the path. For "flags", pass either PT_MOVETO, PT_LINETO
1096 * or PT_BEZIERTO, optionally ORed with PT_CLOSEFIGURE. Returns TRUE if
1097 * successful, FALSE otherwise (e.g. if not enough memory was available).
1098 */
1099 BOOL
1100 FASTCALL
1101 PATH_AddEntry ( GdiPath *pPath, const POINT *pPoint, BYTE flags )
1102 {
1103 assert(pPath!=NULL);
1104
1105 /* FIXME: If newStroke is true, perhaps we want to check that we're
1106 * getting a PT_MOVETO
1107 */
1108
1109 /* Check that path is open */
1110 if ( pPath->state != PATH_Open )
1111 return FALSE;
1112
1113 /* Reserve enough memory for an extra path entry */
1114 if ( !PATH_ReserveEntries(pPath, pPath->numEntriesUsed+1) )
1115 return FALSE;
1116
1117 /* Store information in path entry */
1118 pPath->pPoints[pPath->numEntriesUsed]=*pPoint;
1119 pPath->pFlags[pPath->numEntriesUsed]=flags;
1120
1121 /* If this is PT_CLOSEFIGURE, we have to start a new stroke next time */
1122 if((flags & PT_CLOSEFIGURE) == PT_CLOSEFIGURE)
1123 pPath->newStroke=TRUE;
1124
1125 /* Increment entry count */
1126 pPath->numEntriesUsed++;
1127
1128 return TRUE;
1129 }
1130
1131 /* PATH_ReserveEntries
1132 *
1133 * Ensures that at least "numEntries" entries (for points and flags) have
1134 * been allocated; allocates larger arrays and copies the existing entries
1135 * to those arrays, if necessary. Returns TRUE if successful, else FALSE.
1136 */
1137 BOOL
1138 FASTCALL
1139 PATH_ReserveEntries ( GdiPath *pPath, INT numEntries )
1140 {
1141 INT numEntriesToAllocate;
1142 POINT *pPointsNew;
1143 BYTE *pFlagsNew;
1144
1145 assert(pPath!=NULL);
1146 assert(numEntries>=0);
1147
1148 /* Do we have to allocate more memory? */
1149 if(numEntries > pPath->numEntriesAllocated)
1150 {
1151 /* Find number of entries to allocate. We let the size of the array
1152 * grow exponentially, since that will guarantee linear time
1153 * complexity. */
1154 if(pPath->numEntriesAllocated)
1155 {
1156 numEntriesToAllocate=pPath->numEntriesAllocated;
1157 while(numEntriesToAllocate<numEntries)
1158 numEntriesToAllocate=numEntriesToAllocate*GROW_FACTOR_NUMER/GROW_FACTOR_DENOM;
1159 } else
1160 numEntriesToAllocate=numEntries;
1161
1162 /* Allocate new arrays */
1163 pPointsNew=(POINT *)ExAllocatePoolWithTag(PagedPool, numEntriesToAllocate * sizeof(POINT), TAG_PATH);
1164 if(!pPointsNew)
1165 return FALSE;
1166 pFlagsNew=(BYTE *)ExAllocatePoolWithTag(PagedPool, numEntriesToAllocate * sizeof(BYTE), TAG_PATH);
1167 if(!pFlagsNew)
1168 {
1169 ExFreePool(pPointsNew);
1170 return FALSE;
1171 }
1172
1173 /* Copy old arrays to new arrays and discard old arrays */
1174 if(pPath->pPoints)
1175 {
1176 assert(pPath->pFlags);
1177
1178 memcpy(pPointsNew, pPath->pPoints, sizeof(POINT)*pPath->numEntriesUsed);
1179 memcpy(pFlagsNew, pPath->pFlags, sizeof(BYTE)*pPath->numEntriesUsed);
1180
1181 ExFreePool(pPath->pPoints);
1182 ExFreePool(pPath->pFlags);
1183 }
1184 pPath->pPoints=pPointsNew;
1185 pPath->pFlags=pFlagsNew;
1186 pPath->numEntriesAllocated=numEntriesToAllocate;
1187 }
1188
1189 return TRUE;
1190 }
1191
1192 /* PATH_GetPathFromDC
1193 *
1194 * Retrieves a pointer to the GdiPath structure contained in an HDC and
1195 * places it in *ppPath. TRUE is returned if successful, FALSE otherwise.
1196 */
1197 VOID
1198 FASTCALL
1199 PATH_GetPathFromDC ( PDC dc, GdiPath **ppPath )
1200 {
1201 ASSERT ( dc );
1202 ASSERT ( ppPath );
1203 *ppPath = &dc->w.path;
1204 }
1205
1206 /* PATH_DoArcPart
1207 *
1208 * Creates a Bezier spline that corresponds to part of an arc and appends the
1209 * corresponding points to the path. The start and end angles are passed in
1210 * "angleStart" and "angleEnd"; these angles should span a quarter circle
1211 * at most. If "addMoveTo" is true, a PT_MOVETO entry for the first control
1212 * point is added to the path; otherwise, it is assumed that the current
1213 * position is equal to the first control point.
1214 */
1215 BOOL
1216 FASTCALL
1217 PATH_DoArcPart ( GdiPath *pPath, FLOAT_POINT corners[],
1218 double angleStart, double angleEnd, BOOL addMoveTo )
1219 {
1220 double halfAngle, a;
1221 double xNorm[4], yNorm[4];
1222 POINT point;
1223 int i;
1224
1225 assert(fabs(angleEnd-angleStart)<=M_PI_2);
1226
1227 /* FIXME: Is there an easier way of computing this? */
1228
1229 /* Compute control points */
1230 halfAngle=(angleEnd-angleStart)/2.0;
1231 if(fabs(halfAngle)>1e-8)
1232 {
1233 a=4.0/3.0*(1-cos(halfAngle))/sin(halfAngle);
1234 xNorm[0]=cos(angleStart);
1235 yNorm[0]=sin(angleStart);
1236 xNorm[1]=xNorm[0] - a*yNorm[0];
1237 yNorm[1]=yNorm[0] + a*xNorm[0];
1238 xNorm[3]=cos(angleEnd);
1239 yNorm[3]=sin(angleEnd);
1240 xNorm[2]=xNorm[3] + a*yNorm[3];
1241 yNorm[2]=yNorm[3] - a*xNorm[3];
1242 } else
1243 for(i=0; i<4; i++)
1244 {
1245 xNorm[i]=cos(angleStart);
1246 yNorm[i]=sin(angleStart);
1247 }
1248
1249 /* Add starting point to path if desired */
1250 if(addMoveTo)
1251 {
1252 PATH_ScaleNormalizedPoint(corners, xNorm[0], yNorm[0], &point);
1253 if(!PATH_AddEntry(pPath, &point, PT_MOVETO))
1254 return FALSE;
1255 }
1256
1257 /* Add remaining control points */
1258 for(i=1; i<4; i++)
1259 {
1260 PATH_ScaleNormalizedPoint(corners, xNorm[i], yNorm[i], &point);
1261 if(!PATH_AddEntry(pPath, &point, PT_BEZIERTO))
1262 return FALSE;
1263 }
1264
1265 return TRUE;
1266 }
1267
1268 /* PATH_ScaleNormalizedPoint
1269 *
1270 * Scales a normalized point (x, y) with respect to the box whose corners are
1271 * passed in "corners". The point is stored in "*pPoint". The normalized
1272 * coordinates (-1.0, -1.0) correspond to corners[0], the coordinates
1273 * (1.0, 1.0) correspond to corners[1].
1274 */
1275 VOID
1276 FASTCALL
1277 PATH_ScaleNormalizedPoint ( FLOAT_POINT corners[], double x,
1278 double y, POINT *pPoint )
1279 {
1280 ASSERT ( corners );
1281 ASSERT ( pPoint );
1282 pPoint->x=GDI_ROUND( (double)corners[0].x + (double)(corners[1].x-corners[0].x)*0.5*(x+1.0) );
1283 pPoint->y=GDI_ROUND( (double)corners[0].y + (double)(corners[1].y-corners[0].y)*0.5*(y+1.0) );
1284 }
1285
1286 /* PATH_NormalizePoint
1287 *
1288 * Normalizes a point with respect to the box whose corners are passed in
1289 * corners. The normalized coordinates are stored in *pX and *pY.
1290 */
1291 VOID
1292 FASTCALL
1293 PATH_NormalizePoint ( FLOAT_POINT corners[],
1294 const FLOAT_POINT *pPoint,
1295 double *pX, double *pY)
1296 {
1297 ASSERT ( corners );
1298 ASSERT ( pPoint );
1299 ASSERT ( pX );
1300 ASSERT ( pY );
1301 *pX=(double)(pPoint->x-corners[0].x)/(double)(corners[1].x-corners[0].x) * 2.0 - 1.0;
1302 *pY=(double)(pPoint->y-corners[0].y)/(double)(corners[1].y-corners[0].y) * 2.0 - 1.0;
1303 }
1304 /* EOF */