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 "gdi_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
= CreateRectRgn( 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 BOOL CDECL
nulldrv_PolyBezier( PHYSDEV dev
, const POINT
*points
, DWORD count
)
114 if ((pts
= GDI_Bezier( points
, count
, &n
)))
116 ret
= Polyline( dev
->hdc
, pts
, n
);
117 HeapFree( GetProcessHeap(), 0, pts
);
122 BOOL CDECL
nulldrv_PolyBezierTo( PHYSDEV dev
, const POINT
*points
, DWORD count
)
124 DC
*dc
= get_nulldrv_dc( dev
);
126 POINT
*pts
= HeapAlloc( GetProcessHeap(), 0, sizeof(POINT
) * (count
+ 1) );
130 pts
[0] = dc
->cur_pos
;
131 memcpy( pts
+ 1, points
, sizeof(POINT
) * count
);
132 ret
= PolyBezier( dev
->hdc
, pts
, count
+ 1 );
133 HeapFree( GetProcessHeap(), 0, pts
);
138 BOOL CDECL
nulldrv_PolyDraw( PHYSDEV dev
, const POINT
*points
, const BYTE
*types
, DWORD count
)
140 DC
*dc
= get_nulldrv_dc( dev
);
141 POINT
*line_pts
= NULL
, *bzr_pts
= NULL
, bzr
[4];
143 INT num_pts
, num_bzr_pts
, space
, size
;
145 /* check for valid point types */
146 for (i
= 0; i
< count
; i
++)
151 case PT_LINETO
| PT_CLOSEFIGURE
:
155 if (i
+ 2 >= count
) return FALSE
;
156 if (types
[i
+ 1] != PT_BEZIERTO
) return FALSE
;
157 if ((types
[i
+ 2] & ~PT_CLOSEFIGURE
) != PT_BEZIERTO
) return FALSE
;
166 line_pts
= HeapAlloc( GetProcessHeap(), 0, space
* sizeof(POINT
) );
169 line_pts
[0] = dc
->cur_pos
;
170 for (i
= 0; i
< count
; i
++)
175 if (num_pts
>= 2) Polyline( dev
->hdc
, line_pts
, num_pts
);
177 line_pts
[num_pts
++] = points
[i
];
180 case (PT_LINETO
| PT_CLOSEFIGURE
):
181 line_pts
[num_pts
++] = points
[i
];
184 bzr
[0].x
= line_pts
[num_pts
- 1].x
;
185 bzr
[0].y
= line_pts
[num_pts
- 1].y
;
186 memcpy( &bzr
[1], &points
[i
], 3 * sizeof(POINT
) );
188 if ((bzr_pts
= GDI_Bezier( bzr
, 4, &num_bzr_pts
)))
190 size
= num_pts
+ (count
- i
) + num_bzr_pts
;
194 line_pts
= HeapReAlloc( GetProcessHeap(), 0, line_pts
, space
* sizeof(POINT
) );
196 memcpy( &line_pts
[num_pts
], &bzr_pts
[1], (num_bzr_pts
- 1) * sizeof(POINT
) );
197 num_pts
+= num_bzr_pts
- 1;
198 HeapFree( GetProcessHeap(), 0, bzr_pts
);
203 if (types
[i
] & PT_CLOSEFIGURE
) line_pts
[num_pts
++] = line_pts
[0];
206 if (num_pts
>= 2) Polyline( dev
->hdc
, line_pts
, num_pts
);
207 HeapFree( GetProcessHeap(), 0, line_pts
);
211 BOOL CDECL
nulldrv_PolylineTo( PHYSDEV dev
, const POINT
*points
, INT count
)
213 DC
*dc
= get_nulldrv_dc( dev
);
217 if (!count
) return FALSE
;
218 if ((pts
= HeapAlloc( GetProcessHeap(), 0, sizeof(POINT
) * (count
+ 1) )))
220 pts
[0] = dc
->cur_pos
;
221 memcpy( pts
+ 1, points
, sizeof(POINT
) * count
);
222 ret
= Polyline( dev
->hdc
, pts
, count
+ 1 );
223 HeapFree( GetProcessHeap(), 0, pts
);
228 /***********************************************************************
231 BOOL WINAPI
LineTo( HDC hdc
, INT x
, INT y
)
233 DC
* dc
= get_dc_ptr( hdc
);
237 TRACE( "%p, (%d, %d)\n", hdc
, x
, y
);
239 if(!dc
) return FALSE
;
242 physdev
= GET_DC_PHYSDEV( dc
, pLineTo
);
243 ret
= physdev
->funcs
->pLineTo( physdev
, x
, y
);
250 release_dc_ptr( dc
);
255 /***********************************************************************
258 BOOL WINAPI
MoveToEx( HDC hdc
, INT x
, INT y
, LPPOINT pt
)
262 DC
* dc
= get_dc_ptr( hdc
);
264 TRACE( "%p, (%d, %d), %p\n", hdc
, x
, y
, pt
);
266 if(!dc
) return FALSE
;
274 physdev
= GET_DC_PHYSDEV( dc
, pMoveTo
);
275 ret
= physdev
->funcs
->pMoveTo( physdev
, x
, y
);
276 release_dc_ptr( dc
);
281 /***********************************************************************
284 BOOL WINAPI
Arc( HDC hdc
, INT left
, INT top
, INT right
,
285 INT bottom
, INT xstart
, INT ystart
,
290 DC
* dc
= get_dc_ptr( hdc
);
292 TRACE( "%p, (%d, %d)-(%d, %d), (%d, %d), (%d, %d)\n", hdc
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
);
294 if (!dc
) return FALSE
;
296 physdev
= GET_DC_PHYSDEV( dc
, pArc
);
297 ret
= physdev
->funcs
->pArc( physdev
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
);
298 release_dc_ptr( dc
);
302 /***********************************************************************
305 BOOL WINAPI
ArcTo( HDC hdc
,
307 INT right
, INT bottom
,
308 INT xstart
, INT ystart
,
311 double width
= abs( right
- left
),
312 height
= abs( bottom
- top
),
315 xcenter
= right
> left
? left
+xradius
: right
+xradius
,
316 ycenter
= bottom
> top
? top
+yradius
: bottom
+yradius
,
320 DC
* dc
= get_dc_ptr( hdc
);
322 TRACE( "%p, (%d, %d)-(%d, %d), (%d, %d), (%d, %d)\n", hdc
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
);
324 if(!dc
) return FALSE
;
326 physdev
= GET_DC_PHYSDEV( dc
, pArcTo
);
327 result
= physdev
->funcs
->pArcTo( physdev
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
);
331 angle
= atan2(((yend
-ycenter
)/height
),
332 ((xend
-xcenter
)/width
));
333 dc
->cur_pos
.x
= GDI_ROUND( xcenter
+ (cos( angle
) * xradius
) );
334 dc
->cur_pos
.y
= GDI_ROUND( ycenter
+ (sin( angle
) * yradius
) );
336 release_dc_ptr( dc
);
341 /***********************************************************************
344 BOOL WINAPI
Pie( HDC hdc
, INT left
, INT top
,
345 INT right
, INT bottom
, INT xstart
, INT ystart
,
350 DC
* dc
= get_dc_ptr( hdc
);
352 TRACE( "%p, (%d, %d)-(%d, %d), (%d, %d), (%d, %d)\n", hdc
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
);
354 if (!dc
) return FALSE
;
356 physdev
= GET_DC_PHYSDEV( dc
, pPie
);
357 ret
= physdev
->funcs
->pPie( physdev
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
);
358 release_dc_ptr( dc
);
363 /***********************************************************************
366 BOOL WINAPI
Chord( HDC hdc
, INT left
, INT top
,
367 INT right
, INT bottom
, INT xstart
, INT ystart
,
372 DC
* dc
= get_dc_ptr( hdc
);
374 TRACE( "%p, (%d, %d)-(%d, %d), (%d, %d), (%d, %d)\n", hdc
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
);
376 if (!dc
) return FALSE
;
378 physdev
= GET_DC_PHYSDEV( dc
, pChord
);
379 ret
= physdev
->funcs
->pChord( physdev
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
);
380 release_dc_ptr( dc
);
385 /***********************************************************************
388 BOOL WINAPI
Ellipse( HDC hdc
, INT left
, INT top
,
389 INT right
, INT bottom
)
393 DC
* dc
= get_dc_ptr( hdc
);
395 TRACE( "%p, (%d, %d)-(%d, %d)\n", hdc
, left
, top
, right
, bottom
);
397 if (!dc
) return FALSE
;
399 physdev
= GET_DC_PHYSDEV( dc
, pEllipse
);
400 ret
= physdev
->funcs
->pEllipse( physdev
, left
, top
, right
, bottom
);
401 release_dc_ptr( dc
);
406 /***********************************************************************
407 * Rectangle (GDI32.@)
409 BOOL WINAPI
Rectangle( HDC hdc
, INT left
, INT top
,
410 INT right
, INT bottom
)
414 DC
* dc
= get_dc_ptr( hdc
);
416 TRACE( "%p, (%d, %d)-(%d, %d)\n", hdc
, left
, top
, right
, bottom
);
418 if (!dc
) return FALSE
;
420 physdev
= GET_DC_PHYSDEV( dc
, pRectangle
);
421 ret
= physdev
->funcs
->pRectangle( physdev
, left
, top
, right
, bottom
);
422 release_dc_ptr( dc
);
427 /***********************************************************************
428 * RoundRect (GDI32.@)
430 BOOL WINAPI
RoundRect( HDC hdc
, INT left
, INT top
, INT right
,
431 INT bottom
, INT ell_width
, INT ell_height
)
435 DC
*dc
= get_dc_ptr( hdc
);
437 TRACE( "%p, (%d, %d)-(%d, %d), %dx%d\n", hdc
, left
, top
, right
, bottom
, ell_width
, ell_height
);
439 if (!dc
) return FALSE
;
441 physdev
= GET_DC_PHYSDEV( dc
, pRoundRect
);
442 ret
= physdev
->funcs
->pRoundRect( physdev
, left
, top
, right
, bottom
, ell_width
, ell_height
);
443 release_dc_ptr( dc
);
447 /***********************************************************************
450 COLORREF WINAPI
SetPixel( HDC hdc
, INT x
, INT y
, COLORREF color
)
454 DC
* dc
= get_dc_ptr( hdc
);
458 physdev
= GET_DC_PHYSDEV( dc
, pSetPixel
);
459 ret
= physdev
->funcs
->pSetPixel( physdev
, x
, y
, color
);
460 release_dc_ptr( dc
);
464 /***********************************************************************
465 * SetPixelV (GDI32.@)
467 BOOL WINAPI
SetPixelV( HDC hdc
, INT x
, INT y
, COLORREF color
)
470 DC
* dc
= get_dc_ptr( hdc
);
472 if (!dc
) return FALSE
;
474 physdev
= GET_DC_PHYSDEV( dc
, pSetPixel
);
475 physdev
->funcs
->pSetPixel( physdev
, x
, y
, color
);
476 release_dc_ptr( dc
);
480 /***********************************************************************
483 COLORREF WINAPI
GetPixel( HDC hdc
, INT x
, INT y
)
487 DC
* dc
= get_dc_ptr( hdc
);
489 if (!dc
) return CLR_INVALID
;
491 physdev
= GET_DC_PHYSDEV( dc
, pGetPixel
);
492 ret
= physdev
->funcs
->pGetPixel( physdev
, x
, y
);
493 release_dc_ptr( dc
);
498 /******************************************************************************
499 * GdiSetPixelFormat [GDI32.@]
501 * Probably not the correct semantics, it's supposed to be an internal backend for SetPixelFormat.
503 BOOL WINAPI
GdiSetPixelFormat( HDC hdc
, INT format
, const PIXELFORMATDESCRIPTOR
*descr
)
508 TRACE("(%p,%d,%p)\n", hdc
, format
, descr
);
510 if (!(dc
= get_dc_ptr( hdc
))) return FALSE
;
512 if (!dc
->pixel_format
) dc
->pixel_format
= format
;
513 else ret
= (dc
->pixel_format
== format
);
514 release_dc_ptr( dc
);
519 /******************************************************************************
520 * GdiDescribePixelFormat [GDI32.@]
522 * Probably not the correct semantics, it's supposed to be an internal backend for DescribePixelFormat.
524 INT WINAPI
GdiDescribePixelFormat( HDC hdc
, INT format
, UINT size
, PIXELFORMATDESCRIPTOR
*descr
)
526 FIXME( "(%p,%d,%d,%p): stub\n", hdc
, format
, size
, descr
);
531 /******************************************************************************
532 * GdiSwapBuffers [GDI32.@]
534 * Probably not the correct semantics, it's supposed to be an internal backend for SwapBuffers.
536 BOOL WINAPI
GdiSwapBuffers( HDC hdc
)
538 FIXME( "(%p): stub\n", hdc
);
543 /***********************************************************************
546 BOOL WINAPI
PaintRgn( HDC hdc
, HRGN hrgn
)
550 DC
* dc
= get_dc_ptr( hdc
);
552 TRACE( "%p, %p\n", hdc
, hrgn
);
554 if (!dc
) return FALSE
;
556 physdev
= GET_DC_PHYSDEV( dc
, pPaintRgn
);
557 ret
= physdev
->funcs
->pPaintRgn( physdev
, hrgn
);
558 release_dc_ptr( dc
);
563 /***********************************************************************
566 BOOL WINAPI
FillRgn( HDC hdc
, HRGN hrgn
, HBRUSH hbrush
)
570 DC
* dc
= get_dc_ptr( hdc
);
572 TRACE( "%p, %p, %p\n", hdc
, hrgn
, hbrush
);
574 if (!dc
) return FALSE
;
576 physdev
= GET_DC_PHYSDEV( dc
, pFillRgn
);
577 retval
= physdev
->funcs
->pFillRgn( physdev
, hrgn
, hbrush
);
578 release_dc_ptr( dc
);
583 /***********************************************************************
586 BOOL WINAPI
FrameRgn( HDC hdc
, HRGN hrgn
, HBRUSH hbrush
,
587 INT nWidth
, INT nHeight
)
591 DC
*dc
= get_dc_ptr( hdc
);
593 TRACE( "%p, %p, %p, %dx%d\n", hdc
, hrgn
, hbrush
, nWidth
, nHeight
);
595 if (!dc
) return FALSE
;
597 physdev
= GET_DC_PHYSDEV( dc
, pFrameRgn
);
598 ret
= physdev
->funcs
->pFrameRgn( physdev
, hrgn
, hbrush
, nWidth
, nHeight
);
599 release_dc_ptr( dc
);
604 /***********************************************************************
605 * InvertRgn (GDI32.@)
607 BOOL WINAPI
InvertRgn( HDC hdc
, HRGN hrgn
)
611 DC
*dc
= get_dc_ptr( hdc
);
613 TRACE( "%p, %p\n", hdc
, hrgn
);
615 if (!dc
) return FALSE
;
617 physdev
= GET_DC_PHYSDEV( dc
, pInvertRgn
);
618 ret
= physdev
->funcs
->pInvertRgn( physdev
, hrgn
);
619 release_dc_ptr( dc
);
624 /**********************************************************************
627 BOOL WINAPI
Polyline( HDC hdc
, const POINT
* pt
, INT count
)
631 DC
* dc
= get_dc_ptr( hdc
);
633 TRACE( "%p, %p, %d\n", hdc
, pt
, count
);
635 if (!dc
) return FALSE
;
637 physdev
= GET_DC_PHYSDEV( dc
, pPolyline
);
638 ret
= physdev
->funcs
->pPolyline( physdev
, pt
, count
);
639 release_dc_ptr( dc
);
643 /**********************************************************************
644 * PolylineTo (GDI32.@)
646 BOOL WINAPI
PolylineTo( HDC hdc
, const POINT
* pt
, DWORD cCount
)
648 DC
* dc
= get_dc_ptr( hdc
);
652 TRACE( "%p, %p, %u\n", hdc
, pt
, cCount
);
654 if(!dc
) return FALSE
;
657 physdev
= GET_DC_PHYSDEV( dc
, pPolylineTo
);
658 ret
= physdev
->funcs
->pPolylineTo( physdev
, pt
, cCount
);
661 dc
->cur_pos
= pt
[cCount
- 1];
663 release_dc_ptr( dc
);
668 /**********************************************************************
671 BOOL WINAPI
Polygon( HDC hdc
, const POINT
* pt
, INT count
)
675 DC
* dc
= get_dc_ptr( hdc
);
677 TRACE( "%p, %p, %d\n", hdc
, pt
, count
);
679 if (!dc
) return FALSE
;
681 physdev
= GET_DC_PHYSDEV( dc
, pPolygon
);
682 ret
= physdev
->funcs
->pPolygon( physdev
, pt
, count
);
683 release_dc_ptr( dc
);
688 /**********************************************************************
689 * PolyPolygon (GDI32.@)
691 BOOL WINAPI
PolyPolygon( HDC hdc
, const POINT
* pt
, const INT
* counts
,
696 DC
* dc
= get_dc_ptr( hdc
);
698 TRACE( "%p, %p, %p, %u\n", hdc
, pt
, counts
, polygons
);
700 if (!dc
) return FALSE
;
702 physdev
= GET_DC_PHYSDEV( dc
, pPolyPolygon
);
703 ret
= physdev
->funcs
->pPolyPolygon( physdev
, pt
, counts
, polygons
);
704 release_dc_ptr( dc
);
708 /**********************************************************************
709 * PolyPolyline (GDI32.@)
711 BOOL WINAPI
PolyPolyline( HDC hdc
, const POINT
* pt
, const DWORD
* counts
,
716 DC
* dc
= get_dc_ptr( hdc
);
718 TRACE( "%p, %p, %p, %u\n", hdc
, pt
, counts
, polylines
);
720 if (!dc
) return FALSE
;
722 physdev
= GET_DC_PHYSDEV( dc
, pPolyPolyline
);
723 ret
= physdev
->funcs
->pPolyPolyline( physdev
, pt
, counts
, polylines
);
724 release_dc_ptr( dc
);
728 /**********************************************************************
729 * ExtFloodFill (GDI32.@)
731 BOOL WINAPI
ExtFloodFill( HDC hdc
, INT x
, INT y
, COLORREF color
,
736 DC
* dc
= get_dc_ptr( hdc
);
738 TRACE( "%p, (%d, %d), %08x, %x\n", hdc
, x
, y
, color
, fillType
);
740 if (!dc
) return FALSE
;
742 physdev
= GET_DC_PHYSDEV( dc
, pExtFloodFill
);
743 ret
= physdev
->funcs
->pExtFloodFill( physdev
, x
, y
, color
, fillType
);
744 release_dc_ptr( dc
);
749 /**********************************************************************
750 * FloodFill (GDI32.@)
752 BOOL WINAPI
FloodFill( HDC hdc
, INT x
, INT y
, COLORREF color
)
754 return ExtFloodFill( hdc
, x
, y
, color
, FLOODFILLBORDER
);
758 /******************************************************************************
759 * PolyBezier [GDI32.@]
760 * Draws one or more Bezier curves
763 * hDc [I] Handle to device context
764 * lppt [I] Pointer to endpoints and control points
765 * cPoints [I] Count of endpoints and control points
771 BOOL WINAPI
PolyBezier( HDC hdc
, const POINT
* lppt
, DWORD cPoints
)
777 TRACE( "%p, %p, %u\n", hdc
, lppt
, cPoints
);
779 /* cPoints must be 3 * n + 1 (where n>=1) */
780 if (cPoints
== 1 || (cPoints
% 3) != 1) return FALSE
;
782 dc
= get_dc_ptr( hdc
);
783 if(!dc
) return FALSE
;
786 physdev
= GET_DC_PHYSDEV( dc
, pPolyBezier
);
787 ret
= physdev
->funcs
->pPolyBezier( physdev
, lppt
, cPoints
);
788 release_dc_ptr( dc
);
792 /******************************************************************************
793 * PolyBezierTo [GDI32.@]
794 * Draws one or more Bezier curves
797 * hDc [I] Handle to device context
798 * lppt [I] Pointer to endpoints and control points
799 * cPoints [I] Count of endpoints and control points
805 BOOL WINAPI
PolyBezierTo( HDC hdc
, const POINT
* lppt
, DWORD cPoints
)
811 TRACE( "%p, %p, %u\n", hdc
, lppt
, cPoints
);
813 /* cbPoints must be 3 * n (where n>=1) */
814 if (!cPoints
|| (cPoints
% 3) != 0) return FALSE
;
816 dc
= get_dc_ptr( hdc
);
817 if(!dc
) return FALSE
;
820 physdev
= GET_DC_PHYSDEV( dc
, pPolyBezierTo
);
821 ret
= physdev
->funcs
->pPolyBezierTo( physdev
, lppt
, cPoints
);
824 dc
->cur_pos
= lppt
[cPoints
- 1];
826 release_dc_ptr( dc
);
830 /***********************************************************************
833 BOOL WINAPI
AngleArc(HDC hdc
, INT x
, INT y
, DWORD dwRadius
, FLOAT eStartAngle
, FLOAT eSweepAngle
)
839 TRACE( "%p, (%d, %d), %u, %f, %f\n", hdc
, x
, y
, dwRadius
, eStartAngle
, eSweepAngle
);
841 if( (signed int)dwRadius
< 0 )
844 dc
= get_dc_ptr( hdc
);
845 if(!dc
) return FALSE
;
848 physdev
= GET_DC_PHYSDEV( dc
, pAngleArc
);
849 result
= physdev
->funcs
->pAngleArc( physdev
, x
, y
, dwRadius
, eStartAngle
, eSweepAngle
);
853 dc
->cur_pos
.x
= GDI_ROUND( x
+ cos( (eStartAngle
+ eSweepAngle
) * M_PI
/ 180 ) * dwRadius
);
854 dc
->cur_pos
.y
= GDI_ROUND( y
- sin( (eStartAngle
+ eSweepAngle
) * M_PI
/ 180 ) * dwRadius
);
856 release_dc_ptr( dc
);
860 /***********************************************************************
863 BOOL WINAPI
PolyDraw(HDC hdc
, const POINT
*lppt
, const BYTE
*lpbTypes
,
866 DC
*dc
= get_dc_ptr( hdc
);
870 TRACE( "%p, %p, %p, %u\n", hdc
, lppt
, lpbTypes
, cCount
);
872 if(!dc
) return FALSE
;
875 physdev
= GET_DC_PHYSDEV( dc
, pPolyDraw
);
876 result
= physdev
->funcs
->pPolyDraw( physdev
, lppt
, lpbTypes
, cCount
);
877 if (result
&& cCount
)
878 dc
->cur_pos
= lppt
[cCount
- 1];
880 release_dc_ptr( dc
);
885 /**********************************************************************
888 BOOL WINAPI
LineDDA(INT nXStart
, INT nYStart
, INT nXEnd
, INT nYEnd
,
889 LINEDDAPROC callback
, LPARAM lParam
)
891 INT xadd
= 1, yadd
= 1;
894 INT dx
= nXEnd
- nXStart
;
895 INT dy
= nYEnd
- nYStart
;
897 TRACE( "(%d, %d), (%d, %d), %p, %lx\n", nXStart
, nYStart
, nXEnd
, nYEnd
, callback
, lParam
);
909 if (dx
> dy
) /* line is "more horizontal" */
911 err
= 2*dy
- dx
; erradd
= 2*dy
- 2*dx
;
912 for(cnt
= 0;cnt
< dx
; cnt
++)
914 callback(nXStart
,nYStart
,lParam
);
924 else /* line is "more vertical" */
926 err
= 2*dx
- dy
; erradd
= 2*dx
- 2*dy
;
927 for(cnt
= 0;cnt
< dy
; cnt
++)
929 callback(nXStart
,nYStart
,lParam
);
943 /******************************************************************
945 * *Very* simple bezier drawing code,
947 * It uses a recursive algorithm to divide the curve in a series
948 * of straight line segments. Not ideal but sufficient for me.
949 * If you are in need for something better look for some incremental
952 * 7 July 1998 Rein Klazes
956 * some macro definitions for bezier drawing
958 * to avoid truncation errors the coordinates are
959 * shifted upwards. When used in drawing they are
960 * shifted down again, including correct rounding
961 * and avoiding floating point arithmetic
962 * 4 bits should allow 27 bits coordinates which I saw
963 * somewhere in the win32 doc's
967 #define BEZIERSHIFTBITS 4
968 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
969 #define BEZIERPIXEL BEZIERSHIFTUP(1)
970 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
971 /* maximum depth of recursion */
972 #define BEZIERMAXDEPTH 8
974 /* size of array to store points on */
975 /* enough for one curve */
976 #define BEZIER_INITBUFSIZE (150)
978 /* calculate Bezier average, in this case the middle
979 * correctly rounded...
982 #define BEZIERMIDDLE(Mid, P1, P2) \
983 (Mid).x=((P1).x+(P2).x + 1)/2;\
984 (Mid).y=((P1).y+(P2).y + 1)/2;
986 /**********************************************************
987 * BezierCheck helper function to check
988 * that recursion can be terminated
989 * Points[0] and Points[3] are begin and endpoint
990 * Points[1] and Points[2] are control points
991 * level is the recursion depth
992 * returns true if the recursion can be terminated
994 static BOOL
BezierCheck( int level
, POINT
*Points
)
997 dx
=Points
[3].x
-Points
[0].x
;
998 dy
=Points
[3].y
-Points
[0].y
;
999 if(abs(dy
)<=abs(dx
)){/* shallow line */
1000 /* check that control points are between begin and end */
1001 if(Points
[1].x
< Points
[0].x
){
1002 if(Points
[1].x
< Points
[3].x
)
1005 if(Points
[1].x
> Points
[3].x
)
1007 if(Points
[2].x
< Points
[0].x
){
1008 if(Points
[2].x
< Points
[3].x
)
1011 if(Points
[2].x
> Points
[3].x
)
1013 dx
=BEZIERSHIFTDOWN(dx
);
1014 if(!dx
) return TRUE
;
1015 if(abs(Points
[1].y
-Points
[0].y
-(dy
/dx
)*
1016 BEZIERSHIFTDOWN(Points
[1].x
-Points
[0].x
)) > BEZIERPIXEL
||
1017 abs(Points
[2].y
-Points
[0].y
-(dy
/dx
)*
1018 BEZIERSHIFTDOWN(Points
[2].x
-Points
[0].x
)) > BEZIERPIXEL
)
1022 }else{ /* steep line */
1023 /* check that control points are between begin and end */
1024 if(Points
[1].y
< Points
[0].y
){
1025 if(Points
[1].y
< Points
[3].y
)
1028 if(Points
[1].y
> Points
[3].y
)
1030 if(Points
[2].y
< Points
[0].y
){
1031 if(Points
[2].y
< Points
[3].y
)
1034 if(Points
[2].y
> Points
[3].y
)
1036 dy
=BEZIERSHIFTDOWN(dy
);
1037 if(!dy
) return TRUE
;
1038 if(abs(Points
[1].x
-Points
[0].x
-(dx
/dy
)*
1039 BEZIERSHIFTDOWN(Points
[1].y
-Points
[0].y
)) > BEZIERPIXEL
||
1040 abs(Points
[2].x
-Points
[0].x
-(dx
/dy
)*
1041 BEZIERSHIFTDOWN(Points
[2].y
-Points
[0].y
)) > BEZIERPIXEL
)
1048 /* Helper for GDI_Bezier.
1049 * Just handles one Bezier, so Points should point to four POINTs
1051 static void GDI_InternalBezier( POINT
*Points
, POINT
**PtsOut
, INT
*dwOut
,
1052 INT
*nPtsOut
, INT level
)
1054 if(*nPtsOut
== *dwOut
) {
1056 *PtsOut
= HeapReAlloc( GetProcessHeap(), 0, *PtsOut
,
1057 *dwOut
* sizeof(POINT
) );
1060 if(!level
|| BezierCheck(level
, Points
)) {
1062 (*PtsOut
)[0].x
= BEZIERSHIFTDOWN(Points
[0].x
);
1063 (*PtsOut
)[0].y
= BEZIERSHIFTDOWN(Points
[0].y
);
1066 (*PtsOut
)[*nPtsOut
].x
= BEZIERSHIFTDOWN(Points
[3].x
);
1067 (*PtsOut
)[*nPtsOut
].y
= BEZIERSHIFTDOWN(Points
[3].y
);
1070 POINT Points2
[4]; /* for the second recursive call */
1071 Points2
[3]=Points
[3];
1072 BEZIERMIDDLE(Points2
[2], Points
[2], Points
[3]);
1073 BEZIERMIDDLE(Points2
[0], Points
[1], Points
[2]);
1074 BEZIERMIDDLE(Points2
[1],Points2
[0],Points2
[2]);
1076 BEZIERMIDDLE(Points
[1], Points
[0], Points
[1]);
1077 BEZIERMIDDLE(Points
[2], Points
[1], Points2
[0]);
1078 BEZIERMIDDLE(Points
[3], Points
[2], Points2
[1]);
1080 Points2
[0]=Points
[3];
1082 /* do the two halves */
1083 GDI_InternalBezier(Points
, PtsOut
, dwOut
, nPtsOut
, level
-1);
1084 GDI_InternalBezier(Points2
, PtsOut
, dwOut
, nPtsOut
, level
-1);
1090 /***********************************************************************
1091 * GDI_Bezier [INTERNAL]
1092 * Calculate line segments that approximate -what microsoft calls- a bezier
1094 * The routine recursively divides the curve in two parts until a straight
1099 * Points [I] Ptr to count POINTs which are the end and control points
1100 * of the set of Bezier curves to flatten.
1101 * count [I] Number of Points. Must be 3n+1.
1102 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
1107 * Ptr to an array of POINTs that contain the lines that approximate the
1108 * Beziers. The array is allocated on the process heap and it is the caller's
1109 * responsibility to HeapFree it. [this is not a particularly nice interface
1110 * but since we can't know in advance how many points we will generate, the
1111 * alternative would be to call the function twice, once to determine the size
1112 * and a second time to do the work - I decided this was too much of a pain].
1114 POINT
*GDI_Bezier( const POINT
*Points
, INT count
, INT
*nPtsOut
)
1117 INT Bezier
, dwOut
= BEZIER_INITBUFSIZE
, i
;
1119 if (count
== 1 || (count
- 1) % 3 != 0) {
1120 ERR("Invalid no. of points %d\n", count
);
1124 out
= HeapAlloc( GetProcessHeap(), 0, dwOut
* sizeof(POINT
));
1125 for(Bezier
= 0; Bezier
< (count
-1)/3; Bezier
++) {
1127 memcpy(ptBuf
, Points
+ Bezier
* 3, sizeof(POINT
) * 4);
1128 for(i
= 0; i
< 4; i
++) {
1129 ptBuf
[i
].x
= BEZIERSHIFTUP(ptBuf
[i
].x
);
1130 ptBuf
[i
].y
= BEZIERSHIFTUP(ptBuf
[i
].y
);
1132 GDI_InternalBezier( ptBuf
, &out
, &dwOut
, nPtsOut
, BEZIERMAXDEPTH
);
1134 TRACE("Produced %d points\n", *nPtsOut
);
1138 /******************************************************************************
1139 * GdiGradientFill (GDI32.@)
1141 BOOL WINAPI
GdiGradientFill( HDC hdc
, TRIVERTEX
*vert_array
, ULONG nvert
,
1142 void *grad_array
, ULONG ngrad
, ULONG mode
)
1149 TRACE("%p vert_array:%p nvert:%d grad_array:%p ngrad:%d\n", hdc
, vert_array
, nvert
, grad_array
, ngrad
);
1151 if (!vert_array
|| !nvert
|| !grad_array
|| !ngrad
|| mode
> GRADIENT_FILL_TRIANGLE
)
1153 SetLastError( ERROR_INVALID_PARAMETER
);
1156 for (i
= 0; i
< ngrad
* (mode
== GRADIENT_FILL_TRIANGLE
? 3 : 2); i
++)
1157 if (((ULONG
*)grad_array
)[i
] >= nvert
) return FALSE
;
1159 if (!(dc
= get_dc_ptr( hdc
)))
1161 SetLastError( ERROR_INVALID_PARAMETER
);
1165 physdev
= GET_DC_PHYSDEV( dc
, pGradientFill
);
1166 ret
= physdev
->funcs
->pGradientFill( physdev
, vert_array
, nvert
, grad_array
, ngrad
, mode
);
1167 release_dc_ptr( dc
);
1171 /******************************************************************************
1172 * GdiDrawStream (GDI32.@)
1175 BOOL WINAPI
GdiDrawStream( HDC hdc
, ULONG in
, void * pvin
)
1177 FIXME("stub: %p, %d, %p\n", hdc
, in
, pvin
);