// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Collections.Generic;
using Perspex.Platform;
namespace Perspex.Media
{
///
/// Represents a piece of text with formatting.
///
public class FormattedText : PerspexDisposable
{
///
/// Initializes a new instance of the class.
///
/// The text.
/// The font family.
/// The font size.
/// The font style.
/// The text alignment.
/// The font weight.
public FormattedText(
string text,
string fontFamilyName,
double fontSize,
FontStyle fontStyle,
TextAlignment textAlignment,
FontWeight fontWeight)
{
Contract.Requires(text != null);
Contract.Requires(fontFamilyName != null);
Contract.Requires(fontSize > 0);
Text = text;
FontFamilyName = fontFamilyName;
FontSize = fontSize;
FontStyle = fontStyle;
FontWeight = fontWeight;
TextAlignment = textAlignment;
var platform = PerspexLocator.Current.GetService();
PlatformImpl = platform.CreateFormattedText(
text,
fontFamilyName,
fontSize,
fontStyle,
textAlignment,
fontWeight);
}
///
/// Gets or sets the constraint of the text.
///
public Size Constraint
{
get
{
CheckDisposed();
return PlatformImpl.Constraint;
}
set
{
CheckDisposed();
PlatformImpl.Constraint = value;
}
}
///
/// Gets the font family.
///
public string FontFamilyName
{
get;
private set;
}
///
/// Gets the font size.
///
public double FontSize
{
get;
private set;
}
///
/// Gets the font style.
///
public FontStyle FontStyle
{
get;
private set;
}
///
/// Gets the font weight.
///
public FontWeight FontWeight
{
get;
private set;
}
///
/// Gets the text.
///
public string Text
{
get;
private set;
}
///
/// Gets platform-specific platform implementation.
///
public IFormattedTextImpl PlatformImpl
{
get; }
///
/// Gets the text alignment.
///
public TextAlignment TextAlignment
{
get;
private set;
}
///
/// Disposes of unmanaged resources associated with the formatted text.
///
protected override void DoDispose()
{
PlatformImpl.Dispose();
}
///
/// Gets the lines in the text.
///
///
/// A collection of objects.
///
public IEnumerable GetLines()
{
CheckDisposed();
return PlatformImpl.GetLines();
}
///
/// Hit tests a point in the text.
///
/// The point.
///
/// A describing the result of the hit test.
///
public TextHitTestResult HitTestPoint(Point point)
{
CheckDisposed();
return PlatformImpl.HitTestPoint(point);
}
///
/// Gets the bounds rectangle that the specified character occupies.
///
/// The index of the character.
/// The character bounds.
public Rect HitTestTextPosition(int index)
{
CheckDisposed();
return PlatformImpl.HitTestTextPosition(index);
}
///
/// Gets the bounds rectangles that the specified text range occupies.
///
/// The index of the first character.
/// The number of characters in the text range.
/// The character bounds.
public IEnumerable HitTestTextRange(int index, int length)
{
CheckDisposed();
return PlatformImpl.HitTestTextRange(index, length);
}
///
/// Gets the size of the text, taking into account.
///
/// The bounds box of the text.
public Size Measure()
{
CheckDisposed();
return PlatformImpl.Measure();
}
///
/// Sets the foreground brush for the specified text range.
///
/// The brush.
/// The start of the text range.
/// The length of the text range.
public void SetForegroundBrush(Brush brush, int startIndex, int length)
{
CheckDisposed();
PlatformImpl.SetForegroundBrush(brush, startIndex, length);
}
}
}