diff --git a/src/Consolonia.Core/Drawing/DrawingContextImpl.cs b/src/Consolonia.Core/Drawing/DrawingContextImpl.cs index 8eaa7097..5204feff 100644 --- a/src/Consolonia.Core/Drawing/DrawingContextImpl.cs +++ b/src/Consolonia.Core/Drawing/DrawingContextImpl.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Linq; using System.Text; using Avalonia; @@ -582,14 +583,24 @@ private void DrawPixelAndMoveHead(ref Point head, Line line, LineStyle? lineStyl { if (!GlyphMetrics.TryGetValue(glyph, out width)) { - var (originalLeft, originalTop) = Console.GetCursorPosition(); - Console.SetCursorPosition((int)characterPoint.X, (int)characterPoint.Y); - Console.Write(glyph); - var (left, top) = Console.GetCursorPosition(); - width = (ushort)(left - (int)characterPoint.X); - Debug.WriteLine($"{glyph} {width}"); - GlyphMetrics[glyph] = width; - Console.SetCursorPosition(originalLeft, originalTop); + try + { + + var (originalLeft, originalTop) = Console.GetCursorPosition(); + Console.SetCursorPosition((int)characterPoint.X, (int)characterPoint.Y); + Console.Write(glyph); + var (left, top) = Console.GetCursorPosition(); + width = (ushort)(left - (int)characterPoint.X); + Debug.WriteLine($"{glyph} {width}"); + GlyphMetrics[glyph] = width; + Console.SetCursorPosition(originalLeft, originalTop); + } + catch (IOException) + { + // IOException happens when running unit tests TODO: We should emulate this better + width = 1; + GlyphMetrics[glyph] = width; + } } } var symbol = new SimpleSymbol(glyph, width); diff --git a/src/Tests/Consolonia.GalleryTests/TextBlockTests.cs b/src/Tests/Consolonia.GalleryTests/TextBlockTests.cs index d1beb95e..51c8c3d9 100644 --- a/src/Tests/Consolonia.GalleryTests/TextBlockTests.cs +++ b/src/Tests/Consolonia.GalleryTests/TextBlockTests.cs @@ -1,4 +1,7 @@ +using System.Drawing; using System.Threading.Tasks; +using Avalonia.Controls; +using Avalonia.Media; using Consolonia.GalleryTests.Base; using Consolonia.TestsCore; using NUnit.Framework; @@ -18,9 +21,12 @@ protected override Task PerformSingleTest() "Right aligned text│", // multiline - "│Lorem ipsum dolor sit amet, consectetur adipiscing elit.│", - @"│Vivamus magna. Cras in mi at felis aliquet congue. Ut a │", - @"│est eget ligula molestie gravida. Curabitur massa. Donec│"); + "│Lorem ipsum dolor sit amet, consectetur adipiscing", + "│elit. Vivamus magna. Cras in mi at felis aliquet", + "│congue. Ut a est eget ligula molestie gravida.", + // special chars, emojis, etc. + "𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟", "𝄞","🎵", "“𝔉𝔞𝔫𝔠𝔶”","ff","fi","½"); + } } } \ No newline at end of file