MockRenderInterface.cs 9.1 KB

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