// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) 2020 OxyPlot contributors
//
// --------------------------------------------------------------------------------------------------------------------
namespace OxyPlot.SkiaSharp
{
using global::SkiaSharp;
using System.IO;
///
/// Provides functionality to export plots to pdf using the SkiaSharp renderer.
///
public class PdfExporter : IExporter
{
///
/// Gets or sets the export height (in points, where 1 point equals 1/72 inch).
///
public float Height { get; set; }
///
/// Gets or sets the export width (in points, where 1 point equals 1/72 inch).
///
public float Width { get; set; }
///
/// Gets or sets a value indicating whether text shaping should be used when rendering text.
///
public bool UseTextShaping { get; set; } = true;
///
/// Exports the specified model to a file.
///
/// The model.
/// The path.
/// The width (points).
/// The height (points).
public static void Export(IPlotModel model, string path, float width, float height)
{
using var stream = File.OpenWrite(path);
Export(model, stream, width, height);
}
///
/// Exports the specified model to a stream.
///
/// The model.
/// The output stream.
/// The width (points).
/// The height (points).
public static void Export(IPlotModel model, Stream stream, float width, float height)
{
var exporter = new PdfExporter { Width = width, Height = height };
exporter.Export(model, stream);
}
///
public void Export(IPlotModel model, Stream stream)
{
using var document = SKDocument.CreatePdf(stream);
using var pdfCanvas = document.BeginPage(this.Width, this.Height);
using var context = new SkiaRenderContext { RenderTarget = RenderTarget.VectorGraphic, SkCanvas = pdfCanvas, UseTextShaping = this.UseTextShaping };
const float dpiScale = 72f / 96;
context.DpiScale = dpiScale;
model.Update(true);
pdfCanvas.Clear(model.Background.ToSKColor());
model.Render(context, new OxyRect(0, 0, this.Width / dpiScale, this.Height / dpiScale));
}
}
}