FileDocCategorySizeDatePackage
FontManager.javaAPI DocAndroid 1.5 API29137Wed May 06 22:41:54 BST 2009org.apache.harmony.awt.gl.font

FontManager

public abstract class FontManager extends Object
author
Ilya S. Okomin
version
$Revision$

Fields Summary
boolean
NOT_IMP
public String[]
allFamilies
array of font families names
public static final String
DEFAULT_NAME
public static final String
DIALOG_NAME
public static final byte
FAMILY_NAME_ID
Set of constants applicable to the TrueType 'name' table.
public static final byte
FONT_NAME_ID
public static final byte
POSTSCRIPT_NAME_ID
public static final short
ENGLISH_LANGID
public static final byte
FONT_TYPE_TT
Set of constants describing font type.
public static final byte
FONT_TYPE_T1
public static final byte
FONT_TYPE_UNDEF
static final int
DIALOG
static final int
SANSSERIF
static final int
DIALOGINPUT
static final int
MONOSPACED
static final int
SERIF
public static final String
PLATFORM_FONT_NAME
FontProperty related constants.
public static final String
LOGICAL_FONT_NAME
public static final String
COMPONENT_INDEX
public static final String
STYLE_INDEX
public static final String[]
FONT_MAPPING_KEYS
public static final String
FONT_CHARACTER_ENCODING
public static final String
EXCLUSION_RANGES
public static final String
FONT_FILE_NAME
public static final String[]
LOGICAL_FONT_FAMILIES
Available logical font families names.
public static final String[]
LOGICAL_FONT_NAMES
Available logical font names.
public static final String[]
LOGICAL_FONT_FACES
Available logical font face names.
public static final String[]
STYLE_NAMES
Set of font style names. Font.getStyle() corresponds to indexes in STYLE_NAMES array.
private static final Hashtable
style_keys
Logical font styles names table where font styles names used as the key and the value is the index of this style name.
public static final String[]
OS_VALUES
Set of possible "os" property values.
public static final String[]
FP_FILE_NAMES
Set of possible font.property file names. Language, Country, Encoding, OS, Version should be replaced with the values from current configuration.
public Hashtable
fProperties
Table with all available font properties corresponding to the current system configuration.
public static final int
EMPTY_FONTS_CAPACITY
Maximum number of unreferenced font peers to keep.
Hashtable
tableLCID
Locale - Language ID hash table.
public Hashtable
fontsTable
Hash table that contains FontPeers instances.
public ReferenceQueue
queue
ReferenceQueue for HashMapReference objects to check if they were collected by garbage collector.
public static final FontManager
inst
Singleton instance
Constructors Summary
public FontManager()

    
     
        allFamilies = getAllFamilies();
        /*
         * Creating and registering shutdown hook to free resources
         * before object is destroyed.
         */
        //???AWT
        //DisposeNativeHook shutdownHook = new DisposeNativeHook();
        //Runtime.getRuntime().addShutdownHook(shutdownHook);
    
Methods Summary
public abstract java.awt.peer.FontPeercreateDefaultFont(int style, int size)
Returns new default font peer with "Default" name for the parameters specified. This method must be overridden by subclasses implementations.

param
style style of the font
param
size size of the font

private java.awt.peer.FontPeercreateFontPeer(java.lang.String name, int style, int size, int logicalIndex)
Returns instance of font peer (logical or physical) according to the specified parameters.

param
name font face name
param
style style of the font
param
size size of the font
param
logicalIndex index of the logical face name in LOGICAL_FONT_FACES array or -1 if desired font peer is not logical.

        FontPeer peer;
        if (logicalIndex != -1){
            peer = createLogicalFontPeer(name, style, size);
        }else {
            peer = createPhysicalFontPeer(name, style, size);
        }
        
        return peer;
    
public java.lang.StringcreateLogicalFace(java.lang.String family, int styleIndex)
Returns logical face name corresponding to the logical family name and style of the font.

param
family font family
param
styleIndex index of the style name from the STYLE_NAMES array

        return family + "." + STYLE_NAMES[styleIndex]; //$NON-NLS-1$
    
