Cells (Java) | Font substitution not working on Mac

Hi,

I have created a small Excel-file containing various fonts that I don’t have installed on my local machine to test the font substitution possibilities provided by the Aspose Cells library for Java.

However, I may have not the full picture on how to actually make this thing work. This is my code (condensed):

FontConfigs.setFontSubstitutes("Arial", new String[] { "Courier New" });

File inputFile = new File("font-tests.xlsx");
File outputFile = new File("font-tests.pdf");

try (InputStream in = new BufferedInputStream(new FileInputStream(inputFile))) {
	Workbook workbook = new Workbook(in);
	com.aspose.cells.PdfSaveOptions pdfSaveOptions = new com.aspose.cells.PdfSaveOptions(SaveFormat.PDF);
	pdfSaveOptions.setWarningCallback(info -> {
		System.err.println("Save warning: " + info.getDescription());
	});

	try (OutputStream os = new BufferedOutputStream(new FileOutputStream(outputFile))) {
		workbook.save(os, pdfSaveOptions);
	}
}

So, my expectation would have been that all Arial fonts are to be replaced by Courier New. This is the output when running the code:

Save warning: Font substitution: Font [ Arial; Regular ] has been substituted in Cell [ A1 ] in Sheet [ Tabelle1 ].
Save warning: Font substitution: Font [ Bauhaus 93; Regular ] has been substituted in Cell [ A3 ] in Sheet [ Tabelle1 ].
Save warning: Font substitution: Font [ Bernard MT Condensed; Regular ] has been substituted in Cell [ A4 ] in Sheet [ Tabelle1 ].
Save warning: Font substitution: Font [ Bodoni MT Black; Regular ] has been substituted in Cell [ A5 ] in Sheet [ Tabelle1 ].
Save warning: Font substitution: Font [ Bradley Hand ITC; Regular ] has been substituted in Cell [ A6 ] in Sheet [ Tabelle1 ].
Save warning: Font substitution: Font [ Bookman Old Style; Regular ] has been substituted in Cell [ A7 ] in Sheet [ Tabelle1 ].
Save warning: Font substitution: Font [ Castellar; Regular ] has been substituted in Cell [ A8 ] in Sheet [ Tabelle1 ].
Save warning: Font substitution: Font [ Cooper Black; Regular ] has been substituted in Cell [ A9 ] in Sheet [ Tabelle1 ].
Save warning: Font substitution: Font [ Forte; Regular ] has been substituted in Cell [ A10 ] in Sheet [ Tabelle1 ].
Save warning: Font substitution: Font [ Gill Sans MT Condensed; Regular ] has been substituted in Cell [ A11 ] in Sheet [ Tabelle1 ].
Save warning: Font substitution: Font [ Matura MT Script Capitals; Regular ] has been substituted in Cell [ A12 ] in Sheet [ Tabelle1 ].
Save warning: Font substitution: Font [ Monotype Corsiva; Italic ] has been substituted in Cell [ A13 ] in Sheet [ Tabelle1 ].
Save warning: Font substitution: Font [ Playbill; Regular ] has been substituted in Cell [ A14 ] in Sheet [ Tabelle1 ].
Save warning: Font substitution: Font [ Kunstler Script; Regular ] has been substituted in Cell [ A15 ] in Sheet [ Tabelle1 ].
Save warning: Font substitution: Font [ Showcard Gothic; Regular ] has been substituted in Cell [ A16 ] in Sheet [ Tabelle1 ].
Save warning: Font substitution: Font [ Wide Latin; Regular ] has been substituted in Cell [ A17 ] in Sheet [ Tabelle1 ].
Save warning: Font substitution: Font [ Vladimir Script; Regular ] has been substituted in Cell [ A18 ] in Sheet [ Tabelle1 ].
Save warning: Font substitution: Font [ Comic Sans MS; Regular ] has been substituted in Cell [ A19 ] in Sheet [ Tabelle1 ].

So, there war a substitution in place. Note, I get the same output when I don’t set any font substitution at all. It does seem to have any effect, as the resulting PDF does not use the Courier New font anywhere.

Could you please advise on how to make font substitutions work? On request, I can provide the Excel file for testing purposes.

Thank you!

aspose-cells Version is 20.5
JRE: OpenJDK 64-Bit Server VM 11+28

@braintribe,
Thank you for your query.
Please provide your template Excel file for our testing. We will use it for our testing and provide assistance accordingly.

@braintribe,
We have further evaluated your issue and suggest you to set font folder first and then set font substitutes as follows:

