public class MemoryFontSource extends FontSourceBase
To learn more, visit the Working with Fonts documentation article.
Examples:
Shows how to use a byte array with data from a font file as a font source.
byte[] fontBytes = DocumentHelper.getBytesFromStream(new FileInputStream(getMyDir() + "Alte DIN 1451 Mittelschrift.ttf"));
MemoryFontSource memoryFontSource = new MemoryFontSource(fontBytes, 0);
Document doc = new Document();
doc.setFontSettings(new FontSettings());
doc.getFontSettings().setFontsSources(new FontSourceBase[]{memoryFontSource});
Assert.assertEquals(FontSourceType.MEMORY_FONT, memoryFontSource.getType());
Assert.assertEquals(0, memoryFontSource.getPriority());
| Constructor and Description |
|---|
MemoryFontSource(byte[] fontData)
Ctor.
|
MemoryFontSource(byte[] fontData,
int priority)
Ctor.
|
MemoryFontSource(byte[] fontData,
int priority,
java.lang.String cacheKey)
Ctor.
|
| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
getCacheKey()
The key of this source in the cache.
|
byte[] |
getFontData()
Binary font data.
|
java.lang.Iterable |
getFontDataInternal() |
int |
getType()
Returns the type of the font source.
|
getAvailableFonts, getPriority, getPriorityInternal, getWarningCallback, setWarningCallbackpublic MemoryFontSource(byte[] fontData)
Examples:
Shows how to use a byte array with data from a font file as a font source.
byte[] fontBytes = DocumentHelper.getBytesFromStream(new FileInputStream(getMyDir() + "Alte DIN 1451 Mittelschrift.ttf"));
MemoryFontSource memoryFontSource = new MemoryFontSource(fontBytes, 0);
Document doc = new Document();
doc.setFontSettings(new FontSettings());
doc.getFontSettings().setFontsSources(new FontSourceBase[]{memoryFontSource});
Assert.assertEquals(FontSourceType.MEMORY_FONT, memoryFontSource.getType());
Assert.assertEquals(0, memoryFontSource.getPriority());
fontData - Binary font data.public MemoryFontSource(byte[] fontData,
int priority)
Examples:
Shows how to use a byte array with data from a font file as a font source.
byte[] fontBytes = DocumentHelper.getBytesFromStream(new FileInputStream(getMyDir() + "Alte DIN 1451 Mittelschrift.ttf"));
MemoryFontSource memoryFontSource = new MemoryFontSource(fontBytes, 0);
Document doc = new Document();
doc.setFontSettings(new FontSettings());
doc.getFontSettings().setFontsSources(new FontSourceBase[]{memoryFontSource});
Assert.assertEquals(FontSourceType.MEMORY_FONT, memoryFontSource.getType());
Assert.assertEquals(0, memoryFontSource.getPriority());
fontData - Binary font data.priority - Font source priority. See the FontSourceBase.getPriority() property description for more information.public MemoryFontSource(byte[] fontData,
int priority,
java.lang.String cacheKey)
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");
}
}
fontData - Binary font data.priority - Font source priority. See the FontSourceBase.getPriority() property description for more information.cacheKey - The key of this source in the cache. See getCacheKey() property description for more information.public byte[] getFontData()
Examples:
Shows how to use a byte array with data from a font file as a font source.
byte[] fontBytes = DocumentHelper.getBytesFromStream(new FileInputStream(getMyDir() + "Alte DIN 1451 Mittelschrift.ttf"));
MemoryFontSource memoryFontSource = new MemoryFontSource(fontBytes, 0);
Document doc = new Document();
doc.setFontSettings(new FontSettings());
doc.getFontSettings().setFontsSources(new FontSourceBase[]{memoryFontSource});
Assert.assertEquals(FontSourceType.MEMORY_FONT, memoryFontSource.getType());
Assert.assertEquals(0, memoryFontSource.getPriority());
byte[] value.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()
Examples:
Shows how to use a byte array with data from a font file as a font source.
byte[] fontBytes = DocumentHelper.getBytesFromStream(new FileInputStream(getMyDir() + "Alte DIN 1451 Mittelschrift.ttf"));
MemoryFontSource memoryFontSource = new MemoryFontSource(fontBytes, 0);
Document doc = new Document();
doc.setFontSettings(new FontSettings());
doc.getFontSettings().setFontsSources(new FontSourceBase[]{memoryFontSource});
Assert.assertEquals(FontSourceType.MEMORY_FONT, memoryFontSource.getType());
Assert.assertEquals(0, memoryFontSource.getPriority());
getType in class FontSourceBaseFontSourceType constants.public java.lang.Iterable getFontDataInternal()
getFontDataInternal in class FontSourceBase