MockRenderInterface.cs 9.1 KB

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