Browse Source

Removed ITextService.

Cairo renderer completely broken by this point.
pull/10/head
Steven Kirk 11 years ago
parent
commit
baafad1af7
  1. 13
      Cairo/Perspex.Cairo/CairoPlatform.cs
  2. 15
      Cairo/Perspex.Cairo/Media/DrawingContext.cs
  3. 92
      Cairo/Perspex.Cairo/Media/TextService.cs
  4. 1
      Cairo/Perspex.Cairo/Perspex.Cairo.csproj
  5. 8
      Perspex.Controls/TextBox.cs
  6. 2
      Perspex.Layout.UnitTests/FullLayoutTests.cs
  7. 36
      Perspex.SceneGraph/Media/FormattedText.cs
  8. 1
      Perspex.SceneGraph/Perspex.SceneGraph.csproj
  9. 2
      Perspex.SceneGraph/Platform/IPlatformRenderInterface.cs
  10. 23
      Perspex.SceneGraph/Platform/ITextService.cs
  11. 7
      Windows/Perspex.Direct2D1/Direct2D1Platform.cs
  12. 5
      Windows/Perspex.Direct2D1/Media/DrawingContext.cs
  13. 90
      Windows/Perspex.Direct2D1/Media/TextService.cs
  14. 1
      Windows/Perspex.Direct2D1/Perspex.Direct2D1.csproj

13
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);
}

15
Cairo/Perspex.Cairo/Media/DrawingContext.cs

@ -29,11 +29,6 @@ namespace Perspex.Cairo.Media
/// </summary>
private Cairo.Surface surface;
/// <summary>
/// The text service.
/// </summary>
private TextService textService;
/// <summary>
/// Initializes a new instance of the <see cref="DrawingContext"/> class.
/// </summary>
@ -42,7 +37,6 @@ namespace Perspex.Cairo.Media
{
this.surface = surface;
this.context = new Cairo.Context(surface);
this.textService = Locator.Current.GetService<TextService>() 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<ITextService>() as TextService;
this.CurrentTransform = Matrix.Identity;
}
@ -144,10 +137,10 @@ namespace Perspex.Cairo.Media
/// <param name="text">The text.</param>
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);
}
/// <summary>

92
Cairo/Perspex.Cairo/Media/TextService.cs

@ -1,92 +0,0 @@
// -----------------------------------------------------------------------
// <copyright file="TextService.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Cairo.Media
{
using System;
using System.Linq;
using System.Runtime.InteropServices;
using Perspex.Media;
using Perspex.Platform;
public class TextService : ITextService
{
/// <summary>
/// Gets the pango context to be used by the service.
/// </summary>
/// <remarks>>
/// 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.
/// </remarks>
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));
}
}
}

1
Cairo/Perspex.Cairo/Perspex.Cairo.csproj

@ -68,7 +68,6 @@
<Compile Include="CairoPlatform.cs" />
<Compile Include="Media\DrawingContext.cs" />
<Compile Include="Media\Imaging\BitmapImpl.cs" />
<Compile Include="Media\TextService.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Renderer.cs" />
<Compile Include="CairoExtensions.cs" />

8
Perspex.Controls/TextBox.cs

@ -159,11 +159,9 @@ namespace Perspex.Controls
private void OnPointerPressed(object sender, PointerEventArgs e)
{
IPlatformRenderInterface platform = Locator.Current.GetService<IPlatformRenderInterface>();
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);
}
}
}

2
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<ITextService>().Object, typeof(ITextService));
l.RegisterConstant(new Mock<IFormattedTextImpl>().Object, typeof(IFormattedTextImpl));
}
}
}

36
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<IFormattedTextImpl>();
this.PlatformImpl = Locator.Current.GetService<IFormattedTextImpl>();
}
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();
}
}
}

1
Perspex.SceneGraph/Perspex.SceneGraph.csproj

@ -73,7 +73,6 @@
<Compile Include="Platform\IRenderTargetBitmapImpl.cs" />
<Compile Include="Platform\IStreamGeometryContextImpl.cs" />
<Compile Include="Platform\IStreamGeometryImpl.cs" />
<Compile Include="Platform\ITextService.cs" />
<Compile Include="Point.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Rect.cs" />

2
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();

23
Perspex.SceneGraph/Platform/ITextService.cs

@ -1,23 +0,0 @@
// -----------------------------------------------------------------------
// <copyright file="ITextService.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
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);
}
}

7
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;

5
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);
}

90
Windows/Perspex.Direct2D1/Media/TextService.cs

@ -1,90 +0,0 @@
// -----------------------------------------------------------------------
// <copyright file="TextService.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
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);
}
}
}
}

1
Windows/Perspex.Direct2D1/Perspex.Direct2D1.csproj

@ -77,7 +77,6 @@
<Compile Include="PrimitiveExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Renderer.cs" />
<Compile Include="Media\TextService.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />

Loading…
Cancel
Save