com.aspose.words

Class FontFallbackSettings

  • java.lang.Object
    • com.aspose.words.FontFallbackSettings
public class FontFallbackSettings 
extends java.lang.Object

Specifies font fallback mechanism settings.
By default fallback settings are initialized with predefined settings which mimics the Microsoft Word fallback.

Example:

Shows how to distribute fallback fonts across Unicode character code ranges.
Document doc = new Document();

FontSettings fontSettings = new FontSettings();
doc.setFontSettings(fontSettings);
FontFallbackSettings fontFallbackSettings = fontSettings.getFallbackSettings();

// Configure our font settings to source fonts only from the "MyFonts" folder.
FolderFontSource folderFontSource = new FolderFontSource(getFontsDir(), false);
fontSettings.setFontsSources(new FontSourceBase[]{folderFontSource});

// Calling the "BuildAutomatic" method will generate a fallback scheme that
// distributes accessible fonts across as many Unicode character codes as possible.
// In our case, it only has access to the handful of fonts inside the "MyFonts" folder.
fontFallbackSettings.buildAutomatic();
fontFallbackSettings.save(getArtifactsDir() + "FontSettings.FallbackSettingsCustom.BuildAutomatic.xml");

// We can also load a custom substitution scheme from a file like this.
// This scheme applies the "AllegroOpen" font across the "0000-00ff" Unicode blocks, the "AllegroOpen" font across "0100-024f",
// and the "M+ 2m" font in all other ranges that other fonts in the scheme do not cover.
fontFallbackSettings.load(getMyDir() + "Custom font fallback settings.xml");

// Create a document builder and set its font to one that does not exist in any of our sources.
// Our font settings will invoke the fallback scheme for characters that we type using the unavailable font.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Missing Font");

// Use the builder to print every Unicode character from 0x0021 to 0x052F,
// with descriptive lines dividing Unicode blocks we defined in our custom font fallback scheme.
for (int i = 0x0021; i < 0x0530; i++) {
    switch (i) {
        case 0x0021:
            builder.writeln("\n\n0x0021 - 0x00FF: \nBasic Latin/Latin-1 Supplement Unicode blocks in \"AllegroOpen\" font:");
            break;
        case 0x0100:
            builder.writeln("\n\n0x0100 - 0x024F: \nLatin Extended A/B blocks, mostly in \"AllegroOpen\" font:");
            break;
        case 0x0250:
            builder.writeln("\n\n0x0250 - 0x052F: \nIPA/Greek/Cyrillic blocks in \"M+ 2m\" font:");
            break;
    }

    builder.write(MessageFormat.format("{0}", (char) i));
}

doc.save(getArtifactsDir() + "FontSettings.FallbackSettingsCustom.pdf");

