public void Init() { if(!initDone) { doc = new CssXmlDocument(); doc.AddStyleElement("http://www.w3.org/2000/svg", "style"); doc.Load("http://www.sharpvectors.org/tests/css_unittest_01.svg"); cssStyleSheet = (CssStyleSheet)doc.StyleSheets[0]; colorRules = (CssStyleDeclaration)((CssStyleRule)cssStyleSheet.CssRules[4]).Style; initDone = true; } }
/// <summary> /// Converts a CSS-like string to a <see cref="IStyle"/> object. /// </summary> /// <param name="context"></param> /// <param name="culture"></param> /// <param name="value"></param> /// <returns></returns> public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { CssStyleDeclaration csd = null; // Try to parse the object as string or CssStyleDeclaration if (value is string) { // Parse the string as css style declaration csd = new CssStyleDeclaration((string)value, null, false, CssStyleSheetType.Author); } else if (value is CssStyleDeclaration) { csd = (CssStyleDeclaration)value; } if (csd != null) { // Copy properties to a new Style object IStyle style; // See what kind of style this is string styleType = csd.GetPropertyValue("display-style"); if (styleType == "label") { style = GetLabelStyle(csd); } else { style = GetVectorStyle(csd); } // Generic style properties assignment GetGeneralProperties(csd, style); return style; } // Use the base converter if the target type is unsupported return base.ConvertFrom(context, culture, value); }
/// <summary> /// Converts a <see cref="IStyle"/> object to a CSS-like string /// </summary> /// <param name="context"></param> /// <param name="culture"></param> /// <param name="value"></param> /// <param name="destinationType"></param> /// <returns></returns> public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (value is IStyle && (destinationType.Equals(typeof(CssStyleDeclaration)) || destinationType.Equals(typeof(string)))) { // Get the IStyle to convert from IStyle from = (IStyle)value; CssStyleDeclaration csd = new CssStyleDeclaration(string.Empty, null, false, CssStyleSheetType.Author); // Copy IStyle/Style properties to the CSS declaration SetGeneralProperties(from, csd); if (from is VectorStyle) { SetVectorStyleProperties(from as VectorStyle, csd); } else if (from is LabelStyle) { SetLabelStyleProperties(from as LabelStyle, csd); } // Return as CssStyleDeclaration if (destinationType.Equals(typeof(CssStyleDeclaration))) { return csd; } // Else return as string return csd.CssText; } // Use the base converter if the value was of an unsupported type return base.ConvertTo(context, culture, value, destinationType); }
public CssAbsPrimitiveLengthValue(CssPrimitiveValue cssValue, string propertyName, XmlElement element) { _cssValue = cssValue; if (cssValue.PrimitiveType == CssPrimitiveType.Ident) { // this is primarily to deal with font sizes. float absSize; switch (cssValue.GetStringValue()) { case "xx-small": absSize = 6F; break; case "x-small": absSize = 7.5F; break; case "small": absSize = 8.88F; break; case "large": absSize = 12F; break; case "x-large": absSize = 15F; break; case "xx-large": absSize = 20F; break; case "larger": case "smaller": float parSize; if (_parentElement != null) { CssStyleDeclaration csd = (CssStyleDeclaration)_ownerDocument.GetComputedStyle(_parentElement, string.Empty); CssPrimitiveValue cssPrimValue = csd.GetPropertyCssValue("font-size") as CssPrimitiveValue; // no default font-size set => use 10px if (cssPrimValue == null) { parSize = 10; } else { parSize = (float)cssPrimValue.GetFloatValue(CssPrimitiveType.Px); } } else { parSize = 10; } if (cssValue.GetStringValue() == "smaller") { absSize = parSize / 1.2F; } else { absSize = parSize * 1.2F; } break; default: absSize = 10F; break; } SetFloatValue(absSize); SetPrimitiveType(CssPrimitiveType.Px); } else { SetFloatValue(cssValue.GetFloatValue(cssValue.PrimitiveType)); SetPrimitiveType(cssValue.PrimitiveType); } _propertyName = propertyName; _element = element; }