[LyX/master] Fix support for screen fractional scaling with Wayland

Jean-Marc Lasgouttes lasgouttes at lyx.org
Thu Feb 29 09:41:37 UTC 2024


commit 8f61b0859c9bcb8da71f43f94dcf62da41b84ea4
Author: Jean-Marc Lasgouttes <lasgouttes at lyx.org>
Date:   Sun Feb 25 20:17:31 2024 +0100

    Fix support for screen fractional scaling with Wayland
    
    The display was wrong when a backing store is in use (which is the case
    with Wayland). To fix this in GuiWorkArea::Private::resetScreen(), the
    pixelRatio is now stored as a double instead of an int.
    
    Concerning support for QT_SCALE_FACTOR, the existing code was wrong
    because this value is already taken into account in devicePixelRatioF
    for Qt > 5.6 (no fractional scaling support before that). The
    situation is as follows:
    
                       Qt < 5.6    5.6 <= Qt < 6  Qt 6
    devicePixelRatio   int(ratio)  int(ratio)     ratio
    devicePixelRatioF  N/A         ratio          ratio
    
    So it is only between Qt 5.6 and Qt 6 that devicePixelRatioF() has to
    be used instead of devicePixelRatio().
    QGuiApplication::devicePixelRatio() does not have a 'F' version, it
    always returns the real thing.
    
    Fixes ticket #13039.
---
 src/LyX.cpp                         | 20 --------------------
 src/LyX.h                           |  1 -
 src/frontends/qt/GuiApplication.cpp |  4 ++--
 src/frontends/qt/GuiView.cpp        | 12 ++++++++++--
 src/frontends/qt/GuiWorkArea.cpp    | 12 ++++++++----
 5 files changed, 20 insertions(+), 29 deletions(-)

diff --git a/src/LyX.cpp b/src/LyX.cpp
index 21a84e249e..c0dbd019b9 100644
--- a/src/LyX.cpp
+++ b/src/LyX.cpp
@@ -50,7 +50,6 @@
 #include "frontends/Application.h"
 
 #include "support/ConsoleApplication.h"
-#include "support/convert.h"
 #include "support/lassert.h"
 #include "support/debug.h"
 #include "support/environment.h"
@@ -121,14 +120,6 @@ RunMode run_mode = PREFERRED;
 OverwriteFiles force_overwrite = UNSPECIFIED;
 
 
-// Scale the GUI by this factor. This works whether we have a HiDpi screen
-// or not and scales everything, also fonts. Can only be changed by setting
-// the QT_SCALE_FACTOR environment variable before launching LyX and only
-// works properly with Qt 5.6 or higher.
-
-double qt_scale_factor = 1.0;
-
-
 namespace {
 
 // Filled with the command line arguments "foo" of "-sysdir foo" or
@@ -318,17 +309,6 @@ int LyX::exec(int & argc, char * argv[])
 	// we need to parse for "-dbg" and "-help"
 	easyParse(argc, argv);
 
-#if QT_VERSION >= 0x050600
-	// Check whether Qt will scale all GUI elements and accordingly
-	// set the scale factor so that to avoid blurred images and text
-	char const * const scale_factor = getenv("QT_SCALE_FACTOR");
-	if (scale_factor) {
-		qt_scale_factor = convert<double>(scale_factor);
-		if (qt_scale_factor < 1.0)
-			qt_scale_factor = 1.0;
-	}
-#endif
-
 	try {
 		init_package(os::utf8_argv(0), cl_system_support, cl_user_support);
 	} catch (ExceptionMessage const & message) {
diff --git a/src/LyX.h b/src/LyX.h
index 162151286a..c35464e210 100644
--- a/src/LyX.h
+++ b/src/LyX.h
@@ -56,7 +56,6 @@ extern bool verbose;
 extern bool ignore_missing_glyphs;
 extern RunMode run_mode;
 extern OverwriteFiles force_overwrite;
-extern double qt_scale_factor;
 
 namespace frontend {
 class Application;
diff --git a/src/frontends/qt/GuiApplication.cpp b/src/frontends/qt/GuiApplication.cpp
index 26288a5f55..a70ed358c5 100644
--- a/src/frontends/qt/GuiApplication.cpp
+++ b/src/frontends/qt/GuiApplication.cpp
@@ -1247,7 +1247,7 @@ GuiApplication * theGuiApp()
 
 double GuiApplication::pixelRatio() const
 {
-	return qt_scale_factor * devicePixelRatio();
+	return devicePixelRatio();
 }
 
 
@@ -2669,7 +2669,7 @@ QPixmap GuiApplication::getScaledPixmap(QString imagedir, QString name) const
 	qreal dpr = 1.0;
 	// Consider device/pixel ratio (HiDPI)
 	if (currentView())
-		dpr = currentView()->devicePixelRatio();
+		dpr = currentView()->pixelRatio();
 	// We render SVG directly for HiDPI scalability
 	QPixmap pm = getPixmap(imagedir, name, "svgz,png");
 	FileName fname = imageLibFileSearch(imagedir, name, "svgz,png");
diff --git a/src/frontends/qt/GuiView.cpp b/src/frontends/qt/GuiView.cpp
index 8a97e10417..facdb81b67 100644
--- a/src/frontends/qt/GuiView.cpp
+++ b/src/frontends/qt/GuiView.cpp
@@ -260,7 +260,11 @@ private:
 
 	/// Current ratio between physical pixels and device-independent pixels
 	double pixelRatio() const {
-		return qt_scale_factor * devicePixelRatio();
+#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) && QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
+		return devicePixelRatioF();
+#else
+		return devicePixelRatio();
+#endif
 	}
 
 	qreal fontSize() const {
@@ -1844,7 +1848,11 @@ void GuiView::resetCommandExecute()
 
 double GuiView::pixelRatio() const
 {
-	return qt_scale_factor * devicePixelRatio();
+#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) && QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
+	return devicePixelRatioF();
+#else
+	return devicePixelRatio();
+#endif
 }
 
 
diff --git a/src/frontends/qt/GuiWorkArea.cpp b/src/frontends/qt/GuiWorkArea.cpp
index 978819c4ef..a4b874097c 100644
--- a/src/frontends/qt/GuiWorkArea.cpp
+++ b/src/frontends/qt/GuiWorkArea.cpp
@@ -179,7 +179,11 @@ GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & gv)
 
 double GuiWorkArea::pixelRatio() const
 {
-	return qt_scale_factor * devicePixelRatio();
+#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) && QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
+	return devicePixelRatioF();
+#else
+	return devicePixelRatio();
+#endif
 }
 
 
@@ -1264,9 +1268,9 @@ void GuiWorkArea::Private::paintPreeditText(GuiPainter & pain)
 void GuiWorkArea::Private::resetScreen()
 {
 	if (use_backingstore_) {
-		int const pr = p->pixelRatio();
-		screen_ = QImage(pr * p->viewport()->width(),
-		                 pr * p->viewport()->height(),
+		double const pr = p->pixelRatio();
+		screen_ = QImage(int(pr * p->viewport()->width()),
+		                 int(pr * p->viewport()->height()),
 		                 QImage::Format_ARGB32_Premultiplied);
 		screen_.setDevicePixelRatio(pr);
 	}


More information about the lyx-cvs mailing list