Unicode Labels
Tips & TricksSummary
Description
Java has supported Unicode from its very beginning and so does yFiles, which is written in Java. Although Unicode has been around for a while now, there are still many things which can go wrong when you try to use Unicode in your applications in general. This article describes how to avoid the common pitfalls for the particular case of Unicode characters in node or edge labels in yFiles. Moreover, there is a simple demo source code, which you can use to test your setup.
-
Set the proper encoding. Files or streams contain only bytes which have to be properly interpreted in order to treat the contents in the right way. This is especially true for Unicode characters.
If you want to set a label with Unicode characters encoded in UTF-8 directly in a Java source file, you can do that, but you have to tell your IDE, that your Java source files are UTF-8 encoded. If you are invoking
javac
directly, you can set the-encoding utf8
command line option for compiling.If you want to read labels with UTF-8 characters from an external source, use an instance of
InputStreamReader
with the proper encoding.If you use GraphML to store and retrieve your graphs with UTF-8 labels, you do not have to pay special attention to the encoding, since GraphML is a special kind of XML and all XML files are UTF-8 encoded by default.
-
Use a suitable font. Use a font which defines the Unicode characters you want to use. Using a font, which does not define your Unicode characters typically leads to a display where each character is represented by an empty square. If you are using a Windows operating system, you may need to enable the support for additional languages (see the Microsoft Knowledge Base Article on this subject).
The following is a simple demo which uses UTF-8 samples from the
UTF-8 Sampler by The Kermit Project of the Columbia University
as node labels. It is included as an image into this article and as a text file in the Resources section.
It is a Java source file which is UTF-8 encoded. It is based on the DemoBase class from the yFiles demos
and needs y.jar. You can download the source code from the resources section. If you load the file with your IDE,
it should look similar to the image above.
If you run the the demo it should produce an output similar to the following.