From e0a2a525ab4534ac60708dea33708092785dce14 Mon Sep 17 00:00:00 2001 From: Tom Laird-McConnell Date: Thu, 7 Nov 2024 10:45:45 -0800 Subject: [PATCH] use TextShaper.Text for render text instead of trying to render from glyphinfos --- .../Drawing/DrawingContextImpl.cs | 2 +- src/Consolonia.Core/Text/TextShaper.cs | 19 +++++++------------ .../Controls/Helpers/SymbolsControl.cs | 4 +++- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/Consolonia.Core/Drawing/DrawingContextImpl.cs b/src/Consolonia.Core/Drawing/DrawingContextImpl.cs index 3cf0998a..964fd0de 100644 --- a/src/Consolonia.Core/Drawing/DrawingContextImpl.cs +++ b/src/Consolonia.Core/Drawing/DrawingContextImpl.cs @@ -208,7 +208,7 @@ public void DrawGlyphRun(IBrush foreground, IGlyphRunImpl glyphRun) return; } - var shapedBuffer = glyphRunImpl.GlyphInfos as ShapedBuffer; + var shapedBuffer = (ShapedBuffer)glyphRunImpl.GlyphInfos; var text = shapedBuffer.Text.ToString(); DrawStringInternal(foreground, text, glyphRun.GlyphTypeface); } diff --git a/src/Consolonia.Core/Text/TextShaper.cs b/src/Consolonia.Core/Text/TextShaper.cs index baa8db20..a5271c87 100644 --- a/src/Consolonia.Core/Text/TextShaper.cs +++ b/src/Consolonia.Core/Text/TextShaper.cs @@ -1,5 +1,7 @@ using System; using System.Linq; +using System.Runtime.InteropServices; +using Avalonia.Controls.Documents; using Avalonia.Media.TextFormatting; using Avalonia.Platform; using NullLib.ConsoleEx; @@ -10,25 +12,18 @@ public class TextShaper : ITextShaperImpl { public ShapedBuffer ShapeText(ReadOnlyMemory text, TextShaperOptions options) { - Text = text.Span.ToString(); - - var glyphInfos = Convert(Text); + var glyphInfos = text.Span.ToString().EnumerateRunes() + .Select((rune, index) => + new GlyphInfo((ushort)rune.Value, index, ConsoleText.IsWideChar((char)rune.Value) ? 2 : 1)).ToArray(); var shapedBuffer = new ShapedBuffer(text, glyphInfos.Length, options.Typeface, 1, 0 /*todo: must be 1 for right to left?*/); - for (int i = 0; i < shapedBuffer.Length; i++) shapedBuffer[i] = glyphInfos[i]; + for (int i = 0; i < shapedBuffer.Length; i++) + shapedBuffer[i] = glyphInfos[i]; return shapedBuffer; } - public string Text { get; set; } - - public static GlyphInfo[] Convert(string text) - { - return text.EnumerateRunes() - .Select((rune, index) => - new GlyphInfo((ushort)rune.Value, index, ConsoleText.IsWideChar((char)rune.Value) ? 2 : 1)).ToArray(); - } } diff --git a/src/Consolonia.Themes.TurboVision/Templates/Controls/Helpers/SymbolsControl.cs b/src/Consolonia.Themes.TurboVision/Templates/Controls/Helpers/SymbolsControl.cs index cf9b73c0..50f73493 100644 --- a/src/Consolonia.Themes.TurboVision/Templates/Controls/Helpers/SymbolsControl.cs +++ b/src/Consolonia.Themes.TurboVision/Templates/Controls/Helpers/SymbolsControl.cs @@ -6,6 +6,7 @@ using Avalonia.Controls; using Avalonia.Data; using Avalonia.Media; +using Avalonia.Media.TextFormatting; using Consolonia.Core.Text; using TextShaper = Consolonia.Core.Text.TextShaper; @@ -69,10 +70,11 @@ public string Text { _text = value; + var glyphs = new TextShaper().ShapeText(value.AsMemory(), new TextShaperOptions(new GlyphTypeface(), 1)); _shapedText = new GlyphRun(new GlyphTypeface(), 1, (_text ?? string.Empty).AsMemory(), - TextShaper.Convert(_text ?? string.Empty).ToImmutableArray(), + glyphs, default(Point)); } }