Skip to content

Commit

Permalink
Don't use Windows-specific calls if not on Windows
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
chucker authored and mrbean-bremen committed May 9, 2019
1 parent dcd30e6 commit 88026af
Showing 2 changed files with 46 additions and 8 deletions.
33 changes: 25 additions & 8 deletions Source/SvgDocument.cs
Original file line number Diff line number Diff line change
@@ -29,12 +29,29 @@ public class SvgDocument : SvgFragment, ITypeDescriptorContext
private Dictionary<string, IEnumerable<SvgFontFace>> _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)
/// </summary>
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

/// <summary>
/// Retrieves the <see cref="SvgElement"/> with the specified ID.
21 changes: 21 additions & 0 deletions Tests/Svg.UnitTests/DpiTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Svg.UnitTests
{
/// <summary>
/// Test that basic graphics operations work. Currently only supported
/// on Windows, macOS, and Linux.
/// </summary>
[TestClass]
public class DpiTest
{
/// <summary>
/// We should get a valid dpi (probably 72, 96 or similar).
/// </summary>
[TestMethod]
public void TestDpiAboveZero()
{
Assert.IsTrue(SvgDocument.PointsPerInch > 0);
}
}
}

0 comments on commit 88026af

Please sign in to comment.