Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for <link> css inclusion #721

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
50 changes: 36 additions & 14 deletions Source/SvgDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,32 +260,35 @@ private static bool ExceptionCaughtIsGdiPlusRelated(Exception e)
/// Opens the document at the specified path and loads the SVG contents.
/// </summary>
/// <param name="path">A <see cref="string"/> containing the path of the file to open.</param>
/// <param name="baseUri"> base uri for linked file</param>
/// <returns>An <see cref="SvgDocument"/> with the contents loaded.</returns>
/// <exception cref="FileNotFoundException">The document at the specified <paramref name="path"/> cannot be found.</exception>
public static SvgDocument Open(string path)
public static SvgDocument Open(string path, Uri baseUri = null)
FabioFerrettiMoodys marked this conversation as resolved.
Show resolved Hide resolved
{
return Open<SvgDocument>(path, null);
return Open<SvgDocument>(path, null, baseUri == null ? new Uri(System.IO.Path.GetFullPath(path)) : baseUri);
}

/// <summary>
/// Opens the document at the specified path and loads the SVG contents.
/// </summary>
/// <param name="path">A <see cref="string"/> containing the path of the file to open.</param>
/// <param name="baseUri"> base uri for linked file</param>
/// <returns>An <see cref="SvgDocument"/> with the contents loaded.</returns>
/// <exception cref="FileNotFoundException">The document at the specified <paramref name="path"/> cannot be found.</exception>
public static T Open<T>(string path) where T : SvgDocument, new()
public static T Open<T>(string path, Uri baseUri = null) where T : SvgDocument, new()
{
return Open<T>(path, null);
return Open<T>(path, null, baseUri);
}

/// <summary>
/// Opens the document at the specified path and loads the SVG contents.
/// </summary>
/// <param name="path">A <see cref="string"/> containing the path of the file to open.</param>
/// <param name="baseUri"> base uri for linked file</param>
/// <param name="entities">A dictionary of custom entity definitions to be used when resolving XML entities within the document.</param>
/// <returns>An <see cref="SvgDocument"/> with the contents loaded.</returns>
/// <exception cref="FileNotFoundException">The document at the specified <paramref name="path"/> cannot be found.</exception>
public static T Open<T>(string path, Dictionary<string, string> entities) where T : SvgDocument, new()
public static T Open<T>(string path, Dictionary<string, string> entities, Uri baseUri = null) where T : SvgDocument, new()
{
if (string.IsNullOrEmpty(path))
{
Expand All @@ -299,8 +302,11 @@ public static SvgDocument Open(string path)

using (var stream = File.OpenRead(path))
{
var doc = Open<T>(stream, entities);
doc.BaseUri = new Uri(System.IO.Path.GetFullPath(path));
var doc = Open<T>(stream, entities, baseUri == null ? new Uri(System.IO.Path.GetFullPath(path)) : baseUri);
if (baseUri == null)
doc.BaseUri = new Uri(System.IO.Path.GetFullPath(path));
else
doc.BaseUri = baseUri;
return doc;
}
}
Expand All @@ -309,9 +315,10 @@ public static SvgDocument Open(string path)
/// Attempts to open an SVG document from the specified <see cref="Stream"/>.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> containing the SVG document to open.</param>
public static T Open<T>(Stream stream) where T : SvgDocument, new()
/// <param name="baseUri"> base uri for linked file</param>
public static T Open<T>(Stream stream, Uri baseUri = null) where T : SvgDocument, new()
{
return Open<T>(stream, null);
return Open<T>(stream, null, baseUri );
}


Expand Down Expand Up @@ -342,8 +349,9 @@ public static SvgDocument Open(string path)
/// </summary>
/// <param name="stream">The <see cref="Stream"/> containing the SVG document to open.</param>
/// <param name="entities">Custom entity definitions.</param>
/// <param name="baseUri"> base uri for linked file</param>
/// <exception cref="ArgumentNullException">The <paramref name="stream"/> parameter cannot be <c>null</c>.</exception>
public static T Open<T>(Stream stream, Dictionary<string, string> entities) where T : SvgDocument, new()
public static T Open<T>(Stream stream, Dictionary<string, string> entities, Uri baseUri = null) where T : SvgDocument, new()
{
if (stream == null)
{
Expand All @@ -356,10 +364,10 @@ public static SvgDocument Open(string path)
XmlResolver = new SvgDtdResolver(),
WhitespaceHandling = WhitespaceHandling.None
};
return Open<T>(reader);
return Open<T>(reader, baseUri);
}

private static T Open<T>(XmlReader reader) where T : SvgDocument, new()
private static T Open<T>(XmlReader reader, Uri baseUri = null) where T : SvgDocument, new()
{
if (!SkipGdiPlusCapabilityCheck)
{
Expand Down Expand Up @@ -435,6 +443,19 @@ public static SvgDocument Open(string path)
{
styles.Add(unknown);
}
else if (element.ElementName.ToLower() == "link")
{
if (element.CustomAttributes["rel"] == "stylesheet")
{
var href = element.CustomAttributes["href"];
Uri uri = baseUri == null ? new Uri(href) : new Uri(baseUri, href);
var content = File.ReadAllText(uri.AbsolutePath);
SvgUnknownElement elem = new SvgUnknownElement("style");
FabioFerrettiMoodys marked this conversation as resolved.
Show resolved Hide resolved
element.Content = content;
styles.Add(element);

}
}
break;
case XmlNodeType.CDATA:
case XmlNodeType.Text:
Expand Down Expand Up @@ -491,16 +512,17 @@ public static SvgDocument Open(string path)
/// Opens an SVG document from the specified <see cref="XmlDocument"/>.
/// </summary>
/// <param name="document">The <see cref="XmlDocument"/> containing the SVG document XML.</param>
/// <param name="baseUri"> base uri for linked file</param>
/// <exception cref="ArgumentNullException">The <paramref name="document"/> parameter cannot be <c>null</c>.</exception>
public static SvgDocument Open(XmlDocument document)
public static SvgDocument Open(XmlDocument document, Uri baseUri = null)
{
if (document == null)
{
throw new ArgumentNullException("document");
}

var reader = new SvgNodeReader(document.DocumentElement, null);
return Open<SvgDocument>(reader);
return Open<SvgDocument>(reader, baseUri);
}

public static Bitmap OpenAsBitmap(string path)
Expand Down