committed by
GitHub
90 changed files with 2410 additions and 1201 deletions
@ -0,0 +1,7 @@ |
|||
<UserControl xmlns="https://github.com/avaloniaui" |
|||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
|||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" |
|||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" |
|||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" |
|||
x:Class="RenderDemo.Pages.TextFormatterPage"> |
|||
</UserControl> |
|||
@ -0,0 +1,118 @@ |
|||
using System; |
|||
using Avalonia; |
|||
using Avalonia.Controls; |
|||
using Avalonia.Markup.Xaml; |
|||
using Avalonia.Media; |
|||
using Avalonia.Media.TextFormatting; |
|||
|
|||
namespace RenderDemo.Pages |
|||
{ |
|||
public class TextFormatterPage : UserControl |
|||
{ |
|||
private TextLine _textLine; |
|||
|
|||
public TextFormatterPage() |
|||
{ |
|||
this.InitializeComponent(); |
|||
} |
|||
|
|||
private void InitializeComponent() |
|||
{ |
|||
AvaloniaXamlLoader.Load(this); |
|||
} |
|||
|
|||
public override void Render(DrawingContext context) |
|||
{ |
|||
_textLine?.Draw(context, new Point()); |
|||
} |
|||
|
|||
protected override Size MeasureOverride(Size availableSize) |
|||
{ |
|||
var defaultRunProperties = new GenericTextRunProperties(Typeface.Default, foregroundBrush: Brushes.Black, |
|||
baselineAlignment: BaselineAlignment.Center); |
|||
var paragraphProperties = new GenericTextParagraphProperties(defaultRunProperties); |
|||
|
|||
var control = new Button { Content = new TextBlock { Text = "ClickMe" } }; |
|||
|
|||
Content = control; |
|||
|
|||
var textSource = new CustomTextSource(control, defaultRunProperties); |
|||
|
|||
control.Measure(Size.Infinity); |
|||
|
|||
_textLine = |
|||
TextFormatter.Current.FormatLine(textSource, 0, double.PositiveInfinity, paragraphProperties); |
|||
|
|||
return base.MeasureOverride(availableSize); |
|||
} |
|||
|
|||
protected override Size ArrangeOverride(Size finalSize) |
|||
{ |
|||
var currentX = 0d; |
|||
|
|||
foreach (var textRun in _textLine.TextRuns) |
|||
{ |
|||
if (textRun is ControlRun controlRun) |
|||
{ |
|||
controlRun.Control.Arrange(new Rect(new Point(currentX, 0), controlRun.Size)); |
|||
} |
|||
|
|||
if (textRun is DrawableTextRun drawableTextRun) |
|||
{ |
|||
currentX += drawableTextRun.Size.Width; |
|||
} |
|||
} |
|||
|
|||
return finalSize; |
|||
} |
|||
|
|||
private class CustomTextSource : ITextSource |
|||
{ |
|||
private readonly Control _control; |
|||
private readonly TextRunProperties _defaultProperties; |
|||
private readonly string _text = "<-Hello World->"; |
|||
|
|||
public CustomTextSource(Control control, TextRunProperties defaultProperties) |
|||
{ |
|||
_control = control; |
|||
_defaultProperties = defaultProperties; |
|||
} |
|||
|
|||
public TextRun? GetTextRun(int textSourceIndex) |
|||
{ |
|||
if (textSourceIndex >= _text.Length * 2 + TextRun.DefaultTextSourceLength) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
if (textSourceIndex == _text.Length) |
|||
{ |
|||
return new ControlRun(_control, _defaultProperties); |
|||
} |
|||
|
|||
return new TextCharacters(_text.AsMemory(), _defaultProperties); |
|||
} |
|||
} |
|||
|
|||
private class ControlRun : DrawableTextRun |
|||
{ |
|||
private readonly Control _control; |
|||
|
|||
public ControlRun(Control control, TextRunProperties properties) |
|||
{ |
|||
_control = control; |
|||
Properties = properties; |
|||
} |
|||
|
|||
public Control Control => _control; |
|||
public override Size Size => _control.DesiredSize; |
|||
public override double Baseline => 0; |
|||
public override TextRunProperties? Properties { get; } |
|||
|
|||
public override void Draw(DrawingContext drawingContext, Point origin) |
|||
{ |
|||
// noop
|
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Avalonia.Media.TextFormatting |
|||
{ |
|||
/// <summary>
|
|||
/// The bounding rectangle of a range of characters
|
|||
/// </summary>
|
|||
public sealed class TextBounds |
|||
{ |
|||
/// <summary>
|
|||
/// Constructing TextBounds object
|
|||
/// </summary>
|
|||
internal TextBounds(Rect bounds, FlowDirection flowDirection) |
|||
{ |
|||
Rectangle = bounds; |
|||
FlowDirection = flowDirection; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Bounds rectangle
|
|||
/// </summary>
|
|||
public Rect Rectangle { get; } |
|||
|
|||
/// <summary>
|
|||
/// Text flow direction inside the boundary rectangle
|
|||
/// </summary>
|
|||
public FlowDirection FlowDirection { get; } |
|||
} |
|||
} |
|||
File diff suppressed because it is too large
@ -0,0 +1,49 @@ |
|||
using System.Globalization; |
|||
|
|||
namespace Avalonia.Media.TextFormatting |
|||
{ |
|||
/// <summary>
|
|||
/// Options to customize text shaping.
|
|||
/// </summary>
|
|||
public readonly struct TextShaperOptions |
|||
{ |
|||
public TextShaperOptions( |
|||
GlyphTypeface typeface, |
|||
double fontRenderingEmSize = 12, |
|||
sbyte bidiLevel = 0, |
|||
CultureInfo? culture = null, |
|||
double incrementalTabWidth = 0) |
|||
{ |
|||
Typeface = typeface; |
|||
FontRenderingEmSize = fontRenderingEmSize; |
|||
BidLevel = bidiLevel; |
|||
Culture = culture; |
|||
IncrementalTabWidth = incrementalTabWidth; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Get the typeface.
|
|||
/// </summary>
|
|||
public GlyphTypeface Typeface { get; } |
|||
/// <summary>
|
|||
/// Get the font rendering em size.
|
|||
/// </summary>
|
|||
public double FontRenderingEmSize { get; } |
|||
|
|||
/// <summary>
|
|||
/// Get the bidi level of the text.
|
|||
/// </summary>
|
|||
public sbyte BidLevel { get; } |
|||
|
|||
/// <summary>
|
|||
/// Get the culture.
|
|||
/// </summary>
|
|||
public CultureInfo? Culture { get; } |
|||
|
|||
/// <summary>
|
|||
/// Get the incremental tab width.
|
|||
/// </summary>
|
|||
public double IncrementalTabWidth { get; } |
|||
|
|||
} |
|||
} |
|||
Loading…
Reference in new issue