Free Support Forum - aspose.com

Font substitution does not work for a text for Linux

Hi,

It seems a font substitution does not work for a text for Linux (Ubuntu 15.10 + mono 4.2.3.4) when exporting a document to pdf (see attached files). The version Aspose.Words 16.11.0 for .NET 4.0 has been tried.
It should be pointed that a font for bullets is really substituted.
The bug does not concern to Windows.

Sample code:
{
// for font of a text - it does not work, the font Garuda is used instead of FreeMono.
Aspose.Words.Fonts.FontSettings.DefaultInstance.AddFontSubstitutes(“Arial”, new string[]{“FreeMono”});
// for font of bullets - it works.
Aspose.Words.Fonts.FontSettings.DefaultInstance.AddFontSubstitutes(“Symbol”, new string[]{“DejaVu Serif”});
var doc = new Aspose.Words.Document(“ArialTextAndSymbolBullet.docx”);
doc.Save(“ArialTextAndSymbolBullet.docx.pdf”);
}

Best Regards,
Aleksey.

Hi Aleksey,

Thanks for your inquiry. The second parameter of FontSettings.AddFontSubstitutes method is list of alternative font names to be used if original font is not presented in system. Please make sure that "FreeMono" is installed on the system and "Arial" is not. You can check the shared scenario by using FontSettings.SetFontsFolder method. Put the fonts "FreeMono" and "DejaVu Serif" in a folder and use the folder's name in FontSettings.SetFontsFolder.

If you still face problem, please share the font "FreeMono" here for further testing. We will investigate the issue on our side and provide you more information.

Hi, Tahir,



the method FontSettings.SetFontsFolder() does not lead to success for the issue. And as I wrote above, without this method call a font for bullets is really substituted through FontSettings.AddFontSubstitutes.



The Arial font is really not installed and FreeMono does, that can be shown by the following code run on my Linux machine (Ubuntu 15.10 + mono 4.2.3.4):

C#:

public class FontInfoTrace

{

static public void Main ()

{

traceIsFontInstalled(“Arial”);

traceIsFontInstalled(“FreeMono”);

}



static void traceIsFontInstalled(string fontName)

{

var installedFontCollection = new System.Drawing.Text.InstalledFontCollection();

var fontFamilies = installedFontCollection.Families;



bool found = false;

foreach (var fontFamily in fontFamilies)

{

if (fontName == fontFamily.Name)

{

found = true;

break;

}

}



Console.WriteLine(“The font ‘{0}’ {1}.”, fontName, found ? “is installed” : “does not installed”);

}

}

Output for the code run:

>> The font ‘Arial’ does not installed.

>> The font ‘FreeMono’ is installed.



I believe the problem does not concern a concrete target font (FreeMono) so you can use any other installed font to reproduce the bug.



Best Regards,

Aleksey.

Hi Aleksey,

Thanks for your inquiry. Please make sure that you have installed all fonts on the machine where you're converting documents to Pdf. Please refer to the following articles:

How to Install True Type Fonts on Linux
How to Receive Notification of Missing Fonts and Font Substitution during Rendering

We have tested the scenario using following code example and have not found the shared issue. Please check the attached image for detail.

Aspose.Words.Fonts.FontSettings.DefaultInstance.AddFontSubstitutes("Arial", new string[] { "Calibri" });

Aspose.Words.Fonts.FontSettings.DefaultInstance.AddFontSubstitutes("Symbol", new string[] { "DejaVu Serif" });

Document doc = new Document("/home/tahir/ArialTextAndSymbolBullet.docx");

FontSettings.DefaultInstance.SetFontsFolder("/home/tahir/Fonts from win/Fonts/", false);

doc.WarningCallback = new HandleDocumentWarnings();

doc.Save("/home/tahir/Out v16.11.0.pdf");

public class HandleDocumentWarnings : IWarningCallback

{

public void Warning(WarningInfo info)

{

// We are only interested in fonts being substituted.

if (info.WarningType == WarningType.FontSubstitution)

{

Console.WriteLine("Font substitution: " + info.Description);

}

}

}

Hi, Tahir,



thanks for your reply.

I have installed Calibri font and got exactly the same result as you done when using FontSettings.DefaultInstance.SetFontsFolder with a folder containing Calibri and “DejaVu Serif” but not containing Garuda.

But if you put the fonts Calibri and Garuda (and “DejaVu Serif”) under one super folder (named fontFolder below) (the fonts can be put in different subfolders of the folder) and call SetFontsFolder(fontFolder, true), you will get the wrong behavior: the substitution method does not work properly for Arial font but works for System font (note that the latter one is used for bullets):

>> Font substitution: Font substitutes: ‘Arial’ replaced with ‘Garuda’.

>> Font substitution: Font substitutes: ‘Symbol’ replaced with ‘DejaVu Serif’.

Note that SetFontsFolder has not to be used for the case when the super folder you use (fontFolder) is the system font folder (/usr/share/fonts - for Ubuntu).



So it seems that the real (wrong) substitution mechanism violates the description http://www.aspose.com/docs/display/wordsnet/True+Type+Fonts#TrueTypeFonts-FontAvailabilityandSubstitution

since it searches pp.3-4 of the section “Font Availability and Substitution” before rules provided by AddFontSubstitutes but should do it after the rules if them fail to find fonts. Note that all is ok with bullet fonts (may be accidentally?).



Best Regards,

Aleksey.

Hi Aleksey,

Thanks for sharing the detail. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-14511. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi Aleksey,

Thanks for your patience. It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. So, we have closed this issue (WORDSNET-14511) as 'Not a Bug'.

You are facing the expected behavior of Aspose.Words. FontSettings.AddFontSubstitutes adds provided fonts to the end of existing list of substitutes. In your case the default list of substitutes for "Arial" font is {"Garuda", "FreeSans", "Liberation Sans", "DejaVu Sans"}. So calling AddFontSubstitutes("Arial", new string[] { "Calibri" }); simply adds "Calibri" font to the end of this list and "Garuda" font still has a priority.

We suggest you please use FontSettings.SetFontSubstitutes method instead of FontSettings.AddFontSubstitutes.

Hi Tahir,



thanks for the clarifications - with FontSettings.SetFontSubstitutes the task is performed properly.



Best Regards,

Aleksey.

Hi Aleksey,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.