From 93b893a77f8853f7c4c6832ebc25b0b4906d509e Mon Sep 17 00:00:00 2001 From: paulushub Date: Thu, 1 Feb 2024 22:08:43 +0900 Subject: [PATCH] Reactivated the EnsureViewboxSize support - It is used and required by some applications #278 --- .../Wpf/WpfSvgRendering.cs | 115 +++++++++++++++--- 1 file changed, 97 insertions(+), 18 deletions(-) diff --git a/Source/SharpVectorRenderingWpf/Wpf/WpfSvgRendering.cs b/Source/SharpVectorRenderingWpf/Wpf/WpfSvgRendering.cs index e3db0cc5..d75ef444 100644 --- a/Source/SharpVectorRenderingWpf/Wpf/WpfSvgRendering.cs +++ b/Source/SharpVectorRenderingWpf/Wpf/WpfSvgRendering.cs @@ -120,24 +120,24 @@ public override void Render(WpfDrawingRenderer renderer) var settings = _context.Settings; - if (_isRoot && _drawGroup.ClipGeometry != null) - { - if (settings.IgnoreRootViewbox) - { - _drawGroup.ClipGeometry = null; - } - else if (settings.EnsureViewboxSize) - { - var bounds = _drawGroup.ClipGeometry.Bounds; - if (!bounds.IsEmpty && !bounds.Width.Equals(0) && !bounds.Height.Equals(0)) - { - using (var ctx = _drawGroup.Open()) - { - ctx.DrawRectangle(null, new Pen(Brushes.Transparent, 1), bounds); - } - } - } - } + //if (_isRoot && _drawGroup.ClipGeometry != null) + //{ + // if (settings.IgnoreRootViewbox) + // { + // _drawGroup.ClipGeometry = null; + // } + // else if (settings.EnsureViewboxSize) + // { + // var bounds = _drawGroup.ClipGeometry.Bounds; + // if (!bounds.IsEmpty && !bounds.Width.Equals(0) && !bounds.Height.Equals(0)) + // { + // using (var ctx = _drawGroup.Open()) + // { + // ctx.DrawRectangle(null, new Pen(Brushes.Transparent, 1), bounds); + // } + // } + // } + //} // Register this drawing with the Drawing-Document... // ...but not the root SVG object, since there is not point for that @@ -153,6 +153,42 @@ public override void AfterRender(WpfDrawingRenderer renderer) this.OnAfterRender(renderer); + var settings = _context.Settings; + + if (_isRoot) + { + if (settings.IgnoreRootViewbox) + { + if (_drawGroup.ClipGeometry != null) + { + _drawGroup.ClipGeometry = null; + } + } + else if (settings.EnsureViewboxSize) + { + Rect? clipRect = null; + if (_drawGroup.ClipGeometry != null) + { + clipRect = _drawGroup.ClipGeometry.Bounds; + } + else + { + clipRect = _drawGroup.Bounds; // this.GetViewbox(renderer); + } + if (clipRect != null) + { + Rect bounds = clipRect.Value; + if (!bounds.IsEmpty && !bounds.Width.Equals(0) && !bounds.Height.Equals(0)) + { + var drawing = new GeometryDrawing(null, new Pen(Brushes.Transparent, 1), + new RectangleGeometry(bounds)); + + _drawGroup.Children.Insert(0, drawing); + } + } + } + } + base.AfterRender(renderer); } @@ -239,6 +275,49 @@ private Transform FitToViewbox(WpfDrawingContext context, ISvgFitToViewBox fitTo return null; } + private Rect? GetViewbox(WpfDrawingRenderer renderer) + { + WpfDrawingContext context = renderer.Context; + + 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 null; + } + + Rect elmRect = new Rect(x, y, width, height); + + XmlNode parentNode = _svgElement.ParentNode; + + ISvgFitToViewBox fitToView = svgElm as ISvgFitToViewBox; + if (fitToView != null && fitToView.PreserveAspectRatio != 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; + } + } + } + } + + return elmRect; + } + private void ApplyViewBox(WpfDrawingRenderer renderer) { WpfDrawingContext context = renderer.Context;