4

When I try to use the Wingdings font (or other symbol fonts), the text comes out as rectangles instead of the correct text. How do I get the correct characters to show?

import java.awt.*;
import javax.swing.*;

public class WingdingsFontDisplay extends JFrame
{
    public static void main(String[] args)
    {
        new WingdingsFontDisplay();
    }

    public WingdingsFontDisplay()
    {
        this.setSize(500,150);
        this.setTitle("Fun with Fonts");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //This shows that I do have "Wingdings" available
        GraphicsEnvironment g;
        g = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fonts = g.getAvailableFontFamilyNames();
        for(String f : fonts)
        {
            System.out.println(f);
        }

        //Displaying text in the Wingdings font shows rectangles
        JLabel wingdingsText = new JLabel("All work and no play makes Jack a dull boy");
        Font f1 = new Font("Wingdings", Font.PLAIN, 14);
        wingdingsText.setFont(f1);
        this.add(wingdingsText, BorderLayout.NORTH);

        //Displaying text in Arial works correctly
        JLabel arialText = new JLabel("All work and no play makes Jack a dull boy");
        Font f2 = new Font("Arial", Font.PLAIN, 14);
        arialText.setFont(f2);
        this.add(arialText, BorderLayout.SOUTH);

        this.setVisible(true);
    }
}
Roy
  • 974
  • 6
  • 11

1 Answers1

5

You need to use the appropriate Unicode range for the symbols you seek. In Java, symbols are not overlaid on the ASCII range, but have their own distinct character codes.

You can find a reference to the appropriate symbol codes at http://unicode.org/~asmus/web-wing-ding-ext.pdf . Most common symbols are in the 0x2200 and 0x2700 Unicode ranges.

Your Java install may include the SymbolTest sample applet, which makes it straightforward to preview the presentation of Unicode ranges with available fonts. Be warned, however, that better Java implementations will use font substitutions for symbols or characters not in the specified font, so you'll want to be sure you're actually getting the specified font.

Steven McGrath
  • 1,717
  • 11
  • 20
  • Thanks. I downloaded the SymbolTest applet and worked my way through it. I realize I can display the Wingding character by adding 0x1000 to the character code. – Roy Oct 07 '12 at 21:30
  • But I don't understand why I should have to. For example, if I want to print out character 65, then I want to the program to print character 65. Most fonts will display it looking similar to 'A'. Wingdings should show it as a hand with 2 fingers held up. – Roy Oct 07 '12 at 21:40
  • @Roy, Unicode was designed so that each character value has a semantic meaning. 'A' always means 'A', regardless of font, and "a hand with two fingers held up" always means "a hand with two fingers held up", regardless of font. This means that Unicode text always carries the semantic meaning, even if the original font is not available, which is common for international text. Browse unicode.org for more. – Steven McGrath Oct 08 '12 at 00:57
  • A little more info. Your normal fonts don't support all of Unicode, since you never need to support all languages. If a font doesn't support a character, there is a specified glyph, such as a question mark of a rectangle, you may see. Font substitution may also be used in some instances, so that your symbols show up even if you don't explicitly set the appropriate font. – Steven McGrath Oct 08 '12 at 01:07
  • Does the font somehow know what the Unicode number is for each character? So that even though "a hand with two fingers held up" is character 65 in the Wingding font, it knows it's Unicode 0x1041 (DEC 4161) ? – Roy Oct 08 '12 at 05:04
  • @Roy, The mapping of Unicode characters to glyphs in a font is independent of tables mapping the glyphs to legacy text encodings. See the TrueType reference at https://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html for details on the 'cmap' tables in TrueType fonts. – Steven McGrath Oct 08 '12 at 05:47
  • Thanks again. I think that last link cleared things up for me. I appreciate your help. – Roy Oct 08 '12 at 06:18
  • To anyone reading this thread, I was mistaken when I said that Winding character 65 is "a hand with two fingers held up". I forgot that in trying to understand all this, I had modified my program to add 0x1000 (DEC 4096) to the character number before displaying it. Sorry for any confusion that may cause. – Roy Oct 08 '12 at 06:22