001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: RtfFontManager.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.render.rtf.rtflib.rtfdoc;
021:
022: /*
023: * This file is part of the RTF library of the FOP project, which was originally
024: * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
025: * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
026: * the FOP project.
027: */
028:
029: import java.io.IOException;
030: import java.util.Hashtable;
031: import java.util.Vector;
032:
033: /**
034: * RTF font table
035: * @author Andreas Putz a.putz@skynamics.com
036: */
037: public final class RtfFontManager {
038: //////////////////////////////////////////////////
039: // @@ Members
040: //////////////////////////////////////////////////
041:
042: /** Singelton instance */
043: private static RtfFontManager instance = null;
044:
045: /** Index table for the fonts */
046: private Hashtable fontIndex = null;
047: /** Used fonts to this vector */
048: private Vector fontTable = null;
049:
050: //////////////////////////////////////////////////
051: // @@ Construction
052: //////////////////////////////////////////////////
053:
054: /**
055: * Constructor.
056: */
057: private RtfFontManager() {
058: fontTable = new Vector();
059: fontIndex = new Hashtable();
060:
061: init();
062: }
063:
064: /**
065: * Singelton.
066: *
067: * @return The instance of RtfFontManager
068: */
069: public static RtfFontManager getInstance() {
070: if (instance == null) {
071: instance = new RtfFontManager();
072: }
073:
074: return instance;
075: }
076:
077: //////////////////////////////////////////////////
078: // @@ Initializing
079: //////////////////////////////////////////////////
080:
081: /**
082: * Initialize the font table.
083: */
084: private void init() {
085:
086: // getFontNumber ("Helvetica");
087: //Chanded by R.Marra default font Arial
088: getFontNumber("Arial");
089: getFontNumber("Symbol"); // used by RtfListItem.java
090: getFontNumber("Times New Roman");
091:
092: /*
093: {\\f0\\fswiss Helv;}
094:
095: // f1 is used by RtfList and RtfListItem for bullets
096:
097: {\\f1\\froman\\fcharset2 Symbol;}
098: {\\f2\\froman\\fprq2 Times New Roman;}
099: {\\f3\\froman Times New Roman;}
100: */
101: }
102:
103: //////////////////////////////////////////////////
104: // @@ Public methods
105: //////////////////////////////////////////////////
106:
107: /**
108: * Gets the number of font in the font table
109: *
110: * @param family Font family name ('Helvetica')
111: *
112: * @return The number of the font in the table
113: */
114: public int getFontNumber(String family) {
115:
116: Object o = fontIndex.get(getFontKey(family));
117: int retVal;
118:
119: if (o == null) {
120: addFont(family);
121:
122: retVal = fontTable.size() - 1;
123: } else {
124: retVal = ((Integer) o).intValue();
125: }
126:
127: return retVal;
128: }
129:
130: /**
131: * Writes the font table in the header.
132: *
133: * @param header The header container to write in
134: *
135: * @throws IOException On error
136: */
137: public void writeFonts(RtfHeader header) throws IOException {
138: if (fontTable == null || fontTable.size() == 0) {
139: return;
140: }
141:
142: header.newLine();
143: header.writeGroupMark(true);
144: header.writeControlWord("fonttbl;");
145:
146: int len = fontTable.size();
147:
148: for (int i = 0; i < len; i++) {
149: header.writeGroupMark(true);
150: header.newLine();
151: header.write("\\f" + i);
152: header.write(" " + (String) fontTable.elementAt(i));
153: header.writeGroupMark(false);
154: }
155:
156: header.newLine();
157: header.writeGroupMark(false);
158: }
159:
160: //////////////////////////////////////////////////
161: // @@ Private methods
162: //////////////////////////////////////////////////
163:
164: private String getFontKey(String family) {
165: return family.toLowerCase();
166: }
167:
168: /**
169: * Adds a font to the table.
170: *
171: * @param family Identifier of font
172: */
173: private void addFont(String family) {
174: fontIndex
175: .put(getFontKey(family), new Integer(fontTable.size()));
176: fontTable.addElement(family);
177: }
178: }
|