EllipseOperation.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using ChunkyImageLib.DataHolders;
  2. using Drawie.Backend.Core.ColorsImpl;
  3. using Drawie.Backend.Core.Numerics;
  4. using Drawie.Backend.Core.Surfaces;
  5. using Drawie.Backend.Core.Surfaces.PaintImpl;
  6. using Drawie.Backend.Core.Vector;
  7. using Drawie.Numerics;
  8. namespace ChunkyImageLib.Operations;
  9. internal class EllipseOperation : IMirroredDrawOperation
  10. {
  11. public bool IgnoreEmptyChunks => false;
  12. private readonly RectD location;
  13. private readonly Color strokeColor;
  14. private readonly Color fillColor;
  15. private readonly float strokeWidth;
  16. private readonly double rotation;
  17. private readonly Paint paint;
  18. private bool init = false;
  19. private VectorPath? outerPath;
  20. private VectorPath? innerPath;
  21. private VectorPath? ellipseOutline;
  22. private VecF[]? ellipse;
  23. private VecF[]? ellipseFill;
  24. private RectI? ellipseFillRect;
  25. private bool antialiased;
  26. public EllipseOperation(RectD location, Color strokeColor, Color fillColor, float strokeWidth, double rotationRad,
  27. bool antiAliased, Paint? paint = null)
  28. {
  29. this.location = location;
  30. this.strokeColor = strokeColor;
  31. this.fillColor = fillColor;
  32. this.strokeWidth = strokeWidth;
  33. this.rotation = rotationRad;
  34. this.paint = paint?.Clone() ?? new Paint();
  35. antialiased = antiAliased;
  36. }
  37. private void Init()
  38. {
  39. init = true;
  40. if (strokeWidth - 1 < 0.01)
  41. {
  42. if (Math.Abs(rotation) < 0.001)
  43. {
  44. var ellipseList = EllipseHelper.GenerateEllipseFromRect((RectI)location);
  45. ellipse = ellipseList.Select(a => new VecF(a)).ToArray();
  46. if (fillColor.A > 0 || paint.BlendMode != BlendMode.SrcOver)
  47. {
  48. (var fill, ellipseFillRect) = EllipseHelper.SplitEllipseFillIntoRegions(ellipseList.ToList(), (RectI)location);
  49. ellipseFill = fill.Select(a => new VecF(a)).ToArray();
  50. }
  51. }
  52. else
  53. {
  54. ellipseOutline = EllipseHelper.GenerateEllipseVectorFromRect(location);
  55. }
  56. }
  57. else
  58. {
  59. outerPath = new VectorPath();
  60. outerPath.ArcTo(location, 0, 359, true);
  61. innerPath = new VectorPath();
  62. innerPath.ArcTo(location.Inflate(-strokeWidth), 0, 359, true);
  63. }
  64. }
  65. public void DrawOnChunk(Chunk targetChunk, VecI chunkPos)
  66. {
  67. if (!init)
  68. Init();
  69. var surf = targetChunk.Surface.DrawingSurface;
  70. surf.Canvas.Save();
  71. surf.Canvas.Scale((float)targetChunk.Resolution.Multiplier());
  72. surf.Canvas.Translate(-chunkPos * ChunkyImage.FullChunkSize);
  73. paint.IsAntiAliased = antialiased || targetChunk.Resolution != ChunkResolution.Full;
  74. if (antialiased)
  75. {
  76. DrawAntiAliased(surf);
  77. }
  78. else
  79. {
  80. DrawAliased(surf);
  81. }
  82. surf.Canvas.Restore();
  83. }
  84. private void DrawAliased(DrawingSurface surf)
  85. {
  86. paint.IsAntiAliased = false;
  87. if (strokeWidth - 1 < 0.01)
  88. {
  89. if (Math.Abs(rotation) < 0.001)
  90. {
  91. if (fillColor.A > 0 || paint.BlendMode != BlendMode.SrcOver)
  92. {
  93. paint.Color = fillColor;
  94. surf.Canvas.DrawPoints(PointMode.Lines, ellipseFill!, paint);
  95. surf.Canvas.DrawRect((RectD)ellipseFillRect!.Value, paint);
  96. }
  97. paint.Color = strokeWidth <= 0 ? fillColor : strokeColor;
  98. paint.StrokeWidth = 1f;
  99. surf.Canvas.DrawPoints(PointMode.Points, ellipse!, paint);
  100. }
  101. else
  102. {
  103. surf.Canvas.Save();
  104. surf.Canvas.RotateRadians((float)rotation, (float)location.Center.X, (float)location.Center.Y);
  105. if (fillColor.A > 0 || paint.BlendMode != BlendMode.SrcOver)
  106. {
  107. paint.Color = fillColor;
  108. paint.Style = PaintStyle.Fill;
  109. surf.Canvas.DrawPath(ellipseOutline!, paint);
  110. }
  111. paint.Color = strokeColor;
  112. paint.Style = PaintStyle.Stroke;
  113. paint.StrokeWidth = 1f;
  114. surf.Canvas.DrawPath(ellipseOutline!, paint);
  115. surf.Canvas.Restore();
  116. }
  117. }
  118. else
  119. {
  120. if (fillColor.A > 0 || paint.BlendMode != BlendMode.SrcOver)
  121. {
  122. surf.Canvas.Save();
  123. surf.Canvas.RotateRadians((float)rotation, (float)location.Center.X, (float)location.Center.Y);
  124. surf.Canvas.ClipPath(innerPath!);
  125. surf.Canvas.DrawColor(fillColor, paint.BlendMode);
  126. surf.Canvas.Restore();
  127. }
  128. surf.Canvas.Save();
  129. surf.Canvas.RotateRadians((float)rotation, (float)location.Center.X, (float)location.Center.Y);
  130. surf.Canvas.ClipPath(outerPath!);
  131. surf.Canvas.ClipPath(innerPath!, ClipOperation.Difference);
  132. surf.Canvas.DrawColor(strokeColor, paint.BlendMode);
  133. surf.Canvas.Restore();
  134. }
  135. }
  136. private void DrawAntiAliased(DrawingSurface surf)
  137. {
  138. surf.Canvas.Save();
  139. surf.Canvas.RotateRadians((float)rotation, (float)location.Center.X, (float)location.Center.Y);
  140. paint.IsAntiAliased = false;
  141. paint.Color = fillColor;
  142. paint.Style = PaintStyle.Fill;
  143. RectD fillRect = ((RectD)location).Inflate(-strokeWidth / 2f);
  144. surf.Canvas.DrawOval(fillRect.Center, fillRect.Size / 2f, paint);
  145. paint.IsAntiAliased = true;
  146. paint.Color = strokeWidth <= 0 ? fillColor : strokeColor;
  147. paint.Style = PaintStyle.Stroke;
  148. paint.StrokeWidth = strokeWidth <= 0 ? 1f : strokeWidth;
  149. RectD strokeRect = ((RectD)location).Inflate((-strokeWidth / 2f));
  150. surf.Canvas.DrawOval(strokeRect.Center, strokeRect.Size / 2f, paint);
  151. surf.Canvas.Restore();
  152. }
  153. public AffectedArea FindAffectedArea(VecI imageSize)
  154. {
  155. ShapeCorners corners = new((RectD)location);
  156. corners = corners.AsRotated(rotation, (VecD)location.Center);
  157. RectI bounds = (RectI)corners.AABBBounds.RoundOutwards();
  158. var chunks = OperationHelper.FindChunksTouchingRectangle(bounds, ChunkyImage.FullChunkSize);
  159. if (fillColor.A == 0)
  160. {
  161. chunks.ExceptWith(OperationHelper.FindChunksFullyInsideEllipse
  162. (location.Center, location.Width / 2.0 - strokeWidth * 2, location.Height / 2.0 - strokeWidth * 2, ChunkyImage.FullChunkSize, rotation));
  163. }
  164. return new AffectedArea(chunks, bounds);
  165. }
  166. public IDrawOperation AsMirrored(double? verAxisX, double? horAxisY)
  167. {
  168. RectD newLocation = location;
  169. if (verAxisX is not null)
  170. newLocation = newLocation.ReflectX((double)verAxisX).Round();
  171. if (horAxisY is not null)
  172. newLocation = newLocation.ReflectY((double)horAxisY).Round();
  173. return new EllipseOperation(newLocation, strokeColor, fillColor, strokeWidth, rotation, antialiased, paint);
  174. }
  175. public void Dispose()
  176. {
  177. paint.Dispose();
  178. outerPath?.Dispose();
  179. innerPath?.Dispose();
  180. }
  181. }