Skip to contentSkip to conversation

Fix default font scaling

PATCH In review Serhii Tereshchenko 3 participants 12 messages Jun 15, 2024 – Jun 23, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix default font scaling

This fixes font scaling for default fonts, where we don't set font sizes explicitly.

Without this, on 4k monitor with text-scaling-factor configured in Gnome, labels, buttons and settings are using very small font sizes. (Probably, not just Gnome but anything that sets custom DPI).

Screenshots here: https://twiukraine.com/@partizan/112619567918546426

---
 git-gui.sh | 10 ++++++++++
 1 file changed, 10 insertions(+)
git-gui.sh +10 -0
index 8bc8892..23dd82d 100755
@@ -810,6 +810,16 @@ if {[is_Windows]} {
 ## config defaults
 
 set cursor_ptr arrow
+
+# For whatever reason, Tk does not apply font scaling to default fonts,
+# but applies font scaling when setting size explicitly.
+# Default -size 10 is still 10, when you look at it with `font actual ...`,
+# but explicitl -size 10 becomes 10 * scale factor.
+# So, we need to configure fonts to use their default font sizes, but scaled.
+foreach font_name [font names] {
+	font configure $font_name -size [font actual $font_name -size]
+}
+
 font create font_ui
 if {[lsearch -exact [font names] TkDefaultFont] != -1} {
 	eval [linsert [font actual TkDefaultFont] 0 font configure font_ui]
Signature · 1 line
2.45.2
#1 lore raw
Quoted 9 lines · Serhii Tereshchenko

This fixes font scaling for default fonts, where we don't set font sizes explicitly.

Without this, on 4k monitor with text-scaling-factor configured in Gnome, labels, buttons and settings are using very small font sizes. (Probably, not just Gnome but anything that sets custom DPI).

---

I'm adding Johannes Sixt <[email protected]>, the new git-gui maintainer[*], to the Cc: list.

[*]: https://lore.kernel.org/git/[email protected]/

git-gui.sh if {[is_Windows]} { · line 813

Quoted 6 lines
git-gui.sh +4 -0
@@ -810,6 +810,16 @@ if {[is_Windows]} {
+# For whatever reason, Tk does not apply font scaling to default fonts,
+# but applies font scaling when setting size explicitly.
+# Default -size 10 is still 10, when you look at it with `font actual ...`,
+# but explicitl -size 10 becomes 10 * scale factor.

s/explicitl/explicit/

Quoted 4 lines
git-gui.sh +4 -0
+# So, we need to configure fonts to use their default font sizes, but scaled.
+foreach font_name [font names] {
+       font configure $font_name -size [font actual $font_name -size]
+}
#2 lore raw
In reply to Serhii Tereshchenko

Am 15.06.24 um 10:53 schrieb Serhii Tereshchenko:

Quoted 10 lines

This fixes font scaling for default fonts, where we don't set font sizes explicitly.

Without this, on 4k monitor with text-scaling-factor configured in Gnome, labels, buttons and settings are using very small font sizes. (Probably, not just Gnome but anything that sets custom DPI).

Screenshots here: https://twiukraine.com/@partizan/112619567918546426

Thank you. I have adjusted the title to read

git-gui: fix scaled default fonts

to follow the convention.

Quoted 9 lines
---
 git-gui.sh | 10 ++++++++++
 1 file changed, 10 insertions(+)
git-gui.sh +0 -0
index 8bc8892..23dd82d 100755

git-gui.sh if {[is_Windows]} { · line 817

Quoted 8 lines
git-gui.sh +5 -0
@@ -810,6 +810,16 @@ if {[is_Windows]} {
 ## config defaults
 
 set cursor_ptr arrow
+
+# For whatever reason, Tk does not apply font scaling to default fonts,
+# but applies font scaling when setting size explicitly.
+# Default -size 10 is still 10, when you look at it with `font actual ...`,
+# but explicitl -size 10 becomes 10 * scale factor.

I fixed this typo: s/explicitl/explict/

git-gui.sh if {[is_Windows]} { · line 821

Quoted 4 lines
git-gui.sh +4 -0
+# So, we need to configure fonts to use their default font sizes, but scaled.
+foreach font_name [font names] {
+	font configure $font_name -size [font actual $font_name -size]
+}

Have you seen https://wiki.tcl-lang.org/page/font+scaling where [font configure ...] instead of [font actual ...] is suggested as a preferable solution? I am not so much into Tcl/Tk that I can judge what is best.

git-gui.sh if {[is_Windows]} { · line 825

Quoted 4 lines
git-gui.sh +1 -0
+
 font create font_ui
 if {[lsearch -exact [font names] TkDefaultFont] != -1} {
 	eval [linsert [font actual TkDefaultFont] 0 font configure font_ui]

My setup does not have a 4k monitor, I run KDE and I do not know if I have changed a DPI setting. For the test, I have removed the [gui] sections from my configurations to ensure that the defaults are used.

Under these conditions, this change does not make a difference in how Git GUI appears. I assume this is the expected outcome, so I take it as a good sign.

The patch is available as

https://github.com/j6t/git-gui.git st/font-scaling-fix

for others to test. I'd appreciate your feedback.

-- Hannes

#3 lore raw
Quoted 41 lines · Johannes Sixt

Am 15.06.24 um 10:53 schrieb Serhii Tereshchenko:

Quoted 9 lines

This fixes font scaling for default fonts, where we don't set font sizes explicitly.

Without this, on 4k monitor with text-scaling-factor configured in
Gnome,
labels, buttons and settings are using very small font sizes.
(Probably,
not just Gnome but anything that sets custom DPI).

Quoted 4 lines

Screenshots here:
https://twiukraine.com/@partizan/112619567918546426

Thank you. I have adjusted the title to read

git-gui: fix scaled default fonts

to follow the convention.

Quoted 11 lines

---

  git-gui.sh | 10 ++++++++++
  1 file changed, 10 insertions(+)

diff --git a/git-gui.sh b/git-gui.sh
index 8bc8892..23dd82d 100755
--- a/git-gui.sh
+++ b/git-gui.sh
@@ -810,6 +810,16 @@ if {[is_Windows]} {
## config defaults

Quoted 8 lines

set cursor_ptr arrow
+
+# For whatever reason, Tk does not apply font scaling to default
fonts,
+# but applies font scaling when setting size explicitly. +# Default -size 10 is still 10, when you look at it with `font
actual ...`,
+# but explicitl -size 10 becomes 10 * scale factor.

Quoted 2 lines

I fixed this typo: s/explicitl/explict/

Thanks!

Quoted 10 lines
Quoted 5 lines

+# So, we need to configure fonts to use their default font sizes,
but scaled.
+foreach font_name [font names] {
+ font configure $font_name -size [font actual $font_name -size]
+}

Have you seen https://wiki.tcl-lang.org/page/font+scaling where [font configure ...] instead of [font actual ...] is suggested as a preferable solution? I am not so much into Tcl/Tk that I can judge what is best.

No, I haven't seen this, but suggested preferable solution doesn't work.

font configure $font -size returns negative numbers for me.

According to this
https://tkdocs.com/shipman/fonts.html

it means "size in pixels". And size in pixels does not scale. So, if we want fonts to scale with DPI, we should use "points" - and that's what font actual ... returns (i just hope it returns points, because it works for me and scales like points).

I'm also no expert in Tcl/Tk, this just my second time writing patches for git-gui :)

Probably, after all this we should change comment to:

# Default Tk fonts are defined in pixels, and they

# does not scale with DPI, here we're converting them
# into points, allowing them to scale just like
# explicitly defined fonts.
Quoted 6 lines
Quoted 5 lines

+
font create font_ui
if {[lsearch -exact [font names] TkDefaultFont] != -1} {
eval [linsert [font actual TkDefaultFont] 0 font configure
font_ui]

Quoted 8 lines

My setup does not have a 4k monitor, I run KDE and I do not know if I have changed a DPI setting. For the test, I have removed the [gui] sections from my configurations to ensure that the defaults are used.

Under these conditions, this change does not make a difference in how Git GUI appears. I assume this is the expected outcome, so I take it as a good sign.

You can test it, by changing DPI in ~/.Xresources

xrdb -query to see your current DPI, then increase it a little.

```
Xft.dpi: 154
```

xrdb -merge ~/.Xresources to apply new scaling.

Quoted 9 lines

The patch is available as

https://github.com/j6t/git-gui.git st/font-scaling-fix

for others to test. I'd appreciate your feedback.

-- Hannes

#4 lore raw

Am 16.06.24 um 14:37 schrieb Serhii Tereshchenko:

Quoted 19 lines
Quoted 6 lines · Johannes Sixt

Am 15.06.24 um 10:53 schrieb Serhii Tereshchenko:

Quoted 4 lines

  font create font_ui
  if {[lsearch -exact [font names] TkDefaultFont] != -1} { eval [linsert [font actual TkDefaultFont] 0 font configure font_ui]

Quoted 8 lines

My setup does not have a 4k monitor, I run KDE and I do not know if I have changed a DPI setting. For the test, I have removed the [gui] sections from my configurations to ensure that the defaults are used.

Under these conditions, this change does not make a difference in how Git GUI appears. I assume this is the expected outcome, so I take it as a good sign.

You can test it, by changing DPI in ~/.Xresources

Quoted 7 lines

xrdb -query to see your current DPI, then increase it a little.

```
Xft.dpi: 154
```

xrdb -merge ~/.Xresources to apply new scaling.

Thank you for this recipe. What is the expected result on the font size with small and with large DPI values, with and without this patch?

- When I set Xft.dpi 100, I get tiny fonts.
- When I set it to 200, I get huge fonts.

And that is the case with and without this patch. I expected that the font size is independent from the DPI with the patch. (I run ./git-gui after make, but without installing.)

I also see the change in font size after modifying Xft.dpi in gitk, BTW.

At a minimum, the patch does not make things worse.

-- Hannes

#5 lore raw

Yeah, I'm also testing this way.

No, fonts should not be independent from DPI, but now some fonts are scaling just like the others.

To clearly see the result, set comfortable DPI, so fonts are reasonably sized (if this value is close to 96, better use something larger, like 130 or 200 to see the effect).

When you run "git-gui" from the master branch, look at the fonts for:

- menubar and menu (explicitly set, scaled with DPI - our baseline)
- buttons in the lower part (Commit, Rescan, etc), labels like "Current branch" - (unscaled, visibly smaller than menubar)
- you can also open "Edit -> Options", and everything there will be smaller size as well.

After applying patch, all fonts are scaled equally (e.q. - small DPI - everything small, large DPI - everything big).

If, however - you see second behavior on both cases, I'm really interested to get more details:

- What OS and Tcl/Tk are you using?

I remember few years ago it worked fine on my ArchLinux, so if other distros aren't updated something yet, it may still work.

On Arch we have Tcl/Tk 8.6.14.

And to go even deeper, we may compare results from wish:

Quoted 1 line

package require Tk 8.5

8.6.14

Quoted 1 line

font configure TkDefaultFont

-family sans-serif -size -12 -weight normal -slant roman -underline 0 -overstrike 0

Quoted 1 line

font actual TkDefaultFont

-family {Nokia Sans S60} -size 9 -weight normal -slant roman -underline 0 -overstrike 0

Quoted 49 lines · Johannes Sixt

Am 16.06.24 um 14:37 schrieb Serhii Tereshchenko:

Quoted 24 lines
Quoted 12 lines · Johannes Sixt

Am 15.06.24 um 10:53 schrieb Serhii Tereshchenko:

Quoted 4 lines

font create font_ui
if {[lsearch -exact [font names] TkDefaultFont] != -1} {
eval [linsert [font actual TkDefaultFont] 0 font configure
font_ui]

My setup does not have a 4k monitor, I run KDE and I do not know
if I
have changed a DPI setting. For the test, I have removed the [gui] sections from my configurations to ensure that the defaults are
used.

Quoted 6 lines

Under these conditions, this change does not make a difference in
how
Git GUI appears. I assume this is the expected outcome, so I take
it as
a good sign.

You can test it, by changing DPI in ~/.Xresources

Quoted 7 lines

xrdb -query to see your current DPI, then increase it a little.

 ```
 Xft.dpi: 154
 ```

xrdb -merge ~/.Xresources to apply new scaling.

Thank you for this recipe. What is the expected result on the font size with small and with large DPI values, with and without this patch?

- When I set Xft.dpi 100, I get tiny fonts.
- When I set it to 200, I get huge fonts.

And that is the case with and without this patch. I expected that the font size is independent from the DPI with the patch. (I run ./git-gui after make, but without installing.)

I also see the change in font size after modifying Xft.dpi in gitk, BTW.

At a minimum, the patch does not make things worse.

-- Hannes

#6 lore raw

Am 20.06.24 um 20:11 schrieb Serhii Tereshchenko:

Quoted 17 lines

Yeah, I'm also testing this way.

No, fonts should not be independent from DPI, but now some fonts are scaling just like the others.

To clearly see the result, set comfortable DPI, so fonts are reasonably sized (if this value is close to 96, better use something larger, like 130 or 200 to see the effect).

When you run "git-gui" from the master branch, look at the fonts for:

- menubar and menu (explicitly set, scaled with DPI - our baseline)
- buttons in the lower part (Commit, Rescan, etc), labels like "Current branch" - (unscaled, visibly smaller than menubar)
- you can also open "Edit -> Options", and everything there will be smaller size as well.

Quoted 5 lines

After applying patch, all fonts are scaled equally (e.q. - small DPI - everything small, large DPI - everything big).

If, however - you see second behavior on both cases, I'm really interested to get more details:

I see the second behavior regardless of the patch, i.e. with small DPI all fonts are small, with large DPI all fonts are large.

The label "Current branch" is the same size as other fonts except when I select a different font size in Edit->Options as "Main Font".

Quoted 15 lines

- What OS and Tcl/Tk are you using?

I remember few years ago it worked fine on my ArchLinux, so if other distros aren't updated something yet, it may still work.

On Arch we have Tcl/Tk 8.6.14.

And to go even deeper, we may compare results from wish:

Quoted 1 line

package require Tk 8.5

8.6.14

Quoted 1 line

font configure TkDefaultFont

(quoted diff) +0 -2
-family sans-serif -size -12 -weight normal -slant roman -underline 0
-overstrike 0
Quoted 1 line

font actual TkDefaultFont

Quoted 2 lines

-family {Nokia Sans S60} -size 9 -weight normal -slant roman -underline 0 -overstrike 0

I have this:

% package require Tk 8.5
8.6.12
% font configure TkDefaultFont
-family sans-serif -size -12 -weight normal -slant roman -underline 0 -overstrike 0
%
% font actual TkDefaultFont
-family Arial -size 9 -weight normal -slant roman -underline 0 -overstrike 0 %

I am using openSUSE Leap 15.5 under KDE Frameworks 5.103.0

-- Hannes

#7 lore raw
Quoted 69 lines · Johannes Sixt

Am 20.06.24 um 20:11 schrieb Serhii Tereshchenko:

Quoted 21 lines

Yeah, I'm also testing this way.

No, fonts should not be independent from DPI, but now some fonts are scaling just like the others.

To clearly see the result, set comfortable DPI, so fonts are
reasonably
sized (if this value is close to 96, better use something larger,
like
130 or 200 to see the effect).

When you run "git-gui" from the master branch, look at the fonts
for:

- menubar and menu (explicitly set, scaled with DPI - our baseline)
- buttons in the lower part (Commit, Rescan, etc), labels like
"Current
branch" - (unscaled, visibly smaller than menubar)
- you can also open "Edit -> Options", and everything there will be smaller size as well.

Quoted 6 lines

After applying patch, all fonts are scaled equally (e.q. - small
DPI -
everything small, large DPI - everything big).

If, however - you see second behavior on both cases, I'm really interested to get more details:

I see the second behavior regardless of the patch, i.e. with small DPI all fonts are small, with large DPI all fonts are large.

The label "Current branch" is the same size as other fonts except when I select a different font size in Edit->Options as "Main Font".

Quoted 11 lines

- What OS and Tcl/Tk are you using?

I remember few years ago it worked fine on my ArchLinux, so if other distros aren't updated something yet, it may still work.

On Arch we have Tcl/Tk 8.6.14.

And to go even deeper, we may compare results from wish:

Quoted 1 line

package require Tk 8.5

8.6.14

Quoted 8 lines
Quoted 1 line

font configure TkDefaultFont

-family sans-serif -size -12 -weight normal -slant roman -underline
0
-overstrike 0

Quoted 1 line

font actual TkDefaultFont

-family {Nokia Sans S60} -size 9 -weight normal -slant roman
-underline
0 -overstrike 0

I have this:

% package require Tk 8.5
8.6.12
% font configure TkDefaultFont

(quoted diff) +0 -2
-family sans-serif -size -12 -weight normal -slant roman -underline 0
-overstrike 0

%
% font actual TkDefaultFont

(quoted diff) +0 -2
-family Arial -size 9 -weight normal -slant roman -underline 0 
-overstrike 0

%

Quoted 4 lines

I am using openSUSE Leap 15.5 under KDE Frameworks 5.103.0

-- Hannes

Thanks! I tried booting into Fedora/37 with the same Tk version, and it indeed does not have this scaling problem.

Then, I downgraded packages on Arch to 8.6.12 and 8.6.13 - also no problem.

The only version affected is 8.6.14.

That's probably a bug in Tk (and there's quite a few reported, mostly about "tk scaling" https://core.tcl-lang.org/tk/info/1de3a48312, we don't use it explicitly in git-gui but on my system it is set to 1.3)

#8 lore raw

Am 21.06.24 um 09:18 schrieb Serhii Tereshchenko:

Quoted 10 lines

Thanks! I tried booting into Fedora/37 with the same Tk version, and it indeed does not have this scaling problem.

Then, I downgraded packages on Arch to 8.6.12 and 8.6.13 - also no problem.

The only version affected is 8.6.14.

That's probably a bug in Tk (and there's quite a few reported, mostly about "tk scaling" https://core.tcl-lang.org/tk/info/1de3a48312, we don't use it explicitly in git-gui but on my system it is set to 1.3)

What is the best course of action now? Since this change does not make things any different for me on Linux+KDE, I am inclined to pick up this patch with a modified commit message and code comment that clearly state that this is a work-around for a bug in Tk introduced in 8.6.14. This would help users who upgrade from an earlier version of Tk, because it does not look like a fix of Tk is in the works (or that the issue is regarded as a regression by Tk people at all).

Suggestions?

-- Hannes

#9 lore raw
Quoted 24 lines · Johannes Sixt

Am 21.06.24 um 09:18 schrieb Serhii Tereshchenko:

Quoted 7 lines

Thanks! I tried booting into Fedora/37 with the same Tk version,
and it
indeed does not have this scaling problem.

Then, I downgraded packages on Arch to 8.6.12 and 8.6.13 - also no
problem.

Quoted 7 lines

The only version affected is 8.6.14.

That's probably a bug in Tk (and there's quite a few reported,
mostly
about "tk scaling" https://core.tcl-lang.org/tk/info/1de3a48312, we don't use it explicitly in git-gui but on my system it is set to
1.3)

What is the best course of action now? Since this change does not make things any different for me on Linux+KDE, I am inclined to pick up this patch with a modified commit message and code comment that clearly state that this is a work-around for a bug in Tk introduced in 8.6.14. This would help users who upgrade from an earlier version of Tk, because it does not look like a fix of Tk is in the works (or that the issue is regarded as a regression by Tk people at all).

Quoted 3 lines

Suggestions?

-- Hannes

I tried running default git-gui and this patch on macOS 12 Monterey, and that version does not have font scaling, so there's no problem. I will ask around and maybe find someone with latest macOS, which has font scaling, and see if there's a problem.

Patch does not breaks anything, but produces a warning due to system fonts being present in 'font names'.

Then, if turns out this linux-specific problem - i'd limit this fix only to linux. If this affects other platforms - i'd filter fonts to only include those starting with 'Tk' prefix.

#10 lore raw
Quoted 29 lines · On нд, чер 23 2024 at 13:11:22 +03:00:00, [email protected] wrote:
Quoted 26 lines · Johannes Sixt

Am 21.06.24 um 09:18 schrieb Serhii Tereshchenko:

Quoted 7 lines

Thanks! I tried booting into Fedora/37 with the same Tk version,
and it
indeed does not have this scaling problem.

Then, I downgraded packages on Arch to 8.6.12 and 8.6.13 - also no
problem.

Quoted 7 lines

The only version affected is 8.6.14.

That's probably a bug in Tk (and there's quite a few reported,
mostly
about "tk scaling" https://core.tcl-lang.org/tk/info/1de3a48312, we don't use it explicitly in git-gui but on my system it is set to
1.3)

What is the best course of action now? Since this change does not make things any different for me on Linux+KDE, I am inclined to pick up this patch with a modified commit message and code comment that clearly state that this is a work-around for a bug in Tk introduced in 8.6.14. This would help users who upgrade from an earlier version of Tk, because it does not look like a fix of Tk is in the works (or that the issue is regarded as a regression by Tk people at all).

Suggestions?

Quoted 7 lines
Quoted 1 line

-- Hannes

I tried running default git-gui and this patch on macOS 12 Monterey, and that version does not have font scaling, so there's no problem. I will ask around and maybe find someone with latest macOS, which has font scaling, and see if there's a problem.

Patch does not breaks anything, but produces a warning due to system fonts being present in 'font names'.

Then, if turns out this linux-specific problem - i'd limit this fix only to linux. If this affects other platforms - i'd filter fonts to only include those starting with 'Tk' prefix.

After more reasearch, I found this:

https://core.tcl-lang.org/tk/info/dccd82bdc7

This is x11-only bug, which existed for a long time and i'm suprized we're seeing it only in 8.6.14, because .13 and .12 are using the same negative sizes.

It is also fixed for a long time in the 8.7 branch and not backported into 8.6.

https://github.com/tcltk/tk/blob/7c3e2ff815e23cc6ac1ce9891ca659e709776ea4/library/ttk/fonts.tcl

Also, you may be not seeing this, because your distro may have applied this patch: https://sources.debian.org/patches/tk8.6/8.6.9-2/font-sizes.diff/

With this information, i'm more inclined to making a patch for ArchLinux package. So, it will fix every tk app, and not only git-gui.

What do you think?

#11 lore raw

Am 23.06.24 um 16:00 schrieb [email protected]:

Quoted 9 lines

This is x11-only bug, which existed for a long time and i'm suprized we're seeing it only in 8.6.14, because .13 and .12 are using the same negative sizes.

It is also fixed for a long time in the 8.7 branch and not backported into 8.6.

https://github.com/tcltk/tk/blob/7c3e2ff815e23cc6ac1ce9891ca659e709776ea4/library/ttk/fonts.tcl

Quoted 8 lines

Also, you may be not seeing this, because your distro may have applied this patch:
https://sources.debian.org/patches/tk8.6/8.6.9-2/font-sizes.diff/

With this information, i'm more inclined to making a patch for ArchLinux package. So, it will fix every tk app, and not only git-gui.

What do you think?

I think that would make sense. It would relieve me from taking responsibility for a patch whose effects I do not understand well.

Thanks,
-- Hannes