FontSettings.addFontsFolder only loads TTF fonts

I found that Aspose.Imaging wasn’t finding MS Gothic when attempting to render an EMF containing Japanese text.


It turns out that when you call FontSettings.addFontsFolder, it doesn’t find MS Gothic because MS Gothic is a TTC (TrueType Collection), and Aspose.Imaging is only looking for files ending in “.ttf”.

This is in the obfuscated code of Aspose, in com.aspose.imaging.B:

private void a(File var1) throws IOException, FontFormatException {
File[] var2 = var1.listFiles();
File[] var3 = var2;
int var4 = var2.length;

for(int var5 = 0; var5 < var4; ++var5) {
File var6 = var3[var5];
if(var6.isDirectory()) {
this.a(var6);
} else if(var6.getName().toLowerCase().endsWith(“ttf”)) {
Font var7 = Font.createFont(0, var6);
this.b.add(var7);
}
}

}

This code is clearly not going to find the TTC files since it requires the name to end in “ttf”, but I also worry that it’s using java.awt.Font to create the font here, as this probably won’t find the font, as Java won’t know about it either.

(On a separate note, calls to String.toLowerCase() without passing a Locale are generally very dangerous. Although perhaps in this particular instance it might be safe, it’s safest to avoid using this method entirely. It would be worth getting calls to this removed from all libraries just in case. In this particular case, Locale.ROOT should have been passed in.)

Hi Nicholas,

Thank you for your inquiry and sharing details.

This is to update you that an investigation ticket has been logged into our system with ID IMAGINGJAVA-643. All information has also been shared with our product team. Our product team will further look into it and provide feedback. We will update you with the feedback in this thread once available.

The issues you have found earlier (filed as IMAGINGJAVA-643) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

Seems like I get the same results under v17.4. EmfMetafileImage#getMissedFonts() returns {“MS Gothic”}, same as before, and the resulting image looks the same.


Attaching the EMF we used, as well as the font. Maybe the library failed to parse the font, or something like that?

Hi Nicholas,


I have worked with file shared by you using Aspose.Imaging for Java 17.4 and unable to observe the issue.

Please try using the attached sample code. You can place the MS Gothic font in your computer directory somewhere. By using sample code, you have option to load the fonts externally using FontSettings.setFontsFolder(Fontpath) method. You can try setting the font folder and render the EMF to PDF. Then you can try generating the PDF without setting the fonts folder and you will observe the difference. I have attached the generated PDF file on my end for your reference as well.

The shared code will help you to achieve your requirements. Please share feedback with us if there is still an issue.

Best Regards,

Ah, I believe this helps reproduce a different problem that I hadn’t noticed yet. Our own code had caught the exception and proceeded, which hid the real problem…


Exception in thread “main” class com.aspose.imaging.coreexceptions.FrameworkException: Font name not found —> java.awt.FontFormatException: Font name not found
— End of inner exception stack trace —
com.aspose.imaging.FontSettings.setFontsFolders(Unknown Source)
com.aspose.imaging.FontSettings.setFontsFolders(Unknown Source)
com.nuix.integration.TestEmf.main(TestEmf.java:26)
at com.aspose.imaging.FontSettings.setFontsFolders(Unknown Source)
at com.aspose.imaging.FontSettings.setFontsFolders(Unknown Source)
at com.nuix.integration.TestEmf.main(TestEmf.java:26)
Caused by: java.awt.FontFormatException: Font name not found
at sun.font.TrueTypeFont.init(TrueTypeFont.java:590)
at sun.font.TrueTypeFont.(TrueTypeFont.java:198)
at sun.font.TrueTypeFont.(TrueTypeFont.java:178)
at sun.font.CFontManager.createFont2D(CFontManager.java:155)
at java.awt.Font.(Font.java:615)
at java.awt.Font.createFont(Font.java:1056)
at com.aspose.imaging.z.a(Unknown Source)
at com.aspose.imaging.z.(Unknown Source)
… 3 more

I’m attaching the font which it failed on.

Hi Nicholas,


Thanks for sharing further details with us.

I have tested the EMF file shared by you using code sample given in Testemf.txt and have been able to reproduce the exception. A ticket with ID IMAGINGJAVA-692 has been added in our issue tracking system to resolve the issue. This thread has been linked with ticket so that you may be notified once issue will be fixed.

We are sorry for your inconvenience,

The issues you have found earlier (filed as ) have been fixed in this Aspose.Words for JasperReports 18.3 update.

