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
24 #include "wine/port.h"
34 #include "gdi_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(gdi
);
40 /***********************************************************************
43 BOOL WINAPI
LineTo( HDC hdc
, INT x
, INT y
)
45 DC
* dc
= DC_GetDCUpdate( hdc
);
50 if(PATH_IsPathOpen(dc
->path
))
51 ret
= PATH_LineTo(dc
, x
, y
);
53 ret
= dc
->funcs
->pLineTo
&& dc
->funcs
->pLineTo(dc
->physDev
,x
,y
);
58 DC_ReleaseDCPtr( dc
);
63 /***********************************************************************
66 BOOL WINAPI
MoveToEx( HDC hdc
, INT x
, INT y
, LPPOINT pt
)
69 DC
* dc
= DC_GetDCPtr( hdc
);
80 if(PATH_IsPathOpen(dc
->path
)) ret
= PATH_MoveTo(dc
);
81 else if (dc
->funcs
->pMoveTo
) ret
= dc
->funcs
->pMoveTo(dc
->physDev
,x
,y
);
82 DC_ReleaseDCPtr( dc
);
87 /***********************************************************************
90 BOOL WINAPI
Arc( HDC hdc
, INT left
, INT top
, INT right
,
91 INT bottom
, INT xstart
, INT ystart
,
95 DC
* dc
= DC_GetDCUpdate( hdc
);
98 if(PATH_IsPathOpen(dc
->path
))
99 ret
= PATH_Arc(dc
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
,0);
100 else if (dc
->funcs
->pArc
)
101 ret
= dc
->funcs
->pArc(dc
->physDev
,left
,top
,right
,bottom
,xstart
,ystart
,xend
,yend
);
102 DC_ReleaseDCPtr( dc
);
107 /***********************************************************************
110 BOOL WINAPI
ArcTo( HDC hdc
,
112 INT right
, INT bottom
,
113 INT xstart
, INT ystart
,
116 double width
= fabs(right
-left
),
117 height
= fabs(bottom
-top
),
120 xcenter
= right
> left
? left
+xradius
: right
+xradius
,
121 ycenter
= bottom
> top
? top
+yradius
: bottom
+yradius
,
124 DC
* dc
= DC_GetDCUpdate( hdc
);
125 if(!dc
) return FALSE
;
127 if(PATH_IsPathOpen(dc
->path
))
128 result
= PATH_Arc(dc
,left
,top
,right
,bottom
,xstart
,ystart
,xend
,yend
,-1);
129 else if(dc
->funcs
->pArcTo
)
130 result
= dc
->funcs
->pArcTo( dc
->physDev
, left
, top
, right
, bottom
,
131 xstart
, ystart
, xend
, yend
);
132 else /* We'll draw a line from the current position to the starting point of the arc, then draw the arc */
134 angle
= atan2(((ystart
-ycenter
)/height
),
135 ((xstart
-xcenter
)/width
));
136 LineTo(hdc
, GDI_ROUND(xcenter
+(cos(angle
)*xradius
)),
137 GDI_ROUND(ycenter
+(sin(angle
)*yradius
)));
138 result
= Arc(hdc
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
);
141 angle
= atan2(((yend
-ycenter
)/height
),
142 ((xend
-xcenter
)/width
));
143 dc
->CursPosX
= GDI_ROUND(xcenter
+(cos(angle
)*xradius
));
144 dc
->CursPosY
= GDI_ROUND(ycenter
+(sin(angle
)*yradius
));
146 DC_ReleaseDCPtr( dc
);
151 /***********************************************************************
154 BOOL WINAPI
Pie( HDC hdc
, INT left
, INT top
,
155 INT right
, INT bottom
, INT xstart
, INT ystart
,
159 DC
* dc
= DC_GetDCUpdate( hdc
);
160 if (!dc
) return FALSE
;
162 if(PATH_IsPathOpen(dc
->path
))
163 ret
= PATH_Arc(dc
,left
,top
,right
,bottom
,xstart
,ystart
,xend
,yend
,2);
164 else if(dc
->funcs
->pPie
)
165 ret
= dc
->funcs
->pPie(dc
->physDev
,left
,top
,right
,bottom
,xstart
,ystart
,xend
,yend
);
167 DC_ReleaseDCPtr( dc
);
172 /***********************************************************************
175 BOOL WINAPI
Chord( HDC hdc
, INT left
, INT top
,
176 INT right
, INT bottom
, INT xstart
, INT ystart
,
180 DC
* dc
= DC_GetDCUpdate( hdc
);
181 if (!dc
) return FALSE
;
183 if(PATH_IsPathOpen(dc
->path
))
184 ret
= PATH_Arc(dc
,left
,top
,right
,bottom
,xstart
,ystart
,xend
,yend
,1);
185 else if(dc
->funcs
->pChord
)
186 ret
= dc
->funcs
->pChord(dc
->physDev
,left
,top
,right
,bottom
,xstart
,ystart
,xend
,yend
);
188 DC_ReleaseDCPtr( dc
);
193 /***********************************************************************
196 BOOL WINAPI
Ellipse( HDC hdc
, INT left
, INT top
,
197 INT right
, INT bottom
)
200 DC
* dc
= DC_GetDCUpdate( hdc
);
201 if (!dc
) return FALSE
;
203 if(PATH_IsPathOpen(dc
->path
))
204 ret
= PATH_Ellipse(dc
,left
,top
,right
,bottom
);
205 else if (dc
->funcs
->pEllipse
)
206 ret
= dc
->funcs
->pEllipse(dc
->physDev
,left
,top
,right
,bottom
);
208 DC_ReleaseDCPtr( dc
);
213 /***********************************************************************
214 * Rectangle (GDI32.@)
216 BOOL WINAPI
Rectangle( HDC hdc
, INT left
, INT top
,
217 INT right
, INT bottom
)
220 DC
* dc
= DC_GetDCUpdate( hdc
);
223 if(PATH_IsPathOpen(dc
->path
))
224 ret
= PATH_Rectangle(dc
, left
, top
, right
, bottom
);
225 else if (dc
->funcs
->pRectangle
)
226 ret
= dc
->funcs
->pRectangle(dc
->physDev
,left
,top
,right
,bottom
);
227 DC_ReleaseDCPtr( dc
);
233 /***********************************************************************
234 * RoundRect (GDI32.@)
236 BOOL WINAPI
RoundRect( HDC hdc
, INT left
, INT top
, INT right
,
237 INT bottom
, INT ell_width
, INT ell_height
)
240 DC
*dc
= DC_GetDCUpdate( hdc
);
244 if(PATH_IsPathOpen(dc
->path
))
245 ret
= PATH_RoundRect(dc
,left
,top
,right
,bottom
,ell_width
,ell_height
);
246 else if (dc
->funcs
->pRoundRect
)
247 ret
= dc
->funcs
->pRoundRect(dc
->physDev
,left
,top
,right
,bottom
,ell_width
,ell_height
);
248 DC_ReleaseDCPtr( dc
);
253 /***********************************************************************
256 COLORREF WINAPI
SetPixel( HDC hdc
, INT x
, INT y
, COLORREF color
)
259 DC
* dc
= DC_GetDCUpdate( hdc
);
262 if (dc
->funcs
->pSetPixel
) ret
= dc
->funcs
->pSetPixel(dc
->physDev
,x
,y
,color
);
263 DC_ReleaseDCPtr( dc
);
268 /***********************************************************************
269 * SetPixelV (GDI32.@)
271 BOOL WINAPI
SetPixelV( HDC hdc
, INT x
, INT y
, COLORREF color
)
274 DC
* dc
= DC_GetDCUpdate( hdc
);
277 if (dc
->funcs
->pSetPixel
)
279 dc
->funcs
->pSetPixel(dc
->physDev
,x
,y
,color
);
282 DC_ReleaseDCPtr( dc
);
287 /***********************************************************************
290 COLORREF WINAPI
GetPixel( HDC hdc
, INT x
, INT y
)
292 COLORREF ret
= CLR_INVALID
;
293 DC
* dc
= DC_GetDCUpdate( hdc
);
297 /* FIXME: should this be in the graphics driver? */
298 if (PtVisible( hdc
, x
, y
))
300 if (dc
->funcs
->pGetPixel
) ret
= dc
->funcs
->pGetPixel(dc
->physDev
,x
,y
);
302 DC_ReleaseDCPtr( dc
);
308 /******************************************************************************
309 * ChoosePixelFormat [GDI32.@]
310 * Matches a pixel format to given format
313 * hdc [I] Device context to search for best pixel match
314 * ppfd [I] Pixel format for which a match is sought
317 * Success: Pixel format index closest to given format
320 INT WINAPI
ChoosePixelFormat( HDC hdc
, const PIXELFORMATDESCRIPTOR
* ppfd
)
323 DC
* dc
= DC_GetDCPtr( hdc
);
325 TRACE("(%p,%p)\n",hdc
,ppfd
);
329 if (!dc
->funcs
->pChoosePixelFormat
) FIXME(" :stub\n");
330 else ret
= dc
->funcs
->pChoosePixelFormat(dc
->physDev
,ppfd
);
332 DC_ReleaseDCPtr( dc
);
337 /******************************************************************************
338 * SetPixelFormat [GDI32.@]
339 * Sets pixel format of device context
342 * hdc [I] Device context to search for best pixel match
343 * iPixelFormat [I] Pixel format index
344 * ppfd [I] Pixel format for which a match is sought
350 BOOL WINAPI
SetPixelFormat( HDC hdc
, INT iPixelFormat
,
351 const PIXELFORMATDESCRIPTOR
*ppfd
)
354 DC
* dc
= DC_GetDCPtr( hdc
);
356 TRACE("(%p,%d,%p)\n",hdc
,iPixelFormat
,ppfd
);
360 if (!dc
->funcs
->pSetPixelFormat
) FIXME(" :stub\n");
361 else bRet
= dc
->funcs
->pSetPixelFormat(dc
->physDev
,iPixelFormat
,ppfd
);
363 DC_ReleaseDCPtr( dc
);
368 /******************************************************************************
369 * GetPixelFormat [GDI32.@]
370 * Gets index of pixel format of DC
373 * hdc [I] Device context whose pixel format index is sought
376 * Success: Currently selected pixel format
379 INT WINAPI
GetPixelFormat( HDC hdc
)
382 DC
* dc
= DC_GetDCPtr( hdc
);
388 if (!dc
->funcs
->pGetPixelFormat
) FIXME(" :stub\n");
389 else ret
= dc
->funcs
->pGetPixelFormat(dc
->physDev
);
391 DC_ReleaseDCPtr( dc
);
396 /******************************************************************************
397 * DescribePixelFormat [GDI32.@]
398 * Gets info about pixel format from DC
401 * hdc [I] Device context
402 * iPixelFormat [I] Pixel format selector
403 * nBytes [I] Size of buffer
404 * ppfd [O] Pointer to structure to receive pixel format data
407 * Success: Maximum pixel format index of the device context
410 INT WINAPI
DescribePixelFormat( HDC hdc
, INT iPixelFormat
, UINT nBytes
,
411 LPPIXELFORMATDESCRIPTOR ppfd
)
414 DC
* dc
= DC_GetDCPtr( hdc
);
416 TRACE("(%p,%d,%d,%p): stub\n",hdc
,iPixelFormat
,nBytes
,ppfd
);
420 if (!dc
->funcs
->pDescribePixelFormat
)
423 ppfd
->nSize
= nBytes
;
427 else ret
= dc
->funcs
->pDescribePixelFormat(dc
->physDev
,iPixelFormat
,nBytes
,ppfd
);
429 DC_ReleaseDCPtr( dc
);
434 /******************************************************************************
435 * SwapBuffers [GDI32.@]
436 * Exchanges front and back buffers of window
439 * hdc [I] Device context whose buffers get swapped
445 BOOL WINAPI
SwapBuffers( HDC hdc
)
448 DC
* dc
= DC_GetDCPtr( hdc
);
452 if (!dc
) return TRUE
;
454 if (!dc
->funcs
->pSwapBuffers
)
459 else bRet
= dc
->funcs
->pSwapBuffers(dc
->physDev
);
461 DC_ReleaseDCPtr( dc
);
466 /***********************************************************************
469 BOOL WINAPI
PaintRgn( HDC hdc
, HRGN hrgn
)
472 DC
* dc
= DC_GetDCUpdate( hdc
);
475 if (dc
->funcs
->pPaintRgn
) ret
= dc
->funcs
->pPaintRgn(dc
->physDev
,hrgn
);
476 DC_ReleaseDCPtr( dc
);
482 /***********************************************************************
485 BOOL WINAPI
FillRgn( HDC hdc
, HRGN hrgn
, HBRUSH hbrush
)
489 DC
* dc
= DC_GetDCUpdate( hdc
);
491 if (!dc
) return FALSE
;
492 if(dc
->funcs
->pFillRgn
)
493 retval
= dc
->funcs
->pFillRgn(dc
->physDev
, hrgn
, hbrush
);
494 else if ((prevBrush
= SelectObject( hdc
, hbrush
)))
496 retval
= PaintRgn( hdc
, hrgn
);
497 SelectObject( hdc
, prevBrush
);
499 DC_ReleaseDCPtr( dc
);
504 /***********************************************************************
507 BOOL WINAPI
FrameRgn( HDC hdc
, HRGN hrgn
, HBRUSH hbrush
,
508 INT nWidth
, INT nHeight
)
511 DC
*dc
= DC_GetDCUpdate( hdc
);
513 if (!dc
) return FALSE
;
514 if(dc
->funcs
->pFrameRgn
)
515 ret
= dc
->funcs
->pFrameRgn( dc
->physDev
, hrgn
, hbrush
, nWidth
, nHeight
);
518 HRGN tmp
= CreateRectRgn( 0, 0, 0, 0 );
521 if (REGION_FrameRgn( tmp
, hrgn
, nWidth
, nHeight
))
523 FillRgn( hdc
, tmp
, hbrush
);
529 DC_ReleaseDCPtr( dc
);
534 /***********************************************************************
535 * InvertRgn (GDI32.@)
537 BOOL WINAPI
InvertRgn( HDC hdc
, HRGN hrgn
)
542 DC
*dc
= DC_GetDCUpdate( hdc
);
543 if (!dc
) return FALSE
;
545 if(dc
->funcs
->pInvertRgn
)
546 retval
= dc
->funcs
->pInvertRgn( dc
->physDev
, hrgn
);
549 prevBrush
= SelectObject( hdc
, GetStockObject(BLACK_BRUSH
) );
550 prevROP
= SetROP2( hdc
, R2_NOT
);
551 retval
= PaintRgn( hdc
, hrgn
);
552 SelectObject( hdc
, prevBrush
);
553 SetROP2( hdc
, prevROP
);
555 DC_ReleaseDCPtr( dc
);
560 /**********************************************************************
563 BOOL WINAPI
Polyline( HDC hdc
, const POINT
* pt
, INT count
)
566 DC
* dc
= DC_GetDCUpdate( hdc
);
569 if (PATH_IsPathOpen(dc
->path
)) ret
= PATH_Polyline(dc
, pt
, count
);
570 else if (dc
->funcs
->pPolyline
) ret
= dc
->funcs
->pPolyline(dc
->physDev
,pt
,count
);
571 DC_ReleaseDCPtr( dc
);
576 /**********************************************************************
577 * PolylineTo (GDI32.@)
579 BOOL WINAPI
PolylineTo( HDC hdc
, const POINT
* pt
, DWORD cCount
)
581 DC
* dc
= DC_GetDCUpdate( hdc
);
584 if(!dc
) return FALSE
;
586 if(PATH_IsPathOpen(dc
->path
))
587 ret
= PATH_PolylineTo(dc
, pt
, cCount
);
589 else if(dc
->funcs
->pPolylineTo
)
590 ret
= dc
->funcs
->pPolylineTo(dc
->physDev
, pt
, cCount
);
592 else { /* do it using Polyline */
593 POINT
*pts
= HeapAlloc( GetProcessHeap(), 0,
594 sizeof(POINT
) * (cCount
+ 1) );
597 pts
[0].x
= dc
->CursPosX
;
598 pts
[0].y
= dc
->CursPosY
;
599 memcpy( pts
+ 1, pt
, sizeof(POINT
) * cCount
);
600 ret
= Polyline( hdc
, pts
, cCount
+ 1 );
601 HeapFree( GetProcessHeap(), 0, pts
);
605 dc
->CursPosX
= pt
[cCount
-1].x
;
606 dc
->CursPosY
= pt
[cCount
-1].y
;
608 DC_ReleaseDCPtr( dc
);
613 /**********************************************************************
616 BOOL WINAPI
Polygon( HDC hdc
, const POINT
* pt
, INT count
)
619 DC
* dc
= DC_GetDCUpdate( hdc
);
622 if (PATH_IsPathOpen(dc
->path
)) ret
= PATH_Polygon(dc
, pt
, count
);
623 else if (dc
->funcs
->pPolygon
) ret
= dc
->funcs
->pPolygon(dc
->physDev
,pt
,count
);
624 DC_ReleaseDCPtr( dc
);
630 /**********************************************************************
631 * PolyPolygon (GDI32.@)
633 BOOL WINAPI
PolyPolygon( HDC hdc
, const POINT
* pt
, const INT
* counts
,
637 DC
* dc
= DC_GetDCUpdate( hdc
);
640 if (PATH_IsPathOpen(dc
->path
)) ret
= PATH_PolyPolygon(dc
, pt
, counts
, polygons
);
641 else if (dc
->funcs
->pPolyPolygon
) ret
= dc
->funcs
->pPolyPolygon(dc
->physDev
,pt
,counts
,polygons
);
642 DC_ReleaseDCPtr( dc
);
647 /**********************************************************************
648 * PolyPolyline (GDI32.@)
650 BOOL WINAPI
PolyPolyline( HDC hdc
, const POINT
* pt
, const DWORD
* counts
,
654 DC
* dc
= DC_GetDCUpdate( hdc
);
657 if (PATH_IsPathOpen(dc
->path
)) ret
= PATH_PolyPolyline(dc
, pt
, counts
, polylines
);
658 else if (dc
->funcs
->pPolyPolyline
) ret
= dc
->funcs
->pPolyPolyline(dc
->physDev
,pt
,counts
,polylines
);
659 DC_ReleaseDCPtr( dc
);
664 /**********************************************************************
665 * ExtFloodFill (GDI32.@)
667 BOOL WINAPI
ExtFloodFill( HDC hdc
, INT x
, INT y
, COLORREF color
,
671 DC
* dc
= DC_GetDCUpdate( hdc
);
674 if (dc
->funcs
->pExtFloodFill
) ret
= dc
->funcs
->pExtFloodFill(dc
->physDev
,x
,y
,color
,fillType
);
675 DC_ReleaseDCPtr( dc
);
681 /**********************************************************************
682 * FloodFill (GDI32.@)
684 BOOL WINAPI
FloodFill( HDC hdc
, INT x
, INT y
, COLORREF color
)
686 return ExtFloodFill( hdc
, x
, y
, color
, FLOODFILLBORDER
);
690 /******************************************************************************
691 * PolyBezier [GDI32.@]
692 * Draws one or more Bezier curves
695 * hDc [I] Handle to device context
696 * lppt [I] Pointer to endpoints and control points
697 * cPoints [I] Count of endpoints and control points
703 BOOL WINAPI
PolyBezier( HDC hdc
, const POINT
* lppt
, DWORD cPoints
)
708 /* cPoints must be 3 * n + 1 (where n>=1) */
709 if (cPoints
== 1 || (cPoints
% 3) != 1) return FALSE
;
711 dc
= DC_GetDCUpdate( hdc
);
712 if(!dc
) return FALSE
;
714 if(PATH_IsPathOpen(dc
->path
))
715 ret
= PATH_PolyBezier(dc
, lppt
, cPoints
);
716 else if (dc
->funcs
->pPolyBezier
)
717 ret
= dc
->funcs
->pPolyBezier(dc
->physDev
, lppt
, cPoints
);
718 else /* We'll convert it into line segments and draw them using Polyline */
723 if ((Pts
= GDI_Bezier( lppt
, cPoints
, &nOut
)))
725 TRACE("Pts = %p, no = %d\n", Pts
, nOut
);
726 ret
= Polyline( dc
->hSelf
, Pts
, nOut
);
727 HeapFree( GetProcessHeap(), 0, Pts
);
731 DC_ReleaseDCPtr( dc
);
735 /******************************************************************************
736 * PolyBezierTo [GDI32.@]
737 * Draws one or more Bezier curves
740 * hDc [I] Handle to device context
741 * lppt [I] Pointer to endpoints and control points
742 * cPoints [I] Count of endpoints and control points
748 BOOL WINAPI
PolyBezierTo( HDC hdc
, const POINT
* lppt
, DWORD cPoints
)
753 /* cbPoints must be 3 * n (where n>=1) */
754 if (!cPoints
|| (cPoints
% 3) != 0) return FALSE
;
756 dc
= DC_GetDCUpdate( hdc
);
757 if(!dc
) return FALSE
;
759 if(PATH_IsPathOpen(dc
->path
))
760 ret
= PATH_PolyBezierTo(dc
, lppt
, cPoints
);
761 else if(dc
->funcs
->pPolyBezierTo
)
762 ret
= dc
->funcs
->pPolyBezierTo(dc
->physDev
, lppt
, cPoints
);
763 else { /* We'll do it using PolyBezier */
765 pt
= HeapAlloc( GetProcessHeap(), 0, sizeof(POINT
) * (cPoints
+ 1) );
766 if(!pt
) return FALSE
;
767 pt
[0].x
= dc
->CursPosX
;
768 pt
[0].y
= dc
->CursPosY
;
769 memcpy(pt
+ 1, lppt
, sizeof(POINT
) * cPoints
);
770 ret
= PolyBezier(dc
->hSelf
, pt
, cPoints
+1);
771 HeapFree( GetProcessHeap(), 0, pt
);
774 dc
->CursPosX
= lppt
[cPoints
-1].x
;
775 dc
->CursPosY
= lppt
[cPoints
-1].y
;
777 DC_ReleaseDCPtr( dc
);
781 /***********************************************************************
784 BOOL WINAPI
AngleArc(HDC hdc
, INT x
, INT y
, DWORD dwRadius
, FLOAT eStartAngle
, FLOAT eSweepAngle
)
786 INT x1
,y1
,x2
,y2
, arcdir
;
790 if( (signed int)dwRadius
< 0 )
793 dc
= DC_GetDCUpdate( hdc
);
794 if(!dc
) return FALSE
;
796 /* Calculate the end point */
797 x2
= x
+ cos((eStartAngle
+eSweepAngle
)*M_PI
/180) * dwRadius
;
798 y2
= y
- sin((eStartAngle
+eSweepAngle
)*M_PI
/180) * dwRadius
;
800 if(!PATH_IsPathOpen(dc
->path
) && dc
->funcs
->pAngleArc
)
801 result
= dc
->funcs
->pAngleArc( dc
->physDev
, x
, y
, dwRadius
, eStartAngle
, eSweepAngle
);
802 else { /* do it using ArcTo */
803 x1
= x
+ cos(eStartAngle
*M_PI
/180) * dwRadius
;
804 y1
= y
- sin(eStartAngle
*M_PI
/180) * dwRadius
;
806 arcdir
= SetArcDirection( hdc
, eSweepAngle
>= 0 ? AD_COUNTERCLOCKWISE
: AD_CLOCKWISE
);
807 result
= ArcTo( hdc
, x
-dwRadius
, y
-dwRadius
, x
+dwRadius
, y
+dwRadius
,
809 SetArcDirection( hdc
, arcdir
);
815 DC_ReleaseDCPtr( dc
);
819 /***********************************************************************
822 BOOL WINAPI
PolyDraw(HDC hdc
, const POINT
*lppt
, const BYTE
*lpbTypes
,
827 POINT
* line_pts
= NULL
, * bzr_pts
= NULL
, bzr
[4];
828 INT i
, num_pts
, num_bzr_pts
, space
, size
;
830 dc
= DC_GetDCUpdate( hdc
);
831 if(!dc
) return FALSE
;
833 if( PATH_IsPathOpen( dc
->path
) )
834 result
= PATH_PolyDraw(dc
, lppt
, lpbTypes
, cCount
);
835 else if(dc
->funcs
->pPolyDraw
)
836 result
= dc
->funcs
->pPolyDraw( dc
->physDev
, lppt
, lpbTypes
, cCount
);
838 /* check for valid point types */
839 for(i
= 0; i
< cCount
; i
++) {
840 switch(lpbTypes
[i
]) {
842 case PT_LINETO
| PT_CLOSEFIGURE
:
846 if((i
+ 2 < cCount
) && (lpbTypes
[i
+ 1] == PT_BEZIERTO
) &&
847 ((lpbTypes
[i
+ 2] & ~PT_CLOSEFIGURE
) == PT_BEZIERTO
)){
856 space
= cCount
+ 300;
857 line_pts
= HeapAlloc(GetProcessHeap(), 0, space
* sizeof(POINT
));
860 line_pts
[0].x
= dc
->CursPosX
;
861 line_pts
[0].y
= dc
->CursPosY
;
863 for(i
= 0; i
< cCount
; i
++) {
864 switch(lpbTypes
[i
]) {
867 Polyline(dc
->hSelf
, line_pts
, num_pts
);
869 line_pts
[num_pts
++] = lppt
[i
];
872 case (PT_LINETO
| PT_CLOSEFIGURE
):
873 line_pts
[num_pts
++] = lppt
[i
];
876 bzr
[0].x
= line_pts
[num_pts
- 1].x
;
877 bzr
[0].y
= line_pts
[num_pts
- 1].y
;
878 memcpy(&bzr
[1], &lppt
[i
], 3 * sizeof(POINT
));
880 bzr_pts
= GDI_Bezier(bzr
, 4, &num_bzr_pts
);
882 size
= num_pts
+ (cCount
- i
) + num_bzr_pts
;
885 line_pts
= HeapReAlloc(GetProcessHeap(), 0, line_pts
,
886 space
* sizeof(POINT
));
888 memcpy(&line_pts
[num_pts
], &bzr_pts
[1],
889 (num_bzr_pts
- 1) * sizeof(POINT
));
890 num_pts
+= num_bzr_pts
- 1;
891 HeapFree(GetProcessHeap(), 0, bzr_pts
);
898 if(lpbTypes
[i
] & PT_CLOSEFIGURE
)
899 line_pts
[num_pts
++] = line_pts
[0];
903 Polyline(dc
->hSelf
, line_pts
, num_pts
);
905 MoveToEx(dc
->hSelf
, line_pts
[num_pts
- 1].x
, line_pts
[num_pts
- 1].y
, NULL
);
911 DC_ReleaseDCPtr( dc
);
916 /**********************************************************************
919 BOOL WINAPI
LineDDA(INT nXStart
, INT nYStart
, INT nXEnd
, INT nYEnd
,
920 LINEDDAPROC callback
, LPARAM lParam
)
922 INT xadd
= 1, yadd
= 1;
925 INT dx
= nXEnd
- nXStart
;
926 INT dy
= nYEnd
- nYStart
;
938 if (dx
> dy
) /* line is "more horizontal" */
940 err
= 2*dy
- dx
; erradd
= 2*dy
- 2*dx
;
941 for(cnt
= 0;cnt
<= dx
; cnt
++)
943 callback(nXStart
,nYStart
,lParam
);
953 else /* line is "more vertical" */
955 err
= 2*dx
- dy
; erradd
= 2*dx
- 2*dy
;
956 for(cnt
= 0;cnt
<= dy
; cnt
++)
958 callback(nXStart
,nYStart
,lParam
);
972 /******************************************************************
974 * *Very* simple bezier drawing code,
976 * It uses a recursive algorithm to divide the curve in a series
977 * of straight line segements. Not ideal but for me sufficient.
978 * If you are in need for something better look for some incremental
981 * 7 July 1998 Rein Klazes
985 * some macro definitions for bezier drawing
987 * to avoid truncation errors the coordinates are
988 * shifted upwards. When used in drawing they are
989 * shifted down again, including correct rounding
990 * and avoiding floating point arithmetic
991 * 4 bits should allow 27 bits coordinates which I saw
992 * somewhere in the win32 doc's
996 #define BEZIERSHIFTBITS 4
997 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
998 #define BEZIERPIXEL BEZIERSHIFTUP(1)
999 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
1000 /* maximum depth of recursion */
1001 #define BEZIERMAXDEPTH 8
1003 /* size of array to store points on */
1004 /* enough for one curve */
1005 #define BEZIER_INITBUFSIZE (150)
1007 /* calculate Bezier average, in this case the middle
1008 * correctly rounded...
1011 #define BEZIERMIDDLE(Mid, P1, P2) \
1012 (Mid).x=((P1).x+(P2).x + 1)/2;\
1013 (Mid).y=((P1).y+(P2).y + 1)/2;
1015 /**********************************************************
1016 * BezierCheck helper function to check
1017 * that recursion can be terminated
1018 * Points[0] and Points[3] are begin and endpoint
1019 * Points[1] and Points[2] are control points
1020 * level is the recursion depth
1021 * returns true if the recusion can be terminated
1023 static BOOL
BezierCheck( int level
, POINT
*Points
)
1026 dx
=Points
[3].x
-Points
[0].x
;
1027 dy
=Points
[3].y
-Points
[0].y
;
1028 if(abs(dy
)<=abs(dx
)){/* shallow line */
1029 /* check that control points are between begin and end */
1030 if(Points
[1].x
< Points
[0].x
){
1031 if(Points
[1].x
< Points
[3].x
)
1034 if(Points
[1].x
> Points
[3].x
)
1036 if(Points
[2].x
< Points
[0].x
){
1037 if(Points
[2].x
< Points
[3].x
)
1040 if(Points
[2].x
> Points
[3].x
)
1042 dx
=BEZIERSHIFTDOWN(dx
);
1043 if(!dx
) return TRUE
;
1044 if(abs(Points
[1].y
-Points
[0].y
-(dy
/dx
)*
1045 BEZIERSHIFTDOWN(Points
[1].x
-Points
[0].x
)) > BEZIERPIXEL
||
1046 abs(Points
[2].y
-Points
[0].y
-(dy
/dx
)*
1047 BEZIERSHIFTDOWN(Points
[2].x
-Points
[0].x
)) > BEZIERPIXEL
)
1051 }else{ /* steep line */
1052 /* check that control points are between begin and end */
1053 if(Points
[1].y
< Points
[0].y
){
1054 if(Points
[1].y
< Points
[3].y
)
1057 if(Points
[1].y
> Points
[3].y
)
1059 if(Points
[2].y
< Points
[0].y
){
1060 if(Points
[2].y
< Points
[3].y
)
1063 if(Points
[2].y
> Points
[3].y
)
1065 dy
=BEZIERSHIFTDOWN(dy
);
1066 if(!dy
) return TRUE
;
1067 if(abs(Points
[1].x
-Points
[0].x
-(dx
/dy
)*
1068 BEZIERSHIFTDOWN(Points
[1].y
-Points
[0].y
)) > BEZIERPIXEL
||
1069 abs(Points
[2].x
-Points
[0].x
-(dx
/dy
)*
1070 BEZIERSHIFTDOWN(Points
[2].y
-Points
[0].y
)) > BEZIERPIXEL
)
1077 /* Helper for GDI_Bezier.
1078 * Just handles one Bezier, so Points should point to four POINTs
1080 static void GDI_InternalBezier( POINT
*Points
, POINT
**PtsOut
, INT
*dwOut
,
1081 INT
*nPtsOut
, INT level
)
1083 if(*nPtsOut
== *dwOut
) {
1085 *PtsOut
= HeapReAlloc( GetProcessHeap(), 0, *PtsOut
,
1086 *dwOut
* sizeof(POINT
) );
1089 if(!level
|| BezierCheck(level
, Points
)) {
1091 (*PtsOut
)[0].x
= BEZIERSHIFTDOWN(Points
[0].x
);
1092 (*PtsOut
)[0].y
= BEZIERSHIFTDOWN(Points
[0].y
);
1095 (*PtsOut
)[*nPtsOut
].x
= BEZIERSHIFTDOWN(Points
[3].x
);
1096 (*PtsOut
)[*nPtsOut
].y
= BEZIERSHIFTDOWN(Points
[3].y
);
1099 POINT Points2
[4]; /* for the second recursive call */
1100 Points2
[3]=Points
[3];
1101 BEZIERMIDDLE(Points2
[2], Points
[2], Points
[3]);
1102 BEZIERMIDDLE(Points2
[0], Points
[1], Points
[2]);
1103 BEZIERMIDDLE(Points2
[1],Points2
[0],Points2
[2]);
1105 BEZIERMIDDLE(Points
[1], Points
[0], Points
[1]);
1106 BEZIERMIDDLE(Points
[2], Points
[1], Points2
[0]);
1107 BEZIERMIDDLE(Points
[3], Points
[2], Points2
[1]);
1109 Points2
[0]=Points
[3];
1111 /* do the two halves */
1112 GDI_InternalBezier(Points
, PtsOut
, dwOut
, nPtsOut
, level
-1);
1113 GDI_InternalBezier(Points2
, PtsOut
, dwOut
, nPtsOut
, level
-1);
1119 /***********************************************************************
1120 * GDI_Bezier [INTERNAL]
1121 * Calculate line segments that approximate -what microsoft calls- a bezier
1123 * The routine recursively divides the curve in two parts until a straight
1128 * Points [I] Ptr to count POINTs which are the end and control points
1129 * of the set of Bezier curves to flatten.
1130 * count [I] Number of Points. Must be 3n+1.
1131 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
1136 * Ptr to an array of POINTs that contain the lines that approximinate the
1137 * Beziers. The array is allocated on the process heap and it is the caller's
1138 * responsibility to HeapFree it. [this is not a particularly nice interface
1139 * but since we can't know in advance how many points will generate, the
1140 * alternative would be to call the function twice, once to determine the size
1141 * and a second time to do the work - I decided this was too much of a pain].
1143 POINT
*GDI_Bezier( const POINT
*Points
, INT count
, INT
*nPtsOut
)
1146 INT Bezier
, dwOut
= BEZIER_INITBUFSIZE
, i
;
1148 if (count
== 1 || (count
- 1) % 3 != 0) {
1149 ERR("Invalid no. of points %d\n", count
);
1153 out
= HeapAlloc( GetProcessHeap(), 0, dwOut
* sizeof(POINT
));
1154 for(Bezier
= 0; Bezier
< (count
-1)/3; Bezier
++) {
1156 memcpy(ptBuf
, Points
+ Bezier
* 3, sizeof(POINT
) * 4);
1157 for(i
= 0; i
< 4; i
++) {
1158 ptBuf
[i
].x
= BEZIERSHIFTUP(ptBuf
[i
].x
);
1159 ptBuf
[i
].y
= BEZIERSHIFTUP(ptBuf
[i
].y
);
1161 GDI_InternalBezier( ptBuf
, &out
, &dwOut
, nPtsOut
, BEZIERMAXDEPTH
);
1163 TRACE("Produced %d points\n", *nPtsOut
);
1167 /******************************************************************************
1168 * GdiGradientFill (GDI32.@)
1170 * FIXME: we don't support the Alpha channel properly
1172 BOOL WINAPI
GdiGradientFill( HDC hdc
, TRIVERTEX
*vert_array
, ULONG nvert
,
1173 void * grad_array
, ULONG ngrad
, ULONG mode
)
1177 TRACE("vert_array:0x%08lx nvert:%d grad_array:0x%08lx ngrad:%d\n",
1178 (long)vert_array
, nvert
, (long)grad_array
, ngrad
);
1182 case GRADIENT_FILL_RECT_H
:
1183 for(i
= 0; i
< ngrad
; i
++)
1185 GRADIENT_RECT
*rect
= ((GRADIENT_RECT
*)grad_array
) + i
;
1186 TRIVERTEX
*v1
= vert_array
+ rect
->UpperLeft
;
1187 TRIVERTEX
*v2
= vert_array
+ rect
->LowerRight
;
1188 int y1
= v1
->y
< v2
->y
? v1
->y
: v2
->y
;
1189 int y2
= v2
->y
> v1
->y
? v2
->y
: v1
->y
;
1198 for (x
= 0; x
< dx
; x
++)
1203 hPen
= CreatePen( PS_SOLID
, 1, RGB(
1204 (v1
->Red
* (dx
- x
) + v2
->Red
* x
) / dx
>> 8,
1205 (v1
->Green
* (dx
- x
) + v2
->Green
* x
) / dx
>> 8,
1206 (v1
->Blue
* (dx
- x
) + v2
->Blue
* x
) / dx
>> 8));
1207 hOldPen
= SelectObject( hdc
, hPen
);
1208 pts
[0].x
= v1
->x
+ x
;
1210 pts
[1].x
= v1
->x
+ x
;
1212 Polyline( hdc
, &pts
[0], 2 );
1213 DeleteObject( SelectObject(hdc
, hOldPen
) );
1217 case GRADIENT_FILL_RECT_V
:
1218 for(i
= 0; i
< ngrad
; i
++)
1220 GRADIENT_RECT
*rect
= ((GRADIENT_RECT
*)grad_array
) + i
;
1221 TRIVERTEX
*v1
= vert_array
+ rect
->UpperLeft
;
1222 TRIVERTEX
*v2
= vert_array
+ rect
->LowerRight
;
1223 int x1
= v1
->x
< v2
->x
? v1
->x
: v2
->x
;
1224 int x2
= v2
->x
> v1
->x
? v2
->x
: v1
->x
;
1233 for (y
= 0; y
< dy
; y
++)
1238 hPen
= CreatePen( PS_SOLID
, 1, RGB(
1239 (v1
->Red
* (dy
- y
) + v2
->Red
* y
) / dy
>> 8,
1240 (v1
->Green
* (dy
- y
) + v2
->Green
* y
) / dy
>> 8,
1241 (v1
->Blue
* (dy
- y
) + v2
->Blue
* y
) / dy
>> 8));
1242 hOldPen
= SelectObject( hdc
, hPen
);
1244 pts
[0].y
= v1
->y
+ y
;
1246 pts
[1].y
= v1
->y
+ y
;
1247 Polyline( hdc
, &pts
[0], 2 );
1248 DeleteObject( SelectObject(hdc
, hOldPen
) );
1252 case GRADIENT_FILL_TRIANGLE
:
1253 for (i
= 0; i
< ngrad
; i
++)
1255 GRADIENT_TRIANGLE
*tri
= ((GRADIENT_TRIANGLE
*)grad_array
) + i
;
1256 TRIVERTEX
*v1
= vert_array
+ tri
->Vertex1
;
1257 TRIVERTEX
*v2
= vert_array
+ tri
->Vertex2
;
1258 TRIVERTEX
*v3
= vert_array
+ tri
->Vertex3
;
1262 { TRIVERTEX
*t
= v1
; v1
= v2
; v2
= t
; }
1265 TRIVERTEX
*t
= v2
; v2
= v3
; v3
= t
;
1267 { t
= v1
; v1
= v2
; v2
= t
; }
1269 /* v1->y <= v2->y <= v3->y */
1272 for (y
= 0; y
< dy
; y
++)
1274 /* v1->y <= y < v3->y */
1275 TRIVERTEX
*v
= y
< (v2
->y
- v1
->y
) ? v1
: v3
;
1276 /* (v->y <= y < v2->y) || (v2->y <= y < v->y) */
1277 int dy2
= v2
->y
- v
->y
;
1278 int y2
= y
+ v1
->y
- v
->y
;
1280 int x1
= (v3
->x
* y
+ v1
->x
* (dy
- y
)) / dy
;
1281 int x2
= (v2
->x
* y2
+ v
-> x
* (dy2
- y2
)) / dy2
;
1282 int r1
= (v3
->Red
* y
+ v1
->Red
* (dy
- y
)) / dy
;
1283 int r2
= (v2
->Red
* y2
+ v
-> Red
* (dy2
- y2
)) / dy2
;
1284 int g1
= (v3
->Green
* y
+ v1
->Green
* (dy
- y
)) / dy
;
1285 int g2
= (v2
->Green
* y2
+ v
-> Green
* (dy2
- y2
)) / dy2
;
1286 int b1
= (v3
->Blue
* y
+ v1
->Blue
* (dy
- y
)) / dy
;
1287 int b2
= (v2
->Blue
* y2
+ v
-> Blue
* (dy2
- y2
)) / dy2
;
1293 for (x
= 0; x
< dx
; x
++)
1294 SetPixel (hdc
, x
+ x1
, y
+ v1
->y
, RGB(
1295 (r1
* (dx
- x
) + r2
* x
) / dx
>> 8,
1296 (g1
* (dx
- x
) + g2
* x
) / dx
>> 8,
1297 (b1
* (dx
- x
) + b2
* x
) / dx
>> 8));
1302 for (x
= 0; x
< dx
; x
++)
1303 SetPixel (hdc
, x
+ x2
, y
+ v1
->y
, RGB(
1304 (r2
* (dx
- x
) + r1
* x
) / dx
>> 8,
1305 (g2
* (dx
- x
) + g1
* x
) / dx
>> 8,
1306 (b2
* (dx
- x
) + b1
* x
) / dx
>> 8));