Browse Source

Code refactorization

Marcin Ziąbek 4 years ago
parent
commit
5deea91fe4

+ 0 - 3
QuestPDF.ReportSample/DataSource.cs

@@ -7,9 +7,6 @@ namespace QuestPDF.ReportSample
 {
 {
     public static class DataSource
     public static class DataSource
     {
     {
-        public static int SectionCounter { get; set; }
-        public static int FieldCounter { get; set; }
-        
         public static ReportModel GetReport()
         public static ReportModel GetReport()
         {
         {
             return new ReportModel
             return new ReportModel

+ 2 - 2
QuestPDF.ReportSample/Tests.cs

@@ -57,8 +57,8 @@ namespace QuestPDF.ReportSample
             Console.WriteLine($"Time per document: {performance:N} ms");
             Console.WriteLine($"Time per document: {performance:N} ms");
             Console.WriteLine($"Documents per second: {speed:N} d/s");
             Console.WriteLine($"Documents per second: {speed:N} d/s");
 
 
-            //if (speed < performanceTarget)
-            //    throw new Exception("Rendering algorithm is too slow.");
+            if (speed < performanceTarget)
+                throw new Exception("Rendering algorithm is too slow.");
         }
         }
     }
     }
 }
 }

+ 11 - 14
QuestPDF/Elements/Constrained.cs

@@ -22,8 +22,8 @@ namespace QuestPDF.Elements
                 return new Wrap();
                 return new Wrap();
             
             
             var available = new Size(
             var available = new Size(
-                MathHelpers.Min(MaxWidth, availableSpace.Width),
-                MathHelpers.Min(MaxHeight, availableSpace.Height));
+                Min(MaxWidth, availableSpace.Width),
+                Min(MaxHeight, availableSpace.Height));
 
 
             var measurement = Child?.Measure(available) ?? new FullRender(Size.Zero);
             var measurement = Child?.Measure(available) ?? new FullRender(Size.Zero);
             var size = measurement as Size;
             var size = measurement as Size;
@@ -32,8 +32,8 @@ namespace QuestPDF.Elements
                 return new Wrap();
                 return new Wrap();
             
             
             var actualSize = new Size(
             var actualSize = new Size(
-                MathHelpers.Max(MinWidth, size.Width),
-                MathHelpers.Max(MinHeight, size.Height));
+                Max(MinWidth, size.Width),
+                Max(MinHeight, size.Height));
             
             
             if (size is FullRender)
             if (size is FullRender)
                 return new FullRender(actualSize);
                 return new FullRender(actualSize);
@@ -47,23 +47,20 @@ namespace QuestPDF.Elements
         internal override void Draw(Size availableSpace)
         internal override void Draw(Size availableSpace)
         {
         {
             var available = new Size(
             var available = new Size(
-                MathHelpers.Min(MaxWidth, availableSpace.Width),
-                MathHelpers.Min(MaxHeight, availableSpace.Height));
+                Min(MaxWidth, availableSpace.Width),
+                Min(MaxHeight, availableSpace.Height));
             
             
             Child?.Draw(available);
             Child?.Draw(available);
         }
         }
-    }
-    
-    static class MathHelpers
-    {
-        public static float Min(params float?[] values)
+        
+        private static float Min(float? x, float y)
         {
         {
-            return values.Where(x => x.HasValue).Min().Value;
+            return x.HasValue ? Math.Min(x.Value, y) : y; 
         }
         }
         
         
-        public static float Max(params float?[] values)
+        private static float Max(float? x, float y)
         {
         {
-            return values.Where(x => x.HasValue).Max().Value;
+            return x.HasValue ? Math.Max(x.Value, y) : y;
         }
         }
     }
     }
 }
 }

+ 3 - 3
QuestPDF/Infrastructure/Position.cs

@@ -2,10 +2,10 @@
 {
 {
     internal class Position
     internal class Position
     {
     {
-        public float X { get; set; }
-        public float Y { get; set; }
+        public float X { get; }
+        public float Y { get; }
 
 
-        public static Position Zero => new Position(0, 0);
+        public static Position Zero { get; } = new Position(0, 0);
         
         
         public Position(float x, float y)
         public Position(float x, float y)
         {
         {

+ 2 - 23
QuestPDF/Infrastructure/Size.cs

@@ -7,8 +7,8 @@
         public float Width { get; }
         public float Width { get; }
         public float Height { get; }
         public float Height { get; }
         
         
-        public static Size Zero => new Size(0, 0);
-        public static Size Max => new Size(14_400, 14_400);
+        public static Size Zero { get; } = new Size(0, 0);
+        public static Size Max { get; } = new Size(14_400, 14_400);
 
 
         public Size(float width, float height)
         public Size(float width, float height)
         {
         {
@@ -17,26 +17,5 @@
         }
         }
         
         
         public override string ToString() => $"(W: {Width}, H: {Height})";
         public override string ToString() => $"(W: {Width}, H: {Height})";
-        
-        protected bool Equals(Size other)
-        {
-            return Width.Equals(other.Width) && Height.Equals(other.Height);
-        }
-
-        public override bool Equals(object? obj)
-        {
-            if (ReferenceEquals(null, obj)) return false;
-            if (ReferenceEquals(this, obj)) return true;
-            if (obj.GetType() != this.GetType()) return false;
-            return Equals((Size) obj);
-        }
-
-        public override int GetHashCode()
-        {
-            unchecked
-            {
-                return (Width.GetHashCode() * 397) ^ Height.GetHashCode();
-            }
-        }
     }
     }
 }
 }

+ 2 - 4
QuestPDF/Infrastructure/TextStyle.cs

@@ -32,9 +32,8 @@ namespace QuestPDF.Infrastructure
             HasUnderline = false
             HasUnderline = false
         };
         };
 
 
-        private static TextStyle DefaultTextStyleCache = new TextStyle();
-        public static TextStyle Default => DefaultTextStyleCache;
-
+        public static TextStyle Default => new TextStyle();
+        
         internal void ApplyGlobalStyle(TextStyle globalStyle)
         internal void ApplyGlobalStyle(TextStyle globalStyle)
         {
         {
             if (HasGlobalStyleApplied)
             if (HasGlobalStyleApplied)
@@ -43,7 +42,6 @@ namespace QuestPDF.Infrastructure
             HasGlobalStyleApplied = true;
             HasGlobalStyleApplied = true;
 
 
             ApplyParentStyle(globalStyle);
             ApplyParentStyle(globalStyle);
-            
             Key ??= $"{Color}|{BackgroundColor}|{FontType}|{Size}|{LineHeight}|{FontWeight}|{IsItalic}|{HasStrikethrough}|{HasUnderline}";
             Key ??= $"{Color}|{BackgroundColor}|{FontType}|{Size}|{LineHeight}|{FontWeight}|{IsItalic}|{HasStrikethrough}|{HasUnderline}";
         }
         }