Custom font loading using FontsLoader.loadExternalFonts

Hi,

My requirement is to load the multiple custom fonts in run time,and for that i have written sample test program to check the behavior of custom font loading.

The comments are in correspondence with the code line as my analysis,could you confirm this and why it is behaved like that.
This sample program run with latest aspose.slides-15.5.1-jdk17 as well.
Thanks.

package slide.sample;


import com.aspose.slides.FontsLoader;
import com.aspose.slides.LoadFormat;
import com.aspose.slides.LoadOptions;
import com.aspose.slides.PdfOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


public class SlideCustomFontTest {
public SlideCustomFontTest() {
super();
}

public static void main(String[] args) {
startWithCustomFont();
startWithOutCustomFont();

}

public static void startWithCustomFont() {

//This has been excuted in order(with custom font,without custom font and again with cutome font).

//1:font1 has system font ex:WindDing.ttf and font2 has cutomer font ex:CustomerWingDing.ttf

String[] customFontsDirsTemp1 = new String[] { “/tmp/font1”, “/tmp/font2” };
FontsLoader.loadExternalFonts(customFontsDirsTemp1);


final LoadOptions loadOptions = new LoadOptions(LoadFormat.Auto);
Presentation ppt1 = new Presentation(readPPT(“WingDingCustomTest.pptx”), loadOptions);
writePDF(performConversion(ppt1, loadOptions), “WithWingDingCustomTest1”);//it renders the customer windging.

//2:font1 has system font ex:WindDing.ttf and font3 folder is empty.

String[] customFontsDirsTemp2 = new String[] { “/tmp/font1”, “/tmp/font3” };
FontsLoader.loadExternalFonts(customFontsDirsTemp2);
Presentation ppt2 = new Presentation(readPPT(“WingDingCustomTest.pptx”), loadOptions);
writePDF(performConversion(ppt2, loadOptions), “WithOutWingDingCustomTest2”);//it does not render the customer winding.

//3:font1 has system font ex:WindDing.ttf and font2 has customer font ex:CustomerWingDing.ttf
String[] customFontsDirsTemp3 = new String[] { “/tmp/font1”, “/tmp/font2” };
FontsLoader.loadExternalFonts(customFontsDirsTemp3);
Presentation ppt3 = new Presentation(readPPT(“WingDingCustomTest.pptx”), loadOptions);
writePDF(performConversion(ppt3, loadOptions), “WithWingDingCustomTest3”);//it renders the cutomer windging.
}

public static void startWithOutCustomFont() {

//This has been excuted in order(without custom font,with custom font and again without custome font).

//1:font1 has system font ex:WindDing.ttf and font3 is empty folder.

String[] customFontsDirsTemp1 = new String[] { “/tmp/font1”, “/tmp/font3” };
FontsLoader.loadExternalFonts(customFontsDirsTemp1);

final LoadOptions loadOptions = new LoadOptions(LoadFormat.Auto);
Presentation ppt1 = new Presentation(readPPT(“WingDingCustomTest.pptx”), loadOptions);
writePDF(performConversion(ppt1, loadOptions), “WithOutWingDingCustomTest1”);//it doesn’t render the customer font since font 3 is empty folder.

//2:font1 has system font ex:WindDing.ttf and font2 has customer font ex:CustomerWingDing.ttf
String[] customFontsDirsTemp2 = new String[] { “/tmp/font1”, “/tmp/font2” };
FontsLoader.loadExternalFonts(customFontsDirsTemp2);
Presentation ppt2 = new Presentation(readPPT(“WingDingCustomTest.pptx”), loadOptions);
writePDF(performConversion(ppt2, loadOptions), “WithWingDingCustomTest2”);//it doesn’t render the customer font, but should have rendered the Customer font.

String[] customFontsDirsTemp3 = new String[] { “/tmp/font1”, “/tmp/font3” };
FontsLoader.loadExternalFonts(customFontsDirsTemp3);
Presentation ppt3 = new Presentation(readPPT(“WingDingCustomTest.pptx”), loadOptions);
writePDF(performConversion(ppt3, loadOptions), “WithOutWingDingCustomTest3”);///it doesn’t render the customer font since font3 is empty folder.


}

public static byte[] performConversion(final Presentation ppt, final LoadOptions lops) {
final PdfOptions options = new PdfOptions();
ByteArrayOutputStream pptStream = null;
try {
final byte[] targetBytes;
pptStream = new ByteArrayOutputStream();
ppt.save(pptStream, SaveFormat.Pdf, options);
targetBytes = pptStream.toByteArray();
return targetBytes;
} catch (Exception ex) {
ex = ex;
}
return null;
}

public static void writePDF(byte[] pdf, String fileName) {
FileOutputStream fos = null;
try {
File file = new File("/tmp/" + fileName + “.pdf”);
if (file.exists())
file.delete();
file.createNewFile();
fos = new FileOutputStream(file);
fos.write(pdf);
} catch (IOException e) {
e = e;
}
}

public static InputStream readPPT(String filename) {
File file = new File("/tmp/" + filename);
FileInputStream fin = null;
try {
fin = new FileInputStream(file);
} catch (FileNotFoundException e) {
e = e;
}
return fin;
}
}

Hi,

I have observed the sample code shared by you. Unfortunately, I am unable to develop the understanding about issue incurring on your end. Can you please elaborate the issue or effect appearing on your end in detail so that I may understand and help you accordingly.

