GlyphRunPage.xaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Markup.Xaml;
  5. using Avalonia.Media;
  6. using Avalonia.Threading;
  7. namespace RenderDemo.Pages
  8. {
  9. public class GlyphRunPage : UserControl
  10. {
  11. public GlyphRunPage()
  12. {
  13. this.InitializeComponent();
  14. }
  15. private void InitializeComponent()
  16. {
  17. AvaloniaXamlLoader.Load(this);
  18. }
  19. }
  20. public class GlyphRunControl : Control
  21. {
  22. private IGlyphTypeface _glyphTypeface = Typeface.Default.GlyphTypeface;
  23. private readonly Random _rand = new Random();
  24. private ushort[] _glyphIndices = new ushort[1];
  25. private char[] _characters = new char[1];
  26. private float _fontSize = 20;
  27. private int _direction = 10;
  28. private DispatcherTimer _timer;
  29. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  30. {
  31. _timer = new DispatcherTimer
  32. {
  33. Interval = TimeSpan.FromSeconds(1)
  34. };
  35. _timer.Tick += (s,e) =>
  36. {
  37. InvalidateVisual();
  38. };
  39. _timer.Start();
  40. }
  41. protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
  42. {
  43. _timer.Stop();
  44. _timer = null;
  45. }
  46. public override void Render(DrawingContext context)
  47. {
  48. var c = (char)_rand.Next(65, 90);
  49. if (_fontSize + _direction > 200)
  50. {
  51. _direction = -10;
  52. }
  53. if (_fontSize + _direction < 20)
  54. {
  55. _direction = 10;
  56. }
  57. _fontSize += _direction;
  58. _glyphIndices[0] = _glyphTypeface.GetGlyph(c);
  59. _characters[0] = c;
  60. var glyphRun = new GlyphRun(_glyphTypeface, _fontSize, _characters, _glyphIndices);
  61. context.DrawGlyphRun(Brushes.Black, glyphRun);
  62. }
  63. }
  64. public class GlyphRunGeometryControl : Control
  65. {
  66. private IGlyphTypeface _glyphTypeface = Typeface.Default.GlyphTypeface;
  67. private readonly Random _rand = new Random();
  68. private ushort[] _glyphIndices = new ushort[1];
  69. private char[] _characters = new char[1];
  70. private float _fontSize = 20;
  71. private int _direction = 10;
  72. private DispatcherTimer _timer;
  73. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  74. {
  75. _timer = new DispatcherTimer
  76. {
  77. Interval = TimeSpan.FromSeconds(1)
  78. };
  79. _timer.Tick += (s, e) =>
  80. {
  81. InvalidateVisual();
  82. };
  83. _timer.Start();
  84. }
  85. protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
  86. {
  87. _timer.Stop();
  88. _timer = null;
  89. }
  90. public override void Render(DrawingContext context)
  91. {
  92. var c = (char)_rand.Next(65, 90);
  93. if (_fontSize + _direction > 200)
  94. {
  95. _direction = -10;
  96. }
  97. if (_fontSize + _direction < 20)
  98. {
  99. _direction = 10;
  100. }
  101. _fontSize += _direction;
  102. _glyphIndices[0] = _glyphTypeface.GetGlyph(c);
  103. _characters[0] = c;
  104. var glyphRun = new GlyphRun(_glyphTypeface, _fontSize, _characters, _glyphIndices);
  105. var geometry = glyphRun.BuildGeometry();
  106. context.DrawGeometry(Brushes.Green, null, geometry);
  107. }
  108. }
  109. }