Browse Source

Update teste for the new typeface

pull/10009/head
Benedikt Stebner 4 years ago
parent
commit
42f8c58aef
  1. 21
      src/Avalonia.Base/Media/TextFormatting/TextCharacters.cs
  2. 6
      tests/Avalonia.Skia.UnitTests/Media/CustomFontManagerImpl.cs
  3. 14
      tests/Avalonia.Skia.UnitTests/Media/TextFormatting/TextLayoutTests.cs
  4. 14
      tests/Avalonia.Skia.UnitTests/Media/TextFormatting/TextLineTests.cs

21
src/Avalonia.Base/Media/TextFormatting/TextCharacters.cs

@ -82,24 +82,15 @@ namespace Avalonia.Media.TextFormatting
var previousGlyphTypeface = previousProperties?.CachedGlyphTypeface; var previousGlyphTypeface = previousProperties?.CachedGlyphTypeface;
var textSpan = text.Span; var textSpan = text.Span;
if (TryGetShapeableLength(textSpan, defaultGlyphTypeface, null, out var count, out var script)) if (TryGetShapeableLength(textSpan, defaultGlyphTypeface, null, out var count))
{ {
if (script == Script.Common && previousGlyphTypeface is not null)
{
if (TryGetShapeableLength(textSpan, previousGlyphTypeface, null, out var fallbackCount, out _))
{
return new UnshapedTextRun(text.Slice(0, fallbackCount),
defaultProperties.WithTypeface(previousTypeface!.Value), biDiLevel);
}
}
return new UnshapedTextRun(text.Slice(0, count), defaultProperties.WithTypeface(defaultTypeface), return new UnshapedTextRun(text.Slice(0, count), defaultProperties.WithTypeface(defaultTypeface),
biDiLevel); biDiLevel);
} }
if (previousGlyphTypeface is not null) if (previousGlyphTypeface is not null)
{ {
if (TryGetShapeableLength(textSpan, previousGlyphTypeface, defaultGlyphTypeface, out count, out _)) if (TryGetShapeableLength(textSpan, previousGlyphTypeface, defaultGlyphTypeface, out count))
{ {
return new UnshapedTextRun(text.Slice(0, count), return new UnshapedTextRun(text.Slice(0, count),
defaultProperties.WithTypeface(previousTypeface!.Value), biDiLevel); defaultProperties.WithTypeface(previousTypeface!.Value), biDiLevel);
@ -130,7 +121,7 @@ namespace Avalonia.Media.TextFormatting
var fallbackGlyphTypeface = fontManager.GetOrAddGlyphTypeface(fallbackTypeface); var fallbackGlyphTypeface = fontManager.GetOrAddGlyphTypeface(fallbackTypeface);
if (matchFound && TryGetShapeableLength(textSpan, fallbackGlyphTypeface, defaultGlyphTypeface, out count, out _)) if (matchFound && TryGetShapeableLength(textSpan, fallbackGlyphTypeface, defaultGlyphTypeface, out count))
{ {
//Fallback found //Fallback found
return new UnshapedTextRun(text.Slice(0, count), defaultProperties.WithTypeface(fallbackTypeface), return new UnshapedTextRun(text.Slice(0, count), defaultProperties.WithTypeface(fallbackTypeface),
@ -160,17 +151,15 @@ namespace Avalonia.Media.TextFormatting
/// <param name="glyphTypeface">The typeface that is used to find matching characters.</param> /// <param name="glyphTypeface">The typeface that is used to find matching characters.</param>
/// <param name="defaultGlyphTypeface">The default typeface.</param> /// <param name="defaultGlyphTypeface">The default typeface.</param>
/// <param name="length">The shapeable length.</param> /// <param name="length">The shapeable length.</param>
/// <param name="script"></param>
/// <returns></returns> /// <returns></returns>
internal static bool TryGetShapeableLength( internal static bool TryGetShapeableLength(
ReadOnlySpan<char> text, ReadOnlySpan<char> text,
IGlyphTypeface glyphTypeface, IGlyphTypeface glyphTypeface,
IGlyphTypeface? defaultGlyphTypeface, IGlyphTypeface? defaultGlyphTypeface,
out int length, out int length)
out Script script)
{ {
length = 0; length = 0;
script = Script.Unknown; var script = Script.Unknown;
if (text.IsEmpty) if (text.IsEmpty)
{ {

6
tests/Avalonia.Skia.UnitTests/Media/CustomFontManagerImpl.cs

@ -90,6 +90,12 @@ namespace Avalonia.Skia.UnitTests.Media
skTypeface = typefaceCollection.Get(typeface); skTypeface = typefaceCollection.Get(typeface);
break; break;
} }
case "Noto Sans Hebrew":
{
var typefaceCollection = SKTypefaceCollectionCache.GetOrAddTypefaceCollection(_hebrewTypeface.FontFamily);
skTypeface = typefaceCollection.Get(typeface);
break;
}
case FontFamily.DefaultFontFamilyName: case FontFamily.DefaultFontFamilyName:
case "Noto Mono": case "Noto Mono":
{ {

14
tests/Avalonia.Skia.UnitTests/Media/TextFormatting/TextLayoutTests.cs

@ -724,7 +724,7 @@ namespace Avalonia.Skia.UnitTests.Media.TextFormatting
var selectedRect = rects[0]; var selectedRect = rects[0];
Assert.Equal(selectedText.Bounds.Width, selectedRect.Width); Assert.Equal(selectedText.Bounds.Width, selectedRect.Width, 2);
} }
} }
@ -885,7 +885,7 @@ namespace Avalonia.Skia.UnitTests.Media.TextFormatting
var distance = hitRange.First().Left; var distance = hitRange.First().Left;
Assert.Equal(currentX, distance); Assert.Equal(currentX, distance, 2);
currentX += advance; currentX += advance;
} }
@ -915,7 +915,7 @@ namespace Avalonia.Skia.UnitTests.Media.TextFormatting
var distance = hitRange.First().Left + 0.5; var distance = hitRange.First().Left + 0.5;
Assert.Equal(currentX, distance); Assert.Equal(currentX, distance, 2);
currentX += advance; currentX += advance;
} }
@ -1048,8 +1048,8 @@ namespace Avalonia.Skia.UnitTests.Media.TextFormatting
[InlineData("012🧐210", 2, 4, FlowDirection.LeftToRight, "14.40234375,40.8046875")] [InlineData("012🧐210", 2, 4, FlowDirection.LeftToRight, "14.40234375,40.8046875")]
[InlineData("210🧐012", 2, 4, FlowDirection.RightToLeft, "0,7.201171875;21.603515625,33.603515625;48.005859375,55.20703125")] [InlineData("210🧐012", 2, 4, FlowDirection.RightToLeft, "0,7.201171875;21.603515625,33.603515625;48.005859375,55.20703125")]
[InlineData("שנב🧐שנב", 2, 4, FlowDirection.LeftToRight, "11.63671875,39.779296875")] [InlineData("שנב🧐שנב", 2, 4, FlowDirection.LeftToRight, "11.268,38.208")]
[InlineData("שנב🧐שנב", 2, 4, FlowDirection.RightToLeft, "11.63671875,39.779296875")] [InlineData("שנב🧐שנב", 2, 4, FlowDirection.RightToLeft, "11.268,38.208")]
[Theory] [Theory]
public void Should_HitTextTextRangeBetweenRuns(string text, int start, int length, public void Should_HitTextTextRangeBetweenRuns(string text, int start, int length,
FlowDirection flowDirection, string expected) FlowDirection flowDirection, string expected)
@ -1080,9 +1080,9 @@ namespace Avalonia.Skia.UnitTests.Media.TextFormatting
{ {
var expectedRect = expectedRects[i]; var expectedRect = expectedRects[i];
Assert.Equal(expectedRect.Left, rects[i].Left); Assert.Equal(expectedRect.Left, rects[i].Left, 2);
Assert.Equal(expectedRect.Right, rects[i].Right); Assert.Equal(expectedRect.Right, rects[i].Right, 2);
} }
} }
} }