private java.awt.peer.FontPeercreateLogicalFontPeer(java.lang.String faceName, int style, int size)
Returns new logical font peer for the parameters specified using font properties.

param
faceName face name of the logical font
param
style style of the font
param
size font size

        String family = getFamilyFromLogicalFace(faceName);
        FontProperty[] fps = getFontProperties(family.toLowerCase() + "." + style); //$NON-NLS-1$
        if (fps != null){
            int numFonts = fps.length;
            FontPeerImpl[] physicalFonts = new FontPeerImpl[numFonts];
            for (int i = 0; i < numFonts; i++){
                FontProperty fp = fps[i];
                
                String name = fp.getName();
                int fpStyle = fp.getStyle();
                String key = name.concat(String.valueOf(fpStyle)).
                    concat(String.valueOf(size));
                
                HashMapReference hmr   = fontsTable.get(key);
                if (hmr != null) {
                    physicalFonts[i] = (FontPeerImpl)hmr.get();
                }

                if (physicalFonts[i] == null){
                    physicalFonts[i] = (FontPeerImpl)createPhysicalFontPeer(name, fpStyle, size);
                    fontsTable.put(key, new HashMapReference(key, physicalFonts[i], queue));
                }

                if (physicalFonts[i] == null){
                    physicalFonts[i] = (FontPeerImpl)getDefaultFont(style, size);
                }
            }
            return new CompositeFont(family, faceName, style, size, fps, physicalFonts); 
        }
        
        // if there is no property for this logical font - default font is to be
        // created
        FontPeerImpl peer = (FontPeerImpl)getDefaultFont(style, size);
        
        return peer;
    
public abstract java.awt.peer.FontPeercreatePhysicalFontPeer(java.lang.String name, int style, int size)
Returns new physical font peer for the parameters specified using font properties This method must be overridden by subclasses implementations.

param
faceName face name or family name of the font
param
style style of the font
param
size font size

public abstract java.lang.String[]getAllFamilies()

public abstract java.awt.Font[]getAllFonts()

public java.awt.peer.FontPeergetDefaultFont(int style, int size)
Returns default font peer class with "Default" name that is usually used when font with specified font names and style doesn't exsist on a system.

param
style style of the font
param
size size of the font

        updateFontsTable();
        
        FontPeer peer = null;
        String key = DEFAULT_NAME.concat(String.valueOf(style)).
                    concat(String.valueOf(size));
        
        HashMapReference hmr   = fontsTable.get(key);
        if (hmr != null) {
            peer = hmr.get();
        }

        if (peer == null) {
            peer = createDefaultFont(style, size);
            
            ((FontPeerImpl)peer).setFamily(DEFAULT_NAME);
            ((FontPeerImpl)peer).setPSName(DEFAULT_NAME);
            ((FontPeerImpl)peer).setFontName(DEFAULT_NAME);

            fontsTable.put(key, new HashMapReference(key, peer, queue));
        }

        return peer;
    
public intgetFaceIndex(java.lang.String faceName)
Returns index of face name from the array of face names available in this GraphicsEnvironment or -1 if no face name was found. Default return value is -1, method must be overridden by FontManager implementation.

param
faceName font face name which index is to be searched

        return -1;
    
public java.lang.StringgetFamily(int index)
Returns family with index specified from the array of family names available in this GraphicsEnvironment.

param
index index of the family in families names array

        return allFamilies[index];
    
public java.lang.StringgetFamilyFromLogicalFace(java.lang.String faceName)
Returns family name for logical face names as a parameter.

param
faceName logical font face name

        int pos = faceName.indexOf("."); //$NON-NLS-1$
        if (pos == -1){
            return faceName;
        }
            
        return faceName.substring(0, pos);
    
public intgetFamilyIndex(java.lang.String familyName)
Returns index of family name from the array of family names available in this GraphicsEnvironment or -1 if no family name was found.

param
familyName specified font family name

        for (int i=0; i<allFamilies.length; i++ ){
            if (familyName.equalsIgnoreCase(allFamilies[i])){
                return i;
            }
        }
        return -1;
    
