Simplified rendering operation

This commit is contained in:
MarcinZiabek
2022-03-20 01:53:51 +01:00
parent f0cec8988a
commit 26fdbb714d
7 changed files with 41 additions and 44 deletions
+21 -1
View File
@@ -1,7 +1,9 @@
using QuestPDF.Fluent;
using Avalonia.Media;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
using QuestPDF.Previewer;
using Colors = QuestPDF.Helpers.Colors;
Document
.Create(container =>
@@ -48,5 +50,23 @@ Document
x.CurrentPageNumber();
});
});
container.Page(page =>
{
page.Size(PageSizes.A3);
page.Margin(2, Unit.Centimetre);
page.PageColor(Colors.White);
page.DefaultTextStyle(x => x.FontSize(20));
page.Content()
.PaddingVertical(1, Unit.Centimetre)
.Column(x =>
{
x.Spacing(20);
foreach (var i in Enumerable.Range(0, 10))
x.Item().Background(Colors.Grey.Lighten2).Height(80);
});
});
})
.ShowInPreviewer();
+6 -8
View File
@@ -9,8 +9,6 @@ using SkiaSharp;
namespace QuestPDF.Previewer
{
public record RenderedPageInfo(SKPicture Picture, Size Size);
internal class DocumentRenderer : INotifyPropertyChanged
{
public float PageSpacing { get; set; }
@@ -72,15 +70,15 @@ namespace QuestPDF.Previewer
{
var canvas = new PreviewerCanvas();
DocumentGenerator.RenderDocument(canvas, new SizeTrackingCanvas(), document, s =>
{
var width = s.PageSizes.Max(p => p.Width);
var height = s.PageSizes.Sum(p => p.Height) + ((s.PageSizes.Count - 1) * PageSpacing);
Bounds = new Size(width, height);
});
DocumentGenerator.RenderDocument(canvas, document);
var width = canvas.Pictures.Max(p => p.Size.Width);
var height = canvas.Pictures.Sum(p => p.Size.Height) + (canvas.Pictures.Count - 1) * PageSpacing;
Bounds = new Size(width, height);
foreach (var pages in Pages)
pages?.Picture?.Dispose();
Dispatcher.UIThread.Post(() =>
{
Pages.Clear();
+1
View File
@@ -23,6 +23,7 @@ namespace QuestPDF.Previewer
Document = Document
};
}
base.OnFrameworkInitializationCompleted();
}
}
+7 -5
View File
@@ -4,18 +4,19 @@ using SkiaSharp;
namespace QuestPDF.Previewer
{
public record RenderedPageInfo(SKPicture Picture, Size Size);
internal sealed class PreviewerCanvas : SkiaCanvasBase, IRenderingCanvas
{
private SKPictureRecorder? _currentRecorder;
private readonly List<RenderedPageInfo> _pictures = new();
public IReadOnlyList<RenderedPageInfo> Pictures => _pictures;
public ICollection<RenderedPageInfo> Pictures { get; } = new List<RenderedPageInfo>();
private Size? _currentSize;
public override void BeginDocument()
{
_pictures.Clear();
Pictures.Clear();
}
public override void BeginPage(Size size)
@@ -31,8 +32,9 @@ namespace QuestPDF.Previewer
public override void EndPage()
{
var picture = _currentRecorder?.EndRecording();
if (picture != null && _currentSize.HasValue)
_pictures.Add(new RenderedPageInfo(picture, _currentSize.Value));
Pictures.Add(new RenderedPageInfo(picture, _currentSize.Value));
_currentRecorder?.Dispose();
_currentRecorder = null;
+5 -5
View File
@@ -1,4 +1,4 @@
<FluentWindow xmlns="https://github.com/avaloniaui"
<previewer:FluentWindow xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
@@ -11,11 +11,11 @@
Background="{x:Null}"
x:Name="Window"
Title="QuestPDF Document Preview">
<FluentWindow.Styles>
<previewer:FluentWindow.Styles>
<Style Selector="TitleBar:fullscreen">
<Setter Property="Background" Value="#7F000000" />
<Setter Property="Background" Value="#F00" />
</Style>
</FluentWindow.Styles>
</previewer:FluentWindow.Styles>
<Panel>
<ExperimentalAcrylicBorder IsHitTestVisible="False">
@@ -57,4 +57,4 @@
</ScrollViewer>
</Grid>
</Panel>
</FluentWindow>
</previewer:FluentWindow>
+1 -9
View File
@@ -53,13 +53,6 @@ namespace QuestPDF.Drawing
internal static void RenderDocument<TCanvas>(TCanvas canvas, IDocument document)
where TCanvas : ICanvas, IRenderingCanvas
{
RenderDocument(canvas, new FreeCanvas(), document);
}
internal static void RenderDocument<TCanvas, TFreeCanvas>(TCanvas canvas, TFreeCanvas freeCanvas, IDocument document, Action<TFreeCanvas>? afterFirstPass = null)
where TCanvas : ICanvas, IRenderingCanvas
where TFreeCanvas : ICanvas, IRenderingCanvas
{
var container = new DocumentContainer();
document.Compose(container);
@@ -74,8 +67,7 @@ namespace QuestPDF.Drawing
if (metadata.ApplyCaching)
ApplyCaching(content);
RenderPass(pageContext, freeCanvas, content, metadata, debuggingState);
afterFirstPass?.Invoke(freeCanvas);
RenderPass(pageContext, new FreeCanvas(), content, metadata, debuggingState);
RenderPass(pageContext, canvas, content, metadata, debuggingState);
}
-16
View File
@@ -1,16 +0,0 @@
using System.Collections.Generic;
using QuestPDF.Infrastructure;
namespace QuestPDF.Drawing
{
internal class SizeTrackingCanvas : FreeCanvas
{
private readonly List<Size> _pageSizes = new();
public IReadOnlyList<Size> PageSizes => _pageSizes;
public override void BeginPage(Size size)
{
_pageSizes.Add(size);
}
}
}