Added ImageDpi API
This commit is contained in:
@@ -10,6 +10,7 @@ using QuestPDF.Elements.Text.Items;
|
||||
using QuestPDF.Fluent;
|
||||
using QuestPDF.Helpers;
|
||||
using QuestPDF.Infrastructure;
|
||||
using Image = QuestPDF.Elements.Image;
|
||||
|
||||
namespace QuestPDF.Drawing
|
||||
{
|
||||
@@ -21,7 +22,7 @@ namespace QuestPDF.Drawing
|
||||
|
||||
var metadata = document.GetMetadata();
|
||||
var canvas = new PdfCanvas(stream, metadata);
|
||||
RenderDocument(canvas, document);
|
||||
RenderDocument(canvas, document, metadata);
|
||||
}
|
||||
|
||||
internal static void GenerateXps(Stream stream, IDocument document)
|
||||
@@ -30,7 +31,7 @@ namespace QuestPDF.Drawing
|
||||
|
||||
var metadata = document.GetMetadata();
|
||||
var canvas = new XpsCanvas(stream, metadata);
|
||||
RenderDocument(canvas, document);
|
||||
RenderDocument(canvas, document, metadata);
|
||||
}
|
||||
|
||||
private static void CheckIfStreamIsCompatible(Stream stream)
|
||||
@@ -46,24 +47,26 @@ namespace QuestPDF.Drawing
|
||||
{
|
||||
var metadata = document.GetMetadata();
|
||||
var canvas = new ImageCanvas(metadata);
|
||||
RenderDocument(canvas, document);
|
||||
RenderDocument(canvas, document, metadata);
|
||||
|
||||
return canvas.Images;
|
||||
}
|
||||
|
||||
internal static ICollection<PreviewerPicture> GeneratePreviewerPictures(IDocument document)
|
||||
{
|
||||
var metadata = document.GetMetadata();
|
||||
var canvas = new SkiaPictureCanvas();
|
||||
RenderDocument(canvas, document);
|
||||
RenderDocument(canvas, document, metadata);
|
||||
return canvas.Pictures;
|
||||
}
|
||||
|
||||
internal static void RenderDocument<TCanvas>(TCanvas canvas, IDocument document)
|
||||
internal static void RenderDocument<TCanvas>(TCanvas canvas, IDocument document, DocumentMetadata metadata)
|
||||
where TCanvas : ICanvas, IRenderingCanvas
|
||||
{
|
||||
var container = new DocumentContainer();
|
||||
document.Compose(container);
|
||||
var content = container.Compose();
|
||||
ApplyDefaultImageDpi(content, metadata.RasterDpi);
|
||||
ApplyDefaultTextStyle(content, TextStyle.LibraryDefault);
|
||||
ApplyContentDirection(content, ContentDirection.LeftToRight);
|
||||
|
||||
@@ -173,6 +176,15 @@ namespace QuestPDF.Drawing
|
||||
|
||||
return debuggingState;
|
||||
}
|
||||
|
||||
internal static void ApplyDefaultImageDpi(this Element? content, int targetDpi)
|
||||
{
|
||||
content.VisitChildren(x =>
|
||||
{
|
||||
if (x is Image { DocumentImage: { } image })
|
||||
image.TargetDpi ??= targetDpi;
|
||||
});
|
||||
}
|
||||
|
||||
internal static void ApplyContentDirection(this Element? content, ContentDirection direction)
|
||||
{
|
||||
|
||||
@@ -5,8 +5,10 @@ namespace QuestPDF.Drawing
|
||||
{
|
||||
public class DocumentMetadata
|
||||
{
|
||||
public const int DefaultPdfDpi = 72;
|
||||
|
||||
public int ImageQuality { get; set; } = 101;
|
||||
public int RasterDpi { get; set; } = 72;
|
||||
public int RasterDpi { get; set; } = DefaultPdfDpi;
|
||||
public bool PdfA { get; set; }
|
||||
|
||||
public string? Title { get; set; }
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using QuestPDF.Drawing;
|
||||
using QuestPDF.Drawing.Exceptions;
|
||||
using SkiaSharp;
|
||||
|
||||
@@ -10,6 +11,8 @@ namespace QuestPDF.Infrastructure
|
||||
{
|
||||
private SKImage SkImage { get; }
|
||||
internal List<(Size size, SKImage image)>? ScaledImageCache { get; }
|
||||
|
||||
public int? TargetDpi { get; set; }
|
||||
internal bool IsDocumentScoped { get; set; }
|
||||
|
||||
public int Width => SkImage.Width;
|
||||
@@ -20,12 +23,6 @@ namespace QuestPDF.Infrastructure
|
||||
SkImage = image;
|
||||
}
|
||||
|
||||
public Image DisposeAfterDocumentGeneration()
|
||||
{
|
||||
IsDocumentScoped = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
SkImage.Dispose();
|
||||
@@ -90,5 +87,21 @@ namespace QuestPDF.Infrastructure
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region configuration API
|
||||
|
||||
public Image DisposeAfterDocumentGeneration()
|
||||
{
|
||||
IsDocumentScoped = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Image WithTargetDpi(int dpi = DocumentMetadata.DefaultPdfDpi)
|
||||
{
|
||||
TargetDpi = dpi;
|
||||
return this;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user