Hidpi, font&icon scaling, etc....

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
User avatar
vanuan
Posts: 449
Joined: Wed Oct 24, 2018 9:49 pm

Re: Hidpi, font&icon scaling, etc....

Postby vanuan » Wed Sep 16, 2020 9:35 pm

Elyas wrote:
Wed Sep 16, 2020 8:38 pm
Before your change the default font size for EditSketcherFontSize was 17 pixels. After your change the default font size is 12 pixels multiplied by the dpi scale factor. So, before it was 17 hardware pixels, after it is 12 device independent pixels.
The bug is:
If in Prefereces editSktcherFontSize = 17 (px - is it means pixels?)
According to the current code and screenshots, it is being used as pixels. Haven't you read SoFont class documentation? Do you see the "px" next to the "Sketcher - Sketch editing - Font size" number input?

Unfortunately, settings don't have units enforcement. So it's hard to detect when they are not properly used in code.
Elyas wrote:
Wed Sep 16, 2020 8:38 pm
The real font size in Sketcher window is not 17 pixels & not 17 points.(Coin font size - it is not pixels)
According to your screenshots, you set it to 25px. 17px is a default value. 25px is 18.75pt. But since you probably have HiDPI scaling enabled, that is factored by some constant (from 100% to 400%).

Coin font size is in pixels for 2D text (a SoText2 node).
Elyas wrote:
Wed Sep 16, 2020 8:38 pm
After my edits font size in window match preferences. (means pixels)
Is it wrong direction?
Well, when we speak of pixels, you have to specify whether those are device independent pixels or absolute pixels. Points are better because those are not ambiguous and related to inches on the screen or paper. While pixel meaning can change depending on the DPI scale factor.

It seems your solution is a hack: it works for your font settings / screen size / resolution / dpi scale ratio but you can't explain how and can't guarantee it would work for all users. Did you match it by eye? Or you can provide a formula to demonstrate that "editSketcherFontSize" is properly scaled to accommodate for HiDPI ratio?
Elyas wrote:
Wed Sep 16, 2020 8:38 pm

What do you suggest?
To define font size in Preferences as ponts - may be it right way. But this does not solve most of the questions.
Yes, specifying all font sizes in points and getting rid of any pixel settings should be the end goal for a proper HiDPI support.

On the wiki I provided a clear path to that state. All sizes in pixels should be either scaled by dpi ratio or calculated from the text size. All font sizes should be in points. If you need the font size in pixels, that should be calculated by taking into account the dpi ratio. If you need the font size to change dynamically when changing the window/elements size, that's a separate feature completely independent from HiDPI support.

Ideally, there should be only one setting - "base font size". And all the other font sizes should be specified in relative units - rems or ems. But probably it would be easier to implement in the browser-based UI.
Elyas wrote:
Wed Sep 16, 2020 8:38 pm
NaviCube:
Neither before nor after the corrections the user has any control over the font size. There is no conflict. If you want to automatically scale the NaviCube dimensions, it is possible, but that is a different task. Matching the font size and cube should still be. Now it is being done wrong.
True, there's no separate setting to specify a font size for the cube. But there is a global setting to scale all the font sizes up or down. It is called "dpi scale ratio" and is available in the OS display settings. Qt supports this setting automatically, so developer doesn't have to do anything.

But Coin, unfortunately, doesn't support this. So the dpi scale ratio should be taken into account separately.

As I see from you code, you just changed some magic constant which I suppose was chosen arbitrarily. It doesn't seem to support HiDPI font scaling. So users with HiDPI will need to increase the cube size, while users with small resolution displays will need to decrease the cube size. This is a bad user experience. The default size should work nicely without any fiddling with settings for users with normal eye sight and an appropriate DPI ratio..
Elyas
Posts: 22
Joined: Fri Sep 04, 2020 12:25 pm

Re: Hidpi, font&icon scaling, etc....

Postby Elyas » Mon Sep 28, 2020 8:43 am

4. Tiny icon size of Link objects in tree
Tree_old.png
Tree_old.png (92.47 KiB) Viewed 140 times

Code: Select all

