public WpfSvgPaint(WpfDrawingContext context, SvgStyleableElement elm, string propName) : base(elm.GetComputedStyle("").GetPropertyValue(propName)) { _propertyName = propName; _element = elm; _context = context; }
public void SetUp() { SvgWindow wnd = new SvgWindow(100, 100, new GdiRenderer()); SvgDocument doc = wnd.CreateEmptySvgDocument(); doc.LoadXml("<svg xmlns='" + SvgDocument.SvgNamespace + "'><rect /></svg>"); elm = (SvgStyleableElement)doc.SelectSingleNode("//*[local-name()='rect']"); }
public WpfSvgColor(SvgStyleableElement elm, string propertyName) : base(elm.GetComputedStyle("").GetPropertyValue(propertyName)) { _element = elm; _propertyName = propertyName; }
protected static void PaintMarkers(GdiGraphicsRenderer renderer, SvgStyleableElement styleElm, GdiGraphicsWrapper gr) { // OPTIMIZE if (styleElm is ISharpMarkerHost) { string markerStartUrl = ExtractMarkerUrl(styleElm.GetPropertyValue("marker-start", "marker")); string markerMiddleUrl = ExtractMarkerUrl(styleElm.GetPropertyValue("marker-mid", "marker")); string markerEndUrl = ExtractMarkerUrl(styleElm.GetPropertyValue("marker-end", "marker")); if (markerStartUrl.Length > 0) { GdiMarkerRendering grNode = CreateByUri(styleElm.OwnerDocument, styleElm.BaseURI, markerStartUrl) as GdiMarkerRendering; if (grNode != null) { grNode.PaintMarker(renderer, gr, SvgMarkerPosition.Start, styleElm); } } if (markerMiddleUrl.Length > 0) { // TODO markerMiddleUrl != markerStartUrl GdiMarkerRendering grNode = CreateByUri(styleElm.OwnerDocument, styleElm.BaseURI, markerMiddleUrl) as GdiMarkerRendering; if (grNode != null) { grNode.PaintMarker(renderer, gr, SvgMarkerPosition.Mid, styleElm); } } if (markerEndUrl.Length > 0) { // TODO: markerEndUrl != markerMiddleUrl GdiMarkerRendering grNode = CreateByUri(styleElm.OwnerDocument, styleElm.BaseURI, markerEndUrl) as GdiMarkerRendering; if (grNode != null) { grNode.PaintMarker(renderer, gr, SvgMarkerPosition.End, styleElm); } } } }
public void PaintMarker(GdiGraphicsRenderer renderer, GdiGraphicsWrapper gr, SvgMarkerPosition markerPos, SvgStyleableElement refElement) { ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement; SvgMarkerElement markerElm = (SvgMarkerElement) element; SvgPointF[] vertexPositions = markerHostElm.MarkerPositions; int start; int len; // Choose which part of the position array to use switch (markerPos) { case SvgMarkerPosition.Start: start = 0; len = 1; break; case SvgMarkerPosition.Mid: start = 1; len = vertexPositions.Length - 2; break; default: // == MarkerPosition.End start = vertexPositions.Length-1; len = 1; break; } for (int i = start; i < start+len; i++) { SvgPointF point = vertexPositions[i]; GdiGraphicsContainer gc = gr.BeginContainer(); gr.TranslateTransform(point.X, point.Y); if (markerElm.OrientType.AnimVal.Equals(SvgMarkerOrient.Angle)) { gr.RotateTransform((float)markerElm.OrientAngle.AnimVal.Value); } else { double angle; switch(markerPos) { case SvgMarkerPosition.Start: angle = markerHostElm.GetStartAngle(i + 1); break; case SvgMarkerPosition.Mid: //angle = (markerHostElm.GetEndAngle(i) + markerHostElm.GetStartAngle(i + 1)) / 2; angle = SvgNumber.CalcAngleBisection(markerHostElm.GetEndAngle(i), markerHostElm.GetStartAngle(i + 1)); break; default: angle = markerHostElm.GetEndAngle(i); break; } gr.RotateTransform((float)angle); } if (markerElm.MarkerUnits.AnimVal.Equals(SvgMarkerUnit.StrokeWidth)) { SvgLength strokeWidthLength = new SvgLength(refElement, "stroke-width", SvgLengthSource.Css, SvgLengthDirection.Viewport, "1"); float strokeWidth = (float)strokeWidthLength.Value; gr.ScaleTransform(strokeWidth, strokeWidth); } SvgPreserveAspectRatio spar = (SvgPreserveAspectRatio)markerElm.PreserveAspectRatio.AnimVal; double[] translateAndScale = spar.FitToViewBox((SvgRect)markerElm.ViewBox.AnimVal, new SvgRect(0, 0, markerElm.MarkerWidth.AnimVal.Value, markerElm.MarkerHeight.AnimVal.Value)); gr.TranslateTransform(-(float)(markerElm.RefX.AnimVal.Value * translateAndScale[2]), -(float)(markerElm.RefY.AnimVal.Value * translateAndScale[3])); gr.ScaleTransform((float)translateAndScale[2], (float)translateAndScale[3]); Clip(gr); renderer.RenderChildren(markerElm); gr.EndContainer(gc); } }
public void RenderMarkerEx0(WpfDrawingRenderer renderer, WpfDrawingContext gr, SvgMarkerPosition markerPos, SvgStyleableElement refElement) { //ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement; //SvgMarkerElement markerElm = (SvgMarkerElement)element; //SvgPointF[] vertexPositions = markerHostElm.MarkerPositions; //int start; //int len; //// Choose which part of the position array to use //switch (markerPos) //{ // case SvgMarkerPosition.Start: // start = 0; // len = 1; // break; // case SvgMarkerPosition.Mid: // start = 1; // len = vertexPositions.Length - 2; // break; // default: // // == MarkerPosition.End // start = vertexPositions.Length - 1; // len = 1; // break; //} //for (int i = start; i < start + len; i++) //{ // SvgPointF point = vertexPositions[i]; // GdiGraphicsContainer gc = gr.BeginContainer(); // gr.TranslateTransform(point.X, point.Y); // if (markerElm.OrientType.AnimVal.Equals(SvgMarkerOrient.Angle)) // { // gr.RotateTransform((float)markerElm.OrientAngle.AnimVal.Value); // } // else // { // float angle; // switch (markerPos) // { // case SvgMarkerPosition.Start: // angle = markerHostElm.GetStartAngle(i + 1); // break; // case SvgMarkerPosition.Mid: // //angle = (markerHostElm.GetEndAngle(i) + markerHostElm.GetStartAngle(i + 1)) / 2; // angle = SvgNumber.CalcAngleBisection(markerHostElm.GetEndAngle(i), markerHostElm.GetStartAngle(i + 1)); // break; // default: // angle = markerHostElm.GetEndAngle(i); // break; // } // gr.RotateTransform(angle); // } // if (markerElm.MarkerUnits.AnimVal.Equals(SvgMarkerUnit.StrokeWidth)) // { // SvgLength strokeWidthLength = new SvgLength(refElement, // "stroke-width", SvgLengthSource.Css, SvgLengthDirection.Viewport, "1"); // float strokeWidth = (float)strokeWidthLength.Value; // gr.ScaleTransform(strokeWidth, strokeWidth); // } // SvgPreserveAspectRatio spar = // (SvgPreserveAspectRatio)markerElm.PreserveAspectRatio.AnimVal; // float[] translateAndScale = spar.FitToViewBox((SvgRect)markerElm.ViewBox.AnimVal, // new SvgRect(0, 0, markerElm.MarkerWidth.AnimVal.Value, // markerElm.MarkerHeight.AnimVal.Value)); // gr.TranslateTransform(-(float)markerElm.RefX.AnimVal.Value * translateAndScale[2], // -(float)markerElm.RefY.AnimVal.Value * translateAndScale[3]); // gr.ScaleTransform(translateAndScale[2], translateAndScale[3]); // Clip(gr); // renderer.RenderChildren(markerElm); // gr.EndContainer(gc); //} }
public void RenderMarker0(WpfDrawingRenderer renderer, WpfDrawingContext gr, SvgMarkerPosition markerPos, SvgStyleableElement refElement) { //PathGeometry g; //g.GetPointAtFractionLength( ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement; SvgMarkerElement markerElm = (SvgMarkerElement)_svgElement; SvgPointF[] vertexPositions = markerHostElm.MarkerPositions; int start; int len; // Choose which part of the position array to use switch (markerPos) { case SvgMarkerPosition.Start: start = 0; len = 1; break; case SvgMarkerPosition.Mid: start = 1; len = vertexPositions.Length - 2; break; default: // == MarkerPosition.End start = vertexPositions.Length - 1; len = 1; break; } for (int i = start; i < start + len; i++) { SvgPointF point = vertexPositions[i]; Matrix m = GetTransformMatrix(_svgElement); //GraphicsContainer gc = gr.BeginContainer(); this.BeforeRender(renderer); //gr.TranslateTransform(point.X, point.Y); //PAUL: //m.Translate(point.X, point.Y); if (markerElm.OrientType.AnimVal.Equals(SvgMarkerOrient.Angle)) { m.Rotate(markerElm.OrientAngle.AnimVal.Value); //gr.RotateTransform((double)markerElm.OrientAngle.AnimVal.Value); } else { double angle; switch (markerPos) { case SvgMarkerPosition.Start: angle = markerHostElm.GetStartAngle(i + 1); break; case SvgMarkerPosition.Mid: //angle = (markerHostElm.GetEndAngle(i) + markerHostElm.GetStartAngle(i + 1)) / 2; angle = SvgNumber.CalcAngleBisection(markerHostElm.GetEndAngle(i), markerHostElm.GetStartAngle(i + 1)); break; default: angle = markerHostElm.GetEndAngle(i); break; } //gr.RotateTransform(angle); m.Rotate(angle); } if (markerElm.MarkerUnits.AnimVal.Equals(SvgMarkerUnit.StrokeWidth)) { string propValue = refElement.GetPropertyValue("stroke-width"); if (propValue.Length == 0) propValue = "1"; SvgLength strokeWidthLength = new SvgLength("stroke-width", propValue, refElement, SvgLengthDirection.Viewport); double strokeWidth = strokeWidthLength.Value; //gr.ScaleTransform(strokeWidth, strokeWidth); m.Scale(strokeWidth, strokeWidth); } SvgPreserveAspectRatio spar = (SvgPreserveAspectRatio)markerElm.PreserveAspectRatio.AnimVal; double[] translateAndScale = spar.FitToViewBox( (SvgRect)markerElm.ViewBox.AnimVal, new SvgRect(0, 0, markerElm.MarkerWidth.AnimVal.Value, markerElm.MarkerHeight.AnimVal.Value)); //PAUL: //m.Translate(-(double)markerElm.RefX.AnimVal.Value * translateAndScale[2], -(double)markerElm.RefY.AnimVal.Value * translateAndScale[3]); //PAUL: m.Scale(translateAndScale[2], translateAndScale[3]); m.Translate(point.X, point.Y); //Matrix oldTransform = TransformMatrix; //TransformMatrix = m; //try //{ //newTransform.Append(m); //TransformGroup tg = new TransformGroup(); //renderer.Canvas.re //gr.TranslateTransform( // -(double)markerElm.RefX.AnimVal.Value * translateAndScale[2], // -(double)markerElm.RefY.AnimVal.Value * translateAndScale[3] // ); //gr.ScaleTransform(translateAndScale[2], translateAndScale[3]); renderer.RenderChildren(markerElm); // markerElm.RenderChildren(renderer); //} //finally //{ // TransformMatrix = oldTransform; //} // //gr.EndContainer(gc); _matrix = m; this.Render(renderer); //gr.EndContainer(gc); this.AfterRender(renderer); } }
public void RenderMarker(WpfDrawingRenderer renderer, WpfDrawingContext gr, SvgMarkerPosition markerPos, SvgStyleableElement refElement) { ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement; SvgMarkerElement markerElm = (SvgMarkerElement)_svgElement; SvgPointF[] vertexPositions = markerHostElm.MarkerPositions; int start; int len; // Choose which part of the position array to use switch (markerPos) { case SvgMarkerPosition.Start: start = 0; len = 1; break; case SvgMarkerPosition.Mid: start = 1; len = vertexPositions.Length - 2; break; default: // == MarkerPosition.End start = vertexPositions.Length - 1; len = 1; break; } for (int i = start; i < start + len; i++) { SvgPointF point = vertexPositions[i]; //GdiGraphicsContainer gc = gr.BeginContainer(); this.BeforeRender(renderer); //Matrix matrix = Matrix.Identity; Matrix matrix = GetTransformMatrix(_svgElement); if (markerElm.OrientType.AnimVal.Equals(SvgMarkerOrient.Angle)) { matrix.Rotate(markerElm.OrientAngle.AnimVal.Value); } else { double angle = 0; switch (markerPos) { case SvgMarkerPosition.Start: angle = markerHostElm.GetStartAngle(i + 1); break; case SvgMarkerPosition.Mid: //angle = (markerHostElm.GetEndAngle(i) + markerHostElm.GetStartAngle(i + 1)) / 2; angle = SvgNumber.CalcAngleBisection(markerHostElm.GetEndAngle(i), markerHostElm.GetStartAngle(i + 1)); break; default: angle = markerHostElm.GetEndAngle(i); break; } matrix.Rotate(angle); } if (markerElm.MarkerUnits.AnimVal.Equals(SvgMarkerUnit.StrokeWidth)) { SvgLength strokeWidthLength = new SvgLength(refElement, "stroke-width", SvgLengthSource.Css, SvgLengthDirection.Viewport, "1"); double strokeWidth = strokeWidthLength.Value; matrix.Scale(strokeWidth, strokeWidth); } SvgPreserveAspectRatio spar = (SvgPreserveAspectRatio)markerElm.PreserveAspectRatio.AnimVal; double[] translateAndScale = spar.FitToViewBox((SvgRect)markerElm.ViewBox.AnimVal, new SvgRect(0, 0, markerElm.MarkerWidth.AnimVal.Value, markerElm.MarkerHeight.AnimVal.Value)); matrix.Translate(-markerElm.RefX.AnimVal.Value * translateAndScale[2], -markerElm.RefY.AnimVal.Value * translateAndScale[3]); matrix.Scale(translateAndScale[2], translateAndScale[3]); matrix.Translate(point.X, point.Y); _matrix = matrix; this.Render(renderer); //Clip(gr); renderer.RenderChildren(markerElm); //gr.EndContainer(gc); this.AfterRender(renderer); } }
public SvgLength(string propertyName, SvgStyleableElement ownerElement, SvgLengthDirection direction, string defaultValue) { this.ownerElement = ownerElement; this.direction = direction; string baseVal = ownerElement.GetPropertyValue(propertyName); if (baseVal == null || baseVal == "") { baseVal = defaultValue; } baseVal = SvgNumber.ScientificToDec(baseVal); this.cssLength = new CssAbsPrimitiveLengthValue(new CssPrimitiveLengthValue(baseVal, false), propertyName, ownerElement); }
protected static void RenderMarkers(WpfDrawingRenderer renderer, SvgStyleableElement styleElm, WpfDrawingContext gr) { // OPTIMIZE if (styleElm is ISharpMarkerHost) { string markerStartUrl = ExtractMarkerUrl(styleElm.GetPropertyValue("marker-start", "marker")); string markerMiddleUrl = ExtractMarkerUrl(styleElm.GetPropertyValue("marker-mid", "marker")); string markerEndUrl = ExtractMarkerUrl(styleElm.GetPropertyValue("marker-end", "marker")); if (markerStartUrl.Length > 0) { WpfMarkerRendering markerRenderer = CreateByUri(styleElm.OwnerDocument, styleElm.BaseURI, markerStartUrl) as WpfMarkerRendering; if (markerRenderer != null) { markerRenderer.RenderMarker(renderer, gr, SvgMarkerPosition.Start, styleElm); } } if (markerMiddleUrl.Length > 0) { // TODO markerMiddleUrl != markerStartUrl WpfMarkerRendering markerRenderer = CreateByUri(styleElm.OwnerDocument, styleElm.BaseURI, markerMiddleUrl) as WpfMarkerRendering; if (markerRenderer != null) { markerRenderer.RenderMarker(renderer, gr, SvgMarkerPosition.Mid, styleElm); } } if (markerEndUrl.Length > 0) { // TODO: markerEndUrl != markerMiddleUrl WpfMarkerRendering markerRenderer = CreateByUri(styleElm.OwnerDocument, styleElm.BaseURI, markerEndUrl) as WpfMarkerRendering; if (markerRenderer != null) { markerRenderer.RenderMarker(renderer, gr, SvgMarkerPosition.End, styleElm); } } } }
private GeometryCollection GetTextClippingRegion(SvgStyleableElement element, WpfDrawingContext context) { GeometryCollection geomColl = new GeometryCollection(); WpfDrawingRenderer renderer = new WpfDrawingRenderer(); renderer.Window = _svgElement.OwnerDocument.Window as SvgWindow; WpfDrawingSettings settings = context.Settings.Clone(); settings.TextAsGeometry = true; WpfDrawingContext clipContext = new WpfDrawingContext(true, settings); clipContext.RenderingClipRegion = true; clipContext.Initialize(null, context.FontFamilyVisitor, null); renderer.Render(element, clipContext); DrawingGroup rootGroup = renderer.Drawing as DrawingGroup; if (rootGroup != null && rootGroup.Children.Count == 1) { DrawingGroup textGroup = rootGroup.Children[0] as DrawingGroup; if (textGroup != null) { ExtractGeometry(textGroup, geomColl); } } return geomColl; }
public GdiSvgPaint(SvgStyleableElement elm, string propName) : base(elm.GetComputedStyle("").GetPropertyValue(propName)) { _element = elm; }
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; }
private void PaintMarkers(GdiRenderer renderer, SvgStyleableElement styleElm, GraphicsWrapper gr) { // OPTIMIZE if ( styleElm is ISharpMarkerHost ) { string markerStartUrl = extractMarkerUrl(styleElm.GetPropertyValue("marker-start", "marker")); string markerMiddleUrl = extractMarkerUrl(styleElm.GetPropertyValue("marker-mid", "marker")); string markerEndUrl = extractMarkerUrl(styleElm.GetPropertyValue("marker-end", "marker")); RenderingNode grNode; if ( markerStartUrl.Length > 0 ) { grNode = renderer.GetGraphicsNodeByUri(styleElm.BaseURI, markerStartUrl); if (grNode is SvgMarkerGraphicsNode) { ((SvgMarkerGraphicsNode) grNode).PaintMarker(renderer, gr, SvgMarkerPosition.Start, styleElm); } } if ( markerMiddleUrl.Length > 0 ) { // TODO markerMiddleUrl != markerStartUrl grNode = renderer.GetGraphicsNodeByUri(styleElm.BaseURI, markerMiddleUrl); if ( grNode is SvgMarkerGraphicsNode ) { ((SvgMarkerGraphicsNode) grNode).PaintMarker(renderer, gr, SvgMarkerPosition.Mid, styleElm); } } if ( markerEndUrl.Length > 0 ) { // TODO: markerEndUrl != markerMiddleUrl grNode = renderer.GetGraphicsNodeByUri(styleElm.BaseURI, markerEndUrl); if(grNode is SvgMarkerGraphicsNode) { ((SvgMarkerGraphicsNode) grNode).PaintMarker(renderer, gr, SvgMarkerPosition.End, styleElm); } } } }