// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
using System;
using System.Drawing;
using TheArtOfDev.HtmlRenderer.Adapters;
namespace TheArtOfDev.HtmlRenderer.WinForms.Adapters
{
///
/// Adapter for WinForms Font object for core.
///
internal sealed class FontAdapter : RFont
{
#region Fields and Consts
///
/// the underline win-forms font.
///
private readonly Font _font;
///
/// a handle to this Font.
///
private IntPtr _hFont;
///
/// the vertical offset of the font underline location from the top of the font.
///
private float _underlineOffset = -1;
///
/// Cached font height.
///
private float _height = -1;
///
/// Cached font whitespace width.
///
private double _whitespaceWidth = -1;
#endregion
///
/// Init.
///
public FontAdapter(Font font)
{
_font = font;
}
///
/// the underline win-forms font.
///
public Font Font
{
get { return _font; }
}
///
/// Get the handle to this Font.
///
public IntPtr HFont
{
get
{
if (_hFont == IntPtr.Zero)
_hFont = _font.ToHfont();
return _hFont;
}
}
public override double Size
{
get { return _font.Size; }
}
public override double UnderlineOffset
{
get { return _underlineOffset; }
}
public override double Height
{
get { return _height; }
}
public override double LeftPadding
{
get { return _height / 6f; }
}
public override double GetWhitespaceWidth(RGraphics graphics)
{
if (_whitespaceWidth < 0)
{
_whitespaceWidth = graphics.MeasureString(" ", this).Width;
}
return _whitespaceWidth;
}
///
/// Set font metrics to be cached for the font for future use.
///
/// the full height of the font
/// the vertical offset of the font underline location from the top of the font.
internal void SetMetrics(int height, int underlineOffset)
{
_height = height;
_underlineOffset = underlineOffset;
}
}
}