com.aspose.words
Class FontInfoSubstitutionRule

java.lang.Object
  extended by FontSubstitutionRule
      extended by com.aspose.words.FontInfoSubstitutionRule

public class FontInfoSubstitutionRule 
extends FontSubstitutionRule

Font info substitution rule. According to this rule Aspose.Words evaluates all the related fields in FontInfo (Panose, Sig etc) for the missing font and finds the closest match among the available font sources. If FontInfo is not available for the missing font then nothing will be done.

Example:

Shows how to set the property for finding the closest match font among the available font sources instead missing font.
@Test
public void enableFontSubstitution() throws Exception {
    Document doc = new Document(getMyDir() + "Missing font.docx");

    // Assign a custom warning callback
    HandleDocumentSubstitutionWarnings substitutionWarningHandler = new HandleDocumentSubstitutionWarnings();
    doc.setWarningCallback(substitutionWarningHandler);

    // Set a default font name and enable font substitution
    FontSettings fontSettings = new FontSettings();
    fontSettings.getSubstitutionSettings().getDefaultFontSubstitution().setDefaultFontName("Arial");
    fontSettings.getSubstitutionSettings().getFontInfoSubstitution().setEnabled(true);

    // When saving the document with the missing font, we should get a warning
    doc.setFontSettings(fontSettings);
    doc.save(getArtifactsDir() + "Font.EnableFontSubstitution.pdf");

    // List all warnings using an enumerator
    Iterator<WarningInfo> warnings = substitutionWarningHandler.FontWarnings.iterator();
    try /*JAVA: was using*/ {
        while (warnings.hasNext())
            System.out.println(warnings.next().getDescription());
    } finally {
        if (warnings != null) warnings.remove();
    }

    // Warnings are stored in this format
    Assert.assertEquals(WarningSource.LAYOUT, substitutionWarningHandler.FontWarnings.get(0).getSource());
    Assert.assertEquals("Font '28 Days Later' has not been found. Using 'Calibri' font instead. Reason: alternative name from document.",
            substitutionWarningHandler.FontWarnings.get(0).getDescription());

    // The warning info collection can also be cleared like this
    substitutionWarningHandler.FontWarnings.clear();

    Assert.assertNull(substitutionWarningHandler.FontWarnings);
}

public static class HandleDocumentSubstitutionWarnings implements IWarningCallback {
    /// <summary>
    /// Our callback only needs to implement the "Warning" method. This method is called whenever there is a
    /// potential issue during document processing. The callback can be set to listen for warnings generated during document
    /// load and/or document save.
    /// </summary>
    public void warning(WarningInfo info) {
        // We are only interested in fonts being substituted
        if (info.getWarningType() == WarningType.FONT_SUBSTITUTION)
            FontWarnings.warning(info);
    }

    public WarningInfoCollection FontWarnings = new WarningInfoCollection();
}

Property Getters/Setters Summary
booleangetEnabled()→ inherited from FontSubstitutionRule
voidsetEnabled(boolean value)
           Specifies whether the rule is enabled or not.
 

Property Getters/Setters Detail

getEnabled/setEnabled

→ inherited from FontSubstitutionRule
public boolean getEnabled() / public void setEnabled(boolean value)
Specifies whether the rule is enabled or not.

Example:

Shows OS-dependent font config substitution.
// Create a new FontSettings object and get its font config substitution rule
FontSettings fontSettings = new FontSettings();
FontConfigSubstitutionRule configSubstitution = fontSettings.getSubstitutionSettings().getFontConfigSubstitution();

// The FontConfigSubstitutionRule object works differently on Windows/non-Windows platforms
final String OS = System.getProperty("os.name").toLowerCase();

// On Windows, it is unavailable
if (OS == "win") {
    Assert.assertFalse(configSubstitution.getEnabled());
    Assert.assertFalse(configSubstitution.isFontConfigAvailable());
}

// On Linux/Mac, we will have access and will be able to perform operations
if (OS == "nix") {
    Assert.assertTrue(configSubstitution.getEnabled());
    Assert.assertTrue(configSubstitution.isFontConfigAvailable());

    configSubstitution.resetCache();
}

Example:

Shows how to access a document's system font source and set font substitutes.
Document doc = new Document();

// Create a font settings object for our document
doc.setFontSettings(new FontSettings());

// By default we always start with a system font source
Assert.assertEquals(doc.getFontSettings().getFontsSources().length, 1);

SystemFontSource systemFontSource = (SystemFontSource) doc.getFontSettings().getFontsSources()[0];
Assert.assertEquals(systemFontSource.getType(), FontSourceType.SYSTEM_FONTS);
Assert.assertEquals(systemFontSource.getPriority(), 0);

if (System.getProperty("os.name").startsWith("Windows")) {
    Assert.assertEquals(SystemFontSource.getSystemFontFolders(), new String[]{"C:\\WINDOWS\\Fonts"});
}

for (String systemFontFolder : SystemFontSource.getSystemFontFolders()) {
    System.out.println(systemFontFolder);
}

// Set a font that exists in the windows fonts directory as a substitute for one that doesn't
doc.getFontSettings().getSubstitutionSettings().getFontInfoSubstitution().setEnabled(true);
doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().addSubstitutes("Kreon-Regular", new String[]{"Calibri"});

long substituteSize = StreamSupport.stream(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular").spliterator(), false).count();
Assert.assertEquals(substituteSize, 1);
Assert.assertTrue(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular").toString().contains("Calibri"));

// Alternatively, we could add a folder font source in which the corresponding folder contains the font
FolderFontSource folderFontSource = new FolderFontSource(getFontsDir(), false);
doc.getFontSettings().setFontsSources(new FontSourceBase[]{systemFontSource, folderFontSource});
Assert.assertEquals(2, doc.getFontSettings().getFontsSources().length);

// Resetting the font sources still leaves us with the system font source as well as our substitutes
doc.getFontSettings().resetFontSources();

Assert.assertEquals(1, doc.getFontSettings().getFontsSources().length);
Assert.assertEquals(FontSourceType.SYSTEM_FONTS, doc.getFontSettings().getFontsSources()[0].getType());
Assert.assertEquals(1, IterableUtils.size(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular")));

See Also:
          Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
          Aspose.Words Support Forum - our preferred method of support.