We have tried updating to the latest version of Aspose.Imaging and the problem is still there. I think maybe the fix notification was for another issue as it mentions Aspose.Words.

@nuix,

I have observed your comments. Can you please try to follow below procedure.

  1. Remove all fonts “Ms Mincho” from system.
  2. Download font msmincho.zip file and copy to any folder.
  3. Execute this code.

FontSettings.setFontsFolder("");
EmfRecorderGraphics2D graphics = new EmfRecorderGraphics2D(new Rectangle(0,0,150,150),new Size(15000,15000),new Size(150,150) );
Font font = new Font(“MS Mincho”, 20);
graphics.drawString(“MS Mincho”,font, Color.getBlue(), 0, 50);
EmfImage image = graphics.endRecording();
try
{
PdfOptions bmpOptions = new PdfOptions();
bmpOptions.setVectorRasterizationOptions(new EmfRasterizationOptions()
{{
setPageHeight(1000);
setPageWidth(1000);
setBackgroundColor(Color.getWhiteSmoke());
}});

image.save(“with-munich-font.pdf”,bmpOptions);
}
finally
{
image.dispose();
}

Sorry, we appear to have dropped the ball on this one somehow.

Where do I download this msmincho.zip from?

@nuix,

You may please download the mentioned font from below link.

Okay, with the current copy of Aspose Imaging, if we use that font file, or even our own copy of that font file, in the absence of all other fonts, the fonts load successfully.

But, if I set the font folder to our own font folder, the test still fails.

I’ve tracked it down to the font file I’m attaching now.

Lucida Grande.ttc.zip (1.2 MB)

@nuix,

Can you please share which version of Aspose.Imaging for Java you are using on your end. Please try using latest Aspose.Imaging for Java 19.11 if you are using any older version and share your kind feedback with us.

We were using 19.10, but I have just reproduced the same issue in 19.11 using the sample code above and the Lucida Grande font file previously attached.

@nuix,

Thank you for sharing the feedback. Can you please share that you have performed following steps and used the sample code.

  • Remove all fonts “Ms Mincho” from system

  • Download font and copy to any folder

  • Execute the following code

      FontSettings.setFontsFolder("");
      EmfRecorderGraphics2D graphics = new EmfRecorderGraphics2D(new Rectangle(0, 0, 150, 150), new Size(15000, 15000), new Size(150, 150));
      Font font = new Font("MS Mincho", 20);
      graphics.drawString("MS Mincho",font, Color.getBlue(), 0, 50);
      EmfImage image = graphics.endRecording();
      try
      {
      PdfOptions bmpOptions = new PdfOptions();
              bmpOptions.setVectorRasterizationOptions(new EmfRasterizationOptions()
              {
                  {
                      setPageHeight(1000);
                      setPageWidth(1000);
                      setBackgroundColor(Color.getWhiteSmoke());
                  }
              });
    
      image.save("with-munich-font.pdf",bmpOptions);
      }
      finally
      {
      image.dispose();
      }
    

Then execute the following code, where FontSettings.SetFontsFolder disabled.

//FontSettings.setFontsFolder("");
EmfRecorderGraphics2D graphics = new EmfRecorderGraphics2D(new Rectangle(0,0,150,150),new Size(15000,15000),new Size(150,150) );
Font font = new Font("MS Mincho", 20);
graphics.drawString("MS Mincho",font, Color.getBlue(), 0, 50);
EmfImage image = graphics.endRecording();
try
{
PdfOptions bmpOptions = new PdfOptions();
bmpOptions.setVectorRasterizationOptions(new EmfRasterizationOptions()
{{
setPageHeight(1000);
setPageWidth(1000);
setBackgroundColor(Color.getWhiteSmoke());
}});

image.save("without-munich-font.pdf",bmpOptions);

}
finally
{
image.dispose();
}

Yes, uncommenting the line makes the code work.

In fact, the test can be even shorter:

This fails:

import com.aspose.imaging.FontSettings;

public class AsposeTest
{
    public static void main(String[] args) throws Exception
    {
        FontSettings.setFontsFolder("");
    }
}

@nuix,

Should we consider this issue as resolved now?

Given that it throws an exception merely trying to load the font I attached, I don’t think so?

@nuix,

Based on your following comments, I assumed things are resolved on your end.

If you still fell we need to investigate the issue further on our end then please provide source code reproducing the issue, source fonts, source files, Operating System and Java details on your end. Please share the information so that we may proceed further to help you out.