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 1805173 2017-08-16 10:50:04Z ssteiner $ */
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;
30 import java.util.Hashtable;
31 import java.util.Vector;
32 
33 /**
34  * <p>RTF font table.</p>
35  *
36  * <p>This work was authored by Andreas Putz (a.putz@skynamics.com).</p>
37  */
38 public final class RtfFontManager {
39     //////////////////////////////////////////////////
40     // @@ Members
41     //////////////////////////////////////////////////
42 
43     /** Singelton instance */
44     private static RtfFontManager instance = new RtfFontManager();
45 
46     /** Index table for the fonts */
47     private Hashtable fontIndex;
48     /** Used fonts to this vector */
49     private Vector fontTable;
50 
51 
52     //////////////////////////////////////////////////
53     // @@ Construction
54     //////////////////////////////////////////////////
55 
56     /**
57      * Constructor.
58      */
RtfFontManager()59     private RtfFontManager() {
60         fontTable = new Vector();
61         fontIndex = new Hashtable();
62 
63         init();
64     }
65 
66     /**
67      * Singelton.
68      *
69      * @return The instance of RtfFontManager
70      */
getInstance()71     public static RtfFontManager getInstance() {
72         return instance;
73     }
74 
75 
76     //////////////////////////////////////////////////
77     // @@ Initializing
78     //////////////////////////////////////////////////
79 
80     /**
81      * Initialize the font table.
82      */
init()83     private void init() {
84 
85 //        getFontNumber ("Helvetica");
86         //Chanded by R.Marra default font Arial
87         getFontNumber("Arial");
88         getFontNumber("Symbol"); // used by RtfListItem.java
89         getFontNumber("Times New Roman");
90 
91 /*
92         {\\f0\\fswiss Helv;}
93 
94         // f1 is used by RtfList and RtfListItem for bullets
95 
96         {\\f1\\froman\\fcharset2 Symbol;}
97         {\\f2\\froman\\fprq2 Times New Roman;}
98         {\\f3\\froman Times New Roman;}
99 */
100     }
101 
102 
103     //////////////////////////////////////////////////
104     // @@ Public methods
105     //////////////////////////////////////////////////
106 
107 
108     /**
109      * Gets the number of font in the font table
110      *
111      * @param family Font family name ('Helvetica')
112      *
113      * @return The number of the font in the table
114      */
getFontNumber(String family)115     public int getFontNumber(String family) {
116 
117         Object o = fontIndex.get(getFontKey(family));
118         int retVal;
119 
120         if (o == null) {
121             addFont(family);
122 
123             retVal = fontTable.size() - 1;
124         } else {
125             retVal = (Integer) o;
126         }
127 
128         return retVal;
129     }
130 
131     /**
132      * Writes the font table in the header.
133      *
134      * @param header The header container to write in
135      *
136      * @throws IOException On error
137      */
writeFonts(RtfHeader header)138     public void writeFonts(RtfHeader header) throws IOException {
139         if (fontTable == null || fontTable.size() == 0) {
140             return;
141         }
142 
143         header.newLine();
144         header.writeGroupMark(true);
145         header.writeControlWord("fonttbl");
146 
147         int len = fontTable.size();
148 
149         for (int i = 0; i < len; i++) {
150             header.writeGroupMark(true);
151             header.newLine();
152             header.write("\\f" + i);
153             header.write(" " + (String) fontTable.elementAt(i));
154             header.write(";");
155             header.writeGroupMark(false);
156         }
157 
158         header.newLine();
159         header.writeGroupMark(false);
160     }
161 
162 
163     //////////////////////////////////////////////////
164     // @@ Private methods
165     //////////////////////////////////////////////////
166 
getFontKey(String family)167     private String getFontKey(String family) {
168         return family.toLowerCase();
169     }
170 
171     /**
172      * Adds a font to the table.
173      *
174      * @param family Identifier of font
175      */
addFont(String family)176     private void addFont(String family) {
177         fontIndex.put(getFontKey(family), fontTable.size());
178         fontTable.addElement(family);
179     }
180 }
181