diff --git a/src/Avalonia.Visuals/Media/TextFormatting/ShapedTextCharacters.cs b/src/Avalonia.Visuals/Media/TextFormatting/ShapedTextCharacters.cs index 723d5e81ab..b304b19910 100644 --- a/src/Avalonia.Visuals/Media/TextFormatting/ShapedTextCharacters.cs +++ b/src/Avalonia.Visuals/Media/TextFormatting/ShapedTextCharacters.cs @@ -109,7 +109,8 @@ namespace Avalonia.Media.TextFormatting GlyphRun.GlyphAdvances.Take(glyphCount), GlyphRun.GlyphOffsets.Take(glyphCount), GlyphRun.Characters.Take(length), - GlyphRun.GlyphClusters.Take(glyphCount)); + GlyphRun.GlyphClusters.Take(glyphCount), + GlyphRun.BiDiLevel); var firstTextRun = new ShapedTextCharacters(firstGlyphRun, Properties); @@ -120,7 +121,8 @@ namespace Avalonia.Media.TextFormatting GlyphRun.GlyphAdvances.Skip(glyphCount), GlyphRun.GlyphOffsets.Skip(glyphCount), GlyphRun.Characters.Skip(length), - GlyphRun.GlyphClusters.Skip(glyphCount)); + GlyphRun.GlyphClusters.Skip(glyphCount), + GlyphRun.BiDiLevel); var secondTextRun = new ShapedTextCharacters(secondGlyphRun, Properties); diff --git a/src/Avalonia.Visuals/Media/TextFormatting/TextCharacters.cs b/src/Avalonia.Visuals/Media/TextFormatting/TextCharacters.cs index 86b96b9cfc..0779716ec8 100644 --- a/src/Avalonia.Visuals/Media/TextFormatting/TextCharacters.cs +++ b/src/Avalonia.Visuals/Media/TextFormatting/TextCharacters.cs @@ -135,20 +135,20 @@ namespace Avalonia.Media.TextFormatting count = 0; var script = Script.Common; - //var direction = BiDiClass.LeftToRight; + var direction = BiDiClass.LeftToRight; var font = typeface.GlyphTypeface; var defaultFont = defaultTypeface.GlyphTypeface; - + var enumerator = new GraphemeEnumerator(text); while (enumerator.MoveNext()) { - var grapheme = enumerator.Current; + var currentGrapheme = enumerator.Current; - var currentScript = grapheme.FirstCodepoint.Script; + var currentScript = currentGrapheme.FirstCodepoint.Script; - //var currentDirection = grapheme.FirstCodepoint.BiDiClass; + var currentDirection = currentGrapheme.FirstCodepoint.BiDiClass; //// ToDo: Implement BiDi algorithm //if (currentScript.HorizontalDirection != direction) @@ -176,18 +176,29 @@ namespace Avalonia.Media.TextFormatting if (currentScript != Script.Common && currentScript != Script.Inherited) { - if (isFallback && defaultFont.TryGetGlyph(grapheme.FirstCodepoint, out _)) + if (isFallback && defaultFont.TryGetGlyph(currentGrapheme.FirstCodepoint, out _)) { break; } - if (!font.TryGetGlyph(grapheme.FirstCodepoint, out _)) + if (!font.TryGetGlyph(currentGrapheme.FirstCodepoint, out _)) { break; } } - count += grapheme.Text.Length; + if (!currentGrapheme.FirstCodepoint.IsWhiteSpace && !font.TryGetGlyph(currentGrapheme.FirstCodepoint, out _)) + { + break; + } + + if (direction == BiDiClass.RightToLeft && currentDirection == BiDiClass.CommonSeparator) + { + break; + } + + count += currentGrapheme.Text.Length; + direction = currentDirection; } return count > 0;