Incorrect render of text with some fonts when DefaultFont is used

Hello,

The subject is quite similar to this topic I created few months ago, but with a different outcome: Incorrect render of text with font from Aptos family

My customer is using the font Montserrat in his workbook.

When the range is rendered to an EMF file using Aspose.Cells, the Montserrat font is not used for all cells.

You can see the result of the image created in this screenshot, after copying the EMF to a PowerPoint slide:
image-copied-in-PPT.png (32,0 Ko)
The image shows that on the 3 first rows, the Arial font is used for the numbers (while the texts on the left is using the Montserrat font), and starting from the middle of the 4th row, the Montserrat font is used.

There is my code to generate the image:

public static void ExportToImage(string sourceDocument, string worksheetName, string destinationFile, string rangeAddress)
{
    Console.WriteLine(@"Montserrat is available: " + FontConfigs.IsFontAvailable("Montserrat")); // returns true
    string winFontCacheFolder =
        System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "FontCache");
    FontConfigs.SetFontFolder(winFontCacheFolder, true);
    Console.WriteLine(@"Montserrat is available: " + FontConfigs.IsFontAvailable("Montserrat")); // returns true

    Workbook asposeWorkbook = new Workbook(sourceDocument);

    var asposeSheet = asposeWorkbook.Worksheets[worksheetName];
    var range = asposeSheet.Cells.CreateRange(rangeAddress);
    ImageOrPrintOptions options = new ImageOrPrintOptions()
    {
        ImageType = ImageType.OfficeCompatibleEmf,
        OnlyArea = true,
        Transparent = true,
        DefaultFont = "Arial",
        HorizontalResolution = 600,
        VerticalResolution = 600,
    };

    asposeSheet.PageSetup.PrintArea = range.RefersTo;
    asposeSheet.PageSetup.CustomPaperSize(MeasureUnitsHelper.PointToInch(range.Width), MeasureUnitsHelper.PointToInch(range.Height));
    var renderer = new SheetRender(asposeSheet, options);

    renderer.ToImage(0, destinationFile);
}

What is interesting here is that if I comment the line that sets the default font to Arial DefaultFont = "Arial", from ImageOrPrintOptions, then the image is correctly rendered with the Montserrat font everywhere.

The following zip file contains the source workbook you can use to reproduce the issue, alongside with two EMF files, one with DefaultFont being set (image-with-arial.emf), and the second with the this property not being set (image-without-arial.emf).
montserrat.zip (15,2 Ko)

I am using the latest version of Aspose.Cells at the moment: 26.1.0.

@Gradlon

You are observing an issue where setting DefaultFont to “Arial” in ImageOrPrintOptions causes some text elements to render with Arial instead of the intended Montserrat font when exporting a worksheet to EMF format .

Here’s a summary of the situation and suggested actions:

  1. Problem: When DefaultFont = "Arial" is set in ImageOrPrintOptions, the Montserrat font is not consistently applied to all text elements in the rendered EMF file. Some parts (like numbers in the first few rows) appear in Arial instead .
  2. Workaround: Removing the DefaultFont property entirely allows the Montserrat font to be applied correctly across all text elements .
  3. Root Cause Hypothesis: This behavior suggests a potential issue in Aspose.Cells’ font fallback mechanism when DefaultFont is explicitly specified, particularly affecting certain font families like Montserrat .

Recommended Actions:

  • Confirm the Issue: Try reproducing the problem with the provided sample workbook and code .
  • Check for Updates: Ensure you are using the latest version of Aspose.Cells, as font rendering issues are often addressed in newer releases.
  • Report the Bug: If the issue persists, consider logging it internally with Aspose support, providing the sample files and reproduction steps.

This issue appears to be a bug related to font handling when DefaultFont is specified during EMF rendering . A support engineer will follow up in this thread if needed.

@Gradlon,

The behavior you are seeing is likely due to the order of priority in font substitution when DefaultFont is explicitly set in ImageOrPrintOptions. It seems, when you specify DefaultFont = "Arial", the API may prioritize this setting for certain character types (like numbers) if there is any ambiguity in the font’s availability or compatibility for those specific glyphs.

Anyways, we will evaluate your issue precisely and we need your font “Montserrat”(*.ttf files). Please zip and attach the font file(s), we will look into your issue soon.

@amjad.sahi,

Thanks for the response. Here is the zip file with the Montserrat ttf files:
Montserrat.zip (334,6 Ko)

@Gradlon,

Thanks for the font (Montserrat) files.

