com.aspose.words

Class SystemFontSource

  • All Implemented Interfaces:
    com.aspose.words.internal.zzO0
    public class SystemFontSource 
    extends FontSourceBase

Represents all TrueType fonts installed to the system.

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

Constructor Summary
SystemFontSource(intpriority)
Ctor.
 
Property Getters/Setters Summary
intgetPriority()
Returns the font source priority.
intgetType()
Returns the type of the font source. The value of the property is FontSourceType integer constant.
 
Method Summary
java.util.ArrayList<PhysicalFontInfo>getAvailableFonts()
Returns list of fonts available via this source.
static java.lang.String[]getSystemFontFolders()
Returns system font folders or empty array if folders are not accessible.
 

    • Constructor Detail

      • SystemFontSource

        public SystemFontSource()
        Ctor.

        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);
      • SystemFontSource

        public SystemFontSource(int priority)
        Ctor.
        Parameters:
        priority - Font source priority. See the Priority property description for more information.

        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);
    • Property Getters/Setters Detail

      • getPriority

        public int getPriority()
        
        Returns the font source priority.

        This value is used when there are fonts with the same family name and style in different font sources. In this case Aspose.Words selects the font from the source with the higher priority value.

        The default value is 0.

        Example:

        Shows how to create a file font source.
        Document doc = new Document();
        
        // Create a font settings object for our document
        doc.setFontSettings(new FontSettings());
        
        // Create a font source from a file in our system
        FileFontSource fileFontSource = new FileFontSource(getMyDir() + "Alte DIN 1451 Mittelschrift.ttf", 0);
        
        // Import the font source into our document
        doc.getFontSettings().setFontsSources(new FontSourceBase[]{fileFontSource});
        
        Assert.assertEquals(fileFontSource.getFilePath(), getMyDir() + "Alte DIN 1451 Mittelschrift.ttf");
        Assert.assertEquals(fileFontSource.getType(), FontSourceType.FONT_FILE);
        Assert.assertEquals(fileFontSource.getPriority(), 0);
      • getType

        public int getType()
        
        Returns the type of the font source. The value of the property is FontSourceType integer constant.

        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

      • getAvailableFonts

        public java.util.ArrayList<PhysicalFontInfo> getAvailableFonts()
        Returns list of fonts available via this source.

        Example:

        Shows how to get available fonts and information about them.
        // Add a new folder source which will instruct Aspose.Words to search the following folder for fonts.
        FontSourceBase[] folderFontSource = {new FolderFontSource(getMyDir() + "MyFonts\\", true)};
        
        for (PhysicalFontInfo fontInfo : folderFontSource[0].getAvailableFonts()) {
            System.out.println(MessageFormat.format("FontFamilyName : {0}", fontInfo.getFontFamilyName()));
            System.out.println(MessageFormat.format("FullFontName  : {0}", fontInfo.getFullFontName()));
            System.out.println(MessageFormat.format("Version  : {0}", fontInfo.getVersion()));
            System.out.println(MessageFormat.format("FilePath : {0}\n", fontInfo.getFilePath()));
        }
      • getSystemFontFolders

        public static java.lang.String[] getSystemFontFolders()
                                                 throws java.lang.Exception
        Returns system font folders or empty array if folders are not accessible. 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.

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