Skip to content

Commit 18a0af0

Browse files
committed
Auto-detect HiDPI fractional scaling settings on Linux
1 parent 37f95bd commit 18a0af0

File tree

2 files changed

+99
-41
lines changed
  • harness/apisupport.harness/release/launchers
  • nb/ide.launcher/unix

2 files changed

+99
-41
lines changed

harness/apisupport.harness/release/launchers/app.sh

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,39 +152,68 @@ case "`uname`" in
152152
fi
153153

154154
# See longer comments in nb/ide.launcher/unix/netbeans.
155-
if [ "`command xrdb -query 2> /dev/null | grep Xft.dpi | cut -d ':' -f2 | xargs`" = 192 ]
156-
then
157-
echo "Detected 2x HiDPI scaling in Xft.dpi setting; setting GDK_SCALE=2"
158-
export GDK_SCALE=2
155+
detected_dpi=""
156+
157+
xft_dpi="`command xrdb -query 2>/dev/null | grep Xft.dpi | cut -d ':' -f2 | xargs`"
158+
if [ -z "$GDK_SCALE" ] && [ -z "$J2D_UISCALE" ] && [ -n "$xft_dpi" ] && [ "$xft_dpi" -gt 96 ] && [ "$xft_dpi" -le 192 ]; then
159+
detected_dpi="$xft_dpi"
160+
echo -n "Detected $detected_dpi DPI in Xft.dpi setting; "
159161
fi
160-
if [ "`command xdpyinfo 2> /dev/null | grep 'resolution:.*dots per inch' | cut -d ':' -f2 | cut -d 'x' -f1 | sort -u | xargs`" = 192 ]
161-
then
162-
echo "Detected 192 DPI on all screens in xdpyinfo; setting GDK_SCALE=2"
163-
export GDK_SCALE=2
162+
163+
if [ -z "$xft_dpi" ]; then
164+
xdpyinfo_dpi="`command xdpyinfo 2>/dev/null | grep 'resolution:.*dots per inch' | cut -d ':' -f2 | cut -d 'x' -f1 | sort -u | xargs`"
165+
case "$xdpyinfo_dpi" in
166+
*[!0-9]*) ;;
167+
"") ;;
168+
*)
169+
if [ -z "$GDK_SCALE" ] && [ -z "$J2D_UISCALE" ] && [ "$xdpyinfo_dpi" -gt 96 ] && [ "$xdpyinfo_dpi" -le 192 ]; then
170+
detected_dpi="$xdpyinfo_dpi"
171+
echo -n "Detected $detected_dpi DPI on all screens in xdpyinfo; "
172+
fi
173+
;;
174+
esac
164175
fi
165176

166177
extra_automatic_options=""
167178

179+
if [ -z "$GDK_SCALE" ] && [ -z "$J2D_UISCALE" ] && [ -n "$detected_dpi" ] && [ "$detected_dpi" -gt 96 ] && [ "$detected_dpi" -le 192 ]; then
180+
extra_automatic_options="-J-Dsun.java2d.uiScale=2"
181+
echo -n "using explicit setting for Java UI scaling (-J-Dsun.java2d.uiScale=2)"
182+
if [ "$detected_dpi" -gt 96 ] && [ "$detected_dpi" -lt 192 ]; then
183+
scaling_factor=`awk -v dpi="$detected_dpi" 'BEGIN {print dpi / 192}'`
184+
extra_automatic_options="$extra_automatic_options -J-Dflatlaf.uiScale=$scaling_factor"
185+
echo -n " and FlatLaf UI scaling (-J-Dflatlaf.uiScale=$scaling_factor)"
186+
fi
187+
echo
188+
fi
189+
168190
# See longer comments in nb/ide.launcher/unix/netbeans.
169191
if [ ! -z "$KDE_FULL_SESSION" ] ; then
192+
antialiasing_font_settings=""
193+
170194
case "`command xrdb -query 2> /dev/null | grep Xft.rgba | cut -d ':' -f2 | xargs`" in
171195
rgb)
172-
extra_automatic_options="-J-Dawt.useSystemAAFontSettings=lcd_hrgb"
196+
antialiasing_font_settings="-J-Dawt.useSystemAAFontSettings=lcd_hrgb"
173197
;;
174198
bgr)
175-
extra_automatic_options="-J-Dawt.useSystemAAFontSettings=lcd_hbgr"
199+
antialiasing_font_settings="-J-Dawt.useSystemAAFontSettings=lcd_hbgr"
176200
;;
177201
vrgb)
178-
extra_automatic_options="-J-Dawt.useSystemAAFontSettings=lcd_vrgb"
202+
antialiasing_font_settings="-J-Dawt.useSystemAAFontSettings=lcd_vrgb"
179203
;;
180204
vbgr)
181-
extra_automatic_options="-J-Dawt.useSystemAAFontSettings=lcd_vbgr"
205+
antialiasing_font_settings="-J-Dawt.useSystemAAFontSettings=lcd_vbgr"
182206
;;
183207
*)
184-
extra_automatic_options="-J-Dawt.useSystemAAFontSettings=on"
208+
antialiasing_font_settings="-J-Dawt.useSystemAAFontSettings=on"
185209
;;
186210
esac
187-
echo "Detected KDE; use explicit setting for font antialiasing ($extra_automatic_options)"
211+
echo "Detected KDE; using explicit setting for font antialiasing ($antialiasing_font_settings)"
212+
if [ -z "$extra_automatic_options" ]; then
213+
extra_automatic_options="$antialiasing_font_settings"
214+
else
215+
extra_automatic_options="$extra_automatic_options $antialiasing_font_settings"
216+
fi
188217
fi
189218

