MockRenderInterface.cs 7.0 KB

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