com.aspose.words

Class FontSubstitutionRule

  • java.lang.Object
    • com.aspose.words.FontSubstitutionRule
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()
void
setEnabled(booleanvalue)
           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(getMyDir() + "MyFonts", false);
        doc.getFontSettings().setFontsSources(new FontSourceBase[]{systemFontSource, folderFontSource});
        Assert.assertEquals(doc.getFontSettings().getFontsSources().length, 2);
        
        // Resetting the font sources still leaves us with the system font source as well as our substitutes
        doc.getFontSettings().resetFontSources();
        
        Assert.assertEquals(doc.getFontSettings().getFontsSources().length, 1);
        Assert.assertEquals(doc.getFontSettings().getFontsSources()[0].getType(), FontSourceType.SYSTEM_FONTS);
        
        substituteSize = StreamSupport.stream(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular").spliterator(), false).count();
        Assert.assertEquals(substituteSize, 1);