CssFontProperty
More information available:
https://developer.mozilla.org/en-US/docs/Web/CSS/font
using AngleSharp.Css;
using AngleSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
namespace AngleSharp.Dom.Css
{
internal sealed class CssFontProperty : CssShorthandProperty
{
internal static readonly IValueConverter<SystemFont> SystemFontConverter = new Dictionary<string, SystemFont> {
{
Keywords.Caption,
SystemFont.Caption
},
{
Keywords.Icon,
SystemFont.Icon
},
{
Keywords.Menu,
SystemFont.Menu
},
{
Keywords.MessageBox,
SystemFont.MessageBox
},
{
Keywords.SmallCaption,
SystemFont.SmallCaption
},
{
Keywords.StatusBar,
SystemFont.StatusBar
}
}.ToConverter();
internal static readonly IValueConverter<Tuple<Tuple<CssValue, CssValue, CssValue, CssValue>, Tuple<CssValue, CssValue>, CssValue>> Converter = Converters.WithOrder(Converters.WithAny(Converters.FontStyleConverter.Val().Option(), Converters.FontVariantConverter.Val().Option(), CssFontWeightProperty.Converter.Val().Option(), Converters.FontStretchConverter.Val().Option()), Converters.WithOrder(Converters.FontSizeConverter.Val().Required(), Converters.LineHeightConverter.Val().StartsWithDelimiter().Option()), CssFontFamilyProperty.Converter.Val().Required());
internal CssFontProperty(CssStyleDeclaration rule)
: base(PropertyNames.Font, rule, PropertyFlags.Inherited | PropertyFlags.Animatable)
{
}
protected override bool IsValid(CssValue value)
{
if (!Converter.TryConvert(value, delegate(Tuple<Tuple<CssValue, CssValue, CssValue, CssValue>, Tuple<CssValue, CssValue>, CssValue> m) {
Get<CssFontStyleProperty>().TrySetValue(m.Item1.Item1);
Get<CssFontVariantProperty>().TrySetValue(m.Item1.Item2);
Get<CssFontWeightProperty>().TrySetValue(m.Item1.Item3);
Get<CssFontStretchProperty>().TrySetValue(m.Item1.Item4);
Get<CssFontSizeProperty>().TrySetValue(m.Item2.Item1);
Get<CssLineHeightProperty>().TrySetValue(m.Item2.Item2);
Get<CssFontFamilyProperty>().TrySetValue(m.Item3);
}))
return SystemFontConverter.TryConvert(value, SetSystemFont);
return true;
}
private void SetSystemFont(SystemFont font)
{
switch (font) {
case SystemFont.Caption:
case SystemFont.Icon:
case SystemFont.MessageBox:
SetFont("Arial", "16px");
break;
case SystemFont.Menu:
case SystemFont.StatusBar:
SetFont("Segoe UI", "12px");
break;
case SystemFont.SmallCaption:
SetFont("Segoe UI", "15px");
break;
}
}
private void SetFont(string family, string size)
{
string propertyValue = size + " " + family.CssString();
base.Rule.SetPropertyValue(base.Name, propertyValue);
}
internal override string SerializeValue(IEnumerable<CssProperty> properties)
{
CssFontFamilyProperty cssFontFamilyProperty = properties.OfType<CssFontFamilyProperty>().FirstOrDefault();
CssFontSizeProperty cssFontSizeProperty = properties.OfType<CssFontSizeProperty>().FirstOrDefault();
CssFontStyleProperty cssFontStyleProperty = properties.OfType<CssFontStyleProperty>().FirstOrDefault();
CssFontVariantProperty cssFontVariantProperty = properties.OfType<CssFontVariantProperty>().FirstOrDefault();
CssFontWeightProperty cssFontWeightProperty = properties.OfType<CssFontWeightProperty>().FirstOrDefault();
CssFontStretchProperty cssFontStretchProperty = properties.OfType<CssFontStretchProperty>().FirstOrDefault();
CssLineHeightProperty cssLineHeightProperty = properties.OfType<CssLineHeightProperty>().FirstOrDefault();
if (cssFontFamilyProperty == null || cssFontSizeProperty == null)
return string.Empty;
List<string> list = new List<string>();
if (cssFontStyleProperty != null && cssFontStyleProperty.HasValue)
list.Add(cssFontStyleProperty.SerializeValue());
if (cssFontVariantProperty != null && cssFontVariantProperty.HasValue)
list.Add(cssFontVariantProperty.SerializeValue());
if (cssFontWeightProperty != null && cssFontWeightProperty.HasValue)
list.Add(cssFontWeightProperty.SerializeValue());
if (cssFontStretchProperty != null && cssFontStretchProperty.HasValue)
list.Add(cssFontStretchProperty.SerializeValue());
list.Add(cssFontSizeProperty.SerializeValue());
if (cssLineHeightProperty != null && cssLineHeightProperty.HasValue) {
int index = list.Count - 1;
string item = list[index] + "/" + cssLineHeightProperty.SerializeValue();
list.RemoveAt(index);
list.Add(item);
}
list.Add(cssFontFamilyProperty.SerializeValue());
list.RemoveAll((string m) => string.IsNullOrEmpty(m));
return string.Join(" ", list);
}
}
}