protected void FitToViewbox(GdiGraphics graphics, RectangleF elmRect) { ISvgFitToViewBox fitToVBElm = _svgElement as ISvgFitToViewBox; if (fitToVBElm != null) { SvgPreserveAspectRatio spar = (SvgPreserveAspectRatio)fitToVBElm.PreserveAspectRatio.AnimVal; double[] translateAndScale = spar.FitToViewBox((SvgRect)fitToVBElm.ViewBox.AnimVal, new SvgRect(elmRect.X, elmRect.Y, elmRect.Width, elmRect.Height)); graphics.TranslateTransform((float)translateAndScale[0], (float)translateAndScale[1]); graphics.ScaleTransform((float)translateAndScale[2], (float)translateAndScale[3]); } }
protected void fitToViewbox(GraphicsWrapper graphics, RectangleF elmRect) { if (element is ISvgFitToViewBox) { ISvgFitToViewBox fitToVBElm = (ISvgFitToViewBox)element; SvgPreserveAspectRatio spar = (SvgPreserveAspectRatio)fitToVBElm.PreserveAspectRatio.AnimVal; float[] translateAndScale = spar.FitToViewBox( (SvgRect)fitToVBElm.ViewBox.AnimVal, new SvgRect(elmRect.X, elmRect.Y, elmRect.Width, elmRect.Height) ); graphics.TranslateTransform(translateAndScale[0], translateAndScale[1]); graphics.ScaleTransform(translateAndScale[2], translateAndScale[3]); } }
public override void BeforeRender(WpfDrawingRenderer renderer) { base.BeforeRender(renderer); WpfDrawingContext context = renderer.Context; if (context.Count == 0) { _drawGroup = new DrawingGroup(); context.Push(_drawGroup); context.Root = _drawGroup; } else if (context.Count == 1) { DrawingGroup currentGroup = context.Peek(); if (currentGroup == null) { throw new InvalidOperationException("An existing group is expected."); } if (currentGroup == context.Root) { if (context.IsFragment) { // Do not add extra layer to fragments... _drawGroup = currentGroup; } else { _drawGroup = new DrawingGroup(); SvgObject.SetName(_drawGroup, SvgObject.DrawLayer); if (context.IncludeRuntime) { SvgLink.SetKey(_drawGroup, SvgObject.DrawLayer); } currentGroup.Children.Add(_drawGroup); context.Push(_drawGroup); } } else { _drawGroup = new DrawingGroup(); currentGroup.Children.Add(_drawGroup); context.Push(_drawGroup); } } else { _drawGroup = new DrawingGroup(); DrawingGroup currentGroup = context.Peek(); if (currentGroup == null) { throw new InvalidOperationException("An existing group is expected."); } currentGroup.Children.Add(_drawGroup); context.Push(_drawGroup); } SvgSvgElement svgElm = (SvgSvgElement)_svgElement; double x = Math.Round(svgElm.X.AnimVal.Value, 4); double y = Math.Round(svgElm.Y.AnimVal.Value, 4); double width = Math.Round(svgElm.Width.AnimVal.Value, 4); double height = Math.Round(svgElm.Height.AnimVal.Value, 4); if (width < 0 || height < 0) { // For invalid dimension, prevent the drawing of the children... _isRecursive = true; return; } Rect elmRect = new Rect(x, y, width, height); XmlNode parentNode = _svgElement.ParentNode; ISvgFitToViewBox fitToView = svgElm as ISvgFitToViewBox; ISvgAnimatedPreserveAspectRatio preserveAspectRatio = null; if (fitToView != null && fitToView.PreserveAspectRatio != null) { preserveAspectRatio = fitToView.PreserveAspectRatio; ISvgAnimatedRect animRect = fitToView.ViewBox; if (animRect != null) { ISvgRect viewRect = animRect.AnimVal; if (viewRect != null) { Rect wpfViewRect = WpfConvert.ToRect(viewRect); if (!wpfViewRect.IsEmpty && wpfViewRect.Width > 0 && wpfViewRect.Height > 0) { elmRect = wpfViewRect; } } } } Transform transform = null; var aspectRatio = (preserveAspectRatio != null) ? preserveAspectRatio.AnimVal : null; if (parentNode.NodeType != XmlNodeType.Document || (aspectRatio != null && aspectRatio.Align == SvgPreserveAspectRatioType.None)) { FitToViewbox(context, elmRect); transform = this.Transform; if (transform != null) { _drawGroup.Transform = transform; } } if (!elmRect.IsEmpty && !elmRect.Width.Equals(0) && !elmRect.Height.Equals(0)) { // Elements such as "pattern" are also rendered by this renderer, so we make sure we are // dealing with the root SVG element... if (parentNode != null && parentNode.NodeType == XmlNodeType.Document) { _drawGroup.ClipGeometry = new RectangleGeometry(elmRect); } else { if (transform != null) { // We have already applied the transform, which will translate to the start point... if (transform is TranslateTransform) { //_drawGroup.ClipGeometry = new RectangleGeometry( // new Rect(0, 0, elmRect.Width, elmRect.Height)); } else { _drawGroup.ClipGeometry = new RectangleGeometry(elmRect); } } else { _drawGroup.ClipGeometry = new RectangleGeometry(elmRect); } } } }
public override void BeforeRender(WpfDrawingRenderer renderer) { base.BeforeRender(renderer); _idElement = string.Empty; WpfDrawingContext context = renderer.Context; DrawingGroup currentGroup = context.Peek(); if (currentGroup == null) { throw new InvalidOperationException("An existing group is expected."); } if (currentGroup == context.Root) { if (context.IsFragment) { // Do not add extra layer to fragments... _drawGroup = currentGroup; } else { _drawGroup = new DrawingGroup(); SvgObject.SetName(_drawGroup, SvgObject.DrawLayer); if (context.IncludeRuntime) { SvgLink.SetKey(_drawGroup, SvgObject.DrawLayer); } currentGroup.Children.Add(_drawGroup); context.Push(_drawGroup); } } else { _drawGroup = new DrawingGroup(); currentGroup.Children.Add(_drawGroup); context.Push(_drawGroup); } SvgPatternElement svgElm = (SvgPatternElement)_svgElement; _idElement = svgElm.Id; if (!string.IsNullOrWhiteSpace(_idElement)) { context.AddUrl(_idElement); } double x = Math.Round(svgElm.X.AnimVal.Value, 4); double y = Math.Round(svgElm.Y.AnimVal.Value, 4); double width = Math.Round(svgElm.Width.AnimVal.Value, 4); double height = Math.Round(svgElm.Height.AnimVal.Value, 4); if (width < 0 || height < 0) { // For invalid dimension, prevent the drawing of the children... _isRecursive = true; return; } Rect elmRect = new Rect(x, y, width, height); // XmlNode parentNode = _svgElement.ParentNode; ISvgFitToViewBox fitToView = svgElm as ISvgFitToViewBox; ISvgAnimatedPreserveAspectRatio preserveAspectRatio = null; if (fitToView != null && fitToView.PreserveAspectRatio != null) { preserveAspectRatio = fitToView.PreserveAspectRatio; ISvgAnimatedRect animRect = fitToView.ViewBox; if (animRect != null) { ISvgRect viewRect = animRect.AnimVal; if (viewRect != null) { Rect wpfViewRect = WpfConvert.ToRect(viewRect); if (!wpfViewRect.IsEmpty && wpfViewRect.Width > 0 && wpfViewRect.Height > 0) { elmRect = wpfViewRect; } } } } Transform transform = null; var aspectRatio = (preserveAspectRatio != null) ? preserveAspectRatio.AnimVal : null; if (aspectRatio != null /* && aspectRatio.Align == SvgPreserveAspectRatioType.None*/) { FitToViewbox(context, elmRect); transform = this.Transform; if (transform != null) { _drawGroup.Transform = transform; } } }
public override void BeforeRender(WpfDrawingRenderer renderer) { base.BeforeRender(renderer); WpfDrawingContext context = renderer.Context; _drawGroup = new DrawingGroup(); if (context.Count == 0) { context.Push(_drawGroup); context.Root = _drawGroup; } else if (context.Count == 1) { DrawingGroup currentGroup = context.Peek(); if (currentGroup == null) { throw new InvalidOperationException("An existing group is expected."); } if (currentGroup == context.Root && !context.IsFragment) { _drawGroup.SetValue(FrameworkElement.NameProperty, SvgObject.DrawLayer); if (context.IncludeRuntime) { SvgLink.SetKey(_drawGroup, SvgObject.DrawLayer); } } currentGroup.Children.Add(_drawGroup); context.Push(_drawGroup); } else { DrawingGroup currentGroup = context.Peek(); if (currentGroup == null) { throw new InvalidOperationException("An existing group is expected."); } currentGroup.Children.Add(_drawGroup); context.Push(_drawGroup); } SvgSvgElement svgElm = (SvgSvgElement)_svgElement; double x = Math.Round(svgElm.X.AnimVal.Value, 4); double y = Math.Round(svgElm.Y.AnimVal.Value, 4); double width = Math.Round(svgElm.Width.AnimVal.Value, 4); double height = Math.Round(svgElm.Height.AnimVal.Value, 4); Rect elmRect = new Rect(x, y, width, height); //if (element.ParentNode is SvgElement) //{ // // TODO: should it be moved with x and y? //} XmlNode parentNode = _svgElement.ParentNode; //if (parentNode.NodeType == XmlNodeType.Document) { ISvgFitToViewBox fitToView = svgElm as ISvgFitToViewBox; if (fitToView != null) { ISvgAnimatedRect animRect = fitToView.ViewBox; if (animRect != null) { ISvgRect viewRect = animRect.AnimVal; if (viewRect != null) { Rect wpfViewRect = WpfConvert.ToRect(viewRect); if (!wpfViewRect.IsEmpty && wpfViewRect.Width > 0 && wpfViewRect.Height > 0) { elmRect = wpfViewRect; } } } } } Transform transform = null; if (parentNode.NodeType != XmlNodeType.Document) { FitToViewbox(context, elmRect); transform = this.Transform; if (transform != null) { _drawGroup.Transform = transform; } } //if (height > 0 && width > 0) //{ // ClipGeometry = new RectangleGeometry(elmRect); //} //Geometry clipGeom = this.ClipGeometry; //if (clipGeom != null) //{ // _drawGroup.ClipGeometry = clipGeom; //} if (((float)elmRect.Width != 0 && (float)elmRect.Height != 0)) { // Elements such as "pattern" are also rendered by this renderer, so we make sure we are // dealing with the root SVG element... if (parentNode != null && parentNode.NodeType == XmlNodeType.Document) { _drawGroup.ClipGeometry = new RectangleGeometry(elmRect); } else { if (transform != null) { // We have already applied the transform, which will translate to the start point... if (transform is TranslateTransform) { _drawGroup.ClipGeometry = new RectangleGeometry( new Rect(0, 0, elmRect.Width, elmRect.Height)); } else { _drawGroup.ClipGeometry = new RectangleGeometry(elmRect); } } else { _drawGroup.ClipGeometry = new RectangleGeometry(elmRect); } } //DrawingGroup animationGroup = context.Links; //if (animationGroup != null) //{ // animationGroup.ClipGeometry = clipGeom; //} } }
protected void FitToViewbox(WpfDrawingContext context, SvgElement svgElement, Rect elementBounds) { if (svgElement == null) { return; } ISvgFitToViewBox fitToView = svgElement as ISvgFitToViewBox; if (fitToView == null) { return; } SvgPreserveAspectRatio spar = (SvgPreserveAspectRatio)fitToView.PreserveAspectRatio.AnimVal; SvgRect viewBox = (SvgRect)fitToView.ViewBox.AnimVal; SvgRect rectToFit = new SvgRect(elementBounds.X, elementBounds.Y, elementBounds.Width, elementBounds.Height); double[] transformArray = spar.FitToViewBox(viewBox, rectToFit); double translateX = Math.Round(transformArray[0], 4); double translateY = Math.Round(transformArray[1], 4); double scaleX = Math.Round(transformArray[2], 4); double scaleY = Math.Round(transformArray[3], 4); Transform translateMatrix = null; Transform scaleMatrix = null; if (!translateX.Equals(0.0) || !translateY.Equals(0.0)) { translateMatrix = new TranslateTransform(translateX, translateY); } if (!scaleX.Equals(1.0) || !scaleY.Equals(1.0)) { scaleMatrix = new ScaleTransform(scaleX, scaleY); } if (translateMatrix != null && scaleMatrix != null) { // Create a TransformGroup to contain the transforms // and add the transforms to it. if (translateMatrix.Value.IsIdentity && scaleMatrix.Value.IsIdentity) { return; } if (translateMatrix.Value.IsIdentity) { this.Transform = scaleMatrix; return; } if (scaleMatrix.Value.IsIdentity) { this.Transform = translateMatrix; return; } TransformGroup transformGroup = new TransformGroup(); transformGroup.Children.Add(scaleMatrix); transformGroup.Children.Add(translateMatrix); this.Transform = transformGroup; } else if (translateMatrix != null) { this.Transform = translateMatrix; } else if (scaleMatrix != null) { this.Transform = scaleMatrix; } }
public override Brush GetBrush(Rect elementBounds, WpfDrawingContext context, Transform viewTransform) { Rect bounds = elementBounds; DrawingGroup image = this.GetImage(context, bounds); if (image == null || image.Bounds.Width.Equals(0) || image.Bounds.Height.Equals(0)) { return(null); } bool isUserSpace = true; if (_renderedElement.PatternContentUnits.AnimVal.Equals((ushort)SvgUnitType.ObjectBoundingBox)) { bounds = new Rect(0, 0, 1, 1); isUserSpace = false; } Rect destRect = GetDestRect(bounds); // Check for validity of the brush... if (destRect.Width.Equals(0) || destRect.Height.Equals(0) || destRect.IsEmpty) { return(null); } // Apply a scale if needed if (isUserSpace && image.Transform != null) { ISvgFitToViewBox fitToView = _renderedElement as ISvgFitToViewBox; if (fitToView != null && fitToView.ViewBox != null) { ISvgAnimatedRect animRect = fitToView.ViewBox; ISvgRect viewRect = animRect.AnimVal; if (viewRect != null) { Rect wpfViewRect = WpfConvert.ToRect(viewRect); if (!wpfViewRect.IsEmpty && wpfViewRect.Width > 0 && wpfViewRect.Height > 0) { var scaleX = elementBounds.Width > 0 ? destRect.Width / wpfViewRect.Width : 1; var scaleY = elementBounds.Height > 0 ? destRect.Height / wpfViewRect.Height : 1; if (!scaleX.Equals(1) || !scaleY.Equals(1)) { var currentTransform = image.Transform as ScaleTransform; if (currentTransform != null) { image.Transform = new ScaleTransform(scaleX, scaleY); } } } } } } DrawingBrush tb = new DrawingBrush(image); tb.Viewbox = destRect; tb.Viewport = destRect; //tb.Viewbox = new Rect(0, 0, destRect.Width, destRect.Height); //tb.Viewport = new Rect(0, 0, bounds.Width, bounds.Height); tb.ViewboxUnits = BrushMappingMode.Absolute; tb.ViewportUnits = isUserSpace ? BrushMappingMode.Absolute : BrushMappingMode.RelativeToBoundingBox; tb.TileMode = TileMode.Tile; // tb.Stretch = isUserSpace ? Stretch.Fill : Stretch.Uniform; if (isUserSpace) { MatrixTransform transform = GetTransformMatrix(image.Bounds, isUserSpace); if (transform != null && !transform.Matrix.IsIdentity) { tb.Transform = transform; } } else { MatrixTransform transform = GetTransformMatrix(bounds, isUserSpace); if (transform != null && !transform.Matrix.IsIdentity) { tb.RelativeTransform = transform; } } return(tb); }