GeometryImpl.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Avalonia.Platform;
  4. using SharpDX.Direct2D1;
  5. namespace Avalonia.Direct2D1.Media
  6. {
  7. /// <summary>
  8. /// The platform-specific interface for <see cref="Avalonia.Media.Geometry"/>.
  9. /// </summary>
  10. public abstract class GeometryImpl : IGeometryImpl
  11. {
  12. public GeometryImpl(Geometry geometry)
  13. {
  14. Geometry = geometry;
  15. }
  16. /// <inheritdoc/>
  17. public Rect Bounds => Geometry.GetWidenedBounds(0).ToAvalonia();
  18. public Geometry Geometry { get; }
  19. /// <inheritdoc/>
  20. public Rect GetRenderBounds(Avalonia.Media.Pen pen)
  21. {
  22. return Geometry.GetWidenedBounds((float)(pen?.Thickness ?? 0)).ToAvalonia();
  23. }
  24. /// <inheritdoc/>
  25. public bool FillContains(Point point)
  26. {
  27. return Geometry.FillContainsPoint(point.ToSharpDX());
  28. }
  29. /// <inheritdoc/>
  30. public IGeometryImpl Intersect(IGeometryImpl geometry)
  31. {
  32. var result = new PathGeometry(Geometry.Factory);
  33. using (var sink = result.Open())
  34. {
  35. Geometry.Combine(((GeometryImpl)geometry).Geometry, CombineMode.Intersect, sink);
  36. return new StreamGeometryImpl(result);
  37. }
  38. }
  39. /// <inheritdoc/>
  40. public bool StrokeContains(Avalonia.Media.Pen pen, Point point)
  41. {
  42. return Geometry.StrokeContainsPoint(point.ToSharpDX(), (float)(pen?.Thickness ?? 0));
  43. }
  44. public ITransformedGeometryImpl WithTransform(Matrix transform)
  45. {
  46. var factory = AvaloniaLocator.Current.GetService<Factory>();
  47. return new TransformedGeometryImpl(
  48. new TransformedGeometry(
  49. factory,
  50. GetSourceGeometry(),
  51. transform.ToDirect2D()),
  52. this);
  53. }
  54. protected virtual Geometry GetSourceGeometry() => Geometry;
  55. }
  56. }