浏览代码

Improved the default scaling behavior of Image and SvgImage elements when they are direct descendants of the Constrained element (Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight).

Marcin Ziąbek 1 年之前
父节点
当前提交
aa24992b2c

+ 24 - 2
Source/QuestPDF/Fluent/ImageExtensions.cs

@@ -115,7 +115,7 @@ namespace QuestPDF.Fluent
             return this;
         }
         
-        private ImageDescriptor SetAspectRatio(AspectRatioOption option)
+        internal ImageDescriptor SetAspectRatio(AspectRatioOption option)
         {
             AspectRatioElement.Ratio = ImageAspectRatio;
             AspectRatioElement.Option = option;
@@ -185,7 +185,29 @@ namespace QuestPDF.Fluent
             };
             
             parent.Element(aspectRationElement);
-            return new ImageDescriptor(imageElement, aspectRationElement).FitWidth();
+
+            var bestScalingOption = GetBestAspectRatioOptionFromParent(parent);
+            return new ImageDescriptor(imageElement, aspectRationElement).SetAspectRatio(bestScalingOption);
+        }
+        
+        internal static AspectRatioOption GetBestAspectRatioOptionFromParent(IContainer container)
+        {
+            if (container is not Constrained constrained)
+                return AspectRatioOption.FitWidth;
+
+            var hasWidthConstraint = constrained.MinWidth is not null || constrained.MaxWidth is not null;
+            var hasHeightConstraint = constrained.MinHeight is not null || constrained.MaxHeight is not null;
+                
+            if (hasWidthConstraint && hasHeightConstraint)
+                return AspectRatioOption.FitArea;
+                
+            if (hasWidthConstraint)
+                return AspectRatioOption.FitWidth;
+                
+            if (hasHeightConstraint)
+                return AspectRatioOption.FitHeight;
+                
+            return AspectRatioOption.FitWidth;
         }
         
         /// <summary>

+ 4 - 2
Source/QuestPDF/Fluent/SvgExtensions.cs

@@ -57,7 +57,7 @@ public class SvgImageDescriptor
         return SetAspectRatio(AspectRatioOption.FitArea);
     }
 
-    private SvgImageDescriptor SetAspectRatio(AspectRatioOption option)
+    internal SvgImageDescriptor SetAspectRatio(AspectRatioOption option)
     {
         AspectRatioElement.Ratio = ImageAspectRatio;
         AspectRatioElement.Option = option;
@@ -112,7 +112,9 @@ public static class SvgExtensions
         };
             
         parent.Element(aspectRationElement);
-        return new SvgImageDescriptor(imageElement, aspectRationElement).FitWidth();
+        
+        var bestScalingOption = ImageExtensions.GetBestAspectRatioOptionFromParent(parent);
+        return new SvgImageDescriptor(imageElement, aspectRationElement).SetAspectRatio(bestScalingOption);
     }
     
     /// <summary>

+ 3 - 1
Source/QuestPDF/Resources/ReleaseNotes.txt

@@ -47,4 +47,6 @@ Version 2024.3.2
 Version 2024.3.3
 - Fixed: enhanced the handling of the TextStyle.LineHeight modifier for better consistency with industry-standard software, as well as adjusted the default value.
 - Fixed: when the UseEnvironmentFonts setting is enabled, the Lato font was not being properly registered. This issue could lead to runtime exceptions in specific minimal environments.
-- Enhanced handling of texts containing the carriage return character '\r' within the custom page format setting. 
+- Enhanced handling of texts containing the carriage return character '\r' within the custom page format setting.
+- Improved the default scaling behavior of Image and SvgImage elements when they are direct descendants of the Constrained element (Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight).
+