Skip to content

Commit 88026af

Browse files
chuckermrbean-bremen
authored andcommitted
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
1 parent dcd30e6 commit 88026af

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

Source/SvgDocument.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,29 @@ public class SvgDocument : SvgFragment, ITypeDescriptorContext
2929
private Dictionary<string, IEnumerable<SvgFontFace>> _fontDefns = null;
3030

3131
private static int GetSystemDpi()
32-
{
33-
IntPtr hDC = GetDC(IntPtr.Zero);
34-
const int LOGPIXELSY = 90;
35-
int result = GetDeviceCaps(hDC, LOGPIXELSY);
36-
ReleaseDC(IntPtr.Zero, hDC);
37-
return result;
32+
{
33+
bool isWindows;
34+
35+
#if NETCORE
36+
isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
37+
#else
38+
isWindows = true;
39+
#endif
40+
41+
if (isWindows)
42+
{
43+
// NOTE: starting with Windows 8.1, the DPI is no longer system-wide but screen-specific
44+
IntPtr hDC = GetDC(IntPtr.Zero);
45+
const int LOGPIXELSY = 90;
46+
int result = GetDeviceCaps(hDC, LOGPIXELSY);
47+
ReleaseDC(IntPtr.Zero, hDC);
48+
return result;
49+
}
50+
else
51+
{
52+
// hack for macOS and Linux
53+
return 96;
54+
}
3855
}
3956

4057
[DllImport("gdi32.dll")]
@@ -104,7 +121,7 @@ public void OverwriteIdManager(SvgElementIdManager manager)
104121
/// </summary>
105122
public string ExternalCSSHref { get; set; }
106123

107-
#region ITypeDescriptorContext Members
124+
#region ITypeDescriptorContext Members
108125

109126
IContainer ITypeDescriptorContext.Container
110127
{
@@ -136,7 +153,7 @@ object IServiceProvider.GetService(Type serviceType)
136153
throw new NotImplementedException();
137154
}
138155

139-
#endregion
156+
#endregion
140157

141158
/// <summary>
142159
/// Retrieves the <see cref="SvgElement"/> with the specified ID.

Tests/Svg.UnitTests/DpiTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
namespace Svg.UnitTests
4+
{
5+
/// <summary>
6+
/// Test that basic graphics operations work. Currently only supported
7+
/// on Windows, macOS, and Linux.
8+
/// </summary>
9+
[TestClass]
10+
public class DpiTest
11+
{
12+
/// <summary>
13+
/// We should get a valid dpi (probably 72, 96 or similar).
14+
/// </summary>
15+
[TestMethod]
16+
public void TestDpiAboveZero()
17+
{
18+
Assert.IsTrue(SvgDocument.PointsPerInch > 0);
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)