Moreover, after seeing the code, I like to suggest you to clear the font cache every time before loading the fonts using FontsLoader by using following statement.

//Clear Font Cachce
FontsLoader.clearCache();

Many Thanks,

Hi,

There are 2 scenarios.
***********************First one ***********
From sample code:The step 1,I’m loading 2 font folders, font1 folder has WingDing.ttf font and font3 folder is empty.

The WingDingCustomTest.pptx is file to be converted to pdf after applaying the CustomerWingDing font which is in CustomerWingDing.ttf.

After performConversion i get the pdf is without the CustomerWingDing font since we have loaded only original WingDing.ttf in folder1 and custom folder2 is empty-is expected.

The Step2,font1 folder has system font WindDing.ttf and font2 has customer font CustomerWingDing.ttf.Here again loadExternalFonts is called and again I run the performConversion on WingDingCustomTest.pptx file,now i expect the PDF should render the CustomWingDing,but this is not rendering,why?

Code snippet for first scenario.

public static void startWithOutCustomFont() {

//This has been excuted in order(without custom font,with custom font and again without custome font).

//step1:font1 has system font ex:WindDing.ttf and font3 is empty folder.

String[] customFontsDirsTemp1 = new String[] { “/tmp/font1”, “/tmp/font3” };
FontsLoader.loadExternalFonts(customFontsDirsTemp1);

final LoadOptions loadOptions = new LoadOptions(LoadFormat.Auto);
Presentation ppt1 = new Presentation(readPPT(“WingDingCustomTest.pptx”), loadOptions);
writePDF(performConversion(ppt1, loadOptions), “WithOutWingDingCustomTest1”);//it doesn’t render the customer font since font3 is an empty folder.

//Step2:font1 has system font ex:WindDing.ttf and font2 has customer font ex:CustomerWingDing.ttf
String[] customFontsDirsTemp2 = new String[] { “/tmp/font1”, “/tmp/font2” };
FontsLoader.loadExternalFonts(customFontsDirsTemp2);
Presentation ppt2 = new Presentation(readPPT(“WingDingCustomTest.pptx”), loadOptions);
writePDF(performConversion(ppt2, loadOptions), “WithWingDingCustomTest2”);
//it doesn’t render the customer font, but should have rendered the Customer font.Why not rendering?.

}

First one end,Seccond start

Now running in reverse order.

From sample code:The step 1,I’m loading 2 font folders, font1 folder has WingDing.ttf font and font2 has CustomerWingDing.ttf.After performConversion i get the PDF rendered with the CustomerWingDing
font since we have loaded the CustomerWingDing.tff in folder2 which is expected and correct.

The Step2,font1 folder has system font WindDing.ttf and font3 is empty fonlder.Here again loadExternalFonts is called and again
I run the performConversion on WingDingCustomTest.pptx file,now i
expect the PDF should not render with CustomWingDing and is happening as expected,but the question here is it behaves inconsistently as in scenario 1 the second loadExternalFonts call is not working, but here by not rendering it looks the second loadExternalFonts call is working,why?

Code snippet for second scenario.

public static void startWithCustomFont() {

//This has been excuted in order(with custom font,without custom font and again with cutome font).

//Step1:font1 has system font ex:WindDing.ttf and font2 has cutomer font ex:CustomerWingDing.ttf

String[] customFontsDirsTemp1 = new String[] { “/tmp/font1”, “/tmp/font2” };
FontsLoader.loadExternalFonts(customFontsDirsTemp1);


final LoadOptions loadOptions = new LoadOptions(LoadFormat.Auto);
Presentation ppt1 = new Presentation(readPPT(“WingDingCustomTest.pptx”), loadOptions);
writePDF(performConversion(ppt1, loadOptions), “WithWingDingCustomTest1”);//it renders the customer windging.

//Step2:font1 has system font ex:WindDing.ttf and font3 folder is empty.

String[] customFontsDirsTemp2 = new String[] { “/tmp/font1”, “/tmp/font3” };
FontsLoader.loadExternalFonts(customFontsDirsTemp2);
Presentation ppt2 = new Presentation(readPPT(“WingDingCustomTest.pptx”), loadOptions);
writePDF(performConversion(ppt2, loadOptions), “WithOutWingDingCustomTest2”);//it does not render the customer winding.Since second call to loadExternalFonts has no CustomerWingDing.ttf and hence the pdf genrated is not rendered with CustomerWingDing font,why the second call to loadExternalFonts works here which is inconsistent comapre to first sceario,why?

}

The code is very explanatory ,and if you run the sample you would know the behavior.One more thing i have tested with FontsLoader.clearCache(); but there is no use.
Thanks,
Raju S

Hi Raju S,

Thank you for sharing the clarification. I have observed the two scenarios shared by you and have developed the understanding about that. I request you to please share the source presentation, used sample fonts and generated PDF files in both scenarios to reproduce the effect on my end and help you further in this regard. I will be able to proceed further on my end once the requested information will be shared.

Many Thanks,

I have attached all the sample files.you need to copy the wingding.ttf from windows and use as system folder font1.

Tested environment: java version “1.7.0_72” and OS linux6 64bit.

Hi Raju S,

I have worked with the sample data shared by you and have been able to observe the issue specified. An issue with ID SLIDESJAVA-34953 has been created in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be automatically notified once the issue will be resolved.

We are sorry for your inconvenience,

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


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