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

// Create a FontSettings object for our document and get its FallbackSettings attribute
FontSettings fontSettings = new FontSettings();
doc.setFontSettings(fontSettings);
FontFallbackSettings fontFallbackSettings = fontSettings.getFallbackSettings();

// Set our fonts to be sourced exclusively from the "MyFonts" folder
FolderFontSource folderFontSource = new FolderFontSource(getMyDir() + "\\MyFonts", false);
fontSettings.setFontsSources(new FontSourceBase[]{folderFontSource});

// Calling BuildAutomatic() 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() + "Font.FontFallbackSettings.BuildAutomatic.xml");

// We can also load a custom substitution scheme from a file like this
// This scheme applies the "Arvo" font across the "0000-00ff" unicode blocks, the "Squarish Sans CT" font across "0100-024f",
// and the "M+ 2m" font in every place that none of the other fonts cover
fontFallbackSettings.load(getMyDir() + "Font.FallbackSettings.Custom.xml");

// Create a document builder and set its font to one that doesn't exist in any of our sources
// In doing that we will rely completely on our font fallback scheme to render text
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Missing Font");

// Type out 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 \"Arvo\" font:");
            break;
        case 0x0100:
            builder.writeln("\n\n0x0100 - 0x024F: \nLatin Extended A/B blocks, mostly in \"Squarish Sans CT\" font:");
            break;
        case 0x0250:
            builder.writeln("\n\n0x0250 - 0x052F: \nIPA/Greek/Cyrillic blocks in \"M+ 2m\" font:");
            break;
    }

    builder.write(Character.toString((char) i));
}

doc.save(getArtifactsDir() + "Font.FallbackSettings.Custom.pdf");

Method Summary
voidbuildAutomatic()
Automatically builds the fallback settings by scanning available fonts.
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.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();
        
        // Create a FontSettings object for our document and get its FallbackSettings attribute
        FontSettings fontSettings = new FontSettings();
        doc.setFontSettings(fontSettings);
        FontFallbackSettings fontFallbackSettings = fontSettings.getFallbackSettings();
        
        // Set our fonts to be sourced exclusively from the "MyFonts" folder
        FolderFontSource folderFontSource = new FolderFontSource(getMyDir() + "\\MyFonts", false);
        fontSettings.setFontsSources(new FontSourceBase[]{folderFontSource});
        
        // Calling BuildAutomatic() 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() + "Font.FontFallbackSettings.BuildAutomatic.xml");
        
        // We can also load a custom substitution scheme from a file like this
        // This scheme applies the "Arvo" font across the "0000-00ff" unicode blocks, the "Squarish Sans CT" font across "0100-024f",
        // and the "M+ 2m" font in every place that none of the other fonts cover
        fontFallbackSettings.load(getMyDir() + "Font.FallbackSettings.Custom.xml");
        
        // Create a document builder and set its font to one that doesn't exist in any of our sources
        // In doing that we will rely completely on our font fallback scheme to render text
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.getFont().setName("Missing Font");
        
        // Type out 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 \"Arvo\" font:");
                    break;
                case 0x0100:
                    builder.writeln("\n\n0x0100 - 0x024F: \nLatin Extended A/B blocks, mostly in \"Squarish Sans CT\" font:");
                    break;
                case 0x0250:
                    builder.writeln("\n\n0x0250 - 0x052F: \nIPA/Greek/Cyrillic blocks in \"M+ 2m\" font:");
                    break;
            }
        
            builder.write(Character.toString((char) i));
        }
        
        doc.save(getArtifactsDir() + "Font.FallbackSettings.Custom.pdf");
      • 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 from file.
        Document doc = new Document(getMyDir() + "Rendering.doc");
        
        // By default fallback settings are initialized with predefined settings which mimics the Microsoft Word fallback.
        FontSettings fontSettings = new FontSettings();
        fontSettings.getFallbackSettings().load(getMyDir() + "Fallback.xml");
        
        doc.setFontSettings(fontSettings);
        doc.save(getArtifactsDir() + "LoadFontFallbackSettingsFromFile.pdf");
        
        // Saves font fallback setting by string
        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();
        
        // Create a FontSettings object for our document and get its FallbackSettings attribute
        FontSettings fontSettings = new FontSettings();
        doc.setFontSettings(fontSettings);
        FontFallbackSettings fontFallbackSettings = fontSettings.getFallbackSettings();
        
        // Save the default fallback font scheme in 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 we are using does not have symbols for the 0x0C00-0x0C7F unicode block,
        // the symbols from the "Vani" font will be used as a substitute
        fontFallbackSettings.save(getArtifactsDir() + "Font.FallbackSettings.Default.xml");
        
        // There 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() + "Font.FallbackSettings.LoadMsOfficeFallbackSettings.xml");
        
        // 2: Use the scheme built from Google Noto fonts
        fontFallbackSettings.loadNotoFallbackSettings();
        fontFallbackSettings.save(getArtifactsDir() + "Font.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();
        
        // Create a FontSettings object for our document and get its FallbackSettings attribute
        FontSettings fontSettings = new FontSettings();
        doc.setFontSettings(fontSettings);
        FontFallbackSettings fontFallbackSettings = fontSettings.getFallbackSettings();
        
        // Save the default fallback font scheme in 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 we are using does not have symbols for the 0x0C00-0x0C7F unicode block,
        // the symbols from the "Vani" font will be used as a substitute
        fontFallbackSettings.save(getArtifactsDir() + "Font.FallbackSettings.Default.xml");
        
        // There 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() + "Font.FallbackSettings.LoadMsOfficeFallbackSettings.xml");
        
        // 2: Use the scheme built from Google Noto fonts
        fontFallbackSettings.loadNotoFallbackSettings();
        fontFallbackSettings.save(getArtifactsDir() + "Font.FallbackSettings.LoadNotoFallbackSettings.xml");
      • 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 from file.
        Document doc = new Document(getMyDir() + "Rendering.doc");
        
        // By default fallback settings are initialized with predefined settings which mimics the Microsoft Word fallback.
        FontSettings fontSettings = new FontSettings();
        fontSettings.getFallbackSettings().load(getMyDir() + "Fallback.xml");
        
        doc.setFontSettings(fontSettings);
        doc.save(getArtifactsDir() + "LoadFontFallbackSettingsFromFile.pdf");
        
        // Saves font fallback setting by string
        doc.getFontSettings().getFallbackSettings().save(getArtifactsDir() + "FallbackSettings.xml");