Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are fonts in java 1.7 different to 1.6?

Tags:

java

fonts

java-7

I've been building a development environment in Java (as a small research project). As part of that, I built a custom text component that draws the text itself using Graphics2d like so:

g2.drawString("some text", 100, 100);

Everything worked fine developing things in Eclipse using 1.6 (I assume this is provided by Apple), until I packaged everything up and ran in java 1.7 (provided by Oracle). Obviously in development environments fonts are pretty important, so I was displeased to see the results in 1.7.

enter image description here

I have tried packaging custom fonts, but they all appear grainy and eroded. The comparison above isn't too bad, but some fonts (like Monaco) look terrible.

I'm guessing this is to do with how Apple hooks things into Quartz. But is there any way to improve things so it doesn't look terrible on other systems? Does anyone have a strategy for this?

Update: This is the comparison in Monaco: enter image description here

and a zoomed comparison of the C (in paintComponent) in Monaco (1.7 on left, 1.6 on right). Note that I am loading the font from a local ttf file using:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("monaco.ttf");
Font customFont = null;
try
{
customFont = Font.createFont(Font.TRUETYPE_FONT, is);
customFont = customFont.deriveFont(16.0f).deriveFont(Font.BOLD);
                    is.close();
                    ge.registerFont(customFont);
                } catch (FontFormatException e1)
                {
                    e1.printStackTrace();
                } catch (IOException e1)
                {
                    e1.printStackTrace();
                }
this.setFont(customFont);

enter image description here

Note also that anti aliasing is turned on using:

 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                    RenderingHints.VALUE_ANTIALIAS_ON);
like image 231
alistair Avatar asked Sep 16 '13 12:09

alistair


People also ask

What are the different fonts in Java?

Logical fonts are the five font families defined by the Java platform which must be supported by any Java runtime environment: Serif, SansSerif, Monospaced, Dialog, and DialogInput.

What does Font plain do?

To put it simply, plain text is any text that isn't formatted. It does not take any special formatting, such as varying fonts, font sizes, bold font, or italics. It also only contains standard characters, which are those found in the default set of characters that an application can display.

What is the difference between Java characters and fonts?

In Java, there are two technical terms that are used to represent font are characters and Glyphs. Physical fonts are actual Java font library. It contains tables that maps character sequence to glyph sequences by using the font technology such as TrueType Fonts (TTF) and PostScript Type 1 Font. Note that all implementation of Java must support TTF.

What fonts are supported by the Java platform?

All implementations of the Java Platform must support TrueType fonts; support for other font technologies is implementation dependent. Physical fonts may use names such as Helvetica, Palatino, HonMincho, or any number of other font names.

How to change font size and color in Java?

Click on the option to open the “Colors and Fonts” screen. On that screen, find the “Java” option. Click on the option and find the “Java Editor Text Font (set to default: Text Font) ”. Select that option. Now, click the “Edit” button. Once done, a font dialog box appears. In the dialog box, you will find the “Font name” and “Font size”.

How do I change the default font in Java editor?

Click on the option to open the “Colors and Fonts” screen. On that screen, find the “Java” option. Click on the option and find the “Java Editor Text Font (set to default: Text Font) ”. Select that option.


1 Answers

Java 6 was developed by Apple. Apple put a lot of work into the graphics layer to make it work with Quartz/Cocoa and especially to make it look good.

For Java 7, the development was taken over by Oracle. This doesn't mean that Oracle got the sources from Apple. Instead, they started over (probably porting some Unix code to OS X). This means all the work that Apple put into making Java look good on OS X was basically lost.

Apple cares a lot about design. Oracle ... well ... not so much (exhibit A, exhibit B)

What can you do?

  • Write and submit a patch to the OpenJDK.
  • Get enough people who care to ask Apple to submit a patch (money might help).
  • Go back to Java 6
  • Use JNI to access Cocoa/Quartz directly so you get the OS X font rendering engine.

Related articles:

  • http://chriswjohnson.blogspot.co.uk/2010/08/java-text-when-platform-independence.html
like image 196
Aaron Digulla Avatar answered Oct 21 '22 12:10

Aaron Digulla