--- a/src/Gui/ViewProviderLink.cpp
+++ b/src/Gui/ViewProviderLink.cpp
@@ -649,9 +649,7 @@ public:
     }
 
     QIcon getIcon(QPixmap px) {
-        static int iconSize = -1;
-        if(iconSize < 0) 
-            iconSize = QApplication::style()->standardPixmap(QStyle::SP_DirClosedIcon).width();
+        static int iconSize = 64;
 
         if(!isLinked())
             return QIcon();
Qt can auto scale down, but not up.
Tree_64.png
Tree_64.png (95.7 KiB) Viewed 140 times
Elyas
Posts: 22
Joined: Fri Sep 04, 2020 12:25 pm

Re: Hidpi, font&icon scaling, etc....

Postby Elyas » Tue Sep 29, 2020 11:52 am

Adjusting NaviCube font without magic numbers:

Code: Select all

--- a/src/Gui/NaviCube.cpp
+++ b/src/Gui/NaviCube.cpp
@@ -203,7 +203,7 @@ private:
 	void rotateView(int ,float );
 
 	QString str(char* str);
-	char* enum2str(int);
+    const char* enum2str(int);
 	QMenu* createNaviCubeMenu();
 public:
 	enum { //
@@ -271,6 +271,7 @@ public:
 #endif
 	static vector<string> m_commands;
 	static vector<string> m_labels;
+    int m_adjustedFontSize;
 	QMenu* m_Menu;
 };
 
@@ -337,6 +338,7 @@ NaviCubeImplementation::NaviCubeImplementation(
 	m_PickingFramebuffer = NULL;
 	m_CubeWidgetSize = (hGrp->GetInt("CubeSize", 132));
 	m_Menu = createNaviCubeMenu();
+    m_adjustedFontSize = 0;
 }
 
 NaviCubeImplementation::~NaviCubeImplementation() {
@@ -351,7 +353,7 @@ NaviCubeImplementation::~NaviCubeImplementation() {
 #endif
 }
 
-char* NaviCubeImplementation::enum2str(int e) {
+const char* NaviCubeImplementation::enum2str(int e) {
 	switch (e) {
 	default : return "???";
 	case TEX_FRONT : return "TEX_FRONT";
@@ -394,7 +396,7 @@ GLuint NaviCubeImplementation::createCubeFaceTex(QtGLWidget* gl, float gap, floa
 	if (text) {
 		ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/NaviCube");
 		paint.setPen(Qt::white);
-		QFont sansFont(str("Helvetica"), 0.18 * texSize);
+             QFont sansFont(QStringLiteral("Helvetica"),0.18 * texSize);
 		QString fontString = QString::fromUtf8((hGrp->GetASCII("FontString")).c_str());
 		if (fontString.isEmpty()) {
 			// Improving readability
@@ -411,8 +413,24 @@ GLuint NaviCubeImplementation::createCubeFaceTex(QtGLWidget* gl, float gap, floa
 		if (hGrp->GetInt("FontStretch") > 0) {
 			sansFont.setStretch(hGrp->GetInt("FontStretch"));
 		}
-		paint.setFont(sansFont);
-		paint.drawText(QRect(0, 0, texSize, texSize), Qt::AlignCenter,qApp->translate("Gui::NaviCube",text));
+        if(m_adjustedFontSize == 0){
+            int oldsize = QFontMetrics(sansFont).height();
+            int maxw = 0;
+            for (const auto& label:m_labels){
+                QRect r = QFontMetrics(sansFont).boundingRect(QString::fromStdString(label));
+                if( r.height() > maxw){
+                    maxw = r.height();
+                }
+                if( r.width() > maxw){
+                    maxw = r.width();
+                }
+            }
+            int field = (texSize - 2 * gapi ) * .9 ;
+            m_adjustedFontSize = (oldsize*field)/maxw;
+        }
+        sansFont.setPixelSize(m_adjustedFontSize);
+        paint.setFont(sansFont);
+        paint.drawText(QRect(0, 0, texSize, texSize), Qt::AlignCenter,qApp->translate("Gui::NaviCube",text));
 	}
 	else {
 		QPainterPath path;
@@ -722,10 +740,11 @@ void NaviCubeImplementation::initNaviCube(QtGLWidget* gl) {
         labels.push_back(hGrp->GetASCII("TextBottom", "BOTTOM"));
         labels.push_back(hGrp->GetASCII("TextRight", "RIGHT"));
         labels.push_back(hGrp->GetASCII("TextLeft", "LEFT"));
+        m_labels = labels;
     }
 
How it works:
Current master-
Navi-current.png
Navi-current.png (5.07 KiB) Viewed 44 times
With patch - default label and short label
Navi-long.png
Navi-long.png (6.92 KiB) Viewed 44 times
Navi-short.png
Navi-short.png (6.45 KiB) Viewed 44 times
vocx
Posts: 5068
Joined: Thu Oct 18, 2018 9:18 pm

Re: Hidpi, font&icon scaling, etc....

Postby vocx » Wed Sep 30, 2020 3:56 pm

Elyas wrote:
Tue Sep 29, 2020 11:52 am
Adjusting NaviCube font without magic numbers:
There is an Addon that you can install through the Addon Manager called Cube Menu that provides some tinkering options for the cube. Maybe you can take a look at it.

https://github.com/triplus/CubeMenu
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Elyas
Posts: 22
Joined: Fri Sep 04, 2020 12:25 pm

Re: Hidpi, font&icon scaling, etc....

Postby Elyas » Wed Sep 30, 2020 4:23 pm

vocx wrote:
Wed Sep 30, 2020 3:56 pm
There is an Addon that you can install through the Addon Manager called Cube Menu that provides some tinkering options for the cube. Maybe you can take a look at it.
https://github.com/triplus/CubeMenu
I am using this addon. It does not allow to correctly place the text on the cube face. Existing code is only valid for standard DPI and default decals.