|
|
|
@ -7,6 +7,7 @@ |
|
|
|
namespace Perspex.Direct2D1 |
|
|
|
{ |
|
|
|
using System; |
|
|
|
using System.Linq; |
|
|
|
using Perspex.Media; |
|
|
|
using Perspex.Platform; |
|
|
|
using SharpDX.DirectWrite; |
|
|
|
@ -28,46 +29,60 @@ namespace Perspex.Direct2D1 |
|
|
|
(float)text.FontSize); |
|
|
|
} |
|
|
|
|
|
|
|
public TextLayout GetTextLayout(Factory factory, FormattedText text) |
|
|
|
public TextLayout GetTextLayout(Factory factory, FormattedText text, Size constraint) |
|
|
|
{ |
|
|
|
return new TextLayout( |
|
|
|
factory, |
|
|
|
text.Text, |
|
|
|
GetTextFormat(factory, text), |
|
|
|
float.MaxValue, |
|
|
|
float.MaxValue); |
|
|
|
(float)constraint.Width, |
|
|
|
(float)constraint.Height); |
|
|
|
} |
|
|
|
|
|
|
|
public int GetCaretIndex(FormattedText text, Point point) |
|
|
|
public int GetCaretIndex(FormattedText text, Point point, Size constraint) |
|
|
|
{ |
|
|
|
TextLayout layout = GetTextLayout(this.factory, text); |
|
|
|
SharpDX.Bool isTrailingHit; |
|
|
|
SharpDX.Bool isInside; |
|
|
|
using (TextLayout layout = 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); |
|
|
|
HitTestMetrics result = layout.HitTestPoint( |
|
|
|
(float)point.X, |
|
|
|
(float)point.Y, |
|
|
|
out isTrailingHit, |
|
|
|
out isInside); |
|
|
|
|
|
|
|
return result.TextPosition + (isTrailingHit ? 1 : 0); |
|
|
|
return result.TextPosition + (isTrailingHit ? 1 : 0); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public Point GetCaretPosition(FormattedText text, int caretIndex) |
|
|
|
public Point GetCaretPosition(FormattedText text, int caretIndex, Size constraint) |
|
|
|
{ |
|
|
|
TextLayout layout = GetTextLayout(this.factory, text); |
|
|
|
float x; |
|
|
|
float y; |
|
|
|
layout.HitTestTextPosition(caretIndex, false, out x, out y); |
|
|
|
return new Point(x, y); |
|
|
|
using (TextLayout layout = GetTextLayout(this.factory, text, constraint)) |
|
|
|
{ |
|
|
|
float x; |
|
|
|
float y; |
|
|
|
layout.HitTestTextPosition(caretIndex, false, out x, out y); |
|
|
|
return new Point(x, y); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public Size Measure(FormattedText text) |
|
|
|
public double[] GetLineHeights(FormattedText text, Size constraint) |
|
|
|
{ |
|
|
|
TextLayout layout = GetTextLayout(this.factory, text); |
|
|
|
return new Size( |
|
|
|
layout.Metrics.WidthIncludingTrailingWhitespace, |
|
|
|
layout.Metrics.Height); |
|
|
|
using (TextLayout layout = GetTextLayout(this.factory, text, constraint)) |
|
|
|
{ |
|
|
|
return layout.GetLineMetrics().Select(x => (double)x.Height).ToArray(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public Size Measure(FormattedText text, Size constraint) |
|
|
|
{ |
|
|
|
using (TextLayout layout = GetTextLayout(this.factory, text, constraint)) |
|
|
|
{ |
|
|
|
return new Size( |
|
|
|
layout.Metrics.WidthIncludingTrailingWhitespace, |
|
|
|
layout.Metrics.Height); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|