Problem with display of Lao text in Java Swing Components

Viewed 251

I am trying to display Lao language text:

ຄິ້ງເຄັງສາຍພານ

in a JLabel in Java swing using sample code obtained from the following link.

The font picked is DokChampa. However, the text displayed is:

Sample screen-shot

Note that the characters above the alphabets are displaced and moved to the right. However, the same text entered in Notepad or other editors in the system using the DokChampa font are displayed correctly.

When data entry is done in the JTextField using the Windows supported Lao keyboard also, the behavior is the same.

Other Fonts tried like LaoUI also have the same behavior in Java Swing components

Using JDK 1.8.0_152. My sample program is as below. Expected output is to see the text displayed as mentioned above.

import java.awt.Font;
import java.awt.GraphicsEnvironment;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class TestLaos {

public static void main(String[] args) {
    SwingUtilities.invokeLater( new Runnable() {
        public void run() {
            JLabel l = new JLabel();
            String [] lstr = GraphicsEnvironment.getLocalGraphicsEnvironment ().getAvailableFontFamilyNames();
            Font [] fList = new Font [lstr.length];
            for (int i = 0; i <lstr.length; i ++)
            {
             fList [i] = new Font (lstr [i], Font.PLAIN, 20);
            }
            String rawString = new String("ຄິ້ງເຄັງສາຍພານ");
            l.setText(rawString);
            Font f = l.getFont ();
            if (f.canDisplayUpTo (rawString)!=-1)
            {//Find fonts that can not be displayed, then find available fonts
                for (int i = 0; i <fList.length; i ++)
                {
                    if (fList [i] .canDisplayUpTo (rawString) ==-1 && fList [i].getFontName().startsWith("DokChampa"))
                    {
                        System.out.println("Using font " + fList [i].getFontName() + " to display " + rawString);
                        l.setFont (fList [i]);
                        break;
                    }
                }
            }
           
            JOptionPane.showMessageDialog(null, l);
        }
    });
}
}
1 Answers

I have a Windows 10 operating system. I'm using an Oracle Java JDK 14.0.2 and compiling to a Java 8 standard.

Here are the results of my test.

Laos Fonts

Some of the text is correct and some isn't. I suspect problems with the fonts.

Here's the code I ran. Try it on your system and see what happens.

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestLaos implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TestLaos());
    }
    
    @Override
    public void run() {
        JFrame frame = new JFrame("Laos Fonts");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        String rawString = new String("ຄິ້ງເຄັງສາຍພານ");
        List<Font> laosFonts = getLaosFonts(rawString);
        frame.add(createMainPanel(laosFonts, rawString), BorderLayout.CENTER);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    private JPanel createMainPanel(List<Font> laosFonts, String rawString) {
        JPanel panel = new JPanel(new GridLayout(0, 2, 10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        for (Font font : laosFonts) {
            JLabel label1 = new JLabel(font.getFontName());
            label1.setFont(label1.getFont().deriveFont(Font.PLAIN, 20f));
            panel.add(label1);
            
            JLabel label2 = new JLabel(rawString);
            label2.setFont(font.deriveFont(Font.PLAIN, 20f));
            panel.add(label2);
        }
        
        return panel;
    }
    
    private List<Font> getLaosFonts(String rawString) {
        List<Font> laosFonts = new ArrayList<>();
        Font[] list = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
        
        for (Font font : list) {
            if (font.canDisplayUpTo(rawString) == -1) {
                laosFonts.add(font);
            }
        }
        
        return laosFonts;
    }
    
}
Related