190219
# Add extra_automatic_options before default_options, to allow system

nb/ide.launcher/unix/netbeans

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,10 @@ case "`uname`" in
164164
'"$@"'
165165
;;
166166
*)
167-
# Support 2x HiDPI scaling on Linux systems that have configured this via Xft.dpi but not via
168-
# GDK_SCALE, notably Ubuntu as of 20.04.1. Xft.dpi technically relates to the size of fonts
169-
# only, but Ubuntu sets it when the "Scaling" setting is changed in the "Displays" settings
170-
# UI. OpenJDK supports the GDK_SCALE setting at the GraphicsConfiguration level, but not
171-
# Xft.dpi (as of 2020-11-22 and LTS 11.0.9). Individual LAFs may react to the latter, though
172-
# FlatLAF in particular works correctly even both are set at the same time.
173-
#
174-
# OpenJDK does not support fractional scalings in GDK_SCALE, so we only handle the 2x case
175-
# here. OpenJDK also does not query the separate GDK_DPI_SCALE variable.
167+
# Support HiDPI scaling on Linux systems that have configured this via Xft.dpi but not via
168+
# GDK_SCALE, notably Ubuntu and Fedora. Xft.dpi technically relates to the size of fonts
169+
# only, but Ubuntu and Fedora set it when the "Scaling" setting is changed in the "Displays"
170+
# settings UI.
176171
#
177172
# We do not attempt to support per-monitor DPI scalings here. OpenJDK might support this
178173
# through the ubuntu.user-interface/scale-factor setting, but as of Ubuntu 20.04.1, the
@@ -181,32 +176,59 @@ case "`uname`" in
181176
# saying it is "not well supported yet" (presumably in their own OpenJDK fork).
182177
# https://intellij-support.jetbrains.com/hc/en-us/articles/360007994999-HiDPI-configuration
183178

179+
detected_dpi=""
180+
184181
# If the xrdb command does not exist, no action will be taken here.
185-
if [ "`command xrdb -query 2> /dev/null | grep Xft.dpi | cut -d ':' -f2 | xargs`" = 192 ]
186-
then
187-
echo "Detected 2x HiDPI scaling in Xft.dpi setting; setting GDK_SCALE=2"
188-
export GDK_SCALE=2
182+
xft_dpi="`command xrdb -query 2>/dev/null | grep Xft.dpi | cut -d ':' -f2 | xargs`"
183+
if [ -z "$GDK_SCALE" ] && [ -z "$J2D_UISCALE" ] && [ -n "$xft_dpi" ] && [ "$xft_dpi" -gt 96 ] && [ "$xft_dpi" -le 192 ]; then
184+
detected_dpi="$xft_dpi"
185+
echo -n "Detected $detected_dpi DPI in Xft.dpi setting; "
189186
fi
190187

191-
# Handle another case that indicates a need for 2x HiDPI scaling, observed on openSUSE
192-
# Tumbleweed (see NETBEANS-2360). A user with a HiDPI monitor and 2x HiDPI scaling
188+
# Handle another case that indicates a need for HiDPI scaling, observed on openSUSE
189+
# Tumbleweed (see NETBEANS-2360). A user with a HiDPI monitor and HiDPI scaling
193190
# enabled reported that "xdpyinfo | grep -B 2 resolution" yielded the following:
194191
#
195192
# screen #0:
196193
# dimensions: 3840x2160 pixels (508x285 millimeters)
197194
# resolution: 192x193 dots per inch
198195
#
199196
# Xft.dpi was not set in this case, however. In the following test, we
200-
# set GDK_SCALE=2 if _all_ monitors report a resolution of "192x"
201-
# something (ignoring the odd "193" figure observed above).
202-
if [ "`command xdpyinfo 2> /dev/null | grep 'resolution:.*dots per inch' | cut -d ':' -f2 | cut -d 'x' -f1 | sort -u | xargs`" = 192 ]
203-
then
204-
echo "Detected 192 DPI on all screens in xdpyinfo; setting GDK_SCALE=2"
205-
export GDK_SCALE=2
197+
# enable HiDPI scaling if _all_ monitors report a resolution of
198+
# "(97-192)xsomething" (ignoring the odd 193 figure observed above).
199+
if [ -z "$xft_dpi" ]; then
200+
xdpyinfo_dpi="`command xdpyinfo 2>/dev/null | grep 'resolution:.*dots per inch' | cut -d ':' -f2 | cut -d 'x' -f1 | sort -u | xargs`"
201+
case "$xdpyinfo_dpi" in
202+
*[!0-9]*) ;;
203+
"") ;;
204+
*)
205+
if [ -z "$GDK_SCALE" ] && [ -z "$J2D_UISCALE" ] && [ "$xdpyinfo_dpi" -gt 96 ] && [ "$xdpyinfo_dpi" -le 192 ]; then
206+
detected_dpi="$xdpyinfo_dpi"
207+
echo -n "Detected $detected_dpi DPI on all screens in xdpyinfo; "
208+
fi
209+
;;
210+
esac
206211
fi
207212

