Browse Source

Merge pull request #6256 from AvaloniaUI/fixes/textlayout-transform

Fix TextLayout rendering with transform applied.
pull/6466/head
Max Katz 5 years ago
committed by GitHub
parent
commit
54d685a603
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/Avalonia.Visuals/Media/TextFormatting/ShapedTextCharacters.cs
  2. 106
      tests/Avalonia.RenderTests/Media/TextFormatting/TextLayoutTests.cs
  3. BIN
      tests/TestFiles/Direct2D1/Media/TextFormatting/TextLayout/TextLayout_Basic.expected.png
  4. BIN
      tests/TestFiles/Direct2D1/Media/TextFormatting/TextLayout/TextLayout_Rotated.expected.png
  5. BIN
      tests/TestFiles/Skia/Media/TextFormatting/TextLayout/TextLayout_Basic.expected.png
  6. BIN
      tests/TestFiles/Skia/Media/TextFormatting/TextLayout/TextLayout_Rotated.expected.png

2
src/Avalonia.Visuals/Media/TextFormatting/ShapedTextCharacters.cs

@ -47,7 +47,7 @@ namespace Avalonia.Media.TextFormatting
/// <inheritdoc/>
public override void Draw(DrawingContext drawingContext, Point origin)
{
using (drawingContext.PushPostTransform(Matrix.CreateTranslation(origin)))
using (drawingContext.PushPreTransform(Matrix.CreateTranslation(origin)))
{
if (GlyphRun.GlyphIndices.Length == 0)
{

106
tests/Avalonia.RenderTests/Media/TextFormatting/TextLayoutTests.cs

@ -0,0 +1,106 @@
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
using Avalonia.Platform;
using Avalonia.Utilities;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using System.Runtime.InteropServices;
#if AVALONIA_SKIA
namespace Avalonia.Skia.RenderTests
#else
using Avalonia.Direct2D1.RenderTests;
namespace Avalonia.Direct2D1.RenderTests.Media
#endif
{
public class TextLayoutTests : TestBase
{
public TextLayoutTests()
: base(@"Media\TextFormatting\TextLayout")
{
}
[Fact]
public async Task TextLayout_Basic()
{
// Skip test on OSX: text rendering is subtly different.
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return;
var t = new TextLayout(
"Avalonia!",
new Typeface(TestFontFamily),
24,
Brushes.Black);
var target = new Border
{
Width = 200,
Height = 200,
Background = Brushes.White,
Child = new DrawnControl(c =>
{
var textRect = new Rect(t.Size);
var bounds = new Rect(0, 0, 200, 200);
var rect = bounds.CenterRect(textRect);
c.DrawRectangle(Brushes.Yellow, null, rect);
t.Draw(c, rect.Position);
}),
};
await RenderToFile(target);
CompareImages();
}
[Fact]
public async Task TextLayout_Rotated()
{
// Skip test on OSX: text rendering is subtly different.
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return;
var t = new TextLayout(
"Avalonia!",
new Typeface(TestFontFamily),
24,
Brushes.Black);
var target = new Border
{
Width = 200,
Height = 200,
Background = Brushes.White,
Child = new DrawnControl(c =>
{
var textRect = new Rect(t.Size);
var bounds = new Rect(0, 0, 200, 200);
var rect = bounds.CenterRect(textRect);
var rotate = Matrix.CreateTranslation(-100, -100) *
Matrix.CreateRotation(MathUtilities.Deg2Rad(90)) *
Matrix.CreateTranslation(100, 100);
using var transform = c.PushPreTransform(rotate);
c.DrawRectangle(Brushes.Yellow, null, rect);
t.Draw(c, rect.Position);
}),
};
await RenderToFile(target);
CompareImages();
}
private class DrawnControl : Control
{
private readonly Action<DrawingContext> _render;
public DrawnControl(Action<DrawingContext> render) => _render = render;
public override void Render(DrawingContext context) => _render(context);
}
}
}

BIN
tests/TestFiles/Direct2D1/Media/TextFormatting/TextLayout/TextLayout_Basic.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

BIN
tests/TestFiles/Direct2D1/Media/TextFormatting/TextLayout/TextLayout_Rotated.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

BIN
tests/TestFiles/Skia/Media/TextFormatting/TextLayout/TextLayout_Basic.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

BIN
tests/TestFiles/Skia/Media/TextFormatting/TextLayout/TextLayout_Rotated.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Loading…
Cancel
Save