SvgExporter.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="SvgExporter.cs" company="OxyPlot">
  3. // Copyright (c) 2020 OxyPlot contributors
  4. // </copyright>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. namespace OxyPlot.SkiaSharp
  7. {
  8. using global::SkiaSharp;
  9. using System.IO;
  10. /// <summary>
  11. /// Provides functionality to export plots to scalable vector graphics using the SkiaSharp SVG canvas.
  12. /// </summary>
  13. public class SvgExporter : IExporter
  14. {
  15. /// <summary>
  16. /// Gets or sets the height (in user units) of the output area.
  17. /// </summary>
  18. public float Height { get; set; }
  19. /// <summary>
  20. /// Gets or sets the width (in user units) of the output area.
  21. /// </summary>
  22. public float Width { get; set; }
  23. /// <inheritdoc/>
  24. public void Export(IPlotModel model, Stream stream)
  25. {
  26. using var skStream = new SKManagedWStream(stream);
  27. using var canvas = SKSvgCanvas.Create(new SKRect(0, 0, this.Width, this.Height), skStream);
  28. if (!model.Background.IsInvisible())
  29. {
  30. canvas.Clear(model.Background.ToSKColor());
  31. }
  32. // SVG export does not work with UseTextShaping=true. However SVG does text shaping by itself anyway, so we can just disable it
  33. using var context = new SkiaRenderContext { RenderTarget = RenderTarget.VectorGraphic, SkCanvas = canvas, UseTextShaping = false };
  34. model.Update(true);
  35. model.Render(context, new OxyRect(0, 0, this.Width, this.Height));
  36. }
  37. }
  38. }