Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SvgCodec.cs
1using System;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using Svg.Skia;
7using Waher.Content;
10
12{
17 {
18 public SvgCodec() { }
19
20 public static readonly string[] SvgContentTypes = new string[] { ImageCodec.ContentTypeSvg };
21 public static readonly string[] SvgFileExtensions = new string[] { ImageCodec.FileExtensionSvg };
22 public string[] ContentTypes => SvgContentTypes;
23 public string[] FileExtensions => SvgFileExtensions;
24
25 public bool Decodes(string ContentType, out Grade Grade)
26 {
27 if (string.Equals(ContentType, ImageCodec.ContentTypeSvg, StringComparison.OrdinalIgnoreCase))
28 {
29 Grade = Grade.Excellent;
30 return true;
31 }
32 Grade = Grade.NotAtAll;
33 return false;
34 }
35
36 public Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
37 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
38 {
39 SKSvg Svg;
40 try
41 {
42 Svg = new SKSvg();
43 using (MemoryStream ms = new MemoryStream(Data))
44 {
45 Svg.Load(ms);
46 }
47 }
48 catch (Exception ex)
49 {
50 return Task.FromResult(new ContentResponse(ex));
51 }
52
53 return Task.FromResult(new ContentResponse(ContentType, Svg, Data));
54 }
55
56 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
57 {
58 if (InternetContent.IsAccepted(SvgContentTypes, AcceptedContentTypes))
59 {
60 if (Object is string s && s.IndexOf("<svg", StringComparison.OrdinalIgnoreCase) >= 0)
61 {
62 Grade = Grade.Ok;
63 return true;
64 }
65 else if (Object is SKSvg)
66 {
67 Grade = Grade.Barely; // Cannot re-encode reliably without original XML.
68 return false;
69 }
70 }
71 Grade = Grade.NotAtAll;
72 return false;
73 }
74
75 public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
76 {
77 if (!InternetContent.IsAccepted(SvgContentTypes, out string ContentType, AcceptedContentTypes))
78 return Task.FromResult(new ContentResponse(new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object))));
79
80 if (!(Object is string Xml))
81 return Task.FromResult(new ContentResponse(new ArgumentException("Encoder expects raw SVG markup string.", nameof(Object))));
82
83 if (Xml.IndexOf("<svg", StringComparison.OrdinalIgnoreCase) < 0)
84 return Task.FromResult(new ContentResponse(new ArgumentException("Object does not appear to be valid SVG markup.", nameof(Object))));
85
86 if (Encoding is null)
87 {
88 ContentType += "; charset=utf-8";
89 Encoding = Encoding.UTF8;
90 }
91 else
92 ContentType += "; charset=" + Encoding.WebName;
93
94 return Task.FromResult(new ContentResponse(ContentType, Object, Encoding.GetBytes(Xml)));
95 }
96
97 public bool TryGetContentType(string FileExtension, out string ContentType)
98 {
99 if (string.Equals(FileExtension, "svg", StringComparison.OrdinalIgnoreCase))
100 {
101 ContentType = ImageCodec.ContentTypeSvg;
102 return true;
103 }
104
105 ContentType = string.Empty;
106 return false;
107 }
108
109 public bool TryGetFileExtension(string ContentType, out string FileExtension)
110 {
111 if (string.Equals(ContentType, ImageCodec.ContentTypeSvg, StringComparison.OrdinalIgnoreCase))
112 {
113 FileExtension = "svg";
114 return true;
115 }
116
117 FileExtension = string.Empty;
118 return false;
119 }
120 }
121}
SVG image encoder/decoder. Decodes to an SKSvg.
Definition: SvgCodec.cs:17
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: SvgCodec.cs:109
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
Definition: SvgCodec.cs:36
string[] FileExtensions
Supported file extensions.
Definition: SvgCodec.cs:23
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: SvgCodec.cs:25
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: SvgCodec.cs:97
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
Definition: SvgCodec.cs:75
string[] ContentTypes
Supported content types.
Definition: SvgCodec.cs:22
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: SvgCodec.cs:56
Contains information about a response to a content request.
Image encoder/decoder.
Definition: ImageCodec.cs:14
const string ContentTypeSvg
image/svg+xml
Definition: ImageCodec.cs:45
Static class managing encoding and decoding of internet content.
static bool IsAccepted(string ContentType, params string[] AcceptedContentTypes)
Checks if a given content type is acceptable.
Interface for reporting progress about an encoding or decoding.
Basic interface for Internet Content decoders. A class implementing this interface and having a defau...
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7