MockRenderInterface.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. using Avalonia.Media.Imaging;
  8. namespace Avalonia.Base.UnitTests.VisualTree
  9. {
  10. class MockRenderInterface : IPlatformRenderInterface, IPlatformRenderInterfaceContext
  11. {
  12. public IRenderTarget CreateRenderTarget(IEnumerable<object> surfaces)
  13. {
  14. throw new NotImplementedException();
  15. }
  16. public bool IsLost => false;
  17. public object TryGetFeature(Type featureType) => null;
  18. public IRenderTargetBitmapImpl CreateRenderTargetBitmap(PixelSize size, Vector dpi)
  19. {
  20. throw new NotImplementedException();
  21. }
  22. public IStreamGeometryImpl CreateStreamGeometry()
  23. {
  24. return new MockStreamGeometry();
  25. }
  26. public IGeometryImpl CreateGeometryGroup(FillRule fillRule, IReadOnlyList<Geometry> children)
  27. {
  28. throw new NotImplementedException();
  29. }
  30. public IGeometryImpl CreateCombinedGeometry(GeometryCombineMode combineMode, Geometry g1, Geometry g2)
  31. {
  32. throw new NotImplementedException();
  33. }
  34. public IBitmapImpl LoadBitmap(Stream stream)
  35. {
  36. throw new NotImplementedException();
  37. }
  38. public IWriteableBitmapImpl LoadWriteableBitmapToWidth(Stream stream, int width,
  39. BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality)
  40. {
  41. throw new NotImplementedException();
  42. }
  43. public IWriteableBitmapImpl LoadWriteableBitmapToHeight(Stream stream, int height,
  44. BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality)
  45. {
  46. throw new NotImplementedException();
  47. }
  48. public IWriteableBitmapImpl LoadWriteableBitmap(string fileName)
  49. {
  50. throw new NotImplementedException();
  51. }
  52. public IWriteableBitmapImpl LoadWriteableBitmap(Stream stream)
  53. {
  54. throw new NotImplementedException();
  55. }
  56. public IBitmapImpl LoadBitmap(string fileName)
  57. {
  58. throw new NotImplementedException();
  59. }
  60. public IBitmapImpl LoadBitmap(PixelFormat format, AlphaFormat alphaFormat, IntPtr data, PixelSize size, Vector dpi, int stride)
  61. {
  62. throw new NotImplementedException();
  63. }
  64. public IGlyphRunImpl CreateGlyphRun(IGlyphTypeface glyphTypeface, double fontRenderingEmSize, IReadOnlyList<ushort> glyphIndices, IReadOnlyList<double> glyphAdvances, IReadOnlyList<Vector> glyphOffsets)
  65. {
  66. throw new NotImplementedException();
  67. }
  68. public IPlatformRenderInterfaceContext CreateBackendContext(IPlatformGraphicsContext graphicsContext)
  69. {
  70. return this;
  71. }
  72. public bool SupportsIndividualRoundRects { get; set; }
  73. public AlphaFormat DefaultAlphaFormat { get; }
  74. public PixelFormat DefaultPixelFormat { get; }
  75. public IFontManagerImpl CreateFontManager()
  76. {
  77. return new MockFontManagerImpl();
  78. }
  79. public IWriteableBitmapImpl CreateWriteableBitmap(PixelSize size, Vector dpi, PixelFormat fmt, AlphaFormat alphaFormat)
  80. {
  81. throw new NotImplementedException();
  82. }
  83. public IGeometryImpl CreateEllipseGeometry(Rect rect)
  84. {
  85. throw new NotImplementedException();
  86. }
  87. public IGeometryImpl CreateLineGeometry(Point p1, Point p2)
  88. {
  89. throw new NotImplementedException();
  90. }
  91. public IGeometryImpl CreateRectangleGeometry(Rect rect)
  92. {
  93. throw new NotImplementedException();
  94. }
  95. public IBitmapImpl LoadBitmapToWidth(Stream stream, int width, BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality)
  96. {
  97. throw new NotImplementedException();
  98. }
  99. public IBitmapImpl LoadBitmapToHeight(Stream stream, int height, BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality)
  100. {
  101. throw new NotImplementedException();
  102. }
  103. public IBitmapImpl ResizeBitmap(IBitmapImpl bitmapImpl, PixelSize destinationSize, BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality)
  104. {
  105. throw new NotImplementedException();
  106. }
  107. public IGeometryImpl BuildGlyphRunGeometry(GlyphRun glyphRun)
  108. {
  109. throw new NotImplementedException();
  110. }
  111. class MockStreamGeometry : IStreamGeometryImpl
  112. {
  113. private MockStreamGeometryContext _impl = new MockStreamGeometryContext();
  114. public Rect Bounds
  115. {
  116. get
  117. {
  118. throw new NotImplementedException();
  119. }
  120. }
  121. public double ContourLength { get; }
  122. public IStreamGeometryImpl Clone()
  123. {
  124. return this;
  125. }
  126. public void Dispose()
  127. {
  128. }
  129. public bool FillContains(Point point)
  130. {
  131. return _impl.FillContains(point);
  132. }
  133. public Rect GetRenderBounds(IPen pen)
  134. {
  135. throw new NotImplementedException();
  136. }
  137. public IGeometryImpl Intersect(IGeometryImpl geometry)
  138. {
  139. throw new NotImplementedException();
  140. }
  141. public IStreamGeometryContextImpl Open()
  142. {
  143. return _impl;
  144. }
  145. public bool StrokeContains(IPen pen, Point point)
  146. {
  147. throw new NotImplementedException();
  148. }
  149. public ITransformedGeometryImpl WithTransform(Matrix transform)
  150. {
  151. throw new NotImplementedException();
  152. }
  153. public bool TryGetPointAtDistance(double distance, out Point point)
  154. {
  155. throw new NotImplementedException();
  156. }
  157. public bool TryGetPointAndTangentAtDistance(double distance, out Point point, out Point tangent)
  158. {
  159. throw new NotImplementedException();
  160. }
  161. public bool TryGetSegment(double startDistance, double stopDistance, bool startOnBeginFigure, out IGeometryImpl segmentGeometry)
  162. {
  163. throw new NotImplementedException();
  164. }
  165. class MockStreamGeometryContext : IStreamGeometryContextImpl
  166. {
  167. private List<Point> points = new List<Point>();
  168. public void ArcTo(Point point, Size size, double rotationAngle, bool isLargeArc, SweepDirection sweepDirection)
  169. {
  170. throw new NotImplementedException();
  171. }
  172. public void BeginFigure(Point startPoint, bool isFilled)
  173. {
  174. points.Add(startPoint);
  175. }
  176. public void CubicBezierTo(Point point1, Point point2, Point point3)
  177. {
  178. throw new NotImplementedException();
  179. }
  180. public void Dispose()
  181. {
  182. }
  183. public void EndFigure(bool isClosed)
  184. {
  185. }
  186. public void LineTo(Point point)
  187. {
  188. points.Add(point);
  189. }
  190. public void QuadraticBezierTo(Point control, Point endPoint)
  191. {
  192. throw new NotImplementedException();
  193. }
  194. public void SetFillRule(FillRule fillRule)
  195. {
  196. }
  197. public bool FillContains(Point point)
  198. {
  199. // Use the algorithm from https://www.blackpawn.com/texts/pointinpoly/default.html
  200. // to determine if the point is in the geometry (since it will always be convex in this situation)
  201. for (int i = 0; i < points.Count; i++)
  202. {
  203. var a = points[i];
  204. var b = points[(i + 1) % points.Count];
  205. var c = points[(i + 2) % points.Count];
  206. Vector v0 = c - a;
  207. Vector v1 = b - a;
  208. Vector v2 = point - a;
  209. var dot00 = v0 * v0;
  210. var dot01 = v0 * v1;
  211. var dot02 = v0 * v2;
  212. var dot11 = v1 * v1;
  213. var dot12 = v1 * v2;
  214. var invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
  215. var u = (dot11 * dot02 - dot01 * dot12) * invDenom;
  216. var v = (dot00 * dot12 - dot01 * dot02) * invDenom;
  217. if ((u >= 0) && (v >= 0) && (u + v < 1)) return true;
  218. }
  219. return false;
  220. }
  221. }
  222. }
  223. public void Dispose()
  224. {
  225. }
  226. }
  227. }