|
@@ -1,4 +1,5 @@
|
|
|
using System;
|
|
using System;
|
|
|
|
|
+using System.Collections.Generic;
|
|
|
using System.IO;
|
|
using System.IO;
|
|
|
using QuestPDF.Drawing.Exceptions;
|
|
using QuestPDF.Drawing.Exceptions;
|
|
|
using SkiaSharp;
|
|
using SkiaSharp;
|
|
@@ -7,7 +8,8 @@ namespace QuestPDF.Infrastructure
|
|
|
{
|
|
{
|
|
|
public class Image : IDisposable
|
|
public class Image : IDisposable
|
|
|
{
|
|
{
|
|
|
- internal SKImage SkImage { get; }
|
|
|
|
|
|
|
+ private SKImage SkImage { get; }
|
|
|
|
|
+ internal List<(Size size, SKImage image)>? ScaledImageCache { get; }
|
|
|
internal bool IsDocumentScoped { get; set; }
|
|
internal bool IsDocumentScoped { get; set; }
|
|
|
|
|
|
|
|
public int Width => SkImage.Width;
|
|
public int Width => SkImage.Width;
|
|
@@ -27,6 +29,34 @@ namespace QuestPDF.Infrastructure
|
|
|
public void Dispose()
|
|
public void Dispose()
|
|
|
{
|
|
{
|
|
|
SkImage.Dispose();
|
|
SkImage.Dispose();
|
|
|
|
|
+ ScaledImageCache?.ForEach(x => x.image.Dispose());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ internal SKImage GetVersionOfSize(Size size)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (size.Width > Width || size.Height > Height)
|
|
|
|
|
+ return SkImage;
|
|
|
|
|
+
|
|
|
|
|
+ var target = SKImage.Create(new SKImageInfo((int)size.Width, (int)size.Height));
|
|
|
|
|
+ SkImage.ScalePixels(target.PeekPixels(), SKFilterQuality.High);
|
|
|
|
|
+
|
|
|
|
|
+ return target;
|
|
|
|
|
+
|
|
|
|
|
+ // bool ShouldApplyScaling()
|
|
|
|
|
+ // {
|
|
|
|
|
+ // const float tolerance = 1.1f;
|
|
|
|
|
+ //
|
|
|
|
|
+ // if (width > Width / tolerance || height > Height / tolerance)
|
|
|
|
|
+ // return false;
|
|
|
|
|
+ //
|
|
|
|
|
+ // SkImage.sca
|
|
|
|
|
+ // }
|
|
|
|
|
+
|
|
|
|
|
+ static bool HasSimilarSize(Size a, Size b, float tolerance)
|
|
|
|
|
+ {
|
|
|
|
|
+ return (a.Width / b.Width > 1 / tolerance && a.Width / b.Width < tolerance) ||
|
|
|
|
|
+ (a.Height / b.Height > 1 / tolerance && a.Height / b.Height < tolerance);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#region public constructors
|
|
#region public constructors
|