MockRenderInterface.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Avalonia.Media;
  5. using Avalonia.Platform;
  6. namespace Avalonia.Visuals.UnitTests.VisualTree
  7. {
  8. class MockRenderInterface : IPlatformRenderInterface
  9. {
  10. public IFormattedTextImpl CreateFormattedText(
  11. string text,
  12. string fontFamilyName,
  13. double fontSize,
  14. FontStyle fontStyle,
  15. TextAlignment textAlignment,
  16. FontWeight fontWeight,
  17. TextWrapping wrapping,
  18. Size constraint)
  19. {
  20. throw new NotImplementedException();
  21. }
  22. public IRenderTarget CreateRenderTarget(IPlatformHandle handle)
  23. {
  24. throw new NotImplementedException();
  25. }
  26. public IRenderTargetBitmapImpl CreateRenderTargetBitmap(
  27. int width,
  28. int height,
  29. double dpiX,
  30. double dpiY)
  31. {
  32. throw new NotImplementedException();
  33. }
  34. public IStreamGeometryImpl CreateStreamGeometry()
  35. {
  36. return new MockStreamGeometry();
  37. }
  38. public IBitmapImpl LoadBitmap(Stream stream)
  39. {
  40. throw new NotImplementedException();
  41. }
  42. public IBitmapImpl LoadBitmap(string fileName)
  43. {
  44. throw new NotImplementedException();
  45. }
  46. class MockStreamGeometry : IStreamGeometryImpl
  47. {
  48. private MockStreamGeometryContext _impl = new MockStreamGeometryContext();
  49. public Rect Bounds => _impl.CalculateBounds();
  50. public Matrix Transform { get; set; }
  51. public IStreamGeometryImpl Clone()
  52. {
  53. return this;
  54. }
  55. public bool FillContains(Point point)
  56. {
  57. return _impl.FillContains(point);
  58. }
  59. public bool StrokeContains(Pen pen, Point point)
  60. {
  61. return false;
  62. }
  63. public Rect GetRenderBounds(double strokeThickness) => Bounds;
  64. public IStreamGeometryContextImpl Open()
  65. {
  66. return _impl;
  67. }
  68. public IGeometryImpl WithTransform(Matrix transform)
  69. {
  70. return this;
  71. }
  72. class MockStreamGeometryContext : IStreamGeometryContextImpl
  73. {
  74. private List<Point> points = new List<Point>();
  75. public void ArcTo(Point point, Size size, double rotationAngle, bool isLargeArc, SweepDirection sweepDirection)
  76. {
  77. throw new NotImplementedException();
  78. }
  79. public void BeginFigure(Point startPoint, bool isFilled)
  80. {
  81. points.Add(startPoint);
  82. }
  83. public Rect CalculateBounds()
  84. {
  85. var left = double.MaxValue;
  86. var right = double.MinValue;
  87. var top = double.MaxValue;
  88. var bottom = double.MinValue;
  89. foreach (var p in points)
  90. {
  91. left = Math.Min(p.X, left);
  92. right = Math.Max(p.X, right);
  93. top = Math.Min(p.Y, top);
  94. bottom = Math.Max(p.Y, bottom);
  95. }
  96. return new Rect(new Point(left, top), new Point(right, bottom));
  97. }
  98. public void CubicBezierTo(Point point1, Point point2, Point point3)
  99. {
  100. throw new NotImplementedException();
  101. }
  102. public void Dispose()
  103. {
  104. }
  105. public void EndFigure(bool isClosed)
  106. {
  107. }
  108. public void LineTo(Point point)
  109. {
  110. points.Add(point);
  111. }
  112. public void QuadraticBezierTo(Point control, Point endPoint)
  113. {
  114. throw new NotImplementedException();
  115. }
  116. public void SetFillRule(FillRule fillRule)
  117. {
  118. }
  119. public bool FillContains(Point point)
  120. {
  121. // Use the algorithm from http://www.blackpawn.com/texts/pointinpoly/default.html
  122. // to determine if the point is in the geometry (since it will always be convex in this situation)
  123. for (int i = 0; i < points.Count; i++)
  124. {
  125. var a = points[i];
  126. var b = points[(i + 1) % points.Count];
  127. var c = points[(i + 2) % points.Count];
  128. Vector v0 = c - a;
  129. Vector v1 = b - a;
  130. Vector v2 = point - a;
  131. var dot00 = v0 * v0;
  132. var dot01 = v0 * v1;
  133. var dot02 = v0 * v2;
  134. var dot11 = v1 * v1;
  135. var dot12 = v1 * v2;
  136. var invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
  137. var u = (dot11 * dot02 - dot01 * dot12) * invDenom;
  138. var v = (dot00 * dot12 - dot01 * dot02) * invDenom;
  139. if ((u >= 0) && (v >= 0) && (u + v < 1)) return true;
  140. }
  141. return false;
  142. }
  143. }
  144. }
  145. }
  146. }