Package org.apache.fop.fonts
Class FontCache
- java.lang.Object
-
- org.apache.fop.fonts.FontCache
-
- All Implemented Interfaces:
java.io.Serializable
public final class FontCache extends java.lang.Object implements java.io.Serializable
Fop cache (currently only used for font info caching)- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
FontCache.CachedFontFile
-
Field Summary
Fields Modifier and Type Field Description private boolean
changed
has this cache been changed since it was last read?private boolean[]
changeLock
change lockprivate static java.lang.String
DEFAULT_CACHE_FILENAME
font cache file pathprivate java.util.Map<java.lang.String,java.lang.Long>
failedFontMap
mapping of font url -> file modified date (for all fonts that have failed to load)private java.util.Map<java.lang.String,FontCache.CachedFontFile>
fontfileMap
master mapping of font url -> font info.private static java.lang.String
FOP_USER_DIR
FOP's user directory nameprivate static org.apache.commons.logging.Log
log
logging instanceprivate static long
serialVersionUID
Serialization Version UID.
-
Constructor Summary
Constructors Constructor Description FontCache()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addFont(EmbedFontInfo fontInfo, InternalResourceResolver resourceResolver)
Adds a font info to cachevoid
clear()
Clears font cacheboolean
containsFont(java.lang.String embedUrl)
is this font in the cache?boolean
containsFont(EmbedFontInfo fontInfo)
is this font info in the cache?protected static java.lang.String
getCacheKey(EmbedFontInfo fontInfo)
creates a key given a font info for the font mappingstatic java.io.File
getDefaultCacheFile(boolean forWriting)
Returns the default font cache file.private java.util.Map<java.lang.String,java.lang.Long>
getFailedFontMap()
static java.io.File
getFileFromUrls(java.lang.String[] urls)
Tries to identify a File instance from an array of URLs.FontCache.CachedFontFile
getFontFile(java.lang.String embedUrl)
Returns a font from the cache.private java.util.Map<java.lang.String,FontCache.CachedFontFile>
getFontFileMap()
EmbedFontInfo[]
getFontInfos(java.lang.String embedUrl, long lastModified)
Returns the EmbedFontInfo instances belonging to a font file.static long
getLastModified(java.net.URI uri)
Retrieve the last modified date/time of a URI.private static java.io.File
getTempDirectory()
private static java.io.File
getUserHome()
boolean
hasChanged()
cache has been updated since it was readboolean
isFailedFont(java.lang.String embedUrl, long lastModified)
has this font previously failed to load?static FontCache
load()
Deprecated.useloadFrom(File)
insteadstatic FontCache
loadFrom(java.io.File cacheFile)
Reads a font cache file and returns its contents.private void
readObject(java.io.ObjectInputStream ois)
void
registerFailedFont(java.lang.String embedUrl, long lastModified)
Registers a failed font with the cachevoid
removeFont(java.lang.String embedUrl)
removes font from cachevoid
save()
Deprecated.usesaveTo(File)
insteadvoid
saveTo(java.io.File cacheFile)
Writes the font cache to disk.private static java.io.File
toDirectory(java.lang.String path)
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
Serialization Version UID. Change this value if you want to make sure the user's cache file is purged after an update.- See Also:
- Constant Field Values
-
log
private static org.apache.commons.logging.Log log
logging instance
-
FOP_USER_DIR
private static final java.lang.String FOP_USER_DIR
FOP's user directory name- See Also:
- Constant Field Values
-
DEFAULT_CACHE_FILENAME
private static final java.lang.String DEFAULT_CACHE_FILENAME
font cache file path- See Also:
- Constant Field Values
-
changed
private transient boolean changed
has this cache been changed since it was last read?
-
changeLock
private final boolean[] changeLock
change lock
-
fontfileMap
private java.util.Map<java.lang.String,FontCache.CachedFontFile> fontfileMap
master mapping of font url -> font info. This needs to be a list, since a TTC file may contain more than 1 font.
-
failedFontMap
private java.util.Map<java.lang.String,java.lang.Long> failedFontMap
mapping of font url -> file modified date (for all fonts that have failed to load)
-
-
Method Detail
-
readObject
private void readObject(java.io.ObjectInputStream ois) throws java.lang.ClassNotFoundException, java.io.IOException
- Throws:
java.lang.ClassNotFoundException
java.io.IOException
-
getUserHome
private static java.io.File getUserHome()
-
getTempDirectory
private static java.io.File getTempDirectory()
-
toDirectory
private static java.io.File toDirectory(java.lang.String path)
-
getDefaultCacheFile
public static java.io.File getDefaultCacheFile(boolean forWriting)
Returns the default font cache file.- Parameters:
forWriting
- true if the user directory should be created- Returns:
- the default font cache file
-
load
public static FontCache load()
Deprecated.useloadFrom(File)
insteadReads the default font cache file and returns its contents.- Returns:
- the font cache deserialized from the file (or null if no cache file exists or if it could not be read)
-
loadFrom
public static FontCache loadFrom(java.io.File cacheFile)
Reads a font cache file and returns its contents.- Parameters:
cacheFile
- the cache file- Returns:
- the font cache deserialized from the file (or null if no cache file exists or if it could not be read)
-
save
public void save() throws FOPException
Deprecated.usesaveTo(File)
insteadWrites the font cache to disk.- Throws:
FOPException
- fop exception
-
saveTo
public void saveTo(java.io.File cacheFile) throws FOPException
Writes the font cache to disk.- Parameters:
cacheFile
- the file to write to- Throws:
FOPException
- fop exception
-
getCacheKey
protected static java.lang.String getCacheKey(EmbedFontInfo fontInfo)
creates a key given a font info for the font mapping- Parameters:
fontInfo
- font info- Returns:
- font cache key
-
hasChanged
public boolean hasChanged()
cache has been updated since it was read- Returns:
- if this cache has changed
-
containsFont
public boolean containsFont(java.lang.String embedUrl)
is this font in the cache?- Parameters:
embedUrl
- font info- Returns:
- boolean
-
containsFont
public boolean containsFont(EmbedFontInfo fontInfo)
is this font info in the cache?- Parameters:
fontInfo
- font info- Returns:
- font
-
getFileFromUrls
public static java.io.File getFileFromUrls(java.lang.String[] urls)
Tries to identify a File instance from an array of URLs. If there's no file URL in the array, the method returns null.- Parameters:
urls
- array of possible font urls- Returns:
- file font file
-
getFontFileMap
private java.util.Map<java.lang.String,FontCache.CachedFontFile> getFontFileMap()
-
addFont
public void addFont(EmbedFontInfo fontInfo, InternalResourceResolver resourceResolver)
Adds a font info to cache- Parameters:
fontInfo
- font info
-
getFontFile
public FontCache.CachedFontFile getFontFile(java.lang.String embedUrl)
Returns a font from the cache.- Parameters:
embedUrl
- font info- Returns:
- CachedFontFile object
-
getFontInfos
public EmbedFontInfo[] getFontInfos(java.lang.String embedUrl, long lastModified)
Returns the EmbedFontInfo instances belonging to a font file. If the font file was modified since it was cached the entry is removed and null is returned.- Parameters:
embedUrl
- the font URLlastModified
- the last modified date/time of the font file- Returns:
- the EmbedFontInfo instances or null if there's no cached entry or if it is outdated
-
removeFont
public void removeFont(java.lang.String embedUrl)
removes font from cache- Parameters:
embedUrl
- embed url
-
isFailedFont
public boolean isFailedFont(java.lang.String embedUrl, long lastModified)
has this font previously failed to load?- Parameters:
embedUrl
- embed urllastModified
- last modified- Returns:
- whether this is a failed font
-
registerFailedFont
public void registerFailedFont(java.lang.String embedUrl, long lastModified)
Registers a failed font with the cache- Parameters:
embedUrl
- embed urllastModified
- time last modified
-
getFailedFontMap
private java.util.Map<java.lang.String,java.lang.Long> getFailedFontMap()
-
clear
public void clear()
Clears font cache
-
getLastModified
public static long getLastModified(java.net.URI uri)
Retrieve the last modified date/time of a URI.- Parameters:
uri
- the URI- Returns:
- the last modified date/time
-
-