Method Summary
voidbuildAutomatic()
Automatically builds the fallback settings by scanning available fonts.
voidload(java.io.InputStream stream)
Loads fallback settings from XML stream.
voidload(java.lang.String fileName)
Loads font fallback settings from XML file.
voidloadMsOfficeFallbackSettings()
Loads predefined fallback settings which mimics the Microsoft Word fallback and uses Microsoft office fonts.
voidloadNotoFallbackSettings()
Loads predefined fallback settings which uses Google Noto fonts.
voidsave(java.io.OutputStream stream)
Saves the current fallback settings to stream.
voidsave(java.lang.String fileName)
Saves the current fallback settings to file.
 

    • Method Detail

      • buildAutomatic

        public void buildAutomatic()
        Automatically builds the fallback settings by scanning available fonts. This method may produce non-optimal fallback settings. Fonts are checked by Unicode Character Range fields and not by the actual glyphs presence. Also Unicode ranges are checked individually and several ranges related to single language/script may use different fallback fonts.

        Example:

        Shows how to distribute fallback fonts across Unicode character code ranges.
        Document doc = new Document();
        
        FontSettings fontSettings = new FontSettings();
        doc.setFontSettings(fontSettings);
        FontFallbackSettings fontFallbackSettings = fontSettings.getFallbackSettings();
        
        // Configure our font settings to source fonts only from the "MyFonts" folder.
        FolderFontSource folderFontSource = new FolderFontSource(getFontsDir(), false);
        fontSettings.setFontsSources(new FontSourceBase[]{folderFontSource});
        
        // Calling the "BuildAutomatic" method will generate a fallback scheme that
        // distributes accessible fonts across as many Unicode character codes as possible.
        // In our case, it only has access to the handful of fonts inside the "MyFonts" folder.
        fontFallbackSettings.buildAutomatic();
        fontFallbackSettings.save(getArtifactsDir() + "FontSettings.FallbackSettingsCustom.BuildAutomatic.xml");
        
        // We can also load a custom substitution scheme from a file like this.
        // This scheme applies the "AllegroOpen" font across the "0000-00ff" Unicode blocks, the "AllegroOpen" font across "0100-024f",
        // and the "M+ 2m" font in all other ranges that other fonts in the scheme do not cover.
        fontFallbackSettings.load(getMyDir() + "Custom font fallback settings.xml");
        
        // Create a document builder and set its font to one that does not exist in any of our sources.
        // Our font settings will invoke the fallback scheme for characters that we type using the unavailable font.
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.getFont().setName("Missing Font");
        
        // Use the builder to print every Unicode character from 0x0021 to 0x052F,
        // with descriptive lines dividing Unicode blocks we defined in our custom font fallback scheme.
        for (int i = 0x0021; i < 0x0530; i++) {
            switch (i) {
                case 0x0021:
                    builder.writeln("\n\n0x0021 - 0x00FF: \nBasic Latin/Latin-1 Supplement Unicode blocks in \"AllegroOpen\" font:");
                    break;
                case 0x0100:
                    builder.writeln("\n\n0x0100 - 0x024F: \nLatin Extended A/B blocks, mostly in \"AllegroOpen\" font:");
                    break;
                case 0x0250:
                    builder.writeln("\n\n0x0250 - 0x052F: \nIPA/Greek/Cyrillic blocks in \"M+ 2m\" font:");
                    break;
            }
        
            builder.write(MessageFormat.format("{0}", (char) i));
        }
        
        doc.save(getArtifactsDir() + "FontSettings.FallbackSettingsCustom.pdf");
      • load

        public void load(java.io.InputStream stream)
                 throws java.lang.Exception
        Loads fallback settings from XML stream.
        Parameters:
        stream - Input stream.

        Example:

        Shows how to load and save font fallback settings to/from a stream.
        Document doc = new Document(getMyDir() + "Rendering.docx");
        
        // Load an XML document that defines a set of font fallback settings.
        try (FileInputStream fontFallbackRulesStream = new FileInputStream(getMyDir() + "Font fallback rules.xml")) {
            FontSettings fontSettings = new FontSettings();
            fontSettings.getFallbackSettings().load(fontFallbackRulesStream);
        
            doc.setFontSettings(fontSettings);
        }
        
        doc.save(getArtifactsDir() + "FontSettings.LoadFontFallbackSettingsFromStream.pdf");
        
        // Use a stream to save our document's current font fallback settings as an XML document.
        try (FileOutputStream fontFallbackSettingsStream = new FileOutputStream(getArtifactsDir() + "FallbackSettings.xml")) {
            doc.getFontSettings().getFallbackSettings().save(fontFallbackSettingsStream);
        }
      • load

        public void load(java.lang.String fileName)
                 throws java.lang.Exception
        Loads font fallback settings from XML file.
        Parameters:
        fileName - Input file name.

        Example:

        Shows how to load and save font fallback settings to/from an XML document in the local file system.
        Document doc = new Document(getMyDir() + "Rendering.docx");
        
        // Load an XML document that defines a set of font fallback settings.
        FontSettings fontSettings = new FontSettings();
        fontSettings.getFallbackSettings().load(getMyDir() + "Font fallback rules.xml");
        
        doc.setFontSettings(fontSettings);
        doc.save(getArtifactsDir() + "FontSettings.LoadFontFallbackSettingsFromFile.pdf");
        
        // Save our document's current font fallback settings as an XML document.
        doc.getFontSettings().getFallbackSettings().save(getArtifactsDir() + "FallbackSettings.xml");
      • loadMsOfficeFallbackSettings

        public void loadMsOfficeFallbackSettings()
        Loads predefined fallback settings which mimics the Microsoft Word fallback and uses Microsoft office fonts.

        Example:

        Shows how to load pre-defined fallback font settings.
        Document doc = new Document();
        
        FontSettings fontSettings = new FontSettings();
        doc.setFontSettings(fontSettings);
        FontFallbackSettings fontFallbackSettings = fontSettings.getFallbackSettings();
        
        // Save the default fallback font scheme to an XML document.
        // For example, one of the elements has a value of "0C00-0C7F" for Range and a corresponding "Vani" value for FallbackFonts.
        // This means that if the font some text is using does not have symbols for the 0x0C00-0x0C7F Unicode block,
        // the fallback scheme will use symbols from the "Vani" font substitute.
        fontFallbackSettings.save(getArtifactsDir() + "FontSettings.FallbackSettings.Default.xml");
        
        // Below are two pre-defined font fallback schemes we can choose from.
        // 1 -  Use the default Microsoft Office scheme, which is the same one as the default:
        fontFallbackSettings.loadMsOfficeFallbackSettings();
        fontFallbackSettings.save(getArtifactsDir() + "FontSettings.FallbackSettings.LoadMsOfficeFallbackSettings.xml");
        
        // 2 -  Use the scheme built from Google Noto fonts:
        fontFallbackSettings.loadNotoFallbackSettings();
        fontFallbackSettings.save(getArtifactsDir() + "FontSettings.FallbackSettings.LoadNotoFallbackSettings.xml");
      • loadNotoFallbackSettings

        public void loadNotoFallbackSettings()
                                     throws java.lang.Exception
        Loads predefined fallback settings which uses Google Noto fonts.

        Example:

        Shows how to load pre-defined fallback font settings.
        Document doc = new Document();
        
        FontSettings fontSettings = new FontSettings();
        doc.setFontSettings(fontSettings);
        FontFallbackSettings fontFallbackSettings = fontSettings.getFallbackSettings();
        
        // Save the default fallback font scheme to an XML document.
        // For example, one of the elements has a value of "0C00-0C7F" for Range and a corresponding "Vani" value for FallbackFonts.
        // This means that if the font some text is using does not have symbols for the 0x0C00-0x0C7F Unicode block,
        // the fallback scheme will use symbols from the "Vani" font substitute.
        fontFallbackSettings.save(getArtifactsDir() + "FontSettings.FallbackSettings.Default.xml");
        
        // Below are two pre-defined font fallback schemes we can choose from.
        // 1 -  Use the default Microsoft Office scheme, which is the same one as the default:
        fontFallbackSettings.loadMsOfficeFallbackSettings();
        fontFallbackSettings.save(getArtifactsDir() + "FontSettings.FallbackSettings.LoadMsOfficeFallbackSettings.xml");
        
        // 2 -  Use the scheme built from Google Noto fonts:
        fontFallbackSettings.loadNotoFallbackSettings();
        fontFallbackSettings.save(getArtifactsDir() + "FontSettings.FallbackSettings.LoadNotoFallbackSettings.xml");

        Example:

        Shows how to add predefined font fallback settings for Google Noto fonts.
        FontSettings fontSettings = new FontSettings();
        
        // These are free fonts licensed under the SIL Open Font License.
        // We can download the fonts here:
        // https://www.google.com/get/noto/#sans-lgc
        fontSettings.setFontsFolder(getFontsDir() + "Noto", false);
        
        // Note that the predefined settings only use Sans-style Noto fonts with regular weight. 
        // Some of the Noto fonts use advanced typography features.
        // Fonts featuring advanced typography may not be rendered correctly as Aspose.Words currently do not support them.
        fontSettings.getFallbackSettings().loadNotoFallbackSettings();
        fontSettings.getSubstitutionSettings().getFontInfoSubstitution().setEnabled(false);
        fontSettings.getSubstitutionSettings().getDefaultFontSubstitution().setDefaultFontName("Noto Sans");
        
        Document doc = new Document();
        doc.setFontSettings(fontSettings);
      • save

        public void save(java.io.OutputStream stream)
                 throws java.lang.Exception
        Saves the current fallback settings to stream.
        Parameters:
        outputStream - Output stream.

        Example:

        Shows how to load and save font fallback settings to/from a stream.
        Document doc = new Document(getMyDir() + "Rendering.docx");
        
        // Load an XML document that defines a set of font fallback settings.
        try (FileInputStream fontFallbackRulesStream = new FileInputStream(getMyDir() + "Font fallback rules.xml")) {
            FontSettings fontSettings = new FontSettings();
            fontSettings.getFallbackSettings().load(fontFallbackRulesStream);
        
            doc.setFontSettings(fontSettings);
        }
        
        doc.save(getArtifactsDir() + "FontSettings.LoadFontFallbackSettingsFromStream.pdf");
        
        // Use a stream to save our document's current font fallback settings as an XML document.
        try (FileOutputStream fontFallbackSettingsStream = new FileOutputStream(getArtifactsDir() + "FallbackSettings.xml")) {
            doc.getFontSettings().getFallbackSettings().save(fontFallbackSettingsStream);
        }
      • save

        public void save(java.lang.String fileName)
                 throws java.lang.Exception
        Saves the current fallback settings to file.
        Parameters:
        fileName - Output file name.

        Example:

        Shows how to load and save font fallback settings to/from an XML document in the local file system.
        Document doc = new Document(getMyDir() + "Rendering.docx");
        
        // Load an XML document that defines a set of font fallback settings.
        FontSettings fontSettings = new FontSettings();
        fontSettings.getFallbackSettings().load(getMyDir() + "Font fallback rules.xml");
        
        doc.setFontSettings(fontSettings);
        doc.save(getArtifactsDir() + "FontSettings.LoadFontFallbackSettingsFromFile.pdf");
        
        // Save our document's current font fallback settings as an XML document.
        doc.getFontSettings().getFallbackSettings().save(getArtifactsDir() + "FallbackSettings.xml");