FontConfigs.setFontFolder(the folder which contains your Courier New font file, true);
FontConfigs.setFontSubstitutes("Arial", new String[] { "Courier New" }); 

Please note that currently only ttf and ttc font files are supported.

Thanks for your reply. I added the following lines as you requested:

FontConfigs.setFontFolder("/System/Library/Fonts", true);
FontConfigs.setFontSubstitutes("Arial", new String[] { "Courier" });

If I understood the documentation right, the first line would not be necessary as this is a system font folder. Nevertheless, I gave it a try and got this exception in the first line:

Exception in thread “main” java.lang.ClassCastException: class com.aspose.diagram.a.e.b cannot be cast to class java.lang.Iterable (com.aspose.diagram.a.e.b is in unnamed module of loader ‘app’; java.lang.Iterable is in module java.base of loader ‘bootstrap’)
at com.aspose.diagram.FolderFontSource.b(Unknown Source)
at com.aspose.diagram.FontConfigs.a(Unknown Source)
at com.aspose.diagram.FontConfigs.a(Unknown Source)
at com.aspose.diagram.FontConfigs.a(Unknown Source)
at com.aspose.diagram.FontConfigs.setFontSources(Unknown Source)
at com.aspose.diagram.FontConfigs.setFontFolder(Unknown Source)

Could you please advise on what to do next?

Thanks
Roman

PS: It seems that the setFontFolder should not be necessary anymore (at least on Linux), according to this: Aspose.Cells.FontConfigs.SetFontSubstitutes needs Aspose.Cells.FontConfigs.SetFontFolders for Linux - #5 by babar.raza

I also tried with other fonts (e.g., “Symbol”)

I also made sure that the TTF Font is really there:

% ls -la /System/Library/Fonts/Symbol.ttf
-rw-r–r-- 1 root wheel 80016 25 Aug 2019 /System/Library/Fonts/Symbol.ttf

@braintribe,
I have tried the following sample code in macOS:

FontConfigs.setFontFolder("/System/Library/Fonts", true);
FontConfigs.setFontSubstitutes("Arial", new String[] { "Courier New" });

File inputFile = new File("Book1.xlsx");
File outputFile = new File("font-tests.pdf");

try (InputStream in = new BufferedInputStream(new java.io.FileInputStream(inputFile))) 
{
	Workbook workbook = new Workbook(in);
	com.aspose.cells.PdfSaveOptions pdfSaveOptions = new com.aspose.cells.PdfSaveOptions(SaveFormat.PDF);
	
	pdfSaveOptions.setWarningCallback(info -> {
		System.err.println("Save warning: " + info.getDescription());
		});
	OutputStream os = new java.io.BufferedOutputStream(new java.io.FileOutputStream(outputFile));
	try
	{
		workbook.save(os, pdfSaveOptions);
	}
	catch(Exception ex)
	{
		
	}

}
catch(Exception ex)
{
	
} 

No exception is raised and program executes fine. I tried following two scenarios:

  1. Enable code
    FontConfigs.setFontFolder("/System/Library/Fonts", true);

It produces following result:

Save warning: Font substitution: Font [ Abadi MT Condensed Extra Bold; Regular ] has been substituted in Cell [ A1 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Abadi MT Condensed Extra Bold; Regular ] has been substituted in Cell [ B1 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Abadi MT Condensed Light; Regular ] has been substituted in Cell [ A2 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Abadi MT Condensed Light; Regular ] has been substituted in Cell [ B2 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Al Bayan Plain; Regular ] has been substituted in Cell [ A3 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Al Bayan Plain; Regular ] has been substituted in Cell [ B3 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Angsana New; Regular ] has been substituted in Cell [ A4 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Angsana New; Regular ] has been substituted in Cell [ B4 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Calibri; Regular ] has been substituted in Cell [ A6 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Calibri; Regular ] has been substituted in Cell [ B6 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Arial Italic; Regular ] has been substituted in Cell [ A9 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Arial Italic; Regular ] has been substituted in Cell [ B9 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Arial Bold; Regular ] has been substituted in Cell [ A10 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Arial Bold; Regular ] has been substituted in Cell [ B10 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Arial Bold Italic; Regular ] has been substituted in Cell [ A11 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Arial Bold Italic; Regular ] has been substituted in Cell [ B11 ] in Sheet [ Sheet1 ].

The output pdf file is attached here:font-tests-with-setFontFolder.pdf (42.3 KB)

  1. Disable code
    //FontConfigs.setFontFolder("/System/Library/Fonts", true);

It produces following result:

