From 0aff57a716b9d2ee0a2e62f9982e8bfc4f89837a Mon Sep 17 00:00:00 2001 From: Benedikt Stebner Date: Wed, 26 Oct 2022 12:00:49 +0200 Subject: [PATCH] Prevent a crash that is caused by typefaces that don't contain whitespace glyphs --- .../Media/TextFormatting/TextFormatterImpl.cs | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/Avalonia.Base/Media/TextFormatting/TextFormatterImpl.cs b/src/Avalonia.Base/Media/TextFormatting/TextFormatterImpl.cs index 73dd3366aa..5c43a1c94f 100644 --- a/src/Avalonia.Base/Media/TextFormatting/TextFormatterImpl.cs +++ b/src/Avalonia.Base/Media/TextFormatting/TextFormatterImpl.cs @@ -477,25 +477,28 @@ namespace Avalonia.Media.TextFormatting { case ShapedTextCharacters shapedTextCharacters: { - var firstCluster = shapedTextCharacters.ShapedBuffer.GlyphClusters[0]; - var lastCluster = firstCluster; - - for (var i = 0; i < shapedTextCharacters.ShapedBuffer.Length; i++) + if(shapedTextCharacters.ShapedBuffer.Length > 0) { - var glyphInfo = shapedTextCharacters.ShapedBuffer[i]; + var firstCluster = shapedTextCharacters.ShapedBuffer.GlyphClusters[0]; + var lastCluster = firstCluster; - if (currentWidth + glyphInfo.GlyphAdvance > paragraphWidth) + for (var i = 0; i < shapedTextCharacters.ShapedBuffer.Length; i++) { - measuredLength += Math.Max(0, lastCluster - firstCluster); + var glyphInfo = shapedTextCharacters.ShapedBuffer[i]; - goto found; - } + if (currentWidth + glyphInfo.GlyphAdvance > paragraphWidth) + { + measuredLength += Math.Max(0, lastCluster - firstCluster); - lastCluster = glyphInfo.GlyphCluster; - currentWidth += glyphInfo.GlyphAdvance; - } + goto found; + } - measuredLength += currentRun.TextSourceLength; + lastCluster = glyphInfo.GlyphCluster; + currentWidth += glyphInfo.GlyphAdvance; + } + + measuredLength += currentRun.TextSourceLength; + } break; }