public override void Visit(DrawingGroup group, SvgAElement element, WpfDrawingContext context, float opacity) { _isAggregated = false; if (group == null || element == null || context == null) { return; } AddExtraLinkInformation(group, element); //string linkId = element.GetAttribute("id"); string linkId = GetElementName(element); if (String.IsNullOrEmpty(linkId)) { return; } SvgLink.SetKey(group, linkId); if (_dicLinks.ContainsKey(linkId)) { _isAggregated = _dicLinks[linkId]; return; } string linkAction = element.GetAttribute("onclick"); if (String.IsNullOrEmpty(linkAction)) { linkAction = element.GetAttribute("onmouseover"); if (!String.IsNullOrEmpty(linkAction) && linkAction.StartsWith("parent.svgMouseOverName", StringComparison.OrdinalIgnoreCase)) { SvgLink.SetAction(group, SvgLinkAction.LinkTooltip); } else { SvgLink.SetAction(group, SvgLinkAction.LinkNone); } } else { if (linkAction.StartsWith("parent.svgClick", StringComparison.OrdinalIgnoreCase)) { SvgLink.SetAction(group, SvgLinkAction.LinkPage); } else if (linkAction.StartsWith("parent.svgOpenHtml", StringComparison.OrdinalIgnoreCase)) { SvgLink.SetAction(group, SvgLinkAction.LinkHtml); } else { SvgLink.SetAction(group, SvgLinkAction.LinkNone); } } if (!String.IsNullOrEmpty(linkAction)) { if (linkAction.IndexOf("'Top'") > 0) { SvgLink.SetLocation(group, "Top"); } else if (linkAction.IndexOf("'Bottom'") > 0) { SvgLink.SetLocation(group, "Bottom"); } else { SvgLink.SetLocation(group, "Top"); } } this.AggregateChildren(element, context, opacity); if (_isAggregated) { Geometry drawGeometry = null; if (_aggregatedGeom.Count == 1) { drawGeometry = _aggregatedGeom[0]; } else { GeometryGroup geomGroup = new GeometryGroup(); geomGroup.FillRule = FillRule.Nonzero; for (int i = 0; i < _aggregatedGeom.Count; i++) { geomGroup.Children.Add(_aggregatedGeom[i]); } drawGeometry = geomGroup; } WpfSvgPaint fillPaint = new WpfSvgPaint(context, _aggregatedFill, "fill"); Brush brush = fillPaint.GetBrush(false); brush.SetValue(FrameworkElement.NameProperty, linkId + "_Brush"); GeometryDrawing drawing = new GeometryDrawing(brush, null, drawGeometry); group.Children.Add(drawing); } _dicLinks.Add(linkId, _isAggregated); }
private void AggregateChildren(SvgAElement aElement, WpfDrawingContext context, float opacity) { _isAggregated = false; if (aElement == null || aElement.ChildNodes == null) { return; } string aggregatedFill = aElement.GetAttribute("fill"); bool isFillFound = !String.IsNullOrEmpty(aggregatedFill); SvgStyleableElement paintElement = null; if (isFillFound) { paintElement = aElement; } XmlNode targetNode = aElement; // Check if the children of the link are wrapped in a Group Element... if (aElement.ChildNodes.Count == 1) { SvgGElement groupElement = aElement.ChildNodes[0] as SvgGElement; if (groupElement != null) { targetNode = groupElement; } } WpfDrawingSettings settings = context.Settings; GeometryCollection geomColl = new GeometryCollection(); foreach (XmlNode node in targetNode.ChildNodes) { if (node.NodeType != XmlNodeType.Element) { continue; } // Handle a case where the clip element has "use" element as a child... if (String.Equals(node.LocalName, "use")) { SvgUseElement useElement = (SvgUseElement)node; XmlElement refEl = useElement.ReferencedElement; if (refEl != null) { XmlElement refElParent = (XmlElement)refEl.ParentNode; useElement.OwnerDocument.Static = true; useElement.CopyToReferencedElement(refEl); refElParent.RemoveChild(refEl); useElement.AppendChild(refEl); foreach (XmlNode useChild in useElement.ChildNodes) { if (useChild.NodeType != XmlNodeType.Element) { continue; } SvgStyleableElement element = useChild as SvgStyleableElement; if (element != null && element.RenderingHint == SvgRenderingHint.Shape) { Geometry childPath = WpfRendering.CreateGeometry(element, settings.OptimizePath); if (childPath != null) { if (isFillFound) { string elementFill = element.GetAttribute("fill"); if (!String.IsNullOrEmpty(elementFill) && !String.Equals(elementFill, aggregatedFill, StringComparison.OrdinalIgnoreCase)) { return; } } else { aggregatedFill = element.GetAttribute("fill"); isFillFound = !String.IsNullOrEmpty(aggregatedFill); if (isFillFound) { paintElement = element; } } geomColl.Add(childPath); } } } useElement.RemoveChild(refEl); useElement.RestoreReferencedElement(refEl); refElParent.AppendChild(refEl); useElement.OwnerDocument.Static = false; } } //else if (String.Equals(node.LocalName, "g")) //{ //} else { SvgStyleableElement element = node as SvgStyleableElement; if (element != null && element.RenderingHint == SvgRenderingHint.Shape) { Geometry childPath = WpfRendering.CreateGeometry(element, settings.OptimizePath); if (childPath != null) { if (isFillFound) { string elementFill = element.GetAttribute("fill"); if (!String.IsNullOrEmpty(elementFill) && !String.Equals(elementFill, aggregatedFill, StringComparison.OrdinalIgnoreCase)) { return; } } else { aggregatedFill = element.GetAttribute("fill"); isFillFound = !String.IsNullOrEmpty(aggregatedFill); if (isFillFound) { paintElement = element; } } geomColl.Add(childPath); } } } } if (geomColl.Count == 0 || paintElement == null) { return; } _aggregatedFill = paintElement; _aggregatedGeom = geomColl; _isAggregated = true; }
public override void Render(WpfDrawingRenderer renderer) { _isAggregated = false; if (_isLayer) { base.Render(renderer); return; } WpfDrawingContext context = renderer.Context; Geometry clipGeom = this.ClipGeometry; Transform transform = this.Transform; float opacityValue = -1; SvgAElement element = (SvgAElement)_svgElement; string opacity = element.GetPropertyValue("opacity"); if (string.IsNullOrWhiteSpace(opacity)) { opacity = element.GetPropertyValue("opacity"); } if (opacity != null && opacity.Length > 0) { opacityValue = (float)SvgNumber.ParseNumber(opacity); opacityValue = Math.Min(opacityValue, 1); opacityValue = Math.Max(opacityValue, 0); } WpfLinkVisitor linkVisitor = context.LinkVisitor; if (linkVisitor != null || clipGeom != null || transform != null || opacityValue >= 0) { _drawGroup = new DrawingGroup(); string elementId = this.GetElementName(); if (!string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId)) { _drawGroup.SetValue(FrameworkElement.NameProperty, elementId); context.RegisterId(elementId); if (context.IncludeRuntime) { SvgObject.SetId(_drawGroup, elementId); } } string elementClass = this.GetElementClass(); if (!string.IsNullOrWhiteSpace(elementClass) && context.IncludeRuntime) { SvgObject.SetClass(_drawGroup, elementClass); } DrawingGroup currentGroup = context.Peek(); if (currentGroup == null) { throw new InvalidOperationException("An existing group is expected."); } if (linkVisitor != null && linkVisitor.Aggregates && context.Links != null) { if (!linkVisitor.Exists(elementId)) { context.Links.Children.Add(_drawGroup); } } else { currentGroup.Children.Add(_drawGroup); } context.Push(_drawGroup); if (clipGeom != null) { _drawGroup.ClipGeometry = clipGeom; } if (transform != null) { _drawGroup.Transform = transform; } if (opacityValue >= 0) { _drawGroup.Opacity = opacityValue; } string sVisibility = element.GetPropertyValue("visibility"); string sDisplay = element.GetPropertyValue("display"); if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none")) { opacityValue = 0; _drawGroup.Opacity = 0; } if (linkVisitor != null) { linkVisitor.Visit(_drawGroup, element, context, opacityValue); _isAggregated = linkVisitor.IsAggregate; } } base.Render(renderer); }
public abstract void Visit(DrawingGroup group, SvgAElement element, WpfDrawingContext context, float opacity);
public override void Render(WpfDrawingRenderer renderer) { _isAggregated = false; if (_isLayer) { base.Render(renderer); return; } var comparer = StringComparison.OrdinalIgnoreCase; WpfDrawingContext context = renderer.Context; Geometry clipGeom = this.ClipGeometry; Transform transform = this.Transform; float opacityValue = -1; SvgAElement element = (SvgAElement)_svgElement; string opacity = element.GetPropertyValue("opacity"); if (string.IsNullOrWhiteSpace(opacity)) { opacity = element.GetPropertyValue("opacity"); } if (!string.IsNullOrWhiteSpace(opacity)) { opacityValue = (float)SvgNumber.ParseNumber(opacity); opacityValue = Math.Min(opacityValue, 1); opacityValue = Math.Max(opacityValue, 0); } WpfLinkVisitor linkVisitor = context.LinkVisitor; if (linkVisitor != null || clipGeom != null || transform != null || (opacityValue >= 0 && opacityValue < 1)) { _drawGroup = new DrawingGroup(); string elementId = this.GetElementName(); if (!string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId)) { SvgObject.SetName(_drawGroup, elementId); context.RegisterId(elementId); if (context.IncludeRuntime) { SvgObject.SetId(_drawGroup, elementId); } } string elementClass = this.GetElementClass(); if (!string.IsNullOrWhiteSpace(elementClass) && context.IncludeRuntime) { SvgObject.SetClass(_drawGroup, elementClass); } DrawingGroup currentGroup = context.Peek(); if (currentGroup == null) { throw new InvalidOperationException("An existing group is expected."); } if (linkVisitor != null && linkVisitor.Aggregates && context.Links != null) { if (!linkVisitor.Exists(elementId)) { context.Links.Children.Add(_drawGroup); } } else { currentGroup.Children.Add(_drawGroup); } context.Push(_drawGroup); if (clipGeom != null) { _drawGroup.ClipGeometry = clipGeom; } if (transform != null) { _drawGroup.Transform = transform; } if ((opacityValue >= 0 && opacityValue < 1)) { _drawGroup.Opacity = opacityValue; } string sVisibility = element.GetPropertyValue(CssConstants.PropVisibility); string sDisplay = element.GetPropertyValue(CssConstants.PropDisplay); if (string.Equals(sVisibility, CssConstants.ValHidden, comparer) || string.Equals(sDisplay, CssConstants.ValNone, comparer)) { opacityValue = 0; _drawGroup.Opacity = 0; } if (linkVisitor != null) { linkVisitor.Visit(_drawGroup, element, context, opacityValue); _isAggregated = linkVisitor.IsAggregate; } } // Register this drawing with the Drawing-Document... this.Rendered(_drawGroup); base.Render(renderer); }