KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > Fonts


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.biff;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import common.Assert;
27
28 import jxl.write.biff.File;
29
30 /**
31  * A container for the list of fonts used in this workbook
32  */

33 public class Fonts
34 {
35   /**
36    * The list of fonts
37    */

38   private ArrayList JavaDoc fonts;
39
40   /**
41    * The default number of fonts
42    */

43   private static final int numDefaultFonts = 4;
44
45   /**
46    * Constructor
47    */

48   public Fonts()
49   {
50     fonts = new ArrayList JavaDoc();
51   }
52
53   /**
54    * Adds a font record to this workbook. If the FontRecord passed in has not
55    * been initialized, then its font index is determined based upon the size
56    * of the fonts list. The FontRecord's initialized method is called, and
57    * it is added to the list of fonts.
58    *
59    * @param f the font to add
60    */

61   public void addFont(FontRecord f)
62   {
63     if (!f.isInitialized())
64     {
65       int pos = fonts.size();
66
67       // Remember that the pos with index 4 is skipped
68
if (pos >= 4)
69       {
70         pos++;
71       }
72
73       f.initialize(pos);
74       fonts.add(f);
75     }
76   }
77
78   /**
79    * Used by FormattingRecord for retrieving the fonts for the
80    * hardcoded styles
81    *
82    * @param index the index of the font to return
83    * @return the font with the specified font index
84    */

85   public FontRecord getFont(int index)
86   {
87     // remember to allow for the fact that font index 4 is not used
88
if (index > 4)
89     {
90       index--;
91     }
92
93     return (FontRecord) fonts.get(index);
94   }
95
96   /**
97    * Writes out the list of fonts
98    *
99    * @param outputFile the compound file to write the data to
100    * @exception IOException
101    */

102   public void write(File outputFile) throws IOException JavaDoc
103   {
104     Iterator JavaDoc i = fonts.iterator();
105
106     FontRecord font = null;
107     while (i.hasNext())
108     {
109       font = (FontRecord) i.next();
110       outputFile.write(font);
111     }
112   }
113
114   /**
115    * Rationalizes all the fonts, removing any duplicates
116    *
117    * @return the mappings between new indexes and old ones
118    */

119   IndexMapping rationalize()
120   {
121     IndexMapping mapping = new IndexMapping(fonts.size() + 1);
122       // allow for skipping record 4
123

124     ArrayList JavaDoc newfonts = new ArrayList JavaDoc();
125     FontRecord fr = null;
126     int numremoved = 0;
127
128     // Preserve the default fonts
129
for (int i = 0; i < numDefaultFonts; i++)
130     {
131       fr = (FontRecord) fonts.get(i);
132       newfonts.add(fr);
133       mapping.setMapping(fr.getFontIndex(), fr.getFontIndex());
134     }
135
136     // Now do the rest
137
Iterator JavaDoc it = null;
138     FontRecord fr2 = null;
139     boolean duplicate = false;
140     for (int i = numDefaultFonts; i < fonts.size(); i++)
141     {
142       fr = (FontRecord) fonts.get(i);
143
144       // Compare to all the fonts currently on the list
145
duplicate = false;
146       it = newfonts.iterator();
147       while (it.hasNext() && !duplicate)
148       {
149         fr2 = (FontRecord) it.next();
150         if (fr.equals(fr2))
151         {
152           duplicate = true;
153           mapping.setMapping(fr.getFontIndex(),
154                              mapping.getNewIndex(fr2.getFontIndex()));
155           numremoved++;
156         }
157       }
158
159       if (!duplicate)
160       {
161         // Add to the new list
162
newfonts.add(fr);
163         int newindex = fr.getFontIndex() - numremoved;
164         Assert.verify(newindex > 4);
165         mapping.setMapping(fr.getFontIndex(), newindex);
166       }
167     }
168
169     // Iterate through the remaining fonts, updating all the font indices
170
it = newfonts.iterator();
171     while (it.hasNext())
172     {
173       fr = (FontRecord) it.next();
174       fr.initialize(mapping.getNewIndex(fr.getFontIndex()));
175     }
176
177     fonts = newfonts;
178
179     return mapping;
180   }
181 }
182
Popular Tags