This commit is contained in:
Peter Tribe 2019-02-13 11:53:11 -08:00
Родитель e5e1cb9de1
Коммит cfacb27ddb
4 изменённых файлов: 56 добавлений и 51 удалений

Просмотреть файл

@ -12,7 +12,7 @@
<VersionPrefix Condition="$(packageversion) != ''">$(packageversion)</VersionPrefix>
<VersionPrefix Condition="$(packageversion) == ''">0.0.1</VersionPrefix>
<TargetFrameworks>netstandard1.3;netstandard2.0;netcoreapp2.1;net472</TargetFrameworks>
<TargetFrameworks>netstandard1.3;netstandard2.0;netcoreapp2.1;net462;net472</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>EquinoxLabs.SVGSharpie.ImageSharp</AssemblyName>

Просмотреть файл

@ -1,49 +0,0 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using EquinoxLabs.SVGSharpie.ImageSharp.Dom;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
namespace EquinoxLabs.SVGSharpie.ImageSharp
{
public static class SvgImageRenderer
{
public static Image<TPixel> LoadFromString<TPixel>(string content)
where TPixel : struct, IPixel<TPixel>
=> LoadFromStringInner<TPixel>(content, null, null);
public static Image<TPixel> LoadFromString<TPixel>(string content, int width, int height)
where TPixel : struct, IPixel<TPixel>
=> LoadFromStringInner<TPixel>(content, width, height);
private static Image<TPixel> LoadFromStringInner<TPixel>(string content, int? targetWidth, int? targetHeight)
where TPixel : struct, IPixel<TPixel>
{
var doc = SvgDocument.Parse(content);
float? width = targetWidth ?? doc.RootElement.Width ?? doc.RootElement.ViewWidth;
float? height = targetHeight ?? doc.RootElement.Height ?? doc.RootElement.ViewWidth;
if (!width.HasValue || !height.HasValue)
{
throw new Exception("Svg does not specify a size set one.");
}
var image = new Image<TPixel>((int)Math.Ceiling(width.Value), (int)Math.Ceiling(height.Value));
image.Mutate(x =>
{
var renderer = new SvgDocumentRenderer<TPixel>(image.Size(), x);
doc.RootElement.Accept(renderer);
});
return image;
}
}
}

Просмотреть файл

@ -0,0 +1,54 @@
using System;
using EquinoxLabs.SVGSharpie.ImageSharp.Dom;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
namespace EquinoxLabs.SVGSharpie.ImageSharp
{
public static class SvgImageRenderer
{
public static Image<TPixel> RenderFromString<TPixel>(string content) where TPixel : struct, IPixel<TPixel>
{
var document = SvgDocument.Parse(content);
return RenderInner<TPixel>(document, null, null);
}
public static Image<TPixel> RenderFromString<TPixel>(string content, int width, int height) where TPixel : struct, IPixel<TPixel>
{
var document = SvgDocument.Parse(content);
return RenderInner<TPixel>(document, width, height);
}
public static Image<TPixel> RenderFromDocument<TPixel>(SvgDocument document) where TPixel : struct, IPixel<TPixel>
{
return RenderInner<TPixel>(document, null, null);
}
public static Image<TPixel> RenderFromDocument<TPixel>(SvgDocument document, int width, int height) where TPixel : struct, IPixel<TPixel>
{
return RenderInner<TPixel>(document, width, height);
}
private static Image<TPixel> RenderInner<TPixel>(SvgDocument document, int? targetWidth, int? targetHeight) where TPixel : struct, IPixel<TPixel>
{
float? width = targetWidth ?? document.RootElement.Width ?? document.RootElement.ViewWidth;
float? height = targetHeight ?? document.RootElement.Height ?? document.RootElement.ViewWidth;
if (!width.HasValue || !height.HasValue)
{
throw new Exception("Svg does not specify a size set one.");
}
var image = new Image<TPixel>((int)Math.Ceiling(width.Value), (int)Math.Ceiling(height.Value));
image.Mutate(x =>
{
var renderer = new SvgDocumentRenderer<TPixel>(image.Size(), x);
document.RootElement.Accept(renderer);
});
return image;
}
}
}

Просмотреть файл

@ -12,7 +12,7 @@
<VersionPrefix Condition="$(packageversion) != ''">$(packageversion)</VersionPrefix>
<VersionPrefix Condition="$(packageversion) == ''">0.0.1</VersionPrefix>
<TargetFrameworks>netstandard1.3;netstandard2.0;netcoreapp2.1;net472</TargetFrameworks>
<TargetFrameworks>netstandard1.3;netstandard2.0;netcoreapp2.1;net462;net472</TargetFrameworks>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>EquinoxLabs.SVGSharpie</AssemblyName>