How to enable LCD sub-pixel anti-aliasing in Swing on OS X?

I was asked to find pens in Swing so that the text was as readable as possible. Is it possible to enable anti-aliasing of subpixel text in a Swing application on Java 1.7+ in OS X? Are there any other settings in Java for reading text?

remarks:

  • In Java 1.7 on OS X, no matter what I do, all text is gray.
  • Even if subpixel rendering does not work, RenderingHints.VALUE_TEXT_ANTIALIAS_ON darker than RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB . Why?
  • By default, JComponent on Mac is rendered using RenderingHints.VALUE_TEXT_ANTIALIAS_ON (dark gray anti-aliasing).
  • In Java 1.6, -Dapple.awt.graphics.UseQuartz=true activates LCD anti-aliasing, but this option does not affect Oracle Java 1.7.
  • On Linux, all sub-pixel smoothing hints work correctly, and JComponent uses HRGB anti-aliasing by default.

Antialiasing 300% same screenshot

Here is the code I used:

 import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.UIManager; import sun.swing.SwingUtilities2; public class TextAntialiasDemo { public static JLabel createJlabel(String name, final Object textAntialiasing) { JLabel label = new JLabel("ABCDEabcde text_antialiasing=" + name) { private static final long serialVersionUID = 1L; @Override public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g.create(); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, textAntialiasing); super.paintComponent(g2); } }; // Tell the JComponent to use the Graphics2d text rendering hint // instead of the Toolkit default. label.putClientProperty(SwingUtilities2.AA_TEXT_PROPERTY_KEY, null); // To make all new JComponents use Graphics2d rendering hint, do this: // UIManager.getDefaults().remove(SwingUtilities2.AA_TEXT_PROPERTY_KEY); return label; } public static void main(String[] argv) { System.out.format("Look and feel: %s%n", UIManager.getLookAndFeel()); JFrame jframe = new JFrame(); if (argv.length > 0) jframe.setTitle(argv[0]); jframe.getContentPane().setLayout(new BoxLayout(jframe.getContentPane(), BoxLayout.PAGE_AXIS)); jframe.add(new JLabel("ABCDEabcde")); jframe.add(createJlabel("default", RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT)); jframe.add(createJlabel("off", RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)); jframe.add(createJlabel("on", RenderingHints.VALUE_TEXT_ANTIALIAS_ON)); jframe.add(createJlabel("LCD_VRGB", RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VRGB)); jframe.add(createJlabel("LCD_VBGR", RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VBGR)); jframe.add(createJlabel("LCD_HBGR", RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HBGR)); jframe.add(createJlabel("LCD_HRGB", RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB)); jframe.pack(); jframe.setVisible(true); } } 
+4
source share
1 answer

Now for OSX in OpenJDK 9 b70, the appropriate subpixel rendering of the AA font is now available: https://jdk9.java.net/download/ . It is not available if your surfaces are translucent. I wrote about how to enable this, for example. for use with netbeans on my blog; http://www.nothome.com/2015/07/subpixel-font-rendering-with-netbeans.html or just create and fix using:

 hg clone http://hg.openjdk.java.net/jdk9/jdk9 jdk9 cd ./jdk9 bash ./get_source.sh cd jdk curl http://cr.openjdk.java.net/~bae/8087201/9/webrev.00/jdk.patch | patch --dry-run -p1 curl https://gist.githubusercontent.com/iampivot/5677ccc024afe115d4bc/raw/f7b9acb1c6f356efa26f5772075a32dc899cd8ab/gistfile1.txt | patch -p0 cd .. sh ./configure --disable-warnings-as-errors make all 
+1
source

Source: https://habr.com/ru/post/1487101/


All Articles