14
tests/Avalonia.Skia.UnitTests/Media/TextFormatting/TextLineTests.cs

@ -658,7 +658,7 @@ namespace Avalonia.Skia.UnitTests.Media.TextFormatting
Assert.Equal(TextTestHelper.GetStartCharIndex(run.Text), bounds.TextSourceCharacterIndex); Assert.Equal(TextTestHelper.GetStartCharIndex(run.Text), bounds.TextSourceCharacterIndex);
Assert.Equal(run, bounds.TextRun); Assert.Equal(run, bounds.TextRun);
Assert.Equal(run.Size.Width, bounds.Rectangle.Width); Assert.Equal(run.Size.Width, bounds.Rectangle.Width, 2);
} }
for (var i = 0; i < textBounds.Count; i++) for (var i = 0; i < textBounds.Count; i++)
@ -667,19 +667,19 @@ namespace Avalonia.Skia.UnitTests.Media.TextFormatting
if (lastBounds != null) if (lastBounds != null)
{ {
Assert.Equal(lastBounds.Rectangle.Right, currentBounds.Rectangle.Left); Assert.Equal(lastBounds.Rectangle.Right, currentBounds.Rectangle.Left, 2);
} }
var sumOfRunWidth = currentBounds.TextRunBounds.Sum(x => x.Rectangle.Width); var sumOfRunWidth = currentBounds.TextRunBounds.Sum(x => x.Rectangle.Width);
Assert.Equal(sumOfRunWidth, currentBounds.Rectangle.Width); Assert.Equal(sumOfRunWidth, currentBounds.Rectangle.Width, 2);
lastBounds = currentBounds; lastBounds = currentBounds;
} }
var sumOfBoundsWidth = textBounds.Sum(x => x.Rectangle.Width); var sumOfBoundsWidth = textBounds.Sum(x => x.Rectangle.Width);
Assert.Equal(lineWidth, sumOfBoundsWidth); Assert.Equal(lineWidth, sumOfBoundsWidth, 2);
} }
} }
@ -959,14 +959,14 @@ namespace Avalonia.Skia.UnitTests.Media.TextFormatting
Assert.Equal(secondRun.Size.Width, textBounds[1].Rectangle.Width); Assert.Equal(secondRun.Size.Width, textBounds[1].Rectangle.Width);
Assert.Equal(7.201171875, textBounds[0].Rectangle.Width); Assert.Equal(7.201171875, textBounds[0].Rectangle.Width);
Assert.Equal(textLine.Start + 7.201171875, textBounds[0].Rectangle.Right); Assert.Equal(textLine.Start + 7.201171875, textBounds[0].Rectangle.Right, 2);
Assert.Equal(textLine.Start + firstRun.Size.Width, textBounds[1].Rectangle.Left); Assert.Equal(textLine.Start + firstRun.Size.Width, textBounds[1].Rectangle.Left, 2);
textBounds = textLine.GetTextBounds(0, text.Length); textBounds = textLine.GetTextBounds(0, text.Length);
Assert.Equal(2, textBounds.Count); Assert.Equal(2, textBounds.Count);
Assert.Equal(7, textBounds.Sum(x => x.TextRunBounds.Sum(x => x.Length))); Assert.Equal(7, textBounds.Sum(x => x.TextRunBounds.Sum(x => x.Length)));
Assert.Equal(textLine.WidthIncludingTrailingWhitespace, textBounds.Sum(x => x.Rectangle.Width)); Assert.Equal(textLine.WidthIncludingTrailingWhitespace, textBounds.Sum(x => x.Rectangle.Width), 2);
} }
} }

Loading…
Cancel
Save