95
Source/OxyPlot.SkiaSharp/JpegExporter.cs
Normal file
95
Source/OxyPlot.SkiaSharp/JpegExporter.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="JpegExporter.cs" company="OxyPlot">
|
||||
// Copyright (c) 2020 OxyPlot contributors
|
||||
// </copyright>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace OxyPlot.SkiaSharp
|
||||
{
|
||||
using global::SkiaSharp;
|
||||
using System.IO;
|
||||
|
||||
/// <summary>
|
||||
/// Provides functionality to export plots to jpeg using the SkiaSharp renderer.
|
||||
/// </summary>
|
||||
public class JpegExporter : IExporter
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the DPI.
|
||||
/// </summary>
|
||||
public float Dpi { get; set; } = 96;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the export height (in pixels).
|
||||
/// </summary>
|
||||
public int Height { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the export width (in pixels).
|
||||
/// </summary>
|
||||
public int Width { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the export quality (0-100).
|
||||
/// </summary>
|
||||
public int Quality { get; set; } = 90;
|
||||
|
||||
/// <summary>
|
||||
/// Exports the specified model to a file.
|
||||
/// </summary>
|
||||
/// <param name="model">The model.</param>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <param name="width">The width (points).</param>
|
||||
/// <param name="height">The height (points).</param>
|
||||
/// <param name="quality">The export quality (0-100).</param>
|
||||
/// <param name="dpi">The DPI (dots per inch).</param>
|
||||
public static void Export(IPlotModel model, string path, int width, int height, int quality, float dpi = 96)
|
||||
{
|
||||
using var stream = File.OpenWrite(path);
|
||||
Export(model, stream, width, height, quality, dpi);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Exports the specified model to a stream.
|
||||
/// </summary>
|
||||
/// <param name="model">The model.</param>
|
||||
/// <param name="stream">The output stream.</param>
|
||||
/// <param name="width">The width (points).</param>
|
||||
/// <param name="height">The height (points).</param>
|
||||
/// <param name="quality">The export quality (0-100).</param>
|
||||
/// <param name="dpi">The DPI (dots per inch).</param>
|
||||
public static void Export(IPlotModel model, Stream stream, int width, int height, int quality, float dpi = 96)
|
||||
{
|
||||
var exporter = new JpegExporter { Width = width, Height = height, Quality = quality, Dpi = dpi };
|
||||
exporter.Export(model, stream);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Export(IPlotModel model, Stream stream)
|
||||
{
|
||||
using var bitmap = new SKBitmap(this.Width, this.Height);
|
||||
|
||||
using (var canvas = new SKCanvas(bitmap))
|
||||
using (var context = new SkiaRenderContext { RenderTarget = RenderTarget.PixelGraphic, SkCanvas = canvas })
|
||||
{
|
||||
canvas.Clear(SKColors.White);
|
||||
var dpiScale = this.Dpi / 96;
|
||||
context.DpiScale = dpiScale;
|
||||
model.Update(true);
|
||||
var backgroundColor = model.Background;
|
||||
|
||||
// jpg doesn't support transparency
|
||||
if (!backgroundColor.IsVisible())
|
||||
{
|
||||
backgroundColor = OxyColors.White;
|
||||
}
|
||||
|
||||
canvas.Clear(backgroundColor.ToSKColor());
|
||||
model.Render(context, new OxyRect(0, 0, this.Width / dpiScale, this.Height / dpiScale));
|
||||
}
|
||||
|
||||
using var skStream = new SKManagedWStream(stream);
|
||||
bitmap.Encode(skStream, SKEncodedImageFormat.Jpeg, this.Quality);
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Source/OxyPlot.SkiaSharp/OxyPlot.SkiaSharp.csproj
Normal file
25
Source/OxyPlot.SkiaSharp/OxyPlot.SkiaSharp.csproj
Normal file
@@ -0,0 +1,25 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<LangVersion>8</LangVersion>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Copyright>Copyright (c) 2020 OxyPlot contributors</Copyright>
|
||||
<TargetFrameworks>net462;netstandard2.0</TargetFrameworks>
|
||||
<Authors>OxyPlot contributors</Authors>
|
||||
<Description>OxyPlot is a plotting library for .NET. This package provides rendering based on SkiaSharp.</Description>
|
||||
<PackageTags>plotting plot charting chart skiasharp skia pdf</PackageTags>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
<SignAssembly>true</SignAssembly>
|
||||
<AssemblyOriginatorKeyFile>OxyPlot.SkiaSharp.snk</AssemblyOriginatorKeyFile>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SkiaSharp" Version="2.88.3" />
|
||||
<PackageReference Include="SkiaSharp.HarfBuzz" Version="2.88.3" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="OxyPlot.SkiaSharp.snk" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\OxyPlot\OxyPlot.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
BIN
Source/OxyPlot.SkiaSharp/OxyPlot.SkiaSharp.snk
Normal file
BIN
Source/OxyPlot.SkiaSharp/OxyPlot.SkiaSharp.snk
Normal file
Binary file not shown.
71
Source/OxyPlot.SkiaSharp/PdfExporter.cs
Normal file
71
Source/OxyPlot.SkiaSharp/PdfExporter.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="PdfExporter.cs" company="OxyPlot">
|
||||
// Copyright (c) 2020 OxyPlot contributors
|
||||
// </copyright>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace OxyPlot.SkiaSharp
|
||||
{
|
||||
using global::SkiaSharp;
|
||||
using System.IO;
|
||||
|
||||
/// <summary>
|
||||
/// Provides functionality to export plots to pdf using the SkiaSharp renderer.
|
||||
/// </summary>
|
||||
public class PdfExporter : IExporter
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the export height (in points, where 1 point equals 1/72 inch).
|
||||
/// </summary>
|
||||
public float Height { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the export width (in points, where 1 point equals 1/72 inch).
|
||||
/// </summary>
|
||||
public float Width { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether text shaping should be used when rendering text.
|
||||
/// </summary>
|
||||
public bool UseTextShaping { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Exports the specified model to a file.
|
||||
/// </summary>
|
||||
/// <param name="model">The model.</param>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <param name="width">The width (points).</param>
|
||||
/// <param name="height">The height (points).</param>
|
||||
public static void Export(IPlotModel model, string path, float width, float height)
|
||||
{
|
||||
using var stream = File.OpenWrite(path);
|
||||
Export(model, stream, width, height);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Exports the specified model to a stream.
|
||||
/// </summary>
|
||||
/// <param name="model">The model.</param>
|
||||
/// <param name="stream">The output stream.</param>
|
||||
/// <param name="width">The width (points).</param>
|
||||
/// <param name="height">The height (points).</param>
|
||||
public static void Export(IPlotModel model, Stream stream, float width, float height)
|
||||
{
|
||||
var exporter = new PdfExporter { Width = width, Height = height };
|
||||
exporter.Export(model, stream);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
84
Source/OxyPlot.SkiaSharp/PngExporter.cs
Normal file
84
Source/OxyPlot.SkiaSharp/PngExporter.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="PngExporter.cs" company="OxyPlot">
|
||||
// Copyright (c) 2020 OxyPlot contributors
|
||||
// </copyright>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace OxyPlot.SkiaSharp
|
||||
{
|
||||
using global::SkiaSharp;
|
||||
using System.IO;
|
||||
|
||||
/// <summary>
|
||||
/// Provides functionality to export plots to png using the SkiaSharp renderer.
|
||||
/// </summary>
|
||||
public class PngExporter : IExporter
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the DPI.
|
||||
/// </summary>
|
||||
public float Dpi { get; set; } = 96;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the export height (in pixels).
|
||||
/// </summary>
|
||||
public int Height { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the export width (in pixels).
|
||||
/// </summary>
|
||||
public int Width { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether text shaping should be used when rendering text.
|
||||
/// </summary>
|
||||
public bool UseTextShaping { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Exports the specified model to a file.
|
||||
/// </summary>
|
||||
/// <param name="model">The model.</param>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <param name="width">The width (points).</param>
|
||||
/// <param name="height">The height (points).</param>
|
||||
/// <param name="dpi">The DPI (dots per inch).</param>
|
||||
public static void Export(IPlotModel model, string path, int width, int height, float dpi = 96)
|
||||
{
|
||||
using var stream = File.OpenWrite(path);
|
||||
Export(model, stream, width, height, dpi);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Exports the specified model to a stream.
|
||||
/// </summary>
|
||||
/// <param name="model">The model.</param>
|
||||
/// <param name="stream">The output stream.</param>
|
||||
/// <param name="width">The width (points).</param>
|
||||
/// <param name="height">The height (points).</param>
|
||||
/// <param name="dpi">The DPI (dots per inch).</param>
|
||||
public static void Export(IPlotModel model, Stream stream, int width, int height, float dpi = 96)
|
||||
{
|
||||
var exporter = new PngExporter { Width = width, Height = height, Dpi = dpi };
|
||||
exporter.Export(model, stream);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Export(IPlotModel model, Stream stream)
|
||||
{
|
||||
using var bitmap = new SKBitmap(this.Width, this.Height);
|
||||
|
||||
using (var canvas = new SKCanvas(bitmap))
|
||||
using (var context = new SkiaRenderContext { RenderTarget = RenderTarget.PixelGraphic, SkCanvas = canvas, UseTextShaping = this.UseTextShaping })
|
||||
{
|
||||
var dpiScale = this.Dpi / 96;
|
||||
context.DpiScale = dpiScale;
|
||||
model.Update(true);
|
||||
canvas.Clear(model.Background.ToSKColor());
|
||||
model.Render(context, new OxyRect(0, 0, this.Width / dpiScale, this.Height / dpiScale));
|
||||
}
|
||||
|
||||
using var skStream = new SKManagedWStream(stream);
|
||||
bitmap.Encode(skStream, SKEncodedImageFormat.Png, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
38
Source/OxyPlot.SkiaSharp/RenderTarget.cs
Normal file
38
Source/OxyPlot.SkiaSharp/RenderTarget.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="RenderTarget.cs" company="OxyPlot">
|
||||
// Copyright (c) 2020 OxyPlot contributors
|
||||
// </copyright>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace OxyPlot.SkiaSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a render target for <see cref="SkiaRenderContext"/>.
|
||||
/// </summary>
|
||||
public enum RenderTarget
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates that the <see cref="SkiaRenderContext"/> renders to a screen.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The render context may try to snap shapes to device pixels and will use sub-pixel text rendering.
|
||||
/// </remarks>
|
||||
Screen,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the <see cref="SkiaRenderContext"/> renders to a pixel graphic.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The render context may try to snap shapes to pixels, but will not use sub-pixel text rendering.
|
||||
/// </remarks>
|
||||
PixelGraphic,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the <see cref="SkiaRenderContext"/> renders to a vector graphic.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The render context will not use any rendering enhancements that are specific to pixel graphics.
|
||||
/// </remarks>
|
||||
VectorGraphic
|
||||
}
|
||||
}
|
||||
36
Source/OxyPlot.SkiaSharp/SkiaExtensions.cs
Normal file
36
Source/OxyPlot.SkiaSharp/SkiaExtensions.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="SkiaExtensions.cs" company="OxyPlot">
|
||||
// Copyright (c) 2020 OxyPlot contributors
|
||||
// </copyright>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace OxyPlot.SkiaSharp
|
||||
{
|
||||
using global::SkiaSharp;
|
||||
|
||||
/// <summary>
|
||||
/// Provides extension methods for conversion between SkiaSharp and oxyplot objects.
|
||||
/// </summary>
|
||||
public static class SkiaExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="OxyColor"/> to a <see cref="SKColor"/>;
|
||||
/// </summary>
|
||||
/// <param name="color">The <see cref="OxyColor"/>.</param>
|
||||
/// <returns>The <see cref="SKColor"/>.</returns>
|
||||
public static OxyColor ToOxyColor(this SKColor color)
|
||||
{
|
||||
return OxyColor.FromArgb(color.Alpha, color.Red, color.Green, color.Blue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="SKColor"/> to a <see cref="OxyColor"/>;
|
||||
/// </summary>
|
||||
/// <param name="color">The <see cref="SKColor"/>.</param>
|
||||
/// <returns>The <see cref="OxyColor"/>.</returns>
|
||||
public static SKColor ToSKColor(this OxyColor color)
|
||||
{
|
||||
return new SKColor(color.R, color.G, color.B, color.A);
|
||||
}
|
||||
}
|
||||
}
|
||||
1008
Source/OxyPlot.SkiaSharp/SkiaRenderContext.cs
Normal file
1008
Source/OxyPlot.SkiaSharp/SkiaRenderContext.cs
Normal file
File diff suppressed because it is too large
Load Diff
44
Source/OxyPlot.SkiaSharp/SvgExporter.cs
Normal file
44
Source/OxyPlot.SkiaSharp/SvgExporter.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="SvgExporter.cs" company="OxyPlot">
|
||||
// Copyright (c) 2020 OxyPlot contributors
|
||||
// </copyright>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace OxyPlot.SkiaSharp
|
||||
{
|
||||
using global::SkiaSharp;
|
||||
using System.IO;
|
||||
|
||||
/// <summary>
|
||||
/// Provides functionality to export plots to scalable vector graphics using the SkiaSharp SVG canvas.
|
||||
/// </summary>
|
||||
public class SvgExporter : IExporter
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the height (in user units) of the output area.
|
||||
/// </summary>
|
||||
public float Height { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the width (in user units) of the output area.
|
||||
/// </summary>
|
||||
public float Width { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Export(IPlotModel model, Stream stream)
|
||||
{
|
||||
using var skStream = new SKManagedWStream(stream);
|
||||
using var canvas = SKSvgCanvas.Create(new SKRect(0, 0, this.Width, this.Height), skStream);
|
||||
|
||||
if (!model.Background.IsInvisible())
|
||||
{
|
||||
canvas.Clear(model.Background.ToSKColor());
|
||||
}
|
||||
|
||||
// SVG export does not work with UseTextShaping=true. However SVG does text shaping by itself anyway, so we can just disable it
|
||||
using var context = new SkiaRenderContext { RenderTarget = RenderTarget.VectorGraphic, SkCanvas = canvas, UseTextShaping = false };
|
||||
model.Update(true);
|
||||
model.Render(context, new OxyRect(0, 0, this.Width, this.Height));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user