How to disable font hinting in Swing (or how to strip hints from a font)

In my GNOME desktop, I disable hinting or set hinting level to ‘slight’. The hinting level beyond ‘slight’ (i.e. ‘moderate’ or ‘full’) makes glyph look too thin to read easily. Moreover, especially when screen DPI is high, unhinted or slightly hinted glyphs look much more beautiful than fully hinted ones:

If you think fully hinted one looks better or you are using Mac OS X, you are so lucky that you can skip this article. My font preference is often a problem to Java Swing based applications such as IntelliJ IDEA (and that was always why I go back to SWT-based Eclipse). Although Swing uses Freetype to render glyphs, which is the same library that GTK uses to render glyphs, it doesn’t seem to respect my font appearance settings:

This issue was raised a couple times in the community, but the latest OpenJDK 7 snapshot still doesn’t seem to fix this issue unfortunately. Therefore, there’s no way currently to disable font hinting in Sun/Oracle JDK or OpenJDK on Linux. What’s possible is working around the problem by stripping the hinting information out from your font files so that Swing does not apply any hinting because of the missing information.

To strip the hinting information from a TrueType font, you can use FontForge:

  1. Open your TTF file with FontForge.
  2. Press CTRL+A to select all glyphs.
  3. Choose Hints -> Clear Hints menu item (and wait for a while)
  4. Choose Hints -> Clear Instructions menu item (and wait for a while)
  5. Choose File -> Generate Fonts menu iteam and replace the existing font with the unhinted one.

If you don’t want to replace the existing font, press CTRL+SHIFT+F to update the metadata before the last step. A ‘Font Information’ dialog will show up and you can edit font names there.

Once the new fonts are generated, you can update your ~/.fonts.conf to make the unhinted fonts your default desktop:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
  <!-- Note that I actually appended 'U' to all unhinted font names -->
  <match target="pattern">
    <test qual="any" name="family">
      <string>sans-serif</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>Liberation Sans U</string>
      <string>WenQuanYi Zen Hei U</string>
    </edit>
  </match>
  <match target="pattern">
    <test qual="any" name="family">
      <string>sans-serif</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>Liberation Serif U</string>
      <string>WenQuanYi Zen Hei U</string>
    </edit>
  </match>
  <match target="pattern">
    <test qual="any" name="family">
      <string>monospace</string>
    </test>
    <edit name="family" mode="prepend" binding="strong">
      <string>Liberation Mono U</string>
      <string>WenQuanYi Zen Hei U</string>
    </edit>
  </match>
</fontconfig>

If configured properly, hinting will be automatically disabled for all desktop applications soon no matter what hinting option you specified because there’s no hinting information in the new fonts.

Now open or restart your favorite Swing application and choose GTK Look & Feel. If you have to specify the font name explicitly, choose the unhinted one (e.g. Liberation Mono U). The result is your new-born Swing application with its ever-pleasing look:

Actually, the italicized bold glyphs doesn’t look perfect, but I can live with it. It would be really nice if OpenJDK delivers a proper fix in the near future, though.

4 Comments How to disable font hinting in Swing (or how to strip hints from a font)

  1. Alex

    Thanks for the tip, very useful, I can’t stand full-hinted fonts on Gnome either. I got better results with Liberation than DejaVu using your technique.

    In my opinion Bolds and Italic Bolds are better left untouched, so I just renamed them to fill the modified “Liberation Mono U” font set.

  2. Nils

    Really helpful. No matter what font I tried, the IDEA platform editor just looked awful.

    I removed all the hints from my favourite font (including it’s bold and italic forms) and voila; beautiful.

    Thank you so much for sharing this tip!

Comments are closed.