Fixed SvgContainer support for the .greenshot file

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

View file

@ -21,6 +21,7 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.IO;
using Dapplo.Windows.Common.Structs; using Dapplo.Windows.Common.Structs;
using Greenshot.Base.Core; using Greenshot.Base.Core;
using Greenshot.Base.Interfaces; using Greenshot.Base.Interfaces;
@ -34,14 +35,32 @@ namespace Greenshot.Editor.Drawing
[Serializable] [Serializable]
public class SvgContainer : VectorGraphicsContainer 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; _svgContent = new MemoryStream();
Size = new Size((int)svgDocument.Width, (int)svgDocument.Height); 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() protected override Image ComputeBitmap()
{ {
//var image = ImageHelper.CreateEmpty(Width, Height, PixelFormat.Format32bppArgb, Color.Transparent); //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 /// This is the cached version of the bitmap, pre-rendered to save performance
/// Do not serialized, it can be rebuild with other information. /// Do not serialized, it can be rebuild with other information.
/// </summary> /// </summary>
[NonSerialized] private Image _cachedImage; [NonSerialized]
private Image _cachedImage;
/// <summary> /// <summary>
/// Constructor takes care of calling Init /// Constructor takes care of calling Init

View file

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

View file

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