public java.awt.peer.FontPeergetFontPeer(java.lang.String fontName, int _fontStyle, int size)
Returns platform-dependent Font peer created from the specified Font object from the table with cached FontPeers instances. Note, this method checks whether FontPeer with specified parameters exists in the table with cached FontPeers' instances. If there is no needed instance - it is created and cached.

param
fontName name of the font
param
_fontStyle style of the font
param
size font size
return
platform dependent FontPeer implementation created from the specified parameters

        updateFontsTable();
        
        FontPeer peer = null;
        String key; 
        String name;
        int fontStyle = _fontStyle;
        
        int logicalIndex = getLogicalFaceIndex(fontName);
        
        if (logicalIndex != -1){
            name = getLogicalFaceFromFont(fontStyle, logicalIndex);
            fontStyle = getStyleFromLogicalFace(name);
            key = name.concat(String.valueOf(size));
        } else {
            name = fontName;
            key = name.concat(String.valueOf(fontStyle)).
                    concat(String.valueOf(size));
        }
        
        HashMapReference hmr   = fontsTable.get(key);
        if (hmr != null) {
            peer = hmr.get();
        }

        if (peer == null) {
            peer = createFontPeer(name, fontStyle, size, logicalIndex);
            if (peer == null){
                peer = getFontPeer(DIALOG_NAME, fontStyle, size);
            }
            fontsTable.put(key, new HashMapReference(key, peer, queue));
        }

        return peer;
    
public FontProperty[]getFontProperties(java.lang.String fpName)
Returns an array of FontProperties from the properties file with the specified property name "logical face.style". E.g. "dialog.2" corresponds to the font family Dialog with bold style.

param
fpName key of the font properties in the properties set

        Vector<FontProperty> props = fProperties.get(fpName);
        
        if (props == null){
            return null;
        }

        int size =  props.size();
        
        if (size == 0){
            return null;
        }

        FontProperty[] fps = new FontProperty[size];
        for (int i=0; i < fps.length; i++){
            fps[i] = props.elementAt(i);
        }
        return fps;
    
public static java.io.FilegetFontPropertyFile()
Returns File object with font properties. It's name obtained using current system configuration properties and locale settings. If no appropriate file is found method returns null.

        File file = null;

        String javaHome = System.getProperty("java.home"); //$NON-NLS-1$
        Locale l = Locale.getDefault();
        String language = l.getLanguage();
        String country = l.getCountry();
        String fileEncoding = System.getProperty("file.encoding"); //$NON-NLS-1$

        String os = System.getProperty("os.name"); //$NON-NLS-1$

        int i = 0;

        // OS names from system properties don't match
        // OS identifiers used in font.property files
        for (; i < OS_VALUES.length; i++){
            if (os.endsWith(OS_VALUES[i])){
                os = OS_VALUES[i];
                break;
            }
        }

        if (i == OS_VALUES.length){
            os = null;
        }

        String version = System.getProperty("os.version"); //$NON-NLS-1$
        String pathname;

        for (i = 0; i < FP_FILE_NAMES.length; i++){
            pathname = FP_FILE_NAMES[i];
            if (os != null){
                pathname = pathname.replaceFirst("OS", os); //$NON-NLS-1$
            }

            pathname = javaHome + pathname;

            pathname = pathname.replaceAll("Language", language). //$NON-NLS-1$
                                replaceAll("Country", country). //$NON-NLS-1$
                                replaceAll("Encoding", fileEncoding). //$NON-NLS-1$
                                replaceAll("Version", version); //$NON-NLS-1$

            file = new File(pathname);

            if (file.exists()){
                break;
            }
        }

        return file.exists() ? file : null;
    
public static org.apache.harmony.awt.gl.font.FontManagergetInstance()
Gets singleton instance of FontManager

return
instance of FontManager implementation


                    
        
        return inst;
    
public java.lang.ShortgetLCID(java.util.Locale l)
Return language Id from LCID hash corresponding to the specified locale

param
l specified locale

        if (this.tableLCID.size() == 0){
            initLCIDTable();
        }

        return tableLCID.get(l.toString());
    
public java.lang.StringgetLogicalFaceFromFont(int fontStyle, int logicalIndex)
Returns face name of the logical font, which is the result of specified font style and face style union.

