CssFontFaceRule
Represents the @font-face rule.
using AngleSharp.Css;
using AngleSharp.Parser.Css;
using System.Linq;
namespace AngleSharp.Dom.Css
{
internal sealed class CssFontFaceRule : CssRule, ICssFontFaceRule, ICssRule, IStyleFormattable
{
private readonly CssProperty[] _declarations;
string ICssFontFaceRule.Family {
get {
return GetValue(PropertyNames.FontFamily);
}
set {
SetValue(PropertyNames.FontFamily, value);
}
}
string ICssFontFaceRule.Source {
get {
return GetValue(PropertyNames.Src);
}
set {
SetValue(PropertyNames.Src, value);
}
}
string ICssFontFaceRule.Style {
get {
return GetValue(PropertyNames.FontStyle);
}
set {
SetValue(PropertyNames.FontStyle, value);
}
}
string ICssFontFaceRule.Weight {
get {
return GetValue(PropertyNames.FontWeight);
}
set {
SetValue(PropertyNames.FontWeight, value);
}
}
string ICssFontFaceRule.Stretch {
get {
return GetValue(PropertyNames.FontStretch);
}
set {
SetValue(PropertyNames.FontStretch, value);
}
}
string ICssFontFaceRule.Range {
get {
return GetValue(PropertyNames.UnicodeRange);
}
set {
SetValue(PropertyNames.UnicodeRange, value);
}
}
string ICssFontFaceRule.Variant {
get {
return GetValue(PropertyNames.FontVariant);
}
set {
SetValue(PropertyNames.FontVariant, value);
}
}
string ICssFontFaceRule.Features {
get {
return string.Empty;
}
set {
}
}
internal CssFontFaceRule(CssParser parser)
: base(CssRuleType.FontFace, parser)
{
_declarations = new CssProperty[7] {
new CssFontFamilyProperty(),
new CssSrcProperty(),
new CssFontStyleProperty(),
new CssFontWeightProperty(),
new CssFontStretchProperty(),
new CssUnicodeRangeProperty(),
new CssFontVariantProperty()
};
}
internal void SetProperty(CssProperty property)
{
int num = 0;
while (true) {
if (num >= _declarations.Length)
return;
if (_declarations[num].Name == property.Name)
break;
num++;
}
_declarations[num] = property;
}
protected override void ReplaceWith(ICssRule rule)
{
CssFontFaceRule cssFontFaceRule = (CssFontFaceRule)rule;
for (int i = 0; i < _declarations.Length; i++) {
_declarations[i] = cssFontFaceRule._declarations[i];
}
}
public override string ToCss(IStyleFormatter formatter)
{
string rules = formatter.Block(from m in _declarations
where m.HasValue
select m);
return formatter.Rule("@font-face", null, rules);
}
private string GetValue(string propertyName)
{
CssProperty[] declarations = _declarations;
foreach (CssProperty cssProperty in declarations) {
if (cssProperty.HasValue && cssProperty.Name == propertyName)
return cssProperty.Value;
}
return string.Empty;
}
private void SetValue(string propertyName, string valueText)
{
CssProperty[] declarations = _declarations;
int num = 0;
CssProperty cssProperty;
while (true) {
if (num >= declarations.Length)
return;
cssProperty = declarations[num];
if (cssProperty.Name == propertyName)
break;
num++;
}
CssValue newValue = base.Parser.ParseValue(valueText);
cssProperty.TrySetValue(newValue);
}
}
}