From 53d804572d636d046abd076bc3b1443785e3e016 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 4 Mar 2015 00:35:10 +0100 Subject: [PATCH] Added TextBlock.FontWeight. Closes #31. --- Cairo/Perspex.Cairo/CairoPlatform.cs | 5 ++-- .../Perspex.Cairo/Media/FormattedTextImpl.cs | 3 +- Perspex.Controls/Presenters/TextPresenter.cs | 3 +- Perspex.Controls/TextBlock.cs | 12 +++++++- Perspex.SceneGraph/Media/FontWeight.cs | 28 +++++++++++++++++++ Perspex.SceneGraph/Media/FormattedText.cs | 11 ++++++-- Perspex.SceneGraph/Perspex.SceneGraph.csproj | 1 + .../Platform/IPlatformRenderInterface.cs | 3 +- TestApplication/Program.cs | 5 ++++ .../Perspex.Direct2D1/Direct2D1Platform.cs | 5 ++-- .../Media/FormattedTextImpl.cs | 12 ++++++-- 11 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 Perspex.SceneGraph/Media/FontWeight.cs diff --git a/Cairo/Perspex.Cairo/CairoPlatform.cs b/Cairo/Perspex.Cairo/CairoPlatform.cs index cb5e10019e..902a621cf4 100644 --- a/Cairo/Perspex.Cairo/CairoPlatform.cs +++ b/Cairo/Perspex.Cairo/CairoPlatform.cs @@ -35,9 +35,10 @@ namespace Perspex.Cairo string text, string fontFamily, double fontSize, - FontStyle fontStyle) + FontStyle fontStyle, + Perspex.Media.FontWeight fontWeight) { - return new FormattedTextImpl(text, fontFamily, fontSize, fontStyle); + return new FormattedTextImpl(text, fontFamily, fontSize, fontStyle, 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 873ac450be..0b8d42b567 100644 --- a/Cairo/Perspex.Cairo/Media/FormattedTextImpl.cs +++ b/Cairo/Perspex.Cairo/Media/FormattedTextImpl.cs @@ -19,7 +19,8 @@ namespace Perspex.Cairo.Media string text, string fontFamily, double fontSize, - FontStyle fontStyle) + FontStyle fontStyle, + FontWeight fontWeight) { var context = Locator.Current.GetService(); this.Layout = new Pango.Layout(context); diff --git a/Perspex.Controls/Presenters/TextPresenter.cs b/Perspex.Controls/Presenters/TextPresenter.cs index 3f4ca97322..885724cac0 100644 --- a/Perspex.Controls/Presenters/TextPresenter.cs +++ b/Perspex.Controls/Presenters/TextPresenter.cs @@ -178,7 +178,8 @@ namespace Perspex.Controls.Presenters "X", this.FontFamily, this.FontSize, - this.FontStyle)) + this.FontStyle, + this.FontWeight)) { return formattedText.Measure(); } diff --git a/Perspex.Controls/TextBlock.cs b/Perspex.Controls/TextBlock.cs index 5a845462bd..d7221348ab 100644 --- a/Perspex.Controls/TextBlock.cs +++ b/Perspex.Controls/TextBlock.cs @@ -25,6 +25,9 @@ namespace Perspex.Controls public static readonly PerspexProperty FontStyleProperty = PerspexProperty.Register("FontStyle", inherits: true); + public static readonly PerspexProperty FontWeightProperty = + PerspexProperty.Register("FontWeight", inherits: true, defaultValue: FontWeight.Normal); + public static readonly PerspexProperty ForegroundProperty = PerspexProperty.Register("Foreground", new SolidColorBrush(0xff000000), inherits: true); @@ -80,6 +83,12 @@ namespace Perspex.Controls set { this.SetValue(FontStyleProperty, value); } } + public FontWeight FontWeight + { + get { return this.GetValue(FontWeightProperty); } + set { this.SetValue(FontWeightProperty, value); } + } + public Brush Foreground { get { return this.GetValue(ForegroundProperty); } @@ -123,7 +132,8 @@ namespace Perspex.Controls this.Text, this.FontFamily, this.FontSize, - this.FontStyle); + this.FontStyle, + this.FontWeight); result.Constraint = this.constraint; return result; } diff --git a/Perspex.SceneGraph/Media/FontWeight.cs b/Perspex.SceneGraph/Media/FontWeight.cs new file mode 100644 index 0000000000..e28802d2a0 --- /dev/null +++ b/Perspex.SceneGraph/Media/FontWeight.cs @@ -0,0 +1,28 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Media +{ + public enum FontWeight + { + Thin = 100, + ExtraLight = 200, + UltraLight = 200, + Light = 300, + Normal = 400, + Regular = 400, + Medium = 500, + DemiBold = 600, + SemiBold = 600, + Bold = 700, + ExtraBold = 800, + UltraBold = 800, + Black = 900, + Heavy = 900, + ExtraBlack = 950, + UltraBlack = 950 + } +} diff --git a/Perspex.SceneGraph/Media/FormattedText.cs b/Perspex.SceneGraph/Media/FormattedText.cs index 4db72f576f..e24cf65e5b 100644 --- a/Perspex.SceneGraph/Media/FormattedText.cs +++ b/Perspex.SceneGraph/Media/FormattedText.cs @@ -17,7 +17,8 @@ namespace Perspex.Media string text, string fontFamilyName, double fontSize, - FontStyle fontStyle) + FontStyle fontStyle, + FontWeight fontWeight) { this.Text = text; this.FontFamilyName = fontFamilyName; @@ -25,7 +26,13 @@ namespace Perspex.Media this.FontStyle = fontStyle; var platform = Locator.Current.GetService(); - this.PlatformImpl = platform.CreateFormattedText(text, fontFamilyName, fontSize, fontStyle); + + this.PlatformImpl = platform.CreateFormattedText( + text, + fontFamilyName, + fontSize, + fontStyle, + fontWeight); } public Size Constraint diff --git a/Perspex.SceneGraph/Perspex.SceneGraph.csproj b/Perspex.SceneGraph/Perspex.SceneGraph.csproj index 39768e98a6..ba9fe51244 100644 --- a/Perspex.SceneGraph/Perspex.SceneGraph.csproj +++ b/Perspex.SceneGraph/Perspex.SceneGraph.csproj @@ -50,6 +50,7 @@ + diff --git a/Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs b/Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs index 644e65db21..3d9a7272ff 100644 --- a/Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs +++ b/Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs @@ -16,7 +16,8 @@ namespace Perspex.Platform string text, string fontFamily, double fontSize, - FontStyle fontStyle); + FontStyle fontStyle, + FontWeight fontWeight); IStreamGeometryImpl CreateStreamGeometry(); diff --git a/TestApplication/Program.cs b/TestApplication/Program.cs index 1221d69366..4e9a2441ad 100644 --- a/TestApplication/Program.cs +++ b/TestApplication/Program.cs @@ -300,6 +300,11 @@ namespace TestApplication Text = "Italic text.", FontStyle = FontStyle.Italic, }, + new TextBlock + { + Text = "Bold text.", + FontWeight = FontWeight.Bold, + }, new TextBox { Text = "A non-wrapping text box. Lorem ipsum dolor sit amet.", diff --git a/Windows/Perspex.Direct2D1/Direct2D1Platform.cs b/Windows/Perspex.Direct2D1/Direct2D1Platform.cs index f337f39f23..f649fcc80b 100644 --- a/Windows/Perspex.Direct2D1/Direct2D1Platform.cs +++ b/Windows/Perspex.Direct2D1/Direct2D1Platform.cs @@ -41,9 +41,10 @@ namespace Perspex.Direct2D1 string text, string fontFamily, double fontSize, - FontStyle fontStyle) + FontStyle fontStyle, + FontWeight fontWeight) { - return new FormattedTextImpl(text, fontFamily, fontSize, fontStyle); + return new FormattedTextImpl(text, fontFamily, fontSize, fontStyle, 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 f2c3261317..b2d485d75a 100644 --- a/Windows/Perspex.Direct2D1/Media/FormattedTextImpl.cs +++ b/Windows/Perspex.Direct2D1/Media/FormattedTextImpl.cs @@ -20,14 +20,22 @@ namespace Perspex.Direct2D1.Media string text, string fontFamily, double fontSize, - FontStyle fontStyle) + FontStyle fontStyle, + FontWeight fontWeight) { var factory = Locator.Current.GetService(); + var format = new DWrite.TextFormat( + factory, + fontFamily, + (DWrite.FontWeight)fontWeight, + (DWrite.FontStyle)fontStyle, + (float)fontSize); + this.TextLayout = new DWrite.TextLayout( factory, text ?? string.Empty, - new DWrite.TextFormat(factory, fontFamily, DWrite.FontWeight.Normal, (DWrite.FontStyle)fontStyle, (float)fontSize), + format, float.MaxValue, float.MaxValue); }