/// <summary> Rationalizes the display formats. Duplicate /// formats are removed and the format indices of the cells /// adjusted accordingly. It is invoked immediately prior to writing /// writing out the sheet /// </summary> /// <returns> the index mapping between the old display formats and the /// rationalized ones /// </returns> public virtual IndexMapping rationalizeDisplayFormats() { ArrayList newformats = new ArrayList(); int numremoved = 0; IndexMapping mapping = new IndexMapping(nextCustomIndexNumber); // Iterate through the old list // Iterator i = formatsList.iterator(); // DisplayFormat df = null; // DisplayFormat df2 = null; bool duplicate = false; foreach (DisplayFormat df in formatsList) { Assert.verify(!df.isBuiltIn()); // Compare against formats already on the list duplicate = false; foreach (DisplayFormat df2 in newformats) { if (duplicate) { break; } if (df2.Equals(df)) { duplicate = true; mapping.setMapping(df.FormatIndex, mapping.getNewIndex(df2.FormatIndex)); numremoved++; } } // If this format is not a duplicate then add it to the new list if (!duplicate) { newformats.Add(df); int indexnum = df.FormatIndex - numremoved; if (indexnum > maxFormatRecordsIndex) { logger.warn("Too many number formats - using default format."); indexnum = 0; // the default number format index } mapping.setMapping(df.FormatIndex, df.FormatIndex - numremoved); } } // Set the new list formatsList = newformats; // Update the index codes for the remaining formats foreach (DisplayFormat df in formatsList) { df.initialize(mapping.getNewIndex(df.FormatIndex)); } return(mapping); }
/// <summary> Changes the appropriate indexes during the rationalization process</summary> /// <param name="xfMapping">the xf index re-mappings /// </param> internal virtual void rationalize(IndexMapping xfMapping) { xfIndex = xfMapping.getNewIndex(xfIndex); if (xfFormatType == cell) { parentFormat = xfMapping.getNewIndex(parentFormat); } }
// [TODO-NExcel_Next] // /** // * Writes out the list of fonts // * // * @param outputFile the compound file to write the data to // * @exception IOException // */ // public void write(File outputFile) throws IOException // { // Iterator i = fonts.iterator(); // // FontRecord font = null; // while (i.hasNext()) // { // font = (FontRecord) i.next(); // outputFile.write(font); // } // } // /// <summary> Rationalizes all the fonts, removing any duplicates /// /// </summary> /// <returns> the mappings between new indexes and old ones /// </returns> internal virtual IndexMapping rationalize() { IndexMapping mapping = new IndexMapping(fonts.Count + 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[i]; newfonts.Add(fr); mapping.setMapping(fr.FontIndex, fr.FontIndex); } // Now do the rest // Iterator it = null; // FontRecord fr2 = null; bool duplicate = false; for (int i = numDefaultFonts; i < fonts.Count; i++) { fr = (FontRecord)fonts[i]; // Compare to all the fonts currently on the list duplicate = false; foreach (FontRecord fr2 in newfonts) { if (duplicate) { break; } if (fr.Equals(fr2)) { duplicate = true; mapping.setMapping(fr.FontIndex, mapping.getNewIndex(fr2.FontIndex)); numremoved++; } } if (!duplicate) { // Add to the new list newfonts.Add(fr); int newindex = fr.FontIndex - numremoved; Assert.verify(newindex > 4); mapping.setMapping(fr.FontIndex, newindex); } } // Iterate through the remaining fonts, updating all the font indices foreach (FontRecord fr3 in newfonts) { fr3.initialize(mapping.getNewIndex(fr3.FontIndex)); } fonts = newfonts; return(mapping); }
/// <summary> Rationalizes the cell formats. Duplicate /// formats are removed and the format indexed of the cells /// adjusted accordingly /// /// </summary> /// <param name="fontMapping">the font mapping index numbers /// </param> /// <param name="formatMapping">the format mapping index numbers /// </param> /// <returns> the list of new font index number /// </returns> public virtual IndexMapping rationalize(IndexMapping fontMapping, IndexMapping formatMapping) { // Update the index codes for the XF records using the format // mapping and the font mapping // at the same time foreach (XFRecord xfr in xfRecords) { if (xfr.FormatRecord >= customFormatStartIndex) { xfr.FormatIndex = formatMapping.getNewIndex(xfr.FormatRecord); } xfr.FontIndex = fontMapping.getNewIndex(xfr.FontIndex); } ArrayList newrecords = new ArrayList(minXFRecords); IndexMapping mapping = new IndexMapping(xfRecords.Count); int numremoved = 0; // Copy across the fundamental styles for (int i = 0; i < minXFRecords; i++) { newrecords.Add(xfRecords[i]); mapping.setMapping(i, i); } // Iterate through the old list for (int i = minXFRecords; i < xfRecords.Count; i++) { XFRecord xf = (XFRecord)xfRecords[i]; // Compare against formats already on the list bool duplicate = false; foreach (XFRecord xf2 in newrecords) { if (duplicate) { break; } if (xf2.Equals(xf)) { duplicate = true; mapping.setMapping(i, mapping.getNewIndex(xf2.getXFIndex())); numremoved++; } } // If this format is not a duplicate then add it to the new list if (!duplicate) { newrecords.Add(xf); mapping.setMapping(i, i - numremoved); } } // It is sufficient to merely change the xf index field on all XFRecords // In this case, CellValues which refer to defunct format records // will nevertheless be written out with the correct index number foreach (XFRecord xf in xfRecords) { xf.rationalize(mapping); } // Set the new list xfRecords = newrecords; return(mapping); }