MockRenderInterface.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. class MockStreamGeometry : IStreamGeometryImpl
  50. {
  51. private MockStreamGeometryContext _impl = new MockStreamGeometryContext();
  52. public Rect Bounds
  53. {
  54. get
  55. {
  56. throw new NotImplementedException();
  57. }
  58. }
  59. public IStreamGeometryImpl Clone()
  60. {
  61. return this;
  62. }
  63. public void Dispose()
  64. {
  65. }
  66. public bool FillContains(Point point)
  67. {
  68. return _impl.FillContains(point);
  69. }
  70. public Rect GetRenderBounds(Pen pen)
  71. {
  72. throw new NotImplementedException();
  73. }
  74. public IGeometryImpl Intersect(IGeometryImpl geometry)
  75. {
  76. throw new NotImplementedException();
  77. }
  78. public IStreamGeometryContextImpl Open()
  79. {
  80. return _impl;
  81. }
  82. public bool StrokeContains(Pen pen, Point point)
  83. {
  84. throw new NotImplementedException();
  85. }
  86. public ITransformedGeometryImpl WithTransform(Matrix transform)
  87. {
  88. throw new NotImplementedException();
  89. }
  90. class MockStreamGeometryContext : IStreamGeometryContextImpl
  91. {
  92. private List<Point> points = new List<Point>();
  93. public void ArcTo(Point point, Size size, double rotationAngle, bool isLargeArc, SweepDirection sweepDirection)
  94. {
  95. throw new NotImplementedException();
  96. }
  97. public void BeginFigure(Point startPoint, bool isFilled)
  98. {
  99. points.Add(startPoint);
  100. }
  101. public void CubicBezierTo(Point point1, Point point2, Point point3)
  102. {
  103. throw new NotImplementedException();
  104. }
  105. public void Dispose()
  106. {
  107. }
  108. public void EndFigure(bool isClosed)
  109. {
  110. }
  111. public void LineTo(Point point)
  112. {
  113. points.Add(point);
  114. }
  115. public void QuadraticBezierTo(Point control, Point endPoint)
  116. {
  117. throw new NotImplementedException();
  118. }
  119. public void SetFillRule(FillRule fillRule)
  120. {
  121. }
  122. public bool FillContains(Point point)
  123. {
  124. // Use the algorithm from http://www.blackpawn.com/texts/pointinpoly/default.html
  125. // to determine if the point is in the geometry (since it will always be convex in this situation)
  126. for (int i = 0; i < points.Count; i++)
  127. {
  128. var a = points[i];
  129. var b = points[(i + 1) % points.Count];
  130. var c = points[(i + 2) % points.Count];
  131. Vector v0 = c - a;
  132. Vector v1 = b - a;
  133. Vector v2 = point - a;
  134. var dot00 = v0 * v0;
  135. var dot01 = v0 * v1;
  136. var dot02 = v0 * v2;
  137. var dot11 = v1 * v1;
  138. var dot12 = v1 * v2;
  139. var invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
  140. var u = (dot11 * dot02 - dot01 * dot12) * invDenom;
  141. var v = (dot00 * dot12 - dot01 * dot02) * invDenom;
  142. if ((u >= 0) && (v >= 0) && (u + v < 1)) return true;
  143. }
  144. return false;
  145. }
  146. }
  147. }
  148. }
  149. }