csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
3.5 KiB
101 lines
3.5 KiB
using System.Globalization;
|
|
using System.Threading.Tasks;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Layout;
|
|
using Avalonia.Media;
|
|
using Xunit;
|
|
#pragma warning disable CS0649
|
|
|
|
#if AVALONIA_SKIA
|
|
namespace Avalonia.Skia.RenderTests;
|
|
|
|
public class DrawingContextTests : TestBase
|
|
{
|
|
public DrawingContextTests() : base(@"Media\DrawingContext")
|
|
{
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Should_Render_LinesAndText()
|
|
{
|
|
var target = new Border
|
|
{
|
|
Width = 300,
|
|
Height = 300,
|
|
Background = Brushes.White,
|
|
Child = new RenderControl()
|
|
};
|
|
|
|
await RenderToFile(target);
|
|
CompareImages(skipImmediate: true);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Should_Render_DrawingGroup_With_Effect()
|
|
{
|
|
var group = new DrawingGroup();
|
|
using (var context = group.Open())
|
|
{
|
|
context.DrawRectangle(Brushes.White, null, new Rect(0, 0, 100, 100));
|
|
using (context.PushEffect(new DropShadowEffect { BlurRadius = 10, Color = Colors.Black, Opacity = 1 }, new Rect(0, 0, 100, 100)))
|
|
{
|
|
context.DrawRectangle(Brushes.Red, null, new Rect(20, 20, 60, 60));
|
|
}
|
|
}
|
|
|
|
var target = new Border
|
|
{
|
|
Width = 100,
|
|
Height = 100,
|
|
Background = Brushes.White,
|
|
Child = new Image {
|
|
Source = new DrawingImage { Drawing = group },
|
|
Stretch = Stretch.None,
|
|
VerticalAlignment = VerticalAlignment.Center,
|
|
HorizontalAlignment = HorizontalAlignment.Center,
|
|
ClipToBounds = false
|
|
}
|
|
};
|
|
|
|
await RenderToFile(target);
|
|
CompareImages();
|
|
}
|
|
|
|
internal class RenderControl : Control
|
|
{
|
|
private static readonly Typeface s_typeface = new Typeface(TestFontFamily);
|
|
|
|
public override void Render(DrawingContext context)
|
|
{
|
|
var pen = new Pen(Brushes.LightGray, 10);
|
|
RenderLine1(context, pen);
|
|
RenderLine2(context, pen);
|
|
RenderLine3(context, pen);
|
|
RenderLine4(context, pen);
|
|
|
|
RenderLine1(context, new Pen(Brushes.Red));
|
|
RenderAText(context, new Point(50, 20));
|
|
RenderLine2(context, new Pen(Brushes.Orange));
|
|
RenderAText(context, new Point(50, -50));
|
|
RenderLine3(context, new Pen(Brushes.Yellow));
|
|
RenderAText(context, new Point(0, 0));
|
|
RenderLine4(context, new Pen(Brushes.Green));
|
|
}
|
|
|
|
private static void RenderLine1(DrawingContext context, IPen pen) => context.DrawLine(pen, new Point(100, 100), new Point(200, 100));
|
|
private static void RenderLine2(DrawingContext context, IPen pen) => context.DrawLine(pen, new Point(200, 100), new Point(200, 200));
|
|
private static void RenderLine3(DrawingContext context, IPen pen) => context.DrawLine(pen, new Point(200, 200), new Point(100, 200));
|
|
private static void RenderLine4(DrawingContext context, IPen pen) => context.DrawLine(pen, new Point(100, 200), new Point(100, 100));
|
|
|
|
private static void RenderAText(DrawingContext context, Point point)
|
|
{
|
|
using (context.PushOpacity(0.7))
|
|
{
|
|
context.DrawText(
|
|
new FormattedText("any text to render", CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
|
|
s_typeface, 12, Brushes.Black), point);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|