14 changed files with 31 additions and 265 deletions
@ -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,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); |
|
||||
} |
|
||||
} |
|
||||
@ -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); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue