Fixed SvgContainer support for the .greenshot file

This commit is contained in:
Robin Krom 2023-03-27 22:43:03 +02:00
parent 099e656963
commit f862e79485
4 changed files with 116 additions and 95 deletions

View file

@ -21,6 +21,7 @@
using System;
using System.Drawing;
using System.IO;
using Dapplo.Windows.Common.Structs;
using Greenshot.Base.Core;
using Greenshot.Base.Interfaces;
@ -34,14 +35,32 @@ namespace Greenshot.Editor.Drawing
[Serializable]
public class SvgContainer : VectorGraphicsContainer
{
private readonly SvgDocument _svgDocument;
private MemoryStream _svgContent;
public SvgContainer(SvgDocument svgDocument, ISurface parent) : base(parent)
[NonSerialized]
private SvgDocument _svgDocument;
public SvgContainer(Stream stream, ISurface parent) : base(parent)
{
_svgDocument = svgDocument;
Size = new Size((int)svgDocument.Width, (int)svgDocument.Height);
_svgContent = new MemoryStream();
stream.CopyTo(_svgContent);
Init();
Size = new Size((int)_svgDocument.Width, (int)_svgDocument.Height);
}
protected override void Init()
{
base.Init();
// Do nothing when there is no content
if (_svgContent == null)
{
return;
}
_svgContent.Position = 0;
_svgDocument = SvgDocument.Open<SvgDocument>(_svgContent);
}
protected override Image ComputeBitmap()
{
//var image = ImageHelper.CreateEmpty(Width, Height, PixelFormat.Format32bppArgb, Color.Transparent);

View file

@ -47,7 +47,8 @@ namespace Greenshot.Editor.Drawing
/// This is the cached version of the bitmap, pre-rendered to save performance
/// Do not serialized, it can be rebuild with other information.
/// </summary>
[NonSerialized] private Image _cachedImage;
[NonSerialized]
private Image _cachedImage;
/// <summary>
/// Constructor takes care of calling Init

View file

@ -1,89 +1,89 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2021 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: https://getgreenshot.org/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using Greenshot.Base.Interfaces;
using Greenshot.Base.Interfaces.Drawing;
using Greenshot.Base.Interfaces.Plugin;
using Greenshot.Editor.Drawing;
using log4net;
using Svg;
namespace Greenshot.Editor.FileFormatHandlers
{
/// <summary>
/// This handled the loading of SVG images to the editor
/// </summary>
public class SvgFileFormatHandler : AbstractFileFormatHandler, IFileFormatHandler
{
private static readonly ILog Log = LogManager.GetLogger(typeof(SvgFileFormatHandler));
private readonly IReadOnlyCollection<string> _ourExtensions = new[] { ".svg" };
public SvgFileFormatHandler()
{
SupportedExtensions[FileFormatHandlerActions.LoadDrawableFromStream] = _ourExtensions;
SupportedExtensions[FileFormatHandlerActions.LoadFromStream] = _ourExtensions;
}
public override bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap)
{
var svgDocument = SvgDocument.Open<SvgDocument>(stream);
try
{
bitmap = svgDocument.Draw();
return true;
}
catch (Exception ex)
{
Log.Error("Can't load SVG", ex);
}
bitmap = null;
return false;
}
public override bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension, ISurface surface = null, SurfaceOutputSettings surfaceOutputSettings = null)
{
// TODO: Implement this
return false;
}
public override IEnumerable<IDrawableContainer> LoadDrawablesFromStream(Stream stream, string extension, ISurface parent = null)
{
SvgDocument svgDocument = null;
try
{
svgDocument = SvgDocument.Open<SvgDocument>(stream);
}
catch (Exception ex)
{
Log.Error("Can't load SVG", ex);
}
if (svgDocument != null)
{
yield return new SvgContainer(svgDocument, parent);
}
}
}
}
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2021 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: https://getgreenshot.org/
* The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using Greenshot.Base.Interfaces;
using Greenshot.Base.Interfaces.Drawing;
using Greenshot.Base.Interfaces.Plugin;
using Greenshot.Editor.Drawing;
using log4net;
using Svg;
namespace Greenshot.Editor.FileFormatHandlers
{
/// <summary>
/// This handled the loading of SVG images to the editor
/// </summary>
public class SvgFileFormatHandler : AbstractFileFormatHandler, IFileFormatHandler
{
private static readonly ILog Log = LogManager.GetLogger(typeof(SvgFileFormatHandler));
private readonly IReadOnlyCollection<string> _ourExtensions = new[] { ".svg" };
public SvgFileFormatHandler()
{
SupportedExtensions[FileFormatHandlerActions.LoadDrawableFromStream] = _ourExtensions;
SupportedExtensions[FileFormatHandlerActions.LoadFromStream] = _ourExtensions;
}
public override bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap)
{
var svgDocument = SvgDocument.Open<SvgDocument>(stream);
try
{
bitmap = svgDocument.Draw();
return true;
}
catch (Exception ex)
{
Log.Error("Can't load SVG", ex);
}
bitmap = null;
return false;
}
public override bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension, ISurface surface = null, SurfaceOutputSettings surfaceOutputSettings = null)
{
// TODO: Implement this
return false;
}
public override IEnumerable<IDrawableContainer> LoadDrawablesFromStream(Stream stream, string extension, ISurface parent = null)
{
SvgContainer svgContainer = null;
try
{
svgContainer = new SvgContainer(stream, parent);
}
catch (Exception ex)
{
Log.Error("Can't load SVG", ex);
}
if (svgContainer != null)
{
yield return svgContainer;
}
}
}
}

View file

@ -48,6 +48,7 @@ namespace Greenshot.Editor.Helpers
{"System.Drawing.Bitmap",typeof(System.Drawing.Bitmap) },
{"System.Drawing.Icon",typeof(System.Drawing.Icon) },
{"System.Drawing.Size",typeof(System.Drawing.Size) },
{"System.IO.MemoryStream",typeof(System.IO.MemoryStream) },
{"System.Drawing.StringAlignment",typeof(System.Drawing.StringAlignment) },
{"System.Collections.Generic.List`1[[Greenshot.Base.Interfaces.Drawing.IFieldHolder", typeof(List<IFieldHolder>)},
{"System.Collections.Generic.List`1[[Greenshot.Base.Interfaces.Drawing.IField", typeof(List<IField>)},