208213
extra_automatic_options=""
209214

215+
# OpenJDK does not support fractional scalings in GDK_SCALE. OpenJDK also does not query the
216+
# separate GDK_DPI_SCALE variable. In theory FlatLaf can do fractional scaling, but in
217+
# practice not all parts of NetBeans are scaled properly with a flatlaf.uiScale value
218+
# between 1 and 2. But FlatLaf also supports fractional scaling smaller than 1, so we make
219+
# Java scale by 2 with a sun.java2d.uiScale value of 2, then we make FlatLaf scale down with
220+
# a flatlaf.uiScale value between 0 and 1.
221+
if [ -z "$GDK_SCALE" ] && [ -z "$J2D_UISCALE" ] && [ -n "$detected_dpi" ] && [ "$detected_dpi" -gt 96 ] && [ "$detected_dpi" -le 192 ]; then
222+
extra_automatic_options="-J-Dsun.java2d.uiScale=2"
223+
echo -n "using explicit setting for Java UI scaling (-J-Dsun.java2d.uiScale=2)"
224+
if [ "$detected_dpi" -gt 96 ] && [ "$detected_dpi" -lt 192 ]; then
225+
scaling_factor=`awk -v dpi="$detected_dpi" 'BEGIN {print dpi / 192}'`
226+
extra_automatic_options="$extra_automatic_options -J-Dflatlaf.uiScale=$scaling_factor"
227+
echo -n " and FlatLaf UI scaling (-J-Dflatlaf.uiScale=$scaling_factor)"
228+
fi
229+
echo
230+
fi
231+
210232
# Java/AWT/Swing will correctly detect text anti-aliasing settings on
211233
# GNOME, but not (always) on KDE. Force anti-aliasing on in this case
212234
# using the "awt.useSystemAAFontSettings" property, as recommended in
@@ -227,26 +249,33 @@ case "`uname`" in
227249
# Try to detect the correct subpixel antialiasing mode
228250
# (https://github.com/apache/netbeans/issues/4228)
229251

252+
antialiasing_font_settings=""
253+
230254
# See https://docs.gtk.org/gtk4/property.Settings.gtk-xft-rgba.html
231255
# See https://docs.oracle.com/javase/7/docs/technotes/guides/2d/flags.html#aaFonts
232256
case "`command xrdb -query 2> /dev/null | grep Xft.rgba | cut -d ':' -f2 | xargs`" in
233257
rgb)
234-
extra_automatic_options="-J-Dawt.useSystemAAFontSettings=lcd_hrgb"
258+
antialiasing_font_settings="-J-Dawt.useSystemAAFontSettings=lcd_hrgb"
235259
;;
236260
bgr)
237-
extra_automatic_options="-J-Dawt.useSystemAAFontSettings=lcd_hbgr"
261+
antialiasing_font_settings="-J-Dawt.useSystemAAFontSettings=lcd_hbgr"
238262
;;
239263
vrgb)
240-
extra_automatic_options="-J-Dawt.useSystemAAFontSettings=lcd_vrgb"
264+
antialiasing_font_settings="-J-Dawt.useSystemAAFontSettings=lcd_vrgb"
241265
;;
242266
vbgr)
243-
extra_automatic_options="-J-Dawt.useSystemAAFontSettings=lcd_vbgr"
267+
antialiasing_font_settings="-J-Dawt.useSystemAAFontSettings=lcd_vbgr"
244268
;;
245269
*)
246-
extra_automatic_options="-J-Dawt.useSystemAAFontSettings=on"
270+
antialiasing_font_settings="-J-Dawt.useSystemAAFontSettings=on"
247271
;;
248272
esac
249-
echo "Detected KDE; use explicit setting for font antialiasing ($extra_automatic_options)"
273+
echo "Detected KDE; using explicit setting for font antialiasing ($antialiasing_font_settings)"
274+
if [ -z "$extra_automatic_options" ]; then
275+
extra_automatic_options="$antialiasing_font_settings"
276+
else
277+
extra_automatic_options="$extra_automatic_options $antialiasing_font_settings"
278+
fi
250279
fi
251280

252281
# Add extra_automatic_options before default_options, to allow system

0 commit comments

Comments
 (0)