MockRenderInterface.cs 5.6 KB

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