[WIN32K]
[reactos.git] / reactos / win32ss / gdi / ntgdi / fillshap.c
1 /*
2 * PROJECT: ReactOS win32 kernel mode subsystem
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: subsystems/win32/win32k/objects/fillshap.c
5 * PURPOSE: fillshap
6 * PROGRAMMER:
7 */
8
9 #include <win32k.h>
10
11 #define NDEBUG
12 #include <debug.h>
13
14 #define Rsin(d) ((d) == 0.0 ? 0.0 : ((d) == 90.0 ? 1.0 : sin(d*M_PI/180.0)))
15 #define Rcos(d) ((d) == 0.0 ? 1.0 : ((d) == 90.0 ? 0.0 : cos(d*M_PI/180.0)))
16
17 BOOL FASTCALL
18 IntGdiPolygon(PDC dc,
19 PPOINT Points,
20 int Count)
21 {
22 SURFACE *psurf;
23 PBRUSH pbrLine, pbrFill;
24 BOOL ret = FALSE; // Default to failure
25 RECTL DestRect;
26 int CurrentPoint;
27 PDC_ATTR pdcattr;
28 POINTL BrushOrigin;
29 // int Left;
30 // int Top;
31
32 ASSERT(dc); // Caller's responsibility to pass a valid dc
33
34 if (!Points || Count < 2 )
35 {
36 EngSetLastError(ERROR_INVALID_PARAMETER);
37 return FALSE;
38 }
39
40 /*
41 // Find start x, y
42 Left = Points[0].x;
43 Top = Points[0].y;
44 for (CurrentPoint = 1; CurrentPoint < Count; ++CurrentPoint) {
45 Left = min(Left, Points[CurrentPoint].x);
46 Top = min(Top, Points[CurrentPoint].y);
47 }
48 */
49
50 pdcattr = dc->pdcattr;
51
52 /* Convert to screen coordinates */
53 IntLPtoDP(dc, Points, Count);
54 for (CurrentPoint = 0; CurrentPoint < Count; CurrentPoint++)
55 {
56 Points[CurrentPoint].x += dc->ptlDCOrig.x;
57 Points[CurrentPoint].y += dc->ptlDCOrig.y;
58 }
59 // No need to have path here.
60 {
61 DestRect.left = Points[0].x;
62 DestRect.right = Points[0].x;
63 DestRect.top = Points[0].y;
64 DestRect.bottom = Points[0].y;
65
66 for (CurrentPoint = 1; CurrentPoint < Count; ++CurrentPoint)
67 {
68 DestRect.left = min(DestRect.left, Points[CurrentPoint].x);
69 DestRect.right = max(DestRect.right, Points[CurrentPoint].x);
70 DestRect.top = min(DestRect.top, Points[CurrentPoint].y);
71 DestRect.bottom = max(DestRect.bottom, Points[CurrentPoint].y);
72 }
73
74 if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
75 DC_vUpdateFillBrush(dc);
76
77 if (pdcattr->ulDirty_ & (DIRTY_LINE | DC_PEN_DIRTY))
78 DC_vUpdateLineBrush(dc);
79
80 /* Special locking order to avoid lock-ups */
81 pbrFill = dc->dclevel.pbrFill;
82 pbrLine = dc->dclevel.pbrLine;
83 psurf = dc->dclevel.pSurface;
84 /* FIXME: psurf can be NULL!!!! don't assert but handle this case gracefully! */
85 ASSERT(psurf);
86
87 /* Now fill the polygon with the current fill brush. */
88 if (!(pbrFill->flAttrs & BR_IS_NULL))
89 {
90 BrushOrigin = *((PPOINTL)&pbrFill->ptOrigin);
91 BrushOrigin.x += dc->ptlDCOrig.x;
92 BrushOrigin.y += dc->ptlDCOrig.y;
93 ret = IntFillPolygon (dc,
94 psurf,
95 &dc->eboFill.BrushObject,
96 Points,
97 Count,
98 DestRect,
99 &BrushOrigin);
100 }
101
102 // Draw the Polygon Edges with the current pen ( if not a NULL pen )
103 if (!(pbrLine->flAttrs & BR_IS_NULL))
104 {
105 int i;
106
107 for (i = 0; i < Count-1; i++)
108 {
109
110 // DPRINT1("Polygon Making line from (%d,%d) to (%d,%d)\n",
111 // Points[0].x, Points[0].y,
112 // Points[1].x, Points[1].y );
113
114 ret = IntEngLineTo(&psurf->SurfObj,
115 dc->rosdc.CombinedClip,
116 &dc->eboLine.BrushObject,
117 Points[i].x, /* From */
118 Points[i].y,
119 Points[i+1].x, /* To */
120 Points[i+1].y,
121 &DestRect,
122 ROP2_TO_MIX(pdcattr->jROP2)); /* MIX */
123 if (!ret) break;
124 }
125 /* Close the polygon */
126 if (ret)
127 {
128 ret = IntEngLineTo(&psurf->SurfObj,
129 dc->rosdc.CombinedClip,
130 &dc->eboLine.BrushObject,
131 Points[Count-1].x, /* From */
132 Points[Count-1].y,
133 Points[0].x, /* To */
134 Points[0].y,
135 &DestRect,
136 ROP2_TO_MIX(pdcattr->jROP2)); /* MIX */
137 }
138 }
139 }
140
141 return ret;
142 }
143
144 BOOL FASTCALL
145 IntGdiPolyPolygon(DC *dc,
146 LPPOINT Points,
147 PULONG PolyCounts,
148 int Count)
149 {
150 if (PATH_IsPathOpen(dc->dclevel))
151 return PATH_PolyPolygon ( dc, Points, (PINT)PolyCounts, Count);
152
153 while (--Count >=0)
154 {
155 if (!IntGdiPolygon ( dc, Points, *PolyCounts ))
156 return FALSE;
157 Points+=*PolyCounts++;
158 }
159 return TRUE;
160 }
161
162 BOOL FASTCALL
163 IntPolygon(HDC hdc, POINT *Point, int Count)
164 {
165 PDC dc;
166 if (!(dc = DC_LockDc(hdc)))
167 {
168 EngSetLastError(ERROR_INVALID_HANDLE);
169 return FALSE;
170 }
171 return IntGdiPolygon(dc, Point, Count);
172 }
173
174
175 /******************************************************************************/
176
177 /*
178 * NtGdiEllipse
179 *
180 * Author
181 * Filip Navara
182 *
183 * Remarks
184 * This function uses optimized Bresenham's ellipse algorithm. It draws
185 * four lines of the ellipse in one pass.
186 *
187 */
188
189 BOOL APIENTRY
190 NtGdiEllipse(
191 HDC hDC,
192 int Left,
193 int Top,
194 int Right,
195 int Bottom)
196 {
197 PDC dc;
198 PDC_ATTR pdcattr;
199 RECTL RectBounds;
200 PBRUSH pbrush;
201 BOOL ret = TRUE;
202 LONG PenWidth, PenOrigWidth;
203 LONG RadiusX, RadiusY, CenterX, CenterY;
204 PBRUSH pFillBrushObj;
205 BRUSH tmpFillBrushObj;
206
207 if ((Left == Right) || (Top == Bottom)) return TRUE;
208
209 dc = DC_LockDc(hDC);
210 if (dc == NULL)
211 {
212 EngSetLastError(ERROR_INVALID_HANDLE);
213 return FALSE;
214 }
215 if (dc->dctype == DC_TYPE_INFO)
216 {
217 DC_UnlockDc(dc);
218 /* Yes, Windows really returns TRUE in this case */
219 return TRUE;
220 }
221
222 if (PATH_IsPathOpen(dc->dclevel))
223 {
224 ret = PATH_Ellipse(dc, Left, Top, Right, Bottom);
225 DC_UnlockDc(dc);
226 return ret;
227 }
228
229 if (Right < Left)
230 {
231 INT tmp = Right; Right = Left; Left = tmp;
232 }
233 if (Bottom < Top)
234 {
235 INT tmp = Bottom; Bottom = Top; Top = tmp;
236 }
237
238 pdcattr = dc->pdcattr;
239
240 if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
241 DC_vUpdateFillBrush(dc);
242
243 if (pdcattr->ulDirty_ & (DIRTY_LINE | DC_PEN_DIRTY))
244 DC_vUpdateLineBrush(dc);
245
246 pbrush = PEN_ShareLockPen(pdcattr->hpen);
247 if (!pbrush)
248 {
249 DPRINT1("Ellipse Fail 1\n");
250 DC_UnlockDc(dc);
251 EngSetLastError(ERROR_INTERNAL_ERROR);
252 return FALSE;
253 }
254
255 PenOrigWidth = PenWidth = pbrush->ptPenWidth.x;
256 if (pbrush->ulPenStyle == PS_NULL) PenWidth = 0;
257
258 if (pbrush->ulPenStyle == PS_INSIDEFRAME)
259 {
260 if (2*PenWidth > (Right - Left)) PenWidth = (Right -Left + 1)/2;
261 if (2*PenWidth > (Bottom - Top)) PenWidth = (Bottom -Top + 1)/2;
262 Left += PenWidth / 2;
263 Right -= (PenWidth - 1) / 2;
264 Top += PenWidth / 2;
265 Bottom -= (PenWidth - 1) / 2;
266 }
267
268 if (!PenWidth) PenWidth = 1;
269 pbrush->ptPenWidth.x = PenWidth;
270
271 RectBounds.left = Left;
272 RectBounds.right = Right;
273 RectBounds.top = Top;
274 RectBounds.bottom = Bottom;
275
276 IntLPtoDP(dc, (LPPOINT)&RectBounds, 2);
277
278 RectBounds.left += dc->ptlDCOrig.x;
279 RectBounds.right += dc->ptlDCOrig.x;
280 RectBounds.top += dc->ptlDCOrig.y;
281 RectBounds.bottom += dc->ptlDCOrig.y;
282
283 // Setup for dynamic width and height.
284 RadiusX = max((RectBounds.right - RectBounds.left) / 2, 2); // Needs room
285 RadiusY = max((RectBounds.bottom - RectBounds.top) / 2, 2);
286 CenterX = (RectBounds.right + RectBounds.left) / 2;
287 CenterY = (RectBounds.bottom + RectBounds.top) / 2;
288
289 DPRINT("Ellipse 1: Left: %d, Top: %d, Right: %d, Bottom: %d\n",
290 RectBounds.left,RectBounds.top,RectBounds.right,RectBounds.bottom);
291
292 DPRINT("Ellipse 2: XLeft: %d, YLeft: %d, Width: %d, Height: %d\n",
293 CenterX - RadiusX, CenterY + RadiusY, RadiusX*2, RadiusY*2);
294
295 pFillBrushObj = BRUSH_ShareLockBrush(pdcattr->hbrush);
296 if (NULL == pFillBrushObj)
297 {
298 DPRINT1("FillEllipse Fail\n");
299 EngSetLastError(ERROR_INTERNAL_ERROR);
300 ret = FALSE;
301 }
302 else
303 {
304 RtlCopyMemory(&tmpFillBrushObj, pFillBrushObj, sizeof(tmpFillBrushObj));
305 //tmpFillBrushObj.ptOrigin.x += RectBounds.left - Left;
306 //tmpFillBrushObj.ptOrigin.y += RectBounds.top - Top;
307 tmpFillBrushObj.ptOrigin.x += dc->ptlDCOrig.x;
308 tmpFillBrushObj.ptOrigin.y += dc->ptlDCOrig.y;
309
310 DC_vPrepareDCsForBlit(dc, &RectBounds, NULL, NULL);
311
312 ret = IntFillEllipse( dc,
313 CenterX - RadiusX,
314 CenterY - RadiusY,
315 RadiusX*2, // Width
316 RadiusY*2, // Height
317 &tmpFillBrushObj);
318 BRUSH_ShareUnlockBrush(pFillBrushObj);
319
320 if (ret)
321 {
322 ret = IntDrawEllipse( dc,
323 CenterX - RadiusX,
324 CenterY - RadiusY,
325 RadiusX*2, // Width
326 RadiusY*2, // Height
327 pbrush);
328 }
329
330 DC_vFinishBlit(dc, NULL);
331 }
332
333 pbrush->ptPenWidth.x = PenOrigWidth;
334 PEN_ShareUnlockPen(pbrush);
335 DC_UnlockDc(dc);
336 DPRINT("Ellipse Exit.\n");
337 return ret;
338 }
339
340 #if 0
341
342 // When the fill mode is ALTERNATE, GDI fills the area between odd-numbered and
343 // even-numbered polygon sides on each scan line. That is, GDI fills the area between the
344 // first and second side, between the third and fourth side, and so on.
345
346 // WINDING Selects winding mode (fills any region with a nonzero winding value).
347 // When the fill mode is WINDING, GDI fills any region that has a nonzero winding value.
348 // This value is defined as the number of times a pen used to draw the polygon would go around the region.
349 // The direction of each edge of the polygon is important.
350
351 extern BOOL FillPolygon(PDC dc,
352 SURFOBJ *SurfObj,
353 PBRUSHOBJ BrushObj,
354 MIX RopMode,
355 CONST PPOINT Points,
356 int Count,
357 RECTL BoundRect);
358
359 #endif
360
361
362 ULONG_PTR
363 APIENTRY
364 NtGdiPolyPolyDraw( IN HDC hDC,
365 IN PPOINT UnsafePoints,
366 IN PULONG UnsafeCounts,
367 IN ULONG Count,
368 IN INT iFunc )
369 {
370 DC *dc;
371 PVOID pTemp;
372 LPPOINT SafePoints;
373 PULONG SafeCounts;
374 NTSTATUS Status = STATUS_SUCCESS;
375 BOOL Ret = TRUE;
376 ULONG nPoints = 0, nMaxPoints = 0, nInvalid = 0, i;
377
378 if (!UnsafePoints || !UnsafeCounts ||
379 Count == 0 || iFunc == 0 || iFunc > GdiPolyPolyRgn)
380 {
381 /* Windows doesn't set last error */
382 return FALSE;
383 }
384
385 _SEH2_TRY
386 {
387 ProbeForRead(UnsafePoints, Count * sizeof(POINT), 1);
388 ProbeForRead(UnsafeCounts, Count * sizeof(ULONG), 1);
389
390 /* Count points and validate poligons */
391 for (i = 0; i < Count; i++)
392 {
393 if (UnsafeCounts[i] < 2)
394 {
395 nInvalid++;
396 }
397 nPoints += UnsafeCounts[i];
398 nMaxPoints = max(nMaxPoints, UnsafeCounts[i]);
399 }
400 }
401 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
402 {
403 Status = _SEH2_GetExceptionCode();
404 }
405 _SEH2_END;
406
407 if (!NT_SUCCESS(Status))
408 {
409 /* Windows doesn't set last error */
410 return FALSE;
411 }
412
413 if (nPoints == 0 || nPoints < nMaxPoints)
414 {
415 /* If all polygon counts are zero, or we have overflow,
416 return without setting a last error code. */
417 return FALSE;
418 }
419
420 if (nInvalid != 0)
421 {
422 /* If at least one poly count is 0 or 1, fail */
423 EngSetLastError(ERROR_INVALID_PARAMETER);
424 return FALSE;
425 }
426
427 /* Allocate one buffer for both counts and points */
428 pTemp = ExAllocatePoolWithTag(PagedPool,
429 Count * sizeof(ULONG) + nPoints * sizeof(POINT),
430 TAG_SHAPE);
431 if (!pTemp)
432 {
433 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
434 return FALSE;
435 }
436
437 SafeCounts = pTemp;
438 SafePoints = (PVOID)(SafeCounts + Count);
439
440 _SEH2_TRY
441 {
442 /* Pointers already probed! */
443 RtlCopyMemory(SafeCounts, UnsafeCounts, Count * sizeof(ULONG));
444 RtlCopyMemory(SafePoints, UnsafePoints, nPoints * sizeof(POINT));
445 }
446 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
447 {
448 Status = _SEH2_GetExceptionCode();
449 }
450 _SEH2_END;
451
452 if (!NT_SUCCESS(Status))
453 {
454 ExFreePoolWithTag(pTemp, TAG_SHAPE);
455 return FALSE;
456 }
457
458 /* Special handling for GdiPolyPolyRgn */
459 if (iFunc == GdiPolyPolyRgn)
460 {
461 HRGN hRgn;
462 hRgn = IntCreatePolyPolygonRgn(SafePoints, SafeCounts, Count, (INT_PTR)hDC);
463 ExFreePoolWithTag(pTemp, TAG_SHAPE);
464 return (ULONG_PTR)hRgn;
465 }
466
467 dc = DC_LockDc(hDC);
468 if (!dc)
469 {
470 EngSetLastError(ERROR_INVALID_HANDLE);
471 ExFreePoolWithTag(pTemp, TAG_SHAPE);
472 return FALSE;
473 }
474
475 if (dc->dctype == DC_TYPE_INFO)
476 {
477 DC_UnlockDc(dc);
478 ExFreePoolWithTag(pTemp, TAG_SHAPE);
479 /* Yes, Windows really returns TRUE in this case */
480 return TRUE;
481 }
482
483 DC_vPrepareDCsForBlit(dc, NULL, NULL, NULL);
484
485 if (dc->pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
486 DC_vUpdateFillBrush(dc);
487
488 if (dc->pdcattr->ulDirty_ & (DIRTY_LINE | DC_PEN_DIRTY))
489 DC_vUpdateLineBrush(dc);
490
491 /* Perform the actual work */
492 switch (iFunc)
493 {
494 case GdiPolyPolygon:
495 Ret = IntGdiPolyPolygon(dc, SafePoints, SafeCounts, Count);
496 break;
497 case GdiPolyPolyLine:
498 Ret = IntGdiPolyPolyline(dc, SafePoints, SafeCounts, Count);
499 break;
500 case GdiPolyBezier:
501 Ret = IntGdiPolyBezier(dc, SafePoints, *SafeCounts);
502 break;
503 case GdiPolyLineTo:
504 Ret = IntGdiPolylineTo(dc, SafePoints, *SafeCounts);
505 break;
506 case GdiPolyBezierTo:
507 Ret = IntGdiPolyBezierTo(dc, SafePoints, *SafeCounts);
508 break;
509 default:
510 EngSetLastError(ERROR_INVALID_PARAMETER);
511 Ret = FALSE;
512 }
513
514 /* Cleanup and return */
515 DC_vFinishBlit(dc, NULL);
516 DC_UnlockDc(dc);
517 ExFreePoolWithTag(pTemp, TAG_SHAPE);
518
519 return (ULONG_PTR)Ret;
520 }
521
522
523 BOOL
524 FASTCALL
525 IntRectangle(PDC dc,
526 int LeftRect,
527 int TopRect,
528 int RightRect,
529 int BottomRect)
530 {
531 SURFACE *psurf = NULL;
532 PBRUSH pbrLine, pbrFill;
533 BOOL ret = FALSE; // Default to failure
534 RECTL DestRect;
535 MIX Mix;
536 PDC_ATTR pdcattr;
537 POINTL BrushOrigin;
538
539 ASSERT ( dc ); // Caller's responsibility to set this up
540
541 pdcattr = dc->pdcattr;
542
543 // Rectangle Path only.
544 if ( PATH_IsPathOpen(dc->dclevel) )
545 {
546 return PATH_Rectangle ( dc, LeftRect, TopRect, RightRect, BottomRect );
547 }
548
549 /* Make sure rectangle is not inverted */
550 DestRect.left = min(LeftRect, RightRect);
551 DestRect.right = max(LeftRect, RightRect);
552 DestRect.top = min(TopRect, BottomRect);
553 DestRect.bottom = max(TopRect, BottomRect);
554
555 IntLPtoDP(dc, (LPPOINT)&DestRect, 2);
556
557 DestRect.left += dc->ptlDCOrig.x;
558 DestRect.right += dc->ptlDCOrig.x;
559 DestRect.top += dc->ptlDCOrig.y;
560 DestRect.bottom += dc->ptlDCOrig.y;
561
562 /* In GM_COMPATIBLE, don't include bottom and right edges */
563 if (pdcattr->iGraphicsMode == GM_COMPATIBLE)
564 {
565 DestRect.right--;
566 DestRect.bottom--;
567 }
568
569 DC_vPrepareDCsForBlit(dc, &DestRect, NULL, NULL);
570
571 if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
572 DC_vUpdateFillBrush(dc);
573
574 if (pdcattr->ulDirty_ & (DIRTY_LINE | DC_PEN_DIRTY))
575 DC_vUpdateLineBrush(dc);
576
577 pbrFill = dc->dclevel.pbrFill;
578 pbrLine = dc->dclevel.pbrLine;
579 if (!pbrLine)
580 {
581 ret = FALSE;
582 goto cleanup;
583 }
584
585 psurf = dc->dclevel.pSurface;
586 if (!psurf)
587 {
588 ret = FALSE;
589 goto cleanup;
590 }
591
592 if (pbrFill)
593 {
594 if (!(pbrFill->flAttrs & BR_IS_NULL))
595 {
596 BrushOrigin = *((PPOINTL)&pbrFill->ptOrigin);
597 BrushOrigin.x += dc->ptlDCOrig.x;
598 BrushOrigin.y += dc->ptlDCOrig.y;
599 ret = IntEngBitBlt(&psurf->SurfObj,
600 NULL,
601 NULL,
602 dc->rosdc.CombinedClip,
603 NULL,
604 &DestRect,
605 NULL,
606 NULL,
607 &dc->eboFill.BrushObject,
608 &BrushOrigin,
609 ROP4_FROM_INDEX(R3_OPINDEX_PATCOPY));
610 }
611 }
612
613 // Draw the rectangle with the current pen
614
615 ret = TRUE; // Change default to success
616
617 if (!(pbrLine->flAttrs & BR_IS_NULL))
618 {
619 Mix = ROP2_TO_MIX(pdcattr->jROP2);
620 ret = ret && IntEngLineTo(&psurf->SurfObj,
621 dc->rosdc.CombinedClip,
622 &dc->eboLine.BrushObject,
623 DestRect.left, DestRect.top, DestRect.right, DestRect.top,
624 &DestRect, // Bounding rectangle
625 Mix);
626
627 ret = ret && IntEngLineTo(&psurf->SurfObj,
628 dc->rosdc.CombinedClip,
629 &dc->eboLine.BrushObject,
630 DestRect.right, DestRect.top, DestRect.right, DestRect.bottom,
631 &DestRect, // Bounding rectangle
632 Mix);
633
634 ret = ret && IntEngLineTo(&psurf->SurfObj,
635 dc->rosdc.CombinedClip,
636 &dc->eboLine.BrushObject,
637 DestRect.right, DestRect.bottom, DestRect.left, DestRect.bottom,
638 &DestRect, // Bounding rectangle
639 Mix);
640
641 ret = ret && IntEngLineTo(&psurf->SurfObj,
642 dc->rosdc.CombinedClip,
643 &dc->eboLine.BrushObject,
644 DestRect.left, DestRect.bottom, DestRect.left, DestRect.top,
645 &DestRect, // Bounding rectangle
646 Mix);
647 }
648
649 cleanup:
650 DC_vFinishBlit(dc, NULL);
651
652 /* Move current position in DC?
653 MSDN: The current position is neither used nor updated by Rectangle. */
654
655 return ret;
656 }
657
658 BOOL
659 APIENTRY
660 NtGdiRectangle(HDC hDC,
661 int LeftRect,
662 int TopRect,
663 int RightRect,
664 int BottomRect)
665 {
666 DC *dc;
667 BOOL ret; // Default to failure
668
669 dc = DC_LockDc(hDC);
670 if (!dc)
671 {
672 EngSetLastError(ERROR_INVALID_HANDLE);
673 return FALSE;
674 }
675 if (dc->dctype == DC_TYPE_INFO)
676 {
677 DC_UnlockDc(dc);
678 /* Yes, Windows really returns TRUE in this case */
679 return TRUE;
680 }
681
682 /* Do we rotate or shear? */
683 if (!(dc->pdcattr->mxWorldToDevice.flAccel & XFORM_SCALE))
684 {
685 POINTL DestCoords[4];
686 ULONG PolyCounts = 4;
687
688 DestCoords[0].x = DestCoords[3].x = LeftRect;
689 DestCoords[0].y = DestCoords[1].y = TopRect;
690 DestCoords[1].x = DestCoords[2].x = RightRect;
691 DestCoords[2].y = DestCoords[3].y = BottomRect;
692 // Use IntGdiPolyPolygon so to support PATH.
693 ret = IntGdiPolyPolygon(dc, DestCoords, &PolyCounts, 1);
694 }
695 else
696 {
697 ret = IntRectangle(dc, LeftRect, TopRect, RightRect, BottomRect );
698 }
699
700 DC_UnlockDc(dc);
701
702 return ret;
703 }
704
705
706 BOOL
707 FASTCALL
708 IntRoundRect(
709 PDC dc,
710 int Left,
711 int Top,
712 int Right,
713 int Bottom,
714 int xCurveDiameter,
715 int yCurveDiameter)
716 {
717 PDC_ATTR pdcattr;
718 PBRUSH pbrLine, pbrFill;
719 RECTL RectBounds;
720 LONG PenWidth, PenOrigWidth;
721 BOOL ret = TRUE; // Default to success
722 BRUSH brushTemp;
723
724 ASSERT ( dc ); // Caller's responsibility to set this up
725
726 if ( PATH_IsPathOpen(dc->dclevel) )
727 return PATH_RoundRect ( dc, Left, Top, Right, Bottom,
728 xCurveDiameter, yCurveDiameter );
729
730 if ((Left == Right) || (Top == Bottom)) return TRUE;
731
732 xCurveDiameter = max(abs( xCurveDiameter ), 1);
733 yCurveDiameter = max(abs( yCurveDiameter ), 1);
734
735 if (Right < Left)
736 {
737 INT tmp = Right; Right = Left; Left = tmp;
738 }
739 if (Bottom < Top)
740 {
741 INT tmp = Bottom; Bottom = Top; Top = tmp;
742 }
743
744 pdcattr = dc->pdcattr;
745
746 if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
747 DC_vUpdateFillBrush(dc);
748
749 if (pdcattr->ulDirty_ & (DIRTY_LINE | DC_PEN_DIRTY))
750 DC_vUpdateLineBrush(dc);
751
752 pbrLine = PEN_ShareLockPen(pdcattr->hpen);
753 if (!pbrLine)
754 {
755 /* Nothing to do, as we don't have a bitmap */
756 EngSetLastError(ERROR_INTERNAL_ERROR);
757 return FALSE;
758 }
759
760 PenOrigWidth = PenWidth = pbrLine->ptPenWidth.x;
761 if (pbrLine->ulPenStyle == PS_NULL) PenWidth = 0;
762
763 if (pbrLine->ulPenStyle == PS_INSIDEFRAME)
764 {
765 if (2*PenWidth > (Right - Left)) PenWidth = (Right -Left + 1)/2;
766 if (2*PenWidth > (Bottom - Top)) PenWidth = (Bottom -Top + 1)/2;
767 Left += PenWidth / 2;
768 Right -= (PenWidth - 1) / 2;
769 Top += PenWidth / 2;
770 Bottom -= (PenWidth - 1) / 2;
771 }
772
773 if (!PenWidth) PenWidth = 1;
774 pbrLine->ptPenWidth.x = PenWidth;
775
776 RectBounds.left = Left;
777 RectBounds.top = Top;
778 RectBounds.right = Right;
779 RectBounds.bottom = Bottom;
780
781 IntLPtoDP(dc, (LPPOINT)&RectBounds, 2);
782
783 RectBounds.left += dc->ptlDCOrig.x;
784 RectBounds.top += dc->ptlDCOrig.y;
785 RectBounds.right += dc->ptlDCOrig.x;
786 RectBounds.bottom += dc->ptlDCOrig.y;
787
788 pbrFill = BRUSH_ShareLockBrush(pdcattr->hbrush);
789 if (!pbrFill)
790 {
791 DPRINT1("FillRound Fail\n");
792 EngSetLastError(ERROR_INTERNAL_ERROR);
793 ret = FALSE;
794 }
795 else
796 {
797
798 DC_vPrepareDCsForBlit(dc, &RectBounds, NULL, NULL);
799
800 RtlCopyMemory(&brushTemp, pbrFill, sizeof(brushTemp));
801 brushTemp.ptOrigin.x += RectBounds.left - Left;
802 brushTemp.ptOrigin.y += RectBounds.top - Top;
803 ret = IntFillRoundRect( dc,
804 RectBounds.left,
805 RectBounds.top,
806 RectBounds.right,
807 RectBounds.bottom,
808 xCurveDiameter,
809 yCurveDiameter,
810 &brushTemp);
811 BRUSH_ShareUnlockBrush(pbrFill);
812
813 if (ret)
814 {
815 ret = IntDrawRoundRect( dc,
816 RectBounds.left,
817 RectBounds.top,
818 RectBounds.right,
819 RectBounds.bottom,
820 xCurveDiameter,
821 yCurveDiameter,
822 pbrLine);
823 }
824
825 DC_vFinishBlit(dc, NULL);
826 }
827
828
829 pbrLine->ptPenWidth.x = PenOrigWidth;
830 PEN_ShareUnlockPen(pbrLine);
831 return ret;
832 }
833
834 BOOL
835 APIENTRY
836 NtGdiRoundRect(
837 HDC hDC,
838 int LeftRect,
839 int TopRect,
840 int RightRect,
841 int BottomRect,
842 int Width,
843 int Height)
844 {
845 DC *dc = DC_LockDc(hDC);
846 BOOL ret = FALSE; /* Default to failure */
847
848 DPRINT("NtGdiRoundRect(0x%p,%i,%i,%i,%i,%i,%i)\n",hDC,LeftRect,TopRect,RightRect,BottomRect,Width,Height);
849 if ( !dc )
850 {
851 DPRINT1("NtGdiRoundRect() - hDC is invalid\n");
852 EngSetLastError(ERROR_INVALID_HANDLE);
853 }
854 else if (dc->dctype == DC_TYPE_INFO)
855 {
856 DC_UnlockDc(dc);
857 /* Yes, Windows really returns TRUE in this case */
858 ret = TRUE;
859 }
860 else
861 {
862 ret = IntRoundRect ( dc, LeftRect, TopRect, RightRect, BottomRect, Width, Height );
863 DC_UnlockDc ( dc );
864 }
865
866 return ret;
867 }
868
869 BOOL
870 NTAPI
871 GreGradientFill(
872 HDC hdc,
873 PTRIVERTEX pVertex,
874 ULONG nVertex,
875 PVOID pMesh,
876 ULONG nMesh,
877 ULONG ulMode)
878 {
879 PDC pdc;
880 SURFACE *psurf;
881 EXLATEOBJ exlo;
882 RECTL rclExtent;
883 POINTL ptlDitherOrg;
884 ULONG i;
885 BOOL bRet;
886
887 /* Check parameters */
888 if (ulMode & GRADIENT_FILL_TRIANGLE)
889 {
890 PGRADIENT_TRIANGLE pTriangle = (PGRADIENT_TRIANGLE)pMesh;
891
892 for (i = 0; i < nMesh; i++, pTriangle++)
893 {
894 if (pTriangle->Vertex1 >= nVertex ||
895 pTriangle->Vertex2 >= nVertex ||
896 pTriangle->Vertex3 >= nVertex)
897 {
898 EngSetLastError(ERROR_INVALID_PARAMETER);
899 return FALSE;
900 }
901 }
902 }
903 else
904 {
905 PGRADIENT_RECT pRect = (PGRADIENT_RECT)pMesh;
906 for (i = 0; i < nMesh; i++, pRect++)
907 {
908 if (pRect->UpperLeft >= nVertex || pRect->LowerRight >= nVertex)
909 {
910 EngSetLastError(ERROR_INVALID_PARAMETER);
911 return FALSE;
912 }
913 }
914 }
915
916 /* Lock the output DC */
917 pdc = DC_LockDc(hdc);
918 if(!pdc)
919 {
920 EngSetLastError(ERROR_INVALID_HANDLE);
921 return FALSE;
922 }
923
924 if(pdc->dctype == DC_TYPE_INFO)
925 {
926 DC_UnlockDc(pdc);
927 /* Yes, Windows really returns TRUE in this case */
928 return TRUE;
929 }
930
931 psurf = pdc->dclevel.pSurface;
932 if(!psurf)
933 {
934 /* Memory DC with no surface selected */
935 DC_UnlockDc(pdc);
936 return TRUE; // CHECKME
937 }
938
939 /* Calculate extent */
940 rclExtent.left = rclExtent.right = pVertex->x;
941 rclExtent.top = rclExtent.bottom = pVertex->y;
942 for (i = 0; i < nVertex; i++)
943 {
944 rclExtent.left = min(rclExtent.left, (pVertex + i)->x);
945 rclExtent.right = max(rclExtent.right, (pVertex + i)->x);
946 rclExtent.top = min(rclExtent.top, (pVertex + i)->y);
947 rclExtent.bottom = max(rclExtent.bottom, (pVertex + i)->y);
948 }
949 IntLPtoDP(pdc, (LPPOINT)&rclExtent, 2);
950
951 rclExtent.left += pdc->ptlDCOrig.x;
952 rclExtent.right += pdc->ptlDCOrig.x;
953 rclExtent.top += pdc->ptlDCOrig.y;
954 rclExtent.bottom += pdc->ptlDCOrig.y;
955
956 ptlDitherOrg.x = ptlDitherOrg.y = 0;
957 IntLPtoDP(pdc, (LPPOINT)&ptlDitherOrg, 1);
958
959 ptlDitherOrg.x += pdc->ptlDCOrig.x;
960 ptlDitherOrg.y += pdc->ptlDCOrig.y;
961
962 EXLATEOBJ_vInitialize(&exlo, &gpalRGB, psurf->ppal, 0, 0, 0);
963
964 DC_vPrepareDCsForBlit(pdc, &rclExtent, NULL, NULL);
965
966 ASSERT(pdc->rosdc.CombinedClip);
967
968 bRet = IntEngGradientFill(&psurf->SurfObj,
969 pdc->rosdc.CombinedClip,
970 &exlo.xlo,
971 pVertex,
972 nVertex,
973 pMesh,
974 nMesh,
975 &rclExtent,
976 &ptlDitherOrg,
977 ulMode);
978
979 EXLATEOBJ_vCleanup(&exlo);
980 DC_vFinishBlit(pdc, NULL);
981 DC_UnlockDc(pdc);
982
983 return bRet;
984 }
985
986 BOOL
987 APIENTRY
988 NtGdiGradientFill(
989 HDC hdc,
990 PTRIVERTEX pVertex,
991 ULONG nVertex,
992 PVOID pMesh,
993 ULONG nMesh,
994 ULONG ulMode)
995 {
996 BOOL bRet;
997 PTRIVERTEX SafeVertex;
998 PVOID SafeMesh;
999 ULONG cbVertex, cbMesh;
1000
1001 /* Validate parameters */
1002 if (!pVertex || !nVertex || !pMesh || !nMesh)
1003 {
1004 EngSetLastError(ERROR_INVALID_PARAMETER);
1005 return FALSE;
1006 }
1007
1008 switch (ulMode)
1009 {
1010 case GRADIENT_FILL_RECT_H:
1011 case GRADIENT_FILL_RECT_V:
1012 cbMesh = nMesh * sizeof(GRADIENT_RECT);
1013 break;
1014 case GRADIENT_FILL_TRIANGLE:
1015 cbMesh = nMesh * sizeof(GRADIENT_TRIANGLE);
1016 break;
1017 default:
1018 EngSetLastError(ERROR_INVALID_PARAMETER);
1019 return FALSE;
1020 }
1021
1022 cbVertex = nVertex * sizeof(TRIVERTEX) ;
1023 if(cbVertex + cbMesh <= cbVertex)
1024 {
1025 /* Overflow */
1026 return FALSE ;
1027 }
1028
1029 /* Allocate a kernel mode buffer */
1030 SafeVertex = ExAllocatePoolWithTag(PagedPool, cbVertex + cbMesh, TAG_SHAPE);
1031 if(!SafeVertex)
1032 {
1033 EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
1034 return FALSE;
1035 }
1036
1037 SafeMesh = (PVOID)((ULONG_PTR)SafeVertex + cbVertex);
1038
1039 /* Copy the parameters to kernel mode */
1040 _SEH2_TRY
1041 {
1042 ProbeForRead(pVertex, cbVertex, 1);
1043 ProbeForRead(pMesh, cbMesh, 1);
1044 RtlCopyMemory(SafeVertex, pVertex, cbVertex);
1045 RtlCopyMemory(SafeMesh, pMesh, cbMesh);
1046 }
1047 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1048 {
1049 ExFreePoolWithTag(SafeVertex, TAG_SHAPE);
1050 SetLastNtError(_SEH2_GetExceptionCode());
1051 _SEH2_YIELD(return FALSE;)
1052 }
1053 _SEH2_END;
1054
1055 /* Call the internal function */
1056 bRet = GreGradientFill(hdc, SafeVertex, nVertex, SafeMesh, nMesh, ulMode);
1057
1058 /* Cleanup and return result */
1059 ExFreePoolWithTag(SafeVertex, TAG_SHAPE);
1060 return bRet;
1061 }
1062
1063 BOOL APIENTRY
1064 NtGdiExtFloodFill(
1065 HDC hDC,
1066 INT XStart,
1067 INT YStart,
1068 COLORREF Color,
1069 UINT FillType)
1070 {
1071 PDC dc;
1072 PDC_ATTR pdcattr;
1073 SURFACE *psurf = NULL;
1074 EXLATEOBJ exlo;
1075 BOOL Ret = FALSE;
1076 RECTL DestRect;
1077 POINTL Pt;
1078 ULONG ConvColor;
1079
1080 dc = DC_LockDc(hDC);
1081 if (!dc)
1082 {
1083 EngSetLastError(ERROR_INVALID_HANDLE);
1084 return FALSE;
1085 }
1086 if (dc->dctype == DC_TYPE_INFO)
1087 {
1088 DC_UnlockDc(dc);
1089 /* Yes, Windows really returns TRUE in this case */
1090 return TRUE;
1091 }
1092
1093 psurf = dc->dclevel.pSurface;
1094 if (!psurf)
1095 {
1096 Ret = FALSE;
1097 goto cleanup;
1098 }
1099
1100 pdcattr = dc->pdcattr;
1101
1102 if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
1103 DC_vUpdateFillBrush(dc);
1104
1105 if (pdcattr->ulDirty_ & (DIRTY_LINE | DC_PEN_DIRTY))
1106 DC_vUpdateLineBrush(dc);
1107
1108 Pt.x = XStart;
1109 Pt.y = YStart;
1110 IntLPtoDP(dc, (LPPOINT)&Pt, 1);
1111
1112 if (dc->prgnRao)
1113 {
1114 Ret = REGION_PtInRegion(dc->prgnRao, Pt.x, Pt.y);
1115 if (Ret)
1116 REGION_GetRgnBox(dc->prgnRao ,(LPRECT)&DestRect);
1117 else
1118 goto cleanup;
1119 }
1120 else
1121 RECTL_vSetRect(&DestRect, 0, psurf->SurfObj.sizlBitmap.cx, 0, psurf->SurfObj.sizlBitmap.cy);
1122
1123 DC_vPrepareDCsForBlit(dc, &DestRect, NULL, NULL);
1124
1125 EXLATEOBJ_vInitialize(&exlo, &gpalRGB, psurf->ppal, 0, 0xffffff, 0);
1126
1127 /* Only solid fills supported for now
1128 * How to support pattern brushes and non standard surfaces (not offering dib functions):
1129 * Version a (most likely slow): call DrvPatBlt for every pixel
1130 * Version b: create a flood mask and let MaskBlt blit a masked brush */
1131 ConvColor = XLATEOBJ_iXlate(&exlo.xlo, Color);
1132 Ret = DIB_XXBPP_FloodFillSolid(&psurf->SurfObj, &dc->eboFill.BrushObject, &DestRect, &Pt, ConvColor, FillType);
1133
1134 DC_vFinishBlit(dc, NULL);
1135
1136 EXLATEOBJ_vCleanup(&exlo);
1137
1138 cleanup:
1139 DC_UnlockDc(dc);
1140 return Ret;
1141 }
1142
1143 /* EOF */