KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > rtf > rtflib > rtfdoc > RtfFontManager


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: RtfFontManager.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.rtf.rtflib.rtfdoc;
21
22 /*
23  * This file is part of the RTF library of the FOP project, which was originally
24  * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
25  * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
26  * the FOP project.
27  */

28
29 import java.io.IOException JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 /**
34  * RTF font table
35  * @author Andreas Putz a.putz@skynamics.com
36  */

37 public final class RtfFontManager {
38     //////////////////////////////////////////////////
39
// @@ Members
40
//////////////////////////////////////////////////
41

42     /** Singelton instance */
43     private static RtfFontManager instance = null;
44
45     /** Index table for the fonts */
46     private Hashtable JavaDoc fontIndex = null;
47     /** Used fonts to this vector */
48     private Vector JavaDoc fontTable = null;
49
50
51     //////////////////////////////////////////////////
52
// @@ Construction
53
//////////////////////////////////////////////////
54

55     /**
56      * Constructor.
57      */

58     private RtfFontManager () {
59         fontTable = new Vector JavaDoc ();
60         fontIndex = new Hashtable JavaDoc ();
61
62         init ();
63     }
64
65     /**
66      * Singelton.
67      *
68      * @return The instance of RtfFontManager
69      */

70     public static RtfFontManager getInstance () {
71         if (instance == null) {
72             instance = new RtfFontManager ();
73         }
74
75         return instance;
76     }
77
78
79     //////////////////////////////////////////////////
80
// @@ Initializing
81
//////////////////////////////////////////////////
82

83     /**
84      * Initialize the font table.
85      */

86     private void init () {
87
88 // getFontNumber ("Helvetica");
89
//Chanded by R.Marra default font Arial
90
getFontNumber ("Arial");
91         getFontNumber ("Symbol"); // used by RtfListItem.java
92
getFontNumber ("Times New Roman");
93
94 /*
95         {\\f0\\fswiss Helv;}
96
97         // f1 is used by RtfList and RtfListItem for bullets
98
99         {\\f1\\froman\\fcharset2 Symbol;}
100         {\\f2\\froman\\fprq2 Times New Roman;}
101         {\\f3\\froman Times New Roman;}
102 */

103     }
104
105
106     //////////////////////////////////////////////////
107
// @@ Public methods
108
//////////////////////////////////////////////////
109

110
111     /**
112      * Gets the number of font in the font table
113      *
114      * @param family Font family name ('Helvetica')
115      *
116      * @return The number of the font in the table
117      */

118     public int getFontNumber (String JavaDoc family) {
119
120         Object JavaDoc o = fontIndex.get(getFontKey(family));
121         int retVal;
122
123         if (o == null) {
124             addFont (family);
125
126             retVal = fontTable.size() - 1;
127         } else {
128             retVal = ((Integer JavaDoc)o).intValue();
129         }
130
131         return retVal;
132     }
133
134     /**
135      * Writes the font table in the header.
136      *
137      * @param header The header container to write in
138      *
139      * @throws IOException On error
140      */

141     public void writeFonts (RtfHeader header) throws IOException JavaDoc {
142         if (fontTable == null || fontTable.size () == 0) {
143             return;
144         }
145
146         header.newLine();
147         header.writeGroupMark (true);
148         header.writeControlWord ("fonttbl;");
149
150         int len = fontTable.size ();
151
152         for (int i = 0; i < len; i++) {
153             header.writeGroupMark (true);
154             header.newLine();
155             header.write ("\\f" + i);
156             header.write (" " + (String JavaDoc) fontTable.elementAt (i));
157             header.writeGroupMark (false);
158         }
159
160         header.newLine();
161         header.writeGroupMark (false);
162     }
163
164
165     //////////////////////////////////////////////////
166
// @@ Private methods
167
//////////////////////////////////////////////////
168

169     private String JavaDoc getFontKey(String JavaDoc family) {
170         return family.toLowerCase();
171     }
172     
173     /**
174      * Adds a font to the table.
175      *
176      * @param family Identifier of font
177      */

178     private void addFont(String JavaDoc family) {
179         fontIndex.put(getFontKey(family), new Integer JavaDoc(fontTable.size()));
180         fontTable.addElement(family);
181     }
182 }
183
Popular Tags