param
fontStyle specified style of the font
param
logicalIndex index of the specified face from the LOGICAL_FONT_FACES array
return
resulting face name

        int style = 0;
        String name = LOGICAL_FONT_FACES[logicalIndex];
        int pos = name.indexOf("."); //$NON-NLS-1$
        
        if (pos == -1){
            return createLogicalFace(name, fontStyle);
        }
        
        String styleName = name.substring(pos+1);
        name = name.substring(0, pos);
        
        // appending font style to the face style
        style = fontStyle | getLogicalStyle(styleName);
        
        return createLogicalFace(name, style);
    
public static intgetLogicalFaceIndex(java.lang.String fontName)
Returns index of the font name in array of font names or -1 if this font is not logical.

param
fontName specified font name

        for (int i=0; i<LOGICAL_FONT_NAMES.length; i++ ){
            if (LOGICAL_FONT_NAMES[i].equalsIgnoreCase(fontName)){
                return i;
            }
        }
        return -1;
    
public static intgetLogicalStyle(java.lang.String lName)
Return font style from the logical style name.

param
lName style name of the logical face


              
     
        for (int i = 0; i < STYLE_NAMES.length; i++){
            style_keys.put(STYLE_NAMES[i], Integer.valueOf(i));
        }
    
        Integer value = style_keys.get(lName);
        return value != null ? value.intValue(): -1;
    
public static java.util.PropertiesgetProperties(java.io.File file)
Returns Properties from the properties file or null if there is an error with FileInputStream processing.

param
file File object containing properties

        Properties props = null;
        FileInputStream fis = null;
        try{
            fis = new FileInputStream(file);
            props = new Properties();
            props.load(fis);
        } catch (Exception e){
            System.out.println(e);
        }
        return props;
    
public intgetStyleFromLogicalFace(java.lang.String name)
Function returns style value from logical face name.

param
name face name
return
font style

        int style;
        int pos = name.indexOf("."); //$NON-NLS-1$
        
        if (pos == -1){
            return Font.PLAIN;
        }
        
        String styleName = name.substring(pos+1);
        
        style = getLogicalStyle(styleName);
        
        return style;
    
public java.io.FilegetTempFontFile()
Returns File object, created in a directory according to the System, where JVM is being ran. In Linux case we use ".fonts" directory (for fontconfig purpose), where font file from the stream will be stored, hence in LinuxFontManager this method is overridden. In Windows case we use Windows temp directory (default implementation)

        //???AWT
        /*
        File fontFile = File.createTempFile("jFont", ".ttf"); //$NON-NLS-1$ //$NON-NLS-2$
        fontFile.deleteOnExit();

        return fontFile;
         */
        if(NOT_IMP)
            throw new NotImplementedException("getTempFontFile not Implemented");
        return null;
    
public abstract voidinitLCIDTable()
Platform-dependent LCID table init.

public booleanisFamilyExist(java.lang.String familyName)
Returns true if specified family name is available in this GraphicsEnvironment.

param
familyName the specified font family name

        return (getFamilyIndex(familyName) != -1);
    
public static int[]parseIntervals(java.lang.String exclusionString)
Returns an array of integer range values if the parameter exclusionString has format: Range Range [, exclusionString] Range: Char-Char Char: HexDigit HexDigit HexDigit HexDigit Method returns null if the specified string is null.

param
exclusionString string parameter in specified format

        int[] results = null;

        if (exclusionString == null){
            return null;
        }

        String[] intervals = exclusionString.split(","); //$NON-NLS-1$

        if (intervals != null){
            int num = intervals.length;
            if (num > 0){
                results = new int[intervals.length << 1];
                for (int i = 0; i < intervals.length; i++){
                    String ranges[] = intervals[i].split("-"); //$NON-NLS-1$
                    results[i*2] = Integer.parseInt(ranges[0], 16);
                    results[i*2+1] = Integer.parseInt(ranges[1], 16);

                }
            }
        }
        return results;
    
private voidupdateFontsTable()
Removes keys from the Hashtable with font peers which corresponding HashMapReference objects were garbage collected.

        HashMapReference r;
        //???AWT
        //while ((r = (HashMapReference)queue.poll()) != null) {
        //    fontsTable.remove(r.getKey());
        //}