Making the first things work, still the ClipboardHelper needs to be modified so we do not return an image but a container, otherwise we cannot support vector graphics.

This commit is contained in:
Robin Krom 2022-02-01 13:42:53 +01:00
commit 483bf97dce
No known key found for this signature in database
GPG key ID: BCC01364F1371490
10 changed files with 197 additions and 48 deletions

View file

@ -31,6 +31,7 @@ using System.Text;
using System.Threading;
using System.Windows.Forms;
using Greenshot.Base.Core.Enums;
using Greenshot.Base.Core.FileFormatHandlers;
using Greenshot.Base.IniFile;
using Greenshot.Base.Interfaces;
using Greenshot.Base.Interfaces.Plugin;
@ -1135,10 +1136,11 @@ EndSelection:<<<<<<<4
string[] dropFileNames = (string[]) dataObject.GetData(DataFormats.FileDrop);
if (dropFileNames != null && dropFileNames.Length > 0)
{
var supportedExtensions = FileFormatHandlerRegistry.ExtensionsFor(FileFormatHandlerActions.LoadFromStream).ToList();
return dropFileNames
.Where(filename => !string.IsNullOrEmpty(filename))
.Where(Path.HasExtension)
.Where(filename => ImageHelper.StreamConverters.Keys.Contains(Path.GetExtension(filename).ToLowerInvariant().Substring(1)));
.Where(filename => supportedExtensions.Contains(Path.GetExtension(filename).ToLowerInvariant().Substring(1)));
}
return Enumerable.Empty<string>();

View file

