com.aspose.words
Class FontFallbackSettings

java.lang.Object
    extended by 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(getFontsDir(), 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.FallbackSettingsCustom.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() + "Custom font fallback settings.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.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();

// 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(getFontsDir(), 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.FallbackSettingsCustom.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() + "Custom font fallback settings.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.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 from stream.
Document doc = new Document(getMyDir() + "Rendering.docx");

// By default fallback settings are initialized with predefined settings which mimics the Microsoft Word fallback
InputStream fontFallbackStream = new FileInputStream(getMyDir() + "Font fallback rules.xml");
try {
    FontSettings fontSettings = new FontSettings();
    fontSettings.getFallbackSettings().load(fontFallbackStream);

    doc.setFontSettings(fontSettings);
} finally {
    if (fontFallbackStream != null) fontFallbackStream.close();
}

doc.save(getArtifactsDir() + "Font.LoadFontFallbackSettingsFromStream.pdf");

// Saves font fallback setting by stream
InputStream fontFallbackStream1 =
        new FileInputStream(getArtifactsDir() + "FallbackSettings.xml");
try {
    doc.getFontSettings().getFallbackSettings().save(fontFallbackStream1);
} finally {
    if (fontFallbackStream1 != null) fontFallbackStream1.close();
}

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.docx");

// By default fallback settings are initialized with predefined settings which mimics the Microsoft Word fallback
FontSettings fontSettings = new FontSettings();
fontSettings.getFallbackSettings().load(getMyDir() + "Font fallback rules.xml");

doc.setFontSettings(fontSettings);
doc.save(getArtifactsDir() + "Font.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");

Example:

Shows how to add predefined font fallback settings for Google Noto fonts.
FontSettings fontSettings = new FontSettings();

// These are free fonts licensed under SIL OFL
// They can be downloaded from https://www.google.com/get/noto/#sans-lgc
fontSettings.setFontsFolder(getFontsDir() + "Noto", false);

// Note that only Sans style Noto fonts with regular weight are used in the predefined settings
// Some of the Noto fonts uses advanced typography features
// Advanced typography is currently not supported by AW and these fonts may be rendered inaccurately
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 from stream.
Document doc = new Document(getMyDir() + "Rendering.docx");

// By default fallback settings are initialized with predefined settings which mimics the Microsoft Word fallback
InputStream fontFallbackStream = new FileInputStream(getMyDir() + "Font fallback rules.xml");
try {
    FontSettings fontSettings = new FontSettings();
    fontSettings.getFallbackSettings().load(fontFallbackStream);

    doc.setFontSettings(fontSettings);
} finally {
    if (fontFallbackStream != null) fontFallbackStream.close();
}

doc.save(getArtifactsDir() + "Font.LoadFontFallbackSettingsFromStream.pdf");

// Saves font fallback setting by stream
InputStream fontFallbackStream1 =
        new FileInputStream(getArtifactsDir() + "FallbackSettings.xml");
try {
    doc.getFontSettings().getFallbackSettings().save(fontFallbackStream1);
} finally {
    if (fontFallbackStream1 != null) fontFallbackStream1.close();
}

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.docx");

// By default fallback settings are initialized with predefined settings which mimics the Microsoft Word fallback
FontSettings fontSettings = new FontSettings();
fontSettings.getFallbackSettings().load(getMyDir() + "Font fallback rules.xml");

doc.setFontSettings(fontSettings);
doc.save(getArtifactsDir() + "Font.LoadFontFallbackSettingsFromFile.pdf");

// Saves font fallback setting by string
doc.getFontSettings().getFallbackSettings().save(getArtifactsDir() + "FallbackSettings.xml");

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