From 88026afd618319284061680850d14a8d2fb19e58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?So=CC=88ren=20Kuklau?= Date: Thu, 9 May 2019 20:31:27 +0200 Subject: [PATCH] Don't use Windows-specific calls if not on Windows - don't use Win32-specific interop calls to determine the DPI if not on Windows - possible fix for #427 - add a barebones unit test for this - note that even the Windows code isn't correct as soon as you have multiple screens - on macOS, causes 20 instead of 5 unit tests to pass --- Source/SvgDocument.cs | 33 +++++++++++++++++++++++++-------- Tests/Svg.UnitTests/DpiTest.cs | 21 +++++++++++++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 Tests/Svg.UnitTests/DpiTest.cs diff --git a/Source/SvgDocument.cs b/Source/SvgDocument.cs index b563cdde0..677462487 100644 --- a/Source/SvgDocument.cs +++ b/Source/SvgDocument.cs @@ -29,12 +29,29 @@ public class SvgDocument : SvgFragment, ITypeDescriptorContext private Dictionary> _fontDefns = null; private static int GetSystemDpi() - { - IntPtr hDC = GetDC(IntPtr.Zero); - const int LOGPIXELSY = 90; - int result = GetDeviceCaps(hDC, LOGPIXELSY); - ReleaseDC(IntPtr.Zero, hDC); - return result; + { + bool isWindows; + +#if NETCORE + isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); +#else + isWindows = true; +#endif + + if (isWindows) + { + // NOTE: starting with Windows 8.1, the DPI is no longer system-wide but screen-specific + IntPtr hDC = GetDC(IntPtr.Zero); + const int LOGPIXELSY = 90; + int result = GetDeviceCaps(hDC, LOGPIXELSY); + ReleaseDC(IntPtr.Zero, hDC); + return result; + } + else + { + // hack for macOS and Linux + return 96; + } } [DllImport("gdi32.dll")] @@ -104,7 +121,7 @@ public void OverwriteIdManager(SvgElementIdManager manager) /// public string ExternalCSSHref { get; set; } - #region ITypeDescriptorContext Members +#region ITypeDescriptorContext Members IContainer ITypeDescriptorContext.Container { @@ -136,7 +153,7 @@ object IServiceProvider.GetService(Type serviceType) throw new NotImplementedException(); } - #endregion +#endregion /// /// Retrieves the with the specified ID. diff --git a/Tests/Svg.UnitTests/DpiTest.cs b/Tests/Svg.UnitTests/DpiTest.cs new file mode 100644 index 000000000..a4478551a --- /dev/null +++ b/Tests/Svg.UnitTests/DpiTest.cs @@ -0,0 +1,21 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Svg.UnitTests +{ + /// + /// Test that basic graphics operations work. Currently only supported + /// on Windows, macOS, and Linux. + /// + [TestClass] + public class DpiTest + { + /// + /// We should get a valid dpi (probably 72, 96 or similar). + /// + [TestMethod] + public void TestDpiAboveZero() + { + Assert.IsTrue(SvgDocument.PointsPerInch > 0); + } + } +}