Skip to content

Commit

Permalink
use TextShaper.Text for render text instead of trying to render from …
Browse files Browse the repository at this point in the history
…glyphinfos
  • Loading branch information
tomlm committed Nov 7, 2024
1 parent ebc751d commit e0a2a52
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Consolonia.Core/Drawing/DrawingContextImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
19 changes: 7 additions & 12 deletions src/Consolonia.Core/Text/TextShaper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Linq;
using System.Runtime.InteropServices;

Check warning on line 3 in src/Consolonia.Core/Text/TextShaper.cs

View workflow job for this annotation

GitHub Actions / build

"[RedundantUsingDirective] Using directive is not required by the code and can be safely removed" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Text/TextShaper.cs(3,1)
using Avalonia.Controls.Documents;

Check warning on line 4 in src/Consolonia.Core/Text/TextShaper.cs

View workflow job for this annotation

GitHub Actions / build

"[RedundantUsingDirective] Using directive is not required by the code and can be safely removed" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Text/TextShaper.cs(4,1)
using Avalonia.Media.TextFormatting;
using Avalonia.Platform;
using NullLib.ConsoleEx;
Expand All @@ -10,25 +12,18 @@ public class TextShaper : ITextShaperImpl
{
public ShapedBuffer ShapeText(ReadOnlyMemory<char> 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();
}


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
}
}
Expand Down

0 comments on commit e0a2a52

Please sign in to comment.