// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
//
//
// FontFamilyConverter - converter class for converting between the
// and types.
//
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Helpers
{
using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
///
/// FontFamilyConverter - converter class for converting between the
/// and types.
///
public class FontFamilyConverter : TypeConverter
{
///
/// Returns whether this converter can convert an object of the given type to the type of
/// this converter, using the specified context.
///
///
/// true if this converter can perform the conversion; otherwise, false.
///
///
/// An that provides a format context.
///
///
/// A that represents the type you want to convert from.
///
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
///
/// Returns whether this converter can convert the object to the specified type, using
/// the specified context.
///
///
/// true if this converter can perform the conversion; otherwise, false.
///
///
/// An that provides a format context.
///
///
/// A that represents the type you want to convert to.
///
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string) || destinationType == typeof(FontFamily))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
///
/// Converts the given object to the type of this converter, using the specified context and
/// culture information.
///
///
/// An that represents the converted value.
///
///
/// An that provides a format context.
///
///
/// The to use as the current culture.
///
/// The to convert.
/// The conversion cannot be performed.
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string s = value as string;
if (!string.IsNullOrWhiteSpace(s))
{
return new FontFamily(s);
}
return base.ConvertFrom(context, culture, value);
}
///
/// Converts the given value object to the specified type, using the specified context and culture
/// information.
///
///
/// An that represents the converted value.
///
///
/// An that provides a format context.
///
///
/// A . If null is passed, the current culture is assumed.
/// The to convert.
///
/// The to convert the parameter to.
///
/// The parameter is null.
/// The conversion cannot be performed.
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (null == value)
{
throw new ArgumentNullException("value");
}
FontFamily fontFamily = value as FontFamily;
if (fontFamily == null)
{
throw new ArgumentException("value");
}
if (null == destinationType)
{
throw new ArgumentNullException("destinationType");
}
if (destinationType == typeof(string))
{
return fontFamily.Name;
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}