View Javadoc

1   // Copyright 2001-2005 freehep
2   package org.freehep.graphicsio.font;
3   
4   import java.awt.Font;
5   import java.awt.font.FontRenderContext;
6   import java.awt.geom.Rectangle2D;
7   import java.io.IOException;
8   
9   import org.freehep.graphics2d.font.CharTable;
10  
11  /**
12   * Instances of this class write the information into documents (ps or pdf) that
13   * is necessary in order to include or embed fonts. In order to guarantee a
14   * time-invariant interface the main methods to implement by subclasses
15   * <tt>includeFont</tt> takes no arguments. All necessary data should be
16   * available by getter methods which can easily be added. <br>
17   * The abstract methods are called in the following order:
18   * <ul>
19   * <li><tt>openIncludeFont</tt>
20   * <li><tt>writeEncoding</tt>
21   * <li><tt>closeIncludeFont</tt>
22   * </ul>
23   * 
24   * @author Simon Fischer
25   * @version $Id: FontIncluder.java 8584 2006-08-10 23:06:37Z duns $
26   */
27  public abstract class FontIncluder {
28  
29      public static final double FONT_SIZE = 1000;
30  
31      // -------------------- abstract methods --------------------
32  
33      /**
34       * Writes the given information about the font into the file. When this
35       * method is called all <tt>getXXX()</tt> are guaranteed to return
36       * reasonable values.
37       */
38      protected abstract void openIncludeFont() throws IOException;
39  
40      /** Writes the encoding table to the file. */
41      protected abstract void writeEncoding(CharTable charTable)
42              throws IOException;
43  
44      /** Does nothing, but can be implemented by subclasses if necessary. */
45      protected void closeIncludeFont() throws IOException {
46      }
47  
48      // -----------------------------------------------------------
49  
50      private FontRenderContext context;
51  
52      private Rectangle2D fontBBox;
53  
54      private Font font;
55  
56      private String fontName;
57  
58      private CharTable charTable;
59  
60      private char[] unicode;
61  
62      private String[] charName;
63  
64      private int noDefinedChars;
65  
66      public FontIncluder(FontRenderContext context) {
67          this.context = context;
68          this.noDefinedChars = -1;
69      }
70  
71      // -----------------------------------------------------------
72  
73      protected FontRenderContext getContext() {
74          return context;
75      }
76  
77      protected String getFontName() {
78          return fontName;
79      }
80  
81      protected Font getFont() {
82          return font;
83      }
84  
85      protected CharTable getEncodingTable() {
86          return charTable;
87      }
88  
89      protected Rectangle2D getFontBBox() {
90          return fontBBox;
91      }
92  
93      protected String getCharName(int i) {
94          return charName[i];
95      }
96  
97      protected char getUnicode(int i) {
98          return unicode[i];
99      }
100 
101     protected char[] getUnicode() {
102         return unicode;
103     }
104 
105     protected int getNODefinedChars() {
106         return noDefinedChars;
107     }
108 
109     // -----------------------------------------------------------
110 
111     /**
112      * Embed this font to the file.
113      * 
114      * @param font The font to include
115      * @param name The name under which this font is addressed within the
116      *        document (can be retrieved by <tt>getFontName()</tt>)
117      */
118     public void includeFont(Font font, CharTable charTable, String name)
119             throws IOException {
120 
121         unicode = null;
122         charName = null;
123 
124         this.font = font;
125         this.charTable = charTable;
126         this.fontName = name;
127 
128         // figure out the maximum bounding box for all characters
129         this.fontBBox = font.getMaxCharBounds(context);
130 
131         // figure out the unicodes and character names and
132         // create a glyph vector containing the 256 glyphs of the font
133         this.noDefinedChars = 0;
134         this.unicode = new char[256];
135         this.charName = new String[256];
136         for (int i = 0; i < unicode.length; i++) {
137             charName[i] = charTable.toName(i);
138             if (charName[i] != null) {
139                 unicode[i] = charTable.toUnicode(charName[i]);
140                 noDefinedChars++;
141             } else {
142                 unicode[i] = 0;
143             }
144         }
145 
146         openIncludeFont();
147         writeEncoding(charTable);
148         closeIncludeFont();
149     }
150 
151     protected double getUndefinedWidth() {
152         return FONT_SIZE;
153     }
154 }