Display Traditional and Simplified Chinese in Java

Is it possible to draw both traditional and simplified Chinese characters in a single Java application with logical fonts such as "SansSerif"? I only get the traditional variety with CJK code points.

I tried setting Locale.setDefault () and GraphicsEnvironment.preferLocaleFonts () before creating the font. I tried using -Duser.language and -Duser.country on the command line when running java.exe. Also tried to create a font with the AttributedCharacterIterator.Attribute.LANGUAGE attribute. No effect.

I do not use Swing or AWT. Just trying to drag BufferedImage into the screen. I am on Windows 7, and I confirmed that I have installed fonts that support traditional and simplified Chinese (MingLiU and SimSun). I also checked the Java font configuration file, and I see both of these fonts listed there.

What else should I do?

+4
source share
1 answer

Yes, you can display both simplified and traditional Chinese text in Java if you have a font that includes both character sets.

I wrote this short program to demonstrate:

import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; public class ChineseFonts { public static final int GAP = 35; public static final int FONTS_PER_LINE = 2; public ChineseFonts(String s) { Rectangle rect = new Rectangle(0, 0, 1024, 768); BufferedImage bufferedImage = new BufferedImage((int) Math.ceil(rect.getWidth()), (int) Math.ceil(rect.getHeight()), BufferedImage.TYPE_4BYTE_ABGR); Graphics graphics = bufferedImage.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(rect.x, rect.y, rect.width, rect.height); graphics.setColor(Color.BLUE); String title = "Chinese Fonts on " + System.getProperty("os.name") + ", version " + System.getProperty("os.version"); int fontY = 30; printString(title, graphics, 0, fontY, new Font(Font.SERIF, Font.BOLD | Font.ITALIC, 28), false); fontY += GAP + 10; int counter = 0; for (String fontName : new String[]{Font.MONOSPACED, Font.SANS_SERIF, Font.SERIF}) { Font font = new Font(fontName, Font.PLAIN, 24); printString(s, graphics, counter++, fontY, font, true); if (counter % FONTS_PER_LINE == 0) fontY += GAP; } Font[] localFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts(); List<Font> chineseFonts = new ArrayList<Font>(); String simplifiedAndTraditionalChinese = "????"; for (int j = 0; j < localFonts.length; j++) { if (localFonts[j].canDisplayUpTo(simplifiedAndTraditionalChinese) == -1) { chineseFonts.add(localFonts[j].deriveFont(24F)); } } for (Font font : chineseFonts) { printString(s, graphics, counter++, fontY, font, true); if (counter % FONTS_PER_LINE == 0) fontY += GAP; } graphics.dispose(); try { ImageIO.write(bufferedImage, "png", new File("chineseFonts.png")); } catch (Exception e) { // ignored } } private void printString(String s, Graphics graphics, int counter, int y, Font font, boolean showFontDetails) { graphics.setFont(font); if (showFontDetails) s = font.getFamily() + " " + s; graphics.drawString(s, 20 + (counter % FONTS_PER_LINE) * 510, y); if (showFontDetails) System.out.println("Printing " + s + " using " + font.getName() + ", which is " + font.getFontName() + " in family " + font.getFamily()); } public static void main(String args[]) { new ChineseFonts("S: 漢字 T: 汉字"); } } 

Two simplified and 2 traditional Chinese characters are used on the Wikipedia page on Chinese characters. When you run it, all used fonts are printed on stdout, and the image is displayed with font names and Chinese characters.

Here are three logical fonts and the first physical font that worked:

  • Monospaced
  • Sanserif
  • Serif
  • Arial Unicode MS S
0
source

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


All Articles