Browse Source

Improvement: when drawing content with the Canvas element, the library clips drawn content to the element's boundaries, preventing potential overflow when integrating with other libraries.

Marcin Ziąbek 1 year ago
parent
commit
1a984c0d56

+ 35 - 1
Source/QuestPDF.Examples/ChartExamples.cs

@@ -1,11 +1,14 @@
+using System;
 using System.Linq;
 using System.Linq;
 using NUnit.Framework;
 using NUnit.Framework;
 using QuestPDF.Examples.Engine;
 using QuestPDF.Examples.Engine;
 using QuestPDF.Fluent;
 using QuestPDF.Fluent;
-using QuestPDF.Helpers;
 using QuestPDF.Infrastructure;
 using QuestPDF.Infrastructure;
 using Microcharts;
 using Microcharts;
+using ScottPlot;
 using SkiaSharp;
 using SkiaSharp;
+using Colors = QuestPDF.Helpers.Colors;
+using Orientation = Microcharts.Orientation;
 
 
 namespace QuestPDF.Examples
 namespace QuestPDF.Examples
 {    
 {    
@@ -84,5 +87,36 @@ namespace QuestPDF.Examples
                         });
                         });
                 });
                 });
         }
         }
+        
+        [Test]
+        public void ScottPlotChart()
+        {
+            RenderingTest
+                .Create()
+                .PageSize(400, 300)
+                .ProduceImages()
+                .ShowResults()
+                .Render(container =>
+                {
+                    container
+                        .Background(Colors.White)
+                        .Padding(25)
+                        .Canvas((canvas, availableSpace) =>
+                        {
+                            var points = Enumerable
+                                .Range(0, 100)
+                                .Select(x => new Coordinates(x, Math.Sin(x / 10f)))
+                                .ToArray();
+                            
+                            using var plot = new Plot();
+                            plot.Add.Scatter(points, Color.FromHex("#009688"));
+
+                            canvas.Save();
+                            canvas.ClipRect(new SKRect(0, 0, availableSpace.Width, availableSpace.Height));
+                            plot.Render(canvas, (int)availableSpace.Width, (int)availableSpace.Height);
+                            canvas.Restore();
+                        });
+                });
+        }
     }
     }
 }
 }

+ 1 - 0
Source/QuestPDF.Examples/QuestPDF.Examples.csproj

@@ -12,6 +12,7 @@
         <PackageReference Include="nunit" Version="4.0.1" />
         <PackageReference Include="nunit" Version="4.0.1" />
         <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
         <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
         <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
         <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
+        <PackageReference Include="ScottPlot" Version="5.0.21" />
         <PackageReference Include="SkiaSharp" Version="2.88.7" />
         <PackageReference Include="SkiaSharp" Version="2.88.7" />
         <PackageReference Include="Svg.Skia" Version="1.0.0.10" />
         <PackageReference Include="Svg.Skia" Version="1.0.0.10" />
     </ItemGroup>
     </ItemGroup>

+ 5 - 0
Source/QuestPDF/Elements/Canvas.cs

@@ -26,7 +26,12 @@ namespace QuestPDF.Elements
                 return;
                 return;
 
 
             var originalMatrix = skiaCanvas.TotalMatrix;
             var originalMatrix = skiaCanvas.TotalMatrix;
+            skiaCanvas.Save();
+            
+            skiaCanvas.ClipRect(new SKRect(0, 0, availableSpace.Width, availableSpace.Height));
             Handler.Invoke(skiaCanvas, availableSpace);
             Handler.Invoke(skiaCanvas, availableSpace);
+            
+            skiaCanvas.Restore();
             skiaCanvas.SetMatrix(originalMatrix);
             skiaCanvas.SetMatrix(originalMatrix);
         }
         }
     }
     }

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

@@ -22,3 +22,6 @@ Version 2023.12.3
 
 
 Version 2023.12.4
 Version 2023.12.4
 - Fixed: the TextStyle.LetterSpacing property was not documented correctly, and the FluentAPI code prevented its correct use.
 - Fixed: the TextStyle.LetterSpacing property was not documented correctly, and the FluentAPI code prevented its correct use.
+
+Version 2023.12.5
+- Improvement: when drawing content with the Canvas element, the library clips drawn content to the element's boundaries, preventing potential overflow when integrating with other libraries.