CssFontFaceRule
Represents the @font-face rule.
using AngleSharp.Css;
using AngleSharp.Extensions;
using System;
using System.Collections.Generic;
namespace AngleSharp.Dom.Css
{
internal sealed class CssFontFaceRule : CssRule, ICssFontFaceRule, ICssRule, IPropertyCreator
{
private static Dictionary<string, Func<CssStyleDeclaration, CssProperty>> _creators;
private readonly CssStyleDeclaration _style;
public CssStyleDeclaration Style => _style;
string ICssFontFaceRule.Family {
get {
return _style.GetPropertyValue(PropertyNames.FontFamily);
}
set {
_style.SetProperty(PropertyNames.FontFamily, value, null);
}
}
string ICssFontFaceRule.Source {
get {
return _style.GetPropertyValue(PropertyNames.Src);
}
set {
_style.SetProperty(PropertyNames.Src, value, null);
}
}
string ICssFontFaceRule.Style {
get {
return _style.GetPropertyValue(PropertyNames.FontStyle);
}
set {
_style.SetProperty(PropertyNames.FontStyle, value, null);
}
}
string ICssFontFaceRule.Weight {
get {
return _style.GetPropertyValue(PropertyNames.FontWeight);
}
set {
_style.SetProperty(PropertyNames.FontWeight, value, null);
}
}
string ICssFontFaceRule.Stretch {
get {
return _style.GetPropertyValue(PropertyNames.FontStretch);
}
set {
_style.SetProperty(PropertyNames.FontStretch, value, null);
}
}
string ICssFontFaceRule.Range {
get {
return _style.GetPropertyValue(PropertyNames.UnicodeRange);
}
set {
_style.SetProperty(PropertyNames.UnicodeRange, value, null);
}
}
string ICssFontFaceRule.Variant {
get {
return _style.GetPropertyValue(PropertyNames.FontVariant);
}
set {
_style.SetProperty(PropertyNames.FontVariant, value, null);
}
}
string ICssFontFaceRule.Features {
get {
return _style.GetPropertyValue(PropertyNames.FontFeatureSettings);
}
set {
_style.SetProperty(PropertyNames.FontFeatureSettings, value, null);
}
}
static CssFontFaceRule()
{
_creators = new Dictionary<string, Func<CssStyleDeclaration, CssProperty>>(StringComparer.OrdinalIgnoreCase);
_creators.Add(PropertyNames.FontFamily, (CssStyleDeclaration style) => new CssFontFamilyProperty(style));
_creators.Add(PropertyNames.FontStyle, (CssStyleDeclaration style) => new CssFontStyleProperty(style));
_creators.Add(PropertyNames.FontVariant, (CssStyleDeclaration style) => new CssFontVariantProperty(style));
_creators.Add(PropertyNames.FontWeight, (CssStyleDeclaration style) => new CssFontWeightProperty(style));
_creators.Add(PropertyNames.FontStretch, (CssStyleDeclaration style) => new CssFontStretchProperty(style));
_creators.Add(PropertyNames.UnicodeRange, (CssStyleDeclaration style) => new CssUnicodeRangeProperty(style));
_creators.Add(PropertyNames.Src, (CssStyleDeclaration style) => new CssSrcProperty(style));
}
internal CssFontFaceRule()
: base(CssRuleType.FontFace)
{
_style = new CssStyleDeclaration(this);
}
protected override void ReplaceWith(ICssRule rule)
{
CssFontFaceRule cssFontFaceRule = (CssFontFaceRule)rule;
_style.Clear();
_style.SetDeclarations(cssFontFaceRule._style);
}
protected override string ToCss()
{
return "@font-face " + _style.ToCssBlock();
}
CssProperty IPropertyCreator.Create(string name, CssStyleDeclaration style)
{
if (_creators.TryGetValue(name, out Func<CssStyleDeclaration, CssProperty> value))
return value(style);
return null;
}
}
}