1 19 20 package jxl.biff; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 26 import common.Assert; 27 28 import jxl.write.biff.File; 29 30 33 public class Fonts 34 { 35 38 private ArrayList fonts; 39 40 43 private static final int numDefaultFonts = 4; 44 45 48 public Fonts() 49 { 50 fonts = new ArrayList (); 51 } 52 53 61 public void addFont(FontRecord f) 62 { 63 if (!f.isInitialized()) 64 { 65 int pos = fonts.size(); 66 67 if (pos >= 4) 69 { 70 pos++; 71 } 72 73 f.initialize(pos); 74 fonts.add(f); 75 } 76 } 77 78 85 public FontRecord getFont(int index) 86 { 87 if (index > 4) 89 { 90 index--; 91 } 92 93 return (FontRecord) fonts.get(index); 94 } 95 96 102 public void write(File outputFile) throws IOException 103 { 104 Iterator 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 119 IndexMapping rationalize() 120 { 121 IndexMapping mapping = new IndexMapping(fonts.size() + 1); 122 124 ArrayList newfonts = new ArrayList (); 125 FontRecord fr = null; 126 int numremoved = 0; 127 128 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 Iterator 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 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 newfonts.add(fr); 163 int newindex = fr.getFontIndex() - numremoved; 164 Assert.verify(newindex > 4); 165 mapping.setMapping(fr.getFontIndex(), newindex); 166 } 167 } 168 169 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 |