-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathLayout2DXmlToImageConverter.cs
More file actions
211 lines (183 loc) · 5.51 KB
/
Layout2DXmlToImageConverter.cs
File metadata and controls
211 lines (183 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using SkiaSharp;
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Xsl;
using Waher.Content.Html;
using Waher.Content.Images;
using Waher.Content.Xml;
using Waher.Content.Xml.Text;
using Waher.Content.Xsl;
using Waher.Runtime.Inventory;
using Waher.Runtime.IO;
using Waher.Script;
namespace Waher.Content.Markdown.Layout2D
{
/// <summary>
/// Converts GraphViz documents to images.
/// </summary>
public class Layout2DXmlToImageConverter : IContentConverter
{
private static readonly Regex processingInstructionData= new Regex(
@"(\s*((type=['""](?'Type'[^'""]*)['""])|(href=['""](?'HRef'[^'""]*)['""])|([^ =]+=['""]([^'""]*)['""])))*",
RegexOptions.Singleline | RegexOptions.Compiled);
/// <summary>
/// Converts GraphViz documents to images.
/// </summary>
public Layout2DXmlToImageConverter()
{
}
/// <summary>
/// Converts content from these content types.
/// </summary>
public string[] FromContentTypes => XmlCodec.XmlContentTypes;
/// <summary>
/// Converts content to these content types.
/// </summary>
public virtual string[] ToContentTypes => ImageCodec.ImageContentTypes;
/// <summary>
/// How well the content is converted.
/// </summary>
public virtual Grade ConversionGrade => Grade.Barely;
/// <summary>
/// Performs the actual conversion.
/// </summary>
/// <param name="State">State of the current conversion.</param>
/// <returns>If the result is dynamic (true), or only depends on the source (false).</returns>
public async Task<bool> ConvertAsync(ConversionState State)
{
byte[] Bin = await State.From.ReadAllAsync();
string Xml = Strings.GetString(Bin, Encoding.UTF8);
string s = State.ToContentType;
int i;
i = s.IndexOf(';');
if (i > 0)
s = s.Substring(0, i);
string Extension;
SKEncodedImageFormat Format;
int Quality = 100;
switch (s.ToLower())
{
case ImageCodec.ContentTypePng:
Format = SKEncodedImageFormat.Png;
Extension = ImageCodec.FileExtensionPng;
break;
case ImageCodec.ContentTypeBmp:
Format = SKEncodedImageFormat.Bmp;
Extension = ImageCodec.FileExtensionBmp;
break;
case ImageCodec.ContentTypeGif:
Format = SKEncodedImageFormat.Gif;
Extension = ImageCodec.FileExtensionGif;
break;
case ImageCodec.ContentTypeJpg:
Format = SKEncodedImageFormat.Jpeg;
Extension = ImageCodec.FileExtensionJpg;
Quality = 90;
break;
case ImageCodec.ContentTypeWebP:
Format = SKEncodedImageFormat.Webp;
Extension = ImageCodec.FileExtensionWebP;
break;
case ImageCodec.ContentTypeIcon:
Format = SKEncodedImageFormat.Ico;
Extension = ImageCodec.FileExtensionIcon;
break;
case ImageCodec.ContentTypeTiff:
case ImageCodec.ContentTypeWmf:
case ImageCodec.ContentTypeEmf:
case ImageCodec.ContentTypeSvg:
default:
Format = XmlLayout.DefaultFormat;
Extension = XmlLayout.DefaultFileExtension;
State.ToContentType = XmlLayout.DefaultContentType;
break;
}
string ContentType = State.FromContentType;
Variables Variables;
GraphInfo Graph;
bool Transformed;
bool ValidXml = true;
do
{
Transformed = false;
Variables = new Variables();
Graph = await XmlLayout.GetFileName("layout", Xml, Variables, Format, Quality, Extension);
if (!Graph.Converted && !(Graph.Xml is null))
{
foreach (XmlNode N in Graph.Xml)
{
if (N is XmlProcessingInstruction PI &&
PI.Name == "xml-stylesheet")
{
Match M = processingInstructionData.Match(PI.Data);
if (M.Success && M.Groups["Type"].Value == "text/xsl")
{
string HRef = M.Groups["HRef"].Value;
if (State.TryGetLocalResourceFileName(HRef, null, out string FileName) &&
File.Exists(FileName))
{
using (FileStream fs = File.OpenRead(FileName))
{
XslCompiledTransform Transform = XSL.LoadTransform(fs);
Xml = XSL.Transform(Xml, Transform);
ValidXml = XML.IsValidXml(Xml);
Bin = null;
if (!ValidXml)
{
bool IsHtml;
try
{
HtmlDocument Doc = new HtmlDocument(Xml);
IsHtml = !(Doc.Html is null) && !(Doc.Body is null);
}
catch (Exception)
{
IsHtml = false;
}
if (!IsHtml)
{
MarkdownSettings Settings = new MarkdownSettings(null,
MarkdownDocument.HeaderEndPosition(Xml).HasValue,
State.Session)
{
ResourceMap = State.ResourceMap
};
MarkdownDocument Doc = await MarkdownDocument.CreateAsync(Xml, Settings, State.FromFileName, State.LocalResourceName, State.URL);
Xml = await Doc.GenerateHTML();
}
ContentType = HtmlCodec.DefaultContentType;
}
}
}
}
}
}
}
}
while (!Graph.Converted && Transformed && ValidXml);
if (!Graph.Converted)
{
if (Bin is null)
Bin = Encoding.UTF8.GetBytes(Xml);
await State.To.WriteAsync(Bin, 0, Bin.Length);
State.ToContentType = ContentType;
return true;
}
else if (Graph.Dynamic)
{
await State.To.WriteAsync(Graph.DynamicContent, 0, Graph.DynamicContent.Length);
return true;
}
else
{
byte[] Data = await Files.ReadAllBytesAsync(Graph.FileName);
await State.To.WriteAsync(Data, 0, Data.Length);
return false;
}
}
}
}