VectSharp  2.2.1
A light library for C# vector graphics
ImageUriParser.cs
1 /*
2  VectSharp - A light library for C# vector graphics.
3  Copyright (C) 2020-2022 Giorgio Bianchini
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, version 3.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License
15  along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 using SixLabors.ImageSharp;
19 using System;
20 using System.IO;
21 using System.Linq;
22 using System.Runtime.InteropServices;
23 
25 {
26  /// <summary>
27  /// Provides a method to parse an image URI into a page.
28  /// </summary>
29  public static class ImageURIParser
30  {
31  /// <summary>
32  /// Parses an image URI into a page. This is intended to replace the default image URI interpreter in <c>VectSharp.SVG.Parser.ParseImageURI</c>. To do this, use something like:
33  /// <code>VectSharp.SVG.Parser.ParseImageURI = VectSharp.ImageSharpUtils.ImageURIParser.Parser(VectSharp.SVG.Parser.ParseSVGURI);</code>
34  /// </summary>
35  /// <param name="parseSVG">A function to parse an SVG image uri into a page. You should pass <c>VectSharp.SVG.Parser.ParseSVGURI</c> as this argument.</param>
36  /// <returns>A function to parse an image URI into a page.</returns>
37  public static Func<string, bool, Page> Parser(Func<string, bool, Page> parseSVG)
38  {
39  return (string uri, bool interpolate) =>
40  {
41  if (uri.StartsWith("data:"))
42  {
43  string mimeType = uri.Substring(uri.IndexOf(":") + 1, uri.IndexOf(";") - uri.IndexOf(":") - 1);
44 
45  string type = uri.Substring(uri.IndexOf(";") + 1, uri.IndexOf(",") - uri.IndexOf(";") - 1);
46 
47  if (mimeType != "image/svg+xml")
48  {
49  int offset = uri.IndexOf(",") + 1;
50 
51  byte[] parsed;
52 
53  string substring = uri.Substring(offset);
54 
55  switch (type)
56  {
57  case "base64":
58  parsed = Convert.FromBase64String(uri.Substring(offset));
59  break;
60  case "":
61  parsed = (from el in System.Web.HttpUtility.UrlDecode(uri.Substring(offset)) select (byte)el).ToArray();
62  break;
63  default:
64  throw new InvalidDataException("Unknown data stream type!");
65  }
66 
67  GCHandle handle = GCHandle.Alloc(parsed, GCHandleType.Pinned);
68 
69  RasterImageStream img = new RasterImageStream(handle.AddrOfPinnedObject(), parsed.Length, interpolate: interpolate);
70 
71  handle.Free();
72 
73  Page pag = new Page(img.Width, img.Height);
74 
75  pag.Graphics.DrawRasterImage(0, 0, img);
76 
77  return pag;
78  }
79  else
80  {
81  return parseSVG(uri, interpolate);
82  }
83  }
84 
85  return null;
86  };
87  }
88  }
89 }
VectSharp.ImageSharpUtils.ImageURIParser
Provides a method to parse an image URI into a page.
Definition: ImageUriParser.cs:30
VectSharp.ImageSharpUtils
Definition: ImageUriParser.cs:25
VectSharp.Graphics.DrawRasterImage
void DrawRasterImage(int sourceX, int sourceY, int sourceWidth, int sourceHeight, double destinationX, double destinationY, double destinationWidth, double destinationHeight, RasterImage image, string tag=null)
Draw a raster image.
Definition: Graphics.cs:467
VectSharp.Page
Represents a Graphics object with a width and height.
Definition: Document.cs:48
VectSharp.RasterImage.Width
int Width
The width in pixels of the image.
Definition: RasterImage.cs:123
VectSharp.Page.Graphics
Graphics Graphics
Graphics surface of the page.
Definition: Document.cs:62
VectSharp.RasterImage.Height
int Height
The height in pixels of the image.
Definition: RasterImage.cs:128
VectSharp.ImageSharpUtils.ImageURIParser.Parser
static Func< string, bool, Page > Parser(Func< string, bool, Page > parseSVG)
Parses an image URI into a page. This is intended to replace the default image URI interpreter in Vec...
Definition: ImageUriParser.cs:37
VectSharp.ImageSharpUtils.RasterImageStream
A RasterImage created from a stream.
Definition: RasterImages.cs:107