MockRenderInterface.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Avalonia.Media;
  5. using Avalonia.Platform;
  6. using Avalonia.UnitTests;
  7. namespace Avalonia.Visuals.UnitTests.VisualTree
  8. {
  9. class MockRenderInterface : IPlatformRenderInterface
  10. {
  11. public IFormattedTextImpl CreateFormattedText(
  12. string text,
  13. Typeface typeface,
  14. double fontSize,
  15. TextAlignment textAlignment,
  16. TextWrapping wrapping,
  17. Size constraint,
  18. IReadOnlyList<FormattedTextStyleSpan> spans)
  19. {
  20. throw new NotImplementedException();
  21. }
  22. public IRenderTarget CreateRenderTarget(IEnumerable<object> surfaces)
  23. {
  24. throw new NotImplementedException();
  25. }
  26. public IRenderTargetBitmapImpl CreateRenderTargetBitmap(PixelSize size, Vector dpi)
  27. {
  28. throw new NotImplementedException();
  29. }
  30. public IStreamGeometryImpl CreateStreamGeometry()
  31. {
  32. return new MockStreamGeometry();
  33. }
  34. public IBitmapImpl LoadBitmap(Stream stream)
  35. {
  36. throw new NotImplementedException();
  37. }
  38. public IBitmapImpl LoadBitmap(string fileName)
  39. {
  40. throw new NotImplementedException();
  41. }
  42. public IBitmapImpl LoadBitmap(PixelFormat format, IntPtr data, PixelSize size, Vector dpi, int stride)
  43. {
  44. throw new NotImplementedException();
  45. }
  46. public IGlyphRunImpl CreateGlyphRun(GlyphRun glyphRun, out double width)
  47. {
  48. throw new NotImplementedException();
  49. }
  50. public IFontManagerImpl CreateFontManager()
  51. {
  52. return new MockFontManagerImpl();
  53. }
  54. public IWriteableBitmapImpl CreateWriteableBitmap(PixelSize size, Vector dpi, PixelFormat? fmt)
  55. {
  56. throw new NotImplementedException();
  57. }
  58. public IGeometryImpl CreateEllipseGeometry(Rect rect)
  59. {
  60. throw new NotImplementedException();
  61. }
  62. public IGeometryImpl CreateLineGeometry(Point p1, Point p2)
  63. {
  64. throw new NotImplementedException();
  65. }
  66. public IGeometryImpl CreateRectangleGeometry(Rect rect)
  67. {
  68. throw new NotImplementedException();
  69. }
  70. class MockStreamGeometry : IStreamGeometryImpl
  71. {
  72. private MockStreamGeometryContext _impl = new MockStreamGeometryContext();
  73. public Rect Bounds
  74. {
  75. get
  76. {
  77. throw new NotImplementedException();
  78. }
  79. }
  80. public IStreamGeometryImpl Clone()
  81. {
  82. return this;
  83. }
  84. public void Dispose()
  85. {
  86. }
  87. public bool FillContains(Point point)
  88. {
  89. return _impl.FillContains(point);
  90. }
  91. public Rect GetRenderBounds(IPen pen)
  92. {
  93. throw new NotImplementedException();
  94. }
  95. public IGeometryImpl Intersect(IGeometryImpl geometry)
  96. {
  97. throw new NotImplementedException();
  98. }
  99. public IStreamGeometryContextImpl Open()
  100. {
  101. return _impl;
  102. }
  103. public bool StrokeContains(IPen pen, Point point)
  104. {
  105. throw new NotImplementedException();
  106. }
  107. public ITransformedGeometryImpl WithTransform(Matrix transform)
  108. {
  109. throw new NotImplementedException();
  110. }
  111. class MockStreamGeometryContext : IStreamGeometryContextImpl
  112. {
  113. private List<Point> points = new List<Point>();
  114. public void ArcTo(Point point, Size size, double rotationAngle, bool isLargeArc, SweepDirection sweepDirection)
  115. {
  116. throw new NotImplementedException();
  117. }
  118. public void BeginFigure(Point startPoint, bool isFilled)
  119. {
  120. points.Add(startPoint);
  121. }
  122. public void CubicBezierTo(Point point1, Point point2, Point point3)
  123. {
  124. throw new NotImplementedException();
  125. }
  126. public void Dispose()
  127. {
  128. }
  129. public void EndFigure(bool isClosed)
  130. {
  131. }
  132. public void LineTo(Point point)
  133. {
  134. points.Add(point);
  135. }
  136. public void QuadraticBezierTo(Point control, Point endPoint)
  137. {
  138. throw new NotImplementedException();
  139. }
  140. public void SetFillRule(FillRule fillRule)
  141. {
  142. }
  143. public bool FillContains(Point point)
  144. {
  145. // Use the algorithm from http://www.blackpawn.com/texts/pointinpoly/default.html
  146. // to determine if the point is in the geometry (since it will always be convex in this situation)
  147. for (int i = 0; i < points.Count; i++)
  148. {
  149. var a = points[i];
  150. var b = points[(i + 1) % points.Count];
  151. var c = points[(i + 2) % points.Count];
  152. Vector v0 = c - a;
  153. Vector v1 = b - a;
  154. Vector v2 = point - a;
  155. var dot00 = v0 * v0;
  156. var dot01 = v0 * v1;
  157. var dot02 = v0 * v2;
  158. var dot11 = v1 * v1;
  159. var dot12 = v1 * v2;
  160. var invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
  161. var u = (dot11 * dot02 - dot01 * dot12) * invDenom;
  162. var v = (dot00 * dot12 - dot01 * dot02) * invDenom;
  163. if ((u >= 0) && (v >= 0) && (u + v < 1)) return true;
  164. }
  165. return false;
  166. }
  167. }
  168. }
  169. }
  170. }