TextFormatterPage.axaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Markup.Xaml;
  5. using Avalonia.Media;
  6. using Avalonia.Media.TextFormatting;
  7. namespace RenderDemo.Pages
  8. {
  9. public class TextFormatterPage : UserControl
  10. {
  11. private TextLine _textLine;
  12. public TextFormatterPage()
  13. {
  14. this.InitializeComponent();
  15. }
  16. private void InitializeComponent()
  17. {
  18. AvaloniaXamlLoader.Load(this);
  19. }
  20. public override void Render(DrawingContext context)
  21. {
  22. _textLine?.Draw(context, new Point());
  23. }
  24. protected override Size MeasureOverride(Size availableSize)
  25. {
  26. var defaultRunProperties = new GenericTextRunProperties(Typeface.Default, foregroundBrush: Brushes.Black,
  27. baselineAlignment: BaselineAlignment.Center);
  28. var paragraphProperties = new GenericTextParagraphProperties(defaultRunProperties);
  29. var control = new Button { Content = new TextBlock { Text = "ClickMe" } };
  30. Content = control;
  31. var textSource = new CustomTextSource(control, defaultRunProperties);
  32. control.Measure(Size.Infinity);
  33. _textLine =
  34. TextFormatter.Current.FormatLine(textSource, 0, double.PositiveInfinity, paragraphProperties);
  35. return base.MeasureOverride(availableSize);
  36. }
  37. protected override Size ArrangeOverride(Size finalSize)
  38. {
  39. var currentX = 0d;
  40. foreach (var textRun in _textLine.TextRuns)
  41. {
  42. if (textRun is ControlRun controlRun)
  43. {
  44. controlRun.Control.Arrange(new Rect(new Point(currentX, 0), controlRun.Size));
  45. }
  46. if (textRun is DrawableTextRun drawableTextRun)
  47. {
  48. currentX += drawableTextRun.Size.Width;
  49. }
  50. }
  51. return finalSize;
  52. }
  53. private class CustomTextSource : ITextSource
  54. {
  55. private readonly Control _control;
  56. private readonly TextRunProperties _defaultProperties;
  57. private readonly string _text = "<-Hello World->";
  58. public CustomTextSource(Control control, TextRunProperties defaultProperties)
  59. {
  60. _control = control;
  61. _defaultProperties = defaultProperties;
  62. }
  63. public TextRun GetTextRun(int textSourceIndex)
  64. {
  65. if (textSourceIndex >= _text.Length * 2 + TextRun.DefaultTextSourceLength)
  66. {
  67. return null;
  68. }
  69. if (textSourceIndex == _text.Length)
  70. {
  71. return new ControlRun(_control, _defaultProperties);
  72. }
  73. return new TextCharacters(_text, _defaultProperties);
  74. }
  75. }
  76. private class ControlRun : DrawableTextRun
  77. {
  78. private readonly Control _control;
  79. public ControlRun(Control control, TextRunProperties properties)
  80. {
  81. _control = control;
  82. Properties = properties;
  83. }
  84. public Control Control => _control;
  85. public override Size Size => _control.DesiredSize;
  86. public override double Baseline => 0;
  87. public override TextRunProperties Properties { get; }
  88. public override void Draw(DrawingContext drawingContext, Point origin)
  89. {
  90. // noop
  91. }
  92. }
  93. }
  94. }