com.aspose.words
Class FontSubstitutionRule

java.lang.Object
    extended by com.aspose.words.FontSubstitutionRule
Direct Known Subclasses:
DefaultFontSubstitutionRule, FontConfigSubstitutionRule, FontInfoSubstitutionRule, TableSubstitutionRule

public abstract class FontSubstitutionRule 
extends java.lang.Object

This is an abstract base class for the font substitution rule.

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();
}

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

Property Getters/Setters Detail

getEnabled/setEnabled

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.