java.lang.ObjectFontSubstitutionRule
com.aspose.words.TableSubstitutionRule
public class TableSubstitutionRule
Example:
// Create a blank document and a new FontSettings object
Document doc = new Document();
FontSettings fontSettings = new FontSettings();
doc.setFontSettings(fontSettings);
// Create a new table substitution rule and load the default Windows font substitution table
TableSubstitutionRule substitutionRule = fontSettings.getSubstitutionSettings().getTableSubstitution();
substitutionRule.loadWindowsSettings();
// In Windows, the default substitute for the "Times New Roman CE" font is "Times New Roman"
Assert.assertEquals(substitutionRule.getSubstitutes("Times New Roman CE"), Arrays.asList(new String[]{"Times New Roman"}));
// We can save the table for viewing in the form of an XML document
// Note: Saves table substitution rules by stream is not implemented now
substitutionRule.save(getArtifactsDir() + "Font.TableSubstitutionRule.Windows.xml");
// Linux has its own substitution table
// If "FreeSerif" is unavailable to substitute for "Times New Roman CE", we then look for "Liberation Serif", and so on
substitutionRule.loadLinuxSettings();
Assert.assertEquals(substitutionRule.getSubstitutes("Times New Roman CE"), Arrays.asList(new String[]{"FreeSerif", "Liberation Serif", "DejaVu Serif"}));
| Property Getters/Setters Summary | ||
|---|---|---|
boolean | getEnabled() | → inherited from FontSubstitutionRule |
void | setEnabled(boolean value) | |
| Specifies whether the rule is enabled or not. | ||
| Method Summary | ||
|---|---|---|
void | addSubstitutes(java.lang.String originalFontName, java.lang.String[] substituteFontNames) | |
| Adds substitute font names for given original font name. | ||
System.Collections.Generic.IEnumerable`1 | getSubstitutes(java.lang.String originalFontName) | |
| Returns array containing substitute font names for the specified original font name. | ||
void | load(java.io.InputStream stream) | |
| Loads table substitution settings from XML stream. | ||
void | load(java.lang.String fileName) | |
| Loads table substitution settings from XML file. | ||
void | loadAndroidSettings() | |
| Loads predefined table substitution settings for Linux platform. | ||
void | loadLinuxSettings() | |
| Loads predefined table substitution settings for Linux platform. | ||
void | loadWindowsSettings() | |
| Loads predefined table substitution settings for Windows platform. | ||
void | save(java.io.InputStream stream) | |
| Saves the current table substitution settings to stream. | ||
void | save(java.lang.String fileName) | |
| Saves the current table substitution settings to file. | ||
void | setSubstitutes(java.lang.String originalFontName, java.lang.String[] substituteFontNames) | |
| Override substitute font names for given original font name. | ||
| Property Getters/Setters Detail |
|---|
getEnabled/setEnabled | → inherited from FontSubstitutionRule |
public boolean getEnabled() / public void setEnabled(boolean value) | |
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);| Method Detail |
|---|
addSubstitutes | |
public void addSubstitutes(java.lang.String originalFontName, java.lang.String[] substituteFontNames) | |
originalFontName - Original font name.substituteFontNames - List of alternative font names.Example:
Shows how to work with custom font substitution tables.
// Create a blank document and a new FontSettings object
Document doc = new Document();
FontSettings fontSettings = new FontSettings();
doc.setFontSettings(fontSettings);
// Create a new table substitution rule and load the default Windows font substitution table
TableSubstitutionRule tableSubstitutionRule = fontSettings.getSubstitutionSettings().getTableSubstitution();
// If we select fonts exclusively from our own folder, we will need a custom substitution table
FolderFontSource folderFontSource = new FolderFontSource(getMyDir() + "\\MyFonts", false);
fontSettings.setFontsSources(new FontSourceBase[]{folderFontSource});
// There are two ways of loading a substitution table from a file in the local file system
// 1: Loading from a stream
InputStream fileStream = new FileInputStream(getMyDir() + "Font.TableSubstitutionRule.Custom.xml");
try {
tableSubstitutionRule.load(fileStream);
} finally {
if (fileStream != null) fileStream.close();
}
// 2: Load directly from file
tableSubstitutionRule.load(getMyDir() + "Font.TableSubstitutionRule.Custom.xml");
// Since we no longer have access to "Arial", our font table will first try substitute it with "Nonexistent Font", which we don't have,
// and then with "Kreon", found in the "MyFonts" folder
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Arial"), Arrays.asList(new String[]{"Missing Font", "Kreon"}));
// If we find this substitution table lacking, we can also expand it programmatically
// In this case, we add an entry that substitutes "Times New Roman" with "Arvo"
Assert.assertNull(tableSubstitutionRule.getSubstitutes("Times New Roman"));
tableSubstitutionRule.addSubstitutes("Times New Roman", "Arvo");
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Times New Roman"), Arrays.asList(new String[]{"Arvo"}));
// We can add a secondary fallback substitute for an existing font entry with AddSubstitutes()
// In case "Arvo" is unavailable, our table will look for "M+ 2m"
tableSubstitutionRule.addSubstitutes("Times New Roman", "M+ 2m");
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Times New Roman"), Arrays.asList(new String[]{"Arvo", "M+ 2m"}));
// SetSubstitutes() can set a new list of substitute fonts for a font
tableSubstitutionRule.setSubstitutes("Times New Roman", new String[]{"Squarish Sans CT", "M+ 2m"});
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Times New Roman"), Arrays.asList(new String[]{"Squarish Sans CT", "M+ 2m"}));
// TO demonstrate substitution, write text in fonts we have no access to and render the result in a PDF
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Arial");
builder.writeln("Text written in Arial, to be substituted by Kreon.");
builder.getFont().setName("Times New Roman");
builder.writeln("Text written in Times New Roman, to be substituted by Squarish Sans CT.");
doc.save(getArtifactsDir() + "Font.TableSubstitutionRule.Custom.pdf");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);getSubstitutes | |
public System.Collections.Generic.IEnumerable`1 getSubstitutes(java.lang.String originalFontName) | |
originalFontName - Original font name.Example:
Shows how to work with custom font substitution tables.
// Create a blank document and a new FontSettings object
Document doc = new Document();
FontSettings fontSettings = new FontSettings();
doc.setFontSettings(fontSettings);
// Create a new table substitution rule and load the default Windows font substitution table
TableSubstitutionRule tableSubstitutionRule = fontSettings.getSubstitutionSettings().getTableSubstitution();
// If we select fonts exclusively from our own folder, we will need a custom substitution table
FolderFontSource folderFontSource = new FolderFontSource(getMyDir() + "\\MyFonts", false);
fontSettings.setFontsSources(new FontSourceBase[]{folderFontSource});
// There are two ways of loading a substitution table from a file in the local file system
// 1: Loading from a stream
InputStream fileStream = new FileInputStream(getMyDir() + "Font.TableSubstitutionRule.Custom.xml");
try {
tableSubstitutionRule.load(fileStream);
} finally {
if (fileStream != null) fileStream.close();
}
// 2: Load directly from file
tableSubstitutionRule.load(getMyDir() + "Font.TableSubstitutionRule.Custom.xml");
// Since we no longer have access to "Arial", our font table will first try substitute it with "Nonexistent Font", which we don't have,
// and then with "Kreon", found in the "MyFonts" folder
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Arial"), Arrays.asList(new String[]{"Missing Font", "Kreon"}));
// If we find this substitution table lacking, we can also expand it programmatically
// In this case, we add an entry that substitutes "Times New Roman" with "Arvo"
Assert.assertNull(tableSubstitutionRule.getSubstitutes("Times New Roman"));
tableSubstitutionRule.addSubstitutes("Times New Roman", "Arvo");
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Times New Roman"), Arrays.asList(new String[]{"Arvo"}));
// We can add a secondary fallback substitute for an existing font entry with AddSubstitutes()
// In case "Arvo" is unavailable, our table will look for "M+ 2m"
tableSubstitutionRule.addSubstitutes("Times New Roman", "M+ 2m");
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Times New Roman"), Arrays.asList(new String[]{"Arvo", "M+ 2m"}));
// SetSubstitutes() can set a new list of substitute fonts for a font
tableSubstitutionRule.setSubstitutes("Times New Roman", new String[]{"Squarish Sans CT", "M+ 2m"});
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Times New Roman"), Arrays.asList(new String[]{"Squarish Sans CT", "M+ 2m"}));
// TO demonstrate substitution, write text in fonts we have no access to and render the result in a PDF
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Arial");
builder.writeln("Text written in Arial, to be substituted by Kreon.");
builder.getFont().setName("Times New Roman");
builder.writeln("Text written in Times New Roman, to be substituted by Squarish Sans CT.");
doc.save(getArtifactsDir() + "Font.TableSubstitutionRule.Custom.pdf");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);load | |
public void load(java.io.InputStream stream)
throws java.lang.Exception | |
stream - Input stream.Example:
Shows how to work with custom font substitution tables.
// Create a blank document and a new FontSettings object
Document doc = new Document();
FontSettings fontSettings = new FontSettings();
doc.setFontSettings(fontSettings);
// Create a new table substitution rule and load the default Windows font substitution table
TableSubstitutionRule tableSubstitutionRule = fontSettings.getSubstitutionSettings().getTableSubstitution();
// If we select fonts exclusively from our own folder, we will need a custom substitution table
FolderFontSource folderFontSource = new FolderFontSource(getMyDir() + "\\MyFonts", false);
fontSettings.setFontsSources(new FontSourceBase[]{folderFontSource});
// There are two ways of loading a substitution table from a file in the local file system
// 1: Loading from a stream
InputStream fileStream = new FileInputStream(getMyDir() + "Font.TableSubstitutionRule.Custom.xml");
try {
tableSubstitutionRule.load(fileStream);
} finally {
if (fileStream != null) fileStream.close();
}
// 2: Load directly from file
tableSubstitutionRule.load(getMyDir() + "Font.TableSubstitutionRule.Custom.xml");
// Since we no longer have access to "Arial", our font table will first try substitute it with "Nonexistent Font", which we don't have,
// and then with "Kreon", found in the "MyFonts" folder
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Arial"), Arrays.asList(new String[]{"Missing Font", "Kreon"}));
// If we find this substitution table lacking, we can also expand it programmatically
// In this case, we add an entry that substitutes "Times New Roman" with "Arvo"
Assert.assertNull(tableSubstitutionRule.getSubstitutes("Times New Roman"));
tableSubstitutionRule.addSubstitutes("Times New Roman", "Arvo");
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Times New Roman"), Arrays.asList(new String[]{"Arvo"}));
// We can add a secondary fallback substitute for an existing font entry with AddSubstitutes()
// In case "Arvo" is unavailable, our table will look for "M+ 2m"
tableSubstitutionRule.addSubstitutes("Times New Roman", "M+ 2m");
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Times New Roman"), Arrays.asList(new String[]{"Arvo", "M+ 2m"}));
// SetSubstitutes() can set a new list of substitute fonts for a font
tableSubstitutionRule.setSubstitutes("Times New Roman", new String[]{"Squarish Sans CT", "M+ 2m"});
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Times New Roman"), Arrays.asList(new String[]{"Squarish Sans CT", "M+ 2m"}));
// TO demonstrate substitution, write text in fonts we have no access to and render the result in a PDF
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Arial");
builder.writeln("Text written in Arial, to be substituted by Kreon.");
builder.getFont().setName("Times New Roman");
builder.writeln("Text written in Times New Roman, to be substituted by Squarish Sans CT.");
doc.save(getArtifactsDir() + "Font.TableSubstitutionRule.Custom.pdf");load | |
public void load(java.lang.String fileName)
throws java.lang.Exception | |
fileName - Input file name.Example:
Shows how to work with custom font substitution tables.
// Create a blank document and a new FontSettings object
Document doc = new Document();
FontSettings fontSettings = new FontSettings();
doc.setFontSettings(fontSettings);
// Create a new table substitution rule and load the default Windows font substitution table
TableSubstitutionRule tableSubstitutionRule = fontSettings.getSubstitutionSettings().getTableSubstitution();
// If we select fonts exclusively from our own folder, we will need a custom substitution table
FolderFontSource folderFontSource = new FolderFontSource(getMyDir() + "\\MyFonts", false);
fontSettings.setFontsSources(new FontSourceBase[]{folderFontSource});
// There are two ways of loading a substitution table from a file in the local file system
// 1: Loading from a stream
InputStream fileStream = new FileInputStream(getMyDir() + "Font.TableSubstitutionRule.Custom.xml");
try {
tableSubstitutionRule.load(fileStream);
} finally {
if (fileStream != null) fileStream.close();
}
// 2: Load directly from file
tableSubstitutionRule.load(getMyDir() + "Font.TableSubstitutionRule.Custom.xml");
// Since we no longer have access to "Arial", our font table will first try substitute it with "Nonexistent Font", which we don't have,
// and then with "Kreon", found in the "MyFonts" folder
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Arial"), Arrays.asList(new String[]{"Missing Font", "Kreon"}));
// If we find this substitution table lacking, we can also expand it programmatically
// In this case, we add an entry that substitutes "Times New Roman" with "Arvo"
Assert.assertNull(tableSubstitutionRule.getSubstitutes("Times New Roman"));
tableSubstitutionRule.addSubstitutes("Times New Roman", "Arvo");
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Times New Roman"), Arrays.asList(new String[]{"Arvo"}));
// We can add a secondary fallback substitute for an existing font entry with AddSubstitutes()
// In case "Arvo" is unavailable, our table will look for "M+ 2m"
tableSubstitutionRule.addSubstitutes("Times New Roman", "M+ 2m");
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Times New Roman"), Arrays.asList(new String[]{"Arvo", "M+ 2m"}));
// SetSubstitutes() can set a new list of substitute fonts for a font
tableSubstitutionRule.setSubstitutes("Times New Roman", new String[]{"Squarish Sans CT", "M+ 2m"});
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Times New Roman"), Arrays.asList(new String[]{"Squarish Sans CT", "M+ 2m"}));
// TO demonstrate substitution, write text in fonts we have no access to and render the result in a PDF
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Arial");
builder.writeln("Text written in Arial, to be substituted by Kreon.");
builder.getFont().setName("Times New Roman");
builder.writeln("Text written in Times New Roman, to be substituted by Squarish Sans CT.");
doc.save(getArtifactsDir() + "Font.TableSubstitutionRule.Custom.pdf");loadAndroidSettings | |
public void loadAndroidSettings() | |
loadLinuxSettings | |
public void loadLinuxSettings() | |
Example:
Shows how to access font substitution tables for Windows and Linux.
// Create a blank document and a new FontSettings object
Document doc = new Document();
FontSettings fontSettings = new FontSettings();
doc.setFontSettings(fontSettings);
// Create a new table substitution rule and load the default Windows font substitution table
TableSubstitutionRule substitutionRule = fontSettings.getSubstitutionSettings().getTableSubstitution();
substitutionRule.loadWindowsSettings();
// In Windows, the default substitute for the "Times New Roman CE" font is "Times New Roman"
Assert.assertEquals(substitutionRule.getSubstitutes("Times New Roman CE"), Arrays.asList(new String[]{"Times New Roman"}));
// We can save the table for viewing in the form of an XML document
// Note: Saves table substitution rules by stream is not implemented now
substitutionRule.save(getArtifactsDir() + "Font.TableSubstitutionRule.Windows.xml");
// Linux has its own substitution table
// If "FreeSerif" is unavailable to substitute for "Times New Roman CE", we then look for "Liberation Serif", and so on
substitutionRule.loadLinuxSettings();
Assert.assertEquals(substitutionRule.getSubstitutes("Times New Roman CE"), Arrays.asList(new String[]{"FreeSerif", "Liberation Serif", "DejaVu Serif"}));loadWindowsSettings | |
public void loadWindowsSettings() | |
Example:
Shows how to access font substitution tables for Windows and Linux.
// Create a blank document and a new FontSettings object
Document doc = new Document();
FontSettings fontSettings = new FontSettings();
doc.setFontSettings(fontSettings);
// Create a new table substitution rule and load the default Windows font substitution table
TableSubstitutionRule substitutionRule = fontSettings.getSubstitutionSettings().getTableSubstitution();
substitutionRule.loadWindowsSettings();
// In Windows, the default substitute for the "Times New Roman CE" font is "Times New Roman"
Assert.assertEquals(substitutionRule.getSubstitutes("Times New Roman CE"), Arrays.asList(new String[]{"Times New Roman"}));
// We can save the table for viewing in the form of an XML document
// Note: Saves table substitution rules by stream is not implemented now
substitutionRule.save(getArtifactsDir() + "Font.TableSubstitutionRule.Windows.xml");
// Linux has its own substitution table
// If "FreeSerif" is unavailable to substitute for "Times New Roman CE", we then look for "Liberation Serif", and so on
substitutionRule.loadLinuxSettings();
Assert.assertEquals(substitutionRule.getSubstitutes("Times New Roman CE"), Arrays.asList(new String[]{"FreeSerif", "Liberation Serif", "DejaVu Serif"}));save | |
public void save(java.io.InputStream stream)
throws java.lang.Exception | |
outputStream - Output stream.save | |
public void save(java.lang.String fileName)
throws java.lang.Exception | |
fileName - Output file name.Example:
Shows how to access font substitution tables for Windows and Linux.
// Create a blank document and a new FontSettings object
Document doc = new Document();
FontSettings fontSettings = new FontSettings();
doc.setFontSettings(fontSettings);
// Create a new table substitution rule and load the default Windows font substitution table
TableSubstitutionRule substitutionRule = fontSettings.getSubstitutionSettings().getTableSubstitution();
substitutionRule.loadWindowsSettings();
// In Windows, the default substitute for the "Times New Roman CE" font is "Times New Roman"
Assert.assertEquals(substitutionRule.getSubstitutes("Times New Roman CE"), Arrays.asList(new String[]{"Times New Roman"}));
// We can save the table for viewing in the form of an XML document
// Note: Saves table substitution rules by stream is not implemented now
substitutionRule.save(getArtifactsDir() + "Font.TableSubstitutionRule.Windows.xml");
// Linux has its own substitution table
// If "FreeSerif" is unavailable to substitute for "Times New Roman CE", we then look for "Liberation Serif", and so on
substitutionRule.loadLinuxSettings();
Assert.assertEquals(substitutionRule.getSubstitutes("Times New Roman CE"), Arrays.asList(new String[]{"FreeSerif", "Liberation Serif", "DejaVu Serif"}));setSubstitutes | |
public void setSubstitutes(java.lang.String originalFontName, java.lang.String[] substituteFontNames) | |
originalFontName - Original font name.substituteFontNames - List of alternative font names.Example:
Shows how to work with custom font substitution tables.
// Create a blank document and a new FontSettings object
Document doc = new Document();
FontSettings fontSettings = new FontSettings();
doc.setFontSettings(fontSettings);
// Create a new table substitution rule and load the default Windows font substitution table
TableSubstitutionRule tableSubstitutionRule = fontSettings.getSubstitutionSettings().getTableSubstitution();
// If we select fonts exclusively from our own folder, we will need a custom substitution table
FolderFontSource folderFontSource = new FolderFontSource(getMyDir() + "\\MyFonts", false);
fontSettings.setFontsSources(new FontSourceBase[]{folderFontSource});
// There are two ways of loading a substitution table from a file in the local file system
// 1: Loading from a stream
InputStream fileStream = new FileInputStream(getMyDir() + "Font.TableSubstitutionRule.Custom.xml");
try {
tableSubstitutionRule.load(fileStream);
} finally {
if (fileStream != null) fileStream.close();
}
// 2: Load directly from file
tableSubstitutionRule.load(getMyDir() + "Font.TableSubstitutionRule.Custom.xml");
// Since we no longer have access to "Arial", our font table will first try substitute it with "Nonexistent Font", which we don't have,
// and then with "Kreon", found in the "MyFonts" folder
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Arial"), Arrays.asList(new String[]{"Missing Font", "Kreon"}));
// If we find this substitution table lacking, we can also expand it programmatically
// In this case, we add an entry that substitutes "Times New Roman" with "Arvo"
Assert.assertNull(tableSubstitutionRule.getSubstitutes("Times New Roman"));
tableSubstitutionRule.addSubstitutes("Times New Roman", "Arvo");
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Times New Roman"), Arrays.asList(new String[]{"Arvo"}));
// We can add a secondary fallback substitute for an existing font entry with AddSubstitutes()
// In case "Arvo" is unavailable, our table will look for "M+ 2m"
tableSubstitutionRule.addSubstitutes("Times New Roman", "M+ 2m");
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Times New Roman"), Arrays.asList(new String[]{"Arvo", "M+ 2m"}));
// SetSubstitutes() can set a new list of substitute fonts for a font
tableSubstitutionRule.setSubstitutes("Times New Roman", new String[]{"Squarish Sans CT", "M+ 2m"});
Assert.assertEquals(tableSubstitutionRule.getSubstitutes("Times New Roman"), Arrays.asList(new String[]{"Squarish Sans CT", "M+ 2m"}));
// TO demonstrate substitution, write text in fonts we have no access to and render the result in a PDF
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Arial");
builder.writeln("Text written in Arial, to be substituted by Kreon.");
builder.getFont().setName("Times New Roman");
builder.writeln("Text written in Times New Roman, to be substituted by Squarish Sans CT.");
doc.save(getArtifactsDir() + "Font.TableSubstitutionRule.Custom.pdf");Example:
Shows how to define alternative fonts if original does not exist
FontSettings fontSettings = new FontSettings();
fontSettings.getSubstitutionSettings().getTableSubstitution().addSubstitutes("Times New Roman", "Slab", "Arvo");