public abstract class StreamFontSource extends FontSourceBase
To learn more, visit the Working with Fonts documentation article.
Remarks:
In order to use the stream font source you should create a derived class from the StreamFontSource and provide implementation of the openFontDataStream() method.
openFontDataStream() method could be called several times. For the first time it will be called when Aspose.Words scans the provided font sources to get the list of available fonts. Later it may be called if the font is used in the document to parse the font data and to embed the font data to some output formats.
StreamFontSource may be useful because it allows to load the font data only when it is required and not to store it in the memory for the FontSettings lifetime.
Examples:
Shows how to load fonts from stream.
public void streamFontSourceFileRendering() throws Exception {
FontSettings fontSettings = new FontSettings();
fontSettings.setFontsSources(new FontSourceBase[]{new StreamFontSourceFile()});
DocumentBuilder builder = new DocumentBuilder();
builder.getDocument().setFontSettings(fontSettings);
builder.getFont().setName("Kreon-Regular");
builder.writeln("Test aspose text when saving to PDF.");
builder.getDocument().save(getArtifactsDir() + "FontSettings.StreamFontSourceFileRendering.pdf");
}
/// <summary>
/// Load the font data only when required instead of storing it in the memory for the entire lifetime of the "FontSettings" object.
/// </summary>
private static class StreamFontSourceFile extends StreamFontSource {
public FileInputStream openFontDataStream() throws Exception {
return new FileInputStream(getFontsDir() + "Kreon-Regular.ttf");
}
}
| Modifier | Constructor and Description |
|---|---|
protected |
StreamFontSource()
Initializes a new instance of this class.
|
protected |
StreamFontSource(int priority)
Initializes a new instance of this class.
|
protected |
StreamFontSource(int priority,
java.lang.String cacheKey)
Initializes a new instance of this class.
|
| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
getCacheKey()
The key of this source in the cache.
|
java.lang.String |
getCacheKeyInternal() |
java.lang.String |
getFilePath() |
byte[] |
getFontBytes() |
java.lang.Iterable |
getFontDataInternal() |
int |
getSize() |
int |
getType()
Returns the type of the font source.
|
boolean |
isEmbedded() |
abstract java.io.InputStream |
openFontDataStream()
This method should open the stream with font data on demand.
|
getAvailableFonts, getPriority, getPriorityInternal, getWarningCallback, setWarningCallbackprotected StreamFontSource()
protected StreamFontSource(int priority)
protected StreamFontSource(int priority,
java.lang.String cacheKey)
public java.lang.String getCacheKey()
Remarks:
This key is used to identify cache item when saving/loading font search cache with and methods.
Examples:
Shows how to speed up the font cache initialization process.
public void loadFontSearchCache() throws Exception
{
final String CACHE_KEY_1 = "Arvo";
final String CACHE_KEY_2 = "Arvo-Bold";
FontSettings parsedFonts = new FontSettings();
FontSettings loadedCache = new FontSettings();
parsedFonts.setFontsSources(new FontSourceBase[]
{
new FileFontSource(getFontsDir() + "Arvo-Regular.ttf", 0, CACHE_KEY_1),
new FileFontSource(getFontsDir() + "Arvo-Bold.ttf", 0, CACHE_KEY_2)
});
try (ByteArrayOutputStream cacheStream = new ByteArrayOutputStream())
{
parsedFonts.saveSearchCache(cacheStream);
ByteArrayInputStream inputStream = new ByteArrayInputStream(cacheStream.toByteArray());
loadedCache.setFontsSources(new FontSourceBase[]
{
new SearchCacheStream(CACHE_KEY_1),
new MemoryFontSource(Files.readAllBytes(Paths.get(getFontsDir() + "Arvo-Bold.ttf")), 0, CACHE_KEY_2)
}, inputStream);
}
Assert.assertEquals(parsedFonts.getFontsSources().length, loadedCache.getFontsSources().length);
}
/// <summary>
/// Load the font data only when required instead of storing it in the memory
/// for the entire lifetime of the "FontSettings" object.
/// </summary>
private static class SearchCacheStream extends StreamFontSource
{
public SearchCacheStream(String cacheKey)
{
super(0, cacheKey);
}
public FileInputStream openFontDataStream() throws Exception
{
return new FileInputStream(getFontsDir() + "Arvo-Regular.ttf");
}
}
String value.public int getType()
getType in class FontSourceBaseFontSourceType constants.public abstract java.io.InputStream openFontDataStream()
throws java.lang.Exception
Remarks:
The stream will be closed after reading. There is no need to close it explicitly.
Examples:
Shows how to load fonts from stream.
public void streamFontSourceFileRendering() throws Exception {
FontSettings fontSettings = new FontSettings();
fontSettings.setFontsSources(new FontSourceBase[]{new StreamFontSourceFile()});
DocumentBuilder builder = new DocumentBuilder();
builder.getDocument().setFontSettings(fontSettings);
builder.getFont().setName("Kreon-Regular");
builder.writeln("Test aspose text when saving to PDF.");
builder.getDocument().save(getArtifactsDir() + "FontSettings.StreamFontSourceFileRendering.pdf");
}
/// <summary>
/// Load the font data only when required instead of storing it in the memory for the entire lifetime of the "FontSettings" object.
/// </summary>
private static class StreamFontSourceFile extends StreamFontSource {
public FileInputStream openFontDataStream() throws Exception {
return new FileInputStream(getFontsDir() + "Kreon-Regular.ttf");
}
}
java.lang.Exceptionpublic int getSize()
public java.lang.String getFilePath()
public java.lang.String getCacheKeyInternal()
public byte[] getFontBytes()
throws java.lang.Exception
java.lang.Exceptionpublic boolean isEmbedded()
public java.lang.Iterable getFontDataInternal()
getFontDataInternal in class FontSourceBase