MockRenderInterface.cs 5.4 KB

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