MockRenderInterface.cs 5.8 KB

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