|
12 | 12 | using System.Windows.Resources; |
13 | 13 |
|
14 | 14 | using DotNetProjects.SVGImage.SVG.FileLoaders; |
| 15 | +using System.Runtime.CompilerServices; |
15 | 16 |
|
16 | 17 | namespace SVGImage.SVG |
17 | 18 | { |
@@ -47,6 +48,11 @@ public enum eSizeType |
47 | 48 | /// maximum size for the control, the control is set to maximum size and the image is scaled. |
48 | 49 | /// </summary> |
49 | 50 | 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, |
50 | 56 | } |
51 | 57 |
|
52 | 58 | private Uri _baseUri; |
@@ -443,6 +449,11 @@ void RecalcImage() |
443 | 449 | this.SizeToContentNoStretch(this.HorizontalContentAlignment, this.VerticalContentAlignment); |
444 | 450 | return; |
445 | 451 | } |
| 452 | + if (this.SizeType == eSizeType.ViewBoxToSizeNoStretch) |
| 453 | + { |
| 454 | + this.SizeToViewBoxNoStretch(this.HorizontalContentAlignment, this.VerticalContentAlignment); |
| 455 | + return; |
| 456 | + } |
446 | 457 | if (this.SizeType == eSizeType.ContentToSizeStretch) |
447 | 458 | { |
448 | 459 | double xscale = this.ActualWidth / r.Width; |
@@ -518,6 +529,62 @@ void SizeToContentNoStretch(HorizontalAlignment hAlignment, VerticalAlignment vA |
518 | 529 | } |
519 | 530 | } |
520 | 531 |
|
| 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 | + |
521 | 588 | Uri ResolveUri(Uri svgSource) |
522 | 589 | { |
523 | 590 | if (svgSource == null) |
|
0 commit comments