Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GraphVizDocumentToImageConverter.cs
1using System;
2using System.IO;
3using System.Threading.Tasks;
6using Waher.Script;
7
9{
14 {
19 {
20 }
21
25 public string[] FromContentTypes => new string[] { GraphVizCodec.DefaultContentType };
26
30 public virtual string[] ToContentTypes
31 {
32 get
33 {
34 return new string[]
35 {
37 ImageCodec.ContentTypeSvg
38 };
39 }
40 }
41
45 public virtual Grade ConversionGrade => Grade.Excellent;
46
52 public async Task<bool> ConvertAsync(ConversionState State)
53 {
54 string GraphDescription;
55
56 using (StreamReader rd = new StreamReader(State.From))
57 {
58 GraphDescription = rd.ReadToEnd();
59 }
60
61 string s = State.ToContentType;
62 int i;
63
64 i = s.IndexOf(';');
65 if (i > 0)
66 s = s.Substring(0, i);
67
68 bool Png = string.Compare(s, ImageCodec.ContentTypePng, true) == 0;
69 bool Svg = string.Compare(s, ImageCodec.ContentTypeSvg, true) == 0;
70
71 if (!(State.PossibleContentTypes is null))
72 {
73 foreach (string ContentType in State.PossibleContentTypes)
74 {
75 s = ContentType;
76 i = s.IndexOf(';');
77 if (i > 0)
78 s = s.Substring(0, i);
79
80 Png |= string.Compare(s, ImageCodec.ContentTypePng, true) == 0;
81 Svg |= string.Compare(s, ImageCodec.ContentTypeSvg, true) == 0;
82 }
83 }
84
85 string Language;
86
87 if (string.IsNullOrEmpty(State.FromFileName))
88 Language = "dot";
89 else
90 {
91 Language = Path.GetExtension(State.FromFileName).ToLower();
92 if (Language.StartsWith("."))
93 Language = Language.Substring(1);
94
95 switch (Language)
96 {
97 case "dot":
98 case "neato":
99 case "fdp":
100 case "sfdp":
101 case "twopi":
102 case "circo":
103 break;
104
105 default:
106 Language = "dot";
107 break;
108 }
109 }
110
112 GraphInfo Graph;
113
114 if (Svg)
115 {
116 Graph = await GraphViz.GetFileName(Language, GraphDescription, ResultType.Svg, true, Variables);
117 State.ToContentType = ImageCodec.ContentTypeSvg;
118 }
119 else if (Png)
120 {
121 Graph = await GraphViz.GetFileName(Language, GraphDescription, ResultType.Png, true, Variables);
122 State.ToContentType = ImageCodec.ContentTypePng;
123 }
124 else
125 {
126 State.Error = new Exception("Unable to convert document from " + State.FromContentType + " to " + State.ToContentType);
127 return false;
128 }
129
130 byte[] Data = await Runtime.IO.Files.ReadAllBytesAsync(Graph.FileName);
131
132 await State.To.WriteAsync(Data, 0, Data.Length);
133
134 return false;
135 }
136
137 }
138}
Contains the state of a content conversion process.
string ToContentType
Content type of the content to convert to. This value might be changed, in case the converter finds a...
string[] PossibleContentTypes
Possible content types the converter is allowed to convert to. Can be null if there are no alternativ...
string FromContentType
Content type of the content to convert from.
Stream To
Stream pointing to where binary representation of content is to be sent.
string FromFileName
If the content is coming from a file, this parameter contains the name of that file....
Stream From
Stream pointing to binary representation of content.
Image encoder/decoder.
Definition: ImageCodec.cs:14
const string ContentTypePng
image/png
Definition: ImageCodec.cs:30
const string ContentTypeSvg
image/svg+xml
Definition: ImageCodec.cs:45
virtual string[] ToContentTypes
Converts content to these content types.
async Task< bool > ConvertAsync(ConversionState State)
Performs the actual conversion.
Class managing GraphViz integration into Markdown documents.
Definition: GraphViz.cs:61
Collection of variables.
Definition: Variables.cs:25
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
ResultType
Image types supported by GraphViz
Definition: GraphViz.cs:35
Grade
Grade enumeration
Definition: Grade.cs:7