View Javadoc

1   /*
2    * Copyright 2007 - 2007 JEuclid, http://jeuclid.sf.net
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: FontFactory.java,v e6bd6c2d9bf4 2008/11/28 15:02:26 maxberger $ */
18  
19  package net.sourceforge.jeuclid.font;
20  
21  import java.awt.Font;
22  import java.awt.FontFormatException;
23  import java.io.File;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.List;
27  import java.util.Set;
28  
29  /**
30   * Abstract factory to be used to create instances of java.awt.Font. The
31   * rationale behind this approach is that out-of-the box there is no way in
32   * java platform to load and <b>register</b> and internal font. In other
33   * words, Font.createFont and Font's constructor are not aware of each other.
34   * <p>
35   * The abstract FontFactory just provides a centralized access and extension
36   * point, delegating the actual functionality to subclasses.
37   * <p>
38   * How a concrete subclass of FontFactory is identified is subject to change
39   * in the future versions.
40   * 
41   * @version $Revision: e6bd6c2d9bf4 $
42   */
43  public abstract class FontFactory {
44  
45      /** Name for the default (sans serif) font. */
46      public static final String SANSSERIF = "sansserif";
47  
48      private static FontFactory instance = new DefaultFontFactory();
49  
50      /**
51       * Return an instance of the currently configured concrete FontFactory.
52       * 
53       * @return an instance of FontFactory
54       */
55      public static FontFactory getInstance() {
56          return FontFactory.instance;
57      }
58  
59      /**
60       * Create a font object with specified properties. Font name may refer to
61       * either 'built-in' or loaded externally and 'cached' font.
62       * 
63       * @param name
64       *            font name or font family name
65       * @param style
66       *            font style
67       * @param size
68       *            font size
69       * @return Font instance
70       * @see java.awt.Font#Font(String, int, int)
71       */
72      public abstract Font getFont(final String name, final int style,
73              final float size);
74  
75      /**
76       * Create a font object which is able to display the requested code point.
77       * Uses one of the list of preferred fonts is possible. If no matching
78       * font is found null is returned.
79       * 
80       * @param preferredFonts
81       *            List of preferred fonts
82       * @param codepoint
83       *            code point which must be displayable
84       * @param style
85       *            font style
86       * @param size
87       *            font size
88       * @return a valid Font instance or null if no font could be found.
89       */
90      public abstract Font getFont(final List<String> preferredFonts,
91              final int codepoint, final int style, final float size);
92  
93      /**
94       * Load an external font from a file and 'register' (aka 'cache') it for
95       * future use.
96       * 
97       * @param format
98       *            font format (TTF or TYPE_1 currently supported by the
99       *            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 }