Browse Source

Merge pull request #5864 from Gillibald/fixes/TryGetRunProperties

Fix text tokenization for inherited scripts
pull/5944/head
Benedikt Stebner 5 years ago
committed by GitHub
parent
commit
32dac793d8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/Avalonia.Visuals/Media/TextFormatting/ShapedTextCharacters.cs
  2. 44
      src/Avalonia.Visuals/Media/TextFormatting/TextCharacters.cs
  3. 21
      tests/Avalonia.Skia.UnitTests/Media/TextFormatting/TextFormatterTests.cs

6
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);

44
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)
@ -161,36 +161,44 @@ namespace Avalonia.Media.TextFormatting
if (currentScript != script)
{
if (currentScript != Script.Inherited && currentScript != Script.Common)
if (script == Script.Inherited || script == Script.Common)
{
if (script == Script.Inherited || script == Script.Common)
{
script = currentScript;
}
else
script = currentScript;
}
else
{
if (currentScript != Script.Inherited && currentScript != Script.Common)
{
break;
}
}
}
if (isFallback)
if (currentScript != Script.Common && currentScript != Script.Inherited)
{
if (defaultFont.TryGetGlyph(grapheme.FirstCodepoint, out _))
if (isFallback && defaultFont.TryGetGlyph(currentGrapheme.FirstCodepoint, out _))
{
break;
}
}
if (!font.TryGetGlyph(grapheme.FirstCodepoint, out _))
{
if (!grapheme.FirstCodepoint.IsWhiteSpace)
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;

21
tests/Avalonia.Skia.UnitTests/Media/TextFormatting/TextFormatterTests.cs

@ -125,6 +125,27 @@ namespace Avalonia.Skia.UnitTests.Media.TextFormatting
}
}
[Fact]
public void Should_Produce_A_Single_Fallback_Run()
{
using (Start())
{
var defaultProperties = new GenericTextRunProperties(Typeface.Default);
const string text = "👍 👍 👍 👍";
var textSource = new SingleBufferTextSource(text, defaultProperties);
var formatter = new TextFormatterImpl();
var textLine =
formatter.FormatLine(textSource, 0, double.PositiveInfinity,
new GenericTextParagraphProperties(defaultProperties));
Assert.Equal(1, textLine.TextRuns.Count);
}
}
[Fact]
public void Should_Split_Run_On_Script()
{

Loading…
Cancel
Save