MockRenderInterface.cs 5.5 KB

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