From 74c82f3b635fec3f549195955374c652af533061 Mon Sep 17 00:00:00 2001 From: ncarrillo Date: Tue, 21 Apr 2015 21:22:31 -0400 Subject: [PATCH 1/2] Work on TextAlignment --- Cairo/Perspex.Cairo/CairoExtensions.cs | 11 +++++++++++ Cairo/Perspex.Cairo/CairoPlatform.cs | 3 ++- Cairo/Perspex.Cairo/Media/FormattedTextImpl.cs | 10 ++++++++++ Perspex.Controls/Presenters/TextPresenter.cs | 1 + Perspex.Controls/TextBlock.cs | 11 +++++++++++ Perspex.SceneGraph/Media/FontStyle.cs | 7 +++++++ Perspex.SceneGraph/Media/FormattedText.cs | 2 ++ .../Platform/IPlatformRenderInterface.cs | 1 + 8 files changed, 45 insertions(+), 1 deletion(-) diff --git a/Cairo/Perspex.Cairo/CairoExtensions.cs b/Cairo/Perspex.Cairo/CairoExtensions.cs index 3bbf4c04b6..34099f9757 100644 --- a/Cairo/Perspex.Cairo/CairoExtensions.cs +++ b/Cairo/Perspex.Cairo/CairoExtensions.cs @@ -56,5 +56,16 @@ namespace Perspex.Cairo return Pango.Weight.Ultralight; } + + public static Pango.Alignment ToCairo(this Perspex.Media.TextAlignment alignment) + { + if (alignment == Perspex.Media.TextAlignment.Left) + return Pango.Alignment.Left; + + if (alignment == Perspex.Media.TextAlignment.Centered) + return Pango.Alignment.Center; + + return Pango.Alignment.Right; + } } } \ No newline at end of file diff --git a/Cairo/Perspex.Cairo/CairoPlatform.cs b/Cairo/Perspex.Cairo/CairoPlatform.cs index f1841fe31a..827014afb4 100644 --- a/Cairo/Perspex.Cairo/CairoPlatform.cs +++ b/Cairo/Perspex.Cairo/CairoPlatform.cs @@ -35,9 +35,10 @@ namespace Perspex.Cairo string fontFamily, double fontSize, FontStyle fontStyle, + TextAlignment textAlignment, Perspex.Media.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/Cairo/Perspex.Cairo/Media/FormattedTextImpl.cs b/Cairo/Perspex.Cairo/Media/FormattedTextImpl.cs index a465c9ac71..0bfe888f8f 100644 --- a/Cairo/Perspex.Cairo/Media/FormattedTextImpl.cs +++ b/Cairo/Perspex.Cairo/Media/FormattedTextImpl.cs @@ -20,6 +20,7 @@ namespace Perspex.Cairo.Media string fontFamily, double fontSize, FontStyle fontStyle, + TextAlignment textAlignment, FontWeight fontWeight) { var context = Locator.Current.GetService(); @@ -32,6 +33,8 @@ namespace Perspex.Cairo.Media Style = (Pango.Style)fontStyle, Weight = fontWeight.ToCairo() }; + + this.Layout.Alignment = textAlignment.ToCairo(); } public Size Constraint @@ -43,6 +46,7 @@ namespace Perspex.Cairo.Media set { + this.Layout.Width = Pango.Units.FromDouble(value.Width); } } @@ -98,6 +102,12 @@ namespace Perspex.Cairo.Media int width; int height; this.Layout.GetPixelSize(out width, out height); + + if (this.Layout.Alignment == Pango.Alignment.Right) + { + return new Size(width, height); + } + return new Size(width, height); } diff --git a/Perspex.Controls/Presenters/TextPresenter.cs b/Perspex.Controls/Presenters/TextPresenter.cs index a35c81052f..5b1e1bf59e 100644 --- a/Perspex.Controls/Presenters/TextPresenter.cs +++ b/Perspex.Controls/Presenters/TextPresenter.cs @@ -165,6 +165,7 @@ namespace Perspex.Controls.Presenters this.FontFamily, this.FontSize, this.FontStyle, + TextAlignment.Left, this.FontWeight)) { return formattedText.Measure(); diff --git a/Perspex.Controls/TextBlock.cs b/Perspex.Controls/TextBlock.cs index 1a639fc0d9..642f97d9a2 100644 --- a/Perspex.Controls/TextBlock.cs +++ b/Perspex.Controls/TextBlock.cs @@ -37,6 +37,9 @@ namespace Perspex.Controls public static readonly PerspexProperty TextWrappingProperty = PerspexProperty.Register("TextWrapping"); + public static readonly PerspexProperty TextAlignmentProperty = + PerspexProperty.Register("TextAlignment"); + private FormattedText formattedText; private Size constraint; @@ -45,6 +48,7 @@ namespace Perspex.Controls { Observable.Merge( this.GetObservable(TextProperty).Select(_ => Unit.Default), + this.GetObservable(TextAlignmentProperty).Select(_ => Unit.Default), this.GetObservable(FontSizeProperty).Select(_ => Unit.Default), this.GetObservable(FontStyleProperty).Select(_ => Unit.Default)) .Subscribe(_ => @@ -114,6 +118,12 @@ namespace Perspex.Controls set { this.SetValue(TextWrappingProperty, value); } } + public TextAlignment TextAlignment + { + get { return this.GetValue(TextAlignmentProperty); } + set { this.SetValue(TextAlignmentProperty, value); } + } + public override void Render(IDrawingContext context) { Brush background = this.Background; @@ -133,6 +143,7 @@ namespace Perspex.Controls this.FontFamily, this.FontSize, this.FontStyle, + this.TextAlignment, this.FontWeight); result.Constraint = this.constraint; return result; diff --git a/Perspex.SceneGraph/Media/FontStyle.cs b/Perspex.SceneGraph/Media/FontStyle.cs index 2729233ecd..dbeeb19e82 100644 --- a/Perspex.SceneGraph/Media/FontStyle.cs +++ b/Perspex.SceneGraph/Media/FontStyle.cs @@ -12,4 +12,11 @@ namespace Perspex.Media Oblique, Italic, } + + public enum TextAlignment + { + Left, + Centered, + Right, + } } diff --git a/Perspex.SceneGraph/Media/FormattedText.cs b/Perspex.SceneGraph/Media/FormattedText.cs index e24cf65e5b..c2122faa8d 100644 --- a/Perspex.SceneGraph/Media/FormattedText.cs +++ b/Perspex.SceneGraph/Media/FormattedText.cs @@ -18,6 +18,7 @@ namespace Perspex.Media string fontFamilyName, double fontSize, FontStyle fontStyle, + TextAlignment textAlignment, FontWeight fontWeight) { this.Text = text; @@ -32,6 +33,7 @@ namespace Perspex.Media fontFamilyName, fontSize, fontStyle, + textAlignment, fontWeight); } diff --git a/Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs b/Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs index 3d9a7272ff..d8fc4d7b11 100644 --- a/Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs +++ b/Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs @@ -17,6 +17,7 @@ namespace Perspex.Platform string fontFamily, double fontSize, FontStyle fontStyle, + TextAlignment textAlignment, FontWeight fontWeight); IStreamGeometryImpl CreateStreamGeometry(); From a33ca9de042d1079e8e843853cc16d7282f6be7d Mon Sep 17 00:00:00 2001 From: ncarrillo Date: Tue, 21 Apr 2015 21:40:54 -0400 Subject: [PATCH 2/2] 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; + } } }