public override void BeforeRender(GdiGraphicsRenderer renderer) { if (renderer == null) { return; } if (_uniqueColor.IsEmpty) { _uniqueColor = renderer.GetNextHitColor(_svgElement); } var graphics = renderer.GdiGraphics; if (graphics == null) { return; } _graphicsContainer = graphics.BeginContainer(); SetQuality(graphics); SetTransform(graphics); SetClip(graphics); }
public GdiRenderingHelper(GdiGraphicsRenderer renderer) { _currentLang = CultureInfo.CurrentCulture.TwoLetterISOLanguageName; _renderer = renderer; //_rendererMap = new Dictionary<ISvgElement, GdiRenderingBase>(); _rendererMap = new Stack<GdiRenderingBase>(); }
public GdiRenderingHelper(GdiGraphicsRenderer renderer) { _currentLang = CultureInfo.CurrentCulture.TwoLetterISOLanguageName; _renderer = renderer; //_rendererMap = new Dictionary<ISvgElement, GdiRenderingBase>(); _rendererMap = new Stack <GdiRenderingBase>(); }
public override void AfterRender(GdiGraphicsRenderer renderer) { if (renderer == null || renderer.GdiGraphics == null) { return; } var graphics = renderer.GdiGraphics; graphics.EndContainer(_graphicsContainer); }
public override void BeforeRender(GdiGraphicsRenderer renderer) { if (_uniqueColor.IsEmpty) _uniqueColor = renderer.GetNextColor(element); GdiGraphicsWrapper graphics = renderer.GraphicsWrapper; _graphicsContainer = graphics.BeginContainer(); SetQuality(graphics); Transform(graphics); }
private Image GetImage(RectangleF bounds) { _patternElement.PatternBounds = new SvgRect(bounds.X, bounds.Y, bounds.Width, bounds.Height); // For single image pattern... if (_patternElement.ChildNodes.Count == 1) { var imageElement = _patternElement.ChildNodes[0] as SvgImageElement; if (imageElement != null) { var image = GdiImageRendering.GetBitmap(imageElement); if (image != null) { return(image); } } } GdiGraphicsRenderer renderer = new GdiGraphicsRenderer(true, false); var svgWindow = _patternElement.OwnerDocument.Window as SvgWindow; var ownedWindow = svgWindow.CreateOwnedWindow(); renderer.Window = ownedWindow; SvgSvgElement elm = MoveIntoSvgElement(); int winWidth = (int)elm.Width.BaseVal.Value; int winHeight = (int)elm.Height.BaseVal.Value; if (winWidth == 0 || winHeight == 0) { var size = elm.GetSize(); if (size.Width.Equals(0) || size.Height.Equals(0)) { winWidth = (int)bounds.Width; winHeight = (int)bounds.Height; } else { winWidth = (int)size.Width; winHeight = (int)size.Height; } } ownedWindow.Resize(winWidth, winHeight); renderer.Render(elm as SvgElement); Image img = renderer.RasterImage; MoveOutOfSvgElement(elm); return(img); }
public override void BeforeRender(GdiGraphicsRenderer renderer) { if (_uniqueColor.IsEmpty && _textMode != GdiTextMode.Outlining) { _uniqueColor = renderer.GetNextHitColor(_svgElement); } var graphics = renderer.GdiGraphics; _graphicsContainer = graphics.BeginContainer(); SetQuality(graphics); SetTransform(graphics); }
public override void BeforeRender(GdiGraphicsRenderer renderer) { if (_uniqueColor.IsEmpty) { _uniqueColor = renderer.GetNextColor(element); } GdiGraphicsWrapper graphics = renderer.GraphicsWrapper; _graphicsContainer = graphics.BeginContainer(); SetQuality(graphics); Transform(graphics); }
private Image GetImage(RectangleF bounds) { GdiGraphicsRenderer renderer = new GdiGraphicsRenderer(); renderer.Window = _patternElement.OwnerDocument.Window as SvgWindow; SvgSvgElement elm = MoveIntoSvgElement(); renderer.Render(elm as SvgElement); Image img = renderer.RasterImage; MoveOutOfSvgElement(elm); return img; }
public GdiRenderingHelper(GdiGraphicsRenderer renderer) { var cultureInfo = CultureInfo.CurrentCulture; _currentLang = cultureInfo.TwoLetterISOLanguageName; _currentLangName = cultureInfo.Name; _renderer = renderer; //_rendererMap = new Dictionary<ISvgElement, GdiRenderingBase>(); // _rendererMap = new Stack<GdiRenderingBase>(); _rendererMap = new Dictionary <string, GdiRenderingBase>(StringComparer.OrdinalIgnoreCase); _useElements = new HashSet <int>(); _useIdElements = new HashSet <string>(StringComparer.OrdinalIgnoreCase); }
public override void BeforeRender(GdiGraphicsRenderer renderer) { if (_uniqueColor.IsEmpty) { _uniqueColor = renderer.GetNextHitColor(this.Element); } var graphics = renderer.GdiGraphics; _graphicsContainer = graphics.BeginContainer(); SetQuality(graphics); SetTransform(graphics); }
private Image GetImage(RectangleF bounds) { GdiGraphicsRenderer renderer = new GdiGraphicsRenderer(); renderer.Window = _patternElement.OwnerDocument.Window as SvgWindow; SvgSvgElement elm = MoveIntoSvgElement(); renderer.Render(elm as SvgElement); Image img = renderer.RasterImage; MoveOutOfSvgElement(elm); return(img); }
public virtual bool NeedRender(GdiGraphicsRenderer renderer) { // We make this assumption so that the first pass is still fast // That way we don't have to calculate the screen regions // Before a full rendering if (screenRegion == SvgRectF.Empty) return true; if (renderer.InvalidRect == SvgRectF.Empty) return true; if (renderer.InvalidRect.Intersects(screenRegion)) // TODO: Eventually add a full path check here? return true; return false; }
public SvgPictureBox() { InitializeComponent(); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); //scriptEngineByMimeType = new TypeDictionary(); //SetMimeTypeEngineType("application/ecmascript", typeof(JScriptEngine)); renderer = new GdiGraphicsRenderer(); renderer.OnRender = new RenderEvent(this.OnRender); window = new SvgPictureBoxWindow(this, renderer); }
private SvgWindow GetSvgWindow() { if (_embeddedRenderer == null) { _embeddedRenderer = new GdiGraphicsRenderer(); } SvgImageElement iElm = this.Element as SvgImageElement; SvgWindow wnd = iElm.SvgWindow; wnd.Renderer = _embeddedRenderer; _embeddedRenderer.Window = wnd; return(wnd); }
public override void AfterRender(GdiGraphicsRenderer renderer) { if (renderer == null) { return; } GdiGraphicsWrapper graphics = renderer.GraphicsWrapper; if (graphics == null) { return; } graphics.EndContainer(_graphicsContainer); }
private SvgWindow GetSvgWindow(GdiGraphics graphics) { SvgImageElement iElm = this.Element as SvgImageElement; SvgWindow wnd = iElm.SvgWindow; if (_embeddedRenderer == null) { _embeddedRenderer = new GdiGraphicsRenderer(graphics, wnd); } else { wnd.Renderer = _embeddedRenderer; _embeddedRenderer.Window = wnd; } return(wnd); }
protected static void PaintMarkers(GdiGraphicsRenderer renderer, SvgStyleableElement styleElm, GdiGraphics 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 override void Render(GdiGraphicsRenderer renderer) { var graphics = renderer.GdiGraphics; var svgElm = (SvgSvgElement)_svgElement; float x = (float)svgElm.X.AnimVal.Value; float y = (float)svgElm.Y.AnimVal.Value; float width = (float)svgElm.Width.AnimVal.Value; float height = (float)svgElm.Height.AnimVal.Value; RectangleF elmRect = new RectangleF(x, y, width, height); //if (element.ParentNode is SvgElement) //{ // // TODO: should it be moved with x and y? //} FitToViewbox(graphics, elmRect); }
public override void Render(GdiGraphicsRenderer renderer) { GdiGraphicsWrapper graphics = renderer.GraphicsWrapper; SvgSvgElement svgElm = (SvgSvgElement) element; float x = (float)svgElm.X.AnimVal.Value; float y = (float)svgElm.Y.AnimVal.Value; float width = (float)svgElm.Width.AnimVal.Value; float height = (float)svgElm.Height.AnimVal.Value; RectangleF elmRect = new RectangleF(x, y, width, height); //if (element.ParentNode is SvgElement) //{ // // TODO: should it be moved with x and y? //} FitToViewbox(graphics, elmRect); }
public virtual bool NeedRender(GdiGraphicsRenderer renderer) { // We make this assumption so that the first pass is still fast // That way we don't have to calculate the screen regions // Before a full rendering if (screenRegion == SvgRectF.Empty) { return(true); } if (renderer.InvalidRect == SvgRectF.Empty) { return(true); } if (renderer.InvalidRect.Intersects(screenRegion)) { // TODO: Eventually add a full path check here? return(true); } return(false); }
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 override void Render(GdiGraphicsRenderer renderer) { GdiGraphicsWrapper graphics = renderer.GraphicsWrapper; SvgRenderingHint hint = element.RenderingHint; if (hint != SvgRenderingHint.Shape || hint == SvgRenderingHint.Clipping) { return; } if (element.ParentNode is SvgClipPathElement) { return; } SvgStyleableElement styleElm = (SvgStyleableElement)element; string sVisibility = styleElm.GetPropertyValue("visibility"); string sDisplay = styleElm.GetPropertyValue("display"); if (String.Equals(sVisibility, "hidden") || String.Equals(sDisplay, "none")) { return; } GraphicsPath gp = CreatePath(element); if (gp != null) { Clip(graphics); GdiSvgPaint fillPaint = new GdiSvgPaint(styleElm, "fill"); Brush brush = fillPaint.GetBrush(gp); GdiSvgPaint strokePaint = new GdiSvgPaint(styleElm, "stroke"); Pen pen = strokePaint.GetPen(gp); if (brush != null) { if (brush is PathGradientBrush) { GdiGradientFill gps = fillPaint.PaintFill as GdiGradientFill; graphics.SetClip(gps.GetRadialGradientRegion(gp.GetBounds()), CombineMode.Exclude); SolidBrush tempBrush = new SolidBrush(((PathGradientBrush)brush).InterpolationColors.Colors[0]); graphics.FillPath(this, tempBrush,gp); tempBrush.Dispose(); graphics.ResetClip(); } graphics.FillPath(this, brush, gp); brush.Dispose(); brush = null; } if (pen != null) { if (pen.Brush is PathGradientBrush) { GdiGradientFill gps = strokePaint.PaintFill as GdiGradientFill; GdiGraphicsContainer container = graphics.BeginContainer(); graphics.SetClip(gps.GetRadialGradientRegion(gp.GetBounds()), CombineMode.Exclude); SolidBrush tempBrush = new SolidBrush(((PathGradientBrush)pen.Brush).InterpolationColors.Colors[0]); Pen tempPen = new Pen(tempBrush, pen.Width); graphics.DrawPath(this, tempPen,gp); tempPen.Dispose(); tempBrush.Dispose(); graphics.EndContainer(container); } graphics.DrawPath(this, pen, gp); pen.Dispose(); pen = null; } gp.Dispose(); gp = null; } PaintMarkers(renderer, styleElm, graphics); }
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); } }
private GraphicsPath CreateClippingRegion(GdiGraphics graphics, SvgClipPathElement clipPath) { GraphicsPath path = new GraphicsPath(); foreach (XmlNode node in clipPath.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) { GraphicsPath childPath = CreatePath(element); if (childPath != null) { string clipRule = element.GetPropertyValue("clip-rule"); path.FillMode = (clipRule == "evenodd") ? FillMode.Alternate : FillMode.Winding; path.AddPath(childPath, true); } } } useElement.RemoveChild(refEl); useElement.RestoreReferencedElement(refEl); refElParent.AppendChild(refEl); useElement.OwnerDocument.Static = false; } } else { SvgStyleableElement element = node as SvgStyleableElement; if (element != null) { if (element.RenderingHint == SvgRenderingHint.Shape) { GraphicsPath childPath = CreatePath(element); if (childPath != null) { string clipRule = element.GetPropertyValue("clip-rule"); path.FillMode = (clipRule == "evenodd") ? FillMode.Alternate : FillMode.Winding; path.AddPath(childPath, true); } } else if (element.RenderingHint == SvgRenderingHint.Text) { GdiTextRendering textRendering = new GdiTextRendering(element); textRendering.TextMode = GdiTextMode.Outlining; GdiGraphicsRenderer renderer = new GdiGraphicsRenderer(graphics); textRendering.BeforeRender(renderer); textRendering.Render(renderer); textRendering.AfterRender(renderer); GraphicsPath childPath = textRendering.Path; if (childPath != null) { string clipRule = element.GetPropertyValue("clip-rule"); path.FillMode = (clipRule == "evenodd") ? FillMode.Alternate : FillMode.Winding; path.AddPath(childPath, true); } } } } } return(path); }
public override void Render(GdiGraphicsRenderer renderer) { GdiGraphicsWrapper graphics = renderer.GraphicsWrapper; SvgImageElement iElement = (SvgImageElement)element; ImageAttributes imageAttributes = new ImageAttributes(); string sOpacity = iElement.GetPropertyValue("opacity"); if (sOpacity != null && sOpacity.Length > 0) { double opacity = SvgNumber.ParseNumber(sOpacity); ColorMatrix myColorMatrix = new ColorMatrix(); myColorMatrix.Matrix00 = 1.00f; // Red myColorMatrix.Matrix11 = 1.00f; // Green myColorMatrix.Matrix22 = 1.00f; // Blue myColorMatrix.Matrix33 = (float)opacity; // alpha myColorMatrix.Matrix44 = 1.00f; // w imageAttributes.SetColorMatrix(myColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); } float width = (float)iElement.Width.AnimVal.Value; float height = (float)iElement.Height.AnimVal.Value; Rectangle destRect = new Rectangle(Convert.ToInt32(iElement.X.AnimVal.Value), Convert.ToInt32(iElement.Y.AnimVal.Value), Convert.ToInt32(width), Convert.ToInt32(height)); Image image = null; if (iElement.IsSvgImage) { SvgWindow wnd = GetSvgWindow(); _embeddedRenderer.BackColor = Color.Empty; _embeddedRenderer.Render(wnd.Document); image = _embeddedRenderer.RasterImage; } else { image = GetBitmap(iElement); } if (image != null) { graphics.DrawImage(this, image, destRect, 0f, 0f, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes); image.Dispose(); image = null; } if (_embeddedRenderer != null) { _embeddedRenderer.Dispose(); _embeddedRenderer = null; } if (imageAttributes != null) { imageAttributes.Dispose(); imageAttributes = null; } }
public virtual void Render(GdiGraphicsRenderer renderer) { }
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((ushort)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((ushort)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 virtual void AfterRender(GdiGraphicsRenderer renderer) { }
public override void Render(GdiGraphicsRenderer renderer) { var graphics = renderer.GdiGraphics; SvgImageElement iElement = (SvgImageElement)_svgElement; ImageAttributes imageAttributes = new ImageAttributes(); string sOpacity = iElement.GetPropertyValue("opacity"); if (sOpacity != null && sOpacity.Length > 0) { double opacity = SvgNumber.ParseNumber(sOpacity); ColorMatrix myColorMatrix = new ColorMatrix(); myColorMatrix.Matrix00 = 1.00f; // Red myColorMatrix.Matrix11 = 1.00f; // Green myColorMatrix.Matrix22 = 1.00f; // Blue myColorMatrix.Matrix33 = (float)opacity; // alpha myColorMatrix.Matrix44 = 1.00f; // w imageAttributes.SetColorMatrix(myColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); } float width = (float)iElement.Width.AnimVal.Value; float height = (float)iElement.Height.AnimVal.Value; RectangleF destRect = new RectangleF((float)iElement.X.AnimVal.Value, (float)iElement.Y.AnimVal.Value, (float)iElement.Width.AnimVal.Value, (float)iElement.Height.AnimVal.Value); Image image = null; if (iElement.IsSvgImage) { SvgWindow wnd = GetSvgWindow(); _embeddedRenderer.BackColor = Color.Empty; _embeddedRenderer.Render(wnd.Document); image = _embeddedRenderer.RasterImage; } else { image = GetBitmap(iElement); } if (image != null) { // code extracted from FitToViewbox var spar = (SvgPreserveAspectRatio)iElement.PreserveAspectRatio.AnimVal ?? new SvgPreserveAspectRatio("none", iElement); double[] translateAndScale = spar.FitToViewBox(new SvgRect(0, 0, image.Width, image.Height), new SvgRect(destRect.X, destRect.Y, destRect.Width, destRect.Height)); graphics.TranslateTransform((float)translateAndScale[0], (float)translateAndScale[1]); graphics.ScaleTransform((float)translateAndScale[2], (float)translateAndScale[3]); graphics.DrawImage(this, image, new Rectangle(0, 0, image.Width, image.Height), 0f, 0f, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes); image.Dispose(); image = null; } if (_embeddedRenderer != null) { _embeddedRenderer.Dispose(); _embeddedRenderer = null; } if (imageAttributes != null) { imageAttributes.Dispose(); imageAttributes = null; } }
public void PaintMarker(GdiGraphicsRenderer renderer, GdiGraphics gr, SvgMarkerPosition markerPos, SvgStyleableElement refElement) { ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement; SvgMarkerElement markerElm = (SvgMarkerElement)_svgElement; SvgPointF[] vertexPositions = markerHostElm.MarkerPositions; if (vertexPositions == null) { return; } var comparer = StringComparison.OrdinalIgnoreCase; bool mayHaveCurves = markerHostElm.MayHaveCurves; 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; } int end = start + len; for (int i = start; i < end; i++) { SvgPointF point = vertexPositions[i]; GdiGraphicsContainer gc = gr.BeginContainer(); gr.TranslateTransform(point.X, point.Y); if (markerElm.OrientType.AnimVal.Equals((ushort)SvgMarkerOrient.Angle)) { double scaleValue = markerElm.OrientAngle.AnimVal.Value; if (!scaleValue.Equals(0)) { gr.RotateTransform((float)scaleValue); } } else { double angle; switch (markerPos) { case SvgMarkerPosition.Start: angle = markerHostElm.GetStartAngle(i); //angle = markerHostElm.GetStartAngle(i + 1); if (vertexPositions.Length >= 2) { SvgPointF pMarkerPoint1 = vertexPositions[start]; SvgPointF pMarkerPoint2 = vertexPositions[end]; float xDiff = pMarkerPoint2.X - pMarkerPoint1.X; float yDiff = pMarkerPoint2.Y - pMarkerPoint1.Y; double angleMarker = (float)(Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI); if (!angleMarker.Equals(angle)) { angle = angleMarker; } } 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 - 1); //double angle2 = markerHostElm.GetEndAngle(i); if (vertexPositions.Length >= 2) { SvgPointF pMarkerPoint1 = vertexPositions[start - 1]; SvgPointF pMarkerPoint2 = vertexPositions[start]; float xDiff = pMarkerPoint2.X - pMarkerPoint1.X; float yDiff = pMarkerPoint2.Y - pMarkerPoint1.Y; double angleMarker = (float)(Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI); if (!angleMarker.Equals(angle)) { angle = angleMarker; } } //if (mayHaveCurves) //{ // angle = this.GetAngleAt(start - 1, angle, markerPos, markerHostElm); //} break; } gr.RotateTransform((float)angle); } // 'viewBox' and 'preserveAspectRatio' attributes // viewBox -> viewport(0, 0, markerWidth, markerHeight) var 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)); //// Warning at this time, refX and refY are relative to the painted element's coordinate system. //// We need to move the reference point to the marker's coordinate system //float refX = (float)markerElm.RefX.AnimVal.Value; //float refY = (float)markerElm.RefY.AnimVal.Value; ////if (!(refX.Equals(0) && refY.Equals(0))) ////{ //// var points = new PointF[] { new PointF(refX, refY) }; //// gr.Transform.TransformPoints(points); //// refX = points[0].X; //// refY = points[0].Y; //// gr.TranslateTransform(-refX, -refY); ////} //if (markerElm.MarkerUnits.AnimVal.Equals((ushort)SvgMarkerUnit.StrokeWidth)) //{ // SvgLength strokeWidthLength = new SvgLength(refElement, // "stroke-width", SvgLengthSource.Css, SvgLengthDirection.Viewport, "1"); // float strokeWidth = (float)strokeWidthLength.Value; // gr.ScaleTransform(strokeWidth, strokeWidth); //} //gr.TranslateTransform(-(float)(markerElm.RefX.AnimVal.Value * translateAndScale[2]), // -(float)(markerElm.RefY.AnimVal.Value * translateAndScale[3])); //gr.ScaleTransform((float)translateAndScale[2], (float)translateAndScale[3]); // compute an additional transform for 'strokeWidth' coordinate system ISvgAnimatedEnumeration markerUnits = markerElm.MarkerUnits; if (markerUnits.AnimVal.Equals((ushort)SvgMarkerUnit.StrokeWidth)) { SvgLength strokeWidthLength = new SvgLength(refElement, "stroke-width", SvgLengthSource.Css, SvgLengthDirection.Viewport, SvgConstants.ValOne); double strokeWidth = strokeWidthLength.Value; if (!strokeWidth.Equals(1)) { gr.ScaleTransform((float)strokeWidth, (float)strokeWidth); } } gr.TranslateTransform(-(float)(markerElm.RefX.AnimVal.Value * translateAndScale[2]), -(float)(markerElm.RefY.AnimVal.Value * translateAndScale[3])); if (!(translateAndScale[2].Equals(1) && translateAndScale[3].Equals(1))) { gr.ScaleTransform((float)translateAndScale[2], (float)translateAndScale[3]); } // gr.TranslateTransform(point.X, point.Y); RectangleF rectClip = RectangleF.Empty; if (markerUnits.AnimVal.Equals((ushort)SvgMarkerUnit.StrokeWidth)) { string overflowAttr = markerElm.GetAttribute("overflow"); if (string.IsNullOrWhiteSpace(overflowAttr) || overflowAttr.Equals("scroll", comparer) || overflowAttr.Equals(CssConstants.ValHidden, comparer)) { var markerClip = RectangleF.Empty; SvgRect clipRect = (SvgRect)markerElm.ViewBox.AnimVal; if (clipRect != null && !clipRect.IsEmpty) { rectClip = new RectangleF((float)clipRect.X, (float)clipRect.Y, (float)clipRect.Width, (float)clipRect.Height); } else if (markerElm.IsSizeDefined) { rectClip = new RectangleF(0, 0, (float)markerElm.MarkerWidth.AnimVal.Value, (float)markerElm.MarkerHeight.AnimVal.Value); } } } if (rectClip.IsEmpty) { SetClip(gr); } else { gr.SetClip(rectClip); } renderer.RenderChildren(markerElm); gr.EndContainer(gc); } }
public override void Render(GdiGraphicsRenderer renderer) { _graphics = renderer.GraphicsWrapper; SvgRenderingHint hint = element.RenderingHint; if (hint == SvgRenderingHint.Clipping) { return; } if (element.ParentNode is SvgClipPathElement) { return; } SvgTextElement textElement = element as SvgTextElement; if (textElement == null) { return; } string sVisibility = textElement.GetPropertyValue("visibility"); string sDisplay = textElement.GetPropertyValue("display"); if (String.Equals(sVisibility, "hidden") || String.Equals(sDisplay, "none")) { return; } Clip(_graphics); PointF ctp = new PointF(0, 0); // current text position ctp = GetCurrentTextPosition(textElement, ctp); string sBaselineShift = textElement.GetPropertyValue("baseline-shift").Trim(); double shiftBy = 0; if (sBaselineShift.Length > 0) { float textFontSize = GetComputedFontSize(textElement); if (sBaselineShift.EndsWith("%")) { shiftBy = SvgNumber.ParseNumber(sBaselineShift.Substring(0, sBaselineShift.Length - 1)) / 100 * textFontSize; } else if (sBaselineShift == "sub") { shiftBy = -0.6F * textFontSize; } else if (sBaselineShift == "super") { shiftBy = 0.6F * textFontSize; } else if (sBaselineShift == "baseline") { shiftBy = 0; } else { shiftBy = SvgNumber.ParseNumber(sBaselineShift); } } XmlNodeType nodeType = XmlNodeType.None; foreach (XmlNode child in element.ChildNodes) { nodeType = child.NodeType; if (nodeType == XmlNodeType.Text) { ctp.Y -= (float)shiftBy; AddGraphicsPath(textElement, ref ctp, GetText(textElement, child)); ctp.Y += (float)shiftBy; } else if (nodeType == XmlNodeType.Element) { string nodeName = child.Name; if (String.Equals(nodeName, "tref")) { AddTRefElementPath((SvgTRefElement)child, ref ctp); } else if (String.Equals(nodeName, "tspan")) { AddTSpanElementPath((SvgTSpanElement)child, ref ctp); } } } PaintMarkers(renderer, textElement, _graphics); _graphics = null; }
private SvgWindow GetSvgWindow() { if (_embeddedRenderer == null) { _embeddedRenderer = new GdiGraphicsRenderer(); } SvgImageElement iElm = this.Element as SvgImageElement; SvgWindow wnd = iElm.SvgWindow; wnd.Renderer = _embeddedRenderer; _embeddedRenderer.Window = wnd; return wnd; }
public static Image GetBitmap(SvgImageElement element) { var comparer = StringComparison.OrdinalIgnoreCase; if (!element.IsSvgImage) { if (!element.Href.AnimVal.StartsWith("data:", comparer)) { SvgUriReference svgUri = element.UriReference; Uri imageUri = new Uri(svgUri.AbsoluteUri); if (imageUri.IsFile && File.Exists(imageUri.LocalPath)) { return(Image.FromFile(imageUri.LocalPath, element.ColorProfile != null)); } WebResponse resource = svgUri.ReferencedResource; if (resource == null) { return(null); } Stream stream = resource.GetResponseStream(); if (stream == null) { return(null); } return(Image.FromStream(stream, element.ColorProfile != null)); } string sURI = element.Href.AnimVal.Replace(" ", "").Trim(); sURI = sURI.Replace(@"\n", ""); int nColon = sURI.IndexOf(":", comparer); int nSemiColon = sURI.IndexOf(";", comparer); int nComma = sURI.IndexOf(",", comparer); string sMimeType = sURI.Substring(nColon + 1, nSemiColon - nColon - 1); string sContent = sURI.Substring(nComma + 1); sContent = sContent.Replace('-', '+').Replace('_', '/'); sContent = sContent.PadRight(4 * ((sContent.Length + 3) / 4), '='); byte[] bResult = Convert.FromBase64CharArray(sContent.ToCharArray(), 0, sContent.Length); if (sMimeType.Equals("image/svg+xml", StringComparison.OrdinalIgnoreCase)) { GdiGraphicsRenderer renderer = new GdiGraphicsRenderer(true, false); var currentWindow = element.OwnerDocument.Window as SvgWindow; var svgWindow = currentWindow.CreateOwnedWindow(); renderer.Window = svgWindow; SvgDocument doc = svgWindow.CreateEmptySvgDocument(); bool isGZiped = sContent.StartsWith(GdiObject.GZipSignature, StringComparison.Ordinal); if (isGZiped) { byte[] imageBytes = Convert.FromBase64CharArray(sContent.ToCharArray(), 0, sContent.Length); using (var stream = new MemoryStream(imageBytes)) { using (GZipStream zipStream = new GZipStream(stream, CompressionMode.Decompress)) { doc.Load(zipStream); } } } else { var svgData = Convert.FromBase64String(Convert.ToBase64String(bResult)); var svgFragment = Encoding.ASCII.GetString(svgData); doc.LoadXml(svgFragment); } svgWindow.Document = doc; SvgSvgElement elm = (SvgSvgElement)doc.RootElement; int winWidth = (int)elm.Width.BaseVal.Value; int winHeight = (int)elm.Height.BaseVal.Value; if (winWidth == 0 || winHeight == 0) { var size = elm.GetSize(); winWidth = (int)size.Width; winHeight = (int)size.Height; } svgWindow.Resize(winWidth, winHeight); renderer.Render(elm as SvgElement); Image img = renderer.RasterImage; return(img); } MemoryStream ms = new MemoryStream(bResult); return(Image.FromStream(ms, element.ColorProfile != null)); } return(null); }
public override void Render(GdiGraphicsRenderer renderer) { _graphics = renderer.GdiGraphics; //SvgRenderingHint hint = _svgElement.RenderingHint; //if (hint == SvgRenderingHint.Clipping) //{ // return; //} //if (_svgElement.ParentNode is SvgClipPathElement) //{ // return; //} SvgTextBaseElement textElement = _svgElement as SvgTextBaseElement; if (textElement == null) { return; } string sVisibility = textElement.GetPropertyValue("visibility"); string sDisplay = textElement.GetPropertyValue("display"); if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none")) { return; } if (_textMode != GdiTextMode.Outlining) { SetClip(_graphics); } PointF ctp = new PointF(0, 0); // current text position ctp = GetCurrentTextPosition(textElement, ctp); string sBaselineShift = textElement.GetPropertyValue("baseline-shift").Trim(); double shiftBy = 0; if (sBaselineShift.Length > 0) { float textFontSize = GetComputedFontSize(textElement); if (sBaselineShift.EndsWith("%", StringComparison.OrdinalIgnoreCase)) { shiftBy = SvgNumber.ParseNumber(sBaselineShift.Substring(0, sBaselineShift.Length - 1)) / 100 * textFontSize; } else if (sBaselineShift == "sub") { shiftBy = -0.6F * textFontSize; } else if (sBaselineShift == "super") { shiftBy = 0.6F * textFontSize; } else if (sBaselineShift == "baseline") { shiftBy = 0; } else { shiftBy = SvgNumber.ParseNumber(sBaselineShift); } } // For for fonts loading in the background... var svgDoc = _svgElement.OwnerDocument; if (svgDoc.IsFontsLoaded == false) { //TODO: Use of SpinUntil is known to CPU heavy, but will work for now... SpinWait.SpinUntil(() => svgDoc.IsFontsLoaded == true); } XmlNodeType nodeType = XmlNodeType.None; foreach (XmlNode child in _svgElement.ChildNodes) { SvgStyleableElement stylable = child as SvgStyleableElement; if (stylable != null) { sVisibility = stylable.GetPropertyValue("visibility"); sDisplay = stylable.GetPropertyValue("display"); if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none")) { continue; } } nodeType = child.NodeType; if (nodeType == XmlNodeType.Text) { ctp.Y -= (float)shiftBy; AddGraphicsPath(textElement, ref ctp, GetText(textElement, child)); ctp.Y += (float)shiftBy; } else if (nodeType == XmlNodeType.Element) { string nodeName = child.Name; if (string.Equals(nodeName, "tref")) { AddTRefElementPath((SvgTRefElement)child, ref ctp); } else if (string.Equals(nodeName, "tspan")) { AddTSpanElementPath((SvgTSpanElement)child, ref ctp); } } } PaintMarkers(renderer, textElement, _graphics); _graphics = null; }
public override void Render(GdiGraphicsRenderer renderer) { GdiGraphicsWrapper graphics = renderer.GraphicsWrapper; SvgRenderingHint hint = element.RenderingHint; if (hint != SvgRenderingHint.Shape || hint == SvgRenderingHint.Clipping) { return; } if (element.ParentNode is SvgClipPathElement) { return; } SvgStyleableElement styleElm = (SvgStyleableElement)element; string sVisibility = styleElm.GetPropertyValue("visibility"); string sDisplay = styleElm.GetPropertyValue("display"); if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none")) { return; } GraphicsPath gp = CreatePath(element); if (gp != null) { Clip(graphics); GdiSvgPaint fillPaint = new GdiSvgPaint(styleElm, "fill"); Brush brush = fillPaint.GetBrush(gp); GdiSvgPaint strokePaint = new GdiSvgPaint(styleElm, "stroke"); Pen pen = strokePaint.GetPen(gp); if (brush != null) { if (brush is PathGradientBrush) { GdiGradientFill gps = fillPaint.PaintFill as GdiGradientFill; graphics.SetClip(gps.GetRadialGradientRegion(gp.GetBounds()), CombineMode.Exclude); SolidBrush tempBrush = new SolidBrush(((PathGradientBrush)brush).InterpolationColors.Colors[0]); graphics.FillPath(this, tempBrush, gp); tempBrush.Dispose(); graphics.ResetClip(); } graphics.FillPath(this, brush, gp); brush.Dispose(); brush = null; } if (pen != null) { if (pen.Brush is PathGradientBrush) { GdiGradientFill gps = strokePaint.PaintFill as GdiGradientFill; GdiGraphicsContainer container = graphics.BeginContainer(); graphics.SetClip(gps.GetRadialGradientRegion(gp.GetBounds()), CombineMode.Exclude); SolidBrush tempBrush = new SolidBrush(((PathGradientBrush)pen.Brush).InterpolationColors.Colors[0]); Pen tempPen = new Pen(tempBrush, pen.Width); graphics.DrawPath(this, tempPen, gp); tempPen.Dispose(); tempBrush.Dispose(); graphics.EndContainer(container); } graphics.DrawPath(this, pen, gp); pen.Dispose(); pen = null; } gp.Dispose(); gp = null; } PaintMarkers(renderer, styleElm, graphics); }
protected static void PaintMarkers(GdiGraphicsRenderer renderer, SvgStyleableElement styleElm, GdiGraphics 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")); string markerAll = ExtractMarkerUrl(styleElm.GetPropertyValue("marker", "marker")); // The SVG specification defines three properties to reference markers: marker-start, // marker -mid, marker-end. It also provides a shorthand property, marker. Using the marker // property from a style sheet is equivalent to using all three (start, mid, end). // However, shorthand properties cannot be used as presentation attributes. if (!string.IsNullOrWhiteSpace(markerAll) && !IsPresentationMarker(styleElm)) { if (string.IsNullOrWhiteSpace(markerStartUrl)) { markerStartUrl = markerAll; } if (string.IsNullOrWhiteSpace(markerMiddleUrl)) { markerMiddleUrl = markerAll; } if (string.IsNullOrWhiteSpace(markerEndUrl)) { markerEndUrl = markerAll; } } 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 override void Render(GdiGraphicsRenderer renderer) { _graphics = renderer.GraphicsWrapper; SvgRenderingHint hint = element.RenderingHint; if (hint == SvgRenderingHint.Clipping) { return; } if (element.ParentNode is SvgClipPathElement) { return; } SvgTextElement textElement = element as SvgTextElement; if (textElement == null) { return; } string sVisibility = textElement.GetPropertyValue("visibility"); string sDisplay = textElement.GetPropertyValue("display"); if (String.Equals(sVisibility, "hidden") || String.Equals(sDisplay, "none")) { return; } Clip(_graphics); PointF ctp = new PointF(0, 0); // current text position ctp = GetCurrentTextPosition(textElement, ctp); string sBaselineShift = textElement.GetPropertyValue("baseline-shift").Trim(); double shiftBy = 0; if (sBaselineShift.Length > 0) { float textFontSize = GetComputedFontSize(textElement); if (sBaselineShift.EndsWith("%")) { shiftBy = SvgNumber.ParseNumber(sBaselineShift.Substring(0, sBaselineShift.Length - 1)) / 100 * textFontSize; } else if (sBaselineShift == "sub") { shiftBy = -0.6F * textFontSize; } else if (sBaselineShift == "super") { shiftBy = 0.6F * textFontSize; } else if (sBaselineShift == "baseline") { shiftBy = 0; } else { shiftBy = SvgNumber.ParseNumber(sBaselineShift); } } XmlNodeType nodeType = XmlNodeType.None; foreach (XmlNode child in element.ChildNodes) { SvgStyleableElement stylable = child as SvgStyleableElement; if (stylable != null) { sVisibility = stylable.GetPropertyValue("visibility"); sDisplay = stylable.GetPropertyValue("display"); if (String.Equals(sVisibility, "hidden") || String.Equals(sDisplay, "none")) { continue; } } nodeType = child.NodeType; if (nodeType == XmlNodeType.Text) { ctp.Y -= (float)shiftBy; AddGraphicsPath(textElement, ref ctp, GetText(textElement, child)); ctp.Y += (float)shiftBy; } else if (nodeType == XmlNodeType.Element) { string nodeName = child.Name; if (String.Equals(nodeName, "tref")) { AddTRefElementPath((SvgTRefElement)child, ref ctp); } else if (String.Equals(nodeName, "tspan")) { AddTSpanElementPath((SvgTSpanElement)child, ref ctp); } } } PaintMarkers(renderer, textElement, _graphics); _graphics = null; }
// define empty handlers by default public virtual void BeforeRender(GdiGraphicsRenderer renderer) { }
public override void AfterRender(GdiGraphicsRenderer renderer) { }
// disable default rendering public override void BeforeRender(GdiGraphicsRenderer renderer) { }
public override void Render(GdiGraphicsRenderer renderer) { base.Render(renderer); var graphics = renderer.GdiGraphics; SvgImageElement iElement = (SvgImageElement)_svgElement; ImageAttributes imageAttributes = new ImageAttributes(); float opacityValue = -1; string opacity = iElement.GetAttribute("opacity"); if (string.IsNullOrWhiteSpace(opacity)) { opacity = iElement.GetPropertyValue("opacity"); } if (!string.IsNullOrWhiteSpace(opacity)) { opacityValue = (float)SvgNumber.ParseNumber(opacity); opacityValue = Math.Min(opacityValue, 1); opacityValue = Math.Max(opacityValue, 0); } if (opacityValue >= 0 && opacityValue < 1) { ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.Matrix00 = 1.00f; // Red colorMatrix.Matrix11 = 1.00f; // Green colorMatrix.Matrix22 = 1.00f; // Blue colorMatrix.Matrix33 = opacityValue; // alpha colorMatrix.Matrix44 = 1.00f; // w imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); } float width = (float)iElement.Width.AnimVal.Value; float height = (float)iElement.Height.AnimVal.Value; RectangleF destRect = new RectangleF((float)iElement.X.AnimVal.Value, (float)iElement.Y.AnimVal.Value, (float)iElement.Width.AnimVal.Value, (float)iElement.Height.AnimVal.Value); RectangleF srcRect; RectangleF clipRect = destRect; // var container = graphics.BeginContainer(); graphics.SetClip(new Region(clipRect), CombineMode.Intersect); Image image = null; SvgWindow svgWnd = null; if (iElement.IsSvgImage) { svgWnd = GetSvgWindow(graphics); if (width > 0 && height > 0) { srcRect = new RectangleF(0, 0, width, height); } else { SvgSvgElement svgEl = (SvgSvgElement)svgWnd.Document.RootElement; SvgSizeF size = svgEl.GetSize(); srcRect = new RectangleF(new PointF(0, 0), new SizeF(size.Width, size.Height)); } } else { image = GetBitmap(iElement); if (image == null) { return; } srcRect = new RectangleF(0, 0, image.Width, image.Height); } ISvgAnimatedPreserveAspectRatio animatedAspectRatio = iElement.PreserveAspectRatio; if (animatedAspectRatio != null && animatedAspectRatio.AnimVal != null) { SvgPreserveAspectRatio aspectRatio = animatedAspectRatio.AnimVal as SvgPreserveAspectRatio; SvgPreserveAspectRatioType aspectRatioType = (aspectRatio != null) ? aspectRatio.Align : SvgPreserveAspectRatioType.Unknown; if (aspectRatio != null && aspectRatioType != SvgPreserveAspectRatioType.None && aspectRatioType != SvgPreserveAspectRatioType.Unknown) { var fScaleX = destRect.Width / srcRect.Width; var fScaleY = destRect.Height / srcRect.Height; var xOffset = 0.0f; var yOffset = 0.0f; SvgMeetOrSlice meetOrSlice = aspectRatio.MeetOrSlice; if (meetOrSlice == SvgMeetOrSlice.Slice) { fScaleX = Math.Max(fScaleX, fScaleY); fScaleY = Math.Max(fScaleX, fScaleY); } else { fScaleX = Math.Min(fScaleX, fScaleY); fScaleY = Math.Min(fScaleX, fScaleY); } switch (aspectRatioType) { case SvgPreserveAspectRatioType.XMinYMin: break; case SvgPreserveAspectRatioType.XMidYMin: xOffset = (destRect.Width - srcRect.Width * fScaleX) / 2; break; case SvgPreserveAspectRatioType.XMaxYMin: xOffset = (destRect.Width - srcRect.Width * fScaleX); break; case SvgPreserveAspectRatioType.XMinYMid: yOffset = (destRect.Height - srcRect.Height * fScaleY) / 2; break; case SvgPreserveAspectRatioType.XMidYMid: xOffset = (destRect.Width - srcRect.Width * fScaleX) / 2; yOffset = (destRect.Height - srcRect.Height * fScaleY) / 2; break; case SvgPreserveAspectRatioType.XMaxYMid: xOffset = (destRect.Width - srcRect.Width * fScaleX); yOffset = (destRect.Height - srcRect.Height * fScaleY) / 2; break; case SvgPreserveAspectRatioType.XMinYMax: yOffset = (destRect.Height - srcRect.Height * fScaleY); break; case SvgPreserveAspectRatioType.XMidYMax: xOffset = (destRect.Width - srcRect.Width * fScaleX) / 2; yOffset = (destRect.Height - srcRect.Height * fScaleY); break; case SvgPreserveAspectRatioType.XMaxYMax: xOffset = (destRect.Width - srcRect.Width * fScaleX); yOffset = (destRect.Height - srcRect.Height * fScaleY); break; } destRect = new RectangleF(destRect.X + xOffset, destRect.Y + yOffset, srcRect.Width * fScaleX, srcRect.Height * fScaleY); } if (image != null) { SvgColorProfileElement colorProfile = (SvgColorProfileElement)iElement.ColorProfile; if (colorProfile != null) { SvgUriReference svgUri = colorProfile.UriReference; Uri profileUri = new Uri(svgUri.AbsoluteUri); imageAttributes.SetOutputChannelColorProfile(profileUri.LocalPath, ColorAdjustType.Default); } graphics.DrawImage(this, image, destRect, srcRect, GraphicsUnit.Pixel, imageAttributes); image.Dispose(); image = null; } else if (iElement.IsSvgImage && svgWnd != null) { svgWnd.Resize((int)srcRect.Width, (int)srcRect.Height); var currOffset = new PointF(graphics.Transform.OffsetX, graphics.Transform.OffsetY); if (!currOffset.IsEmpty) { graphics.TranslateTransform(-currOffset.X, -currOffset.Y); } graphics.ScaleTransform(destRect.Width / srcRect.Width, destRect.Height / srcRect.Height); if (!currOffset.IsEmpty) { graphics.TranslateTransform(currOffset.X + destRect.X, currOffset.Y + destRect.Y); } _embeddedRenderer.Render(svgWnd.Document); } graphics.ResetClip(); // graphics.EndContainer(container); } if (_embeddedRenderer != null) { _embeddedRenderer.GdiGraphics = null; _embeddedRenderer.Dispose(); _embeddedRenderer = null; } if (imageAttributes != null) { imageAttributes.Dispose(); imageAttributes = null; } }
public override void Render(GdiGraphicsRenderer renderer) { GdiGraphicsWrapper graphics = renderer.GraphicsWrapper; SvgImageElement iElement = (SvgImageElement)element; ImageAttributes imageAttributes = new ImageAttributes(); string sOpacity = iElement.GetPropertyValue("opacity"); if (sOpacity != null && sOpacity.Length > 0) { double opacity = SvgNumber.ParseNumber(sOpacity); ColorMatrix myColorMatrix = new ColorMatrix(); myColorMatrix.Matrix00 = 1.00f; // Red myColorMatrix.Matrix11 = 1.00f; // Green myColorMatrix.Matrix22 = 1.00f; // Blue myColorMatrix.Matrix33 = (float)opacity; // alpha myColorMatrix.Matrix44 = 1.00f; // w imageAttributes.SetColorMatrix(myColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); } float width = (float)iElement.Width.AnimVal.Value; float height = (float)iElement.Height.AnimVal.Value; //Rectangle destRect = new Rectangle(Convert.ToInt32(iElement.X.AnimVal.Value), // Convert.ToInt32(iElement.Y.AnimVal.Value), // Convert.ToInt32(width), Convert.ToInt32(height)); RectangleF destRect = new RectangleF((float)iElement.X.AnimVal.Value, (float)iElement.Y.AnimVal.Value, (float)iElement.Width.AnimVal.Value, (float)iElement.Height.AnimVal.Value); Image image = null; if (iElement.IsSvgImage) { SvgWindow wnd = GetSvgWindow(); _embeddedRenderer.BackColor = Color.Empty; _embeddedRenderer.Render(wnd.Document); image = _embeddedRenderer.RasterImage; } else { image = GetBitmap(iElement); } if (image != null) { //graphics.DrawImage(this, image, destRect, 0f, 0f, // image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes); // code extracted from FitToViewbox SvgPreserveAspectRatio spar = (SvgPreserveAspectRatio)iElement.PreserveAspectRatio.AnimVal ?? new SvgPreserveAspectRatio("none", iElement); double[] translateAndScale = spar.FitToViewBox(new SvgRect(0, 0, image.Width, image.Height), new SvgRect(destRect.X, destRect.Y, destRect.Width, destRect.Height)); graphics.TranslateTransform((float)translateAndScale[0], (float)translateAndScale[1]); graphics.ScaleTransform((float)translateAndScale[2], (float)translateAndScale[3]); graphics.DrawImage(this, image, new Rectangle(0, 0, image.Width, image.Height), 0f, 0f, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes); image.Dispose(); image = null; } if (_embeddedRenderer != null) { _embeddedRenderer.Dispose(); _embeddedRenderer = null; } if (imageAttributes != null) { imageAttributes.Dispose(); imageAttributes = null; } }