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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.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 GDI_ReleaseObj( hdc
);
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 GDI_ReleaseObj( hdc
);
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 GDI_ReleaseObj( hdc
);
107 /***********************************************************************
110 BOOL WINAPI
ArcTo( HDC hdc
,
112 INT right
, INT bottom
,
113 INT xstart
, INT ystart
,
117 DC
* dc
= DC_GetDCUpdate( hdc
);
118 if(!dc
) return FALSE
;
120 if(dc
->funcs
->pArcTo
)
122 result
= dc
->funcs
->pArcTo( dc
->physDev
, left
, top
, right
, bottom
,
123 xstart
, ystart
, xend
, yend
);
124 GDI_ReleaseObj( hdc
);
127 GDI_ReleaseObj( hdc
);
130 * According to the documentation, a line is drawn from the current
131 * position to the starting point of the arc.
133 LineTo(hdc
, xstart
, ystart
);
135 * Then the arc is drawn.
137 result
= Arc(hdc
, left
, top
, right
, bottom
, xstart
, ystart
, xend
, yend
);
139 * If no error occurred, the current position is moved to the ending
142 if (result
) MoveToEx(hdc
, xend
, yend
, NULL
);
147 /***********************************************************************
150 BOOL WINAPI
Pie( HDC hdc
, INT left
, INT top
,
151 INT right
, INT bottom
, INT xstart
, INT ystart
,
155 DC
* dc
= DC_GetDCUpdate( hdc
);
156 if (!dc
) return FALSE
;
158 if(PATH_IsPathOpen(dc
->path
))
159 ret
= PATH_Arc(dc
,left
,top
,right
,bottom
,xstart
,ystart
,xend
,yend
,2);
160 else if(dc
->funcs
->pPie
)
161 ret
= dc
->funcs
->pPie(dc
->physDev
,left
,top
,right
,bottom
,xstart
,ystart
,xend
,yend
);
163 GDI_ReleaseObj( hdc
);
168 /***********************************************************************
171 BOOL WINAPI
Chord( HDC hdc
, INT left
, INT top
,
172 INT right
, INT bottom
, INT xstart
, INT ystart
,
176 DC
* dc
= DC_GetDCUpdate( hdc
);
177 if (!dc
) return FALSE
;
179 if(PATH_IsPathOpen(dc
->path
))
180 ret
= PATH_Arc(dc
,left
,top
,right
,bottom
,xstart
,ystart
,xend
,yend
,1);
181 else if(dc
->funcs
->pChord
)
182 ret
= dc
->funcs
->pChord(dc
->physDev
,left
,top
,right
,bottom
,xstart
,ystart
,xend
,yend
);
184 GDI_ReleaseObj( hdc
);
189 /***********************************************************************
192 BOOL WINAPI
Ellipse( HDC hdc
, INT left
, INT top
,
193 INT right
, INT bottom
)
196 DC
* dc
= DC_GetDCUpdate( hdc
);
197 if (!dc
) return FALSE
;
199 if(PATH_IsPathOpen(dc
->path
))
200 ret
= PATH_Ellipse(dc
,left
,top
,right
,bottom
);
201 else if (dc
->funcs
->pEllipse
)
202 ret
= dc
->funcs
->pEllipse(dc
->physDev
,left
,top
,right
,bottom
);
204 GDI_ReleaseObj( hdc
);
209 /***********************************************************************
210 * Rectangle (GDI32.@)
212 BOOL WINAPI
Rectangle( HDC hdc
, INT left
, INT top
,
213 INT right
, INT bottom
)
216 DC
* dc
= DC_GetDCUpdate( hdc
);
219 if(PATH_IsPathOpen(dc
->path
))
220 ret
= PATH_Rectangle(dc
, left
, top
, right
, bottom
);
221 else if (dc
->funcs
->pRectangle
)
222 ret
= dc
->funcs
->pRectangle(dc
->physDev
,left
,top
,right
,bottom
);
223 GDI_ReleaseObj( hdc
);
229 /***********************************************************************
230 * RoundRect (GDI32.@)
232 BOOL WINAPI
RoundRect( HDC hdc
, INT left
, INT top
, INT right
,
233 INT bottom
, INT ell_width
, INT ell_height
)
236 DC
*dc
= DC_GetDCUpdate( hdc
);
240 if(PATH_IsPathOpen(dc
->path
))
241 ret
= PATH_RoundRect(dc
,left
,top
,right
,bottom
,ell_width
,ell_height
);
242 else if (dc
->funcs
->pRoundRect
)
243 ret
= dc
->funcs
->pRoundRect(dc
->physDev
,left
,top
,right
,bottom
,ell_width
,ell_height
);
244 GDI_ReleaseObj( hdc
);
249 /***********************************************************************
252 COLORREF WINAPI
SetPixel( HDC hdc
, INT x
, INT y
, COLORREF color
)
255 DC
* dc
= DC_GetDCUpdate( hdc
);
258 if (dc
->funcs
->pSetPixel
) ret
= dc
->funcs
->pSetPixel(dc
->physDev
,x
,y
,color
);
259 GDI_ReleaseObj( hdc
);
264 /***********************************************************************
265 * SetPixelV (GDI32.@)
267 BOOL WINAPI
SetPixelV( HDC hdc
, INT x
, INT y
, COLORREF color
)
270 DC
* dc
= DC_GetDCUpdate( hdc
);
273 if (dc
->funcs
->pSetPixel
)
275 dc
->funcs
->pSetPixel(dc
->physDev
,x
,y
,color
);
278 GDI_ReleaseObj( hdc
);
283 /***********************************************************************
286 COLORREF WINAPI
GetPixel( HDC hdc
, INT x
, INT y
)
288 COLORREF ret
= CLR_INVALID
;
289 DC
* dc
= DC_GetDCUpdate( hdc
);
293 /* FIXME: should this be in the graphics driver? */
294 if (PtVisible( hdc
, x
, y
))
296 if (dc
->funcs
->pGetPixel
) ret
= dc
->funcs
->pGetPixel(dc
->physDev
,x
,y
);
298 GDI_ReleaseObj( hdc
);
304 /******************************************************************************
305 * ChoosePixelFormat [GDI32.@]
306 * Matches a pixel format to given format
309 * hdc [I] Device context to search for best pixel match
310 * ppfd [I] Pixel format for which a match is sought
313 * Success: Pixel format index closest to given format
316 INT WINAPI
ChoosePixelFormat( HDC hdc
, const LPPIXELFORMATDESCRIPTOR ppfd
)
319 DC
* dc
= DC_GetDCPtr( hdc
);
321 TRACE("(%08x,%p)\n",hdc
,ppfd
);
325 if (!dc
->funcs
->pChoosePixelFormat
) FIXME(" :stub\n");
326 else ret
= dc
->funcs
->pChoosePixelFormat(dc
->physDev
,ppfd
);
328 GDI_ReleaseObj( hdc
);
333 /******************************************************************************
334 * SetPixelFormat [GDI32.@]
335 * Sets pixel format of device context
338 * hdc [I] Device context to search for best pixel match
339 * iPixelFormat [I] Pixel format index
340 * ppfd [I] Pixel format for which a match is sought
344 BOOL WINAPI
SetPixelFormat( HDC hdc
, INT iPixelFormat
,
345 const PIXELFORMATDESCRIPTOR
*ppfd
)
348 DC
* dc
= DC_GetDCPtr( hdc
);
350 TRACE("(%d,%d,%p)\n",hdc
,iPixelFormat
,ppfd
);
354 if (!dc
->funcs
->pSetPixelFormat
) FIXME(" :stub\n");
355 else bRet
= dc
->funcs
->pSetPixelFormat(dc
->physDev
,iPixelFormat
,ppfd
);
357 GDI_ReleaseObj( hdc
);
362 /******************************************************************************
363 * GetPixelFormat [GDI32.@]
364 * Gets index of pixel format of DC
367 * hdc [I] Device context whose pixel format index is sought
370 * Success: Currently selected pixel format
373 INT WINAPI
GetPixelFormat( HDC hdc
)
376 DC
* dc
= DC_GetDCPtr( hdc
);
378 TRACE("(%08x)\n",hdc
);
382 if (!dc
->funcs
->pGetPixelFormat
) FIXME(" :stub\n");
383 else ret
= dc
->funcs
->pGetPixelFormat(dc
->physDev
);
385 GDI_ReleaseObj( hdc
);
390 /******************************************************************************
391 * DescribePixelFormat [GDI32.@]
392 * Gets info about pixel format from DC
395 * hdc [I] Device context
396 * iPixelFormat [I] Pixel format selector
397 * nBytes [I] Size of buffer
398 * ppfd [O] Pointer to structure to receive pixel format data
401 * Success: Maximum pixel format index of the device context
404 INT WINAPI
DescribePixelFormat( HDC hdc
, INT iPixelFormat
, UINT nBytes
,
405 LPPIXELFORMATDESCRIPTOR ppfd
)
408 DC
* dc
= DC_GetDCPtr( hdc
);
410 TRACE("(%08x,%d,%d,%p): stub\n",hdc
,iPixelFormat
,nBytes
,ppfd
);
414 if (!dc
->funcs
->pDescribePixelFormat
)
417 ppfd
->nSize
= nBytes
;
421 else ret
= dc
->funcs
->pDescribePixelFormat(dc
->physDev
,iPixelFormat
,nBytes
,ppfd
);
423 GDI_ReleaseObj( hdc
);
428 /******************************************************************************
429 * SwapBuffers [GDI32.@]
430 * Exchanges front and back buffers of window
433 * hdc [I] Device context whose buffers get swapped
437 BOOL WINAPI
SwapBuffers( HDC hdc
)
440 DC
* dc
= DC_GetDCPtr( hdc
);
442 TRACE("(%08x)\n",hdc
);
444 if (!dc
) return TRUE
;
446 if (!dc
->funcs
->pSwapBuffers
)
451 else bRet
= dc
->funcs
->pSwapBuffers(dc
->physDev
);
453 GDI_ReleaseObj( hdc
);
458 /***********************************************************************
461 BOOL WINAPI
PaintRgn( HDC hdc
, HRGN hrgn
)
464 DC
* dc
= DC_GetDCUpdate( hdc
);
467 if (dc
->funcs
->pPaintRgn
) ret
= dc
->funcs
->pPaintRgn(dc
->physDev
,hrgn
);
468 GDI_ReleaseObj( hdc
);
474 /***********************************************************************
477 BOOL WINAPI
FillRgn( HDC hdc
, HRGN hrgn
, HBRUSH hbrush
)
481 DC
* dc
= DC_GetDCUpdate( hdc
);
483 if (!dc
) return FALSE
;
484 if(dc
->funcs
->pFillRgn
)
485 retval
= dc
->funcs
->pFillRgn(dc
->physDev
, hrgn
, hbrush
);
486 else if ((prevBrush
= SelectObject( hdc
, hbrush
)))
488 retval
= PaintRgn( hdc
, hrgn
);
489 SelectObject( hdc
, prevBrush
);
491 GDI_ReleaseObj( hdc
);
496 /***********************************************************************
499 BOOL WINAPI
FrameRgn( HDC hdc
, HRGN hrgn
, HBRUSH hbrush
,
500 INT nWidth
, INT nHeight
)
503 DC
*dc
= DC_GetDCUpdate( hdc
);
505 if (!dc
) return FALSE
;
506 if(dc
->funcs
->pFrameRgn
)
507 ret
= dc
->funcs
->pFrameRgn( dc
->physDev
, hrgn
, hbrush
, nWidth
, nHeight
);
510 HRGN tmp
= CreateRectRgn( 0, 0, 0, 0 );
513 if (REGION_FrameRgn( tmp
, hrgn
, nWidth
, nHeight
))
515 FillRgn( hdc
, tmp
, hbrush
);
521 GDI_ReleaseObj( hdc
);
526 /***********************************************************************
527 * InvertRgn (GDI32.@)
529 BOOL WINAPI
InvertRgn( HDC hdc
, HRGN hrgn
)
534 DC
*dc
= DC_GetDCUpdate( hdc
);
535 if (!dc
) return FALSE
;
537 if(dc
->funcs
->pInvertRgn
)
538 retval
= dc
->funcs
->pInvertRgn( dc
->physDev
, hrgn
);
541 prevBrush
= SelectObject( hdc
, GetStockObject(BLACK_BRUSH
) );
542 prevROP
= SetROP2( hdc
, R2_NOT
);
543 retval
= PaintRgn( hdc
, hrgn
);
544 SelectObject( hdc
, prevBrush
);
545 SetROP2( hdc
, prevROP
);
547 GDI_ReleaseObj( hdc
);
552 /**********************************************************************
555 BOOL WINAPI
Polyline( HDC hdc
, const POINT
* pt
, INT count
)
558 DC
* dc
= DC_GetDCUpdate( hdc
);
561 if (PATH_IsPathOpen(dc
->path
)) ret
= PATH_Polyline(dc
, pt
, count
);
562 else if (dc
->funcs
->pPolyline
) ret
= dc
->funcs
->pPolyline(dc
->physDev
,pt
,count
);
563 GDI_ReleaseObj( hdc
);
568 /**********************************************************************
569 * PolylineTo (GDI32.@)
571 BOOL WINAPI
PolylineTo( HDC hdc
, const POINT
* pt
, DWORD cCount
)
573 DC
* dc
= DC_GetDCUpdate( hdc
);
576 if(!dc
) return FALSE
;
578 if(PATH_IsPathOpen(dc
->path
))
579 ret
= PATH_PolylineTo(dc
, pt
, cCount
);
581 else if(dc
->funcs
->pPolylineTo
)
582 ret
= dc
->funcs
->pPolylineTo(dc
->physDev
, pt
, cCount
);
584 else { /* do it using Polyline */
585 POINT
*pts
= HeapAlloc( GetProcessHeap(), 0,
586 sizeof(POINT
) * (cCount
+ 1) );
589 pts
[0].x
= dc
->CursPosX
;
590 pts
[0].y
= dc
->CursPosY
;
591 memcpy( pts
+ 1, pt
, sizeof(POINT
) * cCount
);
592 ret
= Polyline( hdc
, pts
, cCount
+ 1 );
593 HeapFree( GetProcessHeap(), 0, pts
);
597 dc
->CursPosX
= pt
[cCount
-1].x
;
598 dc
->CursPosY
= pt
[cCount
-1].y
;
600 GDI_ReleaseObj( hdc
);
605 /**********************************************************************
608 BOOL WINAPI
Polygon( HDC hdc
, const POINT
* pt
, INT count
)
611 DC
* dc
= DC_GetDCUpdate( hdc
);
614 if (PATH_IsPathOpen(dc
->path
)) ret
= PATH_Polygon(dc
, pt
, count
);
615 else if (dc
->funcs
->pPolygon
) ret
= dc
->funcs
->pPolygon(dc
->physDev
,pt
,count
);
616 GDI_ReleaseObj( hdc
);
622 /**********************************************************************
623 * PolyPolygon (GDI32.@)
625 BOOL WINAPI
PolyPolygon( HDC hdc
, const POINT
* pt
, const INT
* counts
,
629 DC
* dc
= DC_GetDCUpdate( hdc
);
632 if (PATH_IsPathOpen(dc
->path
)) ret
= PATH_PolyPolygon(dc
, pt
, counts
, polygons
);
633 else if (dc
->funcs
->pPolyPolygon
) ret
= dc
->funcs
->pPolyPolygon(dc
->physDev
,pt
,counts
,polygons
);
634 GDI_ReleaseObj( hdc
);
639 /**********************************************************************
640 * PolyPolyline (GDI32.@)
642 BOOL WINAPI
PolyPolyline( HDC hdc
, const POINT
* pt
, const DWORD
* counts
,
646 DC
* dc
= DC_GetDCUpdate( hdc
);
649 if (PATH_IsPathOpen(dc
->path
)) ret
= PATH_PolyPolyline(dc
, pt
, counts
, polylines
);
650 else if (dc
->funcs
->pPolyPolyline
) ret
= dc
->funcs
->pPolyPolyline(dc
->physDev
,pt
,counts
,polylines
);
651 GDI_ReleaseObj( hdc
);
656 /**********************************************************************
657 * ExtFloodFill (GDI32.@)
659 BOOL WINAPI
ExtFloodFill( HDC hdc
, INT x
, INT y
, COLORREF color
,
663 DC
* dc
= DC_GetDCUpdate( hdc
);
666 if (dc
->funcs
->pExtFloodFill
) ret
= dc
->funcs
->pExtFloodFill(dc
->physDev
,x
,y
,color
,fillType
);
667 GDI_ReleaseObj( hdc
);
673 /**********************************************************************
674 * FloodFill (GDI32.@)
676 BOOL WINAPI
FloodFill( HDC hdc
, INT x
, INT y
, COLORREF color
)
678 return ExtFloodFill( hdc
, x
, y
, color
, FLOODFILLBORDER
);
682 /******************************************************************************
683 * PolyBezier [GDI32.@]
684 * Draws one or more Bezier curves
687 * hDc [I] Handle to device context
688 * lppt [I] Pointer to endpoints and control points
689 * cPoints [I] Count of endpoints and control points
693 BOOL WINAPI
PolyBezier( HDC hdc
, const POINT
* lppt
, DWORD cPoints
)
696 DC
* dc
= DC_GetDCUpdate( hdc
);
698 if(!dc
) return FALSE
;
700 if(PATH_IsPathOpen(dc
->path
))
701 ret
= PATH_PolyBezier(dc
, lppt
, cPoints
);
702 else if (dc
->funcs
->pPolyBezier
)
703 ret
= dc
->funcs
->pPolyBezier(dc
->physDev
, lppt
, cPoints
);
704 else /* We'll convert it into line segments and draw them using Polyline */
709 if ((Pts
= GDI_Bezier( lppt
, cPoints
, &nOut
)))
711 TRACE("Pts = %p, no = %d\n", Pts
, nOut
);
712 ret
= Polyline( dc
->hSelf
, Pts
, nOut
);
713 HeapFree( GetProcessHeap(), 0, Pts
);
717 GDI_ReleaseObj( hdc
);
721 /******************************************************************************
722 * PolyBezierTo [GDI32.@]
723 * Draws one or more Bezier curves
726 * hDc [I] Handle to device context
727 * lppt [I] Pointer to endpoints and control points
728 * cPoints [I] Count of endpoints and control points
732 BOOL WINAPI
PolyBezierTo( HDC hdc
, const POINT
* lppt
, DWORD cPoints
)
734 DC
* dc
= DC_GetDCUpdate( hdc
);
737 if(!dc
) return FALSE
;
739 if(PATH_IsPathOpen(dc
->path
))
740 ret
= PATH_PolyBezierTo(dc
, lppt
, cPoints
);
741 else if(dc
->funcs
->pPolyBezierTo
)
742 ret
= dc
->funcs
->pPolyBezierTo(dc
->physDev
, lppt
, cPoints
);
743 else { /* We'll do it using PolyBezier */
745 pt
= HeapAlloc( GetProcessHeap(), 0, sizeof(POINT
) * (cPoints
+ 1) );
746 if(!pt
) return FALSE
;
747 pt
[0].x
= dc
->CursPosX
;
748 pt
[0].y
= dc
->CursPosY
;
749 memcpy(pt
+ 1, lppt
, sizeof(POINT
) * cPoints
);
750 ret
= PolyBezier(dc
->hSelf
, pt
, cPoints
+1);
751 HeapFree( GetProcessHeap(), 0, pt
);
754 dc
->CursPosX
= lppt
[cPoints
-1].x
;
755 dc
->CursPosY
= lppt
[cPoints
-1].y
;
757 GDI_ReleaseObj( hdc
);
761 /***********************************************************************
764 BOOL WINAPI
AngleArc(HDC hdc
, INT x
, INT y
, DWORD dwRadius
, FLOAT eStartAngle
, FLOAT eSweepAngle
)
766 INT x1
,y1
,x2
,y2
, arcdir
;
770 if( (signed int)dwRadius
< 0 )
773 dc
= DC_GetDCUpdate( hdc
);
774 if(!dc
) return FALSE
;
776 if(dc
->funcs
->pAngleArc
)
778 result
= dc
->funcs
->pAngleArc( dc
->physDev
, x
, y
, dwRadius
, eStartAngle
, eSweepAngle
);
780 GDI_ReleaseObj( hdc
);
783 GDI_ReleaseObj( hdc
);
785 /* AngleArc always works counterclockwise */
786 arcdir
= GetArcDirection( hdc
);
787 SetArcDirection( hdc
, AD_COUNTERCLOCKWISE
);
789 x1
= x
+ cos(eStartAngle
*M_PI
/180) * dwRadius
;
790 y1
= y
- sin(eStartAngle
*M_PI
/180) * dwRadius
;
791 x2
= x
+ cos((eStartAngle
+eSweepAngle
)*M_PI
/180) * dwRadius
;
792 y2
= x
- sin((eStartAngle
+eSweepAngle
)*M_PI
/180) * dwRadius
;
794 LineTo( hdc
, x1
, y1
);
795 if( eSweepAngle
>= 0 )
796 result
= Arc( hdc
, x
-dwRadius
, y
-dwRadius
, x
+dwRadius
, y
+dwRadius
,
799 result
= Arc( hdc
, x
-dwRadius
, y
-dwRadius
, x
+dwRadius
, y
+dwRadius
,
802 if( result
) MoveToEx( hdc
, x2
, y2
, NULL
);
803 SetArcDirection( hdc
, arcdir
);
807 /***********************************************************************
810 BOOL WINAPI
PolyDraw(HDC hdc
, const POINT
*lppt
, const BYTE
*lpbTypes
,
818 dc
= DC_GetDCUpdate( hdc
);
819 if(!dc
) return FALSE
;
821 if(dc
->funcs
->pPolyDraw
)
823 result
= dc
->funcs
->pPolyDraw( dc
->physDev
, lppt
, lpbTypes
, cCount
);
824 GDI_ReleaseObj( hdc
);
827 GDI_ReleaseObj( hdc
);
829 /* check for each bezierto if there are two more points */
830 for( i
= 0; i
< cCount
; i
++ )
831 if( lpbTypes
[i
] != PT_MOVETO
&&
832 lpbTypes
[i
] & PT_BEZIERTO
)
840 /* if no moveto occurs, we will close the figure here */
841 lastmove
.x
= dc
->CursPosX
;
842 lastmove
.y
= dc
->CursPosY
;
845 for( i
= 0; i
< cCount
; i
++ )
847 if( lpbTypes
[i
] == PT_MOVETO
)
849 MoveToEx( hdc
, lppt
[i
].x
, lppt
[i
].y
, NULL
);
850 lastmove
.x
= dc
->CursPosX
;
851 lastmove
.y
= dc
->CursPosY
;
853 else if( lpbTypes
[i
] & PT_LINETO
)
854 LineTo( hdc
, lppt
[i
].x
, lppt
[i
].y
);
855 else if( lpbTypes
[i
] & PT_BEZIERTO
)
857 PolyBezierTo( hdc
, &lppt
[i
], 3 );
863 if( lpbTypes
[i
] & PT_CLOSEFIGURE
)
865 if( PATH_IsPathOpen( dc
->path
) )
868 LineTo( hdc
, lastmove
.x
, lastmove
.y
);
875 /******************************************************************
877 * *Very* simple bezier drawing code,
879 * It uses a recursive algorithm to divide the curve in a series
880 * of straight line segements. Not ideal but for me sufficient.
881 * If you are in need for something better look for some incremental
884 * 7 July 1998 Rein Klazes
888 * some macro definitions for bezier drawing
890 * to avoid trucation errors the coordinates are
891 * shifted upwards. When used in drawing they are
892 * shifted down again, including correct rounding
893 * and avoiding floating point arithmatic
894 * 4 bits should allow 27 bits coordinates which I saw
895 * somewere in the win32 doc's
899 #define BEZIERSHIFTBITS 4
900 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
901 #define BEZIERPIXEL BEZIERSHIFTUP(1)
902 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
903 /* maximum depth of recursion */
904 #define BEZIERMAXDEPTH 8
906 /* size of array to store points on */
907 /* enough for one curve */
908 #define BEZIER_INITBUFSIZE (150)
910 /* calculate Bezier average, in this case the middle
911 * correctly rounded...
914 #define BEZIERMIDDLE(Mid, P1, P2) \
915 (Mid).x=((P1).x+(P2).x + 1)/2;\
916 (Mid).y=((P1).y+(P2).y + 1)/2;
918 /**********************************************************
919 * BezierCheck helper function to check
920 * that recursion can be terminated
921 * Points[0] and Points[3] are begin and endpoint
922 * Points[1] and Points[2] are control points
923 * level is the recursion depth
924 * returns true if the recusion can be terminated
926 static BOOL
BezierCheck( int level
, POINT
*Points
)
929 dx
=Points
[3].x
-Points
[0].x
;
930 dy
=Points
[3].y
-Points
[0].y
;
931 if(abs(dy
)<=abs(dx
)){/* shallow line */
932 /* check that control points are between begin and end */
933 if(Points
[1].x
< Points
[0].x
){
934 if(Points
[1].x
< Points
[3].x
)
937 if(Points
[1].x
> Points
[3].x
)
939 if(Points
[2].x
< Points
[0].x
){
940 if(Points
[2].x
< Points
[3].x
)
943 if(Points
[2].x
> Points
[3].x
)
945 dx
=BEZIERSHIFTDOWN(dx
);
947 if(abs(Points
[1].y
-Points
[0].y
-(dy
/dx
)*
948 BEZIERSHIFTDOWN(Points
[1].x
-Points
[0].x
)) > BEZIERPIXEL
||
949 abs(Points
[2].y
-Points
[0].y
-(dy
/dx
)*
950 BEZIERSHIFTDOWN(Points
[2].x
-Points
[0].x
)) > BEZIERPIXEL
)
954 }else{ /* steep line */
955 /* check that control points are between begin and end */
956 if(Points
[1].y
< Points
[0].y
){
957 if(Points
[1].y
< Points
[3].y
)
960 if(Points
[1].y
> Points
[3].y
)
962 if(Points
[2].y
< Points
[0].y
){
963 if(Points
[2].y
< Points
[3].y
)
966 if(Points
[2].y
> Points
[3].y
)
968 dy
=BEZIERSHIFTDOWN(dy
);
970 if(abs(Points
[1].x
-Points
[0].x
-(dx
/dy
)*
971 BEZIERSHIFTDOWN(Points
[1].y
-Points
[0].y
)) > BEZIERPIXEL
||
972 abs(Points
[2].x
-Points
[0].x
-(dx
/dy
)*
973 BEZIERSHIFTDOWN(Points
[2].y
-Points
[0].y
)) > BEZIERPIXEL
)
980 /* Helper for GDI_Bezier.
981 * Just handles one Bezier, so Points should point to four POINTs
983 static void GDI_InternalBezier( POINT
*Points
, POINT
**PtsOut
, INT
*dwOut
,
984 INT
*nPtsOut
, INT level
)
986 if(*nPtsOut
== *dwOut
) {
988 *PtsOut
= HeapReAlloc( GetProcessHeap(), 0, *PtsOut
,
989 *dwOut
* sizeof(POINT
) );
992 if(!level
|| BezierCheck(level
, Points
)) {
994 (*PtsOut
)[0].x
= BEZIERSHIFTDOWN(Points
[0].x
);
995 (*PtsOut
)[0].y
= BEZIERSHIFTDOWN(Points
[0].y
);
998 (*PtsOut
)[*nPtsOut
].x
= BEZIERSHIFTDOWN(Points
[3].x
);
999 (*PtsOut
)[*nPtsOut
].y
= BEZIERSHIFTDOWN(Points
[3].y
);
1002 POINT Points2
[4]; /* for the second recursive call */
1003 Points2
[3]=Points
[3];
1004 BEZIERMIDDLE(Points2
[2], Points
[2], Points
[3]);
1005 BEZIERMIDDLE(Points2
[0], Points
[1], Points
[2]);
1006 BEZIERMIDDLE(Points2
[1],Points2
[0],Points2
[2]);
1008 BEZIERMIDDLE(Points
[1], Points
[0], Points
[1]);
1009 BEZIERMIDDLE(Points
[2], Points
[1], Points2
[0]);
1010 BEZIERMIDDLE(Points
[3], Points
[2], Points2
[1]);
1012 Points2
[0]=Points
[3];
1014 /* do the two halves */
1015 GDI_InternalBezier(Points
, PtsOut
, dwOut
, nPtsOut
, level
-1);
1016 GDI_InternalBezier(Points2
, PtsOut
, dwOut
, nPtsOut
, level
-1);
1022 /***********************************************************************
1023 * GDI_Bezier [INTERNAL]
1024 * Calculate line segments that approximate -what microsoft calls- a bezier
1026 * The routine recursively divides the curve in two parts until a straight
1031 * Points [I] Ptr to count POINTs which are the end and control points
1032 * of the set of Bezier curves to flatten.
1033 * count [I] Number of Points. Must be 3n+1.
1034 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
1039 * Ptr to an array of POINTs that contain the lines that approximinate the
1040 * Beziers. The array is allocated on the process heap and it is the caller's
1041 * responsibility to HeapFree it. [this is not a particularly nice interface
1042 * but since we can't know in advance how many points will generate, the
1043 * alternative would be to call the function twice, once to determine the size
1044 * and a second time to do the work - I decided this was too much of a pain].
1046 POINT
*GDI_Bezier( const POINT
*Points
, INT count
, INT
*nPtsOut
)
1049 INT Bezier
, dwOut
= BEZIER_INITBUFSIZE
, i
;
1051 if((count
- 1) % 3 != 0) {
1052 ERR("Invalid no. of points\n");
1056 out
= HeapAlloc( GetProcessHeap(), 0, dwOut
* sizeof(POINT
));
1057 for(Bezier
= 0; Bezier
< (count
-1)/3; Bezier
++) {
1059 memcpy(ptBuf
, Points
+ Bezier
* 3, sizeof(POINT
) * 4);
1060 for(i
= 0; i
< 4; i
++) {
1061 ptBuf
[i
].x
= BEZIERSHIFTUP(ptBuf
[i
].x
);
1062 ptBuf
[i
].y
= BEZIERSHIFTUP(ptBuf
[i
].y
);
1064 GDI_InternalBezier( ptBuf
, &out
, &dwOut
, nPtsOut
, BEZIERMAXDEPTH
);
1066 TRACE("Produced %d points\n", *nPtsOut
);