0

Suppose a font Kalpurush.ttf (you may find this font here). As it is an Assamese Font and may not have installed in everyone's computer, so, I've embedded this font in my website. It displays fine in any browser, (except android webview. I do not have any headache about android). In fact I've never found any Assamese font to be so nice.

Now I've tried this same font in a Java Swing Application. I've written this class:

public class AssameseFont {
public AssameseFont(){}

public Font Assamese(){
  File file = new File("kalpurush.ttf");
  Font as = null;
  try{          
   FileInputStream input = new FileInputStream(file);
   as = Font.createFont(Font.TRUETYPE_FONT, file);
   as = as.deriveFont(Font.PLAIN, 18);       
   GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(as);

  }
  catch(FontFormatException | IOException e){
  JOptionPane.showMessageDialog(null, ""+e);
  }
 return as;    
}    
}

I use to call this in my components using setFont() method.

But some of my texts does not display as it should be. Why does it happen? Is it a font problem? Or am I doing anything wrong in my java code?

Pranjal Choladhara
  • 845
  • 2
  • 14
  • 35

1 Answers1

2

As it is an Assamese Font and may not have installed in everyone's computer, so, I've embedded this font in my website. ..

File file = new File("kalpurush.ttf");

That file will point to a (non-existent) file on the user's machine.

The font must instead be accessed by URL.


See also Setting custom font.


The code seen on the linked thread, but with the kalpurush.ttf font.

enter image description here

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

class DisplayFont {
    public static void main(String[] args) throws Exception {
        URL fontUrl = new URL("http://assameseonline.com/css/kalpurush.ttf");
        Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
        font = font.deriveFont(Font.PLAIN,20);
        GraphicsEnvironment ge =
            GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(font);

        JLabel l = new JLabel(
            "The quick brown fox jumps over the lazy dog. 0123456789");
        l.setFont(font);
        JOptionPane.showMessageDialog(null, l);
    }
}
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • This is fine. but when I type `প্ৰাঞ্জল` in java it is displayed something like `পৰাঞ্জল` . It happens only in java. I do not know why. – Pranjal Choladhara Jul 04 '15 at 18:42
  • @PranjalCholadhara If you are using MacOSX, see this: http://stackoverflow.com/questions/5994815/rendering-devanagari-ligatures-unicode-in-java-swing-jcomponent-on-mac-os-x – Karol S Jul 09 '15 at 19:44