You are not logged in.
Hello there,
For some time now, Java applications have issues properly using a monospaced font.
In java, `new Font("Monospaced",Font.PLAIN,12)` should give you a monospace font. It does not however, for me, it just uses the default font (i.e. whatever you see when you use `null` as a font).
This only happens with JDK 15. The next lower one I have installed, JDK 11, properly finds my default monospace font.
I'm out of ideas here. I did not do any weird things with my font configuration as far as I can remember. I tried tricking java by using a custom fontconfig.properties, which did not work out as intended. Do any of you have any ideas what might cause this?
Thanks!
Offline
Can you share a code snippet to reproduce the issue? On my system:
jshell> java.awt.Font font = new java.awt.Font("Monospaced", java.awt.Font.PLAIN, 12);
font ==> java.awt.Font[family=Monospaced,name=Monospaced,style=plain,size=12]
jshell> java.awt.Font font = new java.awt.Font(null, java.awt.Font.PLAIN, 12);
font ==> java.awt.Font[family=Dialog,name=Default,style=plain,size=12]
jshell> System.getProperties().getProperty("java.vm.version");
$2 ==> "15.0.2+7"On your system, does the GraphicsEnvironment.getAvailableFontFamilyNames() contain "Monospaced"? If it doesn't, then Java will return whatever is mapped to "Dialog".
Offline
Can you share a code snippet to reproduce the issue? On my system:
On your system, does the GraphicsEnvironment.getAvailableFontFamilyNames() contain "Monospaced"?
I get the same output. The array contains "Monospaced".
Font f = new Font("Monospaced",Font.PLAIN,12);
System.out.println(f.getStringBounds("iii", new FontRenderContext(new AffineTransform(), false, false)).getWidth());
System.out.println(f.getStringBounds("mmm", new FontRenderContext(new AffineTransform(), false, false)).getWidth());Does output 21 in both cases on for JDK11 (which is not affected), but it outputs different numbers for JDK15.
Offline
On my system, they return the same output for "iii" vs "mmm", but JDK 11 prints 21.0 whereas JDK 15 prints 18.0:
| Welcome to JShell -- Version 15.0.2
jshell> import java.awt.Font;
jshell> import java.awt.font.FontRenderContext;
jshell> import java.awt.geom.AffineTransform;
jshell> Font f = new Font("Monospaced",Font.PLAIN,12);
f ==> java.awt.Font[family=Monospaced,name=Monospaced,style=plain,size=12]
jshell> f.getStringBounds("iii", new FontRenderContext(new AffineTransform(), false, false)).getWidth()
$5 ==> 18.0
jshell> f.getStringBounds("mmm", new FontRenderContext(new AffineTransform(), false, false)).getWidth()
$6 ==> 18.0Offline
On my system, they return the same output for "iii" vs "mmm", but JDK 11 prints 21.0 whereas JDK 15 prints 18.0:
| Welcome to JShell -- Version 15.0.2 jshell> import java.awt.Font; jshell> import java.awt.font.FontRenderContext; jshell> import java.awt.geom.AffineTransform; jshell> Font f = new Font("Monospaced",Font.PLAIN,12); f ==> java.awt.Font[family=Monospaced,name=Monospaced,style=plain,size=12] jshell> f.getStringBounds("iii", new FontRenderContext(new AffineTransform(), false, false)).getWidth() $5 ==> 18.0 jshell> f.getStringBounds("mmm", new FontRenderContext(new AffineTransform(), false, false)).getWidth() $6 ==> 18.0
Well, the actual number don't really matter, they should just be the same. They are not.
Do you have any ideas about how/why Java does not select the proper font?
Offline
FWIW I'd usually associate "monospace" rather than "monospaced" with the family name (though Java is supposed to logically map that monospaced correctly...). What does a plain fc-match give you for the two strings.
Last edited by V1del (2021-05-10 08:06:24)
Offline
What does a plain fc-match give you for the two strings.
$ fc-match monospace
DejaVuSansMono.ttf: "DejaVu Sans Mono" "Book"
$ fc-match monospaced
DejaVuSansMono.ttf: "DejaVu Sans Mono" "Book"monospaced did not work originally, I added a config file to force it to be DejaVu Sans Mono, but that did not help either.
Offline
[...] they should just be the same. They are not.
Both "iii" and "mmm" return the same value on JDK 15, which means it selected a 'monospaced' font. It just appears to be a *different* font compared to JDK 11. Your post #3 suggested that on JDK 15 "iii" and "mmm" return different sizes, is that in fact the case?
If not, it means that JDK 15 matches the logical font "monospaced" to a different, fixed-width, font compared to JDK 11, which would not surprise me, since the mapping can be changed for Look and Feel adjustments. The mapping is also configurable by the end-user. If you want a specific font, you should not be using logical font names, but physical font names.
Offline
…
font.getFamily();
font.getFontName();
font.getName();
font.getAttributes();
…Offline
jojomodding wrote:[...] they should just be the same. They are not.
Your post #3 suggested that on JDK 15 "iii" and "mmm" return different sizes, is that in fact the case?
Yes, that is the case. Sorry if I was unclear. Java 11 uses a monospace font. Java 15 does have the "default" font, the same one you get when you pass in `null`. It is definitely not monospaced, it looks like the normal font, but is italic.
Here's a screenshot of what I expected, taken with JDK 8: https://imgur.com/IZl0UQi.png
Here's a screenshot of the same program on Java 15: https://imgur.com/xQuSWBR.png
..
Java 8:
Monospaced
Monospaced.plain
Monospaced
map of {family="Monospaced", weight=1.0*, width=1.0*, posture=0.0*, size=12.0*, transform=null*, superscript=0*, tracking=0.0*[btx=null, ctx=null]}Java 15: (no difference)
Monospaced
Monospaced.plain
Monospaced
map of {family="Monospaced", weight=1.0*, width=1.0*, posture=0.0*, size=12.0*, transform=null*, superscript=0*, tracking=0.0*[btx=null, ctx=null]}The code I used to get this:
Font f = new Font("Monospaced",Font.PLAIN,12);
System.out.println(f.getStringBounds("iii", new FontRenderContext(new AffineTransform(), false, false)).getWidth());
System.out.println(f.getStringBounds("mmm", new FontRenderContext(new AffineTransform(), false, false)).getWidth());
JFrame jf= new JFrame("TextField Example");
JTextField t1,t2;
t1=new JTextField("Monospaced");
t1.setBounds(50,100, 200,30);
t1.setFont(f);
t2=new JTextField("null");
t2.setBounds(50,150, 200,30);
t2.setFont(null);
jf.add(t1); jf.add(t2);
jf.setSize(400,400);
jf.setLayout(null);
jf.setVisible(true);
System.out.println(f.getFamily());
System.out.println(f.getFontName());
System.out.println(f.getName());
System.out.println(f.getAttributes());Offline
So it doesn't decode them from the generics. Great.
What font is actually resolved (you could strace/grep some java client)?
Also, wild guess:
locale?
Offline
Java 11 uses a monospace font. Java 15 does have the "default" font, the same one you get when you pass in `null`. It is definitely not monospaced, it looks like the normal font, but is italic.
OK - thanks for clarifying - just be clear on my comparison: on my system with JDK 15 it DOES return a monospaced font, so your system and mine behave differently with the same JDK 15.
To eliminate any class initialization / automatic font configuration when you bring in Swing classes, if you execute the exact same statements in jshell as I had in post #4, without passing any .jars to jshell, etc., do you get a different result compared to me?
Offline
So it doesn't decode them from the generics. Great.
What font is actually resolved (you could strace/grep some java client)?
Also, wild guess:locale?
LANG=en_US.UTF8
LC_CTYPE="en_US.UTF8"
LC_NUMERIC="en_US.UTF8"
LC_TIME="en_US.UTF8"
LC_COLLATE="en_US.UTF8"
LC_MONETARY="en_US.UTF8"
LC_MESSAGES="en_US.UTF8"
LC_PAPER="en_US.UTF8"
LC_NAME="en_US.UTF8"
LC_ADDRESS="en_US.UTF8"
LC_TELEPHONE="en_US.UTF8"
LC_MEASUREMENT="en_US.UTF8"
LC_IDENTIFICATION="en_US.UTF8"
LC_ALL=Hm, the problem just disappeared. I ran java with
LC_ALL=C, and the problem was gone. I reran it without this, and monospace fonts still worked correctly. I don't remember messing with my system in any way since my last post, I checked the pacman log and I did not even update java. I'm pretty perplexed by this, but the problem is gone, so I guess I can consider this closed.
Thanks for your help, and thanks to the others who participated!
Last edited by jojomodding (2021-05-19 22:03:22)
Offline