windows.applicationmodel/tests: Use PathRemoveFileSpecW() instead of PathCchRemoveFil...
[wine.git] / dlls / win32u / painting.c
bloba5a0f2e47a510e97cc1b6d6f796c8953ba852b8a
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 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 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 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 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 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, GetStockObject(BLACK_BRUSH) );
116 dc->attr->rop_mode = prev_rop;
117 return ret;
120 static BOOL polyline( HDC hdc, const POINT *points, ULONG count )
122 return NtGdiPolyPolyDraw( hdc, points, &count, 1, NtGdiPolyPolyline );
125 BOOL 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 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, &count, 1, NtGdiPolyBezier );
151 free( pts );
153 return ret;
156 BOOL 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 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 ULONG *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, DWORD start_angle, DWORD sweep_angle )
642 FLOAT eStartAngle = *(FLOAT *)&start_angle;
643 FLOAT eSweepAngle = *(FLOAT *)&sweep_angle;
644 PHYSDEV physdev;
645 BOOL result;
646 DC *dc;
648 if( (signed int)dwRadius < 0 )
649 return FALSE;
651 dc = get_dc_ptr( hdc );
652 if(!dc) return FALSE;
654 update_dc( dc );
655 physdev = GET_DC_PHYSDEV( dc, pAngleArc );
656 result = physdev->funcs->pAngleArc( physdev, x, y, dwRadius, eStartAngle, eSweepAngle );
658 if (result)
660 dc->attr->cur_pos.x = GDI_ROUND( x + cos( (eStartAngle + eSweepAngle) * M_PI / 180 ) * dwRadius );
661 dc->attr->cur_pos.y = GDI_ROUND( y - sin( (eStartAngle + eSweepAngle) * M_PI / 180 ) * dwRadius );
663 release_dc_ptr( dc );
664 return result;
667 /***********************************************************************
668 * NtGdiPolyDraw (win32u.@)
670 BOOL WINAPI NtGdiPolyDraw( HDC hdc, const POINT *points, const BYTE *types, DWORD count )
672 DC *dc = get_dc_ptr( hdc );
673 PHYSDEV physdev;
674 BOOL result;
676 if(!dc) return FALSE;
678 update_dc( dc );
679 physdev = GET_DC_PHYSDEV( dc, pPolyDraw );
680 result = physdev->funcs->pPolyDraw( physdev, points, types, count );
681 if (result && count)
682 dc->attr->cur_pos = points[count - 1];
684 release_dc_ptr( dc );
685 return result;
689 /******************************************************************
691 * *Very* simple bezier drawing code,
693 * It uses a recursive algorithm to divide the curve in a series
694 * of straight line segments. Not ideal but sufficient for me.
695 * If you are in need for something better look for some incremental
696 * algorithm.
698 * 7 July 1998 Rein Klazes
702 * some macro definitions for bezier drawing
704 * to avoid truncation errors the coordinates are
705 * shifted upwards. When used in drawing they are
706 * shifted down again, including correct rounding
707 * and avoiding floating point arithmetic
708 * 4 bits should allow 27 bits coordinates which I saw
709 * somewhere in the win32 doc's
713 #define BEZIERSHIFTBITS 4
714 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
715 #define BEZIERPIXEL BEZIERSHIFTUP(1)
716 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
717 /* maximum depth of recursion */
718 #define BEZIERMAXDEPTH 8
720 /* size of array to store points on */
721 /* enough for one curve */
722 #define BEZIER_INITBUFSIZE (150)
724 /* calculate Bezier average, in this case the middle
725 * correctly rounded...
726 * */
728 #define BEZIERMIDDLE(Mid, P1, P2) \
729 (Mid).x=((P1).x+(P2).x + 1)/2;\
730 (Mid).y=((P1).y+(P2).y + 1)/2;
732 /**********************************************************
733 * BezierCheck helper function to check
734 * that recursion can be terminated
735 * Points[0] and Points[3] are begin and endpoint
736 * Points[1] and Points[2] are control points
737 * level is the recursion depth
738 * returns true if the recursion can be terminated
740 static BOOL BezierCheck( int level, POINT *Points)
742 INT dx, dy;
743 dx=Points[3].x-Points[0].x;
744 dy=Points[3].y-Points[0].y;
745 if(abs(dy)<=abs(dx)){/* shallow line */
746 /* check that control points are between begin and end */
747 if(Points[1].x < Points[0].x){
748 if(Points[1].x < Points[3].x)
749 return FALSE;
750 }else
751 if(Points[1].x > Points[3].x)
752 return FALSE;
753 if(Points[2].x < Points[0].x){
754 if(Points[2].x < Points[3].x)
755 return FALSE;
756 }else
757 if(Points[2].x > Points[3].x)
758 return FALSE;
759 dx=BEZIERSHIFTDOWN(dx);
760 if(!dx) return TRUE;
761 if(abs(Points[1].y-Points[0].y-(dy/dx)*
762 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
763 abs(Points[2].y-Points[0].y-(dy/dx)*
764 BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
765 return FALSE;
766 else
767 return TRUE;
768 }else{ /* steep line */
769 /* check that control points are between begin and end */
770 if(Points[1].y < Points[0].y){
771 if(Points[1].y < Points[3].y)
772 return FALSE;
773 }else
774 if(Points[1].y > Points[3].y)
775 return FALSE;
776 if(Points[2].y < Points[0].y){
777 if(Points[2].y < Points[3].y)
778 return FALSE;
779 }else
780 if(Points[2].y > Points[3].y)
781 return FALSE;
782 dy=BEZIERSHIFTDOWN(dy);
783 if(!dy) return TRUE;
784 if(abs(Points[1].x-Points[0].x-(dx/dy)*
785 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
786 abs(Points[2].x-Points[0].x-(dx/dy)*
787 BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
788 return FALSE;
789 else
790 return TRUE;
794 /* Helper for GDI_Bezier.
795 * Just handles one Bezier, so Points should point to four POINTs
797 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
798 INT *nPtsOut, INT level )
800 if(*nPtsOut == *dwOut) {
801 *dwOut *= 2;
802 *PtsOut = realloc( *PtsOut, *dwOut * sizeof(POINT) );
805 if(!level || BezierCheck(level, Points)) {
806 if(*nPtsOut == 0) {
807 (*PtsOut)[0].x = BEZIERSHIFTDOWN(Points[0].x);
808 (*PtsOut)[0].y = BEZIERSHIFTDOWN(Points[0].y);
809 *nPtsOut = 1;
811 (*PtsOut)[*nPtsOut].x = BEZIERSHIFTDOWN(Points[3].x);
812 (*PtsOut)[*nPtsOut].y = BEZIERSHIFTDOWN(Points[3].y);
813 (*nPtsOut) ++;
814 } else {
815 POINT Points2[4]; /* for the second recursive call */
816 Points2[3]=Points[3];
817 BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
818 BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
819 BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
821 BEZIERMIDDLE(Points[1], Points[0], Points[1]);
822 BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
823 BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
825 Points2[0]=Points[3];
827 /* do the two halves */
828 GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
829 GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
835 /***********************************************************************
836 * GDI_Bezier [INTERNAL]
837 * Calculate line segments that approximate -what microsoft calls- a bezier
838 * curve.
839 * The routine recursively divides the curve in two parts until a straight
840 * line can be drawn
842 * PARAMS
844 * Points [I] Ptr to count POINTs which are the end and control points
845 * of the set of Bezier curves to flatten.
846 * count [I] Number of Points. Must be 3n+1.
847 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
848 * lines+1).
850 * RETURNS
852 * Ptr to an array of POINTs that contain the lines that approximate the
853 * Beziers. The array is allocated on the process heap and it is the caller's
854 * responsibility to free it. [this is not a particularly nice interface
855 * but since we can't know in advance how many points we will generate, the
856 * alternative would be to call the function twice, once to determine the size
857 * and a second time to do the work - I decided this was too much of a pain].
859 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
861 POINT *out;
862 INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
864 if (count == 1 || (count - 1) % 3 != 0) {
865 ERR("Invalid no. of points %d\n", count);
866 return NULL;
868 *nPtsOut = 0;
869 out = malloc( dwOut * sizeof(POINT) );
870 for(Bezier = 0; Bezier < (count-1)/3; Bezier++) {
871 POINT ptBuf[4];
872 memcpy(ptBuf, Points + Bezier * 3, sizeof(POINT) * 4);
873 for(i = 0; i < 4; i++) {
874 ptBuf[i].x = BEZIERSHIFTUP(ptBuf[i].x);
875 ptBuf[i].y = BEZIERSHIFTUP(ptBuf[i].y);
877 GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
879 TRACE("Produced %d points\n", *nPtsOut);
880 return out;
883 /******************************************************************************
884 * NtGdiGdiGradientFill (win32u.@)
886 BOOL WINAPI NtGdiGradientFill( HDC hdc, TRIVERTEX *vert_array, ULONG nvert,
887 void *grad_array, ULONG ngrad, ULONG mode )
889 DC *dc;
890 PHYSDEV physdev;
891 BOOL ret;
892 ULONG i;
894 if (!vert_array || !nvert || !grad_array || !ngrad || mode > GRADIENT_FILL_TRIANGLE)
896 RtlSetLastWin32Error( ERROR_INVALID_PARAMETER );
897 return FALSE;
899 for (i = 0; i < ngrad * (mode == GRADIENT_FILL_TRIANGLE ? 3 : 2); i++)
900 if (((ULONG *)grad_array)[i] >= nvert) return FALSE;
902 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
903 update_dc( dc );
904 physdev = GET_DC_PHYSDEV( dc, pGradientFill );
905 ret = physdev->funcs->pGradientFill( physdev, vert_array, nvert, grad_array, ngrad, mode );
906 release_dc_ptr( dc );
907 return ret;
910 /******************************************************************************
911 * NtGdiDrawStream (win32u.@)
913 BOOL WINAPI NtGdiDrawStream( HDC hdc, ULONG in, void *pvin )
915 FIXME("stub: %p, %d, %p\n", hdc, (int)in, pvin);
916 return FALSE;
919 /*************************************************************************
920 * NtUserScrollDC (win32u.@)
922 BOOL WINAPI NtUserScrollDC( HDC hdc, INT dx, INT dy, const RECT *scroll, const RECT *clip,
923 HRGN ret_update_rgn, RECT *update_rect )
926 HRGN update_rgn = ret_update_rgn;
927 RECT src_rect, clip_rect, offset;
928 INT dxdev, dydev;
929 HRGN dstrgn, cliprgn, visrgn;
930 POINT org;
931 DC *dc;
932 BOOL ret;
934 TRACE( "dx,dy %d,%d scroll %s clip %s update %p rect %p\n",
935 dx, dy, wine_dbgstr_rect(scroll), wine_dbgstr_rect(clip), ret_update_rgn, update_rect );
937 if (!(dc = get_dc_ptr( hdc ))) return FALSE;
938 org.x = dc->attr->vis_rect.left;
939 org.y = dc->attr->vis_rect.top;
940 release_dc_ptr( dc );
942 /* get the visible region */
943 visrgn = NtGdiCreateRectRgn( 0, 0, 0, 0 );
944 NtGdiGetRandomRgn( hdc, visrgn, SYSRGN );
945 if (!is_win9x()) NtGdiOffsetRgn( visrgn, -org.x, -org.y );
947 /* intersect with the clipping region if the DC has one */
948 cliprgn = NtGdiCreateRectRgn( 0, 0, 0, 0);
949 if (NtGdiGetRandomRgn( hdc, cliprgn, NTGDI_RGN_MIRROR_RTL | 1 ) != 1)
951 NtGdiDeleteObjectApp( cliprgn );
952 cliprgn = 0;
954 else NtGdiCombineRgn( visrgn, visrgn, cliprgn, RGN_AND );
956 /* only those pixels in the scroll rectangle that remain in the clipping
957 * rect are scrolled. */
958 if (clip)
959 clip_rect = *clip;
960 else
961 NtGdiGetAppClipBox( hdc, &clip_rect );
962 src_rect = clip_rect;
963 OffsetRect( &clip_rect, -dx, -dy );
964 intersect_rect( &src_rect, &src_rect, &clip_rect );
966 /* if an scroll rectangle is specified, only the pixels within that
967 * rectangle are scrolled */
968 if (scroll) intersect_rect( &src_rect, &src_rect, scroll );
970 /* now convert to device coordinates */
971 NtGdiTransformPoints( hdc, (POINT *)&src_rect, (POINT *)&src_rect, 2, NtGdiLPtoDP );
972 TRACE( "source rect: %s\n", wine_dbgstr_rect(&src_rect) );
973 /* also dx and dy */
974 SetRect( &offset, 0, 0, dx, dy );
975 NtGdiTransformPoints( hdc, (POINT *)&offset, (POINT *)&offset, 2, NtGdiLPtoDP );
976 dxdev = offset.right - offset.left;
977 dydev = offset.bottom - offset.top;
979 /* now intersect with the visible region to get the pixels that will actually scroll */
980 dstrgn = NtGdiCreateRectRgn( src_rect.left, src_rect.top, src_rect.right, src_rect.bottom );
981 NtGdiCombineRgn( dstrgn, dstrgn, visrgn, RGN_AND );
982 NtGdiOffsetRgn( dstrgn, dxdev, dydev );
983 NtGdiExtSelectClipRgn( hdc, dstrgn, RGN_AND );
985 /* compute the update areas. This is the combined clip rectangle
986 * minus the scrolled region, and intersected with the visible region. */
987 if (ret_update_rgn || update_rect)
989 /* intersect clip and scroll rectangles, allowing NULL values */
990 if (scroll)
992 if (clip)
993 intersect_rect( &clip_rect, clip, scroll );
994 else
995 clip_rect = *scroll;
997 else if (clip)
998 clip_rect = *clip;
999 else
1000 NtGdiGetAppClipBox( hdc, &clip_rect );
1002 /* Convert the combined clip rectangle to device coordinates */
1003 NtGdiTransformPoints( hdc, (POINT *)&clip_rect, (POINT *)&clip_rect, 2, NtGdiLPtoDP );
1004 if (update_rgn)
1005 NtGdiSetRectRgn( update_rgn, clip_rect.left, clip_rect.top,
1006 clip_rect.right, clip_rect.bottom );
1007 else
1008 update_rgn = NtGdiCreateRectRgn( clip_rect.left, clip_rect.top,
1009 clip_rect.right, clip_rect.bottom );
1011 NtGdiCombineRgn( update_rgn, update_rgn, visrgn, RGN_AND );
1012 NtGdiCombineRgn( update_rgn, update_rgn, dstrgn, RGN_DIFF );
1015 ret = user_driver->pScrollDC( hdc, dx, dy, update_rgn );
1017 if (ret && update_rect)
1019 NtGdiGetRgnBox( update_rgn, update_rect );
1020 NtGdiTransformPoints( hdc, (POINT *)&update_rect, (POINT *)&update_rect, 2, NtGdiDPtoLP );
1021 TRACE( "returning update_rect %s\n", wine_dbgstr_rect(update_rect) );
1023 if (!ret_update_rgn) NtGdiDeleteObjectApp( update_rgn );
1024 NtGdiExtSelectClipRgn( hdc, cliprgn, RGN_COPY );
1025 if (cliprgn) NtGdiDeleteObjectApp( cliprgn );
1026 NtGdiDeleteObjectApp( visrgn );
1027 NtGdiDeleteObjectApp( dstrgn );
1028 return ret;