Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.2] Fix get_screen_dpi on macOS. #42478

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/classes/OS.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions platform/osx/os_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2471,8 +2471,15 @@ static int get_screen_index(NSScreen *screen) {
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;
Expand Down