|
@@ -9,14 +9,6 @@ namespace RenderDemo.Pages
|
|
|
{
|
|
{
|
|
|
public class GlyphRunPage : UserControl
|
|
public class GlyphRunPage : UserControl
|
|
|
{
|
|
{
|
|
|
- private Image _imageControl;
|
|
|
|
|
- private GlyphTypeface _glyphTypeface = Typeface.Default.GlyphTypeface;
|
|
|
|
|
- private readonly Random _rand = new Random();
|
|
|
|
|
- private ushort[] _glyphIndices = new ushort[1];
|
|
|
|
|
- private char[] _characters = new char[1];
|
|
|
|
|
- private float _fontSize = 20;
|
|
|
|
|
- private int _direction = 10;
|
|
|
|
|
-
|
|
|
|
|
public GlyphRunPage()
|
|
public GlyphRunPage()
|
|
|
{
|
|
{
|
|
|
this.InitializeComponent();
|
|
this.InitializeComponent();
|
|
@@ -25,19 +17,43 @@ namespace RenderDemo.Pages
|
|
|
private void InitializeComponent()
|
|
private void InitializeComponent()
|
|
|
{
|
|
{
|
|
|
AvaloniaXamlLoader.Load(this);
|
|
AvaloniaXamlLoader.Load(this);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public class GlyphRunControl : Control
|
|
|
|
|
+ {
|
|
|
|
|
+ private GlyphTypeface _glyphTypeface = Typeface.Default.GlyphTypeface;
|
|
|
|
|
+ private readonly Random _rand = new Random();
|
|
|
|
|
+ private ushort[] _glyphIndices = new ushort[1];
|
|
|
|
|
+ private char[] _characters = new char[1];
|
|
|
|
|
+ private float _fontSize = 20;
|
|
|
|
|
+ private int _direction = 10;
|
|
|
|
|
|
|
|
- _imageControl = this.FindControl<Image>("imageControl");
|
|
|
|
|
- _imageControl.Source = new DrawingImage();
|
|
|
|
|
|
|
+ private DispatcherTimer _timer;
|
|
|
|
|
|
|
|
- DispatcherTimer.Run(() =>
|
|
|
|
|
|
|
+ protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
|
|
|
|
+ {
|
|
|
|
|
+ _timer = new DispatcherTimer
|
|
|
|
|
+ {
|
|
|
|
|
+ Interval = TimeSpan.FromSeconds(1)
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ _timer.Tick += (s,e) =>
|
|
|
{
|
|
{
|
|
|
- UpdateGlyphRun();
|
|
|
|
|
|
|
+ InvalidateVisual();
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ _timer.Start();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
|
|
|
|
|
+ {
|
|
|
|
|
+ _timer.Stop();
|
|
|
|
|
|
|
|
- return true;
|
|
|
|
|
- }, TimeSpan.FromSeconds(1));
|
|
|
|
|
|
|
+ _timer = null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private void UpdateGlyphRun()
|
|
|
|
|
|
|
+ public override void Render(DrawingContext context)
|
|
|
{
|
|
{
|
|
|
var c = (char)_rand.Next(65, 90);
|
|
var c = (char)_rand.Next(65, 90);
|
|
|
|
|
|
|
@@ -57,27 +73,70 @@ namespace RenderDemo.Pages
|
|
|
|
|
|
|
|
_characters[0] = c;
|
|
_characters[0] = c;
|
|
|
|
|
|
|
|
- var scale = (double)_fontSize / _glyphTypeface.DesignEmHeight;
|
|
|
|
|
|
|
+ var glyphRun = new GlyphRun(_glyphTypeface, _fontSize, _characters, _glyphIndices);
|
|
|
|
|
|
|
|
- var drawingGroup = new DrawingGroup();
|
|
|
|
|
|
|
+ context.DrawGlyphRun(Brushes.Black, glyphRun);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- var glyphRunDrawing = new GlyphRunDrawing
|
|
|
|
|
|
|
+ public class GlyphRunGeometryControl : Control
|
|
|
|
|
+ {
|
|
|
|
|
+ private GlyphTypeface _glyphTypeface = Typeface.Default.GlyphTypeface;
|
|
|
|
|
+ private readonly Random _rand = new Random();
|
|
|
|
|
+ private ushort[] _glyphIndices = new ushort[1];
|
|
|
|
|
+ private char[] _characters = new char[1];
|
|
|
|
|
+ private float _fontSize = 20;
|
|
|
|
|
+ private int _direction = 10;
|
|
|
|
|
+
|
|
|
|
|
+ private DispatcherTimer _timer;
|
|
|
|
|
+
|
|
|
|
|
+ protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
|
|
|
|
+ {
|
|
|
|
|
+ _timer = new DispatcherTimer
|
|
|
{
|
|
{
|
|
|
- Foreground = Brushes.Black,
|
|
|
|
|
- GlyphRun = new GlyphRun(_glyphTypeface, _fontSize, _characters, _glyphIndices)
|
|
|
|
|
|
|
+ Interval = TimeSpan.FromSeconds(1)
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- drawingGroup.Children.Add(glyphRunDrawing);
|
|
|
|
|
-
|
|
|
|
|
- var geometryDrawing = new GeometryDrawing
|
|
|
|
|
|
|
+ _timer.Tick += (s, e) =>
|
|
|
{
|
|
{
|
|
|
- Pen = new Pen(Brushes.Black),
|
|
|
|
|
- Geometry = new RectangleGeometry { Rect = new Rect(glyphRunDrawing.GlyphRun.Size) }
|
|
|
|
|
|
|
+ InvalidateVisual();
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- drawingGroup.Children.Add(geometryDrawing);
|
|
|
|
|
|
|
+ _timer.Start();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
|
|
|
|
|
+ {
|
|
|
|
|
+ _timer.Stop();
|
|
|
|
|
+
|
|
|
|
|
+ _timer = null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public override void Render(DrawingContext context)
|
|
|
|
|
+ {
|
|
|
|
|
+ var c = (char)_rand.Next(65, 90);
|
|
|
|
|
+
|
|
|
|
|
+ if (_fontSize + _direction > 200)
|
|
|
|
|
+ {
|
|
|
|
|
+ _direction = -10;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (_fontSize + _direction < 20)
|
|
|
|
|
+ {
|
|
|
|
|
+ _direction = 10;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ _fontSize += _direction;
|
|
|
|
|
+
|
|
|
|
|
+ _glyphIndices[0] = _glyphTypeface.GetGlyph(c);
|
|
|
|
|
+
|
|
|
|
|
+ _characters[0] = c;
|
|
|
|
|
+
|
|
|
|
|
+ var glyphRun = new GlyphRun(_glyphTypeface, _fontSize, _characters, _glyphIndices);
|
|
|
|
|
+
|
|
|
|
|
+ var geometry = glyphRun.BuildGeometry();
|
|
|
|
|
|
|
|
- (_imageControl.Source as DrawingImage).Drawing = drawingGroup;
|
|
|
|
|
|
|
+ context.DrawGeometry(Brushes.Green, null, geometry);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|