Explorar el Código

Fixed ellipse and improved rectangle gradient fills

Krzysztof Krysiński hace 1 mes
padre
commit
285b0936dc

+ 26 - 18
src/ChunkyImageLib/Operations/EllipseOperation.cs

@@ -8,6 +8,7 @@ using Drawie.Backend.Core.Vector;
 using Drawie.Numerics;
 using Drawie.Numerics;
 
 
 namespace ChunkyImageLib.Operations;
 namespace ChunkyImageLib.Operations;
+
 internal class EllipseOperation : IMirroredDrawOperation
 internal class EllipseOperation : IMirroredDrawOperation
 {
 {
     public bool IgnoreEmptyChunks => false;
     public bool IgnoreEmptyChunks => false;
@@ -21,14 +22,15 @@ internal class EllipseOperation : IMirroredDrawOperation
     private bool init = false;
     private bool init = false;
     private VectorPath? outerPath;
     private VectorPath? outerPath;
     private VectorPath? innerPath;
     private VectorPath? innerPath;
-    
+
     private VectorPath? ellipseOutline;
     private VectorPath? ellipseOutline;
     private VecF[]? ellipse;
     private VecF[]? ellipse;
     private VecF[]? ellipseFill;
     private VecF[]? ellipseFill;
     private RectI? ellipseFillRect;
     private RectI? ellipseFillRect;
     private bool antialiased;
     private bool antialiased;
 
 
-    public EllipseOperation(RectD location, Paintable strokePaintable, Paintable fillPaintable, float strokeWidth, double rotationRad,
+    public EllipseOperation(RectD location, Paintable strokePaintable, Paintable fillPaintable, float strokeWidth,
+        double rotationRad,
         bool antiAliased, Paint? paint = null)
         bool antiAliased, Paint? paint = null)
     {
     {
         this.location = location;
         this.location = location;
@@ -92,7 +94,7 @@ internal class EllipseOperation : IMirroredDrawOperation
 
 
         if (antialiased)
         if (antialiased)
         {
         {
-            DrawAntiAliased(surf);   
+            DrawAntiAliased(surf);
         }
         }
         else
         else
         {
         {
@@ -107,25 +109,28 @@ internal class EllipseOperation : IMirroredDrawOperation
         paint.IsAntiAliased = false;
         paint.IsAntiAliased = false;
         if (strokeWidth - 1 < 0.01)
         if (strokeWidth - 1 < 0.01)
         {
         {
-            // TODO: Fix for strokewidth = 1 and gradient fill
             if (Math.Abs(rotation) < 0.001 && strokeWidth > 0)
             if (Math.Abs(rotation) < 0.001 && strokeWidth > 0)
             {
             {
+                RectD rect = (RectD)ellipseFillRect!.Value;
+                fillPaintable.Bounds = location;
                 if (fillPaintable.AnythingVisible || paint.BlendMode != BlendMode.SrcOver)
                 if (fillPaintable.AnythingVisible || paint.BlendMode != BlendMode.SrcOver)
                 {
                 {
                     paint.SetPaintable(fillPaintable);
                     paint.SetPaintable(fillPaintable);
                     surf.Canvas.DrawPoints(PointMode.Lines, ellipseFill!, paint);
                     surf.Canvas.DrawPoints(PointMode.Lines, ellipseFill!, paint);
-                    surf.Canvas.DrawRect((RectD)ellipseFillRect!.Value, paint);
+                    surf.Canvas.DrawRect(rect, paint);
                 }
                 }
-                
+
                 paint.SetPaintable(strokeWidth <= 0 ? fillPaintable : strokePaintable);
                 paint.SetPaintable(strokeWidth <= 0 ? fillPaintable : strokePaintable);
                 paint.StrokeWidth = 1f;
                 paint.StrokeWidth = 1f;
                 surf.Canvas.DrawPoints(PointMode.Points, ellipse!, paint);
                 surf.Canvas.DrawPoints(PointMode.Points, ellipse!, paint);
+
+                fillPaintable.Bounds = null;
             }
             }
             else
             else
             {
             {
                 surf.Canvas.Save();
                 surf.Canvas.Save();
                 surf.Canvas.RotateRadians((float)rotation, (float)location.Center.X, (float)location.Center.Y);
                 surf.Canvas.RotateRadians((float)rotation, (float)location.Center.X, (float)location.Center.Y);
-                
+
                 if (fillPaintable.AnythingVisible || paint.BlendMode != BlendMode.SrcOver)
                 if (fillPaintable.AnythingVisible || paint.BlendMode != BlendMode.SrcOver)
                 {
                 {
                     paint.SetPaintable(fillPaintable);
                     paint.SetPaintable(fillPaintable);
@@ -155,6 +160,7 @@ internal class EllipseOperation : IMirroredDrawOperation
                 surf.Canvas.DrawPaintable(fillPaintable, paint.BlendMode, location);
                 surf.Canvas.DrawPaintable(fillPaintable, paint.BlendMode, location);
                 surf.Canvas.Restore();
                 surf.Canvas.Restore();
             }
             }
+
             surf.Canvas.Save();
             surf.Canvas.Save();
             surf.Canvas.RotateRadians((float)rotation, (float)location.Center.X, (float)location.Center.Y);
             surf.Canvas.RotateRadians((float)rotation, (float)location.Center.X, (float)location.Center.Y);
             surf.Canvas.ClipPath(outerPath!);
             surf.Canvas.ClipPath(outerPath!);
@@ -168,24 +174,24 @@ internal class EllipseOperation : IMirroredDrawOperation
     {
     {
         surf.Canvas.Save();
         surf.Canvas.Save();
         surf.Canvas.RotateRadians((float)rotation, (float)location.Center.X, (float)location.Center.Y);
         surf.Canvas.RotateRadians((float)rotation, (float)location.Center.X, (float)location.Center.Y);
-        
+
         paint.IsAntiAliased = false;
         paint.IsAntiAliased = false;
         paint.SetPaintable(fillPaintable);
         paint.SetPaintable(fillPaintable);
         paint.Style = PaintStyle.Fill;
         paint.Style = PaintStyle.Fill;
-        
+
         RectD fillRect = ((RectD)location).Inflate(-strokeWidth / 2f);
         RectD fillRect = ((RectD)location).Inflate(-strokeWidth / 2f);
-        
+
         surf.Canvas.DrawOval(fillRect.Center, fillRect.Size / 2f, paint);
         surf.Canvas.DrawOval(fillRect.Center, fillRect.Size / 2f, paint);
 
 
         paint.IsAntiAliased = true;
         paint.IsAntiAliased = true;
         paint.SetPaintable(strokeWidth <= 0 ? fillPaintable : strokePaintable);
         paint.SetPaintable(strokeWidth <= 0 ? fillPaintable : strokePaintable);
         paint.Style = PaintStyle.Stroke;
         paint.Style = PaintStyle.Stroke;
         paint.StrokeWidth = strokeWidth <= 0 ? 1f : strokeWidth;
         paint.StrokeWidth = strokeWidth <= 0 ? 1f : strokeWidth;
-        
+
         RectD strokeRect = ((RectD)location).Inflate((-strokeWidth / 2f));
         RectD strokeRect = ((RectD)location).Inflate((-strokeWidth / 2f));
-        
+
         surf.Canvas.DrawOval(strokeRect.Center, strokeRect.Size / 2f, paint);
         surf.Canvas.DrawOval(strokeRect.Center, strokeRect.Size / 2f, paint);
-        
+
         surf.Canvas.Restore();
         surf.Canvas.Restore();
     }
     }
 
 
@@ -194,14 +200,15 @@ internal class EllipseOperation : IMirroredDrawOperation
         ShapeCorners corners = new((RectD)location);
         ShapeCorners corners = new((RectD)location);
         corners = corners.AsRotated(rotation, (VecD)location.Center);
         corners = corners.AsRotated(rotation, (VecD)location.Center);
         RectI bounds = (RectI)corners.AABBBounds.RoundOutwards();
         RectI bounds = (RectI)corners.AABBBounds.RoundOutwards();
-        
+
         var chunks = OperationHelper.FindChunksTouchingRectangle(bounds, ChunkyImage.FullChunkSize);
         var chunks = OperationHelper.FindChunksTouchingRectangle(bounds, ChunkyImage.FullChunkSize);
         if (!fillPaintable?.AnythingVisible ?? false)
         if (!fillPaintable?.AnythingVisible ?? false)
         {
         {
-             chunks.ExceptWith(OperationHelper.FindChunksFullyInsideEllipse
-                (location.Center, location.Width / 2.0 - strokeWidth * 2, location.Height / 2.0 - strokeWidth * 2, ChunkyImage.FullChunkSize, rotation));
+            chunks.ExceptWith(OperationHelper.FindChunksFullyInsideEllipse
+            (location.Center, location.Width / 2.0 - strokeWidth * 2, location.Height / 2.0 - strokeWidth * 2,
+                ChunkyImage.FullChunkSize, rotation));
         }
         }
-        
+
         return new AffectedArea(chunks, bounds);
         return new AffectedArea(chunks, bounds);
     }
     }
 
 
@@ -227,7 +234,8 @@ internal class EllipseOperation : IMirroredDrawOperation
             ((IPositionPaintable)finalStrokePaintable).Position = newLocation.Center;
             ((IPositionPaintable)finalStrokePaintable).Position = newLocation.Center;
         }
         }
 
 
-        return new EllipseOperation(newLocation, finalStrokePaintable, finalFillPaintable, strokeWidth, rotation, antialiased, paint);
+        return new EllipseOperation(newLocation, finalStrokePaintable, finalFillPaintable, strokeWidth, rotation,
+            antialiased, paint);
     }
     }
 
 
     public void Dispose()
     public void Dispose()

+ 17 - 1
src/ChunkyImageLib/Operations/RectangleOperation.cs

@@ -97,6 +97,16 @@ internal class RectangleOperation : IMirroredDrawOperation
     {
     {
         // shrink radius too so corners match inner curve
         // shrink radius too so corners match inner curve
         // Draw fill first
         // Draw fill first
+        if (Data.FillPaintable != null)
+        {
+            Data.FillPaintable.Bounds = rect;
+        }
+
+        if (Data.Stroke != null)
+        {
+            Data.Stroke.Bounds = rect;
+        }
+
         if (Data.FillPaintable.AnythingVisible)
         if (Data.FillPaintable.AnythingVisible)
         {
         {
             int saved = surf.Canvas.Save();
             int saved = surf.Canvas.Save();
@@ -123,7 +133,7 @@ internal class RectangleOperation : IMirroredDrawOperation
             {
             {
                 if (hasStroke)
                 if (hasStroke)
                 {
                 {
-                    surf.Canvas.DrawPaintable(Data.FillPaintable, Data.BlendMode, fillRect);
+                    surf.Canvas.DrawPaintable(Data.FillPaintable, Data.BlendMode);
                 }
                 }
                 else
                 else
                 {
                 {
@@ -170,6 +180,12 @@ internal class RectangleOperation : IMirroredDrawOperation
                     (float)innerRadius, (float)innerRadius, paint);
                     (float)innerRadius, (float)innerRadius, paint);
             }
             }
 
 
+            if(Data.FillPaintable != null)
+                Data.FillPaintable.Bounds = null;
+
+            if(Data.Stroke != null)
+                Data.Stroke.Bounds = null;
+
             surf.Canvas.Restore();
             surf.Canvas.Restore();
         }
         }
     }
     }

+ 1 - 1
src/Drawie

@@ -1 +1 @@
-Subproject commit 84ebee1bbe671cd8141456e3886e225ef36446d6
+Subproject commit fa8a969891227f91f25f5444be9ad47aab352782