Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading CJK fonts is very slow #954

Closed
asolntsev opened this issue Sep 23, 2023 · 0 comments
Closed

Loading CJK fonts is very slow #954

asolntsev opened this issue Sep 23, 2023 · 0 comments
Labels

Comments

@asolntsev
Copy link
Contributor

Describe the bug
We use library FlyingSaucer which in turn uses OpenPDF 1.3.30.
We have a bunch of unit-tests that generate PDFs.
But every run of such a test takes 4+ seconds which is very slow and annoying.

Solution
I found that the problem is how class com.lowagie.text.pdf.BaseFont loads bytes from a jar.
Wrapping InputStream into BufferInputStream makes it much faster.
I am going to submit a pull request soon.

To Reproduce
To reproduce the issue, run the following code.

NB! Don't run it as a part of "OpenPDF" project.
Run it from a separate project. Then CJK fonts are loaded from JAR (openpdf-1.3.30.jar), and such loading is very slow.

import org.junit.jupiter.api.Test;

class CJKFontLoadingTest {

  /**
   * This method takes ~4 seconds (when using openpdf-1.3.30.jar as a JAR).
   * This is incredibly slow.
   */
  @Test
  void loadCJKFonts() throws Exception {
    for (String[] cjkFont : cjkFonts) {
      String fontName = cjkFont[0];
      String encoding = cjkFont[1];

      BaseFont.createFont(fontName + ",BoldItalic", encoding, false);
      BaseFont.createFont(fontName + ",Italic", encoding, false);
      BaseFont.createFont(fontName + ",Bold", encoding, false);
      BaseFont.createFont(fontName, encoding, false);
    }
  }

  // fontName, encoding
  private static final String[][] cjkFonts = {
    { "STSong-Light", "UniGB-UCS2-H" },
    { "STSong-Light", "UniGB-UCS2-V" },
    { "STSongStd-Light", "UniGB-UCS2-H" },
    { "STSongStd-Light", "UniGB-UCS2-V" },
    { "MHei-Medium", "UniCNS-UCS2-H" },
    { "MHei-Medium", "UniCNS-UCS2-V" },
    { "MSung-Light", "UniCNS-UCS2-H" },
    { "MSung-Light", "UniCNS-UCS2-V" },
    { "MSungStd-Light", "UniCNS-UCS2-H" },
    { "MSungStd-Light", "UniCNS-UCS2-V" },
    { "HeiseiMin-W3", "UniJIS-UCS2-H" },
    { "HeiseiMin-W3", "UniJIS-UCS2-V" },
    { "HeiseiKakuGo-W5", "UniJIS-UCS2-H" },
    { "HeiseiKakuGo-W5", "UniJIS-UCS2-V" },
    { "KozMinPro-Regular", "UniJIS-UCS2-HW-H" },
    { "KozMinPro-Regular", "UniJIS-UCS2-HW-V" },
    { "HYGoThic-Medium", "UniKS-UCS2-H" },
    { "HYGoThic-Medium", "UniKS-UCS2-V" },
    { "HYSMyeongJo-Medium", "UniKS-UCS2-H" },
    { "HYSMyeongJo-Medium", "UniKS-UCS2-V" },
    { "HYSMyeongJoStd-Medium", "UniKS-UCS2-H" },
    { "HYSMyeongJoStd-Medium", "UniKS-UCS2-V" }
  };
}

Expected behavior
We expect our unit-test to run very quickly (in a few milliseconds), not in 4+ seconds.

@asolntsev asolntsev added the bug label Sep 23, 2023
asolntsev added a commit to asolntsev/OpenPDF that referenced this issue Sep 23, 2023
wrapping InputStream to BufferedInputStream makes much faster loading fonts from openpdf-*.jar
andreasrosdal pushed a commit that referenced this issue Nov 1, 2023
wrapping InputStream to BufferedInputStream makes much faster loading fonts from openpdf-*.jar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants