001    /*
002     * Copyright 2007 - 2007 JEuclid, http://jeuclid.sf.net
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    /* $Id: FontFactory.java,v e6bd6c2d9bf4 2008/11/28 15:02:26 maxberger $ */
018    
019    package net.sourceforge.jeuclid.font;
020    
021    import java.awt.Font;
022    import java.awt.FontFormatException;
023    import java.io.File;
024    import java.io.IOException;
025    import java.io.InputStream;
026    import java.util.List;
027    import java.util.Set;
028    
029    /**
030     * Abstract factory to be used to create instances of java.awt.Font. The
031     * rationale behind this approach is that out-of-the box there is no way in
032     * java platform to load and <b>register</b> and internal font. In other
033     * words, Font.createFont and Font's constructor are not aware of each other.
034     * <p>
035     * The abstract FontFactory just provides a centralized access and extension
036     * point, delegating the actual functionality to subclasses.
037     * <p>
038     * How a concrete subclass of FontFactory is identified is subject to change
039     * in the future versions.
040     * 
041     * @version $Revision: e6bd6c2d9bf4 $
042     */
043    public abstract class FontFactory {
044    
045        /** Name for the default (sans serif) font. */
046        public static final String SANSSERIF = "sansserif";
047    
048        private static FontFactory instance = new DefaultFontFactory();
049    
050        /**
051         * Return an instance of the currently configured concrete FontFactory.
052         * 
053         * @return an instance of FontFactory
054         */
055        public static FontFactory getInstance() {
056            return FontFactory.instance;
057        }
058    
059        /**
060         * Create a font object with specified properties. Font name may refer to
061         * either 'built-in' or loaded externally and 'cached' font.
062         * 
063         * @param name
064         *            font name or font family name
065         * @param style
066         *            font style
067         * @param size
068         *            font size
069         * @return Font instance
070         * @see java.awt.Font#Font(String, int, int)
071         */
072        public abstract Font getFont(final String name, final int style,
073                final float size);
074    
075        /**
076         * Create a font object which is able to display the requested code point.
077         * Uses one of the list of preferred fonts is possible. If no matching
078         * font is found null is returned.
079         * 
080         * @param preferredFonts
081         *            List of preferred fonts
082         * @param codepoint
083         *            code point which must be displayable
084         * @param style
085         *            font style
086         * @param size
087         *            font size
088         * @return a valid Font instance or null if no font could be found.
089         */
090        public abstract Font getFont(final List<String> preferredFonts,
091                final int codepoint, final int style, final float size);
092    
093        /**
094         * Load an external font from a file and 'register' (aka 'cache') it for
095         * future use.
096         * 
097         * @param format
098         *            font format (TTF or TYPE_1 currently supported by the
099         *            platform)
100         * @param fontFile
101         *            file which contains the font
102         * @return The newly created Font instance
103         * @throws FontFormatException
104         *             if font contained in the file doesn't match the specified
105         *             format
106         * @throws IOException
107         *             in case of problem while reading the file
108         * @see java.awt.Font#createFont(int, File)
109         */
110        public abstract Font registerFont(int format, File fontFile)
111                throws IOException, FontFormatException;
112    
113        /**
114         * Load an external font from a stream and 'register' (aka 'cache') it for
115         * future use.
116         * 
117         * @param format
118         *            font format (TTF or TYPE_1 currently supported by the
119         *            platform)
120         * @param fontStream
121         *            file which contains the font
122         * @return The newly created Font instance
123         * @throws FontFormatException
124         *             if font contained in the stream doesn't match the specified
125         *             format
126         * @throws IOException
127         *             in case of problem while reading the stream
128         * @see java.awt.Font#createFont(int, InputStream)
129         */
130        public abstract Font registerFont(int format, InputStream fontStream)
131                throws IOException, FontFormatException;
132    
133        /**
134         * Retrieve a list of all fonts registered with this fontFactory.
135         * 
136         * @return A set of recognized font names
137         */
138        public abstract Set<String> listFontNames();
139    }