From a33ca9de042d1079e8e843853cc16d7282f6be7d Mon Sep 17 00:00:00 2001 From: ncarrillo Date: Tue, 21 Apr 2015 21:40:54 -0400 Subject: [PATCH] Add D2D support for TextAlignment --- Windows/Perspex.Direct2D1/Direct2D1Platform.cs | 3 ++- Windows/Perspex.Direct2D1/Media/FormattedTextImpl.cs | 3 +++ Windows/Perspex.Direct2D1/PrimitiveExtensions.cs | 12 ++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Windows/Perspex.Direct2D1/Direct2D1Platform.cs b/Windows/Perspex.Direct2D1/Direct2D1Platform.cs index f649fcc80b..1abeb40d45 100644 --- a/Windows/Perspex.Direct2D1/Direct2D1Platform.cs +++ b/Windows/Perspex.Direct2D1/Direct2D1Platform.cs @@ -42,9 +42,10 @@ namespace Perspex.Direct2D1 string fontFamily, double fontSize, FontStyle fontStyle, + TextAlignment textAlignment, FontWeight fontWeight) { - return new FormattedTextImpl(text, fontFamily, fontSize, fontStyle, fontWeight); + return new FormattedTextImpl(text, fontFamily, fontSize, fontStyle, textAlignment, fontWeight); } public IRenderer CreateRenderer(IPlatformHandle handle, double width, double height) diff --git a/Windows/Perspex.Direct2D1/Media/FormattedTextImpl.cs b/Windows/Perspex.Direct2D1/Media/FormattedTextImpl.cs index b2d485d75a..c8a108d824 100644 --- a/Windows/Perspex.Direct2D1/Media/FormattedTextImpl.cs +++ b/Windows/Perspex.Direct2D1/Media/FormattedTextImpl.cs @@ -21,6 +21,7 @@ namespace Perspex.Direct2D1.Media string fontFamily, double fontSize, FontStyle fontStyle, + TextAlignment textAlignment, FontWeight fontWeight) { var factory = Locator.Current.GetService(); @@ -38,6 +39,8 @@ namespace Perspex.Direct2D1.Media format, float.MaxValue, float.MaxValue); + + this.TextLayout.TextAlignment = textAlignment.ToDirect2D(); } public Size Constraint diff --git a/Windows/Perspex.Direct2D1/PrimitiveExtensions.cs b/Windows/Perspex.Direct2D1/PrimitiveExtensions.cs index b7e43b6fe0..81f403f344 100644 --- a/Windows/Perspex.Direct2D1/PrimitiveExtensions.cs +++ b/Windows/Perspex.Direct2D1/PrimitiveExtensions.cs @@ -9,6 +9,7 @@ namespace Perspex.Direct2D1 using System.Linq; using SharpDX; using SharpDX.Direct2D1; + using DWrite = SharpDX.DirectWrite; public static class PrimitiveExtensions { @@ -128,5 +129,16 @@ namespace Perspex.Direct2D1 (float)rect.Width, (float)rect.Height); } + + public static DWrite.TextAlignment ToDirect2D(this Perspex.Media.TextAlignment alignment) + { + if (alignment == Perspex.Media.TextAlignment.Left) + return DWrite.TextAlignment.Leading; + + if (alignment == Perspex.Media.TextAlignment.Centered) + return DWrite.TextAlignment.Center; + + return DWrite.TextAlignment.Trailing; + } } }