dhkim0800
dhkim0800

Reputation: 111

Java takes too long time to initialize large OpenType fonts

I must use Noto Sans CJK font in my application. But when I call the method below It takes 10 seconds to initialize.

Font font = //Noto Sans CJK font
JComponent.getFontMetrics(An instance of Noto Sans CJK font);

Can I fix this delay problem? The code can be complicated if the method can help fixing this problem(Like implementing FontMetrics, I don't know how).

Please don't recommend me convert otf font to ttf font. I couldn't convert it because font tool said it's alphabetic font and coundn't even load unicode glyphs. Also I have to use another otf font in application. And I won't use already converted version.

I'm not good at English. Sorry.

Upvotes: 1

Views: 338

Answers (1)

SubOptimal
SubOptimal

Reputation: 22963

Maybe you should provide step by step instructions to reproduce your issue.

I did following on my system.

  • install font file NotoSansCJKjp-Regular.otf from NotoSansCJKjp-hinted.zip
  • compile the example code

    $ javac NotoFont.java
    
  • run the compiled code

    $ time java NotoFont
    

The execution takes less than a second on my system.

output of the above run

java.awt.Font[family=Noto Sans CJK JP Regular,name=Noto Sans CJK JP Regular,style=plain,size=10]

real    0m0.522s
user    0m0.565s
sys     0m0.056s

NotoFont.java

import java.awt.Font;
class NotoFont {
    public static void main(String[] args) {
        Font noto = new Font("Noto Sans CJK JP Regular", Font.PLAIN, 10);
        System.out.println(noto);
    }
}

edit An example to read the font file from inside the Jar. The font is not installed on the system.

Assume following structure.

build/fonts/NotoSansCJKjp-Regular.otf
dist/
NotoFont.java

NotoFont.java

import java.awt.*;
import java.io.*;
import java.swing.*;
class NotoFont {
    private void run() throws FontFormatException, IOException {
        try (InputStream is = this.getClass().getResourceAsStream("/fonts/NotoSansCJKjp-Regular.otf")) {
            Font baseFont = Font.createFont(Font.TRUETYPE_FONT, is);
            Font noto = baseFont.deriveFont(Font.PLAIN, 10);
            System.out.println(noto);
            FontMetrics metrics = new JPanel().getFontMetrics(noto);
            System.out.println(metrics);
        }
    }
    public static void main(String[] args) throws FontFormatException, IOException {
        new NotoFont().run();
    }
}
  • compile the code

    $ javac -d build/ NotoFont.java
    
  • create the Jar

    $ jar cf dist/notofont.jar -C build/ .
    
  • check the Jar content

    $ cd dist/
    $ jar tf notofont.jar
    META-INF/
    META-INF/MANIFEST.MF
    NotoFont.class
    fonts/
    fonts/NotoSansCJKjp-Regular.otf
    
  • run the code

    $ time java -cp notofont.jar NotoFont
    

output of above run

java.awt.Font[family=Noto Sans CJK JP Regular,name=Noto Sans CJK JP Regular,style=plain,size=10]
sun.font.FontDesignMetrics[font=java.awt.Font[family=Noto Sans CJK JP Regular,name=Noto Sans CJK JP Regular,style=plain,size=10]ascent=9, descent=3, height=12]
real 3.02
user 2.11
sys 0.84

Upvotes: 0

Related Questions