I have installed the font and then tried your scenario/case using your template Excel file and following sample code (I also generated PDF file after rendering the EMF image) using Aspose.Cells for .NET v26.1 (please try it if you are not already using it). It works fine and the output EMF file and PDF file are fine tuned.
e.g.,
Sample code:

Workbook asposeWorkbook = new Workbook("e:\\test2\\Workbook-with-montserrat-font.xlsx");

var asposeSheet = asposeWorkbook.Worksheets["Sheet1"];
var range = asposeSheet.Cells.CreateRange("A1:E6");
ImageOrPrintOptions options = new ImageOrPrintOptions()
{
        ImageType = ImageType.OfficeCompatibleEmf,
        OnlyArea = true,
        Transparent = true,
        DefaultFont = "Arial",
        HorizontalResolution = 600,
        VerticalResolution = 600,
};

asposeSheet.PageSetup.PrintArea = range.RefersTo;
var renderer = new SheetRender(asposeSheet, options);
renderer.ToImage(0, "e:\\test2\\out_image1.emf");
asposeWorkbook.Save("e:\\test2\\out_pdf.pdf");

Please find attached the output files in the zipped archive here.
files1.zip (12.4 KB)

Please check the files and let us know if you find any issue with them.

PDF file is good, but not the emf file.
This is the result of your emf file, when copied to PPT:
image.png (51,4 Ko)
The font used there is not Montserrat, but Arial. If you compare with your pdf, you’ll see that the outcome is quite different.

@Gradlon ,

I checked the records of the Emf image, the font defined in Emf is the right one “Montserrat”. Emf image is vector, it relies on the installed fonts to render the text. Please make sure that “Montserrat” font is installed on where your PPT with the embedded Emf image is opened.

Pdf file is generated with “Montserrat” font(subset) embedded, so the text in pdf is OK no matter the font is installed or not.

@Gradlon ,

The origin issue is a diffrent one on your customer’s environment, I can see “Montserrat” font is OK for the first column in your screenshot “image-copied-in-PPT.png”.
For the Emf image “image-with-arial.emf”, the font is not right for some cells(e.g. Cell B1). We will investigate it further.

@Gradlon ,

Take Cell B1 for example, in the Emf “image-without-arial.emf” and “image-with-arial.emf”, the thousands separator char is missed comparing @amjad.sahi generated Emf image.
I guess the “Montserrat” font can’t show the thousands separator char on your customer’s environment.
What is the locale on on your customer’s environment?
Please run the following code on your customer’s environment, and share us the printed info.

Workbook asposeWorkbook = new Workbook(sourceDocument);

var asposeSheet = asposeWorkbook.Worksheets["Sheet1"];
string text = asposeSheet.Cells["B1"].DisplayStringValue;
Console.WriteLine($"Cell B1 text: {text}, length: {text.Length}, thousands separator char in index 2: {(int)text[2]}");

Here is the result:

Cell B1 text: (3?124), length: 7, thousands separator char in index 2: 8239

@Gradlon

The font “Montserrat” can’t display the char “\u202F”(8239 in Decimal), but the font “Arial” can. After setting “Arial” as DefaultFont, Aspose.Cells will try “Arial” font with higher priority. Please check the document of Selection of Fonts in Aspose.Cells. That is the cause of different Emf output after setting “Arial” as DefaultFont.

Please try IsFontSubstitutionCharGranularity in ImageOrPrintOptions:

ImageOrPrintOptions options = new ImageOrPrintOptions()
{
    ImageType = ImageType.OfficeCompatibleEmf,
    OnlyArea = true,
    Transparent = true,
    DefaultFont = "Arial",
    IsFontSubstitutionCharGranularity = true,
    HorizontalResolution = 600,
    VerticalResolution = 600,
};

OK, thanks for the explanation.

Indeed, with IsFontSubstitutionCharGranularity = true, the resulting image is good.

Is there any drawback for using this property? For my curiosity, is there a reason this is not the default behavior?

Anyway, thanks a lot for your really good support.

@Gradlon,

Thanks for your confirmation and good to know that with setting IsFontSubstitutionCharGranularity = true your image is fine.

There is no drawback to use this Boolean attribute. Aspose.Cells provides the IsFontSubstitutionCharGranularity property, which you may set to true so that only the font of the specific character that is not displayable is changed to a displayable font, while the rest of the word/value or sentence remains in the original font. See the document with example for your reference: Change the Font on just the specific Unicode characters while saving to PDF|Documentation

Hope, this helps a bit.

@amjad.sahi ,

Thanks for the explanations and the really good support provided.
You may close the ticket.

@Gradlon,

You are welcome. Should you have further queries or comments, please feel free to contact us.