public class SystemFontSource extends FontSourceBase
To learn more, visit the Working with Fonts documentation article.
Examples:
Shows how to access a document's system font source and set font substitutes.
Document doc = new Document();
doc.setFontSettings(new FontSettings());
// By default, a blank document always contains a system font source.
Assert.assertEquals(1, doc.getFontSettings().getFontsSources().length);
SystemFontSource systemFontSource = (SystemFontSource) doc.getFontSettings().getFontsSources()[0];
Assert.assertEquals(FontSourceType.SYSTEM_FONTS, systemFontSource.getType());
Assert.assertEquals(0, systemFontSource.getPriority());
if (SystemUtils.IS_OS_WINDOWS) {
final String FONTS_PATH = "C:\\WINDOWS\\Fonts";
Assert.assertEquals(FONTS_PATH.toLowerCase(), SystemFontSource.getSystemFontFolders()[0].toLowerCase());
}
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 does not.
doc.getFontSettings().getSubstitutionSettings().getFontInfoSubstitution().setEnabled(true);
doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().addSubstitutes("Kreon-Regular", "Calibri");
Assert.assertEquals(1, IterableUtils.size(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular")));
Assert.assertTrue(IterableUtils.toString(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular")).contains("Calibri"));
// Alternatively, we could add a folder font source in which the corresponding folder contains the font.
FolderFontSource folderFontSource = new FolderFontSource(getFontsDir(), false);
doc.getFontSettings().setFontsSources(new FontSourceBase[]{systemFontSource, folderFontSource});
Assert.assertEquals(2, doc.getFontSettings().getFontsSources().length);
// Resetting the font sources still leaves us with the system font source as well as our substitutes.
doc.getFontSettings().resetFontSources();
Assert.assertEquals(1, doc.getFontSettings().getFontsSources().length);
Assert.assertEquals(FontSourceType.SYSTEM_FONTS, doc.getFontSettings().getFontsSources()[0].getType());
Assert.assertEquals(1, IterableUtils.size(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular")));
Assert.assertTrue(doc.getFontSettings().getSubstitutionSettings().getFontNameSubstitution().getEnabled());
| Constructor and Description |
|---|
SystemFontSource()
Ctor.
|
SystemFontSource(int priority)
Ctor.
|
| Modifier and Type | Method and Description |
|---|---|
java.lang.Iterable |
getFontDataInternal() |
static java.lang.String[] |
getSystemFontFolders()
Returns system font folders or empty array if folders are not accessible.
|
int |
getType()
Returns the type of the font source.
|
getAvailableFonts, getPriority, getPriorityInternal, getWarningCallback, setWarningCallbackpublic SystemFontSource()
Examples:
Shows how to access a document's system font source and set font substitutes.
Document doc = new Document();
doc.setFontSettings(new FontSettings());
// By default, a blank document always contains a system font source.
Assert.assertEquals(1, doc.getFontSettings().getFontsSources().length);
SystemFontSource systemFontSource = (SystemFontSource) doc.getFontSettings().getFontsSources()[0];
Assert.assertEquals(FontSourceType.SYSTEM_FONTS, systemFontSource.getType());
Assert.assertEquals(0, systemFontSource.getPriority());
if (SystemUtils.IS_OS_WINDOWS) {
final String FONTS_PATH = "C:\\WINDOWS\\Fonts";
Assert.assertEquals(FONTS_PATH.toLowerCase(), SystemFontSource.getSystemFontFolders()[0].toLowerCase());
}
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 does not.
doc.getFontSettings().getSubstitutionSettings().getFontInfoSubstitution().setEnabled(true);
doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().addSubstitutes("Kreon-Regular", "Calibri");
Assert.assertEquals(1, IterableUtils.size(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular")));
Assert.assertTrue(IterableUtils.toString(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular")).contains("Calibri"));
// Alternatively, we could add a folder font source in which the corresponding folder contains the font.
FolderFontSource folderFontSource = new FolderFontSource(getFontsDir(), false);
doc.getFontSettings().setFontsSources(new FontSourceBase[]{systemFontSource, folderFontSource});
Assert.assertEquals(2, doc.getFontSettings().getFontsSources().length);
// Resetting the font sources still leaves us with the system font source as well as our substitutes.
doc.getFontSettings().resetFontSources();
Assert.assertEquals(1, doc.getFontSettings().getFontsSources().length);
Assert.assertEquals(FontSourceType.SYSTEM_FONTS, doc.getFontSettings().getFontsSources()[0].getType());
Assert.assertEquals(1, IterableUtils.size(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular")));
Assert.assertTrue(doc.getFontSettings().getSubstitutionSettings().getFontNameSubstitution().getEnabled());
public SystemFontSource(int priority)
Examples:
Shows how to access a document's system font source and set font substitutes.
Document doc = new Document();
doc.setFontSettings(new FontSettings());
// By default, a blank document always contains a system font source.
Assert.assertEquals(1, doc.getFontSettings().getFontsSources().length);
SystemFontSource systemFontSource = (SystemFontSource) doc.getFontSettings().getFontsSources()[0];
Assert.assertEquals(FontSourceType.SYSTEM_FONTS, systemFontSource.getType());
Assert.assertEquals(0, systemFontSource.getPriority());
if (SystemUtils.IS_OS_WINDOWS) {
final String FONTS_PATH = "C:\\WINDOWS\\Fonts";
Assert.assertEquals(FONTS_PATH.toLowerCase(), SystemFontSource.getSystemFontFolders()[0].toLowerCase());
}
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 does not.
doc.getFontSettings().getSubstitutionSettings().getFontInfoSubstitution().setEnabled(true);
doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().addSubstitutes("Kreon-Regular", "Calibri");
Assert.assertEquals(1, IterableUtils.size(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular")));
Assert.assertTrue(IterableUtils.toString(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular")).contains("Calibri"));
// Alternatively, we could add a folder font source in which the corresponding folder contains the font.
FolderFontSource folderFontSource = new FolderFontSource(getFontsDir(), false);
doc.getFontSettings().setFontsSources(new FontSourceBase[]{systemFontSource, folderFontSource});
Assert.assertEquals(2, doc.getFontSettings().getFontsSources().length);
// Resetting the font sources still leaves us with the system font source as well as our substitutes.
doc.getFontSettings().resetFontSources();
Assert.assertEquals(1, doc.getFontSettings().getFontsSources().length);
Assert.assertEquals(FontSourceType.SYSTEM_FONTS, doc.getFontSettings().getFontsSources()[0].getType());
Assert.assertEquals(1, IterableUtils.size(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular")));
Assert.assertTrue(doc.getFontSettings().getSubstitutionSettings().getFontNameSubstitution().getEnabled());
priority - Font source priority. See the FontSourceBase.getPriority() property description for more information.public int getType()
Examples:
Shows how to access a document's system font source and set font substitutes.
Document doc = new Document();
doc.setFontSettings(new FontSettings());
// By default, a blank document always contains a system font source.
Assert.assertEquals(1, doc.getFontSettings().getFontsSources().length);
SystemFontSource systemFontSource = (SystemFontSource) doc.getFontSettings().getFontsSources()[0];
Assert.assertEquals(FontSourceType.SYSTEM_FONTS, systemFontSource.getType());
Assert.assertEquals(0, systemFontSource.getPriority());
if (SystemUtils.IS_OS_WINDOWS) {
final String FONTS_PATH = "C:\\WINDOWS\\Fonts";
Assert.assertEquals(FONTS_PATH.toLowerCase(), SystemFontSource.getSystemFontFolders()[0].toLowerCase());
}
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 does not.
doc.getFontSettings().getSubstitutionSettings().getFontInfoSubstitution().setEnabled(true);
doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().addSubstitutes("Kreon-Regular", "Calibri");
Assert.assertEquals(1, IterableUtils.size(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular")));
Assert.assertTrue(IterableUtils.toString(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular")).contains("Calibri"));
// Alternatively, we could add a folder font source in which the corresponding folder contains the font.
FolderFontSource folderFontSource = new FolderFontSource(getFontsDir(), false);
doc.getFontSettings().setFontsSources(new FontSourceBase[]{systemFontSource, folderFontSource});
Assert.assertEquals(2, doc.getFontSettings().getFontsSources().length);
// Resetting the font sources still leaves us with the system font source as well as our substitutes.
doc.getFontSettings().resetFontSources();
Assert.assertEquals(1, doc.getFontSettings().getFontsSources().length);
Assert.assertEquals(FontSourceType.SYSTEM_FONTS, doc.getFontSettings().getFontsSources()[0].getType());
Assert.assertEquals(1, IterableUtils.size(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular")));
Assert.assertTrue(doc.getFontSettings().getSubstitutionSettings().getFontNameSubstitution().getEnabled());
getType in class FontSourceBaseFontSourceType constants.public java.lang.Iterable getFontDataInternal()
getFontDataInternal in class FontSourceBasepublic static java.lang.String[] getSystemFontFolders()
throws java.lang.Exception
Remarks:
On some platforms Aspose.Words could search system fonts not only through folders but in other sources too. For example, on Windows platform Aspose.Words search fonts also in the registry.
Examples:
Shows how to access a document's system font source and set font substitutes.
Document doc = new Document();
doc.setFontSettings(new FontSettings());
// By default, a blank document always contains a system font source.
Assert.assertEquals(1, doc.getFontSettings().getFontsSources().length);
SystemFontSource systemFontSource = (SystemFontSource) doc.getFontSettings().getFontsSources()[0];
Assert.assertEquals(FontSourceType.SYSTEM_FONTS, systemFontSource.getType());
Assert.assertEquals(0, systemFontSource.getPriority());
if (SystemUtils.IS_OS_WINDOWS) {
final String FONTS_PATH = "C:\\WINDOWS\\Fonts";
Assert.assertEquals(FONTS_PATH.toLowerCase(), SystemFontSource.getSystemFontFolders()[0].toLowerCase());
}
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 does not.
doc.getFontSettings().getSubstitutionSettings().getFontInfoSubstitution().setEnabled(true);
doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().addSubstitutes("Kreon-Regular", "Calibri");
Assert.assertEquals(1, IterableUtils.size(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular")));
Assert.assertTrue(IterableUtils.toString(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular")).contains("Calibri"));
// Alternatively, we could add a folder font source in which the corresponding folder contains the font.
FolderFontSource folderFontSource = new FolderFontSource(getFontsDir(), false);
doc.getFontSettings().setFontsSources(new FontSourceBase[]{systemFontSource, folderFontSource});
Assert.assertEquals(2, doc.getFontSettings().getFontsSources().length);
// Resetting the font sources still leaves us with the system font source as well as our substitutes.
doc.getFontSettings().resetFontSources();
Assert.assertEquals(1, doc.getFontSettings().getFontsSources().length);
Assert.assertEquals(FontSourceType.SYSTEM_FONTS, doc.getFontSettings().getFontsSources()[0].getType());
Assert.assertEquals(1, IterableUtils.size(doc.getFontSettings().getSubstitutionSettings().getTableSubstitution().getSubstitutes("Kreon-Regular")));
Assert.assertTrue(doc.getFontSettings().getSubstitutionSettings().getFontNameSubstitution().getEnabled());
java.lang.Exception