📄 htmlfontproperty.cs
字号:
case HtmlFontSize.xLarge:
size = "x-large";
break;
case HtmlFontSize.xxLarge:
size = "xx-large";
break;
case HtmlFontSize.Default:
size = string.Empty; //small
break;
}
// return the calculated size
return size;
} //HtmlFontSizeString
// return the correct bold description for the bold attribute
public static string HtmlFontBoldString(bool fontBold)
{
return (fontBold?"Bold":"Normal");
} //HtmlFontBoldString
// return the correct bold description for the bold attribute
public static string HtmlFontItalicString(bool fontItalic)
{
return (fontItalic?"Italic":"Normal");
} //HtmlFontItalicString
// determine the font size given a selected font in points
public static HtmlFontSize FontSizeToHtml(float fontSize)
{
// make the following mapping
// 1:8pt
// 2:10pt
// 3:12pt
// 4:14pt
// 5:18pt
// 6:24pt
// 7:36pt
int calcFont = 0;
if (fontSize < 10) calcFont = 1; // 8pt
else if (fontSize < 12) calcFont = 2; // 10pt
else if (fontSize < 14) calcFont = 3; // 12pt
else if (fontSize < 18) calcFont = 4; // 14pt
else if (fontSize < 24) calcFont = 5; // 24pt
else if (fontSize < 36) calcFont = 6; // 36pt
else calcFont = 7;
return (HtmlFontSize)calcFont;
} //FontSizeToHtml
// determine the font size given the html font size
public static float FontSizeFromHtml(HtmlFontSize fontSize)
{
return HtmlFontConversion.FontSizeFromHtml((int)fontSize);
} //FontSizeFromHtml
// determine the font size given the html int size
public static float FontSizeFromHtml(int fontSize)
{
// make the following mapping
// 1:8pt
// 2:10pt
// 3:12pt
// 4:14pt
// 5:18pt
// 6:24pt
// 7:36pt
float calcFont = 0;
switch (fontSize)
{
case 1:
calcFont = 8F;
break;
case 2:
calcFont = 10F;
break;
case 3:
calcFont = 12F;
break;
case 4:
calcFont = 14F;
break;
case 5:
calcFont = 18F;
break;
case 6:
calcFont = 24F;
break;
case 7:
calcFont = 36F;
break;
default:
calcFont = 12F;
break;
}
return calcFont;
} //FontSizeFromHtml
// Used to determine the HtmlFontSize from a style attribute
public static HtmlFontSize StyleSizeToHtml(string sizeDesc)
{
// currently assume the value is a fixed point
// should take into account relative and absolute values
float size;
try
{
size = Single.Parse(Regex.Replace(sizeDesc, @"[^\d|\.]", ""));
}
catch (Exception)
{
// set size to zero to return HtmlFontSize.Default
size = 0;
}
// return value as a HtmlFontSize
return HtmlFontConversion.FontSizeToHtml(size);
} //StyleSizeToHtml
// Used to determine the the style attribute is for Bold
public static bool IsStyleBold(string style)
{
return Regex.IsMatch(style, "bold|bolder|700|800|900", RegexOptions.IgnoreCase);
} //IsStyleBold
// Used to determine the the style attribute is for Italic
public static bool IsStyleItalic(string style)
{
return Regex.IsMatch(style, "style|oblique", RegexOptions.IgnoreCase);
} //IsStyleItalic
} //HtmlFontConversion
#endregion
#region HtmlFontPropertyConverter class
/// <summary>
/// Expandable object converter for the HtmlFontProperty
/// Allows it to be viewable from the property browser
/// String format based on "Name, FontSize"
/// </summary>
public class HtmlFontPropertyConverter : ExpandableObjectConverter
{
// constants used for the property names
private const string PROP_NAME = "Name";
private const string PROP_SIZE = "Size";
private const string PROP_BOLD = "Bold";
private const string PROP_ITALIC = "Italic";
private const string PROP_UNDERLINE = "Underline";
private const string PROP_STRIKEOUT = "Strikeout";
private const string PROP_SUBSCRIPT = "Subscript";
private const string PROP_SUPERSCRIPT = "Superscript";
// regular expression parse
private const string FONT_PARSE_EXPRESSION = @"^(?<name>(\w| )+)((\s*,\s*)?)(?<size>\w*)";
private const string FONT_PARSE_NAME = @"${name}";
private const string FONT_PARSE_SIZE = @"${size}";
// Allows expansion sub property change to have string updated
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
// always return a new instance
return true;
} //GetCreateInstanceSupported
// creates a new HtmlFontProperty from a series of values
public override object CreateInstance(ITypeDescriptorContext context, IDictionary values)
{
// obtain the HtmlFontProperty properties
string name = (string)values[PROP_NAME];
HtmlFontSize size = (HtmlFontSize)values[PROP_SIZE];
bool bold = (bool)values[PROP_BOLD];
bool italic = (bool)values[PROP_ITALIC];
bool underline = (bool)values[PROP_UNDERLINE];
bool strikeout = (bool)values[PROP_STRIKEOUT];
bool subscript = (bool)values[PROP_SUBSCRIPT];
bool superscript = (bool)values[PROP_SUPERSCRIPT];
// return the new HtmlFontProperty
return new HtmlFontProperty(name, size, bold, italic, underline, strikeout, subscript, superscript);
} //CreateInstance
// Indicates if a conversion can take place from a HtmlFontProperty
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string) || destinationType == typeof(InstanceDescriptor))
{
return true;
}
else
{
return base.CanConvertTo(context, destinationType);
}
} //CanConvertTo
// Performs the conversion from HtmlFontProperty to a string (only)
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
// ensure working with the intented type HtmlFontProperty
if (value is HtmlFontProperty)
{
HtmlFontProperty font = (HtmlFontProperty)value;
if (destinationType == typeof(string))
{
return font.ToString();
}
if (destinationType == typeof(InstanceDescriptor))
{
// define array to hold the properties and values
Object[] properties = new Object[8];
Type[] types = new Type[8];
// Name property
properties[0] = font.Name;
types[0] = typeof(string);
// Size property
properties[1] = font.Size;
types[1] = typeof(HtmlFontSize);
// Bold property
properties[2] = font.Bold;
types[2] = typeof(bool);
// Italic property
properties[3] = font.Italic;
types[3] = typeof(bool);
// Underline property
properties[4] = font.Underline;
types[4] = typeof(bool);
// Strikeout property
properties[5] = font.Strikeout;
types[5] = typeof(bool);
// Subscript property
properties[6] = font.Subscript;
types[6] = typeof(bool);
// Superscript property
properties[7] = font.Superscript;
types[7] = typeof(bool);
// create the instance constructor to return
ConstructorInfo ci = typeof(HtmlFontProperty).GetConstructor(types);
return new InstanceDescriptor(ci, properties);
}
}
// have something other than InstanceDescriptor or sting
return base.ConvertTo(context, culture, value, destinationType);
} //ConvertTo
// Indicates if a conversion can take place from s string
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
else
{
return base.CanConvertFrom(context, sourceType);
}
} //CanConvertFrom
// Performs the conversion from string to a HtmlFontProperty (only)
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
// define a new font property
string fontString = (string)value;
HtmlFontProperty font = new HtmlFontProperty(string.Empty);;
try
{
// parse the contents of the given string using a regex
string fontName = string.Empty;
string fontSize = string.Empty;
Regex expression = new Regex(FONT_PARSE_EXPRESSION, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.ExplicitCapture);
Match match = expression.Match(fontString);
// see if a match was found
if (match.Success)
{
// extract the content type elements
fontName = match.Result(FONT_PARSE_NAME);
fontSize = match.Result(FONT_PARSE_SIZE);
// set the fontname
TextInfo text = Thread.CurrentThread.CurrentCulture.TextInfo;
font.Name = text.ToTitleCase(fontName);
// determine size from given string using Small if blank
if (fontSize == string.Empty) fontSize = "Small";
font.Size = (HtmlFontSize)Enum.Parse(typeof(HtmlFontSize), fontSize, true);
}
}
catch (Exception)
{
// do nothing but ensure font is a null font
font.Name = string.Empty;
}
if (HtmlFontProperty.IsNull(font))
{
// error performing the string conversion so throw exception given possible format
string error = string.Format(@"Cannot convert '{0}' to Type HtmlFontProperty. Format: 'FontName, HtmlSize', Font Size values: {1}", fontString, string.Join(", ", Enum.GetNames(typeof(HtmlFontSize))));
throw new ArgumentException(error);
}
else
{
// return the font
return font;
}
}
else
{
return base.ConvertFrom(context, culture, value);
}
} //ConvertFrom
} //HtmlFontPropertyConverter
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -