1

I need to draw a special character using graphics2d but some characters are not avaiable. For example, the character ➿ is rendered like a box:

Here is my code:

private void drawIcon(Graphics2D g2d) {
    g2d.setColor(iconColor);
    g2d.setFont(iconFont);
    FontMetrics fm = g2d.getFontMetrics();
    Rectangle2D r = fm.getStringBounds(icon, g2d);
    int x = (int) ((getSize().getWidth() - (int) r.getWidth()) / 2);
    int y = (int) ((getSize().getHeight() - (int) r.getHeight()) / 2 + fm.getAscent());
    System.out.println(x + " " + y);
    System.out.println(icon);
    g2d.drawString(icon, x, y);     
}

Where icon is, for example, the string "\u27BF" which should be displayed as "➿".

How could I add support to this character in my code?

Natanael
  • 2,290
  • 6
  • 23
  • 35
  • 4
    Use a font that supports the character. – Elliott Frisch Oct 11 '15 at 19:50
  • But how can I find a font that supports the character? – Natanael Oct 11 '15 at 20:11
  • 1
    @Natanael You can take a look at this [question](http://stackoverflow.com/questions/8446737/recommended-fonts-for-displaying-unicode-characters) to find fonts that support Unicode. If I understand the wikipedia article that was mentioned correctly, these are some of the fonts supporting \u27BF: `Arial Unicode MS` and `GNU Unifont`, `Linux Libertine`. – Fjotten Oct 11 '15 at 20:30
  • Possible duplicate of [Displaying special unicode characters in Java/Swing](http://stackoverflow.com/questions/8127841/displaying-special-unicode-characters-in-java-swing) – nwellnhof Oct 12 '15 at 11:04
  • This question is very similar and related, but not exactly the same question. This question is about Windows representation and portability of a character. My question is not about a specific platform, but how to show a given character. Although the prescriptions to solve the problemas could be the same for both questions, the questions are not the same. – Natanael Oct 12 '15 at 14:52
  • Entypo is one great font for symbols. – Natanael Oct 18 '15 at 15:39

1 Answers1

2

Look for a freely redistributable (e.g. open source) TrueType font which contains your character. Assuming the font license allows it, you could take an existing font containing your character, and subset it to have just the character you need - using e.g. Font Optimizer. Alternatively, you could create your own font containing just this character using font design tools (e.g. FontForge).

Then you can embed the font as a resource in your JAR, and load it like this:

InputStream inp = getClass().getResourceAsStream("/com/example/myfont.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, inp).deriveFont(18f);
g2d.setFont(font);

The deriveFont() call is because by default the loaded font size will be tiny (1 point), so this resizes it to 18 points.

Of course, rather than loading it every time you want to draw, you should load it once, say in the constructor or a static initialiser, and then stash it in a field for use by your draw routine.

This way your character should be visible across all platforms, regardless of what fonts the user has installed, and furthermore should look the same (or very similar) across all platforms too.

Simon Kissane
  • 4,373
  • 3
  • 34
  • 59