mfmediaengine: Remove unnecessary import library.
[wine.git] / dlls / win32u / painting.c
blob5a080bb5a0be01194a65f5890805ca2eb4c44b0c
1 /*
2 * GDI drawing functions.
4 * Copyright 1993, 1994 Alexandre Julliard
5 * Copyright 1997 Bertho A. Stultiens
6 * 1999 Huw D M Davies
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
23 #if 0
24 #pragma makedep unix
25 #endif
27 #include <stdarg.h>
28 #include <string.h>
29 #include <stdlib.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winerror.h"
35 #include "ntgdi_private.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
41 /***********************************************************************
42 * null driver fallback implementations
45 BOOL CDECL nulldrv_AngleArc( PHYSDEV dev, INT x, INT y, DWORD radius, FLOAT start, FLOAT sweep )
47 DC *dc = get_physdev_dc( dev );
48 INT x1 = GDI_ROUND( x + cos( start * M_PI / 180 ) * radius );
49 INT y1 = GDI_ROUND( y - sin( start * M_PI / 180 ) * radius );
50 INT x2 = GDI_ROUND( x + cos( (start + sweep) * M_PI / 180) * radius );
51 INT y2 = GDI_ROUND( y - sin( (start + sweep) * M_PI / 180) * radius );
52 INT arcdir = dc->attr->arc_direction;
53 BOOL ret;
54 dc->attr->arc_direction = sweep >= 0 ? AD_COUNTERCLOCKWISE : AD_CLOCKWISE;
55 ret = NtGdiArcInternal( NtGdiArcTo, dev->hdc, x - radius, y - radius, x + radius, y + radius,
56 x1, y1, x2, y2 );
57 dc->attr->arc_direction = arcdir;
58 return ret;
61 BOOL CDECL nulldrv_ArcTo( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
62 INT xstart, INT ystart, INT xend, INT yend )
64 INT width = abs( right - left );
65 INT height = abs( bottom - top );
66 double xradius = width / 2.0;
67 double yradius = height / 2.0;
68 double xcenter = right > left ? left + xradius : right + xradius;
69 double ycenter = bottom > top ? top + yradius : bottom + yradius;
70 double angle;
72 if (!height || !width) return FALSE;
73 /* draw a line from the current position to the starting point of the arc, then draw the arc */
74 angle = atan2( (ystart - ycenter) / height, (xstart - xcenter) / width );
75 NtGdiLineTo( dev->hdc, GDI_ROUND( xcenter + cos(angle) * xradius ),
76 GDI_ROUND( ycenter + sin(angle) * yradius ));
77 return NtGdiArcInternal( NtGdiArc, dev->hdc, left, top, right, bottom,
78 xstart, ystart, xend, yend );
81 BOOL CDECL nulldrv_FillRgn( PHYSDEV dev, HRGN rgn, HBRUSH brush )
83 BOOL ret = FALSE;
84 HBRUSH prev;
86 if ((prev = NtGdiSelectBrush( dev->hdc, brush )))
88 PHYSDEV physdev = GET_DC_PHYSDEV( get_physdev_dc( dev ), pPaintRgn );
89 ret = physdev->funcs->pPaintRgn( physdev, rgn );
90 NtGdiSelectBrush( dev->hdc, prev );
92 return ret;
95 BOOL CDECL nulldrv_FrameRgn( PHYSDEV dev, HRGN rgn, HBRUSH brush, INT width, INT height )
97 BOOL ret = FALSE;
98 HRGN tmp = NtGdiCreateRectRgn( 0, 0, 0, 0 );
100 if (tmp)
102 if (REGION_FrameRgn( tmp, rgn, width, height ))
103 ret = NtGdiFillRgn( dev->hdc, tmp, brush );
104 NtGdiDeleteObjectApp( tmp );
106 return ret;
109 BOOL CDECL nulldrv_InvertRgn( PHYSDEV dev, HRGN rgn )
111 DC *dc = get_physdev_dc( dev );
112 INT prev_rop = dc->attr->rop_mode;
113 BOOL ret;
114 dc->attr->rop_mode = R2_NOT;
115 ret = NtGdiFillRgn( dev->hdc, rgn, get_stock_object(BLACK_BRUSH) );
116 dc->attr->rop_mode = prev_rop;
117 return ret;
120 static BOOL polyline( HDC hdc, const POINT *points, UINT count )
122 return NtGdiPolyPolyDraw( hdc, points, &count, 1, NtGdiPolyPolyline );
125 BOOL CDECL nulldrv_PolyBezier( PHYSDEV dev, const POINT *points, DWORD count )
127 BOOL ret = FALSE;
128 POINT *pts;
129 INT n;
131 if ((pts = GDI_Bezier( points, count, &n )))
133 ret = polyline( dev->hdc, pts, n );
134 free( pts );
136 return ret;
139 BOOL CDECL nulldrv_PolyBezierTo( PHYSDEV dev, const POINT *points, DWORD count )
141 DC *dc = get_nulldrv_dc( dev );
142 BOOL ret = FALSE;
143 POINT *pts = malloc( sizeof(POINT) * (count + 1) );
145 if (pts)
147 pts[0] = dc->attr->cur_pos;
148 memcpy( pts + 1, points, sizeof(POINT) * count );
149 count++;
150 ret = NtGdiPolyPolyDraw( dev->hdc, pts, (UINT *)&count, 1, NtGdiPolyBezier );
151 free( pts );
153 return ret;
156 BOOL CDECL nulldrv_PolyDraw( PHYSDEV dev, const POINT *points, const BYTE *types, DWORD count )
158 DC *dc = get_nulldrv_dc( dev );
159 POINT *line_pts = NULL, *new_line_pts, *bzr_pts = NULL, bzr[4];
160 DWORD i;
161 INT num_pts, num_bzr_pts, space, size;
163 /* check for valid point types */
164 for (i = 0; i < count; i++)
166 switch (types[i])
168 case PT_MOVETO:
169 case PT_LINETO | PT_CLOSEFIGURE:
170 case PT_LINETO:
171 break;
172 case PT_BEZIERTO:
173 if (i + 2 >= count) return FALSE;
174 if (types[i + 1] != PT_BEZIERTO) return FALSE;
175 if ((types[i + 2] & ~PT_CLOSEFIGURE) != PT_BEZIERTO) return FALSE;
176 i += 2;
177 break;
178 default:
179 return FALSE;
183 space = count + 300;
184 line_pts = malloc( space * sizeof(POINT) );
185 if (!line_pts) return FALSE;
186 num_pts = 1;
188 line_pts[0] = dc->attr->cur_pos;
189 for (i = 0; i < count; i++)
191 switch (types[i])
193 case PT_MOVETO:
194 if (num_pts >= 2) polyline( dev->hdc, line_pts, num_pts );
195 num_pts = 0;
196 line_pts[num_pts++] = points[i];
197 break;
198 case PT_LINETO:
199 case (PT_LINETO | PT_CLOSEFIGURE):
200 line_pts[num_pts++] = points[i];
201 break;
202 case PT_BEZIERTO:
203 bzr[0].x = line_pts[num_pts - 1].x;
204 bzr[0].y = line_pts[num_pts - 1].y;
205 memcpy( &bzr[1], &points[i], 3 * sizeof(POINT) );
207 if ((bzr_pts = GDI_Bezier( bzr, 4, &num_bzr_pts )))
209 size = num_pts + (count - i) + num_bzr_pts;
210 if (space < size)
212 space = size * 2;
213 new_line_pts = realloc( line_pts, space * sizeof(POINT) );
214 if (!new_line_pts)
216 free( bzr_pts );
217 free( line_pts );
218 return FALSE;
220 line_pts = new_line_pts;
222 memcpy( &line_pts[num_pts], &bzr_pts[1], (num_bzr_pts - 1) * sizeof(POINT) );
223 num_pts += num_bzr_pts - 1;
224 free( bzr_pts );
226 i += 2;
227 break;
229 if (types[i] & PT_CLOSEFIGURE) line_pts[num_pts++] = line_pts[0];
232 if (num_pts >= 2) polyline( dev->hdc, line_pts, num_pts );
233 free( line_pts );
234 return TRUE;
237 BOOL CDECL nulldrv_PolylineTo( PHYSDEV dev, const POINT *points, INT count )
239 DC *dc = get_nulldrv_dc( dev );
240 BOOL ret = FALSE;
241 POINT *pts;
243 if (!count) return FALSE;
244 if ((pts = malloc( sizeof(POINT) * (count + 1) )))
246 pts[0] = dc->attr->cur_pos;
247 memcpy( pts + 1, points, sizeof(POINT) * count );
248 ret = polyline( dev->hdc, pts, count + 1 );
249 free( pts );
251 return ret;
254 /***********************************************************************
255 * NtGdiLineTo (win32u.@)
257 BOOL WINAPI NtGdiLineTo( HDC hdc, INT x, INT y )
259 DC * dc = get_dc_ptr( hdc );
260 PHYSDEV physdev;
261 BOOL ret;
263 if(!dc) return FALSE;
265 update_dc( dc );
266 physdev = GET_DC_PHYSDEV( dc, pLineTo );
267 ret = physdev->funcs->pLineTo( physdev, x, y );
269 if(ret)
271 dc->attr->cur_pos.x = x;
272 dc->attr->cur_pos.y = y;
274 release_dc_ptr( dc );
275 return ret;
279 /***********************************************************************
280 * NtGdiMoveTo (win32u.@)
282 BOOL WINAPI NtGdiMoveTo( HDC hdc, INT x, INT y, POINT *pt )
284 BOOL ret;
285 PHYSDEV physdev;
286 DC * dc = get_dc_ptr( hdc );
288 if(!dc) return FALSE;
290 if(pt)
291 *pt = dc->attr->cur_pos;
293 dc->attr->cur_pos.x = x;
294 dc->attr->cur_pos.y = y;
296 physdev = GET_DC_PHYSDEV( dc, pMoveTo );
297 ret = physdev->funcs->pMoveTo( physdev, x, y );
298 release_dc_ptr( dc );
299 return ret;
303 /***********************************************************************
304 * NtGdiArcInternal (win32u.@)
306 BOOL WINAPI NtGdiArcInternal( UINT type, HDC hdc, INT left, INT top, INT right,
307 INT bottom, INT xstart, INT ystart, INT xend, INT yend )
309 PHYSDEV physdev;
310 BOOL ret;
311 DC *dc;
313 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
314 update_dc( dc );
316 switch (type)
318 case NtGdiArc:
319 physdev = GET_DC_PHYSDEV( dc, pArc );
320 ret = physdev->funcs->pArc( physdev, left, top, right, bottom, xstart, ystart, xend, yend );
321 break;
323 case NtGdiArcTo:
325 double width = abs( right - left );
326 double height = abs( bottom - top );
327 double xradius = width / 2;
328 double yradius = height / 2;
329 double xcenter = right > left ? left + xradius : right + xradius;
330 double ycenter = bottom > top ? top + yradius : bottom + yradius;
332 physdev = GET_DC_PHYSDEV( dc, pArcTo );
333 ret = physdev->funcs->pArcTo( physdev, left, top, right, bottom,
334 xstart, ystart, xend, yend );
335 if (ret)
337 double angle = atan2(((yend - ycenter) / height),
338 ((xend - xcenter) / width));
339 dc->attr->cur_pos.x = GDI_ROUND( xcenter + (cos( angle ) * xradius) );
340 dc->attr->cur_pos.y = GDI_ROUND( ycenter + (sin( angle ) * yradius) );
342 break;
345 case NtGdiChord:
346 physdev = GET_DC_PHYSDEV( dc, pChord );
347 ret = physdev->funcs->pChord( physdev, left, top, right, bottom,
348 xstart, ystart, xend, yend );
349 break;
351 case NtGdiPie:
352 physdev = GET_DC_PHYSDEV( dc, pPie );
353 ret = physdev->funcs->pPie( physdev, left, top, right, bottom,
354 xstart, ystart, xend, yend );
355 break;
357 default:
358 WARN( "invalid arc type %u\n", type );
359 ret = FALSE;
362 release_dc_ptr( dc );
363 return ret;
367 /***********************************************************************
368 * NtGdiEllipse (win32u.@)
370 BOOL WINAPI NtGdiEllipse( HDC hdc, INT left, INT top, INT right, INT bottom )
372 BOOL ret;
373 PHYSDEV physdev;
374 DC * dc = get_dc_ptr( hdc );
376 if (!dc) return FALSE;
377 update_dc( dc );
378 physdev = GET_DC_PHYSDEV( dc, pEllipse );
379 ret = physdev->funcs->pEllipse( physdev, left, top, right, bottom );
380 release_dc_ptr( dc );
381 return ret;
385 /***********************************************************************
386 * NtGdiRectangle (win32u.@)
388 BOOL WINAPI NtGdiRectangle( HDC hdc, INT left, INT top, INT right, INT bottom )
390 PHYSDEV physdev;
391 BOOL ret;
392 DC * dc = get_dc_ptr( hdc );
394 if (!dc) return FALSE;
395 update_dc( dc );
396 physdev = GET_DC_PHYSDEV( dc, pRectangle );
397 ret = physdev->funcs->pRectangle( physdev, left, top, right, bottom );
398 release_dc_ptr( dc );
399 return ret;
403 /***********************************************************************
404 * NtGdiRoundRect (win32u.@)
406 BOOL WINAPI NtGdiRoundRect( HDC hdc, INT left, INT top, INT right,
407 INT bottom, INT ell_width, INT ell_height )
409 PHYSDEV physdev;
410 BOOL ret;
411 DC *dc = get_dc_ptr( hdc );
413 if (!dc) return FALSE;
414 update_dc( dc );
415 physdev = GET_DC_PHYSDEV( dc, pRoundRect );
416 ret = physdev->funcs->pRoundRect( physdev, left, top, right, bottom, ell_width, ell_height );
417 release_dc_ptr( dc );
418 return ret;
421 /***********************************************************************
422 * NtGdiSetPixel (win32u.@)
424 COLORREF WINAPI NtGdiSetPixel( HDC hdc, INT x, INT y, COLORREF color )
426 PHYSDEV physdev;
427 COLORREF ret;
428 DC * dc = get_dc_ptr( hdc );
430 if (!dc) return CLR_INVALID;
431 update_dc( dc );
432 physdev = GET_DC_PHYSDEV( dc, pSetPixel );
433 ret = physdev->funcs->pSetPixel( physdev, x, y, color );
434 release_dc_ptr( dc );
435 return ret;
438 /***********************************************************************
439 * NtGdiGetPixel (win32u.@)
441 COLORREF WINAPI NtGdiGetPixel( HDC hdc, INT x, INT y )
443 PHYSDEV physdev;
444 COLORREF ret;
445 DC * dc = get_dc_ptr( hdc );
447 if (!dc) return CLR_INVALID;
448 update_dc( dc );
449 physdev = GET_DC_PHYSDEV( dc, pGetPixel );
450 ret = physdev->funcs->pGetPixel( physdev, x, y );
451 release_dc_ptr( dc );
452 return ret;
456 /******************************************************************************
457 * NtGdiSetPixelFormat (win32u.@)
459 * Probably not the correct semantics, it's supposed to be an internal backend for SetPixelFormat.
461 BOOL WINAPI NtGdiSetPixelFormat( HDC hdc, INT format )
463 DC *dc;
464 BOOL ret = TRUE;
466 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
468 if (!dc->pixel_format) dc->pixel_format = format;
469 else ret = (dc->pixel_format == format);
470 release_dc_ptr( dc );
471 return ret;
475 /******************************************************************************
476 * NtGdiDescribePixelFormat (win32u.@)
478 INT WINAPI NtGdiDescribePixelFormat( HDC hdc, INT format, UINT size, PIXELFORMATDESCRIPTOR *descr )
480 FIXME( "(%p,%d,%d,%p): stub\n", hdc, format, size, descr );
481 return 0;
485 /******************************************************************************
486 * NtGdiSwapBuffers (win32u.@)
488 BOOL WINAPI NtGdiSwapBuffers( HDC hdc )
490 FIXME( "(%p): stub\n", hdc );
491 return FALSE;
495 /***********************************************************************
496 * NtGdiFillRgn (win32u.@)
498 BOOL WINAPI NtGdiFillRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush )
500 PHYSDEV physdev;
501 BOOL retval;
502 DC * dc = get_dc_ptr( hdc );
504 if (!dc) return FALSE;
505 update_dc( dc );
506 physdev = GET_DC_PHYSDEV( dc, pFillRgn );
507 retval = physdev->funcs->pFillRgn( physdev, hrgn, hbrush );
508 release_dc_ptr( dc );
509 return retval;
513 /***********************************************************************
514 * NtGdiFrameRgn (win32u.@)
516 BOOL WINAPI NtGdiFrameRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush, INT width, INT height )
518 PHYSDEV physdev;
519 BOOL ret;
520 DC *dc = get_dc_ptr( hdc );
522 if (!dc) return FALSE;
523 update_dc( dc );
524 physdev = GET_DC_PHYSDEV( dc, pFrameRgn );
525 ret = physdev->funcs->pFrameRgn( physdev, hrgn, hbrush, width, height );
526 release_dc_ptr( dc );
527 return ret;
531 /***********************************************************************
532 * NtGdiInvertRgn (win32u.@)
534 BOOL WINAPI NtGdiInvertRgn( HDC hdc, HRGN hrgn )
536 PHYSDEV physdev;
537 BOOL ret;
538 DC *dc = get_dc_ptr( hdc );
540 if (!dc) return FALSE;
541 update_dc( dc );
542 physdev = GET_DC_PHYSDEV( dc, pInvertRgn );
543 ret = physdev->funcs->pInvertRgn( physdev, hrgn );
544 release_dc_ptr( dc );
545 return ret;
549 /**********************************************************************
550 * NtGdiPolyPolyDraw (win32u.@)
552 ULONG WINAPI NtGdiPolyPolyDraw( HDC hdc, const POINT *points, const UINT *counts,
553 DWORD count, UINT function )
555 PHYSDEV physdev;
556 ULONG ret;
557 DC *dc;
559 if (function == NtGdiPolyPolygonRgn)
560 return HandleToULong( create_polypolygon_region( points, (const INT *)counts, count,
561 HandleToULong(hdc), NULL ));
563 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
564 update_dc( dc );
566 switch (function)
568 case NtGdiPolyPolygon:
569 physdev = GET_DC_PHYSDEV( dc, pPolyPolygon );
570 ret = physdev->funcs->pPolyPolygon( physdev, points, (const INT *)counts, count );
571 break;
573 case NtGdiPolyPolyline:
574 physdev = GET_DC_PHYSDEV( dc, pPolyPolyline );
575 ret = physdev->funcs->pPolyPolyline( physdev, points, (const DWORD *)counts, count );
576 break;
578 case NtGdiPolyBezier:
579 /* *counts must be 3 * n + 1 (where n >= 1) */
580 if (count == 1 && *counts != 1 && *counts % 3 == 1)
582 physdev = GET_DC_PHYSDEV( dc, pPolyBezier );
583 ret = physdev->funcs->pPolyBezier( physdev, points, *counts );
584 if (ret) dc->attr->cur_pos = points[*counts - 1];
586 else ret = FALSE;
587 break;
589 case NtGdiPolyBezierTo:
590 if (count == 1 && *counts && *counts % 3 == 0)
592 physdev = GET_DC_PHYSDEV( dc, pPolyBezierTo );
593 ret = physdev->funcs->pPolyBezierTo( physdev, points, *counts );
594 if (ret) dc->attr->cur_pos = points[*counts - 1];
596 else ret = FALSE;
597 break;
599 case NtGdiPolylineTo:
600 if (count == 1)
602 physdev = GET_DC_PHYSDEV( dc, pPolylineTo );
603 ret = physdev->funcs->pPolylineTo( physdev, points, *counts );
604 if (ret && *counts) dc->attr->cur_pos = points[*counts - 1];
606 else ret = FALSE;
607 break;
609 default:
610 WARN( "invalid function %u\n", function );
611 ret = FALSE;
612 break;
615 release_dc_ptr( dc );
616 return ret;
619 /**********************************************************************
620 * NtGdiExtFloodFill (win32u.@)
622 BOOL WINAPI NtGdiExtFloodFill( HDC hdc, INT x, INT y, COLORREF color, UINT fill_type )
624 PHYSDEV physdev;
625 BOOL ret;
626 DC * dc = get_dc_ptr( hdc );
628 if (!dc) return FALSE;
629 update_dc( dc );
630 physdev = GET_DC_PHYSDEV( dc, pExtFloodFill );
631 ret = physdev->funcs->pExtFloodFill( physdev, x, y, color, fill_type );
632 release_dc_ptr( dc );
633 return ret;
637 /***********************************************************************
638 * NtGdiAngleArc (win32u.@)
640 BOOL WINAPI NtGdiAngleArc( HDC hdc, INT x, INT y, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle )
642 PHYSDEV physdev;
643 BOOL result;
644 DC *dc;
646 if( (signed int)dwRadius < 0 )
647 return FALSE;
649 dc = get_dc_ptr( hdc );
650 if(!dc) return FALSE;
652 update_dc( dc );
653 physdev = GET_DC_PHYSDEV( dc, pAngleArc );
654 result = physdev->funcs->pAngleArc( physdev, x, y, dwRadius, eStartAngle, eSweepAngle );
656 if (result)
658 dc->attr->cur_pos.x = GDI_ROUND( x + cos( (eStartAngle + eSweepAngle) * M_PI / 180 ) * dwRadius );
659 dc->attr->cur_pos.y = GDI_ROUND( y - sin( (eStartAngle + eSweepAngle) * M_PI / 180 ) * dwRadius );
661 release_dc_ptr( dc );
662 return result;
665 /***********************************************************************
666 * NtGdiPolyDraw (win32u.@)
668 BOOL WINAPI NtGdiPolyDraw( HDC hdc, const POINT *points, const BYTE *types, DWORD count )
670 DC *dc = get_dc_ptr( hdc );
671 PHYSDEV physdev;
672 BOOL result;
674 if(!dc) return FALSE;
676 update_dc( dc );
677 physdev = GET_DC_PHYSDEV( dc, pPolyDraw );
678 result = physdev->funcs->pPolyDraw( physdev, points, types, count );
679 if (result && count)
680 dc->attr->cur_pos = points[count - 1];
682 release_dc_ptr( dc );
683 return result;
687 /******************************************************************
689 * *Very* simple bezier drawing code,
691 * It uses a recursive algorithm to divide the curve in a series
692 * of straight line segments. Not ideal but sufficient for me.
693 * If you are in need for something better look for some incremental
694 * algorithm.
696 * 7 July 1998 Rein Klazes
700 * some macro definitions for bezier drawing
702 * to avoid truncation errors the coordinates are
703 * shifted upwards. When used in drawing they are
704 * shifted down again, including correct rounding
705 * and avoiding floating point arithmetic
706 * 4 bits should allow 27 bits coordinates which I saw
707 * somewhere in the win32 doc's
711 #define BEZIERSHIFTBITS 4
712 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
713 #define BEZIERPIXEL BEZIERSHIFTUP(1)
714 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
715 /* maximum depth of recursion */
716 #define BEZIERMAXDEPTH 8
718 /* size of array to store points on */
719 /* enough for one curve */
720 #define BEZIER_INITBUFSIZE (150)
722 /* calculate Bezier average, in this case the middle
723 * correctly rounded...
724 * */
726 #define BEZIERMIDDLE(Mid, P1, P2) \
727 (Mid).x=((P1).x+(P2).x + 1)/2;\
728 (Mid).y=((P1).y+(P2).y + 1)/2;
730 /**********************************************************
731 * BezierCheck helper function to check
732 * that recursion can be terminated
733 * Points[0] and Points[3] are begin and endpoint
734 * Points[1] and Points[2] are control points
735 * level is the recursion depth
736 * returns true if the recursion can be terminated
738 static BOOL BezierCheck( int level, POINT *Points)
740 INT dx, dy;
741 dx=Points[3].x-Points[0].x;
742 dy=Points[3].y-Points[0].y;
743 if(abs(dy)<=abs(dx)){/* shallow line */
744 /* check that control points are between begin and end */
745 if(Points[1].x < Points[0].x){
746 if(Points[1].x < Points[3].x)
747 return FALSE;
748 }else
749 if(Points[1].x > Points[3].x)
750 return FALSE;
751 if(Points[2].x < Points[0].x){
752 if(Points[2].x < Points[3].x)
753 return FALSE;
754 }else
755 if(Points[2].x > Points[3].x)
756 return FALSE;
757 dx=BEZIERSHIFTDOWN(dx);
758 if(!dx) return TRUE;
759 if(abs(Points[1].y-Points[0].y-(dy/dx)*
760 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
761 abs(Points[2].y-Points[0].y-(dy/dx)*
762 BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
763 return FALSE;
764 else
765 return TRUE;
766 }else{ /* steep line */
767 /* check that control points are between begin and end */
768 if(Points[1].y < Points[0].y){
769 if(Points[1].y < Points[3].y)
770 return FALSE;
771 }else
772 if(Points[1].y > Points[3].y)
773 return FALSE;
774 if(Points[2].y < Points[0].y){
775 if(Points[2].y < Points[3].y)
776 return FALSE;
777 }else
778 if(Points[2].y > Points[3].y)
779 return FALSE;
780 dy=BEZIERSHIFTDOWN(dy);
781 if(!dy) return TRUE;
782 if(abs(Points[1].x-Points[0].x-(dx/dy)*
783 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
784 abs(Points[2].x-Points[0].x-(dx/dy)*
785 BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
786 return FALSE;
787 else
788 return TRUE;
792 /* Helper for GDI_Bezier.
793 * Just handles one Bezier, so Points should point to four POINTs
795 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
796 INT *nPtsOut, INT level )
798 if(*nPtsOut == *dwOut) {
799 *dwOut *= 2;
800 *PtsOut = realloc( *PtsOut, *dwOut * sizeof(POINT) );
803 if(!level || BezierCheck(level, Points)) {
804 if(*nPtsOut == 0) {
805 (*PtsOut)[0].x = BEZIERSHIFTDOWN(Points[0].x);
806 (*PtsOut)[0].y = BEZIERSHIFTDOWN(Points[0].y);
807 *nPtsOut = 1;
809 (*PtsOut)[*nPtsOut].x = BEZIERSHIFTDOWN(Points[3].x);
810 (*PtsOut)[*nPtsOut].y = BEZIERSHIFTDOWN(Points[3].y);
811 (*nPtsOut) ++;
812 } else {
813 POINT Points2[4]; /* for the second recursive call */
814 Points2[3]=Points[3];
815 BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
816 BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
817 BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
819 BEZIERMIDDLE(Points[1], Points[0], Points[1]);
820 BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
821 BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
823 Points2[0]=Points[3];
825 /* do the two halves */
826 GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
827 GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
833 /***********************************************************************
834 * GDI_Bezier [INTERNAL]
835 * Calculate line segments that approximate -what microsoft calls- a bezier
836 * curve.
837 * The routine recursively divides the curve in two parts until a straight
838 * line can be drawn
840 * PARAMS
842 * Points [I] Ptr to count POINTs which are the end and control points
843 * of the set of Bezier curves to flatten.
844 * count [I] Number of Points. Must be 3n+1.
845 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
846 * lines+1).
848 * RETURNS
850 * Ptr to an array of POINTs that contain the lines that approximate the
851 * Beziers. The array is allocated on the process heap and it is the caller's
852 * responsibility to free it. [this is not a particularly nice interface
853 * but since we can't know in advance how many points we will generate, the
854 * alternative would be to call the function twice, once to determine the size
855 * and a second time to do the work - I decided this was too much of a pain].
857 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
859 POINT *out;
860 INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
862 if (count == 1 || (count - 1) % 3 != 0) {
863 ERR("Invalid no. of points %d\n", count);
864 return NULL;
866 *nPtsOut = 0;
867 out = malloc( dwOut * sizeof(POINT) );
868 for(Bezier = 0; Bezier < (count-1)/3; Bezier++) {
869 POINT ptBuf[4];
870 memcpy(ptBuf, Points + Bezier * 3, sizeof(POINT) * 4);
871 for(i = 0; i < 4; i++) {
872 ptBuf[i].x = BEZIERSHIFTUP(ptBuf[i].x);
873 ptBuf[i].y = BEZIERSHIFTUP(ptBuf[i].y);
875 GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
877 TRACE("Produced %d points\n", *nPtsOut);
878 return out;
881 /******************************************************************************
882 * NtGdiGdiGradientFill (win32u.@)
884 BOOL WINAPI NtGdiGradientFill( HDC hdc, TRIVERTEX *vert_array, ULONG nvert,
885 void *grad_array, ULONG ngrad, ULONG mode )
887 DC *dc;
888 PHYSDEV physdev;
889 BOOL ret;
890 ULONG i;
892 if (!vert_array || !nvert || !grad_array || !ngrad || mode > GRADIENT_FILL_TRIANGLE)
894 SetLastError( ERROR_INVALID_PARAMETER );
895 return FALSE;
897 for (i = 0; i < ngrad * (mode == GRADIENT_FILL_TRIANGLE ? 3 : 2); i++)
898 if (((ULONG *)grad_array)[i] >= nvert) return FALSE;
900 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
901 update_dc( dc );
902 physdev = GET_DC_PHYSDEV( dc, pGradientFill );
903 ret = physdev->funcs->pGradientFill( physdev, vert_array, nvert, grad_array, ngrad, mode );
904 release_dc_ptr( dc );
905 return ret;
908 /******************************************************************************
909 * NtGdiDrawStream (win32u.@)
911 BOOL WINAPI NtGdiDrawStream( HDC hdc, ULONG in, void *pvin )
913 FIXME("stub: %p, %d, %p\n", hdc, in, pvin);
914 return FALSE;
917 /*************************************************************************
918 * NtUserScrollDC (win32u.@)
920 BOOL WINAPI NtUserScrollDC( HDC hdc, INT dx, INT dy, const RECT *scroll, const RECT *clip,
921 HRGN ret_update_rgn, RECT *update_rect )
924 HRGN update_rgn = ret_update_rgn;
925 RECT src_rect, clip_rect, offset;
926 INT dxdev, dydev;
927 HRGN dstrgn, cliprgn, visrgn;
928 POINT org;
929 DC *dc;
930 BOOL ret;
932 TRACE( "dx,dy %d,%d scroll %s clip %s update %p rect %p\n",
933 dx, dy, wine_dbgstr_rect(scroll), wine_dbgstr_rect(clip), ret_update_rgn, update_rect );
935 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
936 org.x = dc->attr->vis_rect.left;
937 org.y = dc->attr->vis_rect.top;
938 release_dc_ptr( dc );
940 /* get the visible region */
941 visrgn = NtGdiCreateRectRgn( 0, 0, 0, 0 );
942 NtGdiGetRandomRgn( hdc, visrgn, SYSRGN );
943 if (!is_win9x()) NtGdiOffsetRgn( visrgn, -org.x, -org.y );
945 /* intersect with the clipping region if the DC has one */
946 cliprgn = NtGdiCreateRectRgn( 0, 0, 0, 0);
947 if (NtGdiGetRandomRgn( hdc, cliprgn, NTGDI_RGN_MIRROR_RTL | 1 ) != 1)
949 NtGdiDeleteObjectApp( cliprgn );
950 cliprgn = 0;
952 else NtGdiCombineRgn( visrgn, visrgn, cliprgn, RGN_AND );
954 /* only those pixels in the scroll rectangle that remain in the clipping
955 * rect are scrolled. */
956 if (clip)
957 clip_rect = *clip;
958 else
959 NtGdiGetAppClipBox( hdc, &clip_rect );
960 src_rect = clip_rect;
961 offset_rect( &clip_rect, -dx, -dy );
962 intersect_rect( &src_rect, &src_rect, &clip_rect );
964 /* if an scroll rectangle is specified, only the pixels within that
965 * rectangle are scrolled */
966 if (scroll) intersect_rect( &src_rect, &src_rect, scroll );
968 /* now convert to device coordinates */
969 NtGdiTransformPoints( hdc, (POINT *)&src_rect, (POINT *)&src_rect, 2, NtGdiLPtoDP );
970 TRACE( "source rect: %s\n", wine_dbgstr_rect(&src_rect) );
971 /* also dx and dy */
972 SetRect( &offset, 0, 0, dx, dy );
973 NtGdiTransformPoints( hdc, (POINT *)&offset, (POINT *)&offset, 2, NtGdiLPtoDP );
974 dxdev = offset.right - offset.left;
975 dydev = offset.bottom - offset.top;
977 /* now intersect with the visible region to get the pixels that will actually scroll */
978 dstrgn = NtGdiCreateRectRgn( src_rect.left, src_rect.top, src_rect.right, src_rect.bottom );
979 NtGdiCombineRgn( dstrgn, dstrgn, visrgn, RGN_AND );
980 NtGdiOffsetRgn( dstrgn, dxdev, dydev );
981 NtGdiExtSelectClipRgn( hdc, dstrgn, RGN_AND );
983 /* compute the update areas. This is the combined clip rectangle
984 * minus the scrolled region, and intersected with the visible region. */
985 if (ret_update_rgn || update_rect)
987 /* intersect clip and scroll rectangles, allowing NULL values */
988 if (scroll)
990 if (clip)
991 intersect_rect( &clip_rect, clip, scroll );
992 else
993 clip_rect = *scroll;
995 else if (clip)
996 clip_rect = *clip;
997 else
998 NtGdiGetAppClipBox( hdc, &clip_rect );
1000 /* Convert the combined clip rectangle to device coordinates */
1001 NtGdiTransformPoints( hdc, (POINT *)&clip_rect, (POINT *)&clip_rect, 2, NtGdiLPtoDP );
1002 if (update_rgn)
1003 NtGdiSetRectRgn( update_rgn, clip_rect.left, clip_rect.top,
1004 clip_rect.right, clip_rect.bottom );
1005 else
1006 update_rgn = NtGdiCreateRectRgn( clip_rect.left, clip_rect.top,
1007 clip_rect.right, clip_rect.bottom );
1009 NtGdiCombineRgn( update_rgn, update_rgn, visrgn, RGN_AND );
1010 NtGdiCombineRgn( update_rgn, update_rgn, dstrgn, RGN_DIFF );
1013 ret = user_driver->pScrollDC( hdc, dx, dy, update_rgn );
1015 if (ret && update_rect)
1017 NtGdiGetRgnBox( update_rgn, update_rect );
1018 NtGdiTransformPoints( hdc, (POINT *)&update_rect, (POINT *)&update_rect, 2, NtGdiDPtoLP );
1019 TRACE( "returning update_rect %s\n", wine_dbgstr_rect(update_rect) );
1021 if (!ret_update_rgn) NtGdiDeleteObjectApp( update_rgn );
1022 NtGdiExtSelectClipRgn( hdc, cliprgn, RGN_COPY );
1023 if (cliprgn) NtGdiDeleteObjectApp( cliprgn );
1024 NtGdiDeleteObjectApp( visrgn );
1025 NtGdiDeleteObjectApp( dstrgn );
1026 return ret;