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 1a01bc9 commit dc57aa1
Show file tree
Hide file tree
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
Expand Up @@ -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")]
Expand Down Expand Up @@ -104,7 +121,7 @@ public void OverwriteIdManager(SvgElementIdManager manager)
/// </summary>
public string ExternalCSSHref { get; set; }

#region ITypeDescriptorContext Members
#region ITypeDescriptorContext Members

IContainer ITypeDescriptorContext.Container
{
Expand Down Expand Up @@ -136,7 +153,7 @@ object IServiceProvider.GetService(Type serviceType)
throw new NotImplementedException();
}

#endregion
#endregion

/// <summary>
/// Retrieves the <see cref="SvgElement"/> with the specified ID.
Expand Down
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 dc57aa1

Please sign in to comment.