Save warning: Font substitution: Font [ Abadi MT Condensed Extra Bold; Regular ] has been substituted in Cell [ A1 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Abadi MT Condensed Extra Bold; Regular ] has been substituted in Cell [ B1 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Abadi MT Condensed Light; Regular ] has been substituted in Cell [ A2 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Abadi MT Condensed Light; Regular ] has been substituted in Cell [ B2 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Al Bayan Plain; Regular ] has been substituted in Cell [ A3 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Al Bayan Plain; Regular ] has been substituted in Cell [ B3 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Angsana New; Regular ] has been substituted in Cell [ A4 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Angsana New; Regular ] has been substituted in Cell [ B4 ] in Sheet [ Sheet1 ].
**Save warning: Font substitution: Font [ Courier New; Regular ] has been substituted in Cell [ A5 ] in Sheet [ Sheet1 ].======>Note this line**
**Save warning: Font substitution: Font [ Courier New; Regular ] has been substituted in Cell [ B5 ] in Sheet [ Sheet1 ].======>Note this line**
Save warning: Font substitution: Font [ Calibri; Regular ] has been substituted in Cell [ A6 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Calibri; Regular ] has been substituted in Cell [ B6 ] in Sheet [ Sheet1 ].
**Save warning: Font substitution: Font [ Arial Black; Regular ] has been substituted in Cell [ A7 ] in Sheet [ Sheet1 ].======>Note this line**
**Save warning: Font substitution: Font [ Arial Black; Regular ] has been substituted in Cell [ B7 ] in Sheet [ Sheet1 ].======>Note this line**
**Save warning: Font substitution: Font [ Arial; Regular ] has been substituted in Cell [ A8 ] in Sheet [ Sheet1 ].======>Note this line**
**Save warning: Font substitution: Font [ Arial; Regular ] has been substituted in Cell [ B8 ] in Sheet [ Sheet1 ].======>Note this line**
Save warning: Font substitution: Font [ Arial Italic; Regular ] has been substituted in Cell [ A9 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Arial Italic; Regular ] has been substituted in Cell [ B9 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Arial Bold; Regular ] has been substituted in Cell [ A10 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Arial Bold; Regular ] has been substituted in Cell [ B10 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Arial Bold Italic; Regular ] has been substituted in Cell [ A11 ] in Sheet [ Sheet1 ].
Save warning: Font substitution: Font [ Arial Bold Italic; Regular ] has been substituted in Cell [ B11 ] in Sheet [ Sheet1 ].

The output PDF file is attached here: font-tests-without-setFontFolder.pdf (17.7 KB)

You can see that without setting the fonts folder, substitution does not take place as expected.

Following is the template file which is used for testing:Book1.xlsx 2.zip (7.2 KB)

You may please try the scenario again and share the feedback as no exception is raised here and also first line is required for proper fonts substitution.

@braintribe,
We have further analyzed your exception details and found that you are using FontConfigs.setFontFolder() function from Aspose.Diagram library instead of from Aspose.Cells which is causing this exception. You may please fully qualify the setFontFolder() function by modifying the code as follows:

com.aspose.cells.FontConfigs.setFontFolder("/System/Library/Fonts", true);
com.aspose.cells.FontConfigs.setFontSubstitutes("Arial", new String[] { "Courier New" });

Also, font folder “/System/Library/Fonts” on Mac is not loaded by default and you have to set it. We have logged a ticket in our database to resolve this issue and will fix to load font folder “/System/Library/Fonts” on Mac by default.

This issue has been logged as

CELLSJAVA-43192 – Load font folder /System/Library/Fonts on MacOS by default

@braintribe,
This is to inform you that we have fixed your issue (logged earlier as “CELLSJAVA-43192”) now. We will soon provide you the fixed version after performing QA and incorporating other enhancements and fixes.

@braintribe,
Please try our fix V20.5.3. We load the fonts in the folder /System/Library/Fonts by default.
So you can change the code from

com.aspose.cells.FontConfigs.setFontFolder("/System/Library/Fonts", true);
com.aspose.cells.FontConfigs.setFontSubstitutes("Arial", new String[] { "Courier New" });

to

com.aspose.cells.FontConfigs.setFontSubstitutes("Arial", new String[] { "Courier New" });

If you have any issue, please let us know.

aspose-cells-20.5.3-java.zip (7.1 MB)

The issues you have found earlier (filed as CELLSJAVA-43192) have been fixed in Aspose.Cells for Java v20.6. This message was posted using Bugs notification tool by ahsaniqbalsidiqui