22 using System.Runtime.InteropServices;
37 public static Func<string, bool, Page>
Parser(Func<string, bool, Page> parseSVG)
39 return (
string uri,
bool interpolate) =>
41 if (uri.StartsWith(
"data:"))
43 string mimeType = uri.Substring(uri.IndexOf(
":") + 1, uri.IndexOf(
";") - uri.IndexOf(
":") - 1);
45 string type = uri.Substring(uri.IndexOf(
";") + 1, uri.IndexOf(
",") - uri.IndexOf(
";") - 1);
47 if (mimeType !=
"image/svg+xml")
49 int offset = uri.IndexOf(
",") + 1;
53 bool isVector =
false;
55 InputFileTypes fileType;
60 fileType = InputFileTypes.PNG;
64 fileType = InputFileTypes.JPEG;
67 fileType = InputFileTypes.GIF;
70 fileType = InputFileTypes.BMP;
74 fileType = InputFileTypes.TIFF;
76 case "application/oxps":
77 case "application/vnd.ms-xpsdocument":
78 fileType = InputFileTypes.XPS;
81 case "application/x-cbz":
82 fileType = InputFileTypes.CBZ;
84 case "application/epub+zip":
85 fileType = InputFileTypes.EPUB;
89 fileType = InputFileTypes.FB2;
91 case "image/x-portable-anymap":
92 fileType = InputFileTypes.PNM;
94 case "image/x-portable-arbitrarymap":
95 fileType = InputFileTypes.PAM;
97 case "application/pdf":
98 fileType = InputFileTypes.PDF;
102 fileType = InputFileTypes.PDF;
106 string substring = uri.Substring(offset);
112 parsed = Convert.FromBase64String(uri.Substring(offset));
115 parsed = (from el in System.Web.HttpUtility.UrlDecode(uri.Substring(offset)) select (
byte)el).ToArray();
118 throw new InvalidDataException(
"Unknown data stream type!");
123 GCHandle handle = GCHandle.Alloc(parsed, GCHandleType.Pinned);
137 string tempFile = Path.GetTempFileName();
139 using (MuPDFContext context =
new MuPDFContext())
141 using (MuPDFDocument document =
new MuPDFDocument(context, parsed, fileType))
143 MuPDFDocument.CreateDocument(context, tempFile, DocumentOutputFileTypes.SVG,
true, document.Pages[0]);
147 string tbr =
"data:image/svg+xml;," + System.Web.HttpUtility.UrlEncode(File.ReadAllText(tempFile));
149 File.Delete(tempFile);
151 return parseSVG(tbr, interpolate);
156 return parseSVG(uri, interpolate);