[3.2] Fix get_screen_dpi on macOS for non fractional display scales and update documentation.

This commit is contained in:
bruvzg 2020-10-01 22:55:54 +03:00
parent 8d36b7206c
commit 610728c9f1
No known key found for this signature in database
GPG key ID: FCED35F1CECE0D3A
2 changed files with 11 additions and 3 deletions

View file

@ -361,7 +361,8 @@
</argument>
<description>
Returns the dots per inch density of the specified screen. If [code]screen[/code] is [/code]-1[/code] (the default value), the current screen will be used.
On Android devices, the actual screen densities are grouped into six generalized densities:
[b]Note:[/b] On macOS, returned value is inaccurate if fractional display scaling mode is used.
[b]Note:[/b] On Android devices, the actual screen densities are grouped into six generalized densities:
[codeblock]
ldpi - 120 dpi
mdpi - 160 dpi

View file

@ -2471,8 +2471,15 @@ int OS_OSX::get_screen_dpi(int p_screen) const {
NSArray *screenArray = [NSScreen screens];
if ((NSUInteger)p_screen < [screenArray count]) {
NSDictionary *description = [[screenArray objectAtIndex:p_screen] deviceDescription];
NSSize displayDPI = [[description objectForKey:NSDeviceResolution] sizeValue];
return (displayDPI.width + displayDPI.height) / 2;
const NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
const CGSize displayPhysicalSize = CGDisplayScreenSize([[description objectForKey:@"NSScreenNumber"] unsignedIntValue]);
float scale = [[screenArray objectAtIndex:p_screen] backingScaleFactor];
float den2 = (displayPhysicalSize.width / 25.4f) * (displayPhysicalSize.width / 25.4f) + (displayPhysicalSize.height / 25.4f) * (displayPhysicalSize.height / 25.4f);
if (den2 > 0.0f) {
return ceil(sqrt(displayPixelSize.width * displayPixelSize.width + displayPixelSize.height * displayPixelSize.height) / sqrt(den2) * scale);
}
}
return 72;