Browse Source

Fix text wrapping for languages like chinese that can break at all characters and not just at spaces (#13193)

pull/13255/head
Benedikt Stebner 3 years ago
committed by GitHub
parent
commit
ba3773714b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      src/Avalonia.Base/Media/TextFormatting/TextFormatterImpl.cs
  2. 16
      tests/Avalonia.Skia.UnitTests/Media/TextFormatting/TextFormatterTests.cs

18
src/Avalonia.Base/Media/TextFormatting/TextFormatterImpl.cs

@ -607,19 +607,19 @@ namespace Avalonia.Media.TextFormatting
}
break;
}
}
var clusterLength = Math.Max(0, nextInfo.GlyphCluster - currentInfo.GlyphCluster);
if(clusterLength == 0)
if (clusterLength == 0)
{
clusterLength = currentRun.Length - runLength;
}
if(clusterLength == 0)
if (clusterLength == 0)
{
clusterLength = shapedTextCharacters.GlyphRun.Metrics.FirstCluster + currentRun.Length - currentInfo.GlyphCluster;
}
}
if (currentWidth + clusterWidth > paragraphWidth)
{
@ -713,7 +713,7 @@ namespace Avalonia.Media.TextFormatting
var measuredLength = MeasureLength(textRuns, paragraphWidth);
if(measuredLength == 0 && paragraphProperties.TextWrapping == TextWrapping.WrapWithOverflow)
if (measuredLength == 0 && paragraphProperties.TextWrapping == TextWrapping.WrapWithOverflow)
{
for (int i = 0; i < textRuns.Count; i++)
{
@ -841,7 +841,7 @@ namespace Avalonia.Media.TextFormatting
break;
}
if (lineBreak.PositionMeasure != lineBreak.PositionWrap)
if (lineBreak.PositionMeasure != lineBreak.PositionWrap || lineBreak.PositionWrap != currentRun.Length)
{
lastWrapPosition = currentLength + lineBreak.PositionWrap;
}
@ -858,12 +858,6 @@ namespace Avalonia.Media.TextFormatting
continue;
}
//We don't want to surpass the measuredLength with trailing whitespace when we are in a right to left setting.
if(currentPosition > measuredLength && resolvedFlowDirection == FlowDirection.RightToLeft)
{
break;
}
measuredLength = currentPosition;
break;

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

@ -822,6 +822,22 @@ namespace Avalonia.Skia.UnitTests.Media.TextFormatting
}
[Fact]
public void Should_Wrap_Chinese()
{
using (Start())
{
var defaultRunProperties = new GenericTextRunProperties(Typeface.Default);
var paragraphProperties = new GenericTextParagraphProperties(defaultRunProperties, textWrap: TextWrapping.Wrap);
var text = "一二三四 TEXT 一二三四五六七八九十零";
var textLine = TextFormatter.Current.FormatLine(new SimpleTextSource(text, defaultRunProperties), 0, 120, paragraphProperties);
Assert.Equal(3, textLine.TextRuns.Count);
}
}
protected readonly record struct SimpleTextSource : ITextSource
{
private readonly string _text;

Loading…
Cancel
Save