Skip to content

Commit f5e41f4

Browse files
authored
Merge pull request #71 from TSerious/master
Added new eSizeType.ViewBoxToSizeNoStretch
2 parents da98040 + 2ddaae6 commit f5e41f4

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

Source/SVGImage/SVG/SVGImage.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Windows.Resources;
1313

1414
using DotNetProjects.SVGImage.SVG.FileLoaders;
15+
using System.Runtime.CompilerServices;
1516

1617
namespace SVGImage.SVG
1718
{
@@ -47,6 +48,11 @@ public enum eSizeType
4748
/// maximum size for the control, the control is set to maximum size and the image is scaled.
4849
/// </summary>
4950
SizeToContent,
51+
/// <summary>
52+
/// Not the content of the image but its viewbox is scaled to fit the control without any stretching.
53+
/// Either X or Y direction will be scaled to fill the entire width or height.
54+
/// </summary>
55+
ViewBoxToSizeNoStretch,
5056
}
5157

5258
private Uri _baseUri;
@@ -443,6 +449,11 @@ void RecalcImage()
443449
this.SizeToContentNoStretch(this.HorizontalContentAlignment, this.VerticalContentAlignment);
444450
return;
445451
}
452+
if (this.SizeType == eSizeType.ViewBoxToSizeNoStretch)
453+
{
454+
this.SizeToViewBoxNoStretch(this.HorizontalContentAlignment, this.VerticalContentAlignment);
455+
return;
456+
}
446457
if (this.SizeType == eSizeType.ContentToSizeStretch)
447458
{
448459
double xscale = this.ActualWidth / r.Width;
@@ -518,6 +529,62 @@ void SizeToContentNoStretch(HorizontalAlignment hAlignment, VerticalAlignment vA
518529
}
519530
}
520531

532+
void SizeToViewBoxNoStretch(HorizontalAlignment hAlignment, VerticalAlignment vAlignment)
533+
{
534+
if (!this.SVG.ViewBox.HasValue)
535+
{
536+
SizeToContentNoStretch(hAlignment, vAlignment);
537+
return;
538+
}
539+
540+
Rect r = this.m_drawing.Bounds;
541+
542+
if (this.SVG.ViewBox.HasValue)
543+
{
544+
r = this.SVG.ViewBox.Value;
545+
}
546+
547+
double xscale = this.ActualWidth / r.Width;
548+
double yscale = this.ActualHeight / r.Height;
549+
double scale = xscale;
550+
if (scale > yscale)
551+
scale = yscale;
552+
553+
this.m_scaleTransform.CenterX = r.Left;
554+
this.m_scaleTransform.CenterY = r.Top;
555+
this.m_scaleTransform.ScaleX = scale;
556+
this.m_scaleTransform.ScaleY = scale;
557+
558+
this.m_offsetTransform.X = -r.Left;
559+
if (scale < xscale)
560+
{
561+
switch (this.HorizontalContentAlignment)
562+
{
563+
case System.Windows.HorizontalAlignment.Center:
564+
double width = r.Width * scale;
565+
this.m_offsetTransform.X = this.ActualWidth / 2 - width / 2 - r.Left;
566+
break;
567+
case System.Windows.HorizontalAlignment.Right:
568+
this.m_offsetTransform.X = this.ActualWidth - r.Right * scale;
569+
break;
570+
}
571+
}
572+
this.m_offsetTransform.Y = -r.Top;
573+
if (scale < yscale)
574+
{
575+
switch (this.VerticalContentAlignment)
576+
{
577+
case System.Windows.VerticalAlignment.Center:
578+
double height = r.Height * scale;
579+
this.m_offsetTransform.Y = this.ActualHeight / 2 - height / 2 - r.Top;
580+
break;
581+
case System.Windows.VerticalAlignment.Bottom:
582+
this.m_offsetTransform.Y = this.ActualHeight - r.Height * scale - r.Top;
583+
break;
584+
}
585+
}
586+
}
587+
521588
Uri ResolveUri(Uri svgSource)
522589
{
523590
if (svgSource == null)

0 commit comments

Comments
 (0)