From baafad1af7e565a447185b6fdf4350e353045b07 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 7 Dec 2014 20:54:29 +0100 Subject: [PATCH] Removed ITextService. Cairo renderer completely broken by this point. --- Cairo/Perspex.Cairo/CairoPlatform.cs | 13 --- Cairo/Perspex.Cairo/Media/DrawingContext.cs | 15 +-- Cairo/Perspex.Cairo/Media/TextService.cs | 92 ------------------- Cairo/Perspex.Cairo/Perspex.Cairo.csproj | 1 - Perspex.Controls/TextBox.cs | 8 +- Perspex.Layout.UnitTests/FullLayoutTests.cs | 2 +- Perspex.SceneGraph/Media/FormattedText.cs | 36 ++++---- Perspex.SceneGraph/Perspex.SceneGraph.csproj | 1 - .../Platform/IPlatformRenderInterface.cs | 2 - Perspex.SceneGraph/Platform/ITextService.cs | 23 ----- .../Perspex.Direct2D1/Direct2D1Platform.cs | 7 -- .../Perspex.Direct2D1/Media/DrawingContext.cs | 5 +- .../Perspex.Direct2D1/Media/TextService.cs | 90 ------------------ .../Perspex.Direct2D1.csproj | 1 - 14 files changed, 31 insertions(+), 265 deletions(-) delete mode 100644 Cairo/Perspex.Cairo/Media/TextService.cs delete mode 100644 Perspex.SceneGraph/Platform/ITextService.cs delete mode 100644 Windows/Perspex.Direct2D1/Media/TextService.cs diff --git a/Cairo/Perspex.Cairo/CairoPlatform.cs b/Cairo/Perspex.Cairo/CairoPlatform.cs index 0fd02284c9..2b1016a2e6 100644 --- a/Cairo/Perspex.Cairo/CairoPlatform.cs +++ b/Cairo/Perspex.Cairo/CairoPlatform.cs @@ -18,18 +18,10 @@ namespace Perspex.Cairo { private static CairoPlatform instance = new CairoPlatform(); - private static TextService textService = new TextService(); - - public ITextService TextService - { - get { return textService; } - } - public static void Initialize() { var locator = Locator.CurrentMutable; locator.Register(() => instance, typeof(IPlatformRenderInterface)); - locator.Register(() => textService, typeof(ITextService)); } public IBitmapImpl CreateBitmap(int width, int height) @@ -40,11 +32,6 @@ namespace Perspex.Cairo public IRenderer CreateRenderer(IPlatformHandle handle, double width, double height) { - if (textService.Context == null) - { - textService.Context = this.GetPangoContext(handle); - } - return new Renderer(handle, width, height); } diff --git a/Cairo/Perspex.Cairo/Media/DrawingContext.cs b/Cairo/Perspex.Cairo/Media/DrawingContext.cs index e765a6ce8b..830d2b2f29 100644 --- a/Cairo/Perspex.Cairo/Media/DrawingContext.cs +++ b/Cairo/Perspex.Cairo/Media/DrawingContext.cs @@ -29,11 +29,6 @@ namespace Perspex.Cairo.Media /// private Cairo.Surface surface; - /// - /// The text service. - /// - private TextService textService; - /// /// Initializes a new instance of the class. /// @@ -42,7 +37,6 @@ namespace Perspex.Cairo.Media { this.surface = surface; this.context = new Cairo.Context(surface); - this.textService = Locator.Current.GetService() as TextService; this.CurrentTransform = Matrix.Identity; } @@ -54,7 +48,6 @@ namespace Perspex.Cairo.Media { this.Drawable = drawable; this.context = Gdk.CairoHelper.Create(drawable); - this.textService = Locator.Current.GetService() as TextService; this.CurrentTransform = Matrix.Identity; } @@ -144,10 +137,10 @@ namespace Perspex.Cairo.Media /// The text. public void DrawText(Perspex.Media.Brush foreground, Rect rect, FormattedText text) { - var layout = this.textService.CreateLayout(text); - this.SetBrush(foreground); - this.context.MoveTo(rect.X, rect.Y); - Pango.CairoHelper.ShowLayout(this.context, layout); + ////var layout = this.textService.CreateLayout(text); + ////this.SetBrush(foreground); + ////this.context.MoveTo(rect.X, rect.Y); + ////Pango.CairoHelper.ShowLayout(this.context, layout); } /// diff --git a/Cairo/Perspex.Cairo/Media/TextService.cs b/Cairo/Perspex.Cairo/Media/TextService.cs deleted file mode 100644 index f37ae9ecf2..0000000000 --- a/Cairo/Perspex.Cairo/Media/TextService.cs +++ /dev/null @@ -1,92 +0,0 @@ -// ----------------------------------------------------------------------- -// -// Copyright 2014 MIT Licence. See licence.md for more information. -// -// ----------------------------------------------------------------------- - -namespace Perspex.Cairo.Media -{ - using System; - using System.Linq; - using System.Runtime.InteropServices; - using Perspex.Media; - using Perspex.Platform; - - public class TextService : ITextService - { - /// - /// Gets the pango context to be used by the service. - /// - /// > - /// There seems to be no way in GtkSharp to create a new Pango Context, so this has to - /// be injected by CairoPlatform the first time a renderer is created. - /// - public Pango.Context Context - { - get; - internal set; - } - - public Pango.Layout CreateLayout(FormattedText text) - { - var layout = new Pango.Layout(this.Context) - { - FontDescription = new Pango.FontDescription - { - Family = text.FontFamilyName, - Size = Pango.Units.FromDouble(text.FontSize), - Style = (Pango.Style)text.FontStyle, - } - }; - - layout.SetText(text.Text); - - return layout; - } - - public int GetCaretIndex(FormattedText text, Point point, Size constraint) - { - var layout = this.CreateLayout(text); - int result; - int trailing; - return layout.XyToIndex( - Pango.Units.FromDouble(point.X), - Pango.Units.FromDouble(point.Y), - out result, - out trailing) ? result : text.Text.Length; - } - - public Point GetCaretPosition(FormattedText text, int caretIndex, Size constraint) - { - // TODO: Rather than have this and GetLineHeights, might be best to just return - // the rect if that's also possible in Direct2D backend. - var layout = this.CreateLayout(text); - var rect = layout.IndexToPos(caretIndex); - return new Point(Pango.Units.ToDouble(rect.X), Pango.Units.ToDouble(rect.Y)); - } - - public double[] GetLineHeights(FormattedText text, Size constraint) - { - var layout = this.CreateLayout(text); - var lines = layout.Lines; - return lines.Select(x => - { - var inkRect = new Pango.Rectangle(); - var logicalRect = new Pango.Rectangle(); - x.GetExtents(ref inkRect, ref logicalRect); - return (double)logicalRect.Height; - }).ToArray(); - } - - public Size Measure(FormattedText text, Size constraint) - { - var layout = this.CreateLayout(text); - - Pango.Rectangle inkRect; - Pango.Rectangle logicalRect; - layout.GetExtents(out inkRect, out logicalRect); - - return new Size(Pango.Units.ToDouble(logicalRect.Width), Pango.Units.ToDouble(logicalRect.Height)); - } - } -} diff --git a/Cairo/Perspex.Cairo/Perspex.Cairo.csproj b/Cairo/Perspex.Cairo/Perspex.Cairo.csproj index 56a0e5ac88..a9e2222701 100644 --- a/Cairo/Perspex.Cairo/Perspex.Cairo.csproj +++ b/Cairo/Perspex.Cairo/Perspex.Cairo.csproj @@ -68,7 +68,6 @@ - diff --git a/Perspex.Controls/TextBox.cs b/Perspex.Controls/TextBox.cs index cfb2c858a3..5b4dfa963f 100644 --- a/Perspex.Controls/TextBox.cs +++ b/Perspex.Controls/TextBox.cs @@ -159,11 +159,9 @@ namespace Perspex.Controls private void OnPointerPressed(object sender, PointerEventArgs e) { - IPlatformRenderInterface platform = Locator.Current.GetService(); - this.CaretIndex = platform.TextService.GetCaretIndex( - this.textBoxView.FormattedText, - e.GetPosition(this.textBoxView), - this.ActualSize); + var point = e.GetPosition(this.textBoxView); + var hit = this.textBoxView.FormattedText.HitTestPoint(point); + this.CaretIndex = hit.TextPosition + (hit.IsTrailing ? 1 : 0); } } } diff --git a/Perspex.Layout.UnitTests/FullLayoutTests.cs b/Perspex.Layout.UnitTests/FullLayoutTests.cs index 84694cba57..d894e3fe9b 100644 --- a/Perspex.Layout.UnitTests/FullLayoutTests.cs +++ b/Perspex.Layout.UnitTests/FullLayoutTests.cs @@ -143,7 +143,7 @@ namespace Perspex.Layout.UnitTests l.RegisterConstant(new Styler(), typeof(IStyler)); l.RegisterConstant(globalStyles.Object, typeof(IGlobalStyles)); l.RegisterConstant(windowImpl.Object, typeof(IWindowImpl)); - l.RegisterConstant(new Mock().Object, typeof(ITextService)); + l.RegisterConstant(new Mock().Object, typeof(IFormattedTextImpl)); } } } diff --git a/Perspex.SceneGraph/Media/FormattedText.cs b/Perspex.SceneGraph/Media/FormattedText.cs index 98f11b8cab..61a7c54b3d 100644 --- a/Perspex.SceneGraph/Media/FormattedText.cs +++ b/Perspex.SceneGraph/Media/FormattedText.cs @@ -18,56 +18,60 @@ namespace Perspex.Media public class FormattedText { - private IFormattedTextImpl impl; - public FormattedText() { - this.impl = Locator.Current.GetService(); + this.PlatformImpl = Locator.Current.GetService(); } public Size Constraint { - get { return this.impl.Constraint; } - set { this.impl.Constraint = value; } + get { return this.PlatformImpl.Constraint; } + set { this.PlatformImpl.Constraint = value; } } public string FontFamilyName { - get { return this.impl.FontFamilyName; } - set { this.impl.FontFamilyName = value; } + get { return this.PlatformImpl.FontFamilyName; } + set { this.PlatformImpl.FontFamilyName = value; } } public double FontSize { - get { return this.impl.FontSize; } - set { this.impl.FontSize = value; } + get { return this.PlatformImpl.FontSize; } + set { this.PlatformImpl.FontSize = value; } } public FontStyle FontStyle { - get { return this.impl.FontStyle; } - set { this.impl.FontStyle = value; } + get { return this.PlatformImpl.FontStyle; } + set { this.PlatformImpl.FontStyle = value; } } public string Text { - get { return this.impl.Text; } - set { this.impl.Text = value; } + get { return this.PlatformImpl.Text; } + set { this.PlatformImpl.Text = value; } + } + + public IFormattedTextImpl PlatformImpl + { + get; + private set; } public TextHitTestResult HitTestPoint(Point point) { - return this.impl.HitTestPoint(point); + return this.PlatformImpl.HitTestPoint(point); } public Rect HitTestTextPosition(int index) { - return this.impl.HitTestTextPosition(index); + return this.PlatformImpl.HitTestTextPosition(index); } public Size Measure() { - return this.impl.Measure(); + return this.PlatformImpl.Measure(); } } } diff --git a/Perspex.SceneGraph/Perspex.SceneGraph.csproj b/Perspex.SceneGraph/Perspex.SceneGraph.csproj index 4dcdf4d191..ae3f515249 100644 --- a/Perspex.SceneGraph/Perspex.SceneGraph.csproj +++ b/Perspex.SceneGraph/Perspex.SceneGraph.csproj @@ -73,7 +73,6 @@ - diff --git a/Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs b/Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs index 30b7c82d0e..a142e056bb 100644 --- a/Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs +++ b/Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs @@ -11,8 +11,6 @@ namespace Perspex.Platform public interface IPlatformRenderInterface { - ITextService TextService { get; } - IBitmapImpl CreateBitmap(int width, int height); IStreamGeometryImpl CreateStreamGeometry(); diff --git a/Perspex.SceneGraph/Platform/ITextService.cs b/Perspex.SceneGraph/Platform/ITextService.cs deleted file mode 100644 index cabd99e25d..0000000000 --- a/Perspex.SceneGraph/Platform/ITextService.cs +++ /dev/null @@ -1,23 +0,0 @@ -// ----------------------------------------------------------------------- -// -// Copyright 2014 MIT Licence. See licence.md for more information. -// -// ----------------------------------------------------------------------- - -namespace Perspex.Platform -{ - using System; - using Perspex.Media; - - [Obsolete("Use methods on FormattedText instead.")] - public interface ITextService - { - int GetCaretIndex(FormattedText text, Point point, Size constraint); - - Point GetCaretPosition(FormattedText text, int caretIndex, Size constraint); - - double[] GetLineHeights(FormattedText text, Size constraint); - - Size Measure(FormattedText text, Size constraint); - } -} diff --git a/Windows/Perspex.Direct2D1/Direct2D1Platform.cs b/Windows/Perspex.Direct2D1/Direct2D1Platform.cs index b797b7b345..b070c417ec 100644 --- a/Windows/Perspex.Direct2D1/Direct2D1Platform.cs +++ b/Windows/Perspex.Direct2D1/Direct2D1Platform.cs @@ -22,13 +22,6 @@ namespace Perspex.Direct2D1 private static SharpDX.WIC.ImagingFactory imagingFactory = new SharpDX.WIC.ImagingFactory(); - private static TextService textService = new TextService(dwfactory); - - public ITextService TextService - { - get { return textService; } - } - public static void Initialize() { var locator = Locator.CurrentMutable; diff --git a/Windows/Perspex.Direct2D1/Media/DrawingContext.cs b/Windows/Perspex.Direct2D1/Media/DrawingContext.cs index d02477cc95..f9f5151a37 100644 --- a/Windows/Perspex.Direct2D1/Media/DrawingContext.cs +++ b/Windows/Perspex.Direct2D1/Media/DrawingContext.cs @@ -140,12 +140,13 @@ namespace Perspex.Direct2D1.Media { if (!string.IsNullOrEmpty(text.Text)) { + var impl = (FormattedTextImpl)text.PlatformImpl; + using (SharpDX.Direct2D1.SolidColorBrush brush = this.Convert(foreground)) - using (SharpDX.DirectWrite.TextFormat format = TextService.GetTextFormat(this.directWriteFactory, text)) { this.renderTarget.DrawText( text.Text, - format, + impl.Layout, this.Convert(rect), brush); } diff --git a/Windows/Perspex.Direct2D1/Media/TextService.cs b/Windows/Perspex.Direct2D1/Media/TextService.cs deleted file mode 100644 index eb53ceb62d..0000000000 --- a/Windows/Perspex.Direct2D1/Media/TextService.cs +++ /dev/null @@ -1,90 +0,0 @@ -// ----------------------------------------------------------------------- -// -// Copyright 2014 MIT Licence. See licence.md for more information. -// -// ----------------------------------------------------------------------- - -namespace Perspex.Direct2D1.Media -{ - using System; - using System.Linq; - using Perspex.Media; - using Perspex.Platform; - using SharpDX.DirectWrite; - - public class TextService : ITextService - { - private Factory factory; - - public TextService(Factory factory) - { - this.factory = factory; - } - - public static TextFormat GetTextFormat(Factory factory, FormattedText text) - { - return new TextFormat( - factory, - text.FontFamilyName, - FontWeight.Normal, - (SharpDX.DirectWrite.FontStyle)text.FontStyle, - (float)text.FontSize); - } - - public TextLayout GetTextLayout(Factory factory, FormattedText text, Size constraint) - { - return new TextLayout( - factory, - text.Text ?? string.Empty, - GetTextFormat(factory, text), - (float)constraint.Width, - (float)constraint.Height); - } - - public int GetCaretIndex(FormattedText text, Point point, Size constraint) - { - using (TextLayout layout = this.GetTextLayout(this.factory, text, constraint)) - { - SharpDX.Bool isTrailingHit; - SharpDX.Bool isInside; - - HitTestMetrics result = layout.HitTestPoint( - (float)point.X, - (float)point.Y, - out isTrailingHit, - out isInside); - - return result.TextPosition + (isTrailingHit ? 1 : 0); - } - } - - public Point GetCaretPosition(FormattedText text, int caretIndex, Size constraint) - { - using (TextLayout layout = this.GetTextLayout(this.factory, text, constraint)) - { - float x; - float y; - layout.HitTestTextPosition(caretIndex, false, out x, out y); - return new Point(x, y); - } - } - - public double[] GetLineHeights(FormattedText text, Size constraint) - { - using (TextLayout layout = this.GetTextLayout(this.factory, text, constraint)) - { - return layout.GetLineMetrics().Select(x => (double)x.Height).ToArray(); - } - } - - public Size Measure(FormattedText text, Size constraint) - { - using (TextLayout layout = this.GetTextLayout(this.factory, text, constraint)) - { - return new Size( - layout.Metrics.WidthIncludingTrailingWhitespace, - layout.Metrics.Height); - } - } - } -} diff --git a/Windows/Perspex.Direct2D1/Perspex.Direct2D1.csproj b/Windows/Perspex.Direct2D1/Perspex.Direct2D1.csproj index 0a756543b2..6985f9c11e 100644 --- a/Windows/Perspex.Direct2D1/Perspex.Direct2D1.csproj +++ b/Windows/Perspex.Direct2D1/Perspex.Direct2D1.csproj @@ -77,7 +77,6 @@ -