IndexMapping rationalize() {
IndexMapping mapping = new IndexMapping(fonts.size() + 1);
// allow for skipping record 4
ArrayList newfonts = new ArrayList();
FontRecord fr = null;
int numremoved = 0;
// Preserve the default fonts
for (int i = 0; i < numDefaultFonts; i++)
{
fr = (FontRecord) fonts.get(i);
newfonts.add(fr);
mapping.setMapping(fr.getFontIndex(), fr.getFontIndex());
}
// Now do the rest
Iterator it = null;
FontRecord fr2 = null;
boolean duplicate = false;
for (int i = numDefaultFonts; i < fonts.size(); i++)
{
fr = (FontRecord) fonts.get(i);
// Compare to all the fonts currently on the list
duplicate = false;
it = newfonts.iterator();
while (it.hasNext() && !duplicate)
{
fr2 = (FontRecord) it.next();
if (fr.equals(fr2))
{
duplicate = true;
mapping.setMapping(fr.getFontIndex(),
mapping.getNewIndex(fr2.getFontIndex()));
numremoved++;
}
}
if (!duplicate)
{
// Add to the new list
newfonts.add(fr);
int newindex = fr.getFontIndex() - numremoved;
Assert.verify(newindex > 4);
mapping.setMapping(fr.getFontIndex(), newindex);
}
}
// Iterate through the remaining fonts, updating all the font indices
it = newfonts.iterator();
while (it.hasNext())
{
fr = (FontRecord) it.next();
fr.initialize(mapping.getNewIndex(fr.getFontIndex()));
}
fonts = newfonts;
return mapping;
}
Rationalizes all the fonts, removing any duplicates |