2 * GDI drawing functions.
4 * Copyright 1993, 1994 Alexandre Julliard
5 * Copyright 1997 Bertho A. Stultiens
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "ntgdi_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(gdi
);
37 /***********************************************************************
38 * null driver fallback implementations
41 BOOL CDECL
nulldrv_AngleArc( PHYSDEV dev
, INT x
, INT y
, DWORD radius
, FLOAT start
, FLOAT sweep
)
43 INT x1
= GDI_ROUND( x
+ cos( start
* M_PI
/ 180 ) * radius
);
44 INT y1
= GDI_ROUND( y
- sin( start
* M_PI
/ 180 ) * radius
);
45 INT x2
= GDI_ROUND( x
+ cos( (start
+ sweep
) * M_PI
/ 180) * radius
);
46 INT y2
= GDI_ROUND( y
- sin( (start
+ sweep
) * M_PI
/ 180) * radius
);
47 INT arcdir
= SetArcDirection( dev
->hdc
, sweep
>= 0 ? AD_COUNTERCLOCKWISE
: AD_CLOCKWISE
);
48 BOOL ret
= ArcTo( dev
->hdc
, x
- radius
, y
- radius
, x
+ radius
, y
+ radius
, x1
, y1
, x2
, y2
);
49 SetArcDirection( dev
->hdc
, arcdir
);
53 BOOL CDECL
nulldrv_ArcTo( PHYSDEV dev
, INT left
, INT top
, INT right
, INT bottom
,
54 INT xstart
, INT ystart
, INT xend
, INT yend
)
56 INT width
= abs( right
- left
);
57 INT height
= abs( bottom
- top
);
58 double xradius
= width
/ 2.0;
59 double yradius
= height
/ 2.0;
60 double xcenter
= right
> left
? left
+ xradius
: right
+ xradius
;
61 double ycenter
= bottom
> top
? top
+ yradius
: bottom
+ yradius
;
64 if (!height
|| !width
) return FALSE
;
65 /* draw a line from the current position to the starting point of the arc, then draw the arc */
66 angle
= atan2( (ystart
- ycenter
) / height
, (xstart
- xcenter
) / width
);
67 LineTo( dev
->hdc
, GDI_ROUND( xcenter
+ cos(angle
) * xradius
),
68 GDI_ROUND( ycenter
+ sin(angle
) * yradius
));
69 return Arc( dev
->hdc
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
);
72 BOOL CDECL
nulldrv_FillRgn( PHYSDEV dev
, HRGN rgn
, HBRUSH brush
)
77 if ((prev
= NtGdiSelectBrush( dev
->hdc
, brush
)))
79 ret
= PaintRgn( dev
->hdc
, rgn
);
80 NtGdiSelectBrush( dev
->hdc
, prev
);
85 BOOL CDECL
nulldrv_FrameRgn( PHYSDEV dev
, HRGN rgn
, HBRUSH brush
, INT width
, INT height
)
88 HRGN tmp
= NtGdiCreateRectRgn( 0, 0, 0, 0 );
92 if (REGION_FrameRgn( tmp
, rgn
, width
, height
)) ret
= FillRgn( dev
->hdc
, tmp
, brush
);
98 BOOL CDECL
nulldrv_InvertRgn( PHYSDEV dev
, HRGN rgn
)
100 HBRUSH prev_brush
= NtGdiSelectBrush( dev
->hdc
, GetStockObject(BLACK_BRUSH
) );
101 INT prev_rop
= SetROP2( dev
->hdc
, R2_NOT
);
102 BOOL ret
= PaintRgn( dev
->hdc
, rgn
);
103 NtGdiSelectBrush( dev
->hdc
, prev_brush
);
104 SetROP2( dev
->hdc
, prev_rop
);
108 static BOOL
polyline( HDC hdc
, const POINT
*points
, UINT count
)
110 return NtGdiPolyPolyDraw( hdc
, points
, &count
, 1, NtGdiPolyPolyline
);
113 BOOL CDECL
nulldrv_PolyBezier( PHYSDEV dev
, const POINT
*points
, DWORD count
)
119 if ((pts
= GDI_Bezier( points
, count
, &n
)))
121 ret
= polyline( dev
->hdc
, pts
, n
);
122 HeapFree( GetProcessHeap(), 0, pts
);
127 BOOL CDECL
nulldrv_PolyBezierTo( PHYSDEV dev
, const POINT
*points
, DWORD count
)
129 DC
*dc
= get_nulldrv_dc( dev
);
131 POINT
*pts
= HeapAlloc( GetProcessHeap(), 0, sizeof(POINT
) * (count
+ 1) );
135 pts
[0] = dc
->attr
->cur_pos
;
136 memcpy( pts
+ 1, points
, sizeof(POINT
) * count
);
138 ret
= NtGdiPolyPolyDraw( dev
->hdc
, pts
, &count
, 1, NtGdiPolyBezier
);
139 HeapFree( GetProcessHeap(), 0, pts
);
144 BOOL CDECL
nulldrv_PolyDraw( PHYSDEV dev
, const POINT
*points
, const BYTE
*types
, DWORD count
)
146 DC
*dc
= get_nulldrv_dc( dev
);
147 POINT
*line_pts
= NULL
, *bzr_pts
= NULL
, bzr
[4];
149 INT num_pts
, num_bzr_pts
, space
, size
;
151 /* check for valid point types */
152 for (i
= 0; i
< count
; i
++)
157 case PT_LINETO
| PT_CLOSEFIGURE
:
161 if (i
+ 2 >= count
) return FALSE
;
162 if (types
[i
+ 1] != PT_BEZIERTO
) return FALSE
;
163 if ((types
[i
+ 2] & ~PT_CLOSEFIGURE
) != PT_BEZIERTO
) return FALSE
;
172 line_pts
= HeapAlloc( GetProcessHeap(), 0, space
* sizeof(POINT
) );
175 line_pts
[0] = dc
->attr
->cur_pos
;
176 for (i
= 0; i
< count
; i
++)
181 if (num_pts
>= 2) polyline( dev
->hdc
, line_pts
, num_pts
);
183 line_pts
[num_pts
++] = points
[i
];
186 case (PT_LINETO
| PT_CLOSEFIGURE
):
187 line_pts
[num_pts
++] = points
[i
];
190 bzr
[0].x
= line_pts
[num_pts
- 1].x
;
191 bzr
[0].y
= line_pts
[num_pts
- 1].y
;
192 memcpy( &bzr
[1], &points
[i
], 3 * sizeof(POINT
) );
194 if ((bzr_pts
= GDI_Bezier( bzr
, 4, &num_bzr_pts
)))
196 size
= num_pts
+ (count
- i
) + num_bzr_pts
;
200 line_pts
= HeapReAlloc( GetProcessHeap(), 0, line_pts
, space
* sizeof(POINT
) );
202 memcpy( &line_pts
[num_pts
], &bzr_pts
[1], (num_bzr_pts
- 1) * sizeof(POINT
) );
203 num_pts
+= num_bzr_pts
- 1;
204 HeapFree( GetProcessHeap(), 0, bzr_pts
);
209 if (types
[i
] & PT_CLOSEFIGURE
) line_pts
[num_pts
++] = line_pts
[0];
212 if (num_pts
>= 2) polyline( dev
->hdc
, line_pts
, num_pts
);
213 HeapFree( GetProcessHeap(), 0, line_pts
);
217 BOOL CDECL
nulldrv_PolylineTo( PHYSDEV dev
, const POINT
*points
, INT count
)
219 DC
*dc
= get_nulldrv_dc( dev
);
223 if (!count
) return FALSE
;
224 if ((pts
= HeapAlloc( GetProcessHeap(), 0, sizeof(POINT
) * (count
+ 1) )))
226 pts
[0] = dc
->attr
->cur_pos
;
227 memcpy( pts
+ 1, points
, sizeof(POINT
) * count
);
228 ret
= polyline( dev
->hdc
, pts
, count
+ 1 );
229 HeapFree( GetProcessHeap(), 0, pts
);
234 /***********************************************************************
235 * NtGdiLineTo (win32u.@)
237 BOOL WINAPI
NtGdiLineTo( HDC hdc
, INT x
, INT y
)
239 DC
* dc
= get_dc_ptr( hdc
);
243 if(!dc
) return FALSE
;
246 physdev
= GET_DC_PHYSDEV( dc
, pLineTo
);
247 ret
= physdev
->funcs
->pLineTo( physdev
, x
, y
);
251 dc
->attr
->cur_pos
.x
= x
;
252 dc
->attr
->cur_pos
.y
= y
;
254 release_dc_ptr( dc
);
259 /***********************************************************************
260 * NtGdiMoveTo (win32u.@)
262 BOOL WINAPI
NtGdiMoveTo( HDC hdc
, INT x
, INT y
, POINT
*pt
)
266 DC
* dc
= get_dc_ptr( hdc
);
268 if(!dc
) return FALSE
;
271 *pt
= dc
->attr
->cur_pos
;
273 dc
->attr
->cur_pos
.x
= x
;
274 dc
->attr
->cur_pos
.y
= y
;
276 physdev
= GET_DC_PHYSDEV( dc
, pMoveTo
);
277 ret
= physdev
->funcs
->pMoveTo( physdev
, x
, y
);
278 release_dc_ptr( dc
);
283 /***********************************************************************
284 * NtGdiArcInternal (win32u.@)
286 BOOL WINAPI
NtGdiArcInternal( UINT type
, HDC hdc
, INT left
, INT top
, INT right
,
287 INT bottom
, INT xstart
, INT ystart
, INT xend
, INT yend
)
293 if (!(dc
= get_dc_ptr( hdc
))) return FALSE
;
299 physdev
= GET_DC_PHYSDEV( dc
, pArc
);
300 ret
= physdev
->funcs
->pArc( physdev
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
);
305 double width
= abs( right
- left
);
306 double height
= abs( bottom
- top
);
307 double xradius
= width
/ 2;
308 double yradius
= height
/ 2;
309 double xcenter
= right
> left
? left
+ xradius
: right
+ xradius
;
310 double ycenter
= bottom
> top
? top
+ yradius
: bottom
+ yradius
;
312 physdev
= GET_DC_PHYSDEV( dc
, pArcTo
);
313 ret
= physdev
->funcs
->pArcTo( physdev
, left
, top
, right
, bottom
,
314 xstart
, ystart
, xend
, yend
);
317 double angle
= atan2(((yend
- ycenter
) / height
),
318 ((xend
- xcenter
) / width
));
319 dc
->attr
->cur_pos
.x
= GDI_ROUND( xcenter
+ (cos( angle
) * xradius
) );
320 dc
->attr
->cur_pos
.y
= GDI_ROUND( ycenter
+ (sin( angle
) * yradius
) );
326 physdev
= GET_DC_PHYSDEV( dc
, pChord
);
327 ret
= physdev
->funcs
->pChord( physdev
, left
, top
, right
, bottom
,
328 xstart
, ystart
, xend
, yend
);
332 physdev
= GET_DC_PHYSDEV( dc
, pPie
);
333 ret
= physdev
->funcs
->pPie( physdev
, left
, top
, right
, bottom
,
334 xstart
, ystart
, xend
, yend
);
338 WARN( "invalid arc type %u\n", type
);
342 release_dc_ptr( dc
);
347 /***********************************************************************
348 * NtGdiEllipse (win32u.@)
350 BOOL WINAPI
NtGdiEllipse( HDC hdc
, INT left
, INT top
, INT right
, INT bottom
)
354 DC
* dc
= get_dc_ptr( hdc
);
356 if (!dc
) return FALSE
;
358 physdev
= GET_DC_PHYSDEV( dc
, pEllipse
);
359 ret
= physdev
->funcs
->pEllipse( physdev
, left
, top
, right
, bottom
);
360 release_dc_ptr( dc
);
365 /***********************************************************************
366 * NtGdiRectangle (win32u.@)
368 BOOL WINAPI
NtGdiRectangle( HDC hdc
, INT left
, INT top
, INT right
, INT bottom
)
372 DC
* dc
= get_dc_ptr( hdc
);
374 if (!dc
) return FALSE
;
376 physdev
= GET_DC_PHYSDEV( dc
, pRectangle
);
377 ret
= physdev
->funcs
->pRectangle( physdev
, left
, top
, right
, bottom
);
378 release_dc_ptr( dc
);
383 /***********************************************************************
384 * NtGdiRoundRect (win32u.@)
386 BOOL WINAPI
NtGdiRoundRect( HDC hdc
, INT left
, INT top
, INT right
,
387 INT bottom
, INT ell_width
, INT ell_height
)
391 DC
*dc
= get_dc_ptr( hdc
);
393 if (!dc
) return FALSE
;
395 physdev
= GET_DC_PHYSDEV( dc
, pRoundRect
);
396 ret
= physdev
->funcs
->pRoundRect( physdev
, left
, top
, right
, bottom
, ell_width
, ell_height
);
397 release_dc_ptr( dc
);
401 /***********************************************************************
402 * NtGdiSetPixel (win32u.@)
404 COLORREF WINAPI
NtGdiSetPixel( HDC hdc
, INT x
, INT y
, COLORREF color
)
408 DC
* dc
= get_dc_ptr( hdc
);
410 if (!dc
) return CLR_INVALID
;
412 physdev
= GET_DC_PHYSDEV( dc
, pSetPixel
);
413 ret
= physdev
->funcs
->pSetPixel( physdev
, x
, y
, color
);
414 release_dc_ptr( dc
);
418 /***********************************************************************
419 * NtGdiGetPixel (win32u.@)
421 COLORREF WINAPI
NtGdiGetPixel( HDC hdc
, INT x
, INT y
)
425 DC
* dc
= get_dc_ptr( hdc
);
427 if (!dc
) return CLR_INVALID
;
429 physdev
= GET_DC_PHYSDEV( dc
, pGetPixel
);
430 ret
= physdev
->funcs
->pGetPixel( physdev
, x
, y
);
431 release_dc_ptr( dc
);
436 /******************************************************************************
437 * GdiSetPixelFormat [GDI32.@]
439 * Probably not the correct semantics, it's supposed to be an internal backend for SetPixelFormat.
441 BOOL WINAPI
GdiSetPixelFormat( HDC hdc
, INT format
, const PIXELFORMATDESCRIPTOR
*descr
)
446 TRACE("(%p,%d,%p)\n", hdc
, format
, descr
);
448 if (!(dc
= get_dc_ptr( hdc
))) return FALSE
;
450 if (!dc
->pixel_format
) dc
->pixel_format
= format
;
451 else ret
= (dc
->pixel_format
== format
);
452 release_dc_ptr( dc
);
457 /******************************************************************************
458 * GdiDescribePixelFormat [GDI32.@]
460 * Probably not the correct semantics, it's supposed to be an internal backend for DescribePixelFormat.
462 INT WINAPI
GdiDescribePixelFormat( HDC hdc
, INT format
, UINT size
, PIXELFORMATDESCRIPTOR
*descr
)
464 FIXME( "(%p,%d,%d,%p): stub\n", hdc
, format
, size
, descr
);
469 /******************************************************************************
470 * GdiSwapBuffers [GDI32.@]
472 * Probably not the correct semantics, it's supposed to be an internal backend for SwapBuffers.
474 BOOL WINAPI
GdiSwapBuffers( HDC hdc
)
476 FIXME( "(%p): stub\n", hdc
);
481 /***********************************************************************
484 BOOL WINAPI
PaintRgn( HDC hdc
, HRGN hrgn
)
488 DC
* dc
= get_dc_ptr( hdc
);
490 TRACE( "%p, %p\n", hdc
, hrgn
);
492 if (!dc
) return FALSE
;
494 physdev
= GET_DC_PHYSDEV( dc
, pPaintRgn
);
495 ret
= physdev
->funcs
->pPaintRgn( physdev
, hrgn
);
496 release_dc_ptr( dc
);
501 /***********************************************************************
504 BOOL WINAPI
FillRgn( HDC hdc
, HRGN hrgn
, HBRUSH hbrush
)
508 DC
* dc
= get_dc_ptr( hdc
);
510 TRACE( "%p, %p, %p\n", hdc
, hrgn
, hbrush
);
512 if (!dc
) return FALSE
;
514 physdev
= GET_DC_PHYSDEV( dc
, pFillRgn
);
515 retval
= physdev
->funcs
->pFillRgn( physdev
, hrgn
, hbrush
);
516 release_dc_ptr( dc
);
521 /***********************************************************************
524 BOOL WINAPI
FrameRgn( HDC hdc
, HRGN hrgn
, HBRUSH hbrush
,
525 INT nWidth
, INT nHeight
)
529 DC
*dc
= get_dc_ptr( hdc
);
531 TRACE( "%p, %p, %p, %dx%d\n", hdc
, hrgn
, hbrush
, nWidth
, nHeight
);
533 if (!dc
) return FALSE
;
535 physdev
= GET_DC_PHYSDEV( dc
, pFrameRgn
);
536 ret
= physdev
->funcs
->pFrameRgn( physdev
, hrgn
, hbrush
, nWidth
, nHeight
);
537 release_dc_ptr( dc
);
542 /***********************************************************************
543 * InvertRgn (GDI32.@)
545 BOOL WINAPI
InvertRgn( HDC hdc
, HRGN hrgn
)
549 DC
*dc
= get_dc_ptr( hdc
);
551 TRACE( "%p, %p\n", hdc
, hrgn
);
553 if (!dc
) return FALSE
;
555 physdev
= GET_DC_PHYSDEV( dc
, pInvertRgn
);
556 ret
= physdev
->funcs
->pInvertRgn( physdev
, hrgn
);
557 release_dc_ptr( dc
);
562 /**********************************************************************
563 * NtGdiPolyPolyDraw (win32u.@)
565 ULONG WINAPI
NtGdiPolyPolyDraw( HDC hdc
, const POINT
*points
, const UINT
*counts
,
566 UINT count
, UINT function
)
572 if (function
== NtGdiPolyPolygonRgn
)
573 return HandleToULong( create_polypolygon_region( points
, (const INT
*)counts
, count
,
574 HandleToULong(hdc
), NULL
));
576 if (!(dc
= get_dc_ptr( hdc
))) return FALSE
;
581 case NtGdiPolyPolygon
:
582 physdev
= GET_DC_PHYSDEV( dc
, pPolyPolygon
);
583 ret
= physdev
->funcs
->pPolyPolygon( physdev
, points
, (const INT
*)counts
, count
);
586 case NtGdiPolyPolyline
:
587 physdev
= GET_DC_PHYSDEV( dc
, pPolyPolyline
);
588 ret
= physdev
->funcs
->pPolyPolyline( physdev
, points
, counts
, count
);
591 case NtGdiPolyBezier
:
592 /* *counts must be 3 * n + 1 (where n >= 1) */
593 if (count
== 1 && *counts
!= 1 && *counts
% 3 == 1)
595 physdev
= GET_DC_PHYSDEV( dc
, pPolyBezier
);
596 ret
= physdev
->funcs
->pPolyBezier( physdev
, points
, *counts
);
597 if (ret
) dc
->attr
->cur_pos
= points
[*counts
- 1];
602 case NtGdiPolyBezierTo
:
603 if (count
== 1 && *counts
&& *counts
% 3 == 0)
605 physdev
= GET_DC_PHYSDEV( dc
, pPolyBezierTo
);
606 ret
= physdev
->funcs
->pPolyBezierTo( physdev
, points
, *counts
);
607 if (ret
) dc
->attr
->cur_pos
= points
[*counts
- 1];
612 case NtGdiPolylineTo
:
615 physdev
= GET_DC_PHYSDEV( dc
, pPolylineTo
);
616 ret
= physdev
->funcs
->pPolylineTo( physdev
, points
, *counts
);
617 if (ret
&& *counts
) dc
->attr
->cur_pos
= points
[*counts
- 1];
623 WARN( "invalid function %u\n", function
);
628 release_dc_ptr( dc
);
632 /**********************************************************************
633 * ExtFloodFill (GDI32.@)
635 BOOL WINAPI
ExtFloodFill( HDC hdc
, INT x
, INT y
, COLORREF color
,
640 DC
* dc
= get_dc_ptr( hdc
);
642 TRACE( "%p, (%d, %d), %08x, %x\n", hdc
, x
, y
, color
, fillType
);
644 if (!dc
) return FALSE
;
646 physdev
= GET_DC_PHYSDEV( dc
, pExtFloodFill
);
647 ret
= physdev
->funcs
->pExtFloodFill( physdev
, x
, y
, color
, fillType
);
648 release_dc_ptr( dc
);
653 /**********************************************************************
654 * FloodFill (GDI32.@)
656 BOOL WINAPI
FloodFill( HDC hdc
, INT x
, INT y
, COLORREF color
)
658 return ExtFloodFill( hdc
, x
, y
, color
, FLOODFILLBORDER
);
661 /***********************************************************************
662 * NtGdiAngleArc (win32u.@)
664 BOOL WINAPI
NtGdiAngleArc( HDC hdc
, INT x
, INT y
, DWORD dwRadius
, FLOAT eStartAngle
, FLOAT eSweepAngle
)
670 if( (signed int)dwRadius
< 0 )
673 dc
= get_dc_ptr( hdc
);
674 if(!dc
) return FALSE
;
677 physdev
= GET_DC_PHYSDEV( dc
, pAngleArc
);
678 result
= physdev
->funcs
->pAngleArc( physdev
, x
, y
, dwRadius
, eStartAngle
, eSweepAngle
);
682 dc
->attr
->cur_pos
.x
= GDI_ROUND( x
+ cos( (eStartAngle
+ eSweepAngle
) * M_PI
/ 180 ) * dwRadius
);
683 dc
->attr
->cur_pos
.y
= GDI_ROUND( y
- sin( (eStartAngle
+ eSweepAngle
) * M_PI
/ 180 ) * dwRadius
);
685 release_dc_ptr( dc
);
689 /***********************************************************************
690 * NtGdiPolyDraw (win32u.@)
692 BOOL WINAPI
NtGdiPolyDraw( HDC hdc
, const POINT
*points
, const BYTE
*types
, DWORD count
)
694 DC
*dc
= get_dc_ptr( hdc
);
698 if(!dc
) return FALSE
;
701 physdev
= GET_DC_PHYSDEV( dc
, pPolyDraw
);
702 result
= physdev
->funcs
->pPolyDraw( physdev
, points
, types
, count
);
704 dc
->attr
->cur_pos
= points
[count
- 1];
706 release_dc_ptr( dc
);
711 /**********************************************************************
714 BOOL WINAPI
LineDDA(INT nXStart
, INT nYStart
, INT nXEnd
, INT nYEnd
,
715 LINEDDAPROC callback
, LPARAM lParam
)
717 INT xadd
= 1, yadd
= 1;
720 INT dx
= nXEnd
- nXStart
;
721 INT dy
= nYEnd
- nYStart
;
723 TRACE( "(%d, %d), (%d, %d), %p, %lx\n", nXStart
, nYStart
, nXEnd
, nYEnd
, callback
, lParam
);
735 if (dx
> dy
) /* line is "more horizontal" */
737 err
= 2*dy
- dx
; erradd
= 2*dy
- 2*dx
;
738 for(cnt
= 0;cnt
< dx
; cnt
++)
740 callback(nXStart
,nYStart
,lParam
);
750 else /* line is "more vertical" */
752 err
= 2*dx
- dy
; erradd
= 2*dx
- 2*dy
;
753 for(cnt
= 0;cnt
< dy
; cnt
++)
755 callback(nXStart
,nYStart
,lParam
);
769 /******************************************************************
771 * *Very* simple bezier drawing code,
773 * It uses a recursive algorithm to divide the curve in a series
774 * of straight line segments. Not ideal but sufficient for me.
775 * If you are in need for something better look for some incremental
778 * 7 July 1998 Rein Klazes
782 * some macro definitions for bezier drawing
784 * to avoid truncation errors the coordinates are
785 * shifted upwards. When used in drawing they are
786 * shifted down again, including correct rounding
787 * and avoiding floating point arithmetic
788 * 4 bits should allow 27 bits coordinates which I saw
789 * somewhere in the win32 doc's
793 #define BEZIERSHIFTBITS 4
794 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
795 #define BEZIERPIXEL BEZIERSHIFTUP(1)
796 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
797 /* maximum depth of recursion */
798 #define BEZIERMAXDEPTH 8
800 /* size of array to store points on */
801 /* enough for one curve */
802 #define BEZIER_INITBUFSIZE (150)
804 /* calculate Bezier average, in this case the middle
805 * correctly rounded...
808 #define BEZIERMIDDLE(Mid, P1, P2) \
809 (Mid).x=((P1).x+(P2).x + 1)/2;\
810 (Mid).y=((P1).y+(P2).y + 1)/2;
812 /**********************************************************
813 * BezierCheck helper function to check
814 * that recursion can be terminated
815 * Points[0] and Points[3] are begin and endpoint
816 * Points[1] and Points[2] are control points
817 * level is the recursion depth
818 * returns true if the recursion can be terminated
820 static BOOL
BezierCheck( int level
, POINT
*Points
)
823 dx
=Points
[3].x
-Points
[0].x
;
824 dy
=Points
[3].y
-Points
[0].y
;
825 if(abs(dy
)<=abs(dx
)){/* shallow line */
826 /* check that control points are between begin and end */
827 if(Points
[1].x
< Points
[0].x
){
828 if(Points
[1].x
< Points
[3].x
)
831 if(Points
[1].x
> Points
[3].x
)
833 if(Points
[2].x
< Points
[0].x
){
834 if(Points
[2].x
< Points
[3].x
)
837 if(Points
[2].x
> Points
[3].x
)
839 dx
=BEZIERSHIFTDOWN(dx
);
841 if(abs(Points
[1].y
-Points
[0].y
-(dy
/dx
)*
842 BEZIERSHIFTDOWN(Points
[1].x
-Points
[0].x
)) > BEZIERPIXEL
||
843 abs(Points
[2].y
-Points
[0].y
-(dy
/dx
)*
844 BEZIERSHIFTDOWN(Points
[2].x
-Points
[0].x
)) > BEZIERPIXEL
)
848 }else{ /* steep line */
849 /* check that control points are between begin and end */
850 if(Points
[1].y
< Points
[0].y
){
851 if(Points
[1].y
< Points
[3].y
)
854 if(Points
[1].y
> Points
[3].y
)
856 if(Points
[2].y
< Points
[0].y
){
857 if(Points
[2].y
< Points
[3].y
)
860 if(Points
[2].y
> Points
[3].y
)
862 dy
=BEZIERSHIFTDOWN(dy
);
864 if(abs(Points
[1].x
-Points
[0].x
-(dx
/dy
)*
865 BEZIERSHIFTDOWN(Points
[1].y
-Points
[0].y
)) > BEZIERPIXEL
||
866 abs(Points
[2].x
-Points
[0].x
-(dx
/dy
)*
867 BEZIERSHIFTDOWN(Points
[2].y
-Points
[0].y
)) > BEZIERPIXEL
)
874 /* Helper for GDI_Bezier.
875 * Just handles one Bezier, so Points should point to four POINTs
877 static void GDI_InternalBezier( POINT
*Points
, POINT
**PtsOut
, INT
*dwOut
,
878 INT
*nPtsOut
, INT level
)
880 if(*nPtsOut
== *dwOut
) {
882 *PtsOut
= HeapReAlloc( GetProcessHeap(), 0, *PtsOut
,
883 *dwOut
* sizeof(POINT
) );
886 if(!level
|| BezierCheck(level
, Points
)) {
888 (*PtsOut
)[0].x
= BEZIERSHIFTDOWN(Points
[0].x
);
889 (*PtsOut
)[0].y
= BEZIERSHIFTDOWN(Points
[0].y
);
892 (*PtsOut
)[*nPtsOut
].x
= BEZIERSHIFTDOWN(Points
[3].x
);
893 (*PtsOut
)[*nPtsOut
].y
= BEZIERSHIFTDOWN(Points
[3].y
);
896 POINT Points2
[4]; /* for the second recursive call */
897 Points2
[3]=Points
[3];
898 BEZIERMIDDLE(Points2
[2], Points
[2], Points
[3]);
899 BEZIERMIDDLE(Points2
[0], Points
[1], Points
[2]);
900 BEZIERMIDDLE(Points2
[1],Points2
[0],Points2
[2]);
902 BEZIERMIDDLE(Points
[1], Points
[0], Points
[1]);
903 BEZIERMIDDLE(Points
[2], Points
[1], Points2
[0]);
904 BEZIERMIDDLE(Points
[3], Points
[2], Points2
[1]);
906 Points2
[0]=Points
[3];
908 /* do the two halves */
909 GDI_InternalBezier(Points
, PtsOut
, dwOut
, nPtsOut
, level
-1);
910 GDI_InternalBezier(Points2
, PtsOut
, dwOut
, nPtsOut
, level
-1);
916 /***********************************************************************
917 * GDI_Bezier [INTERNAL]
918 * Calculate line segments that approximate -what microsoft calls- a bezier
920 * The routine recursively divides the curve in two parts until a straight
925 * Points [I] Ptr to count POINTs which are the end and control points
926 * of the set of Bezier curves to flatten.
927 * count [I] Number of Points. Must be 3n+1.
928 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
933 * Ptr to an array of POINTs that contain the lines that approximate the
934 * Beziers. The array is allocated on the process heap and it is the caller's
935 * responsibility to HeapFree it. [this is not a particularly nice interface
936 * but since we can't know in advance how many points we will generate, the
937 * alternative would be to call the function twice, once to determine the size
938 * and a second time to do the work - I decided this was too much of a pain].
940 POINT
*GDI_Bezier( const POINT
*Points
, INT count
, INT
*nPtsOut
)
943 INT Bezier
, dwOut
= BEZIER_INITBUFSIZE
, i
;
945 if (count
== 1 || (count
- 1) % 3 != 0) {
946 ERR("Invalid no. of points %d\n", count
);
950 out
= HeapAlloc( GetProcessHeap(), 0, dwOut
* sizeof(POINT
));
951 for(Bezier
= 0; Bezier
< (count
-1)/3; Bezier
++) {
953 memcpy(ptBuf
, Points
+ Bezier
* 3, sizeof(POINT
) * 4);
954 for(i
= 0; i
< 4; i
++) {
955 ptBuf
[i
].x
= BEZIERSHIFTUP(ptBuf
[i
].x
);
956 ptBuf
[i
].y
= BEZIERSHIFTUP(ptBuf
[i
].y
);
958 GDI_InternalBezier( ptBuf
, &out
, &dwOut
, nPtsOut
, BEZIERMAXDEPTH
);
960 TRACE("Produced %d points\n", *nPtsOut
);
964 /******************************************************************************
965 * GdiGradientFill (GDI32.@)
967 BOOL WINAPI
GdiGradientFill( HDC hdc
, TRIVERTEX
*vert_array
, ULONG nvert
,
968 void *grad_array
, ULONG ngrad
, ULONG mode
)
975 TRACE("%p vert_array:%p nvert:%d grad_array:%p ngrad:%d\n", hdc
, vert_array
, nvert
, grad_array
, ngrad
);
977 if (!vert_array
|| !nvert
|| !grad_array
|| !ngrad
|| mode
> GRADIENT_FILL_TRIANGLE
)
979 SetLastError( ERROR_INVALID_PARAMETER
);
982 for (i
= 0; i
< ngrad
* (mode
== GRADIENT_FILL_TRIANGLE
? 3 : 2); i
++)
983 if (((ULONG
*)grad_array
)[i
] >= nvert
) return FALSE
;
985 if (!(dc
= get_dc_ptr( hdc
)))
987 SetLastError( ERROR_INVALID_PARAMETER
);
991 physdev
= GET_DC_PHYSDEV( dc
, pGradientFill
);
992 ret
= physdev
->funcs
->pGradientFill( physdev
, vert_array
, nvert
, grad_array
, ngrad
, mode
);
993 release_dc_ptr( dc
);
997 /******************************************************************************
998 * GdiDrawStream (GDI32.@)
1001 BOOL WINAPI
GdiDrawStream( HDC hdc
, ULONG in
, void * pvin
)
1003 FIXME("stub: %p, %d, %p\n", hdc
, in
, pvin
);