@ -0,0 +1,44 @@
/*
* 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.Collections.Generic;
using System.Linq;
using Greenshot.Base.Interfaces;
namespace Greenshot.Base.Core.FileFormatHandlers
{
public static class FileFormatHandlerRegistry
{
public static readonly IList<IFileFormatHandler> FileFormatHandlers = new List<IFileFormatHandler>();
static FileFormatHandlerRegistry()
{
FileFormatHandlers.Add(new IconFileFormatHandler());
FileFormatHandlers.Add(new GreenshotFileFormatHandler());
FileFormatHandlers.Add(new DefaultFileFormatHandler());
}
public static IEnumerable<string> ExtensionsFor(FileFormatHandlerActions fileFormatHandlerAction)
{
return FileFormatHandlers.SelectMany(ffh => ffh.SupportedExtensions(fileFormatHandlerAction)).Distinct();
}
}
}

View file

@ -20,6 +20,7 @@
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
@ -30,29 +31,51 @@ namespace Greenshot.Base.Core.FileFormatHandlers
{
public class GreenshotFileFormatHandler : IFileFormatHandler
{
private static readonly string[] SupportedExtensions = { "greenshot" };
private static readonly string[] OurExtensions = { "greenshot" };
public bool CanDoActionForExtension(FileFormatHandlerActions fileFormatHandlerAction, string extension)
/// <inheritdoc />
public IEnumerable<string> SupportedExtensions(FileFormatHandlerActions fileFormatHandlerAction)
{
if (fileFormatHandlerAction == FileFormatHandlerActions.LoadDrawableFromStream)
{
return Enumerable.Empty<string>();
}
return OurExtensions;
}
/// <inheritdoc />
public bool Supports(FileFormatHandlerActions fileFormatHandlerAction, string extension)
{
if (fileFormatHandlerAction == FileFormatHandlerActions.LoadDrawableFromStream)
{
return false;
}
return SupportedExtensions.Contains(extension);
return OurExtensions.Contains(extension);
}
public void SaveToStream(Bitmap bitmap, Stream destination, string extension)
/// <inheritdoc />
public int PriorityFor(FileFormatHandlerActions fileFormatHandlerAction, string extension)
{
throw new NotImplementedException();
return int.MaxValue;
}
public Bitmap Load(Stream stream, string extension)
public bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension)
{
// TODO: Implement this
return false;
}
public bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap)
{
var surface = SimpleServiceProvider.Current.GetInstance<Func<ISurface>>().Invoke();
return (Bitmap)surface.GetImageForExport();
bitmap = (Bitmap)surface.GetImageForExport();
return true;
}
public IDrawableContainer LoadDrawableFromStream(Stream stream, string extension, ISurface parent)
public bool TryLoadDrawableFromStream(Stream stream, string extension, out IDrawableContainer drawableContainer, ISurface parent)
{
throw new NotImplementedException();
}

View file

@ -20,6 +20,7 @@
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
@ -28,31 +29,55 @@ using Greenshot.Base.Interfaces.Drawing;
namespace Greenshot.Base.Core.FileFormatHandlers
{
/// <summary>
/// This can be an ImageSharp implementation
/// </summary>
public class PngFileFormatHandler : IFileFormatHandler
{
private static readonly string[] SupportedExtensions = { "png" };
private static readonly string[] OurExtensions = { "png" };
public bool CanDoActionForExtension(FileFormatHandlerActions fileFormatHandlerAction, string extension)
/// <inheritdoc />
public IEnumerable<string> SupportedExtensions(FileFormatHandlerActions fileFormatHandlerAction)
{
if (fileFormatHandlerAction == FileFormatHandlerActions.LoadDrawableFromStream)
{
return Enumerable.Empty<string>();
}
return OurExtensions;
}
/// <inheritdoc />
public bool Supports(FileFormatHandlerActions fileFormatHandlerAction, string extension)
{
if (fileFormatHandlerAction == FileFormatHandlerActions.LoadDrawableFromStream)
{
return false;
}
return SupportedExtensions.Contains(extension);
return OurExtensions.Contains(extension);
}
/// <inheritdoc />
public int PriorityFor(FileFormatHandlerActions fileFormatHandlerAction, string extension)
{
return int.MaxValue;
}
public void SaveToStream(Bitmap bitmap, Stream destination, string extension)
public bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap)
{
// TODO: Implement this
throw new NotImplementedException();
}
public Bitmap Load(Stream stream, string extension)
public bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension)
{
throw new NotImplementedException();
// TODO: Implement this
return false;
}
public IDrawableContainer LoadDrawableFromStream(Stream stream, string extension, ISurface parent)
public bool TryLoadDrawableFromStream(Stream stream, string extension, out IDrawableContainer drawableContainer, ISurface parent)
{
// TODO: Implement this
throw new NotImplementedException();
}
}

View file

@ -25,6 +25,7 @@ using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using Greenshot.Base.Core.FileFormatHandlers;
using Greenshot.Base.Effects;
using Greenshot.Base.IniFile;
@ -56,14 +57,6 @@ namespace Greenshot.Base.Core
private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection<CoreConfiguration>();
private const int ExifOrientationId = 0x0112;
public static readonly IList<IFileFormatHandler> FileFormatHandlers = new List<IFileFormatHandler>();
static ImageHelper()
{
FileFormatHandlers.Add(new IconFileFormatHandler());
FileFormatHandlers.Add(new GreenshotFileFormatHandler());
FileFormatHandlers.Add(new DefaultFileFormatHandler());
}
/// <summary>
/// Make sure the image is orientated correctly
@ -1720,12 +1713,15 @@ namespace Greenshot.Base.Core
startingPosition = 0;
}
foreach (var fileFormatHandler in FileFormatHandlers)
foreach (var fileFormatHandler in FileFormatHandlerRegistry.FileFormatHandlers
.Where(ffh => ffh.Supports(FileFormatHandlerActions.LoadFromStream, extension))
.OrderBy(ffh => ffh.PriorityFor(FileFormatHandlerActions.LoadFromStream, extension)))
{
if (!fileFormatHandler.CanDoActionForExtension(FileFormatHandlerActions.Load, extension)) continue;
stream.Seek(startingPosition, SeekOrigin.Begin);
return fileFormatHandler.Load(stream, extension);
if (fileFormatHandler.TryLoadFromStream(stream, extension, out var bitmap))
{
return bitmap;
}
}
return null;

View file

@ -24,9 +24,11 @@ using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using Greenshot.Base.Core.FileFormatHandlers;
using Greenshot.Base.IniFile;
using Greenshot.Base.Interfaces;
using Greenshot.Base.Interfaces.Plugin;
@ -94,7 +96,8 @@ namespace Greenshot.Base.Core
public static Image DownloadImage(string url)
{
var extensions = new StringBuilder();
foreach (var extension in ImageHelper.StreamConverters.Keys)
var supportedExtensions = FileFormatHandlerRegistry.ExtensionsFor(FileFormatHandlerActions.LoadFromStream).ToList();
foreach (var extension in supportedExtensions)
{
if (string.IsNullOrEmpty(extension))
{

View file

@ -32,7 +32,7 @@ namespace Greenshot.Base.Interfaces
public enum FileFormatHandlerActions
{
SaveToStream,
Load,
LoadFromStream,
LoadDrawableFromStream
}

View file

@ -0,0 +1,34 @@
/*
* 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 Greenshot.Base.Core.FileFormatHandlers;
using Greenshot.Editor.FileFormatHandlers;
namespace Greenshot.Editor
{
public static class EditorInitialize
{
public static void Initialize()
{
FileFormatHandlerRegistry.FileFormatHandlers.Add(new SvgFileFormatHandler());
}
}
}

View file

@ -20,6 +20,7 @@
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
@ -39,48 +40,67 @@ namespace Greenshot.Editor.FileFormatHandlers
public class SvgFileFormatHandler : IFileFormatHandler
{
private static readonly ILog Log = LogManager.GetLogger(typeof(ImageHelper));
private static readonly string[] OurExtensions = { "svg" };
private static readonly string[] SupportedExtensions = { "svg" };
public bool CanDoActionForExtension(FileFormatHandlerActions fileFormatHandlerAction, string extension)
/// <inheritdoc />
public IEnumerable<string> SupportedExtensions(FileFormatHandlerActions fileFormatHandlerAction)
{
if (fileFormatHandlerAction == FileFormatHandlerActions.SaveToStream)
if (fileFormatHandlerAction == FileFormatHandlerActions.LoadDrawableFromStream)
{
return Enumerable.Empty<string>();
}
return OurExtensions;
}
/// <inheritdoc />
public bool Supports(FileFormatHandlerActions fileFormatHandlerAction, string extension)
{
if (fileFormatHandlerAction == FileFormatHandlerActions.LoadDrawableFromStream)
{
return false;
}
return SupportedExtensions.Contains(extension);
return OurExtensions.Contains(extension);
}
/// <inheritdoc />
public int PriorityFor(FileFormatHandlerActions fileFormatHandlerAction, string extension)
{
return int.MaxValue;
}
public void SaveToStream(Bitmap bitmap, Stream destination, string extension)
public bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap)
{
throw new NotImplementedException();
}
public Bitmap Load(Stream stream, string extension)
{
var svgDocument = SvgDocument.Open<SvgDocument>(stream);
int width = (int)svgDocument.ViewBox.Width;
int height = (int)svgDocument.ViewBox.Height;
try
{
var result = ImageHelper.CreateEmpty(width, height, PixelFormat.Format32bppArgb, Color.Transparent);
svgDocument.Draw(result);
return result;
bitmap = ImageHelper.CreateEmpty(width, height, PixelFormat.Format32bppArgb, Color.Transparent);
svgDocument.Draw(bitmap);
return true;
}
catch (Exception ex)
{
Log.Error("Can't load SVG", ex);
}
return null;
bitmap = null;
return false;
}
public IDrawableContainer LoadDrawableFromStream(Stream stream, string extension, ISurface parent)
public bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension)
{
// TODO: Implement this
return false;
}
public bool TryLoadDrawableFromStream(Stream stream, string extension, out IDrawableContainer drawableContainer, ISurface parent)
{
var svgDocument = SvgDocument.Open<SvgDocument>(stream);
return new SvgContainer(svgDocument, parent);
drawableContainer = new SvgContainer(svgDocument, parent);
return true;
}
}
}

View file

@ -23,6 +23,7 @@ using System;
using System.Globalization;
using System.Net;
using System.Reflection;
using Greenshot.Editor;
using Greenshot.Forms;
namespace Greenshot
@ -65,6 +66,7 @@ namespace Greenshot
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
EditorInitialize.Initialize();
MainForm.Start(args);
}
}