Skip to content

Commit 912770e

Browse files
committed
Fix: SVG images were skipped in Markdown renderer due to stream error
1 parent 8d6bb8f commit 912770e

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
- Fix: empty table cells sometimes caused the document to become corrupted in Markdown to DOCX converter
66
- Fix: don't reset numbering in the target document when appending Markdown to an existing DOCX document (issue #12)
77
- Improvements to styles and lists handling when appending Markdown to an existing DOCX document (issue #12)
8+
- Heading styles used in Markdown to DOCX converter now inherit from default Word heading styles, so that collapsing/expanding is available.
9+
- Fix: SVG images are now preserved in Markdown to DOCX renderer
810

911
## 0.13.1 - 2025.08.31
1012

src/DocSharp.Common/IO/ImageHeader.cs

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.IO;
2020
using System.Linq;
2121
using System.Text;
22+
using System.Xml;
2223
using System.Xml.XPath;
2324

2425
namespace DocSharp.IO;
@@ -179,10 +180,16 @@ public static Size GetDimensions(Stream stream, ImageFormat type)
179180
case ImageFormat.Avif: // supported by web browsers
180181
case ImageFormat.Heif: // supported by Safari
181182
return DecodeAvifOrHeif(reader);
183+
//case ImageFormat.Jxl: // supported by Firefox and Safari
184+
// return DecodeJxl(reader);
182185
case ImageFormat.Jxr: return DecodeJxr(reader); // supported by IE 11
183186
case ImageFormat.Tiff: return DecodeTiff(reader); // supported in DOCX and Safari
184187
case ImageFormat.Jpeg2000: return DecodeJpeg2000(reader); // supported in DOCX and previous Safari versions
185188
case ImageFormat.Emf: return DecodeEmf(reader); // supported in DOCX and RTF
189+
//case ImageFormat.Pcx: // supported in DOCX
190+
// return DecodePcx(reader);
191+
//case ImageFormat.Pict: // supported in RTF
192+
// return DecodePict(reader);
186193
case ImageFormat.Wmf: return DecodeWmf(reader); // supported in DOCX and RTF
187194
// Note: WMF size is in inches, all the others are in pixels.
188195

@@ -664,34 +671,42 @@ private static Size DecodeXml(Stream stream)
664671
{
665672
try
666673
{
667-
var nav = new XPathDocument(stream).CreateNavigator();
668-
// use local-name() to ignore any xml namespace
669-
nav = nav.SelectSingleNode("/*[local-name() = 'svg']");
670-
if (nav is not null)
674+
using (var xmlReader = XmlReader.Create(stream, new XmlReaderSettings()
675+
{
676+
CloseInput = false
677+
}))
671678
{
672-
var width = Unit.Parse(nav.GetAttribute("width", string.Empty), UnitMetric.Pixel);
673-
var height = Unit.Parse(nav.GetAttribute("height", string.Empty), UnitMetric.Pixel);
674-
if (width.IsValid && width.Type.IsValid() && height.IsValid && height.Type.IsValid())
675-
return new Size(width.ValueInPx, height.ValueInPx);
676-
677-
// If width or height are not found or use unsupported units (%, auto, unitless),
678-
// try to get the viewBox
679-
var viewBox = nav.GetAttribute("viewBox", string.Empty);
680-
if (!string.IsNullOrWhiteSpace(viewBox))
679+
var doc = new XPathDocument(xmlReader);
680+
var nav = doc.CreateNavigator();
681+
682+
// use local-name() to ignore any xml namespace
683+
nav = nav.SelectSingleNode("/*[local-name() = 'svg']");
684+
if (nav is not null)
681685
{
682-
var rectParts = viewBox.Split([' ', ',', ';'], StringSplitOptions.RemoveEmptyEntries);
683-
if (rectParts.Length == 4)
684-
{
685-
width = Unit.Parse(rectParts[2], UnitMetric.Pixel);
686-
height = Unit.Parse(rectParts[3], UnitMetric.Pixel);
686+
var width = Unit.Parse(nav.GetAttribute("width", string.Empty), UnitMetric.Pixel);
687+
var height = Unit.Parse(nav.GetAttribute("height", string.Empty), UnitMetric.Pixel);
688+
if (width.IsValid && width.Type.IsValid() && height.IsValid && height.Type.IsValid())
687689
return new Size(width.ValueInPx, height.ValueInPx);
690+
691+
// If width or height are not found or use unsupported units (%, auto, unitless),
692+
// try to get the viewBox
693+
var viewBox = nav.GetAttribute("viewBox", string.Empty);
694+
if (!string.IsNullOrWhiteSpace(viewBox))
695+
{
696+
var rectParts = viewBox.Split([' ', ',', ';'], StringSplitOptions.RemoveEmptyEntries);
697+
if (rectParts.Length == 4)
698+
{
699+
width = Unit.Parse(rectParts[2], UnitMetric.Pixel);
700+
height = Unit.Parse(rectParts[3], UnitMetric.Pixel);
701+
return new Size(width.ValueInPx, height.ValueInPx);
702+
}
688703
}
689-
}
690704

691-
// If viewBox is not found assume unsupported units as pixels
692-
// (at least aspect ratio is preserved, better than returning 0).
693-
if (width.IsValid && height.IsValid)
694-
return new Size(width.ValueInPx, height.ValueInPx);
705+
// If viewBox is not found assume unsupported units as pixels
706+
// (at least aspect ratio is preserved, better than returning 0).
707+
if (width.IsValid && height.IsValid)
708+
return new Size(width.ValueInPx, height.ValueInPx);
709+
}
695710
}
696711
}
697712
catch (SystemException)

src/DocSharp.Markdown/Common/ImageRenderHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,4 @@ public static void ScaleImageSize(ref long width, ref long height, long maxWidth
286286
return null;
287287
}
288288
}
289-
}
289+
}

0 commit comments

Comments
 (0)