|
19 | 19 | using System.IO; |
20 | 20 | using System.Linq; |
21 | 21 | using System.Text; |
| 22 | +using System.Xml; |
22 | 23 | using System.Xml.XPath; |
23 | 24 |
|
24 | 25 | namespace DocSharp.IO; |
@@ -179,10 +180,16 @@ public static Size GetDimensions(Stream stream, ImageFormat type) |
179 | 180 | case ImageFormat.Avif: // supported by web browsers |
180 | 181 | case ImageFormat.Heif: // supported by Safari |
181 | 182 | return DecodeAvifOrHeif(reader); |
| 183 | + //case ImageFormat.Jxl: // supported by Firefox and Safari |
| 184 | + // return DecodeJxl(reader); |
182 | 185 | case ImageFormat.Jxr: return DecodeJxr(reader); // supported by IE 11 |
183 | 186 | case ImageFormat.Tiff: return DecodeTiff(reader); // supported in DOCX and Safari |
184 | 187 | case ImageFormat.Jpeg2000: return DecodeJpeg2000(reader); // supported in DOCX and previous Safari versions |
185 | 188 | 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); |
186 | 193 | case ImageFormat.Wmf: return DecodeWmf(reader); // supported in DOCX and RTF |
187 | 194 | // Note: WMF size is in inches, all the others are in pixels. |
188 | 195 |
|
@@ -664,34 +671,42 @@ private static Size DecodeXml(Stream stream) |
664 | 671 | { |
665 | 672 | try |
666 | 673 | { |
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 | + })) |
671 | 678 | { |
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) |
681 | 685 | { |
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()) |
687 | 689 | 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 | + } |
688 | 703 | } |
689 | | - } |
690 | 704 |
|
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 | + } |
695 | 710 | } |
696 | 711 | } |
697 | 712 | catch (SystemException) |
|
0 commit comments