void Populate(ICssStyleDeclaration declarations) { foreach (var declaration in declarations) { children.Add(new CssRuleViewModel(declaration)); } }
private static void GetGeneralProperties(ICssStyleDeclaration csd, IStyle style) { if (csd.GetPropertyValue("zoom-min-visible") != string.Empty) style.MinVisible = double.Parse(csd.GetPropertyValue("zoom-min-visible")); if (csd.GetPropertyValue("zoom-max-visible") != string.Empty) style.MaxVisible = double.Parse(csd.GetPropertyValue("zoom-max-visible")); }
public virtual ICssStyleDeclaration GetComputedStyle(string pseudoElt) { CssXmlDocument ownerDoc = this.OwnerDocument; if (_isImported && _importDocument != null && _importNode != null) { ownerDoc = _importDocument; } if (_cachedCSD == null) { CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(this); MediaList currentMedia = ownerDoc.Media; if (ownerDoc.UserAgentStyleSheet != null) { ownerDoc.UserAgentStyleSheet.GetStylesForElement(this, pseudoElt, currentMedia, csd); } ((StyleSheetList)ownerDoc.StyleSheets).GetStylesForElement(this, pseudoElt, csd); ((CssStyleDeclaration)Style).GetStylesForElement(csd, 0); if (ownerDoc.UserStyleSheet != null) { ownerDoc.UserStyleSheet.GetStylesForElement(this, pseudoElt, currentMedia, csd); } _cachedCSD = csd; } return(_cachedCSD); }
private void GetCssXmlValue() { if (_source == SvgLengthSource.Css) { ICssStyleDeclaration csd = _ownerElement.GetComputedStyle(string.Empty); CssPrimitiveLengthValue cssValue = csd.GetPropertyCssValue(_propertyName) as CssPrimitiveLengthValue; if (cssValue != null) { _cssLength = new CssAbsPrimitiveLengthValue(cssValue, _propertyName, _ownerElement); } else { throw new DomException(DomExceptionType.SyntaxErr, "Not a length value"); } } else { string baseVal = _ownerElement.GetAttribute(_propertyName); if (baseVal == null || baseVal.Length == 0) { baseVal = _defaultValue; } baseVal = SvgNumber.ScientificToDec(baseVal); _cssLength = new CssAbsPrimitiveLengthValue(new CssPrimitiveLengthValue(baseVal, false), _propertyName, _ownerElement); } }
static StyleRange ExtractStyle(ICssStyleDeclaration style) { var mappedStyle = new StyleRange(); string cssColor = null; try { cssColor = style?.GetPropertyValue("color"); mappedStyle.Color = ParseColor(cssColor); } catch (NullReferenceException) { } try { string fontWeight = null; fontWeight = style?.GetPropertyValue("font-weight"); mappedStyle.Bold = (!string.IsNullOrEmpty(fontWeight) && fontWeight == "bold"); } catch (NullReferenceException) { } try { string fontStyle = null; fontStyle = style?.GetPropertyValue("font-style"); mappedStyle.Italic = (!string.IsNullOrEmpty(fontStyle) && fontStyle == "italic"); } catch (NullReferenceException) { } return(mappedStyle); }
/// <summary> /// Sets the CSS property of a color if it was different from the original /// </summary> /// <param name="csd">The style to add the CSS property to</param> /// <param name="property">The property name to use</param> /// <param name="value">The set color value of the object to serialize</param> /// <param name="original">The original non-changed object to compare changes to</param> private static void SetColorStyleProperty(ICssStyleDeclaration csd, string property, Color value, Color original) { // Compare colors on their ARGB values and set it as an HTML color (hex or well-known) if (value.ToArgb() != original.ToArgb()) { SetColorToCss(value, csd, property); } }
/// <summary> /// This function allows each element to handle it's own behaviors for /// attribute changing. By default, the cached computed style is invalidated /// because most attributes refer to style properties. /// </summary> /// <param name="attribute">The attribute that is changing.</param> public virtual void HandleAttributeChange(XmlAttribute attribute) { if (attribute.NamespaceURI.Length == 0) { if (string.Equals(attribute.LocalName, SvgConstants.TagStyle, StringComparison.OrdinalIgnoreCase)) { _style = null; } } }
private static IStyle GetVectorStyle(ICssStyleDeclaration csd) { // VectorStyle object rebuild. Deserializes: // border-color Line.Color // border-width Line.Width // outline-color Outline.Color // outline-width Outline.Width // outline-style EnableOutline // background-color Fill VectorStyle vStyle = new VectorStyle(); if (csd.GetPropertyValue("border-color") != string.Empty) { vStyle.Line.Color = GetColorFromCss(csd, "border-color"); } if (csd.GetPropertyValue("border-width") != string.Empty) { vStyle.Line.Width = float.Parse(csd.GetPropertyValue("border-width")); } if (csd.GetPropertyValue("outline-color") != string.Empty) { vStyle.Outline.Color = GetColorFromCss(csd, "outline-color"); } if (csd.GetPropertyValue("outline-width") != string.Empty) { vStyle.Outline.Width = float.Parse(csd.GetPropertyValue("outline-width")); } if (csd.GetPropertyValue("outline-style") != string.Empty) { vStyle.EnableOutline = (csd.GetPropertyValue("outline-style") == "enabled" ? true : false); } if (csd.GetPropertyValue("background-color") != string.Empty) { vStyle.Fill = new SolidBrush(GetColorFromCss(csd, "background-color")); } vStyle.Line.EndCap = (LineCap)Enum.Parse(typeof(LineCap), csd.GetPropertyValue(endcapName)); if (csd.GetPropertyValue("geometry-type") != string.Empty) { vStyle.GeometryType = GetGeometryTypeFromCssString(csd); } if (csd.GetPropertyValue("symbol-shape") != string.Empty) { vStyle.Shape = (VectorStyle.ShapeType?)Enum.Parse(typeof(VectorStyle.ShapeType), csd.GetPropertyValue("symbol-shape")); } if (csd.GetPropertyValue("symbol") != string.Empty) { // From a Codepage 1251-encoded string, convert to bytes and next to a Bitmap representing the symbol byte[] bytes = Convert.FromBase64String(csd.GetPropertyValue("symbol")); vStyle.Symbol = (Bitmap)TypeDescriptor.GetConverter(typeof(Bitmap)).ConvertFrom(bytes); } return(vStyle); }
private void SanitizeStyleDeclaration(IElement element, ICssStyleDeclaration styles, string baseUrl) { var removeStyles = new List <Tuple <ICssProperty, RemoveReason> >(); var setStyles = new Dictionary <string, string>(); foreach (var style in styles) { var key = DecodeCss(style.Name); var val = DecodeCss(style.Value); if (!AllowedCssProperties.Contains(key)) { removeStyles.Add(new Tuple <ICssProperty, RemoveReason>(style, RemoveReason.NotAllowedStyle)); continue; } if (CssExpression.IsMatch(val) || DisallowCssPropertyValue.IsMatch(val)) { removeStyles.Add(new Tuple <ICssProperty, RemoveReason>(style, RemoveReason.NotAllowedValue)); continue; } var urls = CssUrl.Matches(val); if (urls.Count > 0) { if (urls.Cast <Match>().Any(m => SanitizeUrl(m.Groups[2].Value, baseUrl) == null)) { removeStyles.Add(new Tuple <ICssProperty, RemoveReason>(style, RemoveReason.NotAllowedUrlValue)); } else { var s = CssUrl.Replace(val, m => "url(" + m.Groups[1].Value + SanitizeUrl(m.Groups[2].Value, baseUrl) + m.Groups[3].Value); if (s != val) { if (key != style.Name) { removeStyles.Add(new Tuple <ICssProperty, RemoveReason>(style, RemoveReason.NotAllowedUrlValue)); } setStyles[key] = s; } } } } foreach (var style in setStyles) { styles.SetProperty(style.Key, style.Value); } foreach (var style in removeStyles) { RemoveStyle(element, styles, style.Item1, style.Item2); } }
/// <summary> /// Sets general properties of the style in the csd. /// </summary> /// <param name="from"></param> /// <param name="csd"></param> private static void SetGeneralProperties(IStyle from, ICssStyleDeclaration csd) { if (from.MinVisible > 0) { csd.SetProperty("zoom-min-visible", from.MinVisible.ToString(), string.Empty); } if (from.MaxVisible < double.MaxValue) { csd.SetProperty("zoom-max-visible", from.MaxVisible.ToString(), string.Empty); } }
private static void GetGeneralProperties(ICssStyleDeclaration csd, IStyle style) { if (csd.GetPropertyValue("zoom-min-visible") != string.Empty) { style.MinVisible = double.Parse(csd.GetPropertyValue("zoom-min-visible")); } if (csd.GetPropertyValue("zoom-max-visible") != string.Empty) { style.MaxVisible = double.Parse(csd.GetPropertyValue("zoom-max-visible")); } }
/// <summary> /// Removes a style from the document. /// </summary> /// <param name="tag">Tag the style belongs to</param> /// <param name="styles">Style rule that contains the style to be removed</param> /// <param name="style">Style to be removed</param> /// <param name="reason">Reason for removal</param> private void RemoveStyle(IElement tag, ICssStyleDeclaration styles, ICssProperty style, RemoveReason reason) { var e = new RemovingStyleEventArgs { Tag = tag, Style = style, Reason = reason }; OnRemovingStyle(e); if (!e.Cancel) { styles.RemoveProperty(style.Name); } }
/// <summary> /// This function allows each element to handle it's own behaviors for /// attribute changing. By default, the cached computed style is invalidated /// because most attributes refer to style properties. /// </summary> /// <param name="attribute">The attribute that is changing.</param> public virtual void HandleAttributeChange(XmlAttribute attribute) { if (attribute.NamespaceURI.Length == 0) { switch (attribute.LocalName) { case "style": style = null; break; } } }
public void TestStyleMultipleWithSame() { CssXmlElement elm = getElm("<a style='foo:bar; kalle:roffe;foo:newvalue' />", "", "a") as CssXmlElement; ICssStyleDeclaration csd = elm.Style; if (!csd.CssText.Equals("kalle:roffe;foo:newvalue;") && !csd.CssText.Equals("foo:newvalue;kalle:roffe;")) { Assert.Fail(); } Assert.AreEqual(2, csd.Length); Assert.AreEqual("newvalue", csd.GetPropertyValue("foo")); }
private static void SetVectorStyleProperties(VectorStyle vectorStyle, ICssStyleDeclaration csd) { // Copy VectorStyle properties to the CSS declaration. Serializes: // border-color Line.Color // border-width Line.Width // outline-color Outline.Color // outline-width Outline.Width // outline-style EnableOutline // background-color Fill // endCap Line.EndCap VectorStyle vectorDefaults = new VectorStyle(); if (vectorStyle != null) { SetColorStyleProperty(csd, "border-color", vectorStyle.Line.Color, vectorDefaults.Line.Color); if (vectorStyle.Line.Width != vectorDefaults.Line.Width) { csd.SetProperty("border-width", vectorStyle.Line.Width.ToString("F0"), string.Empty); } SetColorStyleProperty(csd, "outline-color", vectorStyle.Outline.Color, vectorDefaults.Outline.Color); if (vectorStyle.Outline.Width != vectorDefaults.Outline.Width) { csd.SetProperty("outline-width", vectorStyle.Outline.Width.ToString("F0"), string.Empty); } if (vectorStyle.EnableOutline) { csd.SetProperty("outline-style", "enabled", string.Empty); } SetBrushStyleProperty(csd, "background-color", vectorStyle.Fill, vectorDefaults.Fill); csd.SetProperty(endcapName, vectorStyle.Line.EndCap.ToString(), string.Empty); if (vectorStyle.GeometryType != null) { csd.SetProperty("geometry-type", GeometryType2CssString(vectorStyle), string.Empty); } if (vectorStyle.Shape != null) { csd.SetProperty("symbol-shape", vectorStyle.Shape.ToString(), string.Empty); } if ((vectorStyle.Symbol != null) && (vectorStyle.HasCustomSymbol)) { // Encode a Bitmap symbol as bytes that can be included in the css as string using Codepage 1251 encoding byte[] bytes = (byte[])TypeDescriptor.GetConverter(typeof(Bitmap)).ConvertTo(vectorStyle.Symbol, typeof(byte[])); csd.SetProperty("symbol", Convert.ToBase64String(bytes), string.Empty); } } }
private ICssValue getParentStyle(string propertyName) { CssXmlDocument doc = _element.OwnerDocument as CssXmlDocument; XmlElement parentNode = _element.ParentNode as XmlElement; if (doc != null && parentNode != null) { ICssStyleDeclaration parentCsd = doc.GetComputedStyle(parentNode, string.Empty); if (parentCsd == null) { return(null); } return(parentCsd.GetPropertyCssValue(propertyName)); } return(null); }
private static void SetLabelStyleProperties(LabelStyle labelStyle, ICssStyleDeclaration csd) { // Copy LabelStyle properties to the CSS declaration. Serializes: // font-family Font.Family // font-size Font.Size // font-color ForeColor // background-color BackColor // border-color Halo.Color // border-width Halo.Width // padding-horizontal Offset.X // padding-vertical Offset.Y // text-align HorizontalAlignment // vertical-align VerticalAlignment LabelStyle labelDefaults = new LabelStyle(); if (labelStyle.Font.FontFamily != labelDefaults.Font.FontFamily) { csd.SetProperty("font-family", labelStyle.Font.FontFamily.Name, string.Empty); } if (labelStyle.Font.Size != labelDefaults.Font.Size) { csd.SetProperty("font-size", labelStyle.Font.Size.ToString("F0"), string.Empty); } SetColorStyleProperty(csd, "font-color", labelStyle.ForeColor, labelDefaults.ForeColor); SetBrushStyleProperty(csd, "background-color", labelStyle.BackColor, labelDefaults.BackColor); SetColorStyleProperty(csd, "border-color", labelStyle.Halo.Color, labelDefaults.Halo.Color); if (labelStyle.Halo.Width != labelDefaults.Halo.Width) { csd.SetProperty("border-width", labelStyle.Halo.Width.ToString("F0"), string.Empty); } if (labelStyle.Offset.X != labelDefaults.Offset.X) { csd.SetProperty("padding-horizontal", labelStyle.Offset.X.ToString("F0"), string.Empty); } if (labelStyle.Offset.Y != labelDefaults.Offset.Y) { csd.SetProperty("padding-vertical", labelStyle.Offset.Y.ToString("F0"), string.Empty); } if (labelStyle.HorizontalAlignment != labelDefaults.HorizontalAlignment) { csd.SetProperty("text-align", labelStyle.HorizontalAlignment.ToString(), string.Empty); } if (labelStyle.VerticalAlignment != labelDefaults.VerticalAlignment) { csd.SetProperty("vertical-align", labelStyle.VerticalAlignment.ToString(), string.Empty); } }
public virtual void CssInvalidate() { // TODO: why is this being called during load? foreach (XmlNode child in ChildNodes) { if (child is CssXmlElement) { ((CssXmlElement)child).CssInvalidate(); } } // Kill the cache cachedCSD = null; // Notify FireCssChange(); }
/// <summary> /// Applies a CSS style ato a ITextControl including font style /// </summary> public static void Apply(ICssStyleDeclaration styleDeclaration, ITextControl control) { //first parse as IControl Apply(styleDeclaration, (IControl)control); //now for ITextControl properties control.Bold = styleDeclaration.FontWeight == "bold"; control.Italic = styleDeclaration.FontStyle == "italic"; control.Underline = styleDeclaration.TextDecoration == "underline"; AngleSharp.Css.Values.Color color = AngleSharp.Css.Values.Color.FromHex(styleDeclaration.Color); control.FontColor = new Color(color.A, color.R, color.G, color.B); control.FontFamily = styleDeclaration.FontFamily; Length lenght; if (Length.TryParse(styleDeclaration.FontSize, out lenght)) { control.FontSize = lenght.ToPixel(); } switch (styleDeclaration.TextAlign) { case "left": control.TextHorizontalAlignment = HorizontalAlignment.Left; break; case "center": control.TextHorizontalAlignment = HorizontalAlignment.Center; break; case "right": control.TextHorizontalAlignment = HorizontalAlignment.Right; break; case "justify": control.TextHorizontalAlignment = HorizontalAlignment.Fill; break; } //just grab the sabe vertical aligned parsed form the other mnethod control.TextVerticalAlignment = control.VerticalAlignment; }
private static Color GetColorFromCss(ICssStyleDeclaration csd, string propertyName) { string property = csd.GetPropertyValue(propertyName); string [] components = property.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); Color color; if (components[0].Contains("#")) { color = Color.FromArgb(int.Parse(components[1])); } else { color = Color.FromName(components[0]); } return(color); }
private static Type GetGeometryTypeFromCssString(ICssStyleDeclaration csd) { string property = csd.GetPropertyValue("geometry-type"); if (property == "LineString") { return(typeof(ILineString)); } if (property == "Polygon") { return(typeof(IPolygon)); } if (property == "Point") { return(typeof(IPoint)); } return(new VectorStyle().GeometryType); }
private static IStyle GetVectorStyle(ICssStyleDeclaration csd) { // VectorStyle object rebuild. Deserializes: // border-color Line.Color // border-width Line.Width // outline-color Outline.Color // outline-width Outline.Width // outline-style EnableOutline // background-color Fill VectorStyle vStyle = new VectorStyle(); if (csd.GetPropertyValue("border-color") != string.Empty) vStyle.Line.Color = GetColorFromCss(csd, "border-color"); if (csd.GetPropertyValue("border-width") != string.Empty) vStyle.Line.Width = float.Parse(csd.GetPropertyValue("border-width")); if (csd.GetPropertyValue("outline-color") != string.Empty) vStyle.Outline.Color = GetColorFromCss(csd, "outline-color"); if (csd.GetPropertyValue("outline-width") != string.Empty) vStyle.Outline.Width = float.Parse(csd.GetPropertyValue("outline-width")); if (csd.GetPropertyValue("outline-style") != string.Empty) vStyle.EnableOutline = (csd.GetPropertyValue("outline-style") == "enabled" ? true : false); if (csd.GetPropertyValue("background-color") != string.Empty) vStyle.Fill = new SolidBrush(GetColorFromCss(csd, "background-color")); vStyle.Line.EndCap = (LineCap)Enum.Parse(typeof(LineCap), csd.GetPropertyValue(endcapName)); if (csd.GetPropertyValue("geometry-type") != string.Empty) { vStyle.GeometryType = GetGeometryTypeFromCssString(csd); } if (csd.GetPropertyValue("symbol-shape") != string.Empty) { vStyle.Shape = (VectorStyle.ShapeType?) Enum.Parse(typeof(VectorStyle.ShapeType), csd.GetPropertyValue("symbol-shape")); } if (csd.GetPropertyValue("symbol") != string.Empty) { // From a Codepage 1251-encoded string, convert to bytes and next to a Bitmap representing the symbol byte[] bytes = Convert.FromBase64String(csd.GetPropertyValue("symbol")); vStyle.Symbol = (Bitmap)TypeDescriptor.GetConverter(typeof(Bitmap)).ConvertFrom(bytes); } return vStyle; }
/// <summary> /// 验证规则 /// </summary> /// <param name="styles"></param> /// <returns></returns> protected virtual bool Validate(ICssStyleDeclaration styles) { if (styles == null) { return(false); } var removings = new List <ICssProperty>(); foreach (ICssProperty item in styles) { if (!Validate(item)) { removings.Add(item); } } foreach (var item in removings) { styles.RemoveProperty(item.Name); } return(styles.Length > 0); }
public virtual ICssStyleDeclaration GetComputedStyle(string pseudoElt) { if (cachedCSD == null) { CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(this); MediaList currentMedia = OwnerDocument.Media; if (OwnerDocument.UserAgentStyleSheet != null) { OwnerDocument.UserAgentStyleSheet.GetStylesForElement(this, pseudoElt, currentMedia, csd); } ((StyleSheetList)OwnerDocument.StyleSheets).GetStylesForElement(this, pseudoElt, csd); ((CssStyleDeclaration)Style).GetStylesForElement(csd, 0); if (OwnerDocument.UserStyleSheet != null) { OwnerDocument.UserStyleSheet.GetStylesForElement(this, pseudoElt, currentMedia, csd); } cachedCSD = csd; } return(cachedCSD); }
/// <summary> /// Sets general properties of the style in the csd. /// </summary> /// <param name="from"></param> /// <param name="csd"></param> private static void SetGeneralProperties(IStyle from, ICssStyleDeclaration csd) { if (from.MinVisible > 0) csd.SetProperty("zoom-min-visible", from.MinVisible.ToString(), string.Empty); if (from.MaxVisible < double.MaxValue) csd.SetProperty("zoom-max-visible", from.MaxVisible.ToString(), string.Empty); }
private static void ProcessText(String text, StringBuilder sb, ICssStyleDeclaration style) { var startIndex = sb.Length; var whiteSpace = style?.GetWhiteSpace(); var textTransform = style?.GetTextTransform(); var isWhiteSpace = startIndex > 0 ? Char.IsWhiteSpace(sb[startIndex - 1]) && sb[startIndex - 1] != Symbols.NoBreakSpace : true; for (var i = 0; i < text.Length; i++) { var c = text[i]; if (Char.IsWhiteSpace(c) && c != Symbols.NoBreakSpace) { // https://drafts.csswg.org/css-text/#white-space-property switch (whiteSpace) { case "pre": case "pre-wrap": break; case "pre-line": if (c == Symbols.Space || c == Symbols.Tab) { if (isWhiteSpace) { continue; } c = Symbols.Space; } break; case "nowrap": case "normal": default: if (isWhiteSpace) { continue; } c = Symbols.Space; break; } isWhiteSpace = true; } else { // https://drafts.csswg.org/css-text/#propdef-text-transform switch (textTransform) { case "uppercase": c = Char.ToUpperInvariant(c); break; case "lowercase": c = Char.ToLowerInvariant(c); break; case "capitalize": if (isWhiteSpace) { c = Char.ToUpperInvariant(c); } break; case "none": default: break; } isWhiteSpace = false; } sb.Append(c); } if (isWhiteSpace) // ended with whitespace { for (var offset = sb.Length - 1; offset >= startIndex; offset--) { var c = sb[offset]; if (!Char.IsWhiteSpace(c) || c == Symbols.NoBreakSpace) { sb.Remove(offset + 1, sb.Length - 1 - offset); break; } } } }
private static Color GetColorFromCss(ICssStyleDeclaration csd, string propertyName) { string property = csd.GetPropertyValue(propertyName); string [] components = property.Split(new string[] {" "}, StringSplitOptions.RemoveEmptyEntries); Color color; if (components[0].Contains("#")) { color = Color.FromArgb(int.Parse(components[1])); } else { color = Color.FromName(components[0]); } return color; }
private static void SetLabelStyleProperties(LabelStyle labelStyle, ICssStyleDeclaration csd) { // Copy LabelStyle properties to the CSS declaration. Serializes: // font-family Font.Family // font-size Font.Size // font-color ForeColor // background-color BackColor // border-color Halo.Color // border-width Halo.Width // padding-horizontal Offset.X // padding-vertical Offset.Y // text-align HorizontalAlignment // vertical-align VerticalAlignment LabelStyle labelDefaults = new LabelStyle(); if (labelStyle.Font.FontFamily != labelDefaults.Font.FontFamily) csd.SetProperty("font-family", labelStyle.Font.FontFamily.Name, string.Empty); if (labelStyle.Font.Size != labelDefaults.Font.Size) csd.SetProperty("font-size", labelStyle.Font.Size.ToString("F0"), string.Empty); SetColorStyleProperty(csd, "font-color", labelStyle.ForeColor, labelDefaults.ForeColor); SetBrushStyleProperty(csd, "background-color", labelStyle.BackColor, labelDefaults.BackColor); SetColorStyleProperty(csd, "border-color", labelStyle.Halo.Color, labelDefaults.Halo.Color); if (labelStyle.Halo.Width != labelDefaults.Halo.Width) csd.SetProperty("border-width", labelStyle.Halo.Width.ToString("F0"), string.Empty); if (labelStyle.Offset.X != labelDefaults.Offset.X) csd.SetProperty("padding-horizontal", labelStyle.Offset.X.ToString("F0"), string.Empty); if (labelStyle.Offset.Y != labelDefaults.Offset.Y) csd.SetProperty("padding-vertical", labelStyle.Offset.Y.ToString("F0"), string.Empty); if (labelStyle.HorizontalAlignment != labelDefaults.HorizontalAlignment) csd.SetProperty("text-align", labelStyle.HorizontalAlignment.ToString(), string.Empty); if (labelStyle.VerticalAlignment != labelDefaults.VerticalAlignment) csd.SetProperty("vertical-align", labelStyle.VerticalAlignment.ToString(), string.Empty); }
private static void SetVectorStyleProperties(VectorStyle vectorStyle, ICssStyleDeclaration csd) { // Copy VectorStyle properties to the CSS declaration. Serializes: // border-color Line.Color // border-width Line.Width // outline-color Outline.Color // outline-width Outline.Width // outline-style EnableOutline // background-color Fill // endCap Line.EndCap VectorStyle vectorDefaults = new VectorStyle(); if (vectorStyle != null) { SetColorStyleProperty(csd, "border-color", vectorStyle.Line.Color, vectorDefaults.Line.Color); if (vectorStyle.Line.Width != vectorDefaults.Line.Width) csd.SetProperty("border-width", vectorStyle.Line.Width.ToString("F0"), string.Empty); SetColorStyleProperty(csd, "outline-color", vectorStyle.Outline.Color, vectorDefaults.Outline.Color); if (vectorStyle.Outline.Width != vectorDefaults.Outline.Width) csd.SetProperty("outline-width", vectorStyle.Outline.Width.ToString("F0"), string.Empty); if (vectorStyle.EnableOutline) csd.SetProperty("outline-style", "enabled", string.Empty); SetBrushStyleProperty(csd, "background-color", vectorStyle.Fill, vectorDefaults.Fill); csd.SetProperty(endcapName, vectorStyle.Line.EndCap.ToString(), string.Empty); if (vectorStyle.GeometryType != null) { csd.SetProperty("geometry-type", GeometryType2CssString(vectorStyle), string.Empty); } if (vectorStyle.Shape != null) { csd.SetProperty("symbol-shape", vectorStyle.Shape.ToString(), string.Empty); } if ((vectorStyle.Symbol != null) && (vectorStyle.HasCustomSymbol)) { // Encode a Bitmap symbol as bytes that can be included in the css as string using Codepage 1251 encoding byte[] bytes = (byte[])TypeDescriptor.GetConverter(typeof(Bitmap)).ConvertTo(vectorStyle.Symbol, typeof(byte[])); csd.SetProperty("symbol", Convert.ToBase64String(bytes), string.Empty); } } }
/// <summary> /// Applies a CSS style to a IControl /// <para xml:lang="es"> /// Aplica un estilo CSS a un Control. /// </para> /// </summary> public static void Apply(ICssStyleDeclaration style, IControl control) { AngleSharp.Css.Values.Color color; Length lenght; bool parsed; //background and border colors color = AngleSharp.Css.Values.Color.FromHex(style.BackgroundColor); control.BackgroundColor = new Color(color.A, color.R, color.G, color.B); color = AngleSharp.Css.Values.Color.FromHex(style.BorderColor); control.BorderColor = new Color(color.A, color.R, color.G, color.B); //horizontal alignment http://www.w3schools.com/css/css_align.asp //horizontal alignment using float switch (style.Float) { case "left": control.HorizontalAlignment = HorizontalAlignment.Left; break; case "right": control.HorizontalAlignment = HorizontalAlignment.Right; break; } //horizontal alignment using position parsed = Length.TryParse(style.Left, out lenght); if (parsed && style.Position == "absolute" && lenght.ToPixel() == 0) { control.HorizontalAlignment = HorizontalAlignment.Left; } parsed = Length.TryParse(style.Right, out lenght); if (parsed && style.Position == "absolute" && lenght.ToPixel() == 0) { control.HorizontalAlignment = HorizontalAlignment.Right; } //horizontal alignment using margin if (style.Margin == "auto" || (style.MarginLeft == "auto" && style.MarginRight == "auto")) { control.HorizontalAlignment = HorizontalAlignment.Center; } //horizontal alignment using text align, online for inline elements if (style.Display == "inline") { switch (style.TextAlign) { case "left": control.HorizontalAlignment = HorizontalAlignment.Left; break; case "center": control.HorizontalAlignment = HorizontalAlignment.Center; break; case "right": control.HorizontalAlignment = HorizontalAlignment.Right; break; case "justify": control.HorizontalAlignment = HorizontalAlignment.Fill; break; } } //vertical alignment switch (style.VerticalAlign) { case "top": control.VerticalAlignment = VerticalAlignment.Top; break; case "middle": control.VerticalAlignment = VerticalAlignment.Center; break; case "bottom": control.VerticalAlignment = VerticalAlignment.Bottom; break; } //height and width if (Length.TryParse(style.Height, out lenght)) control.Height = lenght.ToPixel(); if (Length.TryParse(style.Width, out lenght)) control.Width = lenght.ToPixel(); //border Thickness borderWidth = new Thickness(); Length.TryParse(style.BorderTopWidth, out lenght); borderWidth.Top = lenght.ToPixel(); Length.TryParse(style.BorderRightWidth, out lenght); borderWidth.Right = lenght.ToPixel(); Length.TryParse(style.BorderBottomWidth, out lenght); borderWidth.Bottom = lenght.ToPixel(); Length.TryParse(style.BorderLeftWidth, out lenght); borderWidth.Left = lenght.ToPixel(); control.BorderWidth = borderWidth; //margin Thickness margin = new Thickness(); Length.TryParse(style.MarginTop, out lenght); margin.Top = lenght.ToPixel(); Length.TryParse(style.MarginRight, out lenght); margin.Right = lenght.ToPixel(); Length.TryParse(style.MarginBottom, out lenght); margin.Bottom = lenght.ToPixel(); Length.TryParse(style.MarginLeft, out lenght); margin.Left = lenght.ToPixel(); control.Margin = margin; //visibility control.Visible = style.Visibility != "none" && style.Visibility != "hidden"; }
/// <summary> /// This function allows each element to handle it's own behaviors for /// attribute changing. By default, the cached computed style is invalidated /// because most attributes refer to style properties. /// </summary> /// <param name="attribute">The attribute that is changing.</param> public virtual void HandleAttributeChange(XmlAttribute attribute) { if(attribute.NamespaceURI.Length == 0) { switch (attribute.LocalName) { case "style": style = null; break; } } }
public virtual ICssStyleDeclaration GetComputedStyle(string pseudoElt) { if(cachedCSD == null) { CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(this); MediaList currentMedia = OwnerDocument.Media; if(OwnerDocument.UserAgentStyleSheet != null) { OwnerDocument.UserAgentStyleSheet.GetStylesForElement(this, pseudoElt, currentMedia, csd); } ((StyleSheetList)OwnerDocument.StyleSheets).GetStylesForElement(this, pseudoElt, csd); ((CssStyleDeclaration)Style).GetStylesForElement(csd, 0); if(OwnerDocument.UserStyleSheet != null) { OwnerDocument.UserStyleSheet.GetStylesForElement(this, pseudoElt, currentMedia, csd); } cachedCSD = csd; } return cachedCSD; }
public virtual void CssInvalidate() { // TODO: why is this being called during load? foreach(XmlNode child in ChildNodes) { if (child is CssXmlElement) ((CssXmlElement)child).CssInvalidate(); } // Kill the cache cachedCSD = null; // Notify FireCssChange(); }
/// <summary> /// Sets the CSS property of a brush if it was different from the original /// </summary> /// <param name="csd">The style to add the CSS property to</param> /// <param name="property">The property name to use</param> /// <param name="value">The set brush value of the object to serialize</param> /// <param name="original">The original non-changed object to compare changes to</param> private static void SetBrushStyleProperty(ICssStyleDeclaration csd, string property, Brush value, Brush original) { // Compare brushes based on the color values and output the brush' color value as HTML color SetColorStyleProperty(csd, property, ((SolidBrush) value).Color, ((SolidBrush) original).Color); }
/// <summary> /// Sets the CSS property of a color if it was different from the original /// </summary> /// <param name="csd">The style to add the CSS property to</param> /// <param name="property">The property name to use</param> /// <param name="value">The set color value of the object to serialize</param> /// <param name="original">The original non-changed object to compare changes to</param> private static void SetColorStyleProperty(ICssStyleDeclaration csd, string property, Color value, Color original) { // Compare colors on their ARGB values and set it as an HTML color (hex or well-known) if (value.ToArgb() != original.ToArgb()) SetColorToCss(value, csd, property); }
/// <summary> /// Extends the given bag with the set properties of the specified /// styling declaration. /// </summary> /// <param name="bag">The bag to modify.</param> /// <param name="styling">The styling properties to use.</param> /// <param name="priority">Sets the priority of the new properties.</param> public static void ExtendWith(this CssPropertyBag bag, ICssStyleDeclaration styling, Priority priority) { foreach (var property in styling) bag.TryUpdate(property, priority); }
private static Type GetGeometryTypeFromCssString(ICssStyleDeclaration csd) { string property = csd.GetPropertyValue("geometry-type"); if (property == "LineString") { return typeof(ILineString); } if (property == "Polygon") { return typeof(IPolygon); } if (property == "Point") { return typeof(IPoint); } return new VectorStyle().GeometryType; }
/// <summary> /// Applies a CSS style ato a ITextControl including font style /// <para xml:lang="es"> /// Aplica un estilo CSS a un control de texto incluyendo el estilo del texto. /// </para> /// </summary> public static void Apply(ICssStyleDeclaration style, ITextControl control) { //first parse as IControl Apply(style, (IControl) control); //now for ITextControl properties control.Bold = style.FontWeight == "bold"; control.Italic = style.FontStyle == "italic"; control.Underline = style.TextDecoration == "underline"; AngleSharp.Css.Values.Color color = AngleSharp.Css.Values.Color.FromHex(style.Color); control.FontColor = new Color(color.A, color.R, color.G, color.B); control.FontFamily = style.FontFamily; Length lenght; if (Length.TryParse(style.FontSize, out lenght)) { control.FontSize = lenght.ToPixel(); } switch (style.TextAlign) { case "left": control.TextHorizontalAlignment = HorizontalAlignment.Left; break; case "center": control.TextHorizontalAlignment = HorizontalAlignment.Center; break; case "right": control.TextHorizontalAlignment = HorizontalAlignment.Right; break; case "justify": control.TextHorizontalAlignment = HorizontalAlignment.Fill; break; } //just grab the sabe vertical aligned parsed form the other mnethod control.TextVerticalAlignment = control.VerticalAlignment; }
/// <summary> /// Remove a style from the document. /// </summary> /// <param name="tag">tag where the style belongs</param> /// <param name="styles">collection where the style to belongs</param> /// <param name="style">to be removed</param> /// <param name="reason">reason why to be removed</param> private void RemoveStyle(IElement tag, ICssStyleDeclaration styles, ICssProperty style, RemoveReason reason) { var e = new RemovingStyleEventArgs { Tag = tag, Style = style, Reason = reason }; OnRemovingStyle(e); if (!e.Cancel) styles.RemoveProperty(style.Name); }
/// <summary> /// Applies a CSS style to a IControl /// <para xml:lang="es"> /// Aplica un estilo CSS a un Control. /// </para> /// </summary> public static void Apply(ICssStyleDeclaration style, IControl control) { AngleSharp.Css.Values.Color color; Length lenght; bool parsed; //background and border colors color = AngleSharp.Css.Values.Color.FromHex(style.BackgroundColor); control.BackgroundColor = new Color(color.A, color.R, color.G, color.B); color = AngleSharp.Css.Values.Color.FromHex(style.BorderColor); control.BorderColor = new Color(color.A, color.R, color.G, color.B); //horizontal alignment http://www.w3schools.com/css/css_align.asp //horizontal alignment using float switch (style.Float) { case "left": control.HorizontalAlignment = HorizontalAlignment.Left; break; case "right": control.HorizontalAlignment = HorizontalAlignment.Right; break; } //horizontal alignment using position parsed = Length.TryParse(style.Left, out lenght); if (parsed && style.Position == "absolute" && lenght.ToPixel() == 0) { control.HorizontalAlignment = HorizontalAlignment.Left; } parsed = Length.TryParse(style.Right, out lenght); if (parsed && style.Position == "absolute" && lenght.ToPixel() == 0) { control.HorizontalAlignment = HorizontalAlignment.Right; } //horizontal alignment using margin if (style.Margin == "auto" || (style.MarginLeft == "auto" && style.MarginRight == "auto")) { control.HorizontalAlignment = HorizontalAlignment.Center; } //horizontal alignment using text align, online for inline elements if (style.Display == "inline") { switch (style.TextAlign) { case "left": control.HorizontalAlignment = HorizontalAlignment.Left; break; case "center": control.HorizontalAlignment = HorizontalAlignment.Center; break; case "right": control.HorizontalAlignment = HorizontalAlignment.Right; break; case "justify": control.HorizontalAlignment = HorizontalAlignment.Fill; break; } } //vertical alignment switch (style.VerticalAlign) { case "top": control.VerticalAlignment = VerticalAlignment.Top; break; case "middle": control.VerticalAlignment = VerticalAlignment.Center; break; case "bottom": control.VerticalAlignment = VerticalAlignment.Bottom; break; } //height and width if (Length.TryParse(style.Height, out lenght)) { control.Height = lenght.ToPixel(); } if (Length.TryParse(style.Width, out lenght)) { control.Width = lenght.ToPixel(); } //border Thickness borderWidth = new Thickness(); Length.TryParse(style.BorderTopWidth, out lenght); borderWidth.Top = lenght.ToPixel(); Length.TryParse(style.BorderRightWidth, out lenght); borderWidth.Right = lenght.ToPixel(); Length.TryParse(style.BorderBottomWidth, out lenght); borderWidth.Bottom = lenght.ToPixel(); Length.TryParse(style.BorderLeftWidth, out lenght); borderWidth.Left = lenght.ToPixel(); control.BorderWidth = borderWidth; //margin Thickness margin = new Thickness(); Length.TryParse(style.MarginTop, out lenght); margin.Top = lenght.ToPixel(); Length.TryParse(style.MarginRight, out lenght); margin.Right = lenght.ToPixel(); Length.TryParse(style.MarginBottom, out lenght); margin.Bottom = lenght.ToPixel(); Length.TryParse(style.MarginLeft, out lenght); margin.Left = lenght.ToPixel(); control.Margin = margin; //visibility control.Visible = style.Visibility != "none" && style.Visibility != "hidden"; }
void Populate(ICssStyleDeclaration declarations) { foreach (var declaration in declarations) children.Add(new CssRuleViewModel(declaration)); }
private ElementRenderNode RenderElement(IElement reference, StyleCollection collection, ICssStyleDeclaration parent = null) { var style = collection.ComputeCascadedStyle(reference, parent); var children = new List <IRenderNode>(); foreach (var child in reference.ChildNodes) { if (child is IText text) { children.Add(RenderText(text, collection)); } else if (child is IElement element) { children.Add(RenderElement(element, collection, style)); } } return(new ElementRenderNode { Ref = reference, SpecifiedStyle = style, ComputedStyle = style.Compute(_device), Children = children, }); }
/// <summary> /// Stores the color in a string using the name (for support known colors) and the /// ARGB to support the alpha channel which is not supported by ColorTranslator. /// </summary> /// <param name="color"></param> /// <param name="csd"></param> /// <param name="propertyName"></param> private static void SetColorToCss(Color color, ICssStyleDeclaration csd, string propertyName) { string colorString = string.Format("{0} {1}", color.IsKnownColor ? color.Name : "#", color.ToArgb()); csd.SetProperty(propertyName, colorString, string.Empty); }
private IStyle GetLabelStyle(ICssStyleDeclaration csd) { // LabelStyle object rebuild. Deserializes: // font-family Font.Family // font-size Font.Size // font-color ForeColor // background-color BackColor // border-color Halo.Color // border-width Halo.Width // padding-horizontal Offset.X // padding-vertical Offset.Y // text-align HorizontalAlignment // vertical-align VerticalAlignment LabelStyle lStyle =new LabelStyle(); string fontFamily = lStyle.Font.FontFamily.Name; float fontSize = lStyle.Font.Size; if (csd.GetPropertyValue("font-family") != string.Empty) fontFamily = csd.GetPropertyValue("font-family"); if (csd.GetPropertyValue("font-size") != string.Empty) fontSize = float.Parse(csd.GetPropertyValue("font-size")); lStyle.Font = new Font(fontFamily, fontSize); if (csd.GetPropertyValue("font-color") != string.Empty) lStyle.ForeColor = GetColorFromCss(csd, "font-color"); if (csd.GetPropertyValue("background-color") != string.Empty) lStyle.BackColor = new SolidBrush(GetColorFromCss(csd, "back-color")); Color haloColor = lStyle.Halo.Color; float haloWidth = lStyle.Halo.Width; if (csd.GetPropertyValue("border-color") != string.Empty) haloColor = GetColorFromCss(csd, "border-color"); if (csd.GetPropertyValue("border-width") != string.Empty) haloWidth = float.Parse(csd.GetPropertyValue("border-width")); lStyle.Halo = new Pen(haloColor, haloWidth); float offsetX = lStyle.Offset.X; float offsetY = lStyle.Offset.Y; if (csd.GetPropertyValue("padding-horizontal") != null) offsetX = float.Parse(csd.GetPropertyValue("padding-horizontal")); if (csd.GetPropertyValue("padding-vertical") != null) offsetY = float.Parse(csd.GetPropertyValue("padding-vertical")); lStyle.Offset = new PointF(offsetX, offsetY); if (csd.GetPropertyValue("text-align") != null) lStyle.HorizontalAlignment = (LabelStyle.HorizontalAlignmentEnum)Enum.Parse(typeof(LabelStyle.HorizontalAlignmentEnum), csd.GetPropertyValue("text-align")); if (csd.GetPropertyValue("vertical-align") != null) lStyle.VerticalAlignment = (LabelStyle.VerticalAlignmentEnum)Enum.Parse(typeof(LabelStyle.VerticalAlignmentEnum), csd.GetPropertyValue("vertical-align")); return lStyle; }
private static void ItcInCssBox(ICssStyleDeclaration elementStyle, ICssStyleDeclaration parentStyle, INode node, StringBuilder sb, Dictionary <Int32, Int32> requiredLineBreakCounts) { var elementHidden = new Nullable <Boolean>(); if (elementStyle != null) { if (!String.IsNullOrEmpty(elementStyle.GetDisplay())) { elementHidden = elementStyle.GetDisplay() == "none"; } if (!String.IsNullOrEmpty(elementStyle.GetVisibility()) && elementHidden != true) { elementHidden = elementStyle.GetVisibility() != "visible"; } } if (!elementHidden.HasValue) { elementHidden = (node as IHtmlElement)?.IsHidden ?? false; } if (!elementHidden.Value) { var isBlockLevel = new Nullable <Boolean>(); var startIndex = sb.Length; foreach (var child in node.ChildNodes) { InnerTextCollection(child, sb, requiredLineBreakCounts, elementStyle); } if (node is IText) { var textElement = (IText)node; ProcessText(textElement.Data, sb, parentStyle); } else if (node is IHtmlBreakRowElement) { sb.Append(Symbols.LineFeed); } else if ((node is IHtmlTableCellElement && String.IsNullOrEmpty(elementStyle.GetDisplay())) || elementStyle.GetDisplay() == "table-cell") { var nextSibling = node.NextSibling as IElement; if (nextSibling != null) { var nextSiblingCss = nextSibling.ComputeCurrentStyle(); if (nextSibling is IHtmlTableCellElement && String.IsNullOrEmpty(nextSiblingCss.GetDisplay()) || nextSiblingCss.GetDisplay() == "table-cell") { sb.Append(Symbols.Tab); } } } else if ((node is IHtmlTableRowElement && String.IsNullOrEmpty(elementStyle.GetDisplay())) || elementStyle.GetDisplay() == "table-row") { var nextSibling = node.NextSibling as IElement; if (nextSibling != null) { var nextSiblingCss = nextSibling.ComputeCurrentStyle(); if (nextSibling is IHtmlTableRowElement && String.IsNullOrEmpty(nextSiblingCss.GetDisplay()) || nextSiblingCss.GetDisplay() == "table-row") { sb.Append(Symbols.LineFeed); } } } else if (node is IHtmlParagraphElement) { var startIndexCount = 0; requiredLineBreakCounts.TryGetValue(startIndex, out startIndexCount); if (startIndexCount < 2) { requiredLineBreakCounts[startIndex] = 2; } var endIndexCount = 0; requiredLineBreakCounts.TryGetValue(sb.Length, out endIndexCount); if (endIndexCount < 2) { requiredLineBreakCounts[sb.Length] = 2; } } if (elementStyle != null) { if (IsBlockLevelDisplay(elementStyle.GetDisplay())) { isBlockLevel = true; } } if (!isBlockLevel.HasValue) { isBlockLevel = IsBlockLevel(node); } if (isBlockLevel.Value) { var startIndexCount = 0; requiredLineBreakCounts.TryGetValue(startIndex, out startIndexCount); if (startIndexCount < 1) { requiredLineBreakCounts[startIndex] = 1; } var endIndexCount = 0; requiredLineBreakCounts.TryGetValue(sb.Length, out endIndexCount); if (endIndexCount < 1) { requiredLineBreakCounts[sb.Length] = 1; } } } }
private static void InnerTextCollection(INode node, StringBuilder sb, Dictionary <Int32, Int32> requiredLineBreakCounts, ICssStyleDeclaration parentStyle) { if (HasCssBox(node)) { var element = node as IElement; var elementCss = element?.ComputeCurrentStyle(); ItcInCssBox(elementCss, parentStyle, node, sb, requiredLineBreakCounts); } }
/// <summary> /// Sets the CSS property of a brush if it was different from the original /// </summary> /// <param name="csd">The style to add the CSS property to</param> /// <param name="property">The property name to use</param> /// <param name="value">The set brush value of the object to serialize</param> /// <param name="original">The original non-changed object to compare changes to</param> private static void SetBrushStyleProperty(ICssStyleDeclaration csd, string property, Brush value, Brush original) { // Compare brushes based on the color values and output the brush' color value as HTML color SetColorStyleProperty(csd, property, ((SolidBrush)value).Color, ((SolidBrush)original).Color); }
public virtual ICssValue GetComputedCssValue(string propertyName, string pseudoElt) { ICssStyleDeclaration csd = GetComputedStyle(pseudoElt); return(csd.GetPropertyCssValue(propertyName)); }
/// <summary> /// Stores the color in a string using the name (for support known colors) and the /// ARGB to support the alpha channel which is not supported by ColorTranslator. /// </summary> /// <param name="color"></param> /// <param name="csd"></param> /// <param name="propertyName"></param> private static void SetColorToCss(Color color, ICssStyleDeclaration csd, string propertyName) { string colorString = string.Format("{0} {1}", color.IsKnownColor ? color.Name : "#", color.ToArgb()); csd.SetProperty(propertyName, colorString, string.Empty); }